Skip to content

Commit 0b97987

Browse files
committed
Prevent boot from failing early via panic, and allow net boot to attempt boot
1 parent e577319 commit 0b97987

File tree

1 file changed

+57
-36
lines changed

1 file changed

+57
-36
lines changed

uefi/src/main.rs

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use uefi::{
1313
prelude::{entry, Boot, Handle, Status, SystemTable},
1414
proto::{
1515
console::gop::{GraphicsOutput, PixelFormat},
16-
device_path::DevicePath,
17-
loaded_image::LoadedImage,
16+
device_path::{self, DevicePath},
17+
loaded_image::{self, LoadedImage},
1818
media::{
1919
file::{File, FileAttribute, FileInfo, FileMode},
2020
fs::SimpleFileSystem,
@@ -123,7 +123,7 @@ fn main_inner(image: Handle, mut st: SystemTable<Boot>) -> Status {
123123
rsdp.map(|entry| PhysAddr::new(entry.address as u64))
124124
},
125125
ramdisk_addr: None,
126-
ramdisk_len: 0
126+
ramdisk_len: 0,
127127
};
128128

129129
bootloader_x86_64_common::load_and_switch_to_kernel(
@@ -145,62 +145,83 @@ fn load_kernel_file(image: Handle, st: &SystemTable<Boot>) -> Option<&'static mu
145145
.or_else(|| load_kernel_file_from_tftp_boot_server(image, st))
146146
}
147147

148-
fn load_file_from_disk(name: &str, image: Handle, st: &SystemTable<Boot>) -> Option<&'static mut [u8]>{
148+
fn load_file_from_disk(
149+
name: &str,
150+
image: Handle,
151+
st: &SystemTable<Boot>,
152+
) -> Option<&'static mut [u8]> {
149153
let file_system_raw = {
150154
let this = st.boot_services();
151-
let loaded_image = unsafe { this
152-
.open_protocol::<LoadedImage>(
153-
OpenProtocolParams {
154-
handle: image,
155-
agent: image,
156-
controller: None,
157-
},
158-
OpenProtocolAttributes::Exclusive,
159-
)
160-
.expect("Failed to retrieve `LoadedImage` protocol from handle")
161-
};
155+
let loaded_image = this.open_protocol::<LoadedImage>(
156+
OpenProtocolParams {
157+
handle: image,
158+
agent: image,
159+
controller: None,
160+
},
161+
OpenProtocolAttributes::Exclusive,
162+
);
163+
164+
if loaded_image.is_err() {
165+
log::error!("Failed to open protocol SimpleFileSystem while attempting to load {name}");
166+
return None;
167+
}
168+
let loaded_image = loaded_image.unwrap();
162169
let loaded_image = unsafe { &*loaded_image.interface.get() };
163170

164171
let device_handle = loaded_image.device();
165172

166-
let device_path = unsafe { this
167-
.open_protocol::<DevicePath>(
168-
OpenProtocolParams {
169-
handle: device_handle,
170-
agent: image,
171-
controller: None,
172-
},
173-
OpenProtocolAttributes::Exclusive,
174-
)
175-
.expect("Failed to retrieve `DevicePath` protocol from image's device handle")
176-
};
173+
let device_path = this.open_protocol::<DevicePath>(
174+
OpenProtocolParams {
175+
handle: device_handle,
176+
agent: image,
177+
controller: None,
178+
},
179+
OpenProtocolAttributes::Exclusive,
180+
);
181+
if device_path.is_err() {
182+
log::error!("Failed to open protocol DevicePath while attempting to load {name}");
183+
return None;
184+
}
185+
let device_path = device_path.unwrap();
177186
let mut device_path = unsafe { &*device_path.interface.get() };
178187

179-
let fs_handle = this
180-
.locate_device_path::<SimpleFileSystem>(&mut device_path)
181-
.ok()?;
188+
let fs_handle = this.locate_device_path::<SimpleFileSystem>(&mut device_path);
189+
if fs_handle.is_err() {
190+
log::error!("Failed to open device path while attempting to load {name}");
191+
return None;
192+
}
193+
194+
let fs_handle = fs_handle.unwrap();
182195

183-
unsafe { this.open_protocol::<SimpleFileSystem>(
196+
let opened_handle = this.open_protocol::<SimpleFileSystem>(
184197
OpenProtocolParams {
185198
handle: fs_handle,
186199
agent: image,
187200
controller: None,
188201
},
189202
OpenProtocolAttributes::Exclusive,
190-
)
203+
);
204+
205+
if opened_handle.is_err() {
206+
log::error!("Failed to open file system while attempting to load {name}");
207+
return None;
191208
}
209+
Some(opened_handle.unwrap())
210+
};
211+
212+
if file_system_raw.is_none() {
213+
return None();
192214
}
193-
.unwrap();
215+
let file_system_raw = file_system_raw.unwrap();
194216
let file_system = unsafe { &mut *file_system_raw.interface.get() };
195217

196218
let mut root = file_system.open_volume().unwrap();
197-
if(name.len() > 255) {
219+
if (name.len() > 255) {
198220
panic!("File name {}, exceeds maximum length!", name);
199221
}
200222
let mut buf = [0; 512];
201223
let filename = CStr16::from_str_with_buf(name, &mut buf).unwrap();
202-
let kernel_file_handle_result = root
203-
.open(filename, FileMode::Read, FileAttribute::empty());
224+
let kernel_file_handle_result = root.open(filename, FileMode::Read, FileAttribute::empty());
204225

205226
if kernel_file_handle_result.is_err() {
206227
return None;
@@ -233,7 +254,7 @@ fn load_file_from_disk(name: &str, image: Handle, st: &SystemTable<Boot>) -> Opt
233254
}
234255

235256
fn load_kernel_file_from_disk(image: Handle, st: &SystemTable<Boot>) -> Option<&'static mut [u8]> {
236-
Some(load_file_from_disk(KERNEL_FILENAME, image, st).expect("Failed to load kernel (expected file named `kernel-x86_64`)"))
257+
load_file_from_disk(KERNEL_FILENAME, image, st)
237258
}
238259

239260
/// Try to load a kernel from a TFTP boot server.

0 commit comments

Comments
 (0)