2 * (C) Copyright 2014,2016,2018 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
8 * FAT filesystem commands
12 #include <util/delay.h>
17 #include "con-utils.h"
18 #include "print-utils.h"
23 #include "getopt-min.h"
25 /* TODO: use memory size test function (detect_ramsize() in cmd_loadihex.c) */
26 /* TODO: detect_ramsize() should be moved to z80-if.c */
27 #define MAX_MEMORY CONFIG_SYS_RAMSIZE_MAX
28 #define BUFFER_SIZE 512
29 #define MAXBSIZE 512 /* TODO */
33 * Multible (fat) partitions per physical drive are not supported,
34 * but we have up to 2 sdcard slots.
39 command_ret_t command_ret
;
41 void setup_fatfs(void)
43 f_mount(&FatFs0
, "0:", 0);
44 f_mount(&FatFs1
, "1:", 0);
47 DWORD
get_fattime (void)
53 gmtime_r(&timer
, &tm_timer
);
55 return fatfs_time(&tm_timer
);
59 static bool check_abort(void)
64 printf_P(PSTR("Abort\n"));
70 static const FLASH
char * const FLASH rc_strings
[] = {
73 FSTR("internal error"),
80 FSTR("invalid object"),
81 FSTR("write protected"),
82 FSTR("invalid drive"),
84 FSTR("no file system"),
88 FSTR("not enough core"),
89 FSTR("too many open files"),
90 FSTR("invalid parameter")
93 static const FLASH
char * const FLASH rc_names
[] = {
100 FSTR("INVALID_NAME"),
103 FSTR("INVALID_OBJECT"),
104 FSTR("WRITE_PROTECTED"),
105 FSTR("INVALID_DRIVE"),
107 FSTR("NO_FILE_SYSTEM"),
108 FSTR("MKFS_ABORTED"),
111 FSTR("NOT_ENOUGH_CORE"),
112 FSTR("TOO_MANY_OPEN_FILES"),
113 FSTR("INVALID_PARAMETER")
117 void put_rc (FRESULT rc
)
120 printf_P(PSTR("rc=%u FR_"), rc
);
121 my_puts_P(rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
122 my_puts_P(PSTR("\n"));
124 printf_P(PSTR("rc=%u FR_%S\n"), rc
,
125 rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
129 const FLASH
char * rctostr(FRESULT rc
)
131 return rc
< ARRAY_SIZE(rc_strings
) ? rc_strings
[rc
] : PSTR(" Unknown Error");
134 void err(const char *fmt
, ...)
138 // (void)fprintf(stderr, "%s: ", progname);
139 (void)vfprintf_P(stdout
, fmt
, ap
);
141 (void)printf_P(PSTR("\n"));
143 command_ret
= CMD_RET_FAILURE
;
148 static void swirl(void)
150 static const FLASH
char swirlchar
[] = { '-','\\','|','/' };
151 static uint_fast8_t cnt
;
152 static uint32_t tstamp
;
154 if (get_timer(0) > tstamp
) {
155 printf_P(PSTR("\b%c"), swirlchar
[cnt
]);
156 cnt
= (cnt
+1) % ARRAY_SIZE(swirlchar
);
157 tstamp
= get_timer(0) + 250;
162 * pwd - Print current directory of the current drive.
165 command_ret_t
do_pwd(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
170 buf
= (char *) malloc(BUFFER_SIZE
);
172 printf_P(PSTR("pwd: Out of Memory!\n"));
174 return CMD_RET_FAILURE
;
177 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
185 return CMD_RET_FAILURE
;
187 return CMD_RET_SUCCESS
;
192 * cd - Change the current/working directory.
195 command_ret_t
do_cd(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
201 arg
= getenv_str(PSTR(ENV_HOME
));
203 printf_P(PSTR("%s: \"%S\" is not set\n"), argv
[0], PSTR(ENV_HOME
));
204 return CMD_RET_FAILURE
;
214 res
= f_chdrive(drv
);
222 return CMD_RET_FAILURE
;
224 return CMD_RET_SUCCESS
;
227 #define MAX_PATHLEN CONFIG_SYS_MAX_PATHLEN
230 * Remove trailing slashes,
231 * but keep a leading slash (absolute path)
233 void strip_trailing_slash_relpath(char *p
)
237 if (n
>= 2 && (p
[0] & 0x38) == '0' && p
[1] == ':') {
241 if (n
>= 1 && p
[0] == '/') {
245 while (n
-- != 0 && p
[n
] == '/')
249 int print_dirent(FILINFO
*f
)
251 return printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
252 (f
->fattrib
& AM_DIR
) ? 'D' : '-',
253 (f
->fattrib
& AM_RDO
) ? 'R' : '-',
254 (f
->fattrib
& AM_HID
) ? 'H' : '-',
255 (f
->fattrib
& AM_SYS
) ? 'S' : '-',
256 (f
->fattrib
& AM_ARC
) ? 'A' : '-',
257 (f
->fdate
>> 9) + 1980, (f
->fdate
>> 5) & 15, f
->fdate
& 31,
258 (f
->ftime
>> 11), (f
->ftime
>> 5) & 63,
263 * ls path - Directory listing
266 command_ret_t
do_ls(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
269 DIR Dir
; /* Directory object */
278 strip_trailing_slash_relpath(path
);
280 char *p
= strrchr(path
, '/');
286 if ((*q
++ & 0x38) == '0' && *q
++ == ':')
291 if (strpbrk_P(p
, PSTR("*?")) ||
292 (f_stat(path
, &Finfo
) == FR_OK
&& !(Finfo
.fattrib
& AM_DIR
))) {
296 pattern
= strdup("*");
297 strip_trailing_slash_relpath(path
);
299 //printf_P(PSTR("*: |%s| |%s|\n"), path ? path : "<NULL>", pattern ? pattern : "<NULL>");
302 res
= f_findfirst(&Dir
, &Finfo
, path
, pattern
); /* Start to search for files */
303 while (res
== FR_OK
&& Finfo
.fname
[0]) {
304 if (Finfo
.fattrib
& AM_DIR
) {
307 s1
++; p1
+= Finfo
.fsize
;
309 print_dirent(&Finfo
);
312 res
= f_findnext(&Dir
, &Finfo
);
318 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
319 if (f_getfree(path
, (DWORD
*)&p1
, &fs
) == FR_OK
)
320 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
325 return CMD_RET_FAILURE
;
328 return CMD_RET_SUCCESS
;
332 * tst path - for debugging: test access with different functions
335 command_ret_t
do_tst(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
337 DIR Dir
; /* Directory object */
342 printf_P(PSTR("sizeof DIR: %u, sizeof FIL: %u\n"), sizeof (DIR), sizeof (FILINFO
));
344 char * buf
= (char *) malloc(BUFFER_SIZE
);
346 printf_P(PSTR("pwd: Out of Memory!\n"));
348 return CMD_RET_FAILURE
;
350 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
353 printf_P(PSTR("cwd: |%s|\n"), buf
);
358 return CMD_RET_FAILURE
;
366 printf_P(PSTR("arg: |%s|\n"), path
);
367 printf_P(PSTR("==== f_stat: "));
368 res
= f_stat(path
, &Finfo
);
371 print_dirent(&Finfo
);
374 printf_P(PSTR("==== f_findfirst: "));
375 res
= f_findfirst(&Dir
, &Finfo
, path
, "*"); /* Start to search for files */
378 print_dirent(&Finfo
);
382 printf_P(PSTR("==== f_opendir: "));
383 res
= f_opendir(&Dir
, path
);
387 return CMD_RET_SUCCESS
;
392 FRESULT
mkpath(TCHAR
*path
)
400 res
= f_stat (path
, &fd
)
402 p
= strchr(path
, ':');
403 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
406 while ((q
= strchr(p
, '/')) != NULL
) {
410 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
419 /******************************************************************************/
422 * These functions manipulate paths in PATH_T structures.
424 * They eliminate multiple slashes in paths when they notice them,
425 * and keep the path non-slash terminated.
427 * Both path_set() and path_append() return 0 if the requested name
433 char *p_end
; /* pointer to NULL at end of path */
434 char p_path
[MAX_PATHLEN
+ 1]; /* pointer to the start of a path */
437 static void strip_trailing_slash(PATH_T
*p
)
439 while (p
->p_end
> p
->p_path
&& p
->p_end
[-1] == '/')
444 * Move specified string into path. Convert "" to "." to handle BSD
445 * semantics for a null path. Strip trailing slashes.
448 path_set(PATH_T
*p
, char *string
)
450 if (strlen(string
) > MAX_PATHLEN
) {
451 err(PSTR("set: '%s': name too long"), string
);
455 (void)strcpy(p
->p_path
, string
);
456 p
->p_end
= p
->p_path
+ strlen(p
->p_path
);
458 if (p
->p_path
== p
->p_end
) {
463 strip_trailing_slash(p
);
468 * Append specified string to path, inserting '/' if necessary. Return a
469 * pointer to the old end of path for restoration.
472 path_append(PATH_T
*p
, char *name
, int len
)
480 /* The "+ 1" accounts for the '/' between old path and name. */
481 if ((len
+ p
->p_end
- p
->p_path
+ 1) > MAX_PATHLEN
) {
482 err(PSTR("append: '%s/%s': name too long"), p
->p_path
, name
);
487 * This code should always be executed, since paths shouldn't
490 if (p
->p_end
[-1] != '/') {
495 (void)strncat(p
->p_end
, name
, len
);
499 strip_trailing_slash(p
);
504 * Restore path to previous value. (As returned by path_append.)
507 path_restore(PATH_T
*p
, char *old
)
514 * Return basename of path.
516 char *path_basename(PATH_T
*p
)
520 basename
= strrchr(p
->p_path
, '/');
521 return(basename
? basename
+ 1 : p
->p_path
);
529 #define R_FLAG (1<<0)
530 #define I_FLAG (1<<1)
531 #define N_FLAG (1<<2)
532 #define F_FLAG (1<<3)
533 #define P_FLAG (1<<4)
534 #define V_FLAG (1<<5)
537 setfile(FILINFO
*fs
, FIL
*fd
)
541 static struct timeval tv
[2];
543 fs
->st_mode
&= S_ISUID
|S_ISGID
|S_IRWXU
|S_IRWXG
|S_IRWXO
;
545 tv
[0].tv_sec
= fs
->st_atime
;
546 tv
[1].tv_sec
= fs
->st_mtime
;
547 if (utimes(to
->p_path
, tv
))
548 err(PSTR("utimes: %s: %s"), to
->p_path
, strerror(errno
));
552 void copy_file(FILINFO
*fs
, uint_fast8_t dne
)
554 static char buf
[MAXBSIZE
];
561 debug("==== copy_file(): dne: %u\n", dne
);
562 debug(" from:'%s' to:'%s'\n", from
->p_path
, to
->p_path
);
564 if ((fr
= f_open(&from_fd
, from
->p_path
, FA_READ
)) != FR_OK
) {
565 err(PSTR("%s: %S"), from
->p_path
, rctostr(fr
));
570 * If the file exists and we're interactive, verify with the user.
571 * If the file DNE, set the mode to be the from file, minus setuid
572 * bits, modified by the umask; arguably wrong, but it makes copying
573 * executables work right and it's been that way forever. (The
574 * other choice is 666 or'ed with the execute bits on the from file
575 * modified by the umask.)
578 if (flags
& I_FLAG
) {
581 printf_P(PSTR("overwrite %s? "), to
->p_path
);
582 checkch
= ch
= getchar();
583 while (ch
!= '\n' && ch
!= EOF
)
585 if (checkch
!= 'y') {
590 fr
= f_open(&to_fd
, to
->p_path
, FA_WRITE
|FA_CREATE_ALWAYS
);
592 fr
= f_open(&to_fd
, to
->p_path
, FA_WRITE
|FA_CREATE_NEW
);
595 err(PSTR("%s: %S"), to
->p_path
, rctostr(fr
));
600 while ((fr
= f_read(&from_fd
, buf
, MAXBSIZE
, &rcount
)) == FR_OK
&&
602 fr
= f_write(&to_fd
, buf
, rcount
, &wcount
);
603 if (fr
|| wcount
< rcount
) {
604 err(PSTR("%s: %S"), to
->p_path
, rctostr(fr
));
609 err(PSTR("%s: S"), from
->p_path
, rctostr(fr
));
615 if ((fr
= f_close(&to_fd
)) != FR_OK
)
616 err(PSTR("%s: %S"), to
->p_path
, rctostr(fr
));
620 static void copy_dir(void)
622 debug("==== copy_dir()");
623 debug(" from:'%s' to:'%s'\n", from
->p_path
, to
->p_path
);
625 printf_P(PSTR("directory copy not supported, ommitting dir '%s'\n"),
627 command_ret
= CMD_RET_FAILURE
;
630 static void copy_dir(void)
633 struct dirent
*dp
, **dir_list
;
635 char *old_from
, *old_to
;
637 debug("==== copy_file(): dne: %u\n", dne
);
638 debug(" from:'%s' to:'%s'\n", from
->p_path
, to
->p_path
);
640 dir_cnt
= scandir(from
->p_path
, &dir_list
, NULL
, NULL
);
642 (void)fprintf(stderr
, "%s: can't read directory %s.\n",
643 progname
, from
->p_path
);
644 command_ret
= CMD_RET_FAILURE
;
648 * Instead of handling directory entries in the order they appear
649 * on disk, do non-directory files before directory files.
650 * There are two reasons to do directories last. The first is
651 * efficiency. Files tend to be in the same cylinder group as
652 * their parent, whereas directories tend not to be. Copying files
653 * all at once reduces seeking. Second, deeply nested tree's
654 * could use up all the file descriptors if we didn't close one
655 * directory before recursivly starting on the next.
658 for (i
= 0; i
< dir_cnt
; ++i
) {
660 if (dp
->d_namlen
<= 2 && dp
->d_name
[0] == '.'
661 && (dp
->d_name
[1] == NULL
|| dp
->d_name
[1] == '.'))
664 path_append(&from
, dp
->d_name
, (int)dp
->d_namlen
)))
667 if (statfcn(from
->p_path
, &from_stat
) < 0) {
668 err(PSTR("%s: %s"), dp
->d_name
, strerror(errno
));
669 path_restore(&from
, old_from
);
672 if (S_ISDIR(from_stat
.st_mode
)) {
673 path_restore(&from
, old_from
);
676 if (old_to
= path_append(&to
, dp
->d_name
, (int)dp
->d_namlen
)) {
678 path_restore(&to
, old_to
);
680 path_restore(&from
, old_from
);
681 done
: dir_list
[i
] = NULL
;
685 /* copy directories */
686 for (i
= 0; i
< dir_cnt
; ++i
) {
691 path_append(&from
, dp
->d_name
, (int)dp
->d_namlen
))) {
696 path_append(&to
, dp
->d_name
, (int)dp
->d_namlen
))) {
698 path_restore(&from
, old_from
);
703 path_restore(&from
, old_from
);
704 path_restore(&to
, old_to
);
711 * copy file or directory at "from" to "to".
715 FILINFO from_stat
, to_stat
;
719 debug("==== copy()\n");
720 debug(" from:'%s' to:'%s'\n", from
->p_path
, to
->p_path
);
722 fr
= f_stat(from
->p_path
, &from_stat
);
724 err(PSTR("%s: %S"), from
->p_path
, rctostr(fr
));
728 /* not an error, but need to remember it happened */
729 if (f_stat(to
->p_path
, &to_stat
) != FR_OK
)
732 if (strcmp(to
->p_path
, from
->p_path
) == 0) {
733 (void)printf_P(PSTR("%s and %s are identical (not copied).\n"),
734 to
->p_path
, from
->p_path
);
735 command_ret
= CMD_RET_FAILURE
;
741 if(from_stat
.fattrib
& AM_DIR
) {
742 if (!(flags
& R_FLAG
)) {
743 (void)printf_P(PSTR("-r not specified; ommitting dir '%s'\n"),
745 command_ret
= CMD_RET_FAILURE
;
750 * If the directory doesn't exist, create the new one.
752 if ((fr
= f_mkdir(to
->p_path
)) != FR_OK
) {
753 err(PSTR("%s: %S"), to
->p_path
, rctostr(fr
));
757 else if (!(to_stat
.fattrib
& AM_DIR
)) {
758 (void)printf_P(PSTR("%s: not a directory.\n"), to
->p_path
);
763 * If not -p and directory didn't exist, set it to be the
764 * same as the from directory, umodified by the umask;
765 * arguably wrong, but it's been that way forever.
769 setfile(&from_stat
, 0);
771 (void)chmod(to
->p_path
, from_stat
.st_mode
);
775 copy_file(&from_stat
, dne
);
778 command_ret_t
do_cp(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
781 FRESULT fr
; /* Return value */
782 //DIR dj; /* Directory search object */
783 FILINFO to_stat
; /* File information */
787 command_ret
= CMD_RET_SUCCESS
;
793 while ((opt
= getopt(argc
, argv
, PSTR("Rrfip"))) != -1) {
813 return CMD_RET_USAGE
;
821 return CMD_RET_USAGE
;
823 from
= (PATH_T
*) malloc(sizeof(PATH_T
));
824 to
= (PATH_T
*) malloc(sizeof(PATH_T
));
825 if (from
== NULL
|| to
== NULL
) {
826 printf_P(PSTR("cp: Out of Memory!\n"));
827 command_ret
= CMD_RET_FAILURE
;
830 from
->p_end
= from
->p_path
; *from
->p_path
= '\0';
831 to
->p_end
= to
->p_path
; *to
->p_path
= '\0';
833 /* consume last argument first. */
834 if (!path_set(to
, argv
[--argc
])) {
835 command_ret
= CMD_RET_FAILURE
;
840 * Cp has two distinct cases:
842 * % cp [-rip] source target
843 * % cp [-rip] source1 ... directory
845 * In both cases, source can be either a file or a directory.
847 * In (1), the target becomes a copy of the source. That is, if the
848 * source is a file, the target will be a file, and likewise for
851 * In (2), the real target is not directory, but "directory/source".
854 fr
= f_stat(to
->p_path
, &to_stat
);
855 debug("==== main, stat to: fr: %d, attr: %02x\n", fr
, to_stat
.fattrib
);
856 debug(" from:'%s' to:'%s'\n", from
->p_path
, to
->p_path
);
858 if (fr
!= FR_OK
&& fr
!= FR_NO_FILE
&& fr
!= FR_NO_PATH
) {
859 err(PSTR("Test1: %s: %S"), to
->p_path
, rctostr(fr
));
860 command_ret
= CMD_RET_FAILURE
;
863 if (!(fr
== FR_OK
&& (to_stat
.fattrib
& AM_DIR
))) {
865 * Case (1). Target is not a directory.
868 err(PSTR("target '%s' is not a directory"), to
->p_path
);
869 //command_ret = CMD_RET_USAGE;
872 if (!path_set(from
, *argv
)) {
873 command_ret
= CMD_RET_FAILURE
;
880 * Case (2). Target is a directory.
883 if (!path_set(from
, *argv
))
885 if (!(old_to
= path_append(to
, path_basename(from
), -1)))
890 path_restore(to
, old_to
);
903 printf_P((PSTR("%s %s -> %s\n", badcp
? : "ERR:" : " ", curr
->fts_path
, to
->p_path
)));
912 /******************************************************************************/
915 * Work register for stat command
919 WORD AccFiles
, AccDirs
;
925 char *path
, /* Pointer to the working buffer with start path */
926 struct stat_dat_s
*statp
934 res
= f_opendir(&dirs
, path
);
938 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
939 statp
->Finfo
.fname
[0]) {
940 if (_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
942 fn
= statp
->Finfo
.fname
;
943 if (statp
->Finfo
.fattrib
& AM_DIR
) {
946 strcpy(path
+i
+1, fn
);
947 res
= scan_files(path
, statp
);
952 //printf_P(PSTR("%s/%s\n"), path, fn);
954 statp
->AccSize
+= statp
->Finfo
.fsize
;
968 * fatstat path - Show logical drive status
971 command_ret_t
do_stat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
978 struct stat_dat_s statp
;
980 buf
= (char *) malloc(BUFFER_SIZE
);
982 printf_P(PSTR("fat stat: Out of Memory!\n"));
983 return CMD_RET_FAILURE
;
988 res
= f_getfree(path
, &nfreeclst
, &fs
);
992 "Bytes/Cluster: %lu\n"
993 "Number of FATs: %u\n"
994 "Root DIR entries: %u\n"
996 "Number of clusters: %lu\n"
997 "FAT start (lba): %lu\n"
998 "DIR start (lba,cluster): %lu\n"
999 "Data start (lba): %lu\n"),
1000 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
1001 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
1002 fs
->fatbase
, fs
->dirbase
, fs
->database
);
1006 res
= f_getlabel(path
, buf
, &serial
);
1010 "Volume S/N: %04X-%04X\n"),
1011 buf
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
1015 my_puts_P(PSTR("\nCounting... "));
1016 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
1019 res
= scan_files(buf
, &statp
);
1022 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
1023 "%lu KB total disk space.\n%lu KB available.\n"),
1024 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
1025 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
1033 return CMD_RET_FAILURE
;
1035 return CMD_RET_SUCCESS
;
1039 * fatread/write - load binary file to/from a dos filesystem
1040 * read <d:/path/filename> <addr> [bytes [pos]]
1041 * write <d:/path/filename> <addr> <bytes>
1043 command_ret_t
do_rw(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
1047 unsigned long bytes
;
1049 unsigned long bytes_rw
;
1051 bool dowrite
= (argv
[0][0] == 'w');
1052 FRESULT res
= FR_OK
;
1057 if (argc
< (dowrite
? 4 : 3))
1058 return CMD_RET_USAGE
;
1060 addr
= eval_arg(argv
[2], NULL
);
1061 if (addr
>= MAX_MEMORY
) {
1062 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
1063 return CMD_RET_FAILURE
;
1066 bytes
= eval_arg(argv
[3], NULL
);
1070 pos
= eval_arg(argv
[4], NULL
);
1074 if (addr
+ bytes
> MAX_MEMORY
)
1075 bytes
= MAX_MEMORY
- addr
;
1077 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
1078 if (buffer
== NULL
) {
1079 printf_P(PSTR("fatstat: Out of Memory!\n"));
1081 return CMD_RET_FAILURE
;
1085 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
1089 res
= f_lseek(&File
, pos
);
1092 timer
= get_timer(0);
1094 unsigned int cnt
, br
;
1096 if (bytes
>= BUFFER_SIZE
) {
1098 bytes
-= BUFFER_SIZE
;
1100 cnt
= bytes
; bytes
= 0;
1103 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
1107 z80_read_block(buffer
, addr
, cnt
);
1108 z80_bus_cmd(Release
);
1109 res
= f_write(&File
, buffer
, cnt
, &br
);
1113 res
= f_read(&File
, buffer
, cnt
, &br
);
1116 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
1120 z80_write_block(buffer
, addr
, br
);
1121 z80_bus_cmd(Release
);
1128 printf_P(PSTR("Disk full?\n"));
1135 FRESULT fr
= f_close(&File
);
1138 timer
= get_timer(timer
);
1139 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
1140 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
1148 my_puts_P(PSTR("Bus timeout\n"));
1152 return CMD_RET_FAILURE
;
1154 return CMD_RET_SUCCESS
;
1158 * command table for fat subcommands
1161 cmd_tbl_t cmd_tbl_fat
[] = {
1163 stat
, 2, CTBL_RPT
, do_stat
,
1164 "Show logical drive status",
1168 pwd
, 2, CTBL_RPT
, do_pwd
,
1169 "Print name of current/working directory",
1174 "Change the current/working directory.",
1178 ls
, 2, CTBL_RPT
, do_ls
,
1179 "Directory listing",
1183 tst
, 2, CTBL_DBG
|CTBL_RPT
, do_tst
,
1184 "FatFS test function",
1189 "load binary file from a dos filesystem",
1190 "<d:/path/filename> <addr> [bytes [pos]]\n"
1191 " - Load binary file 'path/filename' on logical drive 'd'\n"
1192 " to address 'addr' from dos filesystem.\n"
1193 " 'pos' gives the file position to start loading from.\n"
1194 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
1195 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
1196 " the load stops on end of file."
1200 "write file into a dos filesystem",
1201 "<d:/path/filename> <addr> <bytes>\n"
1202 " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
1203 " starting at address 'addr'.\n"
1207 cp
, CONFIG_SYS_MAXARGS
, CTBL_DBG
, do_cp
,
1209 "[-R] [-f | -i | -n] [-aprv] source_file target_file\n"
1214 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
1215 "Print sub command description/usage",
1217 " - print brief description of all sub commands\n"
1218 "fat help command ...\n"
1219 " - print detailed usage of sub cmd 'command'"
1222 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
1223 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
1224 FSTR("Alias for 'help'"),
1225 #ifdef CONFIG_SYS_LONGHELP
1227 #endif /* CONFIG_SYS_LONGHELP */
1229 #ifdef CONFIG_AUTO_COMPLETE
1233 /* Mark end of table */
1234 CMD_TBL_END(cmd_tbl_fat
)
1238 command_ret_t
do_fat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
1240 puts_P(PSTR("Huch?"));
1242 return CMD_RET_USAGE
;