Skip to content

Commit 3b00a89

Browse files
uefi: Move various types to the uefi::boot module
The types are publicly re-exported from `uefi::table::boot`, so there should be no breaking change to the public API.
1 parent a3e5f33 commit 3b00a89

File tree

2 files changed

+211
-212
lines changed

2 files changed

+211
-212
lines changed

uefi/src/boot.rs

Lines changed: 206 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
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-
ProtocolSearchKey, SearchType, TimerTrigger,
8-
};
95
pub use uefi_raw::table::boot::{EventType, MemoryAttribute, MemoryDescriptor, MemoryType, Tpl};
106

117
use crate::data_types::PhysicalAddress;
128
use crate::mem::memory_map::{MemoryMapBackingMemory, MemoryMapKey, MemoryMapMeta, MemoryMapOwned};
139
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
14-
use crate::proto::device_path::DevicePath;
1510
#[cfg(doc)]
1611
use crate::proto::device_path::LoadedImageDevicePath;
12+
use crate::proto::device_path::{DevicePath, FfiDevicePath};
1713
use crate::proto::loaded_image::LoadedImage;
1814
use crate::proto::media::fs::SimpleFileSystem;
19-
use crate::proto::{Protocol, ProtocolPointer};
15+
use crate::proto::{BootPolicy, Protocol, ProtocolPointer};
2016
use crate::runtime::{self, ResetType};
2117
use crate::table::Revision;
2218
use crate::util::opt_nonnull_to_ptr;
@@ -1438,3 +1434,207 @@ impl Drop for TplGuard {
14381434
}
14391435
}
14401436
}
1437+
1438+
// OpenProtocolAttributes is safe to model as a regular enum because it
1439+
// is only used as an input. The attributes are bitflags, but all valid
1440+
// combinations are listed in the spec and only ByDriver and Exclusive
1441+
// can actually be combined.
1442+
//
1443+
// Some values intentionally excluded:
1444+
//
1445+
// ByHandleProtocol (0x01) excluded because it is only intended to be
1446+
// used in an implementation of `HandleProtocol`.
1447+
//
1448+
// TestProtocol (0x04) excluded because it doesn't actually open the
1449+
// protocol, just tests if it's present on the handle. Since that
1450+
// changes the interface significantly, that's exposed as a separate
1451+
// method: `BootServices::test_protocol`.
1452+
1453+
/// Attributes for [`BootServices::open_protocol`].
1454+
#[repr(u32)]
1455+
#[derive(Debug)]
1456+
pub enum OpenProtocolAttributes {
1457+
/// Used by drivers to get a protocol interface for a handle. The
1458+
/// driver will not be informed if the interface is uninstalled or
1459+
/// reinstalled.
1460+
GetProtocol = 0x02,
1461+
1462+
/// Used by bus drivers to show that a protocol is being used by one
1463+
/// of the child controllers of the bus.
1464+
ByChildController = 0x08,
1465+
1466+
/// Used by a driver to gain access to a protocol interface. When
1467+
/// this mode is used, the driver's `Stop` function will be called
1468+
/// if the protocol interface is reinstalled or uninstalled. Once a
1469+
/// protocol interface is opened with this attribute, no other
1470+
/// drivers will be allowed to open the same protocol interface with
1471+
/// the `ByDriver` attribute.
1472+
ByDriver = 0x10,
1473+
1474+
/// Used by a driver to gain exclusive access to a protocol
1475+
/// interface. If any other drivers have the protocol interface
1476+
/// opened with an attribute of `ByDriver`, then an attempt will be
1477+
/// made to remove them with `DisconnectController`.
1478+
ByDriverExclusive = 0x30,
1479+
1480+
/// Used by applications to gain exclusive access to a protocol
1481+
/// interface. If any drivers have the protocol opened with an
1482+
/// attribute of `ByDriver`, then an attempt will be made to remove
1483+
/// them by calling the driver's `Stop` function.
1484+
Exclusive = 0x20,
1485+
}
1486+
1487+
/// Parameters passed to [`BootServices::open_protocol`].
1488+
#[derive(Debug)]
1489+
pub struct OpenProtocolParams {
1490+
/// The handle for the protocol to open.
1491+
pub handle: Handle,
1492+
1493+
/// The handle of the calling agent. For drivers, this is the handle
1494+
/// containing the `EFI_DRIVER_BINDING_PROTOCOL` instance. For
1495+
/// applications, this is the image handle.
1496+
pub agent: Handle,
1497+
1498+
/// For drivers, this is the controller handle that requires the
1499+
/// protocol interface. For applications this should be set to
1500+
/// `None`.
1501+
pub controller: Option<Handle>,
1502+
}
1503+
1504+
/// Used as a parameter of [`BootServices::load_image`] to provide the
1505+
/// image source.
1506+
#[derive(Debug)]
1507+
pub enum LoadImageSource<'a> {
1508+
/// Load an image from a buffer. The data will be copied from the
1509+
/// buffer, so the input reference doesn't need to remain valid
1510+
/// after the image is loaded.
1511+
FromBuffer {
1512+
/// Raw image data.
1513+
buffer: &'a [u8],
1514+
1515+
/// If set, this path will be added as the file path of the
1516+
/// loaded image. This is not required to load the image, but
1517+
/// may be used by the image itself to load other resources
1518+
/// relative to the image's path.
1519+
file_path: Option<&'a DevicePath>,
1520+
},
1521+
1522+
/// Load an image via the [`SimpleFileSystem`] protocol. If there is
1523+
/// no instance of that protocol associated with the path then the
1524+
/// behavior depends on [`BootPolicy`]. If [`BootPolicy::BootSelection`],
1525+
/// attempt to load via the [`LoadFile`] protocol. If
1526+
/// [`BootPolicy::ExactMatch`], attempt to load via the [`LoadFile2`]
1527+
/// protocol, then fall back to [`LoadFile`].
1528+
///
1529+
/// [`LoadFile`]: crate::proto::media::load_file::LoadFile
1530+
/// [`LoadFile2`]: crate::proto::media::load_file::LoadFile2
1531+
FromDevicePath {
1532+
/// The full device path from which to load the image.
1533+
///
1534+
/// The provided path should be a full device path and not just the
1535+
/// file path portion of it. So for example, it must be (the binary
1536+
/// representation)
1537+
/// `PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x0,0xFFFF,0x0)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/\\EFI\\BOOT\\BOOTX64.EFI`
1538+
/// and not just `\\EFI\\BOOT\\BOOTX64.EFI`.
1539+
device_path: &'a DevicePath,
1540+
1541+
/// The [`BootPolicy`] to use.
1542+
boot_policy: BootPolicy,
1543+
},
1544+
}
1545+
1546+
impl<'a> LoadImageSource<'a> {
1547+
/// Returns the raw FFI parameters for `load_image`.
1548+
#[must_use]
1549+
pub(crate) fn to_ffi_params(
1550+
&self,
1551+
) -> (
1552+
BootPolicy,
1553+
*const FfiDevicePath,
1554+
*const u8, /* buffer */
1555+
usize, /* buffer length */
1556+
) {
1557+
let boot_policy;
1558+
let device_path;
1559+
let source_buffer;
1560+
let source_size;
1561+
match self {
1562+
LoadImageSource::FromBuffer { buffer, file_path } => {
1563+
// Boot policy is ignored when loading from source buffer.
1564+
boot_policy = BootPolicy::default();
1565+
1566+
device_path = file_path.map(|p| p.as_ffi_ptr()).unwrap_or(ptr::null());
1567+
source_buffer = buffer.as_ptr();
1568+
source_size = buffer.len();
1569+
}
1570+
LoadImageSource::FromDevicePath {
1571+
device_path: d_path,
1572+
boot_policy: b_policy,
1573+
} => {
1574+
boot_policy = *b_policy;
1575+
device_path = d_path.as_ffi_ptr();
1576+
source_buffer = ptr::null();
1577+
source_size = 0;
1578+
}
1579+
};
1580+
(boot_policy, device_path, source_buffer, source_size)
1581+
}
1582+
}
1583+
1584+
/// Type of allocation to perform.
1585+
#[derive(Debug, Copy, Clone)]
1586+
pub enum AllocateType {
1587+
/// Allocate any possible pages.
1588+
AnyPages,
1589+
/// Allocate pages at any address below the given address.
1590+
MaxAddress(PhysicalAddress),
1591+
/// Allocate pages at the specified address.
1592+
Address(PhysicalAddress),
1593+
}
1594+
1595+
/// The type of handle search to perform.
1596+
#[derive(Debug, Copy, Clone)]
1597+
pub enum SearchType<'guid> {
1598+
/// Return all handles present on the system.
1599+
AllHandles,
1600+
/// Returns all handles supporting a certain protocol, specified by its GUID.
1601+
///
1602+
/// If the protocol implements the `Protocol` interface,
1603+
/// you can use the `from_proto` function to construct a new `SearchType`.
1604+
ByProtocol(&'guid Guid),
1605+
/// Return all handles that implement a protocol when an interface for that protocol
1606+
/// is (re)installed.
1607+
ByRegisterNotify(ProtocolSearchKey),
1608+
}
1609+
1610+
impl<'guid> SearchType<'guid> {
1611+
/// Constructs a new search type for a specified protocol.
1612+
#[must_use]
1613+
pub const fn from_proto<P: ProtocolPointer + ?Sized>() -> Self {
1614+
SearchType::ByProtocol(&P::GUID)
1615+
}
1616+
}
1617+
1618+
/// Event notification callback type.
1619+
pub type EventNotifyFn = unsafe extern "efiapi" fn(event: Event, context: Option<NonNull<c_void>>);
1620+
1621+
/// Timer events manipulation.
1622+
#[derive(Debug)]
1623+
pub enum TimerTrigger {
1624+
/// Cancel event's timer
1625+
Cancel,
1626+
/// The event is to be signaled periodically.
1627+
/// Parameter is the period in 100ns units.
1628+
/// Delay of 0 will be signalled on every timer tick.
1629+
Periodic(u64),
1630+
/// The event is to be signaled once in 100ns units.
1631+
/// Parameter is the delay in 100ns units.
1632+
/// Delay of 0 will be signalled on next timer tick.
1633+
Relative(u64),
1634+
}
1635+
1636+
/// Opaque pointer returned by [`BootServices::register_protocol_notify`] to be used
1637+
/// with [`BootServices::locate_handle`] via [`SearchType::ByRegisterNotify`].
1638+
#[derive(Debug, Clone, Copy)]
1639+
#[repr(transparent)]
1640+
pub struct ProtocolSearchKey(pub(crate) NonNull<c_void>);

0 commit comments

Comments
 (0)