]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
b083456193a36367ead397853018eff62e4b136d
2 * (C) Copyright 2014,2016 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 (cmd_mem.c) */
24 #define MAX_MEMORY (1ul << 19)
25 #define BUFFER_SIZE 512
33 DWORD
get_fattime (void)
39 gmtime_r(&timer
, &tm_timer
);
41 return fatfs_time(&tm_timer
);
45 static bool check_abort(void)
50 printf_P(PSTR("Abort\n"));
56 static const FLASH
char * const FLASH rc_names
[] = {
66 FSTR("INVALID_OBJECT"),
67 FSTR("WRITE_PROTECTED"),
68 FSTR("INVALID_DRIVE"),
70 FSTR("NO_FILE_SYSTEM"),
74 FSTR("NOT_ENOUGH_CORE"),
75 FSTR("TOO_MANY_OPEN_FILES"),
76 FSTR("INVALID_PARAMETER")
80 void put_rc (FRESULT rc
)
83 printf_P(PSTR("rc=%u FR_"), rc
);
84 my_puts_P(rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
85 my_puts_P(PSTR("\n"));
87 printf_P(PSTR("rc=%u FR_%S\n"), rc
,
88 rc
< ARRAY_SIZE(rc_names
) ? rc_names
[rc
] : PSTR(" Unknown Error"));
93 void setup_fatfs(void)
95 f_mount(&FatFs0
, "0:", 0);
96 f_mount(&FatFs1
, "1:", 0);
100 static void swirl(void)
102 static const FLASH
char swirlchar
[] = { '-','\\','|','/' };
103 static uint_fast8_t cnt
;
104 static uint32_t tstamp
;
106 if (get_timer(0) > tstamp
) {
107 printf_P(PSTR("\b%c"), swirlchar
[cnt
]);
108 cnt
= (cnt
+1) % ARRAY_SIZE(swirlchar
);
109 tstamp
= get_timer(0) + 250;
113 /* Work register for fs command */
116 WORD AccFiles
, AccDirs
;
122 char *path
, /* Pointer to the working buffer with start path */
123 struct stat_dat_s
*statp
131 res
= f_opendir(&dirs
, path
);
135 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
136 statp
->Finfo
.fname
[0]) {
137 if (FF_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
139 fn
= statp
->Finfo
.fname
;
140 if (statp
->Finfo
.fattrib
& AM_DIR
) {
143 strcpy(path
+i
+1, fn
);
144 res
= scan_files(path
, statp
);
149 //printf_P(PSTR("%s/%s\n"), path, fn);
151 statp
->AccSize
+= statp
->Finfo
.fsize
;
165 * fatstat path - Show logical drive status
168 command_ret_t
do_stat(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
174 struct stat_dat_s statp
;
176 (void) cmdtp
; (void) flag
; (void) argc
;
178 path
= (char *) malloc(BUFFER_SIZE
);
180 printf_P(PSTR("fat stat: Out of Memory!\n"));
182 return CMD_RET_FAILURE
;
185 res
= f_getfree(argv
[1], &nfreeclst
, &fs
);
189 "Bytes/Cluster: %lu\n"
190 "Number of FATs: %u\n"
191 "Root DIR entries: %u\n"
193 "Number of clusters: %lu\n"
194 "FAT start (lba): %lu\n"
195 "DIR start (lba,cluster): %lu\n"
196 "Data start (lba): %lu\n"),
197 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
198 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
199 fs
->fatbase
, fs
->dirbase
, fs
->database
);
204 res
= f_getlabel(argv
[1], label
, &serial
);
208 "Volume S/N: %04X-%04X\n"),
209 label
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
213 my_puts_P(PSTR("\nCounting... "));
214 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
215 strcpy(path
, argv
[1]);
217 res
= scan_files(path
, &statp
);
220 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
221 "%lu KB total disk space.\n%lu KB available.\n"),
222 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
223 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
231 return CMD_RET_FAILURE
;
233 return CMD_RET_SUCCESS
;
237 * pwd - Print current directory of the current drive.
240 command_ret_t
do_pwd(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
245 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
247 buf
= (char *) malloc(BUFFER_SIZE
);
249 printf_P(PSTR("pwd: Out of Memory!\n"));
251 return CMD_RET_FAILURE
;
254 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
262 return CMD_RET_FAILURE
;
264 return CMD_RET_SUCCESS
;
269 * cd - Change the current/working directory.
272 command_ret_t
do_cd(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
277 (void) cmdtp
; (void) flag
; (void) argc
;
280 arg
= getenv_str(PSTR(ENV_HOME
));
282 printf_P(PSTR("%s: \"%S\" is not set\n"), argv
[0], PSTR(ENV_HOME
));
283 return CMD_RET_FAILURE
;
293 res
= f_chdrive(drv
);
301 return CMD_RET_FAILURE
;
303 return CMD_RET_SUCCESS
;
308 * fatls path - Directory listing
311 command_ret_t
do_ls(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
314 DIR Dir
; /* Directory object */
321 (void) cmdtp
; (void) flag
; (void) argc
;
323 buf
= (char *) malloc(BUFFER_SIZE
);
325 printf_P(PSTR("pwd: Out of Memory!\n"));
327 return CMD_RET_FAILURE
;
331 res
= f_getcwd(buf
, BUFFER_SIZE
); /* Get current directory path */
333 strncpy(buf
, argv
[1], BUFFER_SIZE
);
336 res
= f_opendir(&Dir
, buf
);
340 return CMD_RET_FAILURE
;
345 res
= f_readdir(&Dir
, &Finfo
);
346 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
348 if (Finfo
.fattrib
& AM_DIR
) {
351 s1
++; p1
+= Finfo
.fsize
;
353 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
354 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
355 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
356 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
357 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
358 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
359 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
360 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63,
361 Finfo
.fsize
, Finfo
.fname
);
367 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
368 if (f_getfree(buf
, (DWORD
*)&p1
, &fs
) == FR_OK
)
369 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
375 return CMD_RET_FAILURE
;
378 return CMD_RET_SUCCESS
;
382 FRESULT
mkpath(TCHAR
*path
)
391 res
= f_stat (path
, &fd
)
393 p
= strchr(path
, ':');
394 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
397 while ((q
= strchr(p
, '/')) != NULL
) {
401 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
411 * fatread/write - load binary file to/from a dos filesystem
412 * read <d:/path/filename> <addr> [bytes [pos]]
413 * write <d:/path/filename> <addr> <bytes>
415 command_ret_t
do_rw(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
421 unsigned long bytes_rw
;
423 bool dowrite
= (argv
[0][0] == 'w');
429 (void) cmdtp
; (void) flag
;
431 if (argc
< (dowrite
? 4 : 3))
432 return CMD_RET_USAGE
;
434 addr
= eval_arg(argv
[2], NULL
);
435 if (addr
>= MAX_MEMORY
) {
436 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
437 return CMD_RET_FAILURE
;
440 bytes
= eval_arg(argv
[3], NULL
);
444 pos
= eval_arg(argv
[4], NULL
);
448 if (addr
+ bytes
> MAX_MEMORY
)
449 bytes
= MAX_MEMORY
- addr
;
451 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
452 if (buffer
== NULL
) {
453 printf_P(PSTR("fatstat: Out of Memory!\n"));
455 return CMD_RET_FAILURE
;
459 res
= mkpath(argv
[1]);
462 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
466 res
= f_lseek(&File
, pos
);
469 timer
= get_timer(0);
471 unsigned int cnt
, br
;
473 if (bytes
>= BUFFER_SIZE
) {
475 bytes
-= BUFFER_SIZE
;
477 cnt
= bytes
; bytes
= 0;
480 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
484 z80_read_block(buffer
, addr
, cnt
);
485 z80_bus_cmd(Release
);
486 res
= f_write(&File
, buffer
, cnt
, &br
);
490 res
= f_read(&File
, buffer
, cnt
, &br
);
493 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
497 z80_write_block(buffer
, addr
, br
);
498 z80_bus_cmd(Release
);
505 printf_P(PSTR("Disk full?\n"));
512 FRESULT fr
= f_close(&File
);
515 timer
= get_timer(timer
);
516 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
517 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
525 my_puts_P(PSTR("Bus timeout\n"));
529 return CMD_RET_FAILURE
;
531 return CMD_RET_SUCCESS
;
535 cmd_tbl_t cmd_tbl_fat
[] = {
537 stat
, 2, CTBL_RPT
, do_stat
,
538 "Show logical drive status",
542 pwd
, 2, CTBL_RPT
, do_pwd
,
543 "Print name of current/working directory",
548 "Change the current/working directory.",
552 ls
, 2, CTBL_RPT
, do_ls
,
558 "load binary file from a dos filesystem",
559 "<d:/path/filename> <addr> [bytes [pos]]\n"
560 " - Load binary file 'path/filename' on logical drive 'd'\n"
561 " to address 'addr' from dos filesystem.\n"
562 " 'pos' gives the file position to start loading from.\n"
563 " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
564 " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
565 " the load stops on end of file."
569 "write file into a dos filesystem",
570 "<d:/path/filename> <addr> <bytes>\n"
571 " - Write file to 'path/filename' on logical drive 'd' from RAM\n"
572 " starting at address 'addr'.\n"
576 help
, CONFIG_SYS_MAXARGS
, CTBL_RPT
, do_help
,
577 "Print sub command description/usage",
579 " - print brief description of all sub commands\n"
580 "fat help command ...\n"
581 " - print detailed usage of sub cmd 'command'"
584 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
585 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
586 FSTR("Alias for 'help'"),
587 #ifdef CONFIG_SYS_LONGHELP
589 #endif /* CONFIG_SYS_LONGHELP */
591 #ifdef CONFIG_AUTO_COMPLETE
595 /* Mark end of table */
596 CMD_TBL_END(cmd_tbl_fat
)
600 command_ret_t
do_fat(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
602 puts_P(PSTR("Huch?"));
604 return CMD_RET_USAGE
;