@@ -13,8 +13,8 @@ use uefi::{
13
13
prelude:: { entry, Boot , Handle , Status , SystemTable } ,
14
14
proto:: {
15
15
console:: gop:: { GraphicsOutput , PixelFormat } ,
16
- device_path:: DevicePath ,
17
- loaded_image:: LoadedImage ,
16
+ device_path:: { self , DevicePath } ,
17
+ loaded_image:: { self , LoadedImage } ,
18
18
media:: {
19
19
file:: { File , FileAttribute , FileInfo , FileMode } ,
20
20
fs:: SimpleFileSystem ,
@@ -122,7 +122,7 @@ fn main_inner(image: Handle, mut st: SystemTable<Boot>) -> Status {
122
122
rsdp. map ( |entry| PhysAddr :: new ( entry. address as u64 ) )
123
123
} ,
124
124
ramdisk_addr : None ,
125
- ramdisk_len : 0
125
+ ramdisk_len : 0 ,
126
126
} ;
127
127
128
128
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
144
144
. or_else ( || load_kernel_file_from_tftp_boot_server ( image, st) )
145
145
}
146
146
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 ] > {
148
152
let file_system_raw = {
149
153
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 ( ) ;
160
168
let loaded_image = unsafe { & * loaded_image. interface . get ( ) } ;
161
169
162
170
let device_handle = loaded_image. device ( ) ;
163
171
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 ( ) ;
174
185
let mut device_path = unsafe { & * device_path. interface . get ( ) } ;
175
186
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 ( ) ;
179
194
180
- this. open_protocol :: < SimpleFileSystem > (
195
+ let opened_handle = this. open_protocol :: < SimpleFileSystem > (
181
196
OpenProtocolParams {
182
197
handle : fs_handle,
183
198
agent : image,
184
199
controller : None ,
185
200
} ,
186
201
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 ( ) ;
188
213
}
189
- . unwrap ( ) ;
214
+ let file_system_raw = file_system_raw . unwrap ( ) ;
190
215
let file_system = unsafe { & mut * file_system_raw. interface . get ( ) } ;
191
216
192
217
let mut root = file_system. open_volume ( ) . unwrap ( ) ;
193
- if ( name. len ( ) > 255 ) {
218
+ if ( name. len ( ) > 255 ) {
194
219
panic ! ( "File name {}, exceeds maximum length!" , name) ;
195
220
}
196
221
let mut buf = [ 0 ; 512 ] ;
197
222
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 ( ) ) ;
200
224
201
225
if kernel_file_handle_result. is_err ( ) {
202
226
return None ;
@@ -229,7 +253,7 @@ fn load_file_from_disk(name: &str, image: Handle, st: &SystemTable<Boot>) -> Opt
229
253
}
230
254
231
255
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)
233
257
}
234
258
235
259
/// Try to load a kernel from a TFTP boot server.
0 commit comments