blob: 99606d9c4b836b3c1eef49c3bc1cd2a1836ca495 [file] [log] [blame]
Masahisa Kojima2e188442022-09-12 17:33:59 +09001# SPDX-License-Identifier: GPL-2.0+
2""" Unit test for UEFI menu-driven configuration
3"""
4
5import pytest
6import time
7
8@pytest.mark.boardspec('sandbox')
9@pytest.mark.buildconfigspec('cmd_eficonfig')
10@pytest.mark.buildconfigspec('cmd_bootefi_bootmgr')
11def test_efi_eficonfig(u_boot_console, efi_eficonfig_data):
12
13 def send_user_input_and_wait(user_str, expect_str):
14 time.sleep(0.1) # TODO: does not work correctly without sleep
15 u_boot_console.run_command(cmd=user_str, wait_for_prompt=False,
16 wait_for_echo=True, send_nl=False)
17 u_boot_console.run_command(cmd='\x0d', wait_for_prompt=False,
18 wait_for_echo=False, send_nl=False)
19 if expect_str is not None:
20 for i in expect_str:
21 u_boot_console.p.expect([i])
22
23 def press_up_down_enter_and_wait(up_count, down_count, enter, expect_str):
24 # press UP key
25 for i in range(up_count):
26 u_boot_console.run_command(cmd='\x1b\x5b\x41', wait_for_prompt=False,
27 wait_for_echo=False, send_nl=False)
28 # press DOWN key
29 for i in range(down_count):
30 u_boot_console.run_command(cmd='\x1b\x5b\x42', wait_for_prompt=False,
31 wait_for_echo=False, send_nl=False)
32 # press ENTER if requested
33 if enter:
34 u_boot_console.run_command(cmd='\x0d', wait_for_prompt=False,
35 wait_for_echo=False, send_nl=False)
36 # wait expected output
37 if expect_str is not None:
38 for i in expect_str:
39 u_boot_console.p.expect([i])
40
41 def press_escape_key(wait_prompt):
42 u_boot_console.run_command(cmd='\x1b', wait_for_prompt=wait_prompt, wait_for_echo=False, send_nl=False)
43
44 def press_enter_key(wait_prompt):
45 u_boot_console.run_command(cmd='\x0d', wait_for_prompt=wait_prompt,
46 wait_for_echo=False, send_nl=False)
47
48 def check_current_is_maintenance_menu():
49 for i in ('UEFI Maintenance Menu', 'Add Boot Option', 'Edit Boot Option',
50 'Change Boot Order', 'Delete Boot Option', 'Quit'):
51 u_boot_console.p.expect([i])
52
53 """ Unit test for "eficonfig" command
54 The menu-driven interface is used to set up UEFI load options.
55 The bootefi bootmgr loads initrddump.efi as a payload.
56 The crc32 of the loaded initrd.img is checked
57
58 Args:
59 u_boot_console -- U-Boot console
60 efi__data -- Path to the disk image used for testing.
61 Test disk image has following files.
62 initrd-1.img
63 initrd-2.img
64 initrddump.efi
65
66 """
67
68 # Restart the system to clean the previous state
69 u_boot_console.restart_uboot()
70
71 with u_boot_console.temporary_timeout(500):
72 #
73 # Test Case 1: Check the menu is displayed
74 #
75 u_boot_console.run_command('eficonfig', wait_for_prompt=False)
76 for i in ('UEFI Maintenance Menu', 'Add Boot Option', 'Edit Boot Option',
77 'Change Boot Order', 'Delete Boot Option', 'Quit'):
78 u_boot_console.p.expect([i])
79 # Select "Add Boot Option"
80 press_enter_key(False)
81 for i in ('Add Boot Option', 'Description:', 'File', 'Initrd File', 'Optional Data',
82 'Save', 'Quit'):
83 u_boot_console.p.expect([i])
84 press_escape_key(False)
85 check_current_is_maintenance_menu()
86 # return to U-Boot console
87 press_escape_key(True)
88
89 #
90 # Test Case 2: check auto generated media device entry
91 #
92
93 # bind the test disk image for succeeding tests
94 u_boot_console.run_command(cmd = f'host bind 0 {efi_eficonfig_data}')
95
96 u_boot_console.run_command('eficonfig', wait_for_prompt=False)
97
98 # Change the Boot Order
99 press_up_down_enter_and_wait(0, 2, True, 'Quit')
100 for i in ('host 0:1', 'Save', 'Quit'):
101 u_boot_console.p.expect([i])
102 # disable auto generated boot option for succeeding test
103 u_boot_console.run_command(cmd=' ', wait_for_prompt=False,
104 wait_for_echo=False, send_nl=False)
105 # Save the BootOrder
106 press_up_down_enter_and_wait(0, 1, True, None)
107 check_current_is_maintenance_menu()
108
109 #
110 # Test Case 3: Add first Boot Option and load it
111 #
112
113 # Select 'Add Boot Option'
114 press_up_down_enter_and_wait(0, 0, True, 'Quit')
115
116 # Press the enter key to select 'Description:' entry, then enter Description
117 press_up_down_enter_and_wait(0, 0, True, 'enter description:')
118 # Send Description user input, press ENTER key to complete
119 send_user_input_and_wait('test 1', 'Quit')
120
121 # Set EFI image(initrddump.efi)
122 press_up_down_enter_and_wait(0, 1, True, 'Quit')
123 press_up_down_enter_and_wait(0, 0, True, 'host 0:1')
124 # Select 'host 0:1'
125 press_up_down_enter_and_wait(0, 0, True, 'Quit')
126 # Press down key to select "initrddump.efi" entry followed by the enter key
127 press_up_down_enter_and_wait(0, 2, True, 'Quit')
128
129 # Set Initrd file(initrd-1.img)
130 press_up_down_enter_and_wait(0, 2, True, 'Quit')
131 press_up_down_enter_and_wait(0, 0, True, 'host 0:1')
132 # Select 'host 0:1'
133 press_up_down_enter_and_wait(0, 0, True, 'Quit')
134 # Press down key to select "initrd-1.img" entry followed by the enter key
135 press_up_down_enter_and_wait(0, 0, True, 'Quit')
136
137 # Set optional_data
138 press_up_down_enter_and_wait(0, 3, True, 'Optional Data:')
139 # Send Description user input, press ENTER key to complete
140 send_user_input_and_wait('nocolor', None)
141 for i in ('Description: test 1', 'File: host 0:1/initrddump.efi',
142 'Initrd File: host 0:1/initrd-1.img', 'Optional Data: nocolor', 'Save', 'Quit'):
143 u_boot_console.p.expect([i])
144
145 # Save the Boot Option
146 press_up_down_enter_and_wait(0, 4, True, None)
147 check_current_is_maintenance_menu()
148
149 # Check the newly added Boot Option is handled correctly
150 # Return to U-Boot console
151 press_escape_key(True)
152 u_boot_console.run_command(cmd = 'bootefi bootmgr')
153 response = u_boot_console.run_command(cmd = 'load', wait_for_echo=False)
154 assert 'crc32: 0x181464af' in response
155 u_boot_console.run_command(cmd = 'exit', wait_for_echo=False)
156
157 #
158 # Test Case 4: Add second Boot Option and load it
159 #
160 u_boot_console.run_command('eficonfig', wait_for_prompt=False)
161
162 # Select 'Add Boot Option'
163 press_up_down_enter_and_wait(0, 0, True, 'Quit')
164
165 # Press the enter key to select 'Description:' entry, then enter Description
166 press_up_down_enter_and_wait(0, 0, True, 'enter description:')
167 # Send Description user input, press ENTER key to complete
168 send_user_input_and_wait('test 2', 'Quit')
169
170 # Set EFI image(initrddump.efi)
171 press_up_down_enter_and_wait(0, 1, True, 'Quit')
172 press_up_down_enter_and_wait(0, 0, True, 'host 0:1')
173 # Select 'host 0:1'
174 press_up_down_enter_and_wait(0, 0, True, 'Quit')
175 # Press down key to select "initrddump.efi" entry followed by the enter key
176 press_up_down_enter_and_wait(0, 2, True, 'Quit')
177
178 # Set Initrd file(initrd-2.img)
179 press_up_down_enter_and_wait(0, 2, True, 'Quit')
180 press_up_down_enter_and_wait(0, 0, True, 'host 0:1')
181 # Select 'host 0:1'
182 press_up_down_enter_and_wait(0, 0, True, 'Quit')
183 # Press down key to select "initrd-2.img" entry followed by the enter key
184 press_up_down_enter_and_wait(0, 1, True, 'Quit')
185
186 # Set optional_data
187 press_up_down_enter_and_wait(0, 3, True, 'Optional Data:')
188 # Send Description user input, press ENTER key to complete
189 send_user_input_and_wait('nocolor', None)
190 for i in ('Description: test 2', 'File: host 0:1/initrddump.efi',
191 'Initrd File: host 0:1/initrd-2.img', 'Optional Data: nocolor', 'Save', 'Quit'):
192 u_boot_console.p.expect([i])
193
194 # Save the Boot Option
195 press_up_down_enter_and_wait(0, 4, True, 'Quit')
196
197 # Change the Boot Order
198 press_up_down_enter_and_wait(0, 2, True, 'Quit')
199 press_up_down_enter_and_wait(0, 1, False, 'Quit')
200 # move 'test 1' to the second entry
201 u_boot_console.run_command(cmd='+', wait_for_prompt=False,
202 wait_for_echo=False, send_nl=False)
203 for i in ('test 2', 'test 1', 'host 0:1', 'Save', 'Quit'):
204 u_boot_console.p.expect([i])
205 # Save the BootOrder
206 press_up_down_enter_and_wait(0, 3, True, None)
207 check_current_is_maintenance_menu()
208
209 # Check the newly added Boot Option is handled correctly
210 # Return to U-Boot console
211 press_escape_key(True)
212 u_boot_console.run_command(cmd = 'bootefi bootmgr')
213 response = u_boot_console.run_command(cmd = 'load', wait_for_echo=False)
214 assert 'crc32: 0x811d3515' in response
215 u_boot_console.run_command(cmd = 'exit', wait_for_echo=False)
216
217 #
218 # Test Case 5: Change BootOrder and load it
219 #
220 u_boot_console.run_command('eficonfig', wait_for_prompt=False)
221
222 # Change the Boot Order
223 press_up_down_enter_and_wait(0, 2, True, None)
224 # Check the curren BootOrder
225 for i in ('test 2', 'test 1', 'host 0:1', 'Save', 'Quit'):
226 u_boot_console.p.expect([i])
227 # move 'test 2' to the second entry
228 u_boot_console.run_command(cmd='-', wait_for_prompt=False,
229 wait_for_echo=False, send_nl=False)
230 for i in ('test 1', 'test 2', 'host 0:1', 'Save', 'Quit'):
231 u_boot_console.p.expect([i])
232 # Save the BootOrder
233 press_up_down_enter_and_wait(0, 2, True, None)
234 check_current_is_maintenance_menu()
235
236 # Return to U-Boot console
237 press_escape_key(True)
238 u_boot_console.run_command(cmd = 'bootefi bootmgr')
239 response = u_boot_console.run_command(cmd = 'load', wait_for_echo=False)
240 assert 'crc32: 0x181464af' in response
241 u_boot_console.run_command(cmd = 'exit', wait_for_echo=False)
242
243 #
244 # Test Case 6: Delete Boot Option(label:test 2)
245 #
246 u_boot_console.run_command('eficonfig', wait_for_prompt=False)
247
248 # Select 'Delete Boot Option'
249 press_up_down_enter_and_wait(0, 3, True, None)
250 # Check the current BootOrder
251 for i in ('test 1', 'test 2', 'Quit'):
252 u_boot_console.p.expect([i])
253
254 # Delete 'test 2'
255 press_up_down_enter_and_wait(0, 1, True, None)
256 for i in ('test 1', 'Quit'):
257 u_boot_console.p.expect([i])
258 press_escape_key(False)
259 check_current_is_maintenance_menu()
260 # Return to U-Boot console
261 press_escape_key(True)
262
263 #
264 # Test Case 7: Edit Boot Option
265 #
266 u_boot_console.run_command('eficonfig', wait_for_prompt=False)
267 # Select 'Edit Boot Option'
268 press_up_down_enter_and_wait(0, 1, True, None)
269 # Check the curren BootOrder
270 for i in ('test 1', 'Quit'):
271 u_boot_console.p.expect([i])
272 press_up_down_enter_and_wait(0, 0, True, None)
273 for i in ('Description: test 1', 'File: host 0:1/initrddump.efi',
274 'Initrd File: host 0:1/initrd-1.img', 'Optional Data: nocolor', 'Save', 'Quit'):
275 u_boot_console.p.expect([i])
276
277 # Press the enter key to select 'Description:' entry, then enter Description
278 press_up_down_enter_and_wait(0, 0, True, 'enter description:')
279 # Send Description user input, press ENTER key to complete
280 send_user_input_and_wait('test 3', 'Quit')
281
282 # Set EFI image(initrddump.efi)
283 press_up_down_enter_and_wait(0, 1, True, 'Quit')
284 press_up_down_enter_and_wait(0, 0, True, 'host 0:1')
285 # Select 'host 0:1'
286 press_up_down_enter_and_wait(0, 0, True, 'Quit')
287 # Press down key to select "initrddump.efi" entry followed by the enter key
288 press_up_down_enter_and_wait(0, 2, True, 'Quit')
289
290 # Set Initrd file(initrd-2.img)
291 press_up_down_enter_and_wait(0, 2, True, 'Quit')
292 press_up_down_enter_and_wait(0, 0, True, 'host 0:1')
293 # Select 'host 0:1'
294 press_up_down_enter_and_wait(0, 0, True, 'Quit')
295 # Press down key to select "initrd-1.img" entry followed by the enter key
296 press_up_down_enter_and_wait(0, 1, True, 'Quit')
297
298 # Set optional_data
299 press_up_down_enter_and_wait(0, 3, True, 'Optional Data:')
300 # Send Description user input, press ENTER key to complete
301 send_user_input_and_wait('', None)
302 for i in ('Description: test 3', 'File: host 0:1/initrddump.efi',
303 'Initrd File: host 0:1/initrd-2.img', 'Optional Data:', 'Save', 'Quit'):
304 u_boot_console.p.expect([i])
305
306 # Save the Boot Option
307 press_up_down_enter_and_wait(0, 4, True, 'Quit')
308 press_escape_key(False)
309 check_current_is_maintenance_menu()
310
311 # Check the updated Boot Option is handled correctly
312 # Return to U-Boot console
313 press_escape_key(True)
314 u_boot_console.run_command(cmd = 'bootefi bootmgr')
315 response = u_boot_console.run_command(cmd = 'load', wait_for_echo=False)
316 assert 'crc32: 0x811d3515' in response
317 u_boot_console.run_command(cmd = 'exit', wait_for_echo=False)
318
319 #
320 # Test Case 8: Delete Boot Option(label:test 3)
321 #
322 u_boot_console.run_command('eficonfig', wait_for_prompt=False)
323
324 # Select 'Delete Boot Option'
325 press_up_down_enter_and_wait(0, 3, True, None)
326 # Check the curren BootOrder
327 for i in ('test 3', 'Quit'):
328 u_boot_console.p.expect([i])
329
330 # Delete 'test 3'
331 press_up_down_enter_and_wait(0, 0, True, 'Quit')
332 press_escape_key(False)
333 check_current_is_maintenance_menu()
334 # Return to U-Boot console
335 press_escape_key(True)
336
337 # remove the host device
338 u_boot_console.run_command(cmd = f'host bind -r 0')
339
340 #
341 # Test Case 9: No block device found
342 #
343 u_boot_console.run_command('eficonfig', wait_for_prompt=False)
344
345 # Select 'Add Boot Option'
346 press_up_down_enter_and_wait(0, 0, True, 'Quit')
347
348 # Set EFI image
349 press_up_down_enter_and_wait(0, 1, True, 'Quit')
350 press_up_down_enter_and_wait(0, 0, True, 'No block device found!')
351 press_escape_key(False)
352 check_current_is_maintenance_menu()
353 # Return to U-Boot console
354 press_escape_key(True)