]>
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) { | |
86ee5f8b L |
207 | if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) |
208 | cmd_error(CMD_RET_FAILURE, EBUSTO, NULL); | |
209 | ||
d20e9438 | 210 | z80_write_block(buffer, addr /*+ base*/, FF_MAX_SS); |
a882a089 | 211 | z80_bus_cmd(Release); |
d20e9438 | 212 | sec++; addr += FF_MAX_SS; |
a882a089 L |
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 | 232 | static |
fcf1d5b3 | 233 | command_ret_t do_write(cmd_tbl_t *cmdtp, uint_fast8_t 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; | |
d20e9438 | 240 | BYTE buffer[FF_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 | ||
2d914b45 | 250 | dev = (BYTE) eval_arg(argv[1], NULL); |
a882a089 L |
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) | |
2d914b45 | 258 | sec = eval_arg(argv[2], NULL); |
a882a089 | 259 | if (argc > 3) |
2d914b45 | 260 | count = eval_arg(argv[3], NULL); |
a882a089 | 261 | if (argc > 4) |
2d914b45 | 262 | addr = eval_arg(argv[4], NULL); |
a882a089 | 263 | } |
7f552300 | 264 | |
a882a089 L |
265 | for (nr = 0; nr < count;) { |
266 | nr++; | |
86ee5f8b L |
267 | if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) |
268 | cmd_error(CMD_RET_FAILURE, EBUSTO, NULL); | |
269 | ||
d20e9438 | 270 | z80_read_block(buffer, addr /*+ base*/, FF_MAX_SS); |
a882a089 L |
271 | z80_bus_cmd(Release); |
272 | ||
273 | res = disk_write(dev, buffer, sec, 1); | |
274 | if (res != RES_OK) | |
275 | break; | |
d20e9438 | 276 | sec++; addr += FF_MAX_SS; |
a882a089 L |
277 | } |
278 | ||
279 | printf_P(PSTR("%d sector(s) written, rc=%.2x.\n"), nr, res); | |
280 | ||
281 | sec_last = sec; | |
282 | addr_last = addr; | |
283 | ||
284 | return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS; | |
7f552300 L |
285 | } |
286 | ||
287 | ||
288 | ||
a882a089 L |
289 | /* |
290 | * Disk ioctl | |
8b6edd92 | 291 | * sync <pd#> - CTRL_SYNC |
a882a089 L |
292 | * |
293 | */ | |
294 | static | |
fcf1d5b3 | 295 | command_ret_t do_ioctl_sync(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) |
7f552300 | 296 | { |
a882a089 | 297 | BYTE dev; |
7f552300 | 298 | |
a882a089 | 299 | (void) cmdtp; (void) flag; |
7f552300 | 300 | |
a882a089 L |
301 | if (argc < 2) |
302 | return CMD_RET_USAGE; | |
7f552300 | 303 | |
2d914b45 | 304 | dev = (BYTE) eval_arg(argv[1], NULL); |
a882a089 | 305 | printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev, CTRL_SYNC, 0)); |
7f552300 L |
306 | |
307 | return CMD_RET_SUCCESS; | |
308 | } | |
309 | ||
7f552300 | 310 | |
8da60ec5 | 311 | cmd_tbl_t cmd_tbl_sd[] = { |
8b6edd92 | 312 | CMD_TBL_ITEM( |
7a1ed620 | 313 | status, 2, CTBL_RPT, do_status, |
04f84937 | 314 | "Socket status", |
2d914b45 | 315 | "drive" |
8b6edd92 | 316 | ), |
7f552300 | 317 | CMD_TBL_ITEM( |
7a1ed620 | 318 | init, 2, CTBL_RPT, do_init, |
7f552300 | 319 | "Initialize disk", |
2d914b45 | 320 | "drive" |
7f552300 L |
321 | ), |
322 | CMD_TBL_ITEM( | |
7a1ed620 | 323 | info, 2, CTBL_RPT, do_info, |
8b6edd92 | 324 | "Disk info", |
2d914b45 | 325 | "drive" |
7f552300 L |
326 | ), |
327 | CMD_TBL_ITEM( | |
7a1ed620 | 328 | dump, CONFIG_SYS_MAXARGS, CTBL_RPT, do_dump, |
15e476bc | 329 | "Dump sector(s)", |
2d914b45 | 330 | "drive [sector [count ]]" |
7f552300 L |
331 | ), |
332 | CMD_TBL_ITEM( | |
7a1ed620 | 333 | read, 2, CTBL_RPT, do_read, |
8b6edd92 | 334 | "Read disk sector(s) into meomory", |
2d914b45 | 335 | "drive [sector [count [memaddr]]]" |
a882a089 L |
336 | ), |
337 | CMD_TBL_ITEM( | |
7a1ed620 | 338 | write, 2, CTBL_RPT, do_write, |
8b6edd92 | 339 | "Write sector(s) from meomory to disk", |
2d914b45 | 340 | "drive [sector [count [memaddr]]]" |
7f552300 L |
341 | ), |
342 | CMD_TBL_ITEM( | |
7a1ed620 | 343 | sync, 2, CTBL_RPT, do_ioctl_sync, |
a882a089 | 344 | "Device control: SYNC", |
2d914b45 | 345 | "drive" |
7f552300 | 346 | ), |
a882a089 | 347 | |
7f552300 | 348 | CMD_TBL_ITEM( |
7a1ed620 | 349 | help, CONFIG_SYS_MAXARGS, CTBL_RPT, do_help, |
8b6edd92 | 350 | "Print sub command description/usage", |
7f552300 L |
351 | "\n" |
352 | " - print brief description of all sub commands\n" | |
353 | "sd help command ...\n" | |
354 | " - print detailed usage of sub cmd 'command'" | |
355 | ), | |
356 | ||
357 | /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */ | |
358 | {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help, | |
04f84937 | 359 | NULL, |
7f552300 L |
360 | #ifdef CONFIG_SYS_LONGHELP |
361 | FSTR(""), | |
362 | #endif /* CONFIG_SYS_LONGHELP */ | |
8da60ec5 | 363 | NULL, |
7f552300 | 364 | #ifdef CONFIG_AUTO_COMPLETE |
8da60ec5 | 365 | NULL, |
7f552300 L |
366 | #endif |
367 | }, | |
8da60ec5 | 368 | /* Mark end of table */ |
5caa8c2b | 369 | CMD_TBL_END(cmd_tbl_sd) |
7f552300 L |
370 | }; |
371 | ||
7f552300 | 372 | |
04f84937 | 373 | command_ret_t do_sd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) |
7f552300 | 374 | { |
7a1ed620 | 375 | puts_P(PSTR("Huch?")); |
7f552300 L |
376 | return CMD_RET_USAGE; |
377 | } |