Skip to content

Commit fd658c1

Browse files
committed
uefi: remove code duplication + fix import style
1. Remove code duplication by moving functionality to a dedicated function. 2. Typically, we first have public uses and then private ones.
1 parent 8541f50 commit fd658c1

File tree

2 files changed

+48
-54
lines changed

2 files changed

+48
-54
lines changed

uefi/src/boot.rs

+8-31
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22
//!
33
//! These functions will panic if called after exiting boot services.
44
5+
pub use crate::table::boot::{
6+
AllocateType, EventNotifyFn, LoadImageSource, OpenProtocolAttributes, OpenProtocolParams,
7+
SearchType, TimerTrigger,
8+
};
9+
pub use uefi_raw::table::boot::{EventType, MemoryType, Tpl};
10+
511
use crate::data_types::PhysicalAddress;
612
use crate::proto::device_path::DevicePath;
713
use crate::proto::{Protocol, ProtocolPointer};
814
use crate::util::opt_nonnull_to_ptr;
15+
use crate::{table, Char16, Error, Event, Guid, Handle, Result, Status, StatusExt};
916
use core::ffi::c_void;
1017
use core::ops::{Deref, DerefMut};
1118
use core::ptr::{self, NonNull};
1219
use core::sync::atomic::{AtomicPtr, Ordering};
1320
use core::{mem, slice};
14-
use uefi::{table, Char16, Error, Event, Guid, Handle, Result, Status, StatusExt};
1521
use uefi_raw::table::boot::InterfaceType;
16-
17-
use crate::proto::BootPolicy;
18-
pub use crate::table::boot::{
19-
AllocateType, EventNotifyFn, LoadImageSource, OpenProtocolAttributes, OpenProtocolParams,
20-
SearchType, TimerTrigger,
21-
};
22-
pub use uefi_raw::table::boot::{EventType, MemoryType, Tpl};
2322
#[cfg(doc)]
2423
use {
2524
crate::proto::device_path::LoadedImageDevicePath, crate::proto::loaded_image::LoadedImage,
@@ -674,29 +673,7 @@ pub fn load_image(parent_image_handle: Handle, source: LoadImageSource) -> Resul
674673
let bt = boot_services_raw_panicking();
675674
let bt = unsafe { bt.as_ref() };
676675

677-
let boot_policy;
678-
let device_path;
679-
let source_buffer;
680-
let source_size;
681-
match source {
682-
LoadImageSource::FromBuffer { buffer, file_path } => {
683-
// Boot policy is ignored when loading from source buffer.
684-
boot_policy = BootPolicy::FALSE;
685-
686-
device_path = file_path.map(|p| p.as_ffi_ptr()).unwrap_or(ptr::null());
687-
source_buffer = buffer.as_ptr();
688-
source_size = buffer.len();
689-
}
690-
LoadImageSource::FromDevicePath {
691-
device_path: file_path,
692-
bool_policy,
693-
} => {
694-
boot_policy = bool_policy;
695-
device_path = file_path.as_ffi_ptr();
696-
source_buffer = ptr::null();
697-
source_size = 0;
698-
}
699-
};
676+
let (boot_policy, device_path, source_buffer, source_size) = source.to_ffi_params();
700677

701678
let mut image_handle = ptr::null_mut();
702679
unsafe {

uefi/src/table/boot.rs

+40-23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use core::mem::{self, MaybeUninit};
2222
use core::ops::{Deref, DerefMut};
2323
use core::ptr::NonNull;
2424
use core::{ptr, slice};
25+
use uefi::proto::device_path::FfiDevicePath;
2526

2627
/// Size in bytes of a UEFI page.
2728
///
@@ -841,29 +842,7 @@ impl BootServices {
841842
parent_image_handle: Handle,
842843
source: LoadImageSource,
843844
) -> uefi::Result<Handle> {
844-
let boot_policy;
845-
let device_path;
846-
let source_buffer;
847-
let source_size;
848-
match source {
849-
LoadImageSource::FromBuffer { buffer, file_path } => {
850-
// Boot policy is ignored when loading from source buffer.
851-
boot_policy = BootPolicy::FALSE;
852-
853-
device_path = file_path.map(|p| p.as_ffi_ptr()).unwrap_or(ptr::null());
854-
source_buffer = buffer.as_ptr();
855-
source_size = buffer.len();
856-
}
857-
LoadImageSource::FromDevicePath {
858-
device_path: file_path,
859-
bool_policy,
860-
} => {
861-
boot_policy = bool_policy;
862-
device_path = file_path.as_ffi_ptr();
863-
source_buffer = ptr::null();
864-
source_size = 0;
865-
}
866-
};
845+
let (boot_policy, device_path, source_buffer, source_size) = source.to_ffi_params();
867846

868847
let mut image_handle = ptr::null_mut();
869848
unsafe {
@@ -1424,6 +1403,44 @@ pub enum LoadImageSource<'a> {
14241403
},
14251404
}
14261405

1406+
impl<'a> LoadImageSource<'a> {
1407+
/// Returns the raw FFI parameters for `load_image`.
1408+
#[must_use]
1409+
pub(crate) fn to_ffi_params(
1410+
&self,
1411+
) -> (
1412+
BootPolicy,
1413+
*const FfiDevicePath,
1414+
*const u8, /* buffer */
1415+
usize, /* buffer length */
1416+
) {
1417+
let boot_policy;
1418+
let device_path;
1419+
let source_buffer;
1420+
let source_size;
1421+
match self {
1422+
LoadImageSource::FromBuffer { buffer, file_path } => {
1423+
// Boot policy is ignored when loading from source buffer.
1424+
boot_policy = BootPolicy::FALSE;
1425+
1426+
device_path = file_path.map(|p| p.as_ffi_ptr()).unwrap_or(ptr::null());
1427+
source_buffer = buffer.as_ptr();
1428+
source_size = buffer.len();
1429+
}
1430+
LoadImageSource::FromDevicePath {
1431+
device_path: file_path,
1432+
bool_policy,
1433+
} => {
1434+
boot_policy = *bool_policy;
1435+
device_path = file_path.as_ffi_ptr();
1436+
source_buffer = ptr::null();
1437+
source_size = 0;
1438+
}
1439+
};
1440+
(boot_policy, device_path, source_buffer, source_size)
1441+
}
1442+
}
1443+
14271444
/// RAII guard for task priority level changes
14281445
///
14291446
/// Will automatically restore the former task priority level when dropped.

0 commit comments

Comments
 (0)