Skip to content

Commit 6afe33f

Browse files
committed
uefi: Add Service Binding Protocol abstraction
- Some UEFI protocols such as TCP4, TCP6, UDP4, UDP6, etc are managed by service binding protocol. - A new instance of such protocols is created and destroyed using the corresponding service binding protocol. - This PR adds abstractions to make using such protocols simpler using Rust Drop trait. - The reason to add these abstractions in a seperate PR from TCP4 Protocol is to make review easier. [EFI_SERVICE_BINDING_PROTCOL](https://uefi.org/specs/UEFI/2.11/11_Protocols_UEFI_Driver_Model.html#efi-service-binding-protocol) Signed-off-by: Ayush Singh <[email protected]>
1 parent bb2cc59 commit 6afe33f

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

library/std/src/sys/pal/uefi/helpers.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! - More information about protocols can be found [here](https://edk2-docs.gitbook.io/edk-ii-uefi-driver-writer-s-guide/3_foundation/36_protocols_and_handles)
1111
1212
use r_efi::efi::{self, Guid};
13-
use r_efi::protocols::{device_path, device_path_to_text, shell};
13+
use r_efi::protocols::{device_path, device_path_to_text, service_binding, shell};
1414

1515
use crate::ffi::{OsStr, OsString};
1616
use crate::io::{self, const_error};
@@ -500,3 +500,61 @@ pub(crate) fn get_device_path_from_map(map: &Path) -> io::Result<BorrowedDeviceP
500500

501501
Ok(BorrowedDevicePath::new(protocol))
502502
}
503+
504+
/// Helper for UEFI Protocols which are created and destroyed using
505+
/// [EFI_SERVICE_BINDING_PROTCOL](https://uefi.org/specs/UEFI/2.11/11_Protocols_UEFI_Driver_Model.html#efi-service-binding-protocol)
506+
#[allow(dead_code)]
507+
pub(crate) struct ServiceProtocol {
508+
service_guid: r_efi::efi::Guid,
509+
handle: NonNull<crate::ffi::c_void>,
510+
child_handle: NonNull<crate::ffi::c_void>,
511+
}
512+
513+
impl ServiceProtocol {
514+
#[allow(dead_code)]
515+
pub(crate) fn open(service_guid: r_efi::efi::Guid) -> io::Result<Self> {
516+
let handles = locate_handles(service_guid)?;
517+
518+
for handle in handles {
519+
if let Ok(protocol) = open_protocol::<service_binding::Protocol>(handle, service_guid) {
520+
let Ok(child_handle) = Self::create_child(protocol) else {
521+
continue;
522+
};
523+
524+
return Ok(Self { service_guid, handle, child_handle });
525+
}
526+
}
527+
528+
Err(io::const_error!(io::ErrorKind::NotFound, "No service binding protocol found"))
529+
}
530+
531+
#[allow(dead_code)]
532+
pub(crate) fn child_handle(&self) -> NonNull<crate::ffi::c_void> {
533+
self.child_handle
534+
}
535+
536+
fn create_child(
537+
sbp: NonNull<service_binding::Protocol>,
538+
) -> io::Result<NonNull<crate::ffi::c_void>> {
539+
let mut child_handle: r_efi::efi::Handle = crate::ptr::null_mut();
540+
let r = unsafe { ((*sbp.as_ptr()).create_child)(sbp.as_ptr(), &mut child_handle) };
541+
542+
if r.is_error() {
543+
Err(crate::io::Error::from_raw_os_error(r.as_usize()))
544+
} else {
545+
NonNull::new(child_handle)
546+
.ok_or(const_error!(io::ErrorKind::Other, "null child handle"))
547+
}
548+
}
549+
}
550+
551+
impl Drop for ServiceProtocol {
552+
fn drop(&mut self) {
553+
if let Ok(sbp) = open_protocol::<service_binding::Protocol>(self.handle, self.service_guid)
554+
{
555+
let _ = unsafe {
556+
((*sbp.as_ptr()).destroy_child)(sbp.as_ptr(), self.child_handle.as_ptr())
557+
};
558+
}
559+
}
560+
}

0 commit comments

Comments
 (0)