diff --git a/kernel_loader/src/lib.rs b/kernel_loader/src/lib.rs index f9bd3bbb56..9d03df0ebe 100644 --- a/kernel_loader/src/lib.rs +++ b/kernel_loader/src/lib.rs @@ -7,6 +7,8 @@ extern crate sys_util; use std::mem; use std::ffi::CStr; use std::io::{Read, Seek, SeekFrom}; +use std::error::{self, Error as KernelLoaderError}; +use std::fmt::{self, Display}; use sys_util::{GuestAddress, GuestMemory}; @@ -34,6 +36,45 @@ pub enum Error { } pub type Result = std::result::Result; +impl error::Error for Error { + fn description(&self) -> &str { + match self { + &Error::BigEndianElfOnLittle => + "Trying to load big-endian binary on little-endian machine", + &Error::CommandLineCopy => + "Failed writing command line to guest memory", + &Error::CommandLineOverflow => + "Command line overflowed guest memory", + &Error::InvalidElfMagicNumber => + "Invalid Elf magic number", + &Error::InvalidProgramHeaderSize => + "Invalid program header size", + &Error::InvalidProgramHeaderOffset => + "Invalid program header offset", + &Error::InvalidProgramHeaderAddress => + "Invalid Program Header Address", + &Error::ReadElfHeader => + "Unable to read elf header", + &Error::ReadKernelImage => + "Unable to read kernel image", + &Error::ReadProgramHeader => + "Unable to read program header", + &Error::SeekKernelStart => + "Unable to seek to kernel start", + &Error::SeekElfStart => + "Unable to seek to elf start", + &Error::SeekProgramHeader => + "Unable to seek to program header", + } + } +} + +impl Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Kernel Loader Error: {}", Error::description(self)) + } +} + /// Loads a kernel from a vmlinux elf image to a slice /// /// # Arguments