1 #include <devices/nvram.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_types.h>
5 extern struct vmm_os_hooks *os_hooks;
7 extern void SerialPrint(const char *format, ...);
9 #define NVRAM_REG_PORT 0x70
10 #define NVRAM_DATA_PORT 0x71
14 typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
17 #define NVRAM_REG_MAX 256
20 // These are borrowed from Bochs, which borrowed from
21 // Ralf Brown's interupt list, and extended
22 #define NVRAM_REG_SEC 0x00
23 #define NVRAM_REG_SEC_ALARM 0x01
24 #define NVRAM_REG_MIN 0x02
25 #define NVRAM_REG_MIN_ALARM 0x03
26 #define NVRAM_REG_HOUR 0x04
27 #define NVRAM_REG_HOUR_ALARM 0x05
28 #define NVRAM_REG_WEEK_DAY 0x06
29 #define NVRAM_REG_MONTH_DAY 0x07
30 #define NVRAM_REG_MONTH 0x08
31 #define NVRAM_REG_YEAR 0x09
32 #define NVRAM_REG_STAT_A 0x0a
33 #define NVRAM_REG_STAT_B 0x0b
34 #define NVRAM_REG_STAT_C 0x0c
35 #define NVRAM_REG_STAT_D 0x0d
36 #define NVRAM_REG_DIAGNOSTIC_STATUS 0x0e
37 #define NVRAM_REG_SHUTDOWN_STATUS 0x0f
39 #define NVRAM_IBM_HD_DATA 0x12
41 #define NVRAM_REG_FLOPPY_TYPE 0x10
42 #define NVRAM_REG_EQUIPMENT_BYTE 0x14
44 #define NVRAM_REG_BASE_MEMORY_HIGH 0x16
45 #define NVRAM_REG_BASE_MEMORY_LOW 0x15
47 #define NVRAM_REG_EXT_MEMORY_HIGH 0x18
48 #define NVRAM_REG_EXT_MEMORY_LOW 0x17
50 #define NVRAM_REG_EXT_MEMORY_2ND_HIGH 0x31
51 #define NVRAM_REG_EXT_MEMORY_2ND_LOW 0x30
53 #define NVRAM_REG_BOOTSEQ_OLD 0x2d
55 #define NVRAM_REG_AMI_BIG_MEMORY_HIGH 0x35
56 #define NVRAM_REG_AMI_BIG_MEMORY_LOW 0x34
59 #define NVRAM_REG_CSUM_HIGH 0x2e
60 #define NVRAM_REG_CSUM_LOW 0x2f
61 #define NVRAM_REG_IBM_CENTURY_BYTE 0x32
62 #define NVRAM_REG_IBM_PS2_CENTURY_BYTE 0x37
64 #define NVRAM_REG_BOOTSEQ_NEW_FIRST 0x3D
65 #define NVRAM_REG_BOOTSEQ_NEW_SECOND 0x38
68 struct nvram_internal {
69 nvram_state_t dev_state;
71 uchar_t mem_state[NVRAM_REG_MAX];
76 static int set_nvram_defaults(struct vm_device *dev)
78 struct nvram_internal * nvram_state = (struct nvram_internal*) dev->private_data;
81 // 2 1.44 MB floppy drives
84 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE]= 0x44;
86 nvram_state->mem_state[NVRAM_REG_FLOPPY_TYPE] = 0x00;
90 // For old boot sequence style, do floppy first
92 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_OLD]= 0x10;
95 // For new boot sequence style, do floppy, cd, then hd
96 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST]= 0x31;
97 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND]= 0x20;
100 // For new boot sequence style, do cd, hd, floppy
101 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_FIRST]= 0x23;
102 nvram_state->mem_state[NVRAM_REG_BOOTSEQ_NEW_SECOND]= 0x10;
105 // Set equipment byte to note 2 floppies, vga display, keyboard,math,floppy
106 nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE]= 0x4f;
107 //nvram_state->mem_state[NVRAM_REG_EQUIPMENT_BYTE] = 0xf;
109 // Set conventional memory to 640K
110 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_HIGH]= 0x02;
111 nvram_state->mem_state[NVRAM_REG_BASE_MEMORY_LOW]= 0x80;
113 // Set extended memory to 15 MB
114 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_HIGH]= 0x3C;
115 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_LOW]= 0x00;
116 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_HIGH]= 0x3C;
117 nvram_state->mem_state[NVRAM_REG_EXT_MEMORY_2ND_LOW]= 0x00;
119 // Set the extended memory beyond 16 MB to 128-16 MB
120 // nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x7;
121 //nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
123 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_HIGH]= 0x00;
124 nvram_state->mem_state[NVRAM_REG_AMI_BIG_MEMORY_LOW]= 0x00;
127 // This is the harddisk type.... Set accordingly...
128 nvram_state->mem_state[NVRAM_IBM_HD_DATA] = 0x20;
130 // Set the shutdown status gently
132 nvram_state->mem_state[NVRAM_REG_SHUTDOWN_STATUS] = 0x0;
136 // time update in progress, default timebase (32KHz, default interrupt rate 1KHz)
138 nvram_state->mem_state[NVRAM_REG_STAT_A] = 0xa6;
141 // time updates, default timebase (32KHz, default interrupt rate 1KHz)
143 //nvram_state->mem_state[NVRAM_REG_STAT_B] = 0xa6;
152 int nvram_reset_device(struct vm_device * dev)
154 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
156 SerialPrint("nvram: reset device\n");
160 data->dev_state = NVRAM_READY;
172 int nvram_start_device(struct vm_device *dev)
174 SerialPrint("nvram: start device\n");
179 int nvram_stop_device(struct vm_device *dev)
181 SerialPrint("nvram: stop device\n");
188 int nvram_write_reg_port(ushort_t port,
191 struct vm_device * dev)
193 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
195 memcpy(&(data->thereg), src, 1);
196 PrintDebug("Writing To NVRAM reg: 0x%x\n", data->thereg);
202 int nvram_read_data_port(ushort_t port,
205 struct vm_device * dev)
207 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
211 memcpy(dst, &(data->mem_state[data->thereg]), 1);
213 PrintDebug("nvram_read_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
216 if (data->thereg==NVRAM_REG_STAT_A) {
217 data->mem_state[data->thereg] ^= 0x80; // toggle Update in progess
224 int nvram_write_data_port(ushort_t port,
227 struct vm_device * dev)
229 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
231 memcpy(&(data->mem_state[data->thereg]), src, 1);
233 PrintDebug("nvram_write_data_port(0x%x)=0x%x\n", data->thereg, data->mem_state[data->thereg]);
240 int nvram_init_device(struct vm_device * dev) {
242 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
244 SerialPrint("nvram: init_device\n");
246 memset(data->mem_state, 0, NVRAM_REG_MAX);
248 // Would read state here
249 set_nvram_defaults(dev);
251 nvram_reset_device(dev);
254 dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
255 dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
260 int nvram_deinit_device(struct vm_device *dev)
264 dev_unhook_io(dev, NVRAM_REG_PORT);
265 dev_unhook_io(dev, NVRAM_DATA_PORT);
267 nvram_reset_device(dev);
275 static struct vm_device_ops dev_ops = {
276 .init = nvram_init_device,
277 .deinit = nvram_deinit_device,
278 .reset = nvram_reset_device,
279 .start = nvram_start_device,
280 .stop = nvram_stop_device,
286 struct vm_device *create_nvram() {
287 struct nvram_internal * nvram_state = os_hooks->malloc(sizeof(struct nvram_internal)+1000);
289 SerialPrint("internal at %x\n",nvram_state);
291 struct vm_device *device = create_device("NVRAM", &dev_ops, nvram_state);