Debian Codegraph @

.cargo/vendor/r-efi/examples/gop-query.rs

raw
1 // Example: Graphics Query
2 //
3 // This is a slightly more complex UEFI application than `hello-world`. It
4 // locates the graphics-output-protocol, queries its current mode and prints
5 // the current resolution to the UEFI console.
6 //
7 // This example should make everyone aware that UEFI programing in Rust really
8 // asks for helper layers. While the C/FFI/Spec API can be used directly, it
9 // is quite cumbersome. Especially the error handling is overly difficult.
10 //
11 // Nevertheless, this example shows how to find UEFI protocol and invoke
12 // their member functions.
13 //
14 // Like all the other r-efi examples, it is a standalone example. That is, no
15 // UTF-16 helpers are pulled in, nor any allocators or panic frameworks. For
16 // real world scenarios, you really should choose such helpers.
17
18 #![no_main]
19 #![no_std]
20
21 use r_efi::efi;
22
23 #[panic_handler]
24 fn panic_handler(_info: &core::panic::PanicInfo) -> ! {
25 loop {}
26 }
27
28 fn fail(_r: efi::Status) -> ! {
29 panic!();
30 }
31
32 // A simple `itoa()`-ish function that takes a u32 and turns it into a UTF-16
33 // string. It always prints exactly 10 characters, so leading zeroes are used
34 // for small numbers.
35 fn utoa(mut u: u32, a: &mut [u16]) {
36 for i in 0..10 {
37 a[9 - i] = 0x0030u16 + ((u % 10) as u16);
38 u = u / 10;
39 }
40 }
41
42 // A simple helper that takes two integers and prints them to the UEFI console
43 // with a short prefix. It uses a UTF-16 buffer and fills in the numbers before
44 // printing the entire buffer.
45 fn print_xy(st: *mut efi::SystemTable, x: u32, y: u32) {
46 let mut s = [
47 0x0058u16, 0x0059u16, 0x003au16, // "XY:"
48 0x0020u16, // " "
49 0x0020u16, 0x0020u16, 0x0020u16, 0x0020u16, // " "
50 0x0020u16, 0x0020u16, 0x0020u16, 0x0020u16, // " "
51 0x0020u16, 0x0020u16, // " "
52 0x0078u16, // "x"
53 0x0020u16, 0x0020u16, 0x0020u16, 0x0020u16, // " "
54 0x0020u16, 0x0020u16, 0x0020u16, 0x0020u16, // " "
55 0x0020u16, 0x0020u16, // " "
56 0x000au16, // "\n"
57 0x0000u16, // NUL
58 ];
59
60 utoa(x, &mut s[4..14]);
61 utoa(y, &mut s[15..25]);
62
63 unsafe {
64 let r = ((*(*st).con_out).output_string)((*st).con_out, s.as_ptr() as *mut efi::Char16);
65 if r.is_error() {
66 fail(r);
67 }
68 }
69 }
70
71 // This function locates singleton UEFI protocols. Those protocols do not
72 // require to register listener handles, but are globally available to all
73 // UEFI applications. It takes a GUID of the protocol to locate and returns
74 // the protocol pointer on success.
75 fn locate_singleton(
76 st: *mut efi::SystemTable,
77 guid: *const efi::Guid,
78 ) -> Result<*mut core::ffi::c_void, efi::Status> {
79 let mut interface: *mut core::ffi::c_void = core::ptr::null_mut();
80 let mut handles: *mut efi::Handle = core::ptr::null_mut();
81 let mut n_handles: usize = 0;
82 let mut r: efi::Status;
83
84 // Use `locate_handle_buffer()` to find all handles that support the
85 // specified protocol.
86 unsafe {
87 if (*st).hdr.revision < efi::SYSTEM_TABLE_REVISION_1_10 {
88 // We use `LocateHandleBuffer`, which was introduced in 1.10.
89 return Err(efi::Status::UNSUPPORTED);
90 }
91
92 let r = ((*(*st).boot_services).locate_handle_buffer)(
93 efi::BY_PROTOCOL,
94 guid as *mut _,
95 core::ptr::null_mut(),
96 &mut n_handles,
97 &mut handles,
98 );
99 match r {
100 efi::Status::SUCCESS => {}
101 efi::Status::NOT_FOUND => return Err(r),
102 efi::Status::OUT_OF_RESOURCES => return Err(r),
103 _ => panic!(),
104 };
105 }
106
107 // Now that we have all handles with the specified protocol, query it for
108 // the protocol interface. We loop here, even though every item should
109 // succeed. Lets be on the safe side.
110 // Secondly, we use `handle_protocol()` here, but really should be using
111 // `open_protocol()`. But for singleton protocols, this does not matter,
112 // so lets use the simple path for now.
113 unsafe {
114 r = efi::Status::NOT_FOUND;
115 for i in 0..n_handles {
116 r = ((*(*st).boot_services).handle_protocol)(
117 *handles.offset(core::convert::TryFrom::<usize>::try_from(i).unwrap()),
118 guid as *mut _,
119 &mut interface,
120 );
121 match r {
122 efi::Status::SUCCESS => break,
123 efi::Status::UNSUPPORTED => continue,
124 _ => panic!(),
125 };
126 }
127 }
128
129 // Free the allocated buffer memory of `handles`. This was allocated on the
130 // pool by `locate_handle_buffer()`.
131 unsafe {
132 let r = ((*(*st).boot_services).free_pool)(handles as *mut core::ffi::c_void);
133 assert!(!r.is_error());
134 }
135
136 // In case we found nothing, return `NOT_FOUND`, otherwise return the
137 // interface identifier.
138 match r {
139 efi::Status::SUCCESS => Ok(interface),
140 _ => Err(efi::Status::NOT_FOUND),
141 }
142 }
143
144 // A simple helper that queries the current mode of the GraphicsOutputProtocol
145 // and returns the x and y dimensions on success.
146 fn query_gop(
147 gop: *mut efi::protocols::graphics_output::Protocol,
148 ) -> Result<(u32, u32), efi::Status> {
149 let mut info: *mut efi::protocols::graphics_output::ModeInformation = core::ptr::null_mut();
150 let mut z_info: usize = 0;
151
152 unsafe {
153 // We could just look at `gop->mode->info`, but lets query the mode
154 // instead to show how to query other modes than the active one.
155 let r = ((*gop).query_mode)(gop, (*(*gop).mode).mode, &mut z_info, &mut info);
156 match r {
157 efi::Status::SUCCESS => {}
158 efi::Status::DEVICE_ERROR => return Err(r),
159 _ => panic!(),
160 };
161 if z_info < core::mem::size_of_val(&*info) {
162 return Err(efi::Status::UNSUPPORTED);
163 }
164
165 Ok(((*info).horizontal_resolution, (*info).vertical_resolution))
166 }
167 }
168
169 // This is the UEFI application entrypoint. We use it to locate the GOP
170 // pointer, query the current mode, and then print it to the system console.
171 #[export_name = "efi_main"]
172 pub extern "C" fn main(_h: efi::Handle, st: *mut efi::SystemTable) -> efi::Status {
173 let r = locate_singleton(st, &efi::protocols::graphics_output::PROTOCOL_GUID);
174 let gop = match r {
175 Ok(v) => v,
176 Err(r) => fail(r),
177 };
178
179 let r = query_gop(gop as _);
180 let v = match r {
181 Ok(v) => v,
182 Err(r) => fail(r),
183 };
184
185 print_xy(st, v.0, v.1);
186
187 efi::Status::SUCCESS
188 }