diff --git a/util/build.mk b/util/build.mk index 062e34c708..9d2f149241 100644 --- a/util/build.mk +++ b/util/build.mk @@ -7,7 +7,7 @@ # host-util-bin=ectool lbplay burn_my_ec -host-util-common=ectool_keyscan comm-host comm-dev +host-util-common=ectool_keyscan comm-host comm-dev misc_util ifeq ($(CONFIG_LPC),y) host-util-common+=comm-lpc else 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) diff --git a/util/misc_util.c b/util/misc_util.c new file mode 100644 index 0000000000..8410f03cfa --- /dev/null +++ b/util/misc_util.c @@ -0,0 +1,83 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include +#include +#include +#include + +#include "misc_util.h" + +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; +} + +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; +} diff --git a/util/misc_util.h b/util/misc_util.h new file mode 100644 index 0000000000..2224d84aaf --- /dev/null +++ b/util/misc_util.h @@ -0,0 +1,40 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __CROS_EC_MISC_UTIL_H +#define __CROS_EC_MISC_UTIL_H + +/* Don't use a macro where an inline will do... */ +static inline int MIN(int a, int b) { return a < b ? a : b; } + +/** + * Write a buffer to the file. + * + * @param filename Target filename + * @param buf Buffer to write + * @param size Size of buffer in bytes + * @return non-zero if error + */ +int write_file(const char *filename, const char *buf, int size); + +/** + * Read a file into a newly-allocated buffer. + * + * @param filename Source filename + * @param size Size of data in bytes will be stored here on success. + * @return A newly allocated buffer with the data, which must be freed with + * free() by the caller, or NULL if error. + */ +char *read_file(const char *filename, int *size); + +/** + * Check if a string contains only printable characters. + * + * @param buf Null-terminated string to check + * @return non-zero if buf contains only printable characters; zero if not. + */ +int is_string_printable(const char *buf); + +#endif