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 use crate::defs::{
16     char16_t, EfiGuid, EfiSimpleTextOutputProtocol, EFI_STATUS_NOT_FOUND, EFI_STATUS_UNSUPPORTED,
17 };
18 use crate::protocol::{Protocol, ProtocolInfo};
19 use crate::{efi_call, map_efi_err, EfiError, EfiResult};
20 use core::fmt::Write;
21 
22 /// EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
23 pub struct SimpleTextOutputProtocol;
24 
25 impl ProtocolInfo for SimpleTextOutputProtocol {
26     type InterfaceType = EfiSimpleTextOutputProtocol;
27 
28     const GUID: EfiGuid =
29         EfiGuid::new(0x387477c2, 0x69c7, 0x11d2, [0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b]);
30 }
31 
32 impl Protocol<'_, SimpleTextOutputProtocol> {
33     /// Wrapper of `EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString()`
output_string(&self, msg: *mut char16_t) -> EfiResult<()>34     pub fn output_string(&self, msg: *mut char16_t) -> EfiResult<()> {
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()?.output_string, self.interface, msg) }
40     }
41 }
42 
43 /// Implement formatted write for `EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL`, so that we can print by
44 /// writing to it. i.e.:
45 ///
46 /// ```
47 /// let protocol: Protocol<SimpleTextOutputProtocol> = ...;
48 /// write!(protocol, "Value = {}\n", 1234);
49 /// ```
50 impl Write for Protocol<'_, SimpleTextOutputProtocol> {
write_str(&mut self, s: &str) -> core::fmt::Result51     fn write_str(&mut self, s: &str) -> core::fmt::Result {
52         for ch in s.chars() {
53             // 2 is enough for encode_utf16(). Add an additional one as NULL.
54             let mut buffer = [0u16; 3];
55             let char16_msg = ch.encode_utf16(&mut buffer[..]);
56             self.output_string(char16_msg.as_mut_ptr()).map_err(|_| core::fmt::Error {})?;
57         }
58         Ok(())
59     }
60 }
61 
62 // A convenient convert to forward error when using write!() on
63 // Protocol<SimpleTextOutputProtocol>.
64 impl From<core::fmt::Error> for EfiError {
from(_: core::fmt::Error) -> EfiError65     fn from(_: core::fmt::Error) -> EfiError {
66         EFI_STATUS_UNSUPPORTED.into()
67     }
68 }
69