Skip to content

Commit 9777fd4

Browse files
felipebalbinicholasbishop
authored andcommitted
uefi: debug: add DebugPort protocol
DebugPort might be useful for some uefi applications.
1 parent 46069ef commit 9777fd4

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

uefi-test-runner/src/proto/debug.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,72 @@
1+
use alloc::vec::Vec;
12
use core::ffi::c_void;
2-
use uefi::proto::debug::{DebugSupport, ExceptionType, ProcessorArch, SystemContext};
3+
use uefi::proto::debug::{DebugPort, DebugSupport, ExceptionType, ProcessorArch, SystemContext};
34
use uefi::table::boot::BootServices;
45

56
pub fn test(bt: &BootServices) {
7+
test_debug_support(bt);
8+
test_debug_port(bt);
9+
}
10+
11+
fn test_debug_port(bt: &BootServices) {
12+
info!("Running UEFI debug port protocol test");
13+
if let Ok(handles) = bt.find_handles::<DebugPort>() {
14+
for handle in handles {
15+
if let Ok(debug_port) = bt.open_protocol_exclusive::<DebugPort>(handle) {
16+
let timeout = 1000;
17+
18+
debug_port
19+
.reset()
20+
.expect("Error while resetting debug port");
21+
let data: Vec<_> = r##"
22+
.. .=- .
23+
. :##+ .*##= -*#+ ..
24+
=#*=:.*####+#####**####-.-*##
25+
.=-. *############################: :-:
26+
-###**##############################*###*
27+
:. -#######################################* .::
28+
=##################################################
29+
.#################################################=
30+
-====#################################################+====.
31+
=##########################################################.
32+
+########################################################.
33+
-==+#########################################################*+=-.
34+
+######################=:=@@%#########+:-%@%####################*:
35+
=####################+ +@@@%######%. -@@@##################*.
36+
:+#####################@-.:%@@@@#####%@*::#@@@%###################+:
37+
+#######################@@@@@@@@@######@@@@@@@@%#####################-
38+
-#########################@@@@@@@########@@@@@@%#######################
39+
-#######%%%##################%##############################%%%%######*
40+
+########%%%%############################################*%%%%#######:
41+
=######+=%%%#####***##%%#####################%#**++####=:%%%#######:
42+
:*#####:.*%#-####+==*########+-::::::=*#######*=+###*- *%*:-####*.
43+
-####* .+. -*###############= .*#############*- .*: *###+
44+
=###: *##############+ .##############+ .###=
45+
.+#* -######*=+++**** =###***++######- :#*.
46+
.- -######- .*#####- .-
47+
=*####*. =####+-
48+
.:--: ::::.
49+
"##
50+
.bytes()
51+
.collect();
52+
53+
debug_port
54+
.write(timeout, &data)
55+
.expect("Error while writing to debug port");
56+
57+
debug_port.poll().expect("Error while polling debug port");
58+
59+
let mut data = Vec::with_capacity(4096);
60+
61+
debug_port
62+
.read(timeout, &mut data)
63+
.expect("Error while reading from debug port");
64+
}
65+
}
66+
}
67+
}
68+
69+
fn test_debug_support(bt: &BootServices) {
670
info!("Running UEFI debug connection protocol test");
771
let handles = bt
872
.find_handles::<DebugSupport>()

uefi/src/proto/debug/mod.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,72 @@ pub enum ProcessorArch: u32 => {
170170
/// RISC-V 128-bit
171171
RISCV_128 = 0x5128,
172172
}}
173+
174+
/// The debug port protocol abstracts the underlying debug port
175+
/// hardware, whether it is a regular Serial port or something else.
176+
#[repr(C)]
177+
#[unsafe_protocol("eba4e8d2-3858-41ec-a281-2647ba9660d0")]
178+
pub struct DebugPort {
179+
reset: extern "efiapi" fn(this: &DebugPort) -> Status,
180+
write: extern "efiapi" fn(
181+
this: &DebugPort,
182+
timeout: u32,
183+
buffer_size: &mut usize,
184+
buffer: *const c_void,
185+
) -> Status,
186+
read: extern "efiapi" fn(
187+
this: &DebugPort,
188+
timeout: u32,
189+
buffer_size: &mut usize,
190+
buffer: *mut c_void,
191+
) -> Status,
192+
poll: extern "efiapi" fn(this: &DebugPort) -> Status,
193+
}
194+
195+
impl DebugPort {
196+
/// Resets the debugport device.
197+
pub fn reset(&self) -> Result {
198+
(self.reset)(self).into()
199+
}
200+
201+
/// Write data to the debugport device.
202+
///
203+
/// Note: `timeout` is given in microseconds
204+
pub fn write(&self, timeout: u32, data: &[u8]) -> Result<(), usize> {
205+
let mut buffer_size = data.len();
206+
207+
(self.write)(
208+
self,
209+
timeout,
210+
&mut buffer_size,
211+
data.as_ptr().cast::<c_void>(),
212+
)
213+
.into_with(
214+
|| debug_assert_eq!(buffer_size, data.len()),
215+
|_| buffer_size,
216+
)
217+
}
218+
219+
/// Read data from the debugport device.
220+
///
221+
/// Note: `timeout` is given in microseconds
222+
pub fn read(&self, timeout: u32, data: &mut [u8]) -> Result<(), usize> {
223+
let mut buffer_size = data.len();
224+
225+
(self.read)(
226+
self,
227+
timeout,
228+
&mut buffer_size,
229+
data.as_mut_ptr().cast::<c_void>(),
230+
)
231+
.into_with(
232+
|| debug_assert_eq!(buffer_size, data.len()),
233+
|_| buffer_size,
234+
)
235+
}
236+
237+
/// Check to see if any data is available to be read from the debugport device.
238+
pub fn poll(&self) -> Result {
239+
(self.poll)(self).into()
240+
}
241+
}

0 commit comments

Comments
 (0)