Skip to content

Conversation

@hgarvison
Copy link

Add tool to VmgsTool to read the IGVMfile from a DLL (passed in as a data file) and write it to VMGS FileId 8 (GUEST_FIRMWARE). To do this pass one of three resource codes (nonconfidential, snp, tdx) into the cmdline tool:

vmgstool.exe copy-igvmfile --filepath --keypath --datapath --resource-code

@hgarvison hgarvison requested a review from a team as a code owner December 15, 2025 20:24
Copilot AI review requested due to automatic review settings December 15, 2025 20:24
@hgarvison hgarvison requested a review from a team as a code owner December 15, 2025 20:24
@github-actions github-actions bot added Guide unsafe Related to unsafe code labels Dec 15, 2025
@github-actions
Copy link

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds functionality to vmgstool for reading IGVM firmware files from Windows DLLs and writing them to VMGS file ID 8 (GUEST_FIRMWARE). The implementation uses Windows API calls to extract resources from DLLs and supports multiple resource codes (nonconfidential, snp, snp_no_hcl, tdx, tdx_no_hcl) for different VM configurations.

Key changes:

  • New copy-igvmfile command that extracts IGVM files from DLLs using Windows resource APIs
  • Support for encrypted and unencrypted VMGS files when writing IGVM data
  • Platform-specific implementation for Windows x86_64 only

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 7 comments.

File Description
vm/vmgs/vmgstool/src/main.rs Implements the copy-igvmfile command with Windows API resource loading, adds error handling, command-line parsing, and test cases
vm/vmgs/vmgstool/build.rs Adds rustc-check-cfg directive for guest_arch configuration
vm/vmgs/vmgstool/Cargo.toml Adds winapi dependency with required Windows API features
Guide/src/dev_guide/dev_tools/vmgstool.md Documents the new copy-igvmfile command usage

@github-actions
Copy link

@github-actions
Copy link

GspUnknown,
#[error("VMGS file is using an unknown encryption algorithm")]
EncryptionUnknown,
#[cfg(all(windows, guest_arch = "x86_64"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes any of this x86_64 specific?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file this command is being used to parse (vmfirmwarehcl.dll) doesn't exist in ARM.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the code is capable of running on ARM we should let it, even if it'll never be used. cfgs like this should only be used when the code can't run on a given platform due to actual hardware differences.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, thanks for the feedback. I'll take out the cfgs and gate the couple of tests that try to access vmfirmwarehcl.dll, so they don't fail in the ARM tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do what I suggested below and use include_bytes, the test should work fine on arm.

Copy link
Contributor

@smalis-msft smalis-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-vmgs portions look good to me, I'll let @tjones60 do the final signoff

@github-actions
Copy link

Ok(())
}

async fn write_igvmfile(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this function is necessary, just put the calls to read_igvmfile and vmgs_write in vmgs_file_copy_igvmfile.


let encrypt = key_path.is_some();
let mut vmgs = vmgs_file_open(file_path, key_path, OpenMode::ReadWrite).await?;
eprintln!("Reading IGVMfile from DLL");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, as written we log "Reading IGVMfile..." three times; we should do that only once.

allow_overwrite: bool,
resource_code: ResourceCode,
) -> Result<(), Error> {
eprintln!("Writing IGVMfile to VMGS");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is redundant, vmgs_write logs the file ID

use std::io::{Read, Seek, SeekFrom};

eprintln!("Reading IGVMfile from DLL");
// Convert the wide string back to a PathBuf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment stale?

#[error("VMGS file is using an unknown encryption algorithm")]
EncryptionUnknown,
#[error("Unable to read IGVM file with Error: {0}")]
UnableToReadIgvmFile(String),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just be

#[error("Unable to read IGVM file")]
IgvmFile(#[source] std::io::Error)

That way you don't have to manually format the string every time. Also, we could probably just use Error::DataFile for this, since for this operation it is obvious that the data file is the igvm file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, since the error from try_find_resource_from_dll is anyhow, how about we use this for the anyhow errors and use DataFile for the io Errors

#[error("Unable to parse IGVM file")]
IgvmFile(#[source] anyhow::Error)

async fn read_igvmfile(dll_path: PathBuf, resource_code: ResourceCode) -> Result<Vec<u8>, Error> {
use std::io::{Read, Seek, SeekFrom};

eprintln!("Reading IGVMfile from DLL");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is this the correct stylization? Seems like "IGVM file" would look better than "IGVMfile" here and elsewhere.

file.read_exact(&mut bytes)
.map_err(|e| Error::UnableToReadIgvmFile(format!("Failed to read resource data: {}", e)))?;

eprintln!("Successfully loaded IGVMfile from DLL");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this seems redundant

/// `keypath` and `encryptionalgorithm` must both be specified if encrypted
/// guest state is required.
Create {
/// VMGS file path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this added? FilePathArg should contain the appropriate doc comment used in the help message.

### Read DLL File to Write IGVMfile to VMGS

Additionally, the VmgsTool contains a tool to read the IGVMfile from a DLL (passed in as a data file)
and write it to VMGS FileId 8 (GUEST_FIRMWARE). To do this pass one of three resource codes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...five resource codes... ?


fn parse_resource_code(resource_code: &str) -> Result<ResourceCode, &'static str> {
match resource_code {
"nonconfidential" => Ok(ResourceCode::NonConfidential),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: All the other values use all caps, would it be ok to be consistent here and use all caps for the resource codes as well? We should probably just make it case insensitive, but I'd rather be consistent for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, since this isn't an OpenEnum used elsewhere, we can probably just have clap auto generate the help message and parsing function (#[derive(clap::ValueEnum)]). It wouldn't be consistent with the other ones (which are used in data structures that rely on their format), but would prevent us from having extra boilerplate.

}

#[derive(Debug, Clone, Copy)]
#[repr(i32)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DllResourceDescriptor::new accepts a u32, but here you are saying it is an i32. Shouldn't they be the same?

) -> Result<(), Error> {
eprintln!("Writing IGVMfile to VMGS");

let encrypt = key_path.is_some();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we ever want to encrypt the IGVM file? Would it ever contain user data?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a holdover, I changed it in the Host Agent version, but I guess Heather didn't get a chance to change it here. I'll update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Guide unsafe Related to unsafe code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants