xref: /aosp_15_r20/bootable/libbootloader/gbl/libefi/src/protocol/simple_text_input.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 `EFI_SIMPLE_TEXT_INPUT_PROTOCOL`.
16 
17 use crate::efi_call;
18 use crate::protocol::{Protocol, ProtocolInfo};
19 use efi_types::{EfiGuid, EfiInputKey, EfiSimpleTextInputProtocol};
20 use liberror::{Error, Result};
21 
22 /// EFI_SIMPLE_TEXT_INPUT_PROTOCOL
23 pub struct SimpleTextInputProtocol;
24 
25 impl ProtocolInfo for SimpleTextInputProtocol {
26     type InterfaceType = EfiSimpleTextInputProtocol;
27 
28     const GUID: EfiGuid =
29         EfiGuid::new(0x387477c1, 0x69c7, 0x11d2, [0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b]);
30 }
31 
32 impl Protocol<'_, SimpleTextInputProtocol> {
33     /// Wrapper of `EFI_SIMPLE_TEXT_INPUT_PROTOCOL.reset()`
reset(&self, extendend_verification: bool) -> Result<()>34     pub fn reset(&self, extendend_verification: bool) -> Result<()> {
35         // SAFETY:
36         // `self.interface()?` guarantees `self.interface` is non-null and points to a valid object
37         // established by `Protocol::new()`.
38         // `self.interface` is input parameter and will not be retained. It outlives the call.
39         unsafe { efi_call!(self.interface()?.reset, self.interface, extendend_verification) }
40     }
41 
42     /// Wrapper of `EFI_SIMPLE_TEXT_INPUT_PROTOCOL.read_key_stroke()`
43     ///
44     /// Returns `Ok(Some(EfiInputKey))` if there is a key stroke, Ok(None) if no key stroke is
45     /// pressed.
read_key_stroke(&self) -> Result<Option<EfiInputKey>>46     pub fn read_key_stroke(&self) -> Result<Option<EfiInputKey>> {
47         let mut key: EfiInputKey = Default::default();
48         // SAFETY:
49         // `self.interface()?` guarantees `self.interface` is non-null and points to a valid object
50         // established by `Protocol::new()`.
51         // `self.interface` is input parameter and will not be retained. It outlives the call.
52         // `key` is an output argument. It outlives the call and will not be taken.
53         match unsafe { efi_call!(self.interface()?.read_key_stroke, self.interface, &mut key) } {
54             Ok(()) => Ok(Some(key)),
55             Err(Error::NotReady) => Ok(None),
56             Err(e) => Err(e),
57         }
58     }
59 }
60