]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
9222c1bf91d4ae61086288c315a5aea9a0874fae
9 #include "print-utils.h"
13 DWORD
get_fattime (void)
19 static const FLASH
char * const FLASH rc_names
[] = {
29 FSTR("INVALID_OBJECT"),
30 FSTR("WRITE_PROTECTED"),
31 FSTR("INVALID_DRIVE"),
33 FSTR("NO_FILE_SYSTEM"),
37 FSTR("NOT_ENOUGH_CORE"),
38 FSTR("TOO_MANY_OPEN_FILES")
42 void put_rc (FRESULT rc
)
45 printf_P(PSTR("rc=%u FR_"), rc
);
46 my_puts_P(rc_names
[rc
]);
47 my_puts_P(PSTR("\n"));
49 printf_P(PSTR("rc=%u FR_%S\n"), rc
, rc_names
[rc
]);
53 /* Work register for fs command */
55 static WORD AccFiles
, AccDirs
;
60 char* path
/* Pointer to the working buffer with start path */
68 res
= f_opendir(&dirs
, path
);
71 while (((res
= f_readdir(&dirs
, &Finfo
)) == FR_OK
) && Finfo
.fname
[0]) {
72 if (_FS_RPATH
&& Finfo
.fname
[0] == '.') continue;
74 fn
= *Finfo
.lfname
? Finfo
.lfname
: Finfo
.fname
;
78 if (Finfo
.fattrib
& AM_DIR
) {
80 path
[i
] = '/'; strcpy(path
+i
+1, fn
);
81 res
= scan_files(path
);
83 if (res
!= FR_OK
) break;
85 // printf_P(PSTR("%s/%s\n"), path, fn);
87 AccSize
+= Finfo
.fsize
;
97 * fatstat path - Show logical drive status
100 command_ret_t
do_fat_stat(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
107 (void) cmdtp
; (void) flag
; (void) argc
;
109 res
= f_mount(&FatFs
, argv
[1], 0);
111 res
= f_getfree(argv
[1], &p2
, &fs
);
114 return CMD_RET_FAILURE
;
119 "Bytes/Cluster: %lu\n"
120 "Number of FATs: %u\n"
121 "Root DIR entries: %u\n"
123 "Number of clusters: %lu\n"
124 "FAT start (lba): %lu\n"
125 "DIR start (lba,cluster): %lu\n"
126 "Data start (lba): %lu\n"),
127 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
128 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
129 fs
->fatbase
, fs
->dirbase
, fs
->database
);
133 res
= f_getlabel(argv
[1], label
, &p1
);
136 return CMD_RET_FAILURE
;
140 "Volume S/N: %04X-%04X\n"),
141 label
, (WORD
)((DWORD
)p1
>> 16), (WORD
)(p1
& 0xFFFF));
143 my_puts_P(PSTR("\n..."));
144 AccSize
= AccFiles
= AccDirs
= 0;
146 strcpy(buffer
, argv
[1]);
148 res
= scan_files(buffer
);
151 return CMD_RET_FAILURE
;
153 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
154 "%lu KB total disk space.\n%lu KB available.\n"),
155 AccFiles
, AccSize
, AccDirs
,
156 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), p2
* (fs
->csize
/ 2)
159 res
= f_mount(NULL
, argv
[1], 0);
162 return CMD_RET_FAILURE
;
164 return CMD_RET_SUCCESS
;
169 * fatls path - Directory listing
172 command_ret_t
do_fat_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
175 DIR Dir
; /* Directory object */
181 char Lfname
[_MAX_LFN
+1];
182 Finfo
.lfname
= Lfname
;
183 Finfo
.lfsize
= sizeof Lfname
;
186 (void) cmdtp
; (void) flag
; (void) argc
;
188 res
= f_mount(&FatFs
, argv
[1], 0);
190 res
= f_opendir(&Dir
, argv
[1]);
193 return CMD_RET_FAILURE
;
198 res
= f_readdir(&Dir
, &Finfo
);
199 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
201 if (Finfo
.fattrib
& AM_DIR
) {
204 s1
++; p1
+= Finfo
.fsize
;
206 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s"),
207 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
208 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
209 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
210 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
211 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
212 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
213 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63,
214 Finfo
.fsize
, &(Finfo
.fname
[0]));
216 for (int i
= strlen(Finfo
.fname
); i
< 14; i
++)
218 printf_P(PSTR("%s\n"), Lfname
);
225 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
226 if (f_getfree(argv
[1], (DWORD
*)&p1
, &fs
) == FR_OK
)
227 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
232 return CMD_RET_FAILURE
;
235 return CMD_RET_SUCCESS
;
240 * fatread - load binary file from a dos filesystem
241 * <d:/path/filename> <addr> [bytes [pos]]
243 command_ret_t
do_fat_read(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
247 unsigned long bytes
= 0x80000;
248 unsigned long pos
= 0;
249 unsigned long bytes_read
;
256 (void) cmdtp
; (void) flag
;
259 return CMD_RET_USAGE
;
261 addr
= strtoul(argv
[2], 0, 16);
263 bytes
= strtoul(argv
[3], 0, 16);
265 pos
= strtoul(argv
[4], 0, 16);
267 res
= f_mount(&FatFs
, argv
[1], 0);
269 res
= f_open(&File
, argv
[1], FA_READ
);
272 res
= f_lseek(&File
, pos
);
275 timer
= get_timer(0);
277 unsigned int cnt
, br
;
279 if (bytes
>= sizeof buffer
) {
281 bytes
-= sizeof buffer
;
283 cnt
= bytes
; bytes
= 0;
285 res
= f_read(&File
, buffer
, cnt
, &br
);
288 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
292 z80_write_block(buffer
, addr
, br
);
293 z80_bus_cmd(Release
);
300 timer
= get_timer(timer
);
301 printf_P(PSTR("%lu bytes read with %lu bytes/sec.\n"),
302 bytes_read
, timer
? (bytes_read
* 1000 / timer
) : 0);
305 f_mount(NULL
, argv
[1], 0);
309 my_puts_P(PSTR("Bus timeout\n"));
313 return CMD_RET_FAILURE
;
315 return CMD_RET_SUCCESS
;
319 * fatwrite - write file into a dos filesystem
320 * <d:/path/filename> <addr> <bytes>
322 command_ret_t
do_fat_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
327 unsigned long bytes_written
;
334 (void) cmdtp
; (void) flag
;
337 return CMD_RET_USAGE
;
339 addr
= strtoul(argv
[2], 0, 16);
340 bytes
= strtoul(argv
[3], 0, 16);
342 res
= f_mount(&FatFs
, argv
[1], 0);
344 res
= f_open(&File
, argv
[1], FA_CREATE_ALWAYS
| FA_WRITE
);
347 timer
= get_timer(0);
349 unsigned int cnt
, br
;
351 if (bytes
>= sizeof buffer
) {
353 bytes
-= sizeof buffer
;
355 cnt
= bytes
; bytes
= 0;
357 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
361 z80_read_block(buffer
, addr
, cnt
);
362 z80_bus_cmd(Release
);
363 res
= f_write(&File
, buffer
, cnt
, &br
);
371 res
= f_close(&File
);
372 timer
= get_timer(timer
);
373 printf_P(PSTR("%lu bytes written with %lu bytes/sec.\n"),
374 bytes_written
, timer
? (bytes_written
* 1000 / timer
) : 0);
376 f_mount(NULL
, argv
[1], 0);
380 my_puts_P(PSTR("Bus timeout\n"));
384 return CMD_RET_FAILURE
;
386 return CMD_RET_SUCCESS
;