1 #include <devices/nvram.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_types.h>
5 extern struct vmm_os_hooks *os_hooks;
8 #define NVRAM_REG_PORT 0x70
9 #define NVRAM_DATA_PORT 0x71
13 typedef enum {NVRAM_READY, NVRAM_REG_POSTED} nvram_state_t;
16 #define NVRAM_REG_MAX 256
19 // These are borrowed from Bochs, which borrowed from
20 // Ralf Brown's interupt list
21 #define NVRAM_REG_SEC 0x00
22 #define NVRAM_REG_SEC_ALARM 0x01
23 #define NVRAM_REG_MIN 0x02
24 #define NVRAM_REG_MIN_ALARM 0x03
25 #define NVRAM_REG_HOUR 0x04
26 #define NVRAM_REG_HOUR_ALARM 0x05
27 #define NVRAM_REG_WEEK_DAY 0x06
28 #define NVRAM_REG_MONTH_DAY 0x07
29 #define NVRAM_REG_MONTH 0x08
30 #define NVRAM_REG_YEAR 0x09
31 #define NVRAM_REG_STAT_A 0x0a
32 #define NVRAM_REG_STAT_B 0x0b
33 #define NVRAM_REG_STAT_C 0x0c
34 #define NVRAM_REG_STAT_D 0x0d
35 #define NVRAM_REG_DIAGNOSTIC_STATUS 0x0e
36 #define NVRAM_REG_SHUTDOWN_STATUS 0x0f
37 #define NVRAM_REG_EQUIPMENT_BYTE 0x14
38 #define NVRAM_REG_CSUM_HIGH 0x2e
39 #define NVRAM_REG_CSUM_LOW 0x2f
40 #define NVRAM_REG_IBM_CENTURY_BYTE 0x32
41 #define NVRAM_REG_IBM_PS2_CENTURY_BYTE 0x37
45 struct nvram_internal {
46 nvram_state_t dev_state;
48 uchar_t mem_state[NVRAM_REG_MAX];
55 int nvram_reset_device(struct vm_device * dev)
57 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
59 data->dev_state = NVRAM_READY;
70 int nvram_start_device(struct vm_device *dev)
76 int nvram_stop_device(struct vm_device *dev)
84 int nvram_write_reg_port(ushort_t port,
87 struct vm_device * dev)
89 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
91 memcpy(&(data->thereg), src, 1);
97 int nvram_read_data_port(ushort_t port,
100 struct vm_device * dev)
102 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
104 memcpy(dst, &(data->mem_state[data->thereg]), 1);
109 int nvram_write_data_port(ushort_t port,
112 struct vm_device * dev)
114 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
116 memcpy(&(data->mem_state[data->thereg]), src, 1);
123 int nvram_init_device(struct vm_device * dev) {
124 struct nvram_internal *data = (struct nvram_internal *) dev->private_data;
126 memset(data->mem_state, 0, NVRAM_REG_MAX);
128 nvram_reset_device(dev);
131 dev_hook_io(dev, NVRAM_REG_PORT, NULL, &nvram_write_reg_port);
132 dev_hook_io(dev, NVRAM_DATA_PORT, &nvram_read_data_port, &nvram_write_data_port);
137 int nvram_deinit_device(struct vm_device *dev)
141 dev_unhook_io(dev, NVRAM_REG_PORT);
142 dev_unhook_io(dev, NVRAM_DATA_PORT);
144 nvram_reset_device(dev);
152 static struct vm_device_ops dev_ops = {
153 .init = nvram_init_device,
154 .deinit = nvram_deinit_device,
155 .reset = nvram_reset_device,
156 .start = nvram_start_device,
157 .stop = nvram_stop_device,
162 struct vm_device *create_nvram() {
163 struct nvram_internal * nvram_state = os_hooks->malloc(sizeof(struct nvram_internal));
165 struct vm_device *device = create_device("NVRAM", &dev_ops, nvram_state);