diff --git a/hw/nvram/fw_cfg.c b/hw/nvram/fw_cfg.c index a9d45adb2d..b057d870c0 100644 --- a/hw/nvram/fw_cfg.c +++ b/hw/nvram/fw_cfg.c @@ -1139,6 +1139,28 @@ void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, fw_cfg_add_bytes(fw_cfg, data_key, data, size); } +void load_image_to_fw_cfg_file(FWCfgState *fw_cfg, + const char *fw_cfg_name, + const char *image_name) +{ + GMappedFile *mapped_file; + GError *gerr = NULL; + + if (image_name == NULL) { + return; + } + + mapped_file = g_mapped_file_new(image_name, false, &gerr); + if (!mapped_file) { + error_report("qemu: error reading %s: %s", + image_name, gerr->message); + exit(1); + } + fw_cfg_add_file(fw_cfg, fw_cfg_name, + g_mapped_file_get_contents(mapped_file), + g_mapped_file_get_length(mapped_file)); +} + static void fw_cfg_class_init(ObjectClass *klass, const void *data) { DeviceClass *dc = DEVICE_CLASS(klass); diff --git a/include/hw/nvram/fw_cfg.h b/include/hw/nvram/fw_cfg.h index b75858025f..20e2c7190e 100644 --- a/include/hw/nvram/fw_cfg.h +++ b/include/hw/nvram/fw_cfg.h @@ -399,4 +399,19 @@ void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key, uint16_t data_key, const char *image_name, bool try_decompress); +/** + * load_image_to_fw_cfg_file() - Load an image file into an fw_cfg entry + * identified by fw_cfg file name. + * @fw_cfg: The firmware config instance to store the data in. + * @fw_cfg_name: The name of the fw_cfg (pseudo) file. + * @image_name: The name of the image file to load. If it is NULL, the + * function returns without doing anything. + * + * In case of failure, the function prints an error message to stderr and the + * process exits with status 1. + */ +void load_image_to_fw_cfg_file(FWCfgState *fw_cfg, + const char *fw_cfg_name, + const char *image_name); + #endif