]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014, 2018 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0 | |
5 | */ | |
6 | ||
7 | #include "cmd_sd.h" | |
8 | ||
9 | #include "diskio.h" | |
10 | #include "ff.h" | |
11 | #include "eval_arg.h" | |
12 | #include "print-utils.h" | |
13 | #include "z80-if.h" | |
14 | #include "errnum.h" | |
15 | ||
16 | ||
17 | /* | |
18 | * status <pd#> - Show socket status | |
19 | * | |
20 | */ | |
21 | static | |
22 | command_ret_t do_status(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) | |
23 | { | |
24 | DSTATUS res; | |
25 | BYTE dev; | |
26 | ||
27 | (void) cmdtp; (void) flag; | |
28 | ||
29 | if (argc < 2) | |
30 | return CMD_RET_USAGE; | |
31 | ||
32 | dev = (BYTE) eval_arg(argv[1], NULL); | |
33 | res = disk_status(dev); | |
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 | |
44 | command_ret_t do_init(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) | |
45 | { | |
46 | DSTATUS res; | |
47 | BYTE dev; | |
48 | ||
49 | (void) cmdtp; (void) flag; | |
50 | ||
51 | if (argc < 2) | |
52 | return CMD_RET_USAGE; | |
53 | ||
54 | dev = (BYTE) eval_arg(argv[1], NULL); | |
55 | ||
56 | if (disk_status(dev) & STA_NODISK) | |
57 | cmd_error(CMD_RET_FAILURE, 0, PSTR("No disk")); | |
58 | ||
59 | res = disk_initialize(dev); | |
60 | printf_P(PSTR("rc=%.2x\n"), res); | |
61 | ||
62 | if (res & (STA_NODISK | STA_NOINIT)) | |
63 | return CMD_RET_FAILURE; | |
64 | ||
65 | return CMD_RET_SUCCESS; | |
66 | } | |
67 | ||
68 | /* | |
69 | * info <pd#> - Show disk info | |
70 | * | |
71 | */ | |
72 | static | |
73 | command_ret_t do_info(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) | |
74 | { | |
75 | DSTATUS res; | |
76 | BYTE dev; | |
77 | ||
78 | union { | |
79 | unsigned char uca[64]; | |
80 | unsigned long ul; | |
81 | unsigned char uc; | |
82 | } dat; | |
83 | ||
84 | (void) cmdtp; (void) flag; | |
85 | ||
86 | if (argc < 2) | |
87 | return CMD_RET_USAGE; | |
88 | ||
89 | dev = (BYTE) eval_arg(argv[1], NULL); | |
90 | ||
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 | } | |
97 | ||
98 | if (disk_ioctl(dev, GET_SECTOR_COUNT, &dat.ul) == RES_OK) | |
99 | printf_P(PSTR("Drive size: %lu sectors\n"), dat.ul); | |
100 | if (disk_ioctl(dev, GET_BLOCK_SIZE, &dat.ul) == RES_OK) | |
101 | printf_P(PSTR("Erase block: %lu sectors\n"), dat.ul); | |
102 | if (disk_ioctl(dev, MMC_GET_TYPE, &dat.uc) == RES_OK) | |
103 | printf_P(PSTR("Card type: %u\n"), dat.uc); | |
104 | if (disk_ioctl(dev, MMC_GET_CSD, dat.uca) == RES_OK) | |
105 | dump_ram(dat.uca, 0, 16, "CSD:"); | |
106 | if (disk_ioctl(dev, MMC_GET_CID, dat.uca) == RES_OK) | |
107 | dump_ram(dat.uca, 0, 16, "CID:"); | |
108 | if (disk_ioctl(dev, MMC_GET_OCR, dat.uca) == RES_OK) | |
109 | dump_ram(dat.uca, 0, 4, "OCR:"); | |
110 | if (disk_ioctl(dev, MMC_GET_SDSTAT, dat.uca) == RES_OK) | |
111 | dump_ram(dat.uca, 0, 64, "SD Status:"); | |
112 | ||
113 | return CMD_RET_SUCCESS; | |
114 | } | |
115 | ||
116 | ||
117 | /* | |
118 | * dump <pd#> [<sector> [count]] - Dump sector | |
119 | * | |
120 | */ | |
121 | static | |
122 | command_ret_t do_dump(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) | |
123 | { | |
124 | static BYTE dev_last; | |
125 | static DWORD sec_last; | |
126 | BYTE buffer[FF_MAX_SS]; | |
127 | char header[20]; | |
128 | ||
129 | DRESULT res; | |
130 | BYTE dev; | |
131 | DWORD sec; | |
132 | UINT count; | |
133 | ||
134 | (void) cmdtp; (void) flag; | |
135 | ||
136 | if (argc < 2) | |
137 | return CMD_RET_USAGE; | |
138 | ||
139 | dev = (BYTE) eval_arg(argv[1], NULL); | |
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) | |
148 | sec = eval_arg(argv[2], NULL); | |
149 | if (argc > 3) | |
150 | count = (UINT) eval_arg(argv[3], NULL); | |
151 | } | |
152 | ||
153 | for ( ; count; count--, sec++) { | |
154 | res = disk_read(dev, buffer, sec, 1); | |
155 | ||
156 | if (res) | |
157 | cmd_error(CMD_RET_FAILURE, 0, PSTR("rc=%.2x"), res); | |
158 | ||
159 | sprintf_P(header, PSTR("Sector: %lu"), sec); | |
160 | dump_ram(buffer, 0, FF_MAX_SS, header); | |
161 | } | |
162 | dev_last = dev; | |
163 | sec_last = sec; | |
164 | ||
165 | return CMD_RET_SUCCESS; | |
166 | } | |
167 | ||
168 | /* | |
169 | * read drive sector count memaddr - Read disk into memory | |
170 | * | |
171 | */ | |
172 | static | |
173 | command_ret_t do_read(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) | |
174 | { | |
175 | DRESULT res; | |
176 | BYTE dev; | |
177 | DWORD sec; | |
178 | uint32_t addr; | |
179 | int count, nr; | |
180 | BYTE buffer[FF_MAX_SS]; | |
181 | ||
182 | static DWORD sec_last; | |
183 | static uint32_t addr_last; | |
184 | ||
185 | (void) cmdtp; | |
186 | ||
187 | if (argc < 2) | |
188 | return CMD_RET_USAGE; | |
189 | ||
190 | dev = (BYTE) eval_arg(argv[1], NULL); | |
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) | |
198 | sec = eval_arg(argv[2], NULL); | |
199 | if (argc > 3) | |
200 | count = eval_arg(argv[3], NULL); | |
201 | if (argc > 4) | |
202 | addr = eval_arg(argv[4], NULL); | |
203 | } | |
204 | ||
205 | for (nr = 0; nr < count;) { | |
206 | nr++; | |
207 | if ((res = disk_read(dev, buffer, sec, 1)) == RES_OK) { | |
208 | if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) | |
209 | cmd_error(CMD_RET_FAILURE, EBUSTO, NULL); | |
210 | ||
211 | z80_write_block(buffer, addr /*+ base*/, FF_MAX_SS); | |
212 | z80_bus_cmd(Release); | |
213 | sec++; addr += FF_MAX_SS; | |
214 | } else | |
215 | break; | |
216 | } | |
217 | ||
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; | |
224 | ||
225 | return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS; | |
226 | } | |
227 | ||
228 | ||
229 | /* | |
230 | * write <pd#> <sector> <memaddr> [<n>] - Write memory to disk | |
231 | * | |
232 | */ | |
233 | static | |
234 | command_ret_t do_write(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) | |
235 | { | |
236 | DRESULT res; | |
237 | BYTE dev; | |
238 | DWORD sec; | |
239 | uint32_t addr; | |
240 | int count, nr; | |
241 | BYTE buffer[FF_MAX_SS]; | |
242 | ||
243 | static DWORD sec_last; | |
244 | static uint32_t addr_last; | |
245 | ||
246 | (void) cmdtp; | |
247 | ||
248 | if (argc < 2) | |
249 | return CMD_RET_USAGE; | |
250 | ||
251 | dev = (BYTE) eval_arg(argv[1], NULL); | |
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) | |
259 | sec = eval_arg(argv[2], NULL); | |
260 | if (argc > 3) | |
261 | count = eval_arg(argv[3], NULL); | |
262 | if (argc > 4) | |
263 | addr = eval_arg(argv[4], NULL); | |
264 | } | |
265 | ||
266 | for (nr = 0; nr < count;) { | |
267 | nr++; | |
268 | if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) | |
269 | cmd_error(CMD_RET_FAILURE, EBUSTO, NULL); | |
270 | ||
271 | z80_read_block(buffer, addr /*+ base*/, FF_MAX_SS); | |
272 | z80_bus_cmd(Release); | |
273 | ||
274 | res = disk_write(dev, buffer, sec, 1); | |
275 | if (res != RES_OK) | |
276 | break; | |
277 | sec++; addr += FF_MAX_SS; | |
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; | |
286 | } | |
287 | ||
288 | ||
289 | ||
290 | /* | |
291 | * Disk ioctl | |
292 | * sync <pd#> - CTRL_SYNC | |
293 | * | |
294 | */ | |
295 | static | |
296 | command_ret_t do_ioctl_sync(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) | |
297 | { | |
298 | BYTE dev; | |
299 | ||
300 | (void) cmdtp; (void) flag; | |
301 | ||
302 | if (argc < 2) | |
303 | return CMD_RET_USAGE; | |
304 | ||
305 | dev = (BYTE) eval_arg(argv[1], NULL); | |
306 | printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev, CTRL_SYNC, 0)); | |
307 | ||
308 | return CMD_RET_SUCCESS; | |
309 | } | |
310 | ||
311 | ||
312 | cmd_tbl_t cmd_tbl_sd[] = { | |
313 | CMD_TBL_ITEM( | |
314 | status, 2, CTBL_RPT, do_status, | |
315 | "Socket status", | |
316 | "drive" | |
317 | ), | |
318 | CMD_TBL_ITEM( | |
319 | init, 2, CTBL_RPT, do_init, | |
320 | "Initialize disk", | |
321 | "drive" | |
322 | ), | |
323 | CMD_TBL_ITEM( | |
324 | info, 2, CTBL_RPT, do_info, | |
325 | "Disk info", | |
326 | "drive" | |
327 | ), | |
328 | CMD_TBL_ITEM( | |
329 | dump, CONFIG_SYS_MAXARGS, CTBL_RPT, do_dump, | |
330 | "Dump sector(s)", | |
331 | "drive [sector [count ]]" | |
332 | ), | |
333 | CMD_TBL_ITEM( | |
334 | read, 2, CTBL_RPT, do_read, | |
335 | "Read disk sector(s) into meomory", | |
336 | "drive [sector [count [memaddr]]]" | |
337 | ), | |
338 | CMD_TBL_ITEM( | |
339 | write, 2, CTBL_RPT, do_write, | |
340 | "Write sector(s) from meomory to disk", | |
341 | "drive [sector [count [memaddr]]]" | |
342 | ), | |
343 | CMD_TBL_ITEM( | |
344 | sync, 2, CTBL_RPT, do_ioctl_sync, | |
345 | "Device control: SYNC", | |
346 | "drive" | |
347 | ), | |
348 | ||
349 | CMD_TBL_ITEM( | |
350 | help, CONFIG_SYS_MAXARGS, CTBL_RPT, do_help, | |
351 | "Print sub command description/usage", | |
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, | |
360 | NULL, | |
361 | #ifdef CONFIG_SYS_LONGHELP | |
362 | FSTR(""), | |
363 | #endif /* CONFIG_SYS_LONGHELP */ | |
364 | NULL, | |
365 | #ifdef CONFIG_AUTO_COMPLETE | |
366 | NULL, | |
367 | #endif | |
368 | }, | |
369 | /* Mark end of table */ | |
370 | CMD_TBL_END(cmd_tbl_sd) | |
371 | }; | |
372 | ||
373 | ||
374 | command_ret_t do_sd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) | |
375 | { | |
376 | puts_P(PSTR("Huch?")); | |
377 | return CMD_RET_USAGE; | |
378 | } |