mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
Historically the Cloud Hypervisor coding style has been to ensure that all imports are ordered and placed in a single group. Unfortunately cargo fmt has no support for ensuring that all imports are in a single group so if whitespace lines were added as part of the import statements then they would only be odered correctly in the group. By adopting "group_imports="StdExternalCrate" we can enforce a style where imports are placed in at most three groups for std, external crates and the crate itself. Choosing a style enforceable by the tooling reduces the reviewer burden. Signed-off-by: Rob Bradford <rbradford@rivosinc.com>
51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
// Copyright 2020 Arm Limited (or its affiliates). All rights reserved.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use std::io::{Read, Seek, SeekFrom};
|
|
use std::os::fd::AsFd;
|
|
use std::result;
|
|
|
|
use thiserror::Error;
|
|
use vm_memory::{GuestAddress, GuestMemory};
|
|
|
|
/// Errors thrown while loading UEFI binary
|
|
#[derive(Debug, Error)]
|
|
pub enum Error {
|
|
/// Unable to seek to UEFI image start.
|
|
#[error("Unable to seek to UEFI image start")]
|
|
SeekUefiStart,
|
|
/// Unable to seek to UEFI image end.
|
|
#[error("Unable to seek to UEFI image end")]
|
|
SeekUefiEnd,
|
|
/// UEFI image too big.
|
|
#[error("UEFI image too big")]
|
|
UefiTooBig,
|
|
/// Unable to read UEFI image
|
|
#[error("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 + AsFd,
|
|
{
|
|
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.rewind().map_err(|_| Error::SeekUefiStart)?;
|
|
guest_mem
|
|
.read_exact_volatile_from(guest_addr, &mut uefi_image.as_fd(), uefi_size)
|
|
.map_err(|_| Error::ReadUefiImage)
|
|
}
|