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