xref: /aosp_15_r20/bootable/libbootloader/gbl/libefi/src/protocol/riscv.rs (revision 5225e6b173e52d2efc6bcf950c27374fd72adabc)
1 // Copyright 2024, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Rust wrapper for `RISCV_EFI_BOOT_PROTOCOL`.
16 
17 use crate::efi_call;
18 use crate::protocol::{Protocol, ProtocolInfo};
19 use efi_types::{EfiGuid, EfiRiscvBootProtocol};
20 use liberror::Result;
21 
22 /// RISCV_EFI_BOOT_PROTOCOL
23 pub struct RiscvBootProtocol;
24 
25 impl ProtocolInfo for RiscvBootProtocol {
26     type InterfaceType = EfiRiscvBootProtocol;
27 
28     const GUID: EfiGuid =
29         EfiGuid::new(0xccd15fec, 0x6f73, 0x4eec, [0x83, 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf]);
30 }
31 
32 impl<'a> Protocol<'a, RiscvBootProtocol> {
33     /// Wraps `RISCV_EFI_BOOT_PROTOCOL.GetBootHartId()`.
get_boot_hartid(&self) -> Result<usize>34     pub fn get_boot_hartid(&self) -> Result<usize> {
35         let mut boot_hart_id: usize = 0;
36         // SAFETY:
37         // `self.interface()?` guarantees `self.interface` is non-null and points to a valid object
38         // established by `Protocol::new()`.
39         // `self.interface` is input parameter and will not be retained. It outlives the call.
40         // `&mut boot_hart_id` is output parameter and will not be retained. It outlives the call.
41         unsafe {
42             efi_call!(self.interface()?.get_boot_hartid, self.interface, &mut boot_hart_id)?;
43         }
44         Ok(boot_hart_id)
45     }
46 
47     /// Wraps `RISCV_EFI_BOOT_PROTOCOL.Revision`.
revision(&self) -> Result<u64>48     pub fn revision(&self) -> Result<u64> {
49         Ok(self.interface()?.revision)
50     }
51 }
52