xref: /openbmc/qemu/include/hw/nvram/fw_cfg.h (revision 311a0106)
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 typedef struct FWCfgEntry FWCfgEntry;
63 
64 struct 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;
83 
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;
89 };
90 
91 struct FWCfgIoState {
92     /*< private >*/
93     FWCfgState parent_obj;
94     /*< public >*/
95 
96     MemoryRegion comb_iomem;
97 };
98 
99 struct 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 
109 /**
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  */
120 void fw_cfg_add_bytes(FWCfgState *s, uint16_t key, void *data, size_t len);
121 
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  */
132 void fw_cfg_add_string(FWCfgState *s, uint16_t key, const char *value);
133 
134 /**
135  * 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  */
146 void fw_cfg_modify_string(FWCfgState *s, uint16_t key, const char *value);
147 
148 /**
149  * 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  */
158 void fw_cfg_add_i16(FWCfgState *s, uint16_t key, uint16_t value);
159 
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  */
172 void fw_cfg_modify_i16(FWCfgState *s, uint16_t key, uint16_t value);
173 
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  */
184 void fw_cfg_add_i32(FWCfgState *s, uint16_t key, uint32_t value);
185 
186 /**
187  * 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  */
198 void fw_cfg_modify_i32(FWCfgState *s, uint16_t key, uint32_t value);
199 
200 /**
201  * 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  */
210 void fw_cfg_add_i64(FWCfgState *s, uint16_t key, uint64_t value);
211 
212 /**
213  * 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  */
224 void fw_cfg_modify_i64(FWCfgState *s, uint16_t key, uint64_t value);
225 
226 /**
227  * 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  */
241 void fw_cfg_add_file(FWCfgState *s, const char *filename, void *data,
242                      size_t len);
243 
244 /**
245  * fw_cfg_add_file_callback:
246  * @s: fw_cfg device being modified
247  * @filename: name of new fw_cfg file item
248  * @select_cb: callback function when selecting
249  * @write_cb: callback function after a write
250  * @callback_opaque: argument to be passed into callback function
251  * @data: pointer to start of item data
252  * @len: size of item data
253  * @read_only: is file read only
254  *
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
263  * 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).
266  */
267 void fw_cfg_add_file_callback(FWCfgState *s, const char *filename,
268                               FWCfgCallback select_cb,
269                               FWCfgWriteCallback write_cb,
270                               void *callback_opaque,
271                               void *data, size_t len, bool read_only);
272 
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  */
290 void *fw_cfg_modify_file(FWCfgState *s, const char *filename, void *data,
291                          size_t len);
292 
293 /**
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.
307  *
308  * Returns: %true on success, %false on error.
309  */
310 bool fw_cfg_add_from_generator(FWCfgState *s, const char *filename,
311                                const char *gen_id, Error **errp);
312 
313 /**
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  */
320 void fw_cfg_add_extra_pci_roots(PCIBus *bus, FWCfgState *s);
321 
322 FWCfgState *fw_cfg_init_io_dma(uint32_t iobase, uint32_t dma_iobase,
323                                 AddressSpace *dma_as);
324 FWCfgState *fw_cfg_init_mem(hwaddr ctl_addr, hwaddr data_addr);
325 FWCfgState *fw_cfg_init_mem_wide(hwaddr ctl_addr,
326                                  hwaddr data_addr, uint32_t data_width,
327                                  hwaddr dma_addr, AddressSpace *dma_as);
328 
329 FWCfgState *fw_cfg_find(void);
330 bool fw_cfg_dma_enabled(void *opaque);
331 
332 /**
333  * fw_cfg_arch_key_name:
334  *
335  * @key: The uint16 selector key.
336  *
337  * The key is architecture-specific (the FW_CFG_ARCH_LOCAL mask is expected
338  * to be set in the key).
339  *
340  * Returns: The stringified architecture-specific name if the selector
341  *          refers to a well-known numerically defined item, or NULL on
342  *          key lookup failure.
343  */
344 const char *fw_cfg_arch_key_name(uint16_t key);
345 
346 /**
347  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
348  *                          by key.
349  * @fw_cfg:         The firmware config instance to store the data in.
350  * @size_key:       The firmware config key to store the size of the loaded
351  *                  data under, with fw_cfg_add_i32().
352  * @data_key:       The firmware config key to store the loaded data under,
353  *                  with fw_cfg_add_bytes().
354  * @image_name:     The name of the image file to load. If it is NULL, the
355  *                  function returns without doing anything.
356  * @try_decompress: Whether the image should be decompressed (gunzipped) before
357  *                  adding it to fw_cfg. If decompression fails, the image is
358  *                  loaded as-is.
359  *
360  * In case of failure, the function prints an error message to stderr and the
361  * process exits with status 1.
362  */
363 void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
364                           uint16_t data_key, const char *image_name,
365                           bool try_decompress);
366 
367 #endif
368