1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "hw/sysbus.h"
27#include "trace.h"
28
29typedef struct {
30 MemoryRegion iomem;
31 uint32_t chip_size;
32 char *filename;
33 FILE *file;
34 uint8_t *contents;
35} NvRamState;
36
37static uint64_t nvram_read(void *opaque, hwaddr addr, unsigned size)
38{
39 NvRamState *s = opaque;
40 uint32_t val;
41
42 val = s->contents[addr];
43 trace_nvram_read(addr, val);
44 return val;
45}
46
47static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
48 unsigned size)
49{
50 NvRamState *s = opaque;
51
52 val &= 0xff;
53 trace_nvram_write(addr, s->contents[addr], val);
54
55 s->contents[addr] = val;
56 if (s->file) {
57 fseek(s->file, addr, SEEK_SET);
58 fputc(val, s->file);
59 fflush(s->file);
60 }
61}
62
63static const MemoryRegionOps nvram_ops = {
64 .read = nvram_read,
65 .write = nvram_write,
66 .impl = {
67 .min_access_size = 1,
68 .max_access_size = 1,
69 },
70 .endianness = DEVICE_LITTLE_ENDIAN,
71};
72
73static int nvram_post_load(void *opaque, int version_id)
74{
75 NvRamState *s = opaque;
76
77
78 if (s->file) {
79 fclose(s->file);
80 }
81
82
83 s->file = fopen(s->filename, "wb");
84 if (s->file) {
85
86 if (fwrite(s->contents, s->chip_size, 1, s->file) != 1) {
87 printf("nvram_post_load: short write\n");
88 }
89 fflush(s->file);
90 }
91
92 return 0;
93}
94
95static const VMStateDescription vmstate_nvram = {
96 .name = "nvram",
97 .version_id = 0,
98 .minimum_version_id = 0,
99 .post_load = nvram_post_load,
100 .fields = (VMStateField[]) {
101 VMSTATE_VARRAY_UINT32(contents, NvRamState, chip_size, 0,
102 vmstate_info_uint8, uint8_t),
103 VMSTATE_END_OF_LIST()
104 }
105};
106
107#define TYPE_DS1225Y "ds1225y"
108#define DS1225Y(obj) OBJECT_CHECK(SysBusNvRamState, (obj), TYPE_DS1225Y)
109
110typedef struct {
111 SysBusDevice parent_obj;
112
113 NvRamState nvram;
114} SysBusNvRamState;
115
116static int nvram_sysbus_initfn(SysBusDevice *dev)
117{
118 SysBusNvRamState *sys = DS1225Y(dev);
119 NvRamState *s = &sys->nvram;
120 FILE *file;
121
122 s->contents = g_malloc0(s->chip_size);
123
124 memory_region_init_io(&s->iomem, OBJECT(s), &nvram_ops, s,
125 "nvram", s->chip_size);
126 sysbus_init_mmio(dev, &s->iomem);
127
128
129 file = fopen(s->filename, "rb");
130 if (file) {
131
132 if (fread(s->contents, s->chip_size, 1, file) != 1) {
133 printf("nvram_sysbus_initfn: short read\n");
134 }
135 fclose(file);
136 }
137 nvram_post_load(s, 0);
138
139 return 0;
140}
141
142static Property nvram_sysbus_properties[] = {
143 DEFINE_PROP_UINT32("size", SysBusNvRamState, nvram.chip_size, 0x2000),
144 DEFINE_PROP_STRING("filename", SysBusNvRamState, nvram.filename),
145 DEFINE_PROP_END_OF_LIST(),
146};
147
148static void nvram_sysbus_class_init(ObjectClass *klass, void *data)
149{
150 DeviceClass *dc = DEVICE_CLASS(klass);
151 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
152
153 k->init = nvram_sysbus_initfn;
154 dc->vmsd = &vmstate_nvram;
155 dc->props = nvram_sysbus_properties;
156}
157
158static const TypeInfo nvram_sysbus_info = {
159 .name = TYPE_DS1225Y,
160 .parent = TYPE_SYS_BUS_DEVICE,
161 .instance_size = sizeof(SysBusNvRamState),
162 .class_init = nvram_sysbus_class_init,
163};
164
165static void nvram_register_types(void)
166{
167 type_register_static(&nvram_sysbus_info);
168}
169
170type_init(nvram_register_types)
171