]>
Commit | Line | Data |
---|---|---|
3cce6243 BS |
1 | /* |
2 | * QEMU Firmware configuration device emulation | |
3 | * | |
4 | * Copyright (c) 2008 Gleb Natapov | |
5 | * | |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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. | |
23 | */ | |
83c9f4ca | 24 | #include "hw/hw.h" |
9c17d615 | 25 | #include "sysemu/sysemu.h" |
0d09e41a PB |
26 | #include "hw/isa/isa.h" |
27 | #include "hw/nvram/fw_cfg.h" | |
83c9f4ca | 28 | #include "hw/sysbus.h" |
f6e35343 | 29 | #include "trace.h" |
1de7afc9 PB |
30 | #include "qemu/error-report.h" |
31 | #include "qemu/config-file.h" | |
3cce6243 | 32 | |
3cce6243 | 33 | #define FW_CFG_SIZE 2 |
600c60b7 MT |
34 | #define FW_CFG_NAME "fw_cfg" |
35 | #define FW_CFG_PATH "/machine/" FW_CFG_NAME | |
5712db6a LE |
36 | |
37 | #define TYPE_FW_CFG "fw_cfg" | |
38 | #define TYPE_FW_CFG_IO "fw_cfg_io" | |
39 | #define TYPE_FW_CFG_MEM "fw_cfg_mem" | |
40 | ||
41 | #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG) | |
42 | #define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO) | |
43 | #define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM) | |
3cce6243 | 44 | |
b96ae2da | 45 | typedef struct FWCfgEntry { |
ff06108b | 46 | uint32_t len; |
3cce6243 BS |
47 | uint8_t *data; |
48 | void *callback_opaque; | |
49 | FWCfgCallback callback; | |
d87072ce | 50 | FWCfgReadCallback read_callback; |
3cce6243 BS |
51 | } FWCfgEntry; |
52 | ||
b96ae2da | 53 | struct FWCfgState { |
2ce92a11 HT |
54 | /*< private >*/ |
55 | SysBusDevice parent_obj; | |
56 | /*< public >*/ | |
57 | ||
3cce6243 | 58 | FWCfgEntry entries[2][FW_CFG_MAX_ENTRY]; |
abe147e0 | 59 | FWCfgFiles *files; |
3cce6243 | 60 | uint16_t cur_entry; |
ff06108b | 61 | uint32_t cur_offset; |
962630f2 | 62 | Notifier machine_ready; |
c2b5bda4 | 63 | }; |
3cce6243 | 64 | |
5712db6a LE |
65 | struct FWCfgIoState { |
66 | /*< private >*/ | |
67 | FWCfgState parent_obj; | |
68 | /*< public >*/ | |
69 | ||
70 | MemoryRegion comb_iomem; | |
71 | uint32_t iobase; | |
72 | }; | |
73 | ||
74 | struct FWCfgMemState { | |
75 | /*< private >*/ | |
76 | FWCfgState parent_obj; | |
77 | /*< public >*/ | |
78 | ||
79 | MemoryRegion ctl_iomem, data_iomem; | |
cfaadf0e LE |
80 | uint32_t data_width; |
81 | MemoryRegionOps wide_data_ops; | |
5712db6a LE |
82 | }; |
83 | ||
3d3b8303 WX |
84 | #define JPG_FILE 0 |
85 | #define BMP_FILE 1 | |
86 | ||
3d1bba20 | 87 | static char *read_splashfile(char *filename, gsize *file_sizep, |
d09acb9b | 88 | int *file_typep) |
3d3b8303 | 89 | { |
9477c87e PB |
90 | GError *err = NULL; |
91 | gboolean res; | |
92 | gchar *content; | |
9f8863eb MA |
93 | int file_type; |
94 | unsigned int filehead; | |
3d3b8303 WX |
95 | int bmp_bpp; |
96 | ||
d09acb9b | 97 | res = g_file_get_contents(filename, &content, file_sizep, &err); |
9477c87e PB |
98 | if (res == FALSE) { |
99 | error_report("failed to read splash file '%s'", filename); | |
100 | g_error_free(err); | |
101 | return NULL; | |
3d3b8303 | 102 | } |
9477c87e | 103 | |
3d3b8303 | 104 | /* check file size */ |
9477c87e PB |
105 | if (*file_sizep < 30) { |
106 | goto error; | |
3d3b8303 | 107 | } |
9477c87e | 108 | |
3d3b8303 | 109 | /* check magic ID */ |
9477c87e PB |
110 | filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff; |
111 | if (filehead == 0xd8ff) { | |
3d3b8303 | 112 | file_type = JPG_FILE; |
9477c87e PB |
113 | } else if (filehead == 0x4d42) { |
114 | file_type = BMP_FILE; | |
3d3b8303 | 115 | } else { |
9477c87e | 116 | goto error; |
3d3b8303 | 117 | } |
9477c87e | 118 | |
3d3b8303 WX |
119 | /* check BMP bpp */ |
120 | if (file_type == BMP_FILE) { | |
9477c87e | 121 | bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff; |
3d3b8303 | 122 | if (bmp_bpp != 24) { |
9477c87e | 123 | goto error; |
3d3b8303 WX |
124 | } |
125 | } | |
9477c87e | 126 | |
3d3b8303 | 127 | /* return values */ |
3d3b8303 | 128 | *file_typep = file_type; |
9477c87e PB |
129 | |
130 | return content; | |
131 | ||
132 | error: | |
133 | error_report("splash file '%s' format not recognized; must be JPEG " | |
134 | "or 24 bit BMP", filename); | |
135 | g_free(content); | |
136 | return NULL; | |
3d3b8303 WX |
137 | } |
138 | ||
139 | static void fw_cfg_bootsplash(FWCfgState *s) | |
140 | { | |
141 | int boot_splash_time = -1; | |
142 | const char *boot_splash_filename = NULL; | |
143 | char *p; | |
9477c87e | 144 | char *filename, *file_data; |
3d1bba20 | 145 | gsize file_size; |
9f8863eb | 146 | int file_type; |
3d3b8303 WX |
147 | const char *temp; |
148 | ||
149 | /* get user configuration */ | |
150 | QemuOptsList *plist = qemu_find_opts("boot-opts"); | |
151 | QemuOpts *opts = QTAILQ_FIRST(&plist->head); | |
152 | if (opts != NULL) { | |
153 | temp = qemu_opt_get(opts, "splash"); | |
154 | if (temp != NULL) { | |
155 | boot_splash_filename = temp; | |
156 | } | |
157 | temp = qemu_opt_get(opts, "splash-time"); | |
158 | if (temp != NULL) { | |
159 | p = (char *)temp; | |
160 | boot_splash_time = strtol(p, (char **)&p, 10); | |
161 | } | |
162 | } | |
163 | ||
164 | /* insert splash time if user configurated */ | |
165 | if (boot_splash_time >= 0) { | |
166 | /* validate the input */ | |
167 | if (boot_splash_time > 0xffff) { | |
168 | error_report("splash time is big than 65535, force it to 65535."); | |
169 | boot_splash_time = 0xffff; | |
170 | } | |
171 | /* use little endian format */ | |
172 | qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff); | |
173 | qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff); | |
174 | fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2); | |
175 | } | |
176 | ||
177 | /* insert splash file if user configurated */ | |
178 | if (boot_splash_filename != NULL) { | |
179 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename); | |
180 | if (filename == NULL) { | |
181 | error_report("failed to find file '%s'.", boot_splash_filename); | |
182 | return; | |
183 | } | |
9477c87e PB |
184 | |
185 | /* loading file data */ | |
186 | file_data = read_splashfile(filename, &file_size, &file_type); | |
187 | if (file_data == NULL) { | |
7267c094 | 188 | g_free(filename); |
3d3b8303 WX |
189 | return; |
190 | } | |
3d3b8303 | 191 | if (boot_splash_filedata != NULL) { |
7267c094 | 192 | g_free(boot_splash_filedata); |
3d3b8303 | 193 | } |
9477c87e | 194 | boot_splash_filedata = (uint8_t *)file_data; |
3d3b8303 | 195 | boot_splash_filedata_size = file_size; |
9477c87e | 196 | |
3d3b8303 WX |
197 | /* insert data */ |
198 | if (file_type == JPG_FILE) { | |
199 | fw_cfg_add_file(s, "bootsplash.jpg", | |
200 | boot_splash_filedata, boot_splash_filedata_size); | |
201 | } else { | |
202 | fw_cfg_add_file(s, "bootsplash.bmp", | |
203 | boot_splash_filedata, boot_splash_filedata_size); | |
204 | } | |
7267c094 | 205 | g_free(filename); |
3d3b8303 WX |
206 | } |
207 | } | |
208 | ||
ac05f349 AK |
209 | static void fw_cfg_reboot(FWCfgState *s) |
210 | { | |
211 | int reboot_timeout = -1; | |
212 | char *p; | |
213 | const char *temp; | |
214 | ||
215 | /* get user configuration */ | |
216 | QemuOptsList *plist = qemu_find_opts("boot-opts"); | |
217 | QemuOpts *opts = QTAILQ_FIRST(&plist->head); | |
218 | if (opts != NULL) { | |
219 | temp = qemu_opt_get(opts, "reboot-timeout"); | |
220 | if (temp != NULL) { | |
221 | p = (char *)temp; | |
222 | reboot_timeout = strtol(p, (char **)&p, 10); | |
223 | } | |
224 | } | |
225 | /* validate the input */ | |
226 | if (reboot_timeout > 0xffff) { | |
227 | error_report("reboot timeout is larger than 65535, force it to 65535."); | |
228 | reboot_timeout = 0xffff; | |
229 | } | |
230 | fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4); | |
231 | } | |
232 | ||
3cce6243 BS |
233 | static void fw_cfg_write(FWCfgState *s, uint8_t value) |
234 | { | |
235 | int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); | |
236 | FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; | |
237 | ||
f6e35343 | 238 | trace_fw_cfg_write(s, value); |
3cce6243 | 239 | |
962d4b28 BS |
240 | if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback && |
241 | s->cur_offset < e->len) { | |
3cce6243 BS |
242 | e->data[s->cur_offset++] = value; |
243 | if (s->cur_offset == e->len) { | |
244 | e->callback(e->callback_opaque, e->data); | |
245 | s->cur_offset = 0; | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
250 | static int fw_cfg_select(FWCfgState *s, uint16_t key) | |
251 | { | |
252 | int ret; | |
253 | ||
254 | s->cur_offset = 0; | |
255 | if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) { | |
256 | s->cur_entry = FW_CFG_INVALID; | |
257 | ret = 0; | |
258 | } else { | |
259 | s->cur_entry = key; | |
260 | ret = 1; | |
261 | } | |
262 | ||
f6e35343 | 263 | trace_fw_cfg_select(s, key, ret); |
3cce6243 BS |
264 | return ret; |
265 | } | |
266 | ||
267 | static uint8_t fw_cfg_read(FWCfgState *s) | |
268 | { | |
269 | int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); | |
270 | FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; | |
271 | uint8_t ret; | |
272 | ||
273 | if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len) | |
274 | ret = 0; | |
d87072ce MT |
275 | else { |
276 | if (e->read_callback) { | |
277 | e->read_callback(e->callback_opaque, s->cur_offset); | |
278 | } | |
3cce6243 | 279 | ret = e->data[s->cur_offset++]; |
d87072ce | 280 | } |
3cce6243 | 281 | |
f6e35343 | 282 | trace_fw_cfg_read(s, ret); |
3cce6243 BS |
283 | return ret; |
284 | } | |
285 | ||
a8170e5e | 286 | static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr, |
561e1827 | 287 | unsigned size) |
3cce6243 | 288 | { |
cfaadf0e | 289 | FWCfgState *s = opaque; |
36b62ae6 | 290 | uint64_t value = 0; |
cfaadf0e LE |
291 | unsigned i; |
292 | ||
293 | for (i = 0; i < size; ++i) { | |
36b62ae6 | 294 | value = (value << 8) | fw_cfg_read(s); |
cfaadf0e | 295 | } |
36b62ae6 | 296 | return value; |
3cce6243 BS |
297 | } |
298 | ||
a8170e5e | 299 | static void fw_cfg_data_mem_write(void *opaque, hwaddr addr, |
561e1827 | 300 | uint64_t value, unsigned size) |
3cce6243 | 301 | { |
cfaadf0e | 302 | FWCfgState *s = opaque; |
36b62ae6 | 303 | unsigned i = size; |
cfaadf0e | 304 | |
36b62ae6 LE |
305 | do { |
306 | fw_cfg_write(s, value >> (8 * --i)); | |
307 | } while (i); | |
cfaadf0e LE |
308 | } |
309 | ||
310 | static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr, | |
311 | unsigned size, bool is_write) | |
312 | { | |
313 | return addr == 0; | |
3cce6243 BS |
314 | } |
315 | ||
a8170e5e | 316 | static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr, |
561e1827 | 317 | uint64_t value, unsigned size) |
3cce6243 BS |
318 | { |
319 | fw_cfg_select(opaque, (uint16_t)value); | |
320 | } | |
321 | ||
a8170e5e | 322 | static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr, |
561e1827 | 323 | unsigned size, bool is_write) |
3cce6243 | 324 | { |
561e1827 | 325 | return is_write && size == 2; |
3cce6243 BS |
326 | } |
327 | ||
a8170e5e | 328 | static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr, |
561e1827 | 329 | unsigned size) |
3cce6243 | 330 | { |
561e1827 | 331 | return fw_cfg_read(opaque); |
3cce6243 BS |
332 | } |
333 | ||
a8170e5e | 334 | static void fw_cfg_comb_write(void *opaque, hwaddr addr, |
561e1827 | 335 | uint64_t value, unsigned size) |
3cce6243 | 336 | { |
561e1827 AK |
337 | switch (size) { |
338 | case 1: | |
339 | fw_cfg_write(opaque, (uint8_t)value); | |
340 | break; | |
341 | case 2: | |
342 | fw_cfg_select(opaque, (uint16_t)value); | |
343 | break; | |
344 | } | |
3cce6243 BS |
345 | } |
346 | ||
a8170e5e | 347 | static bool fw_cfg_comb_valid(void *opaque, hwaddr addr, |
561e1827 AK |
348 | unsigned size, bool is_write) |
349 | { | |
350 | return (size == 1) || (is_write && size == 2); | |
351 | } | |
3cce6243 | 352 | |
561e1827 AK |
353 | static const MemoryRegionOps fw_cfg_ctl_mem_ops = { |
354 | .write = fw_cfg_ctl_mem_write, | |
d789c845 | 355 | .endianness = DEVICE_BIG_ENDIAN, |
561e1827 | 356 | .valid.accepts = fw_cfg_ctl_mem_valid, |
3cce6243 BS |
357 | }; |
358 | ||
561e1827 AK |
359 | static const MemoryRegionOps fw_cfg_data_mem_ops = { |
360 | .read = fw_cfg_data_mem_read, | |
361 | .write = fw_cfg_data_mem_write, | |
d789c845 | 362 | .endianness = DEVICE_BIG_ENDIAN, |
561e1827 AK |
363 | .valid = { |
364 | .min_access_size = 1, | |
365 | .max_access_size = 1, | |
cfaadf0e | 366 | .accepts = fw_cfg_data_mem_valid, |
561e1827 | 367 | }, |
3cce6243 BS |
368 | }; |
369 | ||
561e1827 AK |
370 | static const MemoryRegionOps fw_cfg_comb_mem_ops = { |
371 | .read = fw_cfg_comb_read, | |
372 | .write = fw_cfg_comb_write, | |
6fdf98f2 | 373 | .endianness = DEVICE_LITTLE_ENDIAN, |
561e1827 | 374 | .valid.accepts = fw_cfg_comb_valid, |
3cce6243 BS |
375 | }; |
376 | ||
3a5c16fc | 377 | static void fw_cfg_reset(DeviceState *d) |
3cce6243 | 378 | { |
2ce92a11 | 379 | FWCfgState *s = FW_CFG(d); |
3cce6243 BS |
380 | |
381 | fw_cfg_select(s, 0); | |
382 | } | |
383 | ||
ff06108b JQ |
384 | /* Save restore 32 bit int as uint16_t |
385 | This is a Big hack, but it is how the old state did it. | |
386 | Or we broke compatibility in the state, or we can't use struct tm | |
387 | */ | |
388 | ||
389 | static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size) | |
390 | { | |
391 | uint32_t *v = pv; | |
392 | *v = qemu_get_be16(f); | |
393 | return 0; | |
394 | } | |
395 | ||
396 | static void put_unused(QEMUFile *f, void *pv, size_t size) | |
397 | { | |
66c80e75 | 398 | fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n"); |
ff06108b JQ |
399 | fprintf(stderr, "This functions shouldn't be called.\n"); |
400 | } | |
401 | ||
d05ac8fa | 402 | static const VMStateInfo vmstate_hack_uint32_as_uint16 = { |
ff06108b JQ |
403 | .name = "int32_as_uint16", |
404 | .get = get_uint32_as_uint16, | |
405 | .put = put_unused, | |
406 | }; | |
407 | ||
408 | #define VMSTATE_UINT16_HACK(_f, _s, _t) \ | |
409 | VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t) | |
410 | ||
411 | ||
412 | static bool is_version_1(void *opaque, int version_id) | |
413 | { | |
414 | return version_id == 1; | |
415 | } | |
416 | ||
7d2edd40 JQ |
417 | static const VMStateDescription vmstate_fw_cfg = { |
418 | .name = "fw_cfg", | |
ff06108b | 419 | .version_id = 2, |
7d2edd40 | 420 | .minimum_version_id = 1, |
d49805ae | 421 | .fields = (VMStateField[]) { |
7d2edd40 | 422 | VMSTATE_UINT16(cur_entry, FWCfgState), |
ff06108b JQ |
423 | VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1), |
424 | VMSTATE_UINT32_V(cur_offset, FWCfgState, 2), | |
7d2edd40 JQ |
425 | VMSTATE_END_OF_LIST() |
426 | } | |
427 | }; | |
3cce6243 | 428 | |
d87072ce MT |
429 | static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key, |
430 | FWCfgReadCallback callback, | |
431 | void *callback_opaque, | |
432 | void *data, size_t len) | |
3cce6243 | 433 | { |
3cce6243 BS |
434 | int arch = !!(key & FW_CFG_ARCH_LOCAL); |
435 | ||
436 | key &= FW_CFG_ENTRY_MASK; | |
437 | ||
089da572 | 438 | assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX); |
3cce6243 BS |
439 | |
440 | s->entries[arch][key].data = data; | |
089da572 | 441 | s->entries[arch][key].len = (uint32_t)len; |
d87072ce MT |
442 | s->entries[arch][key].read_callback = callback; |
443 | s->entries[arch][key].callback_opaque = callback_opaque; | |
444 | } | |
445 | ||
bdbb5b17 GA |
446 | static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key, |
447 | void *data, size_t len) | |
448 | { | |
449 | void *ptr; | |
450 | int arch = !!(key & FW_CFG_ARCH_LOCAL); | |
451 | ||
452 | key &= FW_CFG_ENTRY_MASK; | |
453 | ||
454 | assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX); | |
455 | ||
456 | /* return the old data to the function caller, avoid memory leak */ | |
457 | ptr = s->entries[arch][key].data; | |
458 | s->entries[arch][key].data = data; | |
459 | s->entries[arch][key].len = len; | |
460 | s->entries[arch][key].callback_opaque = NULL; | |
461 | s->entries[arch][key].callback = NULL; | |
462 | ||
463 | return ptr; | |
464 | } | |
465 | ||
d87072ce MT |
466 | void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len) |
467 | { | |
468 | fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len); | |
3cce6243 BS |
469 | } |
470 | ||
44687f75 MA |
471 | void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value) |
472 | { | |
473 | size_t sz = strlen(value) + 1; | |
474 | ||
089da572 | 475 | return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz); |
44687f75 MA |
476 | } |
477 | ||
4cad3867 | 478 | void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value) |
3cce6243 BS |
479 | { |
480 | uint16_t *copy; | |
481 | ||
7267c094 | 482 | copy = g_malloc(sizeof(value)); |
3cce6243 | 483 | *copy = cpu_to_le16(value); |
089da572 | 484 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
485 | } |
486 | ||
4cad3867 | 487 | void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value) |
3cce6243 BS |
488 | { |
489 | uint32_t *copy; | |
490 | ||
7267c094 | 491 | copy = g_malloc(sizeof(value)); |
3cce6243 | 492 | *copy = cpu_to_le32(value); |
089da572 | 493 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
494 | } |
495 | ||
4cad3867 | 496 | void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value) |
3cce6243 BS |
497 | { |
498 | uint64_t *copy; | |
499 | ||
7267c094 | 500 | copy = g_malloc(sizeof(value)); |
3cce6243 | 501 | *copy = cpu_to_le64(value); |
089da572 | 502 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
503 | } |
504 | ||
4cad3867 | 505 | void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback, |
089da572 | 506 | void *callback_opaque, void *data, size_t len) |
3cce6243 | 507 | { |
3cce6243 BS |
508 | int arch = !!(key & FW_CFG_ARCH_LOCAL); |
509 | ||
4cad3867 | 510 | assert(key & FW_CFG_WRITE_CHANNEL); |
85df0de4 | 511 | |
3cce6243 BS |
512 | key &= FW_CFG_ENTRY_MASK; |
513 | ||
089da572 | 514 | assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX); |
3cce6243 BS |
515 | |
516 | s->entries[arch][key].data = data; | |
089da572 | 517 | s->entries[arch][key].len = (uint32_t)len; |
3cce6243 BS |
518 | s->entries[arch][key].callback_opaque = callback_opaque; |
519 | s->entries[arch][key].callback = callback; | |
3cce6243 BS |
520 | } |
521 | ||
d87072ce MT |
522 | void fw_cfg_add_file_callback(FWCfgState *s, const char *filename, |
523 | FWCfgReadCallback callback, void *callback_opaque, | |
524 | void *data, size_t len) | |
abe147e0 | 525 | { |
de9352bc | 526 | int i, index; |
089da572 | 527 | size_t dsize; |
abe147e0 GH |
528 | |
529 | if (!s->files) { | |
089da572 | 530 | dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS; |
7267c094 | 531 | s->files = g_malloc0(dsize); |
089da572 | 532 | fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize); |
abe147e0 GH |
533 | } |
534 | ||
535 | index = be32_to_cpu(s->files->count); | |
4cad3867 | 536 | assert(index < FW_CFG_FILE_SLOTS); |
abe147e0 | 537 | |
d87072ce MT |
538 | fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index, |
539 | callback, callback_opaque, data, len); | |
abe147e0 | 540 | |
de1f34cb GN |
541 | pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), |
542 | filename); | |
de9352bc GH |
543 | for (i = 0; i < index; i++) { |
544 | if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) { | |
f6e35343 | 545 | trace_fw_cfg_add_file_dupe(s, s->files->f[index].name); |
4cad3867 | 546 | return; |
de9352bc | 547 | } |
abe147e0 | 548 | } |
de9352bc | 549 | |
abe147e0 GH |
550 | s->files->f[index].size = cpu_to_be32(len); |
551 | s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index); | |
f6e35343 | 552 | trace_fw_cfg_add_file(s, index, s->files->f[index].name, len); |
abe147e0 GH |
553 | |
554 | s->files->count = cpu_to_be32(index+1); | |
abe147e0 GH |
555 | } |
556 | ||
d87072ce MT |
557 | void fw_cfg_add_file(FWCfgState *s, const char *filename, |
558 | void *data, size_t len) | |
559 | { | |
560 | fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len); | |
561 | } | |
562 | ||
bdbb5b17 GA |
563 | void *fw_cfg_modify_file(FWCfgState *s, const char *filename, |
564 | void *data, size_t len) | |
565 | { | |
566 | int i, index; | |
f3b37668 | 567 | void *ptr = NULL; |
bdbb5b17 GA |
568 | |
569 | assert(s->files); | |
570 | ||
571 | index = be32_to_cpu(s->files->count); | |
572 | assert(index < FW_CFG_FILE_SLOTS); | |
573 | ||
574 | for (i = 0; i < index; i++) { | |
575 | if (strcmp(filename, s->files->f[i].name) == 0) { | |
f3b37668 GA |
576 | ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i, |
577 | data, len); | |
578 | s->files->f[i].size = cpu_to_be32(len); | |
579 | return ptr; | |
bdbb5b17 GA |
580 | } |
581 | } | |
582 | /* add new one */ | |
583 | fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len); | |
584 | return NULL; | |
585 | } | |
586 | ||
587 | static void fw_cfg_machine_reset(void *opaque) | |
962630f2 | 588 | { |
bdbb5b17 | 589 | void *ptr; |
0e7a7592 | 590 | size_t len; |
bdbb5b17 | 591 | FWCfgState *s = opaque; |
30e32af7 | 592 | char *bootindex = get_boot_devices_list(&len, false); |
962630f2 | 593 | |
bdbb5b17 GA |
594 | ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len); |
595 | g_free(ptr); | |
596 | } | |
597 | ||
598 | static void fw_cfg_machine_ready(struct Notifier *n, void *data) | |
599 | { | |
600 | FWCfgState *s = container_of(n, FWCfgState, machine_ready); | |
601 | qemu_register_reset(fw_cfg_machine_reset, s); | |
962630f2 GN |
602 | } |
603 | ||
3cce6243 | 604 | |
3a5c16fc | 605 | |
5712db6a LE |
606 | static void fw_cfg_init1(DeviceState *dev) |
607 | { | |
608 | FWCfgState *s = FW_CFG(dev); | |
3cce6243 | 609 | |
cac12210 MT |
610 | assert(!object_resolve_path(FW_CFG_PATH, NULL)); |
611 | ||
612 | object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL); | |
10a584b2 HT |
613 | |
614 | qdev_init_nofail(dev); | |
615 | ||
089da572 | 616 | fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4); |
084a197a | 617 | fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16); |
993fbfdb | 618 | fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC)); |
905fdcb5 | 619 | fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus); |
95387491 | 620 | fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu); |
3d3b8303 | 621 | fw_cfg_bootsplash(s); |
ac05f349 | 622 | fw_cfg_reboot(s); |
962630f2 GN |
623 | |
624 | s->machine_ready.notify = fw_cfg_machine_ready; | |
625 | qemu_add_machine_init_done_notifier(&s->machine_ready); | |
3cce6243 | 626 | } |
3a5c16fc | 627 | |
5712db6a | 628 | FWCfgState *fw_cfg_init_io(uint32_t iobase) |
3a5c16fc | 629 | { |
5712db6a | 630 | DeviceState *dev; |
3a5c16fc | 631 | |
5712db6a LE |
632 | dev = qdev_create(NULL, TYPE_FW_CFG_IO); |
633 | qdev_prop_set_uint32(dev, "iobase", iobase); | |
634 | fw_cfg_init1(dev); | |
635 | ||
636 | return FW_CFG(dev); | |
56383955 HT |
637 | } |
638 | ||
6c87e3d5 LE |
639 | FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr, hwaddr data_addr, |
640 | uint32_t data_width) | |
56383955 | 641 | { |
5712db6a LE |
642 | DeviceState *dev; |
643 | SysBusDevice *sbd; | |
56383955 | 644 | |
5712db6a | 645 | dev = qdev_create(NULL, TYPE_FW_CFG_MEM); |
6c87e3d5 | 646 | qdev_prop_set_uint32(dev, "data_width", data_width); |
cfaadf0e | 647 | |
5712db6a LE |
648 | fw_cfg_init1(dev); |
649 | ||
650 | sbd = SYS_BUS_DEVICE(dev); | |
651 | sysbus_mmio_map(sbd, 0, ctl_addr); | |
652 | sysbus_mmio_map(sbd, 1, data_addr); | |
653 | ||
654 | return FW_CFG(dev); | |
655 | } | |
656 | ||
6c87e3d5 LE |
657 | FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr) |
658 | { | |
659 | return fw_cfg_init_mem_wide(ctl_addr, data_addr, | |
660 | fw_cfg_data_mem_ops.valid.max_access_size); | |
661 | } | |
662 | ||
5712db6a | 663 | |
600c60b7 MT |
664 | FWCfgState *fw_cfg_find(void) |
665 | { | |
2ce92a11 | 666 | return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL)); |
600c60b7 MT |
667 | } |
668 | ||
999e12bb AL |
669 | static void fw_cfg_class_init(ObjectClass *klass, void *data) |
670 | { | |
39bffca2 | 671 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 672 | |
39bffca2 AL |
673 | dc->reset = fw_cfg_reset; |
674 | dc->vmsd = &vmstate_fw_cfg; | |
999e12bb AL |
675 | } |
676 | ||
8c43a6f0 | 677 | static const TypeInfo fw_cfg_info = { |
600c60b7 | 678 | .name = TYPE_FW_CFG, |
39bffca2 AL |
679 | .parent = TYPE_SYS_BUS_DEVICE, |
680 | .instance_size = sizeof(FWCfgState), | |
681 | .class_init = fw_cfg_class_init, | |
3a5c16fc BS |
682 | }; |
683 | ||
5712db6a LE |
684 | |
685 | static Property fw_cfg_io_properties[] = { | |
686 | DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1), | |
687 | DEFINE_PROP_END_OF_LIST(), | |
688 | }; | |
689 | ||
690 | static void fw_cfg_io_realize(DeviceState *dev, Error **errp) | |
691 | { | |
692 | FWCfgIoState *s = FW_CFG_IO(dev); | |
693 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); | |
694 | ||
695 | memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops, | |
696 | FW_CFG(s), "fwcfg", FW_CFG_SIZE); | |
697 | sysbus_add_io(sbd, s->iobase, &s->comb_iomem); | |
698 | } | |
699 | ||
700 | static void fw_cfg_io_class_init(ObjectClass *klass, void *data) | |
701 | { | |
702 | DeviceClass *dc = DEVICE_CLASS(klass); | |
703 | ||
704 | dc->realize = fw_cfg_io_realize; | |
705 | dc->props = fw_cfg_io_properties; | |
706 | } | |
707 | ||
708 | static const TypeInfo fw_cfg_io_info = { | |
709 | .name = TYPE_FW_CFG_IO, | |
710 | .parent = TYPE_FW_CFG, | |
711 | .instance_size = sizeof(FWCfgIoState), | |
712 | .class_init = fw_cfg_io_class_init, | |
713 | }; | |
714 | ||
715 | ||
cfaadf0e LE |
716 | static Property fw_cfg_mem_properties[] = { |
717 | DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1), | |
718 | DEFINE_PROP_END_OF_LIST(), | |
719 | }; | |
720 | ||
5712db6a LE |
721 | static void fw_cfg_mem_realize(DeviceState *dev, Error **errp) |
722 | { | |
723 | FWCfgMemState *s = FW_CFG_MEM(dev); | |
724 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); | |
cfaadf0e | 725 | const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops; |
5712db6a LE |
726 | |
727 | memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops, | |
728 | FW_CFG(s), "fwcfg.ctl", FW_CFG_SIZE); | |
729 | sysbus_init_mmio(sbd, &s->ctl_iomem); | |
730 | ||
cfaadf0e LE |
731 | if (s->data_width > data_ops->valid.max_access_size) { |
732 | /* memberwise copy because the "old_mmio" member is const */ | |
733 | s->wide_data_ops.read = data_ops->read; | |
734 | s->wide_data_ops.write = data_ops->write; | |
735 | s->wide_data_ops.endianness = data_ops->endianness; | |
736 | s->wide_data_ops.valid = data_ops->valid; | |
737 | s->wide_data_ops.impl = data_ops->impl; | |
738 | ||
739 | s->wide_data_ops.valid.max_access_size = s->data_width; | |
740 | s->wide_data_ops.impl.max_access_size = s->data_width; | |
741 | data_ops = &s->wide_data_ops; | |
742 | } | |
743 | memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s), | |
744 | "fwcfg.data", data_ops->valid.max_access_size); | |
5712db6a LE |
745 | sysbus_init_mmio(sbd, &s->data_iomem); |
746 | } | |
747 | ||
748 | static void fw_cfg_mem_class_init(ObjectClass *klass, void *data) | |
749 | { | |
750 | DeviceClass *dc = DEVICE_CLASS(klass); | |
751 | ||
752 | dc->realize = fw_cfg_mem_realize; | |
cfaadf0e | 753 | dc->props = fw_cfg_mem_properties; |
5712db6a LE |
754 | } |
755 | ||
756 | static const TypeInfo fw_cfg_mem_info = { | |
757 | .name = TYPE_FW_CFG_MEM, | |
758 | .parent = TYPE_FW_CFG, | |
759 | .instance_size = sizeof(FWCfgMemState), | |
760 | .class_init = fw_cfg_mem_class_init, | |
761 | }; | |
762 | ||
763 | ||
83f7d43a | 764 | static void fw_cfg_register_types(void) |
3a5c16fc | 765 | { |
39bffca2 | 766 | type_register_static(&fw_cfg_info); |
5712db6a LE |
767 | type_register_static(&fw_cfg_io_info); |
768 | type_register_static(&fw_cfg_mem_info); | |
3a5c16fc BS |
769 | } |
770 | ||
83f7d43a | 771 | type_init(fw_cfg_register_types) |