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