Skip to content

Commit e347d8e

Browse files
committed
efi: console: Move console (stdin/stdout) code to new module
Do this to reduce the amount of the code in the main efi source file. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
1 parent b68cf29 commit e347d8e

File tree

2 files changed

+172
-150
lines changed

2 files changed

+172
-150
lines changed

src/efi/console.rs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Copyright © 2019 Intel Corporation
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 r_efi::efi::{Boolean, Char16, Event, Handle, Status};
16+
use r_efi::protocols::simple_text_input::InputKey;
17+
use r_efi::protocols::simple_text_input::Protocol as SimpleTextInputProtocol;
18+
use r_efi::protocols::simple_text_output::Mode as SimpleTextOutputMode;
19+
use r_efi::protocols::simple_text_output::Protocol as SimpleTextOutputProtocol;
20+
21+
#[cfg(not(test))]
22+
use super::{HandleType, HandleWrapper};
23+
24+
#[cfg(not(test))]
25+
pub const STDIN_HANDLE: Handle = &HandleWrapper {
26+
handle_type: HandleType::None,
27+
} as *const _ as Handle;
28+
#[cfg(not(test))]
29+
pub const STDOUT_HANDLE: Handle = &HandleWrapper {
30+
handle_type: HandleType::None,
31+
} as *const _ as Handle;
32+
#[cfg(not(test))]
33+
pub const STDERR_HANDLE: Handle = &HandleWrapper {
34+
handle_type: HandleType::None,
35+
} as *const _ as Handle;
36+
37+
#[cfg(not(test))]
38+
pub extern "win64" fn stdin_reset(_: *mut SimpleTextInputProtocol, _: Boolean) -> Status {
39+
crate::log!("EFI_STUB: stdin_reset\n");
40+
Status::UNSUPPORTED
41+
}
42+
43+
#[cfg(not(test))]
44+
pub extern "win64" fn stdin_read_key_stroke(
45+
_: *mut SimpleTextInputProtocol,
46+
_: *mut InputKey,
47+
) -> Status {
48+
Status::NOT_READY
49+
}
50+
51+
#[cfg(not(test))]
52+
pub extern "win64" fn stdout_reset(_: *mut SimpleTextOutputProtocol, _: Boolean) -> Status {
53+
crate::log!("EFI_STUB: stdout_reset\n");
54+
Status::UNSUPPORTED
55+
}
56+
57+
#[cfg(not(test))]
58+
pub extern "win64" fn stdout_output_string(
59+
_: *mut SimpleTextOutputProtocol,
60+
message: *mut Char16,
61+
) -> Status {
62+
let mut string_end = false;
63+
64+
loop {
65+
let mut output: [u8; 128] = [0; 128];
66+
let mut i: usize = 0;
67+
while i < output.len() {
68+
output[i] = (unsafe { *message.add(i) } & 0xffu16) as u8;
69+
if output[i] == 0 {
70+
string_end = true;
71+
break;
72+
}
73+
i += 1;
74+
}
75+
crate::log!("{}", unsafe { core::str::from_utf8_unchecked(&output) });
76+
if string_end {
77+
break;
78+
}
79+
}
80+
Status::SUCCESS
81+
}
82+
83+
#[cfg(not(test))]
84+
pub extern "win64" fn stdout_test_string(
85+
_: *mut SimpleTextOutputProtocol,
86+
_: *mut Char16,
87+
) -> Status {
88+
Status::SUCCESS
89+
}
90+
91+
#[cfg(not(test))]
92+
pub extern "win64" fn stdout_query_mode(
93+
_: *mut SimpleTextOutputProtocol,
94+
_: usize,
95+
_: *mut usize,
96+
_: *mut usize,
97+
) -> Status {
98+
crate::log!("EFI_STUB: stdout_query_mode\n");
99+
Status::UNSUPPORTED
100+
}
101+
102+
#[cfg(not(test))]
103+
pub extern "win64" fn stdout_set_mode(_: *mut SimpleTextOutputProtocol, _: usize) -> Status {
104+
crate::log!("EFI_STUB: stdout_set_mode\n");
105+
Status::UNSUPPORTED
106+
}
107+
108+
#[cfg(not(test))]
109+
pub extern "win64" fn stdout_set_attribute(_: *mut SimpleTextOutputProtocol, _: usize) -> Status {
110+
crate::log!("EFI_STUB: stdout_set_attribute\n");
111+
Status::UNSUPPORTED
112+
}
113+
114+
#[cfg(not(test))]
115+
pub extern "win64" fn stdout_clear_screen(_: *mut SimpleTextOutputProtocol) -> Status {
116+
crate::log!("EFI_STUB: stdout_clear_screen\n");
117+
Status::UNSUPPORTED
118+
}
119+
120+
#[cfg(not(test))]
121+
pub extern "win64" fn stdout_set_cursor_position(
122+
_: *mut SimpleTextOutputProtocol,
123+
_: usize,
124+
_: usize,
125+
) -> Status {
126+
crate::log!("EFI_STUB: stdout_set_cursor_position\n");
127+
Status::UNSUPPORTED
128+
}
129+
130+
#[cfg(not(test))]
131+
pub extern "win64" fn stdout_enable_cursor(_: *mut SimpleTextOutputProtocol, _: Boolean) -> Status {
132+
crate::log!("EFI_STUB: stdout_enable_cursor\n");
133+
Status::UNSUPPORTED
134+
}
135+
136+
#[cfg(not(test))]
137+
pub const STDIN: SimpleTextInputProtocol = SimpleTextInputProtocol {
138+
reset: stdin_reset,
139+
read_key_stroke: stdin_read_key_stroke,
140+
wait_for_key: 0 as Event,
141+
};
142+
143+
#[cfg(not(test))]
144+
pub const STDOUT_OUTPUT_MODE: SimpleTextOutputMode = SimpleTextOutputMode {
145+
max_mode: 1,
146+
mode: 0,
147+
attribute: 0,
148+
cursor_column: 0,
149+
cursor_row: 0,
150+
cursor_visible: Boolean::FALSE,
151+
};
152+
153+
#[cfg(not(test))]
154+
pub const STDOUT: SimpleTextOutputProtocol = SimpleTextOutputProtocol {
155+
reset: stdout_reset,
156+
output_string: stdout_output_string,
157+
test_string: stdout_test_string,
158+
query_mode: stdout_query_mode,
159+
set_mode: stdout_set_mode,
160+
set_attribute: stdout_set_attribute,
161+
clear_screen: stdout_clear_screen,
162+
set_cursor_position: stdout_set_cursor_position,
163+
enable_cursor: stdout_enable_cursor,
164+
mode: &STDOUT_OUTPUT_MODE as *const SimpleTextOutputMode as *mut SimpleTextOutputMode,
165+
};

