blob: d173998803c0af5a3904c4262ea910d3567058be [file] [log] [blame]
blueswir13cce6242008-09-18 18:27:29 +00001#ifndef FW_CFG_H
2#define FW_CFG_H
3
Hu Tao1dfe5052013-04-26 11:24:43 +08004#include "exec/hwaddr.h"
Marc-André Lureau5be5df72018-08-17 17:59:10 +02005#include "standard-headers/linux/qemu_fw_cfg.h"
Mark Cave-Ayland39736e12017-07-14 10:40:08 +01006#include "hw/sysbus.h"
7#include "sysemu/dma.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -04008#include "qom/object.h"
Mark Cave-Ayland39736e12017-07-14 10:40:08 +01009
10#define TYPE_FW_CFG "fw_cfg"
11#define TYPE_FW_CFG_IO "fw_cfg_io"
12#define TYPE_FW_CFG_MEM "fw_cfg_mem"
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +020013#define TYPE_FW_CFG_DATA_GENERATOR_INTERFACE "fw_cfg-data-generator"
Mark Cave-Ayland39736e12017-07-14 10:40:08 +010014
Eduardo Habkost80633962020-09-16 14:25:19 -040015OBJECT_DECLARE_SIMPLE_TYPE(FWCfgState, FW_CFG)
16OBJECT_DECLARE_SIMPLE_TYPE(FWCfgIoState, FW_CFG_IO)
17OBJECT_DECLARE_SIMPLE_TYPE(FWCfgMemState, FW_CFG_MEM)
Hu Tao1dfe5052013-04-26 11:24:43 +080018
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040019typedef struct FWCfgDataGeneratorClass FWCfgDataGeneratorClass;
Eduardo Habkost8110fa12020-08-31 17:07:33 -040020DECLARE_CLASS_CHECKERS(FWCfgDataGeneratorClass, FW_CFG_DATA_GENERATOR,
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +020021 TYPE_FW_CFG_DATA_GENERATOR_INTERFACE)
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +020022
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040023struct FWCfgDataGeneratorClass {
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +020024 /*< private >*/
25 InterfaceClass parent_class;
26 /*< public >*/
27
28 /**
29 * get_data:
30 * @obj: the object implementing this interface
31 * @errp: pointer to a NULL-initialized error object
32 *
Philippe Mathieu-Daudéa3ad5832020-07-21 15:05:51 +020033 * Returns: reference to a byte array containing the data on success,
34 * or NULL on error.
35 *
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +020036 * The caller should release the reference when no longer
37 * required.
38 */
39 GByteArray *(*get_data)(Object *obj, Error **errp);
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040040};
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +020041
Marc-André Lureau5be5df72018-08-17 17:59:10 +020042typedef struct fw_cfg_file FWCfgFile;
Gerd Hoffmannabe147e2009-12-18 12:01:10 +010043
Gerd Hoffmannbab47d92016-04-07 09:12:58 -050044#define FW_CFG_ORDER_OVERRIDE_VGA 70
45#define FW_CFG_ORDER_OVERRIDE_NIC 80
46#define FW_CFG_ORDER_OVERRIDE_USER 100
47#define FW_CFG_ORDER_OVERRIDE_DEVICE 110
48
49void fw_cfg_set_order_override(FWCfgState *fw_cfg, int order);
50void fw_cfg_reset_order_override(FWCfgState *fw_cfg);
51
Gerd Hoffmannabe147e2009-12-18 12:01:10 +010052typedef struct FWCfgFiles {
53 uint32_t count;
54 FWCfgFile f[];
55} FWCfgFiles;
56
Marc-André Lureau5be5df72018-08-17 17:59:10 +020057typedef struct fw_cfg_dma_access FWCfgDmaAccess;
Marc María4c0d1d2015-10-08 17:02:55 +020058
Marc-André Lureau6f6f4ae2017-08-07 20:16:11 +020059typedef void (*FWCfgCallback)(void *opaque);
Marc-André Lureau5f9252f2017-09-11 18:59:23 +020060typedef void (*FWCfgWriteCallback)(void *opaque, off_t start, size_t len);
blueswir13cce6242008-09-18 18:27:29 +000061
Paolo Bonzinic71a42b2024-05-02 16:57:08 +020062typedef struct FWCfgEntry FWCfgEntry;
63
Mark Cave-Ayland39736e12017-07-14 10:40:08 +010064struct FWCfgState {
65 /*< private >*/
66 SysBusDevice parent_obj;
67 /*< public >*/
68
69 uint16_t file_slots;
70 FWCfgEntry *entries[2];
71 int *entry_order;
72 FWCfgFiles *files;
73 uint16_t cur_entry;
74 uint32_t cur_offset;
75 Notifier machine_ready;
76
77 int fw_cfg_order_override;
78
79 bool dma_enabled;
80 dma_addr_t dma_addr;
81 AddressSpace *dma_as;
82 MemoryRegion dma_iomem;
Shameer Kolothum394f0f72020-04-03 11:18:26 +010083
84 /* restore during migration */
85 bool acpi_mr_restore;
86 uint64_t table_mr_size;
87 uint64_t linker_mr_size;
88 uint64_t rsdp_mr_size;
Mark Cave-Ayland39736e12017-07-14 10:40:08 +010089};
90
91struct FWCfgIoState {
92 /*< private >*/
93 FWCfgState parent_obj;
94 /*< public >*/
95
96 MemoryRegion comb_iomem;
97};
98
99struct FWCfgMemState {
100 /*< private >*/
101 FWCfgState parent_obj;
102 /*< public >*/
103
104 MemoryRegion ctl_iomem, data_iomem;
105 uint32_t data_width;
106 MemoryRegionOps wide_data_ops;
107};
108
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500109/**
110 * fw_cfg_add_bytes:
111 * @s: fw_cfg device being modified
112 * @key: selector key value for new fw_cfg item
113 * @data: pointer to start of item data
114 * @len: size of item data
115 *
116 * Add a new fw_cfg item, available by selecting the given key, as a raw
117 * "blob" of the given size. The data referenced by the starting pointer
118 * is only linked, NOT copied, into the data structure of the fw_cfg device.
119 */
Markus Armbruster089da572013-01-16 14:50:28 +0100120void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500121
122/**
123 * fw_cfg_add_string:
124 * @s: fw_cfg device being modified
125 * @key: selector key value for new fw_cfg item
126 * @value: NUL-terminated ascii string
127 *
128 * Add a new fw_cfg item, available by selecting the given key. The item
129 * data will consist of a dynamically allocated copy of the provided string,
130 * including its NUL terminator.
131 */
Markus Armbruster44687f72013-01-16 14:50:24 +0100132void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500133
134/**
Sergio Lopeze5f6aa32019-09-24 11:38:18 +0200135 * fw_cfg_modify_string:
136 * @s: fw_cfg device being modified
137 * @key: selector key value for new fw_cfg item
138 * @value: NUL-terminated ascii string
139 *
140 * Replace the fw_cfg item available by selecting the given key. The new
141 * data will consist of a dynamically allocated copy of the provided string,
142 * including its NUL terminator. The data being replaced, assumed to have
143 * been dynamically allocated during an earlier call to either
144 * fw_cfg_add_string() or fw_cfg_modify_string(), is freed before returning.
145 */
146void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value);
147
148/**
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500149 * fw_cfg_add_i16:
150 * @s: fw_cfg device being modified
151 * @key: selector key value for new fw_cfg item
152 * @value: 16-bit integer
153 *
154 * Add a new fw_cfg item, available by selecting the given key. The item
155 * data will consist of a dynamically allocated copy of the given 16-bit
156 * value, converted to little-endian representation.
157 */
Markus Armbruster4cad3862013-01-16 14:50:23 +0100158void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500159
160/**
161 * fw_cfg_modify_i16:
162 * @s: fw_cfg device being modified
163 * @key: selector key value for new fw_cfg item
164 * @value: 16-bit integer
165 *
166 * Replace the fw_cfg item available by selecting the given key. The new
167 * data will consist of a dynamically allocated copy of the given 16-bit
168 * value, converted to little-endian representation. The data being replaced,
169 * assumed to have been dynamically allocated during an earlier call to
170 * either fw_cfg_add_i16() or fw_cfg_modify_i16(), is freed before returning.
171 */
Gabriel L. Somlo1edd34b2015-06-08 14:10:44 -0400172void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500173
174/**
175 * fw_cfg_add_i32:
176 * @s: fw_cfg device being modified
177 * @key: selector key value for new fw_cfg item
178 * @value: 32-bit integer
179 *
180 * Add a new fw_cfg item, available by selecting the given key. The item
181 * data will consist of a dynamically allocated copy of the given 32-bit
182 * value, converted to little-endian representation.
183 */
Markus Armbruster4cad3862013-01-16 14:50:23 +0100184void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500185
186/**
Sergio Lopeze5f6aa32019-09-24 11:38:18 +0200187 * fw_cfg_modify_i32:
188 * @s: fw_cfg device being modified
189 * @key: selector key value for new fw_cfg item
190 * @value: 32-bit integer
191 *
192 * Replace the fw_cfg item available by selecting the given key. The new
193 * data will consist of a dynamically allocated copy of the given 32-bit
194 * value, converted to little-endian representation. The data being replaced,
195 * assumed to have been dynamically allocated during an earlier call to
196 * either fw_cfg_add_i32() or fw_cfg_modify_i32(), is freed before returning.
197 */
198void fw_cfg_modify_i32(FWCfgState *s, uint16_t key, uint32_t value);
199
200/**
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500201 * fw_cfg_add_i64:
202 * @s: fw_cfg device being modified
203 * @key: selector key value for new fw_cfg item
204 * @value: 64-bit integer
205 *
206 * Add a new fw_cfg item, available by selecting the given key. The item
207 * data will consist of a dynamically allocated copy of the given 64-bit
208 * value, converted to little-endian representation.
209 */
Markus Armbruster4cad3862013-01-16 14:50:23 +0100210void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500211
212/**
Sergio Lopeze5f6aa32019-09-24 11:38:18 +0200213 * fw_cfg_modify_i64:
214 * @s: fw_cfg device being modified
215 * @key: selector key value for new fw_cfg item
216 * @value: 64-bit integer
217 *
218 * Replace the fw_cfg item available by selecting the given key. The new
219 * data will consist of a dynamically allocated copy of the given 64-bit
220 * value, converted to little-endian representation. The data being replaced,
221 * assumed to have been dynamically allocated during an earlier call to
222 * either fw_cfg_add_i64() or fw_cfg_modify_i64(), is freed before returning.
223 */
224void fw_cfg_modify_i64(FWCfgState *s, uint16_t key, uint64_t value);
225
226/**
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500227 * fw_cfg_add_file:
228 * @s: fw_cfg device being modified
229 * @filename: name of new fw_cfg file item
230 * @data: pointer to start of item data
231 * @len: size of item data
232 *
233 * Add a new NAMED fw_cfg item as a raw "blob" of the given size. The data
234 * referenced by the starting pointer is only linked, NOT copied, into the
235 * data structure of the fw_cfg device.
236 * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
237 * will be used; also, a new entry will be added to the file directory
238 * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
239 * data size, and assigned selector key value.
240 */
Markus Armbruster089da572013-01-16 14:50:28 +0100241void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
242 size_t len);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500243
244/**
245 * fw_cfg_add_file_callback:
246 * @s: fw_cfg device being modified
247 * @filename: name of new fw_cfg file item
Marc-André Lureau6f6f4ae2017-08-07 20:16:11 +0200248 * @select_cb: callback function when selecting
Marc-André Lureau5f9252f2017-09-11 18:59:23 +0200249 * @write_cb: callback function after a write
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500250 * @callback_opaque: argument to be passed into callback function
251 * @data: pointer to start of item data
252 * @len: size of item data
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100253 * @read_only: is file read only
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500254 *
255 * Add a new NAMED fw_cfg item as a raw "blob" of the given size. The data
256 * referenced by the starting pointer is only linked, NOT copied, into the
257 * data structure of the fw_cfg device.
258 * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
259 * will be used; also, a new entry will be added to the file directory
260 * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
261 * data size, and assigned selector key value.
262 * Additionally, set a callback function (and argument) to be called each
Gabriel L. Somlo3bef7e82015-11-05 09:32:48 -0500263 * time this item is selected (by having its selector key either written to
264 * the fw_cfg control register, or passed to QEMU in FWCfgDmaAccess.control
265 * with FW_CFG_DMA_CTL_SELECT).
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500266 */
Michael S. Tsirkind87072c2013-09-01 17:56:20 +0300267void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
Marc-André Lureau6f6f4ae2017-08-07 20:16:11 +0200268 FWCfgCallback select_cb,
Marc-André Lureau5f9252f2017-09-11 18:59:23 +0200269 FWCfgWriteCallback write_cb,
Marc-André Lureau6f6f4ae2017-08-07 20:16:11 +0200270 void *callback_opaque,
Michael S. Tsirkinbaf2d5b2017-01-12 19:24:14 +0100271 void *data, size_t len, bool read_only);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500272
273/**
274 * fw_cfg_modify_file:
275 * @s: fw_cfg device being modified
276 * @filename: name of new fw_cfg file item
277 * @data: pointer to start of item data
278 * @len: size of item data
279 *
280 * Replace a NAMED fw_cfg item. If an existing item is found, its callback
281 * information will be cleared, and a pointer to its data will be returned
282 * to the caller, so that it may be freed if necessary. If an existing item
283 * is not found, this call defaults to fw_cfg_add_file(), and NULL is
284 * returned to the caller.
285 * In either case, the new item data is only linked, NOT copied, into the
286 * data structure of the fw_cfg device.
287 *
288 * Returns: pointer to old item's data, or NULL if old item does not exist.
289 */
Gongleibdbb5b12014-10-07 16:00:08 +0800290void *fw_cfg_modify_file(FWCfgState *s, const char *filename, void *data,
291 size_t len);
Gabriel L. Somlo9c4a5c52015-11-05 09:32:47 -0500292
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +0200293/**
294 * fw_cfg_add_from_generator:
295 * @s: fw_cfg device being modified
296 * @filename: name of new fw_cfg file item
297 * @gen_id: name of object implementing FW_CFG_DATA_GENERATOR interface
298 * @errp: pointer to a NULL initialized error object
299 *
300 * Add a new NAMED fw_cfg item with the content generated from the
301 * @gen_id object. The data generated by the @gen_id object is copied
302 * into the data structure of the fw_cfg device.
303 * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
304 * will be used; also, a new entry will be added to the file directory
305 * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
306 * data size, and assigned selector key value.
Philippe Mathieu-Daudé07719512020-07-20 14:20:15 +0200307 *
308 * Returns: %true on success, %false on error.
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +0200309 */
Philippe Mathieu-Daudé07719512020-07-20 14:20:15 +0200310bool fw_cfg_add_from_generator(FWCfgState *s, const char *filename,
Philippe Mathieu-Daudé32031482020-05-14 15:15:38 +0200311 const char *gen_id, Error **errp);
312
Jiahui Cen0abd3882020-11-19 09:48:34 +0800313/**
314 * fw_cfg_add_extra_pci_roots:
315 * @bus: main pci root bus to be scanned from
316 * @s: fw_cfg device being modified
317 *
318 * Add a new fw_cfg item...
319 */
320void fw_cfg_add_extra_pci_roots(PCIBus *bus, FWCfgState *s);
321
Marc María4c0d1d2015-10-08 17:02:55 +0200322FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
323 AddressSpace *dma_as);
Laszlo Ersek5712db62014-12-22 13:11:35 +0100324FWCfgState *fw_cfg_init_io(uint32_t iobase);
325FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr);
Marc María4c0d1d2015-10-08 17:02:55 +0200326FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
327 hwaddr data_addr, uint32_t data_width,
328 hwaddr dma_addr, AddressSpace *dma_as);
blueswir13cce6242008-09-18 18:27:29 +0000329
Michael S. Tsirkin600c60b2013-05-30 16:07:58 +0300330FWCfgState *fw_cfg_find(void);
Marc Maríb2a575a2016-05-23 19:11:33 +0100331bool fw_cfg_dma_enabled(void *opaque);
Michael S. Tsirkin600c60b2013-05-30 16:07:58 +0300332
Philippe Mathieu-Daudéb15c0f72019-04-22 15:49:41 +0200333/**
334 * fw_cfg_arch_key_name:
335 *
336 * @key: The uint16 selector key.
337 *
338 * The key is architecture-specific (the FW_CFG_ARCH_LOCAL mask is expected
339 * to be set in the key).
340 *
341 * Returns: The stringified architecture-specific name if the selector
342 * refers to a well-known numerically defined item, or NULL on
343 * key lookup failure.
344 */
345const char *fw_cfg_arch_key_name(uint16_t key);
346
Sunil V L785a7382022-10-04 14:53:49 +0530347/**
348 * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
349 * by key.
350 * @fw_cfg: The firmware config instance to store the data in.
351 * @size_key: The firmware config key to store the size of the loaded
352 * data under, with fw_cfg_add_i32().
353 * @data_key: The firmware config key to store the loaded data under,
354 * with fw_cfg_add_bytes().
355 * @image_name: The name of the image file to load. If it is NULL, the
356 * function returns without doing anything.
357 * @try_decompress: Whether the image should be decompressed (gunzipped) before
358 * adding it to fw_cfg. If decompression fails, the image is
359 * loaded as-is.
360 *
361 * In case of failure, the function prints an error message to stderr and the
362 * process exits with status 1.
363 */
364void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
365 uint16_t data_key, const char *image_name,
366 bool try_decompress);
367
blueswir13cce6242008-09-18 18:27:29 +0000368#endif