]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
add commands type and xxd: print files/dump as hex
authorLeo C. <erbl259-lmu@yahoo.de>
Mon, 5 Aug 2024 22:45:49 +0000 (00:45 +0200)
committerLeo C. <erbl259-lmu@yahoo.de>
Mon, 5 Aug 2024 22:45:49 +0000 (00:45 +0200)
avr/cmd_fat.c

index 18784e0b99ba565ea76b375312d36339d982dd4c..8c8a308d9dd34b1e65e5c730097a35d5c6e39a29 100644 (file)
@@ -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",
+       "<d:/path/filename>\n"
+),
+CMD_TBL_ITEM(
+       type,   2,      1|CTBL_SUBCMDAUTO, do_dump,
+       "print file",
+       "<d:/path/filename>\n"
+),
 
 CMD_TBL_ITEM(
        help,   CONFIG_SYS_MAXARGS,     CTBL_RPT,       do_help,