]>
Commit | Line | Data |
---|---|---|
7f552300 L |
1 | #include "common.h" |
2 | #include <stdlib.h> | |
3 | ||
4 | #include "diskio.h" | |
5 | #include "ff.h" | |
6 | #include "command.h" | |
7 | #include "print-utils.h" | |
8 | ||
9 | ||
10 | static const FLASH char * const FLASH rc_names[] = { | |
11 | FSTR("OK"), | |
12 | FSTR("DISK_ERR"), | |
13 | FSTR("INT_ERR"), | |
14 | FSTR("NOT_READY"), | |
15 | FSTR("NO_FILE"), | |
16 | FSTR("NO_PATH"), | |
17 | FSTR("INVALID_NAME"), | |
18 | FSTR("DENIED"), | |
19 | FSTR("EXIST"), | |
20 | FSTR("INVALID_OBJECT"), | |
21 | FSTR("WRITE_PROTECTED"), | |
22 | FSTR("INVALID_DRIVE"), | |
23 | FSTR("NOT_ENABLED"), | |
24 | FSTR("NO_FILE_SYSTEM"), | |
25 | FSTR("MKFS_ABORTED"), | |
26 | FSTR("TIMEOUT"), | |
27 | FSTR("LOCKED"), | |
28 | FSTR("NOT_ENOUGH_CORE"), | |
29 | FSTR("TOO_MANY_OPEN_FILES") | |
30 | }; | |
31 | ||
32 | static | |
33 | void put_rc (FRESULT rc) | |
34 | { | |
35 | #if 0 | |
36 | printf_P(PSTR("rc=%u FR_%S\n"), rc, rc_names[rc]); | |
37 | #else | |
38 | printf_P(PSTR("rc=%u FR_"), rc); | |
39 | my_puts_P(rc_names[rc]); | |
40 | printf_P(PSTR("\n")); | |
41 | ||
42 | #endif | |
43 | ||
44 | } | |
45 | ||
46 | #define BUFF_SIZE 1024 | |
47 | BYTE Buff[BUFF_SIZE]; /* Working buffer */ | |
48 | ||
49 | static DWORD last_sect; | |
50 | ||
51 | ||
52 | /* | |
53 | * dd <pd#> [<sector>] - Dump sector | |
54 | * | |
55 | */ | |
56 | command_ret_t do_dd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
57 | { | |
58 | FRESULT res; | |
59 | BYTE dev; | |
60 | unsigned long sec; | |
61 | ||
62 | (void) cmdtp; (void) flag; | |
63 | ||
64 | if (argc < 2) | |
65 | return CMD_RET_USAGE; | |
66 | ||
67 | dev = (BYTE) strtoul(argv[1], 0, 10); | |
68 | ||
69 | if (argc > 2) | |
70 | sec = strtoul(argv[2], 0, 10); | |
71 | else | |
72 | sec = last_sect; | |
73 | ||
74 | res = disk_read(dev, Buff, sec, 1); | |
75 | ||
76 | if (res) { | |
77 | printf_P(PSTR("rc=%.2x\n"), res); | |
78 | return CMD_RET_FAILURE; | |
79 | } | |
80 | ||
81 | last_sect = sec + 1; | |
82 | printf_P(PSTR("Sector:%lu\n"), sec); | |
83 | dump_ram((uint32_t) (size_t) Buff, 0, 0x200, NULL); | |
84 | ||
85 | return CMD_RET_SUCCESS; | |
86 | } | |
87 | ||
88 | /* | |
89 | * di <pd#> - Initialize disk | |
90 | * | |
91 | */ | |
92 | command_ret_t do_di(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
93 | { | |
94 | FRESULT res; | |
95 | BYTE dev; | |
96 | ||
97 | (void) cmdtp; (void) flag; | |
98 | ||
99 | if (argc < 2) | |
100 | return CMD_RET_USAGE; | |
101 | ||
102 | dev = (BYTE) strtoul(argv[1], 0, 10); | |
103 | res = disk_status(dev); | |
104 | printf_P(PSTR("disk_status=%.2x\n"), res); | |
105 | ||
106 | if ((res & STA_NODISK) == 0) { | |
107 | res = disk_initialize(dev); | |
108 | } | |
109 | printf_P(PSTR("rc=%.2x\n"), res); | |
110 | if (res) { | |
111 | return CMD_RET_FAILURE; | |
112 | } | |
113 | ||
114 | return CMD_RET_SUCCESS; | |
115 | } | |
116 | ||
117 | /* | |
118 | * ds <pd#> - Show disk status | |
119 | * | |
120 | */ | |
121 | command_ret_t do_ds(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
122 | { | |
123 | BYTE dev; | |
124 | ||
125 | union { | |
126 | unsigned long lval; | |
127 | unsigned char cval; | |
128 | } dat; | |
129 | ||
130 | (void) cmdtp; (void) flag; | |
131 | ||
132 | if (argc < 2) | |
133 | return CMD_RET_USAGE; | |
134 | ||
135 | dev = (BYTE) strtoul(argv[1], 0, 10); | |
136 | ||
137 | ||
138 | if (disk_ioctl(dev, GET_SECTOR_COUNT, &dat.lval) == RES_OK) | |
139 | printf_P(PSTR("Drive size: %lu sectors\n"), dat.lval); | |
140 | ||
141 | if (disk_ioctl(dev, GET_BLOCK_SIZE, &dat.lval) == RES_OK) | |
142 | printf_P(PSTR("Erase block: %lu sectors\n"), dat.lval); | |
143 | ||
144 | if (disk_ioctl(dev, MMC_GET_TYPE, &dat.cval) == RES_OK) | |
145 | printf_P(PSTR("Card type: %u\n"), dat.cval); | |
146 | ||
147 | if (disk_ioctl(dev, MMC_GET_CSD, Buff) == RES_OK) | |
148 | dump_ram((uint32_t) (size_t) Buff, 0, 16, "CSD:"); | |
149 | ||
150 | if (disk_ioctl(dev, MMC_GET_CID, Buff) == RES_OK) | |
151 | dump_ram((uint32_t) (size_t) Buff, 0, 16, "CID:"); | |
152 | ||
153 | if (disk_ioctl(dev, MMC_GET_OCR, Buff) == RES_OK) | |
154 | dump_ram((uint32_t) (size_t) Buff, 0, 4, "OCR:"); | |
155 | ||
156 | if (disk_ioctl(dev, MMC_GET_SDSTAT, Buff) == RES_OK) | |
157 | dump_ram((uint32_t) (size_t) Buff, 0, 64, "SD Status:"); | |
158 | ||
159 | if (disk_ioctl(dev, ATA_GET_MODEL, Buff) == RES_OK) { | |
160 | Buff[40] = '\0'; | |
161 | printf_P(PSTR("Model: %s\n"), Buff); | |
162 | } | |
163 | if (disk_ioctl(dev, ATA_GET_SN, Buff) == RES_OK) { | |
164 | Buff[20] = '\0'; printf_P(PSTR("S/N: %s\n"), Buff); | |
165 | } | |
166 | ||
167 | return CMD_RET_SUCCESS; | |
168 | } | |
169 | ||
170 | ||
171 | ||
172 | /* | |
173 | * Disk ioctl | |
174 | * dcs <pd#> - CTRL_SYNC | |
175 | * | |
176 | */ | |
177 | command_ret_t do_dcs(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
178 | { | |
179 | BYTE dev; | |
180 | ||
181 | (void) cmdtp; (void) flag; | |
182 | ||
183 | if (argc < 2) | |
184 | return CMD_RET_USAGE; | |
185 | ||
186 | dev = (BYTE) strtoul(argv[1], 0, 10); | |
187 | printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev, CTRL_SYNC, 0)); | |
188 | ||
189 | return CMD_RET_SUCCESS; | |
190 | } | |
191 | ||
192 | ||
193 | ||
194 | command_ret_t do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
195 | { | |
196 | (void) cmdtp; (void) flag; | |
197 | (void) argc; (void) argv; | |
198 | ||
199 | printf_P(_USE_LFN ? PSTR("LFN Enabled") : PSTR("LFN Disabled")); | |
200 | printf_P(PSTR(", Code page: %u\n"), _CODE_PAGE); | |
201 | ||
202 | for (FRESULT i=0; i<19; i++) | |
203 | put_rc(i); | |
204 | ||
205 | ||
206 | return CMD_RET_SUCCESS; | |
207 | } | |
208 | ||
209 | extern command_ret_t do_echo(cmd_tbl_t *, int, int, char * const []); | |
210 | ||
211 | ||
212 | static | |
213 | command_ret_t do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); | |
214 | ||
215 | cmd_tbl_t cmd_sd_sub[] = { | |
216 | CMD_TBL_ITEM( | |
217 | dd, CONFIG_SYS_MAXARGS, 1, do_dd, | |
218 | "Dump sector", | |
219 | "" | |
220 | ), | |
221 | CMD_TBL_ITEM( | |
222 | di, 2, 1, do_di, | |
223 | "Initialize disk", | |
224 | "" | |
225 | ), | |
226 | CMD_TBL_ITEM( | |
227 | ds, 2, 1, do_ds, | |
228 | "Disk status", | |
229 | "" | |
230 | ), | |
231 | CMD_TBL_ITEM( | |
232 | dcs, 2, 1, do_dcs, | |
233 | "Device control: SYNC", | |
234 | "" | |
235 | ), | |
236 | CMD_TBL_ITEM( | |
237 | echo, CONFIG_SYS_MAXARGS, 1, do_echo, | |
238 | "sane as echo (simple test)", "" | |
239 | ), | |
240 | CMD_TBL_ITEM( | |
241 | test, CONFIG_SYS_MAXARGS, 1, do_test, | |
242 | "print some compiled in parameters", "" | |
243 | ), | |
244 | CMD_TBL_ITEM( | |
245 | help, CONFIG_SYS_MAXARGS, 1, do_help, | |
246 | "print sub command description/usage", | |
247 | "\n" | |
248 | " - print brief description of all sub commands\n" | |
249 | "sd help command ...\n" | |
250 | " - print detailed usage of sub cmd 'command'" | |
251 | ), | |
252 | ||
253 | /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */ | |
254 | {FSTR("?"), CONFIG_SYS_MAXARGS, 1, do_help, | |
255 | FSTR("alias for 'help'"), | |
256 | #ifdef CONFIG_SYS_LONGHELP | |
257 | FSTR(""), | |
258 | #endif /* CONFIG_SYS_LONGHELP */ | |
259 | #ifdef CONFIG_AUTO_COMPLETE | |
260 | 0, | |
261 | #endif | |
262 | }, | |
263 | }; | |
264 | ||
265 | static | |
266 | command_ret_t do_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
267 | { | |
268 | return _do_help(cmd_sd_sub, ARRAY_SIZE(cmd_sd_sub), cmdtp, flag, argc, argv); | |
269 | } | |
270 | ||
271 | ||
272 | command_ret_t do_sd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
273 | { | |
274 | cmd_tbl_t *cp; | |
275 | ||
276 | if (argc < 2) | |
277 | return CMD_RET_USAGE; | |
278 | ||
279 | /* drop initial "sd" arg */ | |
280 | argc--; | |
281 | argv++; | |
282 | ||
283 | cp = find_cmd_tbl(argv[0], cmd_sd_sub, ARRAY_SIZE(cmd_sd_sub)); | |
284 | ||
285 | if (cp) | |
286 | return cp->cmd(cmdtp, flag, argc, argv); | |
287 | ||
288 | return CMD_RET_USAGE; | |
289 | } |