introduce the use of anyhow to no longer use panic

This commit is contained in:
2025-10-11 14:35:29 -07:00
parent 449eb85ab8
commit fc239ea54d
14 changed files with 121 additions and 84 deletions

View File

@@ -1,5 +1,6 @@
use crate::context::Context;
use crate::context::SproutContext;
use crate::utils;
use anyhow::{Context, Result, bail};
use log::info;
use serde::{Deserialize, Serialize};
use std::rc::Rc;
@@ -14,19 +15,19 @@ pub struct ChainloadConfiguration {
pub options: Vec<String>,
}
pub fn chainload(context: Rc<Context>, configuration: &ChainloadConfiguration) {
pub fn chainload(context: Rc<SproutContext>, configuration: &ChainloadConfiguration) -> Result<()> {
let sprout_image = uefi::boot::image_handle();
let image_device_path_protocol =
uefi::boot::open_protocol_exclusive::<LoadedImageDevicePath>(sprout_image)
.expect("unable to open loaded image device path protocol");
.context("unable to open loaded image device path protocol")?;
let mut full_path = utils::device_path_root(&image_device_path_protocol);
let mut full_path = utils::device_path_root(&image_device_path_protocol)?;
full_path.push_str(&context.stamp(&configuration.path));
info!("path={}", full_path);
let device_path = utils::text_to_device_path(&full_path);
let device_path = utils::text_to_device_path(&full_path)?;
let image = uefi::boot::load_image(
sprout_image,
@@ -35,10 +36,10 @@ pub fn chainload(context: Rc<Context>, configuration: &ChainloadConfiguration) {
boot_policy: uefi::proto::BootPolicy::ExactMatch,
},
)
.expect("failed to load image");
.context("failed to load image")?;
let mut loaded_image_protocol = uefi::boot::open_protocol_exclusive::<LoadedImage>(image)
.expect("unable to open loaded image protocol");
.context("unable to open loaded image protocol")?;
let options = configuration
.options
@@ -49,12 +50,12 @@ pub fn chainload(context: Rc<Context>, configuration: &ChainloadConfiguration) {
if !options.is_empty() {
let options = Box::new(
CString16::try_from(&options[..])
.expect("unable to convert chainloader options to CString16"),
.context("unable to convert chainloader options to CString16")?,
);
info!("options={}", options);
if options.num_bytes() > u32::MAX as usize {
panic!("chainloader options too large");
bail!("chainloader options too large");
}
// SAFETY: options size is checked to validate it is safe to pass.
@@ -68,5 +69,6 @@ pub fn chainload(context: Rc<Context>, configuration: &ChainloadConfiguration) {
let (base, size) = loaded_image_protocol.info();
info!("loaded image base={:#x} size={:#x}", base.addr(), size);
uefi::boot::start_image(image).expect("failed to start image");
uefi::boot::start_image(image).context("failed to start image")?;
Ok(())
}

View File

@@ -1,4 +1,5 @@
use crate::context::Context;
use crate::context::SproutContext;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::rc::Rc;
@@ -8,6 +9,7 @@ pub struct PrintConfiguration {
pub text: String,
}
pub fn print(context: Rc<Context>, configuration: &PrintConfiguration) {
pub fn print(context: Rc<SproutContext>, configuration: &PrintConfiguration) -> Result<()> {
println!("{}", context.stamp(&configuration.text));
Ok(())
}

View File

@@ -1,6 +1,7 @@
use crate::context::Context;
use crate::context::SproutContext;
use crate::utils::framebuffer::Framebuffer;
use crate::utils::read_file_contents;
use anyhow::{Context, Result};
use image::imageops::{FilterType, resize};
use image::math::Rect;
use image::{DynamicImage, ImageBuffer, ImageFormat, ImageReader, Rgba};
@@ -22,11 +23,11 @@ pub fn default_splash_time() -> u32 {
5
}
fn setup_graphics() -> ScopedProtocol<GraphicsOutput> {
fn setup_graphics() -> Result<ScopedProtocol<GraphicsOutput>> {
let gop_handle = uefi::boot::get_handle_for_protocol::<GraphicsOutput>()
.expect("failed to get graphics output");
.context("failed to get graphics output")?;
uefi::boot::open_protocol_exclusive::<GraphicsOutput>(gop_handle)
.expect("failed to open graphics output")
.context("failed to open graphics output")
}
fn fit_to_frame(image: &DynamicImage, frame: Rect) -> Rect {
@@ -67,8 +68,8 @@ fn resize_to_fit(image: &DynamicImage, frame: Rect) -> ImageBuffer<Rgba<u8>, Vec
resize(&image, frame.width, frame.height, FilterType::Lanczos3)
}
fn draw(image: DynamicImage) {
let mut gop = setup_graphics();
fn draw(image: DynamicImage) -> Result<()> {
let mut gop = setup_graphics()?;
let (width, height) = gop.current_mode_info().resolution();
let display_frame = Rect {
x: 0,
@@ -89,15 +90,17 @@ fn draw(image: DynamicImage) {
fb.blue = pixel[2];
}
framebuffer.blit(&mut gop);
framebuffer.blit(&mut gop)?;
Ok(())
}
pub fn splash(context: Rc<Context>, configuration: &SplashConfiguration) {
pub fn splash(context: Rc<SproutContext>, configuration: &SplashConfiguration) -> Result<()> {
let image = context.stamp(&configuration.image);
let image = read_file_contents(&image);
let image = read_file_contents(&image)?;
let image = ImageReader::with_format(Cursor::new(image), ImageFormat::Png)
.decode()
.expect("failed to decode splash image");
draw(image);
.context("failed to decode splash image")?;
draw(image)?;
std::thread::sleep(Duration::from_secs(configuration.time as u64));
Ok(())
}