]>
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 |
561e1827 | 34 | #define FW_CFG_DATA_SIZE 1 |
600c60b7 MT |
35 | #define TYPE_FW_CFG "fw_cfg" |
36 | #define FW_CFG_NAME "fw_cfg" | |
37 | #define FW_CFG_PATH "/machine/" FW_CFG_NAME | |
2ce92a11 | 38 | #define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG) |
3cce6243 | 39 | |
b96ae2da | 40 | typedef struct FWCfgEntry { |
ff06108b | 41 | uint32_t len; |
3cce6243 BS |
42 | uint8_t *data; |
43 | void *callback_opaque; | |
44 | FWCfgCallback callback; | |
45 | } FWCfgEntry; | |
46 | ||
b96ae2da | 47 | struct FWCfgState { |
2ce92a11 HT |
48 | /*< private >*/ |
49 | SysBusDevice parent_obj; | |
50 | /*< public >*/ | |
51 | ||
561e1827 | 52 | MemoryRegion ctl_iomem, data_iomem, comb_iomem; |
3a5c16fc | 53 | uint32_t ctl_iobase, data_iobase; |
3cce6243 | 54 | FWCfgEntry entries[2][FW_CFG_MAX_ENTRY]; |
abe147e0 | 55 | FWCfgFiles *files; |
3cce6243 | 56 | uint16_t cur_entry; |
ff06108b | 57 | uint32_t cur_offset; |
962630f2 | 58 | Notifier machine_ready; |
c2b5bda4 | 59 | }; |
3cce6243 | 60 | |
3d3b8303 WX |
61 | #define JPG_FILE 0 |
62 | #define BMP_FILE 1 | |
63 | ||
3d1bba20 | 64 | static char *read_splashfile(char *filename, gsize *file_sizep, |
d09acb9b | 65 | int *file_typep) |
3d3b8303 | 66 | { |
9477c87e PB |
67 | GError *err = NULL; |
68 | gboolean res; | |
69 | gchar *content; | |
9f8863eb MA |
70 | int file_type; |
71 | unsigned int filehead; | |
3d3b8303 WX |
72 | int bmp_bpp; |
73 | ||
d09acb9b | 74 | res = g_file_get_contents(filename, &content, file_sizep, &err); |
9477c87e PB |
75 | if (res == FALSE) { |
76 | error_report("failed to read splash file '%s'", filename); | |
77 | g_error_free(err); | |
78 | return NULL; | |
3d3b8303 | 79 | } |
9477c87e | 80 | |
3d3b8303 | 81 | /* check file size */ |
9477c87e PB |
82 | if (*file_sizep < 30) { |
83 | goto error; | |
3d3b8303 | 84 | } |
9477c87e | 85 | |
3d3b8303 | 86 | /* check magic ID */ |
9477c87e PB |
87 | filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff; |
88 | if (filehead == 0xd8ff) { | |
3d3b8303 | 89 | file_type = JPG_FILE; |
9477c87e PB |
90 | } else if (filehead == 0x4d42) { |
91 | file_type = BMP_FILE; | |
3d3b8303 | 92 | } else { |
9477c87e | 93 | goto error; |
3d3b8303 | 94 | } |
9477c87e | 95 | |
3d3b8303 WX |
96 | /* check BMP bpp */ |
97 | if (file_type == BMP_FILE) { | |
9477c87e | 98 | bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff; |
3d3b8303 | 99 | if (bmp_bpp != 24) { |
9477c87e | 100 | goto error; |
3d3b8303 WX |
101 | } |
102 | } | |
9477c87e | 103 | |
3d3b8303 | 104 | /* return values */ |
3d3b8303 | 105 | *file_typep = file_type; |
9477c87e PB |
106 | |
107 | return content; | |
108 | ||
109 | error: | |
110 | error_report("splash file '%s' format not recognized; must be JPEG " | |
111 | "or 24 bit BMP", filename); | |
112 | g_free(content); | |
113 | return NULL; | |
3d3b8303 WX |
114 | } |
115 | ||
116 | static void fw_cfg_bootsplash(FWCfgState *s) | |
117 | { | |
118 | int boot_splash_time = -1; | |
119 | const char *boot_splash_filename = NULL; | |
120 | char *p; | |
9477c87e | 121 | char *filename, *file_data; |
3d1bba20 | 122 | gsize file_size; |
9f8863eb | 123 | int file_type; |
3d3b8303 WX |
124 | const char *temp; |
125 | ||
126 | /* get user configuration */ | |
127 | QemuOptsList *plist = qemu_find_opts("boot-opts"); | |
128 | QemuOpts *opts = QTAILQ_FIRST(&plist->head); | |
129 | if (opts != NULL) { | |
130 | temp = qemu_opt_get(opts, "splash"); | |
131 | if (temp != NULL) { | |
132 | boot_splash_filename = temp; | |
133 | } | |
134 | temp = qemu_opt_get(opts, "splash-time"); | |
135 | if (temp != NULL) { | |
136 | p = (char *)temp; | |
137 | boot_splash_time = strtol(p, (char **)&p, 10); | |
138 | } | |
139 | } | |
140 | ||
141 | /* insert splash time if user configurated */ | |
142 | if (boot_splash_time >= 0) { | |
143 | /* validate the input */ | |
144 | if (boot_splash_time > 0xffff) { | |
145 | error_report("splash time is big than 65535, force it to 65535."); | |
146 | boot_splash_time = 0xffff; | |
147 | } | |
148 | /* use little endian format */ | |
149 | qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff); | |
150 | qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff); | |
151 | fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2); | |
152 | } | |
153 | ||
154 | /* insert splash file if user configurated */ | |
155 | if (boot_splash_filename != NULL) { | |
156 | filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename); | |
157 | if (filename == NULL) { | |
158 | error_report("failed to find file '%s'.", boot_splash_filename); | |
159 | return; | |
160 | } | |
9477c87e PB |
161 | |
162 | /* loading file data */ | |
163 | file_data = read_splashfile(filename, &file_size, &file_type); | |
164 | if (file_data == NULL) { | |
7267c094 | 165 | g_free(filename); |
3d3b8303 WX |
166 | return; |
167 | } | |
3d3b8303 | 168 | if (boot_splash_filedata != NULL) { |
7267c094 | 169 | g_free(boot_splash_filedata); |
3d3b8303 | 170 | } |
9477c87e | 171 | boot_splash_filedata = (uint8_t *)file_data; |
3d3b8303 | 172 | boot_splash_filedata_size = file_size; |
9477c87e | 173 | |
3d3b8303 WX |
174 | /* insert data */ |
175 | if (file_type == JPG_FILE) { | |
176 | fw_cfg_add_file(s, "bootsplash.jpg", | |
177 | boot_splash_filedata, boot_splash_filedata_size); | |
178 | } else { | |
179 | fw_cfg_add_file(s, "bootsplash.bmp", | |
180 | boot_splash_filedata, boot_splash_filedata_size); | |
181 | } | |
7267c094 | 182 | g_free(filename); |
3d3b8303 WX |
183 | } |
184 | } | |
185 | ||
ac05f349 AK |
186 | static void fw_cfg_reboot(FWCfgState *s) |
187 | { | |
188 | int reboot_timeout = -1; | |
189 | char *p; | |
190 | const char *temp; | |
191 | ||
192 | /* get user configuration */ | |
193 | QemuOptsList *plist = qemu_find_opts("boot-opts"); | |
194 | QemuOpts *opts = QTAILQ_FIRST(&plist->head); | |
195 | if (opts != NULL) { | |
196 | temp = qemu_opt_get(opts, "reboot-timeout"); | |
197 | if (temp != NULL) { | |
198 | p = (char *)temp; | |
199 | reboot_timeout = strtol(p, (char **)&p, 10); | |
200 | } | |
201 | } | |
202 | /* validate the input */ | |
203 | if (reboot_timeout > 0xffff) { | |
204 | error_report("reboot timeout is larger than 65535, force it to 65535."); | |
205 | reboot_timeout = 0xffff; | |
206 | } | |
207 | fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4); | |
208 | } | |
209 | ||
3cce6243 BS |
210 | static void fw_cfg_write(FWCfgState *s, uint8_t value) |
211 | { | |
212 | int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); | |
213 | FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; | |
214 | ||
f6e35343 | 215 | trace_fw_cfg_write(s, value); |
3cce6243 | 216 | |
962d4b28 BS |
217 | if (s->cur_entry & FW_CFG_WRITE_CHANNEL && e->callback && |
218 | s->cur_offset < e->len) { | |
3cce6243 BS |
219 | e->data[s->cur_offset++] = value; |
220 | if (s->cur_offset == e->len) { | |
221 | e->callback(e->callback_opaque, e->data); | |
222 | s->cur_offset = 0; | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | static int fw_cfg_select(FWCfgState *s, uint16_t key) | |
228 | { | |
229 | int ret; | |
230 | ||
231 | s->cur_offset = 0; | |
232 | if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) { | |
233 | s->cur_entry = FW_CFG_INVALID; | |
234 | ret = 0; | |
235 | } else { | |
236 | s->cur_entry = key; | |
237 | ret = 1; | |
238 | } | |
239 | ||
f6e35343 | 240 | trace_fw_cfg_select(s, key, ret); |
3cce6243 BS |
241 | return ret; |
242 | } | |
243 | ||
244 | static uint8_t fw_cfg_read(FWCfgState *s) | |
245 | { | |
246 | int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL); | |
247 | FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]; | |
248 | uint8_t ret; | |
249 | ||
250 | if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len) | |
251 | ret = 0; | |
252 | else | |
253 | ret = e->data[s->cur_offset++]; | |
254 | ||
f6e35343 | 255 | trace_fw_cfg_read(s, ret); |
3cce6243 BS |
256 | return ret; |
257 | } | |
258 | ||
a8170e5e | 259 | static uint64_t fw_cfg_data_mem_read(void *opaque, hwaddr addr, |
561e1827 | 260 | unsigned size) |
3cce6243 BS |
261 | { |
262 | return fw_cfg_read(opaque); | |
263 | } | |
264 | ||
a8170e5e | 265 | static void fw_cfg_data_mem_write(void *opaque, hwaddr addr, |
561e1827 | 266 | uint64_t value, unsigned size) |
3cce6243 | 267 | { |
7442511c | 268 | fw_cfg_write(opaque, (uint8_t)value); |
3cce6243 BS |
269 | } |
270 | ||
a8170e5e | 271 | static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr, |
561e1827 | 272 | uint64_t value, unsigned size) |
3cce6243 BS |
273 | { |
274 | fw_cfg_select(opaque, (uint16_t)value); | |
275 | } | |
276 | ||
a8170e5e | 277 | static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr, |
561e1827 | 278 | unsigned size, bool is_write) |
3cce6243 | 279 | { |
561e1827 | 280 | return is_write && size == 2; |
3cce6243 BS |
281 | } |
282 | ||
a8170e5e | 283 | static uint64_t fw_cfg_comb_read(void *opaque, hwaddr addr, |
561e1827 | 284 | unsigned size) |
3cce6243 | 285 | { |
561e1827 | 286 | return fw_cfg_read(opaque); |
3cce6243 BS |
287 | } |
288 | ||
a8170e5e | 289 | static void fw_cfg_comb_write(void *opaque, hwaddr addr, |
561e1827 | 290 | uint64_t value, unsigned size) |
3cce6243 | 291 | { |
561e1827 AK |
292 | switch (size) { |
293 | case 1: | |
294 | fw_cfg_write(opaque, (uint8_t)value); | |
295 | break; | |
296 | case 2: | |
297 | fw_cfg_select(opaque, (uint16_t)value); | |
298 | break; | |
299 | } | |
3cce6243 BS |
300 | } |
301 | ||
a8170e5e | 302 | static bool fw_cfg_comb_valid(void *opaque, hwaddr addr, |
561e1827 AK |
303 | unsigned size, bool is_write) |
304 | { | |
305 | return (size == 1) || (is_write && size == 2); | |
306 | } | |
3cce6243 | 307 | |
561e1827 AK |
308 | static const MemoryRegionOps fw_cfg_ctl_mem_ops = { |
309 | .write = fw_cfg_ctl_mem_write, | |
310 | .endianness = DEVICE_NATIVE_ENDIAN, | |
311 | .valid.accepts = fw_cfg_ctl_mem_valid, | |
3cce6243 BS |
312 | }; |
313 | ||
561e1827 AK |
314 | static const MemoryRegionOps fw_cfg_data_mem_ops = { |
315 | .read = fw_cfg_data_mem_read, | |
316 | .write = fw_cfg_data_mem_write, | |
317 | .endianness = DEVICE_NATIVE_ENDIAN, | |
318 | .valid = { | |
319 | .min_access_size = 1, | |
320 | .max_access_size = 1, | |
321 | }, | |
3cce6243 BS |
322 | }; |
323 | ||
561e1827 AK |
324 | static const MemoryRegionOps fw_cfg_comb_mem_ops = { |
325 | .read = fw_cfg_comb_read, | |
326 | .write = fw_cfg_comb_write, | |
327 | .endianness = DEVICE_NATIVE_ENDIAN, | |
328 | .valid.accepts = fw_cfg_comb_valid, | |
3cce6243 BS |
329 | }; |
330 | ||
3a5c16fc | 331 | static void fw_cfg_reset(DeviceState *d) |
3cce6243 | 332 | { |
2ce92a11 | 333 | FWCfgState *s = FW_CFG(d); |
3cce6243 BS |
334 | |
335 | fw_cfg_select(s, 0); | |
336 | } | |
337 | ||
ff06108b JQ |
338 | /* Save restore 32 bit int as uint16_t |
339 | This is a Big hack, but it is how the old state did it. | |
340 | Or we broke compatibility in the state, or we can't use struct tm | |
341 | */ | |
342 | ||
343 | static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size) | |
344 | { | |
345 | uint32_t *v = pv; | |
346 | *v = qemu_get_be16(f); | |
347 | return 0; | |
348 | } | |
349 | ||
350 | static void put_unused(QEMUFile *f, void *pv, size_t size) | |
351 | { | |
66c80e75 | 352 | fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n"); |
ff06108b JQ |
353 | fprintf(stderr, "This functions shouldn't be called.\n"); |
354 | } | |
355 | ||
d05ac8fa | 356 | static const VMStateInfo vmstate_hack_uint32_as_uint16 = { |
ff06108b JQ |
357 | .name = "int32_as_uint16", |
358 | .get = get_uint32_as_uint16, | |
359 | .put = put_unused, | |
360 | }; | |
361 | ||
362 | #define VMSTATE_UINT16_HACK(_f, _s, _t) \ | |
363 | VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t) | |
364 | ||
365 | ||
366 | static bool is_version_1(void *opaque, int version_id) | |
367 | { | |
368 | return version_id == 1; | |
369 | } | |
370 | ||
7d2edd40 JQ |
371 | static const VMStateDescription vmstate_fw_cfg = { |
372 | .name = "fw_cfg", | |
ff06108b | 373 | .version_id = 2, |
7d2edd40 JQ |
374 | .minimum_version_id = 1, |
375 | .minimum_version_id_old = 1, | |
376 | .fields = (VMStateField []) { | |
377 | VMSTATE_UINT16(cur_entry, FWCfgState), | |
ff06108b JQ |
378 | VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1), |
379 | VMSTATE_UINT32_V(cur_offset, FWCfgState, 2), | |
7d2edd40 JQ |
380 | VMSTATE_END_OF_LIST() |
381 | } | |
382 | }; | |
3cce6243 | 383 | |
089da572 | 384 | void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len) |
3cce6243 | 385 | { |
3cce6243 BS |
386 | int arch = !!(key & FW_CFG_ARCH_LOCAL); |
387 | ||
388 | key &= FW_CFG_ENTRY_MASK; | |
389 | ||
089da572 | 390 | assert(key < FW_CFG_MAX_ENTRY && len < UINT32_MAX); |
3cce6243 BS |
391 | |
392 | s->entries[arch][key].data = data; | |
089da572 | 393 | s->entries[arch][key].len = (uint32_t)len; |
3cce6243 BS |
394 | } |
395 | ||
44687f75 MA |
396 | void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value) |
397 | { | |
398 | size_t sz = strlen(value) + 1; | |
399 | ||
089da572 | 400 | return fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz); |
44687f75 MA |
401 | } |
402 | ||
4cad3867 | 403 | void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value) |
3cce6243 BS |
404 | { |
405 | uint16_t *copy; | |
406 | ||
7267c094 | 407 | copy = g_malloc(sizeof(value)); |
3cce6243 | 408 | *copy = cpu_to_le16(value); |
089da572 | 409 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
410 | } |
411 | ||
4cad3867 | 412 | void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value) |
3cce6243 BS |
413 | { |
414 | uint32_t *copy; | |
415 | ||
7267c094 | 416 | copy = g_malloc(sizeof(value)); |
3cce6243 | 417 | *copy = cpu_to_le32(value); |
089da572 | 418 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
419 | } |
420 | ||
4cad3867 | 421 | void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value) |
3cce6243 BS |
422 | { |
423 | uint64_t *copy; | |
424 | ||
7267c094 | 425 | copy = g_malloc(sizeof(value)); |
3cce6243 | 426 | *copy = cpu_to_le64(value); |
089da572 | 427 | fw_cfg_add_bytes(s, key, copy, sizeof(value)); |
3cce6243 BS |
428 | } |
429 | ||
4cad3867 | 430 | void fw_cfg_add_callback(FWCfgState *s, uint16_t key, FWCfgCallback callback, |
089da572 | 431 | void *callback_opaque, void *data, size_t len) |
3cce6243 | 432 | { |
3cce6243 BS |
433 | int arch = !!(key & FW_CFG_ARCH_LOCAL); |
434 | ||
4cad3867 | 435 | assert(key & FW_CFG_WRITE_CHANNEL); |
85df0de4 | 436 | |
3cce6243 BS |
437 | key &= FW_CFG_ENTRY_MASK; |
438 | ||
089da572 | 439 | assert(key < FW_CFG_MAX_ENTRY && len <= UINT32_MAX); |
3cce6243 BS |
440 | |
441 | s->entries[arch][key].data = data; | |
089da572 | 442 | s->entries[arch][key].len = (uint32_t)len; |
3cce6243 BS |
443 | s->entries[arch][key].callback_opaque = callback_opaque; |
444 | s->entries[arch][key].callback = callback; | |
3cce6243 BS |
445 | } |
446 | ||
089da572 MA |
447 | void fw_cfg_add_file(FWCfgState *s, const char *filename, |
448 | void *data, size_t len) | |
abe147e0 | 449 | { |
de9352bc | 450 | int i, index; |
089da572 | 451 | size_t dsize; |
abe147e0 GH |
452 | |
453 | if (!s->files) { | |
089da572 | 454 | dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * FW_CFG_FILE_SLOTS; |
7267c094 | 455 | s->files = g_malloc0(dsize); |
089da572 | 456 | fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize); |
abe147e0 GH |
457 | } |
458 | ||
459 | index = be32_to_cpu(s->files->count); | |
4cad3867 | 460 | assert(index < FW_CFG_FILE_SLOTS); |
abe147e0 GH |
461 | |
462 | fw_cfg_add_bytes(s, FW_CFG_FILE_FIRST + index, data, len); | |
463 | ||
de1f34cb GN |
464 | pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), |
465 | filename); | |
de9352bc GH |
466 | for (i = 0; i < index; i++) { |
467 | if (strcmp(s->files->f[index].name, s->files->f[i].name) == 0) { | |
f6e35343 | 468 | trace_fw_cfg_add_file_dupe(s, s->files->f[index].name); |
4cad3867 | 469 | return; |
de9352bc | 470 | } |
abe147e0 | 471 | } |
de9352bc | 472 | |
abe147e0 GH |
473 | s->files->f[index].size = cpu_to_be32(len); |
474 | s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index); | |
f6e35343 | 475 | trace_fw_cfg_add_file(s, index, s->files->f[index].name, len); |
abe147e0 GH |
476 | |
477 | s->files->count = cpu_to_be32(index+1); | |
abe147e0 GH |
478 | } |
479 | ||
9e8dd451 | 480 | static void fw_cfg_machine_ready(struct Notifier *n, void *data) |
962630f2 | 481 | { |
0e7a7592 | 482 | size_t len; |
962630f2 GN |
483 | FWCfgState *s = container_of(n, FWCfgState, machine_ready); |
484 | char *bootindex = get_boot_devices_list(&len); | |
485 | ||
486 | fw_cfg_add_file(s, "bootorder", (uint8_t*)bootindex, len); | |
487 | } | |
488 | ||
c2b5bda4 | 489 | FWCfgState *fw_cfg_init(uint32_t ctl_port, uint32_t data_port, |
a8170e5e | 490 | hwaddr ctl_addr, hwaddr data_addr) |
3cce6243 | 491 | { |
3a5c16fc BS |
492 | DeviceState *dev; |
493 | SysBusDevice *d; | |
3cce6243 | 494 | FWCfgState *s; |
3cce6243 | 495 | |
2ce92a11 | 496 | dev = qdev_create(NULL, TYPE_FW_CFG); |
3a5c16fc BS |
497 | qdev_prop_set_uint32(dev, "ctl_iobase", ctl_port); |
498 | qdev_prop_set_uint32(dev, "data_iobase", data_port); | |
1356b98d | 499 | d = SYS_BUS_DEVICE(dev); |
3a5c16fc | 500 | |
2ce92a11 | 501 | s = FW_CFG(dev); |
3cce6243 | 502 | |
cac12210 MT |
503 | assert(!object_resolve_path(FW_CFG_PATH, NULL)); |
504 | ||
505 | object_property_add_child(qdev_get_machine(), FW_CFG_NAME, OBJECT(s), NULL); | |
10a584b2 HT |
506 | |
507 | qdev_init_nofail(dev); | |
508 | ||
3cce6243 | 509 | if (ctl_addr) { |
3a5c16fc | 510 | sysbus_mmio_map(d, 0, ctl_addr); |
3cce6243 BS |
511 | } |
512 | if (data_addr) { | |
3a5c16fc | 513 | sysbus_mmio_map(d, 1, data_addr); |
3cce6243 | 514 | } |
089da572 | 515 | fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4); |
084a197a | 516 | fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16); |
993fbfdb | 517 | fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC)); |
905fdcb5 | 518 | fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus); |
95387491 | 519 | fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu); |
3d3b8303 | 520 | fw_cfg_bootsplash(s); |
ac05f349 | 521 | fw_cfg_reboot(s); |
962630f2 GN |
522 | |
523 | s->machine_ready.notify = fw_cfg_machine_ready; | |
524 | qemu_add_machine_init_done_notifier(&s->machine_ready); | |
525 | ||
3cce6243 BS |
526 | return s; |
527 | } | |
3a5c16fc BS |
528 | |
529 | static int fw_cfg_init1(SysBusDevice *dev) | |
530 | { | |
2ce92a11 | 531 | FWCfgState *s = FW_CFG(dev); |
3a5c16fc | 532 | |
eedfac6f | 533 | memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops, s, |
561e1827 | 534 | "fwcfg.ctl", FW_CFG_SIZE); |
750ecd44 | 535 | sysbus_init_mmio(dev, &s->ctl_iomem); |
eedfac6f | 536 | memory_region_init_io(&s->data_iomem, OBJECT(s), &fw_cfg_data_mem_ops, s, |
561e1827 | 537 | "fwcfg.data", FW_CFG_DATA_SIZE); |
750ecd44 | 538 | sysbus_init_mmio(dev, &s->data_iomem); |
561e1827 | 539 | /* In case ctl and data overlap: */ |
eedfac6f | 540 | memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops, s, |
561e1827 AK |
541 | "fwcfg", FW_CFG_SIZE); |
542 | ||
543 | if (s->ctl_iobase + 1 == s->data_iobase) { | |
544 | sysbus_add_io(dev, s->ctl_iobase, &s->comb_iomem); | |
545 | } else { | |
546 | if (s->ctl_iobase) { | |
547 | sysbus_add_io(dev, s->ctl_iobase, &s->ctl_iomem); | |
548 | } | |
549 | if (s->data_iobase) { | |
550 | sysbus_add_io(dev, s->data_iobase, &s->data_iomem); | |
551 | } | |
3a5c16fc BS |
552 | } |
553 | return 0; | |
554 | } | |
555 | ||
999e12bb AL |
556 | static Property fw_cfg_properties[] = { |
557 | DEFINE_PROP_HEX32("ctl_iobase", FWCfgState, ctl_iobase, -1), | |
558 | DEFINE_PROP_HEX32("data_iobase", FWCfgState, data_iobase, -1), | |
559 | DEFINE_PROP_END_OF_LIST(), | |
560 | }; | |
561 | ||
600c60b7 MT |
562 | FWCfgState *fw_cfg_find(void) |
563 | { | |
2ce92a11 | 564 | return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL)); |
600c60b7 MT |
565 | } |
566 | ||
999e12bb AL |
567 | static void fw_cfg_class_init(ObjectClass *klass, void *data) |
568 | { | |
39bffca2 | 569 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb AL |
570 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
571 | ||
572 | k->init = fw_cfg_init1; | |
39bffca2 AL |
573 | dc->no_user = 1; |
574 | dc->reset = fw_cfg_reset; | |
575 | dc->vmsd = &vmstate_fw_cfg; | |
576 | dc->props = fw_cfg_properties; | |
999e12bb AL |
577 | } |
578 | ||
8c43a6f0 | 579 | static const TypeInfo fw_cfg_info = { |
600c60b7 | 580 | .name = TYPE_FW_CFG, |
39bffca2 AL |
581 | .parent = TYPE_SYS_BUS_DEVICE, |
582 | .instance_size = sizeof(FWCfgState), | |
583 | .class_init = fw_cfg_class_init, | |
3a5c16fc BS |
584 | }; |
585 | ||
83f7d43a | 586 | static void fw_cfg_register_types(void) |
3a5c16fc | 587 | { |
39bffca2 | 588 | type_register_static(&fw_cfg_info); |
3a5c16fc BS |
589 | } |
590 | ||
83f7d43a | 591 | type_init(fw_cfg_register_types) |