]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
cmd_fat.c: reorg
authorLeo C <erbl259-lmu@yahoo.de>
Tue, 10 Apr 2018 09:56:13 +0000 (11:56 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Tue, 10 Apr 2018 10:21:55 +0000 (12:21 +0200)
avr/cmd_fat.c

index 90af1becf7ae82b212f411e5b376bf0f08907211..de484b6b27de8e710a6fef3af31528d341b75a74 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * (C) Copyright 2014,2016 Leo C. <erbl259-lmu@yahoo.de>
+ * (C) Copyright 2014,2016,2018 Leo C. <erbl259-lmu@yahoo.de>
  *
  * SPDX-License-Identifier:    GPL-2.0
  */
 #include "env.h"
 #include "getopt-min.h"
 
-/* TODO: use memory size test function (cmd_mem.c) */
-#define MAX_MEMORY     (1ul << 19)
+/* TODO: use memory size test function (detect_ramsize() in cmd_loadihex.c) */
+/* TODO: detect_ramsize() should be moved to z80-if.c */
+#define MAX_MEMORY     CONFIG_SYS_RAMSIZE_MAX
 #define BUFFER_SIZE    512
 
-
+/*
+ * Multible (fat) partitions per physical drive are not supported,
+ * but we have up to 2 sdcard slots.
+ */
 FATFS FatFs0;
 FATFS FatFs1;
 
-char chur_drv[3];
+void setup_fatfs(void)
+{
+       f_mount(&FatFs0, "0:", 0);
+       f_mount(&FatFs1, "1:", 0);
+}
 
 DWORD get_fattime (void)
 {
@@ -91,13 +99,6 @@ void put_rc (FRESULT rc)
 }
 
 
-void setup_fatfs(void)
-{
-       f_mount(&FatFs0, "0:", 0);
-       f_mount(&FatFs1, "1:", 0);
-}
-
-
 static void swirl(void)
 {
        static const FLASH char swirlchar[] = { '-','\\','|','/' };
@@ -111,139 +112,15 @@ static void swirl(void)
        }
 }
 
-/* Work register for fs command */
-struct stat_dat_s {
-       DWORD AccSize;
-       WORD  AccFiles, AccDirs;
-       FILINFO Finfo;
-};
-
-static
-FRESULT scan_files (
-       char *path,                     /* Pointer to the working buffer with start path */
-       struct stat_dat_s *statp
-)
-{
-       DIR dirs;
-       FRESULT res;
-       int i;
-       char *fn;
-
-       res = f_opendir(&dirs, path);
-       swirl();
-       if (res == FR_OK) {
-               i = strlen(path);
-               while (((res = f_readdir(&dirs, &statp->Finfo)) == FR_OK) &&
-                                       statp->Finfo.fname[0]) {
-                       if (_FS_RPATH && statp->Finfo.fname[0] == '.')
-                               continue;
-                       fn = statp->Finfo.fname;
-                       if (statp->Finfo.fattrib & AM_DIR) {
-                               statp->AccDirs++;
-                               path[i] = '/';
-                               strcpy(path+i+1, fn);
-                               res = scan_files(path, statp);
-                               path[i] = '\0';
-                               if (res != FR_OK)
-                                       break;
-                       } else {
-                               //printf_P(PSTR("%s/%s\n"), path, fn);
-                               statp->AccFiles++;
-                               statp->AccSize += statp->Finfo.fsize;
-                       }
-                       if (check_abort()) {
-                               res = 255;
-                               break;
-                       }
-               }
-       }
-
-       return res;
-}
-
-
-/*
- * fatstat path - Show logical drive status
- *
- */
-command_ret_t do_stat(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
-{
-       FATFS *fs;
-       DWORD nfreeclst;
-       FRESULT res;
-       char *buf;
-       char *path = "";
-       struct stat_dat_s statp;
-
-       buf = (char *) malloc(BUFFER_SIZE);
-       if (buf == NULL) {
-               printf_P(PSTR("fat stat: Out of Memory!\n"));
-               return CMD_RET_FAILURE;
-       }
-
-       if (argc > 1)
-               path = argv[1];
-       res = f_getfree(path, &nfreeclst, &fs);
-       if (!res) {
-               printf_P(PSTR(
-                                       "FAT type:                %u\n"
-                                       "Bytes/Cluster:           %lu\n"
-                                       "Number of FATs:          %u\n"
-                                       "Root DIR entries:        %u\n"
-                                       "Sectors/FAT:             %lu\n"
-                                       "Number of clusters:      %lu\n"
-                                       "FAT start (lba):         %lu\n"
-                                       "DIR start (lba,cluster): %lu\n"
-                                       "Data start (lba):        %lu\n"),
-                               fs->fs_type, (DWORD)fs->csize * 512, fs->n_fats,
-                               fs->n_rootdir, fs->fsize, fs->n_fatent - 2,
-                               fs->fatbase, fs->dirbase, fs->database);
-
-#if _USE_LABEL
-               DWORD serial;
-               res = f_getlabel(path, buf, &serial);
-               if (!res) {
-                       printf_P(PSTR(
-                                               "Volume name:             %s\n"
-                                               "Volume S/N:              %04X-%04X\n"),
-                                       buf, (WORD)(serial >> 16), (WORD)(serial & 0xFFFF));
-               }
-#endif
-               if (!res) {
-                       my_puts_P(PSTR("\nCounting...  "));
-                       statp.AccSize = statp.AccFiles = statp.AccDirs = 0;
-                       strcpy(buf, path);
-
-                       res = scan_files(buf, &statp);
-               }
-               if (!res) {
-                       printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
-                                                "%lu KB total disk space.\n%lu KB available.\n"),
-                                       statp.AccFiles, statp.AccSize, statp.AccDirs,
-                                       (fs->n_fatent - 2) * (fs->csize / 2), nfreeclst * (fs->csize / 2)
-                       );
-               }
-       }
-
-       free(buf);
-       if (res) {
-               put_rc(res);
-               return CMD_RET_FAILURE;
-       }
-       return CMD_RET_SUCCESS;
-}
-
 /*
  * pwd - Print current directory of the current drive.
  *
  */
