Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


added forgotten files...
[palacios.git] / palacios / src / devices / ram_cd.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm.h>
21 #include <devices/ram_cd.h>
22 #include <devices/ide.h>
23
24 #ifndef DEBUG_IDE
25 #undef PrintDebug
26 #define PrintDebug(fmt, args...)
27 #endif
28
29
30 struct cd_state {
31     addr_t disk_image;
32     uint32_t capacity; // in bytes
33
34     struct vm_device * ide;
35
36     uint_t bus;
37     uint_t drive;
38 };
39
40
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;
47
48     PrintDebug("Reading RAM CD at (LBA=%d) offset %d (length=%d)\n", (uint32_t)lba, offset, length);
49
50     memcpy(buf, (uint8_t *)(cd->disk_image + offset), length);
51
52     return 0;
53 }
54
55
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;
62 }
63
64 static struct v3_ide_cd_ops cd_ops = {
65     .read = cd_read, 
66     .get_capacity = cd_get_capacity,
67 };
68
69
70 static int cd_init(struct vm_device * dev) {
71     struct cd_state * cd = (struct cd_state *)(dev->private_data);
72
73     if (v3_ide_register_cdrom(cd->ide, cd->bus, cd->drive, "RAM-CD", &cd_ops, dev) == -1) {
74         return -1;
75     }
76     
77     return 0;
78 }
79
80
81 static int cd_deinit(struct vm_device * dev) {
82     return 0;
83 }
84
85 static struct vm_device_ops dev_ops = {
86     .init = cd_init, 
87     .deinit = cd_deinit,
88     .reset = NULL,
89     .start = NULL,
90     .stop = NULL,
91 };
92
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;
97
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);
100         return NULL;
101     }
102
103     cd = (struct cd_state *)V3_Malloc(sizeof(struct cd_state));
104
105     PrintDebug("Registering Ram CD at %p (size=%d)\n", (void *)ramdisk, size);
106
107   
108     cd->disk_image = ramdisk;
109     cd->capacity = size;
110
111     cd->ide = ide;
112     cd->bus = bus;
113     cd->drive = drive;
114         
115     struct vm_device * cd_dev = v3_create_device("RAM-CD", &dev_ops, cd);
116
117     return cd_dev;
118 }