]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
e09b2a6643fb0d10039a172360867a72f3499d92
2 * (C) Copyright 2014,2016 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
8 * FAT filesystem commands
20 #include "con-utils.h"
21 #include "print-utils.h"
27 /* TODO: use memory size test function (cmd_mem.c) */
28 #define MAX_MEMORY (1ul << 19)
29 #define BUFFER_SIZE 512
37 DWORD
get_fattime (void)
43 gmtime_r(&timer
, &tm_timer
);
45 return fatfs_time(&tm_timer
);
49 static bool check_abort(void)
54 printf_P(PSTR("Abort\n"));
60 static const FLASH
char * const FLASH rc_names
[] = {
70 FSTR("INVALID_OBJECT"),
71 FSTR("WRITE_PROTECTED"),
72 FSTR("INVALID_DRIVE"),
74 FSTR("NO_FILE_SYSTEM"),
78 FSTR("NOT_ENOUGH_CORE"),
79 FSTR("TOO_MANY_OPEN_FILES"),
80 FSTR("INVALID_PARAMETER")
84 void put_rc (FRESULT rc
)
87 printf_P(PSTR("rc=%u FR_"), rc
);
88 my_puts_P(rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
89 my_puts_P(PSTR("\n"));
91 printf_P(PSTR("rc=%u FR_%S\n"), rc
,
92 rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
97 void setup_fatfs(void)
99 f_mount(&FatFs0
, "0:", 0);
100 f_mount(&FatFs1
, "1:", 0);
104 static void swirl(void)
106 static const FLASH
char swirlchar
[] = { '-','\\','|','/' };
107 static uint_fast8_t cnt
;
108 static uint32_t tstamp
;
110 if (get_timer(0) > tstamp
) {
111 printf_P(PSTR("\b%c"), swirlchar
[cnt
]);
112 cnt
= (cnt
+1) % ARRAY_SIZE(swirlchar
);
113 tstamp
= get_timer(0) + 250;
117 /* Work register for fs command */
120 WORD AccFiles
, AccDirs
;
126 char *path
, /* Pointer to the working buffer with start path */
127 struct stat_dat_s
*statp
135 res
= f_opendir(&dirs
, path
);
139 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
140 statp
->Finfo
.fname
[0]) {
141 if (_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
143 fn
= statp
->Finfo
.fname
;
144 if (statp
->Finfo
.fattrib
& AM_DIR
) {
147 strcpy(path
+i
+1, fn
);
148 res
= scan_files(path
, statp
);
153 //printf_P(PSTR("%s/%s\n"), path, fn);
155 statp
->AccSize
+= statp
->Finfo
.fsize
;
169 * fatstat path - Show logical drive status
172 command_ret_t
do_stat(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
178 struct stat_dat_s statp
;
180 (void) cmdtp
; (void) flag
; (void) argc
;
182 path
= (char *) malloc(BUFFER_SIZE
);
184 printf_P(PSTR("fatstat: Out of Memory!\n"));
186 return CMD_RET_FAILURE
;
189 res
= f_getfree(argv
[1], &nfreeclst
, &fs
);
193 "Bytes/Cluster: %lu\n"
194 "Number of FATs: %u\n"
195 "Root DIR entries: %u\n"
197 "Number of clusters: %lu\n"
198 "FAT start (lba): %lu\n"
199 "DIR start (lba,cluster): %lu\n"
200 "Data start (lba): %lu\n"),
201 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
202 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
203 fs
->fatbase
, fs
->dirbase
, fs
->database
);
208 res
= f_getlabel(argv
[1], label
, &serial
);
212 "Volume S/N: %04X-%04X\n"),
213 label
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
217 my_puts_P(PSTR("\nCounting... "));
218 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
219 strcpy(path
, argv
[1]);
221 res
= scan_files(path
, &statp
);
224 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
225 "%lu KB total disk space.\n%lu KB available.\n"),
226 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
227 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
235 return CMD_RET_FAILURE
;
237 return CMD_RET_SUCCESS
;
241 * pwd - Print current directory of the current drive.
244 command_ret_t
do_pwd(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
249 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
251 buf
= (char *) malloc(BUFFER_SIZE
);
253 printf_P(PSTR("pwd: Out of Memory!\n"));
255 return CMD_RET_FAILURE
;
258 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
266 return CMD_RET_FAILURE
;
268 return CMD_RET_SUCCESS
;
273 * cd - Change the current/working directory.
276 command_ret_t
do_cd(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
281 (void) cmdtp
; (void) flag
; (void) argc
;
284 arg
= getenv_str(PSTR(ENV_HOME
));
286 printf_P(PSTR("%s: \"%S\" is not set\n"), argv
[0], PSTR(ENV_HOME
));
287 return CMD_RET_FAILURE
;
297 res
= f_chdrive(drv
);
305 return CMD_RET_FAILURE
;
307 return CMD_RET_SUCCESS
;
312 * fatls path - Directory listing
315 command_ret_t
do_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
318 DIR Dir
; /* Directory object */
325 (void) cmdtp
; (void) flag
; (void) argc
;
327 buf
= (char *) malloc(BUFFER_SIZE
);
329 printf_P(PSTR("pwd: Out of Memory!\n"));
331 return CMD_RET_FAILURE
;
335 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
337 strncpy(buf
, argv
[1], BUFFER_SIZE
);
340 res
= f_opendir(&Dir
, buf
);
344 return CMD_RET_FAILURE
;
349 res
= f_readdir(&Dir
, &Finfo
);
350 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
352 if (Finfo
.fattrib
& AM_DIR
) {
355 s1
++; p1
+= Finfo
.fsize
;
357 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
358 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
359 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
360 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
361 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
362 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
363 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
364 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63,
365 Finfo
.fsize
, Finfo
.fname
);
371 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
372 if (f_getfree(buf
, (DWORD
*)&p1
, &fs
) == FR_OK
)
373 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
379 return CMD_RET_FAILURE
;
382 return CMD_RET_SUCCESS
;
386 FRESULT
mkpath(TCHAR
*path
)
395 res
= f_stat (path
, &fd
)
397 p
= strchr(path
, ':');
398 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
401 while ((q
= strchr(p
, '/')) != NULL
) {
405 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
415 * fatread/write - load binary file to/from a dos filesystem
416 * read <d:/path/filename> <addr> [bytes [pos]]
417 * write <d:/path/filename> <addr> <bytes>
419 command_ret_t
do_rw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
425 unsigned long bytes_rw
;
427 bool dowrite
= (argv
[0][3] == 'w');
433 (void) cmdtp
; (void) flag
;
435 if (argc
< (dowrite
? 4 : 3))
436 return CMD_RET_USAGE
;
438 addr
= eval_arg(argv
[2], NULL
);
439 if (addr
>= MAX_MEMORY
) {
440 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
441 return CMD_RET_FAILURE
;
444 bytes
= eval_arg(argv
[3], NULL
);
448 pos
= eval_arg(argv
[4], NULL
);
452 if (addr
+ bytes
> MAX_MEMORY
)
453 bytes
= MAX_MEMORY
- addr
;
455 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
456 if (buffer
== NULL
) {
457 printf_P(PSTR("fatstat: Out of Memory!\n"));
459 return CMD_RET_FAILURE
;
463 res
= mkpath(argv
[1]);
466 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
470 res
= f_lseek(&File
, pos
);
473 timer
= get_timer(0);
475 unsigned int cnt
, br
;
477 if (bytes
>= BUFFER_SIZE
) {
479 bytes
-= BUFFER_SIZE
;
481 cnt
= bytes
; bytes
= 0;
484 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
488 z80_read_block(buffer
, addr
, cnt
);
489 z80_bus_cmd(Release
);
490 res
= f_write(&File
, buffer
, cnt
, &br
);
494 res
= f_read(&File
, buffer
, cnt
, &br
);
497 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
501 z80_write_block(buffer
, addr
, br
);
502 z80_bus_cmd(Release
);
509 printf_P(PSTR("Disk full?\n"));
516 FRESULT fr
= f_close(&File
);
519 timer
= get_timer(timer
);
520 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
521 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
529 my_puts_P(PSTR("Bus timeout\n"));
533 return CMD_RET_FAILURE
;
535 return CMD_RET_SUCCESS
;
540 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[]);
542 cmd_tbl_t cmd_fat_sub
[] = {
545 "Show logical drive status",
550 "Print name of current/working directory",
555 "Change the current/working directory.",
565 "load binary file from a dos filesystem",
566 "<d:/path/filename> <addr> [bytes [pos]]\n"
567 " - Load binary file 'path/filename' on logical drive 'd'\n"
568 " to address 'addr' from dos filesystem.\n"
569 " 'pos' gives the file position to start loading from.\n"
570 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
571 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
572 " the load stops on end of file."
576 "write file into a dos filesystem",
577 "<d:/path/filename> <addr> <bytes>\n"
578 " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
579 " starting at address 'addr'.\n"
583 help
, CONFIG_SYS_MAXARGS
, 1, do_help
,
584 "Print sub command description/usage",
586 " - print brief description of all sub commands\n"
587 "fat help command ...\n"
588 " - print detailed usage of sub cmd 'command'"
591 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
592 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
593 FSTR("Alias for 'help'"),
594 #ifdef CONFIG_SYS_LONGHELP
596 #endif /* CONFIG_SYS_LONGHELP */
597 #ifdef CONFIG_AUTO_COMPLETE
604 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
606 return _do_help(cmd_fat_sub
, ARRAY_SIZE(cmd_fat_sub
), cmdtp
, flag
, argc
, argv
);
610 command_ret_t
do_fat(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
615 return CMD_RET_USAGE
;
617 /* drop initial "fat" arg */
621 cp
= find_cmd_tbl(argv
[0], cmd_fat_sub
, ARRAY_SIZE(cmd_fat_sub
));
624 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
626 return CMD_RET_USAGE
;