int sd_do_command(SDState *sd, SDRequest *req,

                  uint8_t *response) {

    uint32_t last_status = sd->card_status;

    sd_rsp_type_t rtype;

    int rsplen;



    if (!sd->bdrv || !bdrv_is_inserted(sd->bdrv) || !sd->enable) {

        return 0;

    }



    if (sd_req_crc_validate(req)) {

        sd->card_status |= COM_CRC_ERROR;

        rtype = sd_illegal;

        goto send_response;

    }



    sd->card_status &= ~CARD_STATUS_B;

    sd_set_status(sd);



    if (last_status & CARD_IS_LOCKED) {

        if (!cmd_valid_while_locked(sd, req)) {

            sd->card_status |= ILLEGAL_COMMAND;

            fprintf(stderr, "SD: Card is locked\n");

            rtype = sd_illegal;

            goto send_response;

        }

    }



    if (last_status & APP_CMD) {

        rtype = sd_app_command(sd, *req);

        sd->card_status &= ~APP_CMD;

    } else

        rtype = sd_normal_command(sd, *req);



    if (rtype == sd_illegal) {

        sd->card_status |= ILLEGAL_COMMAND;

    }



    sd->current_cmd = req->cmd;



send_response:

    switch (rtype) {

    case sd_r1:

    case sd_r1b:

        sd_response_r1_make(sd, response, last_status);

        rsplen = 4;

        break;



    case sd_r2_i:

        memcpy(response, sd->cid, sizeof(sd->cid));

        rsplen = 16;

        break;



    case sd_r2_s:

        memcpy(response, sd->csd, sizeof(sd->csd));

        rsplen = 16;

        break;



    case sd_r3:

        sd_response_r3_make(sd, response);

        rsplen = 4;

        break;



    case sd_r6:

        sd_response_r6_make(sd, response);

        rsplen = 4;

        break;



    case sd_r7:

        sd_response_r7_make(sd, response);

        rsplen = 4;

        break;



    case sd_r0:

    case sd_illegal:

    default:

        rsplen = 0;

        break;

    }



#ifdef DEBUG_SD

    if (rsplen) {

        int i;

        DPRINTF("Response:");

        for (i = 0; i < rsplen; i++)

            printf(" %02x", response[i]);

        printf(" state %d\n", sd->state);

    } else {

        DPRINTF("No response %d\n", sd->state);

    }

#endif



    return rsplen;

}

int sd_do_command(SDState *sd, SDRequest *req,

                  uint8_t *response) {

    uint32_t last_status = sd->card_status;

    sd_rsp_type_t rtype;

    int rsplen;



    if (!sd->bdrv || !bdrv_is_inserted(sd->bdrv) || !sd->enable) {

        return 0;

    }



    if (sd_req_crc_validate(req)) {

        sd->card_status |= COM_CRC_ERROR;

        rtype = sd_illegal;

        goto send_response;

    }



    sd->card_status &= ~CARD_STATUS_B;

    sd_set_status(sd);



    if (last_status & CARD_IS_LOCKED) {

        if (!cmd_valid_while_locked(sd, req)) {

            sd->card_status |= ILLEGAL_COMMAND;

            fprintf(stderr, "SD: Card is locked\n");

            rtype = sd_illegal;

            goto send_response;

        }

    }



    if (last_status & APP_CMD) {

        rtype = sd_app_command(sd, *req);

        sd->card_status &= ~APP_CMD;

    } else

        rtype = sd_normal_command(sd, *req);



    if (rtype == sd_illegal) {

        sd->card_status |= ILLEGAL_COMMAND;

    }



    sd->current_cmd = req->cmd;



send_response:

    switch (rtype) {

    case sd_r1:

    case sd_r1b:

        sd_response_r1_make(sd, response, last_status);

        rsplen = 4;

        break;



    case sd_r2_i:

        memcpy(response, sd->cid, sizeof(sd->cid));

        rsplen = 16;

        break;



    case sd_r2_s:

        memcpy(response, sd->csd, sizeof(sd->csd));

        rsplen = 16;

        break;



    case sd_r3:

        sd_response_r3_make(sd, response);

        rsplen = 4;

        break;



    case sd_r6:

        sd_response_r6_make(sd, response);

        rsplen = 4;

        break;



    case sd_r7:

        sd_response_r7_make(sd, response);

        rsplen = 4;

        break;



    case sd_r0:

    case sd_illegal:

    default:

        rsplen = 0;

        break;

    }



#ifdef DEBUG_SD

    if (rsplen) {

        int i;

        DPRINTF("Response:");

        for (i = 0; i < rsplen; i++)

            printf(" %02x", response[i]);

        printf(" state %d\n", sd->state);

    } else {

        DPRINTF("No response %d\n", sd->state);

    }

#endif



    return rsplen;

}

