Skip to content

Commit 845d873

Browse files
committed
Prevent boot from failing early via panic, and allow net boot to attempt boot
1 parent e318de5 commit 845d873

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

uefi/src/main.rs

Lines changed: 58 additions & 34 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,
@@ -122,7 +122,7 @@ fn main_inner(image: Handle, mut st: SystemTable<Boot>) -> Status {
122122
rsdp.map(|entry| PhysAddr::new(entry.address as u64))
123123
},
124124
ramdisk_addr: None,
125-
ramdisk_len: 0
125+
ramdisk_len: 0,
126126
};
127127

128128
bootloader_x86_64_common::load_and_switch_to_kernel(
@@ -144,59 +144,83 @@ fn load_kernel_file(image: Handle, st: &SystemTable<Boot>) -> Option<&'static mu
144144
.or_else(|| load_kernel_file_from_tftp_boot_server(image, st))
145145
}
146146

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

162170
let device_handle = loaded_image.device();
163171

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

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

180-
this.open_protocol::<SimpleFileSystem>(
195+
let opened_handle = this.open_protocol::<SimpleFileSystem>(
181196
OpenProtocolParams {
182197
handle: fs_handle,
183198
agent: image,
184199
controller: None,
185200
},
186201
OpenProtocolAttributes::Exclusive,
187-
)
202+
);
203+
204+
if opened_handle.is_err() {
205+
log::error!("Failed to open file system while attempting to load {name}");
206+
return None;
207+
}
208+
Some(opened_handle.unwrap())
209+
};
210+
211+
if file_system_raw.is_none() {
212+
return None();
188213
}
189-
.unwrap();
214+
let file_system_raw = file_system_raw.unwrap();
190215
let file_system = unsafe { &mut *file_system_raw.interface.get() };
191216

192217
let mut root = file_system.open_volume().unwrap();
193-
if(name.len() > 255) {
218+
if (name.len() > 255) {
194219
panic!("File name {}, exceeds maximum length!", name);
195220
}
196221
let mut buf = [0; 512];
197222
let filename = CStr16::from_str_with_buf(name, &mut buf).unwrap();
198-
let kernel_file_handle_result = root
199-
.open(filename, FileMode::Read, FileAttribute::empty());
223+
let kernel_file_handle_result = root.open(filename, FileMode::Read, FileAttribute::empty());
200224

201225
if kernel_file_handle_result.is_err() {
202226
return None;
@@ -229,7 +253,7 @@ fn load_file_from_disk(name: &str, image: Handle, st: &SystemTable<Boot>) -> Opt
229253
}
230254

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

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

0 commit comments

Comments
 (0)