2 * SD Memory Card emulation as defined in the "SD Memory Card Physical
3 * layer specification, Version 1.10."
5 * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
6 * Copyright (c) 2007 CodeSourcery
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "block/block.h"
35 #include "qemu/bitmap.h"
40 #define DPRINTF(fmt, ...) \
41 do { fprintf(stderr, "SD: " fmt , ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...) do {} while(0)
46 #define ACMD41_ENQUIRY_MASK 0x00ffffff
49 sd_r0
= 0, /* no response */
50 sd_r1
, /* normal response command */
51 sd_r2_i
, /* CID register */
52 sd_r2_s
, /* CSD register */
53 sd_r3
, /* OCR register */
54 sd_r6
= 6, /* Published RCA response */
55 sd_r7
, /* Operating voltage */
62 sd_card_identification_mode
,
63 sd_data_transfer_mode
,
67 sd_inactive_state
= -1,
70 sd_identification_state
,
74 sd_receivingdata_state
,
80 uint32_t mode
; /* current card mode, one of SDCardModes */
81 int32_t state
; /* current card state, one of SDCardStates */
88 uint8_t sd_status
[64];
91 unsigned long *wp_groups
;
99 uint8_t function_group
[6];
103 /* True if we will handle the next command as an ACMD. Note that this does
104 * *not* track the APP_CMD status bit!
107 uint32_t blk_written
;
109 uint32_t data_offset
;
111 qemu_irq readonly_cb
;
112 qemu_irq inserted_cb
;
113 BlockDriverState
*bdrv
;
119 static void sd_set_mode(SDState
*sd
)
122 case sd_inactive_state
:
123 sd
->mode
= sd_inactive
;
128 case sd_identification_state
:
129 sd
->mode
= sd_card_identification_mode
;
132 case sd_standby_state
:
133 case sd_transfer_state
:
134 case sd_sendingdata_state
:
135 case sd_receivingdata_state
:
136 case sd_programming_state
:
137 case sd_disconnect_state
:
138 sd
->mode
= sd_data_transfer_mode
;
143 static const sd_cmd_type_t sd_cmd_type
[64] = {
144 sd_bc
, sd_none
, sd_bcr
, sd_bcr
, sd_none
, sd_none
, sd_none
, sd_ac
,
145 sd_bcr
, sd_ac
, sd_ac
, sd_adtc
, sd_ac
, sd_ac
, sd_none
, sd_ac
,
146 sd_ac
, sd_adtc
, sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
147 sd_adtc
, sd_adtc
, sd_adtc
, sd_adtc
, sd_ac
, sd_ac
, sd_adtc
, sd_none
,
148 sd_ac
, sd_ac
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
, sd_none
,
149 sd_none
, sd_none
, sd_bc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
150 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
,
151 sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
154 static const sd_cmd_type_t sd_acmd_type
[64] = {
155 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
, sd_none
,
156 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_adtc
, sd_none
, sd_none
,
157 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_adtc
, sd_ac
,
158 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
159 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
160 sd_none
, sd_bcr
, sd_ac
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
161 sd_none
, sd_none
, sd_none
, sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
,
162 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
165 static const int sd_cmd_class
[64] = {
166 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0, 1, 0, 0, 0, 0,
167 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6,
168 5, 5, 10, 10, 10, 10, 5, 9, 9, 9, 7, 7, 7, 7, 7, 7,
169 7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8,
172 static uint8_t sd_crc7(void *message
, size_t width
)
175 uint8_t shift_reg
= 0x00;
176 uint8_t *msg
= (uint8_t *) message
;
178 for (i
= 0; i
< width
; i
++, msg
++)
179 for (bit
= 7; bit
>= 0; bit
--) {
181 if ((shift_reg
>> 7) ^ ((*msg
>> bit
) & 1))
188 static uint16_t sd_crc16(void *message
, size_t width
)
191 uint16_t shift_reg
= 0x0000;
192 uint16_t *msg
= (uint16_t *) message
;
195 for (i
= 0; i
< width
; i
++, msg
++)
196 for (bit
= 15; bit
>= 0; bit
--) {
198 if ((shift_reg
>> 15) ^ ((*msg
>> bit
) & 1))
205 static void sd_set_ocr(SDState
*sd
)
207 /* All voltages OK, card power-up OK, Standard Capacity SD Memory Card */
208 sd
->ocr
= 0x80ffff00;
211 static void sd_set_scr(SDState
*sd
)
213 sd
->scr
[0] = 0x00; /* SCR Structure */
214 sd
->scr
[1] = 0x2f; /* SD Security Support */
230 static void sd_set_cid(SDState
*sd
)
232 sd
->cid
[0] = MID
; /* Fake card manufacturer ID (MID) */
233 sd
->cid
[1] = OID
[0]; /* OEM/Application ID (OID) */
235 sd
->cid
[3] = PNM
[0]; /* Fake product name (PNM) */
240 sd
->cid
[8] = PRV
; /* Fake product revision (PRV) */
241 sd
->cid
[9] = 0xde; /* Fake serial number (PSN) */
245 sd
->cid
[13] = 0x00 | /* Manufacture date (MDT) */
246 ((MDT_YR
- 2000) / 10);
247 sd
->cid
[14] = ((MDT_YR
% 10) << 4) | MDT_MON
;
248 sd
->cid
[15] = (sd_crc7(sd
->cid
, 15) << 1) | 1;
251 #define HWBLOCK_SHIFT 9 /* 512 bytes */
252 #define SECTOR_SHIFT 5 /* 16 kilobytes */
253 #define WPGROUP_SHIFT 7 /* 2 megs */
254 #define CMULT_SHIFT 9 /* 512 times HWBLOCK_SIZE */
255 #define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))
257 static const uint8_t sd_csd_rw_mask
[16] = {
258 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
259 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
262 static void sd_set_csd(SDState
*sd
, uint64_t size
)
264 uint32_t csize
= (size
>> (CMULT_SHIFT
+ HWBLOCK_SHIFT
)) - 1;
265 uint32_t sectsize
= (1 << (SECTOR_SHIFT
+ 1)) - 1;
266 uint32_t wpsize
= (1 << (WPGROUP_SHIFT
+ 1)) - 1;
268 if (size
<= 0x40000000) { /* Standard Capacity SD */
269 sd
->csd
[0] = 0x00; /* CSD structure */
270 sd
->csd
[1] = 0x26; /* Data read access-time-1 */
271 sd
->csd
[2] = 0x00; /* Data read access-time-2 */
272 sd
->csd
[3] = 0x5a; /* Max. data transfer rate */
273 sd
->csd
[4] = 0x5f; /* Card Command Classes */
274 sd
->csd
[5] = 0x50 | /* Max. read data block length */
276 sd
->csd
[6] = 0xe0 | /* Partial block for read allowed */
277 ((csize
>> 10) & 0x03);
278 sd
->csd
[7] = 0x00 | /* Device size */
279 ((csize
>> 2) & 0xff);
280 sd
->csd
[8] = 0x3f | /* Max. read current */
281 ((csize
<< 6) & 0xc0);
282 sd
->csd
[9] = 0xfc | /* Max. write current */
283 ((CMULT_SHIFT
- 2) >> 1);
284 sd
->csd
[10] = 0x40 | /* Erase sector size */
285 (((CMULT_SHIFT
- 2) << 7) & 0x80) | (sectsize
>> 1);
286 sd
->csd
[11] = 0x00 | /* Write protect group size */
287 ((sectsize
<< 7) & 0x80) | wpsize
;
288 sd
->csd
[12] = 0x90 | /* Write speed factor */
289 (HWBLOCK_SHIFT
>> 2);
290 sd
->csd
[13] = 0x20 | /* Max. write data block length */
291 ((HWBLOCK_SHIFT
<< 6) & 0xc0);
292 sd
->csd
[14] = 0x00; /* File format group */
293 sd
->csd
[15] = (sd_crc7(sd
->csd
, 15) << 1) | 1;
304 sd
->csd
[7] = (size
>> 16) & 0xff;
305 sd
->csd
[8] = (size
>> 8) & 0xff;
306 sd
->csd
[9] = (size
& 0xff);
313 sd
->ocr
|= 1 << 30; /* High Capacity SD Memory Card */
317 static void sd_set_rca(SDState
*sd
)
322 /* Card status bits, split by clear condition:
323 * A : According to the card current state
324 * B : Always related to the previous command
325 * C : Cleared by read
327 #define CARD_STATUS_A 0x02004100
328 #define CARD_STATUS_B 0x00c01e00
329 #define CARD_STATUS_C 0xfd39a028
331 static void sd_set_cardstatus(SDState
*sd
)
333 sd
->card_status
= 0x00000100;
336 static void sd_set_sdstatus(SDState
*sd
)
338 memset(sd
->sd_status
, 0, 64);
341 static int sd_req_crc_validate(SDRequest
*req
)
344 buffer
[0] = 0x40 | req
->cmd
;
345 buffer
[1] = (req
->arg
>> 24) & 0xff;
346 buffer
[2] = (req
->arg
>> 16) & 0xff;
347 buffer
[3] = (req
->arg
>> 8) & 0xff;
348 buffer
[4] = (req
->arg
>> 0) & 0xff;
350 return sd_crc7(buffer
, 5) != req
->crc
; /* TODO */
353 static void sd_response_r1_make(SDState
*sd
, uint8_t *response
)
355 uint32_t status
= sd
->card_status
;
356 /* Clear the "clear on read" status bits */
357 sd
->card_status
&= ~CARD_STATUS_C
;
359 response
[0] = (status
>> 24) & 0xff;
360 response
[1] = (status
>> 16) & 0xff;
361 response
[2] = (status
>> 8) & 0xff;
362 response
[3] = (status
>> 0) & 0xff;
365 static void sd_response_r3_make(SDState
*sd
, uint8_t *response
)
367 response
[0] = (sd
->ocr
>> 24) & 0xff;
368 response
[1] = (sd
->ocr
>> 16) & 0xff;
369 response
[2] = (sd
->ocr
>> 8) & 0xff;
370 response
[3] = (sd
->ocr
>> 0) & 0xff;
373 static void sd_response_r6_make(SDState
*sd
, uint8_t *response
)
379 status
= ((sd
->card_status
>> 8) & 0xc000) |
380 ((sd
->card_status
>> 6) & 0x2000) |
381 (sd
->card_status
& 0x1fff);
382 sd
->card_status
&= ~(CARD_STATUS_C
& 0xc81fff);
384 response
[0] = (arg
>> 8) & 0xff;
385 response
[1] = arg
& 0xff;
386 response
[2] = (status
>> 8) & 0xff;
387 response
[3] = status
& 0xff;
390 static void sd_response_r7_make(SDState
*sd
, uint8_t *response
)
392 response
[0] = (sd
->vhs
>> 24) & 0xff;
393 response
[1] = (sd
->vhs
>> 16) & 0xff;
394 response
[2] = (sd
->vhs
>> 8) & 0xff;
395 response
[3] = (sd
->vhs
>> 0) & 0xff;
398 static inline uint64_t sd_addr_to_wpnum(uint64_t addr
)
400 return addr
>> (HWBLOCK_SHIFT
+ SECTOR_SHIFT
+ WPGROUP_SHIFT
);
403 static void sd_reset(SDState
*sd
, BlockDriverState
*bdrv
)
409 bdrv_get_geometry(bdrv
, §
);
415 sect
= sd_addr_to_wpnum(size
) + 1;
417 sd
->state
= sd_idle_state
;
422 sd_set_csd(sd
, size
);
423 sd_set_cardstatus(sd
);
429 g_free(sd
->wp_groups
);
430 sd
->wp_switch
= bdrv
? bdrv_is_read_only(bdrv
) : false;
431 sd
->wpgrps_size
= sect
;
432 sd
->wp_groups
= bitmap_new(sd
->wpgrps_size
);
433 memset(sd
->function_group
, 0, sizeof(sd
->function_group
));
439 sd
->expecting_acmd
= false;
442 static void sd_cardchange(void *opaque
, bool load
)
444 SDState
*sd
= opaque
;
446 qemu_set_irq(sd
->inserted_cb
, bdrv_is_inserted(sd
->bdrv
));
447 if (bdrv_is_inserted(sd
->bdrv
)) {
448 sd_reset(sd
, sd
->bdrv
);
449 qemu_set_irq(sd
->readonly_cb
, sd
->wp_switch
);
453 static const BlockDevOps sd_block_ops
= {
454 .change_media_cb
= sd_cardchange
,
457 static const VMStateDescription sd_vmstate
= {
460 .minimum_version_id
= 1,
461 .fields
= (VMStateField
[]) {
462 VMSTATE_UINT32(mode
, SDState
),
463 VMSTATE_INT32(state
, SDState
),
464 VMSTATE_UINT8_ARRAY(cid
, SDState
, 16),
465 VMSTATE_UINT8_ARRAY(csd
, SDState
, 16),
466 VMSTATE_UINT16(rca
, SDState
),
467 VMSTATE_UINT32(card_status
, SDState
),
468 VMSTATE_PARTIAL_BUFFER(sd_status
, SDState
, 1),
469 VMSTATE_UINT32(vhs
, SDState
),
470 VMSTATE_BITMAP(wp_groups
, SDState
, 0, wpgrps_size
),
471 VMSTATE_UINT32(blk_len
, SDState
),
472 VMSTATE_UINT32(erase_start
, SDState
),
473 VMSTATE_UINT32(erase_end
, SDState
),
474 VMSTATE_UINT8_ARRAY(pwd
, SDState
, 16),
475 VMSTATE_UINT32(pwd_len
, SDState
),
476 VMSTATE_UINT8_ARRAY(function_group
, SDState
, 6),
477 VMSTATE_UINT8(current_cmd
, SDState
),
478 VMSTATE_BOOL(expecting_acmd
, SDState
),
479 VMSTATE_UINT32(blk_written
, SDState
),
480 VMSTATE_UINT64(data_start
, SDState
),
481 VMSTATE_UINT32(data_offset
, SDState
),
482 VMSTATE_UINT8_ARRAY(data
, SDState
, 512),
483 VMSTATE_BUFFER_POINTER_UNSAFE(buf
, SDState
, 1, 512),
484 VMSTATE_BOOL(enable
, SDState
),
485 VMSTATE_END_OF_LIST()
489 /* We do not model the chip select pin, so allow the board to select
490 whether card should be in SSI or MMC/SD mode. It is also up to the
491 board to ensure that ssi transfers only occur when the chip select
493 SDState
*sd_init(BlockDriverState
*bs
, bool is_spi
)
497 sd
= (SDState
*) g_malloc0(sizeof(SDState
));
498 sd
->buf
= qemu_blockalign(bs
, 512);
503 bdrv_attach_dev_nofail(sd
->bdrv
, sd
);
504 bdrv_set_dev_ops(sd
->bdrv
, &sd_block_ops
, sd
);
506 vmstate_register(NULL
, -1, &sd_vmstate
, sd
);
510 void sd_set_cb(SDState
*sd
, qemu_irq readonly
, qemu_irq insert
)
512 sd
->readonly_cb
= readonly
;
513 sd
->inserted_cb
= insert
;
514 qemu_set_irq(readonly
, sd
->bdrv
? bdrv_is_read_only(sd
->bdrv
) : 0);
515 qemu_set_irq(insert
, sd
->bdrv
? bdrv_is_inserted(sd
->bdrv
) : 0);
518 static void sd_erase(SDState
*sd
)
521 uint64_t erase_start
= sd
->erase_start
;
522 uint64_t erase_end
= sd
->erase_end
;
524 if (!sd
->erase_start
|| !sd
->erase_end
) {
525 sd
->card_status
|= ERASE_SEQ_ERROR
;
529 if (extract32(sd
->ocr
, OCR_CCS_BITN
, 1)) {
530 /* High capacity memory card: erase units are 512 byte blocks */
535 erase_start
= sd_addr_to_wpnum(erase_start
);
536 erase_end
= sd_addr_to_wpnum(erase_end
);
541 for (i
= erase_start
; i
<= erase_end
; i
++) {
542 if (test_bit(i
, sd
->wp_groups
)) {
543 sd
->card_status
|= WP_ERASE_SKIP
;
548 static uint32_t sd_wpbits(SDState
*sd
, uint64_t addr
)
553 wpnum
= sd_addr_to_wpnum(addr
);
555 for (i
= 0; i
< 32; i
++, wpnum
++, addr
+= WPGROUP_SIZE
) {
556 if (addr
< sd
->size
&& test_bit(wpnum
, sd
->wp_groups
)) {
564 static void sd_function_switch(SDState
*sd
, uint32_t arg
)
566 int i
, mode
, new_func
, crc
;
567 mode
= !!(arg
& 0x80000000);
569 sd
->data
[0] = 0x00; /* Maximum current consumption */
571 sd
->data
[2] = 0x80; /* Supported group 6 functions */
573 sd
->data
[4] = 0x80; /* Supported group 5 functions */
575 sd
->data
[6] = 0x80; /* Supported group 4 functions */
577 sd
->data
[8] = 0x80; /* Supported group 3 functions */
579 sd
->data
[10] = 0x80; /* Supported group 2 functions */
581 sd
->data
[12] = 0x80; /* Supported group 1 functions */
583 for (i
= 0; i
< 6; i
++) {
584 new_func
= (arg
>> (i
* 4)) & 0x0f;
585 if (mode
&& new_func
!= 0x0f)
586 sd
->function_group
[i
] = new_func
;
587 sd
->data
[14 + (i
>> 1)] = new_func
<< ((i
* 4) & 4);
589 memset(&sd
->data
[17], 0, 47);
590 crc
= sd_crc16(sd
->data
, 64);
591 sd
->data
[65] = crc
>> 8;
592 sd
->data
[66] = crc
& 0xff;
595 static inline bool sd_wp_addr(SDState
*sd
, uint64_t addr
)
597 return test_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
600 static void sd_lock_command(SDState
*sd
)
602 int erase
, lock
, clr_pwd
, set_pwd
, pwd_len
;
603 erase
= !!(sd
->data
[0] & 0x08);
604 lock
= sd
->data
[0] & 0x04;
605 clr_pwd
= sd
->data
[0] & 0x02;
606 set_pwd
= sd
->data
[0] & 0x01;
609 pwd_len
= sd
->data
[1];
614 if (!(sd
->card_status
& CARD_IS_LOCKED
) || sd
->blk_len
> 1 ||
615 set_pwd
|| clr_pwd
|| lock
|| sd
->wp_switch
||
616 (sd
->csd
[14] & 0x20)) {
617 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
620 bitmap_zero(sd
->wp_groups
, sd
->wpgrps_size
);
621 sd
->csd
[14] &= ~0x10;
622 sd
->card_status
&= ~CARD_IS_LOCKED
;
624 /* Erasing the entire card here! */
625 fprintf(stderr
, "SD: Card force-erased by CMD42\n");
629 if (sd
->blk_len
< 2 + pwd_len
||
630 pwd_len
<= sd
->pwd_len
||
631 pwd_len
> sd
->pwd_len
+ 16) {
632 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
636 if (sd
->pwd_len
&& memcmp(sd
->pwd
, sd
->data
+ 2, sd
->pwd_len
)) {
637 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
641 pwd_len
-= sd
->pwd_len
;
642 if ((pwd_len
&& !set_pwd
) ||
643 (clr_pwd
&& (set_pwd
|| lock
)) ||
644 (lock
&& !sd
->pwd_len
&& !set_pwd
) ||
645 (!set_pwd
&& !clr_pwd
&&
646 (((sd
->card_status
& CARD_IS_LOCKED
) && lock
) ||
647 (!(sd
->card_status
& CARD_IS_LOCKED
) && !lock
)))) {
648 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
653 memcpy(sd
->pwd
, sd
->data
+ 2 + sd
->pwd_len
, pwd_len
);
654 sd
->pwd_len
= pwd_len
;
662 sd
->card_status
|= CARD_IS_LOCKED
;
664 sd
->card_status
&= ~CARD_IS_LOCKED
;
667 static sd_rsp_type_t
sd_normal_command(SDState
*sd
,
670 uint32_t rca
= 0x0000;
671 uint64_t addr
= (sd
->ocr
& (1 << 30)) ? (uint64_t) req
.arg
<< 9 : req
.arg
;
673 /* Not interpreting this as an app command */
674 sd
->card_status
&= ~APP_CMD
;
676 if (sd_cmd_type
[req
.cmd
] == sd_ac
|| sd_cmd_type
[req
.cmd
] == sd_adtc
)
679 DPRINTF("CMD%d 0x%08x state %d\n", req
.cmd
, req
.arg
, sd
->state
);
681 /* Basic commands (Class 0 and Class 1) */
682 case 0: /* CMD0: GO_IDLE_STATE */
684 case sd_inactive_state
:
685 return sd
->spi
? sd_r1
: sd_r0
;
688 sd
->state
= sd_idle_state
;
689 sd_reset(sd
, sd
->bdrv
);
690 return sd
->spi
? sd_r1
: sd_r0
;
694 case 1: /* CMD1: SEND_OP_CMD */
698 sd
->state
= sd_transfer_state
;
701 case 2: /* CMD2: ALL_SEND_CID */
706 sd
->state
= sd_identification_state
;
714 case 3: /* CMD3: SEND_RELATIVE_ADDR */
718 case sd_identification_state
:
719 case sd_standby_state
:
720 sd
->state
= sd_standby_state
;
729 case 4: /* CMD4: SEND_DSR */
733 case sd_standby_state
:
741 case 5: /* CMD5: reserved for SDIO cards */
744 case 6: /* CMD6: SWITCH_FUNCTION */
748 case sd_data_transfer_mode
:
749 sd_function_switch(sd
, req
.arg
);
750 sd
->state
= sd_sendingdata_state
;
760 case 7: /* CMD7: SELECT/DESELECT_CARD */
764 case sd_standby_state
:
768 sd
->state
= sd_transfer_state
;
771 case sd_transfer_state
:
772 case sd_sendingdata_state
:
776 sd
->state
= sd_standby_state
;
779 case sd_disconnect_state
:
783 sd
->state
= sd_programming_state
;
786 case sd_programming_state
:
790 sd
->state
= sd_disconnect_state
;
798 case 8: /* CMD8: SEND_IF_COND */
799 /* Physical Layer Specification Version 2.00 command */
804 /* No response if not exactly one VHS bit is set. */
805 if (!(req
.arg
>> 8) || (req
.arg
>> ffs(req
.arg
& ~0xff)))
806 return sd
->spi
? sd_r7
: sd_r0
;
817 case 9: /* CMD9: SEND_CSD */
819 case sd_standby_state
:
825 case sd_transfer_state
:
828 sd
->state
= sd_sendingdata_state
;
829 memcpy(sd
->data
, sd
->csd
, 16);
830 sd
->data_start
= addr
;
839 case 10: /* CMD10: SEND_CID */
841 case sd_standby_state
:
847 case sd_transfer_state
:
850 sd
->state
= sd_sendingdata_state
;
851 memcpy(sd
->data
, sd
->cid
, 16);
852 sd
->data_start
= addr
;
861 case 11: /* CMD11: READ_DAT_UNTIL_STOP */
865 case sd_transfer_state
:
866 sd
->state
= sd_sendingdata_state
;
867 sd
->data_start
= req
.arg
;
870 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
871 sd
->card_status
|= ADDRESS_ERROR
;
879 case 12: /* CMD12: STOP_TRANSMISSION */
881 case sd_sendingdata_state
:
882 sd
->state
= sd_transfer_state
;
885 case sd_receivingdata_state
:
886 sd
->state
= sd_programming_state
;
887 /* Bzzzzzzztt .... Operation complete. */
888 sd
->state
= sd_transfer_state
;
896 case 13: /* CMD13: SEND_STATUS */
898 case sd_data_transfer_mode
:
909 case 15: /* CMD15: GO_INACTIVE_STATE */
913 case sd_data_transfer_mode
:
917 sd
->state
= sd_inactive_state
;
925 /* Block read commands (Classs 2) */
926 case 16: /* CMD16: SET_BLOCKLEN */
928 case sd_transfer_state
:
929 if (req
.arg
> (1 << HWBLOCK_SHIFT
))
930 sd
->card_status
|= BLOCK_LEN_ERROR
;
932 sd
->blk_len
= req
.arg
;
941 case 17: /* CMD17: READ_SINGLE_BLOCK */
943 case sd_transfer_state
:
944 sd
->state
= sd_sendingdata_state
;
945 sd
->data_start
= addr
;
948 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
949 sd
->card_status
|= ADDRESS_ERROR
;
957 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
959 case sd_transfer_state
:
960 sd
->state
= sd_sendingdata_state
;
961 sd
->data_start
= addr
;
964 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
965 sd
->card_status
|= ADDRESS_ERROR
;
973 /* Block write commands (Class 4) */
974 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
976 goto unimplemented_cmd
;
978 case sd_transfer_state
:
979 /* Writing in SPI mode not implemented. */
982 sd
->state
= sd_receivingdata_state
;
983 sd
->data_start
= addr
;
987 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
988 sd
->card_status
|= ADDRESS_ERROR
;
989 if (sd_wp_addr(sd
, sd
->data_start
))
990 sd
->card_status
|= WP_VIOLATION
;
991 if (sd
->csd
[14] & 0x30)
992 sd
->card_status
|= WP_VIOLATION
;
1000 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1002 goto unimplemented_cmd
;
1003 switch (sd
->state
) {
1004 case sd_transfer_state
:
1005 /* Writing in SPI mode not implemented. */
1008 sd
->state
= sd_receivingdata_state
;
1009 sd
->data_start
= addr
;
1010 sd
->data_offset
= 0;
1011 sd
->blk_written
= 0;
1013 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
1014 sd
->card_status
|= ADDRESS_ERROR
;
1015 if (sd_wp_addr(sd
, sd
->data_start
))
1016 sd
->card_status
|= WP_VIOLATION
;
1017 if (sd
->csd
[14] & 0x30)
1018 sd
->card_status
|= WP_VIOLATION
;
1026 case 26: /* CMD26: PROGRAM_CID */
1029 switch (sd
->state
) {
1030 case sd_transfer_state
:
1031 sd
->state
= sd_receivingdata_state
;
1033 sd
->data_offset
= 0;
1041 case 27: /* CMD27: PROGRAM_CSD */
1043 goto unimplemented_cmd
;
1044 switch (sd
->state
) {
1045 case sd_transfer_state
:
1046 sd
->state
= sd_receivingdata_state
;
1048 sd
->data_offset
= 0;
1056 /* Write protection (Class 6) */
1057 case 28: /* CMD28: SET_WRITE_PROT */
1058 switch (sd
->state
) {
1059 case sd_transfer_state
:
1060 if (addr
>= sd
->size
) {
1061 sd
->card_status
|= ADDRESS_ERROR
;
1065 sd
->state
= sd_programming_state
;
1066 set_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
1067 /* Bzzzzzzztt .... Operation complete. */
1068 sd
->state
= sd_transfer_state
;
1076 case 29: /* CMD29: CLR_WRITE_PROT */
1077 switch (sd
->state
) {
1078 case sd_transfer_state
:
1079 if (addr
>= sd
->size
) {
1080 sd
->card_status
|= ADDRESS_ERROR
;
1084 sd
->state
= sd_programming_state
;
1085 clear_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
1086 /* Bzzzzzzztt .... Operation complete. */
1087 sd
->state
= sd_transfer_state
;
1095 case 30: /* CMD30: SEND_WRITE_PROT */
1096 switch (sd
->state
) {
1097 case sd_transfer_state
:
1098 sd
->state
= sd_sendingdata_state
;
1099 *(uint32_t *) sd
->data
= sd_wpbits(sd
, req
.arg
);
1100 sd
->data_start
= addr
;
1101 sd
->data_offset
= 0;
1109 /* Erase commands (Class 5) */
1110 case 32: /* CMD32: ERASE_WR_BLK_START */
1111 switch (sd
->state
) {
1112 case sd_transfer_state
:
1113 sd
->erase_start
= req
.arg
;
1121 case 33: /* CMD33: ERASE_WR_BLK_END */
1122 switch (sd
->state
) {
1123 case sd_transfer_state
:
1124 sd
->erase_end
= req
.arg
;
1132 case 38: /* CMD38: ERASE */
1133 switch (sd
->state
) {
1134 case sd_transfer_state
:
1135 if (sd
->csd
[14] & 0x30) {
1136 sd
->card_status
|= WP_VIOLATION
;
1140 sd
->state
= sd_programming_state
;
1142 /* Bzzzzzzztt .... Operation complete. */
1143 sd
->state
= sd_transfer_state
;
1151 /* Lock card commands (Class 7) */
1152 case 42: /* CMD42: LOCK_UNLOCK */
1154 goto unimplemented_cmd
;
1155 switch (sd
->state
) {
1156 case sd_transfer_state
:
1157 sd
->state
= sd_receivingdata_state
;
1159 sd
->data_offset
= 0;
1169 /* CMD52, CMD53: reserved for SDIO cards
1170 * (see the SDIO Simplified Specification V2.0)
1171 * Handle as illegal command but do not complain
1172 * on stderr, as some OSes may use these in their
1173 * probing for presence of an SDIO card.
1177 /* Application specific commands (Class 8) */
1178 case 55: /* CMD55: APP_CMD */
1182 sd
->expecting_acmd
= true;
1183 sd
->card_status
|= APP_CMD
;
1186 case 56: /* CMD56: GEN_CMD */
1187 fprintf(stderr
, "SD: GEN_CMD 0x%08x\n", req
.arg
);
1189 switch (sd
->state
) {
1190 case sd_transfer_state
:
1191 sd
->data_offset
= 0;
1193 sd
->state
= sd_sendingdata_state
;
1195 sd
->state
= sd_receivingdata_state
;
1205 fprintf(stderr
, "SD: Unknown CMD%i\n", req
.cmd
);
1209 /* Commands that are recognised but not yet implemented in SPI mode. */
1210 fprintf(stderr
, "SD: CMD%i not implemented in SPI mode\n", req
.cmd
);
1214 fprintf(stderr
, "SD: CMD%i in a wrong state\n", req
.cmd
);
1218 static sd_rsp_type_t
sd_app_command(SDState
*sd
,
1221 DPRINTF("ACMD%d 0x%08x\n", req
.cmd
, req
.arg
);
1222 sd
->card_status
|= APP_CMD
;
1224 case 6: /* ACMD6: SET_BUS_WIDTH */
1225 switch (sd
->state
) {
1226 case sd_transfer_state
:
1227 sd
->sd_status
[0] &= 0x3f;
1228 sd
->sd_status
[0] |= (req
.arg
& 0x03) << 6;
1236 case 13: /* ACMD13: SD_STATUS */
1237 switch (sd
->state
) {
1238 case sd_transfer_state
:
1239 sd
->state
= sd_sendingdata_state
;
1241 sd
->data_offset
= 0;
1249 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
1250 switch (sd
->state
) {
1251 case sd_transfer_state
:
1252 *(uint32_t *) sd
->data
= sd
->blk_written
;
1254 sd
->state
= sd_sendingdata_state
;
1256 sd
->data_offset
= 0;
1264 case 23: /* ACMD23: SET_WR_BLK_ERASE_COUNT */
1265 switch (sd
->state
) {
1266 case sd_transfer_state
:
1274 case 41: /* ACMD41: SD_APP_OP_COND */
1277 sd
->state
= sd_transfer_state
;
1280 switch (sd
->state
) {
1282 /* We accept any voltage. 10000 V is nothing.
1284 * We don't model init delay so just advance straight to ready state
1285 * unless it's an enquiry ACMD41 (bits 23:0 == 0).
1287 if (req
.arg
& ACMD41_ENQUIRY_MASK
) {
1288 sd
->state
= sd_ready_state
;
1298 case 42: /* ACMD42: SET_CLR_CARD_DETECT */
1299 switch (sd
->state
) {
1300 case sd_transfer_state
:
1301 /* Bringing in the 50KOhm pull-up resistor... Done. */
1309 case 51: /* ACMD51: SEND_SCR */
1310 switch (sd
->state
) {
1311 case sd_transfer_state
:
1312 sd
->state
= sd_sendingdata_state
;
1314 sd
->data_offset
= 0;
1323 /* Fall back to standard commands. */
1324 return sd_normal_command(sd
, req
);
1327 fprintf(stderr
, "SD: ACMD%i in a wrong state\n", req
.cmd
);
1331 static int cmd_valid_while_locked(SDState
*sd
, SDRequest
*req
)
1333 /* Valid commands in locked state:
1335 * lock card class (7)
1337 * implicitly, the ACMD prefix CMD55
1339 * Anything else provokes an "illegal command" response.
1341 if (sd
->expecting_acmd
) {
1342 return req
->cmd
== 41 || req
->cmd
== 42;
1344 if (req
->cmd
== 16 || req
->cmd
== 55) {
1347 return sd_cmd_class
[req
->cmd
] == 0 || sd_cmd_class
[req
->cmd
] == 7;
1350 int sd_do_command(SDState
*sd
, SDRequest
*req
,
1351 uint8_t *response
) {
1353 sd_rsp_type_t rtype
;
1356 if (!sd
->bdrv
|| !bdrv_is_inserted(sd
->bdrv
) || !sd
->enable
) {
1360 if (sd_req_crc_validate(req
)) {
1361 sd
->card_status
|= COM_CRC_ERROR
;
1366 if (sd
->card_status
& CARD_IS_LOCKED
) {
1367 if (!cmd_valid_while_locked(sd
, req
)) {
1368 sd
->card_status
|= ILLEGAL_COMMAND
;
1369 sd
->expecting_acmd
= false;
1370 fprintf(stderr
, "SD: Card is locked\n");
1376 last_state
= sd
->state
;
1379 if (sd
->expecting_acmd
) {
1380 sd
->expecting_acmd
= false;
1381 rtype
= sd_app_command(sd
, *req
);
1383 rtype
= sd_normal_command(sd
, *req
);
1386 if (rtype
== sd_illegal
) {
1387 sd
->card_status
|= ILLEGAL_COMMAND
;
1389 /* Valid command, we can update the 'state before command' bits.
1390 * (Do this now so they appear in r1 responses.)
1392 sd
->current_cmd
= req
->cmd
;
1393 sd
->card_status
&= ~CURRENT_STATE
;
1394 sd
->card_status
|= (last_state
<< 9);
1401 sd_response_r1_make(sd
, response
);
1406 memcpy(response
, sd
->cid
, sizeof(sd
->cid
));
1411 memcpy(response
, sd
->csd
, sizeof(sd
->csd
));
1416 sd_response_r3_make(sd
, response
);
1421 sd_response_r6_make(sd
, response
);
1426 sd_response_r7_make(sd
, response
);
1437 if (rtype
!= sd_illegal
) {
1438 /* Clear the "clear on valid command" status bits now we've
1441 sd
->card_status
&= ~CARD_STATUS_B
;
1447 DPRINTF("Response:");
1448 for (i
= 0; i
< rsplen
; i
++)
1449 fprintf(stderr
, " %02x", response
[i
]);
1450 fprintf(stderr
, " state %d\n", sd
->state
);
1452 DPRINTF("No response %d\n", sd
->state
);
1459 static void sd_blk_read(SDState
*sd
, uint64_t addr
, uint32_t len
)
1461 uint64_t end
= addr
+ len
;
1463 DPRINTF("sd_blk_read: addr = 0x%08llx, len = %d\n",
1464 (unsigned long long) addr
, len
);
1465 if (!sd
->bdrv
|| bdrv_read(sd
->bdrv
, addr
>> 9, sd
->buf
, 1) < 0) {
1466 fprintf(stderr
, "sd_blk_read: read error on host side\n");
1470 if (end
> (addr
& ~511) + 512) {
1471 memcpy(sd
->data
, sd
->buf
+ (addr
& 511), 512 - (addr
& 511));
1473 if (bdrv_read(sd
->bdrv
, end
>> 9, sd
->buf
, 1) < 0) {
1474 fprintf(stderr
, "sd_blk_read: read error on host side\n");
1477 memcpy(sd
->data
+ 512 - (addr
& 511), sd
->buf
, end
& 511);
1479 memcpy(sd
->data
, sd
->buf
+ (addr
& 511), len
);
1482 static void sd_blk_write(SDState
*sd
, uint64_t addr
, uint32_t len
)
1484 uint64_t end
= addr
+ len
;
1486 if ((addr
& 511) || len
< 512)
1487 if (!sd
->bdrv
|| bdrv_read(sd
->bdrv
, addr
>> 9, sd
->buf
, 1) < 0) {
1488 fprintf(stderr
, "sd_blk_write: read error on host side\n");
1492 if (end
> (addr
& ~511) + 512) {
1493 memcpy(sd
->buf
+ (addr
& 511), sd
->data
, 512 - (addr
& 511));
1494 if (bdrv_write(sd
->bdrv
, addr
>> 9, sd
->buf
, 1) < 0) {
1495 fprintf(stderr
, "sd_blk_write: write error on host side\n");
1499 if (bdrv_read(sd
->bdrv
, end
>> 9, sd
->buf
, 1) < 0) {
1500 fprintf(stderr
, "sd_blk_write: read error on host side\n");
1503 memcpy(sd
->buf
, sd
->data
+ 512 - (addr
& 511), end
& 511);
1504 if (bdrv_write(sd
->bdrv
, end
>> 9, sd
->buf
, 1) < 0) {
1505 fprintf(stderr
, "sd_blk_write: write error on host side\n");
1508 memcpy(sd
->buf
+ (addr
& 511), sd
->data
, len
);
1509 if (!sd
->bdrv
|| bdrv_write(sd
->bdrv
, addr
>> 9, sd
->buf
, 1) < 0) {
1510 fprintf(stderr
, "sd_blk_write: write error on host side\n");
1515 #define BLK_READ_BLOCK(a, len) sd_blk_read(sd, a, len)
1516 #define BLK_WRITE_BLOCK(a, len) sd_blk_write(sd, a, len)
1517 #define APP_READ_BLOCK(a, len) memset(sd->data, 0xec, len)
1518 #define APP_WRITE_BLOCK(a, len)
1520 void sd_write_data(SDState
*sd
, uint8_t value
)
1524 if (!sd
->bdrv
|| !bdrv_is_inserted(sd
->bdrv
) || !sd
->enable
)
1527 if (sd
->state
!= sd_receivingdata_state
) {
1528 fprintf(stderr
, "sd_write_data: not in Receiving-Data state\n");
1532 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1535 switch (sd
->current_cmd
) {
1536 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
1537 sd
->data
[sd
->data_offset
++] = value
;
1538 if (sd
->data_offset
>= sd
->blk_len
) {
1539 /* TODO: Check CRC before committing */
1540 sd
->state
= sd_programming_state
;
1541 BLK_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1543 sd
->csd
[14] |= 0x40;
1544 /* Bzzzzzzztt .... Operation complete. */
1545 sd
->state
= sd_transfer_state
;
1549 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1550 if (sd
->data_offset
== 0) {
1551 /* Start of the block - let's check the address is valid */
1552 if (sd
->data_start
+ sd
->blk_len
> sd
->size
) {
1553 sd
->card_status
|= ADDRESS_ERROR
;
1556 if (sd_wp_addr(sd
, sd
->data_start
)) {
1557 sd
->card_status
|= WP_VIOLATION
;
1561 sd
->data
[sd
->data_offset
++] = value
;
1562 if (sd
->data_offset
>= sd
->blk_len
) {
1563 /* TODO: Check CRC before committing */
1564 sd
->state
= sd_programming_state
;
1565 BLK_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1567 sd
->data_start
+= sd
->blk_len
;
1568 sd
->data_offset
= 0;
1569 sd
->csd
[14] |= 0x40;
1571 /* Bzzzzzzztt .... Operation complete. */
1572 sd
->state
= sd_receivingdata_state
;
1576 case 26: /* CMD26: PROGRAM_CID */
1577 sd
->data
[sd
->data_offset
++] = value
;
1578 if (sd
->data_offset
>= sizeof(sd
->cid
)) {
1579 /* TODO: Check CRC before committing */
1580 sd
->state
= sd_programming_state
;
1581 for (i
= 0; i
< sizeof(sd
->cid
); i
++)
1582 if ((sd
->cid
[i
] | 0x00) != sd
->data
[i
])
1583 sd
->card_status
|= CID_CSD_OVERWRITE
;
1585 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
1586 for (i
= 0; i
< sizeof(sd
->cid
); i
++) {
1588 sd
->cid
[i
] &= sd
->data
[i
];
1590 /* Bzzzzzzztt .... Operation complete. */
1591 sd
->state
= sd_transfer_state
;
1595 case 27: /* CMD27: PROGRAM_CSD */
1596 sd
->data
[sd
->data_offset
++] = value
;
1597 if (sd
->data_offset
>= sizeof(sd
->csd
)) {
1598 /* TODO: Check CRC before committing */
1599 sd
->state
= sd_programming_state
;
1600 for (i
= 0; i
< sizeof(sd
->csd
); i
++)
1601 if ((sd
->csd
[i
] | sd_csd_rw_mask
[i
]) !=
1602 (sd
->data
[i
] | sd_csd_rw_mask
[i
]))
1603 sd
->card_status
|= CID_CSD_OVERWRITE
;
1605 /* Copy flag (OTP) & Permanent write protect */
1606 if (sd
->csd
[14] & ~sd
->data
[14] & 0x60)
1607 sd
->card_status
|= CID_CSD_OVERWRITE
;
1609 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
1610 for (i
= 0; i
< sizeof(sd
->csd
); i
++) {
1611 sd
->csd
[i
] |= sd_csd_rw_mask
[i
];
1612 sd
->csd
[i
] &= sd
->data
[i
];
1614 /* Bzzzzzzztt .... Operation complete. */
1615 sd
->state
= sd_transfer_state
;
1619 case 42: /* CMD42: LOCK_UNLOCK */
1620 sd
->data
[sd
->data_offset
++] = value
;
1621 if (sd
->data_offset
>= sd
->blk_len
) {
1622 /* TODO: Check CRC before committing */
1623 sd
->state
= sd_programming_state
;
1624 sd_lock_command(sd
);
1625 /* Bzzzzzzztt .... Operation complete. */
1626 sd
->state
= sd_transfer_state
;
1630 case 56: /* CMD56: GEN_CMD */
1631 sd
->data
[sd
->data_offset
++] = value
;
1632 if (sd
->data_offset
>= sd
->blk_len
) {
1633 APP_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1634 sd
->state
= sd_transfer_state
;
1639 fprintf(stderr
, "sd_write_data: unknown command\n");
1644 uint8_t sd_read_data(SDState
*sd
)
1646 /* TODO: Append CRCs */
1650 if (!sd
->bdrv
|| !bdrv_is_inserted(sd
->bdrv
) || !sd
->enable
)
1653 if (sd
->state
!= sd_sendingdata_state
) {
1654 fprintf(stderr
, "sd_read_data: not in Sending-Data state\n");
1658 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1661 io_len
= (sd
->ocr
& (1 << 30)) ? 512 : sd
->blk_len
;
1663 switch (sd
->current_cmd
) {
1664 case 6: /* CMD6: SWITCH_FUNCTION */
1665 ret
= sd
->data
[sd
->data_offset
++];
1667 if (sd
->data_offset
>= 64)
1668 sd
->state
= sd_transfer_state
;
1671 case 9: /* CMD9: SEND_CSD */
1672 case 10: /* CMD10: SEND_CID */
1673 ret
= sd
->data
[sd
->data_offset
++];
1675 if (sd
->data_offset
>= 16)
1676 sd
->state
= sd_transfer_state
;
1679 case 11: /* CMD11: READ_DAT_UNTIL_STOP */
1680 if (sd
->data_offset
== 0)
1681 BLK_READ_BLOCK(sd
->data_start
, io_len
);
1682 ret
= sd
->data
[sd
->data_offset
++];
1684 if (sd
->data_offset
>= io_len
) {
1685 sd
->data_start
+= io_len
;
1686 sd
->data_offset
= 0;
1687 if (sd
->data_start
+ io_len
> sd
->size
) {
1688 sd
->card_status
|= ADDRESS_ERROR
;
1694 case 13: /* ACMD13: SD_STATUS */
1695 ret
= sd
->sd_status
[sd
->data_offset
++];
1697 if (sd
->data_offset
>= sizeof(sd
->sd_status
))
1698 sd
->state
= sd_transfer_state
;
1701 case 17: /* CMD17: READ_SINGLE_BLOCK */
1702 if (sd
->data_offset
== 0)
1703 BLK_READ_BLOCK(sd
->data_start
, io_len
);
1704 ret
= sd
->data
[sd
->data_offset
++];
1706 if (sd
->data_offset
>= io_len
)
1707 sd
->state
= sd_transfer_state
;
1710 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
1711 if (sd
->data_offset
== 0)
1712 BLK_READ_BLOCK(sd
->data_start
, io_len
);
1713 ret
= sd
->data
[sd
->data_offset
++];
1715 if (sd
->data_offset
>= io_len
) {
1716 sd
->data_start
+= io_len
;
1717 sd
->data_offset
= 0;
1718 if (sd
->data_start
+ io_len
> sd
->size
) {
1719 sd
->card_status
|= ADDRESS_ERROR
;
1725 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
1726 ret
= sd
->data
[sd
->data_offset
++];
1728 if (sd
->data_offset
>= 4)
1729 sd
->state
= sd_transfer_state
;
1732 case 30: /* CMD30: SEND_WRITE_PROT */
1733 ret
= sd
->data
[sd
->data_offset
++];
1735 if (sd
->data_offset
>= 4)
1736 sd
->state
= sd_transfer_state
;
1739 case 51: /* ACMD51: SEND_SCR */
1740 ret
= sd
->scr
[sd
->data_offset
++];
1742 if (sd
->data_offset
>= sizeof(sd
->scr
))
1743 sd
->state
= sd_transfer_state
;
1746 case 56: /* CMD56: GEN_CMD */
1747 if (sd
->data_offset
== 0)
1748 APP_READ_BLOCK(sd
->data_start
, sd
->blk_len
);
1749 ret
= sd
->data
[sd
->data_offset
++];
1751 if (sd
->data_offset
>= sd
->blk_len
)
1752 sd
->state
= sd_transfer_state
;
1756 fprintf(stderr
, "sd_read_data: unknown command\n");
1763 bool sd_data_ready(SDState
*sd
)
1765 return sd
->state
== sd_sendingdata_state
;
1768 void sd_enable(SDState
*sd
, bool enable
)
1770 sd
->enable
= enable
;