|
| 1 | +use alloc::boxed::Box; |
| 2 | +use alloc::string::{String, ToString}; |
| 3 | +use alloc::vec::Vec; |
| 4 | +use core::ffi::c_void; |
| 5 | +use core::pin::Pin; |
| 6 | +use core::ptr; |
| 7 | +use core::ptr::addr_of; |
| 8 | +use uefi::prelude::BootServices; |
| 9 | +use uefi::proto::device_path::build::DevicePathBuilder; |
| 10 | +use uefi::proto::media::load_file::{LoadFile, LoadFile2}; |
| 11 | +use uefi::{Guid, Handle}; |
| 12 | +use uefi_raw::protocol::device_path::DevicePathProtocol; |
| 13 | +use uefi_raw::protocol::media::{LoadFile2Protocol, LoadFileProtocol}; |
| 14 | +use uefi_raw::Status; |
| 15 | + |
| 16 | +unsafe extern "efiapi" fn raw_load_file( |
| 17 | + this: *mut LoadFile2Protocol, |
| 18 | + _file_path: *const DevicePathProtocol, |
| 19 | + _boot_policy: bool, |
| 20 | + buffer_size: *mut usize, |
| 21 | + buffer: *mut c_void, |
| 22 | +) -> Status { |
| 23 | + log::debug!("hello"); |
| 24 | + let this = this.cast::<CustomLoadFile2Protocol>().as_ref().unwrap(); |
| 25 | + this.load_file(buffer_size, buffer.cast()) |
| 26 | +} |
| 27 | + |
| 28 | +#[repr(C)] |
| 29 | +struct CustomLoadFile2Protocol { |
| 30 | + inner: LoadFile2Protocol, |
| 31 | + file_data: Vec<u8>, |
| 32 | +} |
| 33 | + |
| 34 | +impl CustomLoadFile2Protocol { |
| 35 | + fn new(file_data: Vec<u8>) -> Pin<Box<Self>> { |
| 36 | + let inner = Self { |
| 37 | + inner: LoadFile2Protocol { |
| 38 | + load_file: raw_load_file, |
| 39 | + }, |
| 40 | + file_data, |
| 41 | + }; |
| 42 | + Box::pin(inner) |
| 43 | + } |
| 44 | + |
| 45 | + fn load_file(&self, buf_len: *mut usize, buf: *mut c_void) -> Status { |
| 46 | + log::debug!("hello"); |
| 47 | + if buf.is_null() || unsafe { *buf_len } < self.file_data.len() { |
| 48 | + log::debug!("hello"); |
| 49 | + unsafe { *buf_len = self.file_data.len() }; |
| 50 | + Status::BUFFER_TOO_SMALL |
| 51 | + } else { |
| 52 | + unsafe { |
| 53 | + ptr::copy_nonoverlapping(self.file_data.as_ptr(), buf.cast(), self.file_data.len()); |
| 54 | + } |
| 55 | + Status::SUCCESS |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +unsafe fn install_protocol( |
| 61 | + bt: &BootServices, |
| 62 | + handle: Handle, |
| 63 | + guid: Guid, |
| 64 | + protocol: &mut CustomLoadFile2Protocol, |
| 65 | +) { |
| 66 | + bt.install_protocol_interface(Some(handle), &guid, addr_of!(*protocol).cast()) |
| 67 | + .unwrap(); |
| 68 | +} |
| 69 | + |
| 70 | +unsafe fn uninstall_protocol( |
| 71 | + bt: &BootServices, |
| 72 | + handle: Handle, |
| 73 | + guid: Guid, |
| 74 | + protocol: &mut CustomLoadFile2Protocol, |
| 75 | +) { |
| 76 | + bt.uninstall_protocol_interface(handle, &guid, addr_of!(*protocol).cast()) |
| 77 | + .unwrap(); |
| 78 | +} |
| 79 | + |
| 80 | +/// This tests the LoadFile and LoadFile2 protocols. As this protocol is not |
| 81 | +/// implemented in OVMF for the default handle, we implement it manually using |
| 82 | +/// `install_protocol_interface`. Then, we load a file from our custom installed |
| 83 | +/// protocol leveraging our protocol abstraction. |
| 84 | +/// |
| 85 | +/// the way we are implementing the LoadFile(2) protocol is roughly what certain |
| 86 | +/// Linux loaders do so that Linux can find its initrd [0, 1]. |
| 87 | +/// |
| 88 | +/// [0] https://github.com/u-boot/u-boot/commit/ec80b4735a593961fe701cc3a5d717d4739b0fd0#diff-1f940face4d1cf74f9d2324952759404d01ee0a81612b68afdcba6b49803bdbbR171 |
| 89 | +/// [1] https://github.com/torvalds/linux/blob/ee9a43b7cfe2d8a3520335fea7d8ce71b8cabd9d/drivers/firmware/efi/libstub/efi-stub-helper.c#L550 |
| 90 | +pub fn test(bt: &BootServices) { |
| 91 | + let image = bt.image_handle(); |
| 92 | + |
| 93 | + let load_data_msg = "Hello World"; |
| 94 | + let load_data = load_data_msg.to_string().into_bytes(); |
| 95 | + let mut proto_load_file = CustomLoadFile2Protocol::new(load_data); |
| 96 | + // Get the ptr to the inner value, not the wrapping smart pointer type. |
| 97 | + let proto_load_file_ptr = proto_load_file.as_mut().get_mut(); |
| 98 | + |
| 99 | + // Install our custom protocol implementation as LoadFile and LoadFile2 |
| 100 | + // protocol. |
| 101 | + unsafe { |
| 102 | + install_protocol(bt, image, LoadFileProtocol::GUID, proto_load_file_ptr); |
| 103 | + install_protocol(bt, image, LoadFile2Protocol::GUID, proto_load_file_ptr); |
| 104 | + } |
| 105 | + |
| 106 | + let mut dvp_vec = Vec::new(); |
| 107 | + let dvp = DevicePathBuilder::with_vec(&mut dvp_vec); |
| 108 | + let dvp = dvp.finalize().unwrap(); |
| 109 | + |
| 110 | + let mut opened_load_file_protocol = bt.open_protocol_exclusive::<LoadFile>(image).unwrap(); |
| 111 | + let loadfile_file = opened_load_file_protocol.load_file(dvp, true).unwrap(); |
| 112 | + let loadfile_file_string = String::from_utf8(loadfile_file.to_vec()).unwrap(); |
| 113 | + |
| 114 | + let mut opened_load_file2_protocol = bt.open_protocol_exclusive::<LoadFile2>(image).unwrap(); |
| 115 | + let loadfile2_file = opened_load_file2_protocol.load_file(dvp).unwrap(); |
| 116 | + let loadfile2_file_string = String::from_utf8(loadfile2_file.to_vec()).unwrap(); |
| 117 | + |
| 118 | + assert_eq!(load_data_msg, &loadfile_file_string); |
| 119 | + assert_eq!(load_data_msg, &loadfile2_file_string); |
| 120 | + |
| 121 | + // Cleanup: Uninstall protocols again. |
| 122 | + drop(opened_load_file_protocol); |
| 123 | + drop(opened_load_file2_protocol); |
| 124 | + unsafe { |
| 125 | + uninstall_protocol(bt, image, LoadFileProtocol::GUID, proto_load_file_ptr); |
| 126 | + uninstall_protocol(bt, image, LoadFile2Protocol::GUID, proto_load_file_ptr); |
| 127 | + } |
| 128 | +} |
0 commit comments