2007-09-16 23:08:06 +02:00
|
|
|
/*
|
2007-04-06 18:49:48 +02:00
|
|
|
* SD Memory Card emulation as defined in the "SD Memory Card Physical
|
2018-06-08 14:15:33 +02:00
|
|
|
* layer specification, Version 2.00."
|
2007-04-06 18:49:48 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
|
|
|
|
* Copyright (c) 2007 CodeSourcery
|
2018-06-08 14:15:33 +02:00
|
|
|
* Copyright (c) 2018 Philippe Mathieu-Daudé <f4bug@amsat.org>
|
2007-04-06 18:49:48 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
|
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:30 +01:00
|
|
|
#include "qemu/osdep.h"
|
2018-06-25 14:42:28 +02:00
|
|
|
#include "qemu/units.h"
|
hw/sd/sdcard: Do not allow invalid SD card sizes
QEMU allows to create SD card with unrealistic sizes. This could
work, but some guests (at least Linux) consider sizes that are not
a power of 2 as a firmware bug and fix the card size to the next
power of 2.
While the possibility to use small SD card images has been seen as
a feature, it became a bug with CVE-2020-13253, where the guest is
able to do OOB read/write accesses past the image size end.
In a pair of commits we will fix CVE-2020-13253 as:
Read command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR
occurred and no data transfer is performed.
Write command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR
occurred and no data transfer is performed.
WP_VIOLATION errors are not modified: the error bit is set, we
stay in receive-data state, wait for a stop command. All further
data transfer is ignored. See the check on sd->card_status at the
beginning of sd_read_data() and sd_write_data().
While this is the correct behavior, in case QEMU create smaller SD
cards, guests still try to access past the image size end, and QEMU
considers this is an invalid address, thus "all further data transfer
is ignored". This is wrong and make the guest looping until
eventually timeouts.
Fix by not allowing invalid SD card sizes (suggesting the expected
size as a hint):
$ qemu-system-arm -M orangepi-pc -drive file=rootfs.ext2,if=sd,format=raw
qemu-system-arm: Invalid SD card size: 60 MiB
SD card size has to be a power of 2, e.g. 64 MiB.
You can resize disk images with 'qemu-img resize <imagefile> <new-size>'
(note that this will lose data if you make the image smaller than it currently is).
Cc: qemu-stable@nongnu.org
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Message-Id: <20200713183209.26308-8-f4bug@amsat.org>
2020-07-07 13:02:34 +02:00
|
|
|
#include "qemu/cutils.h"
|
2019-08-12 07:23:42 +02:00
|
|
|
#include "hw/irq.h"
|
2018-02-22 16:12:52 +01:00
|
|
|
#include "hw/registerfields.h"
|
2014-10-07 13:59:18 +02:00
|
|
|
#include "sysemu/block-backend.h"
|
2015-10-08 15:21:01 +02:00
|
|
|
#include "hw/sd/sd.h"
|
2017-12-23 21:29:33 +01:00
|
|
|
#include "hw/sd/sdcard_legacy.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/bitmap.h"
|
2016-02-18 15:16:18 +01:00
|
|
|
#include "hw/qdev-properties.h"
|
2020-12-11 23:05:12 +01:00
|
|
|
#include "hw/qdev-properties-system.h"
|
2016-02-18 15:16:18 +01:00
|
|
|
#include "qemu/error-report.h"
|
2016-02-18 15:16:20 +01:00
|
|
|
#include "qemu/timer.h"
|
2015-12-15 13:16:16 +01:00
|
|
|
#include "qemu/log.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2018-02-22 16:12:52 +01:00
|
|
|
#include "sdmmc-internal.h"
|
2018-02-22 16:12:52 +01:00
|
|
|
#include "trace.h"
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
//#define DEBUG_SD 1
|
|
|
|
|
2020-08-21 16:45:36 +02:00
|
|
|
#define SDSC_MAX_CAPACITY (2 * GiB)
|
|
|
|
|
2020-09-18 19:06:41 +02:00
|
|
|
#define INVALID_ADDRESS UINT32_MAX
|
2020-09-18 19:05:20 +02:00
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
typedef enum {
|
|
|
|
sd_r0 = 0, /* no response */
|
|
|
|
sd_r1, /* normal response command */
|
|
|
|
sd_r2_i, /* CID register */
|
|
|
|
sd_r2_s, /* CSD register */
|
|
|
|
sd_r3, /* OCR register */
|
|
|
|
sd_r6 = 6, /* Published RCA response */
|
2007-12-24 15:41:39 +01:00
|
|
|
sd_r7, /* Operating voltage */
|
2007-04-06 18:49:48 +02:00
|
|
|
sd_r1b = -1,
|
2011-12-18 21:37:55 +01:00
|
|
|
sd_illegal = -2,
|
2009-10-01 23:12:16 +02:00
|
|
|
} sd_rsp_type_t;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2012-10-30 08:45:12 +01:00
|
|
|
enum SDCardModes {
|
|
|
|
sd_inactive,
|
|
|
|
sd_card_identification_mode,
|
|
|
|
sd_data_transfer_mode,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum SDCardStates {
|
|
|
|
sd_inactive_state = -1,
|
|
|
|
sd_idle_state = 0,
|
|
|
|
sd_ready_state,
|
|
|
|
sd_identification_state,
|
|
|
|
sd_standby_state,
|
|
|
|
sd_transfer_state,
|
|
|
|
sd_sendingdata_state,
|
|
|
|
sd_receivingdata_state,
|
|
|
|
sd_programming_state,
|
|
|
|
sd_disconnect_state,
|
|
|
|
};
|
|
|
|
|
2021-06-24 13:00:08 +02:00
|
|
|
typedef sd_rsp_type_t (*sd_cmd_handler)(SDState *sd, SDRequest req);
|
|
|
|
|
2022-05-30 19:09:27 +02:00
|
|
|
typedef struct SDProto {
|
|
|
|
const char *name;
|
2021-06-24 13:00:08 +02:00
|
|
|
sd_cmd_handler cmd[SDMMC_CMD_MAX];
|
|
|
|
sd_cmd_handler acmd[SDMMC_CMD_MAX];
|
2022-05-30 19:09:27 +02:00
|
|
|
} SDProto;
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
struct SDState {
|
2016-02-18 15:16:18 +01:00
|
|
|
DeviceState parent_obj;
|
|
|
|
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
/* If true, created by sd_init() for a non-qdevified caller */
|
|
|
|
/* TODO purge them with fire */
|
|
|
|
bool me_no_qdev_me_kill_mammoth_with_rocks;
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
/* SD Memory Card Registers */
|
2007-04-06 18:49:48 +02:00
|
|
|
uint32_t ocr;
|
|
|
|
uint8_t scr[8];
|
|
|
|
uint8_t cid[16];
|
|
|
|
uint8_t csd[16];
|
|
|
|
uint16_t rca;
|
|
|
|
uint32_t card_status;
|
|
|
|
uint8_t sd_status[64];
|
2018-02-22 16:12:52 +01:00
|
|
|
|
2018-05-09 07:19:41 +02:00
|
|
|
/* Static properties */
|
|
|
|
|
2018-06-08 14:15:33 +02:00
|
|
|
uint8_t spec_version;
|
2018-02-22 16:12:52 +01:00
|
|
|
BlockBackend *blk;
|
|
|
|
|
2018-05-09 07:19:41 +02:00
|
|
|
/* Runtime changeables */
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
uint32_t mode; /* current card mode, one of SDCardModes */
|
|
|
|
int32_t state; /* current card state, one of SDCardStates */
|
2007-12-24 15:41:39 +01:00
|
|
|
uint32_t vhs;
|
2012-08-13 12:04:06 +02:00
|
|
|
bool wp_switch;
|
2021-07-28 19:42:24 +02:00
|
|
|
unsigned long *wp_group_bmap;
|
|
|
|
int32_t wp_group_bits;
|
2009-11-03 15:28:19 +01:00
|
|
|
uint64_t size;
|
2012-10-30 08:45:12 +01:00
|
|
|
uint32_t blk_len;
|
2016-02-18 15:16:19 +01:00
|
|
|
uint32_t multi_blk_cnt;
|
2007-04-06 18:49:48 +02:00
|
|
|
uint32_t erase_start;
|
|
|
|
uint32_t erase_end;
|
|
|
|
uint8_t pwd[16];
|
2012-10-30 08:45:12 +01:00
|
|
|
uint32_t pwd_len;
|
|
|
|
uint8_t function_group[6];
|
|
|
|
uint8_t current_cmd;
|
2011-12-18 21:37:59 +01:00
|
|
|
/* True if we will handle the next command as an ACMD. Note that this does
|
|
|
|
* *not* track the APP_CMD status bit!
|
|
|
|
*/
|
2012-08-13 12:04:06 +02:00
|
|
|
bool expecting_acmd;
|
2012-10-30 08:45:12 +01:00
|
|
|
uint32_t blk_written;
|
2009-11-03 15:28:19 +01:00
|
|
|
uint64_t data_start;
|
2007-04-06 18:49:48 +02:00
|
|
|
uint32_t data_offset;
|
|
|
|
uint8_t data[512];
|
2007-11-17 15:34:44 +01:00
|
|
|
qemu_irq readonly_cb;
|
|
|
|
qemu_irq inserted_cb;
|
2018-02-22 16:12:52 +01:00
|
|
|
QEMUTimer *ocr_power_timer;
|
2012-08-13 12:04:06 +02:00
|
|
|
bool enable;
|
2018-02-08 17:48:10 +01:00
|
|
|
uint8_t dat_lines;
|
|
|
|
bool cmd_line;
|
2007-04-06 18:49:48 +02:00
|
|
|
};
|
|
|
|
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
static void sd_realize(DeviceState *dev, Error **errp);
|
|
|
|
|
2022-05-30 19:09:27 +02:00
|
|
|
static const struct SDProto *sd_proto(SDState *sd)
|
|
|
|
{
|
|
|
|
SDCardClass *sc = SD_CARD_GET_CLASS(sd);
|
|
|
|
|
|
|
|
return sc->proto;
|
|
|
|
}
|
|
|
|
|
2023-07-03 08:00:08 +02:00
|
|
|
static const SDProto sd_proto_spi;
|
|
|
|
|
|
|
|
static bool sd_is_spi(SDState *sd)
|
|
|
|
{
|
|
|
|
return sd_proto(sd) == &sd_proto_spi;
|
|
|
|
}
|
|
|
|
|
2022-05-30 18:56:06 +02:00
|
|
|
static const char *sd_version_str(enum SDPhySpecificationVersion version)
|
|
|
|
{
|
|
|
|
static const char *sdphy_version[] = {
|
|
|
|
[SD_PHY_SPECv1_10_VERS] = "v1.10",
|
|
|
|
[SD_PHY_SPECv2_00_VERS] = "v2.00",
|
|
|
|
[SD_PHY_SPECv3_01_VERS] = "v3.01",
|
|
|
|
};
|
|
|
|
if (version >= ARRAY_SIZE(sdphy_version)) {
|
|
|
|
return "unsupported version";
|
|
|
|
}
|
|
|
|
return sdphy_version[version];
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
static const char *sd_state_name(enum SDCardStates state)
|
|
|
|
{
|
|
|
|
static const char *state_name[] = {
|
|
|
|
[sd_idle_state] = "idle",
|
|
|
|
[sd_ready_state] = "ready",
|
|
|
|
[sd_identification_state] = "identification",
|
|
|
|
[sd_standby_state] = "standby",
|
|
|
|
[sd_transfer_state] = "transfer",
|
|
|
|
[sd_sendingdata_state] = "sendingdata",
|
|
|
|
[sd_receivingdata_state] = "receivingdata",
|
|
|
|
[sd_programming_state] = "programming",
|
|
|
|
[sd_disconnect_state] = "disconnect",
|
|
|
|
};
|
|
|
|
if (state == sd_inactive_state) {
|
|
|
|
return "inactive";
|
|
|
|
}
|
2019-06-19 21:14:46 +02:00
|
|
|
assert(state < ARRAY_SIZE(state_name));
|
2018-02-22 16:12:52 +01:00
|
|
|
return state_name[state];
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
static const char *sd_response_name(sd_rsp_type_t rsp)
|
|
|
|
{
|
|
|
|
static const char *response_name[] = {
|
|
|
|
[sd_r0] = "RESP#0 (no response)",
|
|
|
|
[sd_r1] = "RESP#1 (normal cmd)",
|
|
|
|
[sd_r2_i] = "RESP#2 (CID reg)",
|
|
|
|
[sd_r2_s] = "RESP#2 (CSD reg)",
|
|
|
|
[sd_r3] = "RESP#3 (OCR reg)",
|
|
|
|
[sd_r6] = "RESP#6 (RCA)",
|
|
|
|
[sd_r7] = "RESP#7 (operating voltage)",
|
|
|
|
};
|
|
|
|
if (rsp == sd_illegal) {
|
|
|
|
return "ILLEGAL RESP";
|
|
|
|
}
|
|
|
|
if (rsp == sd_r1b) {
|
|
|
|
rsp = sd_r1;
|
|
|
|
}
|
2019-06-19 21:14:46 +02:00
|
|
|
assert(rsp < ARRAY_SIZE(response_name));
|
2018-02-22 16:12:52 +01:00
|
|
|
return response_name[rsp];
|
|
|
|
}
|
|
|
|
|
2018-02-08 17:48:10 +01:00
|
|
|
static uint8_t sd_get_dat_lines(SDState *sd)
|
|
|
|
{
|
|
|
|
return sd->enable ? sd->dat_lines : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool sd_get_cmd_line(SDState *sd)
|
|
|
|
{
|
|
|
|
return sd->enable ? sd->cmd_line : false;
|
|
|
|
}
|
|
|
|
|
2018-02-08 17:48:09 +01:00
|
|
|
static void sd_set_voltage(SDState *sd, uint16_t millivolts)
|
|
|
|
{
|
2018-02-22 16:12:52 +01:00
|
|
|
trace_sdcard_set_voltage(millivolts);
|
|
|
|
|
2018-02-08 17:48:09 +01:00
|
|
|
switch (millivolts) {
|
|
|
|
case 3001 ... 3600: /* SD_VOLTAGE_3_3V */
|
|
|
|
case 2001 ... 3000: /* SD_VOLTAGE_3_0V */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "SD card voltage not supported: %.3fV",
|
|
|
|
millivolts / 1000.f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-18 21:37:58 +01:00
|
|
|
static void sd_set_mode(SDState *sd)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_inactive_state:
|
|
|
|
sd->mode = sd_inactive;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sd_idle_state:
|
|
|
|
case sd_ready_state:
|
|
|
|
case sd_identification_state:
|
|
|
|
sd->mode = sd_card_identification_mode;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sd_standby_state:
|
|
|
|
case sd_transfer_state:
|
|
|
|
case sd_sendingdata_state:
|
|
|
|
case sd_receivingdata_state:
|
|
|
|
case sd_programming_state:
|
|
|
|
case sd_disconnect_state:
|
|
|
|
sd->mode = sd_data_transfer_mode;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
static const sd_cmd_type_t sd_cmd_type[SDMMC_CMD_MAX] = {
|
2007-04-06 18:49:48 +02:00
|
|
|
sd_bc, sd_none, sd_bcr, sd_bcr, sd_none, sd_none, sd_none, sd_ac,
|
2007-12-24 15:41:39 +01:00
|
|
|
sd_bcr, sd_ac, sd_ac, sd_adtc, sd_ac, sd_ac, sd_none, sd_ac,
|
2018-02-22 16:12:52 +01:00
|
|
|
/* 16 */
|
2007-04-06 18:49:48 +02:00
|
|
|
sd_ac, sd_adtc, sd_adtc, sd_none, sd_none, sd_none, sd_none, sd_none,
|
|
|
|
sd_adtc, sd_adtc, sd_adtc, sd_adtc, sd_ac, sd_ac, sd_adtc, sd_none,
|
2018-02-22 16:12:52 +01:00
|
|
|
/* 32 */
|
2007-04-06 18:49:48 +02:00
|
|
|
sd_ac, sd_ac, sd_none, sd_none, sd_none, sd_none, sd_ac, sd_none,
|
|
|
|
sd_none, sd_none, sd_bc, sd_none, sd_none, sd_none, sd_none, sd_none,
|
2018-02-22 16:12:52 +01:00
|
|
|
/* 48 */
|
2007-04-06 18:49:48 +02:00
|
|
|
sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_ac,
|
|
|
|
sd_adtc, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none, sd_none,
|
|
|
|
};
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
static const int sd_cmd_class[SDMMC_CMD_MAX] = {
|
2007-04-06 18:49:48 +02:00
|
|
|
0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0, 1, 0, 0, 0, 0,
|
|
|
|
2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6,
|
|
|
|
5, 5, 10, 10, 10, 10, 5, 9, 9, 9, 7, 7, 7, 7, 7, 7,
|
|
|
|
7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8,
|
|
|
|
};
|
|
|
|
|
2018-05-02 21:45:37 +02:00
|
|
|
static uint8_t sd_crc7(const void *message, size_t width)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
int i, bit;
|
|
|
|
uint8_t shift_reg = 0x00;
|
2018-05-02 21:45:37 +02:00
|
|
|
const uint8_t *msg = (const uint8_t *)message;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < width; i ++, msg ++)
|
|
|
|
for (bit = 7; bit >= 0; bit --) {
|
|
|
|
shift_reg <<= 1;
|
|
|
|
if ((shift_reg >> 7) ^ ((*msg >> bit) & 1))
|
|
|
|
shift_reg ^= 0x89;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shift_reg;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
#define OCR_POWER_DELAY_NS 500000 /* 0.5ms */
|
|
|
|
|
2018-02-22 16:12:53 +01:00
|
|
|
FIELD(OCR, VDD_VOLTAGE_WINDOW, 0, 24)
|
|
|
|
FIELD(OCR, VDD_VOLTAGE_WIN_LO, 0, 8)
|
|
|
|
FIELD(OCR, DUAL_VOLTAGE_CARD, 7, 1)
|
|
|
|
FIELD(OCR, VDD_VOLTAGE_WIN_HI, 8, 16)
|
|
|
|
FIELD(OCR, ACCEPT_SWITCH_1V8, 24, 1) /* Only UHS-I */
|
|
|
|
FIELD(OCR, UHS_II_CARD, 29, 1) /* Only UHS-II */
|
2018-02-22 16:12:52 +01:00
|
|
|
FIELD(OCR, CARD_CAPACITY, 30, 1) /* 0:SDSC, 1:SDHC/SDXC */
|
|
|
|
FIELD(OCR, CARD_POWER_UP, 31, 1)
|
|
|
|
|
2018-02-22 16:12:53 +01:00
|
|
|
#define ACMD41_ENQUIRY_MASK 0x00ffffff
|
|
|
|
#define ACMD41_R3_MASK (R_OCR_VDD_VOLTAGE_WIN_HI_MASK \
|
|
|
|
| R_OCR_ACCEPT_SWITCH_1V8_MASK \
|
|
|
|
| R_OCR_UHS_II_CARD_MASK \
|
|
|
|
| R_OCR_CARD_CAPACITY_MASK \
|
|
|
|
| R_OCR_CARD_POWER_UP_MASK)
|
|
|
|
|
2016-02-18 15:16:20 +01:00
|
|
|
static void sd_ocr_powerup(void *opaque)
|
|
|
|
{
|
|
|
|
SDState *sd = opaque;
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
trace_sdcard_powerup();
|
2018-02-22 16:12:52 +01:00
|
|
|
assert(!FIELD_EX32(sd->ocr, OCR, CARD_POWER_UP));
|
|
|
|
|
|
|
|
/* card power-up OK */
|
|
|
|
sd->ocr = FIELD_DP32(sd->ocr, OCR, CARD_POWER_UP, 1);
|
2018-02-22 16:12:52 +01:00
|
|
|
|
2020-08-21 16:45:36 +02:00
|
|
|
if (sd->size > SDSC_MAX_CAPACITY) {
|
2018-02-22 16:12:52 +01:00
|
|
|
sd->ocr = FIELD_DP32(sd->ocr, OCR, CARD_CAPACITY, 1);
|
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2021-12-28 13:57:17 +01:00
|
|
|
static void sd_set_ocr(SDState *sd)
|
|
|
|
{
|
|
|
|
/* All voltages OK */
|
|
|
|
sd->ocr = R_OCR_VDD_VOLTAGE_WIN_HI_MASK;
|
|
|
|
|
2023-07-03 08:00:08 +02:00
|
|
|
if (sd_is_spi(sd)) {
|
2021-12-28 13:57:17 +01:00
|
|
|
/*
|
|
|
|
* We don't need to emulate power up sequence in SPI-mode.
|
|
|
|
* Thus, the card's power up status bit should be set to 1 when reset.
|
|
|
|
* The card's capacity status bit should also be set if SD card size
|
|
|
|
* is larger than 2GB for SDHC support.
|
|
|
|
*/
|
|
|
|
sd_ocr_powerup(sd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
static void sd_set_scr(SDState *sd)
|
|
|
|
{
|
2018-06-08 14:15:33 +02:00
|
|
|
sd->scr[0] = 0 << 4; /* SCR structure version 1.0 */
|
|
|
|
if (sd->spec_version == SD_PHY_SPECv1_10_VERS) {
|
|
|
|
sd->scr[0] |= 1; /* Spec Version 1.10 */
|
|
|
|
} else {
|
2018-06-08 14:15:34 +02:00
|
|
|
sd->scr[0] |= 2; /* Spec Version 2.00 or Version 3.0X */
|
2018-06-08 14:15:33 +02:00
|
|
|
}
|
2018-02-22 16:12:53 +01:00
|
|
|
sd->scr[1] = (2 << 4) /* SDSC Card (Security Version 1.01) */
|
|
|
|
| 0b0101; /* 1-bit or 4-bit width bus modes */
|
|
|
|
sd->scr[2] = 0x00; /* Extended Security is not supported. */
|
2018-06-08 14:15:34 +02:00
|
|
|
if (sd->spec_version >= SD_PHY_SPECv3_01_VERS) {
|
|
|
|
sd->scr[2] |= 1 << 7; /* Spec Version 3.0X */
|
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->scr[3] = 0x00;
|
2018-02-22 16:12:53 +01:00
|
|
|
/* reserved for manufacturer usage */
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->scr[4] = 0x00;
|
|
|
|
sd->scr[5] = 0x00;
|
|
|
|
sd->scr[6] = 0x00;
|
|
|
|
sd->scr[7] = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MID 0xaa
|
|
|
|
#define OID "XY"
|
|
|
|
#define PNM "QEMU!"
|
|
|
|
#define PRV 0x01
|
|
|
|
#define MDT_YR 2006
|
|
|
|
#define MDT_MON 2
|
|
|
|
|
|
|
|
static void sd_set_cid(SDState *sd)
|
|
|
|
{
|
|
|
|
sd->cid[0] = MID; /* Fake card manufacturer ID (MID) */
|
|
|
|
sd->cid[1] = OID[0]; /* OEM/Application ID (OID) */
|
|
|
|
sd->cid[2] = OID[1];
|
|
|
|
sd->cid[3] = PNM[0]; /* Fake product name (PNM) */
|
|
|
|
sd->cid[4] = PNM[1];
|
|
|
|
sd->cid[5] = PNM[2];
|
|
|
|
sd->cid[6] = PNM[3];
|
|
|
|
sd->cid[7] = PNM[4];
|
|
|
|
sd->cid[8] = PRV; /* Fake product revision (PRV) */
|
|
|
|
sd->cid[9] = 0xde; /* Fake serial number (PSN) */
|
|
|
|
sd->cid[10] = 0xad;
|
|
|
|
sd->cid[11] = 0xbe;
|
|
|
|
sd->cid[12] = 0xef;
|
|
|
|
sd->cid[13] = 0x00 | /* Manufacture date (MDT) */
|
|
|
|
((MDT_YR - 2000) / 10);
|
|
|
|
sd->cid[14] = ((MDT_YR % 10) << 4) | MDT_MON;
|
|
|
|
sd->cid[15] = (sd_crc7(sd->cid, 15) << 1) | 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HWBLOCK_SHIFT 9 /* 512 bytes */
|
|
|
|
#define SECTOR_SHIFT 5 /* 16 kilobytes */
|
|
|
|
#define WPGROUP_SHIFT 7 /* 2 megs */
|
|
|
|
#define CMULT_SHIFT 9 /* 512 times HWBLOCK_SIZE */
|
|
|
|
#define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))
|
|
|
|
|
|
|
|
static const uint8_t sd_csd_rw_mask[16] = {
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
|
|
|
|
};
|
|
|
|
|
2009-11-03 15:28:19 +01:00
|
|
|
static void sd_set_csd(SDState *sd, uint64_t size)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
2020-10-25 16:23:57 +01:00
|
|
|
int hwblock_shift = HWBLOCK_SHIFT;
|
|
|
|
uint32_t csize;
|
2007-04-06 18:49:48 +02:00
|
|
|
uint32_t sectsize = (1 << (SECTOR_SHIFT + 1)) - 1;
|
|
|
|
uint32_t wpsize = (1 << (WPGROUP_SHIFT + 1)) - 1;
|
|
|
|
|
2020-10-25 16:23:57 +01:00
|
|
|
/* To indicate 2 GiB card, BLOCK_LEN shall be 1024 bytes */
|
|
|
|
if (size == SDSC_MAX_CAPACITY) {
|
|
|
|
hwblock_shift += 1;
|
|
|
|
}
|
|
|
|
csize = (size >> (CMULT_SHIFT + hwblock_shift)) - 1;
|
|
|
|
|
2020-08-21 16:45:36 +02:00
|
|
|
if (size <= SDSC_MAX_CAPACITY) { /* Standard Capacity SD */
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->csd[0] = 0x00; /* CSD structure */
|
|
|
|
sd->csd[1] = 0x26; /* Data read access-time-1 */
|
|
|
|
sd->csd[2] = 0x00; /* Data read access-time-2 */
|
2018-02-22 16:12:53 +01:00
|
|
|
sd->csd[3] = 0x32; /* Max. data transfer rate: 25 MHz */
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->csd[4] = 0x5f; /* Card Command Classes */
|
|
|
|
sd->csd[5] = 0x50 | /* Max. read data block length */
|
2020-10-25 16:23:57 +01:00
|
|
|
hwblock_shift;
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->csd[6] = 0xe0 | /* Partial block for read allowed */
|
|
|
|
((csize >> 10) & 0x03);
|
|
|
|
sd->csd[7] = 0x00 | /* Device size */
|
|
|
|
((csize >> 2) & 0xff);
|
|
|
|
sd->csd[8] = 0x3f | /* Max. read current */
|
|
|
|
((csize << 6) & 0xc0);
|
|
|
|
sd->csd[9] = 0xfc | /* Max. write current */
|
|
|
|
((CMULT_SHIFT - 2) >> 1);
|
|
|
|
sd->csd[10] = 0x40 | /* Erase sector size */
|
|
|
|
(((CMULT_SHIFT - 2) << 7) & 0x80) | (sectsize >> 1);
|
|
|
|
sd->csd[11] = 0x00 | /* Write protect group size */
|
|
|
|
((sectsize << 7) & 0x80) | wpsize;
|
|
|
|
sd->csd[12] = 0x90 | /* Write speed factor */
|
2020-10-25 16:23:57 +01:00
|
|
|
(hwblock_shift >> 2);
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->csd[13] = 0x20 | /* Max. write data block length */
|
2020-10-25 16:23:57 +01:00
|
|
|
((hwblock_shift << 6) & 0xc0);
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->csd[14] = 0x00; /* File format group */
|
|
|
|
} else { /* SDHC */
|
2018-06-25 14:42:28 +02:00
|
|
|
size /= 512 * KiB;
|
2009-11-03 15:28:19 +01:00
|
|
|
size -= 1;
|
|
|
|
sd->csd[0] = 0x40;
|
|
|
|
sd->csd[1] = 0x0e;
|
|
|
|
sd->csd[2] = 0x00;
|
|
|
|
sd->csd[3] = 0x32;
|
|
|
|
sd->csd[4] = 0x5b;
|
|
|
|
sd->csd[5] = 0x59;
|
|
|
|
sd->csd[6] = 0x00;
|
|
|
|
sd->csd[7] = (size >> 16) & 0xff;
|
|
|
|
sd->csd[8] = (size >> 8) & 0xff;
|
|
|
|
sd->csd[9] = (size & 0xff);
|
|
|
|
sd->csd[10] = 0x7f;
|
|
|
|
sd->csd[11] = 0x80;
|
|
|
|
sd->csd[12] = 0x0a;
|
|
|
|
sd->csd[13] = 0x40;
|
|
|
|
sd->csd[14] = 0x00;
|
|
|
|
}
|
2018-02-22 16:12:53 +01:00
|
|
|
sd->csd[15] = (sd_crc7(sd->csd, 15) << 1) | 1;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sd_set_rca(SDState *sd)
|
|
|
|
{
|
|
|
|
sd->rca += 0x4567;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:12:53 +01:00
|
|
|
FIELD(CSR, AKE_SEQ_ERROR, 3, 1)
|
|
|
|
FIELD(CSR, APP_CMD, 5, 1)
|
|
|
|
FIELD(CSR, FX_EVENT, 6, 1)
|
|
|
|
FIELD(CSR, READY_FOR_DATA, 8, 1)
|
|
|
|
FIELD(CSR, CURRENT_STATE, 9, 4)
|
|
|
|
FIELD(CSR, ERASE_RESET, 13, 1)
|
|
|
|
FIELD(CSR, CARD_ECC_DISABLED, 14, 1)
|
|
|
|
FIELD(CSR, WP_ERASE_SKIP, 15, 1)
|
|
|
|
FIELD(CSR, CSD_OVERWRITE, 16, 1)
|
|
|
|
FIELD(CSR, DEFERRED_RESPONSE, 17, 1)
|
|
|
|
FIELD(CSR, ERROR, 19, 1)
|
|
|
|
FIELD(CSR, CC_ERROR, 20, 1)
|
|
|
|
FIELD(CSR, CARD_ECC_FAILED, 21, 1)
|
|
|
|
FIELD(CSR, ILLEGAL_COMMAND, 22, 1)
|
|
|
|
FIELD(CSR, COM_CRC_ERROR, 23, 1)
|
|
|
|
FIELD(CSR, LOCK_UNLOCK_FAILED, 24, 1)
|
|
|
|
FIELD(CSR, CARD_IS_LOCKED, 25, 1)
|
|
|
|
FIELD(CSR, WP_VIOLATION, 26, 1)
|
|
|
|
FIELD(CSR, ERASE_PARAM, 27, 1)
|
|
|
|
FIELD(CSR, ERASE_SEQ_ERROR, 28, 1)
|
|
|
|
FIELD(CSR, BLOCK_LEN_ERROR, 29, 1)
|
|
|
|
FIELD(CSR, ADDRESS_ERROR, 30, 1)
|
|
|
|
FIELD(CSR, OUT_OF_RANGE, 31, 1)
|
|
|
|
|
2011-12-18 21:37:52 +01:00
|
|
|
/* Card status bits, split by clear condition:
|
|
|
|
* A : According to the card current state
|
|
|
|
* B : Always related to the previous command
|
|
|
|
* C : Cleared by read
|
|
|
|
*/
|
2018-02-22 16:12:53 +01:00
|
|
|
#define CARD_STATUS_A (R_CSR_READY_FOR_DATA_MASK \
|
|
|
|
| R_CSR_CARD_ECC_DISABLED_MASK \
|
|
|
|
| R_CSR_CARD_IS_LOCKED_MASK)
|
|
|
|
#define CARD_STATUS_B (R_CSR_CURRENT_STATE_MASK \
|
|
|
|
| R_CSR_ILLEGAL_COMMAND_MASK \
|
|
|
|
| R_CSR_COM_CRC_ERROR_MASK)
|
|
|
|
#define CARD_STATUS_C (R_CSR_AKE_SEQ_ERROR_MASK \
|
|
|
|
| R_CSR_APP_CMD_MASK \
|
|
|
|
| R_CSR_ERASE_RESET_MASK \
|
|
|
|
| R_CSR_WP_ERASE_SKIP_MASK \
|
|
|
|
| R_CSR_CSD_OVERWRITE_MASK \
|
|
|
|
| R_CSR_ERROR_MASK \
|
|
|
|
| R_CSR_CC_ERROR_MASK \
|
|
|
|
| R_CSR_CARD_ECC_FAILED_MASK \
|
|
|
|
| R_CSR_LOCK_UNLOCK_FAILED_MASK \
|
|
|
|
| R_CSR_WP_VIOLATION_MASK \
|
|
|
|
| R_CSR_ERASE_PARAM_MASK \
|
|
|
|
| R_CSR_ERASE_SEQ_ERROR_MASK \
|
|
|
|
| R_CSR_BLOCK_LEN_ERROR_MASK \
|
|
|
|
| R_CSR_ADDRESS_ERROR_MASK \
|
|
|
|
| R_CSR_OUT_OF_RANGE_MASK)
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
static void sd_set_cardstatus(SDState *sd)
|
|
|
|
{
|
|
|
|
sd->card_status = 0x00000100;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sd_set_sdstatus(SDState *sd)
|
|
|
|
{
|
|
|
|
memset(sd->sd_status, 0, 64);
|
|
|
|
}
|
|
|
|
|
2009-05-10 02:44:56 +02:00
|
|
|
static int sd_req_crc_validate(SDRequest *req)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
uint8_t buffer[5];
|
|
|
|
buffer[0] = 0x40 | req->cmd;
|
2018-02-22 16:12:53 +01:00
|
|
|
stl_be_p(&buffer[1], req->arg);
|
2007-04-06 18:49:48 +02:00
|
|
|
return 0;
|
|
|
|
return sd_crc7(buffer, 5) != req->crc; /* TODO */
|
|
|
|
}
|
|
|
|
|
2011-12-18 21:37:58 +01:00
|
|
|
static void sd_response_r1_make(SDState *sd, uint8_t *response)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
2018-02-22 16:12:53 +01:00
|
|
|
stl_be_p(response, sd->card_status);
|
|
|
|
|
2011-12-18 21:37:59 +01:00
|
|
|
/* Clear the "clear on read" status bits */
|
|
|
|
sd->card_status &= ~CARD_STATUS_C;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2007-11-18 02:44:38 +01:00
|
|
|
static void sd_response_r3_make(SDState *sd, uint8_t *response)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
2018-02-22 16:12:53 +01:00
|
|
|
stl_be_p(response, sd->ocr & ACMD41_R3_MASK);
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2007-11-18 02:44:38 +01:00
|
|
|
static void sd_response_r6_make(SDState *sd, uint8_t *response)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
uint16_t status;
|
|
|
|
|
|
|
|
status = ((sd->card_status >> 8) & 0xc000) |
|
|
|
|
((sd->card_status >> 6) & 0x2000) |
|
|
|
|
(sd->card_status & 0x1fff);
|
2011-12-18 21:38:00 +01:00
|
|
|
sd->card_status &= ~(CARD_STATUS_C & 0xc81fff);
|
2018-02-22 16:12:53 +01:00
|
|
|
stw_be_p(response + 0, sd->rca);
|
|
|
|
stw_be_p(response + 2, status);
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2007-12-24 15:41:39 +01:00
|
|
|
static void sd_response_r7_make(SDState *sd, uint8_t *response)
|
|
|
|
{
|
2018-02-22 16:12:53 +01:00
|
|
|
stl_be_p(response, sd->vhs);
|
2007-12-24 15:41:39 +01:00
|
|
|
}
|
|
|
|
|
2012-08-13 12:04:06 +02:00
|
|
|
static inline uint64_t sd_addr_to_wpnum(uint64_t addr)
|
|
|
|
{
|
|
|
|
return addr >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT);
|
|
|
|
}
|
|
|
|
|
2016-02-18 15:16:18 +01:00
|
|
|
static void sd_reset(DeviceState *dev)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
2016-02-18 15:16:18 +01:00
|
|
|
SDState *sd = SD_CARD(dev);
|
2009-11-03 15:28:19 +01:00
|
|
|
uint64_t size;
|
2007-04-06 18:49:48 +02:00
|
|
|
uint64_t sect;
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
trace_sdcard_reset();
|
2015-05-12 12:57:16 +02:00
|
|
|
if (sd->blk) {
|
|
|
|
blk_get_geometry(sd->blk, §);
|
2009-05-03 17:52:16 +02:00
|
|
|
} else {
|
|
|
|
sect = 0;
|
|
|
|
}
|
2011-07-26 01:19:05 +02:00
|
|
|
size = sect << 9;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2012-08-13 12:04:06 +02:00
|
|
|
sect = sd_addr_to_wpnum(size) + 1;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
sd->state = sd_idle_state;
|
|
|
|
sd->rca = 0x0000;
|
2021-12-28 13:57:17 +01:00
|
|
|
sd->size = size;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd_set_ocr(sd);
|
|
|
|
sd_set_scr(sd);
|
|
|
|
sd_set_cid(sd);
|
|
|
|
sd_set_csd(sd, size);
|
|
|
|
sd_set_cardstatus(sd);
|
|
|
|
sd_set_sdstatus(sd);
|
|
|
|
|
2021-07-28 19:42:24 +02:00
|
|
|
g_free(sd->wp_group_bmap);
|
2021-01-18 13:34:47 +01:00
|
|
|
sd->wp_switch = sd->blk ? !blk_is_writable(sd->blk) : false;
|
2021-07-28 19:42:24 +02:00
|
|
|
sd->wp_group_bits = sect;
|
|
|
|
sd->wp_group_bmap = bitmap_new(sd->wp_group_bits);
|
2012-10-30 08:45:12 +01:00
|
|
|
memset(sd->function_group, 0, sizeof(sd->function_group));
|
2020-09-18 19:05:20 +02:00
|
|
|
sd->erase_start = INVALID_ADDRESS;
|
|
|
|
sd->erase_end = INVALID_ADDRESS;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->blk_len = 0x200;
|
|
|
|
sd->pwd_len = 0;
|
2012-08-13 12:04:06 +02:00
|
|
|
sd->expecting_acmd = false;
|
2018-02-08 17:48:10 +01:00
|
|
|
sd->dat_lines = 0xf;
|
|
|
|
sd->cmd_line = true;
|
2016-02-18 15:16:19 +01:00
|
|
|
sd->multi_blk_cnt = 0;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2016-02-18 15:16:18 +01:00
|
|
|
static bool sd_get_inserted(SDState *sd)
|
|
|
|
{
|
2016-03-04 12:30:17 +01:00
|
|
|
return sd->blk && blk_is_inserted(sd->blk);
|
2016-02-18 15:16:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool sd_get_readonly(SDState *sd)
|
|
|
|
{
|
|
|
|
return sd->wp_switch;
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:21:41 +01:00
|
|
|
static void sd_cardchange(void *opaque, bool load, Error **errp)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
SDState *sd = opaque;
|
2016-02-18 15:16:18 +01:00
|
|
|
DeviceState *dev = DEVICE(sd);
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
SDBus *sdbus;
|
2016-02-18 15:16:18 +01:00
|
|
|
bool inserted = sd_get_inserted(sd);
|
|
|
|
bool readonly = sd_get_readonly(sd);
|
2011-01-24 13:32:41 +01:00
|
|
|
|
2016-02-18 15:16:18 +01:00
|
|
|
if (inserted) {
|
2018-02-22 16:12:52 +01:00
|
|
|
trace_sdcard_inserted(readonly);
|
2016-02-18 15:16:18 +01:00
|
|
|
sd_reset(dev);
|
2018-02-22 16:12:52 +01:00
|
|
|
} else {
|
|
|
|
trace_sdcard_ejected();
|
2016-02-18 15:16:18 +01:00
|
|
|
}
|
|
|
|
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
if (sd->me_no_qdev_me_kill_mammoth_with_rocks) {
|
|
|
|
qemu_set_irq(sd->inserted_cb, inserted);
|
2016-02-18 15:16:18 +01:00
|
|
|
if (inserted) {
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
qemu_set_irq(sd->readonly_cb, readonly);
|
2016-02-18 15:16:18 +01:00
|
|
|
}
|
|
|
|
} else {
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
sdbus = SD_BUS(qdev_get_parent_bus(dev));
|
|
|
|
sdbus_set_inserted(sdbus, inserted);
|
2016-02-18 15:16:18 +01:00
|
|
|
if (inserted) {
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
sdbus_set_readonly(sdbus, readonly);
|
2016-02-18 15:16:18 +01:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-03 15:07:41 +02:00
|
|
|
static const BlockDevOps sd_block_ops = {
|
2011-08-03 15:07:42 +02:00
|
|
|
.change_media_cb = sd_cardchange,
|
2011-08-03 15:07:41 +02:00
|
|
|
};
|
|
|
|
|
2016-02-18 15:16:20 +01:00
|
|
|
static bool sd_ocr_vmstate_needed(void *opaque)
|
|
|
|
{
|
|
|
|
SDState *sd = opaque;
|
|
|
|
|
|
|
|
/* Include the OCR state (and timer) if it is not yet powered up */
|
2018-02-22 16:12:52 +01:00
|
|
|
return !FIELD_EX32(sd->ocr, OCR, CARD_POWER_UP);
|
2016-02-18 15:16:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription sd_ocr_vmstate = {
|
|
|
|
.name = "sd-card/ocr-state",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.needed = sd_ocr_vmstate_needed,
|
2023-12-21 04:16:33 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2016-02-18 15:16:20 +01:00
|
|
|
VMSTATE_UINT32(ocr, SDState),
|
|
|
|
VMSTATE_TIMER_PTR(ocr_power_timer, SDState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sd_vmstate_pre_load(void *opaque)
|
|
|
|
{
|
|
|
|
SDState *sd = opaque;
|
|
|
|
|
|
|
|
/* If the OCR state is not included (prior versions, or not
|
|
|
|
* needed), then the OCR must be set as powered up. If the OCR state
|
|
|
|
* is included, this will be replaced by the state restore.
|
|
|
|
*/
|
|
|
|
sd_ocr_powerup(sd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-30 08:45:12 +01:00
|
|
|
static const VMStateDescription sd_vmstate = {
|
|
|
|
.name = "sd-card",
|
2020-09-18 19:06:41 +02:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2016-02-18 15:16:20 +01:00
|
|
|
.pre_load = sd_vmstate_pre_load,
|
2023-12-21 04:16:33 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2012-10-30 08:45:12 +01:00
|
|
|
VMSTATE_UINT32(mode, SDState),
|
|
|
|
VMSTATE_INT32(state, SDState),
|
|
|
|
VMSTATE_UINT8_ARRAY(cid, SDState, 16),
|
|
|
|
VMSTATE_UINT8_ARRAY(csd, SDState, 16),
|
|
|
|
VMSTATE_UINT16(rca, SDState),
|
|
|
|
VMSTATE_UINT32(card_status, SDState),
|
|
|
|
VMSTATE_PARTIAL_BUFFER(sd_status, SDState, 1),
|
|
|
|
VMSTATE_UINT32(vhs, SDState),
|
2021-07-28 19:42:24 +02:00
|
|
|
VMSTATE_BITMAP(wp_group_bmap, SDState, 0, wp_group_bits),
|
2012-10-30 08:45:12 +01:00
|
|
|
VMSTATE_UINT32(blk_len, SDState),
|
2016-02-18 15:16:19 +01:00
|
|
|
VMSTATE_UINT32(multi_blk_cnt, SDState),
|
2012-10-30 08:45:12 +01:00
|
|
|
VMSTATE_UINT32(erase_start, SDState),
|
|
|
|
VMSTATE_UINT32(erase_end, SDState),
|
|
|
|
VMSTATE_UINT8_ARRAY(pwd, SDState, 16),
|
|
|
|
VMSTATE_UINT32(pwd_len, SDState),
|
|
|
|
VMSTATE_UINT8_ARRAY(function_group, SDState, 6),
|
|
|
|
VMSTATE_UINT8(current_cmd, SDState),
|
|
|
|
VMSTATE_BOOL(expecting_acmd, SDState),
|
|
|
|
VMSTATE_UINT32(blk_written, SDState),
|
|
|
|
VMSTATE_UINT64(data_start, SDState),
|
|
|
|
VMSTATE_UINT32(data_offset, SDState),
|
|
|
|
VMSTATE_UINT8_ARRAY(data, SDState, 512),
|
2016-05-06 18:26:39 +02:00
|
|
|
VMSTATE_UNUSED_V(1, 512),
|
2012-10-30 08:45:12 +01:00
|
|
|
VMSTATE_BOOL(enable, SDState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
2016-02-18 15:16:20 +01:00
|
|
|
},
|
2023-12-21 04:16:33 +01:00
|
|
|
.subsections = (const VMStateDescription * const []) {
|
2016-02-18 15:16:20 +01:00
|
|
|
&sd_ocr_vmstate,
|
|
|
|
NULL
|
|
|
|
},
|
2012-10-30 08:45:12 +01:00
|
|
|
};
|
|
|
|
|
2016-02-18 15:16:18 +01:00
|
|
|
/* Legacy initialization function for use by non-qdevified callers */
|
2014-10-07 13:59:18 +02:00
|
|
|
SDState *sd_init(BlockBackend *blk, bool is_spi)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
2016-03-16 18:06:02 +01:00
|
|
|
Object *obj;
|
2016-02-18 15:16:18 +01:00
|
|
|
DeviceState *dev;
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
SDState *sd;
|
2016-02-18 15:16:18 +01:00
|
|
|
Error *err = NULL;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2023-07-03 08:00:08 +02:00
|
|
|
obj = object_new(is_spi ? TYPE_SD_CARD_SPI : TYPE_SD_CARD);
|
2016-03-16 18:06:02 +01:00
|
|
|
dev = DEVICE(obj);
|
2020-07-07 18:06:00 +02:00
|
|
|
if (!qdev_prop_set_drive_err(dev, "drive", blk, &err)) {
|
2020-05-05 12:19:03 +02:00
|
|
|
error_reportf_err(err, "sd_init failed: ");
|
2013-09-13 15:51:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Realizing the device properly would put it into the QOM
|
|
|
|
* composition tree even though it is not plugged into an
|
|
|
|
* appropriate bus. That's a no-no. Hide the device from
|
|
|
|
* QOM/qdev, and call its qdev realize callback directly.
|
|
|
|
*/
|
|
|
|
object_ref(obj);
|
|
|
|
object_unparent(obj);
|
|
|
|
sd_realize(dev, &err);
|
2016-02-18 15:16:18 +01:00
|
|
|
if (err) {
|
2020-05-05 12:19:03 +02:00
|
|
|
error_reportf_err(err, "sd_init failed: ");
|
2016-02-18 15:16:18 +01:00
|
|
|
return NULL;
|
2009-05-03 17:52:16 +02:00
|
|
|
}
|
2016-02-18 15:16:18 +01:00
|
|
|
|
sd: Hide the qdev-but-not-quite thing created by sd_init()
Commit 260bc9d8aa "hw/sd/sd.c: QOMify" QOMified only the device
itself, not its users. It kept sd_init() around for non-QOMified
users.
More than four years later, three such users remain: omap1 (machines
cheetah, sx1, sx1-v1) and omap2 (machines n800, n810) are not
QOMified, and pl181 (machines integratorcp, realview-eb,
realview-eb-mpcore, realview-pb-a8 realview-pbx-a9, versatileab,
versatilepb, vexpress-a15, vexpress-a9) is not QOMified properly.
The issue I presently have with this: an "sd-card" device should plug
into an "sd-bus" (its DeviceClass member bus_type says so), but
sd_init() leaves it unplugged. This is normally a bug (I just fixed
some instances), and I'd like to assert proper pluggedness to prevent
regressions. However, the qdev-but-not-quite thing returned by
sd_init() would fail the assertion. Meh.
Make sd_init() hide it from QOM/qdev. Visible in "info qom-tree",
here's the change for cheetah:
/machine (cheetah-machine)
[...]
/unattached (container)
[...]
/device[5] (serial-mm)
/serial (serial)
/serial[0] (qemu:memory-region)
- /device[6] (sd-card)
- /device[7] (omap-gpio)
+ /device[6] (omap-gpio)
[rest of device[*] renumbered...]
Cc: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20200609122339.937862-24-armbru@redhat.com>
2020-06-09 14:23:38 +02:00
|
|
|
sd = SD_CARD(dev);
|
|
|
|
sd->me_no_qdev_me_kill_mammoth_with_rocks = true;
|
|
|
|
return sd;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2007-11-17 15:34:44 +01:00
|
|
|
void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
2007-11-17 15:34:44 +01:00
|
|
|
sd->readonly_cb = readonly;
|
|
|
|
sd->inserted_cb = insert;
|
2021-01-18 13:34:47 +01:00
|
|
|
qemu_set_irq(readonly, sd->blk ? !blk_is_writable(sd->blk) : 0);
|
2014-10-07 13:59:18 +02:00
|
|
|
qemu_set_irq(insert, sd->blk ? blk_is_inserted(sd->blk) : 0);
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 16:02:21 +01:00
|
|
|
static void sd_blk_read(SDState *sd, uint64_t addr, uint32_t len)
|
|
|
|
{
|
|
|
|
trace_sdcard_read_block(addr, len);
|
block: Change blk_{pread,pwrite}() param order
Swap 'buf' and 'bytes' around for consistency with
blk_co_{pread,pwrite}(), and in preparation to implement these functions
using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pread(blk, offset, buf, bytes, flags)
+ blk_pread(blk, offset, bytes, buf, flags)
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pwrite(blk, offset, buf, bytes, flags)
+ blk_pwrite(blk, offset, bytes, buf, flags)
It had no effect on hw/block/nand.c, presumably due to the #if, so that
file was updated manually.
Overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20220705161527.1054072-4-afaria@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-07-05 18:15:11 +02:00
|
|
|
if (!sd->blk || blk_pread(sd->blk, addr, len, sd->data, 0) < 0) {
|
2021-02-16 16:02:21 +01:00
|
|
|
fprintf(stderr, "sd_blk_read: read error on host side\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sd_blk_write(SDState *sd, uint64_t addr, uint32_t len)
|
|
|
|
{
|
|
|
|
trace_sdcard_write_block(addr, len);
|
block: Change blk_{pread,pwrite}() param order
Swap 'buf' and 'bytes' around for consistency with
blk_co_{pread,pwrite}(), and in preparation to implement these functions
using generated_co_wrapper.
Callers were updated using this Coccinelle script:
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pread(blk, offset, buf, bytes, flags)
+ blk_pread(blk, offset, bytes, buf, flags)
@@ expression blk, offset, buf, bytes, flags; @@
- blk_pwrite(blk, offset, buf, bytes, flags)
+ blk_pwrite(blk, offset, bytes, buf, flags)
It had no effect on hw/block/nand.c, presumably due to the #if, so that
file was updated manually.
Overly-long lines were then fixed by hand.
Signed-off-by: Alberto Faria <afaria@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20220705161527.1054072-4-afaria@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2022-07-05 18:15:11 +02:00
|
|
|
if (!sd->blk || blk_pwrite(sd->blk, addr, len, sd->data, 0) < 0) {
|
2021-02-16 16:02:21 +01:00
|
|
|
fprintf(stderr, "sd_blk_write: write error on host side\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BLK_READ_BLOCK(a, len) sd_blk_read(sd, a, len)
|
|
|
|
#define BLK_WRITE_BLOCK(a, len) sd_blk_write(sd, a, len)
|
|
|
|
#define APP_READ_BLOCK(a, len) memset(sd->data, 0xec, len)
|
|
|
|
#define APP_WRITE_BLOCK(a, len)
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
static void sd_erase(SDState *sd)
|
|
|
|
{
|
2012-10-30 08:45:12 +01:00
|
|
|
uint64_t erase_start = sd->erase_start;
|
|
|
|
uint64_t erase_end = sd->erase_end;
|
2021-02-16 16:02:22 +01:00
|
|
|
bool sdsc = true;
|
2021-02-20 09:58:13 +01:00
|
|
|
uint64_t wpnum;
|
|
|
|
uint64_t erase_addr;
|
|
|
|
int erase_len = 1 << HWBLOCK_SHIFT;
|
2012-10-30 08:45:12 +01:00
|
|
|
|
2020-09-13 13:18:19 +02:00
|
|
|
trace_sdcard_erase(sd->erase_start, sd->erase_end);
|
2020-09-18 19:05:20 +02:00
|
|
|
if (sd->erase_start == INVALID_ADDRESS
|
|
|
|
|| sd->erase_end == INVALID_ADDRESS) {
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->card_status |= ERASE_SEQ_ERROR;
|
2020-09-13 13:18:31 +02:00
|
|
|
sd->erase_start = INVALID_ADDRESS;
|
|
|
|
sd->erase_end = INVALID_ADDRESS;
|
2007-04-06 18:49:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
if (FIELD_EX32(sd->ocr, OCR, CARD_CAPACITY)) {
|
2012-10-30 08:45:12 +01:00
|
|
|
/* High capacity memory card: erase units are 512 byte blocks */
|
|
|
|
erase_start *= 512;
|
|
|
|
erase_end *= 512;
|
2021-02-16 16:02:22 +01:00
|
|
|
sdsc = false;
|
2012-10-30 08:45:12 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 16:02:18 +01:00
|
|
|
if (erase_start > sd->size || erase_end > sd->size) {
|
2020-09-13 13:18:52 +02:00
|
|
|
sd->card_status |= OUT_OF_RANGE;
|
|
|
|
sd->erase_start = INVALID_ADDRESS;
|
|
|
|
sd->erase_end = INVALID_ADDRESS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-18 19:05:20 +02:00
|
|
|
sd->erase_start = INVALID_ADDRESS;
|
|
|
|
sd->erase_end = INVALID_ADDRESS;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->csd[14] |= 0x40;
|
|
|
|
|
2021-02-20 09:58:13 +01:00
|
|
|
memset(sd->data, 0xff, erase_len);
|
|
|
|
for (erase_addr = erase_start; erase_addr <= erase_end;
|
|
|
|
erase_addr += erase_len) {
|
|
|
|
if (sdsc) {
|
|
|
|
/* Only SDSC cards support write protect groups */
|
|
|
|
wpnum = sd_addr_to_wpnum(erase_addr);
|
2021-07-28 19:42:24 +02:00
|
|
|
assert(wpnum < sd->wp_group_bits);
|
|
|
|
if (test_bit(wpnum, sd->wp_group_bmap)) {
|
2021-02-16 16:02:22 +01:00
|
|
|
sd->card_status |= WP_ERASE_SKIP;
|
2021-02-20 09:58:13 +01:00
|
|
|
continue;
|
2021-02-16 16:02:22 +01:00
|
|
|
}
|
2012-08-13 12:04:06 +02:00
|
|
|
}
|
2021-02-20 09:58:13 +01:00
|
|
|
BLK_WRITE_BLOCK(erase_addr, erase_len);
|
2012-08-13 12:04:06 +02:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 15:28:19 +01:00
|
|
|
static uint32_t sd_wpbits(SDState *sd, uint64_t addr)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
uint32_t i, wpnum;
|
|
|
|
uint32_t ret = 0;
|
|
|
|
|
2012-08-13 12:04:06 +02:00
|
|
|
wpnum = sd_addr_to_wpnum(addr);
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2012-08-13 12:04:06 +02:00
|
|
|
for (i = 0; i < 32; i++, wpnum++, addr += WPGROUP_SIZE) {
|
2021-07-28 19:38:05 +02:00
|
|
|
if (addr >= sd->size) {
|
|
|
|
/*
|
|
|
|
* If the addresses of the last groups are outside the valid range,
|
|
|
|
* then the corresponding write protection bits shall be set to 0.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-28 19:42:24 +02:00
|
|
|
assert(wpnum < sd->wp_group_bits);
|
|
|
|
if (test_bit(wpnum, sd->wp_group_bmap)) {
|
2007-04-06 18:49:48 +02:00
|
|
|
ret |= (1 << i);
|
2012-08-13 12:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sd_function_switch(SDState *sd, uint32_t arg)
|
|
|
|
{
|
2018-02-22 16:12:53 +01:00
|
|
|
int i, mode, new_func;
|
2007-04-06 18:49:48 +02:00
|
|
|
mode = !!(arg & 0x80000000);
|
|
|
|
|
|
|
|
sd->data[0] = 0x00; /* Maximum current consumption */
|
|
|
|
sd->data[1] = 0x01;
|
|
|
|
sd->data[2] = 0x80; /* Supported group 6 functions */
|
|
|
|
sd->data[3] = 0x01;
|
|
|
|
sd->data[4] = 0x80; /* Supported group 5 functions */
|
|
|
|
sd->data[5] = 0x01;
|
|
|
|
sd->data[6] = 0x80; /* Supported group 4 functions */
|
|
|
|
sd->data[7] = 0x01;
|
|
|
|
sd->data[8] = 0x80; /* Supported group 3 functions */
|
|
|
|
sd->data[9] = 0x01;
|
|
|
|
sd->data[10] = 0x80; /* Supported group 2 functions */
|
|
|
|
sd->data[11] = 0x43;
|
|
|
|
sd->data[12] = 0x80; /* Supported group 1 functions */
|
|
|
|
sd->data[13] = 0x03;
|
2020-08-21 16:45:35 +02:00
|
|
|
|
2020-10-24 03:49:54 +02:00
|
|
|
memset(&sd->data[14], 0, 3);
|
2007-04-06 18:49:48 +02:00
|
|
|
for (i = 0; i < 6; i ++) {
|
|
|
|
new_func = (arg >> (i * 4)) & 0x0f;
|
|
|
|
if (mode && new_func != 0x0f)
|
|
|
|
sd->function_group[i] = new_func;
|
2020-08-21 16:45:35 +02:00
|
|
|
sd->data[16 - (i >> 1)] |= new_func << ((i % 2) * 4);
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
memset(&sd->data[17], 0, 47);
|
|
|
|
}
|
|
|
|
|
2012-08-13 12:04:07 +02:00
|
|
|
static inline bool sd_wp_addr(SDState *sd, uint64_t addr)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
2021-07-28 19:42:24 +02:00
|
|
|
return test_bit(sd_addr_to_wpnum(addr), sd->wp_group_bmap);
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
if (lock) {
|
|
|
|
trace_sdcard_lock();
|
|
|
|
} else {
|
|
|
|
trace_sdcard_unlock();
|
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-07-28 19:42:24 +02:00
|
|
|
bitmap_zero(sd->wp_group_bmap, sd->wp_group_bits);
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->csd[14] &= ~0x10;
|
|
|
|
sd->card_status &= ~CARD_IS_LOCKED;
|
|
|
|
sd->pwd_len = 0;
|
|
|
|
/* Erasing the entire card here! */
|
2008-04-14 23:05:22 +02:00
|
|
|
fprintf(stderr, "SD: Card force-erased by CMD42\n");
|
2007-04-06 18:49:48 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:32:06 +02:00
|
|
|
static bool address_in_range(SDState *sd, const char *desc,
|
|
|
|
uint64_t addr, uint32_t length)
|
|
|
|
{
|
|
|
|
if (addr + length > sd->size) {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"%s offset %"PRIu64" > card %"PRIu64" [%%%u]\n",
|
|
|
|
desc, addr, sd->size, length);
|
|
|
|
sd->card_status |= ADDRESS_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:24:58 +02:00
|
|
|
static sd_rsp_type_t sd_invalid_state_for_cmd(SDState *sd, SDRequest req)
|
|
|
|
{
|
2022-05-30 19:09:27 +02:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "%s: CMD%i in a wrong state: %s (spec %s)\n",
|
|
|
|
sd_proto(sd)->name, req.cmd, sd_state_name(sd->state),
|
2022-05-30 18:56:06 +02:00
|
|
|
sd_version_str(sd->spec_version));
|
2021-06-07 17:24:58 +02:00
|
|
|
|
|
|
|
return sd_illegal;
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:22:40 +02:00
|
|
|
static sd_rsp_type_t sd_cmd_illegal(SDState *sd, SDRequest req)
|
|
|
|
{
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "%s: Unknown CMD%i for spec %s\n",
|
|
|
|
sd_proto(sd)->name, req.cmd,
|
|
|
|
sd_version_str(sd->spec_version));
|
|
|
|
|
|
|
|
return sd_illegal;
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:29:49 +02:00
|
|
|
/* Commands that are recognised but not yet implemented. */
|
|
|
|
static sd_rsp_type_t sd_cmd_unimplemented(SDState *sd, SDRequest req)
|
|
|
|
{
|
|
|
|
qemu_log_mask(LOG_UNIMP, "%s: CMD%i not implemented\n",
|
|
|
|
sd_proto(sd)->name, req.cmd);
|
|
|
|
|
|
|
|
return sd_illegal;
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:48:55 +02:00
|
|
|
static sd_rsp_type_t sd_cmd_GO_IDLE_STATE(SDState *sd, SDRequest req)
|
|
|
|
{
|
|
|
|
if (sd->state != sd_inactive_state) {
|
|
|
|
sd->state = sd_idle_state;
|
|
|
|
sd_reset(DEVICE(sd));
|
|
|
|
}
|
|
|
|
|
2023-07-03 08:00:08 +02:00
|
|
|
return sd_is_spi(sd) ? sd_r1 : sd_r0;
|
2021-06-24 15:48:55 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 15:29:27 +02:00
|
|
|
static sd_rsp_type_t sd_cmd_SEND_OP_CMD(SDState *sd, SDRequest req)
|
|
|
|
{
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
|
|
|
|
return sd_r1;
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:36:03 +02:00
|
|
|
static sd_rsp_type_t sd_cmd_ALL_SEND_CID(SDState *sd, SDRequest req)
|
|
|
|
{
|
|
|
|
if (sd->state != sd_ready_state) {
|
|
|
|
return sd_invalid_state_for_cmd(sd, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
sd->state = sd_identification_state;
|
|
|
|
|
|
|
|
return sd_r2_i;
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:46:29 +02:00
|
|
|
static sd_rsp_type_t sd_cmd_SEND_RELATIVE_ADDR(SDState *sd, SDRequest req)
|
|
|
|
{
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_identification_state:
|
|
|
|
case sd_standby_state:
|
|
|
|
sd->state = sd_standby_state;
|
|
|
|
sd_set_rca(sd);
|
|
|
|
return sd_r6;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return sd_invalid_state_for_cmd(sd, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 09:21:21 +02:00
|
|
|
static sd_rsp_type_t sd_cmd_SEND_TUNING_BLOCK(SDState *sd, SDRequest req)
|
|
|
|
{
|
|
|
|
if (sd->spec_version < SD_PHY_SPECv3_01_VERS) {
|
|
|
|
return sd_cmd_illegal(sd, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sd->state != sd_transfer_state) {
|
|
|
|
return sd_invalid_state_for_cmd(sd, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
sd->state = sd_sendingdata_state;
|
|
|
|
sd->data_offset = 0;
|
|
|
|
|
|
|
|
return sd_r1;
|
|
|
|
}
|
|
|
|
|
2022-05-30 19:20:25 +02:00
|
|
|
static sd_rsp_type_t sd_cmd_SET_BLOCK_COUNT(SDState *sd, SDRequest req)
|
|
|
|
{
|
|
|
|
if (sd->spec_version < SD_PHY_SPECv3_01_VERS) {
|
|
|
|
return sd_cmd_illegal(sd, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sd->state != sd_transfer_state) {
|
|
|
|
return sd_invalid_state_for_cmd(sd, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
sd->multi_blk_cnt = req.arg;
|
|
|
|
|
|
|
|
return sd_r1;
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:09:44 +01:00
|
|
|
static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
uint32_t rca = 0x0000;
|
2009-11-03 15:28:19 +01:00
|
|
|
uint64_t addr = (sd->ocr & (1 << 30)) ? (uint64_t) req.arg << 9 : req.arg;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2018-03-09 18:09:44 +01:00
|
|
|
/* CMD55 precedes an ACMD, so we are not interested in tracing it.
|
|
|
|
* However there is no ACMD55, so we want to trace this particular case.
|
|
|
|
*/
|
|
|
|
if (req.cmd != 55 || sd->expecting_acmd) {
|
2022-05-30 19:09:27 +02:00
|
|
|
trace_sdcard_normal_command(sd_proto(sd)->name,
|
2018-03-09 18:09:44 +01:00
|
|
|
sd_cmd_name(req.cmd), req.cmd,
|
2018-03-09 18:09:44 +01:00
|
|
|
req.arg, sd_state_name(sd->state));
|
2018-03-09 18:09:44 +01:00
|
|
|
}
|
2018-02-22 16:12:52 +01:00
|
|
|
|
2011-12-18 21:37:59 +01:00
|
|
|
/* Not interpreting this as an app command */
|
|
|
|
sd->card_status &= ~APP_CMD;
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
if (sd_cmd_type[req.cmd] == sd_ac
|
|
|
|
|| sd_cmd_type[req.cmd] == sd_adtc) {
|
2007-04-06 18:49:48 +02:00
|
|
|
rca = req.arg >> 16;
|
2016-02-11 12:17:32 +01:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2016-02-18 15:16:19 +01:00
|
|
|
/* CMD23 (set block count) must be immediately followed by CMD18 or CMD25
|
|
|
|
* if not, its effects are cancelled */
|
|
|
|
if (sd->multi_blk_cnt != 0 && !(req.cmd == 18 || req.cmd == 25)) {
|
|
|
|
sd->multi_blk_cnt = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-03 19:59:16 +02:00
|
|
|
if (sd_cmd_class[req.cmd] == 6 && FIELD_EX32(sd->ocr, OCR, CARD_CAPACITY)) {
|
|
|
|
/* Only Standard Capacity cards support class 6 commands */
|
|
|
|
return sd_illegal;
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:00:08 +02:00
|
|
|
if (sd_proto(sd)->cmd[req.cmd]) {
|
|
|
|
return sd_proto(sd)->cmd[req.cmd](sd, req);
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
switch (req.cmd) {
|
|
|
|
/* Basic commands (Class 0 and Class 1) */
|
|
|
|
case 4: /* CMD4: SEND_DSR */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_standby_state:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6: /* CMD6: SWITCH_FUNCTION */
|
|
|
|
switch (sd->mode) {
|
|
|
|
case sd_data_transfer_mode:
|
|
|
|
sd_function_switch(sd, req.arg);
|
|
|
|
sd->state = sd_sendingdata_state;
|
|
|
|
sd->data_start = 0;
|
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7: /* CMD7: SELECT/DESELECT_CARD */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_standby_state:
|
|
|
|
if (sd->rca != rca)
|
|
|
|
return sd_r0;
|
|
|
|
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
case sd_transfer_state:
|
|
|
|
case sd_sendingdata_state:
|
|
|
|
if (sd->rca == rca)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sd->state = sd_standby_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
case sd_disconnect_state:
|
|
|
|
if (sd->rca != rca)
|
|
|
|
return sd_r0;
|
|
|
|
|
|
|
|
sd->state = sd_programming_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
case sd_programming_state:
|
|
|
|
if (sd->rca == rca)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sd->state = sd_disconnect_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-12-24 15:41:39 +01:00
|
|
|
case 8: /* CMD8: SEND_IF_COND */
|
2018-06-08 14:15:34 +02:00
|
|
|
if (sd->spec_version < SD_PHY_SPECv2_00_VERS) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-22 16:12:54 +01:00
|
|
|
if (sd->state != sd_idle_state) {
|
2007-12-24 15:41:39 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-02-22 16:12:54 +01:00
|
|
|
sd->vhs = 0;
|
|
|
|
|
|
|
|
/* No response if not exactly one VHS bit is set. */
|
|
|
|
if (!(req.arg >> 8) || (req.arg >> (ctz32(req.arg & ~0xff) + 1))) {
|
2023-07-03 08:00:08 +02:00
|
|
|
return sd_is_spi(sd) ? sd_r7 : sd_r0;
|
2018-02-22 16:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Accept. */
|
|
|
|
sd->vhs = req.arg;
|
|
|
|
return sd_r7;
|
2007-12-24 15:41:39 +01:00
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
case 9: /* CMD9: SEND_CSD */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_standby_state:
|
|
|
|
if (sd->rca != rca)
|
|
|
|
return sd_r0;
|
|
|
|
|
|
|
|
return sd_r2_s;
|
|
|
|
|
2007-11-25 00:35:08 +01:00
|
|
|
case sd_transfer_state:
|
2023-07-03 08:00:08 +02:00
|
|
|
if (!sd_is_spi(sd)) {
|
2007-11-25 00:35:08 +01:00
|
|
|
break;
|
2023-07-03 08:00:08 +02:00
|
|
|
}
|
2007-11-25 00:35:08 +01:00
|
|
|
sd->state = sd_sendingdata_state;
|
|
|
|
memcpy(sd->data, sd->csd, 16);
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->data_start = addr;
|
2007-11-25 00:35:08 +01:00
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 10: /* CMD10: SEND_CID */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_standby_state:
|
|
|
|
if (sd->rca != rca)
|
|
|
|
return sd_r0;
|
|
|
|
|
|
|
|
return sd_r2_i;
|
|
|
|
|
2007-11-25 00:35:08 +01:00
|
|
|
case sd_transfer_state:
|
2023-07-03 08:00:08 +02:00
|
|
|
if (!sd_is_spi(sd)) {
|
2007-11-25 00:35:08 +01:00
|
|
|
break;
|
2023-07-03 08:00:08 +02:00
|
|
|
}
|
2007-11-25 00:35:08 +01:00
|
|
|
sd->state = sd_sendingdata_state;
|
|
|
|
memcpy(sd->data, sd->cid, 16);
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->data_start = addr;
|
2007-11-25 00:35:08 +01:00
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 12: /* CMD12: STOP_TRANSMISSION */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_sendingdata_state:
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
case sd_receivingdata_state:
|
|
|
|
sd->state = sd_programming_state;
|
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 13: /* CMD13: SEND_STATUS */
|
|
|
|
switch (sd->mode) {
|
|
|
|
case sd_data_transfer_mode:
|
2023-07-03 08:00:08 +02:00
|
|
|
if (!sd_is_spi(sd) && sd->rca != rca) {
|
2007-04-06 18:49:48 +02:00
|
|
|
return sd_r0;
|
2021-02-16 16:02:25 +01:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 15: /* CMD15: GO_INACTIVE_STATE */
|
|
|
|
switch (sd->mode) {
|
|
|
|
case sd_data_transfer_mode:
|
|
|
|
if (sd->rca != rca)
|
|
|
|
return sd_r0;
|
|
|
|
|
|
|
|
sd->state = sd_inactive_state;
|
|
|
|
return sd_r0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-08-23 08:53:30 +02:00
|
|
|
/* Block read commands (Class 2) */
|
2007-04-06 18:49:48 +02:00
|
|
|
case 16: /* CMD16: SET_BLOCKLEN */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
2018-02-22 16:12:52 +01:00
|
|
|
if (req.arg > (1 << HWBLOCK_SHIFT)) {
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->card_status |= BLOCK_LEN_ERROR;
|
2018-02-22 16:12:52 +01:00
|
|
|
} else {
|
|
|
|
trace_sdcard_set_blocklen(req.arg);
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->blk_len = req.arg;
|
2018-02-22 16:12:52 +01:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 17: /* CMD17: READ_SINGLE_BLOCK */
|
|
|
|
case 18: /* CMD18: READ_MULTIPLE_BLOCK */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
|
2021-06-07 15:32:06 +02:00
|
|
|
if (!address_in_range(sd, "READ_BLOCK", addr, sd->blk_len)) {
|
2020-06-04 19:22:29 +02:00
|
|
|
return sd_r1;
|
2020-07-13 09:27:35 +02:00
|
|
|
}
|
2020-06-04 19:22:29 +02:00
|
|
|
|
|
|
|
sd->state = sd_sendingdata_state;
|
|
|
|
sd->data_start = addr;
|
|
|
|
sd->data_offset = 0;
|
2007-04-06 18:49:48 +02:00
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Block write commands (Class 4) */
|
|
|
|
case 24: /* CMD24: WRITE_SINGLE_BLOCK */
|
|
|
|
case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
2020-06-04 19:22:29 +02:00
|
|
|
|
2021-06-07 15:32:06 +02:00
|
|
|
if (!address_in_range(sd, "WRITE_BLOCK", addr, sd->blk_len)) {
|
2020-06-04 19:22:29 +02:00
|
|
|
return sd_r1;
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->state = sd_receivingdata_state;
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->data_start = addr;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->data_offset = 0;
|
|
|
|
sd->blk_written = 0;
|
|
|
|
|
2021-02-16 16:02:24 +01:00
|
|
|
if (sd->size <= SDSC_MAX_CAPACITY) {
|
|
|
|
if (sd_wp_addr(sd, sd->data_start)) {
|
|
|
|
sd->card_status |= WP_VIOLATION;
|
|
|
|
}
|
2020-07-13 09:27:35 +02:00
|
|
|
}
|
|
|
|
if (sd->csd[14] & 0x30) {
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->card_status |= WP_VIOLATION;
|
2020-07-13 09:27:35 +02:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 26: /* CMD26: PROGRAM_CID */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
sd->state = sd_receivingdata_state;
|
|
|
|
sd->data_start = 0;
|
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 27: /* CMD27: PROGRAM_CSD */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
sd->state = sd_receivingdata_state;
|
|
|
|
sd->data_start = 0;
|
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Write protection (Class 6) */
|
|
|
|
case 28: /* CMD28: SET_WRITE_PROT */
|
2021-02-16 16:02:19 +01:00
|
|
|
if (sd->size > SDSC_MAX_CAPACITY) {
|
|
|
|
return sd_illegal;
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
2021-06-07 15:32:06 +02:00
|
|
|
if (!address_in_range(sd, "SET_WRITE_PROT", addr, 1)) {
|
2007-04-06 18:49:48 +02:00
|
|
|
return sd_r1b;
|
|
|
|
}
|
|
|
|
|
|
|
|
sd->state = sd_programming_state;
|
2021-07-28 19:42:24 +02:00
|
|
|
set_bit(sd_addr_to_wpnum(addr), sd->wp_group_bmap);
|
2007-04-06 18:49:48 +02:00
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 29: /* CMD29: CLR_WRITE_PROT */
|
2021-02-16 16:02:19 +01:00
|
|
|
if (sd->size > SDSC_MAX_CAPACITY) {
|
|
|
|
return sd_illegal;
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
2021-06-07 15:32:06 +02:00
|
|
|
if (!address_in_range(sd, "CLR_WRITE_PROT", addr, 1)) {
|
2007-04-06 18:49:48 +02:00
|
|
|
return sd_r1b;
|
|
|
|
}
|
|
|
|
|
|
|
|
sd->state = sd_programming_state;
|
2021-07-28 19:42:24 +02:00
|
|
|
clear_bit(sd_addr_to_wpnum(addr), sd->wp_group_bmap);
|
2007-04-06 18:49:48 +02:00
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 30: /* CMD30: SEND_WRITE_PROT */
|
2021-02-16 16:02:19 +01:00
|
|
|
if (sd->size > SDSC_MAX_CAPACITY) {
|
|
|
|
return sd_illegal;
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
2021-06-29 10:09:28 +02:00
|
|
|
if (!address_in_range(sd, "SEND_WRITE_PROT",
|
|
|
|
req.arg, sd->blk_len)) {
|
|
|
|
return sd_r1;
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->state = sd_sendingdata_state;
|
|
|
|
*(uint32_t *) sd->data = sd_wpbits(sd, req.arg);
|
2009-11-03 15:28:19 +01:00
|
|
|
sd->data_start = addr;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->data_offset = 0;
|
2021-02-16 16:02:20 +01:00
|
|
|
return sd_r1;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Erase commands (Class 5) */
|
|
|
|
case 32: /* CMD32: ERASE_WR_BLK_START */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
sd->erase_start = req.arg;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 33: /* CMD33: ERASE_WR_BLK_END */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
sd->erase_end = req.arg;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 38: /* CMD38: ERASE */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
if (sd->csd[14] & 0x30) {
|
|
|
|
sd->card_status |= WP_VIOLATION;
|
|
|
|
return sd_r1b;
|
|
|
|
}
|
|
|
|
|
|
|
|
sd->state = sd_programming_state;
|
|
|
|
sd_erase(sd);
|
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
return sd_r1b;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Lock card commands (Class 7) */
|
|
|
|
case 42: /* CMD42: LOCK_UNLOCK */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
sd->state = sd_receivingdata_state;
|
|
|
|
sd->data_start = 0;
|
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Application specific commands (Class 8) */
|
|
|
|
case 55: /* CMD55: APP_CMD */
|
2018-02-22 16:12:54 +01:00
|
|
|
switch (sd->state) {
|
|
|
|
case sd_ready_state:
|
|
|
|
case sd_identification_state:
|
|
|
|
case sd_inactive_state:
|
|
|
|
return sd_illegal;
|
2018-02-22 16:12:54 +01:00
|
|
|
case sd_idle_state:
|
|
|
|
if (rca) {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"SD: illegal RCA 0x%04x for APP_CMD\n", req.cmd);
|
|
|
|
}
|
2018-02-22 16:12:54 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-07-03 08:00:08 +02:00
|
|
|
if (!sd_is_spi(sd)) {
|
2018-02-22 16:12:53 +01:00
|
|
|
if (sd->rca != rca) {
|
|
|
|
return sd_r0;
|
|
|
|
}
|
|
|
|
}
|
2012-08-13 12:04:06 +02:00
|
|
|
sd->expecting_acmd = true;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->card_status |= APP_CMD;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
case 56: /* CMD56: GEN_CMD */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
sd->data_offset = 0;
|
|
|
|
if (req.arg & 1)
|
|
|
|
sd->state = sd_sendingdata_state;
|
|
|
|
else
|
|
|
|
sd->state = sd_receivingdata_state;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-02-22 16:12:53 +01:00
|
|
|
case 58: /* CMD58: READ_OCR (SPI) */
|
|
|
|
return sd_r3;
|
|
|
|
|
|
|
|
case 59: /* CMD59: CRC_ON_OFF (SPI) */
|
2021-01-23 11:39:55 +01:00
|
|
|
return sd_r1;
|
2018-02-22 16:12:53 +01:00
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
default:
|
2016-02-18 15:16:20 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "SD: Unknown CMD%i\n", req.cmd);
|
2011-12-18 21:37:55 +01:00
|
|
|
return sd_illegal;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 17:24:58 +02:00
|
|
|
return sd_invalid_state_for_cmd(sd, req);
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2009-10-01 23:12:16 +02:00
|
|
|
static sd_rsp_type_t sd_app_command(SDState *sd,
|
2010-04-25 21:31:06 +02:00
|
|
|
SDRequest req)
|
|
|
|
{
|
2022-05-30 19:09:27 +02:00
|
|
|
trace_sdcard_app_command(sd_proto(sd)->name, sd_acmd_name(req.cmd),
|
2018-03-09 18:09:44 +01:00
|
|
|
req.cmd, req.arg, sd_state_name(sd->state));
|
2011-12-18 21:37:59 +01:00
|
|
|
sd->card_status |= APP_CMD;
|
2021-06-24 13:00:08 +02:00
|
|
|
|
|
|
|
if (sd_proto(sd)->acmd[req.cmd]) {
|
|
|
|
return sd_proto(sd)->acmd[req.cmd](sd, req);
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
switch (req.cmd) {
|
|
|
|
case 6: /* ACMD6: SET_BUS_WIDTH */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
sd->sd_status[0] &= 0x3f;
|
|
|
|
sd->sd_status[0] |= (req.arg & 0x03) << 6;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 13: /* ACMD13: SD_STATUS */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
2011-02-18 14:39:00 +01:00
|
|
|
sd->state = sd_sendingdata_state;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->data_start = 0;
|
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
*(uint32_t *) sd->data = sd->blk_written;
|
|
|
|
|
2011-02-18 14:39:00 +01:00
|
|
|
sd->state = sd_sendingdata_state;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->data_start = 0;
|
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 23: /* ACMD23: SET_WR_BLK_ERASE_COUNT */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 41: /* ACMD41: SD_APP_OP_COND */
|
2018-02-22 16:12:54 +01:00
|
|
|
if (sd->state != sd_idle_state) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* If it's the first ACMD41 since reset, we need to decide
|
|
|
|
* whether to power up. If this is not an enquiry ACMD41,
|
|
|
|
* we immediately report power on and proceed below to the
|
|
|
|
* ready state, but if it is, we set a timer to model a
|
|
|
|
* delay for power up. This works around a bug in EDK2
|
|
|
|
* UEFI, which sends an initial enquiry ACMD41, but
|
|
|
|
* assumes that the card is in ready state as soon as it
|
|
|
|
* sees the power up bit set. */
|
|
|
|
if (!FIELD_EX32(sd->ocr, OCR, CARD_POWER_UP)) {
|
|
|
|
if ((req.arg & ACMD41_ENQUIRY_MASK) != 0) {
|
|
|
|
timer_del(sd->ocr_power_timer);
|
|
|
|
sd_ocr_powerup(sd);
|
|
|
|
} else {
|
|
|
|
trace_sdcard_inquiry_cmd41();
|
|
|
|
if (!timer_pending(sd->ocr_power_timer)) {
|
|
|
|
timer_mod_ns(sd->ocr_power_timer,
|
|
|
|
(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)
|
|
|
|
+ OCR_POWER_DELAY_NS));
|
2016-02-18 15:16:20 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 16:12:54 +01:00
|
|
|
}
|
2016-02-18 15:16:20 +01:00
|
|
|
|
2018-02-22 16:12:54 +01:00
|
|
|
if (FIELD_EX32(sd->ocr & req.arg, OCR, VDD_VOLTAGE_WINDOW)) {
|
2013-06-03 18:17:45 +02:00
|
|
|
/* We accept any voltage. 10000 V is nothing.
|
|
|
|
*
|
2016-02-18 15:16:20 +01:00
|
|
|
* Once we're powered up, we advance straight to ready state
|
2013-06-03 18:17:45 +02:00
|
|
|
* unless it's an enquiry ACMD41 (bits 23:0 == 0).
|
|
|
|
*/
|
2018-02-22 16:12:54 +01:00
|
|
|
sd->state = sd_ready_state;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
2018-02-22 16:12:54 +01:00
|
|
|
|
|
|
|
return sd_r3;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
|
|
|
case 42: /* ACMD42: SET_CLR_CARD_DETECT */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
/* Bringing in the 50KOhm pull-up resistor... Done. */
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 51: /* ACMD51: SEND_SCR */
|
|
|
|
switch (sd->state) {
|
|
|
|
case sd_transfer_state:
|
|
|
|
sd->state = sd_sendingdata_state;
|
|
|
|
sd->data_start = 0;
|
|
|
|
sd->data_offset = 0;
|
|
|
|
return sd_r1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-02-22 16:12:53 +01:00
|
|
|
case 18: /* Reserved for SD security applications */
|
|
|
|
case 25:
|
|
|
|
case 26:
|
|
|
|
case 38:
|
|
|
|
case 43 ... 49:
|
|
|
|
/* Refer to the "SD Specifications Part3 Security Specification" for
|
|
|
|
* information about the SD Security Features.
|
|
|
|
*/
|
|
|
|
qemu_log_mask(LOG_UNIMP, "SD: CMD%i Security not implemented\n",
|
|
|
|
req.cmd);
|
|
|
|
return sd_illegal;
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
default:
|
|
|
|
/* Fall back to standard commands. */
|
|
|
|
return sd_normal_command(sd, req);
|
|
|
|
}
|
|
|
|
|
2016-02-18 15:16:20 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "SD: ACMD%i in a wrong state\n", req.cmd);
|
2011-12-18 21:37:57 +01:00
|
|
|
return sd_illegal;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 19:46:44 +02:00
|
|
|
static int cmd_valid_while_locked(SDState *sd, const uint8_t cmd)
|
2011-12-18 21:37:51 +01:00
|
|
|
{
|
|
|
|
/* Valid commands in locked state:
|
|
|
|
* basic class (0)
|
|
|
|
* lock card class (7)
|
|
|
|
* CMD16
|
|
|
|
* implicitly, the ACMD prefix CMD55
|
|
|
|
* ACMD41 and ACMD42
|
|
|
|
* Anything else provokes an "illegal command" response.
|
|
|
|
*/
|
2011-12-18 21:37:59 +01:00
|
|
|
if (sd->expecting_acmd) {
|
2018-06-26 19:46:44 +02:00
|
|
|
return cmd == 41 || cmd == 42;
|
2011-12-18 21:37:51 +01:00
|
|
|
}
|
2018-06-26 19:46:44 +02:00
|
|
|
if (cmd == 16 || cmd == 55) {
|
2011-12-18 21:37:51 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2018-06-26 19:46:44 +02:00
|
|
|
return sd_cmd_class[cmd] == 0 || sd_cmd_class[cmd] == 7;
|
2011-12-18 21:37:51 +01:00
|
|
|
}
|
|
|
|
|
2009-05-10 02:44:56 +02:00
|
|
|
int sd_do_command(SDState *sd, SDRequest *req,
|
2007-04-06 18:49:48 +02:00
|
|
|
uint8_t *response) {
|
2011-12-18 21:37:58 +01:00
|
|
|
int last_state;
|
2009-10-01 23:12:16 +02:00
|
|
|
sd_rsp_type_t rtype;
|
2007-04-06 18:49:48 +02:00
|
|
|
int rsplen;
|
|
|
|
|
2014-10-07 13:59:18 +02:00
|
|
|
if (!sd->blk || !blk_is_inserted(sd->blk) || !sd->enable) {
|
2007-04-06 18:49:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sd_req_crc_validate(req)) {
|
2011-12-18 21:37:53 +01:00
|
|
|
sd->card_status |= COM_CRC_ERROR;
|
2011-12-18 21:37:56 +01:00
|
|
|
rtype = sd_illegal;
|
|
|
|
goto send_response;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
|
2018-02-22 16:12:52 +01:00
|
|
|
if (req->cmd >= SDMMC_CMD_MAX) {
|
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "SD: incorrect command 0x%02x\n",
|
|
|
|
req->cmd);
|
|
|
|
req->cmd &= 0x3f;
|
|
|
|
}
|
|
|
|
|
2011-12-18 21:37:58 +01:00
|
|
|
if (sd->card_status & CARD_IS_LOCKED) {
|
2018-06-26 19:46:44 +02:00
|
|
|
if (!cmd_valid_while_locked(sd, req->cmd)) {
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->card_status |= ILLEGAL_COMMAND;
|
2012-08-13 12:04:06 +02:00
|
|
|
sd->expecting_acmd = false;
|
2016-02-18 15:16:20 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "SD: Card is locked\n");
|
2011-12-18 21:37:56 +01:00
|
|
|
rtype = sd_illegal;
|
|
|
|
goto send_response;
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
2011-12-18 21:37:51 +01:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2011-12-18 21:37:58 +01:00
|
|
|
last_state = sd->state;
|
|
|
|
sd_set_mode(sd);
|
|
|
|
|
2011-12-18 21:37:59 +01:00
|
|
|
if (sd->expecting_acmd) {
|
2012-08-13 12:04:06 +02:00
|
|
|
sd->expecting_acmd = false;
|
2007-04-06 18:49:48 +02:00
|
|
|
rtype = sd_app_command(sd, *req);
|
2011-12-18 21:37:59 +01:00
|
|
|
} else {
|
2007-04-06 18:49:48 +02:00
|
|
|
rtype = sd_normal_command(sd, *req);
|
2011-12-18 21:37:59 +01:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2011-12-18 21:37:55 +01:00
|
|
|
if (rtype == sd_illegal) {
|
|
|
|
sd->card_status |= ILLEGAL_COMMAND;
|
2011-12-18 21:37:58 +01:00
|
|
|
} else {
|
|
|
|
/* Valid command, we can update the 'state before command' bits.
|
|
|
|
* (Do this now so they appear in r1 responses.)
|
|
|
|
*/
|
|
|
|
sd->current_cmd = req->cmd;
|
|
|
|
sd->card_status &= ~CURRENT_STATE;
|
|
|
|
sd->card_status |= (last_state << 9);
|
2011-12-18 21:37:55 +01:00
|
|
|
}
|
|
|
|
|
2011-12-18 21:37:56 +01:00
|
|
|
send_response:
|
2007-04-06 18:49:48 +02:00
|
|
|
switch (rtype) {
|
|
|
|
case sd_r1:
|
|
|
|
case sd_r1b:
|
2011-12-18 21:37:58 +01:00
|
|
|
sd_response_r1_make(sd, response);
|
2007-04-06 18:49:48 +02:00
|
|
|
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;
|
|
|
|
|
2007-12-24 15:41:39 +01:00
|
|
|
case sd_r7:
|
|
|
|
sd_response_r7_make(sd, response);
|
|
|
|
rsplen = 4;
|
|
|
|
break;
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
case sd_r0:
|
2011-12-18 21:37:55 +01:00
|
|
|
case sd_illegal:
|
2007-04-06 18:49:48 +02:00
|
|
|
rsplen = 0;
|
|
|
|
break;
|
2018-02-22 16:12:52 +01:00
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
2018-02-22 16:12:52 +01:00
|
|
|
trace_sdcard_response(sd_response_name(rtype), rsplen);
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2011-12-18 21:37:58 +01:00
|
|
|
if (rtype != sd_illegal) {
|
|
|
|
/* Clear the "clear on valid command" status bits now we've
|
|
|
|
* sent any response
|
|
|
|
*/
|
|
|
|
sd->card_status &= ~CARD_STATUS_B;
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
#ifdef DEBUG_SD
|
2020-08-22 20:09:50 +02:00
|
|
|
qemu_hexdump(stderr, "Response", response, rsplen);
|
2007-04-06 18:49:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return rsplen;
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:23:40 +02:00
|
|
|
void sd_write_byte(SDState *sd, uint8_t value)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2014-10-07 13:59:18 +02:00
|
|
|
if (!sd->blk || !blk_is_inserted(sd->blk) || !sd->enable)
|
2007-04-06 18:49:48 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (sd->state != sd_receivingdata_state) {
|
2016-02-18 15:16:20 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
2020-08-14 11:23:40 +02:00
|
|
|
"%s: not in Receiving-Data state\n", __func__);
|
2007-04-06 18:49:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sd->card_status & (ADDRESS_ERROR | WP_VIOLATION))
|
|
|
|
return;
|
|
|
|
|
2022-05-30 19:09:27 +02:00
|
|
|
trace_sdcard_write_data(sd_proto(sd)->name,
|
2018-03-09 18:09:44 +01:00
|
|
|
sd_acmd_name(sd->current_cmd),
|
2018-03-09 18:09:44 +01:00
|
|
|
sd->current_cmd, value);
|
2007-04-06 18:49:48 +02:00
|
|
|
switch (sd->current_cmd) {
|
|
|
|
case 24: /* CMD24: WRITE_SINGLE_BLOCK */
|
|
|
|
sd->data[sd->data_offset ++] = value;
|
|
|
|
if (sd->data_offset >= sd->blk_len) {
|
|
|
|
/* TODO: Check CRC before committing */
|
|
|
|
sd->state = sd_programming_state;
|
|
|
|
BLK_WRITE_BLOCK(sd->data_start, sd->data_offset);
|
|
|
|
sd->blk_written ++;
|
|
|
|
sd->csd[14] |= 0x40;
|
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
|
2011-07-25 14:21:30 +02:00
|
|
|
if (sd->data_offset == 0) {
|
2013-04-09 13:48:19 +02:00
|
|
|
/* Start of the block - let's check the address is valid */
|
2021-06-07 15:32:06 +02:00
|
|
|
if (!address_in_range(sd, "WRITE_MULTIPLE_BLOCK",
|
|
|
|
sd->data_start, sd->blk_len)) {
|
2007-04-06 18:49:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-02-16 16:02:24 +01:00
|
|
|
if (sd->size <= SDSC_MAX_CAPACITY) {
|
|
|
|
if (sd_wp_addr(sd, sd->data_start)) {
|
|
|
|
sd->card_status |= WP_VIOLATION;
|
|
|
|
break;
|
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
2011-07-25 14:21:30 +02:00
|
|
|
}
|
|
|
|
sd->data[sd->data_offset++] = value;
|
|
|
|
if (sd->data_offset >= sd->blk_len) {
|
|
|
|
/* TODO: Check CRC before committing */
|
|
|
|
sd->state = sd_programming_state;
|
|
|
|
BLK_WRITE_BLOCK(sd->data_start, sd->data_offset);
|
|
|
|
sd->blk_written++;
|
|
|
|
sd->data_start += sd->blk_len;
|
|
|
|
sd->data_offset = 0;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->csd[14] |= 0x40;
|
|
|
|
|
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
2016-02-18 15:16:19 +01:00
|
|
|
if (sd->multi_blk_cnt != 0) {
|
|
|
|
if (--sd->multi_blk_cnt == 0) {
|
|
|
|
/* Stop! */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->state = sd_receivingdata_state;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 26: /* CMD26: PROGRAM_CID */
|
|
|
|
sd->data[sd->data_offset ++] = value;
|
|
|
|
if (sd->data_offset >= sizeof(sd->cid)) {
|
|
|
|
/* TODO: Check CRC before committing */
|
|
|
|
sd->state = sd_programming_state;
|
|
|
|
for (i = 0; i < sizeof(sd->cid); i ++)
|
|
|
|
if ((sd->cid[i] | 0x00) != sd->data[i])
|
|
|
|
sd->card_status |= CID_CSD_OVERWRITE;
|
|
|
|
|
|
|
|
if (!(sd->card_status & CID_CSD_OVERWRITE))
|
|
|
|
for (i = 0; i < sizeof(sd->cid); i ++) {
|
|
|
|
sd->cid[i] |= 0x00;
|
|
|
|
sd->cid[i] &= sd->data[i];
|
|
|
|
}
|
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 27: /* CMD27: PROGRAM_CSD */
|
|
|
|
sd->data[sd->data_offset ++] = value;
|
|
|
|
if (sd->data_offset >= sizeof(sd->csd)) {
|
|
|
|
/* TODO: Check CRC before committing */
|
|
|
|
sd->state = sd_programming_state;
|
|
|
|
for (i = 0; i < sizeof(sd->csd); i ++)
|
|
|
|
if ((sd->csd[i] | sd_csd_rw_mask[i]) !=
|
|
|
|
(sd->data[i] | sd_csd_rw_mask[i]))
|
|
|
|
sd->card_status |= CID_CSD_OVERWRITE;
|
|
|
|
|
|
|
|
/* Copy flag (OTP) & Permanent write protect */
|
|
|
|
if (sd->csd[14] & ~sd->data[14] & 0x60)
|
|
|
|
sd->card_status |= CID_CSD_OVERWRITE;
|
|
|
|
|
|
|
|
if (!(sd->card_status & CID_CSD_OVERWRITE))
|
|
|
|
for (i = 0; i < sizeof(sd->csd); i ++) {
|
|
|
|
sd->csd[i] |= sd_csd_rw_mask[i];
|
|
|
|
sd->csd[i] &= sd->data[i];
|
|
|
|
}
|
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 42: /* CMD42: LOCK_UNLOCK */
|
|
|
|
sd->data[sd->data_offset ++] = value;
|
|
|
|
if (sd->data_offset >= sd->blk_len) {
|
|
|
|
/* TODO: Check CRC before committing */
|
|
|
|
sd->state = sd_programming_state;
|
|
|
|
sd_lock_command(sd);
|
|
|
|
/* Bzzzzzzztt .... Operation complete. */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 56: /* CMD56: GEN_CMD */
|
|
|
|
sd->data[sd->data_offset ++] = value;
|
|
|
|
if (sd->data_offset >= sd->blk_len) {
|
|
|
|
APP_WRITE_BLOCK(sd->data_start, sd->data_offset);
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-08-14 11:23:40 +02:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "%s: unknown command\n", __func__);
|
2007-04-06 18:49:48 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:09:44 +01:00
|
|
|
#define SD_TUNING_BLOCK_SIZE 64
|
|
|
|
|
|
|
|
static const uint8_t sd_tuning_block_pattern[SD_TUNING_BLOCK_SIZE] = {
|
|
|
|
/* See: Physical Layer Simplified Specification Version 3.01, Table 4-2 */
|
|
|
|
0xff, 0x0f, 0xff, 0x00, 0x0f, 0xfc, 0xc3, 0xcc,
|
|
|
|
0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
|
|
|
|
0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
|
|
|
|
0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
|
|
|
|
0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
|
|
|
|
0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
|
|
|
|
0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
|
|
|
|
0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
|
|
|
|
};
|
|
|
|
|
2020-08-14 11:23:40 +02:00
|
|
|
uint8_t sd_read_byte(SDState *sd)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
/* TODO: Append CRCs */
|
|
|
|
uint8_t ret;
|
2020-07-07 06:12:58 +02:00
|
|
|
uint32_t io_len;
|
2007-04-06 18:49:48 +02:00
|
|
|
|
2014-10-07 13:59:18 +02:00
|
|
|
if (!sd->blk || !blk_is_inserted(sd->blk) || !sd->enable)
|
2007-04-06 18:49:48 +02:00
|
|
|
return 0x00;
|
|
|
|
|
|
|
|
if (sd->state != sd_sendingdata_state) {
|
2016-02-18 15:16:20 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
2020-08-14 11:23:40 +02:00
|
|
|
"%s: not in Sending-Data state\n", __func__);
|
2007-04-06 18:49:48 +02:00
|
|
|
return 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sd->card_status & (ADDRESS_ERROR | WP_VIOLATION))
|
|
|
|
return 0x00;
|
|
|
|
|
2009-11-03 15:28:19 +01:00
|
|
|
io_len = (sd->ocr & (1 << 30)) ? 512 : sd->blk_len;
|
|
|
|
|
2022-05-30 19:09:27 +02:00
|
|
|
trace_sdcard_read_data(sd_proto(sd)->name,
|
2018-03-09 18:09:44 +01:00
|
|
|
sd_acmd_name(sd->current_cmd),
|
2018-03-09 18:09:44 +01:00
|
|
|
sd->current_cmd, io_len);
|
2007-04-06 18:49:48 +02:00
|
|
|
switch (sd->current_cmd) {
|
|
|
|
case 6: /* CMD6: SWITCH_FUNCTION */
|
|
|
|
ret = sd->data[sd->data_offset ++];
|
|
|
|
|
|
|
|
if (sd->data_offset >= 64)
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
|
2007-11-25 00:35:08 +01:00
|
|
|
case 9: /* CMD9: SEND_CSD */
|
|
|
|
case 10: /* CMD10: SEND_CID */
|
|
|
|
ret = sd->data[sd->data_offset ++];
|
|
|
|
|
|
|
|
if (sd->data_offset >= 16)
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
case 13: /* ACMD13: SD_STATUS */
|
|
|
|
ret = sd->sd_status[sd->data_offset ++];
|
|
|
|
|
|
|
|
if (sd->data_offset >= sizeof(sd->sd_status))
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 17: /* CMD17: READ_SINGLE_BLOCK */
|
|
|
|
if (sd->data_offset == 0)
|
2009-11-03 15:28:19 +01:00
|
|
|
BLK_READ_BLOCK(sd->data_start, io_len);
|
2007-04-06 18:49:48 +02:00
|
|
|
ret = sd->data[sd->data_offset ++];
|
|
|
|
|
2009-11-03 15:28:19 +01:00
|
|
|
if (sd->data_offset >= io_len)
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 18: /* CMD18: READ_MULTIPLE_BLOCK */
|
2017-10-06 17:46:47 +02:00
|
|
|
if (sd->data_offset == 0) {
|
2021-06-07 15:32:06 +02:00
|
|
|
if (!address_in_range(sd, "READ_MULTIPLE_BLOCK",
|
|
|
|
sd->data_start, io_len)) {
|
2017-10-06 17:46:47 +02:00
|
|
|
return 0x00;
|
|
|
|
}
|
2009-11-03 15:28:19 +01:00
|
|
|
BLK_READ_BLOCK(sd->data_start, io_len);
|
2017-10-06 17:46:47 +02:00
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
ret = sd->data[sd->data_offset ++];
|
|
|
|
|
2009-11-03 15:28:19 +01:00
|
|
|
if (sd->data_offset >= io_len) {
|
|
|
|
sd->data_start += io_len;
|
2007-04-06 18:49:48 +02:00
|
|
|
sd->data_offset = 0;
|
2016-02-18 15:16:19 +01:00
|
|
|
|
|
|
|
if (sd->multi_blk_cnt != 0) {
|
|
|
|
if (--sd->multi_blk_cnt == 0) {
|
|
|
|
/* Stop! */
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-04-06 18:49:48 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-03-09 18:09:44 +01:00
|
|
|
case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
|
|
|
|
if (sd->data_offset >= SD_TUNING_BLOCK_SIZE - 1) {
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
}
|
|
|
|
ret = sd_tuning_block_pattern[sd->data_offset++];
|
|
|
|
break;
|
|
|
|
|
2007-04-06 18:49:48 +02:00
|
|
|
case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
|
|
|
|
ret = sd->data[sd->data_offset ++];
|
|
|
|
|
|
|
|
if (sd->data_offset >= 4)
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 30: /* CMD30: SEND_WRITE_PROT */
|
|
|
|
ret = sd->data[sd->data_offset ++];
|
|
|
|
|
|
|
|
if (sd->data_offset >= 4)
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 51: /* ACMD51: SEND_SCR */
|
|
|
|
ret = sd->scr[sd->data_offset ++];
|
|
|
|
|
|
|
|
if (sd->data_offset >= sizeof(sd->scr))
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 56: /* CMD56: GEN_CMD */
|
|
|
|
if (sd->data_offset == 0)
|
|
|
|
APP_READ_BLOCK(sd->data_start, sd->blk_len);
|
|
|
|
ret = sd->data[sd->data_offset ++];
|
|
|
|
|
|
|
|
if (sd->data_offset >= sd->blk_len)
|
|
|
|
sd->state = sd_transfer_state;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-08-14 11:23:40 +02:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "%s: unknown command\n", __func__);
|
2007-04-06 18:49:48 +02:00
|
|
|
return 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-28 07:30:30 +01:00
|
|
|
static bool sd_receive_ready(SDState *sd)
|
|
|
|
{
|
|
|
|
return sd->state == sd_receivingdata_state;
|
|
|
|
}
|
|
|
|
|
2018-02-16 03:01:26 +01:00
|
|
|
static bool sd_data_ready(SDState *sd)
|
2007-04-06 18:49:48 +02:00
|
|
|
{
|
|
|
|
return sd->state == sd_sendingdata_state;
|
|
|
|
}
|
2008-04-14 23:05:22 +02:00
|
|
|
|
2012-08-13 12:04:06 +02:00
|
|
|
void sd_enable(SDState *sd, bool enable)
|
2008-04-14 23:05:22 +02:00
|
|
|
{
|
|
|
|
sd->enable = enable;
|
|
|
|
}
|
2016-02-18 15:16:18 +01:00
|
|
|
|
2022-05-30 19:09:27 +02:00
|
|
|
static const SDProto sd_proto_spi = {
|
|
|
|
.name = "SPI",
|
2021-06-24 13:22:40 +02:00
|
|
|
.cmd = {
|
2021-06-24 15:48:55 +02:00
|
|
|
[0] = sd_cmd_GO_IDLE_STATE,
|
2021-06-24 15:29:27 +02:00
|
|
|
[1] = sd_cmd_SEND_OP_CMD,
|
2021-06-24 13:22:40 +02:00
|
|
|
[2 ... 4] = sd_cmd_illegal,
|
|
|
|
[5] = sd_cmd_illegal,
|
|
|
|
[7] = sd_cmd_illegal,
|
|
|
|
[15] = sd_cmd_illegal,
|
|
|
|
[26] = sd_cmd_illegal,
|
|
|
|
[52 ... 54] = sd_cmd_illegal,
|
|
|
|
},
|
2021-06-24 13:29:49 +02:00
|
|
|
.acmd = {
|
|
|
|
[6] = sd_cmd_unimplemented,
|
2021-06-24 15:29:27 +02:00
|
|
|
[41] = sd_cmd_SEND_OP_CMD,
|
2021-06-24 13:29:49 +02:00
|
|
|
},
|
2022-05-30 19:09:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const SDProto sd_proto_sd = {
|
|
|
|
.name = "SD",
|
2021-06-24 13:22:40 +02:00
|
|
|
.cmd = {
|
2021-06-24 15:48:55 +02:00
|
|
|
[0] = sd_cmd_GO_IDLE_STATE,
|
2021-06-24 13:22:40 +02:00
|
|
|
[1] = sd_cmd_illegal,
|
2021-06-24 15:36:03 +02:00
|
|
|
[2] = sd_cmd_ALL_SEND_CID,
|
2021-06-24 15:46:29 +02:00
|
|
|
[3] = sd_cmd_SEND_RELATIVE_ADDR,
|
2021-06-24 13:22:40 +02:00
|
|
|
[5] = sd_cmd_illegal,
|
2022-05-25 09:21:21 +02:00
|
|
|
[19] = sd_cmd_SEND_TUNING_BLOCK,
|
2022-05-30 19:20:25 +02:00
|
|
|
[23] = sd_cmd_SET_BLOCK_COUNT,
|
2021-06-24 13:22:40 +02:00
|
|
|
[52 ... 54] = sd_cmd_illegal,
|
|
|
|
[58] = sd_cmd_illegal,
|
|
|
|
[59] = sd_cmd_illegal,
|
|
|
|
},
|
2022-05-30 19:09:27 +02:00
|
|
|
};
|
|
|
|
|
2016-02-18 15:16:18 +01:00
|
|
|
static void sd_instance_init(Object *obj)
|
|
|
|
{
|
|
|
|
SDState *sd = SD_CARD(obj);
|
|
|
|
|
|
|
|
sd->enable = true;
|
2016-02-18 15:16:20 +01:00
|
|
|
sd->ocr_power_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, sd_ocr_powerup, sd);
|
2016-02-18 15:16:18 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 18:03:49 +02:00
|
|
|
static void sd_instance_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
SDState *sd = SD_CARD(obj);
|
|
|
|
|
|
|
|
timer_free(sd->ocr_power_timer);
|
|
|
|
}
|
|
|
|
|
2016-02-18 15:16:18 +01:00
|
|
|
static void sd_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
SDState *sd = SD_CARD(dev);
|
2017-01-24 13:43:31 +01:00
|
|
|
int ret;
|
2016-02-18 15:16:18 +01:00
|
|
|
|
2018-06-08 14:15:33 +02:00
|
|
|
switch (sd->spec_version) {
|
|
|
|
case SD_PHY_SPECv1_10_VERS
|
2018-06-08 14:15:34 +02:00
|
|
|
... SD_PHY_SPECv3_01_VERS:
|
2018-06-08 14:15:33 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_setg(errp, "Invalid SD card Spec version: %u", sd->spec_version);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-18 15:16:18 +01:00
|
|
|
if (sd->blk) {
|
hw/sd/sdcard: Do not allow invalid SD card sizes
QEMU allows to create SD card with unrealistic sizes. This could
work, but some guests (at least Linux) consider sizes that are not
a power of 2 as a firmware bug and fix the card size to the next
power of 2.
While the possibility to use small SD card images has been seen as
a feature, it became a bug with CVE-2020-13253, where the guest is
able to do OOB read/write accesses past the image size end.
In a pair of commits we will fix CVE-2020-13253 as:
Read command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR
occurred and no data transfer is performed.
Write command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR
occurred and no data transfer is performed.
WP_VIOLATION errors are not modified: the error bit is set, we
stay in receive-data state, wait for a stop command. All further
data transfer is ignored. See the check on sd->card_status at the
beginning of sd_read_data() and sd_write_data().
While this is the correct behavior, in case QEMU create smaller SD
cards, guests still try to access past the image size end, and QEMU
considers this is an invalid address, thus "all further data transfer
is ignored". This is wrong and make the guest looping until
eventually timeouts.
Fix by not allowing invalid SD card sizes (suggesting the expected
size as a hint):
$ qemu-system-arm -M orangepi-pc -drive file=rootfs.ext2,if=sd,format=raw
qemu-system-arm: Invalid SD card size: 60 MiB
SD card size has to be a power of 2, e.g. 64 MiB.
You can resize disk images with 'qemu-img resize <imagefile> <new-size>'
(note that this will lose data if you make the image smaller than it currently is).
Cc: qemu-stable@nongnu.org
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Message-Id: <20200713183209.26308-8-f4bug@amsat.org>
2020-07-07 13:02:34 +02:00
|
|
|
int64_t blk_size;
|
|
|
|
|
2021-01-18 13:34:47 +01:00
|
|
|
if (!blk_supports_write_perm(sd->blk)) {
|
2018-06-06 03:28:51 +02:00
|
|
|
error_setg(errp, "Cannot use read-only drive as SD card");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
hw/sd/sdcard: Do not allow invalid SD card sizes
QEMU allows to create SD card with unrealistic sizes. This could
work, but some guests (at least Linux) consider sizes that are not
a power of 2 as a firmware bug and fix the card size to the next
power of 2.
While the possibility to use small SD card images has been seen as
a feature, it became a bug with CVE-2020-13253, where the guest is
able to do OOB read/write accesses past the image size end.
In a pair of commits we will fix CVE-2020-13253 as:
Read command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR
occurred and no data transfer is performed.
Write command is rejected if BLOCK_LEN_ERROR or ADDRESS_ERROR
occurred and no data transfer is performed.
WP_VIOLATION errors are not modified: the error bit is set, we
stay in receive-data state, wait for a stop command. All further
data transfer is ignored. See the check on sd->card_status at the
beginning of sd_read_data() and sd_write_data().
While this is the correct behavior, in case QEMU create smaller SD
cards, guests still try to access past the image size end, and QEMU
considers this is an invalid address, thus "all further data transfer
is ignored". This is wrong and make the guest looping until
eventually timeouts.
Fix by not allowing invalid SD card sizes (suggesting the expected
size as a hint):
$ qemu-system-arm -M orangepi-pc -drive file=rootfs.ext2,if=sd,format=raw
qemu-system-arm: Invalid SD card size: 60 MiB
SD card size has to be a power of 2, e.g. 64 MiB.
You can resize disk images with 'qemu-img resize <imagefile> <new-size>'
(note that this will lose data if you make the image smaller than it currently is).
Cc: qemu-stable@nongnu.org
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Message-Id: <20200713183209.26308-8-f4bug@amsat.org>
2020-07-07 13:02:34 +02:00
|
|
|
blk_size = blk_getlength(sd->blk);
|
|
|
|
if (blk_size > 0 && !is_power_of_2(blk_size)) {
|
|
|
|
int64_t blk_size_aligned = pow2ceil(blk_size);
|
|
|
|
char *blk_size_str;
|
|
|
|
|
|
|
|
blk_size_str = size_to_str(blk_size);
|
|
|
|
error_setg(errp, "Invalid SD card size: %s", blk_size_str);
|
|
|
|
g_free(blk_size_str);
|
|
|
|
|
|
|
|
blk_size_str = size_to_str(blk_size_aligned);
|
|
|
|
error_append_hint(errp,
|
|
|
|
"SD card size has to be a power of 2, e.g. %s.\n"
|
|
|
|
"You can resize disk images with"
|
|
|
|
" 'qemu-img resize <imagefile> <new-size>'\n"
|
|
|
|
"(note that this will lose data if you make the"
|
|
|
|
" image smaller than it currently is).\n",
|
|
|
|
blk_size_str);
|
|
|
|
g_free(blk_size_str);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-24 13:43:31 +01:00
|
|
|
ret = blk_set_perm(sd->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
|
|
|
|
BLK_PERM_ALL, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-18 15:16:18 +01:00
|
|
|
blk_set_dev_ops(sd->blk, &sd_block_ops, sd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property sd_properties[] = {
|
2018-06-08 14:15:33 +02:00
|
|
|
DEFINE_PROP_UINT8("spec_version", SDState,
|
|
|
|
spec_version, SD_PHY_SPECv2_00_VERS),
|
2016-02-18 15:16:18 +01:00
|
|
|
DEFINE_PROP_DRIVE("drive", SDState, blk),
|
|
|
|
/* We do not model the chip select pin, so allow the board to select
|
|
|
|
* whether card should be in SSI or MMC/SD mode. It is also up to the
|
|
|
|
* board to ensure that ssi transfers only occur when the chip select
|
|
|
|
* is asserted. */
|
|
|
|
DEFINE_PROP_END_OF_LIST()
|
|
|
|
};
|
|
|
|
|
|
|
|
static void sd_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2016-02-18 15:16:18 +01:00
|
|
|
SDCardClass *sc = SD_CARD_CLASS(klass);
|
2016-02-18 15:16:18 +01:00
|
|
|
|
|
|
|
dc->realize = sd_realize;
|
2020-01-10 16:30:32 +01:00
|
|
|
device_class_set_props(dc, sd_properties);
|
2016-02-18 15:16:18 +01:00
|
|
|
dc->vmsd = &sd_vmstate;
|
2016-02-18 15:16:18 +01:00
|
|
|
dc->reset = sd_reset;
|
2016-02-18 15:16:18 +01:00
|
|
|
dc->bus_type = TYPE_SD_BUS;
|
2019-01-24 17:20:45 +01:00
|
|
|
set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
|
2016-02-18 15:16:18 +01:00
|
|
|
|
2018-02-08 17:48:09 +01:00
|
|
|
sc->set_voltage = sd_set_voltage;
|
2018-02-08 17:48:10 +01:00
|
|
|
sc->get_dat_lines = sd_get_dat_lines;
|
|
|
|
sc->get_cmd_line = sd_get_cmd_line;
|
2016-02-18 15:16:18 +01:00
|
|
|
sc->do_command = sd_do_command;
|
2020-08-14 11:23:40 +02:00
|
|
|
sc->write_byte = sd_write_byte;
|
|
|
|
sc->read_byte = sd_read_byte;
|
2021-01-28 07:30:30 +01:00
|
|
|
sc->receive_ready = sd_receive_ready;
|
2016-02-18 15:16:18 +01:00
|
|
|
sc->data_ready = sd_data_ready;
|
|
|
|
sc->enable = sd_enable;
|
|
|
|
sc->get_inserted = sd_get_inserted;
|
|
|
|
sc->get_readonly = sd_get_readonly;
|
2023-07-03 08:00:08 +02:00
|
|
|
sc->proto = &sd_proto_sd;
|
2016-02-18 15:16:18 +01:00
|
|
|
}
|
|
|
|
|
2023-07-03 08:00:08 +02:00
|
|
|
/*
|
|
|
|
* We do not model the chip select pin, so allow the board to select
|
|
|
|
* whether card should be in SSI or MMC/SD mode. It is also up to the
|
|
|
|
* board to ensure that ssi transfers only occur when the chip select
|
|
|
|
* is asserted.
|
|
|
|
*/
|
|
|
|
static void sd_spi_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
SDCardClass *sc = SD_CARD_CLASS(klass);
|
|
|
|
|
|
|
|
dc->desc = "SD SPI";
|
|
|
|
sc->proto = &sd_proto_spi;
|
|
|
|
}
|
|
|
|
|
2023-10-31 06:55:39 +01:00
|
|
|
static const TypeInfo sd_types[] = {
|
|
|
|
{
|
|
|
|
.name = TYPE_SD_CARD,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(SDState),
|
|
|
|
.class_size = sizeof(SDCardClass),
|
|
|
|
.class_init = sd_class_init,
|
|
|
|
.instance_init = sd_instance_init,
|
|
|
|
.instance_finalize = sd_instance_finalize,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = TYPE_SD_CARD_SPI,
|
|
|
|
.parent = TYPE_SD_CARD,
|
|
|
|
.class_init = sd_spi_class_init,
|
|
|
|
},
|
2023-07-03 08:00:08 +02:00
|
|
|
};
|
|
|
|
|
2023-10-31 06:55:39 +01:00
|
|
|
DEFINE_TYPES(sd_types)
|