]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_fat.c
9 #include "print-utils.h"
14 DWORD
get_fattime (void)
20 static const FLASH
char * const FLASH rc_names
[] = {
30 FSTR("INVALID_OBJECT"),
31 FSTR("WRITE_PROTECTED"),
32 FSTR("INVALID_DRIVE"),
34 FSTR("NO_FILE_SYSTEM"),
38 FSTR("NOT_ENOUGH_CORE"),
39 FSTR("TOO_MANY_OPEN_FILES")
43 void put_rc (FRESULT rc
)
46 printf_P(PSTR("rc=%u FR_"), rc
);
47 my_puts_P(rc_names
[rc
]);
48 my_puts_P(PSTR("\n"));
50 printf_P(PSTR("rc=%u FR_%S\n"), rc
, rc_names
[rc
]);
54 /* Work register for fs command */
56 static WORD AccFiles
, AccDirs
;
58 char Lfname
[_MAX_LFN
+1];
61 .lfsize
= sizeof Lfname
69 char* path
/* Pointer to the working buffer with start path */
77 res
= f_opendir(&dirs
, path
);
81 while (((res
= f_readdir(&dirs
, &Finfo
)) == FR_OK
) && Finfo
.fname
[0]) {
82 if (_FS_RPATH
&& Finfo
.fname
[0] == '.') continue;
84 fn
= *Finfo
.lfname
? Finfo
.lfname
: Finfo
.fname
;
88 if (Finfo
.fattrib
& AM_DIR
) {
90 path
[i
] = '/'; strcpy(path
+i
+1, fn
);
91 res
= scan_files(path
);
93 if (res
!= FR_OK
) break;
95 // printf_P(PSTR("%s/%s\n"), path, fn);
97 AccSize
+= Finfo
.fsize
;
107 * fatstat path - Show logical drive status
110 command_ret_t
do_fat_stat(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
117 (void) cmdtp
; (void) flag
; (void) argc
;
119 res
= f_mount(&FatFs
, argv
[1], 0);
121 res
= f_getfree(argv
[1], &clusters
, &fs
);
124 return CMD_RET_FAILURE
;
129 "Bytes/Cluster: %lu\n"
130 "Number of FATs: %u\n"
131 "Root DIR entries: %u\n"
133 "Number of clusters: %lu\n"
134 "FAT start (lba): %lu\n"
135 "DIR start (lba,cluster): %lu\n"
136 "Data start (lba): %lu\n"),
137 fs
->fs_type
, (DWORD
)fs
->csize
* 512, fs
->n_fats
,
138 fs
->n_rootdir
, fs
->fsize
, fs
->n_fatent
- 2,
139 fs
->fatbase
, fs
->dirbase
, fs
->database
);
144 res
= f_getlabel(argv
[1], label
, &serial
);
147 return CMD_RET_FAILURE
;
151 "Volume S/N: %04X-%04X\n"),
152 label
, (WORD
)(serial
>> 16), (WORD
)(serial
& 0xFFFF));
154 my_puts_P(PSTR("\n..."));
155 AccSize
= AccFiles
= AccDirs
= 0;
157 strcpy(buffer
, argv
[1]);
159 res
= scan_files(buffer
);
162 return CMD_RET_FAILURE
;
164 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
165 "%lu KB total disk space.\n%lu KB available.\n"),
166 AccFiles
, AccSize
, AccDirs
,
167 (fs
->n_fatent
- 2) * (fs
->csize
/ 2), clusters
* (fs
->csize
/ 2)
170 res
= f_mount(NULL
, argv
[1], 0);
173 return CMD_RET_FAILURE
;
175 return CMD_RET_SUCCESS
;
180 * fatls path - Directory listing
183 command_ret_t
do_fat_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
186 DIR Dir
; /* Directory object */
192 char Lfname
[_MAX_LFN
+1];
193 Finfo
.lfname
= Lfname
;
194 Finfo
.lfsize
= sizeof Lfname
;
197 (void) cmdtp
; (void) flag
; (void) argc
;
199 res
= f_mount(&FatFs
, argv
[1], 0);
201 res
= f_opendir(&Dir
, argv
[1]);
204 return CMD_RET_FAILURE
;
209 res
= f_readdir(&Dir
, &Finfo
);
210 if ((res
!= FR_OK
) || !Finfo
.fname
[0])
212 if (Finfo
.fattrib
& AM_DIR
) {
215 s1
++; p1
+= Finfo
.fsize
;
217 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s"),
218 (Finfo
.fattrib
& AM_DIR
) ? 'D' : '-',
219 (Finfo
.fattrib
& AM_RDO
) ? 'R' : '-',
220 (Finfo
.fattrib
& AM_HID
) ? 'H' : '-',
221 (Finfo
.fattrib
& AM_SYS
) ? 'S' : '-',
222 (Finfo
.fattrib
& AM_ARC
) ? 'A' : '-',
223 (Finfo
.fdate
>> 9) + 1980, (Finfo
.fdate
>> 5) & 15, Finfo
.fdate
& 31,
224 (Finfo
.ftime
>> 11), (Finfo
.ftime
>> 5) & 63,
225 Finfo
.fsize
, &(Finfo
.fname
[0]));
227 for (int i
= strlen(Finfo
.fname
); i
< 14; i
++)
229 printf_P(PSTR("%s\n"), Lfname
);
236 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1
, p1
, s2
);
237 if (f_getfree(argv
[1], (DWORD
*)&p1
, &fs
) == FR_OK
)
238 printf_P(PSTR(", %10luK bytes free\n"), p1
* fs
->csize
/ 2);
243 return CMD_RET_FAILURE
;
246 return CMD_RET_SUCCESS
;
251 * fatread - load binary file from a dos filesystem
252 * <d:/path/filename> <addr> [bytes [pos]]
254 command_ret_t
do_fat_read(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
258 unsigned long bytes
= 0x80000;
259 unsigned long pos
= 0;
260 unsigned long bytes_read
;
267 (void) cmdtp
; (void) flag
;
270 return CMD_RET_USAGE
;
272 addr
= strtoul(argv
[2], 0, 16);
274 bytes
= strtoul(argv
[3], 0, 16);
276 pos
= strtoul(argv
[4], 0, 16);
278 res
= f_mount(&FatFs
, argv
[1], 0);
280 res
= f_open(&File
, argv
[1], FA_READ
);
283 res
= f_lseek(&File
, pos
);
286 timer
= get_timer(0);
288 unsigned int cnt
, br
;
290 if (bytes
>= sizeof buffer
) {
292 bytes
-= sizeof buffer
;
294 cnt
= bytes
; bytes
= 0;
296 res
= f_read(&File
, buffer
, cnt
, &br
);
299 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
303 z80_write_block(buffer
, addr
, br
);
304 z80_bus_cmd(Release
);
311 timer
= get_timer(timer
);
312 printf_P(PSTR("%lu bytes read with %lu bytes/sec.\n"),
313 bytes_read
, timer
? (bytes_read
* 1000 / timer
) : 0);
316 f_mount(NULL
, argv
[1], 0);
320 my_puts_P(PSTR("Bus timeout\n"));
324 return CMD_RET_FAILURE
;
326 return CMD_RET_SUCCESS
;
330 * fatwrite - write file into a dos filesystem
331 * <d:/path/filename> <addr> <bytes>
333 command_ret_t
do_fat_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
338 unsigned long bytes_written
;
345 (void) cmdtp
; (void) flag
;
348 return CMD_RET_USAGE
;
350 addr
= strtoul(argv
[2], 0, 16);
351 bytes
= strtoul(argv
[3], 0, 16);
353 res
= f_mount(&FatFs
, argv
[1], 0);
355 res
= f_open(&File
, argv
[1], FA_CREATE_ALWAYS
| FA_WRITE
);
358 timer
= get_timer(0);
360 unsigned int cnt
, br
;
362 if (bytes
>= sizeof buffer
) {
364 bytes
-= sizeof buffer
;
366 cnt
= bytes
; bytes
= 0;
368 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
372 z80_read_block(buffer
, addr
, cnt
);
373 z80_bus_cmd(Release
);
374 res
= f_write(&File
, buffer
, cnt
, &br
);
382 res
= f_close(&File
);
383 timer
= get_timer(timer
);
384 printf_P(PSTR("%lu bytes written with %lu bytes/sec.\n"),
385 bytes_written
, timer
? (bytes_written
* 1000 / timer
) : 0);
387 f_mount(NULL
, argv
[1], 0);
391 my_puts_P(PSTR("Bus timeout\n"));
395 return CMD_RET_FAILURE
;
397 return CMD_RET_SUCCESS
;