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