blob: 7e6b96dae45a5665d2a50f3f9524093156642e0a [file] [log] [blame]
Simon Glass301e8032013-05-16 13:53:28 +00001# Copyright (c) 2013, Google Inc.
2#
Wolfgang Denk1a459662013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass301e8032013-05-16 13:53:28 +00004#
Simon Glass77b42672017-08-05 10:28:40 -06005# Sanity check of the FIT handling in U-Boot
Simon Glass301e8032013-05-16 13:53:28 +00006
Simon Glass301e8032013-05-16 13:53:28 +00007import os
Simon Glass77b42672017-08-05 10:28:40 -06008import pytest
Simon Glass301e8032013-05-16 13:53:28 +00009import struct
Simon Glass77b42672017-08-05 10:28:40 -060010import u_boot_utils as util
Simon Glass301e8032013-05-16 13:53:28 +000011
12# Define a base ITS which we can adjust using % and a dictionary
13base_its = '''
14/dts-v1/;
15
16/ {
17 description = "Chrome OS kernel image with one or more FDT blobs";
18 #address-cells = <1>;
19
20 images {
21 kernel@1 {
22 data = /incbin/("%(kernel)s");
23 type = "kernel";
24 arch = "sandbox";
25 os = "linux";
26 compression = "none";
27 load = <0x40000>;
28 entry = <0x8>;
29 };
Karl Apsite657fd2d2015-05-21 09:52:50 -040030 kernel@2 {
31 data = /incbin/("%(loadables1)s");
32 type = "kernel";
33 arch = "sandbox";
34 os = "linux";
35 compression = "none";
36 %(loadables1_load)s
37 entry = <0x0>;
38 };
Simon Glass301e8032013-05-16 13:53:28 +000039 fdt@1 {
40 description = "snow";
41 data = /incbin/("u-boot.dtb");
42 type = "flat_dt";
43 arch = "sandbox";
44 %(fdt_load)s
45 compression = "none";
46 signature@1 {
47 algo = "sha1,rsa2048";
48 key-name-hint = "dev";
49 };
50 };
51 ramdisk@1 {
52 description = "snow";
53 data = /incbin/("%(ramdisk)s");
54 type = "ramdisk";
55 arch = "sandbox";
56 os = "linux";
57 %(ramdisk_load)s
58 compression = "none";
59 };
Karl Apsite657fd2d2015-05-21 09:52:50 -040060 ramdisk@2 {
61 description = "snow";
62 data = /incbin/("%(loadables2)s");
63 type = "ramdisk";
64 arch = "sandbox";
65 os = "linux";
66 %(loadables2_load)s
67 compression = "none";
68 };
Simon Glass301e8032013-05-16 13:53:28 +000069 };
70 configurations {
71 default = "conf@1";
72 conf@1 {
73 kernel = "kernel@1";
74 fdt = "fdt@1";
75 %(ramdisk_config)s
Karl Apsite657fd2d2015-05-21 09:52:50 -040076 %(loadables_config)s
Simon Glass301e8032013-05-16 13:53:28 +000077 };
78 };
79};
80'''
81
82# Define a base FDT - currently we don't use anything in this
83base_fdt = '''
84/dts-v1/;
85
86/ {
87 model = "Sandbox Verified Boot Test";
88 compatible = "sandbox";
89
Simon Glass0edd82e2016-02-24 09:14:44 -070090 reset@0 {
91 compatible = "sandbox,reset";
92 };
93
Simon Glass301e8032013-05-16 13:53:28 +000094};
95'''
96
Robert P. J. Dayb28c5fc2017-03-13 06:50:55 -040097# 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
Simon Glass301e8032013-05-16 13:53:28 +000099# we expect 'bootm' to write things. Then quit.
100base_script = '''
Simon Glassdfe6f4d2014-08-22 14:26:44 -0600101sb load hostfs 0 %(fit_addr)x %(fit)s
Simon Glass301e8032013-05-16 13:53:28 +0000102fdt addr %(fit_addr)x
103bootm start %(fit_addr)x
104bootm loados
Simon Glassb5493d12014-12-02 13:17:31 -0700105sb save hostfs 0 %(kernel_addr)x %(kernel_out)s %(kernel_size)x
106sb save hostfs 0 %(fdt_addr)x %(fdt_out)s %(fdt_size)x
107sb save hostfs 0 %(ramdisk_addr)x %(ramdisk_out)s %(ramdisk_size)x
Karl Apsite657fd2d2015-05-21 09:52:50 -0400108sb save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
109sb save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
Simon Glass301e8032013-05-16 13:53:28 +0000110'''
111
Simon Glass77b42672017-08-05 10:28:40 -0600112@pytest.mark.boardspec('sandbox')
113@pytest.mark.buildconfigspec('fit_signature')
114def test_fit(u_boot_console):
Simon Glassbde67122017-08-05 10:28:39 -0600115 def make_fname(leaf):
116 """Make a temporary filename
117
118 Args:
119 leaf: Leaf name of file to create (within temporary directory)
120 Return:
121 Temporary filename
122 """
Simon Glassbde67122017-08-05 10:28:39 -0600123
Simon Glass77b42672017-08-05 10:28:40 -0600124 return os.path.join(cons.config.build_dir, leaf)
Simon Glassbde67122017-08-05 10:28:39 -0600125
126 def filesize(fname):
127 """Get the size of a file
128
129 Args:
130 fname: Filename to check
131 Return:
132 Size of file in bytes
133 """
134 return os.stat(fname).st_size
135
136 def read_file(fname):
137 """Read the contents of a file
138
139 Args:
140 fname: Filename to read
141 Returns:
142 Contents of file as a string
143 """
144 with open(fname, 'r') as fd:
145 return fd.read()
146
147 def make_dtb():
148 """Make a sample .dts file and compile it to a .dtb
149
150 Returns:
151 Filename of .dtb file created
152 """
153 src = make_fname('u-boot.dts')
154 dtb = make_fname('u-boot.dtb')
155 with open(src, 'w') as fd:
Simon Glass77b42672017-08-05 10:28:40 -0600156 print >> fd, base_fdt
157 util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
Simon Glassbde67122017-08-05 10:28:39 -0600158 return dtb
159
160 def make_its(params):
161 """Make a sample .its file with parameters embedded
162
163 Args:
164 params: Dictionary containing parameters to embed in the %() strings
165 Returns:
166 Filename of .its file created
167 """
168 its = make_fname('test.its')
169 with open(its, 'w') as fd:
Simon Glass77b42672017-08-05 10:28:40 -0600170 print >> fd, base_its % params
Simon Glassbde67122017-08-05 10:28:39 -0600171 return its
172
173 def make_fit(mkimage, params):
174 """Make a sample .fit file ready for loading
175
176 This creates a .its script with the selected parameters and uses mkimage to
177 turn this into a .fit image.
178
179 Args:
180 mkimage: Filename of 'mkimage' utility
181 params: Dictionary containing parameters to embed in the %() strings
182 Return:
183 Filename of .fit file created
184 """
185 fit = make_fname('test.fit')
186 its = make_its(params)
Simon Glass77b42672017-08-05 10:28:40 -0600187 util.run_and_log(cons, [mkimage, '-f', its, fit])
Simon Glassbde67122017-08-05 10:28:39 -0600188 with open(make_fname('u-boot.dts'), 'w') as fd:
Simon Glass77b42672017-08-05 10:28:40 -0600189 print >> fd, base_fdt
Simon Glassbde67122017-08-05 10:28:39 -0600190 return fit
191
192 def make_kernel(filename, text):
193 """Make a sample kernel with test data
194
195 Args:
196 filename: the name of the file you want to create
197 Returns:
198 Full path and filename of the kernel it created
199 """
200 fname = make_fname(filename)
201 data = ''
202 for i in range(100):
203 data += 'this %s %d is unlikely to boot\n' % (text, i)
204 with open(fname, 'w') as fd:
Simon Glass77b42672017-08-05 10:28:40 -0600205 print >> fd, data
Simon Glassbde67122017-08-05 10:28:39 -0600206 return fname
207
208 def make_ramdisk(filename, text):
209 """Make a sample ramdisk with test data
210
211 Returns:
212 Filename of ramdisk created
213 """
214 fname = make_fname(filename)
215 data = ''
216 for i in range(100):
217 data += '%s %d was seldom used in the middle ages\n' % (text, i)
218 with open(fname, 'w') as fd:
Simon Glass77b42672017-08-05 10:28:40 -0600219 print >> fd, data
Simon Glassbde67122017-08-05 10:28:39 -0600220 return fname
221
222 def find_matching(text, match):
223 """Find a match in a line of text, and return the unmatched line portion
224
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
227 match text.
228
229 Once we find a match, we discard the match string itself from the line,
230 and return what remains.
231
232 TODO: If this function becomes more generally useful, we could change it
233 to use regex and return groups.
234
235 Args:
Simon Glass77b42672017-08-05 10:28:40 -0600236 text: Text to check (list of strings, one for each command issued)
Simon Glassbde67122017-08-05 10:28:39 -0600237 match: String to search for
238 Return:
239 String containing unmatched portion of line
240 Exceptions:
241 ValueError: If match is not found
242
Simon Glass77b42672017-08-05 10:28:40 -0600243 >>> find_matching(['first line:10', 'second_line:20'], 'first line:')
Simon Glassbde67122017-08-05 10:28:39 -0600244 '10'
Simon Glass77b42672017-08-05 10:28:40 -0600245 >>> find_matching(['first line:10', 'second_line:20'], 'second line')
Simon Glassbde67122017-08-05 10:28:39 -0600246 Traceback (most recent call last):
247 ...
248 ValueError: Test aborted
Simon Glass77b42672017-08-05 10:28:40 -0600249 >>> find_matching('first line:10\', 'second_line:20'], 'second_line:')
Simon Glassbde67122017-08-05 10:28:39 -0600250 '20'
Simon Glass77b42672017-08-05 10:28:40 -0600251 >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'],
252 'third_line:')
253 '30'
Simon Glassbde67122017-08-05 10:28:39 -0600254 """
Simon Glass77b42672017-08-05 10:28:40 -0600255 __tracebackhide__ = True
256 for line in '\n'.join(text).splitlines():
Simon Glassbde67122017-08-05 10:28:39 -0600257 pos = line.find(match)
258 if pos != -1:
259 return line[:pos] + line[pos + len(match):]
260
Simon Glass77b42672017-08-05 10:28:40 -0600261 pytest.fail("Expected '%s' but not found in output")
Simon Glassbde67122017-08-05 10:28:39 -0600262
Simon Glass77b42672017-08-05 10:28:40 -0600263 def check_equal(expected_fname, actual_fname, failure_msg):
264 """Check that a file matches its expected contents
Simon Glassbde67122017-08-05 10:28:39 -0600265
266 Args:
Simon Glass77b42672017-08-05 10:28:40 -0600267 expected_fname: Filename containing expected contents
268 actual_fname: Filename containing actual contents
269 failure_msg: Message to print on failure
Simon Glassbde67122017-08-05 10:28:39 -0600270 """
Simon Glass77b42672017-08-05 10:28:40 -0600271 expected_data = read_file(expected_fname)
272 actual_data = read_file(actual_fname)
273 assert expected_data == actual_data, failure_msg
Simon Glassbde67122017-08-05 10:28:39 -0600274
Simon Glass77b42672017-08-05 10:28:40 -0600275 def check_not_equal(expected_fname, actual_fname, failure_msg):
276 """Check that a file does not match its expected contents
Simon Glassbde67122017-08-05 10:28:39 -0600277
278 Args:
Simon Glass77b42672017-08-05 10:28:40 -0600279 expected_fname: Filename containing expected contents
280 actual_fname: Filename containing actual contents
281 failure_msg: Message to print on failure
Simon Glassbde67122017-08-05 10:28:39 -0600282 """
Simon Glass77b42672017-08-05 10:28:40 -0600283 expected_data = read_file(expected_fname)
284 actual_data = read_file(actual_fname)
285 assert expected_data != actual_data, failure_msg
Simon Glasscc447722014-12-02 13:17:32 -0700286
Simon Glass77b42672017-08-05 10:28:40 -0600287 def run_fit_test(mkimage):
Simon Glassbde67122017-08-05 10:28:39 -0600288 """Basic sanity check of FIT loading in U-Boot
Simon Glass301e8032013-05-16 13:53:28 +0000289
Simon Glassbde67122017-08-05 10:28:39 -0600290 TODO: Almost everything:
291 - hash algorithms - invalid hash/contents should be detected
292 - signature algorithms - invalid sig/contents should be detected
293 - compression
294 - checking that errors are detected like:
295 - image overwriting
296 - missing images
297 - invalid configurations
298 - incorrect os/arch/type fields
299 - empty data
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
305 """
Simon Glassbde67122017-08-05 10:28:39 -0600306 # 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')
Simon Glass301e8032013-05-16 13:53:28 +0000317
Simon Glassbde67122017-08-05 10:28:39 -0600318 # Set up basic parameters with default values
319 params = {
320 'fit_addr' : 0x1000,
Simon Glass301e8032013-05-16 13:53:28 +0000321
Simon Glassbde67122017-08-05 10:28:39 -0600322 'kernel' : kernel,
323 'kernel_out' : kernel_out,
324 'kernel_addr' : 0x40000,
325 'kernel_size' : filesize(kernel),
Simon Glass301e8032013-05-16 13:53:28 +0000326
Simon Glassbde67122017-08-05 10:28:39 -0600327 'fdt_out' : fdt_out,
328 'fdt_addr' : 0x80000,
329 'fdt_size' : filesize(control_dtb),
330 'fdt_load' : '',
Simon Glass301e8032013-05-16 13:53:28 +0000331
Simon Glassbde67122017-08-05 10:28:39 -0600332 'ramdisk' : ramdisk,
333 'ramdisk_out' : ramdisk_out,
334 'ramdisk_addr' : 0xc0000,
335 'ramdisk_size' : filesize(ramdisk),
336 'ramdisk_load' : '',
337 'ramdisk_config' : '',
Simon Glass301e8032013-05-16 13:53:28 +0000338
Simon Glassbde67122017-08-05 10:28:39 -0600339 'loadables1' : loadables1,
340 'loadables1_out' : loadables1_out,
341 'loadables1_addr' : 0x100000,
342 'loadables1_size' : filesize(loadables1),
343 'loadables1_load' : '',
Simon Glass301e8032013-05-16 13:53:28 +0000344
Simon Glassbde67122017-08-05 10:28:39 -0600345 'loadables2' : loadables2,
346 'loadables2_out' : loadables2_out,
347 'loadables2_addr' : 0x140000,
348 'loadables2_size' : filesize(loadables2),
349 'loadables2_load' : '',
Simon Glass301e8032013-05-16 13:53:28 +0000350
Simon Glassbde67122017-08-05 10:28:39 -0600351 'loadables_config' : '',
352 }
Simon Glass301e8032013-05-16 13:53:28 +0000353
Simon Glassbde67122017-08-05 10:28:39 -0600354 # Make a basic FIT and a script to load it
355 fit = make_fit(mkimage, params)
356 params['fit'] = fit
357 cmd = base_script % params
Simon Glass301e8032013-05-16 13:53:28 +0000358
Simon Glassbde67122017-08-05 10:28:39 -0600359 # First check that we can load a kernel
360 # We could perhaps reduce duplication with some loss of readability
Simon Glass77b42672017-08-05 10:28:40 -0600361 cons.config.dtb = control_dtb
362 cons.restart_uboot()
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')
Simon Glass301e8032013-05-16 13:53:28 +0000370
Simon Glassbde67122017-08-05 10:28:39 -0600371 # Find out the offset in the FIT where U-Boot has found the FDT
Simon Glass77b42672017-08-05 10:28:40 -0600372 line = find_matching(output, 'Booting using the fdt blob at ')
Simon Glassbde67122017-08-05 10:28:39 -0600373 fit_offset = int(line, 16) - params['fit_addr']
374 fdt_magic = struct.pack('>L', 0xd00dfeed)
375 data = read_file(fit)
Simon Glass301e8032013-05-16 13:53:28 +0000376
Simon Glassbde67122017-08-05 10:28:39 -0600377 # Now find where it actually is in the FIT (skip the first word)
378 real_fit_offset = data.find(fdt_magic, 4)
Simon Glass77b42672017-08-05 10:28:40 -0600379 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))
Simon Glass301e8032013-05-16 13:53:28 +0000382
Simon Glassbde67122017-08-05 10:28:39 -0600383 # Now a kernel and an FDT
Simon Glass77b42672017-08-05 10:28:40 -0600384 with cons.log.section('Kernel + FDT load'):
Simon Glassbde67122017-08-05 10:28:39 -0600385 params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
386 fit = make_fit(mkimage, params)
Simon Glass77b42672017-08-05 10:28:40 -0600387 cons.restart_uboot()
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')
Simon Glass301e8032013-05-16 13:53:28 +0000393
Simon Glassbde67122017-08-05 10:28:39 -0600394 # Try a ramdisk
Simon Glass77b42672017-08-05 10:28:40 -0600395 with cons.log.section('Kernel + FDT + Ramdisk load'):
Simon Glassbde67122017-08-05 10:28:39 -0600396 params['ramdisk_config'] = 'ramdisk = "ramdisk@1";'
397 params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr']
398 fit = make_fit(mkimage, params)
Simon Glass77b42672017-08-05 10:28:40 -0600399 cons.restart_uboot()
400 output = cons.run_command_list(cmd.splitlines())
401 check_equal(ramdisk, ramdisk_out, 'Ramdisk not loaded')
Simon Glass301e8032013-05-16 13:53:28 +0000402
Simon Glassbde67122017-08-05 10:28:39 -0600403 # Configuration with some Loadables
Simon Glass77b42672017-08-05 10:28:40 -0600404 with cons.log.section('Kernel + FDT + Ramdisk load + Loadables'):
Simon Glassbde67122017-08-05 10:28:39 -0600405 params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";'
Simon Glass77b42672017-08-05 10:28:40 -0600406 params['loadables1_load'] = ('load = <%#x>;' %
407 params['loadables1_addr'])
408 params['loadables2_load'] = ('load = <%#x>;' %
409 params['loadables2_addr'])
Simon Glassbde67122017-08-05 10:28:39 -0600410 fit = make_fit(mkimage, params)
Simon Glass77b42672017-08-05 10:28:40 -0600411 cons.restart_uboot()
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')
Karl Apsite657fd2d2015-05-21 09:52:50 -0400417
Simon Glass77b42672017-08-05 10:28:40 -0600418 cons = u_boot_console
419 try:
420 # We need to use our own device tree file. Remember to restore it
421 # afterwards.
422 old_dtb = cons.config.dtb
423 mkimage = cons.config.build_dir + '/tools/mkimage'
424 run_fit_test(mkimage)
425 finally:
426 # Go back to the original U-Boot with the correct dtb.
427 cons.config.dtb = old_dtb
428 cons.restart_uboot()