Skip to content

Commit

Permalink
loader: support loading device tree on aarch64
Browse files Browse the repository at this point in the history
Signed-off-by: Qiu Wenbo <[email protected]>
  • Loading branch information
crab2313 committed Mar 9, 2020
1 parent 23338de commit 15a04eb
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub enum Error {
CommandLineCopy,
/// Command line overflowed guest memory.
CommandLineOverflow,
/// Device tree bianry too big.
DtbTooBig,
/// Invalid ELF magic number
InvalidElfMagicNumber,
/// Invalid program header size.
Expand Down Expand Up @@ -84,6 +86,8 @@ pub enum Error {
ReadBzImageCompressedKernel,
/// Unable to read Image header
ReadImageHeader,
/// Unable to read DTB image
ReadDtbImage,
/// Unable to seek to kernel start.
SeekKernelStart,
/// Unable to seek to ELF start.
Expand All @@ -100,6 +104,10 @@ pub enum Error {
SeekImageEnd,
/// Unable to seek to Image header.
SeekImageHeader,
/// Unable to seek to DTB start.
SeekDtbStart,
/// Unable to seek to DTB end.
SeekDtbEnd,
}

/// A specialized `Result` type for the kernel loader.
Expand All @@ -113,6 +121,7 @@ impl error::Error for Error {
}
Error::CommandLineCopy => "Failed writing command line to guest memory",
Error::CommandLineOverflow => "Command line overflowed guest memory",
Error::DtbTooBig => "Device tree image too big",
Error::InvalidElfMagicNumber => "Invalid Elf magic number",
Error::InvalidProgramHeaderSize => "Invalid program header size",
Error::InvalidProgramHeaderOffset => "Invalid program header offset",
Expand All @@ -128,6 +137,7 @@ impl error::Error for Error {
Error::ReadProgramHeader => "Unable to read program header",
Error::ReadBzImageHeader => "Unable to read bzImage header",
Error::ReadImageHeader => "Unable to read Image header",
Error::ReadDtbImage => "Unable to read DTB image",
Error::ReadBzImageCompressedKernel => "Unable to read bzImage compressed kernel",
Error::SeekKernelStart => "Unable to seek to kernel start",
Error::SeekElfStart => "Unable to seek to elf start",
Expand All @@ -137,6 +147,8 @@ impl error::Error for Error {
Error::SeekBzImageCompressedKernel => "Unable to seek bzImage compressed kernel",
Error::SeekImageEnd => "Unable to seek Image end",
Error::SeekImageHeader => "Unable to seek image header",
Error::SeekDtbStart => "Unable to seek DTB start",
Error::SeekDtbEnd => "Unable to seek DTB end",
}
}
}
Expand Down Expand Up @@ -422,6 +434,36 @@ pub fn load_cmdline<M: GuestMemory>(
Ok(())
}

/// Writes the device tree to the given memory slice.
///
/// # Arguments
///
/// * `guest_mem` - A u8 slice that will be partially overwritten by the device tree blob.
/// * `guest_addr` - The address in `guest_mem` at which to load the device tree blob.
/// * `dtb_image` - The device tree blob.
#[cfg(target_arch = "aarch64")]
pub fn load_dtb<F, M: GuestMemory>(
guest_mem: &M,
guest_addr: GuestAddress,
dtb_image: &mut F,
) -> Result<()>
where
F: Read + Seek,
{
let dtb_size = dtb_image
.seek(SeekFrom::End(0))
.map_err(|_| Error::SeekDtbEnd)? as usize;
if dtb_size > 0x200000 {
return Err(Error::DtbTooBig);
}
dtb_image
.seek(SeekFrom::Start(0))
.map_err(|_| Error::SeekDtbStart)?;
guest_mem
.read_exact_from(guest_addr, dtb_image, dtb_size)
.map_err(|_| Error::ReadDtbImage)
}

#[cfg(feature = "image")]
#[cfg(target_arch = "aarch64")]
/// ARM64 Image (PE) format support
Expand Down

0 comments on commit 15a04eb

Please sign in to comment.