@samitouri / QOSamiQemu / commits / 40fd898e86

hw/nvram: add load_image_to_fw_cfg_file()

Function is simliar to load_image_to_fw_cfg() but loads the image into a named fw_cfg file instead of fixed keys. Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-id: 20260521112806.504961-2-kraxel@redhat.com Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Gerd Hoffmann committed May 21, 2026 at 13:28 UTC 40fd898e86fbf114f7c929528dc68433fe85cae0
2 files changed +37
hw/nvram/fw_cfg.c
+22
@@ -1139,6 +1139,28 @@ void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
1139 fw_cfg_add_bytes(fw_cfg, data_key, data, size);
1140 }
1141
1142 +void load_image_to_fw_cfg_file(FWCfgState *fw_cfg,
1143 + const char *fw_cfg_name,
1144 + const char *image_name)
1145 +{
1146 + GMappedFile *mapped_file;
1147 + GError *gerr = NULL;
1148 +
1149 + if (image_name == NULL) {
1150 + return;
1151 + }
1152 +
1153 + mapped_file = g_mapped_file_new(image_name, false, &gerr);
1154 + if (!mapped_file) {
1155 + error_report("qemu: error reading %s: %s",
1156 + image_name, gerr->message);
1157 + exit(1);
1158 + }
1159 + fw_cfg_add_file(fw_cfg, fw_cfg_name,
1160 + g_mapped_file_get_contents(mapped_file),
1161 + g_mapped_file_get_length(mapped_file));
1162 +}
1163 +
1164 static void fw_cfg_class_init(ObjectClass *klass, const void *data)
1165 {
1166 DeviceClass *dc = DEVICE_CLASS(klass);
include/hw/nvram/fw_cfg.h
+15
@@ -399,4 +399,19 @@ void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
399 uint16_t data_key, const char *image_name,
400 bool try_decompress);
401
402 +/**
403 + * load_image_to_fw_cfg_file() - Load an image file into an fw_cfg entry
404 + * identified by fw_cfg file name.
405 + * @fw_cfg: The firmware config instance to store the data in.
406 + * @fw_cfg_name: The name of the fw_cfg (pseudo) file.
407 + * @image_name: The name of the image file to load. If it is NULL, the
408 + * function returns without doing anything.
409 + *
410 + * In case of failure, the function prints an error message to stderr and the
411 + * process exits with status 1.
412 + */
413 +void load_image_to_fw_cfg_file(FWCfgState *fw_cfg,
414 + const char *fw_cfg_name,
415 + const char *image_name);
416 +
417 #endif