1010
1111use crate :: proto:: device_path:: { DevicePath , DevicePathNode } ;
1212use crate :: proto:: unsafe_protocol;
13- use crate :: { boot, CStr16 , Char16 , Result , Status } ;
13+ use crate :: table:: boot:: BootServices ;
14+ use crate :: { CStr16 , Char16 , Result , Status } ;
1415use core:: ops:: Deref ;
15- use core:: ptr:: NonNull ;
1616use uefi_raw:: protocol:: device_path:: { DevicePathFromTextProtocol , DevicePathToTextProtocol } ;
1717
1818/// This struct is a wrapper of `display_only` parameter
@@ -42,27 +42,36 @@ pub struct AllowShortcuts(pub bool);
4242/// Wrapper for a string internally allocated from
4343/// UEFI boot services memory.
4444#[ derive( Debug ) ]
45- pub struct PoolString ( NonNull < Char16 > ) ;
45+ pub struct PoolString < ' a > {
46+ boot_services : & ' a BootServices ,
47+ text : * const Char16 ,
48+ }
4649
47- impl PoolString {
48- fn new ( text : * const Char16 ) -> Result < Self > {
49- NonNull :: new ( text. cast_mut ( ) )
50- . map ( Self )
51- . ok_or ( Status :: OUT_OF_RESOURCES . into ( ) )
50+ impl < ' a > PoolString < ' a > {
51+ fn new ( boot_services : & ' a BootServices , text : * const Char16 ) -> Result < Self > {
52+ if text. is_null ( ) {
53+ Err ( Status :: OUT_OF_RESOURCES . into ( ) )
54+ } else {
55+ Ok ( Self {
56+ boot_services,
57+ text,
58+ } )
59+ }
5260 }
5361}
5462
55- impl Deref for PoolString {
63+ impl < ' a > Deref for PoolString < ' a > {
5664 type Target = CStr16 ;
5765
5866 fn deref ( & self ) -> & Self :: Target {
59- unsafe { CStr16 :: from_ptr ( self . 0 . as_ptr ( ) ) }
67+ unsafe { CStr16 :: from_ptr ( self . text ) }
6068 }
6169}
6270
63- impl Drop for PoolString {
71+ impl Drop for PoolString < ' _ > {
6472 fn drop ( & mut self ) {
65- unsafe { boot:: free_pool ( self . 0 . cast ( ) ) } . expect ( "Failed to free pool [{addr:#?}]" ) ;
73+ let addr = self . text as * mut u8 ;
74+ unsafe { self . boot_services . free_pool ( addr) } . expect ( "Failed to free pool [{addr:#?}]" ) ;
6675 }
6776}
6877
@@ -82,20 +91,21 @@ impl DevicePathToText {
8291 /// memory for the conversion.
8392 ///
8493 /// [`OUT_OF_RESOURCES`]: Status::OUT_OF_RESOURCES
85- pub fn convert_device_node_to_text (
94+ pub fn convert_device_node_to_text < ' boot > (
8695 & self ,
96+ boot_services : & ' boot BootServices ,
8797 device_node : & DevicePathNode ,
8898 display_only : DisplayOnly ,
8999 allow_shortcuts : AllowShortcuts ,
90- ) -> Result < PoolString > {
100+ ) -> Result < PoolString < ' boot > > {
91101 let text_device_node = unsafe {
92102 ( self . 0 . convert_device_node_to_text ) (
93103 device_node. as_ffi_ptr ( ) . cast ( ) ,
94104 display_only. 0 ,
95105 allow_shortcuts. 0 ,
96106 )
97107 } ;
98- PoolString :: new ( text_device_node. cast ( ) )
108+ PoolString :: new ( boot_services , text_device_node. cast ( ) )
99109 }
100110
101111 /// Convert a device path to its text representation.
@@ -104,20 +114,21 @@ impl DevicePathToText {
104114 /// memory for the conversion.
105115 ///
106116 /// [`OUT_OF_RESOURCES`]: Status::OUT_OF_RESOURCES
107- pub fn convert_device_path_to_text (
117+ pub fn convert_device_path_to_text < ' boot > (
108118 & self ,
119+ boot_services : & ' boot BootServices ,
109120 device_path : & DevicePath ,
110121 display_only : DisplayOnly ,
111122 allow_shortcuts : AllowShortcuts ,
112- ) -> Result < PoolString > {
123+ ) -> Result < PoolString < ' boot > > {
113124 let text_device_path = unsafe {
114125 ( self . 0 . convert_device_path_to_text ) (
115126 device_path. as_ffi_ptr ( ) . cast ( ) ,
116127 display_only. 0 ,
117128 allow_shortcuts. 0 ,
118129 )
119130 } ;
120- PoolString :: new ( text_device_path. cast ( ) )
131+ PoolString :: new ( boot_services , text_device_path. cast ( ) )
121132 }
122133}
123134
0 commit comments