1 # Copyright (c) 2013, Google Inc.
3 # SPDX-License-Identifier: GPL-2.0+
5 # Sanity check of the FIT handling in U-Boot
10 import u_boot_utils as util
12 # Define a base ITS which we can adjust using % and a dictionary
17 description = "Chrome OS kernel image with one or more FDT blobs";
22 data = /incbin/("%(kernel)s");
31 data = /incbin/("%(loadables1)s");
41 data = /incbin/("u-boot.dtb");
47 algo = "sha1,rsa2048";
48 key-name-hint = "dev";
53 data = /incbin/("%(ramdisk)s");
62 data = /incbin/("%(loadables2)s");
82 # Define a base FDT - currently we don't use anything in this
87 model = "Sandbox Verified Boot Test";
88 compatible = "sandbox";
91 compatible = "sandbox,reset";
97 # This is the U-Boot script that is run for each test. First load the FIT,
98 # then run the 'bootm' command, then save out memory from the places where
99 # we expect 'bootm' to write things. Then quit.
101 sb load hostfs 0 %(fit_addr)x %(fit)s
102 fdt addr %(fit_addr)x
103 bootm start %(fit_addr)x
105 sb save hostfs 0 %(kernel_addr)x %(kernel_out)s %(kernel_size)x
106 sb save hostfs 0 %(fdt_addr)x %(fdt_out)s %(fdt_size)x
107 sb save hostfs 0 %(ramdisk_addr)x %(ramdisk_out)s %(ramdisk_size)x
108 sb save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
109 sb save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
112 @pytest.mark.boardspec('sandbox')
113 @pytest.mark.buildconfigspec('fit_signature')
114 def test_fit(u_boot_console):
115 def make_fname(leaf):
116 """Make a temporary filename
119 leaf: Leaf name of file to create (within temporary directory)
124 return os.path.join(cons.config.build_dir, leaf)
127 """Get the size of a file
130 fname: Filename to check
132 Size of file in bytes
134 return os.stat(fname).st_size
136 def read_file(fname):
137 """Read the contents of a file
140 fname: Filename to read
142 Contents of file as a string
144 with open(fname, 'r') as fd:
148 """Make a sample .dts file and compile it to a .dtb
151 Filename of .dtb file created
153 src = make_fname('u-boot.dts')
154 dtb = make_fname('u-boot.dtb')
155 with open(src, 'w') as fd:
156 print >> fd, base_fdt
157 util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
160 def make_its(params):
161 """Make a sample .its file with parameters embedded
164 params: Dictionary containing parameters to embed in the %() strings
166 Filename of .its file created
168 its = make_fname('test.its')
169 with open(its, 'w') as fd:
170 print >> fd, base_its % params
173 def make_fit(mkimage, params):
174 """Make a sample .fit file ready for loading
176 This creates a .its script with the selected parameters and uses mkimage to
177 turn this into a .fit image.
180 mkimage: Filename of 'mkimage' utility
181 params: Dictionary containing parameters to embed in the %() strings
183 Filename of .fit file created
185 fit = make_fname('test.fit')
186 its = make_its(params)
187 util.run_and_log(cons, [mkimage, '-f', its, fit])
188 with open(make_fname('u-boot.dts'), 'w') as fd:
189 print >> fd, base_fdt
192 def make_kernel(filename, text):
193 """Make a sample kernel with test data
196 filename: the name of the file you want to create
198 Full path and filename of the kernel it created
200 fname = make_fname(filename)
203 data += 'this %s %d is unlikely to boot\n' % (text, i)
204 with open(fname, 'w') as fd:
208 def make_ramdisk(filename, text):
209 """Make a sample ramdisk with test data
212 Filename of ramdisk created
214 fname = make_fname(filename)
217 data += '%s %d was seldom used in the middle ages\n' % (text, i)
218 with open(fname, 'w') as fd:
222 def find_matching(text, match):
223 """Find a match in a line of text, and return the unmatched line portion
225 This is used to extract a part of a line from some text. The match string
226 is used to locate the line - we use the first line that contains that
229 Once we find a match, we discard the match string itself from the line,
230 and return what remains.
232 TODO: If this function becomes more generally useful, we could change it
233 to use regex and return groups.
236 text: Text to check (list of strings, one for each command issued)
237 match: String to search for
239 String containing unmatched portion of line
241 ValueError: If match is not found
243 >>> find_matching(['first line:10', 'second_line:20'], 'first line:')
245 >>> find_matching(['first line:10', 'second_line:20'], 'second line')
246 Traceback (most recent call last):
248 ValueError: Test aborted
249 >>> find_matching('first line:10\', 'second_line:20'], 'second_line:')
251 >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'],
255 __tracebackhide__ = True
256 for line in '\n'.join(text).splitlines():
257 pos = line.find(match)
259 return line[:pos] + line[pos + len(match):]
261 pytest.fail("Expected '%s' but not found in output")
263 def check_equal(expected_fname, actual_fname, failure_msg):
264 """Check that a file matches its expected contents
267 expected_fname: Filename containing expected contents
268 actual_fname: Filename containing actual contents
269 failure_msg: Message to print on failure
271 expected_data = read_file(expected_fname)
272 actual_data = read_file(actual_fname)
273 assert expected_data == actual_data, failure_msg
275 def check_not_equal(expected_fname, actual_fname, failure_msg):
276 """Check that a file does not match its expected contents
279 expected_fname: Filename containing expected contents
280 actual_fname: Filename containing actual contents
281 failure_msg: Message to print on failure
283 expected_data = read_file(expected_fname)
284 actual_data = read_file(actual_fname)
285 assert expected_data != actual_data, failure_msg
287 def run_fit_test(mkimage):
288 """Basic sanity check of FIT loading in U-Boot
290 TODO: Almost everything:
291 - hash algorithms - invalid hash/contents should be detected
292 - signature algorithms - invalid sig/contents should be detected
294 - checking that errors are detected like:
297 - invalid configurations
298 - incorrect os/arch/type fields
300 - images too large/small
301 - invalid FDT (e.g. putting a random binary in instead)
302 - default configuration selection
303 - bootm command line parameters should have desired effect
304 - run code coverage to make sure we are testing all the code
306 # Set up invariant files
307 control_dtb = make_dtb()
308 kernel = make_kernel('test-kernel.bin', 'kernel')
309 ramdisk = make_ramdisk('test-ramdisk.bin', 'ramdisk')
310 loadables1 = make_kernel('test-loadables1.bin', 'lenrek')
311 loadables2 = make_ramdisk('test-loadables2.bin', 'ksidmar')
312 kernel_out = make_fname('kernel-out.bin')
313 fdt_out = make_fname('fdt-out.dtb')
314 ramdisk_out = make_fname('ramdisk-out.bin')
315 loadables1_out = make_fname('loadables1-out.bin')
316 loadables2_out = make_fname('loadables2-out.bin')
318 # Set up basic parameters with default values
323 'kernel_out' : kernel_out,
324 'kernel_addr' : 0x40000,
325 'kernel_size' : filesize(kernel),
328 'fdt_addr' : 0x80000,
329 'fdt_size' : filesize(control_dtb),
333 'ramdisk_out' : ramdisk_out,
334 'ramdisk_addr' : 0xc0000,
335 'ramdisk_size' : filesize(ramdisk),
337 'ramdisk_config' : '',
339 'loadables1' : loadables1,
340 'loadables1_out' : loadables1_out,
341 'loadables1_addr' : 0x100000,
342 'loadables1_size' : filesize(loadables1),
343 'loadables1_load' : '',
345 'loadables2' : loadables2,
346 'loadables2_out' : loadables2_out,
347 'loadables2_addr' : 0x140000,
348 'loadables2_size' : filesize(loadables2),
349 'loadables2_load' : '',
351 'loadables_config' : '',
354 # Make a basic FIT and a script to load it
355 fit = make_fit(mkimage, params)
357 cmd = base_script % params
359 # First check that we can load a kernel
360 # We could perhaps reduce duplication with some loss of readability
361 cons.config.dtb = control_dtb
363 with cons.log.section('Kernel load'):
364 output = cons.run_command_list(cmd.splitlines())
365 check_equal(kernel, kernel_out, 'Kernel not loaded')
366 check_not_equal(control_dtb, fdt_out,
367 'FDT loaded but should be ignored')
368 check_not_equal(ramdisk, ramdisk_out,
369 'Ramdisk loaded but should not be')
371 # Find out the offset in the FIT where U-Boot has found the FDT
372 line = find_matching(output, 'Booting using the fdt blob at ')
373 fit_offset = int(line, 16) - params['fit_addr']
374 fdt_magic = struct.pack('>L', 0xd00dfeed)
375 data = read_file(fit)
377 # Now find where it actually is in the FIT (skip the first word)
378 real_fit_offset = data.find(fdt_magic, 4)
379 assert fit_offset == real_fit_offset, (
380 'U-Boot loaded FDT from offset %#x, FDT is actually at %#x' %
381 (fit_offset, real_fit_offset))
383 # Now a kernel and an FDT
384 with cons.log.section('Kernel + FDT load'):
385 params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
386 fit = make_fit(mkimage, params)
388 output = cons.run_command_list(cmd.splitlines())
389 check_equal(kernel, kernel_out, 'Kernel not loaded')
390 check_equal(control_dtb, fdt_out, 'FDT not loaded')
391 check_not_equal(ramdisk, ramdisk_out,
392 'Ramdisk loaded but should not be')
395 with cons.log.section('Kernel + FDT + Ramdisk load'):
396 params['ramdisk_config'] = 'ramdisk = "ramdisk@1";'
397 params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr']
398 fit = make_fit(mkimage, params)
400 output = cons.run_command_list(cmd.splitlines())
401 check_equal(ramdisk, ramdisk_out, 'Ramdisk not loaded')
403 # Configuration with some Loadables
404 with cons.log.section('Kernel + FDT + Ramdisk load + Loadables'):
405 params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";'
406 params['loadables1_load'] = ('load = <%#x>;' %
407 params['loadables1_addr'])
408 params['loadables2_load'] = ('load = <%#x>;' %
409 params['loadables2_addr'])
410 fit = make_fit(mkimage, params)
412 output = cons.run_command_list(cmd.splitlines())
413 check_equal(loadables1, loadables1_out,
414 'Loadables1 (kernel) not loaded')
415 check_equal(loadables2, loadables2_out,
416 'Loadables2 (ramdisk) not loaded')
418 cons = u_boot_console
420 # We need to use our own device tree file. Remember to restore it
422 old_dtb = cons.config.dtb
423 mkimage = cons.config.build_dir + '/tools/mkimage'
424 run_fit_test(mkimage)
426 # Go back to the original U-Boot with the correct dtb.
427 cons.config.dtb = old_dtb