@@ -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 ,
@@ -123,7 +123,7 @@ fn main_inner(image: Handle, mut st: SystemTable<Boot>) -> Status {
123
123
rsdp. map ( |entry| PhysAddr :: new ( entry. address as u64 ) )
124
124
} ,
125
125
ramdisk_addr : None ,
126
- ramdisk_len : 0
126
+ ramdisk_len : 0 ,
127
127
} ;
128
128
129
129
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
145
145
. or_else ( || load_kernel_file_from_tftp_boot_server ( image, st) )
146
146
}
147
147
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 ] > {
149
153
let file_system_raw = {
150
154
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 ( ) ;
162
169
let loaded_image = unsafe { & * loaded_image. interface . get ( ) } ;
163
170
164
171
let device_handle = loaded_image. device ( ) ;
165
172
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 ( ) ;
177
186
let mut device_path = unsafe { & * device_path. interface . get ( ) } ;
178
187
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 ( ) ;
182
195
183
- unsafe { this. open_protocol :: < SimpleFileSystem > (
196
+ let opened_handle = this. open_protocol :: < SimpleFileSystem > (
184
197
OpenProtocolParams {
185
198
handle : fs_handle,
186
199
agent : image,
187
200
controller : None ,
188
201
} ,
189
202
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 ;
191
208
}
209
+ Some ( opened_handle. unwrap ( ) )
210
+ } ;
211
+
212
+ if file_system_raw. is_none ( ) {
213
+ return None ( ) ;
192
214
}
193
- . unwrap ( ) ;
215
+ let file_system_raw = file_system_raw . unwrap ( ) ;
194
216
let file_system = unsafe { & mut * file_system_raw. interface . get ( ) } ;
195
217
196
218
let mut root = file_system. open_volume ( ) . unwrap ( ) ;
197
- if ( name. len ( ) > 255 ) {
219
+ if ( name. len ( ) > 255 ) {
198
220
panic ! ( "File name {}, exceeds maximum length!" , name) ;
199
221
}
200
222
let mut buf = [ 0 ; 512 ] ;
201
223
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 ( ) ) ;
204
225
205
226
if kernel_file_handle_result. is_err ( ) {
206
227
return None ;
@@ -233,7 +254,7 @@ fn load_file_from_disk(name: &str, image: Handle, st: &SystemTable<Boot>) -> Opt
233
254
}
234
255
235
256
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)
237
258
}
238
259
239
260
/// Try to load a kernel from a TFTP boot server.
0 commit comments