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