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