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