#include <palacios/vmm_dev_mgr.h>
-#ifndef CONFIG_DEBUG_RAMDISK
+#ifndef V3_CONFIG_DEBUG_RAMDISK
#undef PrintDebug
#define PrintDebug(fmt, args...)
#endif
PrintDebug("Reading %d bytes from %p to %p\n", (uint32_t)num_bytes, (uint8_t *)(disk->disk_image + lba), buf);
+ if (lba + num_bytes > disk->capacity) {
+ PrintError("read out of bounds: lba=%llu (%p), num_bytes=%llu, capacity=%d (%p)\n",
+ lba, (void *)(addr_t)lba, num_bytes, disk->capacity, (void *)(addr_t)disk->capacity);
+ return -1;
+ }
+
memcpy(buf, (uint8_t *)(disk->disk_image + lba), num_bytes);
return 0;
PrintDebug("Writing %d bytes from %p to %p\n", (uint32_t)num_bytes, buf, (uint8_t *)(disk->disk_image + lba));
+ if (lba + num_bytes > disk->capacity) {
+ PrintError("write out of bounds: lba=%llu (%p), num_bytes=%llu, capacity=%d (%p)\n",
+ lba, (void *)(addr_t)lba, num_bytes, disk->capacity, (void *)(addr_t)disk->capacity);
+ return -1;
+ }
+
+
memcpy((uint8_t *)(disk->disk_image + lba), buf, num_bytes);
return 0;
-static int disk_free(struct vm_device * dev) {
+static int disk_free(struct disk_state * state) {
+
+ V3_Free(state);
return 0;
}
static struct v3_device_ops dev_ops = {
- .free = disk_free,
+ .free = (int (*)(void *))disk_free,
};
disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
+
+ if (!disk) {
+ PrintError("Cannot allocate in init\n");
+ return -1;
+ }
+
memset(disk, 0, sizeof(struct disk_state));
disk->disk_image = file->data;