-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmod.rs
More file actions
396 lines (342 loc) · 12.3 KB
/
mod.rs
File metadata and controls
396 lines (342 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
use alloc::vec::Vec;
use core::{cell::RefCell, mem, ptr, slice};
use std::proto::Protocol;
use uefi::{
Handle,
boot::LocateSearchType,
memory::MemoryType,
reset::ResetType,
status::{Result, Status},
system::SystemTable,
text::TextInputKey,
};
use crate::os::{Os, OsHwDesc, OsKey, OsVideoMode};
use self::{
device::{device_path_to_string, disk_device_priority},
disk::DiskOrFileEfi,
display::{EdidActive, Output},
video_mode::VideoModeIter,
};
mod acpi;
mod arch;
mod device;
mod disk;
mod display;
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
pub mod dtb;
mod memory_map;
mod video_mode;
#[cfg(target_arch = "riscv64")]
pub use arch::efi_get_boot_hartid;
pub(crate) fn page_size() -> usize {
// EDK2 always uses 4096 as the page size
4096
}
pub(crate) fn alloc_zeroed_page_aligned(size: usize) -> *mut u8 {
assert!(size != 0);
let page_size = page_size();
let pages = size.div_ceil(page_size);
let ptr = {
// Max address mapped by src/arch paging code (8 GiB)
let mut ptr = 0x2_0000_0000;
status_to_result((std::system_table().BootServices.AllocatePages)(
1, // AllocateMaxAddress
MemoryType::EfiRuntimeServicesData, // Keeps this memory out of free space list
pages,
&mut ptr,
))
.unwrap();
ptr as *mut u8
};
assert!(!ptr.is_null());
unsafe { ptr::write_bytes(ptr, 0, pages * page_size) };
ptr
}
pub struct OsEfi {
st: &'static SystemTable,
outputs: RefCell<Vec<(Output, Option<EdidActive>)>>,
}
impl OsEfi {
pub fn new() -> Self {
let st = std::system_table();
let mut outputs = Vec::<(Output, Option<EdidActive>)>::new();
{
let guid = Output::guid();
let mut handles = Vec::with_capacity(256);
let mut len = handles.capacity() * mem::size_of::<Handle>();
match status_to_result((st.BootServices.LocateHandle)(
LocateSearchType::ByProtocol,
&guid,
ptr::null(),
&mut len,
handles.as_mut_ptr(),
)) {
Ok(_) => {
unsafe {
handles.set_len(len / mem::size_of::<Handle>());
}
'handles: for handle in handles {
//TODO: do we have to query all modes to get good edid?
match Output::handle_protocol(handle) {
Ok(output) => {
log::debug!(
"Output {:?} at {:x}",
handle,
output.0.Mode.FrameBufferBase
);
if output.0.Mode.FrameBufferBase == 0 {
log::debug!("Skipping output with frame buffer base of 0");
continue 'handles;
}
for other_output in outputs.iter() {
if output.0.Mode.FrameBufferBase
== other_output.0.0.Mode.FrameBufferBase
{
log::debug!(
"Skipping output with frame buffer base matching another output"
);
continue 'handles;
}
}
outputs.push((
output,
match EdidActive::handle_protocol(handle) {
Ok(efi_edid) => Some(efi_edid),
Err(err) => {
log::warn!(
"Failed to get EFI EDID from handle {:?}: {:?}",
handle,
err
);
None
}
},
));
}
Err(err) => {
log::warn!(
"Failed to get Output from handle {:?}: {:?}",
handle,
err
);
}
}
}
}
Err(err) => {
log::warn!("Failed to locate Outputs: {:?}", err);
}
}
}
Self {
st,
outputs: RefCell::new(outputs),
}
}
}
impl Os for OsEfi {
type D = DiskOrFileEfi;
type V = VideoModeIter;
#[cfg(target_arch = "aarch64")]
fn name(&self) -> &str {
"aarch64/UEFI"
}
#[cfg(target_arch = "x86_64")]
fn name(&self) -> &str {
"x86_64/UEFI"
}
#[cfg(target_arch = "riscv64")]
fn name(&self) -> &str {
"riscv64/UEFI"
}
fn alloc_zeroed_page_aligned(&self, size: usize) -> *mut u8 {
alloc_zeroed_page_aligned(size)
}
fn page_size(&self) -> usize {
page_size()
}
fn filesystem(
&self,
password_opt: Option<&[u8]>,
) -> syscall::Result<redoxfs::FileSystem<DiskOrFileEfi>> {
// Search for RedoxFS on disks in prioritized order
println!("Looking for RedoxFS:");
for device in disk_device_priority() {
if let Some(file_path) = device.file_path {
log::debug!(
" - {}\\{}",
device_path_to_string(device.device_path.0),
file_path
);
} else {
log::debug!(" - {}", device_path_to_string(device.device_path.0));
}
let block = device.partition_offset / redoxfs::BLOCK_SIZE;
match redoxfs::FileSystem::open(device.disk, password_opt, Some(block), false) {
Ok(ok) => return Ok(ok),
Err(err) => match err.errno {
// Ignore header not found error
syscall::ENOENT => (),
// Print any other errors
_ => {
log::warn!("BlockIo error: {:?}", err);
}
},
}
}
log::warn!("No RedoxFS partitions found");
Err(syscall::Error::new(syscall::ENOENT))
}
fn hwdesc(&self) -> OsHwDesc {
//TODO: if both DTB and ACPI are found, we should probably let the OS choose what to use?
// For now we will prefer DTB on platforms that have it
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
if let Some((addr, size)) = dtb::find_dtb(self) {
return OsHwDesc::DeviceTree(addr, size);
}
if let Some((addr, size)) = acpi::find_acpi_table_pointers(self) {
return OsHwDesc::Acpi(addr, size);
}
OsHwDesc::NotFound
}
fn video_outputs(&self) -> usize {
self.outputs.borrow().len()
}
fn video_modes(&self, output_i: usize) -> VideoModeIter {
let output_opt = match self.outputs.borrow_mut().get_mut(output_i) {
Some(output) => unsafe {
// Hack to enable clone
let ptr = output.0.0 as *mut _;
Some(Output::new(&mut *ptr))
},
None => None,
};
VideoModeIter::new(output_opt)
}
fn set_video_mode(&self, output_i: usize, mode: &mut OsVideoMode) {
//TODO: return error?
let mut outputs = self.outputs.borrow_mut();
let (output, _efi_edid_opt) = &mut outputs[output_i];
status_to_result((output.0.SetMode)(output.0, mode.id)).unwrap();
// Update with actual mode information
mode.width = output.0.Mode.Info.HorizontalResolution;
mode.height = output.0.Mode.Info.VerticalResolution;
mode.base = output.0.Mode.FrameBufferBase as u64;
}
fn best_resolution(&self, output_i: usize) -> Option<(u32, u32)> {
let mut outputs = self.outputs.borrow_mut();
let (output, efi_edid_opt) = outputs.get_mut(output_i)?;
if let Some(efi_edid) = efi_edid_opt {
let edid =
unsafe { slice::from_raw_parts(efi_edid.0.Edid, efi_edid.0.SizeOfEdid as usize) };
if edid.len() > 0x3D {
return Some((
(edid[0x38] as u32) | (((edid[0x3A] as u32) & 0xF0) << 4),
(edid[0x3B] as u32) | (((edid[0x3D] as u32) & 0xF0) << 4),
));
} else {
log::warn!("EFI EDID too small: {}", edid.len());
}
}
// Fallback to the current output resolution
Some((
output.0.Mode.Info.HorizontalResolution,
output.0.Mode.Info.VerticalResolution,
))
}
fn get_key(&self) -> OsKey {
//TODO: do not unwrap
let mut index = 0;
status_to_result((self.st.BootServices.WaitForEvent)(
1,
&self.st.ConsoleIn.WaitForKey,
&mut index,
))
.unwrap();
let mut key = TextInputKey {
ScanCode: 0,
UnicodeChar: 0,
};
status_to_result((self.st.ConsoleIn.ReadKeyStroke)(
self.st.ConsoleIn,
&mut key,
))
.unwrap();
match key.ScanCode {
0 => match key.UnicodeChar {
8 => OsKey::Backspace,
13 => OsKey::Enter,
w => match char::from_u32(w as u32) {
Some(c) => OsKey::Char(c),
None => OsKey::Other,
},
},
1 => OsKey::Up,
2 => OsKey::Down,
3 => OsKey::Right,
4 => OsKey::Left,
8 => OsKey::Delete,
_ => OsKey::Other,
}
}
fn clear_text(&self) {
//TODO: why does this sometimes return InvalidParameter, but otherwise appear to work?
let _ = status_to_result((self.st.ConsoleOut.ClearScreen)(self.st.ConsoleOut));
}
fn get_text_position(&self) -> (usize, usize) {
(
self.st.ConsoleOut.Mode.CursorColumn as usize,
self.st.ConsoleOut.Mode.CursorRow as usize,
)
}
fn set_text_position(&self, x: usize, y: usize) {
// Ignore error because Tow-Boot appears to not implement this
let _ = status_to_result((self.st.ConsoleOut.SetCursorPosition)(
self.st.ConsoleOut,
x,
y,
));
}
fn set_text_highlight(&self, highlight: bool) {
let attr = if highlight { 0x70 } else { 0x07 };
status_to_result((self.st.ConsoleOut.SetAttribute)(self.st.ConsoleOut, attr)).unwrap();
}
}
fn status_to_result(status: Status) -> Result<usize> {
match status {
Status(ok) if status.is_success() => Ok(ok),
err => Err(err),
}
}
fn set_max_mode(output: &uefi::text::TextOutput) -> Result<()> {
let mut max_i = None;
let mut max_w = 0;
let mut max_h = 0;
for i in 0..output.Mode.MaxMode as usize {
let mut w = 0;
let mut h = 0;
if (output.QueryMode)(output, i, &mut w, &mut h).is_success() {
if w >= max_w && h >= max_h {
max_i = Some(i);
max_w = w;
max_h = h;
}
}
}
if let Some(i) = max_i {
status_to_result((output.SetMode)(output, i))?;
}
Ok(())
}
#[unsafe(no_mangle)]
pub extern "C" fn main() -> Status {
let uefi = std::system_table();
let _ = (uefi.BootServices.SetWatchdogTimer)(0, 0, 0, ptr::null());
if let Err(err) = set_max_mode(uefi.ConsoleOut) {
println!("Failed to set max mode: {:?}", err);
}
if let Err(err) = arch::main() {
panic!("App error: {:?}", err);
}
(uefi.RuntimeServices.ResetSystem)(ResetType::Cold, Status(0), 0, ptr::null());
}