]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
c1d3770096e3acfa196aae45431bd2d9e73625cf
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"
24 #define DEBUG_FA 0 /* set to 1 to debug */
26 #define debug_fa(fmt, args...) \
27 debug_cond(DEBUG_FA, fmt, ##args)
30 /* TODO: use memory size test function (detect_ramsize() in cmd_loadihex.c) */
31 /* TODO: detect_ramsize() should be moved to z80-if.c */
32 #define MAX_MEMORY CONFIG_SYS_RAMSIZE_MAX
33 #define BUFFER_SIZE 512
36 * Multible (fat) partitions per physical drive are not supported,
37 * but we have up to 2 sdcard slots.
42 void setup_fatfs(void)
44 f_mount(&FatFs0
, "0:", 0);
45 f_mount(&FatFs1
, "1:", 0);
48 DWORD
get_fattime (void)
54 gmtime_r(&timer
, &tm_timer
);
56 return fatfs_time(&tm_timer
);
60 static bool check_abort(void)
65 printf_P(PSTR("Abort\n"));
71 static const FLASH
char * const FLASH rc_names
[] = {
81 FSTR("INVALID_OBJECT"),
82 FSTR("WRITE_PROTECTED"),
83 FSTR("INVALID_DRIVE"),
85 FSTR("NO_FILE_SYSTEM"),
89 FSTR("NOT_ENOUGH_CORE"),
90 FSTR("TOO_MANY_OPEN_FILES"),
91 FSTR("INVALID_PARAMETER")
95 void put_rc (FRESULT rc
)
98 printf_P(PSTR("rc=%u FR_"), rc
);
99 my_puts_P(rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
100 my_puts_P(PSTR("\n"));
102 printf_P(PSTR("rc=%u FR_%S\n"), rc
,
103 rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
108 static void swirl(void)
110 static const FLASH
char swirlchar
[] = { '-','\\','|','/' };
111 static uint_fast8_t cnt
;
112 static uint32_t tstamp
;
114 if (get_timer(0) > tstamp
) {
115 tstamp
= get_timer(0) + 250;
118 putchar(swirlchar
[cnt
]);
123 * pwd - Print current directory of the current drive.
126 command_ret_t
do_pwd(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
131 buf
= (char *) malloc(BUFFER_SIZE
);
133 printf_P(PSTR("pwd: Out of Memory!\n"));
134 return CMD_RET_FAILURE
;
137 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
145 return CMD_RET_FAILURE
;
147 return CMD_RET_SUCCESS
;
152 * cd - Change the current/working directory.
155 command_ret_t
do_cd(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
161 arg
= getenv_str(PSTR(ENV_HOME
));
163 printf_P(PSTR("%s: \"%S\" is not set\n"), argv
[0], PSTR(ENV_HOME
));
164 return CMD_RET_FAILURE
;
170 res
= f_chdrive(arg
);
171 debug_fa("### f_chdrive(): arg: '%s', res: %d\n", arg
, res
);
173 if (arg
[2] == '\0') {
179 debug_fa("### f_chdir(): arg: '%s', res: %d\n", arg
, res
);
182 return CMD_RET_FAILURE
;
184 return CMD_RET_SUCCESS
;
189 * ls path - Directory listing
192 command_ret_t
do_ls(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
195 DIR Dir
; /* Directory object */
203 buf
= (char *) malloc(BUFFER_SIZE
);
205 printf_P(PSTR("pwd: Out of Memory!\n"));
207 return CMD_RET_FAILURE
;
211 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
213 strncpy(buf
, argv
[1], BUFFER_SIZE
);
216 res
= f_opendir(&Dir
, buf
);
220 return CMD_RET_FAILURE
;
225 res
= f_readdir(&Dir
, &Finfo
);
226 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
228 if (Finfo
.fattrib
& AM_DIR
) {
231 s1
++; p1
+= Finfo
.fsize
;
233 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
234 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
235 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
236 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
237 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
238 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
239 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
240 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63,
241 Finfo
.fsize
, Finfo
.fname
);
247 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
248 if (f_getfree(buf
, (DWORD
*)&p1
, &fs
) == FR_OK
)
249 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
255 return CMD_RET_FAILURE
;
258 return CMD_RET_SUCCESS
;
263 FRESULT
mkpath(TCHAR
*path
)
271 res
= f_stat (path
, &fd
)
273 p
= strchr(path
, ':');
274 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
277 while ((q
= strchr(p
, '/')) != NULL
) {
281 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
290 /* Work register for fs command */
293 WORD AccFiles
, AccDirs
;
299 char *path
, /* Pointer to the working buffer with start path */
300 struct stat_dat_s
*statp
308 res
= f_opendir(&dirs
, path
);
312 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
313 statp
->Finfo
.fname
[0]) {
314 if (FF_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
316 fn
= statp
->Finfo
.fname
;
317 if (statp
->Finfo
.fattrib
& AM_DIR
) {
320 strcpy(path
+i
+1, fn
);
321 res
= scan_files(path
, statp
);
326 //printf_P(PSTR("%s/%s\n"), path, fn);
328 statp
->AccSize
+= statp
->Finfo
.fsize
;
342 * fatstat path - Show logical drive status
345 command_ret_t
do_stat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
352 struct stat_dat_s statp
;
354 buf
= (char *) malloc(BUFFER_SIZE
);
356 printf_P(PSTR("fat stat: Out of Memory!\n"));
357 return CMD_RET_FAILURE
;
362 res
= f_getfree(path
, &nfreeclst
, &fs
);
366 "Bytes/Cluster: %lu\n"
367 "Number of FATs: %u\n"
368 "Root DIR entries: %u\n"
370 "Number of clusters: %lu\n"
371 "FAT start (lba): %lu\n"
372 "DIR start (lba,cluster): %lu\n"
373 "Data start (lba): %lu\n"),
374 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
375 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
376 fs
->fatbase
, fs
->dirbase
, fs
->database
);
380 res
= f_getlabel(path
, buf
, &serial
);
384 "Volume S/N: %04X-%04X\n"),
385 buf
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
389 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
392 my_puts_P(PSTR("\nCounting... "));
393 res
= scan_files(buf
, &statp
);
397 printf_P(PSTR("%u files, %lu bytes.\n%u folders.\n"
398 "%lu KB total disk space.\n%lu KB available.\n"),
399 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
400 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
408 return CMD_RET_FAILURE
;
410 return CMD_RET_SUCCESS
;
414 * fatread/write - load binary file to/from a dos filesystem
415 * read <d:/path/filename> <addr> [bytes [pos]]
416 * write <d:/path/filename> <addr> <bytes>
418 command_ret_t
do_rw(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
424 unsigned long bytes_rw
;
426 bool dowrite
= (argv
[0][0] == 'w');
432 if (argc
< (dowrite
? 4 : 3))
433 return CMD_RET_USAGE
;
435 addr
= eval_arg(argv
[2], NULL
);
436 if (addr
>= MAX_MEMORY
) {
437 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
438 return CMD_RET_FAILURE
;
441 bytes
= eval_arg(argv
[3], NULL
);
445 pos
= eval_arg(argv
[4], NULL
);
449 if (addr
+ bytes
> MAX_MEMORY
)
450 bytes
= MAX_MEMORY
- addr
;
452 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
453 if (buffer
== NULL
) {
454 printf_P(PSTR("fatstat: Out of Memory!\n"));
456 return CMD_RET_FAILURE
;
460 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
464 res
= f_lseek(&File
, pos
);
467 timer
= get_timer(0);
469 unsigned int cnt
, br
;
471 if (bytes
>= BUFFER_SIZE
) {
473 bytes
-= BUFFER_SIZE
;
475 cnt
= bytes
; bytes
= 0;
478 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
482 z80_read_block(buffer
, addr
, cnt
);
483 z80_bus_cmd(Release
);
484 res
= f_write(&File
, buffer
, cnt
, &br
);
488 res
= f_read(&File
, buffer
, cnt
, &br
);
491 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
495 z80_write_block(buffer
, addr
, br
);
496 z80_bus_cmd(Release
);
503 printf_P(PSTR("Disk full?\n"));
510 FRESULT fr
= f_close(&File
);
513 timer
= get_timer(timer
);
514 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
515 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
523 my_puts_P(PSTR("Bus timeout\n"));
527 return CMD_RET_FAILURE
;
529 return CMD_RET_SUCCESS
;
533 * command table for fat subcommands
536 cmd_tbl_t cmd_tbl_fat
[] = {
538 status
, 2, CTBL_RPT
, do_stat
,
539 "Show logical drive status",
543 pwd
, 2, CTBL_RPT
|CTBL_SUBCMDAUTO
, do_pwd
,
544 "Print name of current/working directory",
548 cd
, 2, 0|CTBL_SUBCMDAUTO
, do_cd
,
549 "Change the current/working directory.",
553 ls
, 2, CTBL_RPT
|CTBL_SUBCMDAUTO
, do_ls
,
559 "load binary file from a dos filesystem",
560 "<d:/path/filename> <addr> [bytes [pos]]\n"
561 " - Load binary file 'path/filename' on logical drive 'd'\n"
562 " to address 'addr' from dos filesystem.\n"
563 " 'pos' gives the file position to start loading from.\n"
564 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
565 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
566 " the load stops on end of file."
570 "write file into a dos filesystem",
571 "<d:/path/filename> <addr> <bytes>\n"
572 " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
573 " starting at address 'addr'.\n"
577 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
578 "Print sub command description/usage",
580 " - print brief description of all sub commands\n"
581 "fat help command ...\n"
582 " - print detailed usage of sub cmd 'command'"
585 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
586 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
588 #ifdef CONFIG_SYS_LONGHELP
590 #endif /* CONFIG_SYS_LONGHELP */
592 #ifdef CONFIG_AUTO_COMPLETE
596 /* Mark end of table */
597 CMD_TBL_END(cmd_tbl_fat
)
601 command_ret_t
do_fat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
603 puts_P(PSTR("Huch?"));
605 return CMD_RET_USAGE
;