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 @pytest.mark.requiredtool('dtc')
115 def test_fit(u_boot_console):
116 def make_fname(leaf):
117 """Make a temporary filename
120 leaf: Leaf name of file to create (within temporary directory)
125 return os.path.join(cons.config.build_dir, leaf)
128 """Get the size of a file
131 fname: Filename to check
133 Size of file in bytes
135 return os.stat(fname).st_size
137 def read_file(fname):
138 """Read the contents of a file
141 fname: Filename to read
143 Contents of file as a string
145 with open(fname, 'r') as fd:
149 """Make a sample .dts file and compile it to a .dtb
152 Filename of .dtb file created
154 src = make_fname('u-boot.dts')
155 dtb = make_fname('u-boot.dtb')
156 with open(src, 'w') as fd:
157 print >> fd, base_fdt
158 util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
161 def make_its(params):
162 """Make a sample .its file with parameters embedded
165 params: Dictionary containing parameters to embed in the %() strings
167 Filename of .its file created
169 its = make_fname('test.its')
170 with open(its, 'w') as fd:
171 print >> fd, base_its % params
174 def make_fit(mkimage, params):
175 """Make a sample .fit file ready for loading
177 This creates a .its script with the selected parameters and uses mkimage to
178 turn this into a .fit image.
181 mkimage: Filename of 'mkimage' utility
182 params: Dictionary containing parameters to embed in the %() strings
184 Filename of .fit file created
186 fit = make_fname('test.fit')
187 its = make_its(params)
188 util.run_and_log(cons, [mkimage, '-f', its, fit])
189 with open(make_fname('u-boot.dts'), 'w') as fd:
190 print >> fd, base_fdt
193 def make_kernel(filename, text):
194 """Make a sample kernel with test data
197 filename: the name of the file you want to create
199 Full path and filename of the kernel it created
201 fname = make_fname(filename)
204 data += 'this %s %d is unlikely to boot\n' % (text, i)
205 with open(fname, 'w') as fd:
209 def make_ramdisk(filename, text):
210 """Make a sample ramdisk with test data
213 Filename of ramdisk created
215 fname = make_fname(filename)
218 data += '%s %d was seldom used in the middle ages\n' % (text, i)
219 with open(fname, 'w') as fd:
223 def find_matching(text, match):
224 """Find a match in a line of text, and return the unmatched line portion
226 This is used to extract a part of a line from some text. The match string
227 is used to locate the line - we use the first line that contains that
230 Once we find a match, we discard the match string itself from the line,
231 and return what remains.
233 TODO: If this function becomes more generally useful, we could change it
234 to use regex and return groups.
237 text: Text to check (list of strings, one for each command issued)
238 match: String to search for
240 String containing unmatched portion of line
242 ValueError: If match is not found
244 >>> find_matching(['first line:10', 'second_line:20'], 'first line:')
246 >>> find_matching(['first line:10', 'second_line:20'], 'second line')
247 Traceback (most recent call last):
249 ValueError: Test aborted
250 >>> find_matching('first line:10\', 'second_line:20'], 'second_line:')
252 >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'],
256 __tracebackhide__ = True
257 for line in '\n'.join(text).splitlines():
258 pos = line.find(match)
260 return line[:pos] + line[pos + len(match):]
262 pytest.fail("Expected '%s' but not found in output")
264 def check_equal(expected_fname, actual_fname, failure_msg):
265 """Check that a file matches its expected contents
268 expected_fname: Filename containing expected contents
269 actual_fname: Filename containing actual contents
270 failure_msg: Message to print on failure
272 expected_data = read_file(expected_fname)
273 actual_data = read_file(actual_fname)
274 assert expected_data == actual_data, failure_msg
276 def check_not_equal(expected_fname, actual_fname, failure_msg):
277 """Check that a file does not match its expected contents
280 expected_fname: Filename containing expected contents
281 actual_fname: Filename containing actual contents
282 failure_msg: Message to print on failure
284 expected_data = read_file(expected_fname)
285 actual_data = read_file(actual_fname)
286 assert expected_data != actual_data, failure_msg
288 def run_fit_test(mkimage):
289 """Basic sanity check of FIT loading in U-Boot
291 TODO: Almost everything:
292 - hash algorithms - invalid hash/contents should be detected
293 - signature algorithms - invalid sig/contents should be detected
295 - checking that errors are detected like:
298 - invalid configurations
299 - incorrect os/arch/type fields
301 - images too large/small
302 - invalid FDT (e.g. putting a random binary in instead)
303 - default configuration selection
304 - bootm command line parameters should have desired effect
305 - run code coverage to make sure we are testing all the code
307 # Set up invariant files
308 control_dtb = make_dtb()
309 kernel = make_kernel('test-kernel.bin', 'kernel')
310 ramdisk = make_ramdisk('test-ramdisk.bin', 'ramdisk')
311 loadables1 = make_kernel('test-loadables1.bin', 'lenrek')
312 loadables2 = make_ramdisk('test-loadables2.bin', 'ksidmar')
313 kernel_out = make_fname('kernel-out.bin')
314 fdt_out = make_fname('fdt-out.dtb')
315 ramdisk_out = make_fname('ramdisk-out.bin')
316 loadables1_out = make_fname('loadables1-out.bin')
317 loadables2_out = make_fname('loadables2-out.bin')
319 # Set up basic parameters with default values
324 'kernel_out' : kernel_out,
325 'kernel_addr' : 0x40000,
326 'kernel_size' : filesize(kernel),
329 'fdt_addr' : 0x80000,
330 'fdt_size' : filesize(control_dtb),
334 'ramdisk_out' : ramdisk_out,
335 'ramdisk_addr' : 0xc0000,
336 'ramdisk_size' : filesize(ramdisk),
338 'ramdisk_config' : '',
340 'loadables1' : loadables1,
341 'loadables1_out' : loadables1_out,
342 'loadables1_addr' : 0x100000,
343 'loadables1_size' : filesize(loadables1),
344 'loadables1_load' : '',
346 'loadables2' : loadables2,
347 'loadables2_out' : loadables2_out,
348 'loadables2_addr' : 0x140000,
349 'loadables2_size' : filesize(loadables2),
350 'loadables2_load' : '',
352 'loadables_config' : '',
355 # Make a basic FIT and a script to load it
356 fit = make_fit(mkimage, params)
358 cmd = base_script % params
360 # First check that we can load a kernel
361 # We could perhaps reduce duplication with some loss of readability
362 cons.config.dtb = control_dtb
364 with cons.log.section('Kernel load'):
365 output = cons.run_command_list(cmd.splitlines())
366 check_equal(kernel, kernel_out, 'Kernel not loaded')
367 check_not_equal(control_dtb, fdt_out,
368 'FDT loaded but should be ignored')
369 check_not_equal(ramdisk, ramdisk_out,
370 'Ramdisk loaded but should not be')
372 # Find out the offset in the FIT where U-Boot has found the FDT
373 line = find_matching(output, 'Booting using the fdt blob at ')
374 fit_offset = int(line, 16) - params['fit_addr']
375 fdt_magic = struct.pack('>L', 0xd00dfeed)
376 data = read_file(fit)
378 # Now find where it actually is in the FIT (skip the first word)
379 real_fit_offset = data.find(fdt_magic, 4)
380 assert fit_offset == real_fit_offset, (
381 'U-Boot loaded FDT from offset %#x, FDT is actually at %#x' %
382 (fit_offset, real_fit_offset))
384 # Now a kernel and an FDT
385 with cons.log.section('Kernel + FDT load'):
386 params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
387 fit = make_fit(mkimage, params)
389 output = cons.run_command_list(cmd.splitlines())
390 check_equal(kernel, kernel_out, 'Kernel not loaded')
391 check_equal(control_dtb, fdt_out, 'FDT not loaded')
392 check_not_equal(ramdisk, ramdisk_out,
393 'Ramdisk loaded but should not be')
396 with cons.log.section('Kernel + FDT + Ramdisk load'):
397 params['ramdisk_config'] = 'ramdisk = "ramdisk@1";'
398 params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr']
399 fit = make_fit(mkimage, params)
401 output = cons.run_command_list(cmd.splitlines())
402 check_equal(ramdisk, ramdisk_out, 'Ramdisk not loaded')
404 # Configuration with some Loadables
405 with cons.log.section('Kernel + FDT + Ramdisk load + Loadables'):
406 params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";'
407 params['loadables1_load'] = ('load = <%#x>;' %
408 params['loadables1_addr'])
409 params['loadables2_load'] = ('load = <%#x>;' %
410 params['loadables2_addr'])
411 fit = make_fit(mkimage, params)
413 output = cons.run_command_list(cmd.splitlines())
414 check_equal(loadables1, loadables1_out,
415 'Loadables1 (kernel) not loaded')
416 check_equal(loadables2, loadables2_out,
417 'Loadables2 (ramdisk) not loaded')
419 cons = u_boot_console
421 # We need to use our own device tree file. Remember to restore it
423 old_dtb = cons.config.dtb
424 mkimage = cons.config.build_dir + '/tools/mkimage'
425 run_fit_test(mkimage)
427 # Go back to the original U-Boot with the correct dtb.
428 cons.config.dtb = old_dtb