]>
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"
23 /* TODO: use memory size test function (detect_ramsize() in cmd_loadihex.c) */
24 /* TODO: detect_ramsize() should be moved to z80-if.c */
25 #define MAX_MEMORY CONFIG_SYS_RAMSIZE_MAX
26 #define BUFFER_SIZE 512
29 * Multible (fat) partitions per physical drive are not supported,
30 * but we have up to 2 sdcard slots.
35 void setup_fatfs(void)
37 f_mount(&FatFs0
, "0:", 0);
38 f_mount(&FatFs1
, "1:", 0);
41 DWORD
get_fattime (void)
47 gmtime_r(&timer
, &tm_timer
);
49 return fatfs_time(&tm_timer
);
53 static bool check_abort(void)
58 printf_P(PSTR("Abort\n"));
64 static const FLASH
char * const FLASH rc_names
[] = {
74 FSTR("INVALID_OBJECT"),
75 FSTR("WRITE_PROTECTED"),
76 FSTR("INVALID_DRIVE"),
78 FSTR("NO_FILE_SYSTEM"),
82 FSTR("NOT_ENOUGH_CORE"),
83 FSTR("TOO_MANY_OPEN_FILES"),
84 FSTR("INVALID_PARAMETER")
88 void put_rc (FRESULT rc
)
91 printf_P(PSTR("rc=%u FR_"), rc
);
92 my_puts_P(rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
93 my_puts_P(PSTR("\n"));
95 printf_P(PSTR("rc=%u FR_%S\n"), rc
,
96 rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
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;
115 * pwd - Print current directory of the current drive.
118 command_ret_t
do_pwd(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
123 buf
= (char *) malloc(BUFFER_SIZE
);
125 printf_P(PSTR("pwd: Out of Memory!\n"));
127 return CMD_RET_FAILURE
;
130 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
138 return CMD_RET_FAILURE
;
140 return CMD_RET_SUCCESS
;
145 * cd - Change the current/working directory.
148 command_ret_t
do_cd(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
154 arg
= getenv_str(PSTR(ENV_HOME
));
156 printf_P(PSTR("%s: \"%S\" is not set\n"), argv
[0], PSTR(ENV_HOME
));
157 return CMD_RET_FAILURE
;
167 res
= f_chdrive(drv
);
175 return CMD_RET_FAILURE
;
177 return CMD_RET_SUCCESS
;
182 * ls path - Directory listing
185 command_ret_t
do_ls(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
188 DIR Dir
; /* Directory object */
196 buf
= (char *) malloc(BUFFER_SIZE
);
198 printf_P(PSTR("pwd: Out of Memory!\n"));
200 return CMD_RET_FAILURE
;
204 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
206 strncpy(buf
, argv
[1], BUFFER_SIZE
);
209 res
= f_opendir(&Dir
, buf
);
213 return CMD_RET_FAILURE
;
218 res
= f_readdir(&Dir
, &Finfo
);
219 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
221 if (Finfo
.fattrib
& AM_DIR
) {
224 s1
++; p1
+= Finfo
.fsize
;
226 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
227 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
228 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
229 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
230 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
231 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
232 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
233 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63,
234 Finfo
.fsize
, Finfo
.fname
);
240 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
241 if (f_getfree(buf
, (DWORD
*)&p1
, &fs
) == FR_OK
)
242 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
248 return CMD_RET_FAILURE
;
251 return CMD_RET_SUCCESS
;
256 FRESULT
mkpath(TCHAR
*path
)
264 res
= f_stat (path
, &fd
)
266 p
= strchr(path
, ':');
267 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
270 while ((q
= strchr(p
, '/')) != NULL
) {
274 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
283 /* Work register for fs command */
286 WORD AccFiles
, AccDirs
;
292 char *path
, /* Pointer to the working buffer with start path */
293 struct stat_dat_s
*statp
301 res
= f_opendir(&dirs
, path
);
305 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
306 statp
->Finfo
.fname
[0]) {
307 if (FF_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
309 fn
= statp
->Finfo
.fname
;
310 if (statp
->Finfo
.fattrib
& AM_DIR
) {
313 strcpy(path
+i
+1, fn
);
314 res
= scan_files(path
, statp
);
319 //printf_P(PSTR("%s/%s\n"), path, fn);
321 statp
->AccSize
+= statp
->Finfo
.fsize
;
335 * fatstat path - Show logical drive status
338 command_ret_t
do_stat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
345 struct stat_dat_s statp
;
347 buf
= (char *) malloc(BUFFER_SIZE
);
349 printf_P(PSTR("fat stat: Out of Memory!\n"));
350 return CMD_RET_FAILURE
;
355 res
= f_getfree(path
, &nfreeclst
, &fs
);
359 "Bytes/Cluster: %lu\n"
360 "Number of FATs: %u\n"
361 "Root DIR entries: %u\n"
363 "Number of clusters: %lu\n"
364 "FAT start (lba): %lu\n"
365 "DIR start (lba,cluster): %lu\n"
366 "Data start (lba): %lu\n"),
367 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
368 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
369 fs
->fatbase
, fs
->dirbase
, fs
->database
);
373 res
= f_getlabel(path
, buf
, &serial
);
377 "Volume S/N: %04X-%04X\n"),
378 buf
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
382 my_puts_P(PSTR("\nCounting... "));
383 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
386 res
= scan_files(buf
, &statp
);
389 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
390 "%lu KB total disk space.\n%lu KB available.\n"),
391 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
392 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
400 return CMD_RET_FAILURE
;
402 return CMD_RET_SUCCESS
;
406 * fatread/write - load binary file to/from a dos filesystem
407 * read <d:/path/filename> <addr> [bytes [pos]]
408 * write <d:/path/filename> <addr> <bytes>
410 command_ret_t
do_rw(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
416 unsigned long bytes_rw
;
418 bool dowrite
= (argv
[0][0] == 'w');
424 if (argc
< (dowrite
? 4 : 3))
425 return CMD_RET_USAGE
;
427 addr
= eval_arg(argv
[2], NULL
);
428 if (addr
>= MAX_MEMORY
) {
429 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
430 return CMD_RET_FAILURE
;
433 bytes
= eval_arg(argv
[3], NULL
);
437 pos
= eval_arg(argv
[4], NULL
);
441 if (addr
+ bytes
> MAX_MEMORY
)
442 bytes
= MAX_MEMORY
- addr
;
444 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
445 if (buffer
== NULL
) {
446 printf_P(PSTR("fatstat: Out of Memory!\n"));
448 return CMD_RET_FAILURE
;
452 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
456 res
= f_lseek(&File
, pos
);
459 timer
= get_timer(0);
461 unsigned int cnt
, br
;
463 if (bytes
>= BUFFER_SIZE
) {
465 bytes
-= BUFFER_SIZE
;
467 cnt
= bytes
; bytes
= 0;
470 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
474 z80_read_block(buffer
, addr
, cnt
);
475 z80_bus_cmd(Release
);
476 res
= f_write(&File
, buffer
, cnt
, &br
);
480 res
= f_read(&File
, buffer
, cnt
, &br
);
483 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
487 z80_write_block(buffer
, addr
, br
);
488 z80_bus_cmd(Release
);
495 printf_P(PSTR("Disk full?\n"));
502 FRESULT fr
= f_close(&File
);
505 timer
= get_timer(timer
);
506 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
507 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
515 my_puts_P(PSTR("Bus timeout\n"));
519 return CMD_RET_FAILURE
;
521 return CMD_RET_SUCCESS
;
525 * command table for fat subcommands
528 cmd_tbl_t cmd_tbl_fat
[] = {
530 stat
, 2, CTBL_RPT
, do_stat
,
531 "Show logical drive status",
535 pwd
, 2, CTBL_RPT
, do_pwd
,
536 "Print name of current/working directory",
541 "Change the current/working directory.",
545 ls
, 2, CTBL_RPT
, do_ls
,
551 "load binary file from a dos filesystem",
552 "<d:/path/filename> <addr> [bytes [pos]]\n"
553 " - Load binary file 'path/filename' on logical drive 'd'\n"
554 " to address 'addr' from dos filesystem.\n"
555 " 'pos' gives the file position to start loading from.\n"
556 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
557 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
558 " the load stops on end of file."
562 "write file into a dos filesystem",
563 "<d:/path/filename> <addr> <bytes>\n"
564 " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
565 " starting at address 'addr'.\n"
569 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
570 "Print sub command description/usage",
572 " - print brief description of all sub commands\n"
573 "fat help command ...\n"
574 " - print detailed usage of sub cmd 'command'"
577 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
578 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
579 FSTR("Alias for 'help'"),
580 #ifdef CONFIG_SYS_LONGHELP
582 #endif /* CONFIG_SYS_LONGHELP */
584 #ifdef CONFIG_AUTO_COMPLETE
588 /* Mark end of table */
589 CMD_TBL_END(cmd_tbl_fat
)
593 command_ret_t
do_fat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
595 puts_P(PSTR("Huch?"));
597 return CMD_RET_USAGE
;