mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-11-09 05:10:07 +00:00
b8b5dccfd8
Implemented an architecture specific function for loading UEFI binary. Changed the logic of loading kernel image: 1. First try to load the image as kernel in PE format; 2. If failed, try again to load it as formatless UEFI binary. Signed-off-by: Jianyong Wu <jianyong.wu@arm.com>
44 lines
1.1 KiB
Rust
44 lines
1.1 KiB
Rust
// Copyright 2020 Arm Limited (or its affiliates). All rights reserved.
|
|
|
|
use std::io::{Read, Seek, SeekFrom};
|
|
use std::result;
|
|
use vm_memory::{Bytes, GuestAddress, GuestMemory};
|
|
|
|
/// Errors thrown while loading UEFI binary
|
|
#[derive(Debug)]
|
|
pub enum Error {
|
|
/// Unable to seek to UEFI image start.
|
|
SeekUefiStart,
|
|
/// Unable to seek to UEFI image end.
|
|
SeekUefiEnd,
|
|
/// UEFI image too big.
|
|
UefiTooBig,
|
|
/// Unable to read UEFI image
|
|
ReadUefiImage,
|
|
}
|
|
type Result<T> = result::Result<T, Error>;
|
|
|
|
pub fn load_uefi<F, M: GuestMemory>(
|
|
guest_mem: &M,
|
|
guest_addr: GuestAddress,
|
|
uefi_image: &mut F,
|
|
) -> Result<()>
|
|
where
|
|
F: Read + Seek,
|
|
{
|
|
let uefi_size = uefi_image
|
|
.seek(SeekFrom::End(0))
|
|
.map_err(|_| Error::SeekUefiEnd)? as usize;
|
|
|
|
// edk2 image on virtual platform is smaller than 3M
|
|
if uefi_size > 0x300000 {
|
|
return Err(Error::UefiTooBig);
|
|
}
|
|
uefi_image
|
|
.seek(SeekFrom::Start(0))
|
|
.map_err(|_| Error::SeekUefiStart)?;
|
|
guest_mem
|
|
.read_exact_from(guest_addr, uefi_image, uefi_size)
|
|
.map_err(|_| Error::ReadUefiImage)
|
|
}
|