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_cd.h>
22 #include <devices/ide.h>
26 #define PrintDebug(fmt, args...)
32 uint32_t capacity; // in bytes
34 struct vm_device * ide;
41 // CDs always read 2048 byte blocks... ?
42 static int cd_read(uint8_t * buf, int block_count, uint64_t lba, void * private_data) {
43 struct vm_device * cd_dev = (struct vm_device *)private_data;
44 struct cd_state * cd = (struct cd_state *)(cd_dev->private_data);
45 int offset = lba * ATAPI_BLOCK_SIZE;
46 int length = block_count * ATAPI_BLOCK_SIZE;
48 PrintDebug("Reading RAM CD at (LBA=%d) offset %d (length=%d)\n", (uint32_t)lba, offset, length);
50 memcpy(buf, (uint8_t *)(cd->disk_image + offset), length);
56 static uint32_t cd_get_capacity(void * private_data) {
57 struct vm_device * cd_dev = (struct vm_device *)private_data;
58 struct cd_state * cd = (struct cd_state *)(cd_dev->private_data);
59 PrintDebug("Querying RAM CD capacity (bytes=%d) (ret = %d)\n",
60 cd->capacity, cd->capacity / ATAPI_BLOCK_SIZE);
61 return cd->capacity / ATAPI_BLOCK_SIZE;
64 static struct v3_ide_cd_ops cd_ops = {
66 .get_capacity = cd_get_capacity,
70 static int cd_init(struct vm_device * dev) {
71 struct cd_state * cd = (struct cd_state *)(dev->private_data);
73 if (v3_ide_register_cdrom(cd->ide, cd->bus, cd->drive, "RAM-CD", &cd_ops, dev) == -1) {
81 static int cd_deinit(struct vm_device * dev) {
85 static struct vm_device_ops dev_ops = {
93 struct vm_device * v3_create_ram_cd(struct vm_device * ide,
94 uint_t bus, uint_t drive,
95 addr_t ramdisk, uint32_t size) {
96 struct cd_state * cd = NULL;
98 if (size % ATAPI_BLOCK_SIZE) {
99 PrintError("CD image must be an integral of block size (ATAPI_BLOCK_SIZE=%d)\n", ATAPI_BLOCK_SIZE);
103 cd = (struct cd_state *)V3_Malloc(sizeof(struct cd_state));
105 PrintDebug("Registering Ram CD at %p (size=%d)\n", (void *)ramdisk, size);
108 cd->disk_image = ramdisk;
115 struct vm_device * cd_dev = v3_create_device("RAM-CD", &dev_ops, cd);