diff options
-rw-r--r-- | src/eeprom_main.c | 315 |
1 files changed, 231 insertions, 84 deletions
diff --git a/src/eeprom_main.c b/src/eeprom_main.c index 297c7d8..0627124 100644 --- a/src/eeprom_main.c +++ b/src/eeprom_main.c @@ -32,7 +32,10 @@ #include <sys/stat.h> #include <fcntl.h> #include <string.h> -#include <linux/mts_eeprom.h> +#include <mts-kernel-headers/linux/mts_eeprom.h> +#ifdef MTCDT3B +#include <mts-kernel-headers/linux/mtcdt3b_eeprom.h> +#endif #include "log.h" #include "eeprom.h" @@ -42,6 +45,10 @@ static int eeprom_size = 512; static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom); static void mts_ap_eeprom_inspect(struct mts_ap_eeprom_layout *ap_eeprom); +#ifdef MTCDT3B +static void mtcdt3b_eeprom_inspect(struct mtcdt3b_eeprom_layout *mtcdt3b_eeprom); +#endif + //Returns 0 on success, 1 on failure static unsigned int charToInt(char ch, unsigned int* i) { if (ch >= '0' && ch <= '9') { @@ -253,6 +260,49 @@ static int ap_yaml_out(const char *name, struct mts_ap_eeprom_layout *ap_eeprom) return 0; } +#ifdef MTCDT3B +static int mtcdt3b_yaml_out(const char *name, struct mtcdt3b_eeprom_layout *mtcdt3b_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, "vendor-id: \"%.32s\"\n", mtcdt3b_eeprom->vendor_id); + fprintf(file, "product-id: \"%.32s\"\n", mtcdt3b_eeprom->product_id); + fprintf(file, "device-id: \"%.32s\"\n", mtcdt3b_eeprom->device_id); + fprintf(file, "hw-version: \"%.32s\"\n", mtcdt3b_eeprom->hw_version); + fprintf(file, "capa-lora: %s\n", DEVICE_CAPA(mtcdt3b_eeprom->capa, MTCDT3B_CAPA_LORA) ? "true" : "false"); + fprintf(file, "lora-eui: \"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\"\n", + mtcdt3b_eeprom->lora_eui[0], + mtcdt3b_eeprom->lora_eui[1], + mtcdt3b_eeprom->lora_eui[2], + mtcdt3b_eeprom->lora_eui[3], + mtcdt3b_eeprom->lora_eui[4], + mtcdt3b_eeprom->lora_eui[5], + mtcdt3b_eeprom->lora_eui[6], + mtcdt3b_eeprom->lora_eui[7]); + fprintf(file, "lora-product-id: \"%.32s\"\n", mtcdt3b_eeprom->lora_product_id); + fprintf(file, "lora-hw-version: \"%.32s\"\n", mtcdt3b_eeprom->lora_hw_version); + + fprintf(file, "...\n"); + + fclose(file); + + return 0; +} +#endif + static int bin_out(const char *name, char *eeprom) { int fd; @@ -369,9 +419,32 @@ static void mts_ap_eeprom_inspect(struct mts_ap_eeprom_layout *ap_eeprom) ap_eeprom->eui[7]); } +#ifdef MTCDT3B +static void mtcdt3b_eeprom_inspect(struct mtcdt3b_eeprom_layout *mtcdt3b_eeprom) +{ + log_info("sizeof: %u", sizeof(struct mtcdt3b_eeprom_layout)); + log_info("vendor-id: %.32s", mtcdt3b_eeprom->vendor_id); + log_info("product-id: %.32s", mtcdt3b_eeprom->product_id); + log_info("device-id: %.32s", mtcdt3b_eeprom->device_id); + log_info("hw-version: %.32s", mtcdt3b_eeprom->hw_version); + log_info("mtcdt3b-capa-lora: %s", DEVICE_CAPA(mtcdt3b_eeprom->capa, MTCDT3B_CAPA_LORA) ? "yes" : "no"); + log_info("eui: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", + mtcdt3b_eeprom->lora_eui[0], + mtcdt3b_eeprom->lora_eui[1], + mtcdt3b_eeprom->lora_eui[2], + mtcdt3b_eeprom->lora_eui[3], + mtcdt3b_eeprom->lora_eui[4], + mtcdt3b_eeprom->lora_eui[5], + mtcdt3b_eeprom->lora_eui[6], + mtcdt3b_eeprom->lora_eui[7]); + log_info("lora-product-id: %.32s", mtcdt3b_eeprom->lora_product_id); + log_info("lora-hw-version: %.32s", mtcdt3b_eeprom->lora_hw_version); +} +#endif + static void print_version(const char *name) { printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name); - printf("Copyright (C) 2015 by Multi-Tech Systems\n"); + printf("Copyright (C) 2015, 2019 by Multi-Tech Systems\n"); printf( "This program is free software; you may redistribute it under the terms of\n" "the GNU General Public License version 2 or (at your option) any later version.\n" @@ -407,6 +480,9 @@ static void usage(FILE *out) { fprintf(out, " --out-format { bin | yaml (default) } |\n"); fprintf(out, " --update |\n"); fprintf(out, " --accessory-card\n"); +#ifdef MTCDT3B + fprintf(out, " --base-board\n"); +#endif fprintf(out, " }\n"); fprintf(out, "\n"); } @@ -437,7 +513,9 @@ enum { CMD_OPT_OUT_FORMAT, CMD_OPT_UPDATE, CMD_OPT_ACCESSORY_CARD, - +#ifdef MTCDT3B + CMD_OPT_BASE_BOARD, +#endif CMD_OPT_VERSION, CMD_OPT_HELP, CMD_OPT_HEX_TO_BIN, @@ -471,6 +549,9 @@ static struct option long_options[] = { {"out-format", 1, NULL, CMD_OPT_OUT_FORMAT}, {"update", 0, NULL, CMD_OPT_UPDATE}, {"accessory-card", 0, NULL, CMD_OPT_ACCESSORY_CARD}, +#ifdef MTCDT3B + {"base-board", 0, NULL, CMD_OPT_BASE_BOARD}, +#endif {"version", 0, NULL, CMD_OPT_VERSION}, {"help", 0, NULL, CMD_OPT_HELP}, {0, 0, 0, 0}, @@ -487,7 +568,9 @@ int main(int argc, char *argv[]) { char *out_format = "yaml"; int update = 0; int accessory_card = 0; - +#ifdef MTCDT3B + int base_board = 0; +#endif char *vendor_id = NULL; char *product_id = NULL; char *device_id = NULL; @@ -500,6 +583,9 @@ int main(int argc, char *argv[]) { struct mts_id_eeprom_layout id_eeprom; struct mts_ap_eeprom_layout ap_eeprom; +#ifdef MTCDT3B + struct mtcdt3b_eeprom_layout mtcdt3b_eeprom; +#endif memset(&id_eeprom, 0, eeprom_size); memset(&ap_eeprom, 0, eeprom_size); @@ -538,6 +624,12 @@ int main(int argc, char *argv[]) { accessory_card = 1; break; +#ifdef MTCDT3B + case CMD_OPT_BASE_BOARD: + base_board = 1; + break; +#endif + case CMD_OPT_VERSION: print_version("mts-id-eeprom"); exit(0); @@ -604,6 +696,12 @@ int main(int argc, char *argv[]) { // handled above break; +#ifdef MTCDT3B + case CMD_OPT_BASE_BOARD: + // handled above + break; +#endif + case CMD_OPT_OUT_FILE: out_file = optarg; break; @@ -700,6 +798,12 @@ int main(int argc, char *argv[]) { break; case CMD_OPT_CAPA_LORA: +#ifdef MTCDT3B + if (base_board) { + DEVICE_CAPA_SET(mtcdt3b_eeprom.capa, MTCDT3B_CAPA_LORA); + break; + } +#endif DEVICE_CAPA_SET(id_eeprom.capa, CAPA_LORA); break; @@ -738,82 +842,116 @@ int main(int argc, char *argv[]) { } } + +#define EEPROM_SET(eeprom,field) (strncpy(eeprom.field, field, sizeof(eeprom.field) - 1)); + /* these fields can apply to the on board EEPROM or the accessory card EEPROM */ if (vendor_id) { - if (! accessory_card) { - strncpy(id_eeprom.vendor_id, vendor_id, sizeof(id_eeprom.vendor_id) - 1); - } else { - strncpy(ap_eeprom.vendor_id, vendor_id, sizeof(ap_eeprom.vendor_id) - 1); - } - } - if (product_id) { - if (! accessory_card) { - strncpy(id_eeprom.product_id, product_id, sizeof(id_eeprom.product_id) - 1); - } else { - strncpy(ap_eeprom.product_id, product_id, sizeof(ap_eeprom.product_id) - 1); - } - } - if (device_id) { - if (! accessory_card) { - strncpy(id_eeprom.device_id, device_id, sizeof(id_eeprom.device_id) - 1); - } else { - strncpy(ap_eeprom.device_id, device_id, sizeof(ap_eeprom.device_id) - 1); - } - } - if (hw_version) { - if (! accessory_card) { - strncpy(id_eeprom.hw_version, hw_version, sizeof(id_eeprom.hw_version) - 1); - } else { - strncpy(ap_eeprom.hw_version, hw_version, sizeof(ap_eeprom.hw_version) - 1); - } - } + if (accessory_card) + EEPROM_SET(ap_eeprom,vendor_id) +#ifdef MTCDT3B + else if (base_board) + EEPROM_SET(mtcdt3b_eeprom,vendor_id) +#endif + else + EEPROM_SET(id_eeprom,vendor_id) + } + if (product_id) { + if (accessory_card) + EEPROM_SET(ap_eeprom,product_id) +#ifdef MTCDT3B + else if (base_board) + EEPROM_SET(mtcdt3b_eeprom,product_id) +#endif + else + EEPROM_SET(id_eeprom,product_id) + } + if (device_id) { + if (accessory_card) + EEPROM_SET(ap_eeprom,device_id) +#ifdef MTCDT3B + else if (base_board) + EEPROM_SET(mtcdt3b_eeprom,device_id) +#endif + else + EEPROM_SET(id_eeprom,device_id) + } + if (hw_version) { + if (accessory_card) + EEPROM_SET(ap_eeprom,hw_version) +#ifdef MTCDT3B + else if (base_board) + EEPROM_SET(mtcdt3b_eeprom,hw_version) +#endif + else + EEPROM_SET(id_eeprom,hw_version) + } + if (mac_addr) { - if (! accessory_card) { - tmp = hwaddr_aton(mac_addr, id_eeprom.mac_addr, sizeof(id_eeprom.mac_addr)); - } else { - tmp = hwaddr_aton(mac_addr, ap_eeprom.mac_addr, sizeof(ap_eeprom.mac_addr)); - } - if (!tmp) { - log_error("invalid mac-addr %s", mac_addr); - usage(stderr); - exit(1); + if (accessory_card) + tmp = hwaddr_aton(mac_addr, ap_eeprom.mac_addr, sizeof(ap_eeprom.mac_addr)); +#ifdef MTCDT3B + else if(base_board) { + log_error("--mac-addr option is not supported on MTCDT3 base board eeprom"); + usage(stderr); + exit(1); + } +#endif + else + tmp = hwaddr_aton(mac_addr, id_eeprom.mac_addr, sizeof(id_eeprom.mac_addr)); + + if (!tmp) { + log_error("invalid mac-addr %s", mac_addr); + usage(stderr); + exit(1); } } // on-board lora product id if (lora_product_id) { - if (! accessory_card) { - strncpy(id_eeprom.lora_product_id, lora_product_id, sizeof(id_eeprom.lora_product_id) - 1); - } else { - log_error("--lora-product-id option is not supported on accessory card eeprom"); - usage(stderr); - exit(1); - } + if (accessory_card) { + log_error("--lora-product-id option is not supported on accessory card eeprom"); + usage(stderr); + exit(1); + } +#ifdef MTCDT3B + else if (base_board) + EEPROM_SET(mtcdt3b_eeprom,lora_product_id) +#endif + else + EEPROM_SET(id_eeprom,lora_product_id) } + // on-board lora hw version if (lora_hw_version) { - if (! accessory_card) { - strncpy(id_eeprom.lora_hw_version, lora_hw_version, sizeof(id_eeprom.lora_hw_version) - 1); - } else { - log_error("--lora-hw-version option is not supported on accessory card eeprom"); - usage(stderr); - exit(1); - } + if (accessory_card) { + log_error("--lora-hw-version option is not supported on accessory card eeprom"); + usage(stderr); + exit(1); + } +#ifdef MTCDT3B + else if (base_board) + EEPROM_SET(mtcdt3b_eeprom,lora_hw_version) +#endif + else + EEPROM_SET(id_eeprom,lora_hw_version) } if (lora_eui) { - if (!accessory_card) { - tmp = hwaddr_aton(lora_eui, id_eeprom.lora_eui, sizeof(id_eeprom.lora_eui)); - } - else { - tmp = hwaddr_aton(lora_eui, ap_eeprom.eui, sizeof(ap_eeprom.eui)); - } - if (!tmp) { - log_error("invalid EUI %s", lora_eui); - usage(stderr); - exit(1); - } + if (accessory_card) + tmp = hwaddr_aton(lora_eui, ap_eeprom.eui, sizeof(ap_eeprom.eui)); +#ifdef MTCDT3B + else if (base_board) + tmp = hwaddr_aton(lora_eui, mtcdt3b_eeprom.lora_eui, sizeof(ap_eeprom.eui)); +#endif + else + tmp = hwaddr_aton(lora_eui, id_eeprom.lora_eui, sizeof(id_eeprom.lora_eui)); + + if (!tmp) { + log_error("invalid EUI %s", lora_eui); + usage(stderr); + exit(1); + } } - // updating eeprom in place, force bin format if (update && in_file) { out_file = in_file; @@ -822,30 +960,39 @@ int main(int argc, char *argv[]) { if (out_file) { if (!strcmp(out_format, "bin")) { - if (! accessory_card) { - bin_out(out_file, (char*) &id_eeprom); - } else { - bin_out(out_file, (char*) &ap_eeprom); - } + if (accessory_card) + bin_out(out_file, (char*) &ap_eeprom); +#ifdef MTCDT3B + else if (base_board) + bin_out(out_file, (char*) &mtcdt3b_eeprom); +#endif + else + bin_out(out_file, (char*) &id_eeprom); } else if (!strcmp(out_format, "yaml")) { - if (! accessory_card) { - id_yaml_out(out_file, &id_eeprom); - } else { - ap_yaml_out(out_file, &ap_eeprom); - } + if (accessory_card) + ap_yaml_out(out_file, &ap_eeprom); +#ifdef MTCDT3B + else if (base_board) + mtcdt3b_yaml_out(out_file, &mtcdt3b_eeprom); +#endif + else + id_yaml_out(out_file, &id_eeprom); } else { - log_error("un-handled out-format"); - exit(1); + log_error("un-handled out-format"); + exit(1); } /* print out what we wrote to the eeprom, unless stdout */ if (strcmp(out_file, "-")) { - log_info("Data written to %s", out_file); - if (accessory_card) { - mts_ap_eeprom_inspect(&ap_eeprom); - } else { - mts_id_eeprom_inspect(&id_eeprom); - } + log_info("Data written to %s", out_file); + if (accessory_card) + mts_ap_eeprom_inspect(&ap_eeprom); +#ifdef MTCDT3B + else if (base_board) + mtcdt3b_eeprom_inspect(&mtcdt3b_eeprom); +#endif + else + mts_id_eeprom_inspect(&id_eeprom); } } |