struct blk_state {
uint64_t capacity;
- uint8_t * blk_space;
addr_t blk_base_addr;
+ uint8_t * blk_space;
};
static uint64_t blk_get_capacity(void * private_data) {
struct blk_state * blk = (struct blk_state *)private_data;
- PrintDebug("SymBlk: Getting Capacity %d\n", (uint32_t)(blk->capacity));
+ // PrintDebug("SymBlk: Getting Capacity %d\n", (uint32_t)(blk->capacity));
return blk->capacity;
}
static int blk_read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
struct blk_state * blk = (struct blk_state *)private_data;
+ // PrintDebug("TmpDisk Reading %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba);
+
+ if (lba + num_bytes > blk->capacity) {
+ PrintError("TMPDISK Read past end of disk\n");
+ return -1;
+ }
+
memcpy(buf, blk->blk_space + lba, num_bytes);
return 0;
static int blk_write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
struct blk_state * blk = (struct blk_state *)private_data;
+ // PrintDebug("TmpDisk Writing %d bytes to %p (lba=%p)\n", (uint32_t)num_bytes, buf, (void *)(addr_t)lba);
+
+ if (lba + num_bytes > blk->capacity) {
+ PrintError("TMPDISK Write past end of disk\n");
+ return -1;
+ }
+
memcpy(blk->blk_space + lba, buf, num_bytes);
return 0;
}
-static int blk_free(struct vm_device * dev) {
- return -1;
+static int blk_free(struct blk_state * blk) {
+ V3_FreePages((void *)blk->blk_base_addr, blk->capacity / 4096);
+
+ V3_Free(blk);
+ return 0;
}
static struct v3_device_ops dev_ops = {
- .free = blk_free,
- .reset = NULL,
- .start = NULL,
- .stop = NULL,
+ .free = (int (*)(void *))blk_free,
+
};
-static int blk_init(struct guest_info * vm, v3_cfg_tree_t * cfg) {
+static int blk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
struct blk_state * blk = NULL;
v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
- char * name = v3_cfg_val(cfg, "name");
- uint64_t capacity = atoi(v3_cfg_val(cfg, "size"));
+ char * dev_id = v3_cfg_val(cfg, "ID");
+ uint64_t capacity = atoi(v3_cfg_val(cfg, "size")) * 1024 * 1024;
if (!frontend_cfg) {
PrintError("Frontend Configuration not present\n");
return -1;
}
- PrintDebug("Creating Blk Device\n");
+ PrintDebug("Intializing TMPDISK (capacity=%d)\n", (uint32_t)capacity);
- blk = (struct blk_state *)V3_Malloc(sizeof(struct blk_state) + ((capacity / 4096) / 8));
+ blk = (struct blk_state *)V3_Malloc(sizeof(struct blk_state));
blk->capacity = capacity;
-
- blk->blk_base_addr = (addr_t)V3_AllocPages(capacity / 4096);
+
+ blk->blk_base_addr = (addr_t)V3_AllocPages(blk->capacity / 4096);
blk->blk_space = (uint8_t *)V3_VAddr((void *)(blk->blk_base_addr));
memset(blk->blk_space, 0, capacity);
- struct vm_device * dev = v3_allocate_device(name, &dev_ops, blk);
+ struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, blk);
- if (v3_attach_device(vm, dev) == -1) {
- PrintError("Could not attach device %s\n", name);
+ if (dev == NULL) {
+ PrintError("Could not attach device %s\n", dev_id);
+ V3_Free(blk);
return -1;
}
if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"),
&blk_ops, frontend_cfg, blk) == -1) {
PrintError("Could not connect %s to frontend %s\n",
- name, v3_cfg_val(frontend_cfg, "tag"));
+ dev_id, v3_cfg_val(frontend_cfg, "tag"));
+ v3_remove_device(dev);
return -1;
}