xref: /qemu/include/hw/nvram/fw_cfg.h (revision 84615a19)
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_read_bytes_ptr:
144  * @s: fw_cfg device being modified
145  * @key: selector key value for new fw_cfg item
146  *
147  * Reads an existing fw_cfg data pointer.
148  */
149 void *fw_cfg_read_bytes_ptr(FWCfgState *s, uint16_t key);
150 
151 /**
152  * fw_cfg_add_string:
153  * @s: fw_cfg device being modified
154  * @key: selector key value for new fw_cfg item
155  * @value: NUL-terminated ascii string
156  *
157  * Add a new fw_cfg item, available by selecting the given key. The item
158  * data will consist of a dynamically allocated copy of the provided string,
159  * including its NUL terminator.
160  */
161 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value);
162 
163 /**
164  * fw_cfg_modify_string:
165  * @s: fw_cfg device being modified
166  * @key: selector key value for new fw_cfg item
167  * @value: NUL-terminated ascii string
168  *
169  * Replace the fw_cfg item available by selecting the given key. The new
170  * data will consist of a dynamically allocated copy of the provided string,
171  * including its NUL terminator. The data being replaced, assumed to have
172  * been dynamically allocated during an earlier call to either
173  * fw_cfg_add_string() or fw_cfg_modify_string(), is freed before returning.
174  */
175 void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value);
176 
177 /**
178  * fw_cfg_add_i16:
179  * @s: fw_cfg device being modified
180  * @key: selector key value for new fw_cfg item
181  * @value: 16-bit integer
182  *
183  * Add a new fw_cfg item, available by selecting the given key. The item
184  * data will consist of a dynamically allocated copy of the given 16-bit
185  * value, converted to little-endian representation.
186  */
187 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value);
188 
189 /**
190  * fw_cfg_modify_i16:
191  * @s: fw_cfg device being modified
192  * @key: selector key value for new fw_cfg item
193  * @value: 16-bit integer
194  *
195  * Replace the fw_cfg item available by selecting the given key. The new
196  * data will consist of a dynamically allocated copy of the given 16-bit
197  * value, converted to little-endian representation. The data being replaced,
198  * assumed to have been dynamically allocated during an earlier call to
199  * either fw_cfg_add_i16() or fw_cfg_modify_i16(), is freed before returning.
200  */
201 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value);
202 
203 /**
204  * fw_cfg_add_i32:
205  * @s: fw_cfg device being modified
206  * @key: selector key value for new fw_cfg item
207  * @value: 32-bit integer
208  *
209  * Add a new fw_cfg item, available by selecting the given key. The item
210  * data will consist of a dynamically allocated copy of the given 32-bit
211  * value, converted to little-endian representation.
212  */
213 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value);
214 
215 /**
216  * fw_cfg_modify_i32:
217  * @s: fw_cfg device being modified
218  * @key: selector key value for new fw_cfg item
219  * @value: 32-bit integer
220  *
221  * Replace the fw_cfg item available by selecting the given key. The new
222  * data will consist of a dynamically allocated copy of the given 32-bit
223  * value, converted to little-endian representation. The data being replaced,
224  * assumed to have been dynamically allocated during an earlier call to
225  * either fw_cfg_add_i32() or fw_cfg_modify_i32(), is freed before returning.
226  */
227 void fw_cfg_modify_i32(FWCfgState *s, uint16_t key, uint32_t value);
228 
229 /**
230  * fw_cfg_add_i64:
231  * @s: fw_cfg device being modified
232  * @key: selector key value for new fw_cfg item
233  * @value: 64-bit integer
234  *
235  * Add a new fw_cfg item, available by selecting the given key. The item
236  * data will consist of a dynamically allocated copy of the given 64-bit
237  * value, converted to little-endian representation.
238  */
239 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value);
240 
241 /**
242  * fw_cfg_modify_i64:
243  * @s: fw_cfg device being modified
244  * @key: selector key value for new fw_cfg item
245  * @value: 64-bit integer
246  *
247  * Replace the fw_cfg item available by selecting the given key. The new
248  * data will consist of a dynamically allocated copy of the given 64-bit
249  * value, converted to little-endian representation. The data being replaced,
250  * assumed to have been dynamically allocated during an earlier call to
251  * either fw_cfg_add_i64() or fw_cfg_modify_i64(), is freed before returning.
252  */
253 void fw_cfg_modify_i64(FWCfgState *s, uint16_t key, uint64_t value);
254 
255 /**
256  * fw_cfg_add_file:
257  * @s: fw_cfg device being modified
258  * @filename: name of new fw_cfg file item
259  * @data: pointer to start of item data
260  * @len: size of item data
261  *
262  * Add a new NAMED fw_cfg item as a raw "blob" of the given size. The data
263  * referenced by the starting pointer is only linked, NOT copied, into the
264  * data structure of the fw_cfg device.
265  * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
266  * will be used; also, a new entry will be added to the file directory
267  * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
268  * data size, and assigned selector key value.
269  */
270 void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
271                      size_t len);
272 
273 /**
274  * fw_cfg_add_file_callback:
275  * @s: fw_cfg device being modified
276  * @filename: name of new fw_cfg file item
277  * @select_cb: callback function when selecting
278  * @write_cb: callback function after a write
279  * @callback_opaque: argument to be passed into callback function
280  * @data: pointer to start of item data
281  * @len: size of item data
282  * @read_only: is file read only
283  *
284  * Add a new NAMED fw_cfg item as a raw "blob" of the given size. The data
285  * referenced by the starting pointer is only linked, NOT copied, into the
286  * data structure of the fw_cfg device.
287  * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
288  * will be used; also, a new entry will be added to the file directory
289  * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
290  * data size, and assigned selector key value.
291  * Additionally, set a callback function (and argument) to be called each
292  * time this item is selected (by having its selector key either written to
293  * the fw_cfg control register, or passed to QEMU in FWCfgDmaAccess.control
294  * with FW_CFG_DMA_CTL_SELECT).
295  */
296 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
297                               FWCfgCallback select_cb,
298                               FWCfgWriteCallback write_cb,
299                               void *callback_opaque,
300                               void *data, size_t len, bool read_only);
301 
302 /**
303  * fw_cfg_modify_file:
304  * @s: fw_cfg device being modified
305  * @filename: name of new fw_cfg file item
306  * @data: pointer to start of item data
307  * @len: size of item data
308  *
309  * Replace a NAMED fw_cfg item. If an existing item is found, its callback
310  * information will be cleared, and a pointer to its data will be returned
311  * to the caller, so that it may be freed if necessary. If an existing item
312  * is not found, this call defaults to fw_cfg_add_file(), and NULL is
313  * returned to the caller.
314  * In either case, the new item data is only linked, NOT copied, into the
315  * data structure of the fw_cfg device.
316  *
317  * Returns: pointer to old item's data, or NULL if old item does not exist.
318  */
319 void *fw_cfg_modify_file(FWCfgState *s, const char *filename, void *data,
320                          size_t len);
321 
322 /**
323  * fw_cfg_add_from_generator:
324  * @s: fw_cfg device being modified
325  * @filename: name of new fw_cfg file item
326  * @gen_id: name of object implementing FW_CFG_DATA_GENERATOR interface
327  * @errp: pointer to a NULL initialized error object
328  *
329  * Add a new NAMED fw_cfg item with the content generated from the
330  * @gen_id object. The data generated by the @gen_id object is copied
331  * into the data structure of the fw_cfg device.
332  * The next available (unused) selector key starting at FW_CFG_FILE_FIRST
333  * will be used; also, a new entry will be added to the file directory
334  * structure residing at key value FW_CFG_FILE_DIR, containing the item name,
335  * data size, and assigned selector key value.
336  *
337  * Returns: %true on success, %false on error.
338  */
339 bool fw_cfg_add_from_generator(FWCfgState *s, const char *filename,
340                                const char *gen_id, Error **errp);
341 
342 /**
343  * fw_cfg_add_extra_pci_roots:
344  * @bus: main pci root bus to be scanned from
345  * @s: fw_cfg device being modified
346  *
347  * Add a new fw_cfg item...
348  */
349 void fw_cfg_add_extra_pci_roots(PCIBus *bus, FWCfgState *s);
350 
351 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
352                                 AddressSpace *dma_as);
353 FWCfgState *fw_cfg_init_io(uint32_t iobase);
354 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr);
355 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
356                                  hwaddr data_addr, uint32_t data_width,
357                                  hwaddr dma_addr, AddressSpace *dma_as);
358 
359 FWCfgState *fw_cfg_find(void);
360 bool fw_cfg_dma_enabled(void *opaque);
361 
362 /**
363  * fw_cfg_arch_key_name:
364  *
365  * @key: The uint16 selector key.
366  *
367  * The key is architecture-specific (the FW_CFG_ARCH_LOCAL mask is expected
368  * to be set in the key).
369  *
370  * Returns: The stringified architecture-specific name if the selector
371  *          refers to a well-known numerically defined item, or NULL on
372  *          key lookup failure.
373  */
374 const char *fw_cfg_arch_key_name(uint16_t key);
375 
376 /**
377  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
378  *                          by key.
379  * @fw_cfg:         The firmware config instance to store the data in.
380  * @size_key:       The firmware config key to store the size of the loaded
381  *                  data under, with fw_cfg_add_i32().
382  * @data_key:       The firmware config key to store the loaded data under,
383  *                  with fw_cfg_add_bytes().
384  * @image_name:     The name of the image file to load. If it is NULL, the
385  *                  function returns without doing anything.
386  * @try_decompress: Whether the image should be decompressed (gunzipped) before
387  *                  adding it to fw_cfg. If decompression fails, the image is
388  *                  loaded as-is.
389  *
390  * In case of failure, the function prints an error message to stderr and the
391  * process exits with status 1.
392  */
393 void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
394                           uint16_t data_key, const char *image_name,
395                           bool try_decompress);
396 
397 #endif
398