Skip to content

Commit

Permalink
set type_of_loader after measurements
Browse files Browse the repository at this point in the history
QEMU assumes all SEV virtual machines use OVMF and EFI stub for booting, and leaves type_of_loader as 0. In stage0, after reading from fw_cfg, the type_of_loader is overwritten to 0. However, the guest kernel needs a non-zero value for direct kernel boot. This patch adds a setter function for type_of_loader, and set it to 0xFF after measurement calculation.

Bug: b/343736249
Change-Id: If388eced6de4e28c5784eca878584bc671008050
  • Loading branch information
dingelish committed Jun 12, 2024
1 parent 536321e commit 999d0c6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions stage0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ pub fn rust64_start(encrypted: u64) -> ! {
})
.unwrap_or_default();

// QEMU assumes all SEV VMs use OVMF, and leaves type_of_loader 0x00. This
// would make the kernel unable to load stage1. We need to fix it to make
// stage1 loads smoothly.
zero_page.set_type_of_loader(zero_page::BOOT_LOADER_TYPE_UNDEFINED);

let memory_map_sha2_256_digest = measure_byte_slice(zero_page.e820_table().as_bytes());

// Generate Stage0 Event Log data.
Expand Down
11 changes: 9 additions & 2 deletions stage0/src/zero_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ impl Default for ZeroPage {
}
}

pub const BOOT_LOADER_TYPE_UNDEFINED: u8 = 0xFF;

impl ZeroPage {
/// Constructs a empty zero page, filling in some magic values needed by the
/// kernel.
pub fn new() -> Self {
let mut zero_page = BootParams::zeroed();
// Magic constants.
// See https://www.kernel.org/doc/html/latest/x86/boot.html#the-real-mode-kernel-header for more details.
zero_page.hdr.type_of_loader = 0xFF; // loader type undefined
// See https://www.kernel.org/doc/html/latest/arch/x86/boot.html#the-real-mode-kernel-header for more details.
zero_page.hdr.type_of_loader = BOOT_LOADER_TYPE_UNDEFINED; // loader type undefined
zero_page.hdr.boot_flag = 0xAA55; // magic number
zero_page.hdr.header = 0x53726448; // Magic "HdrS" string
zero_page.hdr.kernel_alignment = 0x1000000; // Magic number from crosvm source.
Expand Down Expand Up @@ -345,6 +347,11 @@ impl ZeroPage {
// maximum of 1GiB of RAM.
self.inner.hdr.ramdisk_size = ram_disk.len() as u32;
}

/// Sets the type of loader for direct kernel boot
pub fn set_type_of_loader(&mut self, loader_type: u8) {
self.inner.hdr.type_of_loader = loader_type;
}
}

/// Builds an E820 table by reading the low and high memory amount from CMOS.
Expand Down

0 comments on commit 999d0c6

Please sign in to comment.