]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
2 * (C) Copyright 2014,2016,2018 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 (detect_ramsize() in cmd_loadihex.c) */
25 /* TODO: detect_ramsize() should be moved to z80-if.c */
26 #define MAX_MEMORY CONFIG_SYS_RAMSIZE_MAX
27 #define BUFFER_SIZE 512
30 * Multible (fat) partitions per physical drive are not supported,
31 * but we have up to 2 sdcard slots.
36 void setup_fatfs(void)
38 f_mount(&FatFs0
, "0:", 0);
39 f_mount(&FatFs1
, "1:", 0);
42 DWORD
get_fattime (void)
48 gmtime_r(&timer
, &tm_timer
);
50 return fatfs_time(&tm_timer
);
54 static bool check_abort(void)
59 printf_P(PSTR("Abort\n"));
65 static const FLASH
char * const FLASH rc_names
[] = {
75 FSTR("INVALID_OBJECT"),
76 FSTR("WRITE_PROTECTED"),
77 FSTR("INVALID_DRIVE"),
79 FSTR("NO_FILE_SYSTEM"),
83 FSTR("NOT_ENOUGH_CORE"),
84 FSTR("TOO_MANY_OPEN_FILES"),
85 FSTR("INVALID_PARAMETER")
89 void put_rc (FRESULT rc
)
92 printf_P(PSTR("rc=%u FR_"), rc
);
93 my_puts_P(rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
94 my_puts_P(PSTR("\n"));
96 printf_P(PSTR("rc=%u FR_%S\n"), rc
,
97 rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
102 static void swirl(void)
104 static const FLASH
char swirlchar
[] = { '-','\\','|','/' };
105 static uint_fast8_t cnt
;
106 static uint32_t tstamp
;
108 if (get_timer(0) > tstamp
) {
109 printf_P(PSTR("\b%c"), swirlchar
[cnt
]);
110 cnt
= (cnt
+1) % ARRAY_SIZE(swirlchar
);
111 tstamp
= get_timer(0) + 250;
116 * pwd - Print current directory of the current drive.
119 command_ret_t
do_pwd(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
124 buf
= (char *) malloc(BUFFER_SIZE
);
126 printf_P(PSTR("pwd: Out of Memory!\n"));
128 return CMD_RET_FAILURE
;
131 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
139 return CMD_RET_FAILURE
;
141 return CMD_RET_SUCCESS
;
146 * cd - Change the current/working directory.
149 command_ret_t
do_cd(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
155 arg
= getenv_str(PSTR(ENV_HOME
));
157 printf_P(PSTR("%s: \"%S\" is not set\n"), argv
[0], PSTR(ENV_HOME
));
158 return CMD_RET_FAILURE
;
168 res
= f_chdrive(drv
);
176 return CMD_RET_FAILURE
;
178 return CMD_RET_SUCCESS
;
181 #define MAX_PATHLEN 255
184 * Remove trailing slashes,
185 * but keep a leading slash (absolute path)
187 void strip_trailing_slash_relpath(char *p
)
191 if (n
>= 2 && (p
[0] & 0x38) == '0' && p
[1] == ':') {
195 if (n
>= 1 && p
[0] == '/') {
199 while (n
-- != 0 && p
[n
] == '/')
203 int print_dirent(FILINFO
*f
)
205 return printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
206 (f
->fattrib
& AM_DIR
) ? 'D' : '-',
207 (f
->fattrib
& AM_RDO
) ? 'R' : '-',
208 (f
->fattrib
& AM_HID
) ? 'H' : '-',
209 (f
->fattrib
& AM_SYS
) ? 'S' : '-',
210 (f
->fattrib
& AM_ARC
) ? 'A' : '-',
211 (f
->fdate
>> 9) + 1980, (f
->fdate
>> 5) & 15, f
->fdate
& 31,
212 (f
->ftime
>> 11), (f
->ftime
>> 5) & 63,
217 * ls path - Directory listing
220 command_ret_t
do_ls(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
223 DIR Dir
; /* Directory object */
232 strip_trailing_slash_relpath(path
);
234 char *p
= strrchr(path
, '/');
240 if ((*q
++ & 0x38) == '0' && *q
++ == ':')
244 char *pattern
= NULL
;
245 if (strpbrk_P(p
, PSTR("*?")) ||
246 (f_stat(path
, &Finfo
) == FR_OK
&& !(Finfo
.fattrib
& AM_DIR
))) {
250 strip_trailing_slash_relpath(path
);
252 printf_P(PSTR("*: |%s| |%s|\n"), path
? path
: "<NULL>", pattern
? pattern
: "<NULL>");
255 res
= f_findfirst(&Dir
, &Finfo
, path
, pattern
? pattern
: "*"); /* Start to search for files */
256 while (res
== FR_OK
&& Finfo
.fname
[0]) {
257 if (Finfo
.fattrib
& AM_DIR
) {
260 s1
++; p1
+= Finfo
.fsize
;
262 print_dirent(&Finfo
);
265 res
= f_findnext(&Dir
, &Finfo
);
271 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
272 if (f_getfree(path
, (DWORD
*)&p1
, &fs
) == FR_OK
)
273 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
278 return CMD_RET_FAILURE
;
281 return CMD_RET_SUCCESS
;
285 * tst path - for debugging: test access with different functions
288 command_ret_t
do_tst(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
290 DIR Dir
; /* Directory object */
295 printf_P(PSTR("sizeof DIR: %u, sizeof FIL: %u\n"), sizeof (DIR), sizeof (FILINFO
));
297 char * buf
= (char *) malloc(BUFFER_SIZE
);
299 printf_P(PSTR("pwd: Out of Memory!\n"));
301 return CMD_RET_FAILURE
;
303 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
306 printf_P(PSTR("cwd: |%s|\n"), buf
);
311 return CMD_RET_FAILURE
;
319 printf_P(PSTR("arg: |%s|\n"), path
);
320 printf_P(PSTR("==== f_stat: "));
321 res
= f_stat(path
, &Finfo
);
324 print_dirent(&Finfo
);
327 printf_P(PSTR("==== f_findfirst: "));
328 res
= f_findfirst(&Dir
, &Finfo
, path
, "*"); /* Start to search for files */
331 print_dirent(&Finfo
);
335 printf_P(PSTR("==== f_opendir: "));
336 res
= f_opendir(&Dir
, path
);
340 return CMD_RET_SUCCESS
;
345 FRESULT
mkpath(TCHAR
*path
)
353 res
= f_stat (path
, &fd
)
355 p
= strchr(path
, ':');
356 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
359 while ((q
= strchr(p
, '/')) != NULL
) {
363 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
372 /******************************************************************************/
376 char *p_end
; /* pointer to NULL at end of path */
377 char p_path
[MAX_PATHLEN
+ 1]; /* pointer to the start of a path */
380 static inline void strip_trailing_slash(PATH_T
*p
)
382 while (p
->p_end
> p
->p_path
&& p
->p_end
[-1] == '/')
387 * Move specified string into path. Convert "" to "." to handle BSD
388 * semantics for a null path. Strip trailing slashes.
391 path_set(PATH_T
*p
, char *string
)
393 if (strlen(string
) > MAX_PATHLEN
) {
394 //err("%s: name too long", string);
398 (void)strcpy(p
->p_path
, string
);
399 p
->p_end
= p
->p_path
+ strlen(p
->p_path
);
401 if (p
->p_path
== p
->p_end
) {
406 strip_trailing_slash(p
);
410 #define R_FLAG (1<<0)
411 #define I_FLAG (1<<1)
412 #define N_FLAG (1<<2)
413 #define F_FLAG (1<<3)
414 #define P_FLAG (1<<4)
415 #define V_FLAG (1<<5)
417 command_ret_t
do_cp(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
420 FRESULT fr
; /* Return value */
421 DIR dj
; /* Directory search object */
422 FILINFO fno
; /* File information */
431 while ((opt
= getopt(argc
, argv
, PSTR("Rrfip"))) != -1) {
451 return CMD_RET_USAGE
;
459 return CMD_RET_USAGE
;
462 /* consume last argument first. */
463 if (!path_set(&to
, argv
[--argc
]))
468 printf_P((PSTR("%s %s -> %s\n", badcp
? : "ERR:" : " ", curr
->fts_path
, to
.p_path
)));
472 /* Search a directory for objects and display it */
475 fr
= f_findfirst(&dj
, &fno
, argv
[1], argv
[2]); /* Start to search for files */
477 while (fr
== FR_OK
&& fno
.fname
[0]) { /* Repeat while an item is found */
478 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
479 (fno
.fattrib
& AM_DIR
) ? 'D' : '-',
480 (fno
.fattrib
& AM_RDO
) ? 'R' : '-',
481 (fno
.fattrib
& AM_HID
) ? 'H' : '-',
482 (fno
.fattrib
& AM_SYS
) ? 'S' : '-',
483 (fno
.fattrib
& AM_ARC
) ? 'A' : '-',
484 (fno
.fdate
>> 9) + 1980, (fno
.fdate
>> 5) & 15, fno
.fdate
& 31,
485 (fno
.ftime
>> 11), (fno
.ftime
>> 5) & 63,
486 fno
.fsize
, fno
.fname
);
487 fr
= f_findnext(&dj
, &fno
); /* Search for next item */
492 return CMD_RET_SUCCESS
;
495 /******************************************************************************/
497 /* Work register for fs command */
500 WORD AccFiles
, AccDirs
;
506 char *path
, /* Pointer to the working buffer with start path */
507 struct stat_dat_s
*statp
515 res
= f_opendir(&dirs
, path
);
519 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
520 statp
->Finfo
.fname
[0]) {
521 if (_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
523 fn
= statp
->Finfo
.fname
;
524 if (statp
->Finfo
.fattrib
& AM_DIR
) {
527 strcpy(path
+i
+1, fn
);
528 res
= scan_files(path
, statp
);
533 //printf_P(PSTR("%s/%s\n"), path, fn);
535 statp
->AccSize
+= statp
->Finfo
.fsize
;
549 * fatstat path - Show logical drive status
552 command_ret_t
do_stat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
559 struct stat_dat_s statp
;
561 buf
= (char *) malloc(BUFFER_SIZE
);
563 printf_P(PSTR("fat stat: Out of Memory!\n"));
564 return CMD_RET_FAILURE
;
569 res
= f_getfree(path
, &nfreeclst
, &fs
);
573 "Bytes/Cluster: %lu\n"
574 "Number of FATs: %u\n"
575 "Root DIR entries: %u\n"
577 "Number of clusters: %lu\n"
578 "FAT start (lba): %lu\n"
579 "DIR start (lba,cluster): %lu\n"
580 "Data start (lba): %lu\n"),
581 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
582 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
583 fs
->fatbase
, fs
->dirbase
, fs
->database
);
587 res
= f_getlabel(path
, buf
, &serial
);
591 "Volume S/N: %04X-%04X\n"),
592 buf
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
596 my_puts_P(PSTR("\nCounting... "));
597 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
600 res
= scan_files(buf
, &statp
);
603 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
604 "%lu KB total disk space.\n%lu KB available.\n"),
605 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
606 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
614 return CMD_RET_FAILURE
;
616 return CMD_RET_SUCCESS
;
620 * fatread/write - load binary file to/from a dos filesystem
621 * read <d:/path/filename> <addr> [bytes [pos]]
622 * write <d:/path/filename> <addr> <bytes>
624 command_ret_t
do_rw(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
630 unsigned long bytes_rw
;
632 bool dowrite
= (argv
[0][0] == 'w');
638 if (argc
< (dowrite
? 4 : 3))
639 return CMD_RET_USAGE
;
641 addr
= eval_arg(argv
[2], NULL
);
642 if (addr
>= MAX_MEMORY
) {
643 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
644 return CMD_RET_FAILURE
;
647 bytes
= eval_arg(argv
[3], NULL
);
651 pos
= eval_arg(argv
[4], NULL
);
655 if (addr
+ bytes
> MAX_MEMORY
)
656 bytes
= MAX_MEMORY
- addr
;
658 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
659 if (buffer
== NULL
) {
660 printf_P(PSTR("fatstat: Out of Memory!\n"));
662 return CMD_RET_FAILURE
;
666 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
670 res
= f_lseek(&File
, pos
);
673 timer
= get_timer(0);
675 unsigned int cnt
, br
;
677 if (bytes
>= BUFFER_SIZE
) {
679 bytes
-= BUFFER_SIZE
;
681 cnt
= bytes
; bytes
= 0;
684 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
688 z80_read_block(buffer
, addr
, cnt
);
689 z80_bus_cmd(Release
);
690 res
= f_write(&File
, buffer
, cnt
, &br
);
694 res
= f_read(&File
, buffer
, cnt
, &br
);
697 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
701 z80_write_block(buffer
, addr
, br
);
702 z80_bus_cmd(Release
);
709 printf_P(PSTR("Disk full?\n"));
716 FRESULT fr
= f_close(&File
);
719 timer
= get_timer(timer
);
720 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
721 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
729 my_puts_P(PSTR("Bus timeout\n"));
733 return CMD_RET_FAILURE
;
735 return CMD_RET_SUCCESS
;
739 * command table for fat subcommands
742 cmd_tbl_t cmd_tbl_fat
[] = {
744 stat
, 2, CTBL_RPT
, do_stat
,
745 "Show logical drive status",
749 pwd
, 2, CTBL_RPT
, do_pwd
,
750 "Print name of current/working directory",
755 "Change the current/working directory.",
759 ls
, 2, CTBL_RPT
, do_ls
,
764 tst
, 2, CTBL_DBG
|CTBL_RPT
, do_tst
,
765 "FatFS test function",
770 "load binary file from a dos filesystem",
771 "<d:/path/filename> <addr> [bytes [pos]]\n"
772 " - Load binary file 'path/filename' on logical drive 'd'\n"
773 " to address 'addr' from dos filesystem.\n"
774 " 'pos' gives the file position to start loading from.\n"
775 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
776 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
777 " the load stops on end of file."
781 "write file into a dos filesystem",
782 "<d:/path/filename> <addr> <bytes>\n"
783 " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
784 " starting at address 'addr'.\n"
788 cp
, CONFIG_SYS_MAXARGS
, CTBL_DBG
, do_cp
,
790 "[-R] [-f | -i | -n] [-aprv] source_file target_file\n"
795 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
796 "Print sub command description/usage",
798 " - print brief description of all sub commands\n"
799 "fat help command ...\n"
800 " - print detailed usage of sub cmd 'command'"
803 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
804 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
805 FSTR("Alias for 'help'"),
806 #ifdef CONFIG_SYS_LONGHELP
808 #endif /* CONFIG_SYS_LONGHELP */
810 #ifdef CONFIG_AUTO_COMPLETE
814 /* Mark end of table */
815 CMD_TBL_END(cmd_tbl_fat
)
819 command_ret_t
do_fat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
821 puts_P(PSTR("Huch?"));
823 return CMD_RET_USAGE
;