2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm.h>
21 #include <devices/ram_hd.h>
22 #include <devices/ide.h>
28 #define PrintDebug(fmt, args...)
33 uint32_t capacity; // in bytes
35 struct vm_device * ide;
42 // HDs always read 2048 byte blocks... ?
43 static int hd_read(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
44 struct vm_device * hd_dev = (struct vm_device *)private_data;
45 struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
46 int offset = lba * IDE_SECTOR_SIZE;
47 int length = sector_count * IDE_SECTOR_SIZE;
49 // PrintDebug("Reading RAM HD at (LBA=%d) offset %d (length=%d)\n", (uint32_t)lba, offset, length);
51 memcpy(buf, (uint8_t *)(hd->disk_image + offset), length);
59 static uint64_t hd_get_capacity(void * private_data) {
60 struct vm_device * hd_dev = (struct vm_device *)private_data;
61 struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
62 PrintDebug("Querying RAM HD capacity (bytes=%d) (ret = %d)\n",
63 hd->capacity, hd->capacity / IDE_SECTOR_SIZE);
64 return hd->capacity / IDE_SECTOR_SIZE;
67 static struct v3_ide_hd_ops hd_ops = {
69 .get_capacity = hd_get_capacity,
73 static int hd_init(struct vm_device * dev) {
74 struct hd_state * hd = (struct hd_state *)(dev->private_data);
76 if (v3_ide_register_harddisk(hd->ide, hd->bus, hd->drive, "V3-RAM-HD", &hd_ops, dev) == -1) {
84 static int hd_deinit(struct vm_device * dev) {
88 static struct vm_device_ops dev_ops = {
96 struct vm_device * v3_create_ram_hd(struct vm_device * ide,
97 uint_t bus, uint_t drive,
98 addr_t ramdisk, uint32_t size) {
99 struct hd_state * hd = NULL;
101 if (size % IDE_SECTOR_SIZE) {
102 PrintError("HD image must be an integral of sector size (IDE_SECTOR_SIZE=%d)\n", IDE_SECTOR_SIZE);
106 hd = (struct hd_state *)V3_Malloc(sizeof(struct hd_state));
108 PrintDebug("Registering Ram HDD at %p (size=%d)\n", (void *)ramdisk, size);
110 hd->disk_image = ramdisk;
117 struct vm_device * hd_dev = v3_create_device("RAM-HD", &dev_ops, hd);