diff options
author | Mike Fiore <mfiore@multitech.com> | 2014-01-29 14:59:20 -0600 |
---|---|---|
committer | Mike Fiore <mfiore@multitech.com> | 2014-01-29 14:59:20 -0600 |
commit | 1bfd53cec9faf1de810e24e697ba487757366203 (patch) | |
tree | adc966b99f730b5ad5c24c195c7348f47500a264 | |
parent | 398cd3fec2c60ce8cb9f97be6a768a2ee7b7ff2a (diff) | |
download | mts-id-eeprom-1bfd53cec9faf1de810e24e697ba487757366203.tar.gz mts-id-eeprom-1bfd53cec9faf1de810e24e697ba487757366203.tar.bz2 mts-id-eeprom-1bfd53cec9faf1de810e24e697ba487757366203.zip |
add support for reading and writing daughter card eeprom
-rw-r--r-- | src/eeprom_main.c | 217 |
1 files 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 <capa> |\n"); + fprintf(out, " --dc-serial <daughter-card-serial> |\n"); + fprintf(out, " --dc-hw-rev <daughter-card-hardware-revision> |\n"); + fprintf(out, " --dc-product-id <daughter-card-product-id> |\n"); + fprintf(out, " --dc-mac-addr <daughter-card-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); |