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