]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0+
8 * FAT filesystem commands
19 #include "con-utils.h"
20 #include "print-utils.h"
26 #define MAX_MEMORY (1ul << 20)
27 #define BUFFER_SIZE 512
30 DWORD
get_fattime (void)
36 gmtime_r(&timer
, &tm_timer
);
38 return fatfs_time(&tm_timer
);
42 static bool check_abort(void)
47 printf_P(PSTR("Abort\n"));
53 static const FLASH
char * const FLASH rc_names
[] = {
63 FSTR("INVALID_OBJECT"),
64 FSTR("WRITE_PROTECTED"),
65 FSTR("INVALID_DRIVE"),
67 FSTR("NO_FILE_SYSTEM"),
71 FSTR("NOT_ENOUGH_CORE"),
72 FSTR("TOO_MANY_OPEN_FILES")
76 void put_rc (FRESULT rc
)
78 if (rc
< ARRAY_SIZE(rc_names
)) {
80 printf_P(PSTR("rc=%u FR_"), rc
);
81 my_puts_P(rc_names
[rc
]);
82 my_puts_P(PSTR("\n"));
84 printf_P(PSTR("rc=%u FR_%S\n"), rc
, rc_names
[rc
]);
90 static void swirl(void)
92 static const FLASH
char swirlchar
[] = { '-','\\','|','/' };
93 static uint_fast8_t cnt
;
94 static uint32_t tstamp
;
96 if (get_timer(0) > tstamp
) {
97 printf_P(PSTR("\b%c"), swirlchar
[cnt
]);
98 cnt
= (cnt
+1) % ARRAY_SIZE(swirlchar
);
99 tstamp
= get_timer(0) + 250;
103 /* Work register for fs command */
106 WORD AccFiles
, AccDirs
;
112 char *path
, /* Pointer to the working buffer with start path */
113 struct stat_dat_s
*statp
121 res
= f_opendir(&dirs
, path
);
125 while (((res
= f_readdir(&dirs
, &statp
->Finfo
)) == FR_OK
) &&
126 statp
->Finfo
.fname
[0]) {
127 if (_FS_RPATH
&& statp
->Finfo
.fname
[0] == '.')
129 fn
= statp
->Finfo
.fname
;
130 if (statp
->Finfo
.fattrib
& AM_DIR
) {
133 strcpy(path
+i
+1, fn
);
134 res
= scan_files(path
, statp
);
139 //printf_P(PSTR("%s/%s\n"), path, fn);
141 statp
->AccSize
+= statp
->Finfo
.fsize
;
155 * fatstat path - Show logical drive status
158 command_ret_t
do_fat_stat(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
164 struct stat_dat_s statp
;
166 (void) cmdtp
; (void) flag
; (void) argc
;
168 FatFs
= (FATFS
*) malloc(sizeof (FATFS
));
169 path
= (char *) malloc(BUFFER_SIZE
);
170 if (FatFs
== NULL
|| path
== NULL
) {
171 printf_P(PSTR("fatstat: Out of Memory!\n"));
174 return CMD_RET_FAILURE
;
177 res
= f_mount(FatFs
, argv
[1], 0);
179 res
= f_getfree(argv
[1], &nfreeclst
, &fs
);
183 "Bytes/Cluster: %lu\n"
184 "Number of FATs: %u\n"
185 "Root DIR entries: %u\n"
187 "Number of clusters: %lu\n"
188 "FAT start (lba): %lu\n"
189 "DIR start (lba,cluster): %lu\n"
190 "Data start (lba): %lu\n"),
191 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
192 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
193 fs
->fatbase
, fs
->dirbase
, fs
->database
);
198 res
= f_getlabel(argv
[1], label
, &serial
);
202 "Volume S/N: %04X-%04X\n"),
203 label
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
207 my_puts_P(PSTR("\nCounting... "));
208 statp
.AccSize
= statp
.AccFiles
= statp
.AccDirs
= 0;
209 strcpy(path
, argv
[1]);
211 res
= scan_files(path
, &statp
);
214 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
215 "%lu KB total disk space.\n%lu KB available.\n"),
216 statp
.AccFiles
, statp
.AccSize
, statp
.AccDirs
,
217 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
225 f_mount(NULL
, argv
[1], 0);
228 return CMD_RET_FAILURE
;
230 return CMD_RET_SUCCESS
;
235 * fatls path - Directory listing
238 command_ret_t
do_fat_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
241 DIR Dir
; /* Directory object */
247 char Lfname
[_MAX_LFN
+1];
248 Finfo
.lfname
= Lfname
;
249 Finfo
.lfsize
= sizeof Lfname
;
252 (void) cmdtp
; (void) flag
; (void) argc
;
254 res
= f_mount(&FatFs
, argv
[1], 0);
256 res
= f_opendir(&Dir
, argv
[1]);
259 return CMD_RET_FAILURE
;
264 res
= f_readdir(&Dir
, &Finfo
);
265 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
267 if (Finfo
.fattrib
& AM_DIR
) {
270 s1
++; p1
+= Finfo
.fsize
;
272 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu "),
273 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
274 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
275 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
276 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
277 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
278 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
279 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63, Finfo
.fsize
);
281 printf_P(PSTR("%s\n"), *Lfname
? Lfname
: Finfo
.fname
);
283 printf_P(PSTR("%s\n"), Finfo
.fname
);
290 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
291 if (f_getfree(argv
[1], (DWORD
*)&p1
, &fs
) == FR_OK
)
292 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
297 return CMD_RET_FAILURE
;
300 return CMD_RET_SUCCESS
;
304 FRESULT
mkpath(TCHAR
*path
)
318 res
= f_stat (path
, &fd
)
320 p
= strchr(path
, ':');
321 if (p
== NULL
|| *++p
== '\0' || *p
++ != '/')
324 while ((q
= strchr(p
, '/')) != NULL
) {
328 if (ret
!= FR_OK
&& ret
!= FR_EXIST
)
338 * fatread/write - load binary file to/from a dos filesystem
339 * read <d:/path/filename> <addr> [bytes [pos]]
340 * write <d:/path/filename> <addr> <bytes>
342 command_ret_t
do_fat_rw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
349 unsigned long bytes_rw
;
351 bool dowrite
= (argv
[0][3] == 'w');
357 (void) cmdtp
; (void) flag
;
359 if (argc
< (dowrite
? 4 : 3))
360 return CMD_RET_USAGE
;
362 addr
= strtoul(argv
[2], 0, 16);
363 if (addr
>= MAX_MEMORY
) {
364 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
365 return CMD_RET_FAILURE
;
368 bytes
= strtoul(argv
[3], 0, 16);
372 pos
= strtoul(argv
[4], 0, 16);
376 if (addr
+ bytes
> MAX_MEMORY
)
377 bytes
= MAX_MEMORY
- addr
;
379 FatFs
= (FATFS
*) malloc(sizeof (FATFS
));
380 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
381 if (FatFs
== NULL
|| buffer
== NULL
) {
382 printf_P(PSTR("fatstat: Out of Memory!\n"));
385 return CMD_RET_FAILURE
;
388 res
= f_mount(FatFs
, argv
[1], 0);
392 res
= mkpath(argv
[1]);
396 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
400 res
= f_lseek(&File
, pos
);
403 timer
= get_timer(0);
405 unsigned int cnt
, br
;
407 if (bytes
>= BUFFER_SIZE
) {
409 bytes
-= BUFFER_SIZE
;
411 cnt
= bytes
; bytes
= 0;
414 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
418 z80_read_block(buffer
, addr
, cnt
);
419 z80_bus_cmd(Release
);
420 res
= f_write(&File
, buffer
, cnt
, &br
);
424 res
= f_read(&File
, buffer
, cnt
, &br
);
427 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
431 z80_write_block(buffer
, addr
, br
);
432 z80_bus_cmd(Release
);
439 printf_P(PSTR("Disk full?\n"));
446 FRESULT fr
= f_close(&File
);
449 timer
= get_timer(timer
);
450 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
451 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
454 f_mount(NULL
, argv
[1], 0);
461 my_puts_P(PSTR("Bus timeout\n"));
465 return CMD_RET_FAILURE
;
467 return CMD_RET_SUCCESS
;