int FUNC_0(SDState *VAR_0, SDRequest *VAR_1,

                  uint8_t *VAR_2) {

    uint32_t last_status = VAR_0->card_status;

    sd_rsp_type_t rtype;

    int VAR_3;



    if (!VAR_0->bdrv || !bdrv_is_inserted(VAR_0->bdrv) || !VAR_0->enable) {

        return 0;

    }



    if (sd_req_crc_validate(VAR_1)) {

        VAR_0->card_status |= COM_CRC_ERROR;

        rtype = sd_illegal;

        goto send_response;

    }



    VAR_0->card_status &= ~CARD_STATUS_B;

    sd_set_status(VAR_0);



    if (last_status & CARD_IS_LOCKED) {

        if (!cmd_valid_while_locked(VAR_0, VAR_1)) {

            VAR_0->card_status |= ILLEGAL_COMMAND;

            fprintf(stderr, "SD: Card is locked\n");

            rtype = sd_illegal;

            goto send_response;

        }

    }



    if (last_status & APP_CMD) {

        rtype = sd_app_command(VAR_0, *VAR_1);

        VAR_0->card_status &= ~APP_CMD;

    } else

        rtype = sd_normal_command(VAR_0, *VAR_1);



    if (rtype == sd_illegal) {

        VAR_0->card_status |= ILLEGAL_COMMAND;

    }



    VAR_0->current_cmd = VAR_1->cmd;



send_response:

    switch (rtype) {

    case sd_r1:

    case sd_r1b:

        sd_response_r1_make(VAR_0, VAR_2, last_status);

        VAR_3 = 4;

        break;



    case sd_r2_i:

        memcpy(VAR_2, VAR_0->cid, sizeof(VAR_0->cid));

        VAR_3 = 16;

        break;



    case sd_r2_s:

        memcpy(VAR_2, VAR_0->csd, sizeof(VAR_0->csd));

        VAR_3 = 16;

        break;



    case sd_r3:

        sd_response_r3_make(VAR_0, VAR_2);

        VAR_3 = 4;

        break;



    case sd_r6:

        sd_response_r6_make(VAR_0, VAR_2);

        VAR_3 = 4;

        break;



    case sd_r7:

        sd_response_r7_make(VAR_0, VAR_2);

        VAR_3 = 4;

        break;



    case sd_r0:

    case sd_illegal:

    default:

        VAR_3 = 0;

        break;

    }



#ifdef DEBUG_SD

    if (VAR_3) {

        int i;

        DPRINTF("Response:");

        for (i = 0; i < VAR_3; i++)

            printf(" %02x", VAR_2[i]);

        printf(" state %d\n", VAR_0->state);

    } else {

        DPRINTF("No VAR_2 %d\n", VAR_0->state);

    }

#endif



    return VAR_3;

}

int FUNC_0(SDState *VAR_0, SDRequest *VAR_1,
uint8_t *VAR_2) { uint32_t last_status = VAR_0->card_status; sd_rsp_type_t rtype; int VAR_3; if (!VAR_0->bdrv || !bdrv_is_inserted(VAR_0->bdrv) || !VAR_0->enable) { return 0; } if (sd_req_crc_validate(VAR_1)) { VAR_0->card_status |= COM_CRC_ERROR; rtype = sd_illegal; goto send_response; } VAR_0->card_status &= ~CARD_STATUS_B; sd_set_status(VAR_0); if (last_status & CARD_IS_LOCKED) { if (!cmd_valid_while_locked(VAR_0, VAR_1)) { VAR_0->card_status |= ILLEGAL_COMMAND; fprintf(stderr, "SD: Card is locked\n"); rtype = sd_illegal; goto send_response; } } if (last_status & APP_CMD) { rtype = sd_app_command(VAR_0, *VAR_1); VAR_0->card_status &= ~APP_CMD; } else rtype = sd_normal_command(VAR_0, *VAR_1); if (rtype == sd_illegal) { VAR_0->card_status |= ILLEGAL_COMMAND; } VAR_0->current_cmd = VAR_1->cmd; send_response:
switch (rtype) { case sd_r1:
case sd_r1b:
sd_response_r1_make(VAR_0, VAR_2, last_status); VAR_3 = 4; break; case sd_r2_i:
memcpy(VAR_2, VAR_0->cid, sizeof(VAR_0->cid)); VAR_3 = 16; break; case sd_r2_s:
memcpy(VAR_2, VAR_0->csd, sizeof(VAR_0->csd)); VAR_3 = 16; break; case sd_r3:
sd_response_r3_make(VAR_0, VAR_2); VAR_3 = 4; break; case sd_r6:
sd_response_r6_make(VAR_0, VAR_2); VAR_3 = 4; break; case sd_r7:
sd_response_r7_make(VAR_0, VAR_2); VAR_3 = 4; break; case sd_r0:
case sd_illegal:
default:
VAR_3 = 0; break; } #ifdef DEBUG_SD
if (VAR_3) { int i; DPRINTF("Response:"); for (i = 0; i < VAR_3; i++) printf(" %02x", VAR_2[i]); printf(" state %d\n", VAR_0->state); } else { DPRINTF("No VAR_2 %d\n", VAR_0->state); } #endif
return VAR_3; }