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 | |
| 9 | from optparse import OptionParser |
| 10 | import os |
| 11 | import shutil |
| 12 | import struct |
| 13 | import sys |
| 14 | import tempfile |
| 15 | import unittest |
| 16 | |
| 17 | import binman |
| 18 | import cmdline |
| 19 | import command |
| 20 | import control |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 21 | import elf |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 22 | import fdt |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 23 | import fdt_util |
| 24 | import tools |
| 25 | import tout |
| 26 | |
| 27 | # Contents of test files, corresponding to different entry types |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 28 | U_BOOT_DATA = '1234' |
| 29 | U_BOOT_IMG_DATA = 'img' |
Simon Glass | f689890 | 2017-11-13 18:54:59 -0700 | [diff] [blame] | 30 | U_BOOT_SPL_DATA = '56780123456789abcde' |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 31 | BLOB_DATA = '89' |
| 32 | ME_DATA = '0abcd' |
| 33 | VGA_DATA = 'vga' |
| 34 | U_BOOT_DTB_DATA = 'udtb' |
Simon Glass | 47419ea | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 35 | U_BOOT_SPL_DTB_DATA = 'spldtb' |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 36 | X86_START16_DATA = 'start16' |
| 37 | X86_START16_SPL_DATA = 'start16spl' |
| 38 | U_BOOT_NODTB_DATA = 'nodtb with microcode pointer somewhere in here' |
| 39 | U_BOOT_SPL_NODTB_DATA = 'splnodtb with microcode pointer somewhere in here' |
| 40 | FSP_DATA = 'fsp' |
| 41 | CMC_DATA = 'cmc' |
| 42 | VBT_DATA = 'vbt' |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 43 | MRC_DATA = 'mrc' |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 44 | |
| 45 | class TestFunctional(unittest.TestCase): |
| 46 | """Functional tests for binman |
| 47 | |
| 48 | Most of these use a sample .dts file to build an image and then check |
| 49 | that it looks correct. The sample files are in the test/ subdirectory |
| 50 | and are numbered. |
| 51 | |
| 52 | For each entry type a very small test file is created using fixed |
| 53 | string contents. This makes it easy to test that things look right, and |
| 54 | debug problems. |
| 55 | |
| 56 | In some cases a 'real' file must be used - these are also supplied in |
| 57 | the test/ diurectory. |
| 58 | """ |
| 59 | @classmethod |
| 60 | def setUpClass(self): |
Simon Glass | 4d5994f | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 61 | global entry |
| 62 | import entry |
| 63 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 64 | # Handle the case where argv[0] is 'python' |
| 65 | self._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 66 | self._binman_pathname = os.path.join(self._binman_dir, 'binman') |
| 67 | |
| 68 | # Create a temporary directory for input files |
| 69 | self._indir = tempfile.mkdtemp(prefix='binmant.') |
| 70 | |
| 71 | # Create some test files |
| 72 | TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) |
| 73 | TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA) |
| 74 | TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) |
| 75 | TestFunctional._MakeInputFile('blobfile', BLOB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 76 | TestFunctional._MakeInputFile('me.bin', ME_DATA) |
| 77 | TestFunctional._MakeInputFile('vga.bin', VGA_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 78 | TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) |
Simon Glass | 47419ea | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 79 | TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 80 | TestFunctional._MakeInputFile('u-boot-x86-16bit.bin', X86_START16_DATA) |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 81 | TestFunctional._MakeInputFile('spl/u-boot-x86-16bit-spl.bin', |
| 82 | X86_START16_SPL_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 83 | TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA) |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 84 | TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin', |
| 85 | U_BOOT_SPL_NODTB_DATA) |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 86 | TestFunctional._MakeInputFile('fsp.bin', FSP_DATA) |
| 87 | TestFunctional._MakeInputFile('cmc.bin', CMC_DATA) |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 88 | TestFunctional._MakeInputFile('vbt.bin', VBT_DATA) |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 89 | TestFunctional._MakeInputFile('mrc.bin', MRC_DATA) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 90 | self._output_setup = False |
| 91 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 92 | # ELF file with a '_dt_ucode_base_size' symbol |
| 93 | with open(self.TestFile('u_boot_ucode_ptr')) as fd: |
| 94 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 95 | |
| 96 | # Intel flash descriptor file |
| 97 | with open(self.TestFile('descriptor.bin')) as fd: |
| 98 | TestFunctional._MakeInputFile('descriptor.bin', fd.read()) |
| 99 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 100 | @classmethod |
| 101 | def tearDownClass(self): |
| 102 | """Remove the temporary input directory and its contents""" |
| 103 | if self._indir: |
| 104 | shutil.rmtree(self._indir) |
| 105 | self._indir = None |
| 106 | |
| 107 | def setUp(self): |
| 108 | # Enable this to turn on debugging output |
| 109 | # tout.Init(tout.DEBUG) |
| 110 | command.test_result = None |
| 111 | |
| 112 | def tearDown(self): |
| 113 | """Remove the temporary output directory""" |
| 114 | tools._FinaliseForTest() |
| 115 | |
| 116 | def _RunBinman(self, *args, **kwargs): |
| 117 | """Run binman using the command line |
| 118 | |
| 119 | Args: |
| 120 | Arguments to pass, as a list of strings |
| 121 | kwargs: Arguments to pass to Command.RunPipe() |
| 122 | """ |
| 123 | result = command.RunPipe([[self._binman_pathname] + list(args)], |
| 124 | capture=True, capture_stderr=True, raise_on_error=False) |
| 125 | if result.return_code and kwargs.get('raise_on_error', True): |
| 126 | raise Exception("Error running '%s': %s" % (' '.join(args), |
| 127 | result.stdout + result.stderr)) |
| 128 | return result |
| 129 | |
| 130 | def _DoBinman(self, *args): |
| 131 | """Run binman using directly (in the same process) |
| 132 | |
| 133 | Args: |
| 134 | Arguments to pass, as a list of strings |
| 135 | Returns: |
| 136 | Return value (0 for success) |
| 137 | """ |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 138 | args = list(args) |
| 139 | if '-D' in sys.argv: |
| 140 | args = args + ['-D'] |
| 141 | (options, args) = cmdline.ParseArgs(args) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 142 | options.pager = 'binman-invalid-pager' |
| 143 | options.build_dir = self._indir |
| 144 | |
| 145 | # For testing, you can force an increase in verbosity here |
| 146 | # options.verbosity = tout.DEBUG |
| 147 | return control.Binman(options, args) |
| 148 | |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 149 | def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False): |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 150 | """Run binman with a given test file |
| 151 | |
| 152 | Args: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 153 | fname: Device-tree source filename to use (e.g. 05_simple.dts) |
| 154 | debug: True to enable debugging output |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 155 | map: True to output map files for the images |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 156 | 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] | 157 | tree before packing it into the image |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 158 | """ |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 159 | args = ['-p', '-I', self._indir, '-d', self.TestFile(fname)] |
| 160 | if debug: |
| 161 | args.append('-D') |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 162 | if map: |
| 163 | args.append('-m') |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 164 | if update_dtb: |
| 165 | args.append('-up') |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 166 | return self._DoBinman(*args) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 167 | |
| 168 | def _SetupDtb(self, fname, outfile='u-boot.dtb'): |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 169 | """Set up a new test device-tree file |
| 170 | |
| 171 | The given file is compiled and set up as the device tree to be used |
| 172 | for ths test. |
| 173 | |
| 174 | Args: |
| 175 | fname: Filename of .dts file to read |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 176 | outfile: Output filename for compiled device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 177 | |
| 178 | Returns: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 179 | Contents of device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 180 | """ |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 181 | if not self._output_setup: |
| 182 | tools.PrepareOutputDir(self._indir, True) |
| 183 | self._output_setup = True |
| 184 | dtb = fdt_util.EnsureCompiled(self.TestFile(fname)) |
| 185 | with open(dtb) as fd: |
| 186 | data = fd.read() |
| 187 | TestFunctional._MakeInputFile(outfile, data) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 188 | return data |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 189 | |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 190 | def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False, |
| 191 | update_dtb=False): |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 192 | """Run binman and return the resulting image |
| 193 | |
| 194 | This runs binman with a given test file and then reads the resulting |
| 195 | output file. It is a shortcut function since most tests need to do |
| 196 | these steps. |
| 197 | |
| 198 | Raises an assertion failure if binman returns a non-zero exit code. |
| 199 | |
| 200 | Args: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 201 | fname: Device-tree source filename to use (e.g. 05_simple.dts) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 202 | use_real_dtb: True to use the test file as the contents of |
| 203 | the u-boot-dtb entry. Normally this is not needed and the |
| 204 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 205 | But in some test we need the real contents. |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 206 | map: True to output map files for the images |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 207 | 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] | 208 | tree before packing it into the image |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 209 | |
| 210 | Returns: |
| 211 | Tuple: |
| 212 | Resulting image contents |
| 213 | Device tree contents |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 214 | Map data showing contents of image (or None if none) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 215 | """ |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 216 | dtb_data = None |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 217 | # Use the compiled test file as the u-boot-dtb input |
| 218 | if use_real_dtb: |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 219 | dtb_data = self._SetupDtb(fname) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 220 | |
| 221 | try: |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 222 | retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 223 | self.assertEqual(0, retcode) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 224 | out_dtb_fname = control.GetFdtPath('u-boot.dtb') |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 225 | |
| 226 | # Find the (only) image, read it and return its contents |
| 227 | image = control.images['image'] |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 228 | image_fname = tools.GetOutputFilename('image.bin') |
| 229 | self.assertTrue(os.path.exists(image_fname)) |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 230 | if map: |
| 231 | map_fname = tools.GetOutputFilename('image.map') |
| 232 | with open(map_fname) as fd: |
| 233 | map_data = fd.read() |
| 234 | else: |
| 235 | map_data = None |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 236 | with open(image_fname) as fd: |
| 237 | return fd.read(), dtb_data, map_data, out_dtb_fname |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 238 | finally: |
| 239 | # Put the test file back |
| 240 | if use_real_dtb: |
| 241 | TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) |
| 242 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 243 | def _DoReadFile(self, fname, use_real_dtb=False): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 244 | """Helper function which discards the device-tree binary |
| 245 | |
| 246 | Args: |
| 247 | fname: Device-tree source filename to use (e.g. 05_simple.dts) |
| 248 | use_real_dtb: True to use the test file as the contents of |
| 249 | the u-boot-dtb entry. Normally this is not needed and the |
| 250 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 251 | But in some test we need the real contents. |
| 252 | """ |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 253 | return self._DoReadFileDtb(fname, use_real_dtb)[0] |
| 254 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 255 | @classmethod |
| 256 | def _MakeInputFile(self, fname, contents): |
| 257 | """Create a new test input file, creating directories as needed |
| 258 | |
| 259 | Args: |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 260 | fname: Filename to create |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 261 | contents: File contents to write in to the file |
| 262 | Returns: |
| 263 | Full pathname of file created |
| 264 | """ |
| 265 | pathname = os.path.join(self._indir, fname) |
| 266 | dirname = os.path.dirname(pathname) |
| 267 | if dirname and not os.path.exists(dirname): |
| 268 | os.makedirs(dirname) |
| 269 | with open(pathname, 'wb') as fd: |
| 270 | fd.write(contents) |
| 271 | return pathname |
| 272 | |
| 273 | @classmethod |
| 274 | def TestFile(self, fname): |
| 275 | return os.path.join(self._binman_dir, 'test', fname) |
| 276 | |
| 277 | def AssertInList(self, grep_list, target): |
| 278 | """Assert that at least one of a list of things is in a target |
| 279 | |
| 280 | Args: |
| 281 | grep_list: List of strings to check |
| 282 | target: Target string |
| 283 | """ |
| 284 | for grep in grep_list: |
| 285 | if grep in target: |
| 286 | return |
| 287 | self.fail("Error: '%' not found in '%s'" % (grep_list, target)) |
| 288 | |
| 289 | def CheckNoGaps(self, entries): |
| 290 | """Check that all entries fit together without gaps |
| 291 | |
| 292 | Args: |
| 293 | entries: List of entries to check |
| 294 | """ |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 295 | offset = 0 |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 296 | for entry in entries.values(): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 297 | self.assertEqual(offset, entry.offset) |
| 298 | offset += entry.size |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 299 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 300 | def GetFdtLen(self, dtb): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 301 | """Get the totalsize field from a device-tree binary |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 302 | |
| 303 | Args: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 304 | dtb: Device-tree binary contents |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 305 | |
| 306 | Returns: |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 307 | Total size of device-tree binary, from the header |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 308 | """ |
| 309 | return struct.unpack('>L', dtb[4:8])[0] |
| 310 | |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 311 | def _GetPropTree(self, dtb_data, node_names): |
| 312 | def AddNode(node, path): |
| 313 | if node.name != '/': |
| 314 | path += '/' + node.name |
| 315 | #print 'path', path |
| 316 | for subnode in node.subnodes: |
| 317 | for prop in subnode.props.values(): |
| 318 | if prop.name in node_names: |
| 319 | prop_path = path + '/' + subnode.name + ':' + prop.name |
| 320 | tree[prop_path[len('/binman/'):]] = fdt_util.fdt32_to_cpu( |
| 321 | prop.value) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 322 | AddNode(subnode, path) |
| 323 | |
| 324 | tree = {} |
| 325 | dtb = fdt.Fdt(dtb_data) |
| 326 | dtb.Scan() |
| 327 | AddNode(dtb.GetRoot(), '') |
| 328 | return tree |
| 329 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 330 | def testRun(self): |
| 331 | """Test a basic run with valid args""" |
| 332 | result = self._RunBinman('-h') |
| 333 | |
| 334 | def testFullHelp(self): |
| 335 | """Test that the full help is displayed with -H""" |
| 336 | result = self._RunBinman('-H') |
| 337 | help_file = os.path.join(self._binman_dir, 'README') |
Tom Rini | 3759df0 | 2018-01-16 15:29:50 -0500 | [diff] [blame] | 338 | # Remove possible extraneous strings |
| 339 | extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n' |
| 340 | gothelp = result.stdout.replace(extra, '') |
| 341 | self.assertEqual(len(gothelp), os.path.getsize(help_file)) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 342 | self.assertEqual(0, len(result.stderr)) |
| 343 | self.assertEqual(0, result.return_code) |
| 344 | |
| 345 | def testFullHelpInternal(self): |
| 346 | """Test that the full help is displayed with -H""" |
| 347 | try: |
| 348 | command.test_result = command.CommandResult() |
| 349 | result = self._DoBinman('-H') |
| 350 | help_file = os.path.join(self._binman_dir, 'README') |
| 351 | finally: |
| 352 | command.test_result = None |
| 353 | |
| 354 | def testHelp(self): |
| 355 | """Test that the basic help is displayed with -h""" |
| 356 | result = self._RunBinman('-h') |
| 357 | self.assertTrue(len(result.stdout) > 200) |
| 358 | self.assertEqual(0, len(result.stderr)) |
| 359 | self.assertEqual(0, result.return_code) |
| 360 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 361 | def testBoard(self): |
| 362 | """Test that we can run it with a specific board""" |
| 363 | self._SetupDtb('05_simple.dts', 'sandbox/u-boot.dtb') |
| 364 | TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA) |
| 365 | result = self._DoBinman('-b', 'sandbox') |
| 366 | self.assertEqual(0, result) |
| 367 | |
| 368 | def testNeedBoard(self): |
| 369 | """Test that we get an error when no board ius supplied""" |
| 370 | with self.assertRaises(ValueError) as e: |
| 371 | result = self._DoBinman() |
| 372 | self.assertIn("Must provide a board to process (use -b <board>)", |
| 373 | str(e.exception)) |
| 374 | |
| 375 | def testMissingDt(self): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 376 | """Test that an invalid device-tree file generates an error""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 377 | with self.assertRaises(Exception) as e: |
| 378 | self._RunBinman('-d', 'missing_file') |
| 379 | # We get one error from libfdt, and a different one from fdtget. |
| 380 | self.AssertInList(["Couldn't open blob from 'missing_file'", |
| 381 | 'No such file or directory'], str(e.exception)) |
| 382 | |
| 383 | def testBrokenDt(self): |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 384 | """Test that an invalid device-tree source file generates an error |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 385 | |
| 386 | Since this is a source file it should be compiled and the error |
| 387 | will come from the device-tree compiler (dtc). |
| 388 | """ |
| 389 | with self.assertRaises(Exception) as e: |
| 390 | self._RunBinman('-d', self.TestFile('01_invalid.dts')) |
| 391 | self.assertIn("FATAL ERROR: Unable to parse input tree", |
| 392 | str(e.exception)) |
| 393 | |
| 394 | def testMissingNode(self): |
| 395 | """Test that a device tree without a 'binman' node generates an error""" |
| 396 | with self.assertRaises(Exception) as e: |
| 397 | self._DoBinman('-d', self.TestFile('02_missing_node.dts')) |
| 398 | self.assertIn("does not have a 'binman' node", str(e.exception)) |
| 399 | |
| 400 | def testEmpty(self): |
| 401 | """Test that an empty binman node works OK (i.e. does nothing)""" |
| 402 | result = self._RunBinman('-d', self.TestFile('03_empty.dts')) |
| 403 | self.assertEqual(0, len(result.stderr)) |
| 404 | self.assertEqual(0, result.return_code) |
| 405 | |
| 406 | def testInvalidEntry(self): |
| 407 | """Test that an invalid entry is flagged""" |
| 408 | with self.assertRaises(Exception) as e: |
| 409 | result = self._RunBinman('-d', |
| 410 | self.TestFile('04_invalid_entry.dts')) |
| 411 | #print e.exception |
| 412 | self.assertIn("Unknown entry type 'not-a-valid-type' in node " |
| 413 | "'/binman/not-a-valid-type'", str(e.exception)) |
| 414 | |
| 415 | def testSimple(self): |
| 416 | """Test a simple binman with a single file""" |
| 417 | data = self._DoReadFile('05_simple.dts') |
| 418 | self.assertEqual(U_BOOT_DATA, data) |
| 419 | |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 420 | def testSimpleDebug(self): |
| 421 | """Test a simple binman run with debugging enabled""" |
| 422 | data = self._DoTestFile('05_simple.dts', debug=True) |
| 423 | |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 424 | def testDual(self): |
| 425 | """Test that we can handle creating two images |
| 426 | |
| 427 | This also tests image padding. |
| 428 | """ |
| 429 | retcode = self._DoTestFile('06_dual_image.dts') |
| 430 | self.assertEqual(0, retcode) |
| 431 | |
| 432 | image = control.images['image1'] |
| 433 | self.assertEqual(len(U_BOOT_DATA), image._size) |
| 434 | fname = tools.GetOutputFilename('image1.bin') |
| 435 | self.assertTrue(os.path.exists(fname)) |
| 436 | with open(fname) as fd: |
| 437 | data = fd.read() |
| 438 | self.assertEqual(U_BOOT_DATA, data) |
| 439 | |
| 440 | image = control.images['image2'] |
| 441 | self.assertEqual(3 + len(U_BOOT_DATA) + 5, image._size) |
| 442 | fname = tools.GetOutputFilename('image2.bin') |
| 443 | self.assertTrue(os.path.exists(fname)) |
| 444 | with open(fname) as fd: |
| 445 | data = fd.read() |
| 446 | self.assertEqual(U_BOOT_DATA, data[3:7]) |
| 447 | self.assertEqual(chr(0) * 3, data[:3]) |
| 448 | self.assertEqual(chr(0) * 5, data[7:]) |
| 449 | |
| 450 | def testBadAlign(self): |
| 451 | """Test that an invalid alignment value is detected""" |
| 452 | with self.assertRaises(ValueError) as e: |
| 453 | self._DoTestFile('07_bad_align.dts') |
| 454 | self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power " |
| 455 | "of two", str(e.exception)) |
| 456 | |
| 457 | def testPackSimple(self): |
| 458 | """Test that packing works as expected""" |
| 459 | retcode = self._DoTestFile('08_pack.dts') |
| 460 | self.assertEqual(0, retcode) |
| 461 | self.assertIn('image', control.images) |
| 462 | image = control.images['image'] |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 463 | entries = image.GetEntries() |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 464 | self.assertEqual(5, len(entries)) |
| 465 | |
| 466 | # First u-boot |
| 467 | self.assertIn('u-boot', entries) |
| 468 | entry = entries['u-boot'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 469 | self.assertEqual(0, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 470 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 471 | |
| 472 | # Second u-boot, aligned to 16-byte boundary |
| 473 | self.assertIn('u-boot-align', entries) |
| 474 | entry = entries['u-boot-align'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 475 | self.assertEqual(16, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 476 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 477 | |
| 478 | # Third u-boot, size 23 bytes |
| 479 | self.assertIn('u-boot-size', entries) |
| 480 | entry = entries['u-boot-size'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 481 | self.assertEqual(20, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 482 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 483 | self.assertEqual(23, entry.size) |
| 484 | |
| 485 | # Fourth u-boot, placed immediate after the above |
| 486 | self.assertIn('u-boot-next', entries) |
| 487 | entry = entries['u-boot-next'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 488 | self.assertEqual(43, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 489 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 490 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 491 | # Fifth u-boot, placed at a fixed offset |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 492 | self.assertIn('u-boot-fixed', entries) |
| 493 | entry = entries['u-boot-fixed'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 494 | self.assertEqual(61, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 495 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 496 | |
| 497 | self.assertEqual(65, image._size) |
| 498 | |
| 499 | def testPackExtra(self): |
| 500 | """Test that extra packing feature works as expected""" |
| 501 | retcode = self._DoTestFile('09_pack_extra.dts') |
| 502 | |
| 503 | self.assertEqual(0, retcode) |
| 504 | self.assertIn('image', control.images) |
| 505 | image = control.images['image'] |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 506 | entries = image.GetEntries() |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 507 | self.assertEqual(5, len(entries)) |
| 508 | |
| 509 | # First u-boot with padding before and after |
| 510 | self.assertIn('u-boot', entries) |
| 511 | entry = entries['u-boot'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 512 | self.assertEqual(0, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 513 | self.assertEqual(3, entry.pad_before) |
| 514 | self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size) |
| 515 | |
| 516 | # Second u-boot has an aligned size, but it has no effect |
| 517 | self.assertIn('u-boot-align-size-nop', entries) |
| 518 | entry = entries['u-boot-align-size-nop'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 519 | self.assertEqual(12, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 520 | self.assertEqual(4, entry.size) |
| 521 | |
| 522 | # Third u-boot has an aligned size too |
| 523 | self.assertIn('u-boot-align-size', entries) |
| 524 | entry = entries['u-boot-align-size'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 525 | self.assertEqual(16, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 526 | self.assertEqual(32, entry.size) |
| 527 | |
| 528 | # Fourth u-boot has an aligned end |
| 529 | self.assertIn('u-boot-align-end', entries) |
| 530 | entry = entries['u-boot-align-end'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 531 | self.assertEqual(48, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 532 | self.assertEqual(16, entry.size) |
| 533 | |
| 534 | # Fifth u-boot immediately afterwards |
| 535 | self.assertIn('u-boot-align-both', entries) |
| 536 | entry = entries['u-boot-align-both'] |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 537 | self.assertEqual(64, entry.offset) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 538 | self.assertEqual(64, entry.size) |
| 539 | |
| 540 | self.CheckNoGaps(entries) |
| 541 | self.assertEqual(128, image._size) |
| 542 | |
| 543 | def testPackAlignPowerOf2(self): |
| 544 | """Test that invalid entry alignment is detected""" |
| 545 | with self.assertRaises(ValueError) as e: |
| 546 | self._DoTestFile('10_pack_align_power2.dts') |
| 547 | self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power " |
| 548 | "of two", str(e.exception)) |
| 549 | |
| 550 | def testPackAlignSizePowerOf2(self): |
| 551 | """Test that invalid entry size alignment is detected""" |
| 552 | with self.assertRaises(ValueError) as e: |
| 553 | self._DoTestFile('11_pack_align_size_power2.dts') |
| 554 | self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a " |
| 555 | "power of two", str(e.exception)) |
| 556 | |
| 557 | def testPackInvalidAlign(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 558 | """Test detection of an offset that does not match its alignment""" |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 559 | with self.assertRaises(ValueError) as e: |
| 560 | self._DoTestFile('12_pack_inv_align.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 561 | self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 562 | "align 0x4 (4)", str(e.exception)) |
| 563 | |
| 564 | def testPackInvalidSizeAlign(self): |
| 565 | """Test that invalid entry size alignment is detected""" |
| 566 | with self.assertRaises(ValueError) as e: |
| 567 | self._DoTestFile('13_pack_inv_size_align.dts') |
| 568 | self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match " |
| 569 | "align-size 0x4 (4)", str(e.exception)) |
| 570 | |
| 571 | def testPackOverlap(self): |
| 572 | """Test that overlapping regions are detected""" |
| 573 | with self.assertRaises(ValueError) as e: |
| 574 | self._DoTestFile('14_pack_overlap.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 575 | self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 576 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 577 | str(e.exception)) |
| 578 | |
| 579 | def testPackEntryOverflow(self): |
| 580 | """Test that entries that overflow their size are detected""" |
| 581 | with self.assertRaises(ValueError) as e: |
| 582 | self._DoTestFile('15_pack_overflow.dts') |
| 583 | self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) " |
| 584 | "but entry size is 0x3 (3)", str(e.exception)) |
| 585 | |
| 586 | def testPackImageOverflow(self): |
| 587 | """Test that entries which overflow the image size are detected""" |
| 588 | with self.assertRaises(ValueError) as e: |
| 589 | self._DoTestFile('16_pack_image_overflow.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 590 | self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 591 | "size 0x3 (3)", str(e.exception)) |
| 592 | |
| 593 | def testPackImageSize(self): |
| 594 | """Test that the image size can be set""" |
| 595 | retcode = self._DoTestFile('17_pack_image_size.dts') |
| 596 | self.assertEqual(0, retcode) |
| 597 | self.assertIn('image', control.images) |
| 598 | image = control.images['image'] |
| 599 | self.assertEqual(7, image._size) |
| 600 | |
| 601 | def testPackImageSizeAlign(self): |
| 602 | """Test that image size alignemnt works as expected""" |
| 603 | retcode = self._DoTestFile('18_pack_image_align.dts') |
| 604 | self.assertEqual(0, retcode) |
| 605 | self.assertIn('image', control.images) |
| 606 | image = control.images['image'] |
| 607 | self.assertEqual(16, image._size) |
| 608 | |
| 609 | def testPackInvalidImageAlign(self): |
| 610 | """Test that invalid image alignment is detected""" |
| 611 | with self.assertRaises(ValueError) as e: |
| 612 | self._DoTestFile('19_pack_inv_image_align.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 613 | self.assertIn("Section '/binman': Size 0x7 (7) does not match " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 614 | "align-size 0x8 (8)", str(e.exception)) |
| 615 | |
| 616 | def testPackAlignPowerOf2(self): |
| 617 | """Test that invalid image alignment is detected""" |
| 618 | with self.assertRaises(ValueError) as e: |
| 619 | self._DoTestFile('20_pack_inv_image_align_power2.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 620 | self.assertIn("Section '/binman': Alignment size 131 must be a power of " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 621 | "two", str(e.exception)) |
| 622 | |
| 623 | def testImagePadByte(self): |
| 624 | """Test that the image pad byte can be specified""" |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 625 | with open(self.TestFile('bss_data')) as fd: |
| 626 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 627 | data = self._DoReadFile('21_image_pad.dts') |
Simon Glass | f689890 | 2017-11-13 18:54:59 -0700 | [diff] [blame] | 628 | self.assertEqual(U_BOOT_SPL_DATA + (chr(0xff) * 1) + U_BOOT_DATA, data) |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 629 | |
| 630 | def testImageName(self): |
| 631 | """Test that image files can be named""" |
| 632 | retcode = self._DoTestFile('22_image_name.dts') |
| 633 | self.assertEqual(0, retcode) |
| 634 | image = control.images['image1'] |
| 635 | fname = tools.GetOutputFilename('test-name') |
| 636 | self.assertTrue(os.path.exists(fname)) |
| 637 | |
| 638 | image = control.images['image2'] |
| 639 | fname = tools.GetOutputFilename('test-name.xx') |
| 640 | self.assertTrue(os.path.exists(fname)) |
| 641 | |
| 642 | def testBlobFilename(self): |
| 643 | """Test that generic blobs can be provided by filename""" |
| 644 | data = self._DoReadFile('23_blob.dts') |
| 645 | self.assertEqual(BLOB_DATA, data) |
| 646 | |
| 647 | def testPackSorted(self): |
| 648 | """Test that entries can be sorted""" |
| 649 | data = self._DoReadFile('24_sorted.dts') |
Simon Glass | f689890 | 2017-11-13 18:54:59 -0700 | [diff] [blame] | 650 | self.assertEqual(chr(0) * 1 + U_BOOT_SPL_DATA + chr(0) * 2 + |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 651 | U_BOOT_DATA, data) |
| 652 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 653 | def testPackZeroOffset(self): |
| 654 | """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] | 655 | with self.assertRaises(ValueError) as e: |
| 656 | self._DoTestFile('25_pack_zero_size.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 657 | self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps " |
Simon Glass | 4f44304 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 658 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 659 | str(e.exception)) |
| 660 | |
| 661 | def testPackUbootDtb(self): |
| 662 | """Test that a device tree can be added to U-Boot""" |
| 663 | data = self._DoReadFile('26_pack_u_boot_dtb.dts') |
| 664 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 665 | |
| 666 | def testPackX86RomNoSize(self): |
| 667 | """Test that the end-at-4gb property requires a size property""" |
| 668 | with self.assertRaises(ValueError) as e: |
| 669 | self._DoTestFile('27_pack_4gb_no_size.dts') |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 670 | self.assertIn("Section '/binman': Section size must be provided when " |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 671 | "using end-at-4gb", str(e.exception)) |
| 672 | |
| 673 | def testPackX86RomOutside(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 674 | """Test that the end-at-4gb property checks for offset boundaries""" |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 675 | with self.assertRaises(ValueError) as e: |
| 676 | self._DoTestFile('28_pack_4gb_outside.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 677 | self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside " |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 678 | "the section starting at 0xffffffe0 (4294967264)", |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 679 | str(e.exception)) |
| 680 | |
| 681 | def testPackX86Rom(self): |
| 682 | """Test that a basic x86 ROM can be created""" |
| 683 | data = self._DoReadFile('29_x86-rom.dts') |
Simon Glass | f689890 | 2017-11-13 18:54:59 -0700 | [diff] [blame] | 684 | self.assertEqual(U_BOOT_DATA + chr(0) * 7 + U_BOOT_SPL_DATA + |
| 685 | chr(0) * 2, data) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 686 | |
| 687 | def testPackX86RomMeNoDesc(self): |
| 688 | """Test that an invalid Intel descriptor entry is detected""" |
| 689 | TestFunctional._MakeInputFile('descriptor.bin', '') |
| 690 | with self.assertRaises(ValueError) as e: |
| 691 | self._DoTestFile('31_x86-rom-me.dts') |
| 692 | self.assertIn("Node '/binman/intel-descriptor': Cannot find FD " |
| 693 | "signature", str(e.exception)) |
| 694 | |
| 695 | def testPackX86RomBadDesc(self): |
| 696 | """Test that the Intel requires a descriptor entry""" |
| 697 | with self.assertRaises(ValueError) as e: |
| 698 | self._DoTestFile('30_x86-rom-me-no-desc.dts') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 699 | self.assertIn("Node '/binman/intel-me': No offset set with " |
| 700 | "offset-unset: should another entry provide this correct " |
| 701 | "offset?", str(e.exception)) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 702 | |
| 703 | def testPackX86RomMe(self): |
| 704 | """Test that an x86 ROM with an ME region can be created""" |
| 705 | data = self._DoReadFile('31_x86-rom-me.dts') |
| 706 | self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)]) |
| 707 | |
| 708 | def testPackVga(self): |
| 709 | """Test that an image with a VGA binary can be created""" |
| 710 | data = self._DoReadFile('32_intel-vga.dts') |
| 711 | self.assertEqual(VGA_DATA, data[:len(VGA_DATA)]) |
| 712 | |
| 713 | def testPackStart16(self): |
| 714 | """Test that an image with an x86 start16 region can be created""" |
| 715 | data = self._DoReadFile('33_x86-start16.dts') |
| 716 | self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) |
| 717 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 718 | def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 719 | """Handle running a test for insertion of microcode |
| 720 | |
| 721 | Args: |
| 722 | dts_fname: Name of test .dts file |
| 723 | nodtb_data: Data that we expect in the first section |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 724 | ucode_second: True if the microsecond entry is second instead of |
| 725 | third |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 726 | |
| 727 | Returns: |
| 728 | Tuple: |
| 729 | Contents of first region (U-Boot or SPL) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 730 | Offset and size components of microcode pointer, as inserted |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 731 | in the above (two 4-byte words) |
| 732 | """ |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 733 | data = self._DoReadFile(dts_fname, True) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 734 | |
| 735 | # Now check the device tree has no microcode |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 736 | if ucode_second: |
| 737 | ucode_content = data[len(nodtb_data):] |
| 738 | ucode_pos = len(nodtb_data) |
| 739 | dtb_with_ucode = ucode_content[16:] |
| 740 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 741 | else: |
| 742 | dtb_with_ucode = data[len(nodtb_data):] |
| 743 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 744 | ucode_content = dtb_with_ucode[fdt_len:] |
| 745 | ucode_pos = len(nodtb_data) + fdt_len |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 746 | fname = tools.GetOutputFilename('test.dtb') |
| 747 | with open(fname, 'wb') as fd: |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 748 | fd.write(dtb_with_ucode) |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 749 | dtb = fdt.FdtScan(fname) |
| 750 | ucode = dtb.GetNode('/microcode') |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 751 | self.assertTrue(ucode) |
| 752 | for node in ucode.subnodes: |
| 753 | self.assertFalse(node.props.get('data')) |
| 754 | |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 755 | # Check that the microcode appears immediately after the Fdt |
| 756 | # This matches the concatenation of the data properties in |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 757 | # the /microcode/update@xxx nodes in 34_x86_ucode.dts. |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 758 | ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, |
| 759 | 0x78235609) |
Simon Glass | adc5701 | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 760 | self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 761 | |
| 762 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 763 | # expected offset and size |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 764 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 765 | len(ucode_data)) |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 766 | u_boot = data[:len(nodtb_data)] |
| 767 | return u_boot, pos_and_size |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 768 | |
| 769 | def testPackUbootMicrocode(self): |
| 770 | """Test that x86 microcode can be handled correctly |
| 771 | |
| 772 | We expect to see the following in the image, in order: |
| 773 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 774 | place |
| 775 | u-boot.dtb with the microcode removed |
| 776 | the microcode |
| 777 | """ |
| 778 | first, pos_and_size = self._RunMicrocodeTest('34_x86_ucode.dts', |
| 779 | U_BOOT_NODTB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 780 | self.assertEqual('nodtb with microcode' + pos_and_size + |
| 781 | ' somewhere in here', first) |
| 782 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 783 | def _RunPackUbootSingleMicrocode(self): |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 784 | """Test that x86 microcode can be handled correctly |
| 785 | |
| 786 | We expect to see the following in the image, in order: |
| 787 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 788 | place |
| 789 | u-boot.dtb with the microcode |
| 790 | an empty microcode region |
| 791 | """ |
| 792 | # We need the libfdt library to run this test since only that allows |
| 793 | # finding the offset of a property. This is required by |
| 794 | # Entry_u_boot_dtb_with_ucode.ObtainContents(). |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 795 | data = self._DoReadFile('35_x86_single_ucode.dts', True) |
| 796 | |
| 797 | second = data[len(U_BOOT_NODTB_DATA):] |
| 798 | |
| 799 | fdt_len = self.GetFdtLen(second) |
| 800 | third = second[fdt_len:] |
| 801 | second = second[:fdt_len] |
| 802 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 803 | ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) |
| 804 | self.assertIn(ucode_data, second) |
| 805 | ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) |
Simon Glass | e0ff855 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 806 | |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 807 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 808 | # expected offset and size |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 809 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 810 | len(ucode_data)) |
| 811 | first = data[:len(U_BOOT_NODTB_DATA)] |
| 812 | self.assertEqual('nodtb with microcode' + pos_and_size + |
| 813 | ' somewhere in here', first) |
Simon Glass | c49deb8 | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 814 | |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 815 | def testPackUbootSingleMicrocode(self): |
| 816 | """Test that x86 microcode can be handled correctly with fdt_normal. |
| 817 | """ |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 818 | self._RunPackUbootSingleMicrocode() |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 819 | |
Simon Glass | c49deb8 | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 820 | def testUBootImg(self): |
| 821 | """Test that u-boot.img can be put in a file""" |
| 822 | data = self._DoReadFile('36_u_boot_img.dts') |
| 823 | self.assertEqual(U_BOOT_IMG_DATA, data) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 824 | |
| 825 | def testNoMicrocode(self): |
| 826 | """Test that a missing microcode region is detected""" |
| 827 | with self.assertRaises(ValueError) as e: |
| 828 | self._DoReadFile('37_x86_no_ucode.dts', True) |
| 829 | self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode " |
| 830 | "node found in ", str(e.exception)) |
| 831 | |
| 832 | def testMicrocodeWithoutNode(self): |
| 833 | """Test that a missing u-boot-dtb-with-ucode node is detected""" |
| 834 | with self.assertRaises(ValueError) as e: |
| 835 | self._DoReadFile('38_x86_ucode_missing_node.dts', True) |
| 836 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 837 | "microcode region u-boot-dtb-with-ucode", str(e.exception)) |
| 838 | |
| 839 | def testMicrocodeWithoutNode2(self): |
| 840 | """Test that a missing u-boot-ucode node is detected""" |
| 841 | with self.assertRaises(ValueError) as e: |
| 842 | self._DoReadFile('39_x86_ucode_missing_node2.dts', True) |
| 843 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 844 | "microcode region u-boot-ucode", str(e.exception)) |
| 845 | |
| 846 | def testMicrocodeWithoutPtrInElf(self): |
| 847 | """Test that a U-Boot binary without the microcode symbol is detected""" |
| 848 | # ELF file without a '_dt_ucode_base_size' symbol |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 849 | try: |
| 850 | with open(self.TestFile('u_boot_no_ucode_ptr')) as fd: |
| 851 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 852 | |
| 853 | with self.assertRaises(ValueError) as e: |
Simon Glass | 160a766 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 854 | self._RunPackUbootSingleMicrocode() |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 855 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate " |
| 856 | "_dt_ucode_base_size symbol in u-boot", str(e.exception)) |
| 857 | |
| 858 | finally: |
| 859 | # Put the original file back |
| 860 | with open(self.TestFile('u_boot_ucode_ptr')) as fd: |
| 861 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
| 862 | |
| 863 | def testMicrocodeNotInImage(self): |
| 864 | """Test that microcode must be placed within the image""" |
| 865 | with self.assertRaises(ValueError) as e: |
| 866 | self._DoReadFile('40_x86_ucode_not_in_image.dts', True) |
| 867 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode " |
| 868 | "pointer _dt_ucode_base_size at fffffe14 is outside the " |
Simon Glass | 25ac0e6 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 869 | "section ranging from 00000000 to 0000002e", str(e.exception)) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 870 | |
| 871 | def testWithoutMicrocode(self): |
| 872 | """Test that we can cope with an image without microcode (e.g. qemu)""" |
| 873 | with open(self.TestFile('u_boot_no_ucode_ptr')) as fd: |
| 874 | TestFunctional._MakeInputFile('u-boot', fd.read()) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 875 | data, dtb, _, _ = self._DoReadFileDtb('44_x86_optional_ucode.dts', True) |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 876 | |
| 877 | # Now check the device tree has no microcode |
| 878 | self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)]) |
| 879 | second = data[len(U_BOOT_NODTB_DATA):] |
| 880 | |
| 881 | fdt_len = self.GetFdtLen(second) |
| 882 | self.assertEqual(dtb, second[:fdt_len]) |
| 883 | |
| 884 | used_len = len(U_BOOT_NODTB_DATA) + fdt_len |
| 885 | third = data[used_len:] |
| 886 | self.assertEqual(chr(0) * (0x200 - used_len), third) |
| 887 | |
| 888 | def testUnknownPosSize(self): |
| 889 | """Test that microcode must be placed within the image""" |
| 890 | with self.assertRaises(ValueError) as e: |
| 891 | self._DoReadFile('41_unknown_pos_size.dts', True) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 892 | self.assertIn("Section '/binman': Unable to set offset/size for unknown " |
Simon Glass | 75db086 | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 893 | "entry 'invalid-entry'", str(e.exception)) |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 894 | |
| 895 | def testPackFsp(self): |
| 896 | """Test that an image with a FSP binary can be created""" |
| 897 | data = self._DoReadFile('42_intel-fsp.dts') |
| 898 | self.assertEqual(FSP_DATA, data[:len(FSP_DATA)]) |
| 899 | |
| 900 | def testPackCmc(self): |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 901 | """Test that an image with a CMC binary can be created""" |
Simon Glass | da22909 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 902 | data = self._DoReadFile('43_intel-cmc.dts') |
| 903 | self.assertEqual(CMC_DATA, data[:len(CMC_DATA)]) |
Bin Meng | 59ea8c2 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 904 | |
| 905 | def testPackVbt(self): |
| 906 | """Test that an image with a VBT binary can be created""" |
| 907 | data = self._DoReadFile('46_intel-vbt.dts') |
| 908 | self.assertEqual(VBT_DATA, data[:len(VBT_DATA)]) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 909 | |
Simon Glass | 5650984 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 910 | def testSplBssPad(self): |
| 911 | """Test that we can pad SPL's BSS with zeros""" |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 912 | # ELF file with a '__bss_size' symbol |
| 913 | with open(self.TestFile('bss_data')) as fd: |
| 914 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
Simon Glass | 5650984 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 915 | data = self._DoReadFile('47_spl_bss_pad.dts') |
| 916 | self.assertEqual(U_BOOT_SPL_DATA + (chr(0) * 10) + U_BOOT_DATA, data) |
| 917 | |
Simon Glass | b50e561 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 918 | with open(self.TestFile('u_boot_ucode_ptr')) as fd: |
| 919 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
| 920 | with self.assertRaises(ValueError) as e: |
| 921 | data = self._DoReadFile('47_spl_bss_pad.dts') |
| 922 | self.assertIn('Expected __bss_size symbol in spl/u-boot-spl', |
| 923 | str(e.exception)) |
| 924 | |
Simon Glass | 8772213 | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 925 | def testPackStart16Spl(self): |
| 926 | """Test that an image with an x86 start16 region can be created""" |
| 927 | data = self._DoReadFile('48_x86-start16-spl.dts') |
| 928 | self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)]) |
| 929 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 930 | def _PackUbootSplMicrocode(self, dts, ucode_second=False): |
| 931 | """Helper function for microcode tests |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 932 | |
| 933 | We expect to see the following in the image, in order: |
| 934 | u-boot-spl-nodtb.bin with a microcode pointer inserted at the |
| 935 | correct place |
| 936 | u-boot.dtb with the microcode removed |
| 937 | the microcode |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 938 | |
| 939 | Args: |
| 940 | dts: Device tree file to use for test |
| 941 | ucode_second: True if the microsecond entry is second instead of |
| 942 | third |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 943 | """ |
| 944 | # ELF file with a '_dt_ucode_base_size' symbol |
| 945 | with open(self.TestFile('u_boot_ucode_ptr')) as fd: |
| 946 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 947 | first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, |
| 948 | ucode_second=ucode_second) |
Simon Glass | 6b187df | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 949 | self.assertEqual('splnodtb with microc' + pos_and_size + |
| 950 | 'ter somewhere in here', first) |
| 951 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 952 | def testPackUbootSplMicrocode(self): |
| 953 | """Test that x86 microcode can be handled correctly in SPL""" |
| 954 | self._PackUbootSplMicrocode('49_x86_ucode_spl.dts') |
| 955 | |
| 956 | def testPackUbootSplMicrocodeReorder(self): |
| 957 | """Test that order doesn't matter for microcode entries |
| 958 | |
| 959 | This is the same as testPackUbootSplMicrocode but when we process the |
| 960 | u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode |
| 961 | entry, so we reply on binman to try later. |
| 962 | """ |
| 963 | self._PackUbootSplMicrocode('58_x86_ucode_spl_needs_retry.dts', |
| 964 | ucode_second=True) |
| 965 | |
Simon Glass | ca4f4ff | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 966 | def testPackMrc(self): |
| 967 | """Test that an image with an MRC binary can be created""" |
| 968 | data = self._DoReadFile('50_intel_mrc.dts') |
| 969 | self.assertEqual(MRC_DATA, data[:len(MRC_DATA)]) |
| 970 | |
Simon Glass | 47419ea | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 971 | def testSplDtb(self): |
| 972 | """Test that an image with spl/u-boot-spl.dtb can be created""" |
| 973 | data = self._DoReadFile('51_u_boot_spl_dtb.dts') |
| 974 | self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)]) |
| 975 | |
Simon Glass | 4e6fdbe | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 976 | def testSplNoDtb(self): |
| 977 | """Test that an image with spl/u-boot-spl-nodtb.bin can be created""" |
| 978 | data = self._DoReadFile('52_u_boot_spl_nodtb.dts') |
| 979 | self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)]) |
| 980 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 981 | def testSymbols(self): |
| 982 | """Test binman can assign symbols embedded in U-Boot""" |
| 983 | elf_fname = self.TestFile('u_boot_binman_syms') |
| 984 | syms = elf.GetSymbols(elf_fname, ['binman', 'image']) |
| 985 | addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start') |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 986 | self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 987 | |
| 988 | with open(self.TestFile('u_boot_binman_syms')) as fd: |
| 989 | TestFunctional._MakeInputFile('spl/u-boot-spl', fd.read()) |
| 990 | data = self._DoReadFile('53_symbols.dts') |
| 991 | sym_values = struct.pack('<LQL', 0x24 + 0, 0x24 + 24, 0x24 + 20) |
| 992 | expected = (sym_values + U_BOOT_SPL_DATA[16:] + chr(0xff) + |
| 993 | U_BOOT_DATA + |
| 994 | sym_values + U_BOOT_SPL_DATA[16:]) |
| 995 | self.assertEqual(expected, data) |
| 996 | |
Simon Glass | dd57c13 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 997 | def testPackUnitAddress(self): |
| 998 | """Test that we support multiple binaries with the same name""" |
| 999 | data = self._DoReadFile('54_unit_address.dts') |
| 1000 | self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data) |
| 1001 | |
Simon Glass | 1854695 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1002 | def testSections(self): |
| 1003 | """Basic test of sections""" |
| 1004 | data = self._DoReadFile('55_sections.dts') |
| 1005 | expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 + '&' * 8 |
| 1006 | self.assertEqual(expected, data) |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1007 | |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1008 | def testMap(self): |
| 1009 | """Tests outputting a map of the images""" |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1010 | _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1011 | self.assertEqual(''' Offset Size Name |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1012 | 00000000 00000010 section@0 |
| 1013 | 00000000 00000004 u-boot |
| 1014 | 00000010 00000010 section@1 |
| 1015 | 00000000 00000004 u-boot |
| 1016 | ''', map_data) |
| 1017 | |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1018 | def testNamePrefix(self): |
| 1019 | """Tests that name prefixes are used""" |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1020 | _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1021 | self.assertEqual(''' Offset Size Name |
Simon Glass | c8d48ef | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1022 | 00000000 00000010 section@0 |
| 1023 | 00000000 00000004 ro-u-boot |
| 1024 | 00000010 00000010 section@1 |
| 1025 | 00000000 00000004 rw-u-boot |
| 1026 | ''', map_data) |
| 1027 | |
Simon Glass | 736bb0a | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1028 | def testUnknownContents(self): |
| 1029 | """Test that obtaining the contents works as expected""" |
| 1030 | with self.assertRaises(ValueError) as e: |
| 1031 | self._DoReadFile('57_unknown_contents.dts', True) |
| 1032 | self.assertIn("Section '/binman': Internal error: Could not complete " |
| 1033 | "processing of contents: remaining [<_testing.Entry__testing ", |
| 1034 | str(e.exception)) |
| 1035 | |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1036 | def testBadChangeSize(self): |
| 1037 | """Test that trying to change the size of an entry fails""" |
| 1038 | with self.assertRaises(ValueError) as e: |
| 1039 | self._DoReadFile('59_change_size.dts', True) |
| 1040 | self.assertIn("Node '/binman/_testing': Cannot update entry size from " |
| 1041 | '2 to 1', str(e.exception)) |
| 1042 | |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1043 | def testUpdateFdt(self): |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1044 | """Test that we can update the device tree with offset/size info""" |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1045 | _, _, _, out_dtb_fname = self._DoReadFileDtb('60_fdt_update.dts', |
| 1046 | update_dtb=True) |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1047 | props = self._GetPropTree(out_dtb_fname, ['offset', 'size']) |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1048 | with open('/tmp/x.dtb', 'wb') as outf: |
| 1049 | with open(out_dtb_fname) as inf: |
| 1050 | outf.write(inf.read()) |
| 1051 | self.assertEqual({ |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1052 | '_testing:offset': 32, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1053 | '_testing:size': 1, |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1054 | 'section@0/u-boot:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1055 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1056 | 'section@0:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1057 | 'section@0:size': 16, |
| 1058 | |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1059 | 'section@1/u-boot:offset': 0, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1060 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1061 | 'section@1:offset': 16, |
Simon Glass | 16b8d6b | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1062 | 'section@1:size': 16, |
| 1063 | 'size': 40 |
| 1064 | }, props) |
| 1065 | |
| 1066 | def testUpdateFdtBad(self): |
| 1067 | """Test that we detect when ProcessFdt never completes""" |
| 1068 | with self.assertRaises(ValueError) as e: |
| 1069 | self._DoReadFileDtb('61_fdt_update_bad.dts', update_dtb=True) |
| 1070 | self.assertIn('Could not complete processing of Fdt: remaining ' |
| 1071 | '[<_testing.Entry__testing', str(e.exception)) |
Simon Glass | 5c89023 | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1072 | |
Simon Glass | 9fc60b4 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1073 | if __name__ == "__main__": |
| 1074 | unittest.main() |