]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_sd.c
8 #include "print-utils.h"
11 static const FLASH
char * const FLASH rc_names
[] = {
21 FSTR("INVALID_OBJECT"),
22 FSTR("WRITE_PROTECTED"),
23 FSTR("INVALID_DRIVE"),
25 FSTR("NO_FILE_SYSTEM"),
29 FSTR("NOT_ENOUGH_CORE"),
30 FSTR("TOO_MANY_OPEN_FILES")
34 void put_rc (FRESULT rc
)
37 printf_P(PSTR("rc=%u FR_%S\n"), rc
, rc_names
[rc
]);
39 printf_P(PSTR("rc=%u FR_"), rc
);
40 my_puts_P(rc_names
[rc
]);
41 my_puts_P(PSTR("\n"));
49 * di <pd#> - Initialize disk
53 command_ret_t
do_init(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
58 (void) cmdtp
; (void) flag
;
63 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
64 res
= disk_status(dev
);
65 printf_P(PSTR("disk_status=%.2x\n"), res
);
67 if ((res
& STA_NODISK
) == 0) {
68 res
= disk_initialize(dev
);
70 printf_P(PSTR("rc=%.2x\n"), res
);
72 return CMD_RET_FAILURE
;
75 return CMD_RET_SUCCESS
;
79 * ds <pd#> - Show disk status
83 command_ret_t
do_status(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
88 unsigned char uca
[64];
93 (void) cmdtp
; (void) flag
;
98 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
101 if (disk_ioctl(dev
, GET_SECTOR_COUNT
, &dat
.ul
) == RES_OK
)
102 printf_P(PSTR("Drive size: %lu sectors\n"), dat
.ul
);
104 if (disk_ioctl(dev
, GET_BLOCK_SIZE
, &dat
.ul
) == RES_OK
)
105 printf_P(PSTR("Erase block: %lu sectors\n"), dat
.ul
);
107 if (disk_ioctl(dev
, MMC_GET_TYPE
, &dat
.uc
) == RES_OK
)
108 printf_P(PSTR("Card type: %u\n"), dat
.uc
);
110 if (disk_ioctl(dev
, MMC_GET_CSD
, dat
.uca
) == RES_OK
)
111 dump_ram(dat
.uca
, 0, 16, "CSD:");
113 if (disk_ioctl(dev
, MMC_GET_CID
, dat
.uca
) == RES_OK
)
114 dump_ram(dat
.uca
, 0, 16, "CID:");
116 if (disk_ioctl(dev
, MMC_GET_OCR
, dat
.uca
) == RES_OK
)
117 dump_ram(dat
.uca
, 0, 4, "OCR:");
119 if (disk_ioctl(dev
, MMC_GET_SDSTAT
, dat
.uca
) == RES_OK
)
120 dump_ram(dat
.uca
, 0, 64, "SD Status:");
122 if (disk_ioctl(dev
, ATA_GET_MODEL
, dat
.uca
) == RES_OK
) {
124 printf_P(PSTR("Model: %s\n"), dat
.uca
);
126 if (disk_ioctl(dev
, ATA_GET_SN
, dat
.uca
) == RES_OK
) {
127 dat
.uca
[20] = '\0'; printf_P(PSTR("S/N: %s\n"), dat
.uca
);
130 return CMD_RET_SUCCESS
;
135 * dump <pd#> [<sector> [count]] - Dump sector
139 command_ret_t
do_dump(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
141 static BYTE dev_last
;
142 static DWORD sec_last
;
143 BYTE buffer
[_MAX_SS
];
151 (void) cmdtp
; (void) flag
;
154 return CMD_RET_USAGE
;
156 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
162 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
163 /* If another parameter, it is the sector to dump. */
165 sec
= strtoul(argv
[2], 0, 10);
167 count
= (UINT
) strtoul(argv
[3], 0, 10);
170 for ( ; count
; count
--, sec
++) {
171 res
= disk_read(dev
, buffer
, sec
, 1);
174 printf_P(PSTR("rc=%.2x\n"), res
);
175 return CMD_RET_FAILURE
;
178 sprintf_P(header
, PSTR("Sector: %lu"), sec
);
179 dump_ram(buffer
, 0, _MAX_SS
, header
);
184 return CMD_RET_SUCCESS
;
188 * read drive sector count memaddr - Read disk into memory
192 command_ret_t
do_read(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
199 BYTE buffer
[_MAX_SS
];
201 static DWORD sec_last
;
202 static uint32_t addr_last
;
207 return CMD_RET_USAGE
;
209 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
214 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
215 /* If another parameter, it is the sector to dump. */
217 sec
= strtoul(argv
[2], 0, 10);
219 count
= strtoul(argv
[3], 0, 10);
221 addr
= strtoul(argv
[4], 0, 16);
224 for (nr
= 0; nr
< count
;) {
226 if ((res
= disk_read(dev
, buffer
, sec
, 1)) == RES_OK
) {
227 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
228 my_puts_P(PSTR("Bus timeout\n"));
229 return CMD_RET_FAILURE
;
231 z80_write_block(buffer
, addr
/*+ base*/, _MAX_SS
);
232 z80_bus_cmd(Release
);
233 sec
++; addr
+= _MAX_SS
;
238 printf_P(PSTR("Read %d sector(s), rc=%.2x.\n"), nr
, res
);
240 printf_P(PSTR("Last sector not written!\n"));
245 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
250 * write <pd#> <sector> <memaddr> [<n>] - Write memory to disk
254 command_ret_t
do_write(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
261 BYTE buffer
[_MAX_SS
];
263 static DWORD sec_last
;
264 static uint32_t addr_last
;
269 return CMD_RET_USAGE
;
271 dev
= (BYTE
) strtoul(argv
[1], NULL
, 10);
276 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
277 /* If another parameter, it is the sector to dump. */
279 sec
= strtoul(argv
[2], 0, 10);
281 count
= strtoul(argv
[3], 0, 10);
283 addr
= strtoul(argv
[4], 0, 16);
286 for (nr
= 0; nr
< count
;) {
288 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
289 my_puts_P(PSTR("Bus timeout\n"));
290 return CMD_RET_FAILURE
;
292 z80_read_block(buffer
, addr
/*+ base*/, _MAX_SS
);
293 z80_bus_cmd(Release
);
295 res
= disk_write(dev
, buffer
, sec
, 1);
298 sec
++; addr
+= _MAX_SS
;
301 printf_P(PSTR("%d sector(s) written, rc=%.2x.\n"), nr
, res
);
306 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
313 * dcs <pd#> - CTRL_SYNC
317 command_ret_t
do_ioctl_sync(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
321 (void) cmdtp
; (void) flag
;
324 return CMD_RET_USAGE
;
326 dev
= (BYTE
) strtoul(argv
[1], 0, 10);
327 printf_P(PSTR("rc=%.2x\n"), disk_ioctl(dev
, CTRL_SYNC
, 0));
329 return CMD_RET_SUCCESS
;
334 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[]);
336 cmd_tbl_t cmd_sd_sub
[] = {
343 status
, 2, 1, do_status
,
348 dump
, CONFIG_SYS_MAXARGS
, 1, do_dump
,
354 "read disk sector(s) into meomory",
355 "drive [sector [count [memaddr]]]"
358 write
, 2, 1, do_write
,
359 "write sector(s) from meomory to disk",
360 "drive [sector [count [memaddr]]]"
363 sync
, 2, 1, do_ioctl_sync
,
364 "Device control: SYNC",
369 help
, CONFIG_SYS_MAXARGS
, 1, do_help
,
370 "print sub command description/usage",
372 " - print brief description of all sub commands\n"
373 "sd help command ...\n"
374 " - print detailed usage of sub cmd 'command'"
377 /* This does not use the CMD_TBL_ITEM macro as ? can't be used in symbol names */
378 {FSTR("?"), CONFIG_SYS_MAXARGS
, 1, do_help
,
379 FSTR("alias for 'help'"),
380 #ifdef CONFIG_SYS_LONGHELP
382 #endif /* CONFIG_SYS_LONGHELP */
383 #ifdef CONFIG_AUTO_COMPLETE
390 command_ret_t
do_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
392 return _do_help(cmd_sd_sub
, ARRAY_SIZE(cmd_sd_sub
), cmdtp
, flag
, argc
, argv
);
396 command_ret_t
do_sd(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
401 return CMD_RET_USAGE
;
403 /* drop initial "sd" arg */
407 cp
= find_cmd_tbl(argv
[0], cmd_sd_sub
, ARRAY_SIZE(cmd_sd_sub
));
410 return cp
->cmd(cmdtp
, flag
, argc
, argv
);
412 return CMD_RET_USAGE
;