]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
0c1d0e73aebb0109feec7194bdbe43909711f9d5
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 tstamp
= get_timer(0) + 250;
111 putchar(swirlchar
[cnt
]);
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
;
183 * ls path - Directory listing
186 command_ret_t
do_ls(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
189 DIR Dir
; /* Directory object */
197 buf
= (char *) malloc(BUFFER_SIZE
);
199 printf_P(PSTR("pwd: Out of Memory!\n"));
201 return CMD_RET_FAILURE
;
205 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
207 strncpy(buf
, argv
[1], BUFFER_SIZE
);
210 res
= f_opendir(&Dir
, buf
);
214 return CMD_RET_FAILURE
;
219 res
= f_readdir(&Dir
, &Finfo
);
220 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
222 if (Finfo
.fattrib
& AM_DIR
) {
225 s1
++; p1
+= Finfo
.fsize
;
227 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
228 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
229 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
230 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
231 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
232 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
233 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
234 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63,
235 Finfo
.fsize
, Finfo
.fname
);
241 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
242 if (f_getfree(buf
, (DWORD
*)&p1
, &fs
) == FR_OK
)
243 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
249 return CMD_RET_FAILURE
;
252 return CMD_RET_SUCCESS
;
257 FRESULT
mkpath(TCHAR
*path
)
265 res
= f_stat (path
, &fd
)
267 p
= strchr(path
, ':');
268 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
271 while ((q
= strchr(p
, '/')) != NULL
) {
275 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
284 /* Work register for fs command */
287 WORD AccFiles
, AccDirs
;
293 char *path
, /* Pointer to the working buffer with start path */
294 struct stat_dat_s
*statp
302 res
= f_opendir(&dirs
, path
);
306 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
307 statp
->Finfo
.fname
[0]) {
308 if (FF_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
310 fn
= statp
->Finfo
.fname
;
311 if (statp
->Finfo
.fattrib
& AM_DIR
) {
314 strcpy(path
+i
+1, fn
);
315 res
= scan_files(path
, statp
);
320 //printf_P(PSTR("%s/%s\n"), path, fn);
322 statp
->AccSize
+= statp
->Finfo
.fsize
;
336 * fatstat path - Show logical drive status
339 command_ret_t
do_stat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
346 struct stat_dat_s statp
;
348 buf
= (char *) malloc(BUFFER_SIZE
);
350 printf_P(PSTR("fat stat: Out of Memory!\n"));
351 return CMD_RET_FAILURE
;
356 res
= f_getfree(path
, &nfreeclst
, &fs
);
360 "Bytes/Cluster: %lu\n"
361 "Number of FATs: %u\n"
362 "Root DIR entries: %u\n"
364 "Number of clusters: %lu\n"
365 "FAT start (lba): %lu\n"
366 "DIR start (lba,cluster): %lu\n"
367 "Data start (lba): %lu\n"),
368 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
369 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
370 fs
->fatbase
, fs
->dirbase
, fs
->database
);
374 res
= f_getlabel(path
, buf
, &serial
);
378 "Volume S/N: %04X-%04X\n"),
379 buf
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
383 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
386 my_puts_P(PSTR("\nCounting... "));
387 res
= scan_files(buf
, &statp
);
391 printf_P(PSTR("%u files, %lu bytes.\n%u folders.\n"
392 "%lu KB total disk space.\n%lu KB available.\n"),
393 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
394 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
402 return CMD_RET_FAILURE
;
404 return CMD_RET_SUCCESS
;
408 * fatread/write - load binary file to/from a dos filesystem
409 * read <d:/path/filename> <addr> [bytes [pos]]
410 * write <d:/path/filename> <addr> <bytes>
412 command_ret_t
do_rw(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
418 unsigned long bytes_rw
;
420 bool dowrite
= (argv
[0][0] == 'w');
426 if (argc
< (dowrite
? 4 : 3))
427 return CMD_RET_USAGE
;
429 addr
= eval_arg(argv
[2], NULL
);
430 if (addr
>= MAX_MEMORY
) {
431 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
432 return CMD_RET_FAILURE
;
435 bytes
= eval_arg(argv
[3], NULL
);
439 pos
= eval_arg(argv
[4], NULL
);
443 if (addr
+ bytes
> MAX_MEMORY
)
444 bytes
= MAX_MEMORY
- addr
;
446 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
447 if (buffer
== NULL
) {
448 printf_P(PSTR("fatstat: Out of Memory!\n"));
450 return CMD_RET_FAILURE
;
454 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
458 res
= f_lseek(&File
, pos
);
461 timer
= get_timer(0);
463 unsigned int cnt
, br
;
465 if (bytes
>= BUFFER_SIZE
) {
467 bytes
-= BUFFER_SIZE
;
469 cnt
= bytes
; bytes
= 0;
472 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
476 z80_read_block(buffer
, addr
, cnt
);
477 z80_bus_cmd(Release
);
478 res
= f_write(&File
, buffer
, cnt
, &br
);
482 res
= f_read(&File
, buffer
, cnt
, &br
);
485 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
489 z80_write_block(buffer
, addr
, br
);
490 z80_bus_cmd(Release
);
497 printf_P(PSTR("Disk full?\n"));
504 FRESULT fr
= f_close(&File
);
507 timer
= get_timer(timer
);
508 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
509 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
517 my_puts_P(PSTR("Bus timeout\n"));
521 return CMD_RET_FAILURE
;
523 return CMD_RET_SUCCESS
;
527 * command table for fat subcommands
530 cmd_tbl_t cmd_tbl_fat
[] = {
532 stat
, 2, CTBL_RPT
, do_stat
,
533 "Show logical drive status",
537 pwd
, 2, CTBL_RPT
|CTBL_SUBCMDAUTO
, do_pwd
,
538 "Print name of current/working directory",
542 cd
, 2, 0|CTBL_SUBCMDAUTO
, do_cd
,
543 "Change the current/working directory.",
547 ls
, 2, CTBL_RPT
|CTBL_SUBCMDAUTO
, do_ls
,
553 "load binary file from a dos filesystem",
554 "<d:/path/filename> <addr> [bytes [pos]]\n"
555 " - Load binary file 'path/filename' on logical drive 'd'\n"
556 " to address 'addr' from dos filesystem.\n"
557 " 'pos' gives the file position to start loading from.\n"
558 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
559 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
560 " the load stops on end of file."
564 "write file into a dos filesystem",
565 "<d:/path/filename> <addr> <bytes>\n"
566 " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
567 " starting at address 'addr'.\n"
571 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
572 "Print sub command description/usage",
574 " - print brief description of all sub commands\n"
575 "fat help command ...\n"
576 " - print detailed usage of sub cmd 'command'"
579 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
580 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
582 #ifdef CONFIG_SYS_LONGHELP
584 #endif /* CONFIG_SYS_LONGHELP */
586 #ifdef CONFIG_AUTO_COMPLETE
590 /* Mark end of table */
591 CMD_TBL_END(cmd_tbl_fat
)
595 command_ret_t
do_fat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
597 puts_P(PSTR("Huch?"));
599 return CMD_RET_USAGE
;