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