Skip to content

Commit b0dae77

Browse files
committed
uefi: remove code duplication
1 parent 47aaa44 commit b0dae77

File tree

2 files changed

+45
-53
lines changed

2 files changed

+45
-53
lines changed

uefi/src/boot.rs

+6-30
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
use crate::data_types::PhysicalAddress;
66
use crate::proto::device_path::DevicePath;
77
use crate::proto::{Protocol, ProtocolPointer};
8+
pub use crate::table::boot::{
9+
AllocateType, EventNotifyFn, LoadImageSource, OpenProtocolAttributes, OpenProtocolParams,
10+
SearchType, TimerTrigger,
11+
};
812
use crate::util::opt_nonnull_to_ptr;
13+
use crate::{table, Char16, Event, Guid, Handle, Result, Status, StatusExt};
914
use core::ffi::c_void;
1015
use core::ops::{Deref, DerefMut};
1116
use core::ptr::{self, NonNull};
1217
use core::sync::atomic::{AtomicPtr, Ordering};
1318
use core::{mem, slice};
14-
use uefi::{table, Char16, Event, Guid, Handle, Result, Status, StatusExt};
1519
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-
};
2220
pub use uefi_raw::table::boot::{EventType, MemoryType, Tpl};
2321
#[cfg(doc)]
2422
use {
@@ -642,29 +640,7 @@ pub fn load_image(parent_image_handle: Handle, source: LoadImageSource) -> Resul
642640
let bt = boot_services_raw_panicking();
643641
let bt = unsafe { bt.as_ref() };
644642

645-
let boot_policy;
646-
let device_path;
647-
let source_buffer;
648-
let source_size;
649-
match source {
650-
LoadImageSource::FromBuffer { buffer, file_path } => {
651-
// Boot policy is ignored when loading from source buffer.
652-
boot_policy = BootPolicy::FALSE;
653-
654-
device_path = file_path.map(|p| p.as_ffi_ptr()).unwrap_or(ptr::null());
655-
source_buffer = buffer.as_ptr();
656-
source_size = buffer.len();
657-
}
658-
LoadImageSource::FromDevicePath {
659-
device_path: file_path,
660-
bool_policy,
661-
} => {
662-
boot_policy = bool_policy;
663-
device_path = file_path.as_ffi_ptr();
664-
source_buffer = ptr::null();
665-
source_size = 0;
666-
}
667-
};
643+
let (boot_policy, device_path, source_buffer, source_size) = source.to_ffi_params();
668644

669645
let mut image_handle = ptr::null_mut();
670646
unsafe {

uefi/src/table/boot.rs

+39-23
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use core::mem::{self, MaybeUninit};
2323
use core::ops::{Deref, DerefMut};
2424
use core::ptr::NonNull;
2525
use core::{ptr, slice};
26+
use uefi::proto::device_path::FfiDevicePath;
2627

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

869848
let mut image_handle = ptr::null_mut();
870849
unsafe {
@@ -1422,6 +1401,43 @@ pub enum LoadImageSource<'a> {
14221401
},
14231402
}
14241403

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

0 commit comments

Comments
 (0)