-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathruntime_services.rs
More file actions
276 lines (241 loc) · 7.53 KB
/
runtime_services.rs
File metadata and controls
276 lines (241 loc) · 7.53 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
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2019 Intel Corporation
use core::{
cell::SyncUnsafeCell,
ffi::c_void,
mem::{size_of, transmute},
};
use r_efi::{
efi::{
self, Boolean, CapsuleHeader, Char16, Guid, MemoryDescriptor, PhysicalAddress, ResetType,
Status, Time, TimeCapabilities,
},
system::{ConfigurationTable, RuntimeServices},
};
use crate::rtc;
use super::{ALLOCATOR, ST, VARIABLES};
pub static mut RS: SyncUnsafeCell<efi::RuntimeServices> =
SyncUnsafeCell::new(efi::RuntimeServices {
hdr: efi::TableHeader {
signature: efi::RUNTIME_SERVICES_SIGNATURE,
revision: efi::RUNTIME_SERVICES_REVISION,
header_size: size_of::<efi::RuntimeServices>() as u32,
crc32: 0, // TODO
reserved: 0,
},
get_time,
set_time,
get_wakeup_time,
set_wakeup_time,
set_virtual_address_map,
convert_pointer,
get_variable,
get_next_variable_name,
set_variable,
get_next_high_mono_count,
reset_system: reset_system_boot,
update_capsule,
query_capsule_capabilities,
query_variable_info,
});
#[allow(clippy::missing_transmute_annotations)]
unsafe fn fixup_at_virtual(descriptors: &[MemoryDescriptor]) {
#[allow(static_mut_refs)]
let st = ST.get_mut();
#[allow(static_mut_refs)]
let rs = RS.get_mut();
let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (not_available as *const ()) as u64)
.unwrap();
rs.get_time = transmute(ptr);
rs.set_time = transmute(ptr);
rs.get_wakeup_time = transmute(ptr);
rs.set_wakeup_time = transmute(ptr);
rs.get_variable = transmute(ptr);
rs.set_variable = transmute(ptr);
rs.get_next_variable_name = transmute(ptr);
let reset_ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (reset_system as *const ()) as u64)
.unwrap();
rs.reset_system = transmute(reset_ptr);
rs.update_capsule = transmute(ptr);
rs.query_capsule_capabilities = transmute(ptr);
rs.query_variable_info = transmute(ptr);
let ct = st.configuration_table;
let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (ct as *const _) as u64)
.unwrap();
st.configuration_table = ptr as *mut ConfigurationTable;
let rs = st.runtime_services;
let ptr = ALLOCATOR
.borrow()
.convert_internal_pointer(descriptors, (rs as *const _) as u64)
.unwrap();
st.runtime_services = ptr as *mut RuntimeServices;
}
pub extern "efiapi" fn not_available() -> Status {
Status::UNSUPPORTED
}
pub extern "efiapi" fn get_time(time: *mut Time, _: *mut TimeCapabilities) -> Status {
if time.is_null() {
return Status::INVALID_PARAMETER;
}
let (year, month, day) = match rtc::read_date() {
Ok((y, m, d)) => (y, m, d),
Err(()) => return Status::DEVICE_ERROR,
};
let (hour, minute, second) = match rtc::read_time() {
Ok((h, m, s)) => (h, m, s),
Err(()) => return Status::DEVICE_ERROR,
};
unsafe {
(*time).year = 2000 + year as u16;
(*time).month = month;
(*time).day = day;
(*time).hour = hour;
(*time).minute = minute;
(*time).second = second;
(*time).nanosecond = 0;
(*time).timezone = 0;
(*time).daylight = 0;
}
Status::SUCCESS
}
pub extern "efiapi" fn set_time(_: *mut Time) -> Status {
Status::DEVICE_ERROR
}
pub extern "efiapi" fn get_wakeup_time(_: *mut Boolean, _: *mut Boolean, _: *mut Time) -> Status {
Status::UNSUPPORTED
}
pub extern "efiapi" fn set_wakeup_time(_: Boolean, _: *mut Time) -> Status {
Status::UNSUPPORTED
}
pub extern "efiapi" fn set_virtual_address_map(
map_size: usize,
descriptor_size: usize,
version: u32,
descriptors: *mut MemoryDescriptor,
) -> Status {
let count = map_size / descriptor_size;
if version != efi::MEMORY_DESCRIPTOR_VERSION {
return Status::INVALID_PARAMETER;
}
let descriptors = unsafe { core::slice::from_raw_parts_mut(descriptors, count) };
unsafe {
fixup_at_virtual(descriptors);
}
ALLOCATOR.borrow_mut().update_virtual_addresses(descriptors)
}
pub extern "efiapi" fn convert_pointer(_: usize, _: *mut *mut c_void) -> Status {
Status::UNSUPPORTED
}
pub extern "efiapi" fn get_variable(
variable_name: *mut Char16,
vendor_guid: *mut Guid,
attributes: *mut u32,
data_size: *mut usize,
data: *mut c_void,
) -> Status {
if cfg!(feature = "efi-var") {
VARIABLES
.borrow_mut()
.get(variable_name, vendor_guid, attributes, data_size, data)
} else {
Status::NOT_FOUND
}
}
pub extern "efiapi" fn get_next_variable_name(
_: *mut usize,
_: *mut Char16,
_: *mut Guid,
) -> Status {
Status::NOT_FOUND
}
pub extern "efiapi" fn set_variable(
variable_name: *mut Char16,
vendor_guid: *mut Guid,
attributes: u32,
data_size: usize,
data: *mut c_void,
) -> Status {
if cfg!(feature = "efi-var") {
VARIABLES
.borrow_mut()
.set(variable_name, vendor_guid, attributes, data_size, data)
} else {
Status::UNSUPPORTED
}
}
pub extern "efiapi" fn get_next_high_mono_count(_: *mut u32) -> Status {
Status::DEVICE_ERROR
}
// No-op used during boot services phase. EFI applications like shim may call
// ResetSystem during early boot (e.g. on MOK import failure). Returning here
// lets them continue, matching the original firmware behavior.
pub extern "efiapi" fn reset_system_boot(_: ResetType, _: Status, _: usize, _: *mut c_void) {}
// Activated after ExitBootServices via swap_reset_system(). Writes to Cloud
// Hypervisor's AcpiShutdownDevice at IO port 0x600 to perform the actual
// VM shutdown or reset. On non-CH platforms the port write is a no-op and
// the function falls through to a halt loop.
pub extern "efiapi" fn reset_system(_reset_type: ResetType, _: Status, _: usize, _: *mut c_void) {
#[cfg(target_arch = "x86_64")]
{
const SHUTDOWN_PORT: u16 = 0x600;
const S5_SLEEP_VALUE: u8 = (5 << 2) | (1 << 5); // SLP_TYP=5, SLP_EN=1
const REBOOT_VALUE: u8 = 1;
let value = if _reset_type == efi::RESET_SHUTDOWN {
S5_SLEEP_VALUE
} else {
REBOOT_VALUE
};
unsafe {
core::arch::asm!("out dx, al", in("dx") SHUTDOWN_PORT, in("al") value);
}
}
loop {
#[cfg(target_arch = "x86_64")]
unsafe {
core::arch::asm!("hlt");
}
#[cfg(not(target_arch = "x86_64"))]
core::hint::spin_loop();
}
}
/// Switch the runtime services table from the boot-phase no-op to the real
/// reset_system implementation. Called from exit_boot_services.
pub unsafe fn swap_reset_system() {
#[allow(static_mut_refs)]
let rs = RS.get_mut();
rs.reset_system = reset_system;
}
pub extern "efiapi" fn update_capsule(
_: *mut *mut CapsuleHeader,
_: usize,
_: PhysicalAddress,
) -> Status {
Status::UNSUPPORTED
}
pub extern "efiapi" fn query_capsule_capabilities(
_: *mut *mut CapsuleHeader,
_: usize,
_: *mut u64,
_: *mut ResetType,
) -> Status {
Status::UNSUPPORTED
}
pub extern "efiapi" fn query_variable_info(
_: u32,
max_storage: *mut u64,
remaining_storage: *mut u64,
max_size: *mut u64,
) -> Status {
unsafe {
*max_storage = 0;
*remaining_storage = 0;
*max_size = 0;
}
Status::SUCCESS
}