kernel_loader: implement error trait

This implements the error trait so we can get more useful error
information back on failures.

BUG=chromium:797868
TEST=./build_test passes on all architectures
TEST=crosvm runs on caroline

Change-Id: I888cbe6fca8bc3885eeb4ff747f7b9dc51e0b587
Reviewed-on: https://chromium-review.googlesource.com/938311
Commit-Ready: Sonny Rao <sonnyrao@chromium.org>
Tested-by: Sonny Rao <sonnyrao@chromium.org>
Reviewed-by: Sonny Rao <sonnyrao@chromium.org>
This commit is contained in:
Sonny Rao 2018-02-22 17:35:01 -08:00 committed by chrome-bot
parent 1aa03e00a0
commit 765deaafb4

View file

@ -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<T> = std::result::Result<T, Error>;
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