From 2d40bd4530c2e2ae26663d4d3ea4bd4eae19407d Mon Sep 17 00:00:00 2001 From: Leo C. Date: Tue, 6 Aug 2024 00:45:49 +0200 Subject: add commands type and xxd: print files/dump as hex --- avr/cmd_fat.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/avr/cmd_fat.c b/avr/cmd_fat.c index 18784e0..8c8a308 100644 --- a/avr/cmd_fat.c +++ b/avr/cmd_fat.c @@ -804,6 +804,53 @@ command_ret_t do_rw(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, return CMD_RET_SUCCESS; } +command_ret_t do_dump(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) +{ + FIL File; + FRESULT res; + + if (argc < 2) + return CMD_RET_USAGE; + + bool hexdump = argv[0][0] == 'x'; + + uint8_t *buffer = (uint8_t *) malloc(BUFFER_SIZE); + if (buffer == NULL) + cmd_error(CMD_RET_FAILURE, ENOMEM, NULL); + + res = f_open(&File, argv[1], FA_READ ); + + if (res == FR_OK) { + uint32_t filepos = 0; + uint8_t ch = '\n'; + for (;;) { + unsigned int br; + + res = f_read(&File, buffer, BUFFER_SIZE, &br); + if (br == 0) break; + /* Print the lines. */ + if (hexdump) + dump_ram(buffer, filepos, br, NULL); + else { + for (unsigned int i = 0; i < br; ++i) { + putchar(ch = buffer[i]); + } + } + filepos += br; + } + if (!hexdump && (ch != '\n')) + putchar('\n'); + } + + f_close(&File); + free(buffer); + + if (res) + cmd_error(CMD_RET_FAILURE, res, PSTR("'%s'"), argv[1]); + + return CMD_RET_SUCCESS; +} + /* * command table for fat subcommands */ @@ -912,6 +959,16 @@ CMD_TBL_ITEM( " - Write file to 'path/filename' on logical drive 'd' from RAM\n" " starting at address 'addr'.\n" ), +CMD_TBL_ITEM( + xxd, 2, 1|CTBL_SUBCMDAUTO, do_dump, + "print file as hexdump", + "\n" +), +CMD_TBL_ITEM( + type, 2, 1|CTBL_SUBCMDAUTO, do_dump, + "print file", + "\n" +), CMD_TBL_ITEM( help, CONFIG_SYS_MAXARGS, CTBL_RPT, do_help, -- cgit v1.2.3