@@ -4,7 +4,14 @@ use super::Header;
4
4
use crate :: data_types:: Align ;
5
5
use crate :: proto:: { device_path:: DevicePath , Protocol } ;
6
6
#[ cfg( feature = "exts" ) ]
7
- use crate :: proto:: { loaded_image:: LoadedImage , media:: fs:: SimpleFileSystem } ;
7
+ use crate :: {
8
+ proto:: {
9
+ device_path:: { DeviceSubType , DeviceType } ,
10
+ loaded_image:: LoadedImage ,
11
+ media:: fs:: SimpleFileSystem ,
12
+ } ,
13
+ CStr16 ,
14
+ } ;
8
15
use crate :: { Char16 , Event , Guid , Handle , Result , Status } ;
9
16
#[ cfg( feature = "exts" ) ]
10
17
use alloc_api:: vec:: Vec ;
@@ -993,6 +1000,41 @@ impl BootServices {
993
1000
OpenProtocolAttributes :: Exclusive ,
994
1001
)
995
1002
}
1003
+
1004
+ /// Get a NULL-terminated Path string the given image was loaded from.
1005
+ ///
1006
+ /// Note the path string will be empty if the image was loaded from a
1007
+ /// buffer instead of a path, and the path wasn't set.
1008
+ pub fn get_image_file_path ( & self , image_handle : Handle ) -> Result < & CStr16 > {
1009
+ let loaded_image = self . open_protocol :: < LoadedImage > (
1010
+ OpenProtocolParams {
1011
+ handle : image_handle,
1012
+ agent : image_handle,
1013
+ controller : None ,
1014
+ } ,
1015
+ OpenProtocolAttributes :: Exclusive ,
1016
+ ) ?;
1017
+ let loaded_image = unsafe { & * loaded_image. interface . get ( ) } ;
1018
+
1019
+ // If the file_path is None, it means the operation isn't supported.
1020
+ let file_path = loaded_image. file_path ( ) . ok_or ( Status :: UNSUPPORTED ) ?;
1021
+
1022
+ // check if the type of `file_path` is correct
1023
+ if let ( DeviceType :: MEDIA , DeviceSubType :: MEDIA_FILE_PATH ) =
1024
+ ( file_path. device_type ( ) , file_path. sub_type ( ) )
1025
+ {
1026
+ let file_path = file_path as * const DevicePath ;
1027
+
1028
+ unsafe {
1029
+ // path name follows by the `DevicePathHeader`
1030
+ let path_name = file_path. offset ( 1 ) . cast ( ) ;
1031
+ Ok ( CStr16 :: from_ptr ( path_name) )
1032
+ }
1033
+ } else {
1034
+ // incorrect type, return `UNSUPPORTED`
1035
+ Err ( Status :: UNSUPPORTED . into ( ) )
1036
+ }
1037
+ }
996
1038
}
997
1039
998
1040
impl super :: Table for BootServices {
0 commit comments