@@ -4,51 +4,49 @@ use core::ptr::{self, NonNull};
4
4
use core:: mem;
5
5
use uefi:: proto:: unsafe_protocol;
6
6
use uefi:: table:: boot:: {
7
- BootServices , EventType , MemoryType , OpenProtocolAttributes , OpenProtocolParams , SearchType ,
8
- TimerTrigger , Tpl ,
7
+ EventType , MemoryType , OpenProtocolAttributes , OpenProtocolParams , SearchType , TimerTrigger ,
8
+ Tpl ,
9
9
} ;
10
- use uefi:: table :: { Boot , SystemTable } ;
10
+ use uefi:: { boot , system } ;
11
11
use uefi:: { guid, Event , Guid , Identify } ;
12
12
13
- pub fn test ( st : & SystemTable < Boot > ) {
14
- let bt = st. boot_services ( ) ;
13
+ pub fn test ( ) {
15
14
info ! ( "Testing timer..." ) ;
16
- test_timer ( bt ) ;
15
+ test_timer ( ) ;
17
16
info ! ( "Testing events..." ) ;
18
- test_event_callback ( bt ) ;
19
- test_callback_with_ctx ( bt ) ;
17
+ test_event_callback ( ) ;
18
+ test_callback_with_ctx ( ) ;
20
19
info ! ( "Testing watchdog..." ) ;
21
- test_watchdog ( bt ) ;
20
+ test_watchdog ( ) ;
22
21
info ! ( "Testing protocol handler services..." ) ;
23
- test_register_protocol_notify ( bt ) ;
24
- test_install_protocol_interface ( bt ) ;
25
- test_reinstall_protocol_interface ( bt ) ;
26
- test_uninstall_protocol_interface ( bt ) ;
27
- test_install_configuration_table ( st ) ;
22
+ test_register_protocol_notify ( ) ;
23
+ test_install_protocol_interface ( ) ;
24
+ test_reinstall_protocol_interface ( ) ;
25
+ test_uninstall_protocol_interface ( ) ;
26
+ test_install_configuration_table ( ) ;
28
27
}
29
28
30
- fn test_timer ( bt : & BootServices ) {
31
- let timer_event = unsafe { bt . create_event ( EventType :: TIMER , Tpl :: APPLICATION , None , None ) }
29
+ fn test_timer ( ) {
30
+ let timer_event = unsafe { boot :: create_event ( EventType :: TIMER , Tpl :: APPLICATION , None , None ) }
32
31
. expect ( "Failed to create TIMER event" ) ;
33
32
let mut events = unsafe { [ timer_event. unsafe_clone ( ) ] } ;
34
- bt . set_timer ( & timer_event, TimerTrigger :: Relative ( 5_0 /*00 ns */ ) )
33
+ boot :: set_timer ( & timer_event, TimerTrigger :: Relative ( 5_0 /*00 ns */ ) )
35
34
. expect ( "Failed to set timer" ) ;
36
- bt. wait_for_event ( & mut events)
37
- . expect ( "Wait for event failed" ) ;
35
+ boot:: wait_for_event ( & mut events) . expect ( "Wait for event failed" ) ;
38
36
}
39
37
40
- fn test_event_callback ( bt : & BootServices ) {
38
+ fn test_event_callback ( ) {
41
39
extern "efiapi" fn callback ( _event : Event , _ctx : Option < NonNull < c_void > > ) {
42
40
info ! ( "Inside the event callback" ) ;
43
41
}
44
42
45
43
let event =
46
- unsafe { bt . create_event ( EventType :: NOTIFY_WAIT , Tpl :: CALLBACK , Some ( callback) , None ) }
44
+ unsafe { boot :: create_event ( EventType :: NOTIFY_WAIT , Tpl :: CALLBACK , Some ( callback) , None ) }
47
45
. expect ( "Failed to create custom event" ) ;
48
- bt . check_event ( event) . expect ( "Failed to check event" ) ;
46
+ boot :: check_event ( event) . expect ( "Failed to check event" ) ;
49
47
}
50
48
51
- fn test_callback_with_ctx ( bt : & BootServices ) {
49
+ fn test_callback_with_ctx ( ) {
52
50
let mut data = 123u32 ;
53
51
54
52
extern "efiapi" fn callback ( _event : Event , ctx : Option < NonNull < c_void > > ) {
@@ -65,7 +63,7 @@ fn test_callback_with_ctx(bt: &BootServices) {
65
63
let ctx = NonNull :: new ( ctx. cast :: < c_void > ( ) ) . unwrap ( ) ;
66
64
67
65
let event = unsafe {
68
- bt . create_event (
66
+ boot :: create_event (
69
67
EventType :: NOTIFY_WAIT ,
70
68
Tpl :: CALLBACK ,
71
69
Some ( callback) ,
@@ -74,16 +72,15 @@ fn test_callback_with_ctx(bt: &BootServices) {
74
72
. expect ( "Failed to create event with context" )
75
73
} ;
76
74
77
- bt . check_event ( event) . expect ( "Failed to check event" ) ;
75
+ boot :: check_event ( event) . expect ( "Failed to check event" ) ;
78
76
79
77
// Check that `data` was updated inside the event callback.
80
78
assert_eq ! ( data, 456 ) ;
81
79
}
82
80
83
- fn test_watchdog ( bt : & BootServices ) {
81
+ fn test_watchdog ( ) {
84
82
// Disable the UEFI watchdog timer
85
- bt. set_watchdog_timer ( 0 , 0x10000 , None )
86
- . expect ( "Could not set watchdog timer" ) ;
83
+ boot:: set_watchdog_timer ( 0 , 0x10000 , None ) . expect ( "Could not set watchdog timer" ) ;
87
84
}
88
85
89
86
/// Dummy protocol for tests
@@ -96,10 +93,10 @@ unsafe extern "efiapi" fn _test_notify(_event: Event, _context: Option<NonNull<c
96
93
info ! ( "Protocol was (re)installed and this function notified." )
97
94
}
98
95
99
- fn test_register_protocol_notify ( bt : & BootServices ) {
96
+ fn test_register_protocol_notify ( ) {
100
97
let protocol = & TestProtocol :: GUID ;
101
98
let event = unsafe {
102
- bt . create_event (
99
+ boot :: create_event (
103
100
EventType :: NOTIFY_SIGNAL ,
104
101
Tpl :: NOTIFY ,
105
102
Some ( _test_notify) ,
@@ -108,40 +105,36 @@ fn test_register_protocol_notify(bt: &BootServices) {
108
105
. expect ( "Failed to create an event" )
109
106
} ;
110
107
111
- bt. register_protocol_notify ( protocol, event)
112
- . expect ( "Failed to register protocol notify fn" ) ;
108
+ boot:: register_protocol_notify ( protocol, event) . expect ( "Failed to register protocol notify fn" ) ;
113
109
}
114
110
115
- fn test_install_protocol_interface ( bt : & BootServices ) {
111
+ fn test_install_protocol_interface ( ) {
116
112
info ! ( "Installing TestProtocol" ) ;
117
113
118
- let alloc: * mut TestProtocol = bt
119
- . allocate_pool (
120
- MemoryType :: BOOT_SERVICES_DATA ,
121
- mem:: size_of :: < TestProtocol > ( ) ,
122
- )
123
- . unwrap ( )
124
- . cast ( ) ;
114
+ let alloc: * mut TestProtocol = boot:: allocate_pool (
115
+ MemoryType :: BOOT_SERVICES_DATA ,
116
+ mem:: size_of :: < TestProtocol > ( ) ,
117
+ )
118
+ . unwrap ( )
119
+ . cast ( ) ;
125
120
unsafe { alloc. write ( TestProtocol { data : 123 } ) } ;
126
121
127
122
let _ = unsafe {
128
- bt . install_protocol_interface ( None , & TestProtocol :: GUID , alloc. cast ( ) )
123
+ boot :: install_protocol_interface ( None , & TestProtocol :: GUID , alloc. cast ( ) )
129
124
. expect ( "Failed to install protocol interface" )
130
125
} ;
131
126
132
- let _ = bt
133
- . locate_handle_buffer ( SearchType :: from_proto :: < TestProtocol > ( ) )
127
+ let _ = boot:: locate_handle_buffer ( SearchType :: from_proto :: < TestProtocol > ( ) )
134
128
. expect ( "Failed to find protocol after it was installed" ) ;
135
129
}
136
130
137
- fn test_reinstall_protocol_interface ( bt : & BootServices ) {
131
+ fn test_reinstall_protocol_interface ( ) {
138
132
info ! ( "Reinstalling TestProtocol" ) ;
139
- let handle = bt
140
- . locate_handle_buffer ( SearchType :: from_proto :: < TestProtocol > ( ) )
133
+ let handle = boot:: locate_handle_buffer ( SearchType :: from_proto :: < TestProtocol > ( ) )
141
134
. expect ( "Failed to find protocol to uninstall" ) [ 0 ] ;
142
135
143
136
unsafe {
144
- let _ = bt . reinstall_protocol_interface (
137
+ let _ = boot :: reinstall_protocol_interface (
145
138
handle,
146
139
& TestProtocol :: GUID ,
147
140
ptr:: null_mut ( ) ,
@@ -150,60 +143,56 @@ fn test_reinstall_protocol_interface(bt: &BootServices) {
150
143
}
151
144
}
152
145
153
- fn test_uninstall_protocol_interface ( bt : & BootServices ) {
146
+ fn test_uninstall_protocol_interface ( ) {
154
147
info ! ( "Uninstalling TestProtocol" ) ;
155
148
156
- let handle = bt
157
- . locate_handle_buffer ( SearchType :: from_proto :: < TestProtocol > ( ) )
149
+ let handle = boot:: locate_handle_buffer ( SearchType :: from_proto :: < TestProtocol > ( ) )
158
150
. expect ( "Failed to find protocol to uninstall" ) [ 0 ] ;
159
151
160
152
unsafe {
161
153
// Uninstalling a protocol interface requires knowing the interface
162
154
// pointer. Open the protocol to get that pointer, making sure to drop
163
155
// the `ScopedProtocol` _before_ uninstalling the protocol interface.
164
156
let interface_ptr: * mut TestProtocol = {
165
- let mut sp = bt
166
- . open_protocol :: < TestProtocol > (
167
- OpenProtocolParams {
168
- handle,
169
- agent : bt. image_handle ( ) ,
170
- controller : None ,
171
- } ,
172
- OpenProtocolAttributes :: GetProtocol ,
173
- )
174
- . unwrap ( ) ;
157
+ let mut sp = boot:: open_protocol :: < TestProtocol > (
158
+ OpenProtocolParams {
159
+ handle,
160
+ agent : boot:: image_handle ( ) ,
161
+ controller : None ,
162
+ } ,
163
+ OpenProtocolAttributes :: GetProtocol ,
164
+ )
165
+ . unwrap ( ) ;
175
166
assert_eq ! ( sp. data, 123 ) ;
176
167
& mut * sp
177
168
} ;
178
169
179
- bt . uninstall_protocol_interface ( handle, & TestProtocol :: GUID , interface_ptr. cast ( ) )
170
+ boot :: uninstall_protocol_interface ( handle, & TestProtocol :: GUID , interface_ptr. cast ( ) )
180
171
. expect ( "Failed to uninstall protocol interface" ) ;
181
172
182
- bt . free_pool ( interface_ptr. cast ( ) ) . unwrap ( ) ;
173
+ boot :: free_pool ( interface_ptr. cast ( ) ) . unwrap ( ) ;
183
174
}
184
175
}
185
176
186
- fn test_install_configuration_table ( st : & SystemTable < Boot > ) {
187
- let config = st
188
- . boot_services ( )
189
- . allocate_pool ( MemoryType :: ACPI_RECLAIM , 1 )
190
- . expect ( "Failed to allocate config table" ) ;
177
+ fn test_install_configuration_table ( ) {
178
+ let config =
179
+ boot:: allocate_pool ( MemoryType :: ACPI_RECLAIM , 1 ) . expect ( "Failed to allocate config table" ) ;
191
180
unsafe { config. write ( 42 ) } ;
192
181
193
- let count = st . config_table ( ) . len ( ) ;
182
+ let count = system :: with_config_table ( |t| t . len ( ) ) ;
194
183
const ID : Guid = guid ! ( "3bdb3089-5662-42df-840e-3922ed6467c9" ) ;
195
184
196
185
unsafe {
197
- st. boot_services ( )
198
- . install_configuration_table ( & ID , config. cast ( ) )
186
+ boot:: install_configuration_table ( & ID , config. cast ( ) )
199
187
. expect ( "Failed to install configuration table" ) ;
200
188
}
201
189
202
- assert_eq ! ( count + 1 , st. config_table( ) . len( ) ) ;
203
- let config_entry = st
204
- . config_table ( )
205
- . iter ( )
206
- . find ( |ct| ct. guid == ID )
207
- . expect ( "Failed to find test config table" ) ;
208
- assert_eq ! ( unsafe { * ( config_entry. address as * const u8 ) } , 42 ) ;
190
+ assert_eq ! ( count + 1 , system:: with_config_table( |t| t. len( ) ) ) ;
191
+ let entry_addr = system:: with_config_table ( |t| {
192
+ t. iter ( )
193
+ . find ( |ct| ct. guid == ID )
194
+ . expect ( "Failed to find test config table" )
195
+ . address
196
+ } ) ;
197
+ assert_eq ! ( unsafe { * ( entry_addr as * const u8 ) } , 42 ) ;
209
198
}
0 commit comments