]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_sd.c
b84c4be758ec94b0d6797479c92ad56062dd7fc6
[z180-stamp.git] / avr / cmd_sd.c
1 /*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include "common.h"
8 //#include <stdlib.h>
9
10 #include "command.h"
11 #include "diskio.h"
12 #include "ff.h"
13 #include "eval_arg.h"
14 #include "print-utils.h"
15 #include "z80-if.h"
16
17
18 /*
19 * status <pd#> - Show socket status
20 *
21 */
22 static
23 command_ret_t do_status(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
24 {
25 DSTATUS res;
26 BYTE dev;
27
28 (void) cmdtp; (void) flag;
29
30 if (argc < 2)
31 return CMD_RET_USAGE;
32
33 dev = (BYTE) eval_arg(argv[1], NULL);
34 res = disk_status(dev);
35 printf_P(PSTR("Socket status: %02x\n"), res);
36
37 return CMD_RET_SUCCESS;
38 }
39
40 /*
41 * init <pd#> - Initialize disk
42 *
43 */
44 static
45 command_ret_t do_init(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
46 {
47 DSTATUS res;
48 BYTE dev;
49
50 (void) cmdtp; (void) flag;
51
52 if (argc < 2)
53 return CMD_RET_USAGE;
54
55 dev = (BYTE) eval_arg(argv[1], NULL);
56
57 if (disk_status(dev) & STA_NODISK) {
58 printf_P(PSTR("No Disk\n"));
59 return CMD_RET_FAILURE;
60 }
61
62 res = disk_initialize(dev);
63 printf_P(PSTR("rc=%.2x\n"), res);
64
65 if (res & (STA_NODISK | STA_NOINIT))
66 return CMD_RET_FAILURE;
67
68 return CMD_RET_SUCCESS;
69 }
70
71 /*
72 * info <pd#> - Show disk info
73 *
74 */
75 static
76 command_ret_t do_info(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
77 {
78 DSTATUS res;
79 BYTE dev;
80
81 union {
82 unsigned char uca[64];
83 unsigned long ul;
84 unsigned char uc;
85 } dat;
86
87 (void) cmdtp; (void) flag;
88
89 if (argc < 2)
90 return CMD_RET_USAGE;
91
92 dev = (BYTE) eval_arg(argv[1], NULL);
93
94 res = disk_status(dev);
95 if (res & (STA_NODISK | STA_NOINIT)) {
96 printf_P(res & STA_NODISK ?
97 PSTR("No disk\n") : PSTR("Not initialized\n"));
98 return CMD_RET_FAILURE;
99 }
100
101 if (disk_ioctl(dev, GET_SECTOR_COUNT, &dat.ul) == RES_OK)
102 printf_P(PSTR("Drive size: %lu sectors\n"), dat.ul);
103 if (disk_ioctl(dev, GET_BLOCK_SIZE, &dat.ul) == RES_OK)
104 printf_P(PSTR("Erase block: %lu sectors\n"), dat.ul);
105 if (disk_ioctl(dev, MMC_GET_TYPE, &dat.uc) == RES_OK)
106 printf_P(PSTR("Card type: %u\n"), dat.uc);
107 if (disk_ioctl(dev, MMC_GET_CSD, dat.uca) == RES_OK)
108 dump_ram(dat.uca, 0, 16, "CSD:");
109 if (disk_ioctl(dev, MMC_GET_CID, dat.uca) == RES_OK)
110 dump_ram(dat.uca, 0, 16, "CID:");
111 if (disk_ioctl(dev, MMC_GET_OCR, dat.uca) == RES_OK)
112 dump_ram(dat.uca, 0, 4, "OCR:");
113 if (disk_ioctl(dev, MMC_GET_SDSTAT, dat.uca) == RES_OK)
114 dump_ram(dat.uca, 0, 64, "SD Status:");
115
116 return CMD_RET_SUCCESS;
117 }
118
119
120 /*
121 * dump <pd#> [<sector> [count]] - Dump sector
122 *
123 */
124 static
125 command_ret_t do_dump(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
126 {
127 static BYTE dev_last;
128 static DWORD sec_last;
129 BYTE buffer[_MAX_SS];
130 char header[20];
131
132 DRESULT res;
133 BYTE dev;
134 DWORD sec;
135 UINT count;
136
137 (void) cmdtp; (void) flag;
138
139 if (argc < 2)
140 return CMD_RET_USAGE;
141
142 dev = (BYTE) eval_arg(argv[1], NULL);
143 if (dev != dev_last)
144 sec_last = 0;
145 sec = sec_last;
146 count = 1;
147
148 if ((flag & CMD_FLAG_REPEAT) == 0) {
149 /* If another parameter, it is the sector to dump. */
150 if (argc > 2)
151 sec = eval_arg(argv[2], NULL);
152 if (argc > 3)
153 count = (UINT) eval_arg(argv[3], NULL);
154 }
155
156 for ( ; count; count--, sec++) {
157 res = disk_read(dev, buffer, sec, 1);
158
159 if (res) {
160 printf_P(PSTR("rc=%.2x\n"), res);
161 return CMD_RET_FAILURE;
162 }
163
164 sprintf_P(header, PSTR("Sector: %lu"), sec);
165 dump_ram(buffer, 0, _MAX_SS, header);
166 }
167 dev_last = dev;
168 sec_last = sec;
169
170 return CMD_RET_SUCCESS;
171 }
172
173 /*
174 * read drive sector count memaddr - Read disk into memory
175 *
176 */
177 static
178 command_ret_t do_read(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
179 {
180 DRESULT res;
181 BYTE dev;
182 DWORD sec;
183 uint32_t addr;
184 int count, nr;
185 BYTE buffer[_MAX_SS];
186
187 static DWORD sec_last;
188 static uint32_t addr_last;
189
190 (void) cmdtp;
191
192 if (argc < 2)
193 return CMD_RET_USAGE;
194
195 dev = (BYTE) eval_arg(argv[1], NULL);
196 sec = sec_last;
197 count = 1;
198 addr = addr_last;
199
200 if ((flag & CMD_FLAG_REPEAT) == 0) {
201 /* If another parameter, it is the sector to dump. */
202 if (argc > 2)
203 sec = eval_arg(argv[2], NULL);
204 if (argc > 3)
205 count = eval_arg(argv[3], NULL);
206 if (argc > 4)
207 addr = eval_arg(argv[4], NULL);
208 }
209
210 for (nr = 0; nr < count;) {
211 nr++;
212 if ((res = disk_read(dev, buffer, sec, 1)) == RES_OK) {
213 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
214 my_puts_P(PSTR("Bus timeout\n"));
215 return CMD_RET_FAILURE;
216 }
217 z80_write_block(buffer, addr /*+ base*/, _MAX_SS);
218 z80_bus_cmd(Release);
219 sec++; addr += _MAX_SS;
220 } else
221 break;
222 }
223
224 printf_P(PSTR("Read %d sector(s), rc=%.2x.\n"), nr, res);
225 if (res)
226 printf_P(PSTR("Last sector not written!\n"));
227
228 sec_last = sec;
229 addr_last = addr;
230
231 return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
232 }
233
234
235 /*
236 * write <pd#> <sector> <memaddr> [<n>] - Write memory to disk
237 *
238 */
239 static
240 command_ret_t do_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
241 {
242 DRESULT res;
243 BYTE dev;
244 DWORD sec;
245 uint32_t addr;
246 int count, nr;
247 BYTE buffer[_MAX_SS];
248
249 static DWORD sec_last;
250 static uint32_t addr_last;
251
252 (void) cmdtp;
253
254 if (argc < 2)
255 return CMD_RET_USAGE;
256
257 dev = (BYTE) eval_arg(argv[1], NULL);
258 sec = sec_last;
259 addr = addr_last;
260 count = 1;
261
262 if ((flag & CMD_FLAG_REPEAT) == 0) {
263 /* If another parameter, it is the sector to dump. */
264 if (argc > 2)
265 sec = eval_arg(argv[2], NULL);
266 if (argc > 3)
267 count = eval_arg(argv[3], NULL);
268 if (argc > 4)
269 addr = eval_arg(argv[4], NULL);
270 }
271
272 for (nr = 0; nr < count;) {
273 nr++;
274 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
275 my_puts_P(PSTR("Bus timeout\n"));
276 return CMD_RET_FAILURE;
277 }
278 z80_read_block(buffer, addr /*+ base*/, _MAX_SS);
279 z80_bus_cmd(Release);
280
281 res = disk_write(dev, buffer, sec, 1);
282 if (res != RES_OK)
283 break;
284 sec++; addr += _MAX_SS;
285 }
286
287 printf_P(PSTR("%d sector(s) written, rc=%.2x.\n"), nr, res);
288
289 sec_last = sec;
290 addr_last = addr;
291
292 return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
293 }
294
295
296
297 /*
298 * Disk ioctl
299 * sync <pd#> - CTRL_SYNC
300 *
301 */
302 static
303 command_ret_t do_ioctl_sync(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
304 {
305 BYTE dev;
306
307 (void) cmdtp; (void) flag;
308
309 if (argc < 2)
310 return CMD_RET_USAGE;
311
312 dev = (BYTE) eval_arg(argv[1], NULL);
313 printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev, CTRL_SYNC, 0));
314
315 return CMD_RET_SUCCESS;
316 }
317
318
319 static
320 command_ret_t do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
321
322 cmd_tbl_t cmd_sd_sub[] = {
323 CMD_TBL_ITEM(
324 status, 2, 1, do_status,
325 "Socket staus",
326 "drive"
327 ),
328 CMD_TBL_ITEM(
329 init, 2, 1, do_init,
330 "Initialize disk",
331 "drive"
332 ),
333 CMD_TBL_ITEM(
334 info, 2, 1, do_info,
335 "Disk info",
336 "drive"
337 ),
338 CMD_TBL_ITEM(
339 dump, CONFIG_SYS_MAXARGS, 1, do_dump,
340 "Dump sector(s)",
341 "drive [sector [count ]]"
342 ),
343 CMD_TBL_ITEM(
344 read, 2, 1, do_read,
345 "Read disk sector(s) into meomory",
346 "drive [sector [count [memaddr]]]"
347 ),
348 CMD_TBL_ITEM(
349 write, 2, 1, do_write,
350 "Write sector(s) from meomory to disk",
351 "drive [sector [count [memaddr]]]"
352 ),
353 CMD_TBL_ITEM(
354 sync, 2, 1, do_ioctl_sync,
355 "Device control: SYNC",
356 "drive"
357 ),
358
359 CMD_TBL_ITEM(
360 help, CONFIG_SYS_MAXARGS, 1, do_help,
361 "Print sub command description/usage",
362 "\n"
363 " - print brief description of all sub commands\n"
364 "sd help command ...\n"
365 " - print detailed usage of sub cmd 'command'"
366 ),
367
368 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
369 {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help,
370 FSTR("Alias for 'help'"),
371 #ifdef CONFIG_SYS_LONGHELP
372 FSTR(""),
373 #endif /* CONFIG_SYS_LONGHELP */
374 #ifdef CONFIG_AUTO_COMPLETE
375 0,
376 #endif
377 },
378 };
379
380 static
381 command_ret_t do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
382 {
383 return _do_help(cmd_sd_sub, ARRAY_SIZE(cmd_sd_sub), cmdtp, flag, argc, argv);
384 }
385
386
387 command_ret_t do_sd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
388 {
389 cmd_tbl_t *cp;
390
391 if (argc < 2)
392 return CMD_RET_USAGE;
393
394 /* drop initial "sd" arg */
395 argc--;
396 argv++;
397
398 cp = find_cmd_tbl(argv[0], cmd_sd_sub, ARRAY_SIZE(cmd_sd_sub));
399
400 if (cp)
401 return cp->cmd(cmdtp, flag, argc, argv);
402
403 return CMD_RET_USAGE;
404 }