]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
10 #include "print-utils.h"
15 #define MAX_MEMORY (1ul << 20)
17 DWORD
get_fattime (void)
24 static const FLASH
char * const FLASH rc_names
[] = {
34 FSTR("INVALID_OBJECT"),
35 FSTR("WRITE_PROTECTED"),
36 FSTR("INVALID_DRIVE"),
38 FSTR("NO_FILE_SYSTEM"),
42 FSTR("NOT_ENOUGH_CORE"),
43 FSTR("TOO_MANY_OPEN_FILES")
47 void put_rc (FRESULT rc
)
50 printf_P(PSTR("rc=%u FR_"), rc
);
51 my_puts_P(rc_names
[rc
]);
52 my_puts_P(PSTR("\n"));
54 printf_P(PSTR("rc=%u FR_%S\n"), rc
, rc_names
[rc
]);
58 /* Work register for fs command */
60 static WORD AccFiles
, AccDirs
;
62 char Lfname
[_MAX_LFN
+1];
65 .lfsize
= sizeof Lfname
73 char* path
/* Pointer to the working buffer with start path */
81 res
= f_opendir(&dirs
, path
);
85 while (((res
= f_readdir(&dirs
, &Finfo
)) == FR_OK
) && Finfo
.fname
[0]) {
86 if (_FS_RPATH
&& Finfo
.fname
[0] == '.') continue;
88 fn
= *Finfo
.lfname
? Finfo
.lfname
: Finfo
.fname
;
92 if (Finfo
.fattrib
& AM_DIR
) {
94 path
[i
] = '/'; strcpy(path
+i
+1, fn
);
95 res
= scan_files(path
);
97 if (res
!= FR_OK
) break;
99 // printf_P(PSTR("%s/%s\n"), path, fn);
101 AccSize
+= Finfo
.fsize
;
111 * fatstat path - Show logical drive status
114 command_ret_t
do_fat_stat(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
121 (void) cmdtp
; (void) flag
; (void) argc
;
123 FatFs
= (FATFS
*) malloc(sizeof (FATFS
));
124 buffer
= (char *) malloc(512);
125 if (FatFs
== NULL
|| buffer
== NULL
) {
126 printf_P(PSTR("fatstat: Out of Memory!\n"));
129 return CMD_RET_FAILURE
;
132 res
= f_mount(FatFs
, argv
[1], 0);
134 res
= f_getfree(argv
[1], &nfreeclst
, &fs
);
138 "Bytes/Cluster: %lu\n"
139 "Number of FATs: %u\n"
140 "Root DIR entries: %u\n"
142 "Number of clusters: %lu\n"
143 "FAT start (lba): %lu\n"
144 "DIR start (lba,cluster): %lu\n"
145 "Data start (lba): %lu\n"),
146 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
147 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
148 fs
->fatbase
, fs
->dirbase
, fs
->database
);
153 res
= f_getlabel(argv
[1], label
, &serial
);
157 "Volume S/N: %04X-%04X\n"),
158 label
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
162 my_puts_P(PSTR("\n..."));
163 AccSize
= AccFiles
= AccDirs
= 0;
165 strcpy(buffer
, argv
[1]);
167 res
= scan_files(buffer
);
170 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
171 "%lu KB total disk space.\n%lu KB available.\n"),
172 AccFiles
, AccSize
, AccDirs
,
173 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
179 f_mount(NULL
, argv
[1], 0);
182 return CMD_RET_FAILURE
;
184 return CMD_RET_SUCCESS
;
189 * fatls path - Directory listing
192 command_ret_t
do_fat_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
195 DIR Dir
; /* Directory object */
201 char Lfname
[_MAX_LFN
+1];
202 Finfo
.lfname
= Lfname
;
203 Finfo
.lfsize
= sizeof Lfname
;
206 (void) cmdtp
; (void) flag
; (void) argc
;
208 res
= f_mount(&FatFs
, argv
[1], 0);
210 res
= f_opendir(&Dir
, argv
[1]);
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"),
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
[0]));
236 for (int i
= strlen(Finfo
.fname
); i
< 14; i
++)
238 printf_P(PSTR("%s\n"), Lfname
);
245 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
246 if (f_getfree(argv
[1], (DWORD
*)&p1
, &fs
) == FR_OK
)
247 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
252 return CMD_RET_FAILURE
;
255 return CMD_RET_SUCCESS
;
260 * fatread - load binary file from a dos filesystem
261 * <d:/path/filename> <addr> [bytes [pos]]
263 command_ret_t
do_fat_read(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
267 unsigned long bytes
= 0x80000;
268 unsigned long pos
= 0;
269 unsigned long bytes_read
;
276 (void) cmdtp
; (void) flag
;
279 return CMD_RET_USAGE
;
281 addr
= strtoul(argv
[2], 0, 16);
283 bytes
= strtoul(argv
[3], 0, 16);
285 pos
= strtoul(argv
[4], 0, 16);
287 res
= f_mount(&FatFs
, argv
[1], 0);
289 res
= f_open(&File
, argv
[1], FA_READ
);
292 res
= f_lseek(&File
, pos
);
295 timer
= get_timer(0);
297 unsigned int cnt
, br
;
299 if (bytes
>= sizeof buffer
) {
301 bytes
-= sizeof buffer
;
303 cnt
= bytes
; bytes
= 0;
305 res
= f_read(&File
, buffer
, cnt
, &br
);
308 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
312 z80_write_block(buffer
, addr
, br
);
313 z80_bus_cmd(Release
);
320 timer
= get_timer(timer
);
321 printf_P(PSTR("%lu bytes read with %lu bytes/sec.\n"),
322 bytes_read
, timer
? (bytes_read
* 1000 / timer
) : 0);
325 f_mount(NULL
, argv
[1], 0);
329 my_puts_P(PSTR("Bus timeout\n"));
333 return CMD_RET_FAILURE
;
335 return CMD_RET_SUCCESS
;
339 * fatwrite - write file into a dos filesystem
340 * <d:/path/filename> <addr> <bytes>
342 command_ret_t
do_fat_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
347 unsigned long bytes_written
;
354 (void) cmdtp
; (void) flag
;
357 return CMD_RET_USAGE
;
359 addr
= strtoul(argv
[2], 0, 16);
360 bytes
= strtoul(argv
[3], 0, 16);
362 res
= f_mount(&FatFs
, argv
[1], 0);
364 res
= f_open(&File
, argv
[1], FA_CREATE_ALWAYS
| FA_WRITE
);
367 timer
= get_timer(0);
369 unsigned int cnt
, br
;
371 if (bytes
>= sizeof buffer
) {
373 bytes
-= sizeof buffer
;
375 cnt
= bytes
; bytes
= 0;
377 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
381 z80_read_block(buffer
, addr
, cnt
);
382 z80_bus_cmd(Release
);
383 res
= f_write(&File
, buffer
, cnt
, &br
);
391 res
= f_close(&File
);
392 timer
= get_timer(timer
);
393 printf_P(PSTR("%lu bytes written with %lu bytes/sec.\n"),
394 bytes_written
, timer
? (bytes_written
* 1000 / timer
) : 0);
396 f_mount(NULL
, argv
[1], 0);
400 my_puts_P(PSTR("Bus timeout\n"));
404 return CMD_RET_FAILURE
;
406 return CMD_RET_SUCCESS
;
410 * fatread/write - load binary file to/from a dos filesystem
411 * read <d:/path/filename> <addr> [bytes [pos]]
412 * write <d:/path/filename> <addr> <bytes>
414 command_ret_t
do_fat_rw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
421 unsigned long bytes_rw
;
423 bool dowrite
= (argv
[0][3] == 'w');
429 (void) cmdtp
; (void) flag
;
431 if (argc
< (dowrite
? 4 : 3))
432 return CMD_RET_USAGE
;
434 addr
= strtoul(argv
[2], 0, 16);
435 if (addr
>= MAX_MEMORY
) {
436 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
437 return CMD_RET_FAILURE
;
440 bytes
= strtoul(argv
[3], 0, 16);
444 pos
= strtoul(argv
[4], 0, 16);
448 if (addr
+ bytes
> MAX_MEMORY
)
449 bytes
= MAX_MEMORY
- addr
;
451 res
= f_mount(&FatFs
, argv
[1], 0);
453 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
457 res
= f_lseek(&File
, pos
);
460 timer
= get_timer(0);
462 unsigned int cnt
, br
;
464 if (bytes
>= sizeof buffer
) {
466 bytes
-= sizeof buffer
;
468 cnt
= bytes
; bytes
= 0;
471 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
475 z80_read_block(buffer
, addr
, cnt
);
476 z80_bus_cmd(Release
);
477 res
= f_write(&File
, buffer
, cnt
, &br
);
481 res
= f_read(&File
, buffer
, cnt
, &br
);
484 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
488 z80_write_block(buffer
, addr
, br
);
489 z80_bus_cmd(Release
);
496 printf_P(PSTR("Disk full?\n"));
500 printf_P(PSTR("Abort\n"));
505 FRESULT fr
= f_close(&File
);
508 timer
= get_timer(timer
);
509 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
510 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
513 f_mount(NULL
, argv
[1], 0);
517 my_puts_P(PSTR("Bus timeout\n"));
521 return CMD_RET_FAILURE
;
523 return CMD_RET_SUCCESS
;