vmm, devices: Add fw_cfg string item support

QEMU supports passing inline string values to the guest via fw_cfg
(-fw_cfg name=...,string=...). Cloud Hypervisor previously only
supported file-backed fw_cfg items. This adds the 'string' option
so users can pass values like OVMF's X-PciMmio64Mb without creating
a temporary file on the host.

Each fw_cfg item now accepts exactly one of 'file' or 'string'.
The FwCfgInvalidItem invariant is validated in PayloadConfig::validate()
(via FwCfgConfig::validate()), covering both CLI and JSON API paths.
The populate_fw_cfg match arm uses unreachable!() since validation
guarantees the invariant holds at that point.

CLI syntax:
  --fw-cfg-config items=[name=opt/ovmf/X-PciMmio64Mb,string=262144]

Signed-off-by: Keith Adler <kadler@cloudflare.com>
This commit is contained in:
Keith Adler
2026-04-14 14:48:49 -05:00
committed by Rob Bradford
parent e4e3375a8d
commit 926dd1e141
6 changed files with 209 additions and 24 deletions

View File

@@ -1185,15 +1185,24 @@ impl Vm {
initramfs_option = initramfs;
}
let mut fw_cfg_item_list_option: Option<Vec<FwCfgItem>> = None;
if let Some(fw_cfg_files) = &fw_cfg_config.items {
if let Some(fw_cfg_items) = &fw_cfg_config.items {
let mut fw_cfg_item_list = vec![];
for fw_cfg_file in fw_cfg_files.item_list.clone() {
fw_cfg_item_list.push(FwCfgItem {
name: fw_cfg_file.name,
content: devices::legacy::fw_cfg::FwCfgContent::File(
for fw_cfg_item in fw_cfg_items.item_list.clone() {
let content = match (fw_cfg_item.string, fw_cfg_item.file) {
(Some(string_val), None) => {
devices::legacy::fw_cfg::FwCfgContent::Bytes(string_val.into_bytes())
}
(None, Some(file_path)) => devices::legacy::fw_cfg::FwCfgContent::File(
0,
File::open(fw_cfg_file.file).map_err(Error::AddingFwCfgItem)?,
File::open(file_path).map_err(Error::AddingFwCfgItem)?,
),
_ => unreachable!(
"PayloadConfig::validate() ensures either 'file' or 'string' is present"
),
};
fw_cfg_item_list.push(FwCfgItem {
name: fw_cfg_item.name,
content,
});
}
fw_cfg_item_list_option = Some(fw_cfg_item_list);