blob: 49d6fea57163fd65d09e1c7a7a6f798c08dc28b6 [file] [log] [blame]
Wolfgang Denk1a459662013-07-08 09:37:19 +02001# SPDX-License-Identifier: GPL-2.0+
Tom Rini83d290c2018-05-06 17:58:06 -04002# Copyright (c) 2013, Google Inc.
Simon Glass301e8032013-05-16 13:53:28 +00003#
Simon Glass77b42672017-08-05 10:28:40 -06004# Sanity check of the FIT handling in U-Boot
Simon Glass301e8032013-05-16 13:53:28 +00005
Paul Burtondffd56d2017-09-14 14:34:43 -07006from __future__ import print_function
7
Simon Glass301e8032013-05-16 13:53:28 +00008import os
Simon Glass77b42672017-08-05 10:28:40 -06009import pytest
Simon Glass301e8032013-05-16 13:53:28 +000010import struct
Simon Glass77b42672017-08-05 10:28:40 -060011import u_boot_utils as util
Simon Glass301e8032013-05-16 13:53:28 +000012
13# Define a base ITS which we can adjust using % and a dictionary
14base_its = '''
15/dts-v1/;
16
17/ {
18 description = "Chrome OS kernel image with one or more FDT blobs";
19 #address-cells = <1>;
20
21 images {
22 kernel@1 {
23 data = /incbin/("%(kernel)s");
24 type = "kernel";
25 arch = "sandbox";
26 os = "linux";
27 compression = "none";
28 load = <0x40000>;
29 entry = <0x8>;
30 };
Karl Apsite657fd2d2015-05-21 09:52:50 -040031 kernel@2 {
32 data = /incbin/("%(loadables1)s");
33 type = "kernel";
34 arch = "sandbox";
35 os = "linux";
36 compression = "none";
37 %(loadables1_load)s
38 entry = <0x0>;
39 };
Simon Glass301e8032013-05-16 13:53:28 +000040 fdt@1 {
41 description = "snow";
42 data = /incbin/("u-boot.dtb");
43 type = "flat_dt";
44 arch = "sandbox";
45 %(fdt_load)s
46 compression = "none";
47 signature@1 {
48 algo = "sha1,rsa2048";
49 key-name-hint = "dev";
50 };
51 };
52 ramdisk@1 {
53 description = "snow";
54 data = /incbin/("%(ramdisk)s");
55 type = "ramdisk";
56 arch = "sandbox";
57 os = "linux";
58 %(ramdisk_load)s
59 compression = "none";
60 };
Karl Apsite657fd2d2015-05-21 09:52:50 -040061 ramdisk@2 {
62 description = "snow";
63 data = /incbin/("%(loadables2)s");
64 type = "ramdisk";
65 arch = "sandbox";
66 os = "linux";
67 %(loadables2_load)s
68 compression = "none";
69 };
Simon Glass301e8032013-05-16 13:53:28 +000070 };
71 configurations {
72 default = "conf@1";
73 conf@1 {
74 kernel = "kernel@1";
75 fdt = "fdt@1";
76 %(ramdisk_config)s
Karl Apsite657fd2d2015-05-21 09:52:50 -040077 %(loadables_config)s
Simon Glass301e8032013-05-16 13:53:28 +000078 };
79 };
80};
81'''
82
83# Define a base FDT - currently we don't use anything in this
84base_fdt = '''
85/dts-v1/;
86
87/ {
88 model = "Sandbox Verified Boot Test";
89 compatible = "sandbox";
90
Simon Glass0edd82e2016-02-24 09:14:44 -070091 reset@0 {
92 compatible = "sandbox,reset";
93 };
94
Simon Glass301e8032013-05-16 13:53:28 +000095};
96'''
97
Robert P. J. Dayb28c5fc2017-03-13 06:50:55 -040098# 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
Simon Glass301e8032013-05-16 13:53:28 +0000100# we expect 'bootm' to write things. Then quit.
101base_script = '''
Simon Glass6d07d632018-11-15 18:44:02 -0700102host load hostfs 0 %(fit_addr)x %(fit)s
Simon Glass301e8032013-05-16 13:53:28 +0000103fdt addr %(fit_addr)x
104bootm start %(fit_addr)x
105bootm loados
Simon Glass6d07d632018-11-15 18:44:02 -0700106host save hostfs 0 %(kernel_addr)x %(kernel_out)s %(kernel_size)x
107host save hostfs 0 %(fdt_addr)x %(fdt_out)s %(fdt_size)x
108host save hostfs 0 %(ramdisk_addr)x %(ramdisk_out)s %(ramdisk_size)x
109host save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
110host save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
Simon Glass301e8032013-05-16 13:53:28 +0000111'''
112
Simon Glass77b42672017-08-05 10:28:40 -0600113@pytest.mark.boardspec('sandbox')
114@pytest.mark.buildconfigspec('fit_signature')
Stephen Warren2d26bf62017-09-18 11:11:49 -0600115@pytest.mark.requiredtool('dtc')
Simon Glass77b42672017-08-05 10:28:40 -0600116def test_fit(u_boot_console):
Simon Glassbde67122017-08-05 10:28:39 -0600117 def make_fname(leaf):
118 """Make a temporary filename
119
120 Args:
121 leaf: Leaf name of file to create (within temporary directory)
122 Return:
123 Temporary filename
124 """
Simon Glassbde67122017-08-05 10:28:39 -0600125
Simon Glass77b42672017-08-05 10:28:40 -0600126 return os.path.join(cons.config.build_dir, leaf)
Simon Glassbde67122017-08-05 10:28:39 -0600127
128 def filesize(fname):
129 """Get the size of a file
130
131 Args:
132 fname: Filename to check
133 Return:
134 Size of file in bytes
135 """
136 return os.stat(fname).st_size
137
138 def read_file(fname):
139 """Read the contents of a file
140
141 Args:
142 fname: Filename to read
143 Returns:
144 Contents of file as a string
145 """
Paul Burton57bf9be2017-09-14 14:34:48 -0700146 with open(fname, 'rb') as fd:
Simon Glassbde67122017-08-05 10:28:39 -0600147 return fd.read()
148
149 def make_dtb():
150 """Make a sample .dts file and compile it to a .dtb
151
152 Returns:
153 Filename of .dtb file created
154 """
155 src = make_fname('u-boot.dts')
156 dtb = make_fname('u-boot.dtb')
157 with open(src, 'w') as fd:
Paul Burtondffd56d2017-09-14 14:34:43 -0700158 print(base_fdt, file=fd)
Simon Glass77b42672017-08-05 10:28:40 -0600159 util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
Simon Glassbde67122017-08-05 10:28:39 -0600160 return dtb
161
162 def make_its(params):
163 """Make a sample .its file with parameters embedded
164
165 Args:
166 params: Dictionary containing parameters to embed in the %() strings
167 Returns:
168 Filename of .its file created
169 """
170 its = make_fname('test.its')
171 with open(its, 'w') as fd:
Paul Burtondffd56d2017-09-14 14:34:43 -0700172 print(base_its % params, file=fd)
Simon Glassbde67122017-08-05 10:28:39 -0600173 return its
174
175 def make_fit(mkimage, params):
176 """Make a sample .fit file ready for loading
177
178 This creates a .its script with the selected parameters and uses mkimage to
179 turn this into a .fit image.
180
181 Args:
182 mkimage: Filename of 'mkimage' utility
183 params: Dictionary containing parameters to embed in the %() strings
184 Return:
185 Filename of .fit file created
186 """
187 fit = make_fname('test.fit')
188 its = make_its(params)
Simon Glass77b42672017-08-05 10:28:40 -0600189 util.run_and_log(cons, [mkimage, '-f', its, fit])
Simon Glassbde67122017-08-05 10:28:39 -0600190 with open(make_fname('u-boot.dts'), 'w') as fd:
Paul Burtondffd56d2017-09-14 14:34:43 -0700191 print(base_fdt, file=fd)
Simon Glassbde67122017-08-05 10:28:39 -0600192 return fit
193
194 def make_kernel(filename, text):
195 """Make a sample kernel with test data
196
197 Args:
198 filename: the name of the file you want to create
199 Returns:
200 Full path and filename of the kernel it created
201 """
202 fname = make_fname(filename)
203 data = ''
204 for i in range(100):
205 data += 'this %s %d is unlikely to boot\n' % (text, i)
206 with open(fname, 'w') as fd:
Paul Burtondffd56d2017-09-14 14:34:43 -0700207 print(data, file=fd)
Simon Glassbde67122017-08-05 10:28:39 -0600208 return fname
209
210 def make_ramdisk(filename, text):
211 """Make a sample ramdisk with test data
212
213 Returns:
214 Filename of ramdisk created
215 """
216 fname = make_fname(filename)
217 data = ''
218 for i in range(100):
219 data += '%s %d was seldom used in the middle ages\n' % (text, i)
220 with open(fname, 'w') as fd:
Paul Burtondffd56d2017-09-14 14:34:43 -0700221 print(data, file=fd)
Simon Glassbde67122017-08-05 10:28:39 -0600222 return fname
223
224 def find_matching(text, match):
225 """Find a match in a line of text, and return the unmatched line portion
226
227 This is used to extract a part of a line from some text. The match string
228 is used to locate the line - we use the first line that contains that
229 match text.
230
231 Once we find a match, we discard the match string itself from the line,
232 and return what remains.
233
234 TODO: If this function becomes more generally useful, we could change it
235 to use regex and return groups.
236
237 Args:
Simon Glass77b42672017-08-05 10:28:40 -0600238 text: Text to check (list of strings, one for each command issued)
Simon Glassbde67122017-08-05 10:28:39 -0600239 match: String to search for
240 Return:
241 String containing unmatched portion of line
242 Exceptions:
243 ValueError: If match is not found
244
Simon Glass77b42672017-08-05 10:28:40 -0600245 >>> find_matching(['first line:10', 'second_line:20'], 'first line:')
Simon Glassbde67122017-08-05 10:28:39 -0600246 '10'
Simon Glass77b42672017-08-05 10:28:40 -0600247 >>> find_matching(['first line:10', 'second_line:20'], 'second line')
Simon Glassbde67122017-08-05 10:28:39 -0600248 Traceback (most recent call last):
249 ...
250 ValueError: Test aborted
Simon Glass77b42672017-08-05 10:28:40 -0600251 >>> find_matching('first line:10\', 'second_line:20'], 'second_line:')
Simon Glassbde67122017-08-05 10:28:39 -0600252 '20'
Simon Glass77b42672017-08-05 10:28:40 -0600253 >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'],
254 'third_line:')
255 '30'
Simon Glassbde67122017-08-05 10:28:39 -0600256 """
Simon Glass77b42672017-08-05 10:28:40 -0600257 __tracebackhide__ = True
258 for line in '\n'.join(text).splitlines():
Simon Glassbde67122017-08-05 10:28:39 -0600259 pos = line.find(match)
260 if pos != -1:
261 return line[:pos] + line[pos + len(match):]
262
Simon Glass77b42672017-08-05 10:28:40 -0600263 pytest.fail("Expected '%s' but not found in output")
Simon Glassbde67122017-08-05 10:28:39 -0600264
Simon Glass77b42672017-08-05 10:28:40 -0600265 def check_equal(expected_fname, actual_fname, failure_msg):
266 """Check that a file matches its expected contents
Simon Glassbde67122017-08-05 10:28:39 -0600267
268 Args:
Simon Glass77b42672017-08-05 10:28:40 -0600269 expected_fname: Filename containing expected contents
270 actual_fname: Filename containing actual contents
271 failure_msg: Message to print on failure
Simon Glassbde67122017-08-05 10:28:39 -0600272 """
Simon Glass77b42672017-08-05 10:28:40 -0600273 expected_data = read_file(expected_fname)
274 actual_data = read_file(actual_fname)
275 assert expected_data == actual_data, failure_msg
Simon Glassbde67122017-08-05 10:28:39 -0600276
Simon Glass77b42672017-08-05 10:28:40 -0600277 def check_not_equal(expected_fname, actual_fname, failure_msg):
278 """Check that a file does not match its expected contents
Simon Glassbde67122017-08-05 10:28:39 -0600279
280 Args:
Simon Glass77b42672017-08-05 10:28:40 -0600281 expected_fname: Filename containing expected contents
282 actual_fname: Filename containing actual contents
283 failure_msg: Message to print on failure
Simon Glassbde67122017-08-05 10:28:39 -0600284 """
Simon Glass77b42672017-08-05 10:28:40 -0600285 expected_data = read_file(expected_fname)
286 actual_data = read_file(actual_fname)
287 assert expected_data != actual_data, failure_msg
Simon Glasscc447722014-12-02 13:17:32 -0700288
Simon Glass77b42672017-08-05 10:28:40 -0600289 def run_fit_test(mkimage):
Simon Glassbde67122017-08-05 10:28:39 -0600290 """Basic sanity check of FIT loading in U-Boot
Simon Glass301e8032013-05-16 13:53:28 +0000291
Simon Glassbde67122017-08-05 10:28:39 -0600292 TODO: Almost everything:
293 - hash algorithms - invalid hash/contents should be detected
294 - signature algorithms - invalid sig/contents should be detected
295 - compression
296 - checking that errors are detected like:
297 - image overwriting
298 - missing images
299 - invalid configurations
300 - incorrect os/arch/type fields
301 - empty data
302 - images too large/small
303 - invalid FDT (e.g. putting a random binary in instead)
304 - default configuration selection
305 - bootm command line parameters should have desired effect
306 - run code coverage to make sure we are testing all the code
307 """
Simon Glassbde67122017-08-05 10:28:39 -0600308 # Set up invariant files
309 control_dtb = make_dtb()
310 kernel = make_kernel('test-kernel.bin', 'kernel')
311 ramdisk = make_ramdisk('test-ramdisk.bin', 'ramdisk')
312 loadables1 = make_kernel('test-loadables1.bin', 'lenrek')
313 loadables2 = make_ramdisk('test-loadables2.bin', 'ksidmar')
314 kernel_out = make_fname('kernel-out.bin')
315 fdt_out = make_fname('fdt-out.dtb')
316 ramdisk_out = make_fname('ramdisk-out.bin')
317 loadables1_out = make_fname('loadables1-out.bin')
318 loadables2_out = make_fname('loadables2-out.bin')
Simon Glass301e8032013-05-16 13:53:28 +0000319
Simon Glassbde67122017-08-05 10:28:39 -0600320 # Set up basic parameters with default values
321 params = {
322 'fit_addr' : 0x1000,
Simon Glass301e8032013-05-16 13:53:28 +0000323
Simon Glassbde67122017-08-05 10:28:39 -0600324 'kernel' : kernel,
325 'kernel_out' : kernel_out,
326 'kernel_addr' : 0x40000,
327 'kernel_size' : filesize(kernel),
Simon Glass301e8032013-05-16 13:53:28 +0000328
Simon Glassbde67122017-08-05 10:28:39 -0600329 'fdt_out' : fdt_out,
330 'fdt_addr' : 0x80000,
331 'fdt_size' : filesize(control_dtb),
332 'fdt_load' : '',
Simon Glass301e8032013-05-16 13:53:28 +0000333
Simon Glassbde67122017-08-05 10:28:39 -0600334 'ramdisk' : ramdisk,
335 'ramdisk_out' : ramdisk_out,
336 'ramdisk_addr' : 0xc0000,
337 'ramdisk_size' : filesize(ramdisk),
338 'ramdisk_load' : '',
339 'ramdisk_config' : '',
Simon Glass301e8032013-05-16 13:53:28 +0000340
Simon Glassbde67122017-08-05 10:28:39 -0600341 'loadables1' : loadables1,
342 'loadables1_out' : loadables1_out,
343 'loadables1_addr' : 0x100000,
344 'loadables1_size' : filesize(loadables1),
345 'loadables1_load' : '',
Simon Glass301e8032013-05-16 13:53:28 +0000346
Simon Glassbde67122017-08-05 10:28:39 -0600347 'loadables2' : loadables2,
348 'loadables2_out' : loadables2_out,
349 'loadables2_addr' : 0x140000,
350 'loadables2_size' : filesize(loadables2),
351 'loadables2_load' : '',
Simon Glass301e8032013-05-16 13:53:28 +0000352
Simon Glassbde67122017-08-05 10:28:39 -0600353 'loadables_config' : '',
354 }
Simon Glass301e8032013-05-16 13:53:28 +0000355
Simon Glassbde67122017-08-05 10:28:39 -0600356 # Make a basic FIT and a script to load it
357 fit = make_fit(mkimage, params)
358 params['fit'] = fit
359 cmd = base_script % params
Simon Glass301e8032013-05-16 13:53:28 +0000360
Simon Glassbde67122017-08-05 10:28:39 -0600361 # First check that we can load a kernel
362 # We could perhaps reduce duplication with some loss of readability
Simon Glass77b42672017-08-05 10:28:40 -0600363 cons.config.dtb = control_dtb
364 cons.restart_uboot()
365 with cons.log.section('Kernel load'):
366 output = cons.run_command_list(cmd.splitlines())
367 check_equal(kernel, kernel_out, 'Kernel not loaded')
368 check_not_equal(control_dtb, fdt_out,
369 'FDT loaded but should be ignored')
370 check_not_equal(ramdisk, ramdisk_out,
371 'Ramdisk loaded but should not be')
Simon Glass301e8032013-05-16 13:53:28 +0000372
Simon Glassbde67122017-08-05 10:28:39 -0600373 # Find out the offset in the FIT where U-Boot has found the FDT
Simon Glass77b42672017-08-05 10:28:40 -0600374 line = find_matching(output, 'Booting using the fdt blob at ')
Simon Glassbde67122017-08-05 10:28:39 -0600375 fit_offset = int(line, 16) - params['fit_addr']
376 fdt_magic = struct.pack('>L', 0xd00dfeed)
377 data = read_file(fit)
Simon Glass301e8032013-05-16 13:53:28 +0000378
Simon Glassbde67122017-08-05 10:28:39 -0600379 # Now find where it actually is in the FIT (skip the first word)
380 real_fit_offset = data.find(fdt_magic, 4)
Simon Glass77b42672017-08-05 10:28:40 -0600381 assert fit_offset == real_fit_offset, (
382 'U-Boot loaded FDT from offset %#x, FDT is actually at %#x' %
383 (fit_offset, real_fit_offset))
Simon Glass301e8032013-05-16 13:53:28 +0000384
Simon Glassbde67122017-08-05 10:28:39 -0600385 # Now a kernel and an FDT
Simon Glass77b42672017-08-05 10:28:40 -0600386 with cons.log.section('Kernel + FDT load'):
Simon Glassbde67122017-08-05 10:28:39 -0600387 params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
388 fit = make_fit(mkimage, params)
Simon Glass77b42672017-08-05 10:28:40 -0600389 cons.restart_uboot()
390 output = cons.run_command_list(cmd.splitlines())
391 check_equal(kernel, kernel_out, 'Kernel not loaded')
392 check_equal(control_dtb, fdt_out, 'FDT not loaded')
393 check_not_equal(ramdisk, ramdisk_out,
394 'Ramdisk loaded but should not be')
Simon Glass301e8032013-05-16 13:53:28 +0000395
Simon Glassbde67122017-08-05 10:28:39 -0600396 # Try a ramdisk
Simon Glass77b42672017-08-05 10:28:40 -0600397 with cons.log.section('Kernel + FDT + Ramdisk load'):
Simon Glassbde67122017-08-05 10:28:39 -0600398 params['ramdisk_config'] = 'ramdisk = "ramdisk@1";'
399 params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr']
400 fit = make_fit(mkimage, params)
Simon Glass77b42672017-08-05 10:28:40 -0600401 cons.restart_uboot()
402 output = cons.run_command_list(cmd.splitlines())
403 check_equal(ramdisk, ramdisk_out, 'Ramdisk not loaded')
Simon Glass301e8032013-05-16 13:53:28 +0000404
Simon Glassbde67122017-08-05 10:28:39 -0600405 # Configuration with some Loadables
Simon Glass77b42672017-08-05 10:28:40 -0600406 with cons.log.section('Kernel + FDT + Ramdisk load + Loadables'):
Simon Glassbde67122017-08-05 10:28:39 -0600407 params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";'
Simon Glass77b42672017-08-05 10:28:40 -0600408 params['loadables1_load'] = ('load = <%#x>;' %
409 params['loadables1_addr'])
410 params['loadables2_load'] = ('load = <%#x>;' %
411 params['loadables2_addr'])
Simon Glassbde67122017-08-05 10:28:39 -0600412 fit = make_fit(mkimage, params)
Simon Glass77b42672017-08-05 10:28:40 -0600413 cons.restart_uboot()
414 output = cons.run_command_list(cmd.splitlines())
415 check_equal(loadables1, loadables1_out,
416 'Loadables1 (kernel) not loaded')
417 check_equal(loadables2, loadables2_out,
418 'Loadables2 (ramdisk) not loaded')
Karl Apsite657fd2d2015-05-21 09:52:50 -0400419
Simon Glass77b42672017-08-05 10:28:40 -0600420 cons = u_boot_console
421 try:
422 # We need to use our own device tree file. Remember to restore it
423 # afterwards.
424 old_dtb = cons.config.dtb
425 mkimage = cons.config.build_dir + '/tools/mkimage'
426 run_fit_test(mkimage)
427 finally:
428 # Go back to the original U-Boot with the correct dtb.
429 cons.config.dtb = old_dtb
430 cons.restart_uboot()