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
29 /* debug firmware config */
30 //#define DEBUG_FW_CFG
33 #define FW_CFG_DPRINTF(fmt, ...) \
34 do { printf("FW_CFG: " fmt , ## __VA_ARGS__); } while (0)
36 #define FW_CFG_DPRINTF(fmt, ...)
41 typedef struct _FWCfgEntry
{
44 void *callback_opaque
;
45 FWCfgCallback callback
;
48 typedef struct _FWCfgState
{
49 FWCfgEntry entries
[2][FW_CFG_MAX_ENTRY
];
54 static void fw_cfg_write(FWCfgState
*s
, uint8_t value
)
56 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
57 FWCfgEntry
*e
= &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
59 FW_CFG_DPRINTF("write %d\n", value
);
61 if (s
->cur_entry
& FW_CFG_WRITE_CHANNEL
&& s
->cur_offset
< e
->len
) {
62 e
->data
[s
->cur_offset
++] = value
;
63 if (s
->cur_offset
== e
->len
) {
64 e
->callback(e
->callback_opaque
, e
->data
);
70 static int fw_cfg_select(FWCfgState
*s
, uint16_t key
)
75 if ((key
& FW_CFG_ENTRY_MASK
) >= FW_CFG_MAX_ENTRY
) {
76 s
->cur_entry
= FW_CFG_INVALID
;
83 FW_CFG_DPRINTF("select key %d (%sfound)\n", key
, ret
? "" : "not ");
88 static uint8_t fw_cfg_read(FWCfgState
*s
)
90 int arch
= !!(s
->cur_entry
& FW_CFG_ARCH_LOCAL
);
91 FWCfgEntry
*e
= &s
->entries
[arch
][s
->cur_entry
& FW_CFG_ENTRY_MASK
];
94 if (s
->cur_entry
== FW_CFG_INVALID
|| !e
->data
|| s
->cur_offset
>= e
->len
)
97 ret
= e
->data
[s
->cur_offset
++];
99 FW_CFG_DPRINTF("read %d\n", ret
);
104 static uint32_t fw_cfg_io_readb(void *opaque
, uint32_t addr
)
106 return fw_cfg_read(opaque
);
109 static void fw_cfg_io_writeb(void *opaque
, uint32_t addr
, uint32_t value
)
111 fw_cfg_write(opaque
, (uint8_t)value
);
114 static void fw_cfg_io_writew(void *opaque
, uint32_t addr
, uint32_t value
)
116 fw_cfg_select(opaque
, (uint16_t)value
);
119 static uint32_t fw_cfg_mem_readb(void *opaque
, target_phys_addr_t addr
)
121 return fw_cfg_read(opaque
);
124 static void fw_cfg_mem_writeb(void *opaque
, target_phys_addr_t addr
,
127 fw_cfg_write(opaque
, (uint8_t)value
);
130 static void fw_cfg_mem_writew(void *opaque
, target_phys_addr_t addr
,
133 fw_cfg_select(opaque
, (uint16_t)value
);
136 static CPUReadMemoryFunc
* const fw_cfg_ctl_mem_read
[3] = {
142 static CPUWriteMemoryFunc
* const fw_cfg_ctl_mem_write
[3] = {
148 static CPUReadMemoryFunc
* const fw_cfg_data_mem_read
[3] = {
154 static CPUWriteMemoryFunc
* const fw_cfg_data_mem_write
[3] = {
160 static void fw_cfg_reset(void *opaque
)
162 FWCfgState
*s
= opaque
;
167 static void fw_cfg_save(QEMUFile
*f
, void *opaque
)
169 FWCfgState
*s
= opaque
;
171 qemu_put_be16s(f
, &s
->cur_entry
);
172 qemu_put_be16s(f
, &s
->cur_offset
);
175 static int fw_cfg_load(QEMUFile
*f
, void *opaque
, int version_id
)
177 FWCfgState
*s
= opaque
;
182 qemu_get_be16s(f
, &s
->cur_entry
);
183 qemu_get_be16s(f
, &s
->cur_offset
);
188 int fw_cfg_add_bytes(void *opaque
, uint16_t key
, uint8_t *data
, uint16_t len
)
190 FWCfgState
*s
= opaque
;
191 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
193 key
&= FW_CFG_ENTRY_MASK
;
195 if (key
>= FW_CFG_MAX_ENTRY
)
198 s
->entries
[arch
][key
].data
= data
;
199 s
->entries
[arch
][key
].len
= len
;
204 int fw_cfg_add_i16(void *opaque
, uint16_t key
, uint16_t value
)
208 copy
= qemu_malloc(sizeof(value
));
209 *copy
= cpu_to_le16(value
);
210 return fw_cfg_add_bytes(opaque
, key
, (uint8_t *)copy
, sizeof(value
));
213 int fw_cfg_add_i32(void *opaque
, uint16_t key
, uint32_t value
)
217 copy
= qemu_malloc(sizeof(value
));
218 *copy
= cpu_to_le32(value
);
219 return fw_cfg_add_bytes(opaque
, key
, (uint8_t *)copy
, sizeof(value
));
222 int fw_cfg_add_i64(void *opaque
, uint16_t key
, uint64_t value
)
226 copy
= qemu_malloc(sizeof(value
));
227 *copy
= cpu_to_le64(value
);
228 return fw_cfg_add_bytes(opaque
, key
, (uint8_t *)copy
, sizeof(value
));
231 int fw_cfg_add_callback(void *opaque
, uint16_t key
, FWCfgCallback callback
,
232 void *callback_opaque
, uint8_t *data
, size_t len
)
234 FWCfgState
*s
= opaque
;
235 int arch
= !!(key
& FW_CFG_ARCH_LOCAL
);
237 if (!(key
& FW_CFG_WRITE_CHANNEL
))
240 key
&= FW_CFG_ENTRY_MASK
;
242 if (key
>= FW_CFG_MAX_ENTRY
|| len
> 65535)
245 s
->entries
[arch
][key
].data
= data
;
246 s
->entries
[arch
][key
].len
= len
;
247 s
->entries
[arch
][key
].callback_opaque
= callback_opaque
;
248 s
->entries
[arch
][key
].callback
= callback
;
253 void *fw_cfg_init(uint32_t ctl_port
, uint32_t data_port
,
254 target_phys_addr_t ctl_addr
, target_phys_addr_t data_addr
)
257 int io_ctl_memory
, io_data_memory
;
259 s
= qemu_mallocz(sizeof(FWCfgState
));
262 register_ioport_write(ctl_port
, 2, 2, fw_cfg_io_writew
, s
);
265 register_ioport_read(data_port
, 1, 1, fw_cfg_io_readb
, s
);
266 register_ioport_write(data_port
, 1, 1, fw_cfg_io_writeb
, s
);
269 io_ctl_memory
= cpu_register_io_memory(fw_cfg_ctl_mem_read
,
270 fw_cfg_ctl_mem_write
, s
);
271 cpu_register_physical_memory(ctl_addr
, FW_CFG_SIZE
, io_ctl_memory
);
274 io_data_memory
= cpu_register_io_memory(fw_cfg_data_mem_read
,
275 fw_cfg_data_mem_write
, s
);
276 cpu_register_physical_memory(data_addr
, FW_CFG_SIZE
, io_data_memory
);
278 fw_cfg_add_bytes(s
, FW_CFG_SIGNATURE
, (uint8_t *)"QEMU", 4);
279 fw_cfg_add_bytes(s
, FW_CFG_UUID
, qemu_uuid
, 16);
280 fw_cfg_add_i16(s
, FW_CFG_NOGRAPHIC
, (uint16_t)(display_type
== DT_NOGRAPHIC
));
281 fw_cfg_add_i16(s
, FW_CFG_NB_CPUS
, (uint16_t)smp_cpus
);
282 fw_cfg_add_i16(s
, FW_CFG_MAX_CPUS
, (uint16_t)max_cpus
);
283 fw_cfg_add_i16(s
, FW_CFG_BOOT_MENU
, (uint16_t)boot_menu
);
285 register_savevm("fw_cfg", -1, 1, fw_cfg_save
, fw_cfg_load
, s
);
286 qemu_register_reset(fw_cfg_reset
, s
);