3 # Copyright (c) 2013, Google Inc.
5 # Sanity check of the FIT handling in U-Boot
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 2 of
10 # the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 # make O=sandbox sandbox_config
26 # ./test/image/test-fit.py -u sandbox/u-boot
29 from optparse import OptionParser
36 # The 'command' library in patman is convenient for running commands
37 base_path = os.path.dirname(sys.argv[0])
38 patman = os.path.join(base_path, '../../tools/patman')
39 sys.path.append(patman)
43 # Define a base ITS which we can adjust using % and a dictionary
48 description = "Chrome OS kernel image with one or more FDT blobs";
53 data = /incbin/("%(kernel)s");
63 data = /incbin/("u-boot.dtb");
69 algo = "sha1,rsa2048";
70 key-name-hint = "dev";
75 data = /incbin/("%(ramdisk)s");
94 # Define a base FDT - currently we don't use anything in this
99 model = "Sandbox Verified Boot Test";
100 compatible = "sandbox";
105 # This is the U-Boot script that is run for each test. First load the fit,
106 # then do the 'bootm' command, then save out memory from the places where
107 # we expect 'bootm' to write things. Then quit.
109 sb load host 0 %(fit_addr)x %(fit)s
110 fdt addr %(fit_addr)x
111 bootm start %(fit_addr)x
113 sb save host 0 %(kernel_out)s %(kernel_addr)x %(kernel_size)x
114 sb save host 0 %(fdt_out)s %(fdt_addr)x %(fdt_size)x
115 sb save host 0 %(ramdisk_out)s %(ramdisk_addr)x %(ramdisk_size)x
119 def make_fname(leaf):
120 """Make a temporary filename
123 leaf: Leaf name of file to create (within temporary directory)
129 return os.path.join(base_dir, leaf)
132 """Get the size of a file
135 fname: Filename to check
137 Size of file in bytes
139 return os.stat(fname).st_size
141 def read_file(fname):
142 """Read the contents of a file
145 fname: Filename to read
147 Contents of file as a string
149 with open(fname, 'r') as fd:
153 """Make a sample .dts file and compile it to a .dtb
156 Filename of .dtb file created
158 src = make_fname('u-boot.dts')
159 dtb = make_fname('u-boot.dtb')
160 with open(src, 'w') as fd:
162 command.Output('dtc', src, '-O', 'dtb', '-o', dtb)
165 def make_its(params):
166 """Make a sample .its file with parameters embedded
169 params: Dictionary containing parameters to embed in the %() strings
171 Filename of .its file created
173 its = make_fname('test.its')
174 with open(its, 'w') as fd:
175 print >>fd, base_its % params
178 def make_fit(mkimage, params):
179 """Make a sample .fit file ready for loading
181 This creates a .its script with the selected parameters and uses mkimage to
182 turn this into a .fit image.
185 mkimage: Filename of 'mkimage' utility
186 params: Dictionary containing parameters to embed in the %() strings
188 Filename of .fit file created
190 fit = make_fname('test.fit')
191 its = make_its(params)
192 command.Output(mkimage, '-f', its, fit)
193 with open(make_fname('u-boot.dts'), 'w') as fd:
198 """Make a sample kernel with test data
201 Filename of kernel created
203 fname = make_fname('test-kernel.bin')
206 data += 'this kernel %d is unlikely to boot\n' % i
207 with open(fname, 'w') as fd:
212 """Make a sample ramdisk with test data
215 Filename of ramdisk created
217 fname = make_fname('test-ramdisk.bin')
220 data += 'ramdisk %d was seldom used in the middle ages\n' % i
221 with open(fname, 'w') as fd:
225 def find_matching(text, match):
226 """Find a match in a line of text, and return the unmatched line portion
228 This is used to extract a part of a line from some text. The match string
229 is used to locate the line - we use the first line that contains that
232 Once we find a match, we discard the match string itself from the line,
233 and return what remains.
235 TODO: If this function becomes more generally useful, we could change it
236 to use regex and return groups.
239 text: Text to check (each line separated by \n)
240 match: String to search for
242 String containing unmatched portion of line
244 ValueError: If match is not found
246 >>> find_matching('first line:10\\nsecond_line:20', 'first line:')
248 >>> find_matching('first line:10\\nsecond_line:20', 'second linex')
249 Traceback (most recent call last):
251 ValueError: Test aborted
252 >>> find_matching('first line:10\\nsecond_line:20', 'second_line:')
255 for line in text.splitlines():
256 pos = line.find(match)
258 return line[:pos] + line[pos + len(match):]
260 print "Expected '%s' but not found in output:"
262 raise ValueError('Test aborted')
265 """Set the name of the current test and print a message
276 """Raise an error with a helpful failure message
279 msg: Message to display
281 raise ValueError("Test '%s' failed: %s" % (test_name, msg))
283 def run_fit_test(mkimage, u_boot):
284 """Basic sanity check of FIT loading in U-Boot
286 TODO: Almost everything:
287 - hash algorithms - invalid hash/contents should be detected
288 - signature algorithms - invalid sig/contents should be detected
290 - checking that errors are detected like:
293 - invalid configurations
294 - incorrect os/arch/type fields
296 - images too large/small
297 - invalid FDT (e.g. putting a random binary in instead)
298 - default configuration selection
299 - bootm command line parameters should have desired effect
300 - run code coverage to make sure we are testing all the code
304 # Set up invariant files
305 control_dtb = make_dtb()
306 kernel = make_kernel()
307 ramdisk = make_ramdisk()
308 kernel_out = make_fname('kernel-out.bin')
309 fdt_out = make_fname('fdt-out.dtb')
310 ramdisk_out = make_fname('ramdisk-out.bin')
312 # Set up basic parameters with default values
317 'kernel_out' : kernel_out,
318 'kernel_addr' : 0x40000,
319 'kernel_size' : filesize(kernel),
322 'fdt_addr' : 0x80000,
323 'fdt_size' : filesize(control_dtb),
327 'ramdisk_out' : ramdisk_out,
328 'ramdisk_addr' : 0xc0000,
329 'ramdisk_size' : filesize(ramdisk),
331 'ramdisk_config' : '',
334 # Make a basic FIT and a script to load it
335 fit = make_fit(mkimage, params)
337 cmd = base_script % params
339 # First check that we can load a kernel
340 # We could perhaps reduce duplication with some loss of readability
341 set_test('Kernel load')
342 stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd)
343 if read_file(kernel) != read_file(kernel_out):
344 fail('Kernel not loaded')
345 if read_file(control_dtb) == read_file(fdt_out):
346 fail('FDT loaded but should be ignored')
347 if read_file(ramdisk) == read_file(ramdisk_out):
348 fail('Ramdisk loaded but should not be')
350 # Find out the offset in the FIT where U-Boot has found the FDT
351 line = find_matching(stdout, 'Booting using the fdt blob at ')
352 fit_offset = int(line, 16) - params['fit_addr']
353 fdt_magic = struct.pack('>L', 0xd00dfeed)
354 data = read_file(fit)
356 # Now find where it actually is in the FIT (skip the first word)
357 real_fit_offset = data.find(fdt_magic, 4)
358 if fit_offset != real_fit_offset:
359 fail('U-Boot loaded FDT from offset %#x, FDT is actually at %#x' %
360 (fit_offset, real_fit_offset))
362 # Now a kernel and an FDT
363 set_test('Kernel + FDT load')
364 params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
365 fit = make_fit(mkimage, params)
366 stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd)
367 if read_file(kernel) != read_file(kernel_out):
368 fail('Kernel not loaded')
369 if read_file(control_dtb) != read_file(fdt_out):
370 fail('FDT not loaded')
371 if read_file(ramdisk) == read_file(ramdisk_out):
372 fail('Ramdisk loaded but should not be')
375 set_test('Kernel + FDT + Ramdisk load')
376 params['ramdisk_config'] = 'ramdisk = "ramdisk@1";'
377 params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr']
378 fit = make_fit(mkimage, params)
379 stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd)
380 if read_file(ramdisk) != read_file(ramdisk_out):
381 fail('Ramdisk not loaded')
384 """Parse options, run the FIT tests and print the result"""
385 global base_path, base_dir
387 # Work in a temporary directory
388 base_dir = tempfile.mkdtemp()
389 parser = OptionParser()
390 parser.add_option('-u', '--u-boot',
391 default=os.path.join(base_path, 'u-boot'),
392 help='Select U-Boot sandbox binary')
393 parser.add_option('-k', '--keep', action='store_true',
394 help="Don't delete temporary directory even when tests pass")
395 parser.add_option('-t', '--selftest', action='store_true',
396 help='Run internal self tests')
397 (options, args) = parser.parse_args()
399 # Find the path to U-Boot, and assume mkimage is in its tools/mkimage dir
400 base_path = os.path.dirname(options.u_boot)
401 mkimage = os.path.join(base_path, 'tools/mkimage')
403 # There are a few doctests - handle these here
409 print title, '\n', '=' * len(title)
411 run_fit_test(mkimage, options.u_boot)
413 print '\nTests passed'
414 print 'Caveat: this is only a sanity check - test coverage is poor'
416 # Remove the tempoerary directory unless we are asked to keep it
418 print "Output files are in '%s'" % base_dir
420 shutil.rmtree(base_dir)