xref: /openbmc/u-boot/test/py/tests/test_fit.py (revision 8ee59472)
1# SPDX-License-Identifier:	GPL-2.0+
2# Copyright (c) 2013, Google Inc.
3#
4# Sanity check of the FIT handling in U-Boot
5
6import os
7import pytest
8import struct
9import u_boot_utils as util
10
11# Define a base ITS which we can adjust using % and a dictionary
12base_its = '''
13/dts-v1/;
14
15/ {
16        description = "Chrome OS kernel image with one or more FDT blobs";
17        #address-cells = <1>;
18
19        images {
20                kernel@1 {
21                        data = /incbin/("%(kernel)s");
22                        type = "kernel";
23                        arch = "sandbox";
24                        os = "linux";
25                        compression = "none";
26                        load = <0x40000>;
27                        entry = <0x8>;
28                };
29                kernel@2 {
30                        data = /incbin/("%(loadables1)s");
31                        type = "kernel";
32                        arch = "sandbox";
33                        os = "linux";
34                        compression = "none";
35                        %(loadables1_load)s
36                        entry = <0x0>;
37                };
38                fdt@1 {
39                        description = "snow";
40                        data = /incbin/("u-boot.dtb");
41                        type = "flat_dt";
42                        arch = "sandbox";
43                        %(fdt_load)s
44                        compression = "none";
45                        signature@1 {
46                                algo = "sha1,rsa2048";
47                                key-name-hint = "dev";
48                        };
49                };
50                ramdisk@1 {
51                        description = "snow";
52                        data = /incbin/("%(ramdisk)s");
53                        type = "ramdisk";
54                        arch = "sandbox";
55                        os = "linux";
56                        %(ramdisk_load)s
57                        compression = "none";
58                };
59                ramdisk@2 {
60                        description = "snow";
61                        data = /incbin/("%(loadables2)s");
62                        type = "ramdisk";
63                        arch = "sandbox";
64                        os = "linux";
65                        %(loadables2_load)s
66                        compression = "none";
67                };
68        };
69        configurations {
70                default = "conf@1";
71                conf@1 {
72                        kernel = "kernel@1";
73                        fdt = "fdt@1";
74                        %(ramdisk_config)s
75                        %(loadables_config)s
76                };
77        };
78};
79'''
80
81# Define a base FDT - currently we don't use anything in this
82base_fdt = '''
83/dts-v1/;
84
85/ {
86        model = "Sandbox Verified Boot Test";
87        compatible = "sandbox";
88
89	reset@0 {
90		compatible = "sandbox,reset";
91	};
92
93};
94'''
95
96# This is the U-Boot script that is run for each test. First load the FIT,
97# then run the 'bootm' command, then save out memory from the places where
98# we expect 'bootm' to write things. Then quit.
99base_script = '''
100sb load hostfs 0 %(fit_addr)x %(fit)s
101fdt addr %(fit_addr)x
102bootm start %(fit_addr)x
103bootm loados
104sb save hostfs 0 %(kernel_addr)x %(kernel_out)s %(kernel_size)x
105sb save hostfs 0 %(fdt_addr)x %(fdt_out)s %(fdt_size)x
106sb save hostfs 0 %(ramdisk_addr)x %(ramdisk_out)s %(ramdisk_size)x
107sb save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x
108sb save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x
109'''
110
111@pytest.mark.boardspec('sandbox')
112@pytest.mark.buildconfigspec('fit_signature')
113@pytest.mark.requiredtool('dtc')
114def test_fit(u_boot_console):
115    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        """
123
124        return os.path.join(cons.config.build_dir, leaf)
125
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:
156            print >> fd, base_fdt
157        util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb])
158        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:
170            print >> fd, base_its % params
171        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)
187        util.run_and_log(cons, [mkimage, '-f', its, fit])
188        with open(make_fname('u-boot.dts'), 'w') as fd:
189            print >> fd, base_fdt
190        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:
205            print >> fd, data
206        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:
219            print >> fd, data
220        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:
236            text: Text to check (list of strings, one for each command issued)
237            match: String to search for
238        Return:
239            String containing unmatched portion of line
240        Exceptions:
241            ValueError: If match is not found
242
243        >>> find_matching(['first line:10', 'second_line:20'], 'first line:')
244        '10'
245        >>> find_matching(['first line:10', 'second_line:20'], 'second line')
246        Traceback (most recent call last):
247          ...
248        ValueError: Test aborted
249        >>> find_matching('first line:10\', 'second_line:20'], 'second_line:')
250        '20'
251        >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'],
252                          'third_line:')
253        '30'
254        """
255        __tracebackhide__ = True
256        for line in '\n'.join(text).splitlines():
257            pos = line.find(match)
258            if pos != -1:
259                return line[:pos] + line[pos + len(match):]
260
261        pytest.fail("Expected '%s' but not found in output")
262
263    def check_equal(expected_fname, actual_fname, failure_msg):
264        """Check that a file matches its expected contents
265
266        Args:
267            expected_fname: Filename containing expected contents
268            actual_fname: Filename containing actual contents
269            failure_msg: Message to print on failure
270        """
271        expected_data = read_file(expected_fname)
272        actual_data = read_file(actual_fname)
273        assert expected_data == actual_data, failure_msg
274
275    def check_not_equal(expected_fname, actual_fname, failure_msg):
276        """Check that a file does not match its expected contents
277
278        Args:
279            expected_fname: Filename containing expected contents
280            actual_fname: Filename containing actual contents
281            failure_msg: Message to print on failure
282        """
283        expected_data = read_file(expected_fname)
284        actual_data = read_file(actual_fname)
285        assert expected_data != actual_data, failure_msg
286
287    def run_fit_test(mkimage):
288        """Basic sanity check of FIT loading in U-Boot
289
290        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        """
306        # 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')
317
318        # Set up basic parameters with default values
319        params = {
320            'fit_addr' : 0x1000,
321
322            'kernel' : kernel,
323            'kernel_out' : kernel_out,
324            'kernel_addr' : 0x40000,
325            'kernel_size' : filesize(kernel),
326
327            'fdt_out' : fdt_out,
328            'fdt_addr' : 0x80000,
329            'fdt_size' : filesize(control_dtb),
330            'fdt_load' : '',
331
332            'ramdisk' : ramdisk,
333            'ramdisk_out' : ramdisk_out,
334            'ramdisk_addr' : 0xc0000,
335            'ramdisk_size' : filesize(ramdisk),
336            'ramdisk_load' : '',
337            'ramdisk_config' : '',
338
339            'loadables1' : loadables1,
340            'loadables1_out' : loadables1_out,
341            'loadables1_addr' : 0x100000,
342            'loadables1_size' : filesize(loadables1),
343            'loadables1_load' : '',
344
345            'loadables2' : loadables2,
346            'loadables2_out' : loadables2_out,
347            'loadables2_addr' : 0x140000,
348            'loadables2_size' : filesize(loadables2),
349            'loadables2_load' : '',
350
351            'loadables_config' : '',
352        }
353
354        # 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
358
359        # First check that we can load a kernel
360        # We could perhaps reduce duplication with some loss of readability
361        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')
370
371            # Find out the offset in the FIT where U-Boot has found the FDT
372            line = find_matching(output, 'Booting using the fdt blob at ')
373            fit_offset = int(line, 16) - params['fit_addr']
374            fdt_magic = struct.pack('>L', 0xd00dfeed)
375            data = read_file(fit)
376
377            # Now find where it actually is in the FIT (skip the first word)
378            real_fit_offset = data.find(fdt_magic, 4)
379            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))
382
383        # Now a kernel and an FDT
384        with cons.log.section('Kernel + FDT load'):
385            params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr']
386            fit = make_fit(mkimage, params)
387            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')
393
394        # Try a ramdisk
395        with cons.log.section('Kernel + FDT + Ramdisk load'):
396            params['ramdisk_config'] = 'ramdisk = "ramdisk@1";'
397            params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr']
398            fit = make_fit(mkimage, params)
399            cons.restart_uboot()
400            output = cons.run_command_list(cmd.splitlines())
401            check_equal(ramdisk, ramdisk_out, 'Ramdisk not loaded')
402
403        # Configuration with some Loadables
404        with cons.log.section('Kernel + FDT + Ramdisk load + Loadables'):
405            params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";'
406            params['loadables1_load'] = ('load = <%#x>;' %
407                                         params['loadables1_addr'])
408            params['loadables2_load'] = ('load = <%#x>;' %
409                                         params['loadables2_addr'])
410            fit = make_fit(mkimage, params)
411            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')
417
418    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()
429