summaryrefslogtreecommitdiff
path: root/util/cbfstool/partitioned_file.c
diff options
context:
space:
mode:
authorVadim Bendebury <vbendeb@chromium.org>2015-12-09 09:39:31 -0800
committerPatrick Georgi <pgeorgi@google.com>2016-01-19 18:14:44 +0100
commit755994689630fbec38c1a3e5d61aef69fc7ff8c5 (patch)
treed719c357c2071f51862665398b47f0a1950c6c79 /util/cbfstool/partitioned_file.c
parent6313bc7731fccc1a80762093b38352aba75e694d (diff)
cbfstool: accept read-only files when possible
cbfstool tries opening the input file for write access even if the command does not require modifying the file. Let's not request write access unless it is necessary, this way one can examine write protected files without sudo. BRANCH=none BUG=none TEST=running cbfstool /build/<board>/firmware/image.bin print in chroot does not require root access any more. Change-Id: Ic4e4cc389b160da190e44a676808f5c4e6625567 Signed-off-by: Patrick Georgi <pgeorgi@chromium.org> Original-Commit-Id: ef6a8e25d9e257d7de4cc6b94e510234fe20a56d Original-Change-Id: I871f32f0662221ffbdb13bf0482cb285ec184d07 Original-Signed-off-by: Vadim Bendebury <vbendeb@chromium.org> Original-Reviewed-on: https://chromium-review.googlesource.com/317300 Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org> Reviewed-on: https://review.coreboot.org/12931 Tested-by: build bot (Jenkins) Reviewed-by: Martin Roth <martinroth@google.com>
Diffstat (limited to 'util/cbfstool/partitioned_file.c')
-rw-r--r--util/cbfstool/partitioned_file.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/util/cbfstool/partitioned_file.c b/util/cbfstool/partitioned_file.c
index f019d71fe4..7b4b0035f6 100644
--- a/util/cbfstool/partitioned_file.c
+++ b/util/cbfstool/partitioned_file.c
@@ -49,11 +49,13 @@ static unsigned count_selected_fmap_entries(const struct fmap *fmap,
return count;
}
-static partitioned_file_t *reopen_flat_file(const char *filename)
+static partitioned_file_t *reopen_flat_file(const char *filename,
+ bool write_access)
{
assert(filename);
-
struct partitioned_file *file = calloc(1, sizeof(*file));
+ const char *access_mode;
+
if (!file) {
ERROR("Failed to allocate partitioned file structure\n");
return NULL;
@@ -64,7 +66,9 @@ static partitioned_file_t *reopen_flat_file(const char *filename)
return NULL;
}
- file->stream = fopen(filename, "rb+");
+ access_mode = write_access ? "rb+" : "rb";
+ file->stream = fopen(filename, access_mode);
+
if (!file->stream) {
perror(filename);
partitioned_file_close(file);
@@ -161,11 +165,12 @@ partitioned_file_t *partitioned_file_create(const char *filename,
return file;
}
-partitioned_file_t *partitioned_file_reopen(const char *filename)
+partitioned_file_t *partitioned_file_reopen(const char *filename,
+ bool write_access)
{
assert(filename);
- partitioned_file_t *file = reopen_flat_file(filename);
+ partitioned_file_t *file = reopen_flat_file(filename, write_access);
if (!file)
return NULL;