]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
10 #include "print-utils.h"
16 #define MAX_MEMORY (1ul << 20)
17 #define BUFFER_SIZE 512
20 DWORD
get_fattime (void)
26 gmtime_r(&timer
, &tm_timer
);
28 return fatfs_time(&tm_timer
);
32 static bool check_abort(void)
37 printf_P(PSTR("Abort\n"));
43 static const FLASH
char * const FLASH rc_names
[] = {
53 FSTR("INVALID_OBJECT"),
54 FSTR("WRITE_PROTECTED"),
55 FSTR("INVALID_DRIVE"),
57 FSTR("NO_FILE_SYSTEM"),
61 FSTR("NOT_ENOUGH_CORE"),
62 FSTR("TOO_MANY_OPEN_FILES")
66 void put_rc (FRESULT rc
)
68 if (rc
< ARRAY_SIZE(rc_names
)) {
70 printf_P(PSTR("rc=%u FR_"), rc
);
71 my_puts_P(rc_names
[rc
]);
72 my_puts_P(PSTR("\n"));
74 printf_P(PSTR("rc=%u FR_%S\n"), rc
, rc_names
[rc
]);
79 /* Work register for fs command */
81 static WORD AccFiles
, AccDirs
;
83 char Lfname
[_MAX_LFN
+1];
86 .lfsize
= sizeof Lfname
92 static const FLASH
char swirlchar
[] = { '-','\\','|','/' };
94 static void swirl(void)
96 static uint_fast8_t cnt
;
97 static uint32_t tstamp
;
99 if (get_timer(0) > tstamp
) {
100 printf_P(PSTR("\b%c"), swirlchar
[cnt
]);
101 cnt
= (cnt
+1) % ARRAY_SIZE(swirlchar
);
102 tstamp
= get_timer(0) + 300;
108 char* path
/* Pointer to the working buffer with start path */
116 res
= f_opendir(&dirs
, path
);
120 while (((res
= f_readdir(&dirs
, &Finfo
)) == FR_OK
) && Finfo
.fname
[0]) {
121 if (_FS_RPATH
&& Finfo
.fname
[0] == '.') continue;
123 fn
= *Finfo
.lfname
? Finfo
.lfname
: Finfo
.fname
;
127 if (Finfo
.fattrib
& AM_DIR
) {
129 path
[i
] = '/'; strcpy(path
+i
+1, fn
);
130 res
= scan_files(path
);
132 if (res
!= FR_OK
) break;
134 // printf_P(PSTR("%s/%s\n"), path, fn);
136 AccSize
+= Finfo
.fsize
;
150 * fatstat path - Show logical drive status
153 command_ret_t
do_fat_stat(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
160 (void) cmdtp
; (void) flag
; (void) argc
;
162 FatFs
= (FATFS
*) malloc(sizeof (FATFS
));
163 buffer
= (char *) malloc(BUFFER_SIZE
);
164 if (FatFs
== NULL
|| buffer
== NULL
) {
165 printf_P(PSTR("fatstat: Out of Memory!\n"));
168 return CMD_RET_FAILURE
;
171 res
= f_mount(FatFs
, argv
[1], 0);
173 res
= f_getfree(argv
[1], &nfreeclst
, &fs
);
177 "Bytes/Cluster: %lu\n"
178 "Number of FATs: %u\n"
179 "Root DIR entries: %u\n"
181 "Number of clusters: %lu\n"
182 "FAT start (lba): %lu\n"
183 "DIR start (lba,cluster): %lu\n"
184 "Data start (lba): %lu\n"),
185 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
186 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
187 fs
->fatbase
, fs
->dirbase
, fs
->database
);
192 res
= f_getlabel(argv
[1], label
, &serial
);
196 "Volume S/N: %04X-%04X\n"),
197 label
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
201 my_puts_P(PSTR("\nCounting... "));
202 AccSize
= AccFiles
= AccDirs
= 0;
204 strcpy(buffer
, argv
[1]);
206 res
= scan_files(buffer
);
209 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
210 "%lu KB total disk space.\n%lu KB available.\n"),
211 AccFiles
, AccSize
, AccDirs
,
212 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), nfreeclst
* (fs
->csize
/ 2)
218 f_mount(NULL
, argv
[1], 0);
223 return CMD_RET_FAILURE
;
225 return CMD_RET_SUCCESS
;
230 * fatls path - Directory listing
233 command_ret_t
do_fat_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
236 DIR Dir
; /* Directory object */
242 char Lfname
[_MAX_LFN
+1];
243 Finfo
.lfname
= Lfname
;
244 Finfo
.lfsize
= sizeof Lfname
;
247 (void) cmdtp
; (void) flag
; (void) argc
;
249 res
= f_mount(&FatFs
, argv
[1], 0);
251 res
= f_opendir(&Dir
, argv
[1]);
254 return CMD_RET_FAILURE
;
259 res
= f_readdir(&Dir
, &Finfo
);
260 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
262 if (Finfo
.fattrib
& AM_DIR
) {
265 s1
++; p1
+= Finfo
.fsize
;
267 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu "),
268 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
269 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
270 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
271 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
272 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
273 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
274 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63, Finfo
.fsize
);
276 printf_P(PSTR("%s\n"), *Lfname
? Lfname
: Finfo
.fname
);
278 printf_P(PSTR("%s\n"), Finfo
.fname
);
285 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
286 if (f_getfree(argv
[1], (DWORD
*)&p1
, &fs
) == FR_OK
)
287 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
292 return CMD_RET_FAILURE
;
295 return CMD_RET_SUCCESS
;
300 * fatread/write - load binary file to/from a dos filesystem
301 * read <d:/path/filename> <addr> [bytes [pos]]
302 * write <d:/path/filename> <addr> <bytes>
304 command_ret_t
do_fat_rw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
311 unsigned long bytes_rw
;
313 bool dowrite
= (argv
[0][3] == 'w');
319 (void) cmdtp
; (void) flag
;
321 if (argc
< (dowrite
? 4 : 3))
322 return CMD_RET_USAGE
;
324 addr
= strtoul(argv
[2], 0, 16);
325 if (addr
>= MAX_MEMORY
) {
326 printf_P(PSTR("address too high: 0x%0lx\n"), addr
);
327 return CMD_RET_FAILURE
;
330 bytes
= strtoul(argv
[3], 0, 16);
334 pos
= strtoul(argv
[4], 0, 16);
338 if (addr
+ bytes
> MAX_MEMORY
)
339 bytes
= MAX_MEMORY
- addr
;
341 FatFs
= (FATFS
*) malloc(sizeof (FATFS
));
342 buffer
= (uint8_t *) malloc(BUFFER_SIZE
);
343 if (FatFs
== NULL
|| buffer
== NULL
) {
344 printf_P(PSTR("fatstat: Out of Memory!\n"));
347 return CMD_RET_FAILURE
;
350 res
= f_mount(FatFs
, argv
[1], 0);
352 res
= f_open(&File
, argv
[1], dowrite
? FA_WRITE
| FA_CREATE_ALWAYS
356 res
= f_lseek(&File
, pos
);
359 timer
= get_timer(0);
361 unsigned int cnt
, br
;
363 if (bytes
>= BUFFER_SIZE
) {
365 bytes
-= BUFFER_SIZE
;
367 cnt
= bytes
; bytes
= 0;
370 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
374 z80_read_block(buffer
, addr
, cnt
);
375 z80_bus_cmd(Release
);
376 res
= f_write(&File
, buffer
, cnt
, &br
);
380 res
= f_read(&File
, buffer
, cnt
, &br
);
383 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
387 z80_write_block(buffer
, addr
, br
);
388 z80_bus_cmd(Release
);
395 printf_P(PSTR("Disk full?\n"));
402 FRESULT fr
= f_close(&File
);
405 timer
= get_timer(timer
);
406 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
407 bytes_rw
, bytes_rw
, timer
? (bytes_rw
* 1000 / timer
) : 0);
410 f_mount(NULL
, argv
[1], 0);
417 my_puts_P(PSTR("Bus timeout\n"));
421 return CMD_RET_FAILURE
;
423 return CMD_RET_SUCCESS
;