Skip to content

Commit eb6994e

Browse files
committed
uefi: remove code duplication
The existing code duplication is just temporary until the old API is deleted. Nevertheless, it is nicer to have this conversion close to the actual type.
1 parent 4dd71d5 commit eb6994e

File tree

2 files changed

+41
-47
lines changed

2 files changed

+41
-47
lines changed

uefi/src/boot.rs

+1-24
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use uefi_raw::table::boot::InterfaceType;
2323

2424
#[cfg(doc)]
2525
use crate::proto::device_path::LoadedImageDevicePath;
26-
use uefi::proto::BootPolicy;
2726
#[cfg(feature = "alloc")]
2827
use {alloc::vec::Vec, uefi::ResultExt};
2928

@@ -995,29 +994,7 @@ pub fn load_image(parent_image_handle: Handle, source: LoadImageSource) -> Resul
995994
let bt = boot_services_raw_panicking();
996995
let bt = unsafe { bt.as_ref() };
997996

998-
let boot_policy;
999-
let device_path;
1000-
let source_buffer;
1001-
let source_size;
1002-
match source {
1003-
LoadImageSource::FromBuffer { buffer, file_path } => {
1004-
// Boot policy is ignored when loading from source buffer.
1005-
boot_policy = BootPolicy::ExactMatch;
1006-
1007-
device_path = file_path.map(|p| p.as_ffi_ptr()).unwrap_or(ptr::null());
1008-
source_buffer = buffer.as_ptr();
1009-
source_size = buffer.len();
1010-
}
1011-
LoadImageSource::FromDevicePath {
1012-
device_path: file_path,
1013-
boot_policy: new_boot_policy,
1014-
} => {
1015-
boot_policy = new_boot_policy;
1016-
device_path = file_path.as_ffi_ptr();
1017-
source_buffer = ptr::null();
1018-
source_size = 0;
1019-
}
1020-
};
997+
let (boot_policy, device_path, source_buffer, source_size) = source.to_ffi_params();
1021998

1022999
let mut image_handle = ptr::null_mut();
10231000
unsafe {

uefi/src/table/boot.rs

+40-23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::Revision;
88
use crate::data_types::PhysicalAddress;
99
use crate::mem::memory_map::*;
1010
use crate::proto::device_path::DevicePath;
11+
use crate::proto::device_path::FfiDevicePath;
1112
use crate::proto::loaded_image::LoadedImage;
1213
use crate::proto::media::fs::SimpleFileSystem;
1314
use crate::proto::{BootPolicy, Protocol, ProtocolPointer};
@@ -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::ExactMatch;
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-
boot_policy: new_boot_policy,
860-
} => {
861-
boot_policy = new_boot_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 {
@@ -1422,6 +1401,44 @@ pub enum LoadImageSource<'a> {
14221401
},
14231402
}
14241403

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

0 commit comments

Comments
 (0)