#include #include #include #include #include #include #include #include #include #include struct flash_dev *flash = NULL; /* TODO: implement a parser for hex arrays that is more user-friendly. * Also implement dry runs for programmers. */ static void parse_hex(FILE *fp, char *buf, size_t len) { size_t i; char s[3]; for (i = 0; i < len; ++i) { memset(s, '\0', 3); while ((s[0] = getc(fp)) && !isxdigit(s[0])); while ((s[1] = getc(fp)) && !isxdigit(s[1])); *buf++ = strtoul(s, NULL, 16); } } static void print_hex_ascii(FILE *out, uint32_t offset, const char *buf, size_t len) { size_t n, i; uint8_t c; for (; len; buf += n, len -= n) { n = min(len, 16); fprintf(out, "%08" PRIx32 ": ", offset); for (i = 0; i < 16; ++i) { c = (i < n) ? buf[i] : 0; fprintf(out, "%02x", c); } fprintf(out, " "); for (i = 0; i < 16; ++i) { c = (i < n) ? buf[i] : 0; fprintf(out, "%c", isalnum(c) ? c : '.'); } fprintf(out, "\n"); offset += n; } } static void do_flash_probe(FILE *out, const char **argv, size_t argc) { (void)argv; (void)argc; if (flash) { flash_release(flash); flash = NULL; } if (!(flash = flash_probe())) { fprintf(out, "error: unable to probe the flash device.\n"); return; } } static void do_flash_release(FILE *out, const char **argv, size_t argc) { (void)argv; (void)argc; if (!flash) { fprintf(out, "no flash device currently active.\n"); return; } flash_release(flash); flash = NULL; } static void do_flash_info(FILE *out, const char **argv, size_t argc) { size_t size, capacity; (void)argv; (void)argc; if (!flash) { fprintf(out, "error: no flash device probed.\n"); return; } size = flash_get_size(flash); capacity = flash_get_capacity(flash); fprintf(out, " size: %" PRSZu "/%" PRSZu " bytes (%" PRSZu "%% usage)\n", size, capacity, 100 * size / capacity); } static void do_flash_read(FILE *out, const char **argv, size_t argc) { char buf[256]; size_t addr, len, nbytes; if (argc < 2) { fprintf(out, "usage: flash read \n"); return; } if (!flash) { fprintf(out, "error: no flash device probed.\n"); return; } addr = strtoull(argv[0], NULL, 16); if (errno == EINVAL || errno == ERANGE) { fprintf(out, "error: expected hexadecimal value for addr\n"); return; } len = strtoull(argv[1], NULL, 16); if (errno == EINVAL || errno == ERANGE) { fprintf(out, "error: expected hexadecimal value for len\n"); return; } if (!len) return; while (len) { nbytes = min(len, 256); flash_read(flash, addr, buf, nbytes); print_hex_ascii(out, addr, buf, nbytes); addr += nbytes; len -= nbytes; } } static void do_flash_write(FILE *out, const char **argv, size_t argc) { char buf[256]; size_t addr, len, nbytes; if (argc < 2) { fprintf(out, "usage: flash write \n"); return; } if (!flash) { fprintf(out, "error: no flash device probed.\n"); return; } addr = strtoull(argv[0], NULL, 16); if (errno == EINVAL || errno == ERANGE) { fprintf(out, "error: expected hexadecimal value for addr\n"); return; } len = strtoull(argv[1], NULL, 16); if (errno == EINVAL || errno == ERANGE) { fprintf(out, "error: expected hexadecimal value for len\n"); return; } if (!len) return; while (len) { nbytes = min(len, 256); parse_hex(out, buf, nbytes); printf("\n"); print_hex_ascii(out, addr, buf, nbytes); flash_write(flash, addr, buf, nbytes); addr += nbytes; len -= nbytes; } } static void do_flash_erase(FILE *out, const char **argv, size_t argc) { size_t addr, len; if (argc < 2) { fprintf(out, "usage: flash erase \n"); return; } if (!flash) { fprintf(out, "error: no flash device probed.\n"); return; } addr = strtoull(argv[0], NULL, 16); if (errno == EINVAL || errno == ERANGE) { fprintf(out, "error: expected hexadecimal value for addr\n"); return; } len = strtoull(argv[1], NULL, 16); if (errno == EINVAL || errno == ERANGE) { fprintf(out, "error: expected hexadecimal value for len\n"); return; } if (!len) return; flash_erase(flash, addr, len); } static struct cmd flash_cmds[] = { { "probe", NULL, do_flash_probe }, { "release", NULL, do_flash_release }, { "info", NULL, do_flash_info }, { "read", NULL, do_flash_read }, { "write", NULL, do_flash_write }, { "erase", NULL, do_flash_erase }, { NULL, NULL, NULL }, }; void do_flash_cmd(FILE *out, const char **argv, size_t argc) { if (argc < 1) { fprintf(out, "usage: flash \n"); return; } cmd_exec(flash_cmds, out, argv, argc); }