int cmd_xd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { FAR char *addr; FAR char *endptr; int nbytes; addr = (char*)((uintptr_t)strtol(argv[1], &endptr, 16)); if (argv[0][0] == '\0' || *endptr != '\0') { return ERROR; } nbytes = (int)strtol(argv[2], &endptr, 0); if (argv[0][0] == '\0' || *endptr != '\0' || nbytes < 0) { return ERROR; } nsh_dumpbuffer(vtbl, "Hex dump", (uint8_t*)addr, nbytes); return OK; }
int cmd_hexdump(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) { uint8_t buffer[IOBUFFERSIZE]; char msg[32]; off_t position; int fd; int ret = OK; #ifdef CONFIG_NSH_CMDOPT_HEXDUMP off_t skip = 0; off_t count = 0xfffffff; off_t dumpbytes; int x; #endif /* Open the file for reading */ fd = open(argv[1], O_RDONLY); if (fd < 0) { nsh_output(vtbl, g_fmtcmdfailed, "hexdump", "open", NSH_ERRNO); return ERROR; } #ifdef CONFIG_NSH_CMDOPT_HEXDUMP for (x = 2; x < argc; x++) { if (strncmp(argv[x], "skip=", 5) == 0) { skip = atoi(&argv[x][5]); } else if (strncmp(argv[x], "count=", 6) == 0) { count = atoi(&argv[x][6]); } } #endif position = 0; for (;;) { int nbytesread = read(fd, buffer, IOBUFFERSIZE); /* Check for read errors */ if (nbytesread < 0) { int errval = errno; nsh_output(vtbl, g_fmtcmdfailed, "hexdump", "read", NSH_ERRNO_OF(errval)); ret = ERROR; break; } else if (nbytesread > 0) { #ifdef CONFIG_NSH_CMDOPT_HEXDUMP if (position < skip) { /* Skip bytes until we reach the skip point */ position += nbytesread; if (position > skip) { dumpbytes = position - skip; if (dumpbytes > count) { dumpbytes = count; } snprintf(msg, sizeof(msg), "%s at %08x", argv[1], skip); nsh_dumpbuffer(vtbl, msg, &buffer[nbytesread - (position-skip)], dumpbytes); if (count > dumpbytes) { count -= dumpbytes; } else { break; } } /* Don't print if we are in skip mode */ continue; } /* Limit dumpbuffer to count if less than a full buffer needed */ if (nbytesread > count) { nbytesread = count; } #endif snprintf(msg, sizeof(msg), "%s at %08x", argv[1], position); nsh_dumpbuffer(vtbl, msg, buffer, nbytesread); position += nbytesread; #ifdef CONFIG_NSH_CMDOPT_HEXDUMP if (count > nbytesread) { count -= nbytesread; } else { break; } #endif } else { break; // EOF } } (void)close(fd); return ret; }