MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / add_kernel_data

Method add_kernel_data

devices/src/legacy/fw_cfg.rs:639–689  ·  view source on GitHub ↗
(
        &mut self,
        file: &File,
        #[cfg(target_arch = "x86_64")] kvm_sev_snp_enabled: bool,
    )

Source from the content-addressed store, hash-verified

637 }
638
639 pub fn add_kernel_data(
640 &mut self,
641 file: &File,
642 #[cfg(target_arch = "x86_64")] kvm_sev_snp_enabled: bool,
643 ) -> Result<()> {
644 let mut buffer = vec![0u8; size_of::<boot_params>()];
645 file.read_exact_at(&mut buffer, 0)?;
646 let bp = boot_params::from_mut_slice(&mut buffer).unwrap();
647 #[cfg(target_arch = "x86_64")]
648 {
649 // For SEV-SNP guests on KVM, don't modify the kernel header so the
650 // bytes sent via fw_cfg match what the VMM hashes for the launch digest.
651 // The guest firmware handles these fields itself.
652 if !kvm_sev_snp_enabled {
653 if bp.hdr.setup_sects == 0 {
654 bp.hdr.setup_sects = 4;
655 }
656 bp.hdr.type_of_loader = 0xff;
657 }
658 }
659 #[cfg(target_arch = "aarch64")]
660 let kernel_start = bp.text_offset;
661 #[cfg(target_arch = "x86_64")]
662 let kernel_start = {
663 let sects = if bp.hdr.setup_sects == 0 {
664 4
665 } else {
666 bp.hdr.setup_sects
667 };
668 (sects as usize + 1) * 512
669 };
670
671 #[cfg(target_arch = "x86_64")]
672 if kernel_start <= buffer.len() {
673 buffer.truncate(kernel_start);
674 } else {
675 buffer.resize(kernel_start, 0);
676 file.read_exact_at(
677 &mut buffer[size_of::<boot_params>()..],
678 size_of::<boot_params>() as u64,
679 )?;
680 }
681
682 self.known_items[FW_CFG_SETUP_SIZE as usize] = FwCfgContent::U32(buffer.len() as u32);
683 self.known_items[FW_CFG_SETUP_DATA as usize] = FwCfgContent::Bytes(buffer);
684 self.known_items[FW_CFG_KERNEL_SIZE as usize] =
685 FwCfgContent::U32(file.metadata()?.len() as u32 - kernel_start as u32);
686 self.known_items[FW_CFG_KERNEL_DATA as usize] =
687 FwCfgContent::File(kernel_start as u64, file.try_clone()?);
688 Ok(())
689 }
690
691 pub fn add_kernel_cmdline(&mut self, s: std::ffi::CString) {
692 let bytes = s.into_bytes_with_nul();

Callers 1

populate_fw_cfgMethod · 0.80

Calls 4

metadataMethod · 0.80
lenMethod · 0.45
resizeMethod · 0.45
try_cloneMethod · 0.45

Tested by

no test coverage detected