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

@@ -909,6 +909,32 @@ mod unit_tests {
}
}
#[test]
fn test_string_item() {
let gm = GuestMemoryAtomic::new(
GuestMemoryMmap::from_ranges(&[(GuestAddress(0), RAM_64BIT_START.0 as usize)]).unwrap(),
);
let mut fw_cfg = FwCfg::new(gm);
// Simulate OVMF X-PciMmio64Mb string item for GPU CC passthrough
let item = FwCfgItem {
name: "opt/ovmf/X-PciMmio64Mb".to_owned(),
content: FwCfgContent::Bytes("262144".as_bytes().to_vec()),
};
fw_cfg.add_item(item).unwrap();
let expected = b"262144";
let mut data = vec![0u8];
// Select the first file item (FW_CFG_FILE_FIRST = 0x20)
fw_cfg.write(0, SELECTOR_OFFSET, &[FW_CFG_FILE_FIRST as u8, 0]);
for &byte in expected.iter() {
fw_cfg.read(0, DATA_OFFSET, &mut data);
assert_eq!(data[0], byte);
}
}
#[test]
fn test_dma() {
let code = [