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