blob: 523d585dcfeda1faa7364567553a21c5b187fc6a [file] [log] [blame]
blueswir13cce6242008-09-18 18:27:29 +00001/*
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 */
Peter Maydell04308912016-01-26 18:17:30 +000024#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/hw.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/sysemu.h"
Marc María4c0d1d2015-10-08 17:02:55 +020027#include "sysemu/dma.h"
Eduardo Habkostcfc58cf2016-04-19 16:55:25 -030028#include "hw/boards.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010029#include "hw/isa/isa.h"
30#include "hw/nvram/fw_cfg.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010031#include "hw/sysbus.h"
Markus Armbrusterf6e35342013-01-16 14:50:22 +010032#include "trace.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010033#include "qemu/error-report.h"
34#include "qemu/config-file.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/cutils.h"
Laszlo Erseke12f3a12017-01-12 19:24:15 +010036#include "qapi/error.h"
blueswir13cce6242008-09-18 18:27:29 +000037
Laszlo Erseka5b3ebf2017-01-12 19:24:17 +010038#define FW_CFG_FILE_SLOTS_DFLT 0x20
39
Michael S. Tsirkin600c60b2013-05-30 16:07:58 +030040#define FW_CFG_NAME "fw_cfg"
41#define FW_CFG_PATH "/machine/" FW_CFG_NAME
Laszlo Ersek5712db62014-12-22 13:11:35 +010042
43#define TYPE_FW_CFG "fw_cfg"
44#define TYPE_FW_CFG_IO "fw_cfg_io"
45#define TYPE_FW_CFG_MEM "fw_cfg_mem"
46
47#define FW_CFG(obj) OBJECT_CHECK(FWCfgState, (obj), TYPE_FW_CFG)
48#define FW_CFG_IO(obj) OBJECT_CHECK(FWCfgIoState, (obj), TYPE_FW_CFG_IO)
49#define FW_CFG_MEM(obj) OBJECT_CHECK(FWCfgMemState, (obj), TYPE_FW_CFG_MEM)
blueswir13cce6242008-09-18 18:27:29 +000050
Marc María4c0d1d2015-10-08 17:02:55 +020051/* FW_CFG_VERSION bits */
52#define FW_CFG_VERSION 0x01
53#define FW_CFG_VERSION_DMA 0x02
54
55/* FW_CFG_DMA_CONTROL bits */
56#define FW_CFG_DMA_CTL_ERROR 0x01
57#define FW_CFG_DMA_CTL_READ 0x02
58#define FW_CFG_DMA_CTL_SKIP 0x04
59#define FW_CFG_DMA_CTL_SELECT 0x08
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +010060#define FW_CFG_DMA_CTL_WRITE 0x10
Marc María4c0d1d2015-10-08 17:02:55 +020061
Kevin O'Connor2cc06a82015-10-08 17:02:58 +020062#define FW_CFG_DMA_SIGNATURE 0x51454d5520434647ULL /* "QEMU CFG" */
63
Blue Swirlb96ae2d2010-02-07 09:15:26 +000064typedef struct FWCfgEntry {
Juan Quintelaff061082009-11-13 11:59:20 +010065 uint32_t len;
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +010066 bool allow_write;
blueswir13cce6242008-09-18 18:27:29 +000067 uint8_t *data;
68 void *callback_opaque;
Michael S. Tsirkind87072c2013-09-01 17:56:20 +030069 FWCfgReadCallback read_callback;
blueswir13cce6242008-09-18 18:27:29 +000070} FWCfgEntry;
71
Blue Swirlb96ae2d2010-02-07 09:15:26 +000072struct FWCfgState {
Hu Tao2ce92a12013-07-01 18:18:32 +080073 /*< private >*/
74 SysBusDevice parent_obj;
75 /*< public >*/
76
Laszlo Erseke12f3a12017-01-12 19:24:15 +010077 uint16_t file_slots;
78 FWCfgEntry *entries[2];
79 int *entry_order;
Gerd Hoffmannabe147e2009-12-18 12:01:10 +010080 FWCfgFiles *files;
blueswir13cce6242008-09-18 18:27:29 +000081 uint16_t cur_entry;
Juan Quintelaff061082009-11-13 11:59:20 +010082 uint32_t cur_offset;
Gleb Natapov962630f2010-12-08 13:35:09 +020083 Notifier machine_ready;
Marc María4c0d1d2015-10-08 17:02:55 +020084
Gerd Hoffmannbab47d92016-04-07 09:12:58 -050085 int fw_cfg_order_override;
86
Marc María4c0d1d2015-10-08 17:02:55 +020087 bool dma_enabled;
88 dma_addr_t dma_addr;
89 AddressSpace *dma_as;
90 MemoryRegion dma_iomem;
Gerd Hoffmannc2b5bda2009-12-18 12:01:09 +010091};
blueswir13cce6242008-09-18 18:27:29 +000092
Laszlo Ersek5712db62014-12-22 13:11:35 +010093struct FWCfgIoState {
94 /*< private >*/
95 FWCfgState parent_obj;
96 /*< public >*/
97
98 MemoryRegion comb_iomem;
Marc María4c0d1d2015-10-08 17:02:55 +020099 uint32_t iobase, dma_iobase;
Laszlo Ersek5712db62014-12-22 13:11:35 +0100100};
101
102struct FWCfgMemState {
103 /*< private >*/
104 FWCfgState parent_obj;
105 /*< public >*/
106
107 MemoryRegion ctl_iomem, data_iomem;
Laszlo Ersekcfaadf02014-12-22 13:11:40 +0100108 uint32_t data_width;
109 MemoryRegionOps wide_data_ops;
Laszlo Ersek5712db62014-12-22 13:11:35 +0100110};
111
wayne3d3b8302011-07-27 18:04:55 +0800112#define JPG_FILE 0
113#define BMP_FILE 1
114
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +1000115static char *read_splashfile(char *filename, gsize *file_sizep,
Markus Armbrusterd09acb92013-01-23 18:25:08 +0100116 int *file_typep)
wayne3d3b8302011-07-27 18:04:55 +0800117{
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400118 GError *err = NULL;
119 gboolean res;
120 gchar *content;
Markus Armbruster9f8863e2013-01-23 18:25:09 +0100121 int file_type;
122 unsigned int filehead;
wayne3d3b8302011-07-27 18:04:55 +0800123 int bmp_bpp;
124
Markus Armbrusterd09acb92013-01-23 18:25:08 +0100125 res = g_file_get_contents(filename, &content, file_sizep, &err);
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400126 if (res == FALSE) {
127 error_report("failed to read splash file '%s'", filename);
128 g_error_free(err);
129 return NULL;
wayne3d3b8302011-07-27 18:04:55 +0800130 }
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400131
wayne3d3b8302011-07-27 18:04:55 +0800132 /* check file size */
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400133 if (*file_sizep < 30) {
134 goto error;
wayne3d3b8302011-07-27 18:04:55 +0800135 }
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400136
wayne3d3b8302011-07-27 18:04:55 +0800137 /* check magic ID */
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400138 filehead = ((content[0] & 0xff) + (content[1] << 8)) & 0xffff;
139 if (filehead == 0xd8ff) {
wayne3d3b8302011-07-27 18:04:55 +0800140 file_type = JPG_FILE;
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400141 } else if (filehead == 0x4d42) {
142 file_type = BMP_FILE;
wayne3d3b8302011-07-27 18:04:55 +0800143 } else {
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400144 goto error;
wayne3d3b8302011-07-27 18:04:55 +0800145 }
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400146
wayne3d3b8302011-07-27 18:04:55 +0800147 /* check BMP bpp */
148 if (file_type == BMP_FILE) {
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400149 bmp_bpp = (content[28] + (content[29] << 8)) & 0xffff;
wayne3d3b8302011-07-27 18:04:55 +0800150 if (bmp_bpp != 24) {
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400151 goto error;
wayne3d3b8302011-07-27 18:04:55 +0800152 }
153 }
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400154
wayne3d3b8302011-07-27 18:04:55 +0800155 /* return values */
wayne3d3b8302011-07-27 18:04:55 +0800156 *file_typep = file_type;
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400157
158 return content;
159
160error:
161 error_report("splash file '%s' format not recognized; must be JPEG "
162 "or 24 bit BMP", filename);
163 g_free(content);
164 return NULL;
wayne3d3b8302011-07-27 18:04:55 +0800165}
166
167static void fw_cfg_bootsplash(FWCfgState *s)
168{
169 int boot_splash_time = -1;
170 const char *boot_splash_filename = NULL;
171 char *p;
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400172 char *filename, *file_data;
Peter Crosthwaite3d1bba22013-05-22 13:01:43 +1000173 gsize file_size;
Markus Armbruster9f8863e2013-01-23 18:25:09 +0100174 int file_type;
wayne3d3b8302011-07-27 18:04:55 +0800175 const char *temp;
176
177 /* get user configuration */
178 QemuOptsList *plist = qemu_find_opts("boot-opts");
179 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
180 if (opts != NULL) {
181 temp = qemu_opt_get(opts, "splash");
182 if (temp != NULL) {
183 boot_splash_filename = temp;
184 }
185 temp = qemu_opt_get(opts, "splash-time");
186 if (temp != NULL) {
187 p = (char *)temp;
Laurent Vivierec8193a2016-06-15 18:14:35 +0200188 boot_splash_time = strtol(p, &p, 10);
wayne3d3b8302011-07-27 18:04:55 +0800189 }
190 }
191
192 /* insert splash time if user configurated */
193 if (boot_splash_time >= 0) {
194 /* validate the input */
195 if (boot_splash_time > 0xffff) {
196 error_report("splash time is big than 65535, force it to 65535.");
197 boot_splash_time = 0xffff;
198 }
199 /* use little endian format */
200 qemu_extra_params_fw[0] = (uint8_t)(boot_splash_time & 0xff);
201 qemu_extra_params_fw[1] = (uint8_t)((boot_splash_time >> 8) & 0xff);
202 fw_cfg_add_file(s, "etc/boot-menu-wait", qemu_extra_params_fw, 2);
203 }
204
205 /* insert splash file if user configurated */
206 if (boot_splash_filename != NULL) {
207 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, boot_splash_filename);
208 if (filename == NULL) {
209 error_report("failed to find file '%s'.", boot_splash_filename);
210 return;
211 }
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400212
213 /* loading file data */
214 file_data = read_splashfile(filename, &file_size, &file_type);
215 if (file_data == NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500216 g_free(filename);
wayne3d3b8302011-07-27 18:04:55 +0800217 return;
218 }
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +0100219 g_free(boot_splash_filedata);
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400220 boot_splash_filedata = (uint8_t *)file_data;
wayne3d3b8302011-07-27 18:04:55 +0800221 boot_splash_filedata_size = file_size;
Pavel Borzenkov9477c872011-10-24 07:31:30 -0400222
wayne3d3b8302011-07-27 18:04:55 +0800223 /* insert data */
224 if (file_type == JPG_FILE) {
225 fw_cfg_add_file(s, "bootsplash.jpg",
226 boot_splash_filedata, boot_splash_filedata_size);
227 } else {
228 fw_cfg_add_file(s, "bootsplash.bmp",
229 boot_splash_filedata, boot_splash_filedata_size);
230 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500231 g_free(filename);
wayne3d3b8302011-07-27 18:04:55 +0800232 }
233}
234
Amos Kongac05f342012-09-07 11:11:03 +0800235static void fw_cfg_reboot(FWCfgState *s)
236{
237 int reboot_timeout = -1;
238 char *p;
239 const char *temp;
240
241 /* get user configuration */
242 QemuOptsList *plist = qemu_find_opts("boot-opts");
243 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
244 if (opts != NULL) {
245 temp = qemu_opt_get(opts, "reboot-timeout");
246 if (temp != NULL) {
247 p = (char *)temp;
Laurent Vivierec8193a2016-06-15 18:14:35 +0200248 reboot_timeout = strtol(p, &p, 10);
Amos Kongac05f342012-09-07 11:11:03 +0800249 }
250 }
251 /* validate the input */
252 if (reboot_timeout > 0xffff) {
253 error_report("reboot timeout is larger than 65535, force it to 65535.");
254 reboot_timeout = 0xffff;
255 }
256 fw_cfg_add_file(s, "etc/boot-fail-wait", g_memdup(&reboot_timeout, 4), 4);
257}
258
blueswir13cce6242008-09-18 18:27:29 +0000259static void fw_cfg_write(FWCfgState *s, uint8_t value)
260{
Gabriel L. Somlo023e3142015-04-29 11:21:50 -0400261 /* nothing, write support removed in QEMU v2.4+ */
blueswir13cce6242008-09-18 18:27:29 +0000262}
263
Laszlo Erseke12f3a12017-01-12 19:24:15 +0100264static inline uint16_t fw_cfg_file_slots(const FWCfgState *s)
265{
266 return s->file_slots;
267}
268
269/* Note: this function returns an exclusive limit. */
270static inline uint32_t fw_cfg_max_entry(const FWCfgState *s)
271{
272 return FW_CFG_FILE_FIRST + fw_cfg_file_slots(s);
273}
274
blueswir13cce6242008-09-18 18:27:29 +0000275static int fw_cfg_select(FWCfgState *s, uint16_t key)
276{
Gabriel L. Somlo3bef7e82015-11-05 09:32:48 -0500277 int arch, ret;
278 FWCfgEntry *e;
blueswir13cce6242008-09-18 18:27:29 +0000279
280 s->cur_offset = 0;
Laszlo Erseke12f3a12017-01-12 19:24:15 +0100281 if ((key & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s)) {
blueswir13cce6242008-09-18 18:27:29 +0000282 s->cur_entry = FW_CFG_INVALID;
283 ret = 0;
284 } else {
285 s->cur_entry = key;
286 ret = 1;
Gabriel L. Somlo3bef7e82015-11-05 09:32:48 -0500287 /* entry successfully selected, now run callback if present */
288 arch = !!(key & FW_CFG_ARCH_LOCAL);
289 e = &s->entries[arch][key & FW_CFG_ENTRY_MASK];
290 if (e->read_callback) {
Gabriel L. Somlo3f8752b2015-11-05 09:32:49 -0500291 e->read_callback(e->callback_opaque);
Gabriel L. Somlo3bef7e82015-11-05 09:32:48 -0500292 }
blueswir13cce6242008-09-18 18:27:29 +0000293 }
294
Markus Armbrusterf6e35342013-01-16 14:50:22 +0100295 trace_fw_cfg_select(s, key, ret);
blueswir13cce6242008-09-18 18:27:29 +0000296 return ret;
297}
298
Gabriel L. Somlo38bf2092015-11-05 09:32:51 -0500299static uint64_t fw_cfg_data_read(void *opaque, hwaddr addr, unsigned size)
300{
301 FWCfgState *s = opaque;
302 int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
303 FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
304 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
305 uint64_t value = 0;
306
307 assert(size > 0 && size <= sizeof(value));
308 if (s->cur_entry != FW_CFG_INVALID && e->data && s->cur_offset < e->len) {
309 /* The least significant 'size' bytes of the return value are
310 * expected to contain a string preserving portion of the item
311 * data, padded with zeros on the right in case we run out early.
312 * In technical terms, we're composing the host-endian representation
313 * of the big endian interpretation of the fw_cfg string.
314 */
315 do {
316 value = (value << 8) | e->data[s->cur_offset++];
317 } while (--size && s->cur_offset < e->len);
318 /* If size is still not zero, we *did* run out early, so continue
319 * left-shifting, to add the appropriate number of padding zeros
320 * on the right.
321 */
322 value <<= 8 * size;
323 }
324
325 trace_fw_cfg_read(s, value);
326 return value;
327}
328
Avi Kivitya8170e52012-10-23 12:30:10 +0200329static void fw_cfg_data_mem_write(void *opaque, hwaddr addr,
Avi Kivity561e1822011-11-13 15:05:28 +0200330 uint64_t value, unsigned size)
blueswir13cce6242008-09-18 18:27:29 +0000331{
Laszlo Ersekcfaadf02014-12-22 13:11:40 +0100332 FWCfgState *s = opaque;
Laszlo Ersek36b62ae2015-01-16 11:54:30 +0000333 unsigned i = size;
Laszlo Ersekcfaadf02014-12-22 13:11:40 +0100334
Laszlo Ersek36b62ae2015-01-16 11:54:30 +0000335 do {
336 fw_cfg_write(s, value >> (8 * --i));
337 } while (i);
Laszlo Ersekcfaadf02014-12-22 13:11:40 +0100338}
339
Marc María4c0d1d2015-10-08 17:02:55 +0200340static void fw_cfg_dma_transfer(FWCfgState *s)
341{
342 dma_addr_t len;
343 FWCfgDmaAccess dma;
344 int arch;
345 FWCfgEntry *e;
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100346 int read = 0, write = 0;
Marc María4c0d1d2015-10-08 17:02:55 +0200347 dma_addr_t dma_addr;
348
349 /* Reset the address before the next access */
350 dma_addr = s->dma_addr;
351 s->dma_addr = 0;
352
353 if (dma_memory_read(s->dma_as, dma_addr, &dma, sizeof(dma))) {
354 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
355 FW_CFG_DMA_CTL_ERROR);
356 return;
357 }
358
359 dma.address = be64_to_cpu(dma.address);
360 dma.length = be32_to_cpu(dma.length);
361 dma.control = be32_to_cpu(dma.control);
362
363 if (dma.control & FW_CFG_DMA_CTL_SELECT) {
364 fw_cfg_select(s, dma.control >> 16);
365 }
366
367 arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
Gabriel L. Somlo66f8fd92015-11-05 09:32:50 -0500368 e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
369 &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
Marc María4c0d1d2015-10-08 17:02:55 +0200370
371 if (dma.control & FW_CFG_DMA_CTL_READ) {
372 read = 1;
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100373 write = 0;
374 } else if (dma.control & FW_CFG_DMA_CTL_WRITE) {
375 read = 0;
376 write = 1;
Marc María4c0d1d2015-10-08 17:02:55 +0200377 } else if (dma.control & FW_CFG_DMA_CTL_SKIP) {
378 read = 0;
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100379 write = 0;
Marc María4c0d1d2015-10-08 17:02:55 +0200380 } else {
381 dma.length = 0;
382 }
383
384 dma.control = 0;
385
386 while (dma.length > 0 && !(dma.control & FW_CFG_DMA_CTL_ERROR)) {
387 if (s->cur_entry == FW_CFG_INVALID || !e->data ||
388 s->cur_offset >= e->len) {
389 len = dma.length;
390
391 /* If the access is not a read access, it will be a skip access,
392 * tested before.
393 */
394 if (read) {
395 if (dma_memory_set(s->dma_as, dma.address, 0, len)) {
396 dma.control |= FW_CFG_DMA_CTL_ERROR;
397 }
398 }
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100399 if (write) {
400 dma.control |= FW_CFG_DMA_CTL_ERROR;
401 }
Marc María4c0d1d2015-10-08 17:02:55 +0200402 } else {
403 if (dma.length <= (e->len - s->cur_offset)) {
404 len = dma.length;
405 } else {
406 len = (e->len - s->cur_offset);
407 }
408
Marc María4c0d1d2015-10-08 17:02:55 +0200409 /* If the access is not a read access, it will be a skip access,
410 * tested before.
411 */
412 if (read) {
413 if (dma_memory_write(s->dma_as, dma.address,
414 &e->data[s->cur_offset], len)) {
415 dma.control |= FW_CFG_DMA_CTL_ERROR;
416 }
417 }
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100418 if (write) {
419 if (!e->allow_write ||
420 len != dma.length ||
421 dma_memory_read(s->dma_as, dma.address,
422 &e->data[s->cur_offset], len)) {
423 dma.control |= FW_CFG_DMA_CTL_ERROR;
424 }
425 }
Marc María4c0d1d2015-10-08 17:02:55 +0200426
427 s->cur_offset += len;
428 }
429
430 dma.address += len;
431 dma.length -= len;
432
433 }
434
435 stl_be_dma(s->dma_as, dma_addr + offsetof(FWCfgDmaAccess, control),
436 dma.control);
437
438 trace_fw_cfg_read(s, 0);
439}
440
Kevin O'Connor2cc06a82015-10-08 17:02:58 +0200441static uint64_t fw_cfg_dma_mem_read(void *opaque, hwaddr addr,
442 unsigned size)
443{
444 /* Return a signature value (and handle various read sizes) */
445 return extract64(FW_CFG_DMA_SIGNATURE, (8 - addr - size) * 8, size * 8);
446}
447
Marc María4c0d1d2015-10-08 17:02:55 +0200448static void fw_cfg_dma_mem_write(void *opaque, hwaddr addr,
449 uint64_t value, unsigned size)
450{
451 FWCfgState *s = opaque;
452
453 if (size == 4) {
454 if (addr == 0) {
455 /* FWCfgDmaAccess high address */
456 s->dma_addr = value << 32;
457 } else if (addr == 4) {
458 /* FWCfgDmaAccess low address */
459 s->dma_addr |= value;
460 fw_cfg_dma_transfer(s);
461 }
462 } else if (size == 8 && addr == 0) {
463 s->dma_addr = value;
464 fw_cfg_dma_transfer(s);
465 }
466}
467
468static bool fw_cfg_dma_mem_valid(void *opaque, hwaddr addr,
469 unsigned size, bool is_write)
470{
Kevin O'Connor2cc06a82015-10-08 17:02:58 +0200471 return !is_write || ((size == 4 && (addr == 0 || addr == 4)) ||
472 (size == 8 && addr == 0));
Marc María4c0d1d2015-10-08 17:02:55 +0200473}
474
Laszlo Ersekcfaadf02014-12-22 13:11:40 +0100475static bool fw_cfg_data_mem_valid(void *opaque, hwaddr addr,
476 unsigned size, bool is_write)
477{
478 return addr == 0;
blueswir13cce6242008-09-18 18:27:29 +0000479}
480
Avi Kivitya8170e52012-10-23 12:30:10 +0200481static void fw_cfg_ctl_mem_write(void *opaque, hwaddr addr,
Avi Kivity561e1822011-11-13 15:05:28 +0200482 uint64_t value, unsigned size)
blueswir13cce6242008-09-18 18:27:29 +0000483{
484 fw_cfg_select(opaque, (uint16_t)value);
485}
486
Avi Kivitya8170e52012-10-23 12:30:10 +0200487static bool fw_cfg_ctl_mem_valid(void *opaque, hwaddr addr,
Avi Kivity561e1822011-11-13 15:05:28 +0200488 unsigned size, bool is_write)
489{
490 return is_write && size == 2;
491}
492
Avi Kivitya8170e52012-10-23 12:30:10 +0200493static void fw_cfg_comb_write(void *opaque, hwaddr addr,
Avi Kivity561e1822011-11-13 15:05:28 +0200494 uint64_t value, unsigned size)
blueswir13cce6242008-09-18 18:27:29 +0000495{
Avi Kivity561e1822011-11-13 15:05:28 +0200496 switch (size) {
497 case 1:
498 fw_cfg_write(opaque, (uint8_t)value);
499 break;
500 case 2:
501 fw_cfg_select(opaque, (uint16_t)value);
502 break;
503 }
blueswir13cce6242008-09-18 18:27:29 +0000504}
505
Avi Kivitya8170e52012-10-23 12:30:10 +0200506static bool fw_cfg_comb_valid(void *opaque, hwaddr addr,
Avi Kivity561e1822011-11-13 15:05:28 +0200507 unsigned size, bool is_write)
blueswir13cce6242008-09-18 18:27:29 +0000508{
Avi Kivity561e1822011-11-13 15:05:28 +0200509 return (size == 1) || (is_write && size == 2);
blueswir13cce6242008-09-18 18:27:29 +0000510}
511
Avi Kivity561e1822011-11-13 15:05:28 +0200512static const MemoryRegionOps fw_cfg_ctl_mem_ops = {
513 .write = fw_cfg_ctl_mem_write,
Laszlo Ersekd789c842014-12-22 13:11:38 +0100514 .endianness = DEVICE_BIG_ENDIAN,
Avi Kivity561e1822011-11-13 15:05:28 +0200515 .valid.accepts = fw_cfg_ctl_mem_valid,
blueswir13cce6242008-09-18 18:27:29 +0000516};
517
Avi Kivity561e1822011-11-13 15:05:28 +0200518static const MemoryRegionOps fw_cfg_data_mem_ops = {
Gabriel L. Somlo38bf2092015-11-05 09:32:51 -0500519 .read = fw_cfg_data_read,
Avi Kivity561e1822011-11-13 15:05:28 +0200520 .write = fw_cfg_data_mem_write,
Laszlo Ersekd789c842014-12-22 13:11:38 +0100521 .endianness = DEVICE_BIG_ENDIAN,
Avi Kivity561e1822011-11-13 15:05:28 +0200522 .valid = {
523 .min_access_size = 1,
524 .max_access_size = 1,
Laszlo Ersekcfaadf02014-12-22 13:11:40 +0100525 .accepts = fw_cfg_data_mem_valid,
Avi Kivity561e1822011-11-13 15:05:28 +0200526 },
blueswir13cce6242008-09-18 18:27:29 +0000527};
528
Avi Kivity561e1822011-11-13 15:05:28 +0200529static const MemoryRegionOps fw_cfg_comb_mem_ops = {
Gabriel L. Somlo6c8d56a2015-11-05 09:32:52 -0500530 .read = fw_cfg_data_read,
Avi Kivity561e1822011-11-13 15:05:28 +0200531 .write = fw_cfg_comb_write,
Paolo Bonzini6fdf98f2013-07-28 14:35:54 +0200532 .endianness = DEVICE_LITTLE_ENDIAN,
Avi Kivity561e1822011-11-13 15:05:28 +0200533 .valid.accepts = fw_cfg_comb_valid,
blueswir13cce6242008-09-18 18:27:29 +0000534};
535
Marc María4c0d1d2015-10-08 17:02:55 +0200536static const MemoryRegionOps fw_cfg_dma_mem_ops = {
Kevin O'Connor2cc06a82015-10-08 17:02:58 +0200537 .read = fw_cfg_dma_mem_read,
Marc María4c0d1d2015-10-08 17:02:55 +0200538 .write = fw_cfg_dma_mem_write,
539 .endianness = DEVICE_BIG_ENDIAN,
540 .valid.accepts = fw_cfg_dma_mem_valid,
541 .valid.max_access_size = 8,
542 .impl.max_access_size = 8,
543};
544
Blue Swirl3a5c16f2010-06-27 16:04:55 +0000545static void fw_cfg_reset(DeviceState *d)
blueswir13cce6242008-09-18 18:27:29 +0000546{
Hu Tao2ce92a12013-07-01 18:18:32 +0800547 FWCfgState *s = FW_CFG(d);
blueswir13cce6242008-09-18 18:27:29 +0000548
Gabriel L. Somlo3bef7e82015-11-05 09:32:48 -0500549 /* we never register a read callback for FW_CFG_SIGNATURE */
550 fw_cfg_select(s, FW_CFG_SIGNATURE);
blueswir13cce6242008-09-18 18:27:29 +0000551}
552
Juan Quintelaff061082009-11-13 11:59:20 +0100553/* Save restore 32 bit int as uint16_t
554 This is a Big hack, but it is how the old state did it.
555 Or we broke compatibility in the state, or we can't use struct tm
556 */
557
558static int get_uint32_as_uint16(QEMUFile *f, void *pv, size_t size)
559{
560 uint32_t *v = pv;
561 *v = qemu_get_be16(f);
562 return 0;
563}
564
565static void put_unused(QEMUFile *f, void *pv, size_t size)
566{
Vagrant Cascadian66c80e72010-03-14 08:51:53 +0000567 fprintf(stderr, "uint32_as_uint16 is only used for backward compatibility.\n");
Juan Quintelaff061082009-11-13 11:59:20 +0100568 fprintf(stderr, "This functions shouldn't be called.\n");
569}
570
Blue Swirld05ac8f2009-12-04 20:44:44 +0000571static const VMStateInfo vmstate_hack_uint32_as_uint16 = {
Juan Quintelaff061082009-11-13 11:59:20 +0100572 .name = "int32_as_uint16",
573 .get = get_uint32_as_uint16,
574 .put = put_unused,
575};
576
577#define VMSTATE_UINT16_HACK(_f, _s, _t) \
578 VMSTATE_SINGLE_TEST(_f, _s, _t, 0, vmstate_hack_uint32_as_uint16, uint32_t)
579
580
581static bool is_version_1(void *opaque, int version_id)
582{
583 return version_id == 1;
584}
585
Marc Maríb2a575a2016-05-23 19:11:33 +0100586bool fw_cfg_dma_enabled(void *opaque)
Marc María4c0d1d2015-10-08 17:02:55 +0200587{
588 FWCfgState *s = opaque;
589
590 return s->dma_enabled;
591}
592
593static const VMStateDescription vmstate_fw_cfg_dma = {
594 .name = "fw_cfg/dma",
595 .needed = fw_cfg_dma_enabled,
596 .fields = (VMStateField[]) {
597 VMSTATE_UINT64(dma_addr, FWCfgState),
598 VMSTATE_END_OF_LIST()
599 },
600};
601
Juan Quintela7d2edd42009-09-10 03:04:34 +0200602static const VMStateDescription vmstate_fw_cfg = {
603 .name = "fw_cfg",
Juan Quintelaff061082009-11-13 11:59:20 +0100604 .version_id = 2,
Juan Quintela7d2edd42009-09-10 03:04:34 +0200605 .minimum_version_id = 1,
Juan Quintelad49805a2014-04-16 15:32:32 +0200606 .fields = (VMStateField[]) {
Juan Quintela7d2edd42009-09-10 03:04:34 +0200607 VMSTATE_UINT16(cur_entry, FWCfgState),
Juan Quintelaff061082009-11-13 11:59:20 +0100608 VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),
609 VMSTATE_UINT32_V(cur_offset, FWCfgState, 2),
Juan Quintela7d2edd42009-09-10 03:04:34 +0200610 VMSTATE_END_OF_LIST()
Marc María4c0d1d2015-10-08 17:02:55 +0200611 },
612 .subsections = (const VMStateDescription*[]) {
613 &vmstate_fw_cfg_dma,
614 NULL,
Juan Quintela7d2edd42009-09-10 03:04:34 +0200615 }
616};
blueswir13cce6242008-09-18 18:27:29 +0000617
Michael S. Tsirkind87072c2013-09-01 17:56:20 +0300618static void fw_cfg_add_bytes_read_callback(FWCfgState *s, uint16_t key,
619 FWCfgReadCallback callback,
620 void *callback_opaque,
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100621 void *data, size_t len,
622 bool read_only)
blueswir13cce6242008-09-18 18:27:29 +0000623{
blueswir13cce6242008-09-18 18:27:29 +0000624 int arch = !!(key & FW_CFG_ARCH_LOCAL);
625
626 key &= FW_CFG_ENTRY_MASK;
627
Laszlo Erseke12f3a12017-01-12 19:24:15 +0100628 assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
Gabriel L. Somlo0f9b2142015-04-29 11:21:51 -0400629 assert(s->entries[arch][key].data == NULL); /* avoid key conflict */
blueswir13cce6242008-09-18 18:27:29 +0000630
631 s->entries[arch][key].data = data;
Markus Armbruster089da572013-01-16 14:50:28 +0100632 s->entries[arch][key].len = (uint32_t)len;
Michael S. Tsirkind87072c2013-09-01 17:56:20 +0300633 s->entries[arch][key].read_callback = callback;
634 s->entries[arch][key].callback_opaque = callback_opaque;
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100635 s->entries[arch][key].allow_write = !read_only;
Michael S. Tsirkind87072c2013-09-01 17:56:20 +0300636}
637
Gongleibdbb5b12014-10-07 16:00:08 +0800638static void *fw_cfg_modify_bytes_read(FWCfgState *s, uint16_t key,
639 void *data, size_t len)
640{
641 void *ptr;
642 int arch = !!(key & FW_CFG_ARCH_LOCAL);
643
644 key &= FW_CFG_ENTRY_MASK;
645
Laszlo Erseke12f3a12017-01-12 19:24:15 +0100646 assert(key < fw_cfg_max_entry(s) && len < UINT32_MAX);
Gongleibdbb5b12014-10-07 16:00:08 +0800647
648 /* return the old data to the function caller, avoid memory leak */
649 ptr = s->entries[arch][key].data;
650 s->entries[arch][key].data = data;
651 s->entries[arch][key].len = len;
652 s->entries[arch][key].callback_opaque = NULL;
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100653 s->entries[arch][key].allow_write = false;
Gongleibdbb5b12014-10-07 16:00:08 +0800654
655 return ptr;
656}
657
Michael S. Tsirkind87072c2013-09-01 17:56:20 +0300658void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len)
659{
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100660 fw_cfg_add_bytes_read_callback(s, key, NULL, NULL, data, len, true);
blueswir13cce6242008-09-18 18:27:29 +0000661}
662
Markus Armbruster44687f72013-01-16 14:50:24 +0100663void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value)
664{
665 size_t sz = strlen(value) + 1;
666
Stefan Weile7ae7712015-03-08 19:30:01 +0100667 fw_cfg_add_bytes(s, key, g_memdup(value, sz), sz);
Markus Armbruster44687f72013-01-16 14:50:24 +0100668}
669
Markus Armbruster4cad3862013-01-16 14:50:23 +0100670void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value)
blueswir13cce6242008-09-18 18:27:29 +0000671{
672 uint16_t *copy;
673
Anthony Liguori7267c092011-08-20 22:09:37 -0500674 copy = g_malloc(sizeof(value));
blueswir13cce6242008-09-18 18:27:29 +0000675 *copy = cpu_to_le16(value);
Markus Armbruster089da572013-01-16 14:50:28 +0100676 fw_cfg_add_bytes(s, key, copy, sizeof(value));
blueswir13cce6242008-09-18 18:27:29 +0000677}
678
Gabriel L. Somlo1edd34b2015-06-08 14:10:44 -0400679void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value)
680{
681 uint16_t *copy, *old;
682
683 copy = g_malloc(sizeof(value));
684 *copy = cpu_to_le16(value);
685 old = fw_cfg_modify_bytes_read(s, key, copy, sizeof(value));
686 g_free(old);
687}
688
Markus Armbruster4cad3862013-01-16 14:50:23 +0100689void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value)
blueswir13cce6242008-09-18 18:27:29 +0000690{
691 uint32_t *copy;
692
Anthony Liguori7267c092011-08-20 22:09:37 -0500693 copy = g_malloc(sizeof(value));
blueswir13cce6242008-09-18 18:27:29 +0000694 *copy = cpu_to_le32(value);
Markus Armbruster089da572013-01-16 14:50:28 +0100695 fw_cfg_add_bytes(s, key, copy, sizeof(value));
blueswir13cce6242008-09-18 18:27:29 +0000696}
697
Markus Armbruster4cad3862013-01-16 14:50:23 +0100698void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value)
blueswir13cce6242008-09-18 18:27:29 +0000699{
700 uint64_t *copy;
701
Anthony Liguori7267c092011-08-20 22:09:37 -0500702 copy = g_malloc(sizeof(value));
blueswir13cce6242008-09-18 18:27:29 +0000703 *copy = cpu_to_le64(value);
Markus Armbruster089da572013-01-16 14:50:28 +0100704 fw_cfg_add_bytes(s, key, copy, sizeof(value));
blueswir13cce6242008-09-18 18:27:29 +0000705}
706
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500707void fw_cfg_set_order_override(FWCfgState *s, int order)
708{
709 assert(s->fw_cfg_order_override == 0);
710 s->fw_cfg_order_override = order;
711}
712
713void fw_cfg_reset_order_override(FWCfgState *s)
714{
715 assert(s->fw_cfg_order_override != 0);
716 s->fw_cfg_order_override = 0;
717}
718
719/*
720 * This is the legacy order list. For legacy systems, files are in
721 * the fw_cfg in the order defined below, by the "order" value. Note
722 * that some entries (VGA ROMs, NIC option ROMS, etc.) go into a
723 * specific area, but there may be more than one and they occur in the
724 * order that the user specifies them on the command line. Those are
725 * handled in a special manner, using the order override above.
726 *
727 * For non-legacy, the files are sorted by filename to avoid this kind
728 * of complexity in the future.
729 *
730 * This is only for x86, other arches don't implement versioning so
731 * they won't set legacy mode.
732 */
733static struct {
734 const char *name;
735 int order;
736} fw_cfg_order[] = {
737 { "etc/boot-menu-wait", 10 },
738 { "bootsplash.jpg", 11 },
739 { "bootsplash.bmp", 12 },
740 { "etc/boot-fail-wait", 15 },
741 { "etc/smbios/smbios-tables", 20 },
742 { "etc/smbios/smbios-anchor", 30 },
743 { "etc/e820", 40 },
744 { "etc/reserved-memory-end", 50 },
745 { "genroms/kvmvapic.bin", 55 },
746 { "genroms/linuxboot.bin", 60 },
747 { }, /* VGA ROMs from pc_vga_init come here, 70. */
748 { }, /* NIC option ROMs from pc_nic_init come here, 80. */
749 { "etc/system-states", 90 },
750 { }, /* User ROMs come here, 100. */
751 { }, /* Device FW comes here, 110. */
752 { "etc/extra-pci-roots", 120 },
753 { "etc/acpi/tables", 130 },
754 { "etc/table-loader", 140 },
755 { "etc/tpm/log", 150 },
756 { "etc/acpi/rsdp", 160 },
757 { "bootorder", 170 },
758
759#define FW_CFG_ORDER_OVERRIDE_LAST 200
760};
761
762static int get_fw_cfg_order(FWCfgState *s, const char *name)
763{
764 int i;
765
Cao jina8d38f32016-05-18 18:59:36 +0800766 if (s->fw_cfg_order_override > 0) {
767 return s->fw_cfg_order_override;
768 }
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500769
770 for (i = 0; i < ARRAY_SIZE(fw_cfg_order); i++) {
Cao jina8d38f32016-05-18 18:59:36 +0800771 if (fw_cfg_order[i].name == NULL) {
772 continue;
773 }
774
775 if (strcmp(name, fw_cfg_order[i].name) == 0) {
776 return fw_cfg_order[i].order;
777 }
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500778 }
Cao jina8d38f32016-05-18 18:59:36 +0800779
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500780 /* Stick unknown stuff at the end. */
Markus Armbrusterdf3c2862016-08-03 13:37:51 +0200781 error_report("warning: Unknown firmware file in legacy mode: %s", name);
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500782 return FW_CFG_ORDER_OVERRIDE_LAST;
783}
784
Michael S. Tsirkind87072c2013-09-01 17:56:20 +0300785void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
786 FWCfgReadCallback callback, void *callback_opaque,
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100787 void *data, size_t len, bool read_only)
Gerd Hoffmannabe147e2009-12-18 12:01:10 +0100788{
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500789 int i, index, count;
Markus Armbruster089da572013-01-16 14:50:28 +0100790 size_t dsize;
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500791 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
792 int order = 0;
Gerd Hoffmannabe147e2009-12-18 12:01:10 +0100793
794 if (!s->files) {
Laszlo Erseke12f3a12017-01-12 19:24:15 +0100795 dsize = sizeof(uint32_t) + sizeof(FWCfgFile) * fw_cfg_file_slots(s);
Anthony Liguori7267c092011-08-20 22:09:37 -0500796 s->files = g_malloc0(dsize);
Markus Armbruster089da572013-01-16 14:50:28 +0100797 fw_cfg_add_bytes(s, FW_CFG_FILE_DIR, s->files, dsize);
Gerd Hoffmannabe147e2009-12-18 12:01:10 +0100798 }
799
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500800 count = be32_to_cpu(s->files->count);
Laszlo Erseke12f3a12017-01-12 19:24:15 +0100801 assert(count < fw_cfg_file_slots(s));
Gerd Hoffmannabe147e2009-12-18 12:01:10 +0100802
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500803 /* Find the insertion point. */
804 if (mc->legacy_fw_cfg_order) {
805 /*
806 * Sort by order. For files with the same order, we keep them
807 * in the sequence in which they were added.
808 */
809 order = get_fw_cfg_order(s, filename);
810 for (index = count;
811 index > 0 && order < s->entry_order[index - 1];
812 index--);
813 } else {
814 /* Sort by file name. */
815 for (index = count;
816 index > 0 && strcmp(filename, s->files->f[index - 1].name) < 0;
817 index--);
818 }
819
820 /*
821 * Move all the entries from the index point and after down one
822 * to create a slot for the new entry. Because calculations are
823 * being done with the index, make it so that "i" is the current
824 * index and "i - 1" is the one being copied from, thus the
825 * unusual start and end in the for statement.
826 */
827 for (i = count + 1; i > index; i--) {
828 s->files->f[i] = s->files->f[i - 1];
829 s->files->f[i].select = cpu_to_be16(FW_CFG_FILE_FIRST + i);
830 s->entries[0][FW_CFG_FILE_FIRST + i] =
831 s->entries[0][FW_CFG_FILE_FIRST + i - 1];
832 s->entry_order[i] = s->entry_order[i - 1];
833 }
834
835 memset(&s->files->f[index], 0, sizeof(FWCfgFile));
836 memset(&s->entries[0][FW_CFG_FILE_FIRST + index], 0, sizeof(FWCfgEntry));
837
838 pstrcpy(s->files->f[index].name, sizeof(s->files->f[index].name), filename);
839 for (i = 0; i <= count; i++) {
840 if (i != index &&
841 strcmp(s->files->f[index].name, s->files->f[i].name) == 0) {
Gabriel L. Somlo0eb973f2015-04-29 11:21:52 -0400842 error_report("duplicate fw_cfg file name: %s",
843 s->files->f[index].name);
844 exit(1);
Gerd Hoffmannde9352b2010-01-08 15:25:39 +0100845 }
Gerd Hoffmannabe147e2009-12-18 12:01:10 +0100846 }
Gerd Hoffmannde9352b2010-01-08 15:25:39 +0100847
Gabriel L. Somlo0eb973f2015-04-29 11:21:52 -0400848 fw_cfg_add_bytes_read_callback(s, FW_CFG_FILE_FIRST + index,
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100849 callback, callback_opaque, data, len,
850 read_only);
Gabriel L. Somlo0eb973f2015-04-29 11:21:52 -0400851
Gerd Hoffmannabe147e2009-12-18 12:01:10 +0100852 s->files->f[index].size = cpu_to_be32(len);
853 s->files->f[index].select = cpu_to_be16(FW_CFG_FILE_FIRST + index);
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500854 s->entry_order[index] = order;
Markus Armbrusterf6e35342013-01-16 14:50:22 +0100855 trace_fw_cfg_add_file(s, index, s->files->f[index].name, len);
Gerd Hoffmannabe147e2009-12-18 12:01:10 +0100856
Gerd Hoffmannbab47d92016-04-07 09:12:58 -0500857 s->files->count = cpu_to_be32(count+1);
Gerd Hoffmannabe147e2009-12-18 12:01:10 +0100858}
859
Michael S. Tsirkind87072c2013-09-01 17:56:20 +0300860void fw_cfg_add_file(FWCfgState *s, const char *filename,
861 void *data, size_t len)
862{
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100863 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true);
Michael S. Tsirkind87072c2013-09-01 17:56:20 +0300864}
865
Gongleibdbb5b12014-10-07 16:00:08 +0800866void *fw_cfg_modify_file(FWCfgState *s, const char *filename,
867 void *data, size_t len)
Gleb Natapov962630f2010-12-08 13:35:09 +0200868{
Gongleibdbb5b12014-10-07 16:00:08 +0800869 int i, index;
Gongleif3b37662014-11-25 12:38:19 +0800870 void *ptr = NULL;
Gongleibdbb5b12014-10-07 16:00:08 +0800871
872 assert(s->files);
873
874 index = be32_to_cpu(s->files->count);
Laszlo Erseke12f3a12017-01-12 19:24:15 +0100875 assert(index < fw_cfg_file_slots(s));
Gongleibdbb5b12014-10-07 16:00:08 +0800876
877 for (i = 0; i < index; i++) {
878 if (strcmp(filename, s->files->f[i].name) == 0) {
Gongleif3b37662014-11-25 12:38:19 +0800879 ptr = fw_cfg_modify_bytes_read(s, FW_CFG_FILE_FIRST + i,
880 data, len);
881 s->files->f[i].size = cpu_to_be32(len);
882 return ptr;
Gongleibdbb5b12014-10-07 16:00:08 +0800883 }
884 }
885 /* add new one */
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100886 fw_cfg_add_file_callback(s, filename, NULL, NULL, data, len, true);
Gongleibdbb5b12014-10-07 16:00:08 +0800887 return NULL;
888}
889
890static void fw_cfg_machine_reset(void *opaque)
891{
892 void *ptr;
Markus Armbruster0e7a7592013-01-16 14:50:29 +0100893 size_t len;
Gongleibdbb5b12014-10-07 16:00:08 +0800894 FWCfgState *s = opaque;
Alexey Kardashevskiy30e32af2014-03-17 13:40:22 +1100895 char *bootindex = get_boot_devices_list(&len, false);
Gleb Natapov962630f2010-12-08 13:35:09 +0200896
Gongleibdbb5b12014-10-07 16:00:08 +0800897 ptr = fw_cfg_modify_file(s, "bootorder", (uint8_t *)bootindex, len);
898 g_free(ptr);
899}
900
901static void fw_cfg_machine_ready(struct Notifier *n, void *data)
902{
903 FWCfgState *s = container_of(n, FWCfgState, machine_ready);
904 qemu_register_reset(fw_cfg_machine_reset, s);
Gleb Natapov962630f2010-12-08 13:35:09 +0200905}
906
Laszlo Ersek5712db62014-12-22 13:11:35 +0100907
908
909static void fw_cfg_init1(DeviceState *dev)
blueswir13cce6242008-09-18 18:27:29 +0000910{
Laszlo Ersek5712db62014-12-22 13:11:35 +0100911 FWCfgState *s = FW_CFG(dev);
Eduardo Habkostcfc58cf2016-04-19 16:55:25 -0300912 MachineState *machine = MACHINE(qdev_get_machine());
Blue Swirl3a5c16f2010-06-27 16:04:55 +0000913
Michael S. Tsirkincac12212013-05-30 16:21:24 +0300914 assert(!object_resolve_path(FW_CFG_PATH, NULL));
915
Eduardo Habkostcfc58cf2016-04-19 16:55:25 -0300916 object_property_add_child(OBJECT(machine), FW_CFG_NAME, OBJECT(s), NULL);
Hu Tao10a584b2013-04-26 11:24:44 +0800917
918 qdev_init_nofail(dev);
919
Markus Armbruster089da572013-01-16 14:50:28 +0100920 fw_cfg_add_bytes(s, FW_CFG_SIGNATURE, (char *)"QEMU", 4);
Fam Zheng9c5ce8d2016-09-21 12:27:22 +0800921 fw_cfg_add_bytes(s, FW_CFG_UUID, &qemu_uuid, 16);
Eduardo Habkostcfc58cf2016-04-19 16:55:25 -0300922 fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)!machine->enable_graphics);
Jan Kiszka95387492009-07-02 00:19:02 +0200923 fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
wayne3d3b8302011-07-27 18:04:55 +0800924 fw_cfg_bootsplash(s);
Amos Kongac05f342012-09-07 11:11:03 +0800925 fw_cfg_reboot(s);
Gleb Natapov962630f2010-12-08 13:35:09 +0200926
927 s->machine_ready.notify = fw_cfg_machine_ready;
928 qemu_add_machine_init_done_notifier(&s->machine_ready);
blueswir13cce6242008-09-18 18:27:29 +0000929}
Blue Swirl3a5c16f2010-06-27 16:04:55 +0000930
Marc María4c0d1d2015-10-08 17:02:55 +0200931FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
932 AddressSpace *dma_as)
Blue Swirl3a5c16f2010-06-27 16:04:55 +0000933{
Laszlo Ersek5712db62014-12-22 13:11:35 +0100934 DeviceState *dev;
Marc María4c0d1d2015-10-08 17:02:55 +0200935 FWCfgState *s;
936 uint32_t version = FW_CFG_VERSION;
Laszlo Erseke6915b52016-02-18 20:31:00 +0100937 bool dma_requested = dma_iobase && dma_as;
Blue Swirl3a5c16f2010-06-27 16:04:55 +0000938
Laszlo Ersek5712db62014-12-22 13:11:35 +0100939 dev = qdev_create(NULL, TYPE_FW_CFG_IO);
940 qdev_prop_set_uint32(dev, "iobase", iobase);
Marc María4c0d1d2015-10-08 17:02:55 +0200941 qdev_prop_set_uint32(dev, "dma_iobase", dma_iobase);
Laszlo Erseke6915b52016-02-18 20:31:00 +0100942 if (!dma_requested) {
943 qdev_prop_set_bit(dev, "dma_enabled", false);
944 }
Laszlo Ersek5712db62014-12-22 13:11:35 +0100945
Marc María4c0d1d2015-10-08 17:02:55 +0200946 fw_cfg_init1(dev);
947 s = FW_CFG(dev);
948
Laszlo Erseke6915b52016-02-18 20:31:00 +0100949 if (s->dma_enabled) {
Marc María4c0d1d2015-10-08 17:02:55 +0200950 /* 64 bits for the address field */
951 s->dma_as = dma_as;
952 s->dma_addr = 0;
953
954 version |= FW_CFG_VERSION_DMA;
955 }
956
957 fw_cfg_add_i32(s, FW_CFG_ID, version);
958
959 return s;
Hu Tao56383952013-07-01 18:18:33 +0800960}
961
Marc María4c0d1d2015-10-08 17:02:55 +0200962FWCfgState *fw_cfg_init_io(uint32_t iobase)
963{
964 return fw_cfg_init_io_dma(iobase, 0, NULL);
965}
966
967FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
968 hwaddr data_addr, uint32_t data_width,
969 hwaddr dma_addr, AddressSpace *dma_as)
Hu Tao56383952013-07-01 18:18:33 +0800970{
Laszlo Ersek5712db62014-12-22 13:11:35 +0100971 DeviceState *dev;
972 SysBusDevice *sbd;
Marc María4c0d1d2015-10-08 17:02:55 +0200973 FWCfgState *s;
974 uint32_t version = FW_CFG_VERSION;
Laszlo Erseke6915b52016-02-18 20:31:00 +0100975 bool dma_requested = dma_addr && dma_as;
Hu Tao56383952013-07-01 18:18:33 +0800976
Laszlo Ersek5712db62014-12-22 13:11:35 +0100977 dev = qdev_create(NULL, TYPE_FW_CFG_MEM);
Laszlo Ersek6c87e3d2014-12-22 13:11:41 +0100978 qdev_prop_set_uint32(dev, "data_width", data_width);
Laszlo Erseke6915b52016-02-18 20:31:00 +0100979 if (!dma_requested) {
980 qdev_prop_set_bit(dev, "dma_enabled", false);
981 }
Laszlo Ersekcfaadf02014-12-22 13:11:40 +0100982
Laszlo Ersek5712db62014-12-22 13:11:35 +0100983 fw_cfg_init1(dev);
984
985 sbd = SYS_BUS_DEVICE(dev);
986 sysbus_mmio_map(sbd, 0, ctl_addr);
987 sysbus_mmio_map(sbd, 1, data_addr);
988
Marc María4c0d1d2015-10-08 17:02:55 +0200989 s = FW_CFG(dev);
990
Laszlo Erseke6915b52016-02-18 20:31:00 +0100991 if (s->dma_enabled) {
Marc María4c0d1d2015-10-08 17:02:55 +0200992 s->dma_as = dma_as;
993 s->dma_addr = 0;
994 sysbus_mmio_map(sbd, 2, dma_addr);
995 version |= FW_CFG_VERSION_DMA;
996 }
997
998 fw_cfg_add_i32(s, FW_CFG_ID, version);
999
1000 return s;
Laszlo Ersek5712db62014-12-22 13:11:35 +01001001}
1002
Laszlo Ersek6c87e3d2014-12-22 13:11:41 +01001003FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr)
1004{
1005 return fw_cfg_init_mem_wide(ctl_addr, data_addr,
Marc María4c0d1d2015-10-08 17:02:55 +02001006 fw_cfg_data_mem_ops.valid.max_access_size,
1007 0, NULL);
Laszlo Ersek6c87e3d2014-12-22 13:11:41 +01001008}
1009
Laszlo Ersek5712db62014-12-22 13:11:35 +01001010
Michael S. Tsirkin600c60b2013-05-30 16:07:58 +03001011FWCfgState *fw_cfg_find(void)
1012{
Hu Tao2ce92a12013-07-01 18:18:32 +08001013 return FW_CFG(object_resolve_path(FW_CFG_PATH, NULL));
Michael S. Tsirkin600c60b2013-05-30 16:07:58 +03001014}
1015
Anthony Liguori999e12b2012-01-24 13:12:29 -06001016static void fw_cfg_class_init(ObjectClass *klass, void *data)
1017{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001018 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -06001019
Anthony Liguori39bffca2011-12-07 21:34:16 -06001020 dc->reset = fw_cfg_reset;
1021 dc->vmsd = &vmstate_fw_cfg;
Anthony Liguori999e12b2012-01-24 13:12:29 -06001022}
1023
Andreas Färber8c43a6f2013-01-10 16:19:07 +01001024static const TypeInfo fw_cfg_info = {
Michael S. Tsirkin600c60b2013-05-30 16:07:58 +03001025 .name = TYPE_FW_CFG,
Anthony Liguori39bffca2011-12-07 21:34:16 -06001026 .parent = TYPE_SYS_BUS_DEVICE,
Markus Armbrustere061fa32016-07-29 09:29:13 +02001027 .abstract = true,
Anthony Liguori39bffca2011-12-07 21:34:16 -06001028 .instance_size = sizeof(FWCfgState),
1029 .class_init = fw_cfg_class_init,
Blue Swirl3a5c16f2010-06-27 16:04:55 +00001030};
1031
Laszlo Erseke12f3a12017-01-12 19:24:15 +01001032static void fw_cfg_file_slots_allocate(FWCfgState *s, Error **errp)
1033{
1034 uint16_t file_slots_max;
1035
1036 if (fw_cfg_file_slots(s) < FW_CFG_FILE_SLOTS_MIN) {
1037 error_setg(errp, "\"file_slots\" must be at least 0x%x",
1038 FW_CFG_FILE_SLOTS_MIN);
1039 return;
1040 }
1041
1042 /* (UINT16_MAX & FW_CFG_ENTRY_MASK) is the highest inclusive selector value
1043 * that we permit. The actual (exclusive) value coming from the
1044 * configuration is (FW_CFG_FILE_FIRST + fw_cfg_file_slots(s)). */
1045 file_slots_max = (UINT16_MAX & FW_CFG_ENTRY_MASK) - FW_CFG_FILE_FIRST + 1;
1046 if (fw_cfg_file_slots(s) > file_slots_max) {
1047 error_setg(errp, "\"file_slots\" must not exceed 0x%" PRIx16,
1048 file_slots_max);
1049 return;
1050 }
1051
1052 s->entries[0] = g_new0(FWCfgEntry, fw_cfg_max_entry(s));
1053 s->entries[1] = g_new0(FWCfgEntry, fw_cfg_max_entry(s));
1054 s->entry_order = g_new0(int, fw_cfg_max_entry(s));
1055}
Laszlo Ersek5712db62014-12-22 13:11:35 +01001056
1057static Property fw_cfg_io_properties[] = {
1058 DEFINE_PROP_UINT32("iobase", FWCfgIoState, iobase, -1),
Marc María4c0d1d2015-10-08 17:02:55 +02001059 DEFINE_PROP_UINT32("dma_iobase", FWCfgIoState, dma_iobase, -1),
1060 DEFINE_PROP_BOOL("dma_enabled", FWCfgIoState, parent_obj.dma_enabled,
Laszlo Erseke6915b52016-02-18 20:31:00 +01001061 true),
Laszlo Erseke12f3a12017-01-12 19:24:15 +01001062 DEFINE_PROP_UINT16("x-file-slots", FWCfgIoState, parent_obj.file_slots,
Laszlo Erseka5b3ebf2017-01-12 19:24:17 +01001063 FW_CFG_FILE_SLOTS_DFLT),
Laszlo Ersek5712db62014-12-22 13:11:35 +01001064 DEFINE_PROP_END_OF_LIST(),
1065};
1066
1067static void fw_cfg_io_realize(DeviceState *dev, Error **errp)
1068{
1069 FWCfgIoState *s = FW_CFG_IO(dev);
1070 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Laszlo Erseke12f3a12017-01-12 19:24:15 +01001071 Error *local_err = NULL;
1072
1073 fw_cfg_file_slots_allocate(FW_CFG(s), &local_err);
1074 if (local_err) {
1075 error_propagate(errp, local_err);
1076 return;
1077 }
Laszlo Ersek5712db62014-12-22 13:11:35 +01001078
Gabriel L. Somloce9a2aa2016-02-19 13:20:25 -05001079 /* when using port i/o, the 8-bit data register ALWAYS overlaps
1080 * with half of the 16-bit control register. Hence, the total size
1081 * of the i/o region used is FW_CFG_CTL_SIZE */
Laszlo Ersek5712db62014-12-22 13:11:35 +01001082 memory_region_init_io(&s->comb_iomem, OBJECT(s), &fw_cfg_comb_mem_ops,
Marc María4c0d1d2015-10-08 17:02:55 +02001083 FW_CFG(s), "fwcfg", FW_CFG_CTL_SIZE);
Laszlo Ersek5712db62014-12-22 13:11:35 +01001084 sysbus_add_io(sbd, s->iobase, &s->comb_iomem);
Marc María4c0d1d2015-10-08 17:02:55 +02001085
1086 if (FW_CFG(s)->dma_enabled) {
1087 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
1088 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
1089 sizeof(dma_addr_t));
1090 sysbus_add_io(sbd, s->dma_iobase, &FW_CFG(s)->dma_iomem);
1091 }
Laszlo Ersek5712db62014-12-22 13:11:35 +01001092}
1093
1094static void fw_cfg_io_class_init(ObjectClass *klass, void *data)
1095{
1096 DeviceClass *dc = DEVICE_CLASS(klass);
1097
1098 dc->realize = fw_cfg_io_realize;
1099 dc->props = fw_cfg_io_properties;
1100}
1101
1102static const TypeInfo fw_cfg_io_info = {
1103 .name = TYPE_FW_CFG_IO,
1104 .parent = TYPE_FW_CFG,
1105 .instance_size = sizeof(FWCfgIoState),
1106 .class_init = fw_cfg_io_class_init,
1107};
1108
1109
Laszlo Ersekcfaadf02014-12-22 13:11:40 +01001110static Property fw_cfg_mem_properties[] = {
1111 DEFINE_PROP_UINT32("data_width", FWCfgMemState, data_width, -1),
Marc María4c0d1d2015-10-08 17:02:55 +02001112 DEFINE_PROP_BOOL("dma_enabled", FWCfgMemState, parent_obj.dma_enabled,
Laszlo Erseke6915b52016-02-18 20:31:00 +01001113 true),
Laszlo Erseke12f3a12017-01-12 19:24:15 +01001114 DEFINE_PROP_UINT16("x-file-slots", FWCfgMemState, parent_obj.file_slots,
Laszlo Erseka5b3ebf2017-01-12 19:24:17 +01001115 FW_CFG_FILE_SLOTS_DFLT),
Laszlo Ersekcfaadf02014-12-22 13:11:40 +01001116 DEFINE_PROP_END_OF_LIST(),
1117};
1118
Laszlo Ersek5712db62014-12-22 13:11:35 +01001119static void fw_cfg_mem_realize(DeviceState *dev, Error **errp)
1120{
1121 FWCfgMemState *s = FW_CFG_MEM(dev);
1122 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Laszlo Ersekcfaadf02014-12-22 13:11:40 +01001123 const MemoryRegionOps *data_ops = &fw_cfg_data_mem_ops;
Laszlo Erseke12f3a12017-01-12 19:24:15 +01001124 Error *local_err = NULL;
1125
1126 fw_cfg_file_slots_allocate(FW_CFG(s), &local_err);
1127 if (local_err) {
1128 error_propagate(errp, local_err);
1129 return;
1130 }
Laszlo Ersek5712db62014-12-22 13:11:35 +01001131
1132 memory_region_init_io(&s->ctl_iomem, OBJECT(s), &fw_cfg_ctl_mem_ops,
Marc María4c0d1d2015-10-08 17:02:55 +02001133 FW_CFG(s), "fwcfg.ctl", FW_CFG_CTL_SIZE);
Laszlo Ersek5712db62014-12-22 13:11:35 +01001134 sysbus_init_mmio(sbd, &s->ctl_iomem);
1135
Laszlo Ersekcfaadf02014-12-22 13:11:40 +01001136 if (s->data_width > data_ops->valid.max_access_size) {
1137 /* memberwise copy because the "old_mmio" member is const */
1138 s->wide_data_ops.read = data_ops->read;
1139 s->wide_data_ops.write = data_ops->write;
1140 s->wide_data_ops.endianness = data_ops->endianness;
1141 s->wide_data_ops.valid = data_ops->valid;
1142 s->wide_data_ops.impl = data_ops->impl;
1143
1144 s->wide_data_ops.valid.max_access_size = s->data_width;
1145 s->wide_data_ops.impl.max_access_size = s->data_width;
1146 data_ops = &s->wide_data_ops;
1147 }
1148 memory_region_init_io(&s->data_iomem, OBJECT(s), data_ops, FW_CFG(s),
1149 "fwcfg.data", data_ops->valid.max_access_size);
Laszlo Ersek5712db62014-12-22 13:11:35 +01001150 sysbus_init_mmio(sbd, &s->data_iomem);
Marc María4c0d1d2015-10-08 17:02:55 +02001151
1152 if (FW_CFG(s)->dma_enabled) {
1153 memory_region_init_io(&FW_CFG(s)->dma_iomem, OBJECT(s),
1154 &fw_cfg_dma_mem_ops, FW_CFG(s), "fwcfg.dma",
1155 sizeof(dma_addr_t));
1156 sysbus_init_mmio(sbd, &FW_CFG(s)->dma_iomem);
1157 }
Laszlo Ersek5712db62014-12-22 13:11:35 +01001158}
1159
1160static void fw_cfg_mem_class_init(ObjectClass *klass, void *data)
1161{
1162 DeviceClass *dc = DEVICE_CLASS(klass);
1163
1164 dc->realize = fw_cfg_mem_realize;
Laszlo Ersekcfaadf02014-12-22 13:11:40 +01001165 dc->props = fw_cfg_mem_properties;
Laszlo Ersek5712db62014-12-22 13:11:35 +01001166}
1167
1168static const TypeInfo fw_cfg_mem_info = {
1169 .name = TYPE_FW_CFG_MEM,
1170 .parent = TYPE_FW_CFG,
1171 .instance_size = sizeof(FWCfgMemState),
1172 .class_init = fw_cfg_mem_class_init,
1173};
1174
1175
Andreas Färber83f7d432012-02-09 15:20:55 +01001176static void fw_cfg_register_types(void)
Blue Swirl3a5c16f2010-06-27 16:04:55 +00001177{
Anthony Liguori39bffca2011-12-07 21:34:16 -06001178 type_register_static(&fw_cfg_info);
Laszlo Ersek5712db62014-12-22 13:11:35 +01001179 type_register_static(&fw_cfg_io_info);
1180 type_register_static(&fw_cfg_mem_info);
Blue Swirl3a5c16f2010-06-27 16:04:55 +00001181}
1182
Andreas Färber83f7d432012-02-09 15:20:55 +01001183type_init(fw_cfg_register_types)