From 32e1c6c9c8f122271d6ece1ed68c6994809daa3e Mon Sep 17 00:00:00 2001 From: Jesse Gilles Date: Mon, 3 Mar 2014 11:18:33 -0600 Subject: read in-file first if specified, fixes update option --- src/eeprom_main.c | 112 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 67 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/eeprom_main.c b/src/eeprom_main.c index 29c0f99..a86b567 100644 --- a/src/eeprom_main.c +++ b/src/eeprom_main.c @@ -220,7 +220,7 @@ static int bin_out(const char *name, char *eeprom) static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom) { - log_info("sizeof: %lu", sizeof(struct mts_id_eeprom_layout)); + log_info("sizeof: %u", sizeof(struct mts_id_eeprom_layout)); log_info("vendor-id: %.32s", id_eeprom->vendor_id); log_info("product-id: %.32s", id_eeprom->product_id); log_info("device-id: %.32s", id_eeprom->device_id); @@ -258,7 +258,7 @@ static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom) static void mts_dc_eeprom_inspect(struct mts_dc_eeprom_layout *dc_eeprom) { - log_info("sizeof: %lu", sizeof(struct mts_dc_eeprom_layout)); + log_info("sizeof: %u", sizeof(struct mts_dc_eeprom_layout)); log_info("vendor-id: %.32s", dc_eeprom->vendor_id); log_info("product-id: %.32s", dc_eeprom->product_id); log_info("device-id: %.32s", dc_eeprom->device_id); @@ -404,6 +404,7 @@ int main(int argc, char *argv[]) { hwaddr_aton("00:d0:a0:02:0d:e3", id_eeprom.mac_wifi, sizeof(id_eeprom.mac_wifi)); #endif + // parse options once to get input file and daughter_card flag only while((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) { switch(i) { case 0: @@ -413,12 +414,74 @@ int main(int argc, char *argv[]) { in_file = optarg; break; + case CMD_OPT_DAUGHTER_CARD: + daughter_card = 1; + break; + + case CMD_OPT_VERSION: + print_version("mts-id-eeprom"); + exit(0); + break; + + case CMD_OPT_HELP: + usage(stdout); + exit(0); + break; + } + } + + // read eeprom before parsing other options and modifying fields + // this is needed if --update is specified + if (in_file) { + fd = open(in_file, O_RDONLY); + if (fd < 0) { + log_error("could not open in-file %s: %m", in_file); + exit(1); + } + + if (daughter_card) { + tmp = read(fd, (char *) &dc_eeprom, eeprom_size); + } else { + tmp = read(fd, (char *) &id_eeprom, eeprom_size); + } + + if (tmp < 0) { + log_error("reading %s failed: %m", in_file); + exit(1); + } else if (tmp != eeprom_size) { + log_error("only read %d bytes from %s", tmp, in_file); + exit(1); + } + + if (daughter_card) { + log_info("loaded dc eeprom from %s successfully", in_file); + mts_dc_eeprom_inspect(&dc_eeprom); + } else { + log_info("loaded id eeprom from %s successfully", in_file); + mts_id_eeprom_inspect(&id_eeprom); + } + + close(fd); + } + + // reset getopt index and parse again + optind = 0; + + while((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) { + switch(i) { + case 0: + break; + + case CMD_OPT_IN_FILE: + // handled above + break; + case CMD_OPT_UPDATE: update = 1; break; case CMD_OPT_DAUGHTER_CARD: - daughter_card = 1; + // handled above break; case CMD_OPT_OUT_FILE: @@ -506,16 +569,6 @@ int main(int argc, char *argv[]) { break; - case CMD_OPT_VERSION: - print_version("mts-id-eeprom"); - exit(0); - break; - - case CMD_OPT_HELP: - usage(stdout); - exit(0); - break; - default: usage(stderr); exit(1); @@ -564,40 +617,9 @@ int main(int argc, char *argv[]) { } } - if (in_file) { - fd = open(in_file, O_RDONLY); - if (fd < 0) { - log_error("could not open in-file %s: %m", in_file); - exit(1); - } - - if (daughter_card) { - tmp = read(fd, (char *) &dc_eeprom, eeprom_size); - } else { - tmp = read(fd, (char *) &id_eeprom, eeprom_size); - } - - if (tmp < 0) { - log_error("reading %s failed: %m", in_file); - exit(1); - } else if (tmp != eeprom_size) { - log_error("only read %d bytes from %s", tmp, in_file); - exit(1); - } - - if (daughter_card) { - log_info("loaded dc eeprom from %s successfully", in_file); - mts_dc_eeprom_inspect(&dc_eeprom); - } else { - log_info("loaded id eeprom from %s successfully", in_file); - mts_id_eeprom_inspect(&id_eeprom); - } - - close(fd); - } // updating eeprom in place, force bin format - if (update) { + if (update && in_file) { out_file = in_file; out_format = "bin"; } -- cgit v1.2.3