xref: /qemu/include/hw/nvram/fw_cfg.h (revision 75ac231c)
1 #ifndef FW_CFG_H
2 #define FW_CFG_H
3 
4 #include "exec/hwaddr.h"
5 #include "standard-headers/linux/qemu_fw_cfg.h"
6 #include "hw/sysbus.h"
7 #include "sysemu/dma.h"
8 #include "qom/object.h"
9 
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"
13 #define TYPE_FW_CFG_DATA_GENERATOR_INTERFACE "fw_cfg-data-generator"
14 
15 OBJECT_DECLARE_SIMPLE_TYPE(FWCfgState, FW_CFG)
16 OBJECT_DECLARE_SIMPLE_TYPE(FWCfgIoState, FW_CFG_IO)
17 OBJECT_DECLARE_SIMPLE_TYPE(FWCfgMemState, FW_CFG_MEM)
18 
19 typedef struct FWCfgDataGeneratorClass FWCfgDataGeneratorClass;
20 DECLARE_CLASS_CHECKERS(FWCfgDataGeneratorClass, FW_CFG_DATA_GENERATOR,
21                        TYPE_FW_CFG_DATA_GENERATOR_INTERFACE)
22 
23 struct FWCfgDataGeneratorClass {
24     /*< 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      *
33      * Returns: reference to a byte array containing the data on success,
34      *          or NULL on error.
35      *
36      * The caller should release the reference when no longer
37      * required.
38      */
39     GByteArray *(*get_data)(Object *obj, Error **errp);
40 };
41 
42 typedef struct fw_cfg_file FWCfgFile;
43 
44 #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 
49 void fw_cfg_set_order_override(FWCfgState *fw_cfg, int order);
50 void fw_cfg_reset_order_override(FWCfgState *fw_cfg);
51 
52 typedef struct FWCfgFiles {
53     uint32_t  count;
54     FWCfgFile f[];
55 } FWCfgFiles;
56 
57 typedef struct fw_cfg_dma_access FWCfgDmaAccess;
58 
59 typedef void (*FWCfgCallback)(void *opaque);
60 typedef void (*FWCfgWriteCallback)(void *opaque, off_t start, size_t len);
61 
62 struct FWCfgState {
63     /*< private >*/
64     SysBusDevice parent_obj;
65     /*< public >*/
66 
67     uint16_t file_slots;
68     FWCfgEntry *entries[2];
69     int *entry_order;
70     FWCfgFiles *files;
71     uint16_t cur_entry;
72     uint32_t cur_offset;
73     Notifier machine_ready;
74 
75     int fw_cfg_order_override;
76 
77     bool dma_enabled;
78     dma_addr_t dma_addr;
79     AddressSpace *dma_as;
80     MemoryRegion dma_iomem;
81 
82     /* restore during migration */
83     bool acpi_mr_restore;
84     uint64_t table_mr_size;
85     uint64_t linker_mr_size;
86     uint64_t rsdp_mr_size;
87 };
88 
89 struct FWCfgIoState {
90     /*< private >*/
91     FWCfgState parent_obj;
92     /*< public >*/
93 
94     MemoryRegion comb_iomem;
95 };
96 
97 struct FWCfgMemState {
98     /*< private >*/
99     FWCfgState parent_obj;
100     /*< public >*/
101 
102     MemoryRegion ctl_iomem, data_iomem;
103     uint32_t data_width;
104     MemoryRegionOps wide_data_ops;
105 };
106 
107 /**
108  * fw_cfg_add_bytes:
109  * @s: fw_cfg device being modified
110  * @key: selector key value for new fw_cfg item
111  * @data: pointer to start of item data
112  * @len: size of item data
113  *
114  * Add a new fw_cfg item, available by selecting the given key, as a raw
115  * "blob" of the given size. The data referenced by the starting pointer
116  * is only linked, NOT copied, into the data structure of the fw_cfg device.
117  */
118 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len);
119 
120 /**
121  * fw_cfg_add_bytes_callback:
122  * @s: fw_cfg device being modified
123  * @key: selector key value for new fw_cfg item
124  * @select_cb: callback function when selecting
125  * @write_cb: callback function after a write
126  * @callback_opaque: argument to be passed into callback function
127  * @data: pointer to start of item data
128  * @len: size of item data
129  * @read_only: is file read only
130  *
131  * Add a new fw_cfg item, available by selecting the given key, as a raw
132  * "blob" of the given size. The data referenced by the starting pointer
133  * is only linked, NOT copied, into the data structure of the fw_cfg device.
134  */
135 void fw_cfg_add_bytes_callback(FWCfgState *s, uint16_t key,
136                                FWCfgCallback select_cb,
137                                FWCfgWriteCallback write_cb,
138                                void *callback_opaque,
139                                void *data, size_t len,
140                                bool read_only);
141 
142 /**
143  * fw_cfg_add_string:
144  * @s: fw_cfg device being modified
145  * @key: selector key value for new fw_cfg item
146  * @value: NUL-terminated ascii string
147  *
148  * Add a new fw_cfg item, available by selecting the given key. The item
149  * data will consist of a dynamically allocated copy of the provided string,
150  * including its NUL terminator.
151  */
152 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value);
153 
154 /**
155  * fw_cfg_modify_string:
156  * @s: fw_cfg device being modified
157  * @key: selector key value for new fw_cfg item
158  * @value: NUL-terminated ascii string
159  *
160  * Replace the fw_cfg item available by selecting the given key. The new
161  * data will consist of a dynamically allocated copy of the provided string,
162  * including its NUL terminator. The data being replaced, assumed to have
163  * been dynamically allocated during an earlier call to either
164  * fw_cfg_add_string() or fw_cfg_modify_string(), is freed before returning.
165  */
166 void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value);
167 
168 /**
169  * fw_cfg_add_i16:
170  * @s: fw_cfg device being modified
171  * @key: selector key value for new fw_cfg item
172  * @value: 16-bit integer
173  *
174  * Add a new fw_cfg item, available by selecting the given key. The item
175  * data will consist of a dynamically allocated copy of the given 16-bit
176  * value, converted to little-endian representation.
177  */
178 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value);
179 
180 /**
181  * fw_cfg_modify_i16:
182  * @s: fw_cfg device being modified
183  * @key: selector key value for new fw_cfg item
184  * @value: 16-bit integer
185  *
186  * Replace the fw_cfg item available by selecting the given key. The new
187  * data will consist of a dynamically allocated copy of the given 16-bit
188  * value, converted to little-endian representation. The data being replaced,
189  * assumed to have been dynamically allocated during an earlier call to
190  * either fw_cfg_add_i16() or fw_cfg_modify_i16(), is freed before returning.
191  */
192 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value);
193 
194 /**
195  * fw_cfg_add_i32:
196  * @s: fw_cfg device being modified
197  * @key: selector key value for new fw_cfg item
198  * @value: 32-bit integer
199  *
200  * Add a new fw_cfg item, available by selecting the given key. The item
201  * data will consist of a dynamically allocated copy of the given 32-bit
202  * value, converted to little-endian representation.
203  */
204 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value);
205 
206 /**
207  * fw_cfg_modify_i32:
208  * @s: fw_cfg device being modified
209  * @key: selector key value for new fw_cfg item
210  * @value: 32-bit integer
211  *
212  * Replace the fw_cfg item available by selecting the given key. The new
213  * data will consist of a dynamically allocated copy of the given 32-bit
214  * value, converted to little-endian representation. The data being replaced,
215  * assumed to have been dynamically allocated during an earlier call to
216  * either fw_cfg_add_i32() or fw_cfg_modify_i32(), is freed before returning.
217  */
218 void fw_cfg_modify_i32(FWCfgState *s, uint16_t key, uint32_t value);
219 
220 /**
221  * fw_cfg_add_i64:
222  * @s: fw_cfg device being modified
223  * @key: selector key value for new fw_cfg item
224  * @value: 64-bit integer
225  *
226  * Add a new fw_cfg item, available by selecting the given key. The item
227  * data will consist of a dynamically allocated copy of the given 64-bit
228  * value, converted to little-endian representation.
229  */
230 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value);
231 
232 /**
233  * fw_cfg_modify_i64:
234  * @s: fw_cfg device being modified
235  * @key: selector key value for new fw_cfg item
236  * @value: 64-bit integer
237  *
238  * Replace the fw_cfg item available by selecting the given key. The new
239  * data will consist of a dynamically allocated copy of the given 64-bit
240  * value, converted to little-endian representation. The data being replaced,
241  * assumed to have been dynamically allocated during an earlier call to
242  * either fw_cfg_add_i64() or fw_cfg_modify_i64(), is freed before returning.
243  */
244 void fw_cfg_modify_i64(FWCfgState *s, uint16_t key, uint64_t value);
245 
246 /**
247  * fw_cfg_add_file:
248  * @s: fw_cfg device being modified
249  * @filename: name of new fw_cfg file item
250  * @data: pointer to start of item data
251  * @len: size of item data
252  *
253  * Add a new NAMED fw_cfg item as a raw "blob" of the given size. The data
254  * referenced by the starting pointer is only linked, NOT copied, into the
255  * data structure of the fw_cfg device.
256  * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
257  * will be used; also, a new entry will be added to the file directory
258  * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
259  * data size, and assigned selector key value.
260  */
261 void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
262                      size_t len);
263 
264 /**
265  * fw_cfg_add_file_callback:
266  * @s: fw_cfg device being modified
267  * @filename: name of new fw_cfg file item
268  * @select_cb: callback function when selecting
269  * @write_cb: callback function after a write
270  * @callback_opaque: argument to be passed into callback function
271  * @data: pointer to start of item data
272  * @len: size of item data
273  * @read_only: is file read only
274  *
275  * Add a new NAMED fw_cfg item as a raw "blob" of the given size. The data
276  * referenced by the starting pointer is only linked, NOT copied, into the
277  * data structure of the fw_cfg device.
278  * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
279  * will be used; also, a new entry will be added to the file directory
280  * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
281  * data size, and assigned selector key value.
282  * Additionally, set a callback function (and argument) to be called each
283  * time this item is selected (by having its selector key either written to
284  * the fw_cfg control register, or passed to QEMU in FWCfgDmaAccess.control
285  * with FW_CFG_DMA_CTL_SELECT).
286  */
287 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
288                               FWCfgCallback select_cb,
289                               FWCfgWriteCallback write_cb,
290                               void *callback_opaque,
291                               void *data, size_t len, bool read_only);
292 
293 /**
294  * fw_cfg_modify_file:
295  * @s: fw_cfg device being modified
296  * @filename: name of new fw_cfg file item
297  * @data: pointer to start of item data
298  * @len: size of item data
299  *
300  * Replace a NAMED fw_cfg item. If an existing item is found, its callback
301  * information will be cleared, and a pointer to its data will be returned
302  * to the caller, so that it may be freed if necessary. If an existing item
303  * is not found, this call defaults to fw_cfg_add_file(), and NULL is
304  * returned to the caller.
305  * In either case, the new item data is only linked, NOT copied, into the
306  * data structure of the fw_cfg device.
307  *
308  * Returns: pointer to old item's data, or NULL if old item does not exist.
309  */
310 void *fw_cfg_modify_file(FWCfgState *s, const char *filename, void *data,
311                          size_t len);
312 
313 /**
314  * fw_cfg_add_from_generator:
315  * @s: fw_cfg device being modified
316  * @filename: name of new fw_cfg file item
317  * @gen_id: name of object implementing FW_CFG_DATA_GENERATOR interface
318  * @errp: pointer to a NULL initialized error object
319  *
320  * Add a new NAMED fw_cfg item with the content generated from the
321  * @gen_id object. The data generated by the @gen_id object is copied
322  * into the data structure of the fw_cfg device.
323  * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
324  * will be used; also, a new entry will be added to the file directory
325  * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
326  * data size, and assigned selector key value.
327  *
328  * Returns: %true on success, %false on error.
329  */
330 bool fw_cfg_add_from_generator(FWCfgState *s, const char *filename,
331                                const char *gen_id, Error **errp);
332 
333 /**
334  * fw_cfg_add_extra_pci_roots:
335  * @bus: main pci root bus to be scanned from
336  * @s: fw_cfg device being modified
337  *
338  * Add a new fw_cfg item...
339  */
340 void fw_cfg_add_extra_pci_roots(PCIBus *bus, FWCfgState *s);
341 
342 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
343                                 AddressSpace *dma_as);
344 FWCfgState *fw_cfg_init_io(uint32_t iobase);
345 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr);
346 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
347                                  hwaddr data_addr, uint32_t data_width,
348                                  hwaddr dma_addr, AddressSpace *dma_as);
349 
350 FWCfgState *fw_cfg_find(void);
351 bool fw_cfg_dma_enabled(void *opaque);
352 
353 /**
354  * fw_cfg_arch_key_name:
355  *
356  * @key: The uint16 selector key.
357  *
358  * The key is architecture-specific (the FW_CFG_ARCH_LOCAL mask is expected
359  * to be set in the key).
360  *
361  * Returns: The stringified architecture-specific name if the selector
362  *          refers to a well-known numerically defined item, or NULL on
363  *          key lookup failure.
364  */
365 const char *fw_cfg_arch_key_name(uint16_t key);
366 
367 /**
368  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
369  *                          by key.
370  * @fw_cfg:         The firmware config instance to store the data in.
371  * @size_key:       The firmware config key to store the size of the loaded
372  *                  data under, with fw_cfg_add_i32().
373  * @data_key:       The firmware config key to store the loaded data under,
374  *                  with fw_cfg_add_bytes().
375  * @image_name:     The name of the image file to load. If it is NULL, the
376  *                  function returns without doing anything.
377  * @try_decompress: Whether the image should be decompressed (gunzipped) before
378  *                  adding it to fw_cfg. If decompression fails, the image is
379  *                  loaded as-is.
380  *
381  * In case of failure, the function prints an error message to stderr and the
382  * process exits with status 1.
383  */
384 void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
385                           uint16_t data_key, const char *image_name,
386                           bool try_decompress);
387 
388 #endif
389