Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 1 | # Copyright (c) 2013, Google Inc. |
| 2 | # |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 4 | # |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 5 | # Sanity check of the FIT handling in U-Boot |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 6 | |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 7 | import os |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 8 | import pytest |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 9 | import struct |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 10 | import u_boot_utils as util |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 11 | |
| 12 | # Define a base ITS which we can adjust using % and a dictionary |
| 13 | base_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 Apsite | aad8dd7 | 2015-05-21 09:52:50 -0400 | [diff] [blame] | 30 | 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 Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 39 | 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 Apsite | aad8dd7 | 2015-05-21 09:52:50 -0400 | [diff] [blame] | 60 | 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 Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 69 | }; |
| 70 | configurations { |
| 71 | default = "conf@1"; |
| 72 | conf@1 { |
| 73 | kernel = "kernel@1"; |
| 74 | fdt = "fdt@1"; |
| 75 | %(ramdisk_config)s |
Karl Apsite | aad8dd7 | 2015-05-21 09:52:50 -0400 | [diff] [blame] | 76 | %(loadables_config)s |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 77 | }; |
| 78 | }; |
| 79 | }; |
| 80 | ''' |
| 81 | |
| 82 | # Define a base FDT - currently we don't use anything in this |
| 83 | base_fdt = ''' |
| 84 | /dts-v1/; |
| 85 | |
| 86 | / { |
| 87 | model = "Sandbox Verified Boot Test"; |
| 88 | compatible = "sandbox"; |
| 89 | |
Simon Glass | 546bab1 | 2016-02-24 09:14:44 -0700 | [diff] [blame] | 90 | reset@0 { |
| 91 | compatible = "sandbox,reset"; |
| 92 | }; |
| 93 | |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 94 | }; |
| 95 | ''' |
| 96 | |
Robert P. J. Day | 8445f73 | 2017-03-13 06:50:55 -0400 | [diff] [blame] | 97 | # 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 Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 99 | # we expect 'bootm' to write things. Then quit. |
| 100 | base_script = ''' |
Simon Glass | 943b417 | 2014-08-22 14:26:44 -0600 | [diff] [blame] | 101 | sb load hostfs 0 %(fit_addr)x %(fit)s |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 102 | fdt addr %(fit_addr)x |
| 103 | bootm start %(fit_addr)x |
| 104 | bootm loados |
Simon Glass | 73a0445 | 2014-12-02 13:17:31 -0700 | [diff] [blame] | 105 | sb save hostfs 0 %(kernel_addr)x %(kernel_out)s %(kernel_size)x |
| 106 | sb save hostfs 0 %(fdt_addr)x %(fdt_out)s %(fdt_size)x |
| 107 | sb save hostfs 0 %(ramdisk_addr)x %(ramdisk_out)s %(ramdisk_size)x |
Karl Apsite | aad8dd7 | 2015-05-21 09:52:50 -0400 | [diff] [blame] | 108 | sb save hostfs 0 %(loadables1_addr)x %(loadables1_out)s %(loadables1_size)x |
| 109 | sb save hostfs 0 %(loadables2_addr)x %(loadables2_out)s %(loadables2_size)x |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 110 | ''' |
| 111 | |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 112 | @pytest.mark.boardspec('sandbox') |
| 113 | @pytest.mark.buildconfigspec('fit_signature') |
Stephen Warren | 2079db3 | 2017-09-18 11:11:49 -0600 | [diff] [blame] | 114 | @pytest.mark.requiredtool('dtc') |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 115 | def test_fit(u_boot_console): |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 116 | def make_fname(leaf): |
| 117 | """Make a temporary filename |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 118 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 119 | Args: |
| 120 | leaf: Leaf name of file to create (within temporary directory) |
| 121 | Return: |
| 122 | Temporary filename |
| 123 | """ |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 124 | |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 125 | return os.path.join(cons.config.build_dir, leaf) |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 126 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 127 | def filesize(fname): |
| 128 | """Get the size of a file |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 129 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 130 | Args: |
| 131 | fname: Filename to check |
| 132 | Return: |
| 133 | Size of file in bytes |
| 134 | """ |
| 135 | return os.stat(fname).st_size |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 136 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 137 | def read_file(fname): |
| 138 | """Read the contents of a file |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 139 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 140 | Args: |
| 141 | fname: Filename to read |
| 142 | Returns: |
| 143 | Contents of file as a string |
| 144 | """ |
| 145 | with open(fname, 'r') as fd: |
| 146 | return fd.read() |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 147 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 148 | def make_dtb(): |
| 149 | """Make a sample .dts file and compile it to a .dtb |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 150 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 151 | Returns: |
| 152 | Filename of .dtb file created |
| 153 | """ |
| 154 | src = make_fname('u-boot.dts') |
| 155 | dtb = make_fname('u-boot.dtb') |
| 156 | with open(src, 'w') as fd: |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 157 | print >> fd, base_fdt |
| 158 | util.run_and_log(cons, ['dtc', src, '-O', 'dtb', '-o', dtb]) |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 159 | return dtb |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 160 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 161 | def make_its(params): |
| 162 | """Make a sample .its file with parameters embedded |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 163 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 164 | Args: |
| 165 | params: Dictionary containing parameters to embed in the %() strings |
| 166 | Returns: |
| 167 | Filename of .its file created |
| 168 | """ |
| 169 | its = make_fname('test.its') |
| 170 | with open(its, 'w') as fd: |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 171 | print >> fd, base_its % params |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 172 | return its |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 173 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 174 | def make_fit(mkimage, params): |
| 175 | """Make a sample .fit file ready for loading |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 176 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 177 | This creates a .its script with the selected parameters and uses mkimage to |
| 178 | turn this into a .fit image. |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 179 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 180 | Args: |
| 181 | mkimage: Filename of 'mkimage' utility |
| 182 | params: Dictionary containing parameters to embed in the %() strings |
| 183 | Return: |
| 184 | Filename of .fit file created |
| 185 | """ |
| 186 | fit = make_fname('test.fit') |
| 187 | its = make_its(params) |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 188 | util.run_and_log(cons, [mkimage, '-f', its, fit]) |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 189 | with open(make_fname('u-boot.dts'), 'w') as fd: |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 190 | print >> fd, base_fdt |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 191 | return fit |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 192 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 193 | def make_kernel(filename, text): |
| 194 | """Make a sample kernel with test data |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 195 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 196 | Args: |
| 197 | filename: the name of the file you want to create |
| 198 | Returns: |
| 199 | Full path and filename of the kernel it created |
| 200 | """ |
| 201 | fname = make_fname(filename) |
| 202 | data = '' |
| 203 | for i in range(100): |
| 204 | data += 'this %s %d is unlikely to boot\n' % (text, i) |
| 205 | with open(fname, 'w') as fd: |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 206 | print >> fd, data |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 207 | return fname |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 208 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 209 | def make_ramdisk(filename, text): |
| 210 | """Make a sample ramdisk with test data |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 211 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 212 | Returns: |
| 213 | Filename of ramdisk created |
| 214 | """ |
| 215 | fname = make_fname(filename) |
| 216 | data = '' |
| 217 | for i in range(100): |
| 218 | data += '%s %d was seldom used in the middle ages\n' % (text, i) |
| 219 | with open(fname, 'w') as fd: |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 220 | print >> fd, data |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 221 | return fname |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 222 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 223 | def find_matching(text, match): |
| 224 | """Find a match in a line of text, and return the unmatched line portion |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 225 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 226 | This is used to extract a part of a line from some text. The match string |
| 227 | is used to locate the line - we use the first line that contains that |
| 228 | match text. |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 229 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 230 | Once we find a match, we discard the match string itself from the line, |
| 231 | and return what remains. |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 232 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 233 | TODO: If this function becomes more generally useful, we could change it |
| 234 | to use regex and return groups. |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 235 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 236 | Args: |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 237 | text: Text to check (list of strings, one for each command issued) |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 238 | match: String to search for |
| 239 | Return: |
| 240 | String containing unmatched portion of line |
| 241 | Exceptions: |
| 242 | ValueError: If match is not found |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 243 | |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 244 | >>> find_matching(['first line:10', 'second_line:20'], 'first line:') |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 245 | '10' |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 246 | >>> find_matching(['first line:10', 'second_line:20'], 'second line') |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 247 | Traceback (most recent call last): |
| 248 | ... |
| 249 | ValueError: Test aborted |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 250 | >>> find_matching('first line:10\', 'second_line:20'], 'second_line:') |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 251 | '20' |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 252 | >>> find_matching('first line:10\', 'second_line:20\nthird_line:30'], |
| 253 | 'third_line:') |
| 254 | '30' |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 255 | """ |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 256 | __tracebackhide__ = True |
| 257 | for line in '\n'.join(text).splitlines(): |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 258 | pos = line.find(match) |
| 259 | if pos != -1: |
| 260 | return line[:pos] + line[pos + len(match):] |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 261 | |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 262 | pytest.fail("Expected '%s' but not found in output") |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 263 | |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 264 | def check_equal(expected_fname, actual_fname, failure_msg): |
| 265 | """Check that a file matches its expected contents |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 266 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 267 | Args: |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 268 | expected_fname: Filename containing expected contents |
| 269 | actual_fname: Filename containing actual contents |
| 270 | failure_msg: Message to print on failure |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 271 | """ |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 272 | expected_data = read_file(expected_fname) |
| 273 | actual_data = read_file(actual_fname) |
| 274 | assert expected_data == actual_data, failure_msg |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 275 | |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 276 | def check_not_equal(expected_fname, actual_fname, failure_msg): |
| 277 | """Check that a file does not match its expected contents |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 278 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 279 | Args: |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 280 | expected_fname: Filename containing expected contents |
| 281 | actual_fname: Filename containing actual contents |
| 282 | failure_msg: Message to print on failure |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 283 | """ |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 284 | expected_data = read_file(expected_fname) |
| 285 | actual_data = read_file(actual_fname) |
| 286 | assert expected_data != actual_data, failure_msg |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 287 | |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 288 | def run_fit_test(mkimage): |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 289 | """Basic sanity check of FIT loading in U-Boot |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 290 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 291 | TODO: Almost everything: |
| 292 | - hash algorithms - invalid hash/contents should be detected |
| 293 | - signature algorithms - invalid sig/contents should be detected |
| 294 | - compression |
| 295 | - checking that errors are detected like: |
| 296 | - image overwriting |
| 297 | - missing images |
| 298 | - invalid configurations |
| 299 | - incorrect os/arch/type fields |
| 300 | - empty data |
| 301 | - images too large/small |
| 302 | - invalid FDT (e.g. putting a random binary in instead) |
| 303 | - default configuration selection |
| 304 | - bootm command line parameters should have desired effect |
| 305 | - run code coverage to make sure we are testing all the code |
| 306 | """ |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 307 | # Set up invariant files |
| 308 | control_dtb = make_dtb() |
| 309 | kernel = make_kernel('test-kernel.bin', 'kernel') |
| 310 | ramdisk = make_ramdisk('test-ramdisk.bin', 'ramdisk') |
| 311 | loadables1 = make_kernel('test-loadables1.bin', 'lenrek') |
| 312 | loadables2 = make_ramdisk('test-loadables2.bin', 'ksidmar') |
| 313 | kernel_out = make_fname('kernel-out.bin') |
| 314 | fdt_out = make_fname('fdt-out.dtb') |
| 315 | ramdisk_out = make_fname('ramdisk-out.bin') |
| 316 | loadables1_out = make_fname('loadables1-out.bin') |
| 317 | loadables2_out = make_fname('loadables2-out.bin') |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 318 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 319 | # Set up basic parameters with default values |
| 320 | params = { |
| 321 | 'fit_addr' : 0x1000, |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 322 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 323 | 'kernel' : kernel, |
| 324 | 'kernel_out' : kernel_out, |
| 325 | 'kernel_addr' : 0x40000, |
| 326 | 'kernel_size' : filesize(kernel), |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 327 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 328 | 'fdt_out' : fdt_out, |
| 329 | 'fdt_addr' : 0x80000, |
| 330 | 'fdt_size' : filesize(control_dtb), |
| 331 | 'fdt_load' : '', |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 332 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 333 | 'ramdisk' : ramdisk, |
| 334 | 'ramdisk_out' : ramdisk_out, |
| 335 | 'ramdisk_addr' : 0xc0000, |
| 336 | 'ramdisk_size' : filesize(ramdisk), |
| 337 | 'ramdisk_load' : '', |
| 338 | 'ramdisk_config' : '', |
Karl Apsite | aad8dd7 | 2015-05-21 09:52:50 -0400 | [diff] [blame] | 339 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 340 | 'loadables1' : loadables1, |
| 341 | 'loadables1_out' : loadables1_out, |
| 342 | 'loadables1_addr' : 0x100000, |
| 343 | 'loadables1_size' : filesize(loadables1), |
| 344 | 'loadables1_load' : '', |
Karl Apsite | aad8dd7 | 2015-05-21 09:52:50 -0400 | [diff] [blame] | 345 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 346 | 'loadables2' : loadables2, |
| 347 | 'loadables2_out' : loadables2_out, |
| 348 | 'loadables2_addr' : 0x140000, |
| 349 | 'loadables2_size' : filesize(loadables2), |
| 350 | 'loadables2_load' : '', |
Karl Apsite | aad8dd7 | 2015-05-21 09:52:50 -0400 | [diff] [blame] | 351 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 352 | 'loadables_config' : '', |
| 353 | } |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 354 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 355 | # Make a basic FIT and a script to load it |
| 356 | fit = make_fit(mkimage, params) |
| 357 | params['fit'] = fit |
| 358 | cmd = base_script % params |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 359 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 360 | # First check that we can load a kernel |
| 361 | # We could perhaps reduce duplication with some loss of readability |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 362 | cons.config.dtb = control_dtb |
| 363 | cons.restart_uboot() |
| 364 | with cons.log.section('Kernel load'): |
| 365 | output = cons.run_command_list(cmd.splitlines()) |
| 366 | check_equal(kernel, kernel_out, 'Kernel not loaded') |
| 367 | check_not_equal(control_dtb, fdt_out, |
| 368 | 'FDT loaded but should be ignored') |
| 369 | check_not_equal(ramdisk, ramdisk_out, |
| 370 | 'Ramdisk loaded but should not be') |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 371 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 372 | # Find out the offset in the FIT where U-Boot has found the FDT |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 373 | line = find_matching(output, 'Booting using the fdt blob at ') |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 374 | fit_offset = int(line, 16) - params['fit_addr'] |
| 375 | fdt_magic = struct.pack('>L', 0xd00dfeed) |
| 376 | data = read_file(fit) |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 377 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 378 | # Now find where it actually is in the FIT (skip the first word) |
| 379 | real_fit_offset = data.find(fdt_magic, 4) |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 380 | assert fit_offset == real_fit_offset, ( |
| 381 | 'U-Boot loaded FDT from offset %#x, FDT is actually at %#x' % |
| 382 | (fit_offset, real_fit_offset)) |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 383 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 384 | # Now a kernel and an FDT |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 385 | with cons.log.section('Kernel + FDT load'): |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 386 | params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr'] |
| 387 | fit = make_fit(mkimage, params) |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 388 | cons.restart_uboot() |
| 389 | output = cons.run_command_list(cmd.splitlines()) |
| 390 | check_equal(kernel, kernel_out, 'Kernel not loaded') |
| 391 | check_equal(control_dtb, fdt_out, 'FDT not loaded') |
| 392 | check_not_equal(ramdisk, ramdisk_out, |
| 393 | 'Ramdisk loaded but should not be') |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 394 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 395 | # Try a ramdisk |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 396 | with cons.log.section('Kernel + FDT + Ramdisk load'): |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 397 | params['ramdisk_config'] = 'ramdisk = "ramdisk@1";' |
| 398 | params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr'] |
| 399 | fit = make_fit(mkimage, params) |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 400 | cons.restart_uboot() |
| 401 | output = cons.run_command_list(cmd.splitlines()) |
| 402 | check_equal(ramdisk, ramdisk_out, 'Ramdisk not loaded') |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 403 | |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 404 | # Configuration with some Loadables |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 405 | with cons.log.section('Kernel + FDT + Ramdisk load + Loadables'): |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 406 | params['loadables_config'] = 'loadables = "kernel@2", "ramdisk@2";' |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 407 | params['loadables1_load'] = ('load = <%#x>;' % |
| 408 | params['loadables1_addr']) |
| 409 | params['loadables2_load'] = ('load = <%#x>;' % |
| 410 | params['loadables2_addr']) |
Simon Glass | 6867ec0 | 2017-08-05 10:28:39 -0600 | [diff] [blame] | 411 | fit = make_fit(mkimage, params) |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 412 | cons.restart_uboot() |
| 413 | output = cons.run_command_list(cmd.splitlines()) |
| 414 | check_equal(loadables1, loadables1_out, |
| 415 | 'Loadables1 (kernel) not loaded') |
| 416 | check_equal(loadables2, loadables2_out, |
| 417 | 'Loadables2 (ramdisk) not loaded') |
Simon Glass | db74919 | 2013-05-16 13:53:28 +0000 | [diff] [blame] | 418 | |
Simon Glass | fe39411 | 2017-08-05 10:28:40 -0600 | [diff] [blame] | 419 | cons = u_boot_console |
| 420 | try: |
| 421 | # We need to use our own device tree file. Remember to restore it |
| 422 | # afterwards. |
| 423 | old_dtb = cons.config.dtb |
| 424 | mkimage = cons.config.build_dir + '/tools/mkimage' |
| 425 | run_fit_test(mkimage) |
| 426 | finally: |
| 427 | # Go back to the original U-Boot with the correct dtb. |
| 428 | cons.config.dtb = old_dtb |
| 429 | cons.restart_uboot() |