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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | # SPDX-License-Identifier: GPL-2.0+ # Copyright (c) 2013, Google Inc. """Sanity check of the FIT handling in U-Boot""" import os import struct import pytest import fit_util import utils # Define a base ITS which we can adjust using % and a dictionary BASE_ITS = ''' /dts-v1/; / { description = "Chrome OS kernel image with one or more FDT blobs"; #address-cells = <1>; images { kernel-1 { data = /incbin/("%(kernel)s"); type = "kernel"; arch = "sandbox"; os = "linux"; compression = "%(compression)s"; load = <0x40000>; entry = <0x8>; }; kernel-2 { data = /incbin/("%(loadables1)s"); type = "kernel"; arch = "sandbox"; os = "linux"; compression = "none"; %(loadables1_load)s entry = <0x0>; }; fdt-1 { description = "snow"; data = /incbin/("%(fdt)s"); type = "flat_dt"; arch = "sandbox"; %(fdt_load)s compression = "%(compression)s"; signature-1 { algo = "sha1,rsa2048"; key-name-hint = "dev"; }; }; ramdisk-1 { description = "snow"; data = /incbin/("%(ramdisk)s"); type = "ramdisk"; arch = "sandbox"; os = "linux"; %(ramdisk_load)s compression = "%(compression)s"; }; ramdisk-2 { description = "snow"; data = /incbin/("%(loadables2)s"); type = "ramdisk"; arch = "sandbox"; os = "linux"; %(loadables2_load)s compression = "none"; }; }; configurations { default = "conf-1"; conf-1 { %(kernel_config)s %(fdt_config)s %(ramdisk_config)s %(loadables_config)s }; }; }; ''' # Define a base FDT - currently we don't use anything in this BASE_FDT = ''' /dts-v1/; / { #address-cells = <1>; #size-cells = <0>; model = "Sandbox Verified Boot Test"; compatible = "sandbox"; binman { }; reset@0 { compatible = "sandbox,reset"; reg = <0>; }; }; ''' # This is the U-Boot script that is run for each test. First load the FIT, # then run the 'bootm' command, then save out memory from the places where # we expect 'bootm' to write things. Then quit. BASE_SCRIPT = ''' %(mem_clear)s host load hostfs 0 %(fit_addr)x %(fit)s fdt addr %(fit_addr)x bootm %(start_subcmd)s %(fit_addr)x bootm loados%(cmd_extra)s host save hostfs 0 %(kernel_addr)x %(kernel_out)s %(kernel_size)x host save hostfs 0 %(fdt_addr)x %(fdt_out)s %(fdt_size)x host save hostfs 0 %(ramdisk_addr)x %(ramdisk_out)s %(ramdisk_size)x host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x host save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x ''' @pytest.mark.boardspec('sandbox') @pytest.mark.buildconfigspec('fit_signature') @pytest.mark.requiredtool('dtc') class TestFitImage: """Test class for FIT image handling in U-Boot TODO: Almost everything: - hash algorithms - invalid hash/contents should be detected - signature algorithms - invalid sig/contents should be detected - compression - checking that errors are detected like: - image overwriting - missing images - invalid configurations - incorrect os/arch/type fields - empty data - images too large/small - invalid FDT (e.g. putting a random binary in instead) - default configuration selection - bootm command line parameters should have desired effect - run code coverage to make sure we are testing all the code """ @pytest.fixture(autouse=True) def save_bootstage(self, ubman): """Save and restore bootstage around each test.""" with utils.preserve_bootstage(ubman): yield def make_fname(self, ubman, leaf): """Make a temporary filename Args: ubman (ConsoleBase): U-Boot fixture leaf (str): Leaf name of file to create (within temporary directory) Return: str: Temporary filename """ return os.path.join(ubman.config.build_dir, leaf) def filesize(self, fname): """Get the size of a file Args: fname (str): Filename to check Return: int: Size of file in bytes """ return os.stat(fname).st_size def read_file(self, fname): """Read the contents of a file Args: fname (str): Filename to read Return: str: Contents of file """ with open(fname, 'rb') as fd: return fd.read() def make_ramdisk(self, ubman, filename, text): """Make a sample ramdisk with test data Returns: str: Filename of ramdisk created """ fname = self.make_fname(ubman, filename) data = '' for i in range(100): data += f'{text} {i} was seldom used in the middle ages\n' with open(fname, 'w', encoding='ascii') as fd: print(data, file=fd) return fname def make_compressed(self, ubman, filename): """Compress a file using gzip""" utils.run_and_log(ubman, ['gzip', '-f', '-k', filename]) return filename + '.gz' def find_matching(self, text, match): """Find a match in a line of text, and return the unmatched line portion This is used to extract a part of a line from some text. The match string is used to locate the line - we use the first line that contains that match text. Once we find a match, we discard the match string itself from the line, and return what remains. TODO: If this function becomes more generally useful, we could change it to use regex and return groups. Args: text (list of str): Text to check, one for each command issued match (str): String to search for Return: str: unmatched portion of line Exceptions: ValueError: If match is not found >>> find_matching(['first line:10', 'second_line:20'], 'first line:') '10' >>> find_matching(['first line:10', 'second_line:20'], 'second line') Traceback (most recent call last): ... ValueError: Test aborted >>> find_matching('first line:10\', 'second_line:20'], 'second_line:') '20' >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'], 'third_line:') '30' """ # pylint: disable=W0612 __tracebackhide__ = True for line in '\n'.join(text).splitlines(): pos = line.find(match) if pos != -1: return line[:pos] + line[pos + len(match):] pytest.fail("Expected '%s' but not found in output") return '<no-match>' def check_equal(self, params, expected_fname, actual_fname, failure_msg): """Check that a file matches its expected contents This is always used on out-buffers whose size is decided by the test script anyway, which in some cases may be larger than what we're actually looking for. So it's safe to truncate it to the size of the expected data. Args: params (dict): Parameters dict from which to get the filenames expected_fname (str): Key for filename containing expected contents actual_fname (str): Key for filename containing actual contents failure_msg (str): Message to print on failure """ expected_data = self.read_file(params[expected_fname]) actual_data = self.read_file(params[actual_fname]) if len(expected_data) < len(actual_data): actual_data = actual_data[:len(expected_data)] assert expected_data == actual_data, failure_msg def check_not_equal(self, params, expected_fname, actual_fname, failure_msg): """Check that a file does not match its expected contents Args: params (dict): Parameters dict from which to get the filenames expected_fname (str): Key for filename containing expected contents actual_fname (str): Key for filename containing actual contents failure_msg (str): Message to print on failure """ expected_data = self.read_file(params[expected_fname]) actual_data = self.read_file(params[actual_fname]) assert expected_data != actual_data, failure_msg @pytest.fixture() def fsetup(self, ubman): """A fixture to set up files and parameters for FIT tests Args: ubman (ConsoleBase): U-Boot fixture Yields: dict: Parameters needed by the test """ mkimage = os.path.join(ubman.config.build_dir, 'tools/mkimage') # Set up invariant files fdt_data = fit_util.make_dtb(ubman, BASE_FDT, 'u-boot') kernel = fit_util.make_kernel(ubman, 'test-kernel.bin', 'kernel') ramdisk = self.make_ramdisk(ubman, 'test-ramdisk.bin', 'ramdisk') loadables1 = fit_util.make_kernel(ubman, 'test-loadables1.bin', 'lenrek') loadables2 = self.make_ramdisk(ubman, 'test-loadables2.bin', 'ksidmar') kernel_out = self.make_fname(ubman, 'kernel-out.bin') fdt = self.make_fname(ubman, 'u-boot.dtb') fdt_out = self.make_fname(ubman, 'fdt-out.dtb') ramdisk_out = self.make_fname(ubman, 'ramdisk-out.bin') loadables1_out = self.make_fname(ubman, 'loadables1-out.bin') loadables2_out = self.make_fname(ubman, 'loadables2-out.bin') # Set up basic parameters with default values params = { 'fit_addr' : 0x1000, 'kernel' : kernel, 'kernel_out' : kernel_out, 'kernel_addr' : 0x40000, 'kernel_size' : self.filesize(kernel), 'kernel_config' : 'kernel = "kernel-1";', 'fdt' : fdt, 'fdt_out' : fdt_out, 'fdt_addr' : 0x80000, 'fdt_size' : self.filesize(fdt_data), 'fdt_load' : '', 'fdt_config' : 'fdt = "fdt-1";', 'ramdisk' : ramdisk, 'ramdisk_out' : ramdisk_out, 'ramdisk_addr' : 0xc0000, 'ramdisk_size' : self.filesize(ramdisk), 'ramdisk_load' : '', 'ramdisk_config' : '', 'loadables1' : loadables1, 'loadables1_out' : loadables1_out, 'loadables1_addr' : 0x100000, 'loadables1_size' : self.filesize(loadables1), 'loadables1_load' : '', 'loadables2' : loadables2, 'loadables2_out' : loadables2_out, 'loadables2_addr' : 0x140000, 'loadables2_size' : self.filesize(loadables2), 'loadables2_load' : '', 'loadables_config' : '', 'compression' : 'none', 'mem_clear': 'mw 0 0 180000', 'start_subcmd': 'start', 'cmd_extra': '', 'fit_basename': 'test.fit', } # Yield all necessary components to the tests params.update({ 'mkimage': mkimage, 'fdt_data': fdt_data, }) yield params def prepare(self, ubman, fsetup, **kwargs): """Build a FIT with given params Uses the base parameters to create a FIT and a script to test it, For the use of eval here to evaluate f-strings, see: https://stackoverflow.com/questions/42497625/how-to-postpone-defer-the-evaluation-of-f-strings Args: ubman (ConsoleBase): U-Boot fixture fsetup (dict): Information about the test setup kwargs (dict): Changes to make to the default params key (str): parameter to change val (str): f-string to evaluate to get the parameter value Return: tuple: list of str: Commands to run for the test dict: Parameters used by the test str: Filename of the FIT that was created """ params = fsetup.copy() for name, template in kwargs.items(): # pylint: disable=W0123 params[name] = eval(f'f"""{template}"""') # Make a basic FIT and a script to load it fit = fit_util.make_fit(ubman, params['mkimage'], BASE_ITS, params, basename=params['fit_basename']) params['fit'] = fit cmds = BASE_SCRIPT % params return cmds.splitlines(), params, fit def test_fit_kernel_load(self, ubman, fsetup): """Test loading a FIT image with only a kernel""" cmds, params, fit = self.prepare(ubman, fsetup) # Run and verify output = ubman.run_command_list(cmds) self.check_equal(params, 'kernel', 'kernel_out', 'Kernel not loaded') self.check_not_equal(params, 'fdt_data', 'fdt_out', 'FDT loaded but should be ignored') self.check_not_equal(params, 'ramdisk','ramdisk_out', 'Ramdisk loaded but should not be') # Find out the offset in the FIT where U-Boot has found the FDT line = self.find_matching(output, 'Booting using the fdt blob at ') fit_offset = int(line, 16) - params['fit_addr'] fdt_magic = struct.pack('>L', 0xd00dfeed) data = self.read_file(fit) # Now find where it actually is in the FIT (skip the first word) real_fit_offset = data.find(fdt_magic, 4) assert fit_offset == real_fit_offset, ( f'U-Boot loaded FDT from offset {fit_offset:#x}, ' 'FDT is actually at {real_fit_offset:#x}') # Check bootargs string substitution output = ubman.run_command_list([ 'env set bootargs \\"\'my_boot_var=${foo}\'\\"', 'env set foo bar', 'bootm prep', 'env print bootargs']) assert 'bootargs="my_boot_var=bar"' in output, \ "Bootargs strings not substituted" def test_fit_kernel_fdt_load(self, ubman, fsetup): """Test loading a FIT image with a kernel and FDT""" cmds, params, _ = self.prepare( ubman, fsetup, fdt_load="load = <{params['fdt_addr']:#x}>;") # Run and verify ubman.run_command_list(cmds) self.check_equal(params, 'kernel', 'kernel_out', 'Kernel not loaded') self.check_equal(params, 'fdt_data', 'fdt_out', 'FDT not loaded') self.check_not_equal(params, 'ramdisk', 'ramdisk_out', 'Ramdisk loaded but should not be') def test_fit_kernel_fdt_ramdisk_load(self, ubman, fsetup): """Test loading a FIT image with kernel, FDT, and ramdisk""" cmds, params, _ = self.prepare( ubman, fsetup, fdt_load="load = <{params['fdt_addr']:#x}>;", ramdisk_config='ramdisk = "ramdisk-1";', ramdisk_load="load = <{params['ramdisk_addr']:#x}>;") # Run and verify ubman.run_command_list(cmds) self.check_equal(params, 'ramdisk', 'ramdisk_out', 'Ramdisk not loaded') def test_fit_loadables_load(self, ubman, fsetup): """Test a configuration with 'loadables' properties""" # Configure for FDT, ramdisk, and loadables cmds, params, _ = self.prepare( ubman, fsetup, fdt_load="load = <{params['fdt_addr']:#x}>;", ramdisk_config='ramdisk = "ramdisk-1";', ramdisk_load="load = <{params['ramdisk_addr']:#x}>;", loadables_config='loadables="kernel-2", "ramdisk-2";', loadables1_load="load = <{params['loadables1_addr']:#x}>;", loadables2_load="load = <{params['loadables2_addr']:#x}>;") # Run and verify ubman.run_command_list(cmds) self.check_equal(params, 'loadables1', 'loadables1_out', 'Loadables1 (kernel) not loaded') self.check_equal(params, 'loadables2', 'loadables2_out', 'Loadables2 (ramdisk) not loaded') def test_fit_compressed_images_load(self, ubman, fsetup): """Test loading compressed kernel, FDT, and ramdisk images""" # Configure for FDT and ramdisk load cmds, params, _ = self.prepare( ubman, fsetup, fdt_load="load = <{params['fdt_addr']:#x}>;", ramdisk_config='ramdisk = "ramdisk-1";', ramdisk_load="load = <{params['ramdisk_addr']:#x}>;", # Configure for compression compression='gzip', kernel=self.make_compressed(ubman, fsetup['kernel']), fdt=self.make_compressed(ubman, fsetup['fdt']), ramdisk=self.make_compressed(ubman, fsetup['ramdisk'])) # Run and verify ubman.run_command_list(cmds) self.check_equal(fsetup, 'kernel', 'kernel_out', 'Kernel not loaded') self.check_equal(fsetup, 'fdt_data', 'fdt_out', 'FDT not loaded') self.check_not_equal(fsetup, 'ramdisk', 'ramdisk_out', 'Ramdisk got decompressed?') self.check_equal(params, 'ramdisk', 'ramdisk_out', 'Ramdisk not loaded') def test_fit_no_kernel_load(self, ubman, fsetup): """Test that 'bootm' fails when no kernel is specified""" # Configure for FDT load but no kernel or ramdisk cmds = self.prepare( ubman, fsetup, fdt_load="load = <{params['fdt_addr']:#x}>;", kernel_config='', ramdisk_config='', ramdisk_load='')[0] # Run and verify failure output = ubman.run_command_list(cmds) assert "can't get kernel image!" in '\n'.join(output) def test_fit_no_kernel_load_only(self, ubman, fsetup): """Test that 'bootm' handles a load-only FIT""" cmds = self.prepare( ubman, fsetup, kernel_config='load-only;', fdt_load="load = <{params['fdt_addr']:#x}>;", ramdisk_config='ramdisk = "ramdisk-1";', ramdisk_load="load = <{params['ramdisk_addr']:#x}>;", loadables_config='loadables = "kernel-2", "ramdisk-2";', loadables1_load="load = <{params['loadables1_addr']:#x}>;", loadables2_load="load = <{params['loadables2_addr']:#x}>;")[0] lines = ubman.run_command_list(cmds) output = '\n'.join(lines) assert "can't get kernel image!" not in output assert "Detected load-only image: skipping 'kernel'" in output self.check_equal(fsetup, 'fdt_data', 'fdt_out', 'FDT not loaded') self.check_equal(fsetup, 'ramdisk', 'ramdisk_out', 'Ramdisk not loaded') self.check_equal(fsetup, 'loadables1', 'loadables1_out', 'Loadables1 (kernel) not loaded') self.check_equal(fsetup, 'loadables2', 'loadables2_out', 'Loadables2 (ramdisk) not loaded') def test_fit_no_kernel_second(self, ubman, fsetup): """Test that 'bootm' handles a load-only FIT and a second FIT""" cmds = self.prepare( ubman, fsetup, kernel_config='load-only;', # Just load a devicetree for the first FIT ramdisk_config='', ramdisk_load='', fdt_load="load = <{params['fdt_addr']:#x}>;")[0] lines = ubman.run_command_list(cmds) output = '\n'.join(lines) assert "can't get kernel image!" not in output assert "Detected load-only image: skipping 'kernel'" in output self.check_equal(fsetup, 'fdt_data', 'fdt_out', 'FDT not loaded') # Now load a second FIT which has a kernel and ramdisk cmds = self.prepare( ubman, fsetup, fdt_config= '', fdt_load='', ramdisk_config='ramdisk = "ramdisk-1";', ramdisk_load="load = <{params['ramdisk_addr']:#x}>;", # fit=fit mem_clear='', # Run the prep stage so we can check that the FDT-fixups are done cmd_extra='\nbootm cmdline\nbootm prep', start_subcmd='restart', fit_basename='second.fit')[0] # Run and verify lines = ubman.run_command_list(cmds) output = '\n'.join(lines) assert "can't get kernel image!" not in output assert "Detected load-only image: skipping 'kernel'" not in output self.check_equal(fsetup, 'kernel', 'kernel_out', 'Kernel not loaded') self.check_equal(fsetup, 'ramdisk', 'ramdisk_out', 'Ramdisk not loaded') # Check that the devicetree is still there self.check_equal(fsetup, 'fdt_data', 'fdt_out', 'FDT not loaded') |