src/efi/mod.rs

Lines changed: 7 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
mod alloc;
1616
mod block;
17+
mod console;
1718
mod file;
1819

1920
use lazy_static::lazy_static;
@@ -28,10 +29,6 @@ use r_efi::efi::{
2829

2930
use r_efi::protocols::device_path::Protocol as DevicePathProtocol;
3031
use r_efi::protocols::loaded_image::Protocol as LoadedImageProtocol;
31-
use r_efi::protocols::simple_text_input::InputKey;
32-
use r_efi::protocols::simple_text_input::Protocol as SimpleTextInputProtocol;
33-
use r_efi::protocols::simple_text_output::Mode as SimpleTextOutputMode;
34-
use r_efi::protocols::simple_text_output::Protocol as SimpleTextOutputProtocol;
3532

3633
use core::ffi::c_void;
3734

@@ -63,105 +60,6 @@ static mut BLOCK_WRAPPERS: block::BlockWrappers = block::BlockWrappers {
6360
count: 0,
6461
};
6562

66-
#[cfg(not(test))]
67-
pub extern "win64" fn stdin_reset(_: *mut SimpleTextInputProtocol, _: Boolean) -> Status {
68-
crate::log!("EFI_STUB: stdin_reset\n");
69-
Status::UNSUPPORTED
70-
}
71-
72-
#[cfg(not(test))]
73-
pub extern "win64" fn stdin_read_key_stroke(
74-
_: *mut SimpleTextInputProtocol,
75-
_: *mut InputKey,
76-
) -> Status {
77-
Status::NOT_READY
78-
}
79-
80-
#[cfg(not(test))]
81-
pub extern "win64" fn stdout_reset(_: *mut SimpleTextOutputProtocol, _: Boolean) -> Status {
82-
crate::log!("EFI_STUB: stdout_reset\n");
83-
Status::UNSUPPORTED
84-
}
85-
86-
#[cfg(not(test))]
87-
pub extern "win64" fn stdout_output_string(
88-
_: *mut SimpleTextOutputProtocol,
89-
message: *mut Char16,
90-
) -> Status {
91-
let mut string_end = false;
92-
93-
loop {
94-
let mut output: [u8; 128] = [0; 128];
95-
let mut i: usize = 0;
96-
while i < output.len() {
97-
output[i] = (unsafe { *message.add(i) } & 0xffu16) as u8;
98-
if output[i] == 0 {
99-
string_end = true;
100-
break;
101-
}
102-
i += 1;
103-
}
104-
crate::log!("{}", unsafe { core::str::from_utf8_unchecked(&output) });
105-
if string_end {
106-
break;
107-
}
108-
}
109-
Status::SUCCESS
110-
}
111-
112-
#[cfg(not(test))]
113-
pub extern "win64" fn stdout_test_string(
114-
_: *mut SimpleTextOutputProtocol,
115-
_: *mut Char16,
116-
) -> Status {
117-
Status::SUCCESS
118-
}
119-
120-
#[cfg(not(test))]
121-
pub extern "win64" fn stdout_query_mode(
122-
_: *mut SimpleTextOutputProtocol,
123-
_: usize,
124-
_: *mut usize,
125-
_: *mut usize,
126-
) -> Status {
127-
crate::log!("EFI_STUB: stdout_query_mode\n");
128-
Status::UNSUPPORTED
129-
}
130-
131-
#[cfg(not(test))]
132-
pub extern "win64" fn stdout_set_mode(_: *mut SimpleTextOutputProtocol, _: usize) -> Status {
133-
crate::log!("EFI_STUB: stdout_set_mode\n");
134-
Status::UNSUPPORTED
135-
}
136-
137-
#[cfg(not(test))]
138-
pub extern "win64" fn stdout_set_attribute(_: *mut SimpleTextOutputProtocol, _: usize) -> Status {
139-
crate::log!("EFI_STUB: stdout_set_attribute\n");
140-
Status::UNSUPPORTED
141-
}
142-
143-
#[cfg(not(test))]
144-
pub extern "win64" fn stdout_clear_screen(_: *mut SimpleTextOutputProtocol) -> Status {
145-
crate::log!("EFI_STUB: stdout_clear_screen\n");
146-
Status::UNSUPPORTED
147-
}
148-
149-
#[cfg(not(test))]
150-
pub extern "win64" fn stdout_set_cursor_position(
151-
_: *mut SimpleTextOutputProtocol,
152-
_: usize,
153-
_: usize,
154-
) -> Status {
155-
crate::log!("EFI_STUB: stdout_set_cursor_position\n");
156-
Status::UNSUPPORTED
157-
}
158-
159-
#[cfg(not(test))]
160-
pub extern "win64" fn stdout_enable_cursor(_: *mut SimpleTextOutputProtocol, _: Boolean) -> Status {
161-
crate::log!("EFI_STUB: stdout_enable_cursor\n");
162-
Status::UNSUPPORTED
163-
}
164-
16563
#[cfg(not(test))]
16664
pub extern "win64" fn get_time(_: *mut Time, _: *mut TimeCapabilities) -> Status {
16765
crate::log!("EFI_STUB: get_time\n");
@@ -830,19 +728,6 @@ fn populate_allocator(image_address: u64, image_size: u64) {
830728
);
831729
}
832730

833-
#[cfg(not(test))]
834-
const STDIN_HANDLE: Handle = &HandleWrapper {
835-
handle_type: HandleType::None,
836-
} as *const _ as Handle;
837-
#[cfg(not(test))]
838-
const STDOUT_HANDLE: Handle = &HandleWrapper {
839-
handle_type: HandleType::None,
840-
} as *const _ as Handle;
841-
#[cfg(not(test))]
842-
const STDERR_HANDLE: Handle = &HandleWrapper {
843-
handle_type: HandleType::None,
844-
} as *const _ as Handle;
845-
846731
#[cfg(not(test))]
847732
#[repr(C)]
848733
struct LoadedImageWrapper {
@@ -858,34 +743,6 @@ pub fn efi_exec(
858743
fs: &crate::fat::Filesystem,
859744
block: *const crate::block::VirtioBlockDevice,
860745
) {
861-
let mut stdin = SimpleTextInputProtocol {
862-
reset: stdin_reset,
863-
read_key_stroke: stdin_read_key_stroke,
864-
wait_for_key: 0 as Event,
865-
};
866-
867-
let mut stdout_mode = SimpleTextOutputMode {
868-
max_mode: 1,
869-
mode: 0,
870-
attribute: 0,
871-
cursor_column: 0,
872-
cursor_row: 0,
873-
cursor_visible: Boolean::FALSE,
874-
};
875-
876-
let mut stdout = SimpleTextOutputProtocol {
877-
reset: stdout_reset,
878-
output_string: stdout_output_string,
879-
test_string: stdout_test_string,
880-
query_mode: stdout_query_mode,
881-
set_mode: stdout_set_mode,
882-
set_attribute: stdout_set_attribute,
883-
clear_screen: stdout_clear_screen,
884-
set_cursor_position: stdout_set_cursor_position,
885-
enable_cursor: stdout_enable_cursor,
886-
mode: &mut stdout_mode,
887-
};
888-
889746
let mut rs = efi::RuntimeServices {
890747
hdr: efi::TableHeader {
891748
signature: efi::RUNTIME_SERVICES_SIGNATURE,
@@ -979,12 +836,12 @@ pub fn efi_exec(
979836
},
980837
firmware_vendor: core::ptr::null_mut(), // TODO,
981838
firmware_revision: 0,
982-
console_in_handle: STDIN_HANDLE,
983-
con_in: &mut stdin,
984-
console_out_handle: STDOUT_HANDLE,
985-
con_out: &mut stdout,
986-
standard_error_handle: STDERR_HANDLE,
987-
std_err: &mut stdout,
839+
console_in_handle: console::STDIN_HANDLE,
840+
con_in: &mut console::STDIN,
841+
console_out_handle: console::STDOUT_HANDLE,
842+
con_out: &mut console::STDOUT,
843+
standard_error_handle: console::STDERR_HANDLE,
844+
std_err: &mut console::STDOUT,
988845
runtime_services: &mut rs,
989846
boot_services: &mut bs,
990847
number_of_table_entries: 0,

0 commit comments

Comments
 (0)