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, Zheng Cui <cuizheng@cs.unm.edu>
11 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
12 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
13 * All rights reserved.
15 * Author: Zheng Cui <cuizheng@cs.unm.edu>
17 * This is free software. You are permitted to use,
18 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <devices/cdrom.h>
23 #include <devices/ide.h>
24 #include <palacios/vmm.h>
28 #define PrintDebug(fmt, args...)
34 uchar_t * image_addr; //memory address
35 ulong_t capacity_in_bytes;
36 ulong_t head; //current position
38 struct vm_device * ide_dev;
47 * Load CD-ROM. Returns false if CD is not ready.
50 static rd_bool cdrom_insert(void * private_data) {
51 PrintDebug("[cdrom_insert]\n");
56 * Logically eject the CD.
58 static void cdrom_eject(void * private_data) {
59 PrintDebug("[cdrom_eject]\n");
64 * Read CD TOC. Returns false if start track is out of bounds.
66 static rd_bool cdrom_read_toc(void * private_data, uint8_t* buf, int* length, rd_bool msf, int start_track)
68 PrintDebug("[cdrom_read_toc]\n");
73 * Return CD-ROM capacity (in 2048 byte frames)
75 static uint32_t cdrom_capacity(void * private_data) {
76 struct cdrom_state * cdrom = (struct cdrom_state *)private_data;
78 PrintDebug("[cdrom_capacity] s_ramdiskSize = %d\n", cdrom->capacity_in_bytes);
81 if (cdrom->capacity_in_bytes % 2048) {
82 PrintDebug("\t\t capacity in LBA is %d\n", (cdrom->capacity_in_bytes / 2048) + 1);
83 return (cdrom->capacity_in_bytes / 2048) + 1;
85 PrintDebug("\t\t capacity in LBA is %d\n", cdrom->capacity_in_bytes / 2048);
86 return cdrom->capacity_in_bytes / 2048;
89 PrintError("Unsupported CDROM mode in capacity query\n");
96 * Read a single block from the CD
98 static void cdrom_read_block(void * private_data, uint8_t * buf, int lba)/* __attribute__(regparm(2)); */ {
99 struct cdrom_state * cdrom = (struct cdrom_state *)private_data;
103 PrintDebug("[cdrom_read_block] lba = %d (cdrom_image_start=%x)\n", lba, cdrom->image_addr);
104 memcpy(buf, (uchar_t *)(cdrom->image_addr + lba * 2048), 2048);
105 PrintDebug("Returning from read block\n");
109 static void set_LBA(void * private_data, uchar_t lba) {
110 struct cdrom_state * cdrom = (struct cdrom_state *)private_data;
116 * Start (spin up) the CD.
118 static int cdrom_start(void * private_data) {
119 PrintDebug("[cdrom_start]\n");
124 static struct cdrom_ops cd_ops = {
125 .insert_cdrom = cdrom_insert,
126 .eject_cdrom = cdrom_eject,
127 .read_toc = cdrom_read_toc,
128 .capacity = cdrom_capacity,
129 .read_block = cdrom_read_block,
130 .start_cdrom = cdrom_start,
137 static int cdrom_device_init(struct vm_device * dev) {
138 struct cdrom_state * cdrom = (struct cdrom_state *)dev->private_data;
139 PrintDebug("[cdrom_init]\n");
140 PrintDebug("CDIMAGE located at: %x\n", cdrom->image_addr);
145 v3_ramdisk_register_cdrom(cdrom->ide_dev, 1, 0, &cd_ops, cdrom);
151 static int cdrom_device_deinit(struct vm_device * dev) {
155 static struct vm_device_ops dev_ops = {
156 .init = cdrom_device_init,
157 .deinit = cdrom_device_deinit,
163 struct vm_device * v3_create_cdrom(struct vm_device * ramdisk_dev, void * ramdisk, uint_t ramdisk_size){
164 struct cdrom_state * cd = (struct cdrom_state *)V3_Malloc(sizeof(struct cdrom_state));
165 V3_ASSERT(cd != NULL);
167 memset(cd, 0, sizeof(struct cdrom_state));
169 cd->image_addr = (uchar_t *)ramdisk;
170 cd->capacity_in_bytes = ramdisk_size;
171 cd->ide_dev = ramdisk_dev;
173 PrintDebug("Creating RamDISK CDROM\n");
175 struct vm_device * cd_dev = create_device("Ram Based CD", &dev_ops, cd);