Patchwork [v4,2/4] hw/sd.c: add SD card save/load support

login
register
mail settings
Submitter Mitsyanko Igor
Date Jan. 25, 2012, 10:58 a.m.
Message ID <1327489134-32091-3-git-send-email-i.mitsyanko@samsung.com>
Download mbox | patch
Permalink /patch/137733/
State New
Headers show

Comments

Mitsyanko Igor - Jan. 25, 2012, 10:58 a.m.
This patch updates SD card emulation to support save/load of card's state.

Signed-off-by: Mitsyanko Igor <i.mitsyanko@samsung.com>
---
 hw/sd.c |  102 +++++++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 73 insertions(+), 29 deletions(-)

Patch

diff --git a/hw/sd.c b/hw/sd.c
index 07eb263..bfe7f58 100644
--- a/hw/sd.c
+++ b/hw/sd.c
@@ -54,24 +54,28 @@  typedef enum {
     sd_illegal = -2,
 } sd_rsp_type_t;
 
+enum {
+    sd_inactive,
+    sd_card_identification_mode,
+    sd_data_transfer_mode,
+};
+
+enum {
+    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,
+};
+
 struct SDState {
-    enum {
-        sd_inactive,
-        sd_card_identification_mode,
-        sd_data_transfer_mode,
-    } mode;
-    enum {
-        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,
-    } state;
+    uint32_t mode;
+    int32_t state;
     uint32_t ocr;
     uint8_t scr[8];
     uint8_t cid[16];
@@ -80,23 +84,23 @@  struct SDState {
     uint32_t card_status;
     uint8_t sd_status[64];
     uint32_t vhs;
-    int wp_switch;
-    int *wp_groups;
+    uint32_t wp_switch;
+    uint32_t *wp_groups;
     uint64_t size;
-    int blk_len;
+    uint32_t blk_len;
     uint32_t erase_start;
     uint32_t erase_end;
     uint8_t pwd[16];
-    int pwd_len;
-    int function_group[6];
+    uint32_t pwd_len;
+    uint8_t function_group[6];
 
     int spi;
-    int current_cmd;
+    uint8_t current_cmd;
     /* True if we will handle the next command as an ACMD. Note that this does
      * *not* track the APP_CMD status bit!
      */
-    int expecting_acmd;
-    int blk_written;
+    uint32_t expecting_acmd;
+    uint32_t blk_written;
     uint64_t data_start;
     uint32_t data_offset;
     uint8_t data[512];
@@ -105,7 +109,7 @@  struct SDState {
     BlockDriverState *bdrv;
     uint8_t *buf;
 
-    int enable;
+    uint32_t enable;
 };
 
 static void sd_set_mode(SDState *sd)
@@ -415,8 +419,8 @@  static void sd_reset(SDState *sd, BlockDriverState *bdrv)
     if (sd->wp_groups)
         g_free(sd->wp_groups);
     sd->wp_switch = bdrv ? bdrv_is_read_only(bdrv) : 0;
-    sd->wp_groups = (int *) g_malloc0(sizeof(int) * sect);
-    memset(sd->function_group, 0, sizeof(int) * 6);
+    sd->wp_groups = g_new0(uint32_t, sect);
+    memset(sd->function_group, 0, sizeof(sd->function_group));
     sd->erase_start = 0;
     sd->erase_end = 0;
     sd->size = size;
@@ -440,6 +444,45 @@  static const BlockDevOps sd_block_ops = {
     .change_media_cb = sd_cardchange,
 };
 
+static size_t sd_get_wpgroups_size(void *opaque, int version_id)
+{
+    SDState *sd = (SDState *)opaque;
+    return sizeof(uint32_t) * (sd->size >> (HWBLOCK_SHIFT + SECTOR_SHIFT +
+            WPGROUP_SHIFT));
+}
+
+static const VMStateDescription sd_vmstate = {
+    .name = "sd_card",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields      = (VMStateField[]) {
+        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),
+        VMSTATE_VBUFFER(wp_groups, SDState, 1, NULL, 0, sd_get_wpgroups_size),
+        VMSTATE_UINT32(blk_len, SDState),
+        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_UINT32(expecting_acmd, SDState),
+        VMSTATE_UINT32(blk_written, SDState),
+        VMSTATE_UINT64(data_start, SDState),
+        VMSTATE_UINT32(data_offset, SDState),
+        VMSTATE_UINT8_ARRAY(data, SDState, 512),
+        VMSTATE_BUFFER_UNSAFE(buf, SDState, 1, 512),
+        VMSTATE_UINT32(enable, SDState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 /* 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
@@ -457,6 +500,7 @@  SDState *sd_init(BlockDriverState *bs, int is_spi)
         bdrv_attach_dev_nofail(sd->bdrv, sd);
         bdrv_set_dev_ops(sd->bdrv, &sd_block_ops, sd);
     }
+    vmstate_register(NULL, -1, &sd_vmstate, sd);
     return sd;
 }
 
@@ -560,7 +604,7 @@  static void sd_lock_command(SDState *sd)
             sd->card_status |= LOCK_UNLOCK_FAILED;
             return;
         }
-        memset(sd->wp_groups, 0, sizeof(int) * (sd->size >>
+        memset(sd->wp_groups, 0, sizeof(uint32_t) * (sd->size >>
                         (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)));
         sd->csd[14] &= ~0x10;
         sd->card_status &= ~CARD_IS_LOCKED;