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.


various changes...
[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 <palacios/vmm_dev_mgr.h>
23 #include <devices/ide.h>
24
25 #ifndef CONFIG_DEBUG_IDE
26 #undef PrintDebug
27 #define PrintDebug(fmt, args...)
28 #endif
29
30
31 struct cd_state {
32     addr_t disk_image;
33     uint32_t capacity; // in bytes
34
35     struct vm_device * ide;
36
37     uint_t bus;
38     uint_t drive;
39 };
40
41
42 // CDs always read 2048 byte blocks... ?
43 static int cd_read(uint8_t * buf, int block_count, uint64_t lba,  void * private_data) {
44     struct vm_device * cd_dev = (struct vm_device *)private_data;
45     struct cd_state * cd = (struct cd_state *)(cd_dev->private_data);
46     int offset = lba * ATAPI_BLOCK_SIZE;
47     int length = block_count * ATAPI_BLOCK_SIZE;
48
49     PrintDebug("Reading RAM CD at (LBA=%d) offset %d (length=%d)\n", (uint32_t)lba, offset, length);
50
51     memcpy(buf, (uint8_t *)(cd->disk_image + offset), length);
52
53     return 0;
54 }
55
56
57 static uint32_t cd_get_capacity(void * private_data) {
58     struct vm_device * cd_dev = (struct vm_device *)private_data;
59     struct cd_state * cd = (struct cd_state *)(cd_dev->private_data);
60     PrintDebug("Querying RAM CD capacity (bytes=%d) (ret = %d)\n", 
61                cd->capacity, cd->capacity  / ATAPI_BLOCK_SIZE);
62     return cd->capacity / ATAPI_BLOCK_SIZE;
63 }
64
65 static struct v3_cd_ops cd_ops = {
66     .read = cd_read, 
67     .get_capacity = cd_get_capacity,
68 };
69
70
71
72
73 static int cd_free(struct vm_device * dev) {
74     return 0;
75 }
76
77 static struct v3_device_ops dev_ops = {
78     .free = cd_free,
79     .reset = NULL,
80     .start = NULL,
81     .stop = NULL,
82 };
83
84
85 static int cd_init(struct guest_info * vm, void * cfg_data) {
86     struct cd_state * cd = NULL;
87     struct ram_cd_cfg * cfg = (struct ram_cd_cfg *)cfg_data;
88
89     if (cfg->size % ATAPI_BLOCK_SIZE) {
90         PrintError("CD image must be an integral of block size (ATAPI_BLOCK_SIZE=%d)\n", ATAPI_BLOCK_SIZE);
91         return -1;
92     }
93
94     cd = (struct cd_state *)V3_Malloc(sizeof(struct cd_state));
95
96     PrintDebug("Registering Ram CD at %p (size=%d)\n", (void *)cfg->ramdisk, cfg->size);
97
98   
99     cd->disk_image = cfg->ramdisk;
100     cd->capacity = cfg->size;
101
102     cd->ide = v3_find_dev(vm, cfg->ide);
103
104     if (cd->ide == 0) {
105         PrintError("Could not find backend %s\n", cfg->ide);
106         return -1;
107     }
108
109     cd->bus = cfg->bus;
110     cd->drive = cfg->drive;
111         
112     struct vm_device * dev = v3_allocate_device("RAM-CD", &dev_ops, cd);
113
114     if (v3_attach_device(vm, dev) == -1) {
115         PrintError("Could not attach device %s\n", "RAM-CD");
116         return -1;
117     }
118
119
120     if (v3_ide_register_cdrom(cd->ide, cd->bus, cd->drive, "RAM-CD", &cd_ops, dev) == -1) {
121         return -1;
122     }
123     
124     return 0;
125 }
126
127
128 device_register("RAM-CD", cd_init)