Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 5 | # To run a single test, change to this directory, and: |
| 6 | # |
| 7 | # python -m unittest func_test.TestFunctional.testHelp |
| 8 | |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 9 | import hashlib |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 10 | from optparse import OptionParser |
| 11 | import os |
| 12 | import shutil |
| 13 | import struct |
| 14 | import sys |
| 15 | import tempfile |
| 16 | import unittest |
| 17 | |
| 18 | import binman |
| 19 | import cmdline |
| 20 | import command |
| 21 | import control |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 22 | import elf |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 23 | import fdt |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 24 | import fdt_util |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 25 | import fmap_util |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 26 | import test_util |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 27 | import state |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 28 | import tools |
| 29 | import tout |
| 30 | |
| 31 | # Contents of test files, corresponding to different entry types |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 32 | U_BOOT_DATA = b'1234' |
| 33 | U_BOOT_IMG_DATA = b'img' |
| 34 | U_BOOT_SPL_DATA = b'56780123456789abcde' |
| 35 | U_BOOT_TPL_DATA = b'tpl' |
| 36 | BLOB_DATA = b'89' |
| 37 | ME_DATA = b'0abcd' |
| 38 | VGA_DATA = b'vga' |
| 39 | U_BOOT_DTB_DATA = b'udtb' |
| 40 | U_BOOT_SPL_DTB_DATA = b'spldtb' |
| 41 | U_BOOT_TPL_DTB_DATA = b'tpldtb' |
| 42 | X86_START16_DATA = b'start16' |
| 43 | X86_START16_SPL_DATA = b'start16spl' |
| 44 | X86_START16_TPL_DATA = b'start16tpl' |
| 45 | PPC_MPC85XX_BR_DATA = b'ppcmpc85xxbr' |
| 46 | U_BOOT_NODTB_DATA = b'nodtb with microcode pointer somewhere in here' |
| 47 | U_BOOT_SPL_NODTB_DATA = b'splnodtb with microcode pointer somewhere in here' |
| 48 | U_BOOT_TPL_NODTB_DATA = b'tplnodtb with microcode pointer somewhere in here' |
| 49 | FSP_DATA = b'fsp' |
| 50 | CMC_DATA = b'cmc' |
| 51 | VBT_DATA = b'vbt' |
| 52 | MRC_DATA = b'mrc' |
Simon Glass | bb74837 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 53 | TEXT_DATA = 'text' |
| 54 | TEXT_DATA2 = 'text2' |
| 55 | TEXT_DATA3 = 'text3' |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 56 | CROS_EC_RW_DATA = b'ecrw' |
| 57 | GBB_DATA = b'gbbd' |
| 58 | BMPBLK_DATA = b'bmp' |
| 59 | VBLOCK_DATA = b'vblk' |
| 60 | FILES_DATA = (b"sorry I'm late\nOh, don't bother apologising, I'm " + |
| 61 | b"sorry you're alive\n") |
Simon Glass | ff5c7e3 | 2019-07-08 13:18:42 -0600 | [diff] [blame] | 62 | COMPRESS_DATA = b'compress xxxxxxxxxxxxxxxxxxxxxx data' |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 63 | REFCODE_DATA = b'refcode' |
Simon Glass | ec127af | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 64 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 65 | |
| 66 | class TestFunctional(unittest.TestCase): |
| 67 | """Functional tests for binman |
| 68 | |
| 69 | Most of these use a sample .dts file to build an image and then check |
| 70 | that it looks correct. The sample files are in the test/ subdirectory |
| 71 | and are numbered. |
| 72 | |
| 73 | For each entry type a very small test file is created using fixed |
| 74 | string contents. This makes it easy to test that things look right, and |
| 75 | debug problems. |
| 76 | |
| 77 | In some cases a 'real' file must be used - these are also supplied in |
| 78 | the test/ diurectory. |
| 79 | """ |
| 80 | @classmethod |
| 81 | def setUpClass(self): |
Simon Glass | 4d5994f | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 82 | global entry |
| 83 | import entry |
| 84 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 85 | # Handle the case where argv[0] is 'python' |
| 86 | self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 87 | self._binman_pathname = os.path.join(self._binman_dir, 'binman') |
| 88 | |
| 89 | # Create a temporary directory for input files |
| 90 | self._indir = tempfile.mkdtemp(prefix='binmant.') |
| 91 | |
| 92 | # Create some test files |
| 93 | TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) |
| 94 | TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA) |
| 95 | TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 96 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 97 | TestFunctional._MakeInputFile('blobfile', BLOB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 98 | TestFunctional._MakeInputFile('me.bin', ME_DATA) |
| 99 | TestFunctional._MakeInputFile('vga.bin', VGA_DATA) |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 100 | self._ResetDtbs() |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 101 | TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA) |
Jagdish Gediya | 9d368f3 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 102 | TestFunctional._MakeInputFile('u-boot-br.bin', PPC_MPC85XX_BR_DATA) |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 103 | TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin', |
| 104 | X86_START16_SPL_DATA) |
Simon Glass | 35b384c | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 105 | TestFunctional._MakeInputFile('tpl/u-boot-x86-16bit-tpl.bin', |
| 106 | X86_START16_TPL_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 107 | TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA) |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 108 | TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin', |
| 109 | U_BOOT_SPL_NODTB_DATA) |
Simon Glass | f025363 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 110 | TestFunctional._MakeInputFile('tpl/u-boot-tpl-nodtb.bin', |
| 111 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 112 | TestFunctional._MakeInputFile('fsp.bin', FSP_DATA) |
| 113 | TestFunctional._MakeInputFile('cmc.bin', CMC_DATA) |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 114 | TestFunctional._MakeInputFile('vbt.bin', VBT_DATA) |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 115 | TestFunctional._MakeInputFile('mrc.bin', MRC_DATA) |
Simon Glass | ec127af | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 116 | TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA) |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 117 | TestFunctional._MakeInputDir('devkeys') |
| 118 | TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA) |
Simon Glass | 3ae192c | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 119 | TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 120 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 121 | # ELF file with a '_dt_ucode_base_size' symbol |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 122 | with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd: |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 123 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 124 | |
| 125 | # Intel flash descriptor file |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 126 | with open(self.TestFile('descriptor.bin'), 'rb') as fd: |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 127 | TestFunctional._MakeInputFile('descriptor.bin', fd.read()) |
| 128 | |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 129 | shutil.copytree(self.TestFile('files'), |
| 130 | os.path.join(self._indir, 'files')) |
| 131 | |
Simon Glass | 83d73c2 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 132 | TestFunctional._MakeInputFile('compress', COMPRESS_DATA) |
| 133 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 134 | @classmethod |
| 135 | def tearDownClass(self): |
| 136 | """Remove the temporary input directory and its contents""" |
| 137 | if self._indir: |
| 138 | shutil.rmtree(self._indir) |
| 139 | self._indir = None |
| 140 | |
| 141 | def setUp(self): |
| 142 | # Enable this to turn on debugging output |
| 143 | # tout.Init(tout.DEBUG) |
| 144 | command.test_result = None |
| 145 | |
| 146 | def tearDown(self): |
| 147 | """Remove the temporary output directory""" |
| 148 | tools._FinaliseForTest() |
| 149 | |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 150 | @classmethod |
| 151 | def _ResetDtbs(self): |
| 152 | TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) |
| 153 | TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA) |
| 154 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA) |
| 155 | |
Simon Glass | ee0c9a7 | 2019-07-08 13:18:48 -0600 | [diff] [blame^] | 156 | def _GetVerbosity(self): |
| 157 | """Check if verbosity should be enabled |
| 158 | |
| 159 | Returns: |
| 160 | list containing either: |
| 161 | - Verbosity flag (e.g. '-v2') if it is present on the cmd line |
| 162 | - nothing if the flag is not present |
| 163 | """ |
| 164 | for arg in sys.argv[1:]: |
| 165 | if arg.startswith('-v'): |
| 166 | return [arg] |
| 167 | return [] |
| 168 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 169 | def _RunBinman(self, *args, **kwargs): |
| 170 | """Run binman using the command line |
| 171 | |
| 172 | Args: |
| 173 | Arguments to pass, as a list of strings |
| 174 | kwargs: Arguments to pass to Command.RunPipe() |
| 175 | """ |
| 176 | result = command.RunPipe([[self._binman_pathname] + list(args)], |
| 177 | capture=True, capture_stderr=True, raise_on_error=False) |
| 178 | if result.return_code and kwargs.get('raise_on_error', True): |
| 179 | raise Exception("Error running '%s': %s" % (' '.join(args), |
| 180 | result.stdout + result.stderr)) |
| 181 | return result |
| 182 | |
| 183 | def _DoBinman(self, *args): |
| 184 | """Run binman using directly (in the same process) |
| 185 | |
| 186 | Args: |
| 187 | Arguments to pass, as a list of strings |
| 188 | Returns: |
| 189 | Return value (0 for success) |
| 190 | """ |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 191 | args = list(args) |
| 192 | if '-D' in sys.argv: |
| 193 | args = args + ['-D'] |
| 194 | (options, args) = cmdline.ParseArgs(args) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 195 | options.pager = 'binman-invalid-pager' |
| 196 | options.build_dir = self._indir |
| 197 | |
| 198 | # For testing, you can force an increase in verbosity here |
| 199 | # options.verbosity = tout.DEBUG |
| 200 | return control.Binman(options, args) |
| 201 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 202 | def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False, |
Simon Glass | eb833d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 203 | entry_args=None, images=None, use_real_dtb=False, |
| 204 | verbosity=None): |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 205 | """Run binman with a given test file |
| 206 | |
| 207 | Args: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 208 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 209 | debug: True to enable debugging output |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 210 | map: True to output map files for the images |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 211 | update_dtb: Update the offset and size of each entry in the device |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 212 | tree before packing it into the image |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 213 | entry_args: Dict of entry args to supply to binman |
| 214 | key: arg name |
| 215 | value: value of that arg |
| 216 | images: List of image names to build |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 217 | """ |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 218 | args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)] |
| 219 | if debug: |
| 220 | args.append('-D') |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 221 | if map: |
| 222 | args.append('-m') |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 223 | if update_dtb: |
Simon Glass | 2569e10 | 2019-07-08 13:18:47 -0600 | [diff] [blame] | 224 | args.append('-u') |
Simon Glass | 93d1741 | 2018-09-14 04:57:23 -0600 | [diff] [blame] | 225 | if not use_real_dtb: |
| 226 | args.append('--fake-dtb') |
Simon Glass | eb833d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 227 | if verbosity is not None: |
| 228 | args.append('-v%d' % verbosity) |
Simon Glass | ee0c9a7 | 2019-07-08 13:18:48 -0600 | [diff] [blame^] | 229 | else: |
| 230 | args += self._GetVerbosity() |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 231 | if entry_args: |
Simon Glass | 5097915 | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 232 | for arg, value in entry_args.items(): |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 233 | args.append('-a%s=%s' % (arg, value)) |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 234 | if images: |
| 235 | for image in images: |
| 236 | args += ['-i', image] |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 237 | return self._DoBinman(*args) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 238 | |
| 239 | def _SetupDtb(self, fname, outfile='u-boot.dtb'): |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 240 | """Set up a new test device-tree file |
| 241 | |
| 242 | The given file is compiled and set up as the device tree to be used |
| 243 | for ths test. |
| 244 | |
| 245 | Args: |
| 246 | fname: Filename of .dts file to read |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 247 | outfile: Output filename for compiled device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 248 | |
| 249 | Returns: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 250 | Contents of device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 251 | """ |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 252 | tools.PrepareOutputDir(None) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 253 | dtb = fdt_util.EnsureCompiled(self.TestFile(fname)) |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 254 | with open(dtb, 'rb') as fd: |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 255 | data = fd.read() |
| 256 | TestFunctional._MakeInputFile(outfile, data) |
Simon Glass | e0e6275 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 257 | tools.FinaliseOutputDir() |
| 258 | return data |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 259 | |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 260 | def _GetDtbContentsForSplTpl(self, dtb_data, name): |
| 261 | """Create a version of the main DTB for SPL or SPL |
| 262 | |
| 263 | For testing we don't actually have different versions of the DTB. With |
| 264 | U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests |
| 265 | we don't normally have any unwanted nodes. |
| 266 | |
| 267 | We still want the DTBs for SPL and TPL to be different though, since |
| 268 | otherwise it is confusing to know which one we are looking at. So add |
| 269 | an 'spl' or 'tpl' property to the top-level node. |
| 270 | """ |
| 271 | dtb = fdt.Fdt.FromData(dtb_data) |
| 272 | dtb.Scan() |
| 273 | dtb.GetNode('/binman').AddZeroProp(name) |
| 274 | dtb.Sync(auto_resize=True) |
| 275 | dtb.Pack() |
| 276 | return dtb.GetContents() |
| 277 | |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 278 | def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False, |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 279 | update_dtb=False, entry_args=None, reset_dtbs=True): |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 280 | """Run binman and return the resulting image |
| 281 | |
| 282 | This runs binman with a given test file and then reads the resulting |
| 283 | output file. It is a shortcut function since most tests need to do |
| 284 | these steps. |
| 285 | |
| 286 | Raises an assertion failure if binman returns a non-zero exit code. |
| 287 | |
| 288 | Args: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 289 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 290 | use_real_dtb: True to use the test file as the contents of |
| 291 | the u-boot-dtb entry. Normally this is not needed and the |
| 292 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 293 | But in some test we need the real contents. |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 294 | map: True to output map files for the images |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 295 | update_dtb: Update the offset and size of each entry in the device |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 296 | tree before packing it into the image |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 297 | |
| 298 | Returns: |
| 299 | Tuple: |
| 300 | Resulting image contents |
| 301 | Device tree contents |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 302 | Map data showing contents of image (or None if none) |
Simon Glass | ea6922e | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 303 | Output device tree binary filename ('u-boot.dtb' path) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 304 | """ |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 305 | dtb_data = None |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 306 | # Use the compiled test file as the u-boot-dtb input |
| 307 | if use_real_dtb: |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 308 | dtb_data = self._SetupDtb(fname) |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 309 | |
| 310 | # For testing purposes, make a copy of the DT for SPL and TPL. Add |
| 311 | # a node indicating which it is, so aid verification. |
| 312 | for name in ['spl', 'tpl']: |
| 313 | dtb_fname = '%s/u-boot-%s.dtb' % (name, name) |
| 314 | outfile = os.path.join(self._indir, dtb_fname) |
| 315 | TestFunctional._MakeInputFile(dtb_fname, |
| 316 | self._GetDtbContentsForSplTpl(dtb_data, name)) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 317 | |
| 318 | try: |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 319 | retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb, |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 320 | entry_args=entry_args, use_real_dtb=use_real_dtb) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 321 | self.assertEqual(0, retcode) |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 322 | out_dtb_fname = tools.GetOutputFilename('u-boot.dtb.out') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 323 | |
| 324 | # Find the (only) image, read it and return its contents |
| 325 | image = control.images['image'] |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 326 | image_fname = tools.GetOutputFilename('image.bin') |
| 327 | self.assertTrue(os.path.exists(image_fname)) |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 328 | if map: |
| 329 | map_fname = tools.GetOutputFilename('image.map') |
| 330 | with open(map_fname) as fd: |
| 331 | map_data = fd.read() |
| 332 | else: |
| 333 | map_data = None |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 334 | with open(image_fname, 'rb') as fd: |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 335 | return fd.read(), dtb_data, map_data, out_dtb_fname |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 336 | finally: |
| 337 | # Put the test file back |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 338 | if reset_dtbs and use_real_dtb: |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 339 | self._ResetDtbs() |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 340 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 341 | def _DoReadFile(self, fname, use_real_dtb=False): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 342 | """Helper function which discards the device-tree binary |
| 343 | |
| 344 | Args: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 345 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 346 | use_real_dtb: True to use the test file as the contents of |
| 347 | the u-boot-dtb entry. Normally this is not needed and the |
| 348 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 349 | But in some test we need the real contents. |
Simon Glass | ea6922e | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 350 | |
| 351 | Returns: |
| 352 | Resulting image contents |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 353 | """ |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 354 | return self._DoReadFileDtb(fname, use_real_dtb)[0] |
| 355 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 356 | @classmethod |
| 357 | def _MakeInputFile(self, fname, contents): |
| 358 | """Create a new test input file, creating directories as needed |
| 359 | |
| 360 | Args: |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 361 | fname: Filename to create |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 362 | contents: File contents to write in to the file |
| 363 | Returns: |
| 364 | Full pathname of file created |
| 365 | """ |
| 366 | pathname = os.path.join(self._indir, fname) |
| 367 | dirname = os.path.dirname(pathname) |
| 368 | if dirname and not os.path.exists(dirname): |
| 369 | os.makedirs(dirname) |
| 370 | with open(pathname, 'wb') as fd: |
| 371 | fd.write(contents) |
| 372 | return pathname |
| 373 | |
| 374 | @classmethod |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 375 | def _MakeInputDir(self, dirname): |
| 376 | """Create a new test input directory, creating directories as needed |
| 377 | |
| 378 | Args: |
| 379 | dirname: Directory name to create |
| 380 | |
| 381 | Returns: |
| 382 | Full pathname of directory created |
| 383 | """ |
| 384 | pathname = os.path.join(self._indir, dirname) |
| 385 | if not os.path.exists(pathname): |
| 386 | os.makedirs(pathname) |
| 387 | return pathname |
| 388 | |
| 389 | @classmethod |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 390 | def _SetupSplElf(self, src_fname='bss_data'): |
| 391 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 392 | |
| 393 | Args: |
| 394 | Filename of ELF file to use as SPL |
| 395 | """ |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 396 | with open(self.TestFile(src_fname), 'rb') as fd: |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 397 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
| 398 | |
| 399 | @classmethod |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 400 | def TestFile(self, fname): |
| 401 | return os.path.join(self._binman_dir, 'test', fname) |
| 402 | |
| 403 | def AssertInList(self, grep_list, target): |
| 404 | """Assert that at least one of a list of things is in a target |
| 405 | |
| 406 | Args: |
| 407 | grep_list: List of strings to check |
| 408 | target: Target string |
| 409 | """ |
| 410 | for grep in grep_list: |
| 411 | if grep in target: |
| 412 | return |
Simon Glass | 1fc62de | 2019-05-17 22:00:50 -0600 | [diff] [blame] | 413 | self.fail("Error: '%s' not found in '%s'" % (grep_list, target)) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 414 | |
| 415 | def CheckNoGaps(self, entries): |
| 416 | """Check that all entries fit together without gaps |
| 417 | |
| 418 | Args: |
| 419 | entries: List of entries to check |
| 420 | """ |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 421 | offset = 0 |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 422 | for entry in entries.values(): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 423 | self.assertEqual(offset, entry.offset) |
| 424 | offset += entry.size |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 425 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 426 | def GetFdtLen(self, dtb): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 427 | """Get the totalsize field from a device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 428 | |
| 429 | Args: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 430 | dtb: Device-tree binary contents |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 431 | |
| 432 | Returns: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 433 | Total size of device-tree binary, from the header |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 434 | """ |
| 435 | return struct.unpack('>L', dtb[4:8])[0] |
| 436 | |
Simon Glass | cee02e6 | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 437 | def _GetPropTree(self, dtb, prop_names): |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 438 | def AddNode(node, path): |
| 439 | if node.name != '/': |
| 440 | path += '/' + node.name |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 441 | for subnode in node.subnodes: |
| 442 | for prop in subnode.props.values(): |
Simon Glass | cee02e6 | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 443 | if prop.name in prop_names: |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 444 | prop_path = path + '/' + subnode.name + ':' + prop.name |
| 445 | tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu( |
| 446 | prop.value) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 447 | AddNode(subnode, path) |
| 448 | |
| 449 | tree = {} |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 450 | AddNode(dtb.GetRoot(), '') |
| 451 | return tree |
| 452 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 453 | def testRun(self): |
| 454 | """Test a basic run with valid args""" |
| 455 | result = self._RunBinman('-h') |
| 456 | |
| 457 | def testFullHelp(self): |
| 458 | """Test that the full help is displayed with -H""" |
| 459 | result = self._RunBinman('-H') |
| 460 | help_file = os.path.join(self._binman_dir, 'README') |
Tom Rini | 3759df0 | 2018-01-16 15:29:50 -0500 | [diff] [blame] | 461 | # Remove possible extraneous strings |
| 462 | extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n' |
| 463 | gothelp = result.stdout.replace(extra, '') |
| 464 | self.assertEqual(len(gothelp), os.path.getsize(help_file)) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 465 | self.assertEqual(0, len(result.stderr)) |
| 466 | self.assertEqual(0, result.return_code) |
| 467 | |
| 468 | def testFullHelpInternal(self): |
| 469 | """Test that the full help is displayed with -H""" |
| 470 | try: |
| 471 | command.test_result = command.CommandResult() |
| 472 | result = self._DoBinman('-H') |
| 473 | help_file = os.path.join(self._binman_dir, 'README') |
| 474 | finally: |
| 475 | command.test_result = None |
| 476 | |
| 477 | def testHelp(self): |
| 478 | """Test that the basic help is displayed with -h""" |
| 479 | result = self._RunBinman('-h') |
| 480 | self.assertTrue(len(result.stdout) > 200) |
| 481 | self.assertEqual(0, len(result.stderr)) |
| 482 | self.assertEqual(0, result.return_code) |
| 483 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 484 | def testBoard(self): |
| 485 | """Test that we can run it with a specific board""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 486 | self._SetupDtb('005_simple.dts', 'sandbox/u-boot.dtb') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 487 | TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA) |
| 488 | result = self._DoBinman('-b', 'sandbox') |
| 489 | self.assertEqual(0, result) |
| 490 | |
| 491 | def testNeedBoard(self): |
| 492 | """Test that we get an error when no board ius supplied""" |
| 493 | with self.assertRaises(ValueError) as e: |
| 494 | result = self._DoBinman() |
| 495 | self.assertIn("Must provide a board to process (use -b <board>)", |
| 496 | str(e.exception)) |
| 497 | |
| 498 | def testMissingDt(self): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 499 | """Test that an invalid device-tree file generates an error""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 500 | with self.assertRaises(Exception) as e: |
| 501 | self._RunBinman('-d', 'missing_file') |
| 502 | # We get one error from libfdt, and a different one from fdtget. |
| 503 | self.AssertInList(["Couldn't open blob from 'missing_file'", |
| 504 | 'No such file or directory'], str(e.exception)) |
| 505 | |
| 506 | def testBrokenDt(self): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 507 | """Test that an invalid device-tree source file generates an error |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 508 | |
| 509 | Since this is a source file it should be compiled and the error |
| 510 | will come from the device-tree compiler (dtc). |
| 511 | """ |
| 512 | with self.assertRaises(Exception) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 513 | self._RunBinman('-d', self.TestFile('001_invalid.dts')) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 514 | self.assertIn("FATAL ERROR: Unable to parse input tree", |
| 515 | str(e.exception)) |
| 516 | |
| 517 | def testMissingNode(self): |
| 518 | """Test that a device tree without a 'binman' node generates an error""" |
| 519 | with self.assertRaises(Exception) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 520 | self._DoBinman('-d', self.TestFile('002_missing_node.dts')) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 521 | self.assertIn("does not have a 'binman' node", str(e.exception)) |
| 522 | |
| 523 | def testEmpty(self): |
| 524 | """Test that an empty binman node works OK (i.e. does nothing)""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 525 | result = self._RunBinman('-d', self.TestFile('003_empty.dts')) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 526 | self.assertEqual(0, len(result.stderr)) |
| 527 | self.assertEqual(0, result.return_code) |
| 528 | |
| 529 | def testInvalidEntry(self): |
| 530 | """Test that an invalid entry is flagged""" |
| 531 | with self.assertRaises(Exception) as e: |
| 532 | result = self._RunBinman('-d', |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 533 | self.TestFile('004_invalid_entry.dts')) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 534 | self.assertIn("Unknown entry type 'not-a-valid-type' in node " |
| 535 | "'/binman/not-a-valid-type'", str(e.exception)) |
| 536 | |
| 537 | def testSimple(self): |
| 538 | """Test a simple binman with a single file""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 539 | data = self._DoReadFile('005_simple.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 540 | self.assertEqual(U_BOOT_DATA, data) |
| 541 | |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 542 | def testSimpleDebug(self): |
| 543 | """Test a simple binman run with debugging enabled""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 544 | data = self._DoTestFile('005_simple.dts', debug=True) |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 545 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 546 | def testDual(self): |
| 547 | """Test that we can handle creating two images |
| 548 | |
| 549 | This also tests image padding. |
| 550 | """ |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 551 | retcode = self._DoTestFile('006_dual_image.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 552 | self.assertEqual(0, retcode) |
| 553 | |
| 554 | image = control.images['image1'] |
| 555 | self.assertEqual(len(U_BOOT_DATA), image._size) |
| 556 | fname = tools.GetOutputFilename('image1.bin') |
| 557 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 558 | with open(fname, 'rb') as fd: |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 559 | data = fd.read() |
| 560 | self.assertEqual(U_BOOT_DATA, data) |
| 561 | |
| 562 | image = control.images['image2'] |
| 563 | self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size) |
| 564 | fname = tools.GetOutputFilename('image2.bin') |
| 565 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 566 | with open(fname, 'rb') as fd: |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 567 | data = fd.read() |
| 568 | self.assertEqual(U_BOOT_DATA, data[3:7]) |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 569 | self.assertEqual(tools.GetBytes(0, 3), data[:3]) |
| 570 | self.assertEqual(tools.GetBytes(0, 5), data[7:]) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 571 | |
| 572 | def testBadAlign(self): |
| 573 | """Test that an invalid alignment value is detected""" |
| 574 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 575 | self._DoTestFile('007_bad_align.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 576 | self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power " |
| 577 | "of two", str(e.exception)) |
| 578 | |
| 579 | def testPackSimple(self): |
| 580 | """Test that packing works as expected""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 581 | retcode = self._DoTestFile('008_pack.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 582 | self.assertEqual(0, retcode) |
| 583 | self.assertIn('image', control.images) |
| 584 | image = control.images['image'] |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 585 | entries = image.GetEntries() |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 586 | self.assertEqual(5, len(entries)) |
| 587 | |
| 588 | # First u-boot |
| 589 | self.assertIn('u-boot', entries) |
| 590 | entry = entries['u-boot'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 591 | self.assertEqual(0, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 592 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 593 | |
| 594 | # Second u-boot, aligned to 16-byte boundary |
| 595 | self.assertIn('u-boot-align', entries) |
| 596 | entry = entries['u-boot-align'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 597 | self.assertEqual(16, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 598 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 599 | |
| 600 | # Third u-boot, size 23 bytes |
| 601 | self.assertIn('u-boot-size', entries) |
| 602 | entry = entries['u-boot-size'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 603 | self.assertEqual(20, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 604 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 605 | self.assertEqual(23, entry.size) |
| 606 | |
| 607 | # Fourth u-boot, placed immediate after the above |
| 608 | self.assertIn('u-boot-next', entries) |
| 609 | entry = entries['u-boot-next'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 610 | self.assertEqual(43, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 611 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 612 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 613 | # Fifth u-boot, placed at a fixed offset |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 614 | self.assertIn('u-boot-fixed', entries) |
| 615 | entry = entries['u-boot-fixed'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 616 | self.assertEqual(61, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 617 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 618 | |
| 619 | self.assertEqual(65, image._size) |
| 620 | |
| 621 | def testPackExtra(self): |
| 622 | """Test that extra packing feature works as expected""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 623 | retcode = self._DoTestFile('009_pack_extra.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 624 | |
| 625 | self.assertEqual(0, retcode) |
| 626 | self.assertIn('image', control.images) |
| 627 | image = control.images['image'] |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 628 | entries = image.GetEntries() |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 629 | self.assertEqual(5, len(entries)) |
| 630 | |
| 631 | # First u-boot with padding before and after |
| 632 | self.assertIn('u-boot', entries) |
| 633 | entry = entries['u-boot'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 634 | self.assertEqual(0, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 635 | self.assertEqual(3, entry.pad_before) |
| 636 | self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size) |
| 637 | |
| 638 | # Second u-boot has an aligned size, but it has no effect |
| 639 | self.assertIn('u-boot-align-size-nop', entries) |
| 640 | entry = entries['u-boot-align-size-nop'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 641 | self.assertEqual(12, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 642 | self.assertEqual(4, entry.size) |
| 643 | |
| 644 | # Third u-boot has an aligned size too |
| 645 | self.assertIn('u-boot-align-size', entries) |
| 646 | entry = entries['u-boot-align-size'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 647 | self.assertEqual(16, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 648 | self.assertEqual(32, entry.size) |
| 649 | |
| 650 | # Fourth u-boot has an aligned end |
| 651 | self.assertIn('u-boot-align-end', entries) |
| 652 | entry = entries['u-boot-align-end'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 653 | self.assertEqual(48, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 654 | self.assertEqual(16, entry.size) |
| 655 | |
| 656 | # Fifth u-boot immediately afterwards |
| 657 | self.assertIn('u-boot-align-both', entries) |
| 658 | entry = entries['u-boot-align-both'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 659 | self.assertEqual(64, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 660 | self.assertEqual(64, entry.size) |
| 661 | |
| 662 | self.CheckNoGaps(entries) |
| 663 | self.assertEqual(128, image._size) |
| 664 | |
| 665 | def testPackAlignPowerOf2(self): |
| 666 | """Test that invalid entry alignment is detected""" |
| 667 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 668 | self._DoTestFile('010_pack_align_power2.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 669 | self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power " |
| 670 | "of two", str(e.exception)) |
| 671 | |
| 672 | def testPackAlignSizePowerOf2(self): |
| 673 | """Test that invalid entry size alignment is detected""" |
| 674 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 675 | self._DoTestFile('011_pack_align_size_power2.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 676 | self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a " |
| 677 | "power of two", str(e.exception)) |
| 678 | |
| 679 | def testPackInvalidAlign(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 680 | """Test detection of an offset that does not match its alignment""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 681 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 682 | self._DoTestFile('012_pack_inv_align.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 683 | self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 684 | "align 0x4 (4)", str(e.exception)) |
| 685 | |
| 686 | def testPackInvalidSizeAlign(self): |
| 687 | """Test that invalid entry size alignment is detected""" |
| 688 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 689 | self._DoTestFile('013_pack_inv_size_align.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 690 | self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match " |
| 691 | "align-size 0x4 (4)", str(e.exception)) |
| 692 | |
| 693 | def testPackOverlap(self): |
| 694 | """Test that overlapping regions are detected""" |
| 695 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 696 | self._DoTestFile('014_pack_overlap.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 697 | self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 698 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 699 | str(e.exception)) |
| 700 | |
| 701 | def testPackEntryOverflow(self): |
| 702 | """Test that entries that overflow their size are detected""" |
| 703 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 704 | self._DoTestFile('015_pack_overflow.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 705 | self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) " |
| 706 | "but entry size is 0x3 (3)", str(e.exception)) |
| 707 | |
| 708 | def testPackImageOverflow(self): |
| 709 | """Test that entries which overflow the image size are detected""" |
| 710 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 711 | self._DoTestFile('016_pack_image_overflow.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 712 | self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 713 | "size 0x3 (3)", str(e.exception)) |
| 714 | |
| 715 | def testPackImageSize(self): |
| 716 | """Test that the image size can be set""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 717 | retcode = self._DoTestFile('017_pack_image_size.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 718 | self.assertEqual(0, retcode) |
| 719 | self.assertIn('image', control.images) |
| 720 | image = control.images['image'] |
| 721 | self.assertEqual(7, image._size) |
| 722 | |
| 723 | def testPackImageSizeAlign(self): |
| 724 | """Test that image size alignemnt works as expected""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 725 | retcode = self._DoTestFile('018_pack_image_align.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 726 | self.assertEqual(0, retcode) |
| 727 | self.assertIn('image', control.images) |
| 728 | image = control.images['image'] |
| 729 | self.assertEqual(16, image._size) |
| 730 | |
| 731 | def testPackInvalidImageAlign(self): |
| 732 | """Test that invalid image alignment is detected""" |
| 733 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 734 | self._DoTestFile('019_pack_inv_image_align.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 735 | self.assertIn("Section '/binman': Size 0x7 (7) does not match " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 736 | "align-size 0x8 (8)", str(e.exception)) |
| 737 | |
| 738 | def testPackAlignPowerOf2(self): |
| 739 | """Test that invalid image alignment is detected""" |
| 740 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 741 | self._DoTestFile('020_pack_inv_image_align_power2.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 742 | self.assertIn("Section '/binman': Alignment size 131 must be a power of " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 743 | "two", str(e.exception)) |
| 744 | |
| 745 | def testImagePadByte(self): |
| 746 | """Test that the image pad byte can be specified""" |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 747 | self._SetupSplElf() |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 748 | data = self._DoReadFile('021_image_pad.dts') |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 749 | self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0xff, 1) + |
| 750 | U_BOOT_DATA, data) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 751 | |
| 752 | def testImageName(self): |
| 753 | """Test that image files can be named""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 754 | retcode = self._DoTestFile('022_image_name.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 755 | self.assertEqual(0, retcode) |
| 756 | image = control.images['image1'] |
| 757 | fname = tools.GetOutputFilename('test-name') |
| 758 | self.assertTrue(os.path.exists(fname)) |
| 759 | |
| 760 | image = control.images['image2'] |
| 761 | fname = tools.GetOutputFilename('test-name.xx') |
| 762 | self.assertTrue(os.path.exists(fname)) |
| 763 | |
| 764 | def testBlobFilename(self): |
| 765 | """Test that generic blobs can be provided by filename""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 766 | data = self._DoReadFile('023_blob.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 767 | self.assertEqual(BLOB_DATA, data) |
| 768 | |
| 769 | def testPackSorted(self): |
| 770 | """Test that entries can be sorted""" |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 771 | self._SetupSplElf() |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 772 | data = self._DoReadFile('024_sorted.dts') |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 773 | self.assertEqual(tools.GetBytes(0, 1) + U_BOOT_SPL_DATA + |
| 774 | tools.GetBytes(0, 2) + U_BOOT_DATA, data) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 775 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 776 | def testPackZeroOffset(self): |
| 777 | """Test that an entry at offset 0 is not given a new offset""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 778 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 779 | self._DoTestFile('025_pack_zero_size.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 780 | self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 781 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 782 | str(e.exception)) |
| 783 | |
| 784 | def testPackUbootDtb(self): |
| 785 | """Test that a device tree can be added to U-Boot""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 786 | data = self._DoReadFile('026_pack_u_boot_dtb.dts') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 787 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 788 | |
| 789 | def testPackX86RomNoSize(self): |
| 790 | """Test that the end-at-4gb property requires a size property""" |
| 791 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 792 | self._DoTestFile('027_pack_4gb_no_size.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 793 | self.assertIn("Section '/binman': Section size must be provided when " |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 794 | "using end-at-4gb", str(e.exception)) |
| 795 | |
Jagdish Gediya | 94b57db | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 796 | def test4gbAndSkipAtStartTogether(self): |
| 797 | """Test that the end-at-4gb and skip-at-size property can't be used |
| 798 | together""" |
| 799 | with self.assertRaises(ValueError) as e: |
| 800 | self._DoTestFile('80_4gb_and_skip_at_start_together.dts') |
| 801 | self.assertIn("Section '/binman': Provide either 'end-at-4gb' or " |
| 802 | "'skip-at-start'", str(e.exception)) |
| 803 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 804 | def testPackX86RomOutside(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 805 | """Test that the end-at-4gb property checks for offset boundaries""" |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 806 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 807 | self._DoTestFile('028_pack_4gb_outside.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 808 | self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside " |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 809 | "the section starting at 0xffffffe0 (4294967264)", |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 810 | str(e.exception)) |
| 811 | |
| 812 | def testPackX86Rom(self): |
| 813 | """Test that a basic x86 ROM can be created""" |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 814 | self._SetupSplElf() |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 815 | data = self._DoReadFile('029_x86-rom.dts') |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 816 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 7) + U_BOOT_SPL_DATA + |
| 817 | tools.GetBytes(0, 2), data) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 818 | |
| 819 | def testPackX86RomMeNoDesc(self): |
| 820 | """Test that an invalid Intel descriptor entry is detected""" |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 821 | TestFunctional._MakeInputFile('descriptor.bin', b'') |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 822 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 823 | self._DoTestFile('031_x86-rom-me.dts') |
Simon Glass | 458be45 | 2019-07-08 13:18:32 -0600 | [diff] [blame] | 824 | self.assertIn("Node '/binman/intel-descriptor': Cannot find Intel Flash Descriptor (FD) signature", |
| 825 | str(e.exception)) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 826 | |
| 827 | def testPackX86RomBadDesc(self): |
| 828 | """Test that the Intel requires a descriptor entry""" |
| 829 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 830 | self._DoTestFile('030_x86-rom-me-no-desc.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 831 | self.assertIn("Node '/binman/intel-me': No offset set with " |
| 832 | "offset-unset: should another entry provide this correct " |
| 833 | "offset?", str(e.exception)) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 834 | |
| 835 | def testPackX86RomMe(self): |
| 836 | """Test that an x86 ROM with an ME region can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 837 | data = self._DoReadFile('031_x86-rom-me.dts') |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 838 | self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)]) |
| 839 | |
| 840 | def testPackVga(self): |
| 841 | """Test that an image with a VGA binary can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 842 | data = self._DoReadFile('032_intel-vga.dts') |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 843 | self.assertEqual(VGA_DATA, data[:len(VGA_DATA)]) |
| 844 | |
| 845 | def testPackStart16(self): |
| 846 | """Test that an image with an x86 start16 region can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 847 | data = self._DoReadFile('033_x86-start16.dts') |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 848 | self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) |
| 849 | |
Jagdish Gediya | 9d368f3 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 850 | def testPackPowerpcMpc85xxBootpgResetvec(self): |
| 851 | """Test that an image with powerpc-mpc85xx-bootpg-resetvec can be |
| 852 | created""" |
| 853 | data = self._DoReadFile('81_powerpc_mpc85xx_bootpg_resetvec.dts') |
| 854 | self.assertEqual(PPC_MPC85XX_BR_DATA, data[:len(PPC_MPC85XX_BR_DATA)]) |
| 855 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 856 | def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 857 | """Handle running a test for insertion of microcode |
| 858 | |
| 859 | Args: |
| 860 | dts_fname: Name of test .dts file |
| 861 | nodtb_data: Data that we expect in the first section |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 862 | ucode_second: True if the microsecond entry is second instead of |
| 863 | third |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 864 | |
| 865 | Returns: |
| 866 | Tuple: |
| 867 | Contents of first region (U-Boot or SPL) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 868 | Offset and size components of microcode pointer, as inserted |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 869 | in the above (two 4-byte words) |
| 870 | """ |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 871 | data = self._DoReadFile(dts_fname, True) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 872 | |
| 873 | # Now check the device tree has no microcode |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 874 | if ucode_second: |
| 875 | ucode_content = data[len(nodtb_data):] |
| 876 | ucode_pos = len(nodtb_data) |
| 877 | dtb_with_ucode = ucode_content[16:] |
| 878 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 879 | else: |
| 880 | dtb_with_ucode = data[len(nodtb_data):] |
| 881 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 882 | ucode_content = dtb_with_ucode[fdt_len:] |
| 883 | ucode_pos = len(nodtb_data) + fdt_len |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 884 | fname = tools.GetOutputFilename('test.dtb') |
| 885 | with open(fname, 'wb') as fd: |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 886 | fd.write(dtb_with_ucode) |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 887 | dtb = fdt.FdtScan(fname) |
| 888 | ucode = dtb.GetNode('/microcode') |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 889 | self.assertTrue(ucode) |
| 890 | for node in ucode.subnodes: |
| 891 | self.assertFalse(node.props.get('data')) |
| 892 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 893 | # Check that the microcode appears immediately after the Fdt |
| 894 | # This matches the concatenation of the data properties in |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 895 | # the /microcode/update@xxx nodes in 34_x86_ucode.dts. |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 896 | ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, |
| 897 | 0x78235609) |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 898 | self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 899 | |
| 900 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 901 | # expected offset and size |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 902 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 903 | len(ucode_data)) |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 904 | u_boot = data[:len(nodtb_data)] |
| 905 | return u_boot, pos_and_size |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 906 | |
| 907 | def testPackUbootMicrocode(self): |
| 908 | """Test that x86 microcode can be handled correctly |
| 909 | |
| 910 | We expect to see the following in the image, in order: |
| 911 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 912 | place |
| 913 | u-boot.dtb with the microcode removed |
| 914 | the microcode |
| 915 | """ |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 916 | first, pos_and_size = self._RunMicrocodeTest('034_x86_ucode.dts', |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 917 | U_BOOT_NODTB_DATA) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 918 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 919 | b' somewhere in here', first) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 920 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 921 | def _RunPackUbootSingleMicrocode(self): |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 922 | """Test that x86 microcode can be handled correctly |
| 923 | |
| 924 | We expect to see the following in the image, in order: |
| 925 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 926 | place |
| 927 | u-boot.dtb with the microcode |
| 928 | an empty microcode region |
| 929 | """ |
| 930 | # We need the libfdt library to run this test since only that allows |
| 931 | # finding the offset of a property. This is required by |
| 932 | # Entry_u_boot_dtb_with_ucode.ObtainContents(). |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 933 | data = self._DoReadFile('035_x86_single_ucode.dts', True) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 934 | |
| 935 | second = data[len(U_BOOT_NODTB_DATA):] |
| 936 | |
| 937 | fdt_len = self.GetFdtLen(second) |
| 938 | third = second[fdt_len:] |
| 939 | second = second[:fdt_len] |
| 940 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 941 | ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) |
| 942 | self.assertIn(ucode_data, second) |
| 943 | ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 944 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 945 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 946 | # expected offset and size |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 947 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 948 | len(ucode_data)) |
| 949 | first = data[:len(U_BOOT_NODTB_DATA)] |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 950 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 951 | b' somewhere in here', first) |
Simon Glass | c49deb8 | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 952 | |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 953 | def testPackUbootSingleMicrocode(self): |
| 954 | """Test that x86 microcode can be handled correctly with fdt_normal. |
| 955 | """ |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 956 | self._RunPackUbootSingleMicrocode() |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 957 | |
Simon Glass | c49deb8 | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 958 | def testUBootImg(self): |
| 959 | """Test that u-boot.img can be put in a file""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 960 | data = self._DoReadFile('036_u_boot_img.dts') |
Simon Glass | c49deb8 | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 961 | self.assertEqual(U_BOOT_IMG_DATA, data) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 962 | |
| 963 | def testNoMicrocode(self): |
| 964 | """Test that a missing microcode region is detected""" |
| 965 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 966 | self._DoReadFile('037_x86_no_ucode.dts', True) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 967 | self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode " |
| 968 | "node found in ", str(e.exception)) |
| 969 | |
| 970 | def testMicrocodeWithoutNode(self): |
| 971 | """Test that a missing u-boot-dtb-with-ucode node is detected""" |
| 972 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 973 | self._DoReadFile('038_x86_ucode_missing_node.dts', True) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 974 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 975 | "microcode region u-boot-dtb-with-ucode", str(e.exception)) |
| 976 | |
| 977 | def testMicrocodeWithoutNode2(self): |
| 978 | """Test that a missing u-boot-ucode node is detected""" |
| 979 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 980 | self._DoReadFile('039_x86_ucode_missing_node2.dts', True) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 981 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 982 | "microcode region u-boot-ucode", str(e.exception)) |
| 983 | |
| 984 | def testMicrocodeWithoutPtrInElf(self): |
| 985 | """Test that a U-Boot binary without the microcode symbol is detected""" |
| 986 | # ELF file without a '_dt_ucode_base_size' symbol |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 987 | try: |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 988 | with open(self.TestFile('u_boot_no_ucode_ptr'), 'rb') as fd: |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 989 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 990 | |
| 991 | with self.assertRaises(ValueError) as e: |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 992 | self._RunPackUbootSingleMicrocode() |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 993 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate " |
| 994 | "_dt_ucode_base_size symbol in u-boot", str(e.exception)) |
| 995 | |
| 996 | finally: |
| 997 | # Put the original file back |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 998 | with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd: |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 999 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 1000 | |
| 1001 | def testMicrocodeNotInImage(self): |
| 1002 | """Test that microcode must be placed within the image""" |
| 1003 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1004 | self._DoReadFile('040_x86_ucode_not_in_image.dts', True) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1005 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode " |
| 1006 | "pointer _dt_ucode_base_size at fffffe14 is outside the " |
Simon Glass | 25ac0e6 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 1007 | "section ranging from 00000000 to 0000002e", str(e.exception)) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1008 | |
| 1009 | def testWithoutMicrocode(self): |
| 1010 | """Test that we can cope with an image without microcode (e.g. qemu)""" |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1011 | with open(self.TestFile('u_boot_no_ucode_ptr'), 'rb') as fd: |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1012 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1013 | data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1014 | |
| 1015 | # Now check the device tree has no microcode |
| 1016 | self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)]) |
| 1017 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1018 | |
| 1019 | fdt_len = self.GetFdtLen(second) |
| 1020 | self.assertEqual(dtb, second[:fdt_len]) |
| 1021 | |
| 1022 | used_len = len(U_BOOT_NODTB_DATA) + fdt_len |
| 1023 | third = data[used_len:] |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1024 | self.assertEqual(tools.GetBytes(0, 0x200 - used_len), third) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1025 | |
| 1026 | def testUnknownPosSize(self): |
| 1027 | """Test that microcode must be placed within the image""" |
| 1028 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1029 | self._DoReadFile('041_unknown_pos_size.dts', True) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1030 | self.assertIn("Section '/binman': Unable to set offset/size for unknown " |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1031 | "entry 'invalid-entry'", str(e.exception)) |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1032 | |
| 1033 | def testPackFsp(self): |
| 1034 | """Test that an image with a FSP binary can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1035 | data = self._DoReadFile('042_intel-fsp.dts') |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1036 | self.assertEqual(FSP_DATA, data[:len(FSP_DATA)]) |
| 1037 | |
| 1038 | def testPackCmc(self): |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1039 | """Test that an image with a CMC binary can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1040 | data = self._DoReadFile('043_intel-cmc.dts') |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1041 | self.assertEqual(CMC_DATA, data[:len(CMC_DATA)]) |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1042 | |
| 1043 | def testPackVbt(self): |
| 1044 | """Test that an image with a VBT binary can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1045 | data = self._DoReadFile('046_intel-vbt.dts') |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1046 | self.assertEqual(VBT_DATA, data[:len(VBT_DATA)]) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1047 | |
Simon Glass | 5650984 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1048 | def testSplBssPad(self): |
| 1049 | """Test that we can pad SPL's BSS with zeros""" |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1050 | # ELF file with a '__bss_size' symbol |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1051 | self._SetupSplElf() |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1052 | data = self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1053 | self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA, |
| 1054 | data) |
Simon Glass | 5650984 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1055 | |
Simon Glass | 86af511 | 2018-10-01 21:12:42 -0600 | [diff] [blame] | 1056 | def testSplBssPadMissing(self): |
| 1057 | """Test that a missing symbol is detected""" |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1058 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1059 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1060 | self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1061 | self.assertIn('Expected __bss_size symbol in spl/u-boot-spl', |
| 1062 | str(e.exception)) |
| 1063 | |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1064 | def testPackStart16Spl(self): |
Simon Glass | 35b384c | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1065 | """Test that an image with an x86 start16 SPL region can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1066 | data = self._DoReadFile('048_x86-start16-spl.dts') |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1067 | self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)]) |
| 1068 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1069 | def _PackUbootSplMicrocode(self, dts, ucode_second=False): |
| 1070 | """Helper function for microcode tests |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1071 | |
| 1072 | We expect to see the following in the image, in order: |
| 1073 | u-boot-spl-nodtb.bin with a microcode pointer inserted at the |
| 1074 | correct place |
| 1075 | u-boot.dtb with the microcode removed |
| 1076 | the microcode |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1077 | |
| 1078 | Args: |
| 1079 | dts: Device tree file to use for test |
| 1080 | ucode_second: True if the microsecond entry is second instead of |
| 1081 | third |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1082 | """ |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1083 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1084 | first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, |
| 1085 | ucode_second=ucode_second) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1086 | self.assertEqual(b'splnodtb with microc' + pos_and_size + |
| 1087 | b'ter somewhere in here', first) |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1088 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1089 | def testPackUbootSplMicrocode(self): |
| 1090 | """Test that x86 microcode can be handled correctly in SPL""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1091 | self._PackUbootSplMicrocode('049_x86_ucode_spl.dts') |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1092 | |
| 1093 | def testPackUbootSplMicrocodeReorder(self): |
| 1094 | """Test that order doesn't matter for microcode entries |
| 1095 | |
| 1096 | This is the same as testPackUbootSplMicrocode but when we process the |
| 1097 | u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode |
| 1098 | entry, so we reply on binman to try later. |
| 1099 | """ |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1100 | self._PackUbootSplMicrocode('058_x86_ucode_spl_needs_retry.dts', |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1101 | ucode_second=True) |
| 1102 | |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1103 | def testPackMrc(self): |
| 1104 | """Test that an image with an MRC binary can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1105 | data = self._DoReadFile('050_intel_mrc.dts') |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1106 | self.assertEqual(MRC_DATA, data[:len(MRC_DATA)]) |
| 1107 | |
Simon Glass | 47419ea | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1108 | def testSplDtb(self): |
| 1109 | """Test that an image with spl/u-boot-spl.dtb can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1110 | data = self._DoReadFile('051_u_boot_spl_dtb.dts') |
Simon Glass | 47419ea | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1111 | self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)]) |
| 1112 | |
Simon Glass | 4e6fdbe | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1113 | def testSplNoDtb(self): |
| 1114 | """Test that an image with spl/u-boot-spl-nodtb.bin can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1115 | data = self._DoReadFile('052_u_boot_spl_nodtb.dts') |
Simon Glass | 4e6fdbe | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1116 | self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)]) |
| 1117 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1118 | def testSymbols(self): |
| 1119 | """Test binman can assign symbols embedded in U-Boot""" |
| 1120 | elf_fname = self.TestFile('u_boot_binman_syms') |
| 1121 | syms = elf.GetSymbols(elf_fname, ['binman', 'image']) |
| 1122 | addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1123 | self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1124 | |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1125 | self._SetupSplElf('u_boot_binman_syms') |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1126 | data = self._DoReadFile('053_symbols.dts') |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1127 | sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20) |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1128 | expected = (sym_values + U_BOOT_SPL_DATA[16:] + |
| 1129 | tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values + |
| 1130 | U_BOOT_SPL_DATA[16:]) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1131 | self.assertEqual(expected, data) |
| 1132 | |
Simon Glass | dd57c13 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1133 | def testPackUnitAddress(self): |
| 1134 | """Test that we support multiple binaries with the same name""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1135 | data = self._DoReadFile('054_unit_address.dts') |
Simon Glass | dd57c13 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1136 | self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data) |
| 1137 | |
Simon Glass | 1854695 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1138 | def testSections(self): |
| 1139 | """Basic test of sections""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1140 | data = self._DoReadFile('055_sections.dts') |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1141 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 1142 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12) + |
| 1143 | U_BOOT_DATA + tools.GetBytes(ord('&'), 4)) |
Simon Glass | 1854695 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1144 | self.assertEqual(expected, data) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1145 | |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1146 | def testMap(self): |
| 1147 | """Tests outputting a map of the images""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1148 | _, _, map_data, _ = self._DoReadFileDtb('055_sections.dts', map=True) |
Simon Glass | 1be70d2 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1149 | self.assertEqual('''ImagePos Offset Size Name |
| 1150 | 00000000 00000000 00000028 main-section |
| 1151 | 00000000 00000000 00000010 section@0 |
| 1152 | 00000000 00000000 00000004 u-boot |
| 1153 | 00000010 00000010 00000010 section@1 |
| 1154 | 00000010 00000000 00000004 u-boot |
| 1155 | 00000020 00000020 00000004 section@2 |
| 1156 | 00000020 00000000 00000004 u-boot |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1157 | ''', map_data) |
| 1158 | |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1159 | def testNamePrefix(self): |
| 1160 | """Tests that name prefixes are used""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1161 | _, _, map_data, _ = self._DoReadFileDtb('056_name_prefix.dts', map=True) |
Simon Glass | 1be70d2 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1162 | self.assertEqual('''ImagePos Offset Size Name |
| 1163 | 00000000 00000000 00000028 main-section |
| 1164 | 00000000 00000000 00000010 section@0 |
| 1165 | 00000000 00000000 00000004 ro-u-boot |
| 1166 | 00000010 00000010 00000010 section@1 |
| 1167 | 00000010 00000000 00000004 rw-u-boot |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1168 | ''', map_data) |
| 1169 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1170 | def testUnknownContents(self): |
| 1171 | """Test that obtaining the contents works as expected""" |
| 1172 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1173 | self._DoReadFile('057_unknown_contents.dts', True) |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1174 | self.assertIn("Section '/binman': Internal error: Could not complete " |
| 1175 | "processing of contents: remaining [<_testing.Entry__testing ", |
| 1176 | str(e.exception)) |
| 1177 | |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1178 | def testBadChangeSize(self): |
| 1179 | """Test that trying to change the size of an entry fails""" |
| 1180 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1181 | self._DoReadFile('059_change_size.dts', True) |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1182 | self.assertIn("Node '/binman/_testing': Cannot update entry size from " |
| 1183 | '2 to 1', str(e.exception)) |
| 1184 | |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1185 | def testUpdateFdt(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1186 | """Test that we can update the device tree with offset/size info""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1187 | _, _, _, out_dtb_fname = self._DoReadFileDtb('060_fdt_update.dts', |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1188 | update_dtb=True) |
Simon Glass | cee02e6 | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 1189 | dtb = fdt.Fdt(out_dtb_fname) |
| 1190 | dtb.Scan() |
| 1191 | props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos']) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1192 | self.assertEqual({ |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1193 | 'image-pos': 0, |
Simon Glass | 8122f39 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 1194 | 'offset': 0, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1195 | '_testing:offset': 32, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1196 | '_testing:size': 1, |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1197 | '_testing:image-pos': 32, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1198 | 'section@0/u-boot:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1199 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1200 | 'section@0/u-boot:image-pos': 0, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1201 | 'section@0:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1202 | 'section@0:size': 16, |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1203 | 'section@0:image-pos': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1204 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1205 | 'section@1/u-boot:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1206 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1207 | 'section@1/u-boot:image-pos': 16, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1208 | 'section@1:offset': 16, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1209 | 'section@1:size': 16, |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1210 | 'section@1:image-pos': 16, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1211 | 'size': 40 |
| 1212 | }, props) |
| 1213 | |
| 1214 | def testUpdateFdtBad(self): |
| 1215 | """Test that we detect when ProcessFdt never completes""" |
| 1216 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1217 | self._DoReadFileDtb('061_fdt_update_bad.dts', update_dtb=True) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1218 | self.assertIn('Could not complete processing of Fdt: remaining ' |
| 1219 | '[<_testing.Entry__testing', str(e.exception)) |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1220 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1221 | def testEntryArgs(self): |
| 1222 | """Test passing arguments to entries from the command line""" |
| 1223 | entry_args = { |
| 1224 | 'test-str-arg': 'test1', |
| 1225 | 'test-int-arg': '456', |
| 1226 | } |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1227 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1228 | self.assertIn('image', control.images) |
| 1229 | entry = control.images['image'].GetEntries()['_testing'] |
| 1230 | self.assertEqual('test0', entry.test_str_fdt) |
| 1231 | self.assertEqual('test1', entry.test_str_arg) |
| 1232 | self.assertEqual(123, entry.test_int_fdt) |
| 1233 | self.assertEqual(456, entry.test_int_arg) |
| 1234 | |
| 1235 | def testEntryArgsMissing(self): |
| 1236 | """Test missing arguments and properties""" |
| 1237 | entry_args = { |
| 1238 | 'test-int-arg': '456', |
| 1239 | } |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1240 | self._DoReadFileDtb('063_entry_args_missing.dts', entry_args=entry_args) |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1241 | entry = control.images['image'].GetEntries()['_testing'] |
| 1242 | self.assertEqual('test0', entry.test_str_fdt) |
| 1243 | self.assertEqual(None, entry.test_str_arg) |
| 1244 | self.assertEqual(None, entry.test_int_fdt) |
| 1245 | self.assertEqual(456, entry.test_int_arg) |
| 1246 | |
| 1247 | def testEntryArgsRequired(self): |
| 1248 | """Test missing arguments and properties""" |
| 1249 | entry_args = { |
| 1250 | 'test-int-arg': '456', |
| 1251 | } |
| 1252 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1253 | self._DoReadFileDtb('064_entry_args_required.dts') |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1254 | self.assertIn("Node '/binman/_testing': Missing required " |
| 1255 | 'properties/entry args: test-str-arg, test-int-fdt, test-int-arg', |
| 1256 | str(e.exception)) |
| 1257 | |
| 1258 | def testEntryArgsInvalidFormat(self): |
| 1259 | """Test that an invalid entry-argument format is detected""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1260 | args = ['-d', self.TestFile('064_entry_args_required.dts'), '-ano-value'] |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1261 | with self.assertRaises(ValueError) as e: |
| 1262 | self._DoBinman(*args) |
| 1263 | self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception)) |
| 1264 | |
| 1265 | def testEntryArgsInvalidInteger(self): |
| 1266 | """Test that an invalid entry-argument integer is detected""" |
| 1267 | entry_args = { |
| 1268 | 'test-int-arg': 'abc', |
| 1269 | } |
| 1270 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1271 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1272 | self.assertIn("Node '/binman/_testing': Cannot convert entry arg " |
| 1273 | "'test-int-arg' (value 'abc') to integer", |
| 1274 | str(e.exception)) |
| 1275 | |
| 1276 | def testEntryArgsInvalidDatatype(self): |
| 1277 | """Test that an invalid entry-argument datatype is detected |
| 1278 | |
| 1279 | This test could be written in entry_test.py except that it needs |
| 1280 | access to control.entry_args, which seems more than that module should |
| 1281 | be able to see. |
| 1282 | """ |
| 1283 | entry_args = { |
| 1284 | 'test-bad-datatype-arg': '12', |
| 1285 | } |
| 1286 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1287 | self._DoReadFileDtb('065_entry_args_unknown_datatype.dts', |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1288 | entry_args=entry_args) |
| 1289 | self.assertIn('GetArg() internal error: Unknown data type ', |
| 1290 | str(e.exception)) |
| 1291 | |
Simon Glass | bb74837 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1292 | def testText(self): |
| 1293 | """Test for a text entry type""" |
| 1294 | entry_args = { |
| 1295 | 'test-id': TEXT_DATA, |
| 1296 | 'test-id2': TEXT_DATA2, |
| 1297 | 'test-id3': TEXT_DATA3, |
| 1298 | } |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1299 | data, _, _, _ = self._DoReadFileDtb('066_text.dts', |
Simon Glass | bb74837 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1300 | entry_args=entry_args) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1301 | expected = (tools.ToBytes(TEXT_DATA) + |
| 1302 | tools.GetBytes(0, 8 - len(TEXT_DATA)) + |
| 1303 | tools.ToBytes(TEXT_DATA2) + tools.ToBytes(TEXT_DATA3) + |
Simon Glass | aa88b50 | 2019-07-08 13:18:40 -0600 | [diff] [blame] | 1304 | b'some text' + b'more text') |
Simon Glass | bb74837 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1305 | self.assertEqual(expected, data) |
| 1306 | |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1307 | def testEntryDocs(self): |
| 1308 | """Test for creation of entry documentation""" |
| 1309 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1310 | control.WriteEntryDocs(binman.GetEntryModules()) |
| 1311 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 1312 | |
| 1313 | def testEntryDocsMissing(self): |
| 1314 | """Test handling of missing entry documentation""" |
| 1315 | with self.assertRaises(ValueError) as e: |
| 1316 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1317 | control.WriteEntryDocs(binman.GetEntryModules(), 'u_boot') |
| 1318 | self.assertIn('Documentation is missing for modules: u_boot', |
| 1319 | str(e.exception)) |
| 1320 | |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1321 | def testFmap(self): |
| 1322 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1323 | data = self._DoReadFile('067_fmap.dts') |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1324 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1325 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 1326 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12)) |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1327 | self.assertEqual(expected, data[:32]) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1328 | self.assertEqual(b'__FMAP__', fhdr.signature) |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1329 | self.assertEqual(1, fhdr.ver_major) |
| 1330 | self.assertEqual(0, fhdr.ver_minor) |
| 1331 | self.assertEqual(0, fhdr.base) |
| 1332 | self.assertEqual(16 + 16 + |
| 1333 | fmap_util.FMAP_HEADER_LEN + |
| 1334 | fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1335 | self.assertEqual(b'FMAP', fhdr.name) |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1336 | self.assertEqual(3, fhdr.nareas) |
| 1337 | for fentry in fentries: |
| 1338 | self.assertEqual(0, fentry.flags) |
| 1339 | |
| 1340 | self.assertEqual(0, fentries[0].offset) |
| 1341 | self.assertEqual(4, fentries[0].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1342 | self.assertEqual(b'RO_U_BOOT', fentries[0].name) |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1343 | |
| 1344 | self.assertEqual(16, fentries[1].offset) |
| 1345 | self.assertEqual(4, fentries[1].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1346 | self.assertEqual(b'RW_U_BOOT', fentries[1].name) |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1347 | |
| 1348 | self.assertEqual(32, fentries[2].offset) |
| 1349 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 1350 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1351 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | 11e36cc | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1352 | |
Simon Glass | ec127af | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1353 | def testBlobNamedByArg(self): |
| 1354 | """Test we can add a blob with the filename coming from an entry arg""" |
| 1355 | entry_args = { |
| 1356 | 'cros-ec-rw-path': 'ecrw.bin', |
| 1357 | } |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1358 | data, _, _, _ = self._DoReadFileDtb('068_blob_named_by_arg.dts', |
Simon Glass | ec127af | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1359 | entry_args=entry_args) |
| 1360 | |
Simon Glass | 3af8e49 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1361 | def testFill(self): |
| 1362 | """Test for an fill entry type""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1363 | data = self._DoReadFile('069_fill.dts') |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1364 | expected = tools.GetBytes(0xff, 8) + tools.GetBytes(0, 8) |
Simon Glass | 3af8e49 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1365 | self.assertEqual(expected, data) |
| 1366 | |
| 1367 | def testFillNoSize(self): |
| 1368 | """Test for an fill entry type with no size""" |
| 1369 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1370 | self._DoReadFile('070_fill_no_size.dts') |
Simon Glass | 3af8e49 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1371 | self.assertIn("'fill' entry must have a size property", |
| 1372 | str(e.exception)) |
| 1373 | |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1374 | def _HandleGbbCommand(self, pipe_list): |
| 1375 | """Fake calls to the futility utility""" |
| 1376 | if pipe_list[0][0] == 'futility': |
| 1377 | fname = pipe_list[0][-1] |
| 1378 | # Append our GBB data to the file, which will happen every time the |
| 1379 | # futility command is called. |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1380 | with open(fname, 'ab') as fd: |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1381 | fd.write(GBB_DATA) |
| 1382 | return command.CommandResult() |
| 1383 | |
| 1384 | def testGbb(self): |
| 1385 | """Test for the Chromium OS Google Binary Block""" |
| 1386 | command.test_result = self._HandleGbbCommand |
| 1387 | entry_args = { |
| 1388 | 'keydir': 'devkeys', |
| 1389 | 'bmpblk': 'bmpblk.bin', |
| 1390 | } |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1391 | data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args) |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1392 | |
| 1393 | # Since futility |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1394 | expected = (GBB_DATA + GBB_DATA + tools.GetBytes(0, 8) + |
| 1395 | tools.GetBytes(0, 0x2180 - 16)) |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1396 | self.assertEqual(expected, data) |
| 1397 | |
| 1398 | def testGbbTooSmall(self): |
| 1399 | """Test for the Chromium OS Google Binary Block being large enough""" |
| 1400 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1401 | self._DoReadFileDtb('072_gbb_too_small.dts') |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1402 | self.assertIn("Node '/binman/gbb': GBB is too small", |
| 1403 | str(e.exception)) |
| 1404 | |
| 1405 | def testGbbNoSize(self): |
| 1406 | """Test for the Chromium OS Google Binary Block having a size""" |
| 1407 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1408 | self._DoReadFileDtb('073_gbb_no_size.dts') |
Simon Glass | 0ef87aa | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1409 | self.assertIn("Node '/binman/gbb': GBB must have a fixed size", |
| 1410 | str(e.exception)) |
| 1411 | |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1412 | def _HandleVblockCommand(self, pipe_list): |
| 1413 | """Fake calls to the futility utility""" |
| 1414 | if pipe_list[0][0] == 'futility': |
| 1415 | fname = pipe_list[0][3] |
Simon Glass | a326b49 | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 1416 | with open(fname, 'wb') as fd: |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1417 | fd.write(VBLOCK_DATA) |
| 1418 | return command.CommandResult() |
| 1419 | |
| 1420 | def testVblock(self): |
| 1421 | """Test for the Chromium OS Verified Boot Block""" |
| 1422 | command.test_result = self._HandleVblockCommand |
| 1423 | entry_args = { |
| 1424 | 'keydir': 'devkeys', |
| 1425 | } |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1426 | data, _, _, _ = self._DoReadFileDtb('074_vblock.dts', |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1427 | entry_args=entry_args) |
| 1428 | expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA |
| 1429 | self.assertEqual(expected, data) |
| 1430 | |
| 1431 | def testVblockNoContent(self): |
| 1432 | """Test we detect a vblock which has no content to sign""" |
| 1433 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1434 | self._DoReadFile('075_vblock_no_content.dts') |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1435 | self.assertIn("Node '/binman/vblock': Vblock must have a 'content' " |
| 1436 | 'property', str(e.exception)) |
| 1437 | |
| 1438 | def testVblockBadPhandle(self): |
| 1439 | """Test that we detect a vblock with an invalid phandle in contents""" |
| 1440 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1441 | self._DoReadFile('076_vblock_bad_phandle.dts') |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1442 | self.assertIn("Node '/binman/vblock': Cannot find node for phandle " |
| 1443 | '1000', str(e.exception)) |
| 1444 | |
| 1445 | def testVblockBadEntry(self): |
| 1446 | """Test that we detect an entry that points to a non-entry""" |
| 1447 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1448 | self._DoReadFile('077_vblock_bad_entry.dts') |
Simon Glass | 24d0d3c | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1449 | self.assertIn("Node '/binman/vblock': Cannot find entry for node " |
| 1450 | "'other'", str(e.exception)) |
| 1451 | |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1452 | def testTpl(self): |
| 1453 | """Test that an image with TPL and ots device tree can be created""" |
| 1454 | # ELF file with a '__bss_size' symbol |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1455 | with open(self.TestFile('bss_data'), 'rb') as fd: |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1456 | TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read()) |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1457 | data = self._DoReadFile('078_u_boot_tpl.dts') |
Simon Glass | b8ef5b6 | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1458 | self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data) |
| 1459 | |
Simon Glass | 15a587c | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1460 | def testUsesPos(self): |
| 1461 | """Test that the 'pos' property cannot be used anymore""" |
| 1462 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1463 | data = self._DoReadFile('079_uses_pos.dts') |
Simon Glass | 15a587c | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1464 | self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of " |
| 1465 | "'pos'", str(e.exception)) |
| 1466 | |
Simon Glass | d178eab | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1467 | def testFillZero(self): |
| 1468 | """Test for an fill entry type with a size of 0""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1469 | data = self._DoReadFile('080_fill_empty.dts') |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1470 | self.assertEqual(tools.GetBytes(0, 16), data) |
Simon Glass | d178eab | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1471 | |
Simon Glass | 0b48936 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1472 | def testTextMissing(self): |
| 1473 | """Test for a text entry type where there is no text""" |
| 1474 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1475 | self._DoReadFileDtb('066_text.dts',) |
Simon Glass | 0b48936 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1476 | self.assertIn("Node '/binman/text': No value provided for text label " |
| 1477 | "'test-id'", str(e.exception)) |
| 1478 | |
Simon Glass | 35b384c | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1479 | def testPackStart16Tpl(self): |
| 1480 | """Test that an image with an x86 start16 TPL region can be created""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1481 | data = self._DoReadFile('081_x86-start16-tpl.dts') |
Simon Glass | 35b384c | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1482 | self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)]) |
| 1483 | |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1484 | def testSelectImage(self): |
| 1485 | """Test that we can select which images to build""" |
Simon Glass | eb833d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1486 | expected = 'Skipping images: image1' |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1487 | |
Simon Glass | eb833d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1488 | # We should only get the expected message in verbose mode |
Simon Glass | ee0c9a7 | 2019-07-08 13:18:48 -0600 | [diff] [blame^] | 1489 | for verbosity in (0, 2): |
Simon Glass | eb833d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1490 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1491 | retcode = self._DoTestFile('006_dual_image.dts', |
| 1492 | verbosity=verbosity, |
| 1493 | images=['image2']) |
| 1494 | self.assertEqual(0, retcode) |
| 1495 | if verbosity: |
| 1496 | self.assertIn(expected, stdout.getvalue()) |
| 1497 | else: |
| 1498 | self.assertNotIn(expected, stdout.getvalue()) |
| 1499 | |
| 1500 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin'))) |
| 1501 | self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin'))) |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1502 | |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1503 | def testUpdateFdtAll(self): |
| 1504 | """Test that all device trees are updated with offset/size info""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1505 | data, _, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts', |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1506 | use_real_dtb=True, update_dtb=True) |
| 1507 | |
| 1508 | base_expected = { |
| 1509 | 'section:image-pos': 0, |
| 1510 | 'u-boot-tpl-dtb:size': 513, |
| 1511 | 'u-boot-spl-dtb:size': 513, |
| 1512 | 'u-boot-spl-dtb:offset': 493, |
| 1513 | 'image-pos': 0, |
| 1514 | 'section/u-boot-dtb:image-pos': 0, |
| 1515 | 'u-boot-spl-dtb:image-pos': 493, |
| 1516 | 'section/u-boot-dtb:size': 493, |
| 1517 | 'u-boot-tpl-dtb:image-pos': 1006, |
| 1518 | 'section/u-boot-dtb:offset': 0, |
| 1519 | 'section:size': 493, |
| 1520 | 'offset': 0, |
| 1521 | 'section:offset': 0, |
| 1522 | 'u-boot-tpl-dtb:offset': 1006, |
| 1523 | 'size': 1519 |
| 1524 | } |
| 1525 | |
| 1526 | # We expect three device-tree files in the output, one after the other. |
| 1527 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 1528 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 1529 | # main U-Boot tree. All three should have the same postions and offset. |
| 1530 | start = 0 |
| 1531 | for item in ['', 'spl', 'tpl']: |
| 1532 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1533 | dtb.Scan() |
| 1534 | props = self._GetPropTree(dtb, ['offset', 'size', 'image-pos', |
| 1535 | 'spl', 'tpl']) |
| 1536 | expected = dict(base_expected) |
| 1537 | if item: |
| 1538 | expected[item] = 0 |
| 1539 | self.assertEqual(expected, props) |
| 1540 | start += dtb._fdt_obj.totalsize() |
| 1541 | |
| 1542 | def testUpdateFdtOutput(self): |
| 1543 | """Test that output DTB files are updated""" |
| 1544 | try: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1545 | data, dtb_data, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts', |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1546 | use_real_dtb=True, update_dtb=True, reset_dtbs=False) |
| 1547 | |
| 1548 | # Unfortunately, compiling a source file always results in a file |
| 1549 | # called source.dtb (see fdt_util.EnsureCompiled()). The test |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1550 | # source file (e.g. test/075_fdt_update_all.dts) thus does not enter |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1551 | # binman as a file called u-boot.dtb. To fix this, copy the file |
| 1552 | # over to the expected place. |
| 1553 | #tools.WriteFile(os.path.join(self._indir, 'u-boot.dtb'), |
| 1554 | #tools.ReadFile(tools.GetOutputFilename('source.dtb'))) |
| 1555 | start = 0 |
| 1556 | for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out', |
| 1557 | 'tpl/u-boot-tpl.dtb.out']: |
| 1558 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1559 | size = dtb._fdt_obj.totalsize() |
| 1560 | pathname = tools.GetOutputFilename(os.path.split(fname)[1]) |
| 1561 | outdata = tools.ReadFile(pathname) |
| 1562 | name = os.path.split(fname)[0] |
| 1563 | |
| 1564 | if name: |
| 1565 | orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name) |
| 1566 | else: |
| 1567 | orig_indata = dtb_data |
| 1568 | self.assertNotEqual(outdata, orig_indata, |
| 1569 | "Expected output file '%s' be updated" % pathname) |
| 1570 | self.assertEqual(outdata, data[start:start + size], |
| 1571 | "Expected output file '%s' to match output image" % |
| 1572 | pathname) |
| 1573 | start += size |
| 1574 | finally: |
| 1575 | self._ResetDtbs() |
| 1576 | |
Simon Glass | 83d73c2 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1577 | def _decompress(self, data): |
Simon Glass | ff5c7e3 | 2019-07-08 13:18:42 -0600 | [diff] [blame] | 1578 | return tools.Decompress(data, 'lz4') |
Simon Glass | 83d73c2 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1579 | |
| 1580 | def testCompress(self): |
| 1581 | """Test compression of blobs""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1582 | data, _, _, out_dtb_fname = self._DoReadFileDtb('083_compress.dts', |
Simon Glass | 83d73c2 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1583 | use_real_dtb=True, update_dtb=True) |
| 1584 | dtb = fdt.Fdt(out_dtb_fname) |
| 1585 | dtb.Scan() |
| 1586 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 1587 | orig = self._decompress(data) |
| 1588 | self.assertEquals(COMPRESS_DATA, orig) |
| 1589 | expected = { |
| 1590 | 'blob:uncomp-size': len(COMPRESS_DATA), |
| 1591 | 'blob:size': len(data), |
| 1592 | 'size': len(data), |
| 1593 | } |
| 1594 | self.assertEqual(expected, props) |
| 1595 | |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1596 | def testFiles(self): |
| 1597 | """Test bringing in multiple files""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1598 | data = self._DoReadFile('084_files.dts') |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1599 | self.assertEqual(FILES_DATA, data) |
| 1600 | |
| 1601 | def testFilesCompress(self): |
| 1602 | """Test bringing in multiple files and compressing them""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1603 | data = self._DoReadFile('085_files_compress.dts') |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1604 | |
| 1605 | image = control.images['image'] |
| 1606 | entries = image.GetEntries() |
| 1607 | files = entries['files'] |
| 1608 | entries = files._section._entries |
| 1609 | |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1610 | orig = b'' |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1611 | for i in range(1, 3): |
| 1612 | key = '%d.dat' % i |
| 1613 | start = entries[key].image_pos |
| 1614 | len = entries[key].size |
| 1615 | chunk = data[start:start + len] |
| 1616 | orig += self._decompress(chunk) |
| 1617 | |
| 1618 | self.assertEqual(FILES_DATA, orig) |
| 1619 | |
| 1620 | def testFilesMissing(self): |
| 1621 | """Test missing files""" |
| 1622 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1623 | data = self._DoReadFile('086_files_none.dts') |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1624 | self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched " |
| 1625 | 'no files', str(e.exception)) |
| 1626 | |
| 1627 | def testFilesNoPattern(self): |
| 1628 | """Test missing files""" |
| 1629 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1630 | data = self._DoReadFile('087_files_no_pattern.dts') |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1631 | self.assertIn("Node '/binman/files': Missing 'pattern' property", |
| 1632 | str(e.exception)) |
| 1633 | |
Simon Glass | ba64a0b | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1634 | def testExpandSize(self): |
| 1635 | """Test an expanding entry""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1636 | data, _, map_data, _ = self._DoReadFileDtb('088_expand_size.dts', |
Simon Glass | ba64a0b | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1637 | map=True) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1638 | expect = (tools.GetBytes(ord('a'), 8) + U_BOOT_DATA + |
| 1639 | MRC_DATA + tools.GetBytes(ord('b'), 1) + U_BOOT_DATA + |
| 1640 | tools.GetBytes(ord('c'), 8) + U_BOOT_DATA + |
| 1641 | tools.GetBytes(ord('d'), 8)) |
Simon Glass | ba64a0b | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1642 | self.assertEqual(expect, data) |
| 1643 | self.assertEqual('''ImagePos Offset Size Name |
| 1644 | 00000000 00000000 00000028 main-section |
| 1645 | 00000000 00000000 00000008 fill |
| 1646 | 00000008 00000008 00000004 u-boot |
| 1647 | 0000000c 0000000c 00000004 section |
| 1648 | 0000000c 00000000 00000003 intel-mrc |
| 1649 | 00000010 00000010 00000004 u-boot2 |
| 1650 | 00000014 00000014 0000000c section2 |
| 1651 | 00000014 00000000 00000008 fill |
| 1652 | 0000001c 00000008 00000004 u-boot |
| 1653 | 00000020 00000020 00000008 fill2 |
| 1654 | ''', map_data) |
| 1655 | |
| 1656 | def testExpandSizeBad(self): |
| 1657 | """Test an expanding entry which fails to provide contents""" |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1658 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1659 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1660 | self._DoReadFileDtb('089_expand_size_bad.dts', map=True) |
Simon Glass | ba64a0b | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1661 | self.assertIn("Node '/binman/_testing': Cannot obtain contents when " |
| 1662 | 'expanding entry', str(e.exception)) |
| 1663 | |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1664 | def testHash(self): |
| 1665 | """Test hashing of the contents of an entry""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1666 | _, _, _, out_dtb_fname = self._DoReadFileDtb('090_hash.dts', |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1667 | use_real_dtb=True, update_dtb=True) |
| 1668 | dtb = fdt.Fdt(out_dtb_fname) |
| 1669 | dtb.Scan() |
| 1670 | hash_node = dtb.GetNode('/binman/u-boot/hash').props['value'] |
| 1671 | m = hashlib.sha256() |
| 1672 | m.update(U_BOOT_DATA) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1673 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1674 | |
| 1675 | def testHashNoAlgo(self): |
| 1676 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1677 | self._DoReadFileDtb('091_hash_no_algo.dts', update_dtb=True) |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1678 | self.assertIn("Node \'/binman/u-boot\': Missing \'algo\' property for " |
| 1679 | 'hash node', str(e.exception)) |
| 1680 | |
| 1681 | def testHashBadAlgo(self): |
| 1682 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1683 | self._DoReadFileDtb('092_hash_bad_algo.dts', update_dtb=True) |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1684 | self.assertIn("Node '/binman/u-boot': Unknown hash algorithm", |
| 1685 | str(e.exception)) |
| 1686 | |
| 1687 | def testHashSection(self): |
| 1688 | """Test hashing of the contents of an entry""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1689 | _, _, _, out_dtb_fname = self._DoReadFileDtb('099_hash_section.dts', |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1690 | use_real_dtb=True, update_dtb=True) |
| 1691 | dtb = fdt.Fdt(out_dtb_fname) |
| 1692 | dtb.Scan() |
| 1693 | hash_node = dtb.GetNode('/binman/section/hash').props['value'] |
| 1694 | m = hashlib.sha256() |
| 1695 | m.update(U_BOOT_DATA) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1696 | m.update(tools.GetBytes(ord('a'), 16)) |
| 1697 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1698 | |
Simon Glass | f025363 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1699 | def testPackUBootTplMicrocode(self): |
| 1700 | """Test that x86 microcode can be handled correctly in TPL |
| 1701 | |
| 1702 | We expect to see the following in the image, in order: |
| 1703 | u-boot-tpl-nodtb.bin with a microcode pointer inserted at the correct |
| 1704 | place |
| 1705 | u-boot-tpl.dtb with the microcode removed |
| 1706 | the microcode |
| 1707 | """ |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1708 | with open(self.TestFile('u_boot_ucode_ptr'), 'rb') as fd: |
Simon Glass | f025363 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1709 | TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read()) |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1710 | first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts', |
Simon Glass | f025363 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1711 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1712 | self.assertEqual(b'tplnodtb with microc' + pos_and_size + |
| 1713 | b'ter somewhere in here', first) |
Simon Glass | f025363 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1714 | |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1715 | def testFmapX86(self): |
| 1716 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1717 | data = self._DoReadFile('094_fmap_x86.dts') |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1718 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1719 | expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('a'), 32 - 7) |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1720 | self.assertEqual(expected, data[:32]) |
| 1721 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
| 1722 | |
| 1723 | self.assertEqual(0x100, fhdr.image_size) |
| 1724 | |
| 1725 | self.assertEqual(0, fentries[0].offset) |
| 1726 | self.assertEqual(4, fentries[0].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1727 | self.assertEqual(b'U_BOOT', fentries[0].name) |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1728 | |
| 1729 | self.assertEqual(4, fentries[1].offset) |
| 1730 | self.assertEqual(3, fentries[1].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1731 | self.assertEqual(b'INTEL_MRC', fentries[1].name) |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1732 | |
| 1733 | self.assertEqual(32, fentries[2].offset) |
| 1734 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 1735 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1736 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1737 | |
| 1738 | def testFmapX86Section(self): |
| 1739 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1740 | data = self._DoReadFile('095_fmap_x86_section.dts') |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1741 | expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('b'), 32 - 7) |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1742 | self.assertEqual(expected, data[:32]) |
| 1743 | fhdr, fentries = fmap_util.DecodeFmap(data[36:]) |
| 1744 | |
| 1745 | self.assertEqual(0x100, fhdr.image_size) |
| 1746 | |
| 1747 | self.assertEqual(0, fentries[0].offset) |
| 1748 | self.assertEqual(4, fentries[0].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1749 | self.assertEqual(b'U_BOOT', fentries[0].name) |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1750 | |
| 1751 | self.assertEqual(4, fentries[1].offset) |
| 1752 | self.assertEqual(3, fentries[1].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1753 | self.assertEqual(b'INTEL_MRC', fentries[1].name) |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1754 | |
| 1755 | self.assertEqual(36, fentries[2].offset) |
| 1756 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 1757 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | c6c10e7 | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1758 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | f8f8df6 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1759 | |
Simon Glass | fe1ae3e | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1760 | def testElf(self): |
| 1761 | """Basic test of ELF entries""" |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1762 | self._SetupSplElf() |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1763 | with open(self.TestFile('bss_data'), 'rb') as fd: |
Simon Glass | 4c65025 | 2019-07-08 13:18:46 -0600 | [diff] [blame] | 1764 | TestFunctional._MakeInputFile('tpl/u-boot-tpl', fd.read()) |
| 1765 | with open(self.TestFile('bss_data'), 'rb') as fd: |
Simon Glass | fe1ae3e | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1766 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1767 | data = self._DoReadFile('096_elf.dts') |
Simon Glass | fe1ae3e | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1768 | |
Simon Glass | 093d168 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 1769 | def testElfStrip(self): |
Simon Glass | fe1ae3e | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1770 | """Basic test of ELF entries""" |
Simon Glass | 11ae93e | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1771 | self._SetupSplElf() |
Simon Glass | 1d0ebf7 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1772 | with open(self.TestFile('bss_data'), 'rb') as fd: |
Simon Glass | fe1ae3e | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1773 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1774 | data = self._DoReadFile('097_elf_strip.dts') |
Simon Glass | fe1ae3e | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1775 | |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1776 | def testPackOverlapMap(self): |
| 1777 | """Test that overlapping regions are detected""" |
| 1778 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1779 | with self.assertRaises(ValueError) as e: |
Simon Glass | 741f2d6 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1780 | self._DoTestFile('014_pack_overlap.dts', map=True) |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1781 | map_fname = tools.GetOutputFilename('image.map') |
| 1782 | self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname, |
| 1783 | stdout.getvalue()) |
| 1784 | |
| 1785 | # We should not get an inmage, but there should be a map file |
| 1786 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin'))) |
| 1787 | self.assertTrue(os.path.exists(map_fname)) |
Simon Glass | eb546ac | 2019-05-17 22:00:51 -0600 | [diff] [blame] | 1788 | map_data = tools.ReadFile(map_fname, binary=False) |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1789 | self.assertEqual('''ImagePos Offset Size Name |
| 1790 | <none> 00000000 00000007 main-section |
| 1791 | <none> 00000000 00000004 u-boot |
| 1792 | <none> 00000003 00000004 u-boot-align |
| 1793 | ''', map_data) |
| 1794 | |
Simon Glass | 093d168 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 1795 | def testPackRefCode(self): |
Simon Glass | 3ae192c | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 1796 | """Test that an image with an Intel Reference code binary works""" |
| 1797 | data = self._DoReadFile('100_intel_refcode.dts') |
| 1798 | self.assertEqual(REFCODE_DATA, data[:len(REFCODE_DATA)]) |
| 1799 | |
Simon Glass | 9481c80 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 1800 | def testSectionOffset(self): |
| 1801 | """Tests use of a section with an offset""" |
| 1802 | data, _, map_data, _ = self._DoReadFileDtb('101_sections_offset.dts', |
| 1803 | map=True) |
| 1804 | self.assertEqual('''ImagePos Offset Size Name |
| 1805 | 00000000 00000000 00000038 main-section |
| 1806 | 00000004 00000004 00000010 section@0 |
| 1807 | 00000004 00000000 00000004 u-boot |
| 1808 | 00000018 00000018 00000010 section@1 |
| 1809 | 00000018 00000000 00000004 u-boot |
| 1810 | 0000002c 0000002c 00000004 section@2 |
| 1811 | 0000002c 00000000 00000004 u-boot |
| 1812 | ''', map_data) |
| 1813 | self.assertEqual(data, |
Simon Glass | e6d85ff | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1814 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 1815 | tools.GetBytes(0x21, 12) + |
| 1816 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 1817 | tools.GetBytes(0x61, 12) + |
| 1818 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 1819 | tools.GetBytes(0x26, 8)) |
Simon Glass | 9481c80 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 1820 | |
Simon Glass | 53af22a | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1821 | |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1822 | if __name__ == "__main__": |
| 1823 | unittest.main() |