blob: 7e6b96dae45a5665d2a50f3f9524093156642e0a [file] [log] [blame]
Simon Glassdb749192013-05-16 13:53:28 +00001# Copyright (c) 2013, Google Inc.
2#
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glassdb749192013-05-16 13:53:28 +00004#
Simon Glassfe394112017-08-05 10:28:40 -06005# Sanity check of the FIT handling in U-Boot
Simon Glassdb749192013-05-16 13:53:28 +00006
Simon Glassdb749192013-05-16 13:53:28 +00007import os
Simon Glassfe394112017-08-05 10:28:40 -06008import pytest
Simon Glassdb749192013-05-16 13:53:28 +00009import struct
Simon Glassfe394112017-08-05 10:28:40 -060010import u_boot_utils as util
Simon Glassdb749192013-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 Apsiteaad8dd72015-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 Glassdb749192013-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 Apsiteaad8dd72015-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 Glassdb749192013-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 Apsiteaad8dd72015-05-21 09:52:50 -040076 %(loadables_config)s
Simon Glassdb749192013-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 Glass546bab12016-02-24 09:14:44 -070090 reset@0 {
91 compatible = "sandbox,reset";
92 };
93
Simon Glassdb749192013-05-16 13:53:28 +000094};
95'''
96
Robert P. J. Day8445f732017-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 Glassdb749192013-05-16 13:53:28 +000099# we expect 'bootm' to write things. Then quit.
100base_script = '''
Simon Glass943b4172014-08-22 14:26:44 -0600101sb load hostfs 0 %(fit_addr)x %(fit)s
Simon Glassdb749192013-05-16 13:53:28 +0000102fdt addr %(fit_addr)x
103bootm start %(fit_addr)x
104bootm loados
Simon Glass73a04452014-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 Apsiteaad8dd72015-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 Glassdb749192013-05-16 13:53:28 +0000110'''
111
Simon Glassfe394112017-08-05 10:28:40 -0600112@pytest.mark.boardspec('sandbox')
113@pytest.mark.buildconfigspec('fit_signature')
114def test_fit(u_boot_console):
Simon Glass6867ec02017-08-05 10:28:39 -0600115 def make_fname(leaf):
116 """Make a temporary filename
Simon Glassdb749192013-05-16 13:53:28 +0000117
Simon Glass6867ec02017-08-05 10:28:39 -0600118 Args:
119 leaf: Leaf name of file to create (within temporary directory)
120 Return:
121 Temporary filename
122 """
Simon Glassdb749192013-05-16 13:53:28 +0000123
Simon Glassfe394112017-08-05 10:28:40 -0600124 return os.path.join(cons.config.build_dir, leaf)
Simon Glassdb749192013-05-16 13:53:28 +0000125
Simon Glass6867ec02017-08-05 10:28:39 -0600126 def filesize(fname):
127 """Get the size of a file
Simon Glassdb749192013-05-16 13:53:28 +0000128
Simon Glass6867ec02017-08-05 10:28:39 -0600129 Args:
130 fname: Filename to check
131 Return:
132 Size of file in bytes
133 """
134 return os.stat(fname).st_size
Simon Glassdb749192013-05-16 13:53:28 +0000135
Simon Glass6867ec02017-08-05 10:28:39 -0600136 def read_file(fname):
137 """Read the contents of a file
Simon Glassdb749192013-05-16 13:53:28 +0000138
Simon Glass6867ec02017-08-05 10:28:39 -0600139 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()
Simon Glassdb749192013-05-16 13:53:28 +0000146
Simon Glass6867ec02017-08-05 10:28:39 -0600147 def make_dtb():
148 """Make a sample .dts file and compile it to a .dtb
Simon Glassdb749192013-05-16 13:53:28 +0000149
Simon Glass6867ec02017-08-05 10:28:39 -0600150 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 Glassfe394112017-08-05 10:28:40 -0600156 print >> fd, base_fdt
157 util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
Simon Glass6867ec02017-08-05 10:28:39 -0600158 return dtb
Simon Glassdb749192013-05-16 13:53:28 +0000159
Simon Glass6867ec02017-08-05 10:28:39 -0600160 def make_its(params):
161 """Make a sample .its file with parameters embedded
Simon Glassdb749192013-05-16 13:53:28 +0000162
Simon Glass6867ec02017-08-05 10:28:39 -0600163 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 Glassfe394112017-08-05 10:28:40 -0600170 print >> fd, base_its % params
Simon Glass6867ec02017-08-05 10:28:39 -0600171 return its
Simon Glassdb749192013-05-16 13:53:28 +0000172
Simon Glass6867ec02017-08-05 10:28:39 -0600173 def make_fit(mkimage, params):
174 """Make a sample .fit file ready for loading
Simon Glassdb749192013-05-16 13:53:28 +0000175
Simon Glass6867ec02017-08-05 10:28:39 -0600176 This creates a .its script with the selected parameters and uses mkimage to
177 turn this into a .fit image.
Simon Glassdb749192013-05-16 13:53:28 +0000178
Simon Glass6867ec02017-08-05 10:28:39 -0600179 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 Glassfe394112017-08-05 10:28:40 -0600187 util.run_and_log(cons, [mkimage, '-f', its, fit])
Simon Glass6867ec02017-08-05 10:28:39 -0600188 with open(make_fname('u-boot.dts'), 'w') as fd:
Simon Glassfe394112017-08-05 10:28:40 -0600189 print >> fd, base_fdt
Simon Glass6867ec02017-08-05 10:28:39 -0600190 return fit
Simon Glassdb749192013-05-16 13:53:28 +0000191
Simon Glass6867ec02017-08-05 10:28:39 -0600192 def make_kernel(filename, text):
193 """Make a sample kernel with test data
Simon Glassdb749192013-05-16 13:53:28 +0000194
Simon Glass6867ec02017-08-05 10:28:39 -0600195 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 Glassfe394112017-08-05 10:28:40 -0600205 print >> fd, data
Simon Glass6867ec02017-08-05 10:28:39 -0600206 return fname
Simon Glassdb749192013-05-16 13:53:28 +0000207
Simon Glass6867ec02017-08-05 10:28:39 -0600208 def make_ramdisk(filename, text):
209 """Make a sample ramdisk with test data
Simon Glassdb749192013-05-16 13:53:28 +0000210
Simon Glass6867ec02017-08-05 10:28:39 -0600211 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 Glassfe394112017-08-05 10:28:40 -0600219 print >> fd, data
Simon Glass6867ec02017-08-05 10:28:39 -0600220 return fname
Simon Glassdb749192013-05-16 13:53:28 +0000221
Simon Glass6867ec02017-08-05 10:28:39 -0600222 def find_matching(text, match):
223 """Find a match in a line of text, and return the unmatched line portion
Simon Glassdb749192013-05-16 13:53:28 +0000224
Simon Glass6867ec02017-08-05 10:28:39 -0600225 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.
Simon Glassdb749192013-05-16 13:53:28 +0000228
Simon Glass6867ec02017-08-05 10:28:39 -0600229 Once we find a match, we discard the match string itself from the line,
230 and return what remains.
Simon Glassdb749192013-05-16 13:53:28 +0000231
Simon Glass6867ec02017-08-05 10:28:39 -0600232 TODO: If this function becomes more generally useful, we could change it
233 to use regex and return groups.
Simon Glassdb749192013-05-16 13:53:28 +0000234
Simon Glass6867ec02017-08-05 10:28:39 -0600235 Args:
Simon Glassfe394112017-08-05 10:28:40 -0600236 text: Text to check (list of strings, one for each command issued)
Simon Glass6867ec02017-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
Simon Glassdb749192013-05-16 13:53:28 +0000242
Simon Glassfe394112017-08-05 10:28:40 -0600243 >>> find_matching(['first line:10', 'second_line:20'], 'first line:')
Simon Glass6867ec02017-08-05 10:28:39 -0600244 '10'
Simon Glassfe394112017-08-05 10:28:40 -0600245 >>> find_matching(['first line:10', 'second_line:20'], 'second line')
Simon Glass6867ec02017-08-05 10:28:39 -0600246 Traceback (most recent call last):
247 ...
248 ValueError: Test aborted
Simon Glassfe394112017-08-05 10:28:40 -0600249 >>> find_matching('first line:10\', 'second_line:20'], 'second_line:')
Simon Glass6867ec02017-08-05 10:28:39 -0600250 '20'
Simon Glassfe394112017-08-05 10:28:40 -0600251 >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'],
252 'third_line:')
253 '30'
Simon Glass6867ec02017-08-05 10:28:39 -0600254 """
Simon Glassfe394112017-08-05 10:28:40 -0600255 __tracebackhide__ = True
256 for line in '\n'.join(text).splitlines():
Simon Glass6867ec02017-08-05 10:28:39 -0600257 pos = line.find(match)
258 if pos != -1:
259 return line[:pos] + line[pos + len(match):]
Simon Glassdb749192013-05-16 13:53:28 +0000260
Simon Glassfe394112017-08-05 10:28:40 -0600261 pytest.fail("Expected '%s' but not found in output")
Simon Glassdb749192013-05-16 13:53:28 +0000262
Simon Glassfe394112017-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 Glassdb749192013-05-16 13:53:28 +0000265
Simon Glass6867ec02017-08-05 10:28:39 -0600266 Args:
Simon Glassfe394112017-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 Glass6867ec02017-08-05 10:28:39 -0600270 """
Simon Glassfe394112017-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 Glassdb749192013-05-16 13:53:28 +0000274
Simon Glassfe394112017-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 Glassdb749192013-05-16 13:53:28 +0000277
Simon Glass6867ec02017-08-05 10:28:39 -0600278 Args:
Simon Glassfe394112017-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 Glass6867ec02017-08-05 10:28:39 -0600282 """
Simon Glassfe394112017-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 Glassdb749192013-05-16 13:53:28 +0000286
Simon Glassfe394112017-08-05 10:28:40 -0600287 def run_fit_test(mkimage):
Simon Glass6867ec02017-08-05 10:28:39 -0600288 """Basic sanity check of FIT loading in U-Boot
Simon Glassdb749192013-05-16 13:53:28 +0000289
Simon Glass6867ec02017-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 Glass6867ec02017-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 Glassdb749192013-05-16 13:53:28 +0000317
Simon Glass6867ec02017-08-05 10:28:39 -0600318 # Set up basic parameters with default values
319 params = {
320 'fit_addr' : 0x1000,
Simon Glassdb749192013-05-16 13:53:28 +0000321
Simon Glass6867ec02017-08-05 10:28:39 -0600322 'kernel' : kernel,
323 'kernel_out' : kernel_out,
324 'kernel_addr' : 0x40000,
325 'kernel_size' : filesize(kernel),
Simon Glassdb749192013-05-16 13:53:28 +0000326
Simon Glass6867ec02017-08-05 10:28:39 -0600327 'fdt_out' : fdt_out,
328 'fdt_addr' : 0x80000,
329 'fdt_size' : filesize(control_dtb),
330 'fdt_load' : '',
Simon Glassdb749192013-05-16 13:53:28 +0000331
Simon Glass6867ec02017-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' : '',
Karl Apsiteaad8dd72015-05-21 09:52:50 -0400338
Simon Glass6867ec02017-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' : '',
Karl Apsiteaad8dd72015-05-21 09:52:50 -0400344
Simon Glass6867ec02017-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' : '',
Karl Apsiteaad8dd72015-05-21 09:52:50 -0400350
Simon Glass6867ec02017-08-05 10:28:39 -0600351 'loadables_config' : '',
352 }
Simon Glassdb749192013-05-16 13:53:28 +0000353
Simon Glass6867ec02017-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 Glassdb749192013-05-16 13:53:28 +0000358
Simon Glass6867ec02017-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 Glassfe394112017-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 Glassdb749192013-05-16 13:53:28 +0000370
Simon Glass6867ec02017-08-05 10:28:39 -0600371 # Find out the offset in the FIT where U-Boot has found the FDT
Simon Glassfe394112017-08-05 10:28:40 -0600372 line = find_matching(output, 'Booting using the fdt blob at ')
Simon Glass6867ec02017-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 Glassdb749192013-05-16 13:53:28 +0000376
Simon Glass6867ec02017-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 Glassfe394112017-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 Glassdb749192013-05-16 13:53:28 +0000382
Simon Glass6867ec02017-08-05 10:28:39 -0600383 # Now a kernel and an FDT
Simon Glassfe394112017-08-05 10:28:40 -0600384 with cons.log.section('Kernel + FDT load'):
Simon Glass6867ec02017-08-05 10:28:39 -0600385 params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
386 fit = make_fit(mkimage, params)
Simon Glassfe394112017-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 Glassdb749192013-05-16 13:53:28 +0000393
Simon Glass6867ec02017-08-05 10:28:39 -0600394 # Try a ramdisk
Simon Glassfe394112017-08-05 10:28:40 -0600395 with cons.log.section('Kernel + FDT + Ramdisk load'):
Simon Glass6867ec02017-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 Glassfe394112017-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 Glassdb749192013-05-16 13:53:28 +0000402
Simon Glass6867ec02017-08-05 10:28:39 -0600403 # Configuration with some Loadables
Simon Glassfe394112017-08-05 10:28:40 -0600404 with cons.log.section('Kernel + FDT + Ramdisk load + Loadables'):
Simon Glass6867ec02017-08-05 10:28:39 -0600405 params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";'
Simon Glassfe394112017-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 Glass6867ec02017-08-05 10:28:39 -0600410 fit = make_fit(mkimage, params)
Simon Glassfe394112017-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')
Simon Glassdb749192013-05-16 13:53:28 +0000417
Simon Glassfe394112017-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()