-command_ret_t do_pwd(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_pwd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
 {
        FRESULT res;
        char *buf;
 
-       (void) cmdtp; (void) flag; (void) argc; (void) argv;
-
        buf = (char *) malloc(BUFFER_SIZE);
        if (buf == NULL) {
                printf_P(PSTR("pwd: Out of Memory!\n"));
@@ -269,13 +146,11 @@ command_ret_t do_pwd(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
  * cd - Change the current/working directory.
  *
  */
-command_ret_t do_cd(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_cd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
 {
        char *arg;
        FRESULT res = 0;
 
-       (void) cmdtp; (void) flag; (void) argc;
-
        if (argc < 2) {
                arg = getenv_str(PSTR(ENV_HOME));
                if (arg == NULL) {
@@ -305,9 +180,10 @@ command_ret_t do_cd(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
 
 #define MAX_PATHLEN 255
 
-       /* Remove trailing slashes,
-        * but keep a leading slash (absolute path)
-        */
+/*
+ * Remove trailing slashes,
+ * but keep a leading slash (absolute path)
+ */
 void strip_trailing_slash_relpath(char *p)
 {
        int n = strlen(p);
@@ -338,10 +214,10 @@ int print_dirent(FILINFO *f)
 }
 
 /*
- * fatls path - Directory listing
+ * ls path - Directory listing
  *
  */
-command_ret_t do_ls(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_ls(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
 {
        FATFS *fs;
        DIR Dir;                                        /* Directory object */
@@ -349,8 +225,6 @@ command_ret_t do_ls(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
        unsigned long p1;
        unsigned int s1, s2;
        FRESULT res;
-       (void) cmdtp; (void) flag; (void) argc;
-
 
        char *path = "";
        if (argc > 1)
@@ -407,15 +281,17 @@ printf_P(PSTR("*: |%s| |%s|\n"), path ? path : "<NULL>", pattern ? pattern : "<N
        return CMD_RET_SUCCESS;
 }
 
-command_ret_t do_tst(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+/*
+ * tst path - for debugging: test access with different functions
+ *
+ */
+command_ret_t do_tst(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
 {
        DIR Dir;                                        /* Directory object */
        FILINFO Finfo;
        FRESULT res = FR_OK;
        char *path;
 
-       (void) cmdtp; (void) flag;
-
        printf_P(PSTR("sizeof DIR: %u, sizeof FIL: %u\n"), sizeof (DIR), sizeof (FILINFO));
 
        char * buf = (char *) malloc(BUFFER_SIZE);
@@ -464,12 +340,12 @@ command_ret_t do_tst(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
        return CMD_RET_SUCCESS;
 }
 
+#if 0
 static
 FRESULT mkpath(TCHAR *path)
 {
        /* TODO: */
        (void) path;
-#if 0
        FILINFO fd
        TCHAR *p, *q;
        FRESULT ret;
@@ -488,17 +364,264 @@ FRESULT mkpath(TCHAR *path)
                                return ret;
                p = q + 1;
        }
-#endif
 
        return FR_OK;
 }
+#endif
+
+/******************************************************************************/
+
+
+typedef struct {
+       char *p_end;                    /* pointer to NULL at end of path */
+       char p_path[MAX_PATHLEN + 1];   /* pointer to the start of a path */
+} PATH_T;
+
+static inline void strip_trailing_slash(PATH_T *p)
+{
+       while (p->p_end > p->p_path && p->p_end[-1] == '/')
+               *--p->p_end = 0;
+}
+
+/*
+ * Move specified string into path.  Convert "" to "." to handle BSD
+ * semantics for a null path.  Strip trailing slashes.
+ */
+int
+path_set(PATH_T *p, char *string)
+{
+       if (strlen(string) > MAX_PATHLEN) {
+               //err("%s: name too long", string);
+               return(0);
+       }
+
+       (void)strcpy(p->p_path, string);
+       p->p_end = p->p_path + strlen(p->p_path);
+
+       if (p->p_path == p->p_end) {
+               *p->p_end++ = '.';
+               *p->p_end = 0;
+       }
+
+       strip_trailing_slash(p);
+       return(1);
+}
+
+#define R_FLAG (1<<0)
+#define I_FLAG (1<<1)
+#define N_FLAG (1<<2)
+#define F_FLAG (1<<3)
+#define P_FLAG (1<<4)
+#define V_FLAG (1<<5)
+
+command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+
+    FRESULT fr;     /* Return value */
+    DIR dj;         /* Directory search object */
+    FILINFO fno;    /* File information */
+
+       uint8_t flags = 0;
+
+
+       /* reset getopt() */
+       optind = 0;
+
+       int opt;
+       while ((opt = getopt(argc, argv, PSTR("Rrfip"))) != -1) {
+               switch (opt) {
+                       case 'f':
+                               flags &= I_FLAG;
+                               break;
+                       case 'i':
+                               flags |= I_FLAG;
+                               flags &= F_FLAG;
+                               break;
+                       case 'p':
+                               flags |= P_FLAG;
+                               break;
+                       case 'R':
+                       case 'r':
+                               flags |= R_FLAG;
+                               break;
+                       case 'v':
+                               flags |= V_FLAG;
+                               break;
+                       default:
+                               return CMD_RET_USAGE;
+                               break;
+               }
+       }
+       argc -= optind;
+       argv += optind;
+
+       if (argc < 2)
+               return CMD_RET_USAGE;
+
+#if 0
+       /* consume last argument first. */
+       if (!path_set(&to, argv[--argc]))
+               exit(1);
+
+
+               if (flags & V_FLAG)
+                       printf_P((PSTR("%s  %s -> %s\n", badcp ? : "ERR:" : "    ", curr->fts_path, to.p_path)));
+
+#endif
+
+/* Search a directory for objects and display it */
+
+
+    fr = f_findfirst(&dj, &fno, argv[1], argv[2]);  /* Start to search for files */
+
+    while (fr == FR_OK && fno.fname[0]) {         /* Repeat while an item is found */
+               printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s\n"),
+                                       (fno.fattrib & AM_DIR) ? 'D' : '-',
+                                       (fno.fattrib & AM_RDO) ? 'R' : '-',
+                                       (fno.fattrib & AM_HID) ? 'H' : '-',
+                                       (fno.fattrib & AM_SYS) ? 'S' : '-',
+                                       (fno.fattrib & AM_ARC) ? 'A' : '-',
+                                       (fno.fdate >> 9) + 1980, (fno.fdate >> 5) & 15, fno.fdate & 31,
+                                       (fno.ftime >> 11), (fno.ftime >> 5) & 63,
+                                       fno.fsize, fno.fname);
+        fr = f_findnext(&dj, &fno);               /* Search for next item */
+    }
+
+    f_closedir(&dj);
+
+       return CMD_RET_SUCCESS;
+}
+
+/******************************************************************************/
+
+/* Work register for fs command */
+struct stat_dat_s {
+       DWORD AccSize;
+       WORD  AccFiles, AccDirs;
+       FILINFO Finfo;
+};
+
+static
+FRESULT scan_files (
+       char *path,                     /* Pointer to the working buffer with start path */
+       struct stat_dat_s *statp
+)
+{
+       DIR dirs;
+       FRESULT res;
+       int i;
+       char *fn;
+
+       res = f_opendir(&dirs, path);
+       swirl();
+       if (res == FR_OK) {
+               i = strlen(path);
+               while (((res = f_readdir(&dirs, &statp->Finfo)) == FR_OK) &&
+                                       statp->Finfo.fname[0]) {
+                       if (_FS_RPATH && statp->Finfo.fname[0] == '.')
+                               continue;
+                       fn = statp->Finfo.fname;
+                       if (statp->Finfo.fattrib & AM_DIR) {
+                               statp->AccDirs++;
+                               path[i] = '/';
+                               strcpy(path+i+1, fn);
+                               res = scan_files(path, statp);
+                               path[i] = '\0';
+                               if (res != FR_OK)
+                                       break;
+                       } else {
+                               //printf_P(PSTR("%s/%s\n"), path, fn);
+                               statp->AccFiles++;
+                               statp->AccSize += statp->Finfo.fsize;
+                       }
+                       if (check_abort()) {
+                               res = 255;
+                               break;
+                       }
+               }
+       }
+
+       return res;
+}
+
+
+/*
+ * fatstat path - Show logical drive status
+ *
+ */
+command_ret_t do_stat(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
+{
+       FATFS *fs;
+       DWORD nfreeclst;
+       FRESULT res;
+       char *buf;
+       char *path = "";
+       struct stat_dat_s statp;
+
+       buf = (char *) malloc(BUFFER_SIZE);
+       if (buf == NULL) {
+               printf_P(PSTR("fat stat: Out of Memory!\n"));
+               return CMD_RET_FAILURE;
+       }
+
+       if (argc > 1)
+               path = argv[1];
+       res = f_getfree(path, &nfreeclst, &fs);
+       if (!res) {
+               printf_P(PSTR(
+                                       "FAT type:                %u\n"
+                                       "Bytes/Cluster:           %lu\n"
+                                       "Number of FATs:          %u\n"
+                                       "Root DIR entries:        %u\n"
+                                       "Sectors/FAT:             %lu\n"
+                                       "Number of clusters:      %lu\n"
+                                       "FAT start (lba):         %lu\n"
+                                       "DIR start (lba,cluster): %lu\n"
+                                       "Data start (lba):        %lu\n"),
+                               fs->fs_type, (DWORD)fs->csize * 512, fs->n_fats,
+                               fs->n_rootdir, fs->fsize, fs->n_fatent - 2,
+                               fs->fatbase, fs->dirbase, fs->database);
+
+#if _USE_LABEL
+               DWORD serial;
+               res = f_getlabel(path, buf, &serial);
+               if (!res) {
+                       printf_P(PSTR(
+                                               "Volume name:             %s\n"
+                                               "Volume S/N:              %04X-%04X\n"),
+                                       buf, (WORD)(serial >> 16), (WORD)(serial & 0xFFFF));
+               }
+#endif
+               if (!res) {
+                       my_puts_P(PSTR("\nCounting...  "));
+                       statp.AccSize = statp.AccFiles = statp.AccDirs = 0;
+                       strcpy(buf, path);
+
+                       res = scan_files(buf, &statp);
+               }
+               if (!res) {
+                       printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
+                                                "%lu KB total disk space.\n%lu KB available.\n"),
+                                       statp.AccFiles, statp.AccSize, statp.AccDirs,
+                                       (fs->n_fatent - 2) * (fs->csize / 2), nfreeclst * (fs->csize / 2)
+                       );
+               }
+       }
+
+       free(buf);
+       if (res) {
+               put_rc(res);
+               return CMD_RET_FAILURE;
+       }
+       return CMD_RET_SUCCESS;
+}
 
 /*
  * fatread/write - load binary file to/from a dos filesystem
  *             read  <d:/path/filename> <addr> [bytes [pos]]
  *             write <d:/path/filename> <addr> <bytes>
  */
-command_ret_t do_rw(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_rw(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
 {
        FIL File;
        uint32_t addr;
@@ -512,8 +635,6 @@ command_ret_t do_rw(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
        uint32_t timer;
        uint8_t *buffer;
 
-       (void) cmdtp; (void) flag;
-
        if (argc < (dowrite ? 4 : 3))
                return CMD_RET_USAGE;
 
@@ -541,9 +662,6 @@ command_ret_t do_rw(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
                return CMD_RET_FAILURE;
        }
 
-       if (dowrite) {
-               res = mkpath(argv[1]);
-       }
        if (!res) {
                res = f_open(&File, argv[1], dowrite ? FA_WRITE | FA_CREATE_ALWAYS
                                                                                         : FA_READ );
@@ -617,129 +735,9 @@ command_ret_t do_rw(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const
        return CMD_RET_SUCCESS;
 }
 
-/* ========================================================================== */
-
-
-typedef struct {
-       char *p_end;                    /* pointer to NULL at end of path */
-       char p_path[MAX_PATHLEN + 1];   /* pointer to the start of a path */
-} PATH_T;
-
-static inline void strip_trailing_slash(PATH_T *p)
-{
-       while (p->p_end > p->p_path && p->p_end[-1] == '/')
-               *--p->p_end = 0;
-}
-
 /*
- * Move specified string into path.  Convert "" to "." to handle BSD
- * semantics for a null path.  Strip trailing slashes.
+ * command table for fat subcommands
  */
-int
-path_set(PATH_T *p, char *string)
-{
-       if (strlen(string) > MAX_PATHLEN) {
-               //err("%s: name too long", string);
-               return(0);
-       }
-
-       (void)strcpy(p->p_path, string);
-       p->p_end = p->p_path + strlen(p->p_path);
-
-       if (p->p_path == p->p_end) {
-               *p->p_end++ = '.';
-               *p->p_end = 0;
-       }
-
-       strip_trailing_slash(p);
-       return(1);
-}
-
-#define R_FLAG (1<<0)
-#define I_FLAG (1<<1)
-#define N_FLAG (1<<2)
-#define F_FLAG (1<<3)
-#define P_FLAG (1<<4)
-#define V_FLAG (1<<5)
-
-command_ret_t do_cp(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
-{
-
-    FRESULT fr;     /* Return value */
-    DIR dj;         /* Directory search object */
-    FILINFO fno;    /* File information */
-
-       uint8_t flags = 0;
-
-
-       /* reset getopt() */
-       optind = 0;
-
-       int opt;
-       while ((opt = getopt(argc, argv, PSTR("Rrfip"))) != -1) {
-       switch (opt) {
-               case 'f':
-                       flags &= I_FLAG;
-                       break;
-               case 'i':
-                       flags |= I_FLAG;
-                       flags &= F_FLAG;
-                       break;
-               case 'p':
-                       flags |= P_FLAG;
-                       break;
-               case 'R':
-               case 'r':
-                       flags |= R_FLAG;
-                       break;
-               case 'v':
-                       flags |= V_FLAG;
-                       break;
-               default:
-                       return CMD_RET_USAGE;
-                       break;
-               }
-       }
-       argc -= optind;
-       argv += optind;
-
-       if (argc < 2)
-               return CMD_RET_USAGE;
-
-#if 0
-       /* consume last argument first. */
-       if (!path_set(&to, argv[--argc]))
-               exit(1);
-
-
-               if (flags & V_FLAG)
-                       printf_P((PSTR("%s  %s -> %s\n", badcp ? : "ERR:" : "    ",
-                                       curr->fts_path, to.p_path);
-#endif
-
-/* Search a directory for objects and display it */
-
-
-    fr = f_findfirst(&dj, &fno, argv[1], argv[2]);  /* Start to search for files */
-
-    while (fr == FR_OK && fno.fname[0]) {         /* Repeat while an item is found */
-               printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s\n"),
-                                       (fno.fattrib & AM_DIR) ? 'D' : '-',
-                                       (fno.fattrib & AM_RDO) ? 'R' : '-',
-                                       (fno.fattrib & AM_HID) ? 'H' : '-',
-                                       (fno.fattrib & AM_SYS) ? 'S' : '-',
-                                       (fno.fattrib & AM_ARC) ? 'A' : '-',
-                                       (fno.fdate >> 9) + 1980, (fno.fdate >> 5) & 15, fno.fdate & 31,
-                                       (fno.ftime >> 11), (fno.ftime >> 5) & 63,
-                                       fno.fsize, fno.fname);
-        fr = f_findnext(&dj, &fno);               /* Search for next item */
-    }
-
-    f_closedir(&dj);
-
-       return CMD_RET_SUCCESS;
-}
-
 
 cmd_tbl_t cmd_tbl_fat[] = {
 CMD_TBL_ITEM(
@@ -818,7 +816,7 @@ CMD_TBL_END(cmd_tbl_fat)
 };
 
 
-command_ret_t do_fat(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
+command_ret_t do_fat(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
 {
        puts_P(PSTR("Huch?"));