]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_fat.c
Adaptions for fatfs R0.12b
[z180-stamp.git] / avr / cmd_fat.c
CommitLineData
35edb766 1/*
2d914b45 2 * (C) Copyright 2014,2016 Leo C. <erbl259-lmu@yahoo.de>
35edb766 3 *
2d914b45 4 * SPDX-License-Identifier: GPL-2.0
35edb766
L
5 */
6
7/*
8 * FAT filesystem commands
9 */
10
2f53dd65 11#include "common.h"
2f53dd65
L
12#include <string.h>
13#include <stdbool.h>
14
15#include "command.h"
16#include "ff.h"
17#include "z80-if.h"
2d914b45 18#include "eval_arg.h"
4565be9a 19#include "con-utils.h"
2f53dd65 20#include "print-utils.h"
19b9a7d8 21#include "time.h"
2f53dd65 22#include "timer.h"
4f881b02 23#include "debug.h"
2f53dd65 24
5f7f3586
L
25/* TODO: use memory size test function (cmd_mem.c) */
26#define MAX_MEMORY (1ul << 19)
19b9a7d8
L
27#define BUFFER_SIZE 512
28
4565be9a 29
2f53dd65
L
30DWORD get_fattime (void)
31{
19b9a7d8
L
32 time_t timer;
33 struct tm tm_timer;
34
35 time(&timer);
36 gmtime_r(&timer, &tm_timer);
37
38 return fatfs_time(&tm_timer);
2f53dd65
L
39}
40
41
b15d22a4
L
42static bool check_abort(void)
43{
44 bool ret = ctrlc();
45
46 if (ret)
47 printf_P(PSTR("Abort\n"));
48
49 return ret;
50}
51
52
2f53dd65
L
53static const FLASH char * const FLASH rc_names[] = {
54 FSTR("OK"),
55 FSTR("DISK_ERR"),
56 FSTR("INT_ERR"),
57 FSTR("NOT_READY"),
58 FSTR("NO_FILE"),
59 FSTR("NO_PATH"),
60 FSTR("INVALID_NAME"),
61 FSTR("DENIED"),
62 FSTR("EXIST"),
63 FSTR("INVALID_OBJECT"),
64 FSTR("WRITE_PROTECTED"),
65 FSTR("INVALID_DRIVE"),
66 FSTR("NOT_ENABLED"),
67 FSTR("NO_FILE_SYSTEM"),
68 FSTR("MKFS_ABORTED"),
69 FSTR("TIMEOUT"),
70 FSTR("LOCKED"),
71 FSTR("NOT_ENOUGH_CORE"),
7af9364e
L
72 FSTR("TOO_MANY_OPEN_FILES"),
73 FSTR("INVALID_PARAMETER")
2f53dd65
L
74 };
75
76static
77void put_rc (FRESULT rc)
78{
79#if GCC_BUG_61443
7af9364e
L
80 printf_P(PSTR("rc=%u FR_"), rc);
81 my_puts_P(rc < ARRAY_SIZE(rc_names) ? rc_names[rc] : PSTR(" Unknown Error"));
82 my_puts_P(PSTR("\n"));
2f53dd65 83#else
7af9364e
L
84 printf_P(PSTR("rc=%u FR_%S\n"), rc,
85 rc < ARRAY_SIZE(rc_names) ? rc_names[rc] : PSTR(" Unknown Error"));
2f53dd65
L
86#endif
87}
88
b15d22a4
L
89
90static void swirl(void)
91{
6204987c 92 static const FLASH char swirlchar[] = { '-','\\','|','/' };
b15d22a4
L
93 static uint_fast8_t cnt;
94 static uint32_t tstamp;
95
96 if (get_timer(0) > tstamp) {
97 printf_P(PSTR("\b%c"), swirlchar[cnt]);
98 cnt = (cnt+1) % ARRAY_SIZE(swirlchar);
6204987c 99 tstamp = get_timer(0) + 250;
b15d22a4
L
100 }
101}
102
6204987c
L
103/* Work register for fs command */
104struct stat_dat_s {
105 DWORD AccSize;
106 WORD AccFiles, AccDirs;
107 FILINFO Finfo;
108};
109
2f53dd65
L
110static
111FRESULT scan_files (
6204987c
L
112 char *path, /* Pointer to the working buffer with start path */
113 struct stat_dat_s *statp
2f53dd65
L
114)
115{
116 DIR dirs;
117 FRESULT res;
118 int i;
119 char *fn;
120
121 res = f_opendir(&dirs, path);
b15d22a4 122 swirl();
2f53dd65
L
123 if (res == FR_OK) {
124 i = strlen(path);
6204987c
L
125 while (((res = f_readdir(&dirs, &statp->Finfo)) == FR_OK) &&
126 statp->Finfo.fname[0]) {
127 if (_FS_RPATH && statp->Finfo.fname[0] == '.')
128 continue;
129 fn = statp->Finfo.fname;
130 if (statp->Finfo.fattrib & AM_DIR) {
131 statp->AccDirs++;
132 path[i] = '/';
133 strcpy(path+i+1, fn);
134 res = scan_files(path, statp);
2f53dd65 135 path[i] = '\0';
6204987c
L
136 if (res != FR_OK)
137 break;
2f53dd65 138 } else {
6204987c
L
139 //printf_P(PSTR("%s/%s\n"), path, fn);
140 statp->AccFiles++;
141 statp->AccSize += statp->Finfo.fsize;
2f53dd65 142 }
b15d22a4
L
143 if (check_abort()) {
144 res = 255;
145 break;
146 }
2f53dd65
L
147 }
148 }
149
150 return res;
151}
152
153
154/*
155 * fatstat path - Show logical drive status
156 *
157 */
158command_ret_t do_fat_stat(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
159{
bbd45c46 160 FATFS *fs;
4565be9a 161 DWORD nfreeclst;
2f53dd65 162 FRESULT res;
6204987c
L
163 char *path;
164 struct stat_dat_s statp;
2f53dd65
L
165
166 (void) cmdtp; (void) flag; (void) argc;
167
6204987c 168 path = (char *) malloc(BUFFER_SIZE);
bbd45c46 169 if (path == NULL) {
4565be9a 170 printf_P(PSTR("fatstat: Out of Memory!\n"));
6204987c 171 free(path);
2f53dd65
L
172 return CMD_RET_FAILURE;
173 }
174
bbd45c46 175 res = f_getfree(argv[1], &nfreeclst, &fs);
4565be9a 176 if (!res) {
bbd45c46
L
177 printf_P(PSTR(
178 "FAT type: %u\n"
179 "Bytes/Cluster: %lu\n"
180 "Number of FATs: %u\n"
181 "Root DIR entries: %u\n"
182 "Sectors/FAT: %lu\n"
183 "Number of clusters: %lu\n"
184 "FAT start (lba): %lu\n"
185 "DIR start (lba,cluster): %lu\n"
186 "Data start (lba): %lu\n"),
187 fs->fs_type, (DWORD)fs->csize * 512, fs->n_fats,
188 fs->n_rootdir, fs->fsize, fs->n_fatent - 2,
189 fs->fatbase, fs->dirbase, fs->database);
2f53dd65
L
190
191#if _USE_LABEL
bbd45c46
L
192 TCHAR label[12];
193 DWORD serial;
194 res = f_getlabel(argv[1], label, &serial);
195 if (!res) {
196 printf_P(PSTR(
197 "Volume name: %s\n"
198 "Volume S/N: %04X-%04X\n"),
199 label, (WORD)(serial >> 16), (WORD)(serial & 0xFFFF));
200 }
2f53dd65 201#endif
bbd45c46
L
202 if (!res) {
203 my_puts_P(PSTR("\nCounting... "));
204 statp.AccSize = statp.AccFiles = statp.AccDirs = 0;
205 strcpy(path, argv[1]);
2f53dd65 206
bbd45c46
L
207 res = scan_files(path, &statp);
208 }
209 if (!res) {
210 printf_P(PSTR("\r%u files, %lu bytes.\n%u folders.\n"
211 "%lu KB total disk space.\n%lu KB available.\n"),
212 statp.AccFiles, statp.AccSize, statp.AccDirs,
213 (fs->n_fatent - 2) * (fs->csize / 2), nfreeclst * (fs->csize / 2)
214 );
4565be9a 215 }
2f53dd65 216 }
2f53dd65 217
6204987c 218 free(path);
2f53dd65
L
219 if (res) {
220 put_rc(res);
221 return CMD_RET_FAILURE;
222 }
223 return CMD_RET_SUCCESS;
224}
225
226
227/*
228 * fatls path - Directory listing
229 *
230 */
231command_ret_t do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
232{
bbd45c46 233 FATFS *fs;
2f53dd65
L
234 DIR Dir; /* Directory object */
235 FILINFO Finfo;
236 unsigned long p1;
237 unsigned int s1, s2;
238 FRESULT res;
2f53dd65
L
239
240 (void) cmdtp; (void) flag; (void) argc;
241
bbd45c46 242 res = f_opendir(&Dir, argv[1]);
2f53dd65
L
243 if (res) {
244 put_rc(res);
245 return CMD_RET_FAILURE;
246 }
247
248 p1 = s1 = s2 = 0;
249 for(;;) {
250 res = f_readdir(&Dir, &Finfo);
251 if ((res != FR_OK) || !Finfo.fname[0])
252 break;
253 if (Finfo.fattrib & AM_DIR) {
254 s2++;
255 } else {
256 s1++; p1 += Finfo.fsize;
257 }
7af9364e 258 printf_P(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
2f53dd65
L
259 (Finfo.fattrib & AM_DIR) ? 'D' : '-',
260 (Finfo.fattrib & AM_RDO) ? 'R' : '-',
261 (Finfo.fattrib & AM_HID) ? 'H' : '-',
262 (Finfo.fattrib & AM_SYS) ? 'S' : '-',
263 (Finfo.fattrib & AM_ARC) ? 'A' : '-',
264 (Finfo.fdate >> 9) + 1980, (Finfo.fdate >> 5) & 15, Finfo.fdate & 31,
7af9364e
L
265 (Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63,
266 Finfo.fsize, Finfo.fname);
b15d22a4
L
267 if (check_abort())
268 break;
2f53dd65
L
269 }
270
271 if (res == FR_OK) {
272 printf_P(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
273 if (f_getfree(argv[1], (DWORD*)&p1, &fs) == FR_OK)
274 printf_P(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);
275 }
276
277 if (res) {
278 put_rc(res);
279 return CMD_RET_FAILURE;
280 }
281
282 return CMD_RET_SUCCESS;
283}
284
35edb766
L
285static
286FRESULT mkpath(TCHAR *path)
287{
288 /* TODO: */
289 (void) path;
290#if 0
291 FILINFO fd
292 TCHAR *p, *q;
293 FRESULT ret;
294
35edb766
L
295 res = f_stat (path, &fd)
296
297 p = strchr(path, ':');
298 if (p == NULL || *++p == '\0' || *p++ != '/')
299 return FR_OK;
300
301 while ((q = strchr(p, '/')) != NULL) {
302 *q = '\0';
303 ret = f_mkdir(path);
304 *q = '/';
305 if (ret != FR_OK && ret != FR_EXIST)
306 return ret;
307 p = q + 1;
308 }
309#endif
310
311 return FR_OK;
312}
2f53dd65 313
4565be9a
L
314/*
315 * fatread/write - load binary file to/from a dos filesystem
316 * read <d:/path/filename> <addr> [bytes [pos]]
317 * write <d:/path/filename> <addr> <bytes>
318 */
319command_ret_t do_fat_rw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
320{
4565be9a
L
321 FIL File;
322 uint32_t addr;
323 unsigned long bytes;
324 unsigned long pos;
325 unsigned long bytes_rw;
326
327 bool dowrite = (argv[0][3] == 'w');
3b841cea 328 FRESULT res = FR_OK;
4565be9a
L
329 bool buserr = 0;
330 uint32_t timer;
19b9a7d8 331 uint8_t *buffer;
4565be9a
L
332
333 (void) cmdtp; (void) flag;
334
335 if (argc < (dowrite ? 4 : 3))
336 return CMD_RET_USAGE;
337
2d914b45 338 addr = eval_arg(argv[2], NULL);
4565be9a
L
339 if (addr >= MAX_MEMORY) {
340 printf_P(PSTR("address too high: 0x%0lx\n"), addr);
341 return CMD_RET_FAILURE;
342 }
343 if (argc > 3)
2d914b45 344 bytes = eval_arg(argv[3], NULL);
4565be9a
L
345 else
346 bytes = MAX_MEMORY;
347 if (argc > 4)
2d914b45 348 pos = eval_arg(argv[4], NULL);
4565be9a
L
349 else
350 pos = 0;
351
352 if (addr + bytes > MAX_MEMORY)
353 bytes = MAX_MEMORY - addr;
354
19b9a7d8 355 buffer = (uint8_t *) malloc(BUFFER_SIZE);
bbd45c46 356 if (buffer == NULL) {
19b9a7d8 357 printf_P(PSTR("fatstat: Out of Memory!\n"));
19b9a7d8
L
358 free(buffer);
359 return CMD_RET_FAILURE;
360 }
361
bbd45c46
L
362 if (dowrite) {
363 res = mkpath(argv[1]);
35edb766 364 }
4565be9a
L
365 if (!res) {
366 res = f_open(&File, argv[1], dowrite ? FA_WRITE | FA_CREATE_ALWAYS
367 : FA_READ );
368
369 if (!res) {
370 res = f_lseek(&File, pos);
371 if (!res) {
372 bytes_rw = 0;
373 timer = get_timer(0);
374 while (bytes) {
375 unsigned int cnt, br;
376
19b9a7d8
L
377 if (bytes >= BUFFER_SIZE) {
378 cnt = BUFFER_SIZE;
379 bytes -= BUFFER_SIZE;
4565be9a
L
380 } else {
381 cnt = bytes; bytes = 0;
382 }
383 if (dowrite) {
384 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
385 buserr = 1;
386 break;
387 }
388 z80_read_block(buffer, addr, cnt);
389 z80_bus_cmd(Release);
390 res = f_write(&File, buffer, cnt, &br);
391 if (res != FR_OK)
392 break;
393 } else {
394 res = f_read(&File, buffer, cnt, &br);
395 if (res != FR_OK)
396 break;
397 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
398 buserr = 1;
399 break;
400 }
401 z80_write_block(buffer, addr, br);
402 z80_bus_cmd(Release);
403 }
404 addr += br;
405
406 bytes_rw += br;
407 if (cnt != br) {
408 if (dowrite)
409 printf_P(PSTR("Disk full?\n"));
410 break;
411 }
b15d22a4 412 if (check_abort())
4565be9a 413 break;
4565be9a
L
414 }
415
416 FRESULT fr = f_close(&File);
417 if (!res)
418 res = fr;
419 timer = get_timer(timer);
420 printf_P(PSTR("%lu (0x%lx) bytes read/written with %lu bytes/sec.\n"),
421 bytes_rw, bytes_rw, timer ? (bytes_rw * 1000 / timer) : 0);
422 }
423 }
4565be9a
L
424 }
425
19b9a7d8 426 free(buffer);
19b9a7d8 427
4565be9a
L
428 if (buserr)
429 my_puts_P(PSTR("Bus timeout\n"));
430 if (res)
431 put_rc(res);
432 if (buserr || res)
433 return CMD_RET_FAILURE;
434
435 return CMD_RET_SUCCESS;
436}