1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2013, Google Inc.
4 # Sanity check of the FIT handling in U-Boot
6 from __future__ import print_function
11 import u_boot_utils as util
13 # Define a base ITS which we can adjust using % and a dictionary
18 description = "Chrome OS kernel image with one or more FDT blobs";
23 data = /incbin/("%(kernel)s");
27 compression = "%(compression)s";
32 data = /incbin/("%(loadables1)s");
42 data = /incbin/("%(fdt)s");
46 compression = "%(compression)s";
48 algo = "sha1,rsa2048";
49 key-name-hint = "dev";
54 data = /incbin/("%(ramdisk)s");
59 compression = "%(compression)s";
63 data = /incbin/("%(loadables2)s");
83 # Define a base FDT - currently we don't use anything in this
88 model = "Sandbox Verified Boot Test";
89 compatible = "sandbox";
92 compatible = "sandbox,reset";
98 # This is the U-Boot script that is run for each test. First load the FIT,
99 # then run the 'bootm' command, then save out memory from the places where
100 # we expect 'bootm' to write things. Then quit.
102 host load hostfs 0 %(fit_addr)x %(fit)s
103 fdt addr %(fit_addr)x
104 bootm start %(fit_addr)x
106 host save hostfs 0 %(kernel_addr)x %(kernel_out)s %(kernel_size)x
107 host save hostfs 0 %(fdt_addr)x %(fdt_out)s %(fdt_size)x
108 host save hostfs 0 %(ramdisk_addr)x %(ramdisk_out)s %(ramdisk_size)x
109 host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
110 host save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
113 @pytest.mark.boardspec('sandbox')
114 @pytest.mark.buildconfigspec('fit_signature')
115 @pytest.mark.requiredtool('dtc')
116 def test_fit(u_boot_console):
117 def make_fname(leaf):
118 """Make a temporary filename
121 leaf: Leaf name of file to create (within temporary directory)
126 return os.path.join(cons.config.build_dir, leaf)
129 """Get the size of a file
132 fname: Filename to check
134 Size of file in bytes
136 return os.stat(fname).st_size
138 def read_file(fname):
139 """Read the contents of a file
142 fname: Filename to read
144 Contents of file as a string
146 with open(fname, 'rb') as fd:
150 """Make a sample .dts file and compile it to a .dtb
153 Filename of .dtb file created
155 src = make_fname('u-boot.dts')
156 dtb = make_fname('u-boot.dtb')
157 with open(src, 'w') as fd:
158 print(base_fdt, file=fd)
159 util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
162 def make_its(params):
163 """Make a sample .its file with parameters embedded
166 params: Dictionary containing parameters to embed in the %() strings
168 Filename of .its file created
170 its = make_fname('test.its')
171 with open(its, 'w') as fd:
172 print(base_its % params, file=fd)
175 def make_fit(mkimage, params):
176 """Make a sample .fit file ready for loading
178 This creates a .its script with the selected parameters and uses mkimage to
179 turn this into a .fit image.
182 mkimage: Filename of 'mkimage' utility
183 params: Dictionary containing parameters to embed in the %() strings
185 Filename of .fit file created
187 fit = make_fname('test.fit')
188 its = make_its(params)
189 util.run_and_log(cons, [mkimage, '-f', its, fit])
190 with open(make_fname('u-boot.dts'), 'w') as fd:
191 print(base_fdt, file=fd)
194 def make_kernel(filename, text):
195 """Make a sample kernel with test data
198 filename: the name of the file you want to create
200 Full path and filename of the kernel it created
202 fname = make_fname(filename)
205 data += 'this %s %d is unlikely to boot\n' % (text, i)
206 with open(fname, 'w') as fd:
210 def make_ramdisk(filename, text):
211 """Make a sample ramdisk with test data
214 Filename of ramdisk created
216 fname = make_fname(filename)
219 data += '%s %d was seldom used in the middle ages\n' % (text, i)
220 with open(fname, 'w') as fd:
224 def make_compressed(filename):
225 util.run_and_log(cons, ['gzip', '-f', '-k', filename])
226 return filename + '.gz'
228 def find_matching(text, match):
229 """Find a match in a line of text, and return the unmatched line portion
231 This is used to extract a part of a line from some text. The match string
232 is used to locate the line - we use the first line that contains that
235 Once we find a match, we discard the match string itself from the line,
236 and return what remains.
238 TODO: If this function becomes more generally useful, we could change it
239 to use regex and return groups.
242 text: Text to check (list of strings, one for each command issued)
243 match: String to search for
245 String containing unmatched portion of line
247 ValueError: If match is not found
249 >>> find_matching(['first line:10', 'second_line:20'], 'first line:')
251 >>> find_matching(['first line:10', 'second_line:20'], 'second line')
252 Traceback (most recent call last):
254 ValueError: Test aborted
255 >>> find_matching('first line:10\', 'second_line:20'], 'second_line:')
257 >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'],
261 __tracebackhide__ = True
262 for line in '\n'.join(text).splitlines():
263 pos = line.find(match)
265 return line[:pos] + line[pos + len(match):]
267 pytest.fail("Expected '%s' but not found in output")
269 def check_equal(expected_fname, actual_fname, failure_msg):
270 """Check that a file matches its expected contents
273 expected_fname: Filename containing expected contents
274 actual_fname: Filename containing actual contents
275 failure_msg: Message to print on failure
277 expected_data = read_file(expected_fname)
278 actual_data = read_file(actual_fname)
279 assert expected_data == actual_data, failure_msg
281 def check_not_equal(expected_fname, actual_fname, failure_msg):
282 """Check that a file does not match its expected contents
285 expected_fname: Filename containing expected contents
286 actual_fname: Filename containing actual contents
287 failure_msg: Message to print on failure
289 expected_data = read_file(expected_fname)
290 actual_data = read_file(actual_fname)
291 assert expected_data != actual_data, failure_msg
293 def run_fit_test(mkimage):
294 """Basic sanity check of FIT loading in U-Boot
296 TODO: Almost everything:
297 - hash algorithms - invalid hash/contents should be detected
298 - signature algorithms - invalid sig/contents should be detected
300 - checking that errors are detected like:
303 - invalid configurations
304 - incorrect os/arch/type fields
306 - images too large/small
307 - invalid FDT (e.g. putting a random binary in instead)
308 - default configuration selection
309 - bootm command line parameters should have desired effect
310 - run code coverage to make sure we are testing all the code
312 # Set up invariant files
313 control_dtb = make_dtb()
314 kernel = make_kernel('test-kernel.bin', 'kernel')
315 ramdisk = make_ramdisk('test-ramdisk.bin', 'ramdisk')
316 loadables1 = make_kernel('test-loadables1.bin', 'lenrek')
317 loadables2 = make_ramdisk('test-loadables2.bin', 'ksidmar')
318 kernel_out = make_fname('kernel-out.bin')
319 fdt = make_fname('u-boot.dtb')
320 fdt_out = make_fname('fdt-out.dtb')
321 ramdisk_out = make_fname('ramdisk-out.bin')
322 loadables1_out = make_fname('loadables1-out.bin')
323 loadables2_out = make_fname('loadables2-out.bin')
325 # Set up basic parameters with default values
330 'kernel_out' : kernel_out,
331 'kernel_addr' : 0x40000,
332 'kernel_size' : filesize(kernel),
336 'fdt_addr' : 0x80000,
337 'fdt_size' : filesize(control_dtb),
341 'ramdisk_out' : ramdisk_out,
342 'ramdisk_addr' : 0xc0000,
343 'ramdisk_size' : filesize(ramdisk),
345 'ramdisk_config' : '',
347 'loadables1' : loadables1,
348 'loadables1_out' : loadables1_out,
349 'loadables1_addr' : 0x100000,
350 'loadables1_size' : filesize(loadables1),
351 'loadables1_load' : '',
353 'loadables2' : loadables2,
354 'loadables2_out' : loadables2_out,
355 'loadables2_addr' : 0x140000,
356 'loadables2_size' : filesize(loadables2),
357 'loadables2_load' : '',
359 'loadables_config' : '',
360 'compression' : 'none',
363 # Make a basic FIT and a script to load it
364 fit = make_fit(mkimage, params)
366 cmd = base_script % params
368 # First check that we can load a kernel
369 # We could perhaps reduce duplication with some loss of readability
370 cons.config.dtb = control_dtb
372 with cons.log.section('Kernel load'):
373 output = cons.run_command_list(cmd.splitlines())
374 check_equal(kernel, kernel_out, 'Kernel not loaded')
375 check_not_equal(control_dtb, fdt_out,
376 'FDT loaded but should be ignored')
377 check_not_equal(ramdisk, ramdisk_out,
378 'Ramdisk loaded but should not be')
380 # Find out the offset in the FIT where U-Boot has found the FDT
381 line = find_matching(output, 'Booting using the fdt blob at ')
382 fit_offset = int(line, 16) - params['fit_addr']
383 fdt_magic = struct.pack('>L', 0xd00dfeed)
384 data = read_file(fit)
386 # Now find where it actually is in the FIT (skip the first word)
387 real_fit_offset = data.find(fdt_magic, 4)
388 assert fit_offset == real_fit_offset, (
389 'U-Boot loaded FDT from offset %#x, FDT is actually at %#x' %
390 (fit_offset, real_fit_offset))
392 # Now a kernel and an FDT
393 with cons.log.section('Kernel + FDT load'):
394 params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
395 fit = make_fit(mkimage, params)
397 output = cons.run_command_list(cmd.splitlines())
398 check_equal(kernel, kernel_out, 'Kernel not loaded')
399 check_equal(control_dtb, fdt_out, 'FDT not loaded')
400 check_not_equal(ramdisk, ramdisk_out,
401 'Ramdisk loaded but should not be')
404 with cons.log.section('Kernel + FDT + Ramdisk load'):
405 params['ramdisk_config'] = 'ramdisk = "ramdisk@1";'
406 params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr']
407 fit = make_fit(mkimage, params)
409 output = cons.run_command_list(cmd.splitlines())
410 check_equal(ramdisk, ramdisk_out, 'Ramdisk not loaded')
412 # Configuration with some Loadables
413 with cons.log.section('Kernel + FDT + Ramdisk load + Loadables'):
414 params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";'
415 params['loadables1_load'] = ('load = <%#x>;' %
416 params['loadables1_addr'])
417 params['loadables2_load'] = ('load = <%#x>;' %
418 params['loadables2_addr'])
419 fit = make_fit(mkimage, params)
421 output = cons.run_command_list(cmd.splitlines())
422 check_equal(loadables1, loadables1_out,
423 'Loadables1 (kernel) not loaded')
424 check_equal(loadables2, loadables2_out,
425 'Loadables2 (ramdisk) not loaded')
427 # Kernel, FDT and Ramdisk all compressed
428 with cons.log.section('(Kernel + FDT + Ramdisk) compressed'):
429 params['compression'] = 'gzip'
430 params['kernel'] = make_compressed(kernel)
431 params['fdt'] = make_compressed(fdt)
432 params['ramdisk'] = make_compressed(ramdisk)
433 fit = make_fit(mkimage, params)
435 output = cons.run_command_list(cmd.splitlines())
436 check_equal(kernel, kernel_out, 'Kernel not loaded')
437 check_equal(control_dtb, fdt_out, 'FDT not loaded')
438 check_equal(ramdisk, ramdisk_out, 'Ramdisk not loaded')
441 cons = u_boot_console
443 # We need to use our own device tree file. Remember to restore it
445 old_dtb = cons.config.dtb
446 mkimage = cons.config.build_dir + '/tools/mkimage'
447 run_fit_test(mkimage)
449 # Go back to the original U-Boot with the correct dtb.
450 cons.config.dtb = old_dtb