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