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
#![no_std]
#![feature(alloc_error_handler)]
#![feature(llvm_asm)]
#![feature(lang_items)]
#![feature(panic_info_message)]
#[macro_use]
extern crate log;
extern crate uefi;
use core::ptr::NonNull;
use cfg_if::cfg_if;
use uefi::prelude::*;
use uefi::table::boot::{EventType, Tpl};
use uefi::table::{Boot, SystemTable};
use uefi::{Event, Result};
static mut SYSTEM_TABLE: Option<SystemTable<Boot>> = None;
static mut LOGGER: Option<uefi::logger::Logger> = None;
pub fn system_table() -> NonNull<SystemTable<Boot>> {
unsafe {
let table_ref = SYSTEM_TABLE
.as_ref()
.expect("The system table handle is not available");
NonNull::new(table_ref as *const _ as *mut _).unwrap()
}
}
pub fn init(st: &SystemTable<Boot>) -> Result {
unsafe {
if SYSTEM_TABLE.is_some() {
return Status::SUCCESS.into();
}
SYSTEM_TABLE = Some(st.unsafe_clone());
let boot_services = st.boot_services();
init_logger(st);
uefi::alloc::init(boot_services);
boot_services
.create_event(
EventType::SIGNAL_EXIT_BOOT_SERVICES,
Tpl::NOTIFY,
Some(exit_boot_services),
)
.map_inner(|_| ())
}
}
unsafe fn init_logger(st: &SystemTable<Boot>) {
let stdout = st.stdout();
let logger = {
LOGGER = Some(uefi::logger::Logger::new(stdout));
LOGGER.as_ref().unwrap()
};
log::set_logger(logger).unwrap();
log::set_max_level(log::LevelFilter::Info);
}
fn exit_boot_services(_e: Event) {
unsafe {
SYSTEM_TABLE = None;
if let Some(ref mut logger) = LOGGER {
logger.disable();
}
}
uefi::alloc::exit_boot_services();
}
#[lang = "eh_personality"]
fn eh_personality() {}
#[panic_handler]
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
if let Some(location) = info.location() {
error!(
"Panic in {} at ({}, {}):",
location.file(),
location.line(),
location.column()
);
if let Some(message) = info.message() {
error!("{}", message);
}
}
if let Some(st) = unsafe { SYSTEM_TABLE.as_ref() } {
st.boot_services().stall(10_000_000);
} else {
let mut dummy = 0u64;
for i in 0..300_000_000 {
unsafe {
core::ptr::write_volatile(&mut dummy, i);
}
}
}
if cfg!(feature = "qemu") {
cfg_if! {
if #[cfg(target_arch = "x86_64")] {
use x86_64::instructions::port::Port;
let mut port = Port::<u32>::new(0xf4);
unsafe {
port.write(42);
}
} else if #[cfg(target_arch = "aarch64")] {
}
}
}
if let Some(st) = unsafe { SYSTEM_TABLE.as_ref() } {
use uefi::table::runtime::ResetType;
st.runtime_services()
.reset(ResetType::Shutdown, uefi::Status::ABORTED, None);
}
error!("Could not shut down, please power off the system manually...");
cfg_if! {
if #[cfg(target_arch = "x86_64")] {
loop {
unsafe {
llvm_asm!("hlt" :::: "volatile");
}
}
} else if #[cfg(target_arch = "aarch64")] {
loop {
unsafe {
llvm_asm!("hlt 420" :::: "volatile");
}
}
} else {
loop {
}
}
}
}
#[alloc_error_handler]
fn out_of_memory(layout: ::core::alloc::Layout) -> ! {
panic!(
"Ran out of free memory while trying to allocate {:#?}",
layout
);
}