From 1bfd53cec9faf1de810e24e697ba487757366203 Mon Sep 17 00:00:00 2001 From: Mike Fiore Date: Wed, 29 Jan 2014 14:59:20 -0600 Subject: add support for reading and writing daughter card eeprom --- src/eeprom_main.c | 217 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 182 insertions(+), 35 deletions(-) diff --git a/src/eeprom_main.c b/src/eeprom_main.c index 29cd45d..8abe744 100644 --- a/src/eeprom_main.c +++ b/src/eeprom_main.c @@ -37,6 +37,11 @@ #include "log.h" #include "eeprom.h" +static int eeprom_size = 512; + +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); + static int hwaddr_aton(const char *str, uint8_t *buf, size_t len) { size_t count = 0; @@ -85,7 +90,7 @@ static int hwaddr_aton(const char *str, uint8_t *buf, size_t len) return 1; } -static int yaml_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) +static int id_yaml_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) { FILE *file; @@ -149,7 +154,70 @@ static int yaml_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) return 0; } -static int bin_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) +static int dc_yaml_out(const char *name, struct mts_dc_eeprom_layout *dc_eeprom) +{ + FILE *file; + + if (!strcmp(name, "-")) { + file = stdout; + } else { + file = fopen(name, "w+"); + if (!file) { + log_error("could not open %s: %m", name); + return -1; + } + } + + fprintf(file, "---\n"); + fprintf(file, "\n"); + + fprintf(file, "serial: \"%.32s\"\n", dc_eeprom->serial); + fprintf(file, "hw-rev: \"%.32s\"\n", dc_eeprom->hw_rev); + fprintf(file, "product-id: \"%.32s\"\n", dc_eeprom->product_id); + fprintf(file, "mac-addr: \"%02X:%02X:%02X:%02X:%02X:%02X\"\n", + dc_eeprom->mac_addr[0], + dc_eeprom->mac_addr[1], + dc_eeprom->mac_addr[2], + dc_eeprom->mac_addr[3], + dc_eeprom->mac_addr[4], + dc_eeprom->mac_addr[5]); + fprintf(file, "...\n"); + + fclose(file); + + return 0; +} + +static int id_bin_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) +{ + int fd; + int tmp; + + if (!strcmp(name, "-")) { + fd = fileno(stdout); + } else { + fd = open(name, O_CREAT | O_WRONLY, 0644); + if (fd < 0) { + log_error("could not open %s: %m", name); + return fd; + } + } + + tmp = write(fd, (char*) id_eeprom, eeprom_size); + if (tmp < 0) { + log_error("writing %s failed: %m", name); + return fd; + } else if (tmp != eeprom_size) { + log_error("only wrote %d bytes to %s", tmp, name); + return -1; + } + + close(fd); + + return tmp; +} + +static int dc_bin_out(const char *name, struct mts_dc_eeprom_layout *dc_eeprom) { int fd; int tmp; @@ -164,11 +232,11 @@ static int bin_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) } } - tmp = write(fd, (char *) id_eeprom, sizeof(*id_eeprom)); + tmp = write(fd, (char*) dc_eeprom, eeprom_size); if (tmp < 0) { log_error("writing %s failed: %m", name); return fd; - } else if (tmp != sizeof(*id_eeprom)) { + } else if (tmp != eeprom_size) { log_error("only wrote %d bytes to %s", tmp, name); return -1; } @@ -207,7 +275,7 @@ static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom) id_eeprom->mac_bluetooth[3], id_eeprom->mac_bluetooth[4], id_eeprom->mac_bluetooth[5]); - log_info("mac-wifi: %02X:%02X:%02X:%02X:%02X:%02X", + log_info("mac-wifi: %02X:%02X:%02X:%02X:%02X:%02X", id_eeprom->mac_wifi[0], id_eeprom->mac_wifi[1], id_eeprom->mac_wifi[2], @@ -216,6 +284,21 @@ static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom) id_eeprom->mac_wifi[5]); } +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("serial: %.32s", dc_eeprom->serial); + log_info("hw-rev: %.32s", dc_eeprom->hw_rev); + log_info("product-id: %.32s", dc_eeprom->product_id); + log_info("mac-addr: %02X:%02X:%02X:%02X:%02X:%02X", + dc_eeprom->mac_addr[0], + dc_eeprom->mac_addr[1], + dc_eeprom->mac_addr[2], + dc_eeprom->mac_addr[3], + dc_eeprom->mac_addr[4], + dc_eeprom->mac_addr[5]); +} + static void print_version(const char *name) { printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name); printf("Copyright (C) 2010 by Multi-Tech Systems\n"); @@ -245,8 +328,13 @@ static void usage(FILE *out) { fprintf(out, " --capa-wifi |\n"); fprintf(out, " --capa-bluetooth |\n"); fprintf(out, " --capa |\n"); + fprintf(out, " --dc-serial |\n"); + fprintf(out, " --dc-hw-rev |\n"); + fprintf(out, " --dc-product-id |\n"); + fprintf(out, " --dc-mac-addr |\n"); fprintf(out, " --out-format { bin | yaml (default) } |\n"); fprintf(out, " --update\n"); + fprintf(out, " --daughter-card |\n"); fprintf(out, " }\n"); fprintf(out, "\n"); } @@ -269,8 +357,13 @@ enum { CMD_OPT_CAPA_WIFI, CMD_OPT_CAPA_BLUETOOTH, CMD_OPT_CAPA, + CMD_OPT_DAUGHTER_CARD_SERIAL, + CMD_OPT_DAUGHTER_CARD_HW_REVISION, + CMD_OPT_DAUGHTER_CARD_PRODUCT_ID, + CMD_OPT_DAUGHTER_CARD_MAC_ADDR, CMD_OPT_OUT_FORMAT, CMD_OPT_UPDATE, + CMD_OPT_DAUGHTER_CARD, CMD_OPT_VERSION, CMD_OPT_HELP, }; @@ -294,8 +387,13 @@ static struct option long_options[] = { {"capa-wifi", 0, NULL, CMD_OPT_CAPA_WIFI}, {"capa-bluetooth", 0, NULL, CMD_OPT_CAPA_BLUETOOTH}, {"capa", 1, NULL, CMD_OPT_CAPA}, + {"dc-serial", 1, NULL, CMD_OPT_DAUGHTER_CARD_SERIAL}, + {"dc-hw-rev", 1, NULL, CMD_OPT_DAUGHTER_CARD_HW_REVISION}, + {"dc-product-id", 1, NULL, CMD_OPT_DAUGHTER_CARD_PRODUCT_ID}, + {"dc-mac-addr", 1, NULL, CMD_OPT_DAUGHTER_CARD_MAC_ADDR}, {"out-format", 1, NULL, CMD_OPT_OUT_FORMAT}, {"update", 0, NULL, CMD_OPT_UPDATE}, + {"daughter-card", 0, NULL, CMD_OPT_DAUGHTER_CARD}, {"version", 0, NULL, CMD_OPT_VERSION}, {"help", 0, NULL, CMD_OPT_HELP}, {0, 0, 0, 0}, @@ -311,10 +409,13 @@ int main(int argc, char *argv[]) { char *out_file = "-"; char *out_format = "yaml"; int update = 0; + int daughter_card = 0; struct mts_id_eeprom_layout id_eeprom; + struct mts_dc_eeprom_layout dc_eeprom; - memset(&id_eeprom, 0, sizeof(id_eeprom)); + memset(&id_eeprom, 0, eeprom_size); + memset(&dc_eeprom, 0, eeprom_size); #if 0 strncpy(id_eeprom.vendor_id, VENDOR_ID_MULTITECH, sizeof(id_eeprom.vendor_id) - 1); @@ -343,31 +444,16 @@ int main(int argc, char *argv[]) { case CMD_OPT_IN_FILE: in_file = optarg; - fd = open(in_file, O_RDONLY); - if (fd < 0) { - log_error("could not open in-file %s: %m", in_file); - exit(1); - } - - tmp = read(fd, (char *) &id_eeprom, sizeof(id_eeprom)); - if (tmp < 0) { - log_error("reading %s failed: %m", in_file); - exit(1); - } else if (tmp != sizeof(id_eeprom)) { - log_error("only read %d bytes from %s", tmp, in_file); - exit(1); - } - - close(fd); + break; - log_info("loaded eeprom from %s successfully", in_file); - mts_id_eeprom_inspect(&id_eeprom); + case CMD_OPT_UPDATE: + update = 1; + break; + case CMD_OPT_DAUGHTER_CARD: + daughter_card = 1; break; - case CMD_OPT_UPDATE: - update = 1; - break; case CMD_OPT_OUT_FILE: out_file = optarg; break; @@ -435,19 +521,40 @@ int main(int argc, char *argv[]) { DEVICE_CAPA_SET(id_eeprom.capa, CAPA_ADC); break; - case CMD_OPT_CAPA_WIFI: - DEVICE_CAPA_SET(id_eeprom.capa, CAPA_WIFI); - break; + case CMD_OPT_CAPA_WIFI: + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_WIFI); + break; - case CMD_OPT_CAPA_BLUETOOTH: - DEVICE_CAPA_SET(id_eeprom.capa, CAPA_BLUETOOTH); - break; + case CMD_OPT_CAPA_BLUETOOTH: + DEVICE_CAPA_SET(id_eeprom.capa, CAPA_BLUETOOTH); + break; case CMD_OPT_CAPA: log_error("capa option not implemented"); exit(1); break; + case CMD_OPT_DAUGHTER_CARD_SERIAL: + strncpy(dc_eeprom.serial, optarg, sizeof(dc_eeprom.serial) - 1); + break; + + case CMD_OPT_DAUGHTER_CARD_HW_REVISION: + strncpy(dc_eeprom.hw_rev, optarg, sizeof(dc_eeprom.hw_rev) - 1); + break; + + case CMD_OPT_DAUGHTER_CARD_PRODUCT_ID: + strncpy(dc_eeprom.product_id, optarg, sizeof(dc_eeprom.product_id) - 1); + break; + + case CMD_OPT_DAUGHTER_CARD_MAC_ADDR: + tmp = hwaddr_aton(optarg, dc_eeprom.mac_addr, sizeof(dc_eeprom.mac_addr)); + if (!tmp) { + log_error("invalid daughter card mac-addr %s", optarg); + usage(stderr); + exit(1); + } + break; + case CMD_OPT_OUT_FORMAT: if (strcmp(optarg, "bin") && strcmp(optarg, "yaml")) { log_error("invalid out-format %s", optarg); @@ -474,6 +581,38 @@ 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) { out_file = in_file; @@ -482,9 +621,17 @@ int main(int argc, char *argv[]) { if (out_file) { if (!strcmp(out_format, "bin")) { - bin_out(out_file, &id_eeprom); + if (! daughter_card) { + id_bin_out(out_file, &id_eeprom); + } else { + dc_bin_out(out_file, &dc_eeprom); + } } else if (!strcmp(out_format, "yaml")) { - yaml_out(out_file, &id_eeprom); + if (! daughter_card) { + id_yaml_out(out_file, &id_eeprom); + } else { + dc_yaml_out(out_file, &dc_eeprom); + } } else { log_error("un-handled out-format"); exit(1); -- cgit v1.2.3 From 43287cd1d3216686b92cec8f693e88ed1d6396ed Mon Sep 17 00:00:00 2001 From: Mike Fiore Date: Thu, 30 Jan 2014 08:24:37 -0600 Subject: single function for writing bin data to eeprom --- src/eeprom_main.c | 37 ++++--------------------------------- 1 file changed, 4 insertions(+), 33 deletions(-) diff --git a/src/eeprom_main.c b/src/eeprom_main.c index 8abe744..c7956e8 100644 --- a/src/eeprom_main.c +++ b/src/eeprom_main.c @@ -188,7 +188,7 @@ static int dc_yaml_out(const char *name, struct mts_dc_eeprom_layout *dc_eeprom) return 0; } -static int id_bin_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) +static int bin_out(const char *name, char *eeprom) { int fd; int tmp; @@ -203,36 +203,7 @@ static int id_bin_out(const char *name, struct mts_id_eeprom_layout *id_eeprom) } } - tmp = write(fd, (char*) id_eeprom, eeprom_size); - if (tmp < 0) { - log_error("writing %s failed: %m", name); - return fd; - } else if (tmp != eeprom_size) { - log_error("only wrote %d bytes to %s", tmp, name); - return -1; - } - - close(fd); - - return tmp; -} - -static int dc_bin_out(const char *name, struct mts_dc_eeprom_layout *dc_eeprom) -{ - int fd; - int tmp; - - if (!strcmp(name, "-")) { - fd = fileno(stdout); - } else { - fd = open(name, O_CREAT | O_WRONLY, 0644); - if (fd < 0) { - log_error("could not open %s: %m", name); - return fd; - } - } - - tmp = write(fd, (char*) dc_eeprom, eeprom_size); + tmp = write(fd, eeprom, eeprom_size); if (tmp < 0) { log_error("writing %s failed: %m", name); return fd; @@ -622,9 +593,9 @@ int main(int argc, char *argv[]) { if (out_file) { if (!strcmp(out_format, "bin")) { if (! daughter_card) { - id_bin_out(out_file, &id_eeprom); + bin_out(out_file, (char*) &id_eeprom); } else { - dc_bin_out(out_file, &dc_eeprom); + bin_out(out_file, (char*) &dc_eeprom); } } else if (!strcmp(out_format, "yaml")) { if (! daughter_card) { -- cgit v1.2.3 From e92217e366678be4d556646038963c24bf51982f Mon Sep 17 00:00:00 2001 From: Mike Fiore Date: Thu, 30 Jan 2014 15:41:29 -0600 Subject: use updated eeprom layout, only need to specify daughter card parameter and supply proper fields to flash daughter card eeprom --- src/eeprom_main.c | 110 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 61 insertions(+), 49 deletions(-) diff --git a/src/eeprom_main.c b/src/eeprom_main.c index c7956e8..29c0f99 100644 --- a/src/eeprom_main.c +++ b/src/eeprom_main.c @@ -171,9 +171,10 @@ static int dc_yaml_out(const char *name, struct mts_dc_eeprom_layout *dc_eeprom) fprintf(file, "---\n"); fprintf(file, "\n"); - fprintf(file, "serial: \"%.32s\"\n", dc_eeprom->serial); - fprintf(file, "hw-rev: \"%.32s\"\n", dc_eeprom->hw_rev); + fprintf(file, "vendor-id: \"%.32s\"\n", dc_eeprom->vendor_id); fprintf(file, "product-id: \"%.32s\"\n", dc_eeprom->product_id); + fprintf(file, "device-id: \"%.32s\"\n", dc_eeprom->device_id); + fprintf(file, "hw-version: \"%.32s\"\n", dc_eeprom->hw_version); fprintf(file, "mac-addr: \"%02X:%02X:%02X:%02X:%02X:%02X\"\n", dc_eeprom->mac_addr[0], dc_eeprom->mac_addr[1], @@ -258,9 +259,10 @@ 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("serial: %.32s", dc_eeprom->serial); - log_info("hw-rev: %.32s", dc_eeprom->hw_rev); + 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); + log_info("hw-version: %.32s", dc_eeprom->hw_version); log_info("mac-addr: %02X:%02X:%02X:%02X:%02X:%02X", dc_eeprom->mac_addr[0], dc_eeprom->mac_addr[1], @@ -299,13 +301,9 @@ static void usage(FILE *out) { fprintf(out, " --capa-wifi |\n"); fprintf(out, " --capa-bluetooth |\n"); fprintf(out, " --capa |\n"); - fprintf(out, " --dc-serial |\n"); - fprintf(out, " --dc-hw-rev |\n"); - fprintf(out, " --dc-product-id |\n"); - fprintf(out, " --dc-mac-addr |\n"); fprintf(out, " --out-format { bin | yaml (default) } |\n"); - fprintf(out, " --update\n"); - fprintf(out, " --daughter-card |\n"); + fprintf(out, " --update |\n"); + fprintf(out, " --daughter-card\n"); fprintf(out, " }\n"); fprintf(out, "\n"); } @@ -328,10 +326,6 @@ enum { CMD_OPT_CAPA_WIFI, CMD_OPT_CAPA_BLUETOOTH, CMD_OPT_CAPA, - CMD_OPT_DAUGHTER_CARD_SERIAL, - CMD_OPT_DAUGHTER_CARD_HW_REVISION, - CMD_OPT_DAUGHTER_CARD_PRODUCT_ID, - CMD_OPT_DAUGHTER_CARD_MAC_ADDR, CMD_OPT_OUT_FORMAT, CMD_OPT_UPDATE, CMD_OPT_DAUGHTER_CARD, @@ -358,10 +352,6 @@ static struct option long_options[] = { {"capa-wifi", 0, NULL, CMD_OPT_CAPA_WIFI}, {"capa-bluetooth", 0, NULL, CMD_OPT_CAPA_BLUETOOTH}, {"capa", 1, NULL, CMD_OPT_CAPA}, - {"dc-serial", 1, NULL, CMD_OPT_DAUGHTER_CARD_SERIAL}, - {"dc-hw-rev", 1, NULL, CMD_OPT_DAUGHTER_CARD_HW_REVISION}, - {"dc-product-id", 1, NULL, CMD_OPT_DAUGHTER_CARD_PRODUCT_ID}, - {"dc-mac-addr", 1, NULL, CMD_OPT_DAUGHTER_CARD_MAC_ADDR}, {"out-format", 1, NULL, CMD_OPT_OUT_FORMAT}, {"update", 0, NULL, CMD_OPT_UPDATE}, {"daughter-card", 0, NULL, CMD_OPT_DAUGHTER_CARD}, @@ -382,6 +372,12 @@ int main(int argc, char *argv[]) { int update = 0; int daughter_card = 0; + char *vendor_id = NULL; + char *product_id = NULL; + char *device_id = NULL; + char *hw_version = NULL; + char *mac_addr = NULL; + struct mts_id_eeprom_layout id_eeprom; struct mts_dc_eeprom_layout dc_eeprom; @@ -430,28 +426,23 @@ int main(int argc, char *argv[]) { break; case CMD_OPT_VENDOR_ID: - strncpy(id_eeprom.vendor_id, optarg, sizeof(id_eeprom.vendor_id) - 1); + vendor_id = optarg; break; case CMD_OPT_PRODUCT_ID: - strncpy(id_eeprom.product_id, optarg, sizeof(id_eeprom.product_id) - 1); + product_id = optarg; break; case CMD_OPT_DEVICE_ID: - strncpy(id_eeprom.device_id, optarg, sizeof(id_eeprom.device_id) - 1); + device_id = optarg; break; case CMD_OPT_HW_VERSION: - strncpy(id_eeprom.hw_version, optarg, sizeof(id_eeprom.hw_version) - 1); + hw_version = optarg; break; case CMD_OPT_MAC_ADDR: - tmp = hwaddr_aton(optarg, id_eeprom.mac_addr, sizeof(id_eeprom.mac_addr)); - if (!tmp) { - log_error("invalid mac-addr %s", optarg); - usage(stderr); - exit(1); - } + mac_addr = optarg; break; case CMD_OPT_MAC_BLUETOOTH: @@ -505,27 +496,6 @@ int main(int argc, char *argv[]) { exit(1); break; - case CMD_OPT_DAUGHTER_CARD_SERIAL: - strncpy(dc_eeprom.serial, optarg, sizeof(dc_eeprom.serial) - 1); - break; - - case CMD_OPT_DAUGHTER_CARD_HW_REVISION: - strncpy(dc_eeprom.hw_rev, optarg, sizeof(dc_eeprom.hw_rev) - 1); - break; - - case CMD_OPT_DAUGHTER_CARD_PRODUCT_ID: - strncpy(dc_eeprom.product_id, optarg, sizeof(dc_eeprom.product_id) - 1); - break; - - case CMD_OPT_DAUGHTER_CARD_MAC_ADDR: - tmp = hwaddr_aton(optarg, dc_eeprom.mac_addr, sizeof(dc_eeprom.mac_addr)); - if (!tmp) { - log_error("invalid daughter card mac-addr %s", optarg); - usage(stderr); - exit(1); - } - break; - case CMD_OPT_OUT_FORMAT: if (strcmp(optarg, "bin") && strcmp(optarg, "yaml")) { log_error("invalid out-format %s", optarg); @@ -552,6 +522,48 @@ int main(int argc, char *argv[]) { } } + /* these fields can apply to the on board EEPROM or the daughter card EEPROM */ + if (vendor_id) { + if (! daughter_card) { + strncpy(id_eeprom.vendor_id, vendor_id, sizeof(id_eeprom.vendor_id) - 1); + } else { + strncpy(dc_eeprom.vendor_id, vendor_id, sizeof(dc_eeprom.vendor_id) - 1); + } + } + if (product_id) { + if (! daughter_card) { + strncpy(id_eeprom.product_id, product_id, sizeof(id_eeprom.product_id) - 1); + } else { + strncpy(dc_eeprom.product_id, product_id, sizeof(dc_eeprom.product_id) - 1); + } + } + if (device_id) { + if (! daughter_card) { + strncpy(id_eeprom.device_id, device_id, sizeof(id_eeprom.device_id) - 1); + } else { + strncpy(dc_eeprom.device_id, device_id, sizeof(dc_eeprom.device_id) - 1); + } + } + if (hw_version) { + if (! daughter_card) { + strncpy(id_eeprom.hw_version, hw_version, sizeof(id_eeprom.hw_version) - 1); + } else { + strncpy(dc_eeprom.hw_version, hw_version, sizeof(dc_eeprom.hw_version) - 1); + } + } + if (mac_addr) { + if (! daughter_card) { + tmp = hwaddr_aton(mac_addr, id_eeprom.mac_addr, sizeof(id_eeprom.mac_addr)); + } else { + tmp = hwaddr_aton(mac_addr, dc_eeprom.mac_addr, sizeof(dc_eeprom.mac_addr)); + } + if (!tmp) { + log_error("invalid mac-addr %s", optarg); + usage(stderr); + exit(1); + } + } + if (in_file) { fd = open(in_file, O_RDONLY); if (fd < 0) { -- cgit v1.2.3