Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 57454f4 | 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 | 57454f4 | 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 | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 9 | import collections |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 10 | import gzip |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 11 | import hashlib |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 12 | from optparse import OptionParser |
| 13 | import os |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 14 | import re |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 15 | import shutil |
| 16 | import struct |
| 17 | import sys |
| 18 | import tempfile |
| 19 | import unittest |
| 20 | |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 21 | from binman import cbfs_util |
| 22 | from binman import cmdline |
| 23 | from binman import control |
| 24 | from binman import elf |
| 25 | from binman import elf_test |
| 26 | from binman import fmap_util |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 27 | from binman import state |
| 28 | from dtoc import fdt |
| 29 | from dtoc import fdt_util |
| 30 | from binman.etype import fdtmap |
| 31 | from binman.etype import image_header |
Simon Glass | 90cd6f0 | 2020-08-05 13:27:47 -0600 | [diff] [blame] | 32 | from binman.image import Image |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 33 | from patman import command |
| 34 | from patman import test_util |
| 35 | from patman import tools |
| 36 | from patman import tout |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 37 | |
| 38 | # Contents of test files, corresponding to different entry types |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 39 | U_BOOT_DATA = b'1234' |
| 40 | U_BOOT_IMG_DATA = b'img' |
Simon Glass | 4e353e2 | 2019-08-24 07:23:04 -0600 | [diff] [blame] | 41 | U_BOOT_SPL_DATA = b'56780123456789abcdefghi' |
| 42 | U_BOOT_TPL_DATA = b'tpl9876543210fedcbazyw' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 43 | BLOB_DATA = b'89' |
| 44 | ME_DATA = b'0abcd' |
| 45 | VGA_DATA = b'vga' |
| 46 | U_BOOT_DTB_DATA = b'udtb' |
| 47 | U_BOOT_SPL_DTB_DATA = b'spldtb' |
| 48 | U_BOOT_TPL_DTB_DATA = b'tpldtb' |
| 49 | X86_START16_DATA = b'start16' |
| 50 | X86_START16_SPL_DATA = b'start16spl' |
| 51 | X86_START16_TPL_DATA = b'start16tpl' |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 52 | X86_RESET16_DATA = b'reset16' |
| 53 | X86_RESET16_SPL_DATA = b'reset16spl' |
| 54 | X86_RESET16_TPL_DATA = b'reset16tpl' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 55 | PPC_MPC85XX_BR_DATA = b'ppcmpc85xxbr' |
| 56 | U_BOOT_NODTB_DATA = b'nodtb with microcode pointer somewhere in here' |
| 57 | U_BOOT_SPL_NODTB_DATA = b'splnodtb with microcode pointer somewhere in here' |
| 58 | U_BOOT_TPL_NODTB_DATA = b'tplnodtb with microcode pointer somewhere in here' |
| 59 | FSP_DATA = b'fsp' |
| 60 | CMC_DATA = b'cmc' |
| 61 | VBT_DATA = b'vbt' |
| 62 | MRC_DATA = b'mrc' |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 63 | TEXT_DATA = 'text' |
| 64 | TEXT_DATA2 = 'text2' |
| 65 | TEXT_DATA3 = 'text3' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 66 | CROS_EC_RW_DATA = b'ecrw' |
| 67 | GBB_DATA = b'gbbd' |
| 68 | BMPBLK_DATA = b'bmp' |
| 69 | VBLOCK_DATA = b'vblk' |
| 70 | FILES_DATA = (b"sorry I'm late\nOh, don't bother apologising, I'm " + |
| 71 | b"sorry you're alive\n") |
Simon Glass | ccec026 | 2019-07-08 13:18:42 -0600 | [diff] [blame] | 72 | COMPRESS_DATA = b'compress xxxxxxxxxxxxxxxxxxxxxx data' |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 73 | REFCODE_DATA = b'refcode' |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 74 | FSP_M_DATA = b'fsp_m' |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 75 | FSP_S_DATA = b'fsp_s' |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 76 | FSP_T_DATA = b'fsp_t' |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 77 | |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 78 | # The expected size for the device tree in some tests |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 79 | EXTRACT_DTB_SIZE = 0x3c9 |
| 80 | |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 81 | # Properties expected to be in the device tree when update_dtb is used |
| 82 | BASE_DTB_PROPS = ['offset', 'size', 'image-pos'] |
| 83 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 84 | # Extra properties expected to be in the device tree when allow-repack is used |
| 85 | REPACK_DTB_PROPS = ['orig-offset', 'orig-size'] |
| 86 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 87 | |
| 88 | class TestFunctional(unittest.TestCase): |
| 89 | """Functional tests for binman |
| 90 | |
| 91 | Most of these use a sample .dts file to build an image and then check |
| 92 | that it looks correct. The sample files are in the test/ subdirectory |
| 93 | and are numbered. |
| 94 | |
| 95 | For each entry type a very small test file is created using fixed |
| 96 | string contents. This makes it easy to test that things look right, and |
| 97 | debug problems. |
| 98 | |
| 99 | In some cases a 'real' file must be used - these are also supplied in |
| 100 | the test/ diurectory. |
| 101 | """ |
| 102 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 103 | def setUpClass(cls): |
Simon Glass | b339326 | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 104 | global entry |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 105 | from binman import entry |
Simon Glass | b339326 | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 106 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 107 | # Handle the case where argv[0] is 'python' |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 108 | cls._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 109 | cls._binman_pathname = os.path.join(cls._binman_dir, 'binman') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 110 | |
| 111 | # Create a temporary directory for input files |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 112 | cls._indir = tempfile.mkdtemp(prefix='binmant.') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 113 | |
| 114 | # Create some test files |
| 115 | TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) |
| 116 | TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA) |
| 117 | TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 118 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 119 | TestFunctional._MakeInputFile('blobfile', BLOB_DATA) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 120 | TestFunctional._MakeInputFile('me.bin', ME_DATA) |
| 121 | TestFunctional._MakeInputFile('vga.bin', VGA_DATA) |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 122 | cls._ResetDtbs() |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 123 | |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 124 | TestFunctional._MakeInputFile('u-boot-br.bin', PPC_MPC85XX_BR_DATA) |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 125 | |
Simon Glass | abab18c | 2019-08-24 07:22:49 -0600 | [diff] [blame] | 126 | TestFunctional._MakeInputFile('u-boot-x86-start16.bin', X86_START16_DATA) |
| 127 | TestFunctional._MakeInputFile('spl/u-boot-x86-start16-spl.bin', |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 128 | X86_START16_SPL_DATA) |
Simon Glass | abab18c | 2019-08-24 07:22:49 -0600 | [diff] [blame] | 129 | TestFunctional._MakeInputFile('tpl/u-boot-x86-start16-tpl.bin', |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 130 | X86_START16_TPL_DATA) |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 131 | |
| 132 | TestFunctional._MakeInputFile('u-boot-x86-reset16.bin', |
| 133 | X86_RESET16_DATA) |
| 134 | TestFunctional._MakeInputFile('spl/u-boot-x86-reset16-spl.bin', |
| 135 | X86_RESET16_SPL_DATA) |
| 136 | TestFunctional._MakeInputFile('tpl/u-boot-x86-reset16-tpl.bin', |
| 137 | X86_RESET16_TPL_DATA) |
| 138 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 139 | TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA) |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 140 | TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin', |
| 141 | U_BOOT_SPL_NODTB_DATA) |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 142 | TestFunctional._MakeInputFile('tpl/u-boot-tpl-nodtb.bin', |
| 143 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 144 | TestFunctional._MakeInputFile('fsp.bin', FSP_DATA) |
| 145 | TestFunctional._MakeInputFile('cmc.bin', CMC_DATA) |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 146 | TestFunctional._MakeInputFile('vbt.bin', VBT_DATA) |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 147 | TestFunctional._MakeInputFile('mrc.bin', MRC_DATA) |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 148 | TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 149 | TestFunctional._MakeInputDir('devkeys') |
| 150 | TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA) |
Simon Glass | 41902e4 | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 151 | TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA) |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 152 | TestFunctional._MakeInputFile('fsp_m.bin', FSP_M_DATA) |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 153 | TestFunctional._MakeInputFile('fsp_s.bin', FSP_S_DATA) |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 154 | TestFunctional._MakeInputFile('fsp_t.bin', FSP_T_DATA) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 155 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 156 | cls._elf_testdir = os.path.join(cls._indir, 'elftest') |
| 157 | elf_test.BuildElfTestFiles(cls._elf_testdir) |
| 158 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 159 | # ELF file with a '_dt_ucode_base_size' symbol |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 160 | TestFunctional._MakeInputFile('u-boot', |
| 161 | tools.ReadFile(cls.ElfTestFile('u_boot_ucode_ptr'))) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 162 | |
| 163 | # Intel flash descriptor file |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 164 | cls._SetupDescriptor() |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 165 | |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 166 | shutil.copytree(cls.TestFile('files'), |
| 167 | os.path.join(cls._indir, 'files')) |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 168 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 169 | TestFunctional._MakeInputFile('compress', COMPRESS_DATA) |
| 170 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 171 | # Travis-CI may have an old lz4 |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 172 | cls.have_lz4 = True |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 173 | try: |
| 174 | tools.Run('lz4', '--no-frame-crc', '-c', |
Simon Glass | cc311ac | 2019-10-31 07:42:50 -0600 | [diff] [blame] | 175 | os.path.join(cls._indir, 'u-boot.bin'), binary=True) |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 176 | except: |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 177 | cls.have_lz4 = False |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 178 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 179 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 180 | def tearDownClass(cls): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 181 | """Remove the temporary input directory and its contents""" |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 182 | if cls.preserve_indir: |
| 183 | print('Preserving input dir: %s' % cls._indir) |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 184 | else: |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 185 | if cls._indir: |
| 186 | shutil.rmtree(cls._indir) |
| 187 | cls._indir = None |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 188 | |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 189 | @classmethod |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 190 | def setup_test_args(cls, preserve_indir=False, preserve_outdirs=False, |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 191 | toolpath=None, verbosity=None): |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 192 | """Accept arguments controlling test execution |
| 193 | |
| 194 | Args: |
| 195 | preserve_indir: Preserve the shared input directory used by all |
| 196 | tests in this class. |
| 197 | preserve_outdir: Preserve the output directories used by tests. Each |
| 198 | test has its own, so this is normally only useful when running a |
| 199 | single test. |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 200 | toolpath: ist of paths to use for tools |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 201 | """ |
| 202 | cls.preserve_indir = preserve_indir |
| 203 | cls.preserve_outdirs = preserve_outdirs |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 204 | cls.toolpath = toolpath |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 205 | cls.verbosity = verbosity |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 206 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 207 | def _CheckLz4(self): |
| 208 | if not self.have_lz4: |
| 209 | self.skipTest('lz4 --no-frame-crc not available') |
| 210 | |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 211 | def _CleanupOutputDir(self): |
| 212 | """Remove the temporary output directory""" |
| 213 | if self.preserve_outdirs: |
| 214 | print('Preserving output dir: %s' % tools.outdir) |
| 215 | else: |
| 216 | tools._FinaliseForTest() |
| 217 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 218 | def setUp(self): |
| 219 | # Enable this to turn on debugging output |
| 220 | # tout.Init(tout.DEBUG) |
| 221 | command.test_result = None |
| 222 | |
| 223 | def tearDown(self): |
| 224 | """Remove the temporary output directory""" |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 225 | self._CleanupOutputDir() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 226 | |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 227 | def _SetupImageInTmpdir(self): |
| 228 | """Set up the output image in a new temporary directory |
| 229 | |
| 230 | This is used when an image has been generated in the output directory, |
| 231 | but we want to run binman again. This will create a new output |
| 232 | directory and fail to delete the original one. |
| 233 | |
| 234 | This creates a new temporary directory, copies the image to it (with a |
| 235 | new name) and removes the old output directory. |
| 236 | |
| 237 | Returns: |
| 238 | Tuple: |
| 239 | Temporary directory to use |
| 240 | New image filename |
| 241 | """ |
| 242 | image_fname = tools.GetOutputFilename('image.bin') |
| 243 | tmpdir = tempfile.mkdtemp(prefix='binman.') |
| 244 | updated_fname = os.path.join(tmpdir, 'image-updated.bin') |
| 245 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
| 246 | self._CleanupOutputDir() |
| 247 | return tmpdir, updated_fname |
| 248 | |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 249 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 250 | def _ResetDtbs(cls): |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 251 | TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) |
| 252 | TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA) |
| 253 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA) |
| 254 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 255 | def _RunBinman(self, *args, **kwargs): |
| 256 | """Run binman using the command line |
| 257 | |
| 258 | Args: |
| 259 | Arguments to pass, as a list of strings |
| 260 | kwargs: Arguments to pass to Command.RunPipe() |
| 261 | """ |
| 262 | result = command.RunPipe([[self._binman_pathname] + list(args)], |
| 263 | capture=True, capture_stderr=True, raise_on_error=False) |
| 264 | if result.return_code and kwargs.get('raise_on_error', True): |
| 265 | raise Exception("Error running '%s': %s" % (' '.join(args), |
| 266 | result.stdout + result.stderr)) |
| 267 | return result |
| 268 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 269 | def _DoBinman(self, *argv): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 270 | """Run binman using directly (in the same process) |
| 271 | |
| 272 | Args: |
| 273 | Arguments to pass, as a list of strings |
| 274 | Returns: |
| 275 | Return value (0 for success) |
| 276 | """ |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 277 | argv = list(argv) |
| 278 | args = cmdline.ParseArgs(argv) |
| 279 | args.pager = 'binman-invalid-pager' |
| 280 | args.build_dir = self._indir |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 281 | |
| 282 | # For testing, you can force an increase in verbosity here |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 283 | # args.verbosity = tout.DEBUG |
| 284 | return control.Binman(args) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 285 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 286 | def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False, |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 287 | entry_args=None, images=None, use_real_dtb=False, |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 288 | verbosity=None, allow_missing=False): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 289 | """Run binman with a given test file |
| 290 | |
| 291 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 292 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 293 | debug: True to enable debugging output |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 294 | map: True to output map files for the images |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 295 | update_dtb: Update the offset and size of each entry in the device |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 296 | tree before packing it into the image |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 297 | entry_args: Dict of entry args to supply to binman |
| 298 | key: arg name |
| 299 | value: value of that arg |
| 300 | images: List of image names to build |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame^] | 301 | use_real_dtb: True to use the test file as the contents of |
| 302 | the u-boot-dtb entry. Normally this is not needed and the |
| 303 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 304 | But in some test we need the real contents. |
| 305 | verbosity: Verbosity level to use (0-3, None=don't set it) |
| 306 | allow_missing: Set the '--allow-missing' flag so that missing |
| 307 | external binaries just produce a warning instead of an error |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 308 | """ |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 309 | args = [] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 310 | if debug: |
| 311 | args.append('-D') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 312 | if verbosity is not None: |
| 313 | args.append('-v%d' % verbosity) |
| 314 | elif self.verbosity: |
| 315 | args.append('-v%d' % self.verbosity) |
| 316 | if self.toolpath: |
| 317 | for path in self.toolpath: |
| 318 | args += ['--toolpath', path] |
| 319 | args += ['build', '-p', '-I', self._indir, '-d', self.TestFile(fname)] |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 320 | if map: |
| 321 | args.append('-m') |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 322 | if update_dtb: |
Simon Glass | 38a411c | 2019-07-08 13:18:47 -0600 | [diff] [blame] | 323 | args.append('-u') |
Simon Glass | 3140201 | 2018-09-14 04:57:23 -0600 | [diff] [blame] | 324 | if not use_real_dtb: |
| 325 | args.append('--fake-dtb') |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 326 | if entry_args: |
Simon Glass | 5f3645b | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 327 | for arg, value in entry_args.items(): |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 328 | args.append('-a%s=%s' % (arg, value)) |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 329 | if allow_missing: |
| 330 | args.append('-M') |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 331 | if images: |
| 332 | for image in images: |
| 333 | args += ['-i', image] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 334 | return self._DoBinman(*args) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 335 | |
| 336 | def _SetupDtb(self, fname, outfile='u-boot.dtb'): |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 337 | """Set up a new test device-tree file |
| 338 | |
| 339 | The given file is compiled and set up as the device tree to be used |
| 340 | for ths test. |
| 341 | |
| 342 | Args: |
| 343 | fname: Filename of .dts file to read |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 344 | outfile: Output filename for compiled device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 345 | |
| 346 | Returns: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 347 | Contents of device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 348 | """ |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 349 | tmpdir = tempfile.mkdtemp(prefix='binmant.') |
| 350 | dtb = fdt_util.EnsureCompiled(self.TestFile(fname), tmpdir) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 351 | with open(dtb, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 352 | data = fd.read() |
| 353 | TestFunctional._MakeInputFile(outfile, data) |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 354 | shutil.rmtree(tmpdir) |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 355 | return data |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 356 | |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 357 | def _GetDtbContentsForSplTpl(self, dtb_data, name): |
| 358 | """Create a version of the main DTB for SPL or SPL |
| 359 | |
| 360 | For testing we don't actually have different versions of the DTB. With |
| 361 | U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests |
| 362 | we don't normally have any unwanted nodes. |
| 363 | |
| 364 | We still want the DTBs for SPL and TPL to be different though, since |
| 365 | otherwise it is confusing to know which one we are looking at. So add |
| 366 | an 'spl' or 'tpl' property to the top-level node. |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame^] | 367 | |
| 368 | Args: |
| 369 | dtb_data: dtb data to modify (this should be a value devicetree) |
| 370 | name: Name of a new property to add |
| 371 | |
| 372 | Returns: |
| 373 | New dtb data with the property added |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 374 | """ |
| 375 | dtb = fdt.Fdt.FromData(dtb_data) |
| 376 | dtb.Scan() |
| 377 | dtb.GetNode('/binman').AddZeroProp(name) |
| 378 | dtb.Sync(auto_resize=True) |
| 379 | dtb.Pack() |
| 380 | return dtb.GetContents() |
| 381 | |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 382 | def _DoReadFileDtb(self, fname, use_real_dtb=False, map=False, |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 383 | update_dtb=False, entry_args=None, reset_dtbs=True): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 384 | """Run binman and return the resulting image |
| 385 | |
| 386 | This runs binman with a given test file and then reads the resulting |
| 387 | output file. It is a shortcut function since most tests need to do |
| 388 | these steps. |
| 389 | |
| 390 | Raises an assertion failure if binman returns a non-zero exit code. |
| 391 | |
| 392 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 393 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 394 | use_real_dtb: True to use the test file as the contents of |
| 395 | the u-boot-dtb entry. Normally this is not needed and the |
| 396 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 397 | But in some test we need the real contents. |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 398 | map: True to output map files for the images |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 399 | update_dtb: Update the offset and size of each entry in the device |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 400 | tree before packing it into the image |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame^] | 401 | entry_args: Dict of entry args to supply to binman |
| 402 | key: arg name |
| 403 | value: value of that arg |
| 404 | reset_dtbs: With use_real_dtb the test dtb is overwritten by this |
| 405 | function. If reset_dtbs is True, then the original test dtb |
| 406 | is written back before this function finishes |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 407 | |
| 408 | Returns: |
| 409 | Tuple: |
| 410 | Resulting image contents |
| 411 | Device tree contents |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 412 | Map data showing contents of image (or None if none) |
Simon Glass | def77b5 | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 413 | Output device tree binary filename ('u-boot.dtb' path) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 414 | """ |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 415 | dtb_data = None |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 416 | # Use the compiled test file as the u-boot-dtb input |
| 417 | if use_real_dtb: |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 418 | dtb_data = self._SetupDtb(fname) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 419 | |
| 420 | # For testing purposes, make a copy of the DT for SPL and TPL. Add |
| 421 | # a node indicating which it is, so aid verification. |
| 422 | for name in ['spl', 'tpl']: |
| 423 | dtb_fname = '%s/u-boot-%s.dtb' % (name, name) |
| 424 | outfile = os.path.join(self._indir, dtb_fname) |
| 425 | TestFunctional._MakeInputFile(dtb_fname, |
| 426 | self._GetDtbContentsForSplTpl(dtb_data, name)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 427 | |
| 428 | try: |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 429 | retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb, |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 430 | entry_args=entry_args, use_real_dtb=use_real_dtb) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 431 | self.assertEqual(0, retcode) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 432 | out_dtb_fname = tools.GetOutputFilename('u-boot.dtb.out') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 433 | |
| 434 | # Find the (only) image, read it and return its contents |
| 435 | image = control.images['image'] |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 436 | image_fname = tools.GetOutputFilename('image.bin') |
| 437 | self.assertTrue(os.path.exists(image_fname)) |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 438 | if map: |
| 439 | map_fname = tools.GetOutputFilename('image.map') |
| 440 | with open(map_fname) as fd: |
| 441 | map_data = fd.read() |
| 442 | else: |
| 443 | map_data = None |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 444 | with open(image_fname, 'rb') as fd: |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 445 | return fd.read(), dtb_data, map_data, out_dtb_fname |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 446 | finally: |
| 447 | # Put the test file back |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 448 | if reset_dtbs and use_real_dtb: |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 449 | self._ResetDtbs() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 450 | |
Simon Glass | 5b4bce3 | 2019-07-08 14:25:26 -0600 | [diff] [blame] | 451 | def _DoReadFileRealDtb(self, fname): |
| 452 | """Run binman with a real .dtb file and return the resulting data |
| 453 | |
| 454 | Args: |
| 455 | fname: DT source filename to use (e.g. 082_fdt_update_all.dts) |
| 456 | |
| 457 | Returns: |
| 458 | Resulting image contents |
| 459 | """ |
| 460 | return self._DoReadFileDtb(fname, use_real_dtb=True, update_dtb=True)[0] |
| 461 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 462 | def _DoReadFile(self, fname, use_real_dtb=False): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 463 | """Helper function which discards the device-tree binary |
| 464 | |
| 465 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 466 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 467 | use_real_dtb: True to use the test file as the contents of |
| 468 | the u-boot-dtb entry. Normally this is not needed and the |
| 469 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 470 | But in some test we need the real contents. |
Simon Glass | def77b5 | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 471 | |
| 472 | Returns: |
| 473 | Resulting image contents |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 474 | """ |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 475 | return self._DoReadFileDtb(fname, use_real_dtb)[0] |
| 476 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 477 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 478 | def _MakeInputFile(cls, fname, contents): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 479 | """Create a new test input file, creating directories as needed |
| 480 | |
| 481 | Args: |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 482 | fname: Filename to create |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 483 | contents: File contents to write in to the file |
| 484 | Returns: |
| 485 | Full pathname of file created |
| 486 | """ |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 487 | pathname = os.path.join(cls._indir, fname) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 488 | dirname = os.path.dirname(pathname) |
| 489 | if dirname and not os.path.exists(dirname): |
| 490 | os.makedirs(dirname) |
| 491 | with open(pathname, 'wb') as fd: |
| 492 | fd.write(contents) |
| 493 | return pathname |
| 494 | |
| 495 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 496 | def _MakeInputDir(cls, dirname): |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 497 | """Create a new test input directory, creating directories as needed |
| 498 | |
| 499 | Args: |
| 500 | dirname: Directory name to create |
| 501 | |
| 502 | Returns: |
| 503 | Full pathname of directory created |
| 504 | """ |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 505 | pathname = os.path.join(cls._indir, dirname) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 506 | if not os.path.exists(pathname): |
| 507 | os.makedirs(pathname) |
| 508 | return pathname |
| 509 | |
| 510 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 511 | def _SetupSplElf(cls, src_fname='bss_data'): |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 512 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 513 | |
| 514 | Args: |
| 515 | Filename of ELF file to use as SPL |
| 516 | """ |
Simon Glass | 93a806f | 2019-08-24 07:22:59 -0600 | [diff] [blame] | 517 | TestFunctional._MakeInputFile('spl/u-boot-spl', |
| 518 | tools.ReadFile(cls.ElfTestFile(src_fname))) |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 519 | |
| 520 | @classmethod |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 521 | def _SetupTplElf(cls, src_fname='bss_data'): |
| 522 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 523 | |
| 524 | Args: |
| 525 | Filename of ELF file to use as TPL |
| 526 | """ |
| 527 | TestFunctional._MakeInputFile('tpl/u-boot-tpl', |
| 528 | tools.ReadFile(cls.ElfTestFile(src_fname))) |
| 529 | |
| 530 | @classmethod |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 531 | def _SetupDescriptor(cls): |
| 532 | with open(cls.TestFile('descriptor.bin'), 'rb') as fd: |
| 533 | TestFunctional._MakeInputFile('descriptor.bin', fd.read()) |
| 534 | |
| 535 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 536 | def TestFile(cls, fname): |
| 537 | return os.path.join(cls._binman_dir, 'test', fname) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 538 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 539 | @classmethod |
| 540 | def ElfTestFile(cls, fname): |
| 541 | return os.path.join(cls._elf_testdir, fname) |
| 542 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 543 | def AssertInList(self, grep_list, target): |
| 544 | """Assert that at least one of a list of things is in a target |
| 545 | |
| 546 | Args: |
| 547 | grep_list: List of strings to check |
| 548 | target: Target string |
| 549 | """ |
| 550 | for grep in grep_list: |
| 551 | if grep in target: |
| 552 | return |
Simon Glass | 848cdb5 | 2019-05-17 22:00:50 -0600 | [diff] [blame] | 553 | self.fail("Error: '%s' not found in '%s'" % (grep_list, target)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 554 | |
| 555 | def CheckNoGaps(self, entries): |
| 556 | """Check that all entries fit together without gaps |
| 557 | |
| 558 | Args: |
| 559 | entries: List of entries to check |
| 560 | """ |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 561 | offset = 0 |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 562 | for entry in entries.values(): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 563 | self.assertEqual(offset, entry.offset) |
| 564 | offset += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 565 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 566 | def GetFdtLen(self, dtb): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 567 | """Get the totalsize field from a device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 568 | |
| 569 | Args: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 570 | dtb: Device-tree binary contents |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 571 | |
| 572 | Returns: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 573 | Total size of device-tree binary, from the header |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 574 | """ |
| 575 | return struct.unpack('>L', dtb[4:8])[0] |
| 576 | |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 577 | def _GetPropTree(self, dtb, prop_names, prefix='/binman/'): |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 578 | def AddNode(node, path): |
| 579 | if node.name != '/': |
| 580 | path += '/' + node.name |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 581 | for prop in node.props.values(): |
| 582 | if prop.name in prop_names: |
| 583 | prop_path = path + ':' + prop.name |
| 584 | tree[prop_path[len(prefix):]] = fdt_util.fdt32_to_cpu( |
| 585 | prop.value) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 586 | for subnode in node.subnodes: |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 587 | AddNode(subnode, path) |
| 588 | |
| 589 | tree = {} |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 590 | AddNode(dtb.GetRoot(), '') |
| 591 | return tree |
| 592 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 593 | def testRun(self): |
| 594 | """Test a basic run with valid args""" |
| 595 | result = self._RunBinman('-h') |
| 596 | |
| 597 | def testFullHelp(self): |
| 598 | """Test that the full help is displayed with -H""" |
| 599 | result = self._RunBinman('-H') |
| 600 | help_file = os.path.join(self._binman_dir, 'README') |
Tom Rini | c3c0b6d | 2018-01-16 15:29:50 -0500 | [diff] [blame] | 601 | # Remove possible extraneous strings |
| 602 | extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n' |
| 603 | gothelp = result.stdout.replace(extra, '') |
| 604 | self.assertEqual(len(gothelp), os.path.getsize(help_file)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 605 | self.assertEqual(0, len(result.stderr)) |
| 606 | self.assertEqual(0, result.return_code) |
| 607 | |
| 608 | def testFullHelpInternal(self): |
| 609 | """Test that the full help is displayed with -H""" |
| 610 | try: |
| 611 | command.test_result = command.CommandResult() |
| 612 | result = self._DoBinman('-H') |
| 613 | help_file = os.path.join(self._binman_dir, 'README') |
| 614 | finally: |
| 615 | command.test_result = None |
| 616 | |
| 617 | def testHelp(self): |
| 618 | """Test that the basic help is displayed with -h""" |
| 619 | result = self._RunBinman('-h') |
| 620 | self.assertTrue(len(result.stdout) > 200) |
| 621 | self.assertEqual(0, len(result.stderr)) |
| 622 | self.assertEqual(0, result.return_code) |
| 623 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 624 | def testBoard(self): |
| 625 | """Test that we can run it with a specific board""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 626 | self._SetupDtb('005_simple.dts', 'sandbox/u-boot.dtb') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 627 | TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA) |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 628 | result = self._DoBinman('build', '-b', 'sandbox') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 629 | self.assertEqual(0, result) |
| 630 | |
| 631 | def testNeedBoard(self): |
| 632 | """Test that we get an error when no board ius supplied""" |
| 633 | with self.assertRaises(ValueError) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 634 | result = self._DoBinman('build') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 635 | self.assertIn("Must provide a board to process (use -b <board>)", |
| 636 | str(e.exception)) |
| 637 | |
| 638 | def testMissingDt(self): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 639 | """Test that an invalid device-tree file generates an error""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 640 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 641 | self._RunBinman('build', '-d', 'missing_file') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 642 | # We get one error from libfdt, and a different one from fdtget. |
| 643 | self.AssertInList(["Couldn't open blob from 'missing_file'", |
| 644 | 'No such file or directory'], str(e.exception)) |
| 645 | |
| 646 | def testBrokenDt(self): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 647 | """Test that an invalid device-tree source file generates an error |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 648 | |
| 649 | Since this is a source file it should be compiled and the error |
| 650 | will come from the device-tree compiler (dtc). |
| 651 | """ |
| 652 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 653 | self._RunBinman('build', '-d', self.TestFile('001_invalid.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 654 | self.assertIn("FATAL ERROR: Unable to parse input tree", |
| 655 | str(e.exception)) |
| 656 | |
| 657 | def testMissingNode(self): |
| 658 | """Test that a device tree without a 'binman' node generates an error""" |
| 659 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 660 | self._DoBinman('build', '-d', self.TestFile('002_missing_node.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 661 | self.assertIn("does not have a 'binman' node", str(e.exception)) |
| 662 | |
| 663 | def testEmpty(self): |
| 664 | """Test that an empty binman node works OK (i.e. does nothing)""" |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 665 | result = self._RunBinman('build', '-d', self.TestFile('003_empty.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 666 | self.assertEqual(0, len(result.stderr)) |
| 667 | self.assertEqual(0, result.return_code) |
| 668 | |
| 669 | def testInvalidEntry(self): |
| 670 | """Test that an invalid entry is flagged""" |
| 671 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 672 | result = self._RunBinman('build', '-d', |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 673 | self.TestFile('004_invalid_entry.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 674 | self.assertIn("Unknown entry type 'not-a-valid-type' in node " |
| 675 | "'/binman/not-a-valid-type'", str(e.exception)) |
| 676 | |
| 677 | def testSimple(self): |
| 678 | """Test a simple binman with a single file""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 679 | data = self._DoReadFile('005_simple.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 680 | self.assertEqual(U_BOOT_DATA, data) |
| 681 | |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 682 | def testSimpleDebug(self): |
| 683 | """Test a simple binman run with debugging enabled""" |
Simon Glass | 52d0621 | 2019-07-08 14:25:53 -0600 | [diff] [blame] | 684 | self._DoTestFile('005_simple.dts', debug=True) |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 685 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 686 | def testDual(self): |
| 687 | """Test that we can handle creating two images |
| 688 | |
| 689 | This also tests image padding. |
| 690 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 691 | retcode = self._DoTestFile('006_dual_image.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 692 | self.assertEqual(0, retcode) |
| 693 | |
| 694 | image = control.images['image1'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 695 | self.assertEqual(len(U_BOOT_DATA), image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 696 | fname = tools.GetOutputFilename('image1.bin') |
| 697 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 698 | with open(fname, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 699 | data = fd.read() |
| 700 | self.assertEqual(U_BOOT_DATA, data) |
| 701 | |
| 702 | image = control.images['image2'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 703 | self.assertEqual(3 + len(U_BOOT_DATA) + 5, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 704 | fname = tools.GetOutputFilename('image2.bin') |
| 705 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 706 | with open(fname, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 707 | data = fd.read() |
| 708 | self.assertEqual(U_BOOT_DATA, data[3:7]) |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 709 | self.assertEqual(tools.GetBytes(0, 3), data[:3]) |
| 710 | self.assertEqual(tools.GetBytes(0, 5), data[7:]) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 711 | |
| 712 | def testBadAlign(self): |
| 713 | """Test that an invalid alignment value is detected""" |
| 714 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 715 | self._DoTestFile('007_bad_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 716 | self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power " |
| 717 | "of two", str(e.exception)) |
| 718 | |
| 719 | def testPackSimple(self): |
| 720 | """Test that packing works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 721 | retcode = self._DoTestFile('008_pack.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 722 | self.assertEqual(0, retcode) |
| 723 | self.assertIn('image', control.images) |
| 724 | image = control.images['image'] |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 725 | entries = image.GetEntries() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 726 | self.assertEqual(5, len(entries)) |
| 727 | |
| 728 | # First u-boot |
| 729 | self.assertIn('u-boot', entries) |
| 730 | entry = entries['u-boot'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 731 | self.assertEqual(0, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 732 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 733 | |
| 734 | # Second u-boot, aligned to 16-byte boundary |
| 735 | self.assertIn('u-boot-align', entries) |
| 736 | entry = entries['u-boot-align'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 737 | self.assertEqual(16, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 738 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 739 | |
| 740 | # Third u-boot, size 23 bytes |
| 741 | self.assertIn('u-boot-size', entries) |
| 742 | entry = entries['u-boot-size'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 743 | self.assertEqual(20, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 744 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 745 | self.assertEqual(23, entry.size) |
| 746 | |
| 747 | # Fourth u-boot, placed immediate after the above |
| 748 | self.assertIn('u-boot-next', entries) |
| 749 | entry = entries['u-boot-next'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 750 | self.assertEqual(43, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 751 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 752 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 753 | # Fifth u-boot, placed at a fixed offset |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 754 | self.assertIn('u-boot-fixed', entries) |
| 755 | entry = entries['u-boot-fixed'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 756 | self.assertEqual(61, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 757 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 758 | |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 759 | self.assertEqual(65, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 760 | |
| 761 | def testPackExtra(self): |
| 762 | """Test that extra packing feature works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 763 | retcode = self._DoTestFile('009_pack_extra.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 764 | |
| 765 | self.assertEqual(0, retcode) |
| 766 | self.assertIn('image', control.images) |
| 767 | image = control.images['image'] |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 768 | entries = image.GetEntries() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 769 | self.assertEqual(5, len(entries)) |
| 770 | |
| 771 | # First u-boot with padding before and after |
| 772 | self.assertIn('u-boot', entries) |
| 773 | entry = entries['u-boot'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 774 | self.assertEqual(0, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 775 | self.assertEqual(3, entry.pad_before) |
| 776 | self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size) |
| 777 | |
| 778 | # Second u-boot has an aligned size, but it has no effect |
| 779 | self.assertIn('u-boot-align-size-nop', entries) |
| 780 | entry = entries['u-boot-align-size-nop'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 781 | self.assertEqual(12, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 782 | self.assertEqual(4, entry.size) |
| 783 | |
| 784 | # Third u-boot has an aligned size too |
| 785 | self.assertIn('u-boot-align-size', entries) |
| 786 | entry = entries['u-boot-align-size'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 787 | self.assertEqual(16, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 788 | self.assertEqual(32, entry.size) |
| 789 | |
| 790 | # Fourth u-boot has an aligned end |
| 791 | self.assertIn('u-boot-align-end', entries) |
| 792 | entry = entries['u-boot-align-end'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 793 | self.assertEqual(48, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 794 | self.assertEqual(16, entry.size) |
| 795 | |
| 796 | # Fifth u-boot immediately afterwards |
| 797 | self.assertIn('u-boot-align-both', entries) |
| 798 | entry = entries['u-boot-align-both'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 799 | self.assertEqual(64, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 800 | self.assertEqual(64, entry.size) |
| 801 | |
| 802 | self.CheckNoGaps(entries) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 803 | self.assertEqual(128, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 804 | |
| 805 | def testPackAlignPowerOf2(self): |
| 806 | """Test that invalid entry alignment is detected""" |
| 807 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 808 | self._DoTestFile('010_pack_align_power2.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 809 | self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power " |
| 810 | "of two", str(e.exception)) |
| 811 | |
| 812 | def testPackAlignSizePowerOf2(self): |
| 813 | """Test that invalid entry size alignment is detected""" |
| 814 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 815 | self._DoTestFile('011_pack_align_size_power2.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 816 | self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a " |
| 817 | "power of two", str(e.exception)) |
| 818 | |
| 819 | def testPackInvalidAlign(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 820 | """Test detection of an offset that does not match its alignment""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 821 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 822 | self._DoTestFile('012_pack_inv_align.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 823 | self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 824 | "align 0x4 (4)", str(e.exception)) |
| 825 | |
| 826 | def testPackInvalidSizeAlign(self): |
| 827 | """Test that invalid entry size alignment is detected""" |
| 828 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 829 | self._DoTestFile('013_pack_inv_size_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 830 | self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match " |
| 831 | "align-size 0x4 (4)", str(e.exception)) |
| 832 | |
| 833 | def testPackOverlap(self): |
| 834 | """Test that overlapping regions are detected""" |
| 835 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 836 | self._DoTestFile('014_pack_overlap.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 837 | self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 838 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 839 | str(e.exception)) |
| 840 | |
| 841 | def testPackEntryOverflow(self): |
| 842 | """Test that entries that overflow their size are detected""" |
| 843 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 844 | self._DoTestFile('015_pack_overflow.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 845 | self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) " |
| 846 | "but entry size is 0x3 (3)", str(e.exception)) |
| 847 | |
| 848 | def testPackImageOverflow(self): |
| 849 | """Test that entries which overflow the image size are detected""" |
| 850 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 851 | self._DoTestFile('016_pack_image_overflow.dts') |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 852 | self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 853 | "size 0x3 (3)", str(e.exception)) |
| 854 | |
| 855 | def testPackImageSize(self): |
| 856 | """Test that the image size can be set""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 857 | retcode = self._DoTestFile('017_pack_image_size.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 858 | self.assertEqual(0, retcode) |
| 859 | self.assertIn('image', control.images) |
| 860 | image = control.images['image'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 861 | self.assertEqual(7, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 862 | |
| 863 | def testPackImageSizeAlign(self): |
| 864 | """Test that image size alignemnt works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 865 | retcode = self._DoTestFile('018_pack_image_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 866 | self.assertEqual(0, retcode) |
| 867 | self.assertIn('image', control.images) |
| 868 | image = control.images['image'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 869 | self.assertEqual(16, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 870 | |
| 871 | def testPackInvalidImageAlign(self): |
| 872 | """Test that invalid image alignment is detected""" |
| 873 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 874 | self._DoTestFile('019_pack_inv_image_align.dts') |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 875 | self.assertIn("Section '/binman': Size 0x7 (7) does not match " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 876 | "align-size 0x8 (8)", str(e.exception)) |
| 877 | |
| 878 | def testPackAlignPowerOf2(self): |
| 879 | """Test that invalid image alignment is detected""" |
| 880 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 881 | self._DoTestFile('020_pack_inv_image_align_power2.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 882 | self.assertIn("Image '/binman': Alignment size 131 must be a power of " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 883 | "two", str(e.exception)) |
| 884 | |
| 885 | def testImagePadByte(self): |
| 886 | """Test that the image pad byte can be specified""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 887 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 888 | data = self._DoReadFile('021_image_pad.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 889 | self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0xff, 1) + |
| 890 | U_BOOT_DATA, data) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 891 | |
| 892 | def testImageName(self): |
| 893 | """Test that image files can be named""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 894 | retcode = self._DoTestFile('022_image_name.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 895 | self.assertEqual(0, retcode) |
| 896 | image = control.images['image1'] |
| 897 | fname = tools.GetOutputFilename('test-name') |
| 898 | self.assertTrue(os.path.exists(fname)) |
| 899 | |
| 900 | image = control.images['image2'] |
| 901 | fname = tools.GetOutputFilename('test-name.xx') |
| 902 | self.assertTrue(os.path.exists(fname)) |
| 903 | |
| 904 | def testBlobFilename(self): |
| 905 | """Test that generic blobs can be provided by filename""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 906 | data = self._DoReadFile('023_blob.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 907 | self.assertEqual(BLOB_DATA, data) |
| 908 | |
| 909 | def testPackSorted(self): |
| 910 | """Test that entries can be sorted""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 911 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 912 | data = self._DoReadFile('024_sorted.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 913 | self.assertEqual(tools.GetBytes(0, 1) + U_BOOT_SPL_DATA + |
| 914 | tools.GetBytes(0, 2) + U_BOOT_DATA, data) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 915 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 916 | def testPackZeroOffset(self): |
| 917 | """Test that an entry at offset 0 is not given a new offset""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 918 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 919 | self._DoTestFile('025_pack_zero_size.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 920 | self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 921 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 922 | str(e.exception)) |
| 923 | |
| 924 | def testPackUbootDtb(self): |
| 925 | """Test that a device tree can be added to U-Boot""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 926 | data = self._DoReadFile('026_pack_u_boot_dtb.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 927 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 928 | |
| 929 | def testPackX86RomNoSize(self): |
| 930 | """Test that the end-at-4gb property requires a size property""" |
| 931 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 932 | self._DoTestFile('027_pack_4gb_no_size.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 933 | self.assertIn("Image '/binman': Section size must be provided when " |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 934 | "using end-at-4gb", str(e.exception)) |
| 935 | |
Jagdish Gediya | 0fb978c | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 936 | def test4gbAndSkipAtStartTogether(self): |
| 937 | """Test that the end-at-4gb and skip-at-size property can't be used |
| 938 | together""" |
| 939 | with self.assertRaises(ValueError) as e: |
Simon Glass | 11f2bd0 | 2019-08-24 07:23:02 -0600 | [diff] [blame] | 940 | self._DoTestFile('098_4gb_and_skip_at_start_together.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 941 | self.assertIn("Image '/binman': Provide either 'end-at-4gb' or " |
Jagdish Gediya | 0fb978c | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 942 | "'skip-at-start'", str(e.exception)) |
| 943 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 944 | def testPackX86RomOutside(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 945 | """Test that the end-at-4gb property checks for offset boundaries""" |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 946 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 947 | self._DoTestFile('028_pack_4gb_outside.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 948 | self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) is outside " |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 949 | "the section starting at 0xffffffe0 (4294967264)", |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 950 | str(e.exception)) |
| 951 | |
| 952 | def testPackX86Rom(self): |
| 953 | """Test that a basic x86 ROM can be created""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 954 | self._SetupSplElf() |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 955 | data = self._DoReadFile('029_x86_rom.dts') |
Simon Glass | 4e353e2 | 2019-08-24 07:23:04 -0600 | [diff] [blame] | 956 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 3) + U_BOOT_SPL_DATA + |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 957 | tools.GetBytes(0, 2), data) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 958 | |
| 959 | def testPackX86RomMeNoDesc(self): |
| 960 | """Test that an invalid Intel descriptor entry is detected""" |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 961 | try: |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 962 | TestFunctional._MakeInputFile('descriptor-empty.bin', b'') |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 963 | with self.assertRaises(ValueError) as e: |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 964 | self._DoTestFile('163_x86_rom_me_empty.dts') |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 965 | self.assertIn("Node '/binman/intel-descriptor': Cannot find Intel Flash Descriptor (FD) signature", |
| 966 | str(e.exception)) |
| 967 | finally: |
| 968 | self._SetupDescriptor() |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 969 | |
| 970 | def testPackX86RomBadDesc(self): |
| 971 | """Test that the Intel requires a descriptor entry""" |
| 972 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 973 | self._DoTestFile('030_x86_rom_me_no_desc.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 974 | self.assertIn("Node '/binman/intel-me': No offset set with " |
| 975 | "offset-unset: should another entry provide this correct " |
| 976 | "offset?", str(e.exception)) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 977 | |
| 978 | def testPackX86RomMe(self): |
| 979 | """Test that an x86 ROM with an ME region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 980 | data = self._DoReadFile('031_x86_rom_me.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 981 | expected_desc = tools.ReadFile(self.TestFile('descriptor.bin')) |
| 982 | if data[:0x1000] != expected_desc: |
| 983 | self.fail('Expected descriptor binary at start of image') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 984 | self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)]) |
| 985 | |
| 986 | def testPackVga(self): |
| 987 | """Test that an image with a VGA binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 988 | data = self._DoReadFile('032_intel_vga.dts') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 989 | self.assertEqual(VGA_DATA, data[:len(VGA_DATA)]) |
| 990 | |
| 991 | def testPackStart16(self): |
| 992 | """Test that an image with an x86 start16 region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 993 | data = self._DoReadFile('033_x86_start16.dts') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 994 | self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) |
| 995 | |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 996 | def testPackPowerpcMpc85xxBootpgResetvec(self): |
| 997 | """Test that an image with powerpc-mpc85xx-bootpg-resetvec can be |
| 998 | created""" |
Simon Glass | 11f2bd0 | 2019-08-24 07:23:02 -0600 | [diff] [blame] | 999 | data = self._DoReadFile('150_powerpc_mpc85xx_bootpg_resetvec.dts') |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 1000 | self.assertEqual(PPC_MPC85XX_BR_DATA, data[:len(PPC_MPC85XX_BR_DATA)]) |
| 1001 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1002 | def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1003 | """Handle running a test for insertion of microcode |
| 1004 | |
| 1005 | Args: |
| 1006 | dts_fname: Name of test .dts file |
| 1007 | nodtb_data: Data that we expect in the first section |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1008 | ucode_second: True if the microsecond entry is second instead of |
| 1009 | third |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1010 | |
| 1011 | Returns: |
| 1012 | Tuple: |
| 1013 | Contents of first region (U-Boot or SPL) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1014 | Offset and size components of microcode pointer, as inserted |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1015 | in the above (two 4-byte words) |
| 1016 | """ |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1017 | data = self._DoReadFile(dts_fname, True) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1018 | |
| 1019 | # Now check the device tree has no microcode |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1020 | if ucode_second: |
| 1021 | ucode_content = data[len(nodtb_data):] |
| 1022 | ucode_pos = len(nodtb_data) |
| 1023 | dtb_with_ucode = ucode_content[16:] |
| 1024 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 1025 | else: |
| 1026 | dtb_with_ucode = data[len(nodtb_data):] |
| 1027 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 1028 | ucode_content = dtb_with_ucode[fdt_len:] |
| 1029 | ucode_pos = len(nodtb_data) + fdt_len |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1030 | fname = tools.GetOutputFilename('test.dtb') |
| 1031 | with open(fname, 'wb') as fd: |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1032 | fd.write(dtb_with_ucode) |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 1033 | dtb = fdt.FdtScan(fname) |
| 1034 | ucode = dtb.GetNode('/microcode') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1035 | self.assertTrue(ucode) |
| 1036 | for node in ucode.subnodes: |
| 1037 | self.assertFalse(node.props.get('data')) |
| 1038 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1039 | # Check that the microcode appears immediately after the Fdt |
| 1040 | # This matches the concatenation of the data properties in |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1041 | # the /microcode/update@xxx nodes in 34_x86_ucode.dts. |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1042 | ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, |
| 1043 | 0x78235609) |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1044 | self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1045 | |
| 1046 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1047 | # expected offset and size |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1048 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 1049 | len(ucode_data)) |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1050 | u_boot = data[:len(nodtb_data)] |
| 1051 | return u_boot, pos_and_size |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1052 | |
| 1053 | def testPackUbootMicrocode(self): |
| 1054 | """Test that x86 microcode can be handled correctly |
| 1055 | |
| 1056 | We expect to see the following in the image, in order: |
| 1057 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 1058 | place |
| 1059 | u-boot.dtb with the microcode removed |
| 1060 | the microcode |
| 1061 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1062 | first, pos_and_size = self._RunMicrocodeTest('034_x86_ucode.dts', |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1063 | U_BOOT_NODTB_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1064 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 1065 | b' somewhere in here', first) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1066 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1067 | def _RunPackUbootSingleMicrocode(self): |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1068 | """Test that x86 microcode can be handled correctly |
| 1069 | |
| 1070 | We expect to see the following in the image, in order: |
| 1071 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 1072 | place |
| 1073 | u-boot.dtb with the microcode |
| 1074 | an empty microcode region |
| 1075 | """ |
| 1076 | # We need the libfdt library to run this test since only that allows |
| 1077 | # finding the offset of a property. This is required by |
| 1078 | # Entry_u_boot_dtb_with_ucode.ObtainContents(). |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1079 | data = self._DoReadFile('035_x86_single_ucode.dts', True) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1080 | |
| 1081 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1082 | |
| 1083 | fdt_len = self.GetFdtLen(second) |
| 1084 | third = second[fdt_len:] |
| 1085 | second = second[:fdt_len] |
| 1086 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1087 | ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) |
| 1088 | self.assertIn(ucode_data, second) |
| 1089 | ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1090 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1091 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1092 | # expected offset and size |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1093 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 1094 | len(ucode_data)) |
| 1095 | first = data[:len(U_BOOT_NODTB_DATA)] |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1096 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 1097 | b' somewhere in here', first) |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1098 | |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1099 | def testPackUbootSingleMicrocode(self): |
| 1100 | """Test that x86 microcode can be handled correctly with fdt_normal. |
| 1101 | """ |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1102 | self._RunPackUbootSingleMicrocode() |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1103 | |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1104 | def testUBootImg(self): |
| 1105 | """Test that u-boot.img can be put in a file""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1106 | data = self._DoReadFile('036_u_boot_img.dts') |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1107 | self.assertEqual(U_BOOT_IMG_DATA, data) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1108 | |
| 1109 | def testNoMicrocode(self): |
| 1110 | """Test that a missing microcode region is detected""" |
| 1111 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1112 | self._DoReadFile('037_x86_no_ucode.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1113 | self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode " |
| 1114 | "node found in ", str(e.exception)) |
| 1115 | |
| 1116 | def testMicrocodeWithoutNode(self): |
| 1117 | """Test that a missing u-boot-dtb-with-ucode node is detected""" |
| 1118 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1119 | self._DoReadFile('038_x86_ucode_missing_node.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1120 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 1121 | "microcode region u-boot-dtb-with-ucode", str(e.exception)) |
| 1122 | |
| 1123 | def testMicrocodeWithoutNode2(self): |
| 1124 | """Test that a missing u-boot-ucode node is detected""" |
| 1125 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1126 | self._DoReadFile('039_x86_ucode_missing_node2.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1127 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 1128 | "microcode region u-boot-ucode", str(e.exception)) |
| 1129 | |
| 1130 | def testMicrocodeWithoutPtrInElf(self): |
| 1131 | """Test that a U-Boot binary without the microcode symbol is detected""" |
| 1132 | # ELF file without a '_dt_ucode_base_size' symbol |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1133 | try: |
Simon Glass | faaaa16 | 2019-08-24 07:22:55 -0600 | [diff] [blame] | 1134 | TestFunctional._MakeInputFile('u-boot', |
| 1135 | tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr'))) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1136 | |
| 1137 | with self.assertRaises(ValueError) as e: |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1138 | self._RunPackUbootSingleMicrocode() |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1139 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate " |
| 1140 | "_dt_ucode_base_size symbol in u-boot", str(e.exception)) |
| 1141 | |
| 1142 | finally: |
| 1143 | # Put the original file back |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 1144 | TestFunctional._MakeInputFile('u-boot', |
| 1145 | tools.ReadFile(self.ElfTestFile('u_boot_ucode_ptr'))) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1146 | |
| 1147 | def testMicrocodeNotInImage(self): |
| 1148 | """Test that microcode must be placed within the image""" |
| 1149 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1150 | self._DoReadFile('040_x86_ucode_not_in_image.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1151 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode " |
| 1152 | "pointer _dt_ucode_base_size at fffffe14 is outside the " |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 1153 | "section ranging from 00000000 to 0000002e", str(e.exception)) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1154 | |
| 1155 | def testWithoutMicrocode(self): |
| 1156 | """Test that we can cope with an image without microcode (e.g. qemu)""" |
Simon Glass | faaaa16 | 2019-08-24 07:22:55 -0600 | [diff] [blame] | 1157 | TestFunctional._MakeInputFile('u-boot', |
| 1158 | tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr'))) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1159 | data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1160 | |
| 1161 | # Now check the device tree has no microcode |
| 1162 | self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)]) |
| 1163 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1164 | |
| 1165 | fdt_len = self.GetFdtLen(second) |
| 1166 | self.assertEqual(dtb, second[:fdt_len]) |
| 1167 | |
| 1168 | used_len = len(U_BOOT_NODTB_DATA) + fdt_len |
| 1169 | third = data[used_len:] |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1170 | self.assertEqual(tools.GetBytes(0, 0x200 - used_len), third) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1171 | |
| 1172 | def testUnknownPosSize(self): |
| 1173 | """Test that microcode must be placed within the image""" |
| 1174 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1175 | self._DoReadFile('041_unknown_pos_size.dts', True) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1176 | self.assertIn("Section '/binman': Unable to set offset/size for unknown " |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1177 | "entry 'invalid-entry'", str(e.exception)) |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1178 | |
| 1179 | def testPackFsp(self): |
| 1180 | """Test that an image with a FSP binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1181 | data = self._DoReadFile('042_intel_fsp.dts') |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1182 | self.assertEqual(FSP_DATA, data[:len(FSP_DATA)]) |
| 1183 | |
| 1184 | def testPackCmc(self): |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1185 | """Test that an image with a CMC binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1186 | data = self._DoReadFile('043_intel_cmc.dts') |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1187 | self.assertEqual(CMC_DATA, data[:len(CMC_DATA)]) |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1188 | |
| 1189 | def testPackVbt(self): |
| 1190 | """Test that an image with a VBT binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1191 | data = self._DoReadFile('046_intel_vbt.dts') |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1192 | self.assertEqual(VBT_DATA, data[:len(VBT_DATA)]) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1193 | |
Simon Glass | 7f94e83 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1194 | def testSplBssPad(self): |
| 1195 | """Test that we can pad SPL's BSS with zeros""" |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1196 | # ELF file with a '__bss_size' symbol |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1197 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1198 | data = self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1199 | self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA, |
| 1200 | data) |
Simon Glass | 7f94e83 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1201 | |
Simon Glass | 04cda03 | 2018-10-01 21:12:42 -0600 | [diff] [blame] | 1202 | def testSplBssPadMissing(self): |
| 1203 | """Test that a missing symbol is detected""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1204 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1205 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1206 | self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1207 | self.assertIn('Expected __bss_size symbol in spl/u-boot-spl', |
| 1208 | str(e.exception)) |
| 1209 | |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1210 | def testPackStart16Spl(self): |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1211 | """Test that an image with an x86 start16 SPL region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1212 | data = self._DoReadFile('048_x86_start16_spl.dts') |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1213 | self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)]) |
| 1214 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1215 | def _PackUbootSplMicrocode(self, dts, ucode_second=False): |
| 1216 | """Helper function for microcode tests |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1217 | |
| 1218 | We expect to see the following in the image, in order: |
| 1219 | u-boot-spl-nodtb.bin with a microcode pointer inserted at the |
| 1220 | correct place |
| 1221 | u-boot.dtb with the microcode removed |
| 1222 | the microcode |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1223 | |
| 1224 | Args: |
| 1225 | dts: Device tree file to use for test |
| 1226 | ucode_second: True if the microsecond entry is second instead of |
| 1227 | third |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1228 | """ |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1229 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1230 | first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, |
| 1231 | ucode_second=ucode_second) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1232 | self.assertEqual(b'splnodtb with microc' + pos_and_size + |
| 1233 | b'ter somewhere in here', first) |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1234 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1235 | def testPackUbootSplMicrocode(self): |
| 1236 | """Test that x86 microcode can be handled correctly in SPL""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1237 | self._PackUbootSplMicrocode('049_x86_ucode_spl.dts') |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1238 | |
| 1239 | def testPackUbootSplMicrocodeReorder(self): |
| 1240 | """Test that order doesn't matter for microcode entries |
| 1241 | |
| 1242 | This is the same as testPackUbootSplMicrocode but when we process the |
| 1243 | u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode |
| 1244 | entry, so we reply on binman to try later. |
| 1245 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1246 | self._PackUbootSplMicrocode('058_x86_ucode_spl_needs_retry.dts', |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1247 | ucode_second=True) |
| 1248 | |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1249 | def testPackMrc(self): |
| 1250 | """Test that an image with an MRC binary can be created""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1251 | data = self._DoReadFile('050_intel_mrc.dts') |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1252 | self.assertEqual(MRC_DATA, data[:len(MRC_DATA)]) |
| 1253 | |
Simon Glass | 9aa6a6f | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1254 | def testSplDtb(self): |
| 1255 | """Test that an image with spl/u-boot-spl.dtb can be created""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1256 | data = self._DoReadFile('051_u_boot_spl_dtb.dts') |
Simon Glass | 9aa6a6f | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1257 | self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)]) |
| 1258 | |
Simon Glass | 0a6da31 | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1259 | def testSplNoDtb(self): |
| 1260 | """Test that an image with spl/u-boot-spl-nodtb.bin can be created""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1261 | data = self._DoReadFile('052_u_boot_spl_nodtb.dts') |
Simon Glass | 0a6da31 | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1262 | self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)]) |
| 1263 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1264 | def testSymbols(self): |
| 1265 | """Test binman can assign symbols embedded in U-Boot""" |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 1266 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1267 | syms = elf.GetSymbols(elf_fname, ['binman', 'image']) |
| 1268 | addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1269 | self.assertEqual(syms['_binman_u_boot_spl_prop_offset'].address, addr) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1270 | |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1271 | self._SetupSplElf('u_boot_binman_syms') |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1272 | data = self._DoReadFile('053_symbols.dts') |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 1273 | sym_values = struct.pack('<LQLL', 0x00, 0x1c, 0x28, 0x04) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 1274 | expected = (sym_values + U_BOOT_SPL_DATA[20:] + |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1275 | tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values + |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 1276 | U_BOOT_SPL_DATA[20:]) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1277 | self.assertEqual(expected, data) |
| 1278 | |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1279 | def testPackUnitAddress(self): |
| 1280 | """Test that we support multiple binaries with the same name""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1281 | data = self._DoReadFile('054_unit_address.dts') |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1282 | self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data) |
| 1283 | |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1284 | def testSections(self): |
| 1285 | """Basic test of sections""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1286 | data = self._DoReadFile('055_sections.dts') |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1287 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 1288 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12) + |
| 1289 | U_BOOT_DATA + tools.GetBytes(ord('&'), 4)) |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1290 | self.assertEqual(expected, data) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1291 | |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1292 | def testMap(self): |
| 1293 | """Tests outputting a map of the images""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1294 | _, _, map_data, _ = self._DoReadFileDtb('055_sections.dts', map=True) |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1295 | self.assertEqual('''ImagePos Offset Size Name |
| 1296 | 00000000 00000000 00000028 main-section |
| 1297 | 00000000 00000000 00000010 section@0 |
| 1298 | 00000000 00000000 00000004 u-boot |
| 1299 | 00000010 00000010 00000010 section@1 |
| 1300 | 00000010 00000000 00000004 u-boot |
| 1301 | 00000020 00000020 00000004 section@2 |
| 1302 | 00000020 00000000 00000004 u-boot |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1303 | ''', map_data) |
| 1304 | |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1305 | def testNamePrefix(self): |
| 1306 | """Tests that name prefixes are used""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1307 | _, _, map_data, _ = self._DoReadFileDtb('056_name_prefix.dts', map=True) |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1308 | self.assertEqual('''ImagePos Offset Size Name |
| 1309 | 00000000 00000000 00000028 main-section |
| 1310 | 00000000 00000000 00000010 section@0 |
| 1311 | 00000000 00000000 00000004 ro-u-boot |
| 1312 | 00000010 00000010 00000010 section@1 |
| 1313 | 00000010 00000000 00000004 rw-u-boot |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1314 | ''', map_data) |
| 1315 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1316 | def testUnknownContents(self): |
| 1317 | """Test that obtaining the contents works as expected""" |
| 1318 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1319 | self._DoReadFile('057_unknown_contents.dts', True) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1320 | self.assertIn("Image '/binman': Internal error: Could not complete " |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 1321 | "processing of contents: remaining [" |
| 1322 | "<binman.etype._testing.Entry__testing ", str(e.exception)) |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1323 | |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1324 | def testBadChangeSize(self): |
| 1325 | """Test that trying to change the size of an entry fails""" |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 1326 | try: |
| 1327 | state.SetAllowEntryExpansion(False) |
| 1328 | with self.assertRaises(ValueError) as e: |
| 1329 | self._DoReadFile('059_change_size.dts', True) |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 1330 | self.assertIn("Node '/binman/_testing': Cannot update entry size from 2 to 3", |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 1331 | str(e.exception)) |
| 1332 | finally: |
| 1333 | state.SetAllowEntryExpansion(True) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1334 | |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1335 | def testUpdateFdt(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1336 | """Test that we can update the device tree with offset/size info""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1337 | _, _, _, out_dtb_fname = self._DoReadFileDtb('060_fdt_update.dts', |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1338 | update_dtb=True) |
Simon Glass | 5463a6a | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 1339 | dtb = fdt.Fdt(out_dtb_fname) |
| 1340 | dtb.Scan() |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 1341 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1342 | self.assertEqual({ |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1343 | 'image-pos': 0, |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 1344 | 'offset': 0, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1345 | '_testing:offset': 32, |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 1346 | '_testing:size': 2, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1347 | '_testing:image-pos': 32, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1348 | 'section@0/u-boot:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1349 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1350 | 'section@0/u-boot:image-pos': 0, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1351 | 'section@0:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1352 | 'section@0:size': 16, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1353 | 'section@0:image-pos': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1354 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1355 | 'section@1/u-boot:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1356 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1357 | 'section@1/u-boot:image-pos': 16, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1358 | 'section@1:offset': 16, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1359 | 'section@1:size': 16, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1360 | 'section@1:image-pos': 16, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1361 | 'size': 40 |
| 1362 | }, props) |
| 1363 | |
| 1364 | def testUpdateFdtBad(self): |
| 1365 | """Test that we detect when ProcessFdt never completes""" |
| 1366 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1367 | self._DoReadFileDtb('061_fdt_update_bad.dts', update_dtb=True) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1368 | self.assertIn('Could not complete processing of Fdt: remaining ' |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 1369 | '[<binman.etype._testing.Entry__testing', |
| 1370 | str(e.exception)) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1371 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1372 | def testEntryArgs(self): |
| 1373 | """Test passing arguments to entries from the command line""" |
| 1374 | entry_args = { |
| 1375 | 'test-str-arg': 'test1', |
| 1376 | 'test-int-arg': '456', |
| 1377 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1378 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1379 | self.assertIn('image', control.images) |
| 1380 | entry = control.images['image'].GetEntries()['_testing'] |
| 1381 | self.assertEqual('test0', entry.test_str_fdt) |
| 1382 | self.assertEqual('test1', entry.test_str_arg) |
| 1383 | self.assertEqual(123, entry.test_int_fdt) |
| 1384 | self.assertEqual(456, entry.test_int_arg) |
| 1385 | |
| 1386 | def testEntryArgsMissing(self): |
| 1387 | """Test missing arguments and properties""" |
| 1388 | entry_args = { |
| 1389 | 'test-int-arg': '456', |
| 1390 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1391 | self._DoReadFileDtb('063_entry_args_missing.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1392 | entry = control.images['image'].GetEntries()['_testing'] |
| 1393 | self.assertEqual('test0', entry.test_str_fdt) |
| 1394 | self.assertEqual(None, entry.test_str_arg) |
| 1395 | self.assertEqual(None, entry.test_int_fdt) |
| 1396 | self.assertEqual(456, entry.test_int_arg) |
| 1397 | |
| 1398 | def testEntryArgsRequired(self): |
| 1399 | """Test missing arguments and properties""" |
| 1400 | entry_args = { |
| 1401 | 'test-int-arg': '456', |
| 1402 | } |
| 1403 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1404 | self._DoReadFileDtb('064_entry_args_required.dts') |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 1405 | self.assertIn("Node '/binman/_testing': " |
| 1406 | 'Missing required properties/entry args: test-str-arg, ' |
| 1407 | 'test-int-fdt, test-int-arg', |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1408 | str(e.exception)) |
| 1409 | |
| 1410 | def testEntryArgsInvalidFormat(self): |
| 1411 | """Test that an invalid entry-argument format is detected""" |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 1412 | args = ['build', '-d', self.TestFile('064_entry_args_required.dts'), |
| 1413 | '-ano-value'] |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1414 | with self.assertRaises(ValueError) as e: |
| 1415 | self._DoBinman(*args) |
| 1416 | self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception)) |
| 1417 | |
| 1418 | def testEntryArgsInvalidInteger(self): |
| 1419 | """Test that an invalid entry-argument integer is detected""" |
| 1420 | entry_args = { |
| 1421 | 'test-int-arg': 'abc', |
| 1422 | } |
| 1423 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1424 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1425 | self.assertIn("Node '/binman/_testing': Cannot convert entry arg " |
| 1426 | "'test-int-arg' (value 'abc') to integer", |
| 1427 | str(e.exception)) |
| 1428 | |
| 1429 | def testEntryArgsInvalidDatatype(self): |
| 1430 | """Test that an invalid entry-argument datatype is detected |
| 1431 | |
| 1432 | This test could be written in entry_test.py except that it needs |
| 1433 | access to control.entry_args, which seems more than that module should |
| 1434 | be able to see. |
| 1435 | """ |
| 1436 | entry_args = { |
| 1437 | 'test-bad-datatype-arg': '12', |
| 1438 | } |
| 1439 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1440 | self._DoReadFileDtb('065_entry_args_unknown_datatype.dts', |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1441 | entry_args=entry_args) |
| 1442 | self.assertIn('GetArg() internal error: Unknown data type ', |
| 1443 | str(e.exception)) |
| 1444 | |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1445 | def testText(self): |
| 1446 | """Test for a text entry type""" |
| 1447 | entry_args = { |
| 1448 | 'test-id': TEXT_DATA, |
| 1449 | 'test-id2': TEXT_DATA2, |
| 1450 | 'test-id3': TEXT_DATA3, |
| 1451 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1452 | data, _, _, _ = self._DoReadFileDtb('066_text.dts', |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1453 | entry_args=entry_args) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1454 | expected = (tools.ToBytes(TEXT_DATA) + |
| 1455 | tools.GetBytes(0, 8 - len(TEXT_DATA)) + |
| 1456 | tools.ToBytes(TEXT_DATA2) + tools.ToBytes(TEXT_DATA3) + |
Simon Glass | 47f6a62 | 2019-07-08 13:18:40 -0600 | [diff] [blame] | 1457 | b'some text' + b'more text') |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1458 | self.assertEqual(expected, data) |
| 1459 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1460 | def testEntryDocs(self): |
| 1461 | """Test for creation of entry documentation""" |
| 1462 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 1463 | control.WriteEntryDocs(control.GetEntryModules()) |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1464 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 1465 | |
| 1466 | def testEntryDocsMissing(self): |
| 1467 | """Test handling of missing entry documentation""" |
| 1468 | with self.assertRaises(ValueError) as e: |
| 1469 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 1470 | control.WriteEntryDocs(control.GetEntryModules(), 'u_boot') |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1471 | self.assertIn('Documentation is missing for modules: u_boot', |
| 1472 | str(e.exception)) |
| 1473 | |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1474 | def testFmap(self): |
| 1475 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1476 | data = self._DoReadFile('067_fmap.dts') |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1477 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1478 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 1479 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12)) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1480 | self.assertEqual(expected, data[:32]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1481 | self.assertEqual(b'__FMAP__', fhdr.signature) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1482 | self.assertEqual(1, fhdr.ver_major) |
| 1483 | self.assertEqual(0, fhdr.ver_minor) |
| 1484 | self.assertEqual(0, fhdr.base) |
| 1485 | self.assertEqual(16 + 16 + |
| 1486 | fmap_util.FMAP_HEADER_LEN + |
| 1487 | fmap_util.FMAP_AREA_LEN * 3, fhdr.image_size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1488 | self.assertEqual(b'FMAP', fhdr.name) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1489 | self.assertEqual(3, fhdr.nareas) |
| 1490 | for fentry in fentries: |
| 1491 | self.assertEqual(0, fentry.flags) |
| 1492 | |
| 1493 | self.assertEqual(0, fentries[0].offset) |
| 1494 | self.assertEqual(4, fentries[0].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1495 | self.assertEqual(b'RO_U_BOOT', fentries[0].name) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1496 | |
| 1497 | self.assertEqual(16, fentries[1].offset) |
| 1498 | self.assertEqual(4, fentries[1].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1499 | self.assertEqual(b'RW_U_BOOT', fentries[1].name) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1500 | |
| 1501 | self.assertEqual(32, fentries[2].offset) |
| 1502 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 1503 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1504 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1505 | |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1506 | def testBlobNamedByArg(self): |
| 1507 | """Test we can add a blob with the filename coming from an entry arg""" |
| 1508 | entry_args = { |
| 1509 | 'cros-ec-rw-path': 'ecrw.bin', |
| 1510 | } |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 1511 | self._DoReadFileDtb('068_blob_named_by_arg.dts', entry_args=entry_args) |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1512 | |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1513 | def testFill(self): |
| 1514 | """Test for an fill entry type""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1515 | data = self._DoReadFile('069_fill.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1516 | expected = tools.GetBytes(0xff, 8) + tools.GetBytes(0, 8) |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1517 | self.assertEqual(expected, data) |
| 1518 | |
| 1519 | def testFillNoSize(self): |
| 1520 | """Test for an fill entry type with no size""" |
| 1521 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1522 | self._DoReadFile('070_fill_no_size.dts') |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1523 | self.assertIn("'fill' entry must have a size property", |
| 1524 | str(e.exception)) |
| 1525 | |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1526 | def _HandleGbbCommand(self, pipe_list): |
| 1527 | """Fake calls to the futility utility""" |
| 1528 | if pipe_list[0][0] == 'futility': |
| 1529 | fname = pipe_list[0][-1] |
| 1530 | # Append our GBB data to the file, which will happen every time the |
| 1531 | # futility command is called. |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1532 | with open(fname, 'ab') as fd: |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1533 | fd.write(GBB_DATA) |
| 1534 | return command.CommandResult() |
| 1535 | |
| 1536 | def testGbb(self): |
| 1537 | """Test for the Chromium OS Google Binary Block""" |
| 1538 | command.test_result = self._HandleGbbCommand |
| 1539 | entry_args = { |
| 1540 | 'keydir': 'devkeys', |
| 1541 | 'bmpblk': 'bmpblk.bin', |
| 1542 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1543 | data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1544 | |
| 1545 | # Since futility |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1546 | expected = (GBB_DATA + GBB_DATA + tools.GetBytes(0, 8) + |
| 1547 | tools.GetBytes(0, 0x2180 - 16)) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1548 | self.assertEqual(expected, data) |
| 1549 | |
| 1550 | def testGbbTooSmall(self): |
| 1551 | """Test for the Chromium OS Google Binary Block being large enough""" |
| 1552 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1553 | self._DoReadFileDtb('072_gbb_too_small.dts') |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1554 | self.assertIn("Node '/binman/gbb': GBB is too small", |
| 1555 | str(e.exception)) |
| 1556 | |
| 1557 | def testGbbNoSize(self): |
| 1558 | """Test for the Chromium OS Google Binary Block having a size""" |
| 1559 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1560 | self._DoReadFileDtb('073_gbb_no_size.dts') |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1561 | self.assertIn("Node '/binman/gbb': GBB must have a fixed size", |
| 1562 | str(e.exception)) |
| 1563 | |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1564 | def _HandleVblockCommand(self, pipe_list): |
| 1565 | """Fake calls to the futility utility""" |
| 1566 | if pipe_list[0][0] == 'futility': |
| 1567 | fname = pipe_list[0][3] |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 1568 | with open(fname, 'wb') as fd: |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1569 | fd.write(VBLOCK_DATA) |
| 1570 | return command.CommandResult() |
| 1571 | |
| 1572 | def testVblock(self): |
| 1573 | """Test for the Chromium OS Verified Boot Block""" |
| 1574 | command.test_result = self._HandleVblockCommand |
| 1575 | entry_args = { |
| 1576 | 'keydir': 'devkeys', |
| 1577 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1578 | data, _, _, _ = self._DoReadFileDtb('074_vblock.dts', |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1579 | entry_args=entry_args) |
| 1580 | expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA |
| 1581 | self.assertEqual(expected, data) |
| 1582 | |
| 1583 | def testVblockNoContent(self): |
| 1584 | """Test we detect a vblock which has no content to sign""" |
| 1585 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1586 | self._DoReadFile('075_vblock_no_content.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1587 | self.assertIn("Node '/binman/vblock': Vblock must have a 'content' " |
| 1588 | 'property', str(e.exception)) |
| 1589 | |
| 1590 | def testVblockBadPhandle(self): |
| 1591 | """Test that we detect a vblock with an invalid phandle in contents""" |
| 1592 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1593 | self._DoReadFile('076_vblock_bad_phandle.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1594 | self.assertIn("Node '/binman/vblock': Cannot find node for phandle " |
| 1595 | '1000', str(e.exception)) |
| 1596 | |
| 1597 | def testVblockBadEntry(self): |
| 1598 | """Test that we detect an entry that points to a non-entry""" |
| 1599 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1600 | self._DoReadFile('077_vblock_bad_entry.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1601 | self.assertIn("Node '/binman/vblock': Cannot find entry for node " |
| 1602 | "'other'", str(e.exception)) |
| 1603 | |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1604 | def testTpl(self): |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1605 | """Test that an image with TPL and its device tree can be created""" |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1606 | # ELF file with a '__bss_size' symbol |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1607 | self._SetupTplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1608 | data = self._DoReadFile('078_u_boot_tpl.dts') |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1609 | self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data) |
| 1610 | |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1611 | def testUsesPos(self): |
| 1612 | """Test that the 'pos' property cannot be used anymore""" |
| 1613 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1614 | data = self._DoReadFile('079_uses_pos.dts') |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1615 | self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of " |
| 1616 | "'pos'", str(e.exception)) |
| 1617 | |
Simon Glass | 274bf09 | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1618 | def testFillZero(self): |
| 1619 | """Test for an fill entry type with a size of 0""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1620 | data = self._DoReadFile('080_fill_empty.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1621 | self.assertEqual(tools.GetBytes(0, 16), data) |
Simon Glass | 274bf09 | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1622 | |
Simon Glass | 267de43 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1623 | def testTextMissing(self): |
| 1624 | """Test for a text entry type where there is no text""" |
| 1625 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1626 | self._DoReadFileDtb('066_text.dts',) |
Simon Glass | 267de43 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1627 | self.assertIn("Node '/binman/text': No value provided for text label " |
| 1628 | "'test-id'", str(e.exception)) |
| 1629 | |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1630 | def testPackStart16Tpl(self): |
| 1631 | """Test that an image with an x86 start16 TPL region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1632 | data = self._DoReadFile('081_x86_start16_tpl.dts') |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1633 | self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)]) |
| 1634 | |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1635 | def testSelectImage(self): |
| 1636 | """Test that we can select which images to build""" |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1637 | expected = 'Skipping images: image1' |
| 1638 | |
| 1639 | # We should only get the expected message in verbose mode |
Simon Glass | 8a50b4a | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 1640 | for verbosity in (0, 2): |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1641 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1642 | retcode = self._DoTestFile('006_dual_image.dts', |
| 1643 | verbosity=verbosity, |
| 1644 | images=['image2']) |
| 1645 | self.assertEqual(0, retcode) |
| 1646 | if verbosity: |
| 1647 | self.assertIn(expected, stdout.getvalue()) |
| 1648 | else: |
| 1649 | self.assertNotIn(expected, stdout.getvalue()) |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1650 | |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1651 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin'))) |
| 1652 | self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin'))) |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 1653 | self._CleanupOutputDir() |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1654 | |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1655 | def testUpdateFdtAll(self): |
| 1656 | """Test that all device trees are updated with offset/size info""" |
Simon Glass | 5b4bce3 | 2019-07-08 14:25:26 -0600 | [diff] [blame] | 1657 | data = self._DoReadFileRealDtb('082_fdt_update_all.dts') |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1658 | |
| 1659 | base_expected = { |
| 1660 | 'section:image-pos': 0, |
| 1661 | 'u-boot-tpl-dtb:size': 513, |
| 1662 | 'u-boot-spl-dtb:size': 513, |
| 1663 | 'u-boot-spl-dtb:offset': 493, |
| 1664 | 'image-pos': 0, |
| 1665 | 'section/u-boot-dtb:image-pos': 0, |
| 1666 | 'u-boot-spl-dtb:image-pos': 493, |
| 1667 | 'section/u-boot-dtb:size': 493, |
| 1668 | 'u-boot-tpl-dtb:image-pos': 1006, |
| 1669 | 'section/u-boot-dtb:offset': 0, |
| 1670 | 'section:size': 493, |
| 1671 | 'offset': 0, |
| 1672 | 'section:offset': 0, |
| 1673 | 'u-boot-tpl-dtb:offset': 1006, |
| 1674 | 'size': 1519 |
| 1675 | } |
| 1676 | |
| 1677 | # We expect three device-tree files in the output, one after the other. |
| 1678 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 1679 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 1680 | # main U-Boot tree. All three should have the same postions and offset. |
| 1681 | start = 0 |
| 1682 | for item in ['', 'spl', 'tpl']: |
| 1683 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1684 | dtb.Scan() |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 1685 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS + |
| 1686 | ['spl', 'tpl']) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1687 | expected = dict(base_expected) |
| 1688 | if item: |
| 1689 | expected[item] = 0 |
| 1690 | self.assertEqual(expected, props) |
| 1691 | start += dtb._fdt_obj.totalsize() |
| 1692 | |
| 1693 | def testUpdateFdtOutput(self): |
| 1694 | """Test that output DTB files are updated""" |
| 1695 | try: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1696 | data, dtb_data, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts', |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1697 | use_real_dtb=True, update_dtb=True, reset_dtbs=False) |
| 1698 | |
| 1699 | # Unfortunately, compiling a source file always results in a file |
| 1700 | # called source.dtb (see fdt_util.EnsureCompiled()). The test |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1701 | # source file (e.g. test/075_fdt_update_all.dts) thus does not enter |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1702 | # binman as a file called u-boot.dtb. To fix this, copy the file |
| 1703 | # over to the expected place. |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1704 | start = 0 |
| 1705 | for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out', |
| 1706 | 'tpl/u-boot-tpl.dtb.out']: |
| 1707 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1708 | size = dtb._fdt_obj.totalsize() |
| 1709 | pathname = tools.GetOutputFilename(os.path.split(fname)[1]) |
| 1710 | outdata = tools.ReadFile(pathname) |
| 1711 | name = os.path.split(fname)[0] |
| 1712 | |
| 1713 | if name: |
| 1714 | orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name) |
| 1715 | else: |
| 1716 | orig_indata = dtb_data |
| 1717 | self.assertNotEqual(outdata, orig_indata, |
| 1718 | "Expected output file '%s' be updated" % pathname) |
| 1719 | self.assertEqual(outdata, data[start:start + size], |
| 1720 | "Expected output file '%s' to match output image" % |
| 1721 | pathname) |
| 1722 | start += size |
| 1723 | finally: |
| 1724 | self._ResetDtbs() |
| 1725 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1726 | def _decompress(self, data): |
Simon Glass | ccec026 | 2019-07-08 13:18:42 -0600 | [diff] [blame] | 1727 | return tools.Decompress(data, 'lz4') |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1728 | |
| 1729 | def testCompress(self): |
| 1730 | """Test compression of blobs""" |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 1731 | self._CheckLz4() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1732 | data, _, _, out_dtb_fname = self._DoReadFileDtb('083_compress.dts', |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1733 | use_real_dtb=True, update_dtb=True) |
| 1734 | dtb = fdt.Fdt(out_dtb_fname) |
| 1735 | dtb.Scan() |
| 1736 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 1737 | orig = self._decompress(data) |
| 1738 | self.assertEquals(COMPRESS_DATA, orig) |
| 1739 | expected = { |
| 1740 | 'blob:uncomp-size': len(COMPRESS_DATA), |
| 1741 | 'blob:size': len(data), |
| 1742 | 'size': len(data), |
| 1743 | } |
| 1744 | self.assertEqual(expected, props) |
| 1745 | |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1746 | def testFiles(self): |
| 1747 | """Test bringing in multiple files""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1748 | data = self._DoReadFile('084_files.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1749 | self.assertEqual(FILES_DATA, data) |
| 1750 | |
| 1751 | def testFilesCompress(self): |
| 1752 | """Test bringing in multiple files and compressing them""" |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 1753 | self._CheckLz4() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1754 | data = self._DoReadFile('085_files_compress.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1755 | |
| 1756 | image = control.images['image'] |
| 1757 | entries = image.GetEntries() |
| 1758 | files = entries['files'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1759 | entries = files._entries |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1760 | |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1761 | orig = b'' |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1762 | for i in range(1, 3): |
| 1763 | key = '%d.dat' % i |
| 1764 | start = entries[key].image_pos |
| 1765 | len = entries[key].size |
| 1766 | chunk = data[start:start + len] |
| 1767 | orig += self._decompress(chunk) |
| 1768 | |
| 1769 | self.assertEqual(FILES_DATA, orig) |
| 1770 | |
| 1771 | def testFilesMissing(self): |
| 1772 | """Test missing files""" |
| 1773 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1774 | data = self._DoReadFile('086_files_none.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1775 | self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched " |
| 1776 | 'no files', str(e.exception)) |
| 1777 | |
| 1778 | def testFilesNoPattern(self): |
| 1779 | """Test missing files""" |
| 1780 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1781 | data = self._DoReadFile('087_files_no_pattern.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1782 | self.assertIn("Node '/binman/files': Missing 'pattern' property", |
| 1783 | str(e.exception)) |
| 1784 | |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1785 | def testExpandSize(self): |
| 1786 | """Test an expanding entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1787 | data, _, map_data, _ = self._DoReadFileDtb('088_expand_size.dts', |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1788 | map=True) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1789 | expect = (tools.GetBytes(ord('a'), 8) + U_BOOT_DATA + |
| 1790 | MRC_DATA + tools.GetBytes(ord('b'), 1) + U_BOOT_DATA + |
| 1791 | tools.GetBytes(ord('c'), 8) + U_BOOT_DATA + |
| 1792 | tools.GetBytes(ord('d'), 8)) |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1793 | self.assertEqual(expect, data) |
| 1794 | self.assertEqual('''ImagePos Offset Size Name |
| 1795 | 00000000 00000000 00000028 main-section |
| 1796 | 00000000 00000000 00000008 fill |
| 1797 | 00000008 00000008 00000004 u-boot |
| 1798 | 0000000c 0000000c 00000004 section |
| 1799 | 0000000c 00000000 00000003 intel-mrc |
| 1800 | 00000010 00000010 00000004 u-boot2 |
| 1801 | 00000014 00000014 0000000c section2 |
| 1802 | 00000014 00000000 00000008 fill |
| 1803 | 0000001c 00000008 00000004 u-boot |
| 1804 | 00000020 00000020 00000008 fill2 |
| 1805 | ''', map_data) |
| 1806 | |
| 1807 | def testExpandSizeBad(self): |
| 1808 | """Test an expanding entry which fails to provide contents""" |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1809 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1810 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1811 | self._DoReadFileDtb('089_expand_size_bad.dts', map=True) |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1812 | self.assertIn("Node '/binman/_testing': Cannot obtain contents when " |
| 1813 | 'expanding entry', str(e.exception)) |
| 1814 | |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1815 | def testHash(self): |
| 1816 | """Test hashing of the contents of an entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1817 | _, _, _, out_dtb_fname = self._DoReadFileDtb('090_hash.dts', |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1818 | use_real_dtb=True, update_dtb=True) |
| 1819 | dtb = fdt.Fdt(out_dtb_fname) |
| 1820 | dtb.Scan() |
| 1821 | hash_node = dtb.GetNode('/binman/u-boot/hash').props['value'] |
| 1822 | m = hashlib.sha256() |
| 1823 | m.update(U_BOOT_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1824 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1825 | |
| 1826 | def testHashNoAlgo(self): |
| 1827 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1828 | self._DoReadFileDtb('091_hash_no_algo.dts', update_dtb=True) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1829 | self.assertIn("Node \'/binman/u-boot\': Missing \'algo\' property for " |
| 1830 | 'hash node', str(e.exception)) |
| 1831 | |
| 1832 | def testHashBadAlgo(self): |
| 1833 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1834 | self._DoReadFileDtb('092_hash_bad_algo.dts', update_dtb=True) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1835 | self.assertIn("Node '/binman/u-boot': Unknown hash algorithm", |
| 1836 | str(e.exception)) |
| 1837 | |
| 1838 | def testHashSection(self): |
| 1839 | """Test hashing of the contents of an entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1840 | _, _, _, out_dtb_fname = self._DoReadFileDtb('099_hash_section.dts', |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1841 | use_real_dtb=True, update_dtb=True) |
| 1842 | dtb = fdt.Fdt(out_dtb_fname) |
| 1843 | dtb.Scan() |
| 1844 | hash_node = dtb.GetNode('/binman/section/hash').props['value'] |
| 1845 | m = hashlib.sha256() |
| 1846 | m.update(U_BOOT_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1847 | m.update(tools.GetBytes(ord('a'), 16)) |
| 1848 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 1849 | |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1850 | def testPackUBootTplMicrocode(self): |
| 1851 | """Test that x86 microcode can be handled correctly in TPL |
| 1852 | |
| 1853 | We expect to see the following in the image, in order: |
| 1854 | u-boot-tpl-nodtb.bin with a microcode pointer inserted at the correct |
| 1855 | place |
| 1856 | u-boot-tpl.dtb with the microcode removed |
| 1857 | the microcode |
| 1858 | """ |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1859 | self._SetupTplElf('u_boot_ucode_ptr') |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1860 | first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts', |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1861 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1862 | self.assertEqual(b'tplnodtb with microc' + pos_and_size + |
| 1863 | b'ter somewhere in here', first) |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 1864 | |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1865 | def testFmapX86(self): |
| 1866 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1867 | data = self._DoReadFile('094_fmap_x86.dts') |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1868 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1869 | expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('a'), 32 - 7) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1870 | self.assertEqual(expected, data[:32]) |
| 1871 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
| 1872 | |
| 1873 | self.assertEqual(0x100, fhdr.image_size) |
| 1874 | |
| 1875 | self.assertEqual(0, fentries[0].offset) |
| 1876 | self.assertEqual(4, fentries[0].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1877 | self.assertEqual(b'U_BOOT', fentries[0].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1878 | |
| 1879 | self.assertEqual(4, fentries[1].offset) |
| 1880 | self.assertEqual(3, fentries[1].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1881 | self.assertEqual(b'INTEL_MRC', fentries[1].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1882 | |
| 1883 | self.assertEqual(32, fentries[2].offset) |
| 1884 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 1885 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1886 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1887 | |
| 1888 | def testFmapX86Section(self): |
| 1889 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1890 | data = self._DoReadFile('095_fmap_x86_section.dts') |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1891 | expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('b'), 32 - 7) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1892 | self.assertEqual(expected, data[:32]) |
| 1893 | fhdr, fentries = fmap_util.DecodeFmap(data[36:]) |
| 1894 | |
| 1895 | self.assertEqual(0x100, fhdr.image_size) |
| 1896 | |
| 1897 | self.assertEqual(0, fentries[0].offset) |
| 1898 | self.assertEqual(4, fentries[0].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1899 | self.assertEqual(b'U_BOOT', fentries[0].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1900 | |
| 1901 | self.assertEqual(4, fentries[1].offset) |
| 1902 | self.assertEqual(3, fentries[1].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1903 | self.assertEqual(b'INTEL_MRC', fentries[1].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1904 | |
| 1905 | self.assertEqual(36, fentries[2].offset) |
| 1906 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 1907 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1908 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 1909 | |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1910 | def testElf(self): |
| 1911 | """Basic test of ELF entries""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1912 | self._SetupSplElf() |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1913 | self._SetupTplElf() |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 1914 | with open(self.ElfTestFile('bss_data'), 'rb') as fd: |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1915 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1916 | data = self._DoReadFile('096_elf.dts') |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1917 | |
Simon Glass | 0d67379 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 1918 | def testElfStrip(self): |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1919 | """Basic test of ELF entries""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1920 | self._SetupSplElf() |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 1921 | with open(self.ElfTestFile('bss_data'), 'rb') as fd: |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1922 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1923 | data = self._DoReadFile('097_elf_strip.dts') |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 1924 | |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1925 | def testPackOverlapMap(self): |
| 1926 | """Test that overlapping regions are detected""" |
| 1927 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1928 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1929 | self._DoTestFile('014_pack_overlap.dts', map=True) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1930 | map_fname = tools.GetOutputFilename('image.map') |
| 1931 | self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname, |
| 1932 | stdout.getvalue()) |
| 1933 | |
| 1934 | # We should not get an inmage, but there should be a map file |
| 1935 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin'))) |
| 1936 | self.assertTrue(os.path.exists(map_fname)) |
Simon Glass | b377475 | 2019-05-17 22:00:51 -0600 | [diff] [blame] | 1937 | map_data = tools.ReadFile(map_fname, binary=False) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 1938 | self.assertEqual('''ImagePos Offset Size Name |
| 1939 | <none> 00000000 00000007 main-section |
| 1940 | <none> 00000000 00000004 u-boot |
| 1941 | <none> 00000003 00000004 u-boot-align |
| 1942 | ''', map_data) |
| 1943 | |
Simon Glass | 0d67379 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 1944 | def testPackRefCode(self): |
Simon Glass | 41902e4 | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 1945 | """Test that an image with an Intel Reference code binary works""" |
| 1946 | data = self._DoReadFile('100_intel_refcode.dts') |
| 1947 | self.assertEqual(REFCODE_DATA, data[:len(REFCODE_DATA)]) |
| 1948 | |
Simon Glass | eb023b3 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 1949 | def testSectionOffset(self): |
| 1950 | """Tests use of a section with an offset""" |
| 1951 | data, _, map_data, _ = self._DoReadFileDtb('101_sections_offset.dts', |
| 1952 | map=True) |
| 1953 | self.assertEqual('''ImagePos Offset Size Name |
| 1954 | 00000000 00000000 00000038 main-section |
| 1955 | 00000004 00000004 00000010 section@0 |
| 1956 | 00000004 00000000 00000004 u-boot |
| 1957 | 00000018 00000018 00000010 section@1 |
| 1958 | 00000018 00000000 00000004 u-boot |
| 1959 | 0000002c 0000002c 00000004 section@2 |
| 1960 | 0000002c 00000000 00000004 u-boot |
| 1961 | ''', map_data) |
| 1962 | self.assertEqual(data, |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1963 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 1964 | tools.GetBytes(0x21, 12) + |
| 1965 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 1966 | tools.GetBytes(0x61, 12) + |
| 1967 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 1968 | tools.GetBytes(0x26, 8)) |
Simon Glass | eb023b3 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 1969 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 1970 | def testCbfsRaw(self): |
| 1971 | """Test base handling of a Coreboot Filesystem (CBFS) |
| 1972 | |
| 1973 | The exact contents of the CBFS is verified by similar tests in |
| 1974 | cbfs_util_test.py. The tests here merely check that the files added to |
| 1975 | the CBFS can be found in the final image. |
| 1976 | """ |
| 1977 | data = self._DoReadFile('102_cbfs_raw.dts') |
| 1978 | size = 0xb0 |
| 1979 | |
| 1980 | cbfs = cbfs_util.CbfsReader(data) |
| 1981 | self.assertEqual(size, cbfs.rom_size) |
| 1982 | |
| 1983 | self.assertIn('u-boot-dtb', cbfs.files) |
| 1984 | cfile = cbfs.files['u-boot-dtb'] |
| 1985 | self.assertEqual(U_BOOT_DTB_DATA, cfile.data) |
| 1986 | |
| 1987 | def testCbfsArch(self): |
| 1988 | """Test on non-x86 architecture""" |
| 1989 | data = self._DoReadFile('103_cbfs_raw_ppc.dts') |
| 1990 | size = 0x100 |
| 1991 | |
| 1992 | cbfs = cbfs_util.CbfsReader(data) |
| 1993 | self.assertEqual(size, cbfs.rom_size) |
| 1994 | |
| 1995 | self.assertIn('u-boot-dtb', cbfs.files) |
| 1996 | cfile = cbfs.files['u-boot-dtb'] |
| 1997 | self.assertEqual(U_BOOT_DTB_DATA, cfile.data) |
| 1998 | |
| 1999 | def testCbfsStage(self): |
| 2000 | """Tests handling of a Coreboot Filesystem (CBFS)""" |
| 2001 | if not elf.ELF_TOOLS: |
| 2002 | self.skipTest('Python elftools not available') |
| 2003 | elf_fname = os.path.join(self._indir, 'cbfs-stage.elf') |
| 2004 | elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA) |
| 2005 | size = 0xb0 |
| 2006 | |
| 2007 | data = self._DoReadFile('104_cbfs_stage.dts') |
| 2008 | cbfs = cbfs_util.CbfsReader(data) |
| 2009 | self.assertEqual(size, cbfs.rom_size) |
| 2010 | |
| 2011 | self.assertIn('u-boot', cbfs.files) |
| 2012 | cfile = cbfs.files['u-boot'] |
| 2013 | self.assertEqual(U_BOOT_DATA + U_BOOT_DTB_DATA, cfile.data) |
| 2014 | |
| 2015 | def testCbfsRawCompress(self): |
| 2016 | """Test handling of compressing raw files""" |
| 2017 | self._CheckLz4() |
| 2018 | data = self._DoReadFile('105_cbfs_raw_compress.dts') |
| 2019 | size = 0x140 |
| 2020 | |
| 2021 | cbfs = cbfs_util.CbfsReader(data) |
| 2022 | self.assertIn('u-boot', cbfs.files) |
| 2023 | cfile = cbfs.files['u-boot'] |
| 2024 | self.assertEqual(COMPRESS_DATA, cfile.data) |
| 2025 | |
| 2026 | def testCbfsBadArch(self): |
| 2027 | """Test handling of a bad architecture""" |
| 2028 | with self.assertRaises(ValueError) as e: |
| 2029 | self._DoReadFile('106_cbfs_bad_arch.dts') |
| 2030 | self.assertIn("Invalid architecture 'bad-arch'", str(e.exception)) |
| 2031 | |
| 2032 | def testCbfsNoSize(self): |
| 2033 | """Test handling of a missing size property""" |
| 2034 | with self.assertRaises(ValueError) as e: |
| 2035 | self._DoReadFile('107_cbfs_no_size.dts') |
| 2036 | self.assertIn('entry must have a size property', str(e.exception)) |
| 2037 | |
| 2038 | def testCbfsNoCOntents(self): |
| 2039 | """Test handling of a CBFS entry which does not provide contentsy""" |
| 2040 | with self.assertRaises(ValueError) as e: |
| 2041 | self._DoReadFile('108_cbfs_no_contents.dts') |
| 2042 | self.assertIn('Could not complete processing of contents', |
| 2043 | str(e.exception)) |
| 2044 | |
| 2045 | def testCbfsBadCompress(self): |
| 2046 | """Test handling of a bad architecture""" |
| 2047 | with self.assertRaises(ValueError) as e: |
| 2048 | self._DoReadFile('109_cbfs_bad_compress.dts') |
| 2049 | self.assertIn("Invalid compression in 'u-boot': 'invalid-algo'", |
| 2050 | str(e.exception)) |
| 2051 | |
| 2052 | def testCbfsNamedEntries(self): |
| 2053 | """Test handling of named entries""" |
| 2054 | data = self._DoReadFile('110_cbfs_name.dts') |
| 2055 | |
| 2056 | cbfs = cbfs_util.CbfsReader(data) |
| 2057 | self.assertIn('FRED', cbfs.files) |
| 2058 | cfile1 = cbfs.files['FRED'] |
| 2059 | self.assertEqual(U_BOOT_DATA, cfile1.data) |
| 2060 | |
| 2061 | self.assertIn('hello', cbfs.files) |
| 2062 | cfile2 = cbfs.files['hello'] |
| 2063 | self.assertEqual(U_BOOT_DTB_DATA, cfile2.data) |
| 2064 | |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2065 | def _SetupIfwi(self, fname): |
| 2066 | """Set up to run an IFWI test |
| 2067 | |
| 2068 | Args: |
| 2069 | fname: Filename of input file to provide (fitimage.bin or ifwi.bin) |
| 2070 | """ |
| 2071 | self._SetupSplElf() |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2072 | self._SetupTplElf() |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2073 | |
| 2074 | # Intel Integrated Firmware Image (IFWI) file |
| 2075 | with gzip.open(self.TestFile('%s.gz' % fname), 'rb') as fd: |
| 2076 | data = fd.read() |
| 2077 | TestFunctional._MakeInputFile(fname,data) |
| 2078 | |
| 2079 | def _CheckIfwi(self, data): |
| 2080 | """Check that an image with an IFWI contains the correct output |
| 2081 | |
| 2082 | Args: |
| 2083 | data: Conents of output file |
| 2084 | """ |
| 2085 | expected_desc = tools.ReadFile(self.TestFile('descriptor.bin')) |
| 2086 | if data[:0x1000] != expected_desc: |
| 2087 | self.fail('Expected descriptor binary at start of image') |
| 2088 | |
| 2089 | # We expect to find the TPL wil in subpart IBBP entry IBBL |
| 2090 | image_fname = tools.GetOutputFilename('image.bin') |
| 2091 | tpl_fname = tools.GetOutputFilename('tpl.out') |
| 2092 | tools.RunIfwiTool(image_fname, tools.CMD_EXTRACT, fname=tpl_fname, |
| 2093 | subpart='IBBP', entry_name='IBBL') |
| 2094 | |
| 2095 | tpl_data = tools.ReadFile(tpl_fname) |
Simon Glass | f55bd69 | 2019-08-24 07:22:51 -0600 | [diff] [blame] | 2096 | self.assertEqual(U_BOOT_TPL_DATA, tpl_data[:len(U_BOOT_TPL_DATA)]) |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2097 | |
| 2098 | def testPackX86RomIfwi(self): |
| 2099 | """Test that an x86 ROM with Integrated Firmware Image can be created""" |
| 2100 | self._SetupIfwi('fitimage.bin') |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2101 | data = self._DoReadFile('111_x86_rom_ifwi.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2102 | self._CheckIfwi(data) |
| 2103 | |
| 2104 | def testPackX86RomIfwiNoDesc(self): |
| 2105 | """Test that an x86 ROM with IFWI can be created from an ifwi.bin file""" |
| 2106 | self._SetupIfwi('ifwi.bin') |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2107 | data = self._DoReadFile('112_x86_rom_ifwi_nodesc.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2108 | self._CheckIfwi(data) |
| 2109 | |
| 2110 | def testPackX86RomIfwiNoData(self): |
| 2111 | """Test that an x86 ROM with IFWI handles missing data""" |
| 2112 | self._SetupIfwi('ifwi.bin') |
| 2113 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2114 | data = self._DoReadFile('113_x86_rom_ifwi_nodata.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2115 | self.assertIn('Could not complete processing of contents', |
| 2116 | str(e.exception)) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 2117 | |
Simon Glass | c2f1aed | 2019-07-08 13:18:56 -0600 | [diff] [blame] | 2118 | def testCbfsOffset(self): |
| 2119 | """Test a CBFS with files at particular offsets |
| 2120 | |
| 2121 | Like all CFBS tests, this is just checking the logic that calls |
| 2122 | cbfs_util. See cbfs_util_test for fully tests (e.g. test_cbfs_offset()). |
| 2123 | """ |
| 2124 | data = self._DoReadFile('114_cbfs_offset.dts') |
| 2125 | size = 0x200 |
| 2126 | |
| 2127 | cbfs = cbfs_util.CbfsReader(data) |
| 2128 | self.assertEqual(size, cbfs.rom_size) |
| 2129 | |
| 2130 | self.assertIn('u-boot', cbfs.files) |
| 2131 | cfile = cbfs.files['u-boot'] |
| 2132 | self.assertEqual(U_BOOT_DATA, cfile.data) |
| 2133 | self.assertEqual(0x40, cfile.cbfs_offset) |
| 2134 | |
| 2135 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2136 | cfile2 = cbfs.files['u-boot-dtb'] |
| 2137 | self.assertEqual(U_BOOT_DTB_DATA, cfile2.data) |
| 2138 | self.assertEqual(0x140, cfile2.cbfs_offset) |
| 2139 | |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2140 | def testFdtmap(self): |
| 2141 | """Test an FDT map can be inserted in the image""" |
| 2142 | data = self.data = self._DoReadFileRealDtb('115_fdtmap.dts') |
| 2143 | fdtmap_data = data[len(U_BOOT_DATA):] |
| 2144 | magic = fdtmap_data[:8] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2145 | self.assertEqual(b'_FDTMAP_', magic) |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2146 | self.assertEqual(tools.GetBytes(0, 8), fdtmap_data[8:16]) |
| 2147 | |
| 2148 | fdt_data = fdtmap_data[16:] |
| 2149 | dtb = fdt.Fdt.FromData(fdt_data) |
| 2150 | dtb.Scan() |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 2151 | props = self._GetPropTree(dtb, BASE_DTB_PROPS, prefix='/') |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2152 | self.assertEqual({ |
| 2153 | 'image-pos': 0, |
| 2154 | 'offset': 0, |
| 2155 | 'u-boot:offset': 0, |
| 2156 | 'u-boot:size': len(U_BOOT_DATA), |
| 2157 | 'u-boot:image-pos': 0, |
| 2158 | 'fdtmap:image-pos': 4, |
| 2159 | 'fdtmap:offset': 4, |
| 2160 | 'fdtmap:size': len(fdtmap_data), |
| 2161 | 'size': len(data), |
| 2162 | }, props) |
| 2163 | |
| 2164 | def testFdtmapNoMatch(self): |
| 2165 | """Check handling of an FDT map when the section cannot be found""" |
| 2166 | self.data = self._DoReadFileRealDtb('115_fdtmap.dts') |
| 2167 | |
| 2168 | # Mangle the section name, which should cause a mismatch between the |
| 2169 | # correct FDT path and the one expected by the section |
| 2170 | image = control.images['image'] |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2171 | image._node.path += '-suffix' |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2172 | entries = image.GetEntries() |
| 2173 | fdtmap = entries['fdtmap'] |
| 2174 | with self.assertRaises(ValueError) as e: |
| 2175 | fdtmap._GetFdtmap() |
| 2176 | self.assertIn("Cannot locate node for path '/binman-suffix'", |
| 2177 | str(e.exception)) |
| 2178 | |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2179 | def testFdtmapHeader(self): |
| 2180 | """Test an FDT map and image header can be inserted in the image""" |
| 2181 | data = self.data = self._DoReadFileRealDtb('116_fdtmap_hdr.dts') |
| 2182 | fdtmap_pos = len(U_BOOT_DATA) |
| 2183 | fdtmap_data = data[fdtmap_pos:] |
| 2184 | fdt_data = fdtmap_data[16:] |
| 2185 | dtb = fdt.Fdt.FromData(fdt_data) |
| 2186 | fdt_size = dtb.GetFdtObj().totalsize() |
| 2187 | hdr_data = data[-8:] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2188 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2189 | offset = struct.unpack('<I', hdr_data[4:])[0] & 0xffffffff |
| 2190 | self.assertEqual(fdtmap_pos - 0x400, offset - (1 << 32)) |
| 2191 | |
| 2192 | def testFdtmapHeaderStart(self): |
| 2193 | """Test an image header can be inserted at the image start""" |
| 2194 | data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts') |
| 2195 | fdtmap_pos = 0x100 + len(U_BOOT_DATA) |
| 2196 | hdr_data = data[:8] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2197 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2198 | offset = struct.unpack('<I', hdr_data[4:])[0] |
| 2199 | self.assertEqual(fdtmap_pos, offset) |
| 2200 | |
| 2201 | def testFdtmapHeaderPos(self): |
| 2202 | """Test an image header can be inserted at a chosen position""" |
| 2203 | data = self.data = self._DoReadFileRealDtb('118_fdtmap_hdr_pos.dts') |
| 2204 | fdtmap_pos = 0x100 + len(U_BOOT_DATA) |
| 2205 | hdr_data = data[0x80:0x88] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2206 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2207 | offset = struct.unpack('<I', hdr_data[4:])[0] |
| 2208 | self.assertEqual(fdtmap_pos, offset) |
| 2209 | |
| 2210 | def testHeaderMissingFdtmap(self): |
| 2211 | """Test an image header requires an fdtmap""" |
| 2212 | with self.assertRaises(ValueError) as e: |
| 2213 | self.data = self._DoReadFileRealDtb('119_fdtmap_hdr_missing.dts') |
| 2214 | self.assertIn("'image_header' section must have an 'fdtmap' sibling", |
| 2215 | str(e.exception)) |
| 2216 | |
| 2217 | def testHeaderNoLocation(self): |
| 2218 | """Test an image header with a no specified location is detected""" |
| 2219 | with self.assertRaises(ValueError) as e: |
| 2220 | self.data = self._DoReadFileRealDtb('120_hdr_no_location.dts') |
| 2221 | self.assertIn("Invalid location 'None', expected 'start' or 'end'", |
| 2222 | str(e.exception)) |
| 2223 | |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2224 | def testEntryExpand(self): |
| 2225 | """Test expanding an entry after it is packed""" |
| 2226 | data = self._DoReadFile('121_entry_expand.dts') |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 2227 | self.assertEqual(b'aaa', data[:3]) |
| 2228 | self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)]) |
| 2229 | self.assertEqual(b'aaa', data[-3:]) |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2230 | |
| 2231 | def testEntryExpandBad(self): |
| 2232 | """Test expanding an entry after it is packed, twice""" |
| 2233 | with self.assertRaises(ValueError) as e: |
| 2234 | self._DoReadFile('122_entry_expand_twice.dts') |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 2235 | self.assertIn("Image '/binman': Entries changed size after packing", |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2236 | str(e.exception)) |
| 2237 | |
| 2238 | def testEntryExpandSection(self): |
| 2239 | """Test expanding an entry within a section after it is packed""" |
| 2240 | data = self._DoReadFile('123_entry_expand_section.dts') |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 2241 | self.assertEqual(b'aaa', data[:3]) |
| 2242 | self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)]) |
| 2243 | self.assertEqual(b'aaa', data[-3:]) |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2244 | |
Simon Glass | 90d2968 | 2019-07-08 14:25:38 -0600 | [diff] [blame] | 2245 | def testCompressDtb(self): |
| 2246 | """Test that compress of device-tree files is supported""" |
| 2247 | self._CheckLz4() |
| 2248 | data = self.data = self._DoReadFileRealDtb('124_compress_dtb.dts') |
| 2249 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 2250 | comp_data = data[len(U_BOOT_DATA):] |
| 2251 | orig = self._decompress(comp_data) |
| 2252 | dtb = fdt.Fdt.FromData(orig) |
| 2253 | dtb.Scan() |
| 2254 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 2255 | expected = { |
| 2256 | 'u-boot:size': len(U_BOOT_DATA), |
| 2257 | 'u-boot-dtb:uncomp-size': len(orig), |
| 2258 | 'u-boot-dtb:size': len(comp_data), |
| 2259 | 'size': len(data), |
| 2260 | } |
| 2261 | self.assertEqual(expected, props) |
| 2262 | |
Simon Glass | 151bbbf | 2019-07-08 14:25:41 -0600 | [diff] [blame] | 2263 | def testCbfsUpdateFdt(self): |
| 2264 | """Test that we can update the device tree with CBFS offset/size info""" |
| 2265 | self._CheckLz4() |
| 2266 | data, _, _, out_dtb_fname = self._DoReadFileDtb('125_cbfs_update.dts', |
| 2267 | update_dtb=True) |
| 2268 | dtb = fdt.Fdt(out_dtb_fname) |
| 2269 | dtb.Scan() |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 2270 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + ['uncomp-size']) |
Simon Glass | 151bbbf | 2019-07-08 14:25:41 -0600 | [diff] [blame] | 2271 | del props['cbfs/u-boot:size'] |
| 2272 | self.assertEqual({ |
| 2273 | 'offset': 0, |
| 2274 | 'size': len(data), |
| 2275 | 'image-pos': 0, |
| 2276 | 'cbfs:offset': 0, |
| 2277 | 'cbfs:size': len(data), |
| 2278 | 'cbfs:image-pos': 0, |
| 2279 | 'cbfs/u-boot:offset': 0x38, |
| 2280 | 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA), |
| 2281 | 'cbfs/u-boot:image-pos': 0x38, |
| 2282 | 'cbfs/u-boot-dtb:offset': 0xb8, |
| 2283 | 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA), |
| 2284 | 'cbfs/u-boot-dtb:image-pos': 0xb8, |
| 2285 | }, props) |
| 2286 | |
Simon Glass | 3c9b4f2 | 2019-07-08 14:25:42 -0600 | [diff] [blame] | 2287 | def testCbfsBadType(self): |
| 2288 | """Test an image header with a no specified location is detected""" |
| 2289 | with self.assertRaises(ValueError) as e: |
| 2290 | self._DoReadFile('126_cbfs_bad_type.dts') |
| 2291 | self.assertIn("Unknown cbfs-type 'badtype'", str(e.exception)) |
| 2292 | |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2293 | def testList(self): |
| 2294 | """Test listing the files in an image""" |
| 2295 | self._CheckLz4() |
| 2296 | data = self._DoReadFile('127_list.dts') |
| 2297 | image = control.images['image'] |
| 2298 | entries = image.BuildEntryList() |
| 2299 | self.assertEqual(7, len(entries)) |
| 2300 | |
| 2301 | ent = entries[0] |
| 2302 | self.assertEqual(0, ent.indent) |
| 2303 | self.assertEqual('main-section', ent.name) |
| 2304 | self.assertEqual('section', ent.etype) |
| 2305 | self.assertEqual(len(data), ent.size) |
| 2306 | self.assertEqual(0, ent.image_pos) |
| 2307 | self.assertEqual(None, ent.uncomp_size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2308 | self.assertEqual(0, ent.offset) |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2309 | |
| 2310 | ent = entries[1] |
| 2311 | self.assertEqual(1, ent.indent) |
| 2312 | self.assertEqual('u-boot', ent.name) |
| 2313 | self.assertEqual('u-boot', ent.etype) |
| 2314 | self.assertEqual(len(U_BOOT_DATA), ent.size) |
| 2315 | self.assertEqual(0, ent.image_pos) |
| 2316 | self.assertEqual(None, ent.uncomp_size) |
| 2317 | self.assertEqual(0, ent.offset) |
| 2318 | |
| 2319 | ent = entries[2] |
| 2320 | self.assertEqual(1, ent.indent) |
| 2321 | self.assertEqual('section', ent.name) |
| 2322 | self.assertEqual('section', ent.etype) |
| 2323 | section_size = ent.size |
| 2324 | self.assertEqual(0x100, ent.image_pos) |
| 2325 | self.assertEqual(None, ent.uncomp_size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2326 | self.assertEqual(0x100, ent.offset) |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2327 | |
| 2328 | ent = entries[3] |
| 2329 | self.assertEqual(2, ent.indent) |
| 2330 | self.assertEqual('cbfs', ent.name) |
| 2331 | self.assertEqual('cbfs', ent.etype) |
| 2332 | self.assertEqual(0x400, ent.size) |
| 2333 | self.assertEqual(0x100, ent.image_pos) |
| 2334 | self.assertEqual(None, ent.uncomp_size) |
| 2335 | self.assertEqual(0, ent.offset) |
| 2336 | |
| 2337 | ent = entries[4] |
| 2338 | self.assertEqual(3, ent.indent) |
| 2339 | self.assertEqual('u-boot', ent.name) |
| 2340 | self.assertEqual('u-boot', ent.etype) |
| 2341 | self.assertEqual(len(U_BOOT_DATA), ent.size) |
| 2342 | self.assertEqual(0x138, ent.image_pos) |
| 2343 | self.assertEqual(None, ent.uncomp_size) |
| 2344 | self.assertEqual(0x38, ent.offset) |
| 2345 | |
| 2346 | ent = entries[5] |
| 2347 | self.assertEqual(3, ent.indent) |
| 2348 | self.assertEqual('u-boot-dtb', ent.name) |
| 2349 | self.assertEqual('text', ent.etype) |
| 2350 | self.assertGreater(len(COMPRESS_DATA), ent.size) |
| 2351 | self.assertEqual(0x178, ent.image_pos) |
| 2352 | self.assertEqual(len(COMPRESS_DATA), ent.uncomp_size) |
| 2353 | self.assertEqual(0x78, ent.offset) |
| 2354 | |
| 2355 | ent = entries[6] |
| 2356 | self.assertEqual(2, ent.indent) |
| 2357 | self.assertEqual('u-boot-dtb', ent.name) |
| 2358 | self.assertEqual('u-boot-dtb', ent.etype) |
| 2359 | self.assertEqual(0x500, ent.image_pos) |
| 2360 | self.assertEqual(len(U_BOOT_DTB_DATA), ent.uncomp_size) |
| 2361 | dtb_size = ent.size |
| 2362 | # Compressing this data expands it since headers are added |
| 2363 | self.assertGreater(dtb_size, len(U_BOOT_DTB_DATA)) |
| 2364 | self.assertEqual(0x400, ent.offset) |
| 2365 | |
| 2366 | self.assertEqual(len(data), 0x100 + section_size) |
| 2367 | self.assertEqual(section_size, 0x400 + dtb_size) |
| 2368 | |
Simon Glass | 8d8bf4e | 2019-07-08 14:25:44 -0600 | [diff] [blame] | 2369 | def testFindFdtmap(self): |
| 2370 | """Test locating an FDT map in an image""" |
| 2371 | self._CheckLz4() |
| 2372 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
| 2373 | image = control.images['image'] |
| 2374 | entries = image.GetEntries() |
| 2375 | entry = entries['fdtmap'] |
| 2376 | self.assertEqual(entry.image_pos, fdtmap.LocateFdtmap(data)) |
| 2377 | |
| 2378 | def testFindFdtmapMissing(self): |
| 2379 | """Test failing to locate an FDP map""" |
| 2380 | data = self._DoReadFile('005_simple.dts') |
| 2381 | self.assertEqual(None, fdtmap.LocateFdtmap(data)) |
| 2382 | |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2383 | def testFindImageHeader(self): |
| 2384 | """Test locating a image header""" |
| 2385 | self._CheckLz4() |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2386 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2387 | image = control.images['image'] |
| 2388 | entries = image.GetEntries() |
| 2389 | entry = entries['fdtmap'] |
| 2390 | # The header should point to the FDT map |
| 2391 | self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data)) |
| 2392 | |
| 2393 | def testFindImageHeaderStart(self): |
| 2394 | """Test locating a image header located at the start of an image""" |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2395 | data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts') |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2396 | image = control.images['image'] |
| 2397 | entries = image.GetEntries() |
| 2398 | entry = entries['fdtmap'] |
| 2399 | # The header should point to the FDT map |
| 2400 | self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data)) |
| 2401 | |
| 2402 | def testFindImageHeaderMissing(self): |
| 2403 | """Test failing to locate an image header""" |
| 2404 | data = self._DoReadFile('005_simple.dts') |
| 2405 | self.assertEqual(None, image_header.LocateHeaderOffset(data)) |
| 2406 | |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2407 | def testReadImage(self): |
| 2408 | """Test reading an image and accessing its FDT map""" |
| 2409 | self._CheckLz4() |
| 2410 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
| 2411 | image_fname = tools.GetOutputFilename('image.bin') |
| 2412 | orig_image = control.images['image'] |
| 2413 | image = Image.FromFile(image_fname) |
| 2414 | self.assertEqual(orig_image.GetEntries().keys(), |
| 2415 | image.GetEntries().keys()) |
| 2416 | |
| 2417 | orig_entry = orig_image.GetEntries()['fdtmap'] |
| 2418 | entry = image.GetEntries()['fdtmap'] |
| 2419 | self.assertEquals(orig_entry.offset, entry.offset) |
| 2420 | self.assertEquals(orig_entry.size, entry.size) |
| 2421 | self.assertEquals(orig_entry.image_pos, entry.image_pos) |
| 2422 | |
| 2423 | def testReadImageNoHeader(self): |
| 2424 | """Test accessing an image's FDT map without an image header""" |
| 2425 | self._CheckLz4() |
| 2426 | data = self._DoReadFileRealDtb('129_decode_image_nohdr.dts') |
| 2427 | image_fname = tools.GetOutputFilename('image.bin') |
| 2428 | image = Image.FromFile(image_fname) |
| 2429 | self.assertTrue(isinstance(image, Image)) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2430 | self.assertEqual('image', image.image_name[-5:]) |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2431 | |
| 2432 | def testReadImageFail(self): |
| 2433 | """Test failing to read an image image's FDT map""" |
| 2434 | self._DoReadFile('005_simple.dts') |
| 2435 | image_fname = tools.GetOutputFilename('image.bin') |
| 2436 | with self.assertRaises(ValueError) as e: |
| 2437 | image = Image.FromFile(image_fname) |
| 2438 | self.assertIn("Cannot find FDT map in image", str(e.exception)) |
Simon Glass | c2f1aed | 2019-07-08 13:18:56 -0600 | [diff] [blame] | 2439 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2440 | def testListCmd(self): |
| 2441 | """Test listing the files in an image using an Fdtmap""" |
| 2442 | self._CheckLz4() |
| 2443 | data = self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2444 | |
| 2445 | # lz4 compression size differs depending on the version |
| 2446 | image = control.images['image'] |
| 2447 | entries = image.GetEntries() |
| 2448 | section_size = entries['section'].size |
| 2449 | fdt_size = entries['section'].GetEntries()['u-boot-dtb'].size |
| 2450 | fdtmap_offset = entries['fdtmap'].offset |
| 2451 | |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2452 | try: |
| 2453 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2454 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2455 | self._DoBinman('ls', '-i', updated_fname) |
| 2456 | finally: |
| 2457 | shutil.rmtree(tmpdir) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2458 | lines = stdout.getvalue().splitlines() |
| 2459 | expected = [ |
| 2460 | 'Name Image-pos Size Entry-type Offset Uncomp-size', |
| 2461 | '----------------------------------------------------------------------', |
| 2462 | 'main-section 0 c00 section 0', |
| 2463 | ' u-boot 0 4 u-boot 0', |
| 2464 | ' section 100 %x section 100' % section_size, |
| 2465 | ' cbfs 100 400 cbfs 0', |
| 2466 | ' u-boot 138 4 u-boot 38', |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2467 | ' u-boot-dtb 180 105 u-boot-dtb 80 3c9', |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2468 | ' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size, |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2469 | ' fdtmap %x 3bd fdtmap %x' % |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2470 | (fdtmap_offset, fdtmap_offset), |
| 2471 | ' image-header bf8 8 image-header bf8', |
| 2472 | ] |
| 2473 | self.assertEqual(expected, lines) |
| 2474 | |
| 2475 | def testListCmdFail(self): |
| 2476 | """Test failing to list an image""" |
| 2477 | self._DoReadFile('005_simple.dts') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2478 | try: |
| 2479 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2480 | with self.assertRaises(ValueError) as e: |
| 2481 | self._DoBinman('ls', '-i', updated_fname) |
| 2482 | finally: |
| 2483 | shutil.rmtree(tmpdir) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2484 | self.assertIn("Cannot find FDT map in image", str(e.exception)) |
| 2485 | |
| 2486 | def _RunListCmd(self, paths, expected): |
| 2487 | """List out entries and check the result |
| 2488 | |
| 2489 | Args: |
| 2490 | paths: List of paths to pass to the list command |
| 2491 | expected: Expected list of filenames to be returned, in order |
| 2492 | """ |
| 2493 | self._CheckLz4() |
| 2494 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2495 | image_fname = tools.GetOutputFilename('image.bin') |
| 2496 | image = Image.FromFile(image_fname) |
| 2497 | lines = image.GetListEntries(paths)[1] |
| 2498 | files = [line[0].strip() for line in lines[1:]] |
| 2499 | self.assertEqual(expected, files) |
| 2500 | |
| 2501 | def testListCmdSection(self): |
| 2502 | """Test listing the files in a section""" |
| 2503 | self._RunListCmd(['section'], |
| 2504 | ['section', 'cbfs', 'u-boot', 'u-boot-dtb', 'u-boot-dtb']) |
| 2505 | |
| 2506 | def testListCmdFile(self): |
| 2507 | """Test listing a particular file""" |
| 2508 | self._RunListCmd(['*u-boot-dtb'], ['u-boot-dtb', 'u-boot-dtb']) |
| 2509 | |
| 2510 | def testListCmdWildcard(self): |
| 2511 | """Test listing a wildcarded file""" |
| 2512 | self._RunListCmd(['*boot*'], |
| 2513 | ['u-boot', 'u-boot', 'u-boot-dtb', 'u-boot-dtb']) |
| 2514 | |
| 2515 | def testListCmdWildcardMulti(self): |
| 2516 | """Test listing a wildcarded file""" |
| 2517 | self._RunListCmd(['*cb*', '*head*'], |
| 2518 | ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header']) |
| 2519 | |
| 2520 | def testListCmdEmpty(self): |
| 2521 | """Test listing a wildcarded file""" |
| 2522 | self._RunListCmd(['nothing'], []) |
| 2523 | |
| 2524 | def testListCmdPath(self): |
| 2525 | """Test listing the files in a sub-entry of a section""" |
| 2526 | self._RunListCmd(['section/cbfs'], ['cbfs', 'u-boot', 'u-boot-dtb']) |
| 2527 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2528 | def _RunExtractCmd(self, entry_name, decomp=True): |
| 2529 | """Extract an entry from an image |
| 2530 | |
| 2531 | Args: |
| 2532 | entry_name: Entry name to extract |
| 2533 | decomp: True to decompress the data if compressed, False to leave |
| 2534 | it in its raw uncompressed format |
| 2535 | |
| 2536 | Returns: |
| 2537 | data from entry |
| 2538 | """ |
| 2539 | self._CheckLz4() |
| 2540 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2541 | image_fname = tools.GetOutputFilename('image.bin') |
| 2542 | return control.ReadEntry(image_fname, entry_name, decomp) |
| 2543 | |
| 2544 | def testExtractSimple(self): |
| 2545 | """Test extracting a single file""" |
| 2546 | data = self._RunExtractCmd('u-boot') |
| 2547 | self.assertEqual(U_BOOT_DATA, data) |
| 2548 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2549 | def testExtractSection(self): |
| 2550 | """Test extracting the files in a section""" |
| 2551 | data = self._RunExtractCmd('section') |
| 2552 | cbfs_data = data[:0x400] |
| 2553 | cbfs = cbfs_util.CbfsReader(cbfs_data) |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2554 | self.assertEqual(['u-boot', 'u-boot-dtb', ''], list(cbfs.files.keys())) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2555 | dtb_data = data[0x400:] |
| 2556 | dtb = self._decompress(dtb_data) |
| 2557 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2558 | |
| 2559 | def testExtractCompressed(self): |
| 2560 | """Test extracting compressed data""" |
| 2561 | data = self._RunExtractCmd('section/u-boot-dtb') |
| 2562 | self.assertEqual(EXTRACT_DTB_SIZE, len(data)) |
| 2563 | |
| 2564 | def testExtractRaw(self): |
| 2565 | """Test extracting compressed data without decompressing it""" |
| 2566 | data = self._RunExtractCmd('section/u-boot-dtb', decomp=False) |
| 2567 | dtb = self._decompress(data) |
| 2568 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2569 | |
| 2570 | def testExtractCbfs(self): |
| 2571 | """Test extracting CBFS data""" |
| 2572 | data = self._RunExtractCmd('section/cbfs/u-boot') |
| 2573 | self.assertEqual(U_BOOT_DATA, data) |
| 2574 | |
| 2575 | def testExtractCbfsCompressed(self): |
| 2576 | """Test extracting CBFS compressed data""" |
| 2577 | data = self._RunExtractCmd('section/cbfs/u-boot-dtb') |
| 2578 | self.assertEqual(EXTRACT_DTB_SIZE, len(data)) |
| 2579 | |
| 2580 | def testExtractCbfsRaw(self): |
| 2581 | """Test extracting CBFS compressed data without decompressing it""" |
| 2582 | data = self._RunExtractCmd('section/cbfs/u-boot-dtb', decomp=False) |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 2583 | dtb = tools.Decompress(data, 'lzma', with_header=False) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2584 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2585 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2586 | def testExtractBadEntry(self): |
| 2587 | """Test extracting a bad section path""" |
| 2588 | with self.assertRaises(ValueError) as e: |
| 2589 | self._RunExtractCmd('section/does-not-exist') |
| 2590 | self.assertIn("Entry 'does-not-exist' not found in '/section'", |
| 2591 | str(e.exception)) |
| 2592 | |
| 2593 | def testExtractMissingFile(self): |
| 2594 | """Test extracting file that does not exist""" |
| 2595 | with self.assertRaises(IOError) as e: |
| 2596 | control.ReadEntry('missing-file', 'name') |
| 2597 | |
| 2598 | def testExtractBadFile(self): |
| 2599 | """Test extracting an invalid file""" |
| 2600 | fname = os.path.join(self._indir, 'badfile') |
| 2601 | tools.WriteFile(fname, b'') |
| 2602 | with self.assertRaises(ValueError) as e: |
| 2603 | control.ReadEntry(fname, 'name') |
| 2604 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2605 | def testExtractCmd(self): |
| 2606 | """Test extracting a file fron an image on the command line""" |
| 2607 | self._CheckLz4() |
| 2608 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2609 | fname = os.path.join(self._indir, 'output.extact') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2610 | try: |
| 2611 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2612 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2613 | self._DoBinman('extract', '-i', updated_fname, 'u-boot', |
| 2614 | '-f', fname) |
| 2615 | finally: |
| 2616 | shutil.rmtree(tmpdir) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2617 | data = tools.ReadFile(fname) |
| 2618 | self.assertEqual(U_BOOT_DATA, data) |
| 2619 | |
| 2620 | def testExtractOneEntry(self): |
| 2621 | """Test extracting a single entry fron an image """ |
| 2622 | self._CheckLz4() |
| 2623 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2624 | image_fname = tools.GetOutputFilename('image.bin') |
| 2625 | fname = os.path.join(self._indir, 'output.extact') |
| 2626 | control.ExtractEntries(image_fname, fname, None, ['u-boot']) |
| 2627 | data = tools.ReadFile(fname) |
| 2628 | self.assertEqual(U_BOOT_DATA, data) |
| 2629 | |
| 2630 | def _CheckExtractOutput(self, decomp): |
| 2631 | """Helper to test file output with and without decompression |
| 2632 | |
| 2633 | Args: |
| 2634 | decomp: True to decompress entry data, False to output it raw |
| 2635 | """ |
| 2636 | def _CheckPresent(entry_path, expect_data, expect_size=None): |
| 2637 | """Check and remove expected file |
| 2638 | |
| 2639 | This checks the data/size of a file and removes the file both from |
| 2640 | the outfiles set and from the output directory. Once all files are |
| 2641 | processed, both the set and directory should be empty. |
| 2642 | |
| 2643 | Args: |
| 2644 | entry_path: Entry path |
| 2645 | expect_data: Data to expect in file, or None to skip check |
| 2646 | expect_size: Size of data to expect in file, or None to skip |
| 2647 | """ |
| 2648 | path = os.path.join(outdir, entry_path) |
| 2649 | data = tools.ReadFile(path) |
| 2650 | os.remove(path) |
| 2651 | if expect_data: |
| 2652 | self.assertEqual(expect_data, data) |
| 2653 | elif expect_size: |
| 2654 | self.assertEqual(expect_size, len(data)) |
| 2655 | outfiles.remove(path) |
| 2656 | |
| 2657 | def _CheckDirPresent(name): |
| 2658 | """Remove expected directory |
| 2659 | |
| 2660 | This gives an error if the directory does not exist as expected |
| 2661 | |
| 2662 | Args: |
| 2663 | name: Name of directory to remove |
| 2664 | """ |
| 2665 | path = os.path.join(outdir, name) |
| 2666 | os.rmdir(path) |
| 2667 | |
| 2668 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2669 | image_fname = tools.GetOutputFilename('image.bin') |
| 2670 | outdir = os.path.join(self._indir, 'extract') |
| 2671 | einfos = control.ExtractEntries(image_fname, None, outdir, [], decomp) |
| 2672 | |
| 2673 | # Create a set of all file that were output (should be 9) |
| 2674 | outfiles = set() |
| 2675 | for root, dirs, files in os.walk(outdir): |
| 2676 | outfiles |= set([os.path.join(root, fname) for fname in files]) |
| 2677 | self.assertEqual(9, len(outfiles)) |
| 2678 | self.assertEqual(9, len(einfos)) |
| 2679 | |
| 2680 | image = control.images['image'] |
| 2681 | entries = image.GetEntries() |
| 2682 | |
| 2683 | # Check the 9 files in various ways |
| 2684 | section = entries['section'] |
| 2685 | section_entries = section.GetEntries() |
| 2686 | cbfs_entries = section_entries['cbfs'].GetEntries() |
| 2687 | _CheckPresent('u-boot', U_BOOT_DATA) |
| 2688 | _CheckPresent('section/cbfs/u-boot', U_BOOT_DATA) |
| 2689 | dtb_len = EXTRACT_DTB_SIZE |
| 2690 | if not decomp: |
| 2691 | dtb_len = cbfs_entries['u-boot-dtb'].size |
| 2692 | _CheckPresent('section/cbfs/u-boot-dtb', None, dtb_len) |
| 2693 | if not decomp: |
| 2694 | dtb_len = section_entries['u-boot-dtb'].size |
| 2695 | _CheckPresent('section/u-boot-dtb', None, dtb_len) |
| 2696 | |
| 2697 | fdtmap = entries['fdtmap'] |
| 2698 | _CheckPresent('fdtmap', fdtmap.data) |
| 2699 | hdr = entries['image-header'] |
| 2700 | _CheckPresent('image-header', hdr.data) |
| 2701 | |
| 2702 | _CheckPresent('section/root', section.data) |
| 2703 | cbfs = section_entries['cbfs'] |
| 2704 | _CheckPresent('section/cbfs/root', cbfs.data) |
| 2705 | data = tools.ReadFile(image_fname) |
| 2706 | _CheckPresent('root', data) |
| 2707 | |
| 2708 | # There should be no files left. Remove all the directories to check. |
| 2709 | # If there are any files/dirs remaining, one of these checks will fail. |
| 2710 | self.assertEqual(0, len(outfiles)) |
| 2711 | _CheckDirPresent('section/cbfs') |
| 2712 | _CheckDirPresent('section') |
| 2713 | _CheckDirPresent('') |
| 2714 | self.assertFalse(os.path.exists(outdir)) |
| 2715 | |
| 2716 | def testExtractAllEntries(self): |
| 2717 | """Test extracting all entries""" |
| 2718 | self._CheckLz4() |
| 2719 | self._CheckExtractOutput(decomp=True) |
| 2720 | |
| 2721 | def testExtractAllEntriesRaw(self): |
| 2722 | """Test extracting all entries without decompressing them""" |
| 2723 | self._CheckLz4() |
| 2724 | self._CheckExtractOutput(decomp=False) |
| 2725 | |
| 2726 | def testExtractSelectedEntries(self): |
| 2727 | """Test extracting some entries""" |
| 2728 | self._CheckLz4() |
| 2729 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2730 | image_fname = tools.GetOutputFilename('image.bin') |
| 2731 | outdir = os.path.join(self._indir, 'extract') |
| 2732 | einfos = control.ExtractEntries(image_fname, None, outdir, |
| 2733 | ['*cb*', '*head*']) |
| 2734 | |
| 2735 | # File output is tested by testExtractAllEntries(), so just check that |
| 2736 | # the expected entries are selected |
| 2737 | names = [einfo.name for einfo in einfos] |
| 2738 | self.assertEqual(names, |
| 2739 | ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header']) |
| 2740 | |
| 2741 | def testExtractNoEntryPaths(self): |
| 2742 | """Test extracting some entries""" |
| 2743 | self._CheckLz4() |
| 2744 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2745 | image_fname = tools.GetOutputFilename('image.bin') |
| 2746 | with self.assertRaises(ValueError) as e: |
| 2747 | control.ExtractEntries(image_fname, 'fname', None, []) |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 2748 | self.assertIn('Must specify an entry path to write with -f', |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2749 | str(e.exception)) |
| 2750 | |
| 2751 | def testExtractTooManyEntryPaths(self): |
| 2752 | """Test extracting some entries""" |
| 2753 | self._CheckLz4() |
| 2754 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2755 | image_fname = tools.GetOutputFilename('image.bin') |
| 2756 | with self.assertRaises(ValueError) as e: |
| 2757 | control.ExtractEntries(image_fname, 'fname', None, ['a', 'b']) |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 2758 | self.assertIn('Must specify exactly one entry path to write with -f', |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2759 | str(e.exception)) |
| 2760 | |
Simon Glass | 52d0621 | 2019-07-08 14:25:53 -0600 | [diff] [blame] | 2761 | def testPackAlignSection(self): |
| 2762 | """Test that sections can have alignment""" |
| 2763 | self._DoReadFile('131_pack_align_section.dts') |
| 2764 | |
| 2765 | self.assertIn('image', control.images) |
| 2766 | image = control.images['image'] |
| 2767 | entries = image.GetEntries() |
| 2768 | self.assertEqual(3, len(entries)) |
| 2769 | |
| 2770 | # First u-boot |
| 2771 | self.assertIn('u-boot', entries) |
| 2772 | entry = entries['u-boot'] |
| 2773 | self.assertEqual(0, entry.offset) |
| 2774 | self.assertEqual(0, entry.image_pos) |
| 2775 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2776 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2777 | |
| 2778 | # Section0 |
| 2779 | self.assertIn('section0', entries) |
| 2780 | section0 = entries['section0'] |
| 2781 | self.assertEqual(0x10, section0.offset) |
| 2782 | self.assertEqual(0x10, section0.image_pos) |
| 2783 | self.assertEqual(len(U_BOOT_DATA), section0.size) |
| 2784 | |
| 2785 | # Second u-boot |
| 2786 | section_entries = section0.GetEntries() |
| 2787 | self.assertIn('u-boot', section_entries) |
| 2788 | entry = section_entries['u-boot'] |
| 2789 | self.assertEqual(0, entry.offset) |
| 2790 | self.assertEqual(0x10, entry.image_pos) |
| 2791 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2792 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2793 | |
| 2794 | # Section1 |
| 2795 | self.assertIn('section1', entries) |
| 2796 | section1 = entries['section1'] |
| 2797 | self.assertEqual(0x14, section1.offset) |
| 2798 | self.assertEqual(0x14, section1.image_pos) |
| 2799 | self.assertEqual(0x20, section1.size) |
| 2800 | |
| 2801 | # Second u-boot |
| 2802 | section_entries = section1.GetEntries() |
| 2803 | self.assertIn('u-boot', section_entries) |
| 2804 | entry = section_entries['u-boot'] |
| 2805 | self.assertEqual(0, entry.offset) |
| 2806 | self.assertEqual(0x14, entry.image_pos) |
| 2807 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2808 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2809 | |
| 2810 | # Section2 |
| 2811 | self.assertIn('section2', section_entries) |
| 2812 | section2 = section_entries['section2'] |
| 2813 | self.assertEqual(0x4, section2.offset) |
| 2814 | self.assertEqual(0x18, section2.image_pos) |
| 2815 | self.assertEqual(4, section2.size) |
| 2816 | |
| 2817 | # Third u-boot |
| 2818 | section_entries = section2.GetEntries() |
| 2819 | self.assertIn('u-boot', section_entries) |
| 2820 | entry = section_entries['u-boot'] |
| 2821 | self.assertEqual(0, entry.offset) |
| 2822 | self.assertEqual(0x18, entry.image_pos) |
| 2823 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2824 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2825 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2826 | def _RunReplaceCmd(self, entry_name, data, decomp=True, allow_resize=True, |
| 2827 | dts='132_replace.dts'): |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2828 | """Replace an entry in an image |
| 2829 | |
| 2830 | This writes the entry data to update it, then opens the updated file and |
| 2831 | returns the value that it now finds there. |
| 2832 | |
| 2833 | Args: |
| 2834 | entry_name: Entry name to replace |
| 2835 | data: Data to replace it with |
| 2836 | decomp: True to compress the data if needed, False if data is |
| 2837 | already compressed so should be used as is |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2838 | allow_resize: True to allow entries to change size, False to raise |
| 2839 | an exception |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2840 | |
| 2841 | Returns: |
| 2842 | Tuple: |
| 2843 | data from entry |
| 2844 | data from fdtmap (excluding header) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2845 | Image object that was modified |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2846 | """ |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2847 | dtb_data = self._DoReadFileDtb(dts, use_real_dtb=True, |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2848 | update_dtb=True)[1] |
| 2849 | |
| 2850 | self.assertIn('image', control.images) |
| 2851 | image = control.images['image'] |
| 2852 | entries = image.GetEntries() |
| 2853 | orig_dtb_data = entries['u-boot-dtb'].data |
| 2854 | orig_fdtmap_data = entries['fdtmap'].data |
| 2855 | |
| 2856 | image_fname = tools.GetOutputFilename('image.bin') |
| 2857 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 2858 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2859 | image = control.WriteEntry(updated_fname, entry_name, data, decomp, |
| 2860 | allow_resize) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2861 | data = control.ReadEntry(updated_fname, entry_name, decomp) |
| 2862 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2863 | # The DT data should not change unless resized: |
| 2864 | if not allow_resize: |
| 2865 | new_dtb_data = entries['u-boot-dtb'].data |
| 2866 | self.assertEqual(new_dtb_data, orig_dtb_data) |
| 2867 | new_fdtmap_data = entries['fdtmap'].data |
| 2868 | self.assertEqual(new_fdtmap_data, orig_fdtmap_data) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2869 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2870 | return data, orig_fdtmap_data[fdtmap.FDTMAP_HDR_LEN:], image |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2871 | |
| 2872 | def testReplaceSimple(self): |
| 2873 | """Test replacing a single file""" |
| 2874 | expected = b'x' * len(U_BOOT_DATA) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2875 | data, expected_fdtmap, _ = self._RunReplaceCmd('u-boot', expected, |
| 2876 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2877 | self.assertEqual(expected, data) |
| 2878 | |
| 2879 | # Test that the state looks right. There should be an FDT for the fdtmap |
| 2880 | # that we jsut read back in, and it should match what we find in the |
| 2881 | # 'control' tables. Checking for an FDT that does not exist should |
| 2882 | # return None. |
| 2883 | path, fdtmap = state.GetFdtContents('fdtmap') |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2884 | self.assertIsNotNone(path) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2885 | self.assertEqual(expected_fdtmap, fdtmap) |
| 2886 | |
| 2887 | dtb = state.GetFdtForEtype('fdtmap') |
| 2888 | self.assertEqual(dtb.GetContents(), fdtmap) |
| 2889 | |
| 2890 | missing_path, missing_fdtmap = state.GetFdtContents('missing') |
| 2891 | self.assertIsNone(missing_path) |
| 2892 | self.assertIsNone(missing_fdtmap) |
| 2893 | |
| 2894 | missing_dtb = state.GetFdtForEtype('missing') |
| 2895 | self.assertIsNone(missing_dtb) |
| 2896 | |
| 2897 | self.assertEqual('/binman', state.fdt_path_prefix) |
| 2898 | |
| 2899 | def testReplaceResizeFail(self): |
| 2900 | """Test replacing a file by something larger""" |
| 2901 | expected = U_BOOT_DATA + b'x' |
| 2902 | with self.assertRaises(ValueError) as e: |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2903 | self._RunReplaceCmd('u-boot', expected, allow_resize=False, |
| 2904 | dts='139_replace_repack.dts') |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2905 | self.assertIn("Node '/u-boot': Entry data size does not match, but resize is disabled", |
| 2906 | str(e.exception)) |
| 2907 | |
| 2908 | def testReplaceMulti(self): |
| 2909 | """Test replacing entry data where multiple images are generated""" |
| 2910 | data = self._DoReadFileDtb('133_replace_multi.dts', use_real_dtb=True, |
| 2911 | update_dtb=True)[0] |
| 2912 | expected = b'x' * len(U_BOOT_DATA) |
| 2913 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 2914 | tools.WriteFile(updated_fname, data) |
| 2915 | entry_name = 'u-boot' |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2916 | control.WriteEntry(updated_fname, entry_name, expected, |
| 2917 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2918 | data = control.ReadEntry(updated_fname, entry_name) |
| 2919 | self.assertEqual(expected, data) |
| 2920 | |
| 2921 | # Check the state looks right. |
| 2922 | self.assertEqual('/binman/image', state.fdt_path_prefix) |
| 2923 | |
| 2924 | # Now check we can write the first image |
| 2925 | image_fname = tools.GetOutputFilename('first-image.bin') |
| 2926 | updated_fname = tools.GetOutputFilename('first-updated.bin') |
| 2927 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
| 2928 | entry_name = 'u-boot' |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 2929 | control.WriteEntry(updated_fname, entry_name, expected, |
| 2930 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2931 | data = control.ReadEntry(updated_fname, entry_name) |
| 2932 | self.assertEqual(expected, data) |
| 2933 | |
| 2934 | # Check the state looks right. |
| 2935 | self.assertEqual('/binman/first-image', state.fdt_path_prefix) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2936 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 2937 | def testUpdateFdtAllRepack(self): |
| 2938 | """Test that all device trees are updated with offset/size info""" |
| 2939 | data = self._DoReadFileRealDtb('134_fdt_update_all_repack.dts') |
| 2940 | SECTION_SIZE = 0x300 |
| 2941 | DTB_SIZE = 602 |
| 2942 | FDTMAP_SIZE = 608 |
| 2943 | base_expected = { |
| 2944 | 'offset': 0, |
| 2945 | 'size': SECTION_SIZE + DTB_SIZE * 2 + FDTMAP_SIZE, |
| 2946 | 'image-pos': 0, |
| 2947 | 'section:offset': 0, |
| 2948 | 'section:size': SECTION_SIZE, |
| 2949 | 'section:image-pos': 0, |
| 2950 | 'section/u-boot-dtb:offset': 4, |
| 2951 | 'section/u-boot-dtb:size': 636, |
| 2952 | 'section/u-boot-dtb:image-pos': 4, |
| 2953 | 'u-boot-spl-dtb:offset': SECTION_SIZE, |
| 2954 | 'u-boot-spl-dtb:size': DTB_SIZE, |
| 2955 | 'u-boot-spl-dtb:image-pos': SECTION_SIZE, |
| 2956 | 'u-boot-tpl-dtb:offset': SECTION_SIZE + DTB_SIZE, |
| 2957 | 'u-boot-tpl-dtb:image-pos': SECTION_SIZE + DTB_SIZE, |
| 2958 | 'u-boot-tpl-dtb:size': DTB_SIZE, |
| 2959 | 'fdtmap:offset': SECTION_SIZE + DTB_SIZE * 2, |
| 2960 | 'fdtmap:size': FDTMAP_SIZE, |
| 2961 | 'fdtmap:image-pos': SECTION_SIZE + DTB_SIZE * 2, |
| 2962 | } |
| 2963 | main_expected = { |
| 2964 | 'section:orig-size': SECTION_SIZE, |
| 2965 | 'section/u-boot-dtb:orig-offset': 4, |
| 2966 | } |
| 2967 | |
| 2968 | # We expect three device-tree files in the output, with the first one |
| 2969 | # within a fixed-size section. |
| 2970 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 2971 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 2972 | # main U-Boot tree. All three should have the same positions and offset |
| 2973 | # except that the main tree should include the main_expected properties |
| 2974 | start = 4 |
| 2975 | for item in ['', 'spl', 'tpl', None]: |
| 2976 | if item is None: |
| 2977 | start += 16 # Move past fdtmap header |
| 2978 | dtb = fdt.Fdt.FromData(data[start:]) |
| 2979 | dtb.Scan() |
| 2980 | props = self._GetPropTree(dtb, |
| 2981 | BASE_DTB_PROPS + REPACK_DTB_PROPS + ['spl', 'tpl'], |
| 2982 | prefix='/' if item is None else '/binman/') |
| 2983 | expected = dict(base_expected) |
| 2984 | if item: |
| 2985 | expected[item] = 0 |
| 2986 | else: |
| 2987 | # Main DTB and fdtdec should include the 'orig-' properties |
| 2988 | expected.update(main_expected) |
| 2989 | # Helpful for debugging: |
| 2990 | #for prop in sorted(props): |
| 2991 | #print('prop %s %s %s' % (prop, props[prop], expected[prop])) |
| 2992 | self.assertEqual(expected, props) |
| 2993 | if item == '': |
| 2994 | start = SECTION_SIZE |
| 2995 | else: |
| 2996 | start += dtb._fdt_obj.totalsize() |
| 2997 | |
Simon Glass | 1145376 | 2019-07-20 12:23:55 -0600 | [diff] [blame] | 2998 | def testFdtmapHeaderMiddle(self): |
| 2999 | """Test an FDT map in the middle of an image when it should be at end""" |
| 3000 | with self.assertRaises(ValueError) as e: |
| 3001 | self._DoReadFileRealDtb('135_fdtmap_hdr_middle.dts') |
| 3002 | self.assertIn("Invalid sibling order 'middle' for image-header: Must be at 'end' to match location", |
| 3003 | str(e.exception)) |
| 3004 | |
| 3005 | def testFdtmapHeaderStartBad(self): |
| 3006 | """Test an FDT map in middle of an image when it should be at start""" |
| 3007 | with self.assertRaises(ValueError) as e: |
| 3008 | self._DoReadFileRealDtb('136_fdtmap_hdr_startbad.dts') |
| 3009 | self.assertIn("Invalid sibling order 'end' for image-header: Must be at 'start' to match location", |
| 3010 | str(e.exception)) |
| 3011 | |
| 3012 | def testFdtmapHeaderEndBad(self): |
| 3013 | """Test an FDT map at the start of an image when it should be at end""" |
| 3014 | with self.assertRaises(ValueError) as e: |
| 3015 | self._DoReadFileRealDtb('137_fdtmap_hdr_endbad.dts') |
| 3016 | self.assertIn("Invalid sibling order 'start' for image-header: Must be at 'end' to match location", |
| 3017 | str(e.exception)) |
| 3018 | |
| 3019 | def testFdtmapHeaderNoSize(self): |
| 3020 | """Test an image header at the end of an image with undefined size""" |
| 3021 | self._DoReadFileRealDtb('138_fdtmap_hdr_nosize.dts') |
| 3022 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3023 | def testReplaceResize(self): |
| 3024 | """Test replacing a single file in an entry with a larger file""" |
| 3025 | expected = U_BOOT_DATA + b'x' |
| 3026 | data, _, image = self._RunReplaceCmd('u-boot', expected, |
| 3027 | dts='139_replace_repack.dts') |
| 3028 | self.assertEqual(expected, data) |
| 3029 | |
| 3030 | entries = image.GetEntries() |
| 3031 | dtb_data = entries['u-boot-dtb'].data |
| 3032 | dtb = fdt.Fdt.FromData(dtb_data) |
| 3033 | dtb.Scan() |
| 3034 | |
| 3035 | # The u-boot section should now be larger in the dtb |
| 3036 | node = dtb.GetNode('/binman/u-boot') |
| 3037 | self.assertEqual(len(expected), fdt_util.GetInt(node, 'size')) |
| 3038 | |
| 3039 | # Same for the fdtmap |
| 3040 | fdata = entries['fdtmap'].data |
| 3041 | fdtb = fdt.Fdt.FromData(fdata[fdtmap.FDTMAP_HDR_LEN:]) |
| 3042 | fdtb.Scan() |
| 3043 | fnode = fdtb.GetNode('/u-boot') |
| 3044 | self.assertEqual(len(expected), fdt_util.GetInt(fnode, 'size')) |
| 3045 | |
| 3046 | def testReplaceResizeNoRepack(self): |
| 3047 | """Test replacing an entry with a larger file when not allowed""" |
| 3048 | expected = U_BOOT_DATA + b'x' |
| 3049 | with self.assertRaises(ValueError) as e: |
| 3050 | self._RunReplaceCmd('u-boot', expected) |
| 3051 | self.assertIn('Entry data size does not match, but allow-repack is not present for this image', |
| 3052 | str(e.exception)) |
| 3053 | |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 3054 | def testEntryShrink(self): |
| 3055 | """Test contracting an entry after it is packed""" |
| 3056 | try: |
| 3057 | state.SetAllowEntryContraction(True) |
| 3058 | data = self._DoReadFileDtb('140_entry_shrink.dts', |
| 3059 | update_dtb=True)[0] |
| 3060 | finally: |
| 3061 | state.SetAllowEntryContraction(False) |
| 3062 | self.assertEqual(b'a', data[:1]) |
| 3063 | self.assertEqual(U_BOOT_DATA, data[1:1 + len(U_BOOT_DATA)]) |
| 3064 | self.assertEqual(b'a', data[-1:]) |
| 3065 | |
| 3066 | def testEntryShrinkFail(self): |
| 3067 | """Test not being allowed to contract an entry after it is packed""" |
| 3068 | data = self._DoReadFileDtb('140_entry_shrink.dts', update_dtb=True)[0] |
| 3069 | |
| 3070 | # In this case there is a spare byte at the end of the data. The size of |
| 3071 | # the contents is only 1 byte but we still have the size before it |
| 3072 | # shrunk. |
| 3073 | self.assertEqual(b'a\0', data[:2]) |
| 3074 | self.assertEqual(U_BOOT_DATA, data[2:2 + len(U_BOOT_DATA)]) |
| 3075 | self.assertEqual(b'a\0', data[-2:]) |
| 3076 | |
Simon Glass | 70e3298 | 2019-07-20 12:24:01 -0600 | [diff] [blame] | 3077 | def testDescriptorOffset(self): |
| 3078 | """Test that the Intel descriptor is always placed at at the start""" |
| 3079 | data = self._DoReadFileDtb('141_descriptor_offset.dts') |
| 3080 | image = control.images['image'] |
| 3081 | entries = image.GetEntries() |
| 3082 | desc = entries['intel-descriptor'] |
| 3083 | self.assertEqual(0xff800000, desc.offset); |
| 3084 | self.assertEqual(0xff800000, desc.image_pos); |
| 3085 | |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 3086 | def testReplaceCbfs(self): |
| 3087 | """Test replacing a single file in CBFS without changing the size""" |
| 3088 | self._CheckLz4() |
| 3089 | expected = b'x' * len(U_BOOT_DATA) |
| 3090 | data = self._DoReadFileRealDtb('142_replace_cbfs.dts') |
| 3091 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3092 | tools.WriteFile(updated_fname, data) |
| 3093 | entry_name = 'section/cbfs/u-boot' |
| 3094 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3095 | allow_resize=True) |
| 3096 | data = control.ReadEntry(updated_fname, entry_name) |
| 3097 | self.assertEqual(expected, data) |
| 3098 | |
| 3099 | def testReplaceResizeCbfs(self): |
| 3100 | """Test replacing a single file in CBFS with one of a different size""" |
| 3101 | self._CheckLz4() |
| 3102 | expected = U_BOOT_DATA + b'x' |
| 3103 | data = self._DoReadFileRealDtb('142_replace_cbfs.dts') |
| 3104 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3105 | tools.WriteFile(updated_fname, data) |
| 3106 | entry_name = 'section/cbfs/u-boot' |
| 3107 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3108 | allow_resize=True) |
| 3109 | data = control.ReadEntry(updated_fname, entry_name) |
| 3110 | self.assertEqual(expected, data) |
| 3111 | |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3112 | def _SetupForReplace(self): |
| 3113 | """Set up some files to use to replace entries |
| 3114 | |
| 3115 | This generates an image, copies it to a new file, extracts all the files |
| 3116 | in it and updates some of them |
| 3117 | |
| 3118 | Returns: |
| 3119 | List |
| 3120 | Image filename |
| 3121 | Output directory |
| 3122 | Expected values for updated entries, each a string |
| 3123 | """ |
| 3124 | data = self._DoReadFileRealDtb('143_replace_all.dts') |
| 3125 | |
| 3126 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3127 | tools.WriteFile(updated_fname, data) |
| 3128 | |
| 3129 | outdir = os.path.join(self._indir, 'extract') |
| 3130 | einfos = control.ExtractEntries(updated_fname, None, outdir, []) |
| 3131 | |
| 3132 | expected1 = b'x' + U_BOOT_DATA + b'y' |
| 3133 | u_boot_fname1 = os.path.join(outdir, 'u-boot') |
| 3134 | tools.WriteFile(u_boot_fname1, expected1) |
| 3135 | |
| 3136 | expected2 = b'a' + U_BOOT_DATA + b'b' |
| 3137 | u_boot_fname2 = os.path.join(outdir, 'u-boot2') |
| 3138 | tools.WriteFile(u_boot_fname2, expected2) |
| 3139 | |
| 3140 | expected_text = b'not the same text' |
| 3141 | text_fname = os.path.join(outdir, 'text') |
| 3142 | tools.WriteFile(text_fname, expected_text) |
| 3143 | |
| 3144 | dtb_fname = os.path.join(outdir, 'u-boot-dtb') |
| 3145 | dtb = fdt.FdtScan(dtb_fname) |
| 3146 | node = dtb.GetNode('/binman/text') |
| 3147 | node.AddString('my-property', 'the value') |
| 3148 | dtb.Sync(auto_resize=True) |
| 3149 | dtb.Flush() |
| 3150 | |
| 3151 | return updated_fname, outdir, expected1, expected2, expected_text |
| 3152 | |
| 3153 | def _CheckReplaceMultiple(self, entry_paths): |
| 3154 | """Handle replacing the contents of multiple entries |
| 3155 | |
| 3156 | Args: |
| 3157 | entry_paths: List of entry paths to replace |
| 3158 | |
| 3159 | Returns: |
| 3160 | List |
| 3161 | Dict of entries in the image: |
| 3162 | key: Entry name |
| 3163 | Value: Entry object |
| 3164 | Expected values for updated entries, each a string |
| 3165 | """ |
| 3166 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3167 | self._SetupForReplace()) |
| 3168 | control.ReplaceEntries(updated_fname, None, outdir, entry_paths) |
| 3169 | |
| 3170 | image = Image.FromFile(updated_fname) |
| 3171 | image.LoadData() |
| 3172 | return image.GetEntries(), expected1, expected2, expected_text |
| 3173 | |
| 3174 | def testReplaceAll(self): |
| 3175 | """Test replacing the contents of all entries""" |
| 3176 | entries, expected1, expected2, expected_text = ( |
| 3177 | self._CheckReplaceMultiple([])) |
| 3178 | data = entries['u-boot'].data |
| 3179 | self.assertEqual(expected1, data) |
| 3180 | |
| 3181 | data = entries['u-boot2'].data |
| 3182 | self.assertEqual(expected2, data) |
| 3183 | |
| 3184 | data = entries['text'].data |
| 3185 | self.assertEqual(expected_text, data) |
| 3186 | |
| 3187 | # Check that the device tree is updated |
| 3188 | data = entries['u-boot-dtb'].data |
| 3189 | dtb = fdt.Fdt.FromData(data) |
| 3190 | dtb.Scan() |
| 3191 | node = dtb.GetNode('/binman/text') |
| 3192 | self.assertEqual('the value', node.props['my-property'].value) |
| 3193 | |
| 3194 | def testReplaceSome(self): |
| 3195 | """Test replacing the contents of a few entries""" |
| 3196 | entries, expected1, expected2, expected_text = ( |
| 3197 | self._CheckReplaceMultiple(['u-boot2', 'text'])) |
| 3198 | |
| 3199 | # This one should not change |
| 3200 | data = entries['u-boot'].data |
| 3201 | self.assertEqual(U_BOOT_DATA, data) |
| 3202 | |
| 3203 | data = entries['u-boot2'].data |
| 3204 | self.assertEqual(expected2, data) |
| 3205 | |
| 3206 | data = entries['text'].data |
| 3207 | self.assertEqual(expected_text, data) |
| 3208 | |
| 3209 | def testReplaceCmd(self): |
| 3210 | """Test replacing a file fron an image on the command line""" |
| 3211 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3212 | |
| 3213 | try: |
| 3214 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 3215 | |
| 3216 | fname = os.path.join(tmpdir, 'update-u-boot.bin') |
| 3217 | expected = b'x' * len(U_BOOT_DATA) |
| 3218 | tools.WriteFile(fname, expected) |
| 3219 | |
| 3220 | self._DoBinman('replace', '-i', updated_fname, 'u-boot', '-f', fname) |
| 3221 | data = tools.ReadFile(updated_fname) |
| 3222 | self.assertEqual(expected, data[:len(expected)]) |
| 3223 | map_fname = os.path.join(tmpdir, 'image-updated.map') |
| 3224 | self.assertFalse(os.path.exists(map_fname)) |
| 3225 | finally: |
| 3226 | shutil.rmtree(tmpdir) |
| 3227 | |
| 3228 | def testReplaceCmdSome(self): |
| 3229 | """Test replacing some files fron an image on the command line""" |
| 3230 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3231 | self._SetupForReplace()) |
| 3232 | |
| 3233 | self._DoBinman('replace', '-i', updated_fname, '-I', outdir, |
| 3234 | 'u-boot2', 'text') |
| 3235 | |
| 3236 | tools.PrepareOutputDir(None) |
| 3237 | image = Image.FromFile(updated_fname) |
| 3238 | image.LoadData() |
| 3239 | entries = image.GetEntries() |
| 3240 | |
| 3241 | # This one should not change |
| 3242 | data = entries['u-boot'].data |
| 3243 | self.assertEqual(U_BOOT_DATA, data) |
| 3244 | |
| 3245 | data = entries['u-boot2'].data |
| 3246 | self.assertEqual(expected2, data) |
| 3247 | |
| 3248 | data = entries['text'].data |
| 3249 | self.assertEqual(expected_text, data) |
| 3250 | |
| 3251 | def testReplaceMissing(self): |
| 3252 | """Test replacing entries where the file is missing""" |
| 3253 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3254 | self._SetupForReplace()) |
| 3255 | |
| 3256 | # Remove one of the files, to generate a warning |
| 3257 | u_boot_fname1 = os.path.join(outdir, 'u-boot') |
| 3258 | os.remove(u_boot_fname1) |
| 3259 | |
| 3260 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3261 | control.ReplaceEntries(updated_fname, None, outdir, []) |
| 3262 | self.assertIn("Skipping entry '/u-boot' from missing file", |
Simon Glass | 6e02f7c | 2020-07-09 18:39:39 -0600 | [diff] [blame] | 3263 | stderr.getvalue()) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3264 | |
| 3265 | def testReplaceCmdMap(self): |
| 3266 | """Test replacing a file fron an image on the command line""" |
| 3267 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3268 | |
| 3269 | try: |
| 3270 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 3271 | |
| 3272 | fname = os.path.join(self._indir, 'update-u-boot.bin') |
| 3273 | expected = b'x' * len(U_BOOT_DATA) |
| 3274 | tools.WriteFile(fname, expected) |
| 3275 | |
| 3276 | self._DoBinman('replace', '-i', updated_fname, 'u-boot', |
| 3277 | '-f', fname, '-m') |
| 3278 | map_fname = os.path.join(tmpdir, 'image-updated.map') |
| 3279 | self.assertTrue(os.path.exists(map_fname)) |
| 3280 | finally: |
| 3281 | shutil.rmtree(tmpdir) |
| 3282 | |
| 3283 | def testReplaceNoEntryPaths(self): |
| 3284 | """Test replacing an entry without an entry path""" |
| 3285 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3286 | image_fname = tools.GetOutputFilename('image.bin') |
| 3287 | with self.assertRaises(ValueError) as e: |
| 3288 | control.ReplaceEntries(image_fname, 'fname', None, []) |
| 3289 | self.assertIn('Must specify an entry path to read with -f', |
| 3290 | str(e.exception)) |
| 3291 | |
| 3292 | def testReplaceTooManyEntryPaths(self): |
| 3293 | """Test extracting some entries""" |
| 3294 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3295 | image_fname = tools.GetOutputFilename('image.bin') |
| 3296 | with self.assertRaises(ValueError) as e: |
| 3297 | control.ReplaceEntries(image_fname, 'fname', None, ['a', 'b']) |
| 3298 | self.assertIn('Must specify exactly one entry path to write with -f', |
| 3299 | str(e.exception)) |
| 3300 | |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 3301 | def testPackReset16(self): |
| 3302 | """Test that an image with an x86 reset16 region can be created""" |
| 3303 | data = self._DoReadFile('144_x86_reset16.dts') |
| 3304 | self.assertEqual(X86_RESET16_DATA, data[:len(X86_RESET16_DATA)]) |
| 3305 | |
| 3306 | def testPackReset16Spl(self): |
| 3307 | """Test that an image with an x86 reset16-spl region can be created""" |
| 3308 | data = self._DoReadFile('145_x86_reset16_spl.dts') |
| 3309 | self.assertEqual(X86_RESET16_SPL_DATA, data[:len(X86_RESET16_SPL_DATA)]) |
| 3310 | |
| 3311 | def testPackReset16Tpl(self): |
| 3312 | """Test that an image with an x86 reset16-tpl region can be created""" |
| 3313 | data = self._DoReadFile('146_x86_reset16_tpl.dts') |
| 3314 | self.assertEqual(X86_RESET16_TPL_DATA, data[:len(X86_RESET16_TPL_DATA)]) |
| 3315 | |
Simon Glass | 232f90c | 2019-08-24 07:22:50 -0600 | [diff] [blame] | 3316 | def testPackIntelFit(self): |
| 3317 | """Test that an image with an Intel FIT and pointer can be created""" |
| 3318 | data = self._DoReadFile('147_intel_fit.dts') |
| 3319 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3320 | fit = data[16:32]; |
| 3321 | self.assertEqual(b'_FIT_ \x01\x00\x00\x00\x00\x01\x80}' , fit) |
| 3322 | ptr = struct.unpack('<i', data[0x40:0x44])[0] |
| 3323 | |
| 3324 | image = control.images['image'] |
| 3325 | entries = image.GetEntries() |
| 3326 | expected_ptr = entries['intel-fit'].image_pos - (1 << 32) |
| 3327 | self.assertEqual(expected_ptr, ptr) |
| 3328 | |
| 3329 | def testPackIntelFitMissing(self): |
| 3330 | """Test detection of a FIT pointer with not FIT region""" |
| 3331 | with self.assertRaises(ValueError) as e: |
| 3332 | self._DoReadFile('148_intel_fit_missing.dts') |
| 3333 | self.assertIn("'intel-fit-ptr' section must have an 'intel-fit' sibling", |
| 3334 | str(e.exception)) |
| 3335 | |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3336 | def _CheckSymbolsTplSection(self, dts, expected_vals): |
| 3337 | data = self._DoReadFile(dts) |
| 3338 | sym_values = struct.pack('<LQLL', *expected_vals) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3339 | upto1 = 4 + len(U_BOOT_SPL_DATA) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3340 | expected1 = tools.GetBytes(0xff, 4) + sym_values + U_BOOT_SPL_DATA[20:] |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3341 | self.assertEqual(expected1, data[:upto1]) |
| 3342 | |
| 3343 | upto2 = upto1 + 1 + len(U_BOOT_SPL_DATA) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3344 | expected2 = tools.GetBytes(0xff, 1) + sym_values + U_BOOT_SPL_DATA[20:] |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3345 | self.assertEqual(expected2, data[upto1:upto2]) |
| 3346 | |
Simon Glass | 4e353e2 | 2019-08-24 07:23:04 -0600 | [diff] [blame] | 3347 | upto3 = 0x34 + len(U_BOOT_DATA) |
| 3348 | expected3 = tools.GetBytes(0xff, 1) + U_BOOT_DATA |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3349 | self.assertEqual(expected3, data[upto2:upto3]) |
| 3350 | |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3351 | expected4 = sym_values + U_BOOT_TPL_DATA[20:] |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3352 | self.assertEqual(expected4, data[upto3:upto3 + len(U_BOOT_TPL_DATA)]) |
| 3353 | |
| 3354 | def testSymbolsTplSection(self): |
| 3355 | """Test binman can assign symbols embedded in U-Boot TPL in a section""" |
| 3356 | self._SetupSplElf('u_boot_binman_syms') |
| 3357 | self._SetupTplElf('u_boot_binman_syms') |
| 3358 | self._CheckSymbolsTplSection('149_symbols_tpl.dts', |
| 3359 | [0x04, 0x1c, 0x10 + 0x34, 0x04]) |
| 3360 | |
| 3361 | def testSymbolsTplSectionX86(self): |
| 3362 | """Test binman can assign symbols in a section with end-at-4gb""" |
| 3363 | self._SetupSplElf('u_boot_binman_syms_x86') |
| 3364 | self._SetupTplElf('u_boot_binman_syms_x86') |
| 3365 | self._CheckSymbolsTplSection('155_symbols_tpl_x86.dts', |
| 3366 | [0xffffff04, 0xffffff1c, 0xffffff34, |
| 3367 | 0x04]) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3368 | |
Simon Glass | 98c5957 | 2019-08-24 07:23:03 -0600 | [diff] [blame] | 3369 | def testPackX86RomIfwiSectiom(self): |
| 3370 | """Test that a section can be placed in an IFWI region""" |
| 3371 | self._SetupIfwi('fitimage.bin') |
| 3372 | data = self._DoReadFile('151_x86_rom_ifwi_section.dts') |
| 3373 | self._CheckIfwi(data) |
| 3374 | |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 3375 | def testPackFspM(self): |
| 3376 | """Test that an image with a FSP memory-init binary can be created""" |
| 3377 | data = self._DoReadFile('152_intel_fsp_m.dts') |
| 3378 | self.assertEqual(FSP_M_DATA, data[:len(FSP_M_DATA)]) |
| 3379 | |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 3380 | def testPackFspS(self): |
| 3381 | """Test that an image with a FSP silicon-init binary can be created""" |
| 3382 | data = self._DoReadFile('153_intel_fsp_s.dts') |
| 3383 | self.assertEqual(FSP_S_DATA, data[:len(FSP_S_DATA)]) |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 3384 | |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 3385 | def testPackFspT(self): |
| 3386 | """Test that an image with a FSP temp-ram-init binary can be created""" |
| 3387 | data = self._DoReadFile('154_intel_fsp_t.dts') |
| 3388 | self.assertEqual(FSP_T_DATA, data[:len(FSP_T_DATA)]) |
| 3389 | |
Simon Glass | 48f3aad | 2020-07-09 18:39:31 -0600 | [diff] [blame] | 3390 | def testMkimage(self): |
| 3391 | """Test using mkimage to build an image""" |
| 3392 | data = self._DoReadFile('156_mkimage.dts') |
| 3393 | |
| 3394 | # Just check that the data appears in the file somewhere |
| 3395 | self.assertIn(U_BOOT_SPL_DATA, data) |
| 3396 | |
Simon Glass | 5e56018 | 2020-07-09 18:39:36 -0600 | [diff] [blame] | 3397 | def testExtblob(self): |
| 3398 | """Test an image with an external blob""" |
| 3399 | data = self._DoReadFile('157_blob_ext.dts') |
| 3400 | self.assertEqual(REFCODE_DATA, data) |
| 3401 | |
| 3402 | def testExtblobMissing(self): |
| 3403 | """Test an image with a missing external blob""" |
| 3404 | with self.assertRaises(ValueError) as e: |
| 3405 | self._DoReadFile('158_blob_ext_missing.dts') |
| 3406 | self.assertIn("Filename 'missing-file' not found in input path", |
| 3407 | str(e.exception)) |
| 3408 | |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 3409 | def testExtblobMissingOk(self): |
| 3410 | """Test an image with an missing external blob that is allowed""" |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 3411 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3412 | self._DoTestFile('158_blob_ext_missing.dts', allow_missing=True) |
| 3413 | err = stderr.getvalue() |
| 3414 | self.assertRegex(err, "Image 'main-section'.*missing.*: blob-ext") |
| 3415 | |
| 3416 | def testExtblobMissingOkSect(self): |
| 3417 | """Test an image with an missing external blob that is allowed""" |
| 3418 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3419 | self._DoTestFile('159_blob_ext_missing_sect.dts', |
| 3420 | allow_missing=True) |
| 3421 | err = stderr.getvalue() |
| 3422 | self.assertRegex(err, "Image 'main-section'.*missing.*: " |
| 3423 | "blob-ext blob-ext2") |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 3424 | |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3425 | def testPackX86RomMeMissingDesc(self): |
| 3426 | """Test that an missing Intel descriptor entry is allowed""" |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3427 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 3428 | self._DoTestFile('164_x86_rom_me_missing.dts', allow_missing=True) |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3429 | err = stderr.getvalue() |
| 3430 | self.assertRegex(err, |
| 3431 | "Image 'main-section'.*missing.*: intel-descriptor") |
| 3432 | |
| 3433 | def testPackX86RomMissingIfwi(self): |
| 3434 | """Test that an x86 ROM with Integrated Firmware Image can be created""" |
| 3435 | self._SetupIfwi('fitimage.bin') |
| 3436 | pathname = os.path.join(self._indir, 'fitimage.bin') |
| 3437 | os.remove(pathname) |
| 3438 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3439 | self._DoTestFile('111_x86_rom_ifwi.dts', allow_missing=True) |
| 3440 | err = stderr.getvalue() |
| 3441 | self.assertRegex(err, "Image 'main-section'.*missing.*: intel-ifwi") |
| 3442 | |
Simon Glass | d70829a | 2020-07-09 18:39:42 -0600 | [diff] [blame] | 3443 | def testPackOverlap(self): |
| 3444 | """Test that zero-size overlapping regions are ignored""" |
| 3445 | self._DoTestFile('160_pack_overlap_zero.dts') |
| 3446 | |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3447 | def testSimpleFit(self): |
| 3448 | """Test an image with a FIT inside""" |
| 3449 | data = self._DoReadFile('161_fit.dts') |
| 3450 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3451 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3452 | fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3453 | |
| 3454 | # The data should be inside the FIT |
| 3455 | dtb = fdt.Fdt.FromData(fit_data) |
| 3456 | dtb.Scan() |
| 3457 | fnode = dtb.GetNode('/images/kernel') |
| 3458 | self.assertIn('data', fnode.props) |
| 3459 | |
| 3460 | fname = os.path.join(self._indir, 'fit_data.fit') |
| 3461 | tools.WriteFile(fname, fit_data) |
| 3462 | out = tools.Run('dumpimage', '-l', fname) |
| 3463 | |
| 3464 | # Check a few features to make sure the plumbing works. We don't need |
| 3465 | # to test the operation of mkimage or dumpimage here. First convert the |
| 3466 | # output into a dict where the keys are the fields printed by dumpimage |
| 3467 | # and the values are a list of values for each field |
| 3468 | lines = out.splitlines() |
| 3469 | |
| 3470 | # Converts "Compression: gzip compressed" into two groups: |
| 3471 | # 'Compression' and 'gzip compressed' |
| 3472 | re_line = re.compile(r'^ *([^:]*)(?:: *(.*))?$') |
| 3473 | vals = collections.defaultdict(list) |
| 3474 | for line in lines: |
| 3475 | mat = re_line.match(line) |
| 3476 | vals[mat.group(1)].append(mat.group(2)) |
| 3477 | |
| 3478 | self.assertEquals('FIT description: test-desc', lines[0]) |
| 3479 | self.assertIn('Created:', lines[1]) |
| 3480 | self.assertIn('Image 0 (kernel)', vals) |
| 3481 | self.assertIn('Hash value', vals) |
| 3482 | data_sizes = vals.get('Data Size') |
| 3483 | self.assertIsNotNone(data_sizes) |
| 3484 | self.assertEqual(2, len(data_sizes)) |
| 3485 | # Format is "4 Bytes = 0.00 KiB = 0.00 MiB" so take the first word |
| 3486 | self.assertEqual(len(U_BOOT_DATA), int(data_sizes[0].split()[0])) |
| 3487 | self.assertEqual(len(U_BOOT_SPL_DTB_DATA), int(data_sizes[1].split()[0])) |
| 3488 | |
| 3489 | def testFitExternal(self): |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame^] | 3490 | """Test an image with an FIT with external images""" |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3491 | data = self._DoReadFile('162_fit_external.dts') |
| 3492 | fit_data = data[len(U_BOOT_DATA):-2] # _testing is 2 bytes |
| 3493 | |
| 3494 | # The data should be outside the FIT |
| 3495 | dtb = fdt.Fdt.FromData(fit_data) |
| 3496 | dtb.Scan() |
| 3497 | fnode = dtb.GetNode('/images/kernel') |
| 3498 | self.assertNotIn('data', fnode.props) |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 3499 | |
Alper Nebi Yasak | 6aae239 | 2020-08-31 12:58:18 +0300 | [diff] [blame] | 3500 | def testSectionIgnoreHashSignature(self): |
| 3501 | """Test that sections ignore hash, signature nodes for its data""" |
| 3502 | data = self._DoReadFile('165_section_ignore_hash_signature.dts') |
| 3503 | expected = (U_BOOT_DATA + U_BOOT_DATA) |
| 3504 | self.assertEqual(expected, data) |
| 3505 | |
Alper Nebi Yasak | c503060 | 2020-08-31 12:58:19 +0300 | [diff] [blame] | 3506 | def testPadInSections(self): |
| 3507 | """Test pad-before, pad-after for entries in sections""" |
| 3508 | data = self._DoReadFile('166_pad_in_sections.dts') |
| 3509 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 3510 | U_BOOT_DATA + tools.GetBytes(ord('!'), 6) + |
| 3511 | U_BOOT_DATA) |
| 3512 | self.assertEqual(expected, data) |
| 3513 | |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 3514 | def testFitImageSubentryAlignment(self): |
| 3515 | """Test relative alignability of FIT image subentries""" |
| 3516 | entry_args = { |
| 3517 | 'test-id': TEXT_DATA, |
| 3518 | } |
| 3519 | data, _, _, _ = self._DoReadFileDtb('167_fit_image_subentry_alignment.dts', |
| 3520 | entry_args=entry_args) |
| 3521 | dtb = fdt.Fdt.FromData(data) |
| 3522 | dtb.Scan() |
| 3523 | |
| 3524 | node = dtb.GetNode('/images/kernel') |
| 3525 | data = dtb.GetProps(node)["data"].bytes |
| 3526 | align_pad = 0x10 - (len(U_BOOT_SPL_DATA) % 0x10) |
| 3527 | expected = (tools.GetBytes(0, 0x20) + U_BOOT_SPL_DATA + |
| 3528 | tools.GetBytes(0, align_pad) + U_BOOT_DATA) |
| 3529 | self.assertEqual(expected, data) |
| 3530 | |
| 3531 | node = dtb.GetNode('/images/fdt-1') |
| 3532 | data = dtb.GetProps(node)["data"].bytes |
| 3533 | expected = (U_BOOT_SPL_DTB_DATA + tools.GetBytes(0, 20) + |
| 3534 | tools.ToBytes(TEXT_DATA) + tools.GetBytes(0, 30) + |
| 3535 | U_BOOT_DTB_DATA) |
| 3536 | self.assertEqual(expected, data) |
| 3537 | |
| 3538 | def testFitExtblobMissingOk(self): |
| 3539 | """Test a FIT with a missing external blob that is allowed""" |
| 3540 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3541 | self._DoTestFile('168_fit_missing_blob.dts', |
| 3542 | allow_missing=True) |
| 3543 | err = stderr.getvalue() |
| 3544 | self.assertRegex(err, "Image 'main-section'.*missing.*: blob-ext") |
| 3545 | |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 3546 | def testBlobNamedByArgMissing(self): |
| 3547 | """Test handling of a missing entry arg""" |
| 3548 | with self.assertRaises(ValueError) as e: |
| 3549 | self._DoReadFile('068_blob_named_by_arg.dts') |
| 3550 | self.assertIn("Missing required properties/entry args: cros-ec-rw-path", |
| 3551 | str(e.exception)) |
| 3552 | |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 3553 | if __name__ == "__main__": |
| 3554 | unittest.main() |