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