diff options
author | Randall Spangler <rspangler@chromium.org> | 2013-06-27 12:57:31 -0700 |
---|---|---|
committer | ChromeBot <chrome-bot@google.com> | 2013-06-28 10:27:33 -0700 |
commit | 61820ceb435917607107674cd0eb888598072dbf (patch) | |
tree | db948e312da9e1b04b50d11d9d2597d3598821d4 /util/ectool.c | |
parent | bbbc7ebc5baceed3b3f7e477e09d299da1b0b935 (diff) | |
download | chrome-ec-61820ceb435917607107674cd0eb888598072dbf.tar.gz |
Split file read/write functions out of ectool.c
ectool.c has gotten monstrously huge. Refactor out some utility functions.
This is precursor work to refactoring out a lower-level flash
read/write interface.
BUG=chrome-os-partner:20571
BRANCH=none
TEST=ectool flashread 0x20000 0x80 /tmp foo -> works
Change-Id: I26dae609a73e54e8adaec56edbdce6a0bb4b8758
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/60265
Reviewed-by: Vic Yang <victoryang@chromium.org>
Diffstat (limited to 'util/ectool.c')
-rw-r--r-- | util/ectool.c | 81 |
1 files changed, 1 insertions, 80 deletions
diff --git a/util/ectool.c b/util/ectool.c index 3f6a0b7758..c10689a533 100644 --- a/util/ectool.c +++ b/util/ectool.c @@ -18,11 +18,9 @@ #include "ectool.h" #include "lightbar.h" #include "lock/gec_lock.h" +#include "misc_util.h" #include "panic.h" -/* Don't use a macro where an inline will do... */ -static inline int MIN(int a, int b) { return a < b ? a : b; } - #define GEC_LOCK_TIMEOUT_SECS 30 /* 30 secs */ const char help_str[] = @@ -168,83 +166,6 @@ static const char * const image_names[] = {"unknown", "RO", "RW"}; static const char * const led_color_names[EC_LED_COLOR_COUNT] = { "red", "green", "blue", "yellow", "white"}; -/* Write a buffer to the file. Return non-zero if error. */ -static int write_file(const char *filename, const char *buf, int size) -{ - FILE *f; - int i; - - /* Write to file */ - f = fopen(filename, "wb"); - if (!f) { - perror("Error opening output file"); - return -1; - } - i = fwrite(buf, 1, size, f); - fclose(f); - if (i != size) { - perror("Error writing to file"); - return -1; - } - - return 0; -} - - -/* Read a file into a buffer. Sets *size to the size of the buffer. Returns - * the buffer, which must be freed with free() by the caller. Returns NULL if - * error. */ -static char *read_file(const char *filename, int *size) -{ - FILE *f = fopen(filename, "rb"); - char *buf; - int i; - - if (!f) { - perror("Error opening input file"); - return NULL; - } - - fseek(f, 0, SEEK_END); - *size = ftell(f); - rewind(f); - if (*size > 0x100000) { - fprintf(stderr, "File seems unreasonably large\n"); - fclose(f); - return NULL; - } - - buf = (char *)malloc(*size); - if (!buf) { - fprintf(stderr, "Unable to allocate buffer.\n"); - fclose(f); - return NULL; - } - - printf("Reading %d bytes from %s...\n", *size, filename); - i = fread(buf, 1, *size, f); - fclose(f); - if (i != *size) { - perror("Error reading file"); - free(buf); - return NULL; - } - - return buf; -} - - -int is_string_printable(const char *buf) -{ - while (*buf) { - if (!isprint(*buf)) - return 0; - buf++; - } - - return 1; -} - /* Check SBS numerical value range */ int is_battery_range(int val) |