Revert "usb-linux: remove unreachable default in switch statement"
[qemu.git] / hw / fw_cfg.c
blob22ebb50011dea4fd459a116599103c75859b1767
1 /*
2 * QEMU Firmware configuration device emulation
4 * Copyright (c) 2008 Gleb Natapov
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "sysemu.h"
26 #include "isa.h"
27 #include "fw_cfg.h"
29 /* debug firmware config */
30 //#define DEBUG_FW_CFG
32 #ifdef DEBUG_FW_CFG
33 #define FW_CFG_DPRINTF(fmt, ...) \
34 do { printf("FW_CFG: " fmt , ## __VA_ARGS__); } while (0)
35 #else
36 #define FW_CFG_DPRINTF(fmt, ...)
37 #endif
39 #define FW_CFG_SIZE 2
41 typedef struct FWCfgEntry {
42 uint32_t len;
43 uint8_t *data;
44 void *callback_opaque;
45 FWCfgCallback callback;
46 } FWCfgEntry;
48 struct FWCfgState {
49 FWCfgEntry entries[2][FW_CFG_MAX_ENTRY];
50 FWCfgFiles *files;
51 uint16_t cur_entry;
52 uint32_t cur_offset;
55 static void fw_cfg_write(FWCfgState *s, uint8_t value)
57 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
58 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
60 FW_CFG_DPRINTF("write %d\n", value);
62 if (s->cur_entry & FW_CFG_WRITE_CHANNEL && s->cur_offset < e->len) {
63 e->data[s->cur_offset++] = value;
64 if (s->cur_offset == e->len) {
65 e->callback(e->callback_opaque, e->data);
66 s->cur_offset = 0;
71 static int fw_cfg_select(FWCfgState *s, uint16_t key)
73 int ret;
75 s->cur_offset = 0;
76 if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
77 s->cur_entry = FW_CFG_INVALID;
78 ret = 0;
79 } else {
80 s->cur_entry = key;
81 ret = 1;
84 FW_CFG_DPRINTF("select key %d (%sfound)\n", key, ret ? "" : "not ");
86 return ret;
89 static uint8_t fw_cfg_read(FWCfgState *s)
91 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
92 FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
93 uint8_t ret;
95 if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
96 ret = 0;
97 else
98 ret = e->data[s->cur_offset++];
100 FW_CFG_DPRINTF("read %d\n", ret);
102 return ret;
105 static uint32_t fw_cfg_io_readb(void *opaque, uint32_t addr)
107 return fw_cfg_read(opaque);
110 static void fw_cfg_io_writeb(void *opaque, uint32_t addr, uint32_t value)
112 fw_cfg_write(opaque, (uint8_t)value);
115 static void fw_cfg_io_writew(void *opaque, uint32_t addr, uint32_t value)
117 fw_cfg_select(opaque, (uint16_t)value);
120 static uint32_t fw_cfg_mem_readb(void *opaque, target_phys_addr_t addr)
122 return fw_cfg_read(opaque);
125 static void fw_cfg_mem_writeb(void *opaque, target_phys_addr_t addr,
126 uint32_t value)
128 fw_cfg_write(opaque, (uint8_t)value);
131 static void fw_cfg_mem_writew(void *opaque, target_phys_addr_t addr,
132 uint32_t value)
134 fw_cfg_select(opaque, (uint16_t)value);
137 static CPUReadMemoryFunc * const fw_cfg_ctl_mem_read[3] = {
138 NULL,
139 NULL,
140 NULL,
143 static CPUWriteMemoryFunc * const fw_cfg_ctl_mem_write[3] = {
144 NULL,
145 fw_cfg_mem_writew,
146 NULL,
149 static CPUReadMemoryFunc * const fw_cfg_data_mem_read[3] = {
150 fw_cfg_mem_readb,
151 NULL,
152 NULL,
155 static CPUWriteMemoryFunc * const fw_cfg_data_mem_write[3] = {
156 fw_cfg_mem_writeb,
157 NULL,
158 NULL,
161 static void fw_cfg_reset(void *opaque)
163 FWCfgState *s = opaque;
165 fw_cfg_select(s, 0);
168 /* Save restore 32 bit int as uint16_t
169 This is a Big hack, but it is how the old state did it.
170 Or we broke compatibility in the state, or we can't use struct tm
173 static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
175 uint32_t *v = pv;
176 *v = qemu_get_be16(f);
177 return 0;
180 static void put_unused(QEMUFile *f, void *pv, size_t size)
182 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
183 fprintf(stderr, "This functions shouldn't be called.\n");
186 static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
187 .name = "int32_as_uint16",
188 .get = get_uint32_as_uint16,
189 .put = put_unused,
192 #define VMSTATE_UINT16_HACK(_f, _s, _t) \
193 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
196 static bool is_version_1(void *opaque, int version_id)
198 return version_id == 1;
201 static const VMStateDescription vmstate_fw_cfg = {
202 .name = "fw_cfg",
203 .version_id = 2,
204 .minimum_version_id = 1,
205 .minimum_version_id_old = 1,
206 .fields = (VMStateField []) {
207 VMSTATE_UINT16(cur_entry, FWCfgState),
208 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
209 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
210 VMSTATE_END_OF_LIST()
214 int fw_cfg_add_bytes(FWCfgState *s, uint16_t key, uint8_t *data, uint32_t len)
216 int arch = !!(key & FW_CFG_ARCH_LOCAL);
218 key &= FW_CFG_ENTRY_MASK;
220 if (key >= FW_CFG_MAX_ENTRY)
221 return 0;
223 s->entries[arch][key].data = data;
224 s->entries[arch][key].len = len;
226 return 1;
229 int fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
231 uint16_t *copy;
233 copy = qemu_malloc(sizeof(value));
234 *copy = cpu_to_le16(value);
235 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
238 int fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
240 uint32_t *copy;
242 copy = qemu_malloc(sizeof(value));
243 *copy = cpu_to_le32(value);
244 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
247 int fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
249 uint64_t *copy;
251 copy = qemu_malloc(sizeof(value));
252 *copy = cpu_to_le64(value);
253 return fw_cfg_add_bytes(s, key, (uint8_t *)copy, sizeof(value));
256 int fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback,
257 void *callback_opaque, uint8_t *data, size_t len)
259 int arch = !!(key & FW_CFG_ARCH_LOCAL);
261 if (!(key & FW_CFG_WRITE_CHANNEL))
262 return 0;
264 key &= FW_CFG_ENTRY_MASK;
266 if (key >= FW_CFG_MAX_ENTRY || len > 65535)
267 return 0;
269 s->entries[arch][key].data = data;
270 s->entries[arch][key].len = len;
271 s->entries[arch][key].callback_opaque = callback_opaque;
272 s->entries[arch][key].callback = callback;
274 return 1;
277 int fw_cfg_add_file(FWCfgState *s, const char *dir, const char *filename,
278 uint8_t *data, uint32_t len)
280 const char *basename;
281 int i, index;
283 if (!s->files) {
284 int dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS;
285 s->files = qemu_mallocz(dsize);
286 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, (uint8_t*)s->files, dsize);
289 index = be32_to_cpu(s->files->count);
290 if (index == FW_CFG_FILE_SLOTS) {
291 fprintf(stderr, "fw_cfg: out of file slots\n");
292 return 0;
295 fw_cfg_add_bytes(s, FW_CFG_FILE_FIRST + index, data, len);
297 basename = strrchr(filename, '/');
298 if (basename) {
299 basename++;
300 } else {
301 basename = filename;
304 snprintf(s->files->f[index].name, sizeof(s->files->f[index].name),
305 "%s/%s", dir, basename);
306 for (i = 0; i < index; i++) {
307 if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
308 FW_CFG_DPRINTF("%s: skip duplicate: %s\n", __FUNCTION__,
309 s->files->f[index].name);
310 return 1;
314 s->files->f[index].size = cpu_to_be32(len);
315 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
316 FW_CFG_DPRINTF("%s: #%d: %s (%d bytes)\n", __FUNCTION__,
317 index, s->files->f[index].name, len);
319 s->files->count = cpu_to_be32(index+1);
320 return 1;
323 FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
324 target_phys_addr_t ctl_addr, target_phys_addr_t data_addr)
326 FWCfgState *s;
327 int io_ctl_memory, io_data_memory;
329 s = qemu_mallocz(sizeof(FWCfgState));
331 if (ctl_port) {
332 register_ioport_write(ctl_port, 2, 2, fw_cfg_io_writew, s);
334 if (data_port) {
335 register_ioport_read(data_port, 1, 1, fw_cfg_io_readb, s);
336 register_ioport_write(data_port, 1, 1, fw_cfg_io_writeb, s);
338 if (ctl_addr) {
339 io_ctl_memory = cpu_register_io_memory(fw_cfg_ctl_mem_read,
340 fw_cfg_ctl_mem_write, s);
341 cpu_register_physical_memory(ctl_addr, FW_CFG_SIZE, io_ctl_memory);
343 if (data_addr) {
344 io_data_memory = cpu_register_io_memory(fw_cfg_data_mem_read,
345 fw_cfg_data_mem_write, s);
346 cpu_register_physical_memory(data_addr, FW_CFG_SIZE, io_data_memory);
348 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (uint8_t *)"QEMU", 4);
349 fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
350 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
351 fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
352 fw_cfg_add_i16(s, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
353 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
355 vmstate_register(-1, &vmstate_fw_cfg, s);
356 qemu_register_reset(fw_cfg_reset, s);
358 return s;