static void sd_lock_command(SDState *sd)

{

    int erase, lock, clr_pwd, set_pwd, pwd_len;

    erase = !!(sd->data[0] & 0x08);

    lock = sd->data[0] & 0x04;

    clr_pwd = sd->data[0] & 0x02;

    set_pwd = sd->data[0] & 0x01;



    if (sd->blk_len > 1)

        pwd_len = sd->data[1];

    else

        pwd_len = 0;



    if (erase) {

        if (!(sd->card_status & CARD_IS_LOCKED) || sd->blk_len > 1 ||

                        set_pwd || clr_pwd || lock || sd->wp_switch ||

                        (sd->csd[14] & 0x20)) {

            sd->card_status |= LOCK_UNLOCK_FAILED;

            return;

        }

        memset(sd->wp_groups, 0, sizeof(int) * (sd->size >>

                        (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)));

        sd->csd[14] &= ~0x10;

        sd->card_status &= ~CARD_IS_LOCKED;

        sd->pwd_len = 0;

        /* Erasing the entire card here! */

        fprintf(stderr, "SD: Card force-erased by CMD42\n");

        return;

    }



    if (sd->blk_len < 2 + pwd_len ||

                    pwd_len <= sd->pwd_len ||

                    pwd_len > sd->pwd_len + 16) {

        sd->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    if (sd->pwd_len && memcmp(sd->pwd, sd->data + 2, sd->pwd_len)) {

        sd->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    pwd_len -= sd->pwd_len;

    if ((pwd_len && !set_pwd) ||

                    (clr_pwd && (set_pwd || lock)) ||

                    (lock && !sd->pwd_len && !set_pwd) ||

                    (!set_pwd && !clr_pwd &&

                     (((sd->card_status & CARD_IS_LOCKED) && lock) ||

                      (!(sd->card_status & CARD_IS_LOCKED) && !lock)))) {

        sd->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    if (set_pwd) {

        memcpy(sd->pwd, sd->data + 2 + sd->pwd_len, pwd_len);

        sd->pwd_len = pwd_len;

    }



    if (clr_pwd) {

        sd->pwd_len = 0;

    }



    if (lock)

        sd->card_status |= CARD_IS_LOCKED;

    else

        sd->card_status &= ~CARD_IS_LOCKED;

}

static void sd_lock_command(SDState *sd)

{

    int erase, lock, clr_pwd, set_pwd, pwd_len;

    erase = !!(sd->data[0] & 0x08);

    lock = sd->data[0] & 0x04;

    clr_pwd = sd->data[0] & 0x02;

    set_pwd = sd->data[0] & 0x01;



    if (sd->blk_len > 1)

        pwd_len = sd->data[1];

    else

        pwd_len = 0;



    if (erase) {

        if (!(sd->card_status & CARD_IS_LOCKED) || sd->blk_len > 1 ||

                        set_pwd || clr_pwd || lock || sd->wp_switch ||

                        (sd->csd[14] & 0x20)) {

            sd->card_status |= LOCK_UNLOCK_FAILED;

            return;

        }

        memset(sd->wp_groups, 0, sizeof(int) * (sd->size >>

                        (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)));

        sd->csd[14] &= ~0x10;

        sd->card_status &= ~CARD_IS_LOCKED;

        sd->pwd_len = 0;

        

        fprintf(stderr, "SD: Card force-erased by CMD42\n");

        return;

    }



    if (sd->blk_len < 2 + pwd_len ||

                    pwd_len <= sd->pwd_len ||

                    pwd_len > sd->pwd_len + 16) {

        sd->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    if (sd->pwd_len && memcmp(sd->pwd, sd->data + 2, sd->pwd_len)) {

        sd->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    pwd_len -= sd->pwd_len;

    if ((pwd_len && !set_pwd) ||

                    (clr_pwd && (set_pwd || lock)) ||

                    (lock && !sd->pwd_len && !set_pwd) ||

                    (!set_pwd && !clr_pwd &&

                     (((sd->card_status & CARD_IS_LOCKED) && lock) ||

                      (!(sd->card_status & CARD_IS_LOCKED) && !lock)))) {

        sd->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    if (set_pwd) {

        memcpy(sd->pwd, sd->data + 2 + sd->pwd_len, pwd_len);

        sd->pwd_len = pwd_len;

    }



    if (clr_pwd) {

        sd->pwd_len = 0;

    }



    if (lock)

        sd->card_status |= CARD_IS_LOCKED;

    else

        sd->card_status &= ~CARD_IS_LOCKED;

}

static void FUNC_0(SDState *VAR_0)

{

    int VAR_1, VAR_2, VAR_3, VAR_4, VAR_5;

    VAR_1 = !!(VAR_0->data[0] & 0x08);

    VAR_2 = VAR_0->data[0] & 0x04;

    VAR_3 = VAR_0->data[0] & 0x02;

    VAR_4 = VAR_0->data[0] & 0x01;



    if (VAR_0->blk_len > 1)

        VAR_5 = VAR_0->data[1];

    else

        VAR_5 = 0;



    if (VAR_1) {

        if (!(VAR_0->card_status & CARD_IS_LOCKED) || VAR_0->blk_len > 1 ||

                        VAR_4 || VAR_3 || VAR_2 || VAR_0->wp_switch ||

                        (VAR_0->csd[14] & 0x20)) {

            VAR_0->card_status |= LOCK_UNLOCK_FAILED;

            return;

        }

        memset(VAR_0->wp_groups, 0, sizeof(int) * (VAR_0->size >>

                        (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)));

        VAR_0->csd[14] &= ~0x10;

        VAR_0->card_status &= ~CARD_IS_LOCKED;

        VAR_0->VAR_5 = 0;

        

        fprintf(stderr, "SD: Card force-erased by CMD42\n");

        return;

    }



    if (VAR_0->blk_len < 2 + VAR_5 ||

                    VAR_5 <= VAR_0->VAR_5 ||

                    VAR_5 > VAR_0->VAR_5 + 16) {

        VAR_0->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    if (VAR_0->VAR_5 && memcmp(VAR_0->pwd, VAR_0->data + 2, VAR_0->VAR_5)) {

        VAR_0->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    VAR_5 -= VAR_0->VAR_5;

    if ((VAR_5 && !VAR_4) ||

                    (VAR_3 && (VAR_4 || VAR_2)) ||

                    (VAR_2 && !VAR_0->VAR_5 && !VAR_4) ||

                    (!VAR_4 && !VAR_3 &&

                     (((VAR_0->card_status & CARD_IS_LOCKED) && VAR_2) ||

                      (!(VAR_0->card_status & CARD_IS_LOCKED) && !VAR_2)))) {

        VAR_0->card_status |= LOCK_UNLOCK_FAILED;

        return;

    }



    if (VAR_4) {

        memcpy(VAR_0->pwd, VAR_0->data + 2 + VAR_0->VAR_5, VAR_5);

        VAR_0->VAR_5 = VAR_5;

    }



    if (VAR_3) {

        VAR_0->VAR_5 = 0;

    }



    if (VAR_2)

        VAR_0->card_status |= CARD_IS_LOCKED;

    else

        VAR_0->card_status &= ~CARD_IS_LOCKED;

}

static void FUNC_0(SDState *VAR_0)
{ int VAR_1, VAR_2, VAR_3, VAR_4, VAR_5; VAR_1 = !!(VAR_0->data[0] & 0x08); VAR_2 = VAR_0->data[0] & 0x04; VAR_3 = VAR_0->data[0] & 0x02; VAR_4 = VAR_0->data[0] & 0x01; if (VAR_0->blk_len > 1)
VAR_5 = VAR_0->data[1]; else
VAR_5 = 0; if (VAR_1) { if (!(VAR_0->card_status & CARD_IS_LOCKED) || VAR_0->blk_len > 1 ||
VAR_4 || VAR_3 || VAR_2 || VAR_0->wp_switch ||
(VAR_0->csd[14] & 0x20)) { VAR_0->card_status |= LOCK_UNLOCK_FAILED; return; } memset(VAR_0->wp_groups, 0, sizeof(int) * (VAR_0->size >>
(HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))); VAR_0->csd[14] &= ~0x10; VAR_0->card_status &= ~CARD_IS_LOCKED; VAR_0->VAR_5 = 0; fprintf(stderr, "SD: Card force-erased by CMD42\n"); return; } if (VAR_0->blk_len < 2 + VAR_5 ||
VAR_5 <= VAR_0->VAR_5 ||
VAR_5 > VAR_0->VAR_5 + 16) { VAR_0->card_status |= LOCK_UNLOCK_FAILED; return; } if (VAR_0->VAR_5 && memcmp(VAR_0->pwd, VAR_0->data + 2, VAR_0->VAR_5)) { VAR_0->card_status |= LOCK_UNLOCK_FAILED; return; } VAR_5 -= VAR_0->VAR_5; if ((VAR_5 && !VAR_4) ||
(VAR_3 && (VAR_4 || VAR_2)) ||
(VAR_2 && !VAR_0->VAR_5 && !VAR_4) ||
(!VAR_4 && !VAR_3 &&
(((VAR_0->card_status & CARD_IS_LOCKED) && VAR_2) ||
(!(VAR_0->card_status & CARD_IS_LOCKED) && !VAR_2)))) { VAR_0->card_status |= LOCK_UNLOCK_FAILED; return; } if (VAR_4) { memcpy(VAR_0->pwd, VAR_0->data + 2 + VAR_0->VAR_5, VAR_5); VAR_0->VAR_5 = VAR_5; } if (VAR_3) { VAR_0->VAR_5 = 0; } if (VAR_2)
VAR_0->card_status |= CARD_IS_LOCKED; else
VAR_0->card_status &= ~CARD_IS_LOCKED; }