]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
90af1becf7ae82b212f411e5b376bf0f08907211
2 * (C) Copyright 2014,2016 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
8 * FAT filesystem commands
16 #include "con-utils.h"
17 #include "print-utils.h"
22 #include "getopt-min.h"
24 /* TODO: use memory size test function (cmd_mem.c) */
25 #define MAX_MEMORY (1ul << 19)
26 #define BUFFER_SIZE 512
34 DWORD
get_fattime (void)
40 gmtime_r(&timer
, &tm_timer
);
42 return fatfs_time(&tm_timer
);
46 static bool check_abort(void)
51 printf_P(PSTR("Abort\n"));
57 static const FLASH
char * const FLASH rc_names
[] = {
67 FSTR("INVALID_OBJECT"),
68 FSTR("WRITE_PROTECTED"),
69 FSTR("INVALID_DRIVE"),
71 FSTR("NO_FILE_SYSTEM"),
75 FSTR("NOT_ENOUGH_CORE"),
76 FSTR("TOO_MANY_OPEN_FILES"),
77 FSTR("INVALID_PARAMETER")
81 void put_rc (FRESULT rc
)
84 printf_P(PSTR("rc=%u FR_"), rc
);
85 my_puts_P(rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
86 my_puts_P(PSTR("\n"));
88 printf_P(PSTR("rc=%u FR_%S\n"), rc
,
89 rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
94 void setup_fatfs(void)
96 f_mount(&FatFs0
, "0:", 0);
97 f_mount(&FatFs1
, "1:", 0);
101 static void swirl(void)
103 static const FLASH
char swirlchar
[] = { '-','\\','|','/' };
104 static uint_fast8_t cnt
;
105 static uint32_t tstamp
;
107 if (get_timer(0) > tstamp
) {
108 printf_P(PSTR("\b%c"), swirlchar
[cnt
]);
109 cnt
= (cnt
+1) % ARRAY_SIZE(swirlchar
);
110 tstamp
= get_timer(0) + 250;
114 /* Work register for fs command */
117 WORD AccFiles
, AccDirs
;
123 char *path
, /* Pointer to the working buffer with start path */
124 struct stat_dat_s
*statp
132 res
= f_opendir(&dirs
, path
);
136 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
137 statp
->Finfo
.fname
[0]) {
138 if (_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
140 fn
= statp
->Finfo
.fname
;
141 if (statp
->Finfo
.fattrib
& AM_DIR
) {
144 strcpy(path
+i
+1, fn
);
145 res
= scan_files(path
, statp
);
150 //printf_P(PSTR("%s/%s\n"), path, fn);
152 statp
->AccSize
+= statp
->Finfo
.fsize
;
166 * fatstat path - Show logical drive status
169 command_ret_t
do_stat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
176 struct stat_dat_s statp
;
178 buf
= (char *) malloc(BUFFER_SIZE
);
180 printf_P(PSTR("fat stat: Out of Memory!\n"));
181 return CMD_RET_FAILURE
;
186 res
= f_getfree(path
, &nfreeclst
, &fs
);
190 "Bytes/Cluster: %lu\n"
191 "Number of FATs: %u\n"
192 "Root DIR entries: %u\n"
194 "Number of clusters: %lu\n"
195 "FAT start (lba): %lu\n"
196 "DIR start (lba,cluster): %lu\n"
197 "Data start (lba): %lu\n"),
198 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
199 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
200 fs
->fatbase
, fs
->dirbase
, fs
->database
);
204 res
= f_getlabel(path
, buf
, &serial
);
208 "Volume S/N: %04X-%04X\n"),
209 buf
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
213 my_puts_P(PSTR("\nCounting... "));
214 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
217 res
= scan_files(buf
, &statp
);
220 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
221 "%lu KB total disk space.\n%lu KB available.\n"),
222 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
223 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
231 return CMD_RET_FAILURE
;
233 return CMD_RET_SUCCESS
;
237 * pwd - Print current directory of the current drive.
240 command_ret_t
do_pwd(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
245 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
247 buf
= (char *) malloc(BUFFER_SIZE
);
249 printf_P(PSTR("pwd: Out of Memory!\n"));
251 return CMD_RET_FAILURE
;
254 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
262 return CMD_RET_FAILURE
;
264 return CMD_RET_SUCCESS
;
269 * cd - Change the current/working directory.
272 command_ret_t
do_cd(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
277 (void) cmdtp
; (void) flag
; (void) argc
;
280 arg
= getenv_str(PSTR(ENV_HOME
));
282 printf_P(PSTR("%s: \"%S\" is not set\n"), argv
[0], PSTR(ENV_HOME
));
283 return CMD_RET_FAILURE
;
293 res
= f_chdrive(drv
);
301 return CMD_RET_FAILURE
;
303 return CMD_RET_SUCCESS
;
306 #define MAX_PATHLEN 255
308 /* Remove trailing slashes,
309 * but keep a leading slash (absolute path)
311 void strip_trailing_slash_relpath(char *p
)
315 if (n
>= 2 && (p
[0] & 0x38) == '0' && p
[1] == ':') {
319 if (n
>= 1 && p
[0] == '/') {
323 while (n
-- != 0 && p
[n
] == '/')
327 int print_dirent(FILINFO
*f
)
329 return printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
330 (f
->fattrib
& AM_DIR
) ? 'D' : '-',
331 (f
->fattrib
& AM_RDO
) ? 'R' : '-',
332 (f
->fattrib
& AM_HID
) ? 'H' : '-',
333 (f
->fattrib
& AM_SYS
) ? 'S' : '-',
334 (f
->fattrib
& AM_ARC
) ? 'A' : '-',
335 (f
->fdate
>> 9) + 1980, (f
->fdate
>> 5) & 15, f
->fdate
& 31,
336 (f
->ftime
>> 11), (f
->ftime
>> 5) & 63,
341 * fatls path - Directory listing
344 command_ret_t
do_ls(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
347 DIR Dir
; /* Directory object */
352 (void) cmdtp
; (void) flag
; (void) argc
;
358 strip_trailing_slash_relpath(path
);
360 char *p
= strrchr(path
, '/');
366 if ((*q
++ & 0x38) == '0' && *q
++ == ':')
370 char *pattern
= NULL
;
371 if (strpbrk_P(p
, PSTR("*?")) ||
372 (f_stat(path
, &Finfo
) == FR_OK
&& !(Finfo
.fattrib
& AM_DIR
))) {
376 strip_trailing_slash_relpath(path
);
378 printf_P(PSTR("*: |%s| |%s|\n"), path
? path
: "<NULL>", pattern
? pattern
: "<NULL>");
381 res
= f_findfirst(&Dir
, &Finfo
, path
, pattern
? pattern
: "*"); /* Start to search for files */
382 while (res
== FR_OK
&& Finfo
.fname
[0]) {
383 if (Finfo
.fattrib
& AM_DIR
) {
386 s1
++; p1
+= Finfo
.fsize
;
388 print_dirent(&Finfo
);
391 res
= f_findnext(&Dir
, &Finfo
);
397 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
398 if (f_getfree(path
, (DWORD
*)&p1
, &fs
) == FR_OK
)
399 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
404 return CMD_RET_FAILURE
;
407 return CMD_RET_SUCCESS
;
410 command_ret_t
do_tst(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
412 DIR Dir
; /* Directory object */
417 (void) cmdtp
; (void) flag
;
419 printf_P(PSTR("sizeof DIR: %u, sizeof FIL: %u\n"), sizeof (DIR), sizeof (FILINFO
));
421 char * buf
= (char *) malloc(BUFFER_SIZE
);
423 printf_P(PSTR("pwd: Out of Memory!\n"));
425 return CMD_RET_FAILURE
;
427 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
430 printf_P(PSTR("cwd: |%s|\n"), buf
);
435 return CMD_RET_FAILURE
;
443 printf_P(PSTR("arg: |%s|\n"), path
);
444 printf_P(PSTR("==== f_stat: "));
445 res
= f_stat(path
, &Finfo
);
448 print_dirent(&Finfo
);
451 printf_P(PSTR("==== f_findfirst: "));
452 res
= f_findfirst(&Dir
, &Finfo
, path
, "*"); /* Start to search for files */
455 print_dirent(&Finfo
);
459 printf_P(PSTR("==== f_opendir: "));
460 res
= f_opendir(&Dir
, path
);
464 return CMD_RET_SUCCESS
;
468 FRESULT
mkpath(TCHAR
*path
)
477 res
= f_stat (path
, &fd
)
479 p
= strchr(path
, ':');
480 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
483 while ((q
= strchr(p
, '/')) != NULL
) {
487 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
497 * fatread/write - load binary file to/from a dos filesystem
498 * read <d:/path/filename> <addr> [bytes [pos]]
499 * write <d:/path/filename> <addr> <bytes>
501 command_ret_t
do_rw(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
507 unsigned long bytes_rw
;
509 bool dowrite
= (argv
[0][0] == 'w');
515 (void) cmdtp
; (void) flag
;
517 if (argc
< (dowrite
? 4 : 3))
518 return CMD_RET_USAGE
;
520 addr
= eval_arg(argv
[2], NULL
);
521 if (addr
>= MAX_MEMORY
) {
522 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
523 return CMD_RET_FAILURE
;
526 bytes
= eval_arg(argv
[3], NULL
);
530 pos
= eval_arg(argv
[4], NULL
);
534 if (addr
+ bytes
> MAX_MEMORY
)
535 bytes
= MAX_MEMORY
- addr
;
537 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
538 if (buffer
== NULL
) {
539 printf_P(PSTR("fatstat: Out of Memory!\n"));
541 return CMD_RET_FAILURE
;
545 res
= mkpath(argv
[1]);
548 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
552 res
= f_lseek(&File
, pos
);
555 timer
= get_timer(0);
557 unsigned int cnt
, br
;
559 if (bytes
>= BUFFER_SIZE
) {
561 bytes
-= BUFFER_SIZE
;
563 cnt
= bytes
; bytes
= 0;
566 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
570 z80_read_block(buffer
, addr
, cnt
);
571 z80_bus_cmd(Release
);
572 res
= f_write(&File
, buffer
, cnt
, &br
);
576 res
= f_read(&File
, buffer
, cnt
, &br
);
579 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
583 z80_write_block(buffer
, addr
, br
);
584 z80_bus_cmd(Release
);
591 printf_P(PSTR("Disk full?\n"));
598 FRESULT fr
= f_close(&File
);
601 timer
= get_timer(timer
);
602 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
603 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
611 my_puts_P(PSTR("Bus timeout\n"));
615 return CMD_RET_FAILURE
;
617 return CMD_RET_SUCCESS
;
620 /* ========================================================================== */
624 char *p_end
; /* pointer to NULL at end of path */
625 char p_path
[MAX_PATHLEN
+ 1]; /* pointer to the start of a path */
628 static inline void strip_trailing_slash(PATH_T
*p
)
630 while (p
->p_end
> p
->p_path
&& p
->p_end
[-1] == '/')
635 * Move specified string into path. Convert "" to "." to handle BSD
636 * semantics for a null path. Strip trailing slashes.
639 path_set(PATH_T
*p
, char *string
)
641 if (strlen(string
) > MAX_PATHLEN
) {
642 //err("%s: name too long", string);
646 (void)strcpy(p
->p_path
, string
);
647 p
->p_end
= p
->p_path
+ strlen(p
->p_path
);
649 if (p
->p_path
== p
->p_end
) {
654 strip_trailing_slash(p
);
658 #define R_FLAG (1<<0)
659 #define I_FLAG (1<<1)
660 #define N_FLAG (1<<2)
661 #define F_FLAG (1<<3)
662 #define P_FLAG (1<<4)
663 #define V_FLAG (1<<5)
665 command_ret_t
do_cp(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
668 FRESULT fr
; /* Return value */
669 DIR dj
; /* Directory search object */
670 FILINFO fno
; /* File information */
679 while ((opt
= getopt(argc
, argv
, PSTR("Rrfip"))) != -1) {
699 return CMD_RET_USAGE
;
707 return CMD_RET_USAGE
;
710 /* consume last argument first. */
711 if (!path_set(&to
, argv
[--argc
]))
716 printf_P((PSTR("%s %s -> %s\n", badcp
? : "ERR:" : " ",
717 curr
->fts_path
, to
.p_path
);
720 /* Search a directory for objects and display it */
723 fr
= f_findfirst(&dj
, &fno
, argv
[1], argv
[2]); /* Start to search for files */
725 while (fr
== FR_OK
&& fno
.fname
[0]) { /* Repeat while an item is found */
726 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
727 (fno
.fattrib
& AM_DIR
) ? 'D' : '-',
728 (fno
.fattrib
& AM_RDO
) ? 'R' : '-',
729 (fno
.fattrib
& AM_HID
) ? 'H' : '-',
730 (fno
.fattrib
& AM_SYS
) ? 'S' : '-',
731 (fno
.fattrib
& AM_ARC
) ? 'A' : '-',
732 (fno
.fdate
>> 9) + 1980, (fno
.fdate
>> 5) & 15, fno
.fdate
& 31,
733 (fno
.ftime
>> 11), (fno
.ftime
>> 5) & 63,
734 fno
.fsize
, fno
.fname
);
735 fr
= f_findnext(&dj
, &fno
); /* Search for next item */
740 return CMD_RET_SUCCESS
;
744 cmd_tbl_t cmd_tbl_fat
[] = {
746 stat
, 2, CTBL_RPT
, do_stat
,
747 "Show logical drive status",
751 pwd
, 2, CTBL_RPT
, do_pwd
,
752 "Print name of current/working directory",
757 "Change the current/working directory.",
761 ls
, 2, CTBL_RPT
, do_ls
,
766 tst
, 2, CTBL_DBG
|CTBL_RPT
, do_tst
,
767 "FatFS test function",
772 "load binary file from a dos filesystem",
773 "<d:/path/filename> <addr> [bytes [pos]]\n"
774 " - Load binary file 'path/filename' on logical drive 'd'\n"
775 " to address 'addr' from dos filesystem.\n"
776 " 'pos' gives the file position to start loading from.\n"
777 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
778 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
779 " the load stops on end of file."
783 "write file into a dos filesystem",
784 "<d:/path/filename> <addr> <bytes>\n"
785 " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
786 " starting at address 'addr'.\n"
790 cp
, CONFIG_SYS_MAXARGS
, CTBL_DBG
, do_cp
,
792 "[-R] [-f | -i | -n] [-aprv] source_file target_file\n"
797 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
798 "Print sub command description/usage",
800 " - print brief description of all sub commands\n"
801 "fat help command ...\n"
802 " - print detailed usage of sub cmd 'command'"
805 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
806 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
807 FSTR("Alias for 'help'"),
808 #ifdef CONFIG_SYS_LONGHELP
810 #endif /* CONFIG_SYS_LONGHELP */
812 #ifdef CONFIG_AUTO_COMPLETE
816 /* Mark end of table */
817 CMD_TBL_END(cmd_tbl_fat
)
821 command_ret_t
do_fat(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
823 puts_P(PSTR("Huch?"));
825 return CMD_RET_USAGE
;