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 | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 73 | COMPRESS_DATA_BIG = COMPRESS_DATA * 2 |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 74 | REFCODE_DATA = b'refcode' |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 75 | FSP_M_DATA = b'fsp_m' |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 76 | FSP_S_DATA = b'fsp_s' |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 77 | FSP_T_DATA = b'fsp_t' |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 78 | ATF_BL31_DATA = b'bl31' |
Bin Meng | c0b1574 | 2021-05-10 20:23:33 +0800 | [diff] [blame] | 79 | OPENSBI_DATA = b'opensbi' |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 80 | SCP_DATA = b'scp' |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 81 | TEST_FDT1_DATA = b'fdt1' |
| 82 | TEST_FDT2_DATA = b'test-fdt2' |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 83 | ENV_DATA = b'var1=1\nvar2="2"' |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 84 | |
| 85 | # Subdirectory of the input dir to use to put test FDTs |
| 86 | TEST_FDT_SUBDIR = 'fdts' |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 87 | |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 88 | # The expected size for the device tree in some tests |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 89 | EXTRACT_DTB_SIZE = 0x3c9 |
| 90 | |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 91 | # Properties expected to be in the device tree when update_dtb is used |
| 92 | BASE_DTB_PROPS = ['offset', 'size', 'image-pos'] |
| 93 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 94 | # Extra properties expected to be in the device tree when allow-repack is used |
| 95 | REPACK_DTB_PROPS = ['orig-offset', 'orig-size'] |
| 96 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 97 | |
| 98 | class TestFunctional(unittest.TestCase): |
| 99 | """Functional tests for binman |
| 100 | |
| 101 | Most of these use a sample .dts file to build an image and then check |
| 102 | that it looks correct. The sample files are in the test/ subdirectory |
| 103 | and are numbered. |
| 104 | |
| 105 | For each entry type a very small test file is created using fixed |
| 106 | string contents. This makes it easy to test that things look right, and |
| 107 | debug problems. |
| 108 | |
| 109 | In some cases a 'real' file must be used - these are also supplied in |
| 110 | the test/ diurectory. |
| 111 | """ |
| 112 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 113 | def setUpClass(cls): |
Simon Glass | b339326 | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 114 | global entry |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 115 | from binman import entry |
Simon Glass | b339326 | 2017-11-12 21:52:20 -0700 | [diff] [blame] | 116 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 117 | # Handle the case where argv[0] is 'python' |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 118 | cls._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 119 | cls._binman_pathname = os.path.join(cls._binman_dir, 'binman') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 120 | |
| 121 | # Create a temporary directory for input files |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 122 | cls._indir = tempfile.mkdtemp(prefix='binmant.') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 123 | |
| 124 | # Create some test files |
| 125 | TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA) |
| 126 | TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA) |
| 127 | TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA) |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 128 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 129 | TestFunctional._MakeInputFile('blobfile', BLOB_DATA) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 130 | TestFunctional._MakeInputFile('me.bin', ME_DATA) |
| 131 | TestFunctional._MakeInputFile('vga.bin', VGA_DATA) |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 132 | cls._ResetDtbs() |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 133 | |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 134 | TestFunctional._MakeInputFile('u-boot-br.bin', PPC_MPC85XX_BR_DATA) |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 135 | |
Simon Glass | abab18c | 2019-08-24 07:22:49 -0600 | [diff] [blame] | 136 | TestFunctional._MakeInputFile('u-boot-x86-start16.bin', X86_START16_DATA) |
| 137 | TestFunctional._MakeInputFile('spl/u-boot-x86-start16-spl.bin', |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 138 | X86_START16_SPL_DATA) |
Simon Glass | abab18c | 2019-08-24 07:22:49 -0600 | [diff] [blame] | 139 | TestFunctional._MakeInputFile('tpl/u-boot-x86-start16-tpl.bin', |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 140 | X86_START16_TPL_DATA) |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 141 | |
| 142 | TestFunctional._MakeInputFile('u-boot-x86-reset16.bin', |
| 143 | X86_RESET16_DATA) |
| 144 | TestFunctional._MakeInputFile('spl/u-boot-x86-reset16-spl.bin', |
| 145 | X86_RESET16_SPL_DATA) |
| 146 | TestFunctional._MakeInputFile('tpl/u-boot-x86-reset16-tpl.bin', |
| 147 | X86_RESET16_TPL_DATA) |
| 148 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 149 | TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA) |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 150 | TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin', |
| 151 | U_BOOT_SPL_NODTB_DATA) |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 152 | TestFunctional._MakeInputFile('tpl/u-boot-tpl-nodtb.bin', |
| 153 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 154 | TestFunctional._MakeInputFile('fsp.bin', FSP_DATA) |
| 155 | TestFunctional._MakeInputFile('cmc.bin', CMC_DATA) |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 156 | TestFunctional._MakeInputFile('vbt.bin', VBT_DATA) |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 157 | TestFunctional._MakeInputFile('mrc.bin', MRC_DATA) |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 158 | TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 159 | TestFunctional._MakeInputDir('devkeys') |
| 160 | TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA) |
Simon Glass | 41902e4 | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 161 | TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA) |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 162 | TestFunctional._MakeInputFile('fsp_m.bin', FSP_M_DATA) |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 163 | TestFunctional._MakeInputFile('fsp_s.bin', FSP_S_DATA) |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 164 | TestFunctional._MakeInputFile('fsp_t.bin', FSP_T_DATA) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 165 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 166 | cls._elf_testdir = os.path.join(cls._indir, 'elftest') |
| 167 | elf_test.BuildElfTestFiles(cls._elf_testdir) |
| 168 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 169 | # ELF file with a '_dt_ucode_base_size' symbol |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 170 | TestFunctional._MakeInputFile('u-boot', |
| 171 | tools.ReadFile(cls.ElfTestFile('u_boot_ucode_ptr'))) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 172 | |
| 173 | # Intel flash descriptor file |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 174 | cls._SetupDescriptor() |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 175 | |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 176 | shutil.copytree(cls.TestFile('files'), |
| 177 | os.path.join(cls._indir, 'files')) |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 178 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 179 | TestFunctional._MakeInputFile('compress', COMPRESS_DATA) |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 180 | TestFunctional._MakeInputFile('compress_big', COMPRESS_DATA_BIG) |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 181 | TestFunctional._MakeInputFile('bl31.bin', ATF_BL31_DATA) |
Bin Meng | c0b1574 | 2021-05-10 20:23:33 +0800 | [diff] [blame] | 182 | TestFunctional._MakeInputFile('fw_dynamic.bin', OPENSBI_DATA) |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 183 | TestFunctional._MakeInputFile('scp.bin', SCP_DATA) |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 184 | |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 185 | # Add a few .dtb files for testing |
| 186 | TestFunctional._MakeInputFile('%s/test-fdt1.dtb' % TEST_FDT_SUBDIR, |
| 187 | TEST_FDT1_DATA) |
| 188 | TestFunctional._MakeInputFile('%s/test-fdt2.dtb' % TEST_FDT_SUBDIR, |
| 189 | TEST_FDT2_DATA) |
| 190 | |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 191 | TestFunctional._MakeInputFile('env.txt', ENV_DATA) |
| 192 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 193 | # Travis-CI may have an old lz4 |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 194 | cls.have_lz4 = True |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 195 | try: |
| 196 | tools.Run('lz4', '--no-frame-crc', '-c', |
Simon Glass | cc311ac | 2019-10-31 07:42:50 -0600 | [diff] [blame] | 197 | os.path.join(cls._indir, 'u-boot.bin'), binary=True) |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 198 | except: |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 199 | cls.have_lz4 = False |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 200 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 201 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 202 | def tearDownClass(cls): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 203 | """Remove the temporary input directory and its contents""" |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 204 | if cls.preserve_indir: |
| 205 | print('Preserving input dir: %s' % cls._indir) |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 206 | else: |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 207 | if cls._indir: |
| 208 | shutil.rmtree(cls._indir) |
| 209 | cls._indir = None |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 210 | |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 211 | @classmethod |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 212 | def setup_test_args(cls, preserve_indir=False, preserve_outdirs=False, |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 213 | toolpath=None, verbosity=None): |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 214 | """Accept arguments controlling test execution |
| 215 | |
| 216 | Args: |
| 217 | preserve_indir: Preserve the shared input directory used by all |
| 218 | tests in this class. |
| 219 | preserve_outdir: Preserve the output directories used by tests. Each |
| 220 | test has its own, so this is normally only useful when running a |
| 221 | single test. |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 222 | toolpath: ist of paths to use for tools |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 223 | """ |
| 224 | cls.preserve_indir = preserve_indir |
| 225 | cls.preserve_outdirs = preserve_outdirs |
Simon Glass | cebfab2 | 2019-07-08 13:18:50 -0600 | [diff] [blame] | 226 | cls.toolpath = toolpath |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 227 | cls.verbosity = verbosity |
Simon Glass | 1c420c9 | 2019-07-08 13:18:49 -0600 | [diff] [blame] | 228 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 229 | def _CheckLz4(self): |
| 230 | if not self.have_lz4: |
| 231 | self.skipTest('lz4 --no-frame-crc not available') |
| 232 | |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 233 | def _CleanupOutputDir(self): |
| 234 | """Remove the temporary output directory""" |
| 235 | if self.preserve_outdirs: |
| 236 | print('Preserving output dir: %s' % tools.outdir) |
| 237 | else: |
| 238 | tools._FinaliseForTest() |
| 239 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 240 | def setUp(self): |
| 241 | # Enable this to turn on debugging output |
| 242 | # tout.Init(tout.DEBUG) |
| 243 | command.test_result = None |
| 244 | |
| 245 | def tearDown(self): |
| 246 | """Remove the temporary output directory""" |
Simon Glass | ee9d10d | 2019-07-20 12:24:09 -0600 | [diff] [blame] | 247 | self._CleanupOutputDir() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 248 | |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 249 | def _SetupImageInTmpdir(self): |
| 250 | """Set up the output image in a new temporary directory |
| 251 | |
| 252 | This is used when an image has been generated in the output directory, |
| 253 | but we want to run binman again. This will create a new output |
| 254 | directory and fail to delete the original one. |
| 255 | |
| 256 | This creates a new temporary directory, copies the image to it (with a |
| 257 | new name) and removes the old output directory. |
| 258 | |
| 259 | Returns: |
| 260 | Tuple: |
| 261 | Temporary directory to use |
| 262 | New image filename |
| 263 | """ |
| 264 | image_fname = tools.GetOutputFilename('image.bin') |
| 265 | tmpdir = tempfile.mkdtemp(prefix='binman.') |
| 266 | updated_fname = os.path.join(tmpdir, 'image-updated.bin') |
| 267 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
| 268 | self._CleanupOutputDir() |
| 269 | return tmpdir, updated_fname |
| 270 | |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 271 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 272 | def _ResetDtbs(cls): |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 273 | TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA) |
| 274 | TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA) |
| 275 | TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA) |
| 276 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 277 | def _RunBinman(self, *args, **kwargs): |
| 278 | """Run binman using the command line |
| 279 | |
| 280 | Args: |
| 281 | Arguments to pass, as a list of strings |
| 282 | kwargs: Arguments to pass to Command.RunPipe() |
| 283 | """ |
| 284 | result = command.RunPipe([[self._binman_pathname] + list(args)], |
| 285 | capture=True, capture_stderr=True, raise_on_error=False) |
| 286 | if result.return_code and kwargs.get('raise_on_error', True): |
| 287 | raise Exception("Error running '%s': %s" % (' '.join(args), |
| 288 | result.stdout + result.stderr)) |
| 289 | return result |
| 290 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 291 | def _DoBinman(self, *argv): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 292 | """Run binman using directly (in the same process) |
| 293 | |
| 294 | Args: |
| 295 | Arguments to pass, as a list of strings |
| 296 | Returns: |
| 297 | Return value (0 for success) |
| 298 | """ |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 299 | argv = list(argv) |
| 300 | args = cmdline.ParseArgs(argv) |
| 301 | args.pager = 'binman-invalid-pager' |
| 302 | args.build_dir = self._indir |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 303 | |
| 304 | # For testing, you can force an increase in verbosity here |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 305 | # args.verbosity = tout.DEBUG |
| 306 | return control.Binman(args) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 307 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 308 | def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False, |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 309 | entry_args=None, images=None, use_real_dtb=False, |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 310 | use_expanded=False, verbosity=None, allow_missing=False, |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 311 | extra_indirs=None, threads=None, |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame^] | 312 | test_section_timeout=False, update_fdt_in_elf=None): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 313 | """Run binman with a given test file |
| 314 | |
| 315 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 316 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 317 | debug: True to enable debugging output |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 318 | map: True to output map files for the images |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 319 | 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] | 320 | tree before packing it into the image |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 321 | entry_args: Dict of entry args to supply to binman |
| 322 | key: arg name |
| 323 | value: value of that arg |
| 324 | images: List of image names to build |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 325 | use_real_dtb: True to use the test file as the contents of |
| 326 | the u-boot-dtb entry. Normally this is not needed and the |
| 327 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 328 | But in some test we need the real contents. |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 329 | use_expanded: True to use expanded entries where available, e.g. |
| 330 | 'u-boot-expanded' instead of 'u-boot' |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 331 | verbosity: Verbosity level to use (0-3, None=don't set it) |
| 332 | allow_missing: Set the '--allow-missing' flag so that missing |
| 333 | external binaries just produce a warning instead of an error |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 334 | extra_indirs: Extra input directories to add using -I |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 335 | threads: Number of threads to use (None for default, 0 for |
| 336 | single-threaded) |
Simon Glass | 9a79840 | 2021-11-03 21:09:17 -0600 | [diff] [blame] | 337 | test_section_timeout: True to force the first time to timeout, as |
| 338 | used in testThreadTimeout() |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame^] | 339 | update_fdt_in_elf: Value to pass with --update-fdt-in-elf=xxx |
Simon Glass | 9a79840 | 2021-11-03 21:09:17 -0600 | [diff] [blame] | 340 | |
| 341 | Returns: |
| 342 | int return code, 0 on success |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 343 | """ |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 344 | args = [] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 345 | if debug: |
| 346 | args.append('-D') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 347 | if verbosity is not None: |
| 348 | args.append('-v%d' % verbosity) |
| 349 | elif self.verbosity: |
| 350 | args.append('-v%d' % self.verbosity) |
| 351 | if self.toolpath: |
| 352 | for path in self.toolpath: |
| 353 | args += ['--toolpath', path] |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 354 | if threads is not None: |
| 355 | args.append('-T%d' % threads) |
| 356 | if test_section_timeout: |
| 357 | args.append('--test-section-timeout') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 358 | args += ['build', '-p', '-I', self._indir, '-d', self.TestFile(fname)] |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 359 | if map: |
| 360 | args.append('-m') |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 361 | if update_dtb: |
Simon Glass | 38a411c | 2019-07-08 13:18:47 -0600 | [diff] [blame] | 362 | args.append('-u') |
Simon Glass | 3140201 | 2018-09-14 04:57:23 -0600 | [diff] [blame] | 363 | if not use_real_dtb: |
| 364 | args.append('--fake-dtb') |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 365 | if not use_expanded: |
| 366 | args.append('--no-expanded') |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 367 | if entry_args: |
Simon Glass | 5f3645b | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 368 | for arg, value in entry_args.items(): |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 369 | args.append('-a%s=%s' % (arg, value)) |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 370 | if allow_missing: |
| 371 | args.append('-M') |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame^] | 372 | if update_fdt_in_elf: |
| 373 | args += ['--update-fdt-in-elf', update_fdt_in_elf] |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 374 | if images: |
| 375 | for image in images: |
| 376 | args += ['-i', image] |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 377 | if extra_indirs: |
| 378 | for indir in extra_indirs: |
| 379 | args += ['-I', indir] |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 380 | return self._DoBinman(*args) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 381 | |
| 382 | def _SetupDtb(self, fname, outfile='u-boot.dtb'): |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 383 | """Set up a new test device-tree file |
| 384 | |
| 385 | The given file is compiled and set up as the device tree to be used |
| 386 | for ths test. |
| 387 | |
| 388 | Args: |
| 389 | fname: Filename of .dts file to read |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 390 | outfile: Output filename for compiled device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 391 | |
| 392 | Returns: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 393 | Contents of device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 394 | """ |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 395 | tmpdir = tempfile.mkdtemp(prefix='binmant.') |
| 396 | dtb = fdt_util.EnsureCompiled(self.TestFile(fname), tmpdir) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 397 | with open(dtb, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 398 | data = fd.read() |
| 399 | TestFunctional._MakeInputFile(outfile, data) |
Simon Glass | b8d2daa | 2019-07-20 12:23:49 -0600 | [diff] [blame] | 400 | shutil.rmtree(tmpdir) |
Simon Glass | 752e755 | 2018-10-01 21:12:41 -0600 | [diff] [blame] | 401 | return data |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 402 | |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 403 | def _GetDtbContentsForSplTpl(self, dtb_data, name): |
| 404 | """Create a version of the main DTB for SPL or SPL |
| 405 | |
| 406 | For testing we don't actually have different versions of the DTB. With |
| 407 | U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests |
| 408 | we don't normally have any unwanted nodes. |
| 409 | |
| 410 | We still want the DTBs for SPL and TPL to be different though, since |
| 411 | otherwise it is confusing to know which one we are looking at. So add |
| 412 | an 'spl' or 'tpl' property to the top-level node. |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 413 | |
| 414 | Args: |
| 415 | dtb_data: dtb data to modify (this should be a value devicetree) |
| 416 | name: Name of a new property to add |
| 417 | |
| 418 | Returns: |
| 419 | New dtb data with the property added |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 420 | """ |
| 421 | dtb = fdt.Fdt.FromData(dtb_data) |
| 422 | dtb.Scan() |
| 423 | dtb.GetNode('/binman').AddZeroProp(name) |
| 424 | dtb.Sync(auto_resize=True) |
| 425 | dtb.Pack() |
| 426 | return dtb.GetContents() |
| 427 | |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 428 | def _DoReadFileDtb(self, fname, use_real_dtb=False, use_expanded=False, |
| 429 | map=False, update_dtb=False, entry_args=None, |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 430 | reset_dtbs=True, extra_indirs=None, threads=None): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 431 | """Run binman and return the resulting image |
| 432 | |
| 433 | This runs binman with a given test file and then reads the resulting |
| 434 | output file. It is a shortcut function since most tests need to do |
| 435 | these steps. |
| 436 | |
| 437 | Raises an assertion failure if binman returns a non-zero exit code. |
| 438 | |
| 439 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 440 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 441 | use_real_dtb: True to use the test file as the contents of |
| 442 | the u-boot-dtb entry. Normally this is not needed and the |
| 443 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 444 | But in some test we need the real contents. |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 445 | use_expanded: True to use expanded entries where available, e.g. |
| 446 | 'u-boot-expanded' instead of 'u-boot' |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 447 | map: True to output map files for the images |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 448 | 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] | 449 | tree before packing it into the image |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 450 | entry_args: Dict of entry args to supply to binman |
| 451 | key: arg name |
| 452 | value: value of that arg |
| 453 | reset_dtbs: With use_real_dtb the test dtb is overwritten by this |
| 454 | function. If reset_dtbs is True, then the original test dtb |
| 455 | is written back before this function finishes |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 456 | extra_indirs: Extra input directories to add using -I |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 457 | threads: Number of threads to use (None for default, 0 for |
| 458 | single-threaded) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 459 | |
| 460 | Returns: |
| 461 | Tuple: |
| 462 | Resulting image contents |
| 463 | Device tree contents |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 464 | Map data showing contents of image (or None if none) |
Simon Glass | def77b5 | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 465 | Output device tree binary filename ('u-boot.dtb' path) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 466 | """ |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 467 | dtb_data = None |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 468 | # Use the compiled test file as the u-boot-dtb input |
| 469 | if use_real_dtb: |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 470 | dtb_data = self._SetupDtb(fname) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 471 | |
| 472 | # For testing purposes, make a copy of the DT for SPL and TPL. Add |
| 473 | # a node indicating which it is, so aid verification. |
| 474 | for name in ['spl', 'tpl']: |
| 475 | dtb_fname = '%s/u-boot-%s.dtb' % (name, name) |
| 476 | outfile = os.path.join(self._indir, dtb_fname) |
| 477 | TestFunctional._MakeInputFile(dtb_fname, |
| 478 | self._GetDtbContentsForSplTpl(dtb_data, name)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 479 | |
| 480 | try: |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 481 | retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb, |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 482 | entry_args=entry_args, use_real_dtb=use_real_dtb, |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 483 | use_expanded=use_expanded, extra_indirs=extra_indirs, |
| 484 | threads=threads) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 485 | self.assertEqual(0, retcode) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 486 | out_dtb_fname = tools.GetOutputFilename('u-boot.dtb.out') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 487 | |
| 488 | # Find the (only) image, read it and return its contents |
| 489 | image = control.images['image'] |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 490 | image_fname = tools.GetOutputFilename('image.bin') |
| 491 | self.assertTrue(os.path.exists(image_fname)) |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 492 | if map: |
| 493 | map_fname = tools.GetOutputFilename('image.map') |
| 494 | with open(map_fname) as fd: |
| 495 | map_data = fd.read() |
| 496 | else: |
| 497 | map_data = None |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 498 | with open(image_fname, 'rb') as fd: |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 499 | return fd.read(), dtb_data, map_data, out_dtb_fname |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 500 | finally: |
| 501 | # Put the test file back |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 502 | if reset_dtbs and use_real_dtb: |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 503 | self._ResetDtbs() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 504 | |
Simon Glass | 5b4bce3 | 2019-07-08 14:25:26 -0600 | [diff] [blame] | 505 | def _DoReadFileRealDtb(self, fname): |
| 506 | """Run binman with a real .dtb file and return the resulting data |
| 507 | |
| 508 | Args: |
| 509 | fname: DT source filename to use (e.g. 082_fdt_update_all.dts) |
| 510 | |
| 511 | Returns: |
| 512 | Resulting image contents |
| 513 | """ |
| 514 | return self._DoReadFileDtb(fname, use_real_dtb=True, update_dtb=True)[0] |
| 515 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 516 | def _DoReadFile(self, fname, use_real_dtb=False): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 517 | """Helper function which discards the device-tree binary |
| 518 | |
| 519 | Args: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 520 | fname: Device-tree source filename to use (e.g. 005_simple.dts) |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 521 | use_real_dtb: True to use the test file as the contents of |
| 522 | the u-boot-dtb entry. Normally this is not needed and the |
| 523 | test contents (the U_BOOT_DTB_DATA string) can be used. |
| 524 | But in some test we need the real contents. |
Simon Glass | def77b5 | 2018-07-17 13:25:27 -0600 | [diff] [blame] | 525 | |
| 526 | Returns: |
| 527 | Resulting image contents |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 528 | """ |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 529 | return self._DoReadFileDtb(fname, use_real_dtb)[0] |
| 530 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 531 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 532 | def _MakeInputFile(cls, fname, contents): |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 533 | """Create a new test input file, creating directories as needed |
| 534 | |
| 535 | Args: |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 536 | fname: Filename to create |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 537 | contents: File contents to write in to the file |
| 538 | Returns: |
| 539 | Full pathname of file created |
| 540 | """ |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 541 | pathname = os.path.join(cls._indir, fname) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 542 | dirname = os.path.dirname(pathname) |
| 543 | if dirname and not os.path.exists(dirname): |
| 544 | os.makedirs(dirname) |
| 545 | with open(pathname, 'wb') as fd: |
| 546 | fd.write(contents) |
| 547 | return pathname |
| 548 | |
| 549 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 550 | def _MakeInputDir(cls, dirname): |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 551 | """Create a new test input directory, creating directories as needed |
| 552 | |
| 553 | Args: |
| 554 | dirname: Directory name to create |
| 555 | |
| 556 | Returns: |
| 557 | Full pathname of directory created |
| 558 | """ |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 559 | pathname = os.path.join(cls._indir, dirname) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 560 | if not os.path.exists(pathname): |
| 561 | os.makedirs(pathname) |
| 562 | return pathname |
| 563 | |
| 564 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 565 | def _SetupSplElf(cls, src_fname='bss_data'): |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 566 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 567 | |
| 568 | Args: |
| 569 | Filename of ELF file to use as SPL |
| 570 | """ |
Simon Glass | 93a806f | 2019-08-24 07:22:59 -0600 | [diff] [blame] | 571 | TestFunctional._MakeInputFile('spl/u-boot-spl', |
| 572 | tools.ReadFile(cls.ElfTestFile(src_fname))) |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 573 | |
| 574 | @classmethod |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 575 | def _SetupTplElf(cls, src_fname='bss_data'): |
| 576 | """Set up an ELF file with a '_dt_ucode_base_size' symbol |
| 577 | |
| 578 | Args: |
| 579 | Filename of ELF file to use as TPL |
| 580 | """ |
| 581 | TestFunctional._MakeInputFile('tpl/u-boot-tpl', |
| 582 | tools.ReadFile(cls.ElfTestFile(src_fname))) |
| 583 | |
| 584 | @classmethod |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 585 | def _SetupDescriptor(cls): |
| 586 | with open(cls.TestFile('descriptor.bin'), 'rb') as fd: |
| 587 | TestFunctional._MakeInputFile('descriptor.bin', fd.read()) |
| 588 | |
| 589 | @classmethod |
Simon Glass | 862f8e2 | 2019-08-24 07:22:43 -0600 | [diff] [blame] | 590 | def TestFile(cls, fname): |
| 591 | return os.path.join(cls._binman_dir, 'test', fname) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 592 | |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 593 | @classmethod |
| 594 | def ElfTestFile(cls, fname): |
| 595 | return os.path.join(cls._elf_testdir, fname) |
| 596 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 597 | def AssertInList(self, grep_list, target): |
| 598 | """Assert that at least one of a list of things is in a target |
| 599 | |
| 600 | Args: |
| 601 | grep_list: List of strings to check |
| 602 | target: Target string |
| 603 | """ |
| 604 | for grep in grep_list: |
| 605 | if grep in target: |
| 606 | return |
Simon Glass | 848cdb5 | 2019-05-17 22:00:50 -0600 | [diff] [blame] | 607 | self.fail("Error: '%s' not found in '%s'" % (grep_list, target)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 608 | |
| 609 | def CheckNoGaps(self, entries): |
| 610 | """Check that all entries fit together without gaps |
| 611 | |
| 612 | Args: |
| 613 | entries: List of entries to check |
| 614 | """ |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 615 | offset = 0 |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 616 | for entry in entries.values(): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 617 | self.assertEqual(offset, entry.offset) |
| 618 | offset += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 619 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 620 | def GetFdtLen(self, dtb): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 621 | """Get the totalsize field from a device-tree binary |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 622 | |
| 623 | Args: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 624 | dtb: Device-tree binary contents |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 625 | |
| 626 | Returns: |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 627 | Total size of device-tree binary, from the header |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 628 | """ |
| 629 | return struct.unpack('>L', dtb[4:8])[0] |
| 630 | |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 631 | def _GetPropTree(self, dtb, prop_names, prefix='/binman/'): |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 632 | def AddNode(node, path): |
| 633 | if node.name != '/': |
| 634 | path += '/' + node.name |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 635 | for prop in node.props.values(): |
| 636 | if prop.name in prop_names: |
| 637 | prop_path = path + ':' + prop.name |
| 638 | tree[prop_path[len(prefix):]] = fdt_util.fdt32_to_cpu( |
| 639 | prop.value) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 640 | for subnode in node.subnodes: |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 641 | AddNode(subnode, path) |
| 642 | |
| 643 | tree = {} |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 644 | AddNode(dtb.GetRoot(), '') |
| 645 | return tree |
| 646 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 647 | def testRun(self): |
| 648 | """Test a basic run with valid args""" |
| 649 | result = self._RunBinman('-h') |
| 650 | |
| 651 | def testFullHelp(self): |
| 652 | """Test that the full help is displayed with -H""" |
| 653 | result = self._RunBinman('-H') |
Simon Glass | 75ead66 | 2021-03-18 20:25:13 +1300 | [diff] [blame] | 654 | help_file = os.path.join(self._binman_dir, 'README.rst') |
Tom Rini | c3c0b6d | 2018-01-16 15:29:50 -0500 | [diff] [blame] | 655 | # Remove possible extraneous strings |
| 656 | extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n' |
| 657 | gothelp = result.stdout.replace(extra, '') |
| 658 | self.assertEqual(len(gothelp), os.path.getsize(help_file)) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 659 | self.assertEqual(0, len(result.stderr)) |
| 660 | self.assertEqual(0, result.return_code) |
| 661 | |
| 662 | def testFullHelpInternal(self): |
| 663 | """Test that the full help is displayed with -H""" |
| 664 | try: |
| 665 | command.test_result = command.CommandResult() |
| 666 | result = self._DoBinman('-H') |
Simon Glass | 75ead66 | 2021-03-18 20:25:13 +1300 | [diff] [blame] | 667 | help_file = os.path.join(self._binman_dir, 'README.rst') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 668 | finally: |
| 669 | command.test_result = None |
| 670 | |
| 671 | def testHelp(self): |
| 672 | """Test that the basic help is displayed with -h""" |
| 673 | result = self._RunBinman('-h') |
| 674 | self.assertTrue(len(result.stdout) > 200) |
| 675 | self.assertEqual(0, len(result.stderr)) |
| 676 | self.assertEqual(0, result.return_code) |
| 677 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 678 | def testBoard(self): |
| 679 | """Test that we can run it with a specific board""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 680 | self._SetupDtb('005_simple.dts', 'sandbox/u-boot.dtb') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 681 | TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA) |
Simon Glass | ed93067 | 2021-03-18 20:25:05 +1300 | [diff] [blame] | 682 | result = self._DoBinman('build', '-n', '-b', 'sandbox') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 683 | self.assertEqual(0, result) |
| 684 | |
| 685 | def testNeedBoard(self): |
| 686 | """Test that we get an error when no board ius supplied""" |
| 687 | with self.assertRaises(ValueError) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 688 | result = self._DoBinman('build') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 689 | self.assertIn("Must provide a board to process (use -b <board>)", |
| 690 | str(e.exception)) |
| 691 | |
| 692 | def testMissingDt(self): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 693 | """Test that an invalid device-tree file generates an error""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 694 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 695 | self._RunBinman('build', '-d', 'missing_file') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 696 | # We get one error from libfdt, and a different one from fdtget. |
| 697 | self.AssertInList(["Couldn't open blob from 'missing_file'", |
| 698 | 'No such file or directory'], str(e.exception)) |
| 699 | |
| 700 | def testBrokenDt(self): |
Simon Glass | 1e32400 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 701 | """Test that an invalid device-tree source file generates an error |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 702 | |
| 703 | Since this is a source file it should be compiled and the error |
| 704 | will come from the device-tree compiler (dtc). |
| 705 | """ |
| 706 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 707 | self._RunBinman('build', '-d', self.TestFile('001_invalid.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 708 | self.assertIn("FATAL ERROR: Unable to parse input tree", |
| 709 | str(e.exception)) |
| 710 | |
| 711 | def testMissingNode(self): |
| 712 | """Test that a device tree without a 'binman' node generates an error""" |
| 713 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 714 | self._DoBinman('build', '-d', self.TestFile('002_missing_node.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 715 | self.assertIn("does not have a 'binman' node", str(e.exception)) |
| 716 | |
| 717 | def testEmpty(self): |
| 718 | """Test that an empty binman node works OK (i.e. does nothing)""" |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 719 | result = self._RunBinman('build', '-d', self.TestFile('003_empty.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 720 | self.assertEqual(0, len(result.stderr)) |
| 721 | self.assertEqual(0, result.return_code) |
| 722 | |
| 723 | def testInvalidEntry(self): |
| 724 | """Test that an invalid entry is flagged""" |
| 725 | with self.assertRaises(Exception) as e: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 726 | result = self._RunBinman('build', '-d', |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 727 | self.TestFile('004_invalid_entry.dts')) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 728 | self.assertIn("Unknown entry type 'not-a-valid-type' in node " |
| 729 | "'/binman/not-a-valid-type'", str(e.exception)) |
| 730 | |
| 731 | def testSimple(self): |
| 732 | """Test a simple binman with a single file""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 733 | data = self._DoReadFile('005_simple.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 734 | self.assertEqual(U_BOOT_DATA, data) |
| 735 | |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 736 | def testSimpleDebug(self): |
| 737 | """Test a simple binman run with debugging enabled""" |
Simon Glass | 52d0621 | 2019-07-08 14:25:53 -0600 | [diff] [blame] | 738 | self._DoTestFile('005_simple.dts', debug=True) |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 739 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 740 | def testDual(self): |
| 741 | """Test that we can handle creating two images |
| 742 | |
| 743 | This also tests image padding. |
| 744 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 745 | retcode = self._DoTestFile('006_dual_image.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 746 | self.assertEqual(0, retcode) |
| 747 | |
| 748 | image = control.images['image1'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 749 | self.assertEqual(len(U_BOOT_DATA), image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 750 | fname = tools.GetOutputFilename('image1.bin') |
| 751 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 752 | with open(fname, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 753 | data = fd.read() |
| 754 | self.assertEqual(U_BOOT_DATA, data) |
| 755 | |
| 756 | image = control.images['image2'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 757 | self.assertEqual(3 + len(U_BOOT_DATA) + 5, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 758 | fname = tools.GetOutputFilename('image2.bin') |
| 759 | self.assertTrue(os.path.exists(fname)) |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 760 | with open(fname, 'rb') as fd: |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 761 | data = fd.read() |
| 762 | self.assertEqual(U_BOOT_DATA, data[3:7]) |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 763 | self.assertEqual(tools.GetBytes(0, 3), data[:3]) |
| 764 | self.assertEqual(tools.GetBytes(0, 5), data[7:]) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 765 | |
| 766 | def testBadAlign(self): |
| 767 | """Test that an invalid alignment value is detected""" |
| 768 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 769 | self._DoTestFile('007_bad_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 770 | self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power " |
| 771 | "of two", str(e.exception)) |
| 772 | |
| 773 | def testPackSimple(self): |
| 774 | """Test that packing works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 775 | retcode = self._DoTestFile('008_pack.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 776 | self.assertEqual(0, retcode) |
| 777 | self.assertIn('image', control.images) |
| 778 | image = control.images['image'] |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 779 | entries = image.GetEntries() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 780 | self.assertEqual(5, len(entries)) |
| 781 | |
| 782 | # First u-boot |
| 783 | self.assertIn('u-boot', entries) |
| 784 | entry = entries['u-boot'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 785 | self.assertEqual(0, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 786 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 787 | |
| 788 | # Second u-boot, aligned to 16-byte boundary |
| 789 | self.assertIn('u-boot-align', entries) |
| 790 | entry = entries['u-boot-align'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 791 | self.assertEqual(16, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 792 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 793 | |
| 794 | # Third u-boot, size 23 bytes |
| 795 | self.assertIn('u-boot-size', entries) |
| 796 | entry = entries['u-boot-size'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 797 | self.assertEqual(20, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 798 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 799 | self.assertEqual(23, entry.size) |
| 800 | |
| 801 | # Fourth u-boot, placed immediate after the above |
| 802 | self.assertIn('u-boot-next', entries) |
| 803 | entry = entries['u-boot-next'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 804 | self.assertEqual(43, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 805 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 806 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 807 | # Fifth u-boot, placed at a fixed offset |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 808 | self.assertIn('u-boot-fixed', entries) |
| 809 | entry = entries['u-boot-fixed'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 810 | self.assertEqual(61, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 811 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 812 | |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 813 | self.assertEqual(65, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 814 | |
| 815 | def testPackExtra(self): |
| 816 | """Test that extra packing feature works as expected""" |
Simon Glass | afb9caa | 2020-10-26 17:40:10 -0600 | [diff] [blame] | 817 | data, _, _, out_dtb_fname = self._DoReadFileDtb('009_pack_extra.dts', |
| 818 | update_dtb=True) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 819 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 820 | self.assertIn('image', control.images) |
| 821 | image = control.images['image'] |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 822 | entries = image.GetEntries() |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 823 | self.assertEqual(5, len(entries)) |
| 824 | |
| 825 | # First u-boot with padding before and after |
| 826 | self.assertIn('u-boot', entries) |
| 827 | entry = entries['u-boot'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 828 | self.assertEqual(0, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 829 | self.assertEqual(3, entry.pad_before) |
| 830 | self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 831 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 832 | self.assertEqual(tools.GetBytes(0, 3) + U_BOOT_DATA + |
| 833 | tools.GetBytes(0, 5), data[:entry.size]) |
| 834 | pos = entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 835 | |
| 836 | # Second u-boot has an aligned size, but it has no effect |
| 837 | self.assertIn('u-boot-align-size-nop', entries) |
| 838 | entry = entries['u-boot-align-size-nop'] |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 839 | self.assertEqual(pos, entry.offset) |
| 840 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 841 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 842 | self.assertEqual(U_BOOT_DATA, data[pos:pos + entry.size]) |
| 843 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 844 | |
| 845 | # Third u-boot has an aligned size too |
| 846 | self.assertIn('u-boot-align-size', entries) |
| 847 | entry = entries['u-boot-align-size'] |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 848 | self.assertEqual(pos, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 849 | self.assertEqual(32, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 850 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 851 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 32 - len(U_BOOT_DATA)), |
| 852 | data[pos:pos + entry.size]) |
| 853 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 854 | |
| 855 | # Fourth u-boot has an aligned end |
| 856 | self.assertIn('u-boot-align-end', entries) |
| 857 | entry = entries['u-boot-align-end'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 858 | self.assertEqual(48, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 859 | self.assertEqual(16, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 860 | self.assertEqual(U_BOOT_DATA, entry.data[:len(U_BOOT_DATA)]) |
| 861 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 16 - len(U_BOOT_DATA)), |
| 862 | data[pos:pos + entry.size]) |
| 863 | pos += entry.size |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 864 | |
| 865 | # Fifth u-boot immediately afterwards |
| 866 | self.assertIn('u-boot-align-both', entries) |
| 867 | entry = entries['u-boot-align-both'] |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 868 | self.assertEqual(64, entry.offset) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 869 | self.assertEqual(64, entry.size) |
Simon Glass | 187202f | 2020-10-26 17:40:08 -0600 | [diff] [blame] | 870 | self.assertEqual(U_BOOT_DATA, entry.data[:len(U_BOOT_DATA)]) |
| 871 | self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 64 - len(U_BOOT_DATA)), |
| 872 | data[pos:pos + entry.size]) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 873 | |
| 874 | self.CheckNoGaps(entries) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 875 | self.assertEqual(128, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 876 | |
Simon Glass | afb9caa | 2020-10-26 17:40:10 -0600 | [diff] [blame] | 877 | dtb = fdt.Fdt(out_dtb_fname) |
| 878 | dtb.Scan() |
| 879 | props = self._GetPropTree(dtb, ['size', 'offset', 'image-pos']) |
| 880 | expected = { |
| 881 | 'image-pos': 0, |
| 882 | 'offset': 0, |
| 883 | 'size': 128, |
| 884 | |
| 885 | 'u-boot:image-pos': 0, |
| 886 | 'u-boot:offset': 0, |
| 887 | 'u-boot:size': 3 + 5 + len(U_BOOT_DATA), |
| 888 | |
| 889 | 'u-boot-align-size-nop:image-pos': 12, |
| 890 | 'u-boot-align-size-nop:offset': 12, |
| 891 | 'u-boot-align-size-nop:size': 4, |
| 892 | |
| 893 | 'u-boot-align-size:image-pos': 16, |
| 894 | 'u-boot-align-size:offset': 16, |
| 895 | 'u-boot-align-size:size': 32, |
| 896 | |
| 897 | 'u-boot-align-end:image-pos': 48, |
| 898 | 'u-boot-align-end:offset': 48, |
| 899 | 'u-boot-align-end:size': 16, |
| 900 | |
| 901 | 'u-boot-align-both:image-pos': 64, |
| 902 | 'u-boot-align-both:offset': 64, |
| 903 | 'u-boot-align-both:size': 64, |
| 904 | } |
| 905 | self.assertEqual(expected, props) |
| 906 | |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 907 | def testPackAlignPowerOf2(self): |
| 908 | """Test that invalid entry alignment is detected""" |
| 909 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 910 | self._DoTestFile('010_pack_align_power2.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 911 | self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power " |
| 912 | "of two", str(e.exception)) |
| 913 | |
| 914 | def testPackAlignSizePowerOf2(self): |
| 915 | """Test that invalid entry size alignment is detected""" |
| 916 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 917 | self._DoTestFile('011_pack_align_size_power2.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 918 | self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a " |
| 919 | "power of two", str(e.exception)) |
| 920 | |
| 921 | def testPackInvalidAlign(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 922 | """Test detection of an offset that does not match its alignment""" |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 923 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 924 | self._DoTestFile('012_pack_inv_align.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 925 | self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 926 | "align 0x4 (4)", str(e.exception)) |
| 927 | |
| 928 | def testPackInvalidSizeAlign(self): |
| 929 | """Test that invalid entry size alignment is detected""" |
| 930 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 931 | self._DoTestFile('013_pack_inv_size_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 932 | self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match " |
| 933 | "align-size 0x4 (4)", str(e.exception)) |
| 934 | |
| 935 | def testPackOverlap(self): |
| 936 | """Test that overlapping regions are detected""" |
| 937 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 938 | self._DoTestFile('014_pack_overlap.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 939 | self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 940 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 941 | str(e.exception)) |
| 942 | |
| 943 | def testPackEntryOverflow(self): |
| 944 | """Test that entries that overflow their size are detected""" |
| 945 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 946 | self._DoTestFile('015_pack_overflow.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 947 | self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) " |
| 948 | "but entry size is 0x3 (3)", str(e.exception)) |
| 949 | |
| 950 | def testPackImageOverflow(self): |
| 951 | """Test that entries which overflow the image size are detected""" |
| 952 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 953 | self._DoTestFile('016_pack_image_overflow.dts') |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 954 | self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 955 | "size 0x3 (3)", str(e.exception)) |
| 956 | |
| 957 | def testPackImageSize(self): |
| 958 | """Test that the image size can be set""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 959 | retcode = self._DoTestFile('017_pack_image_size.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 960 | self.assertEqual(0, retcode) |
| 961 | self.assertIn('image', control.images) |
| 962 | image = control.images['image'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 963 | self.assertEqual(7, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 964 | |
| 965 | def testPackImageSizeAlign(self): |
| 966 | """Test that image size alignemnt works as expected""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 967 | retcode = self._DoTestFile('018_pack_image_align.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 968 | self.assertEqual(0, retcode) |
| 969 | self.assertIn('image', control.images) |
| 970 | image = control.images['image'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 971 | self.assertEqual(16, image.size) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 972 | |
| 973 | def testPackInvalidImageAlign(self): |
| 974 | """Test that invalid image alignment is detected""" |
| 975 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 976 | self._DoTestFile('019_pack_inv_image_align.dts') |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 977 | self.assertIn("Section '/binman': Size 0x7 (7) does not match " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 978 | "align-size 0x8 (8)", str(e.exception)) |
| 979 | |
| 980 | def testPackAlignPowerOf2(self): |
| 981 | """Test that invalid image alignment is detected""" |
| 982 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 983 | self._DoTestFile('020_pack_inv_image_align_power2.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 984 | self.assertIn("Image '/binman': Alignment size 131 must be a power of " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 985 | "two", str(e.exception)) |
| 986 | |
| 987 | def testImagePadByte(self): |
| 988 | """Test that the image pad byte can be specified""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 989 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 990 | data = self._DoReadFile('021_image_pad.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 991 | self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0xff, 1) + |
| 992 | U_BOOT_DATA, data) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 993 | |
| 994 | def testImageName(self): |
| 995 | """Test that image files can be named""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 996 | retcode = self._DoTestFile('022_image_name.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 997 | self.assertEqual(0, retcode) |
| 998 | image = control.images['image1'] |
| 999 | fname = tools.GetOutputFilename('test-name') |
| 1000 | self.assertTrue(os.path.exists(fname)) |
| 1001 | |
| 1002 | image = control.images['image2'] |
| 1003 | fname = tools.GetOutputFilename('test-name.xx') |
| 1004 | self.assertTrue(os.path.exists(fname)) |
| 1005 | |
| 1006 | def testBlobFilename(self): |
| 1007 | """Test that generic blobs can be provided by filename""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1008 | data = self._DoReadFile('023_blob.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1009 | self.assertEqual(BLOB_DATA, data) |
| 1010 | |
| 1011 | def testPackSorted(self): |
| 1012 | """Test that entries can be sorted""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1013 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1014 | data = self._DoReadFile('024_sorted.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1015 | self.assertEqual(tools.GetBytes(0, 1) + U_BOOT_SPL_DATA + |
| 1016 | tools.GetBytes(0, 2) + U_BOOT_DATA, data) |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1017 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1018 | def testPackZeroOffset(self): |
| 1019 | """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] | 1020 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1021 | self._DoTestFile('025_pack_zero_size.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1022 | self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps " |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1023 | "with previous entry '/binman/u-boot' ending at 0x4 (4)", |
| 1024 | str(e.exception)) |
| 1025 | |
| 1026 | def testPackUbootDtb(self): |
| 1027 | """Test that a device tree can be added to U-Boot""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1028 | data = self._DoReadFile('026_pack_u_boot_dtb.dts') |
Simon Glass | 57454f4 | 2016-11-25 20:15:52 -0700 | [diff] [blame] | 1029 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1030 | |
| 1031 | def testPackX86RomNoSize(self): |
| 1032 | """Test that the end-at-4gb property requires a size property""" |
| 1033 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1034 | self._DoTestFile('027_pack_4gb_no_size.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1035 | self.assertIn("Image '/binman': Section size must be provided when " |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1036 | "using end-at-4gb", str(e.exception)) |
| 1037 | |
Jagdish Gediya | 0fb978c | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 1038 | def test4gbAndSkipAtStartTogether(self): |
| 1039 | """Test that the end-at-4gb and skip-at-size property can't be used |
| 1040 | together""" |
| 1041 | with self.assertRaises(ValueError) as e: |
Simon Glass | 11f2bd0 | 2019-08-24 07:23:02 -0600 | [diff] [blame] | 1042 | self._DoTestFile('098_4gb_and_skip_at_start_together.dts') |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1043 | self.assertIn("Image '/binman': Provide either 'end-at-4gb' or " |
Jagdish Gediya | 0fb978c | 2018-09-03 21:35:07 +0530 | [diff] [blame] | 1044 | "'skip-at-start'", str(e.exception)) |
| 1045 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1046 | def testPackX86RomOutside(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1047 | """Test that the end-at-4gb property checks for offset boundaries""" |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1048 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1049 | self._DoTestFile('028_pack_4gb_outside.dts') |
Simon Glass | d617986 | 2020-10-26 17:40:05 -0600 | [diff] [blame] | 1050 | self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) size 0x4 (4) " |
| 1051 | "is outside the section '/binman' starting at " |
| 1052 | '0xffffffe0 (4294967264) of size 0x20 (32)', |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1053 | str(e.exception)) |
| 1054 | |
| 1055 | def testPackX86Rom(self): |
| 1056 | """Test that a basic x86 ROM can be created""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1057 | self._SetupSplElf() |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1058 | data = self._DoReadFile('029_x86_rom.dts') |
Simon Glass | 4e353e2 | 2019-08-24 07:23:04 -0600 | [diff] [blame] | 1059 | 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] | 1060 | tools.GetBytes(0, 2), data) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1061 | |
| 1062 | def testPackX86RomMeNoDesc(self): |
| 1063 | """Test that an invalid Intel descriptor entry is detected""" |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1064 | try: |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 1065 | TestFunctional._MakeInputFile('descriptor-empty.bin', b'') |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1066 | with self.assertRaises(ValueError) as e: |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 1067 | self._DoTestFile('163_x86_rom_me_empty.dts') |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 1068 | self.assertIn("Node '/binman/intel-descriptor': Cannot find Intel Flash Descriptor (FD) signature", |
| 1069 | str(e.exception)) |
| 1070 | finally: |
| 1071 | self._SetupDescriptor() |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1072 | |
| 1073 | def testPackX86RomBadDesc(self): |
| 1074 | """Test that the Intel requires a descriptor entry""" |
| 1075 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1076 | self._DoTestFile('030_x86_rom_me_no_desc.dts') |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1077 | self.assertIn("Node '/binman/intel-me': No offset set with " |
| 1078 | "offset-unset: should another entry provide this correct " |
| 1079 | "offset?", str(e.exception)) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1080 | |
| 1081 | def testPackX86RomMe(self): |
| 1082 | """Test that an x86 ROM with an ME region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1083 | data = self._DoReadFile('031_x86_rom_me.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 1084 | expected_desc = tools.ReadFile(self.TestFile('descriptor.bin')) |
| 1085 | if data[:0x1000] != expected_desc: |
| 1086 | self.fail('Expected descriptor binary at start of image') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1087 | self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)]) |
| 1088 | |
| 1089 | def testPackVga(self): |
| 1090 | """Test that an image with a VGA binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1091 | data = self._DoReadFile('032_intel_vga.dts') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1092 | self.assertEqual(VGA_DATA, data[:len(VGA_DATA)]) |
| 1093 | |
| 1094 | def testPackStart16(self): |
| 1095 | """Test that an image with an x86 start16 region can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1096 | data = self._DoReadFile('033_x86_start16.dts') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1097 | self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)]) |
| 1098 | |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 1099 | def testPackPowerpcMpc85xxBootpgResetvec(self): |
| 1100 | """Test that an image with powerpc-mpc85xx-bootpg-resetvec can be |
| 1101 | created""" |
Simon Glass | 11f2bd0 | 2019-08-24 07:23:02 -0600 | [diff] [blame] | 1102 | data = self._DoReadFile('150_powerpc_mpc85xx_bootpg_resetvec.dts') |
Jagdish Gediya | 311d484 | 2018-09-03 21:35:08 +0530 | [diff] [blame] | 1103 | self.assertEqual(PPC_MPC85XX_BR_DATA, data[:len(PPC_MPC85XX_BR_DATA)]) |
| 1104 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1105 | def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False): |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1106 | """Handle running a test for insertion of microcode |
| 1107 | |
| 1108 | Args: |
| 1109 | dts_fname: Name of test .dts file |
| 1110 | nodtb_data: Data that we expect in the first section |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1111 | ucode_second: True if the microsecond entry is second instead of |
| 1112 | third |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1113 | |
| 1114 | Returns: |
| 1115 | Tuple: |
| 1116 | Contents of first region (U-Boot or SPL) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1117 | Offset and size components of microcode pointer, as inserted |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1118 | in the above (two 4-byte words) |
| 1119 | """ |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1120 | data = self._DoReadFile(dts_fname, True) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1121 | |
| 1122 | # Now check the device tree has no microcode |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1123 | if ucode_second: |
| 1124 | ucode_content = data[len(nodtb_data):] |
| 1125 | ucode_pos = len(nodtb_data) |
| 1126 | dtb_with_ucode = ucode_content[16:] |
| 1127 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 1128 | else: |
| 1129 | dtb_with_ucode = data[len(nodtb_data):] |
| 1130 | fdt_len = self.GetFdtLen(dtb_with_ucode) |
| 1131 | ucode_content = dtb_with_ucode[fdt_len:] |
| 1132 | ucode_pos = len(nodtb_data) + fdt_len |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1133 | fname = tools.GetOutputFilename('test.dtb') |
| 1134 | with open(fname, 'wb') as fd: |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1135 | fd.write(dtb_with_ucode) |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 1136 | dtb = fdt.FdtScan(fname) |
| 1137 | ucode = dtb.GetNode('/microcode') |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1138 | self.assertTrue(ucode) |
| 1139 | for node in ucode.subnodes: |
| 1140 | self.assertFalse(node.props.get('data')) |
| 1141 | |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1142 | # Check that the microcode appears immediately after the Fdt |
| 1143 | # This matches the concatenation of the data properties in |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1144 | # the /microcode/update@xxx nodes in 34_x86_ucode.dts. |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1145 | ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000, |
| 1146 | 0x78235609) |
Simon Glass | 820af1d | 2018-07-06 10:27:16 -0600 | [diff] [blame] | 1147 | self.assertEqual(ucode_data, ucode_content[:len(ucode_data)]) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1148 | |
| 1149 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1150 | # expected offset and size |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1151 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 1152 | len(ucode_data)) |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1153 | u_boot = data[:len(nodtb_data)] |
| 1154 | return u_boot, pos_and_size |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1155 | |
| 1156 | def testPackUbootMicrocode(self): |
| 1157 | """Test that x86 microcode can be handled correctly |
| 1158 | |
| 1159 | We expect to see the following in the image, in order: |
| 1160 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 1161 | place |
| 1162 | u-boot.dtb with the microcode removed |
| 1163 | the microcode |
| 1164 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1165 | first, pos_and_size = self._RunMicrocodeTest('034_x86_ucode.dts', |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1166 | U_BOOT_NODTB_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1167 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 1168 | b' somewhere in here', first) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1169 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1170 | def _RunPackUbootSingleMicrocode(self): |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1171 | """Test that x86 microcode can be handled correctly |
| 1172 | |
| 1173 | We expect to see the following in the image, in order: |
| 1174 | u-boot-nodtb.bin with a microcode pointer inserted at the correct |
| 1175 | place |
| 1176 | u-boot.dtb with the microcode |
| 1177 | an empty microcode region |
| 1178 | """ |
| 1179 | # We need the libfdt library to run this test since only that allows |
| 1180 | # finding the offset of a property. This is required by |
| 1181 | # Entry_u_boot_dtb_with_ucode.ObtainContents(). |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1182 | data = self._DoReadFile('035_x86_single_ucode.dts', True) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1183 | |
| 1184 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1185 | |
| 1186 | fdt_len = self.GetFdtLen(second) |
| 1187 | third = second[fdt_len:] |
| 1188 | second = second[:fdt_len] |
| 1189 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1190 | ucode_data = struct.pack('>2L', 0x12345678, 0x12345679) |
| 1191 | self.assertIn(ucode_data, second) |
| 1192 | ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA) |
Simon Glass | 7223245 | 2016-11-25 20:15:53 -0700 | [diff] [blame] | 1193 | |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1194 | # Check that the microcode pointer was inserted. It should match the |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1195 | # expected offset and size |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1196 | pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos, |
| 1197 | len(ucode_data)) |
| 1198 | first = data[:len(U_BOOT_NODTB_DATA)] |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1199 | self.assertEqual(b'nodtb with microcode' + pos_and_size + |
| 1200 | b' somewhere in here', first) |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1201 | |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1202 | def testPackUbootSingleMicrocode(self): |
| 1203 | """Test that x86 microcode can be handled correctly with fdt_normal. |
| 1204 | """ |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1205 | self._RunPackUbootSingleMicrocode() |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1206 | |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1207 | def testUBootImg(self): |
| 1208 | """Test that u-boot.img can be put in a file""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1209 | data = self._DoReadFile('036_u_boot_img.dts') |
Simon Glass | 996021e | 2016-11-25 20:15:54 -0700 | [diff] [blame] | 1210 | self.assertEqual(U_BOOT_IMG_DATA, data) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1211 | |
| 1212 | def testNoMicrocode(self): |
| 1213 | """Test that a missing microcode region is detected""" |
| 1214 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1215 | self._DoReadFile('037_x86_no_ucode.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1216 | self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode " |
| 1217 | "node found in ", str(e.exception)) |
| 1218 | |
| 1219 | def testMicrocodeWithoutNode(self): |
| 1220 | """Test that a missing u-boot-dtb-with-ucode node is detected""" |
| 1221 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1222 | self._DoReadFile('038_x86_ucode_missing_node.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1223 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 1224 | "microcode region u-boot-dtb-with-ucode", str(e.exception)) |
| 1225 | |
| 1226 | def testMicrocodeWithoutNode2(self): |
| 1227 | """Test that a missing u-boot-ucode node is detected""" |
| 1228 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1229 | self._DoReadFile('039_x86_ucode_missing_node2.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1230 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find " |
| 1231 | "microcode region u-boot-ucode", str(e.exception)) |
| 1232 | |
| 1233 | def testMicrocodeWithoutPtrInElf(self): |
| 1234 | """Test that a U-Boot binary without the microcode symbol is detected""" |
| 1235 | # ELF file without a '_dt_ucode_base_size' symbol |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1236 | try: |
Simon Glass | faaaa16 | 2019-08-24 07:22:55 -0600 | [diff] [blame] | 1237 | TestFunctional._MakeInputFile('u-boot', |
| 1238 | tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr'))) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1239 | |
| 1240 | with self.assertRaises(ValueError) as e: |
Simon Glass | bac25c8 | 2017-05-27 07:38:26 -0600 | [diff] [blame] | 1241 | self._RunPackUbootSingleMicrocode() |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1242 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate " |
| 1243 | "_dt_ucode_base_size symbol in u-boot", str(e.exception)) |
| 1244 | |
| 1245 | finally: |
| 1246 | # Put the original file back |
Simon Glass | 4affd4b | 2019-08-24 07:22:54 -0600 | [diff] [blame] | 1247 | TestFunctional._MakeInputFile('u-boot', |
| 1248 | tools.ReadFile(self.ElfTestFile('u_boot_ucode_ptr'))) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1249 | |
| 1250 | def testMicrocodeNotInImage(self): |
| 1251 | """Test that microcode must be placed within the image""" |
| 1252 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1253 | self._DoReadFile('040_x86_ucode_not_in_image.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1254 | self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode " |
| 1255 | "pointer _dt_ucode_base_size at fffffe14 is outside the " |
Simon Glass | ad5a771 | 2018-06-01 09:38:14 -0600 | [diff] [blame] | 1256 | "section ranging from 00000000 to 0000002e", str(e.exception)) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1257 | |
| 1258 | def testWithoutMicrocode(self): |
| 1259 | """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] | 1260 | TestFunctional._MakeInputFile('u-boot', |
| 1261 | tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr'))) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1262 | data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1263 | |
| 1264 | # Now check the device tree has no microcode |
| 1265 | self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)]) |
| 1266 | second = data[len(U_BOOT_NODTB_DATA):] |
| 1267 | |
| 1268 | fdt_len = self.GetFdtLen(second) |
| 1269 | self.assertEqual(dtb, second[:fdt_len]) |
| 1270 | |
| 1271 | used_len = len(U_BOOT_NODTB_DATA) + fdt_len |
| 1272 | third = data[used_len:] |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1273 | self.assertEqual(tools.GetBytes(0, 0x200 - used_len), third) |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1274 | |
| 1275 | def testUnknownPosSize(self): |
| 1276 | """Test that microcode must be placed within the image""" |
| 1277 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1278 | self._DoReadFile('041_unknown_pos_size.dts', True) |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1279 | self.assertIn("Section '/binman': Unable to set offset/size for unknown " |
Simon Glass | d2dfb5f | 2016-11-25 20:15:55 -0700 | [diff] [blame] | 1280 | "entry 'invalid-entry'", str(e.exception)) |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1281 | |
| 1282 | def testPackFsp(self): |
| 1283 | """Test that an image with a FSP binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1284 | data = self._DoReadFile('042_intel_fsp.dts') |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1285 | self.assertEqual(FSP_DATA, data[:len(FSP_DATA)]) |
| 1286 | |
| 1287 | def testPackCmc(self): |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1288 | """Test that an image with a CMC binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1289 | data = self._DoReadFile('043_intel_cmc.dts') |
Simon Glass | b4176d4 | 2016-11-25 20:15:56 -0700 | [diff] [blame] | 1290 | self.assertEqual(CMC_DATA, data[:len(CMC_DATA)]) |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1291 | |
| 1292 | def testPackVbt(self): |
| 1293 | """Test that an image with a VBT binary can be created""" |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 1294 | data = self._DoReadFile('046_intel_vbt.dts') |
Bin Meng | d7bcdf5 | 2017-08-15 22:41:54 -0700 | [diff] [blame] | 1295 | self.assertEqual(VBT_DATA, data[:len(VBT_DATA)]) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1296 | |
Simon Glass | 7f94e83 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1297 | def testSplBssPad(self): |
| 1298 | """Test that we can pad SPL's BSS with zeros""" |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1299 | # ELF file with a '__bss_size' symbol |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1300 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1301 | data = self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1302 | self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA, |
| 1303 | data) |
Simon Glass | 7f94e83 | 2017-11-12 21:52:25 -0700 | [diff] [blame] | 1304 | |
Simon Glass | 04cda03 | 2018-10-01 21:12:42 -0600 | [diff] [blame] | 1305 | def testSplBssPadMissing(self): |
| 1306 | """Test that a missing symbol is detected""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1307 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1308 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1309 | self._DoReadFile('047_spl_bss_pad.dts') |
Simon Glass | 24ad365 | 2017-11-13 18:54:54 -0700 | [diff] [blame] | 1310 | self.assertIn('Expected __bss_size symbol in spl/u-boot-spl', |
| 1311 | str(e.exception)) |
| 1312 | |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1313 | def testPackStart16Spl(self): |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1314 | """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] | 1315 | data = self._DoReadFile('048_x86_start16_spl.dts') |
Simon Glass | e83679d | 2017-11-12 21:52:26 -0700 | [diff] [blame] | 1316 | self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)]) |
| 1317 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1318 | def _PackUbootSplMicrocode(self, dts, ucode_second=False): |
| 1319 | """Helper function for microcode tests |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1320 | |
| 1321 | We expect to see the following in the image, in order: |
| 1322 | u-boot-spl-nodtb.bin with a microcode pointer inserted at the |
| 1323 | correct place |
| 1324 | u-boot.dtb with the microcode removed |
| 1325 | the microcode |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1326 | |
| 1327 | Args: |
| 1328 | dts: Device tree file to use for test |
| 1329 | ucode_second: True if the microsecond entry is second instead of |
| 1330 | third |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1331 | """ |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1332 | self._SetupSplElf('u_boot_ucode_ptr') |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1333 | first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA, |
| 1334 | ucode_second=ucode_second) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1335 | self.assertEqual(b'splnodtb with microc' + pos_and_size + |
| 1336 | b'ter somewhere in here', first) |
Simon Glass | 3d27423 | 2017-11-12 21:52:27 -0700 | [diff] [blame] | 1337 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1338 | def testPackUbootSplMicrocode(self): |
| 1339 | """Test that x86 microcode can be handled correctly in SPL""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1340 | self._PackUbootSplMicrocode('049_x86_ucode_spl.dts') |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1341 | |
| 1342 | def testPackUbootSplMicrocodeReorder(self): |
| 1343 | """Test that order doesn't matter for microcode entries |
| 1344 | |
| 1345 | This is the same as testPackUbootSplMicrocode but when we process the |
| 1346 | u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode |
| 1347 | entry, so we reply on binman to try later. |
| 1348 | """ |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1349 | self._PackUbootSplMicrocode('058_x86_ucode_spl_needs_retry.dts', |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1350 | ucode_second=True) |
| 1351 | |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1352 | def testPackMrc(self): |
| 1353 | """Test that an image with an MRC binary can be created""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1354 | data = self._DoReadFile('050_intel_mrc.dts') |
Simon Glass | a409c93 | 2017-11-12 21:52:28 -0700 | [diff] [blame] | 1355 | self.assertEqual(MRC_DATA, data[:len(MRC_DATA)]) |
| 1356 | |
Simon Glass | 9aa6a6f | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1357 | def testSplDtb(self): |
| 1358 | """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] | 1359 | data = self._DoReadFile('051_u_boot_spl_dtb.dts') |
Simon Glass | 9aa6a6f | 2017-11-13 18:54:55 -0700 | [diff] [blame] | 1360 | self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)]) |
| 1361 | |
Simon Glass | 0a6da31 | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1362 | def testSplNoDtb(self): |
| 1363 | """Test that an image with spl/u-boot-spl-nodtb.bin can be created""" |
Simon Glass | 13089cc | 2021-04-25 08:39:32 +1200 | [diff] [blame] | 1364 | self._SetupSplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1365 | data = self._DoReadFile('052_u_boot_spl_nodtb.dts') |
Simon Glass | 0a6da31 | 2017-11-13 18:54:56 -0700 | [diff] [blame] | 1366 | self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)]) |
| 1367 | |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 1368 | def checkSymbols(self, dts, base_data, u_boot_offset, entry_args=None, |
| 1369 | use_expanded=False): |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1370 | """Check the image contains the expected symbol values |
| 1371 | |
| 1372 | Args: |
| 1373 | dts: Device tree file to use for test |
| 1374 | base_data: Data before and after 'u-boot' section |
| 1375 | u_boot_offset: Offset of 'u-boot' section in image |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 1376 | entry_args: Dict of entry args to supply to binman |
| 1377 | key: arg name |
| 1378 | value: value of that arg |
| 1379 | use_expanded: True to use expanded entries where available, e.g. |
| 1380 | 'u-boot-expanded' instead of 'u-boot' |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1381 | """ |
Simon Glass | 5d0c026 | 2019-08-24 07:22:56 -0600 | [diff] [blame] | 1382 | elf_fname = self.ElfTestFile('u_boot_binman_syms') |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1383 | syms = elf.GetSymbols(elf_fname, ['binman', 'image']) |
| 1384 | addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start') |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1385 | self.assertEqual(syms['_binman_u_boot_spl_any_prop_offset'].address, |
| 1386 | addr) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1387 | |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 1388 | self._SetupSplElf('u_boot_binman_syms') |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 1389 | data = self._DoReadFileDtb(dts, entry_args=entry_args, |
| 1390 | use_expanded=use_expanded)[0] |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1391 | # The image should contain the symbols from u_boot_binman_syms.c |
| 1392 | # Note that image_pos is adjusted by the base address of the image, |
| 1393 | # which is 0x10 in our test image |
| 1394 | sym_values = struct.pack('<LQLL', 0x00, |
| 1395 | u_boot_offset + len(U_BOOT_DATA), |
| 1396 | 0x10 + u_boot_offset, 0x04) |
| 1397 | expected = (sym_values + base_data[20:] + |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1398 | tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values + |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1399 | base_data[20:]) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 1400 | self.assertEqual(expected, data) |
| 1401 | |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1402 | def testSymbols(self): |
| 1403 | """Test binman can assign symbols embedded in U-Boot""" |
| 1404 | self.checkSymbols('053_symbols.dts', U_BOOT_SPL_DATA, 0x18) |
| 1405 | |
| 1406 | def testSymbolsNoDtb(self): |
| 1407 | """Test binman can assign symbols embedded in U-Boot SPL""" |
Simon Glass | 3bbc993 | 2021-03-21 18:24:29 +1300 | [diff] [blame] | 1408 | self.checkSymbols('196_symbols_nodtb.dts', |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 1409 | U_BOOT_SPL_NODTB_DATA + U_BOOT_SPL_DTB_DATA, |
| 1410 | 0x38) |
| 1411 | |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1412 | def testPackUnitAddress(self): |
| 1413 | """Test that we support multiple binaries with the same name""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1414 | data = self._DoReadFile('054_unit_address.dts') |
Simon Glass | e76a3e6 | 2018-06-01 09:38:11 -0600 | [diff] [blame] | 1415 | self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data) |
| 1416 | |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1417 | def testSections(self): |
| 1418 | """Basic test of sections""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1419 | data = self._DoReadFile('055_sections.dts') |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1420 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 1421 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12) + |
| 1422 | U_BOOT_DATA + tools.GetBytes(ord('&'), 4)) |
Simon Glass | a91e115 | 2018-06-01 09:38:16 -0600 | [diff] [blame] | 1423 | self.assertEqual(expected, data) |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 1424 | |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1425 | def testMap(self): |
| 1426 | """Tests outputting a map of the images""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1427 | _, _, map_data, _ = self._DoReadFileDtb('055_sections.dts', map=True) |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1428 | self.assertEqual('''ImagePos Offset Size Name |
| 1429 | 00000000 00000000 00000028 main-section |
| 1430 | 00000000 00000000 00000010 section@0 |
| 1431 | 00000000 00000000 00000004 u-boot |
| 1432 | 00000010 00000010 00000010 section@1 |
| 1433 | 00000010 00000000 00000004 u-boot |
| 1434 | 00000020 00000020 00000004 section@2 |
| 1435 | 00000020 00000000 00000004 u-boot |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 1436 | ''', map_data) |
| 1437 | |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1438 | def testNamePrefix(self): |
| 1439 | """Tests that name prefixes are used""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1440 | _, _, map_data, _ = self._DoReadFileDtb('056_name_prefix.dts', map=True) |
Simon Glass | 7eca792 | 2018-07-17 13:25:49 -0600 | [diff] [blame] | 1441 | self.assertEqual('''ImagePos Offset Size Name |
| 1442 | 00000000 00000000 00000028 main-section |
| 1443 | 00000000 00000000 00000010 section@0 |
| 1444 | 00000000 00000000 00000004 ro-u-boot |
| 1445 | 00000010 00000010 00000010 section@1 |
| 1446 | 00000010 00000000 00000004 rw-u-boot |
Simon Glass | 3b78d53 | 2018-06-01 09:38:21 -0600 | [diff] [blame] | 1447 | ''', map_data) |
| 1448 | |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1449 | def testUnknownContents(self): |
| 1450 | """Test that obtaining the contents works as expected""" |
| 1451 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1452 | self._DoReadFile('057_unknown_contents.dts', True) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1453 | self.assertIn("Image '/binman': Internal error: Could not complete " |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 1454 | "processing of contents: remaining [" |
| 1455 | "<binman.etype._testing.Entry__testing ", str(e.exception)) |
Simon Glass | 6ba679c | 2018-07-06 10:27:17 -0600 | [diff] [blame] | 1456 | |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1457 | def testBadChangeSize(self): |
| 1458 | """Test that trying to change the size of an entry fails""" |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 1459 | try: |
| 1460 | state.SetAllowEntryExpansion(False) |
| 1461 | with self.assertRaises(ValueError) as e: |
| 1462 | self._DoReadFile('059_change_size.dts', True) |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 1463 | 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] | 1464 | str(e.exception)) |
| 1465 | finally: |
| 1466 | state.SetAllowEntryExpansion(True) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1467 | |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1468 | def testUpdateFdt(self): |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1469 | """Test that we can update the device tree with offset/size info""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1470 | _, _, _, out_dtb_fname = self._DoReadFileDtb('060_fdt_update.dts', |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1471 | update_dtb=True) |
Simon Glass | 5463a6a | 2018-07-17 13:25:52 -0600 | [diff] [blame] | 1472 | dtb = fdt.Fdt(out_dtb_fname) |
| 1473 | dtb.Scan() |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 1474 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1475 | self.assertEqual({ |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1476 | 'image-pos': 0, |
Simon Glass | 3a9a2b8 | 2018-07-17 13:25:28 -0600 | [diff] [blame] | 1477 | 'offset': 0, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1478 | '_testing:offset': 32, |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 1479 | '_testing:size': 2, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1480 | '_testing:image-pos': 32, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1481 | 'section@0/u-boot:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1482 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1483 | 'section@0/u-boot:image-pos': 0, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1484 | 'section@0:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1485 | 'section@0:size': 16, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1486 | 'section@0:image-pos': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1487 | |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1488 | 'section@1/u-boot:offset': 0, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1489 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1490 | 'section@1/u-boot:image-pos': 16, |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 1491 | 'section@1:offset': 16, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1492 | 'section@1:size': 16, |
Simon Glass | 9dcc861 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 1493 | 'section@1:image-pos': 16, |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1494 | 'size': 40 |
| 1495 | }, props) |
| 1496 | |
| 1497 | def testUpdateFdtBad(self): |
| 1498 | """Test that we detect when ProcessFdt never completes""" |
| 1499 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1500 | self._DoReadFileDtb('061_fdt_update_bad.dts', update_dtb=True) |
Simon Glass | a87014e | 2018-07-06 10:27:42 -0600 | [diff] [blame] | 1501 | self.assertIn('Could not complete processing of Fdt: remaining ' |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 1502 | '[<binman.etype._testing.Entry__testing', |
| 1503 | str(e.exception)) |
Simon Glass | 2e1169f | 2018-07-06 10:27:19 -0600 | [diff] [blame] | 1504 | |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1505 | def testEntryArgs(self): |
| 1506 | """Test passing arguments to entries from the command line""" |
| 1507 | entry_args = { |
| 1508 | 'test-str-arg': 'test1', |
| 1509 | 'test-int-arg': '456', |
| 1510 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1511 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1512 | self.assertIn('image', control.images) |
| 1513 | entry = control.images['image'].GetEntries()['_testing'] |
| 1514 | self.assertEqual('test0', entry.test_str_fdt) |
| 1515 | self.assertEqual('test1', entry.test_str_arg) |
| 1516 | self.assertEqual(123, entry.test_int_fdt) |
| 1517 | self.assertEqual(456, entry.test_int_arg) |
| 1518 | |
| 1519 | def testEntryArgsMissing(self): |
| 1520 | """Test missing arguments and properties""" |
| 1521 | entry_args = { |
| 1522 | 'test-int-arg': '456', |
| 1523 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1524 | self._DoReadFileDtb('063_entry_args_missing.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1525 | entry = control.images['image'].GetEntries()['_testing'] |
| 1526 | self.assertEqual('test0', entry.test_str_fdt) |
| 1527 | self.assertEqual(None, entry.test_str_arg) |
| 1528 | self.assertEqual(None, entry.test_int_fdt) |
| 1529 | self.assertEqual(456, entry.test_int_arg) |
| 1530 | |
| 1531 | def testEntryArgsRequired(self): |
| 1532 | """Test missing arguments and properties""" |
| 1533 | entry_args = { |
| 1534 | 'test-int-arg': '456', |
| 1535 | } |
| 1536 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1537 | self._DoReadFileDtb('064_entry_args_required.dts') |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 1538 | self.assertIn("Node '/binman/_testing': " |
| 1539 | 'Missing required properties/entry args: test-str-arg, ' |
| 1540 | 'test-int-fdt, test-int-arg', |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1541 | str(e.exception)) |
| 1542 | |
| 1543 | def testEntryArgsInvalidFormat(self): |
| 1544 | """Test that an invalid entry-argument format is detected""" |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 1545 | args = ['build', '-d', self.TestFile('064_entry_args_required.dts'), |
| 1546 | '-ano-value'] |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1547 | with self.assertRaises(ValueError) as e: |
| 1548 | self._DoBinman(*args) |
| 1549 | self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception)) |
| 1550 | |
| 1551 | def testEntryArgsInvalidInteger(self): |
| 1552 | """Test that an invalid entry-argument integer is detected""" |
| 1553 | entry_args = { |
| 1554 | 'test-int-arg': 'abc', |
| 1555 | } |
| 1556 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1557 | self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1558 | self.assertIn("Node '/binman/_testing': Cannot convert entry arg " |
| 1559 | "'test-int-arg' (value 'abc') to integer", |
| 1560 | str(e.exception)) |
| 1561 | |
| 1562 | def testEntryArgsInvalidDatatype(self): |
| 1563 | """Test that an invalid entry-argument datatype is detected |
| 1564 | |
| 1565 | This test could be written in entry_test.py except that it needs |
| 1566 | access to control.entry_args, which seems more than that module should |
| 1567 | be able to see. |
| 1568 | """ |
| 1569 | entry_args = { |
| 1570 | 'test-bad-datatype-arg': '12', |
| 1571 | } |
| 1572 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1573 | self._DoReadFileDtb('065_entry_args_unknown_datatype.dts', |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 1574 | entry_args=entry_args) |
| 1575 | self.assertIn('GetArg() internal error: Unknown data type ', |
| 1576 | str(e.exception)) |
| 1577 | |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1578 | def testText(self): |
| 1579 | """Test for a text entry type""" |
| 1580 | entry_args = { |
| 1581 | 'test-id': TEXT_DATA, |
| 1582 | 'test-id2': TEXT_DATA2, |
| 1583 | 'test-id3': TEXT_DATA3, |
| 1584 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1585 | data, _, _, _ = self._DoReadFileDtb('066_text.dts', |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1586 | entry_args=entry_args) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1587 | expected = (tools.ToBytes(TEXT_DATA) + |
| 1588 | tools.GetBytes(0, 8 - len(TEXT_DATA)) + |
| 1589 | tools.ToBytes(TEXT_DATA2) + tools.ToBytes(TEXT_DATA3) + |
Simon Glass | 47f6a62 | 2019-07-08 13:18:40 -0600 | [diff] [blame] | 1590 | b'some text' + b'more text') |
Simon Glass | 2ca5203 | 2018-07-17 13:25:33 -0600 | [diff] [blame] | 1591 | self.assertEqual(expected, data) |
| 1592 | |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1593 | def testEntryDocs(self): |
| 1594 | """Test for creation of entry documentation""" |
| 1595 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 1596 | control.WriteEntryDocs(control.GetEntryModules()) |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1597 | self.assertTrue(len(stdout.getvalue()) > 0) |
| 1598 | |
| 1599 | def testEntryDocsMissing(self): |
| 1600 | """Test handling of missing entry documentation""" |
| 1601 | with self.assertRaises(ValueError) as e: |
| 1602 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 220ff5f | 2020-08-05 13:27:46 -0600 | [diff] [blame] | 1603 | control.WriteEntryDocs(control.GetEntryModules(), 'u_boot') |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 1604 | self.assertIn('Documentation is missing for modules: u_boot', |
| 1605 | str(e.exception)) |
| 1606 | |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1607 | def testFmap(self): |
| 1608 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1609 | data = self._DoReadFile('067_fmap.dts') |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1610 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1611 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 1612 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12)) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1613 | self.assertEqual(expected, data[:32]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1614 | self.assertEqual(b'__FMAP__', fhdr.signature) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1615 | self.assertEqual(1, fhdr.ver_major) |
| 1616 | self.assertEqual(0, fhdr.ver_minor) |
| 1617 | self.assertEqual(0, fhdr.base) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 1618 | expect_size = fmap_util.FMAP_HEADER_LEN + fmap_util.FMAP_AREA_LEN * 5 |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1619 | self.assertEqual(16 + 16 + expect_size, fhdr.image_size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1620 | self.assertEqual(b'FMAP', fhdr.name) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 1621 | self.assertEqual(5, fhdr.nareas) |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1622 | fiter = iter(fentries) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1623 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1624 | fentry = next(fiter) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 1625 | self.assertEqual(b'SECTION0', fentry.name) |
| 1626 | self.assertEqual(0, fentry.offset) |
| 1627 | self.assertEqual(16, fentry.size) |
| 1628 | self.assertEqual(0, fentry.flags) |
| 1629 | |
| 1630 | fentry = next(fiter) |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1631 | self.assertEqual(b'RO_U_BOOT', fentry.name) |
| 1632 | self.assertEqual(0, fentry.offset) |
| 1633 | self.assertEqual(4, fentry.size) |
| 1634 | self.assertEqual(0, fentry.flags) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1635 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1636 | fentry = next(fiter) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 1637 | self.assertEqual(b'SECTION1', fentry.name) |
| 1638 | self.assertEqual(16, fentry.offset) |
| 1639 | self.assertEqual(16, fentry.size) |
| 1640 | self.assertEqual(0, fentry.flags) |
| 1641 | |
| 1642 | fentry = next(fiter) |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1643 | self.assertEqual(b'RW_U_BOOT', fentry.name) |
| 1644 | self.assertEqual(16, fentry.offset) |
| 1645 | self.assertEqual(4, fentry.size) |
| 1646 | self.assertEqual(0, fentry.flags) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1647 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 1648 | fentry = next(fiter) |
| 1649 | self.assertEqual(b'FMAP', fentry.name) |
| 1650 | self.assertEqual(32, fentry.offset) |
| 1651 | self.assertEqual(expect_size, fentry.size) |
| 1652 | self.assertEqual(0, fentry.flags) |
Simon Glass | 704784b | 2018-07-17 13:25:38 -0600 | [diff] [blame] | 1653 | |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1654 | def testBlobNamedByArg(self): |
| 1655 | """Test we can add a blob with the filename coming from an entry arg""" |
| 1656 | entry_args = { |
| 1657 | 'cros-ec-rw-path': 'ecrw.bin', |
| 1658 | } |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 1659 | self._DoReadFileDtb('068_blob_named_by_arg.dts', entry_args=entry_args) |
Simon Glass | db168d4 | 2018-07-17 13:25:39 -0600 | [diff] [blame] | 1660 | |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1661 | def testFill(self): |
| 1662 | """Test for an fill entry type""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1663 | data = self._DoReadFile('069_fill.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1664 | expected = tools.GetBytes(0xff, 8) + tools.GetBytes(0, 8) |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1665 | self.assertEqual(expected, data) |
| 1666 | |
| 1667 | def testFillNoSize(self): |
| 1668 | """Test for an fill entry type with no size""" |
| 1669 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1670 | self._DoReadFile('070_fill_no_size.dts') |
Simon Glass | 53f5399 | 2018-07-17 13:25:40 -0600 | [diff] [blame] | 1671 | self.assertIn("'fill' entry must have a size property", |
| 1672 | str(e.exception)) |
| 1673 | |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1674 | def _HandleGbbCommand(self, pipe_list): |
| 1675 | """Fake calls to the futility utility""" |
| 1676 | if pipe_list[0][0] == 'futility': |
| 1677 | fname = pipe_list[0][-1] |
| 1678 | # Append our GBB data to the file, which will happen every time the |
| 1679 | # futility command is called. |
Simon Glass | 3348666 | 2019-05-14 15:53:42 -0600 | [diff] [blame] | 1680 | with open(fname, 'ab') as fd: |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1681 | fd.write(GBB_DATA) |
| 1682 | return command.CommandResult() |
| 1683 | |
| 1684 | def testGbb(self): |
| 1685 | """Test for the Chromium OS Google Binary Block""" |
| 1686 | command.test_result = self._HandleGbbCommand |
| 1687 | entry_args = { |
| 1688 | 'keydir': 'devkeys', |
| 1689 | 'bmpblk': 'bmpblk.bin', |
| 1690 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1691 | data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1692 | |
| 1693 | # Since futility |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1694 | expected = (GBB_DATA + GBB_DATA + tools.GetBytes(0, 8) + |
| 1695 | tools.GetBytes(0, 0x2180 - 16)) |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1696 | self.assertEqual(expected, data) |
| 1697 | |
| 1698 | def testGbbTooSmall(self): |
| 1699 | """Test for the Chromium OS Google Binary Block being large enough""" |
| 1700 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1701 | self._DoReadFileDtb('072_gbb_too_small.dts') |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1702 | self.assertIn("Node '/binman/gbb': GBB is too small", |
| 1703 | str(e.exception)) |
| 1704 | |
| 1705 | def testGbbNoSize(self): |
| 1706 | """Test for the Chromium OS Google Binary Block having a size""" |
| 1707 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1708 | self._DoReadFileDtb('073_gbb_no_size.dts') |
Simon Glass | c1ae83c | 2018-07-17 13:25:44 -0600 | [diff] [blame] | 1709 | self.assertIn("Node '/binman/gbb': GBB must have a fixed size", |
| 1710 | str(e.exception)) |
| 1711 | |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1712 | def _HandleVblockCommand(self, pipe_list): |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1713 | """Fake calls to the futility utility |
| 1714 | |
| 1715 | The expected pipe is: |
| 1716 | |
| 1717 | [('futility', 'vbutil_firmware', '--vblock', |
| 1718 | 'vblock.vblock', '--keyblock', 'devkeys/firmware.keyblock', |
| 1719 | '--signprivate', 'devkeys/firmware_data_key.vbprivk', |
| 1720 | '--version', '1', '--fv', 'input.vblock', '--kernelkey', |
| 1721 | 'devkeys/kernel_subkey.vbpubk', '--flags', '1')] |
| 1722 | |
| 1723 | This writes to the output file (here, 'vblock.vblock'). If |
| 1724 | self._hash_data is False, it writes VBLOCK_DATA, else it writes a hash |
| 1725 | of the input data (here, 'input.vblock'). |
| 1726 | """ |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1727 | if pipe_list[0][0] == 'futility': |
| 1728 | fname = pipe_list[0][3] |
Simon Glass | 639505b | 2018-09-14 04:57:11 -0600 | [diff] [blame] | 1729 | with open(fname, 'wb') as fd: |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1730 | if self._hash_data: |
| 1731 | infile = pipe_list[0][11] |
| 1732 | m = hashlib.sha256() |
| 1733 | data = tools.ReadFile(infile) |
| 1734 | m.update(data) |
| 1735 | fd.write(m.digest()) |
| 1736 | else: |
| 1737 | fd.write(VBLOCK_DATA) |
| 1738 | |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1739 | return command.CommandResult() |
| 1740 | |
| 1741 | def testVblock(self): |
| 1742 | """Test for the Chromium OS Verified Boot Block""" |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1743 | self._hash_data = False |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1744 | command.test_result = self._HandleVblockCommand |
| 1745 | entry_args = { |
| 1746 | 'keydir': 'devkeys', |
| 1747 | } |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1748 | data, _, _, _ = self._DoReadFileDtb('074_vblock.dts', |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1749 | entry_args=entry_args) |
| 1750 | expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA |
| 1751 | self.assertEqual(expected, data) |
| 1752 | |
| 1753 | def testVblockNoContent(self): |
| 1754 | """Test we detect a vblock which has no content to sign""" |
| 1755 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1756 | self._DoReadFile('075_vblock_no_content.dts') |
Simon Glass | e191578 | 2021-03-21 18:24:31 +1300 | [diff] [blame] | 1757 | self.assertIn("Node '/binman/vblock': Collection must have a 'content' " |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1758 | 'property', str(e.exception)) |
| 1759 | |
| 1760 | def testVblockBadPhandle(self): |
| 1761 | """Test that we detect a vblock with an invalid phandle in contents""" |
| 1762 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1763 | self._DoReadFile('076_vblock_bad_phandle.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1764 | self.assertIn("Node '/binman/vblock': Cannot find node for phandle " |
| 1765 | '1000', str(e.exception)) |
| 1766 | |
| 1767 | def testVblockBadEntry(self): |
| 1768 | """Test that we detect an entry that points to a non-entry""" |
| 1769 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1770 | self._DoReadFile('077_vblock_bad_entry.dts') |
Simon Glass | 5c35016 | 2018-07-17 13:25:47 -0600 | [diff] [blame] | 1771 | self.assertIn("Node '/binman/vblock': Cannot find entry for node " |
| 1772 | "'other'", str(e.exception)) |
| 1773 | |
Simon Glass | 220c622 | 2021-01-06 21:35:17 -0700 | [diff] [blame] | 1774 | def testVblockContent(self): |
| 1775 | """Test that the vblock signs the right data""" |
| 1776 | self._hash_data = True |
| 1777 | command.test_result = self._HandleVblockCommand |
| 1778 | entry_args = { |
| 1779 | 'keydir': 'devkeys', |
| 1780 | } |
| 1781 | data = self._DoReadFileDtb( |
| 1782 | '189_vblock_content.dts', use_real_dtb=True, update_dtb=True, |
| 1783 | entry_args=entry_args)[0] |
| 1784 | hashlen = 32 # SHA256 hash is 32 bytes |
| 1785 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 1786 | hashval = data[-hashlen:] |
| 1787 | dtb = data[len(U_BOOT_DATA):-hashlen] |
| 1788 | |
| 1789 | expected_data = U_BOOT_DATA + dtb |
| 1790 | |
| 1791 | # The hashval should be a hash of the dtb |
| 1792 | m = hashlib.sha256() |
| 1793 | m.update(expected_data) |
| 1794 | expected_hashval = m.digest() |
| 1795 | self.assertEqual(expected_hashval, hashval) |
| 1796 | |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1797 | def testTpl(self): |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1798 | """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] | 1799 | # ELF file with a '__bss_size' symbol |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 1800 | self._SetupTplElf() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1801 | data = self._DoReadFile('078_u_boot_tpl.dts') |
Simon Glass | 8425a1f | 2018-07-17 13:25:48 -0600 | [diff] [blame] | 1802 | self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data) |
| 1803 | |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1804 | def testUsesPos(self): |
| 1805 | """Test that the 'pos' property cannot be used anymore""" |
| 1806 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1807 | data = self._DoReadFile('079_uses_pos.dts') |
Simon Glass | 24b9744 | 2018-07-17 13:25:51 -0600 | [diff] [blame] | 1808 | self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of " |
| 1809 | "'pos'", str(e.exception)) |
| 1810 | |
Simon Glass | 274bf09 | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1811 | def testFillZero(self): |
| 1812 | """Test for an fill entry type with a size of 0""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1813 | data = self._DoReadFile('080_fill_empty.dts') |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 1814 | self.assertEqual(tools.GetBytes(0, 16), data) |
Simon Glass | 274bf09 | 2018-09-14 04:57:08 -0600 | [diff] [blame] | 1815 | |
Simon Glass | 267de43 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1816 | def testTextMissing(self): |
| 1817 | """Test for a text entry type where there is no text""" |
| 1818 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1819 | self._DoReadFileDtb('066_text.dts',) |
Simon Glass | 267de43 | 2018-09-14 04:57:09 -0600 | [diff] [blame] | 1820 | self.assertIn("Node '/binman/text': No value provided for text label " |
| 1821 | "'test-id'", str(e.exception)) |
| 1822 | |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1823 | def testPackStart16Tpl(self): |
| 1824 | """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] | 1825 | data = self._DoReadFile('081_x86_start16_tpl.dts') |
Simon Glass | ed40e96 | 2018-09-14 04:57:10 -0600 | [diff] [blame] | 1826 | self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)]) |
| 1827 | |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1828 | def testSelectImage(self): |
| 1829 | """Test that we can select which images to build""" |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1830 | expected = 'Skipping images: image1' |
| 1831 | |
| 1832 | # We should only get the expected message in verbose mode |
Simon Glass | 8a50b4a | 2019-07-08 13:18:48 -0600 | [diff] [blame] | 1833 | for verbosity in (0, 2): |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1834 | with test_util.capture_sys_output() as (stdout, stderr): |
| 1835 | retcode = self._DoTestFile('006_dual_image.dts', |
| 1836 | verbosity=verbosity, |
| 1837 | images=['image2']) |
| 1838 | self.assertEqual(0, retcode) |
| 1839 | if verbosity: |
| 1840 | self.assertIn(expected, stdout.getvalue()) |
| 1841 | else: |
| 1842 | self.assertNotIn(expected, stdout.getvalue()) |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1843 | |
Simon Glass | b4595d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 1844 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin'))) |
| 1845 | self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin'))) |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 1846 | self._CleanupOutputDir() |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 1847 | |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1848 | def testUpdateFdtAll(self): |
| 1849 | """Test that all device trees are updated with offset/size info""" |
Simon Glass | 5b4bce3 | 2019-07-08 14:25:26 -0600 | [diff] [blame] | 1850 | data = self._DoReadFileRealDtb('082_fdt_update_all.dts') |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1851 | |
| 1852 | base_expected = { |
| 1853 | 'section:image-pos': 0, |
| 1854 | 'u-boot-tpl-dtb:size': 513, |
| 1855 | 'u-boot-spl-dtb:size': 513, |
| 1856 | 'u-boot-spl-dtb:offset': 493, |
| 1857 | 'image-pos': 0, |
| 1858 | 'section/u-boot-dtb:image-pos': 0, |
| 1859 | 'u-boot-spl-dtb:image-pos': 493, |
| 1860 | 'section/u-boot-dtb:size': 493, |
| 1861 | 'u-boot-tpl-dtb:image-pos': 1006, |
| 1862 | 'section/u-boot-dtb:offset': 0, |
| 1863 | 'section:size': 493, |
| 1864 | 'offset': 0, |
| 1865 | 'section:offset': 0, |
| 1866 | 'u-boot-tpl-dtb:offset': 1006, |
| 1867 | 'size': 1519 |
| 1868 | } |
| 1869 | |
| 1870 | # We expect three device-tree files in the output, one after the other. |
| 1871 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 1872 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 1873 | # main U-Boot tree. All three should have the same postions and offset. |
| 1874 | start = 0 |
| 1875 | for item in ['', 'spl', 'tpl']: |
| 1876 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1877 | dtb.Scan() |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 1878 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS + |
| 1879 | ['spl', 'tpl']) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1880 | expected = dict(base_expected) |
| 1881 | if item: |
| 1882 | expected[item] = 0 |
| 1883 | self.assertEqual(expected, props) |
| 1884 | start += dtb._fdt_obj.totalsize() |
| 1885 | |
| 1886 | def testUpdateFdtOutput(self): |
| 1887 | """Test that output DTB files are updated""" |
| 1888 | try: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1889 | data, dtb_data, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts', |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1890 | use_real_dtb=True, update_dtb=True, reset_dtbs=False) |
| 1891 | |
| 1892 | # Unfortunately, compiling a source file always results in a file |
| 1893 | # called source.dtb (see fdt_util.EnsureCompiled()). The test |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1894 | # 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] | 1895 | # binman as a file called u-boot.dtb. To fix this, copy the file |
| 1896 | # over to the expected place. |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 1897 | start = 0 |
| 1898 | for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out', |
| 1899 | 'tpl/u-boot-tpl.dtb.out']: |
| 1900 | dtb = fdt.Fdt.FromData(data[start:]) |
| 1901 | size = dtb._fdt_obj.totalsize() |
| 1902 | pathname = tools.GetOutputFilename(os.path.split(fname)[1]) |
| 1903 | outdata = tools.ReadFile(pathname) |
| 1904 | name = os.path.split(fname)[0] |
| 1905 | |
| 1906 | if name: |
| 1907 | orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name) |
| 1908 | else: |
| 1909 | orig_indata = dtb_data |
| 1910 | self.assertNotEqual(outdata, orig_indata, |
| 1911 | "Expected output file '%s' be updated" % pathname) |
| 1912 | self.assertEqual(outdata, data[start:start + size], |
| 1913 | "Expected output file '%s' to match output image" % |
| 1914 | pathname) |
| 1915 | start += size |
| 1916 | finally: |
| 1917 | self._ResetDtbs() |
| 1918 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1919 | def _decompress(self, data): |
Simon Glass | ccec026 | 2019-07-08 13:18:42 -0600 | [diff] [blame] | 1920 | return tools.Decompress(data, 'lz4') |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1921 | |
| 1922 | def testCompress(self): |
| 1923 | """Test compression of blobs""" |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 1924 | self._CheckLz4() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1925 | data, _, _, out_dtb_fname = self._DoReadFileDtb('083_compress.dts', |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1926 | use_real_dtb=True, update_dtb=True) |
| 1927 | dtb = fdt.Fdt(out_dtb_fname) |
| 1928 | dtb.Scan() |
| 1929 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 1930 | orig = self._decompress(data) |
| 1931 | self.assertEquals(COMPRESS_DATA, orig) |
Simon Glass | 789b3440 | 2020-10-26 17:40:15 -0600 | [diff] [blame] | 1932 | |
| 1933 | # Do a sanity check on various fields |
| 1934 | image = control.images['image'] |
| 1935 | entries = image.GetEntries() |
| 1936 | self.assertEqual(1, len(entries)) |
| 1937 | |
| 1938 | entry = entries['blob'] |
| 1939 | self.assertEqual(COMPRESS_DATA, entry.uncomp_data) |
| 1940 | self.assertEqual(len(COMPRESS_DATA), entry.uncomp_size) |
| 1941 | orig = self._decompress(entry.data) |
| 1942 | self.assertEqual(orig, entry.uncomp_data) |
| 1943 | |
Simon Glass | 72eeff1 | 2020-10-26 17:40:16 -0600 | [diff] [blame] | 1944 | self.assertEqual(image.data, entry.data) |
| 1945 | |
Simon Glass | 7ba3359 | 2018-09-14 04:57:26 -0600 | [diff] [blame] | 1946 | expected = { |
| 1947 | 'blob:uncomp-size': len(COMPRESS_DATA), |
| 1948 | 'blob:size': len(data), |
| 1949 | 'size': len(data), |
| 1950 | } |
| 1951 | self.assertEqual(expected, props) |
| 1952 | |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1953 | def testFiles(self): |
| 1954 | """Test bringing in multiple files""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1955 | data = self._DoReadFile('084_files.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1956 | self.assertEqual(FILES_DATA, data) |
| 1957 | |
| 1958 | def testFilesCompress(self): |
| 1959 | """Test bringing in multiple files and compressing them""" |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 1960 | self._CheckLz4() |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1961 | data = self._DoReadFile('085_files_compress.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1962 | |
| 1963 | image = control.images['image'] |
| 1964 | entries = image.GetEntries() |
| 1965 | files = entries['files'] |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 1966 | entries = files._entries |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1967 | |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1968 | orig = b'' |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1969 | for i in range(1, 3): |
| 1970 | key = '%d.dat' % i |
| 1971 | start = entries[key].image_pos |
| 1972 | len = entries[key].size |
| 1973 | chunk = data[start:start + len] |
| 1974 | orig += self._decompress(chunk) |
| 1975 | |
| 1976 | self.assertEqual(FILES_DATA, orig) |
| 1977 | |
| 1978 | def testFilesMissing(self): |
| 1979 | """Test missing files""" |
| 1980 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1981 | data = self._DoReadFile('086_files_none.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1982 | self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched " |
| 1983 | 'no files', str(e.exception)) |
| 1984 | |
| 1985 | def testFilesNoPattern(self): |
| 1986 | """Test missing files""" |
| 1987 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1988 | data = self._DoReadFile('087_files_no_pattern.dts') |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 1989 | self.assertIn("Node '/binman/files': Missing 'pattern' property", |
| 1990 | str(e.exception)) |
| 1991 | |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1992 | def testExpandSize(self): |
| 1993 | """Test an expanding entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 1994 | data, _, map_data, _ = self._DoReadFileDtb('088_expand_size.dts', |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 1995 | map=True) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 1996 | expect = (tools.GetBytes(ord('a'), 8) + U_BOOT_DATA + |
| 1997 | MRC_DATA + tools.GetBytes(ord('b'), 1) + U_BOOT_DATA + |
| 1998 | tools.GetBytes(ord('c'), 8) + U_BOOT_DATA + |
| 1999 | tools.GetBytes(ord('d'), 8)) |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 2000 | self.assertEqual(expect, data) |
| 2001 | self.assertEqual('''ImagePos Offset Size Name |
| 2002 | 00000000 00000000 00000028 main-section |
| 2003 | 00000000 00000000 00000008 fill |
| 2004 | 00000008 00000008 00000004 u-boot |
| 2005 | 0000000c 0000000c 00000004 section |
| 2006 | 0000000c 00000000 00000003 intel-mrc |
| 2007 | 00000010 00000010 00000004 u-boot2 |
| 2008 | 00000014 00000014 0000000c section2 |
| 2009 | 00000014 00000000 00000008 fill |
| 2010 | 0000001c 00000008 00000004 u-boot |
| 2011 | 00000020 00000020 00000008 fill2 |
| 2012 | ''', map_data) |
| 2013 | |
| 2014 | def testExpandSizeBad(self): |
| 2015 | """Test an expanding entry which fails to provide contents""" |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2016 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2017 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2018 | self._DoReadFileDtb('089_expand_size_bad.dts', map=True) |
Simon Glass | fa79a81 | 2018-09-14 04:57:29 -0600 | [diff] [blame] | 2019 | self.assertIn("Node '/binman/_testing': Cannot obtain contents when " |
| 2020 | 'expanding entry', str(e.exception)) |
| 2021 | |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2022 | def testHash(self): |
| 2023 | """Test hashing of the contents of an entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2024 | _, _, _, out_dtb_fname = self._DoReadFileDtb('090_hash.dts', |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2025 | use_real_dtb=True, update_dtb=True) |
| 2026 | dtb = fdt.Fdt(out_dtb_fname) |
| 2027 | dtb.Scan() |
| 2028 | hash_node = dtb.GetNode('/binman/u-boot/hash').props['value'] |
| 2029 | m = hashlib.sha256() |
| 2030 | m.update(U_BOOT_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2031 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2032 | |
| 2033 | def testHashNoAlgo(self): |
| 2034 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2035 | self._DoReadFileDtb('091_hash_no_algo.dts', update_dtb=True) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2036 | self.assertIn("Node \'/binman/u-boot\': Missing \'algo\' property for " |
| 2037 | 'hash node', str(e.exception)) |
| 2038 | |
| 2039 | def testHashBadAlgo(self): |
| 2040 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2041 | self._DoReadFileDtb('092_hash_bad_algo.dts', update_dtb=True) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2042 | self.assertIn("Node '/binman/u-boot': Unknown hash algorithm", |
| 2043 | str(e.exception)) |
| 2044 | |
| 2045 | def testHashSection(self): |
| 2046 | """Test hashing of the contents of an entry""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2047 | _, _, _, out_dtb_fname = self._DoReadFileDtb('099_hash_section.dts', |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2048 | use_real_dtb=True, update_dtb=True) |
| 2049 | dtb = fdt.Fdt(out_dtb_fname) |
| 2050 | dtb.Scan() |
| 2051 | hash_node = dtb.GetNode('/binman/section/hash').props['value'] |
| 2052 | m = hashlib.sha256() |
| 2053 | m.update(U_BOOT_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2054 | m.update(tools.GetBytes(ord('a'), 16)) |
| 2055 | self.assertEqual(m.digest(), b''.join(hash_node.value)) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 2056 | |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 2057 | def testPackUBootTplMicrocode(self): |
| 2058 | """Test that x86 microcode can be handled correctly in TPL |
| 2059 | |
| 2060 | We expect to see the following in the image, in order: |
| 2061 | u-boot-tpl-nodtb.bin with a microcode pointer inserted at the correct |
| 2062 | place |
| 2063 | u-boot-tpl.dtb with the microcode removed |
| 2064 | the microcode |
| 2065 | """ |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2066 | self._SetupTplElf('u_boot_ucode_ptr') |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2067 | first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts', |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 2068 | U_BOOT_TPL_NODTB_DATA) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2069 | self.assertEqual(b'tplnodtb with microc' + pos_and_size + |
| 2070 | b'ter somewhere in here', first) |
Simon Glass | 3fb4f42 | 2018-09-14 04:57:32 -0600 | [diff] [blame] | 2071 | |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2072 | def testFmapX86(self): |
| 2073 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2074 | data = self._DoReadFile('094_fmap_x86.dts') |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2075 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2076 | expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('a'), 32 - 7) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2077 | self.assertEqual(expected, data[:32]) |
| 2078 | fhdr, fentries = fmap_util.DecodeFmap(data[32:]) |
| 2079 | |
| 2080 | self.assertEqual(0x100, fhdr.image_size) |
| 2081 | |
| 2082 | self.assertEqual(0, fentries[0].offset) |
| 2083 | self.assertEqual(4, fentries[0].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2084 | self.assertEqual(b'U_BOOT', fentries[0].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2085 | |
| 2086 | self.assertEqual(4, fentries[1].offset) |
| 2087 | self.assertEqual(3, fentries[1].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2088 | self.assertEqual(b'INTEL_MRC', fentries[1].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2089 | |
| 2090 | self.assertEqual(32, fentries[2].offset) |
| 2091 | self.assertEqual(fmap_util.FMAP_HEADER_LEN + |
| 2092 | fmap_util.FMAP_AREA_LEN * 3, fentries[2].size) |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2093 | self.assertEqual(b'FMAP', fentries[2].name) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2094 | |
| 2095 | def testFmapX86Section(self): |
| 2096 | """Basic test of generation of a flashrom fmap""" |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2097 | data = self._DoReadFile('095_fmap_x86_section.dts') |
Simon Glass | 303f62f | 2019-05-17 22:00:46 -0600 | [diff] [blame] | 2098 | expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('b'), 32 - 7) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2099 | self.assertEqual(expected, data[:32]) |
| 2100 | fhdr, fentries = fmap_util.DecodeFmap(data[36:]) |
| 2101 | |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 2102 | self.assertEqual(0x180, fhdr.image_size) |
| 2103 | expect_size = fmap_util.FMAP_HEADER_LEN + fmap_util.FMAP_AREA_LEN * 4 |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2104 | fiter = iter(fentries) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2105 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2106 | fentry = next(fiter) |
| 2107 | self.assertEqual(b'U_BOOT', fentry.name) |
| 2108 | self.assertEqual(0, fentry.offset) |
| 2109 | self.assertEqual(4, fentry.size) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2110 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2111 | fentry = next(fiter) |
Simon Glass | b1d414c | 2021-04-03 11:05:10 +1300 | [diff] [blame] | 2112 | self.assertEqual(b'SECTION', fentry.name) |
| 2113 | self.assertEqual(4, fentry.offset) |
| 2114 | self.assertEqual(0x20 + expect_size, fentry.size) |
| 2115 | |
| 2116 | fentry = next(fiter) |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2117 | self.assertEqual(b'INTEL_MRC', fentry.name) |
| 2118 | self.assertEqual(4, fentry.offset) |
| 2119 | self.assertEqual(3, fentry.size) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2120 | |
Simon Glass | 82059c2 | 2021-04-03 11:05:09 +1300 | [diff] [blame] | 2121 | fentry = next(fiter) |
| 2122 | self.assertEqual(b'FMAP', fentry.name) |
| 2123 | self.assertEqual(36, fentry.offset) |
| 2124 | self.assertEqual(expect_size, fentry.size) |
Simon Glass | c64aea5 | 2018-09-14 04:57:34 -0600 | [diff] [blame] | 2125 | |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2126 | def testElf(self): |
| 2127 | """Basic test of ELF entries""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 2128 | self._SetupSplElf() |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2129 | self._SetupTplElf() |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 2130 | with open(self.ElfTestFile('bss_data'), 'rb') as fd: |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2131 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2132 | data = self._DoReadFile('096_elf.dts') |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2133 | |
Simon Glass | 0d67379 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 2134 | def testElfStrip(self): |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2135 | """Basic test of ELF entries""" |
Simon Glass | 7057d02 | 2018-10-01 21:12:47 -0600 | [diff] [blame] | 2136 | self._SetupSplElf() |
Simon Glass | f629089 | 2019-08-24 07:22:53 -0600 | [diff] [blame] | 2137 | with open(self.ElfTestFile('bss_data'), 'rb') as fd: |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2138 | TestFunctional._MakeInputFile('-boot', fd.read()) |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2139 | data = self._DoReadFile('097_elf_strip.dts') |
Simon Glass | b171423 | 2018-09-14 04:57:35 -0600 | [diff] [blame] | 2140 | |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2141 | def testPackOverlapMap(self): |
| 2142 | """Test that overlapping regions are detected""" |
| 2143 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2144 | with self.assertRaises(ValueError) as e: |
Simon Glass | 511f658 | 2018-10-01 12:22:30 -0600 | [diff] [blame] | 2145 | self._DoTestFile('014_pack_overlap.dts', map=True) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2146 | map_fname = tools.GetOutputFilename('image.map') |
| 2147 | self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname, |
| 2148 | stdout.getvalue()) |
| 2149 | |
| 2150 | # We should not get an inmage, but there should be a map file |
| 2151 | self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin'))) |
| 2152 | self.assertTrue(os.path.exists(map_fname)) |
Simon Glass | b377475 | 2019-05-17 22:00:51 -0600 | [diff] [blame] | 2153 | map_data = tools.ReadFile(map_fname, binary=False) |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2154 | self.assertEqual('''ImagePos Offset Size Name |
Simon Glass | d99850b | 2020-10-26 17:40:24 -0600 | [diff] [blame] | 2155 | <none> 00000000 00000008 main-section |
Simon Glass | cd817d5 | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 2156 | <none> 00000000 00000004 u-boot |
| 2157 | <none> 00000003 00000004 u-boot-align |
| 2158 | ''', map_data) |
| 2159 | |
Simon Glass | 0d67379 | 2019-07-08 13:18:25 -0600 | [diff] [blame] | 2160 | def testPackRefCode(self): |
Simon Glass | 41902e4 | 2018-10-01 12:22:31 -0600 | [diff] [blame] | 2161 | """Test that an image with an Intel Reference code binary works""" |
| 2162 | data = self._DoReadFile('100_intel_refcode.dts') |
| 2163 | self.assertEqual(REFCODE_DATA, data[:len(REFCODE_DATA)]) |
| 2164 | |
Simon Glass | eb023b3 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 2165 | def testSectionOffset(self): |
| 2166 | """Tests use of a section with an offset""" |
| 2167 | data, _, map_data, _ = self._DoReadFileDtb('101_sections_offset.dts', |
| 2168 | map=True) |
| 2169 | self.assertEqual('''ImagePos Offset Size Name |
| 2170 | 00000000 00000000 00000038 main-section |
| 2171 | 00000004 00000004 00000010 section@0 |
| 2172 | 00000004 00000000 00000004 u-boot |
| 2173 | 00000018 00000018 00000010 section@1 |
| 2174 | 00000018 00000000 00000004 u-boot |
| 2175 | 0000002c 0000002c 00000004 section@2 |
| 2176 | 0000002c 00000000 00000004 u-boot |
| 2177 | ''', map_data) |
| 2178 | self.assertEqual(data, |
Simon Glass | ac0d495 | 2019-05-14 15:53:47 -0600 | [diff] [blame] | 2179 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 2180 | tools.GetBytes(0x21, 12) + |
| 2181 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 2182 | tools.GetBytes(0x61, 12) + |
| 2183 | tools.GetBytes(0x26, 4) + U_BOOT_DATA + |
| 2184 | tools.GetBytes(0x26, 8)) |
Simon Glass | eb023b3 | 2019-04-25 21:58:39 -0600 | [diff] [blame] | 2185 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 2186 | def testCbfsRaw(self): |
| 2187 | """Test base handling of a Coreboot Filesystem (CBFS) |
| 2188 | |
| 2189 | The exact contents of the CBFS is verified by similar tests in |
| 2190 | cbfs_util_test.py. The tests here merely check that the files added to |
| 2191 | the CBFS can be found in the final image. |
| 2192 | """ |
| 2193 | data = self._DoReadFile('102_cbfs_raw.dts') |
| 2194 | size = 0xb0 |
| 2195 | |
| 2196 | cbfs = cbfs_util.CbfsReader(data) |
| 2197 | self.assertEqual(size, cbfs.rom_size) |
| 2198 | |
| 2199 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2200 | cfile = cbfs.files['u-boot-dtb'] |
| 2201 | self.assertEqual(U_BOOT_DTB_DATA, cfile.data) |
| 2202 | |
| 2203 | def testCbfsArch(self): |
| 2204 | """Test on non-x86 architecture""" |
| 2205 | data = self._DoReadFile('103_cbfs_raw_ppc.dts') |
| 2206 | size = 0x100 |
| 2207 | |
| 2208 | cbfs = cbfs_util.CbfsReader(data) |
| 2209 | self.assertEqual(size, cbfs.rom_size) |
| 2210 | |
| 2211 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2212 | cfile = cbfs.files['u-boot-dtb'] |
| 2213 | self.assertEqual(U_BOOT_DTB_DATA, cfile.data) |
| 2214 | |
| 2215 | def testCbfsStage(self): |
| 2216 | """Tests handling of a Coreboot Filesystem (CBFS)""" |
| 2217 | if not elf.ELF_TOOLS: |
| 2218 | self.skipTest('Python elftools not available') |
| 2219 | elf_fname = os.path.join(self._indir, 'cbfs-stage.elf') |
| 2220 | elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA) |
| 2221 | size = 0xb0 |
| 2222 | |
| 2223 | data = self._DoReadFile('104_cbfs_stage.dts') |
| 2224 | cbfs = cbfs_util.CbfsReader(data) |
| 2225 | self.assertEqual(size, cbfs.rom_size) |
| 2226 | |
| 2227 | self.assertIn('u-boot', cbfs.files) |
| 2228 | cfile = cbfs.files['u-boot'] |
| 2229 | self.assertEqual(U_BOOT_DATA + U_BOOT_DTB_DATA, cfile.data) |
| 2230 | |
| 2231 | def testCbfsRawCompress(self): |
| 2232 | """Test handling of compressing raw files""" |
| 2233 | self._CheckLz4() |
| 2234 | data = self._DoReadFile('105_cbfs_raw_compress.dts') |
| 2235 | size = 0x140 |
| 2236 | |
| 2237 | cbfs = cbfs_util.CbfsReader(data) |
| 2238 | self.assertIn('u-boot', cbfs.files) |
| 2239 | cfile = cbfs.files['u-boot'] |
| 2240 | self.assertEqual(COMPRESS_DATA, cfile.data) |
| 2241 | |
| 2242 | def testCbfsBadArch(self): |
| 2243 | """Test handling of a bad architecture""" |
| 2244 | with self.assertRaises(ValueError) as e: |
| 2245 | self._DoReadFile('106_cbfs_bad_arch.dts') |
| 2246 | self.assertIn("Invalid architecture 'bad-arch'", str(e.exception)) |
| 2247 | |
| 2248 | def testCbfsNoSize(self): |
| 2249 | """Test handling of a missing size property""" |
| 2250 | with self.assertRaises(ValueError) as e: |
| 2251 | self._DoReadFile('107_cbfs_no_size.dts') |
| 2252 | self.assertIn('entry must have a size property', str(e.exception)) |
| 2253 | |
| 2254 | def testCbfsNoCOntents(self): |
| 2255 | """Test handling of a CBFS entry which does not provide contentsy""" |
| 2256 | with self.assertRaises(ValueError) as e: |
| 2257 | self._DoReadFile('108_cbfs_no_contents.dts') |
| 2258 | self.assertIn('Could not complete processing of contents', |
| 2259 | str(e.exception)) |
| 2260 | |
| 2261 | def testCbfsBadCompress(self): |
| 2262 | """Test handling of a bad architecture""" |
| 2263 | with self.assertRaises(ValueError) as e: |
| 2264 | self._DoReadFile('109_cbfs_bad_compress.dts') |
| 2265 | self.assertIn("Invalid compression in 'u-boot': 'invalid-algo'", |
| 2266 | str(e.exception)) |
| 2267 | |
| 2268 | def testCbfsNamedEntries(self): |
| 2269 | """Test handling of named entries""" |
| 2270 | data = self._DoReadFile('110_cbfs_name.dts') |
| 2271 | |
| 2272 | cbfs = cbfs_util.CbfsReader(data) |
| 2273 | self.assertIn('FRED', cbfs.files) |
| 2274 | cfile1 = cbfs.files['FRED'] |
| 2275 | self.assertEqual(U_BOOT_DATA, cfile1.data) |
| 2276 | |
| 2277 | self.assertIn('hello', cbfs.files) |
| 2278 | cfile2 = cbfs.files['hello'] |
| 2279 | self.assertEqual(U_BOOT_DTB_DATA, cfile2.data) |
| 2280 | |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2281 | def _SetupIfwi(self, fname): |
| 2282 | """Set up to run an IFWI test |
| 2283 | |
| 2284 | Args: |
| 2285 | fname: Filename of input file to provide (fitimage.bin or ifwi.bin) |
| 2286 | """ |
| 2287 | self._SetupSplElf() |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 2288 | self._SetupTplElf() |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2289 | |
| 2290 | # Intel Integrated Firmware Image (IFWI) file |
| 2291 | with gzip.open(self.TestFile('%s.gz' % fname), 'rb') as fd: |
| 2292 | data = fd.read() |
| 2293 | TestFunctional._MakeInputFile(fname,data) |
| 2294 | |
| 2295 | def _CheckIfwi(self, data): |
| 2296 | """Check that an image with an IFWI contains the correct output |
| 2297 | |
| 2298 | Args: |
| 2299 | data: Conents of output file |
| 2300 | """ |
| 2301 | expected_desc = tools.ReadFile(self.TestFile('descriptor.bin')) |
| 2302 | if data[:0x1000] != expected_desc: |
| 2303 | self.fail('Expected descriptor binary at start of image') |
| 2304 | |
| 2305 | # We expect to find the TPL wil in subpart IBBP entry IBBL |
| 2306 | image_fname = tools.GetOutputFilename('image.bin') |
| 2307 | tpl_fname = tools.GetOutputFilename('tpl.out') |
| 2308 | tools.RunIfwiTool(image_fname, tools.CMD_EXTRACT, fname=tpl_fname, |
| 2309 | subpart='IBBP', entry_name='IBBL') |
| 2310 | |
| 2311 | tpl_data = tools.ReadFile(tpl_fname) |
Simon Glass | f55bd69 | 2019-08-24 07:22:51 -0600 | [diff] [blame] | 2312 | 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] | 2313 | |
| 2314 | def testPackX86RomIfwi(self): |
| 2315 | """Test that an x86 ROM with Integrated Firmware Image can be created""" |
| 2316 | self._SetupIfwi('fitimage.bin') |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2317 | data = self._DoReadFile('111_x86_rom_ifwi.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2318 | self._CheckIfwi(data) |
| 2319 | |
| 2320 | def testPackX86RomIfwiNoDesc(self): |
| 2321 | """Test that an x86 ROM with IFWI can be created from an ifwi.bin file""" |
| 2322 | self._SetupIfwi('ifwi.bin') |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2323 | data = self._DoReadFile('112_x86_rom_ifwi_nodesc.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2324 | self._CheckIfwi(data) |
| 2325 | |
| 2326 | def testPackX86RomIfwiNoData(self): |
| 2327 | """Test that an x86 ROM with IFWI handles missing data""" |
| 2328 | self._SetupIfwi('ifwi.bin') |
| 2329 | with self.assertRaises(ValueError) as e: |
Simon Glass | 1d16776 | 2019-08-24 07:23:01 -0600 | [diff] [blame] | 2330 | data = self._DoReadFile('113_x86_rom_ifwi_nodata.dts') |
Simon Glass | 759af87 | 2019-07-08 13:18:54 -0600 | [diff] [blame] | 2331 | self.assertIn('Could not complete processing of contents', |
| 2332 | str(e.exception)) |
Simon Glass | 91710b3 | 2018-07-17 13:25:32 -0600 | [diff] [blame] | 2333 | |
Simon Glass | c2f1aed | 2019-07-08 13:18:56 -0600 | [diff] [blame] | 2334 | def testCbfsOffset(self): |
| 2335 | """Test a CBFS with files at particular offsets |
| 2336 | |
| 2337 | Like all CFBS tests, this is just checking the logic that calls |
| 2338 | cbfs_util. See cbfs_util_test for fully tests (e.g. test_cbfs_offset()). |
| 2339 | """ |
| 2340 | data = self._DoReadFile('114_cbfs_offset.dts') |
| 2341 | size = 0x200 |
| 2342 | |
| 2343 | cbfs = cbfs_util.CbfsReader(data) |
| 2344 | self.assertEqual(size, cbfs.rom_size) |
| 2345 | |
| 2346 | self.assertIn('u-boot', cbfs.files) |
| 2347 | cfile = cbfs.files['u-boot'] |
| 2348 | self.assertEqual(U_BOOT_DATA, cfile.data) |
| 2349 | self.assertEqual(0x40, cfile.cbfs_offset) |
| 2350 | |
| 2351 | self.assertIn('u-boot-dtb', cbfs.files) |
| 2352 | cfile2 = cbfs.files['u-boot-dtb'] |
| 2353 | self.assertEqual(U_BOOT_DTB_DATA, cfile2.data) |
| 2354 | self.assertEqual(0x140, cfile2.cbfs_offset) |
| 2355 | |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2356 | def testFdtmap(self): |
| 2357 | """Test an FDT map can be inserted in the image""" |
| 2358 | data = self.data = self._DoReadFileRealDtb('115_fdtmap.dts') |
| 2359 | fdtmap_data = data[len(U_BOOT_DATA):] |
| 2360 | magic = fdtmap_data[:8] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2361 | self.assertEqual(b'_FDTMAP_', magic) |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2362 | self.assertEqual(tools.GetBytes(0, 8), fdtmap_data[8:16]) |
| 2363 | |
| 2364 | fdt_data = fdtmap_data[16:] |
| 2365 | dtb = fdt.Fdt.FromData(fdt_data) |
| 2366 | dtb.Scan() |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 2367 | props = self._GetPropTree(dtb, BASE_DTB_PROPS, prefix='/') |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2368 | self.assertEqual({ |
| 2369 | 'image-pos': 0, |
| 2370 | 'offset': 0, |
| 2371 | 'u-boot:offset': 0, |
| 2372 | 'u-boot:size': len(U_BOOT_DATA), |
| 2373 | 'u-boot:image-pos': 0, |
| 2374 | 'fdtmap:image-pos': 4, |
| 2375 | 'fdtmap:offset': 4, |
| 2376 | 'fdtmap:size': len(fdtmap_data), |
| 2377 | 'size': len(data), |
| 2378 | }, props) |
| 2379 | |
| 2380 | def testFdtmapNoMatch(self): |
| 2381 | """Check handling of an FDT map when the section cannot be found""" |
| 2382 | self.data = self._DoReadFileRealDtb('115_fdtmap.dts') |
| 2383 | |
| 2384 | # Mangle the section name, which should cause a mismatch between the |
| 2385 | # correct FDT path and the one expected by the section |
| 2386 | image = control.images['image'] |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2387 | image._node.path += '-suffix' |
Simon Glass | 0f62133 | 2019-07-08 14:25:27 -0600 | [diff] [blame] | 2388 | entries = image.GetEntries() |
| 2389 | fdtmap = entries['fdtmap'] |
| 2390 | with self.assertRaises(ValueError) as e: |
| 2391 | fdtmap._GetFdtmap() |
| 2392 | self.assertIn("Cannot locate node for path '/binman-suffix'", |
| 2393 | str(e.exception)) |
| 2394 | |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2395 | def testFdtmapHeader(self): |
| 2396 | """Test an FDT map and image header can be inserted in the image""" |
| 2397 | data = self.data = self._DoReadFileRealDtb('116_fdtmap_hdr.dts') |
| 2398 | fdtmap_pos = len(U_BOOT_DATA) |
| 2399 | fdtmap_data = data[fdtmap_pos:] |
| 2400 | fdt_data = fdtmap_data[16:] |
| 2401 | dtb = fdt.Fdt.FromData(fdt_data) |
| 2402 | fdt_size = dtb.GetFdtObj().totalsize() |
| 2403 | hdr_data = data[-8:] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2404 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2405 | offset = struct.unpack('<I', hdr_data[4:])[0] & 0xffffffff |
| 2406 | self.assertEqual(fdtmap_pos - 0x400, offset - (1 << 32)) |
| 2407 | |
| 2408 | def testFdtmapHeaderStart(self): |
| 2409 | """Test an image header can be inserted at the image start""" |
| 2410 | data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts') |
| 2411 | fdtmap_pos = 0x100 + len(U_BOOT_DATA) |
| 2412 | hdr_data = data[:8] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2413 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2414 | offset = struct.unpack('<I', hdr_data[4:])[0] |
| 2415 | self.assertEqual(fdtmap_pos, offset) |
| 2416 | |
| 2417 | def testFdtmapHeaderPos(self): |
| 2418 | """Test an image header can be inserted at a chosen position""" |
| 2419 | data = self.data = self._DoReadFileRealDtb('118_fdtmap_hdr_pos.dts') |
| 2420 | fdtmap_pos = 0x100 + len(U_BOOT_DATA) |
| 2421 | hdr_data = data[0x80:0x88] |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2422 | self.assertEqual(b'BinM', hdr_data[:4]) |
Simon Glass | cec34ba | 2019-07-08 14:25:28 -0600 | [diff] [blame] | 2423 | offset = struct.unpack('<I', hdr_data[4:])[0] |
| 2424 | self.assertEqual(fdtmap_pos, offset) |
| 2425 | |
| 2426 | def testHeaderMissingFdtmap(self): |
| 2427 | """Test an image header requires an fdtmap""" |
| 2428 | with self.assertRaises(ValueError) as e: |
| 2429 | self.data = self._DoReadFileRealDtb('119_fdtmap_hdr_missing.dts') |
| 2430 | self.assertIn("'image_header' section must have an 'fdtmap' sibling", |
| 2431 | str(e.exception)) |
| 2432 | |
| 2433 | def testHeaderNoLocation(self): |
| 2434 | """Test an image header with a no specified location is detected""" |
| 2435 | with self.assertRaises(ValueError) as e: |
| 2436 | self.data = self._DoReadFileRealDtb('120_hdr_no_location.dts') |
| 2437 | self.assertIn("Invalid location 'None', expected 'start' or 'end'", |
| 2438 | str(e.exception)) |
| 2439 | |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2440 | def testEntryExpand(self): |
| 2441 | """Test expanding an entry after it is packed""" |
| 2442 | data = self._DoReadFile('121_entry_expand.dts') |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 2443 | self.assertEqual(b'aaa', data[:3]) |
| 2444 | self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)]) |
| 2445 | self.assertEqual(b'aaa', data[-3:]) |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2446 | |
| 2447 | def testEntryExpandBad(self): |
| 2448 | """Test expanding an entry after it is packed, twice""" |
| 2449 | with self.assertRaises(ValueError) as e: |
| 2450 | self._DoReadFile('122_entry_expand_twice.dts') |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 2451 | self.assertIn("Image '/binman': Entries changed size after packing", |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2452 | str(e.exception)) |
| 2453 | |
| 2454 | def testEntryExpandSection(self): |
| 2455 | """Test expanding an entry within a section after it is packed""" |
| 2456 | data = self._DoReadFile('123_entry_expand_section.dts') |
Simon Glass | 8c702fb | 2019-07-20 12:23:57 -0600 | [diff] [blame] | 2457 | self.assertEqual(b'aaa', data[:3]) |
| 2458 | self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)]) |
| 2459 | self.assertEqual(b'aaa', data[-3:]) |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 2460 | |
Simon Glass | 90d2968 | 2019-07-08 14:25:38 -0600 | [diff] [blame] | 2461 | def testCompressDtb(self): |
| 2462 | """Test that compress of device-tree files is supported""" |
| 2463 | self._CheckLz4() |
| 2464 | data = self.data = self._DoReadFileRealDtb('124_compress_dtb.dts') |
| 2465 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 2466 | comp_data = data[len(U_BOOT_DATA):] |
| 2467 | orig = self._decompress(comp_data) |
| 2468 | dtb = fdt.Fdt.FromData(orig) |
| 2469 | dtb.Scan() |
| 2470 | props = self._GetPropTree(dtb, ['size', 'uncomp-size']) |
| 2471 | expected = { |
| 2472 | 'u-boot:size': len(U_BOOT_DATA), |
| 2473 | 'u-boot-dtb:uncomp-size': len(orig), |
| 2474 | 'u-boot-dtb:size': len(comp_data), |
| 2475 | 'size': len(data), |
| 2476 | } |
| 2477 | self.assertEqual(expected, props) |
| 2478 | |
Simon Glass | 151bbbf | 2019-07-08 14:25:41 -0600 | [diff] [blame] | 2479 | def testCbfsUpdateFdt(self): |
| 2480 | """Test that we can update the device tree with CBFS offset/size info""" |
| 2481 | self._CheckLz4() |
| 2482 | data, _, _, out_dtb_fname = self._DoReadFileDtb('125_cbfs_update.dts', |
| 2483 | update_dtb=True) |
| 2484 | dtb = fdt.Fdt(out_dtb_fname) |
| 2485 | dtb.Scan() |
Simon Glass | 2c6adba | 2019-07-20 12:23:47 -0600 | [diff] [blame] | 2486 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + ['uncomp-size']) |
Simon Glass | 151bbbf | 2019-07-08 14:25:41 -0600 | [diff] [blame] | 2487 | del props['cbfs/u-boot:size'] |
| 2488 | self.assertEqual({ |
| 2489 | 'offset': 0, |
| 2490 | 'size': len(data), |
| 2491 | 'image-pos': 0, |
| 2492 | 'cbfs:offset': 0, |
| 2493 | 'cbfs:size': len(data), |
| 2494 | 'cbfs:image-pos': 0, |
| 2495 | 'cbfs/u-boot:offset': 0x38, |
| 2496 | 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA), |
| 2497 | 'cbfs/u-boot:image-pos': 0x38, |
| 2498 | 'cbfs/u-boot-dtb:offset': 0xb8, |
| 2499 | 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA), |
| 2500 | 'cbfs/u-boot-dtb:image-pos': 0xb8, |
| 2501 | }, props) |
| 2502 | |
Simon Glass | 3c9b4f2 | 2019-07-08 14:25:42 -0600 | [diff] [blame] | 2503 | def testCbfsBadType(self): |
| 2504 | """Test an image header with a no specified location is detected""" |
| 2505 | with self.assertRaises(ValueError) as e: |
| 2506 | self._DoReadFile('126_cbfs_bad_type.dts') |
| 2507 | self.assertIn("Unknown cbfs-type 'badtype'", str(e.exception)) |
| 2508 | |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2509 | def testList(self): |
| 2510 | """Test listing the files in an image""" |
| 2511 | self._CheckLz4() |
| 2512 | data = self._DoReadFile('127_list.dts') |
| 2513 | image = control.images['image'] |
| 2514 | entries = image.BuildEntryList() |
| 2515 | self.assertEqual(7, len(entries)) |
| 2516 | |
| 2517 | ent = entries[0] |
| 2518 | self.assertEqual(0, ent.indent) |
| 2519 | self.assertEqual('main-section', ent.name) |
| 2520 | self.assertEqual('section', ent.etype) |
| 2521 | self.assertEqual(len(data), ent.size) |
| 2522 | self.assertEqual(0, ent.image_pos) |
| 2523 | self.assertEqual(None, ent.uncomp_size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2524 | self.assertEqual(0, ent.offset) |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2525 | |
| 2526 | ent = entries[1] |
| 2527 | self.assertEqual(1, ent.indent) |
| 2528 | self.assertEqual('u-boot', ent.name) |
| 2529 | self.assertEqual('u-boot', ent.etype) |
| 2530 | self.assertEqual(len(U_BOOT_DATA), ent.size) |
| 2531 | self.assertEqual(0, ent.image_pos) |
| 2532 | self.assertEqual(None, ent.uncomp_size) |
| 2533 | self.assertEqual(0, ent.offset) |
| 2534 | |
| 2535 | ent = entries[2] |
| 2536 | self.assertEqual(1, ent.indent) |
| 2537 | self.assertEqual('section', ent.name) |
| 2538 | self.assertEqual('section', ent.etype) |
| 2539 | section_size = ent.size |
| 2540 | self.assertEqual(0x100, ent.image_pos) |
| 2541 | self.assertEqual(None, ent.uncomp_size) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 2542 | self.assertEqual(0x100, ent.offset) |
Simon Glass | 6b156f8 | 2019-07-08 14:25:43 -0600 | [diff] [blame] | 2543 | |
| 2544 | ent = entries[3] |
| 2545 | self.assertEqual(2, ent.indent) |
| 2546 | self.assertEqual('cbfs', ent.name) |
| 2547 | self.assertEqual('cbfs', ent.etype) |
| 2548 | self.assertEqual(0x400, ent.size) |
| 2549 | self.assertEqual(0x100, ent.image_pos) |
| 2550 | self.assertEqual(None, ent.uncomp_size) |
| 2551 | self.assertEqual(0, ent.offset) |
| 2552 | |
| 2553 | ent = entries[4] |
| 2554 | self.assertEqual(3, ent.indent) |
| 2555 | self.assertEqual('u-boot', ent.name) |
| 2556 | self.assertEqual('u-boot', ent.etype) |
| 2557 | self.assertEqual(len(U_BOOT_DATA), ent.size) |
| 2558 | self.assertEqual(0x138, ent.image_pos) |
| 2559 | self.assertEqual(None, ent.uncomp_size) |
| 2560 | self.assertEqual(0x38, ent.offset) |
| 2561 | |
| 2562 | ent = entries[5] |
| 2563 | self.assertEqual(3, ent.indent) |
| 2564 | self.assertEqual('u-boot-dtb', ent.name) |
| 2565 | self.assertEqual('text', ent.etype) |
| 2566 | self.assertGreater(len(COMPRESS_DATA), ent.size) |
| 2567 | self.assertEqual(0x178, ent.image_pos) |
| 2568 | self.assertEqual(len(COMPRESS_DATA), ent.uncomp_size) |
| 2569 | self.assertEqual(0x78, ent.offset) |
| 2570 | |
| 2571 | ent = entries[6] |
| 2572 | self.assertEqual(2, ent.indent) |
| 2573 | self.assertEqual('u-boot-dtb', ent.name) |
| 2574 | self.assertEqual('u-boot-dtb', ent.etype) |
| 2575 | self.assertEqual(0x500, ent.image_pos) |
| 2576 | self.assertEqual(len(U_BOOT_DTB_DATA), ent.uncomp_size) |
| 2577 | dtb_size = ent.size |
| 2578 | # Compressing this data expands it since headers are added |
| 2579 | self.assertGreater(dtb_size, len(U_BOOT_DTB_DATA)) |
| 2580 | self.assertEqual(0x400, ent.offset) |
| 2581 | |
| 2582 | self.assertEqual(len(data), 0x100 + section_size) |
| 2583 | self.assertEqual(section_size, 0x400 + dtb_size) |
| 2584 | |
Simon Glass | 8d8bf4e | 2019-07-08 14:25:44 -0600 | [diff] [blame] | 2585 | def testFindFdtmap(self): |
| 2586 | """Test locating an FDT map in an image""" |
| 2587 | self._CheckLz4() |
| 2588 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
| 2589 | image = control.images['image'] |
| 2590 | entries = image.GetEntries() |
| 2591 | entry = entries['fdtmap'] |
| 2592 | self.assertEqual(entry.image_pos, fdtmap.LocateFdtmap(data)) |
| 2593 | |
| 2594 | def testFindFdtmapMissing(self): |
| 2595 | """Test failing to locate an FDP map""" |
| 2596 | data = self._DoReadFile('005_simple.dts') |
| 2597 | self.assertEqual(None, fdtmap.LocateFdtmap(data)) |
| 2598 | |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2599 | def testFindImageHeader(self): |
| 2600 | """Test locating a image header""" |
| 2601 | self._CheckLz4() |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2602 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2603 | image = control.images['image'] |
| 2604 | entries = image.GetEntries() |
| 2605 | entry = entries['fdtmap'] |
| 2606 | # The header should point to the FDT map |
| 2607 | self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data)) |
| 2608 | |
| 2609 | def testFindImageHeaderStart(self): |
| 2610 | """Test locating a image header located at the start of an image""" |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2611 | data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts') |
Simon Glass | ed39a3c | 2019-07-08 14:25:45 -0600 | [diff] [blame] | 2612 | image = control.images['image'] |
| 2613 | entries = image.GetEntries() |
| 2614 | entry = entries['fdtmap'] |
| 2615 | # The header should point to the FDT map |
| 2616 | self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data)) |
| 2617 | |
| 2618 | def testFindImageHeaderMissing(self): |
| 2619 | """Test failing to locate an image header""" |
| 2620 | data = self._DoReadFile('005_simple.dts') |
| 2621 | self.assertEqual(None, image_header.LocateHeaderOffset(data)) |
| 2622 | |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2623 | def testReadImage(self): |
| 2624 | """Test reading an image and accessing its FDT map""" |
| 2625 | self._CheckLz4() |
| 2626 | data = self.data = self._DoReadFileRealDtb('128_decode_image.dts') |
| 2627 | image_fname = tools.GetOutputFilename('image.bin') |
| 2628 | orig_image = control.images['image'] |
| 2629 | image = Image.FromFile(image_fname) |
| 2630 | self.assertEqual(orig_image.GetEntries().keys(), |
| 2631 | image.GetEntries().keys()) |
| 2632 | |
| 2633 | orig_entry = orig_image.GetEntries()['fdtmap'] |
| 2634 | entry = image.GetEntries()['fdtmap'] |
| 2635 | self.assertEquals(orig_entry.offset, entry.offset) |
| 2636 | self.assertEquals(orig_entry.size, entry.size) |
| 2637 | self.assertEquals(orig_entry.image_pos, entry.image_pos) |
| 2638 | |
| 2639 | def testReadImageNoHeader(self): |
| 2640 | """Test accessing an image's FDT map without an image header""" |
| 2641 | self._CheckLz4() |
| 2642 | data = self._DoReadFileRealDtb('129_decode_image_nohdr.dts') |
| 2643 | image_fname = tools.GetOutputFilename('image.bin') |
| 2644 | image = Image.FromFile(image_fname) |
| 2645 | self.assertTrue(isinstance(image, Image)) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 2646 | self.assertEqual('image', image.image_name[-5:]) |
Simon Glass | b8424fa | 2019-07-08 14:25:46 -0600 | [diff] [blame] | 2647 | |
| 2648 | def testReadImageFail(self): |
| 2649 | """Test failing to read an image image's FDT map""" |
| 2650 | self._DoReadFile('005_simple.dts') |
| 2651 | image_fname = tools.GetOutputFilename('image.bin') |
| 2652 | with self.assertRaises(ValueError) as e: |
| 2653 | image = Image.FromFile(image_fname) |
| 2654 | self.assertIn("Cannot find FDT map in image", str(e.exception)) |
Simon Glass | c2f1aed | 2019-07-08 13:18:56 -0600 | [diff] [blame] | 2655 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2656 | def testListCmd(self): |
| 2657 | """Test listing the files in an image using an Fdtmap""" |
| 2658 | self._CheckLz4() |
| 2659 | data = self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2660 | |
| 2661 | # lz4 compression size differs depending on the version |
| 2662 | image = control.images['image'] |
| 2663 | entries = image.GetEntries() |
| 2664 | section_size = entries['section'].size |
| 2665 | fdt_size = entries['section'].GetEntries()['u-boot-dtb'].size |
| 2666 | fdtmap_offset = entries['fdtmap'].offset |
| 2667 | |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2668 | try: |
| 2669 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2670 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2671 | self._DoBinman('ls', '-i', updated_fname) |
| 2672 | finally: |
| 2673 | shutil.rmtree(tmpdir) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2674 | lines = stdout.getvalue().splitlines() |
| 2675 | expected = [ |
| 2676 | 'Name Image-pos Size Entry-type Offset Uncomp-size', |
| 2677 | '----------------------------------------------------------------------', |
| 2678 | 'main-section 0 c00 section 0', |
| 2679 | ' u-boot 0 4 u-boot 0', |
| 2680 | ' section 100 %x section 100' % section_size, |
| 2681 | ' cbfs 100 400 cbfs 0', |
| 2682 | ' u-boot 138 4 u-boot 38', |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2683 | ' u-boot-dtb 180 105 u-boot-dtb 80 3c9', |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2684 | ' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size, |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2685 | ' fdtmap %x 3bd fdtmap %x' % |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2686 | (fdtmap_offset, fdtmap_offset), |
| 2687 | ' image-header bf8 8 image-header bf8', |
| 2688 | ] |
| 2689 | self.assertEqual(expected, lines) |
| 2690 | |
| 2691 | def testListCmdFail(self): |
| 2692 | """Test failing to list an image""" |
| 2693 | self._DoReadFile('005_simple.dts') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2694 | try: |
| 2695 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2696 | with self.assertRaises(ValueError) as e: |
| 2697 | self._DoBinman('ls', '-i', updated_fname) |
| 2698 | finally: |
| 2699 | shutil.rmtree(tmpdir) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 2700 | self.assertIn("Cannot find FDT map in image", str(e.exception)) |
| 2701 | |
| 2702 | def _RunListCmd(self, paths, expected): |
| 2703 | """List out entries and check the result |
| 2704 | |
| 2705 | Args: |
| 2706 | paths: List of paths to pass to the list command |
| 2707 | expected: Expected list of filenames to be returned, in order |
| 2708 | """ |
| 2709 | self._CheckLz4() |
| 2710 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2711 | image_fname = tools.GetOutputFilename('image.bin') |
| 2712 | image = Image.FromFile(image_fname) |
| 2713 | lines = image.GetListEntries(paths)[1] |
| 2714 | files = [line[0].strip() for line in lines[1:]] |
| 2715 | self.assertEqual(expected, files) |
| 2716 | |
| 2717 | def testListCmdSection(self): |
| 2718 | """Test listing the files in a section""" |
| 2719 | self._RunListCmd(['section'], |
| 2720 | ['section', 'cbfs', 'u-boot', 'u-boot-dtb', 'u-boot-dtb']) |
| 2721 | |
| 2722 | def testListCmdFile(self): |
| 2723 | """Test listing a particular file""" |
| 2724 | self._RunListCmd(['*u-boot-dtb'], ['u-boot-dtb', 'u-boot-dtb']) |
| 2725 | |
| 2726 | def testListCmdWildcard(self): |
| 2727 | """Test listing a wildcarded file""" |
| 2728 | self._RunListCmd(['*boot*'], |
| 2729 | ['u-boot', 'u-boot', 'u-boot-dtb', 'u-boot-dtb']) |
| 2730 | |
| 2731 | def testListCmdWildcardMulti(self): |
| 2732 | """Test listing a wildcarded file""" |
| 2733 | self._RunListCmd(['*cb*', '*head*'], |
| 2734 | ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header']) |
| 2735 | |
| 2736 | def testListCmdEmpty(self): |
| 2737 | """Test listing a wildcarded file""" |
| 2738 | self._RunListCmd(['nothing'], []) |
| 2739 | |
| 2740 | def testListCmdPath(self): |
| 2741 | """Test listing the files in a sub-entry of a section""" |
| 2742 | self._RunListCmd(['section/cbfs'], ['cbfs', 'u-boot', 'u-boot-dtb']) |
| 2743 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2744 | def _RunExtractCmd(self, entry_name, decomp=True): |
| 2745 | """Extract an entry from an image |
| 2746 | |
| 2747 | Args: |
| 2748 | entry_name: Entry name to extract |
| 2749 | decomp: True to decompress the data if compressed, False to leave |
| 2750 | it in its raw uncompressed format |
| 2751 | |
| 2752 | Returns: |
| 2753 | data from entry |
| 2754 | """ |
| 2755 | self._CheckLz4() |
| 2756 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2757 | image_fname = tools.GetOutputFilename('image.bin') |
| 2758 | return control.ReadEntry(image_fname, entry_name, decomp) |
| 2759 | |
| 2760 | def testExtractSimple(self): |
| 2761 | """Test extracting a single file""" |
| 2762 | data = self._RunExtractCmd('u-boot') |
| 2763 | self.assertEqual(U_BOOT_DATA, data) |
| 2764 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2765 | def testExtractSection(self): |
| 2766 | """Test extracting the files in a section""" |
| 2767 | data = self._RunExtractCmd('section') |
| 2768 | cbfs_data = data[:0x400] |
| 2769 | cbfs = cbfs_util.CbfsReader(cbfs_data) |
Simon Glass | c5fd10a | 2019-10-31 07:43:03 -0600 | [diff] [blame] | 2770 | self.assertEqual(['u-boot', 'u-boot-dtb', ''], list(cbfs.files.keys())) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2771 | dtb_data = data[0x400:] |
| 2772 | dtb = self._decompress(dtb_data) |
| 2773 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2774 | |
| 2775 | def testExtractCompressed(self): |
| 2776 | """Test extracting compressed data""" |
| 2777 | data = self._RunExtractCmd('section/u-boot-dtb') |
| 2778 | self.assertEqual(EXTRACT_DTB_SIZE, len(data)) |
| 2779 | |
| 2780 | def testExtractRaw(self): |
| 2781 | """Test extracting compressed data without decompressing it""" |
| 2782 | data = self._RunExtractCmd('section/u-boot-dtb', decomp=False) |
| 2783 | dtb = self._decompress(data) |
| 2784 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2785 | |
| 2786 | def testExtractCbfs(self): |
| 2787 | """Test extracting CBFS data""" |
| 2788 | data = self._RunExtractCmd('section/cbfs/u-boot') |
| 2789 | self.assertEqual(U_BOOT_DATA, data) |
| 2790 | |
| 2791 | def testExtractCbfsCompressed(self): |
| 2792 | """Test extracting CBFS compressed data""" |
| 2793 | data = self._RunExtractCmd('section/cbfs/u-boot-dtb') |
| 2794 | self.assertEqual(EXTRACT_DTB_SIZE, len(data)) |
| 2795 | |
| 2796 | def testExtractCbfsRaw(self): |
| 2797 | """Test extracting CBFS compressed data without decompressing it""" |
| 2798 | data = self._RunExtractCmd('section/cbfs/u-boot-dtb', decomp=False) |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 2799 | dtb = tools.Decompress(data, 'lzma', with_header=False) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2800 | self.assertEqual(EXTRACT_DTB_SIZE, len(dtb)) |
| 2801 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 2802 | def testExtractBadEntry(self): |
| 2803 | """Test extracting a bad section path""" |
| 2804 | with self.assertRaises(ValueError) as e: |
| 2805 | self._RunExtractCmd('section/does-not-exist') |
| 2806 | self.assertIn("Entry 'does-not-exist' not found in '/section'", |
| 2807 | str(e.exception)) |
| 2808 | |
| 2809 | def testExtractMissingFile(self): |
| 2810 | """Test extracting file that does not exist""" |
| 2811 | with self.assertRaises(IOError) as e: |
| 2812 | control.ReadEntry('missing-file', 'name') |
| 2813 | |
| 2814 | def testExtractBadFile(self): |
| 2815 | """Test extracting an invalid file""" |
| 2816 | fname = os.path.join(self._indir, 'badfile') |
| 2817 | tools.WriteFile(fname, b'') |
| 2818 | with self.assertRaises(ValueError) as e: |
| 2819 | control.ReadEntry(fname, 'name') |
| 2820 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2821 | def testExtractCmd(self): |
| 2822 | """Test extracting a file fron an image on the command line""" |
| 2823 | self._CheckLz4() |
| 2824 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2825 | fname = os.path.join(self._indir, 'output.extact') |
Simon Glass | b3d6fc7 | 2019-07-20 12:24:10 -0600 | [diff] [blame] | 2826 | try: |
| 2827 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 2828 | with test_util.capture_sys_output() as (stdout, stderr): |
| 2829 | self._DoBinman('extract', '-i', updated_fname, 'u-boot', |
| 2830 | '-f', fname) |
| 2831 | finally: |
| 2832 | shutil.rmtree(tmpdir) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2833 | data = tools.ReadFile(fname) |
| 2834 | self.assertEqual(U_BOOT_DATA, data) |
| 2835 | |
| 2836 | def testExtractOneEntry(self): |
| 2837 | """Test extracting a single entry fron an image """ |
| 2838 | self._CheckLz4() |
| 2839 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2840 | image_fname = tools.GetOutputFilename('image.bin') |
| 2841 | fname = os.path.join(self._indir, 'output.extact') |
| 2842 | control.ExtractEntries(image_fname, fname, None, ['u-boot']) |
| 2843 | data = tools.ReadFile(fname) |
| 2844 | self.assertEqual(U_BOOT_DATA, data) |
| 2845 | |
| 2846 | def _CheckExtractOutput(self, decomp): |
| 2847 | """Helper to test file output with and without decompression |
| 2848 | |
| 2849 | Args: |
| 2850 | decomp: True to decompress entry data, False to output it raw |
| 2851 | """ |
| 2852 | def _CheckPresent(entry_path, expect_data, expect_size=None): |
| 2853 | """Check and remove expected file |
| 2854 | |
| 2855 | This checks the data/size of a file and removes the file both from |
| 2856 | the outfiles set and from the output directory. Once all files are |
| 2857 | processed, both the set and directory should be empty. |
| 2858 | |
| 2859 | Args: |
| 2860 | entry_path: Entry path |
| 2861 | expect_data: Data to expect in file, or None to skip check |
| 2862 | expect_size: Size of data to expect in file, or None to skip |
| 2863 | """ |
| 2864 | path = os.path.join(outdir, entry_path) |
| 2865 | data = tools.ReadFile(path) |
| 2866 | os.remove(path) |
| 2867 | if expect_data: |
| 2868 | self.assertEqual(expect_data, data) |
| 2869 | elif expect_size: |
| 2870 | self.assertEqual(expect_size, len(data)) |
| 2871 | outfiles.remove(path) |
| 2872 | |
| 2873 | def _CheckDirPresent(name): |
| 2874 | """Remove expected directory |
| 2875 | |
| 2876 | This gives an error if the directory does not exist as expected |
| 2877 | |
| 2878 | Args: |
| 2879 | name: Name of directory to remove |
| 2880 | """ |
| 2881 | path = os.path.join(outdir, name) |
| 2882 | os.rmdir(path) |
| 2883 | |
| 2884 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2885 | image_fname = tools.GetOutputFilename('image.bin') |
| 2886 | outdir = os.path.join(self._indir, 'extract') |
| 2887 | einfos = control.ExtractEntries(image_fname, None, outdir, [], decomp) |
| 2888 | |
| 2889 | # Create a set of all file that were output (should be 9) |
| 2890 | outfiles = set() |
| 2891 | for root, dirs, files in os.walk(outdir): |
| 2892 | outfiles |= set([os.path.join(root, fname) for fname in files]) |
| 2893 | self.assertEqual(9, len(outfiles)) |
| 2894 | self.assertEqual(9, len(einfos)) |
| 2895 | |
| 2896 | image = control.images['image'] |
| 2897 | entries = image.GetEntries() |
| 2898 | |
| 2899 | # Check the 9 files in various ways |
| 2900 | section = entries['section'] |
| 2901 | section_entries = section.GetEntries() |
| 2902 | cbfs_entries = section_entries['cbfs'].GetEntries() |
| 2903 | _CheckPresent('u-boot', U_BOOT_DATA) |
| 2904 | _CheckPresent('section/cbfs/u-boot', U_BOOT_DATA) |
| 2905 | dtb_len = EXTRACT_DTB_SIZE |
| 2906 | if not decomp: |
| 2907 | dtb_len = cbfs_entries['u-boot-dtb'].size |
| 2908 | _CheckPresent('section/cbfs/u-boot-dtb', None, dtb_len) |
| 2909 | if not decomp: |
| 2910 | dtb_len = section_entries['u-boot-dtb'].size |
| 2911 | _CheckPresent('section/u-boot-dtb', None, dtb_len) |
| 2912 | |
| 2913 | fdtmap = entries['fdtmap'] |
| 2914 | _CheckPresent('fdtmap', fdtmap.data) |
| 2915 | hdr = entries['image-header'] |
| 2916 | _CheckPresent('image-header', hdr.data) |
| 2917 | |
| 2918 | _CheckPresent('section/root', section.data) |
| 2919 | cbfs = section_entries['cbfs'] |
| 2920 | _CheckPresent('section/cbfs/root', cbfs.data) |
| 2921 | data = tools.ReadFile(image_fname) |
| 2922 | _CheckPresent('root', data) |
| 2923 | |
| 2924 | # There should be no files left. Remove all the directories to check. |
| 2925 | # If there are any files/dirs remaining, one of these checks will fail. |
| 2926 | self.assertEqual(0, len(outfiles)) |
| 2927 | _CheckDirPresent('section/cbfs') |
| 2928 | _CheckDirPresent('section') |
| 2929 | _CheckDirPresent('') |
| 2930 | self.assertFalse(os.path.exists(outdir)) |
| 2931 | |
| 2932 | def testExtractAllEntries(self): |
| 2933 | """Test extracting all entries""" |
| 2934 | self._CheckLz4() |
| 2935 | self._CheckExtractOutput(decomp=True) |
| 2936 | |
| 2937 | def testExtractAllEntriesRaw(self): |
| 2938 | """Test extracting all entries without decompressing them""" |
| 2939 | self._CheckLz4() |
| 2940 | self._CheckExtractOutput(decomp=False) |
| 2941 | |
| 2942 | def testExtractSelectedEntries(self): |
| 2943 | """Test extracting some entries""" |
| 2944 | self._CheckLz4() |
| 2945 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2946 | image_fname = tools.GetOutputFilename('image.bin') |
| 2947 | outdir = os.path.join(self._indir, 'extract') |
| 2948 | einfos = control.ExtractEntries(image_fname, None, outdir, |
| 2949 | ['*cb*', '*head*']) |
| 2950 | |
| 2951 | # File output is tested by testExtractAllEntries(), so just check that |
| 2952 | # the expected entries are selected |
| 2953 | names = [einfo.name for einfo in einfos] |
| 2954 | self.assertEqual(names, |
| 2955 | ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header']) |
| 2956 | |
| 2957 | def testExtractNoEntryPaths(self): |
| 2958 | """Test extracting some entries""" |
| 2959 | self._CheckLz4() |
| 2960 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2961 | image_fname = tools.GetOutputFilename('image.bin') |
| 2962 | with self.assertRaises(ValueError) as e: |
| 2963 | control.ExtractEntries(image_fname, 'fname', None, []) |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 2964 | self.assertIn('Must specify an entry path to write with -f', |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2965 | str(e.exception)) |
| 2966 | |
| 2967 | def testExtractTooManyEntryPaths(self): |
| 2968 | """Test extracting some entries""" |
| 2969 | self._CheckLz4() |
| 2970 | self._DoReadFileRealDtb('130_list_fdtmap.dts') |
| 2971 | image_fname = tools.GetOutputFilename('image.bin') |
| 2972 | with self.assertRaises(ValueError) as e: |
| 2973 | control.ExtractEntries(image_fname, 'fname', None, ['a', 'b']) |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 2974 | self.assertIn('Must specify exactly one entry path to write with -f', |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 2975 | str(e.exception)) |
| 2976 | |
Simon Glass | 52d0621 | 2019-07-08 14:25:53 -0600 | [diff] [blame] | 2977 | def testPackAlignSection(self): |
| 2978 | """Test that sections can have alignment""" |
| 2979 | self._DoReadFile('131_pack_align_section.dts') |
| 2980 | |
| 2981 | self.assertIn('image', control.images) |
| 2982 | image = control.images['image'] |
| 2983 | entries = image.GetEntries() |
| 2984 | self.assertEqual(3, len(entries)) |
| 2985 | |
| 2986 | # First u-boot |
| 2987 | self.assertIn('u-boot', entries) |
| 2988 | entry = entries['u-boot'] |
| 2989 | self.assertEqual(0, entry.offset) |
| 2990 | self.assertEqual(0, entry.image_pos) |
| 2991 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 2992 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 2993 | |
| 2994 | # Section0 |
| 2995 | self.assertIn('section0', entries) |
| 2996 | section0 = entries['section0'] |
| 2997 | self.assertEqual(0x10, section0.offset) |
| 2998 | self.assertEqual(0x10, section0.image_pos) |
| 2999 | self.assertEqual(len(U_BOOT_DATA), section0.size) |
| 3000 | |
| 3001 | # Second u-boot |
| 3002 | section_entries = section0.GetEntries() |
| 3003 | self.assertIn('u-boot', section_entries) |
| 3004 | entry = section_entries['u-boot'] |
| 3005 | self.assertEqual(0, entry.offset) |
| 3006 | self.assertEqual(0x10, entry.image_pos) |
| 3007 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 3008 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3009 | |
| 3010 | # Section1 |
| 3011 | self.assertIn('section1', entries) |
| 3012 | section1 = entries['section1'] |
| 3013 | self.assertEqual(0x14, section1.offset) |
| 3014 | self.assertEqual(0x14, section1.image_pos) |
| 3015 | self.assertEqual(0x20, section1.size) |
| 3016 | |
| 3017 | # Second u-boot |
| 3018 | section_entries = section1.GetEntries() |
| 3019 | self.assertIn('u-boot', section_entries) |
| 3020 | entry = section_entries['u-boot'] |
| 3021 | self.assertEqual(0, entry.offset) |
| 3022 | self.assertEqual(0x14, entry.image_pos) |
| 3023 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 3024 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3025 | |
| 3026 | # Section2 |
| 3027 | self.assertIn('section2', section_entries) |
| 3028 | section2 = section_entries['section2'] |
| 3029 | self.assertEqual(0x4, section2.offset) |
| 3030 | self.assertEqual(0x18, section2.image_pos) |
| 3031 | self.assertEqual(4, section2.size) |
| 3032 | |
| 3033 | # Third u-boot |
| 3034 | section_entries = section2.GetEntries() |
| 3035 | self.assertIn('u-boot', section_entries) |
| 3036 | entry = section_entries['u-boot'] |
| 3037 | self.assertEqual(0, entry.offset) |
| 3038 | self.assertEqual(0x18, entry.image_pos) |
| 3039 | self.assertEqual(len(U_BOOT_DATA), entry.contents_size) |
| 3040 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3041 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3042 | def _RunReplaceCmd(self, entry_name, data, decomp=True, allow_resize=True, |
| 3043 | dts='132_replace.dts'): |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3044 | """Replace an entry in an image |
| 3045 | |
| 3046 | This writes the entry data to update it, then opens the updated file and |
| 3047 | returns the value that it now finds there. |
| 3048 | |
| 3049 | Args: |
| 3050 | entry_name: Entry name to replace |
| 3051 | data: Data to replace it with |
| 3052 | decomp: True to compress the data if needed, False if data is |
| 3053 | already compressed so should be used as is |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3054 | allow_resize: True to allow entries to change size, False to raise |
| 3055 | an exception |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3056 | |
| 3057 | Returns: |
| 3058 | Tuple: |
| 3059 | data from entry |
| 3060 | data from fdtmap (excluding header) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3061 | Image object that was modified |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3062 | """ |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3063 | dtb_data = self._DoReadFileDtb(dts, use_real_dtb=True, |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3064 | update_dtb=True)[1] |
| 3065 | |
| 3066 | self.assertIn('image', control.images) |
| 3067 | image = control.images['image'] |
| 3068 | entries = image.GetEntries() |
| 3069 | orig_dtb_data = entries['u-boot-dtb'].data |
| 3070 | orig_fdtmap_data = entries['fdtmap'].data |
| 3071 | |
| 3072 | image_fname = tools.GetOutputFilename('image.bin') |
| 3073 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3074 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3075 | image = control.WriteEntry(updated_fname, entry_name, data, decomp, |
| 3076 | allow_resize) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3077 | data = control.ReadEntry(updated_fname, entry_name, decomp) |
| 3078 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3079 | # The DT data should not change unless resized: |
| 3080 | if not allow_resize: |
| 3081 | new_dtb_data = entries['u-boot-dtb'].data |
| 3082 | self.assertEqual(new_dtb_data, orig_dtb_data) |
| 3083 | new_fdtmap_data = entries['fdtmap'].data |
| 3084 | self.assertEqual(new_fdtmap_data, orig_fdtmap_data) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3085 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3086 | return data, orig_fdtmap_data[fdtmap.FDTMAP_HDR_LEN:], image |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3087 | |
| 3088 | def testReplaceSimple(self): |
| 3089 | """Test replacing a single file""" |
| 3090 | expected = b'x' * len(U_BOOT_DATA) |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3091 | data, expected_fdtmap, _ = self._RunReplaceCmd('u-boot', expected, |
| 3092 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3093 | self.assertEqual(expected, data) |
| 3094 | |
| 3095 | # Test that the state looks right. There should be an FDT for the fdtmap |
| 3096 | # that we jsut read back in, and it should match what we find in the |
| 3097 | # 'control' tables. Checking for an FDT that does not exist should |
| 3098 | # return None. |
| 3099 | path, fdtmap = state.GetFdtContents('fdtmap') |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3100 | self.assertIsNotNone(path) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3101 | self.assertEqual(expected_fdtmap, fdtmap) |
| 3102 | |
| 3103 | dtb = state.GetFdtForEtype('fdtmap') |
| 3104 | self.assertEqual(dtb.GetContents(), fdtmap) |
| 3105 | |
| 3106 | missing_path, missing_fdtmap = state.GetFdtContents('missing') |
| 3107 | self.assertIsNone(missing_path) |
| 3108 | self.assertIsNone(missing_fdtmap) |
| 3109 | |
| 3110 | missing_dtb = state.GetFdtForEtype('missing') |
| 3111 | self.assertIsNone(missing_dtb) |
| 3112 | |
| 3113 | self.assertEqual('/binman', state.fdt_path_prefix) |
| 3114 | |
| 3115 | def testReplaceResizeFail(self): |
| 3116 | """Test replacing a file by something larger""" |
| 3117 | expected = U_BOOT_DATA + b'x' |
| 3118 | with self.assertRaises(ValueError) as e: |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3119 | self._RunReplaceCmd('u-boot', expected, allow_resize=False, |
| 3120 | dts='139_replace_repack.dts') |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3121 | self.assertIn("Node '/u-boot': Entry data size does not match, but resize is disabled", |
| 3122 | str(e.exception)) |
| 3123 | |
| 3124 | def testReplaceMulti(self): |
| 3125 | """Test replacing entry data where multiple images are generated""" |
| 3126 | data = self._DoReadFileDtb('133_replace_multi.dts', use_real_dtb=True, |
| 3127 | update_dtb=True)[0] |
| 3128 | expected = b'x' * len(U_BOOT_DATA) |
| 3129 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3130 | tools.WriteFile(updated_fname, data) |
| 3131 | entry_name = 'u-boot' |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3132 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3133 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3134 | data = control.ReadEntry(updated_fname, entry_name) |
| 3135 | self.assertEqual(expected, data) |
| 3136 | |
| 3137 | # Check the state looks right. |
| 3138 | self.assertEqual('/binman/image', state.fdt_path_prefix) |
| 3139 | |
| 3140 | # Now check we can write the first image |
| 3141 | image_fname = tools.GetOutputFilename('first-image.bin') |
| 3142 | updated_fname = tools.GetOutputFilename('first-updated.bin') |
| 3143 | tools.WriteFile(updated_fname, tools.ReadFile(image_fname)) |
| 3144 | entry_name = 'u-boot' |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3145 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3146 | allow_resize=False) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 3147 | data = control.ReadEntry(updated_fname, entry_name) |
| 3148 | self.assertEqual(expected, data) |
| 3149 | |
| 3150 | # Check the state looks right. |
| 3151 | self.assertEqual('/binman/first-image', state.fdt_path_prefix) |
Simon Glass | 39dd215 | 2019-07-08 14:25:47 -0600 | [diff] [blame] | 3152 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 3153 | def testUpdateFdtAllRepack(self): |
| 3154 | """Test that all device trees are updated with offset/size info""" |
| 3155 | data = self._DoReadFileRealDtb('134_fdt_update_all_repack.dts') |
| 3156 | SECTION_SIZE = 0x300 |
| 3157 | DTB_SIZE = 602 |
| 3158 | FDTMAP_SIZE = 608 |
| 3159 | base_expected = { |
| 3160 | 'offset': 0, |
| 3161 | 'size': SECTION_SIZE + DTB_SIZE * 2 + FDTMAP_SIZE, |
| 3162 | 'image-pos': 0, |
| 3163 | 'section:offset': 0, |
| 3164 | 'section:size': SECTION_SIZE, |
| 3165 | 'section:image-pos': 0, |
| 3166 | 'section/u-boot-dtb:offset': 4, |
| 3167 | 'section/u-boot-dtb:size': 636, |
| 3168 | 'section/u-boot-dtb:image-pos': 4, |
| 3169 | 'u-boot-spl-dtb:offset': SECTION_SIZE, |
| 3170 | 'u-boot-spl-dtb:size': DTB_SIZE, |
| 3171 | 'u-boot-spl-dtb:image-pos': SECTION_SIZE, |
| 3172 | 'u-boot-tpl-dtb:offset': SECTION_SIZE + DTB_SIZE, |
| 3173 | 'u-boot-tpl-dtb:image-pos': SECTION_SIZE + DTB_SIZE, |
| 3174 | 'u-boot-tpl-dtb:size': DTB_SIZE, |
| 3175 | 'fdtmap:offset': SECTION_SIZE + DTB_SIZE * 2, |
| 3176 | 'fdtmap:size': FDTMAP_SIZE, |
| 3177 | 'fdtmap:image-pos': SECTION_SIZE + DTB_SIZE * 2, |
| 3178 | } |
| 3179 | main_expected = { |
| 3180 | 'section:orig-size': SECTION_SIZE, |
| 3181 | 'section/u-boot-dtb:orig-offset': 4, |
| 3182 | } |
| 3183 | |
| 3184 | # We expect three device-tree files in the output, with the first one |
| 3185 | # within a fixed-size section. |
| 3186 | # Read them in sequence. We look for an 'spl' property in the SPL tree, |
| 3187 | # and 'tpl' in the TPL tree, to make sure they are distinct from the |
| 3188 | # main U-Boot tree. All three should have the same positions and offset |
| 3189 | # except that the main tree should include the main_expected properties |
| 3190 | start = 4 |
| 3191 | for item in ['', 'spl', 'tpl', None]: |
| 3192 | if item is None: |
| 3193 | start += 16 # Move past fdtmap header |
| 3194 | dtb = fdt.Fdt.FromData(data[start:]) |
| 3195 | dtb.Scan() |
| 3196 | props = self._GetPropTree(dtb, |
| 3197 | BASE_DTB_PROPS + REPACK_DTB_PROPS + ['spl', 'tpl'], |
| 3198 | prefix='/' if item is None else '/binman/') |
| 3199 | expected = dict(base_expected) |
| 3200 | if item: |
| 3201 | expected[item] = 0 |
| 3202 | else: |
| 3203 | # Main DTB and fdtdec should include the 'orig-' properties |
| 3204 | expected.update(main_expected) |
| 3205 | # Helpful for debugging: |
| 3206 | #for prop in sorted(props): |
| 3207 | #print('prop %s %s %s' % (prop, props[prop], expected[prop])) |
| 3208 | self.assertEqual(expected, props) |
| 3209 | if item == '': |
| 3210 | start = SECTION_SIZE |
| 3211 | else: |
| 3212 | start += dtb._fdt_obj.totalsize() |
| 3213 | |
Simon Glass | 1145376 | 2019-07-20 12:23:55 -0600 | [diff] [blame] | 3214 | def testFdtmapHeaderMiddle(self): |
| 3215 | """Test an FDT map in the middle of an image when it should be at end""" |
| 3216 | with self.assertRaises(ValueError) as e: |
| 3217 | self._DoReadFileRealDtb('135_fdtmap_hdr_middle.dts') |
| 3218 | self.assertIn("Invalid sibling order 'middle' for image-header: Must be at 'end' to match location", |
| 3219 | str(e.exception)) |
| 3220 | |
| 3221 | def testFdtmapHeaderStartBad(self): |
| 3222 | """Test an FDT map in middle of an image when it should be at start""" |
| 3223 | with self.assertRaises(ValueError) as e: |
| 3224 | self._DoReadFileRealDtb('136_fdtmap_hdr_startbad.dts') |
| 3225 | self.assertIn("Invalid sibling order 'end' for image-header: Must be at 'start' to match location", |
| 3226 | str(e.exception)) |
| 3227 | |
| 3228 | def testFdtmapHeaderEndBad(self): |
| 3229 | """Test an FDT map at the start of an image when it should be at end""" |
| 3230 | with self.assertRaises(ValueError) as e: |
| 3231 | self._DoReadFileRealDtb('137_fdtmap_hdr_endbad.dts') |
| 3232 | self.assertIn("Invalid sibling order 'start' for image-header: Must be at 'end' to match location", |
| 3233 | str(e.exception)) |
| 3234 | |
| 3235 | def testFdtmapHeaderNoSize(self): |
| 3236 | """Test an image header at the end of an image with undefined size""" |
| 3237 | self._DoReadFileRealDtb('138_fdtmap_hdr_nosize.dts') |
| 3238 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 3239 | def testReplaceResize(self): |
| 3240 | """Test replacing a single file in an entry with a larger file""" |
| 3241 | expected = U_BOOT_DATA + b'x' |
| 3242 | data, _, image = self._RunReplaceCmd('u-boot', expected, |
| 3243 | dts='139_replace_repack.dts') |
| 3244 | self.assertEqual(expected, data) |
| 3245 | |
| 3246 | entries = image.GetEntries() |
| 3247 | dtb_data = entries['u-boot-dtb'].data |
| 3248 | dtb = fdt.Fdt.FromData(dtb_data) |
| 3249 | dtb.Scan() |
| 3250 | |
| 3251 | # The u-boot section should now be larger in the dtb |
| 3252 | node = dtb.GetNode('/binman/u-boot') |
| 3253 | self.assertEqual(len(expected), fdt_util.GetInt(node, 'size')) |
| 3254 | |
| 3255 | # Same for the fdtmap |
| 3256 | fdata = entries['fdtmap'].data |
| 3257 | fdtb = fdt.Fdt.FromData(fdata[fdtmap.FDTMAP_HDR_LEN:]) |
| 3258 | fdtb.Scan() |
| 3259 | fnode = fdtb.GetNode('/u-boot') |
| 3260 | self.assertEqual(len(expected), fdt_util.GetInt(fnode, 'size')) |
| 3261 | |
| 3262 | def testReplaceResizeNoRepack(self): |
| 3263 | """Test replacing an entry with a larger file when not allowed""" |
| 3264 | expected = U_BOOT_DATA + b'x' |
| 3265 | with self.assertRaises(ValueError) as e: |
| 3266 | self._RunReplaceCmd('u-boot', expected) |
| 3267 | self.assertIn('Entry data size does not match, but allow-repack is not present for this image', |
| 3268 | str(e.exception)) |
| 3269 | |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 3270 | def testEntryShrink(self): |
| 3271 | """Test contracting an entry after it is packed""" |
| 3272 | try: |
| 3273 | state.SetAllowEntryContraction(True) |
| 3274 | data = self._DoReadFileDtb('140_entry_shrink.dts', |
| 3275 | update_dtb=True)[0] |
| 3276 | finally: |
| 3277 | state.SetAllowEntryContraction(False) |
| 3278 | self.assertEqual(b'a', data[:1]) |
| 3279 | self.assertEqual(U_BOOT_DATA, data[1:1 + len(U_BOOT_DATA)]) |
| 3280 | self.assertEqual(b'a', data[-1:]) |
| 3281 | |
| 3282 | def testEntryShrinkFail(self): |
| 3283 | """Test not being allowed to contract an entry after it is packed""" |
| 3284 | data = self._DoReadFileDtb('140_entry_shrink.dts', update_dtb=True)[0] |
| 3285 | |
| 3286 | # In this case there is a spare byte at the end of the data. The size of |
| 3287 | # the contents is only 1 byte but we still have the size before it |
| 3288 | # shrunk. |
| 3289 | self.assertEqual(b'a\0', data[:2]) |
| 3290 | self.assertEqual(U_BOOT_DATA, data[2:2 + len(U_BOOT_DATA)]) |
| 3291 | self.assertEqual(b'a\0', data[-2:]) |
| 3292 | |
Simon Glass | 70e3298 | 2019-07-20 12:24:01 -0600 | [diff] [blame] | 3293 | def testDescriptorOffset(self): |
| 3294 | """Test that the Intel descriptor is always placed at at the start""" |
| 3295 | data = self._DoReadFileDtb('141_descriptor_offset.dts') |
| 3296 | image = control.images['image'] |
| 3297 | entries = image.GetEntries() |
| 3298 | desc = entries['intel-descriptor'] |
| 3299 | self.assertEqual(0xff800000, desc.offset); |
| 3300 | self.assertEqual(0xff800000, desc.image_pos); |
| 3301 | |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 3302 | def testReplaceCbfs(self): |
| 3303 | """Test replacing a single file in CBFS without changing the size""" |
| 3304 | self._CheckLz4() |
| 3305 | expected = b'x' * len(U_BOOT_DATA) |
| 3306 | data = self._DoReadFileRealDtb('142_replace_cbfs.dts') |
| 3307 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3308 | tools.WriteFile(updated_fname, data) |
| 3309 | entry_name = 'section/cbfs/u-boot' |
| 3310 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3311 | allow_resize=True) |
| 3312 | data = control.ReadEntry(updated_fname, entry_name) |
| 3313 | self.assertEqual(expected, data) |
| 3314 | |
| 3315 | def testReplaceResizeCbfs(self): |
| 3316 | """Test replacing a single file in CBFS with one of a different size""" |
| 3317 | self._CheckLz4() |
| 3318 | expected = U_BOOT_DATA + b'x' |
| 3319 | data = self._DoReadFileRealDtb('142_replace_cbfs.dts') |
| 3320 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3321 | tools.WriteFile(updated_fname, data) |
| 3322 | entry_name = 'section/cbfs/u-boot' |
| 3323 | control.WriteEntry(updated_fname, entry_name, expected, |
| 3324 | allow_resize=True) |
| 3325 | data = control.ReadEntry(updated_fname, entry_name) |
| 3326 | self.assertEqual(expected, data) |
| 3327 | |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3328 | def _SetupForReplace(self): |
| 3329 | """Set up some files to use to replace entries |
| 3330 | |
| 3331 | This generates an image, copies it to a new file, extracts all the files |
| 3332 | in it and updates some of them |
| 3333 | |
| 3334 | Returns: |
| 3335 | List |
| 3336 | Image filename |
| 3337 | Output directory |
| 3338 | Expected values for updated entries, each a string |
| 3339 | """ |
| 3340 | data = self._DoReadFileRealDtb('143_replace_all.dts') |
| 3341 | |
| 3342 | updated_fname = tools.GetOutputFilename('image-updated.bin') |
| 3343 | tools.WriteFile(updated_fname, data) |
| 3344 | |
| 3345 | outdir = os.path.join(self._indir, 'extract') |
| 3346 | einfos = control.ExtractEntries(updated_fname, None, outdir, []) |
| 3347 | |
| 3348 | expected1 = b'x' + U_BOOT_DATA + b'y' |
| 3349 | u_boot_fname1 = os.path.join(outdir, 'u-boot') |
| 3350 | tools.WriteFile(u_boot_fname1, expected1) |
| 3351 | |
| 3352 | expected2 = b'a' + U_BOOT_DATA + b'b' |
| 3353 | u_boot_fname2 = os.path.join(outdir, 'u-boot2') |
| 3354 | tools.WriteFile(u_boot_fname2, expected2) |
| 3355 | |
| 3356 | expected_text = b'not the same text' |
| 3357 | text_fname = os.path.join(outdir, 'text') |
| 3358 | tools.WriteFile(text_fname, expected_text) |
| 3359 | |
| 3360 | dtb_fname = os.path.join(outdir, 'u-boot-dtb') |
| 3361 | dtb = fdt.FdtScan(dtb_fname) |
| 3362 | node = dtb.GetNode('/binman/text') |
| 3363 | node.AddString('my-property', 'the value') |
| 3364 | dtb.Sync(auto_resize=True) |
| 3365 | dtb.Flush() |
| 3366 | |
| 3367 | return updated_fname, outdir, expected1, expected2, expected_text |
| 3368 | |
| 3369 | def _CheckReplaceMultiple(self, entry_paths): |
| 3370 | """Handle replacing the contents of multiple entries |
| 3371 | |
| 3372 | Args: |
| 3373 | entry_paths: List of entry paths to replace |
| 3374 | |
| 3375 | Returns: |
| 3376 | List |
| 3377 | Dict of entries in the image: |
| 3378 | key: Entry name |
| 3379 | Value: Entry object |
| 3380 | Expected values for updated entries, each a string |
| 3381 | """ |
| 3382 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3383 | self._SetupForReplace()) |
| 3384 | control.ReplaceEntries(updated_fname, None, outdir, entry_paths) |
| 3385 | |
| 3386 | image = Image.FromFile(updated_fname) |
| 3387 | image.LoadData() |
| 3388 | return image.GetEntries(), expected1, expected2, expected_text |
| 3389 | |
| 3390 | def testReplaceAll(self): |
| 3391 | """Test replacing the contents of all entries""" |
| 3392 | entries, expected1, expected2, expected_text = ( |
| 3393 | self._CheckReplaceMultiple([])) |
| 3394 | data = entries['u-boot'].data |
| 3395 | self.assertEqual(expected1, data) |
| 3396 | |
| 3397 | data = entries['u-boot2'].data |
| 3398 | self.assertEqual(expected2, data) |
| 3399 | |
| 3400 | data = entries['text'].data |
| 3401 | self.assertEqual(expected_text, data) |
| 3402 | |
| 3403 | # Check that the device tree is updated |
| 3404 | data = entries['u-boot-dtb'].data |
| 3405 | dtb = fdt.Fdt.FromData(data) |
| 3406 | dtb.Scan() |
| 3407 | node = dtb.GetNode('/binman/text') |
| 3408 | self.assertEqual('the value', node.props['my-property'].value) |
| 3409 | |
| 3410 | def testReplaceSome(self): |
| 3411 | """Test replacing the contents of a few entries""" |
| 3412 | entries, expected1, expected2, expected_text = ( |
| 3413 | self._CheckReplaceMultiple(['u-boot2', 'text'])) |
| 3414 | |
| 3415 | # This one should not change |
| 3416 | data = entries['u-boot'].data |
| 3417 | self.assertEqual(U_BOOT_DATA, data) |
| 3418 | |
| 3419 | data = entries['u-boot2'].data |
| 3420 | self.assertEqual(expected2, data) |
| 3421 | |
| 3422 | data = entries['text'].data |
| 3423 | self.assertEqual(expected_text, data) |
| 3424 | |
| 3425 | def testReplaceCmd(self): |
| 3426 | """Test replacing a file fron an image on the command line""" |
| 3427 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3428 | |
| 3429 | try: |
| 3430 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 3431 | |
| 3432 | fname = os.path.join(tmpdir, 'update-u-boot.bin') |
| 3433 | expected = b'x' * len(U_BOOT_DATA) |
| 3434 | tools.WriteFile(fname, expected) |
| 3435 | |
| 3436 | self._DoBinman('replace', '-i', updated_fname, 'u-boot', '-f', fname) |
| 3437 | data = tools.ReadFile(updated_fname) |
| 3438 | self.assertEqual(expected, data[:len(expected)]) |
| 3439 | map_fname = os.path.join(tmpdir, 'image-updated.map') |
| 3440 | self.assertFalse(os.path.exists(map_fname)) |
| 3441 | finally: |
| 3442 | shutil.rmtree(tmpdir) |
| 3443 | |
| 3444 | def testReplaceCmdSome(self): |
| 3445 | """Test replacing some files fron an image on the command line""" |
| 3446 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3447 | self._SetupForReplace()) |
| 3448 | |
| 3449 | self._DoBinman('replace', '-i', updated_fname, '-I', outdir, |
| 3450 | 'u-boot2', 'text') |
| 3451 | |
| 3452 | tools.PrepareOutputDir(None) |
| 3453 | image = Image.FromFile(updated_fname) |
| 3454 | image.LoadData() |
| 3455 | entries = image.GetEntries() |
| 3456 | |
| 3457 | # This one should not change |
| 3458 | data = entries['u-boot'].data |
| 3459 | self.assertEqual(U_BOOT_DATA, data) |
| 3460 | |
| 3461 | data = entries['u-boot2'].data |
| 3462 | self.assertEqual(expected2, data) |
| 3463 | |
| 3464 | data = entries['text'].data |
| 3465 | self.assertEqual(expected_text, data) |
| 3466 | |
| 3467 | def testReplaceMissing(self): |
| 3468 | """Test replacing entries where the file is missing""" |
| 3469 | updated_fname, outdir, expected1, expected2, expected_text = ( |
| 3470 | self._SetupForReplace()) |
| 3471 | |
| 3472 | # Remove one of the files, to generate a warning |
| 3473 | u_boot_fname1 = os.path.join(outdir, 'u-boot') |
| 3474 | os.remove(u_boot_fname1) |
| 3475 | |
| 3476 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3477 | control.ReplaceEntries(updated_fname, None, outdir, []) |
| 3478 | self.assertIn("Skipping entry '/u-boot' from missing file", |
Simon Glass | 6e02f7c | 2020-07-09 18:39:39 -0600 | [diff] [blame] | 3479 | stderr.getvalue()) |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 3480 | |
| 3481 | def testReplaceCmdMap(self): |
| 3482 | """Test replacing a file fron an image on the command line""" |
| 3483 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3484 | |
| 3485 | try: |
| 3486 | tmpdir, updated_fname = self._SetupImageInTmpdir() |
| 3487 | |
| 3488 | fname = os.path.join(self._indir, 'update-u-boot.bin') |
| 3489 | expected = b'x' * len(U_BOOT_DATA) |
| 3490 | tools.WriteFile(fname, expected) |
| 3491 | |
| 3492 | self._DoBinman('replace', '-i', updated_fname, 'u-boot', |
| 3493 | '-f', fname, '-m') |
| 3494 | map_fname = os.path.join(tmpdir, 'image-updated.map') |
| 3495 | self.assertTrue(os.path.exists(map_fname)) |
| 3496 | finally: |
| 3497 | shutil.rmtree(tmpdir) |
| 3498 | |
| 3499 | def testReplaceNoEntryPaths(self): |
| 3500 | """Test replacing an entry without an entry path""" |
| 3501 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3502 | image_fname = tools.GetOutputFilename('image.bin') |
| 3503 | with self.assertRaises(ValueError) as e: |
| 3504 | control.ReplaceEntries(image_fname, 'fname', None, []) |
| 3505 | self.assertIn('Must specify an entry path to read with -f', |
| 3506 | str(e.exception)) |
| 3507 | |
| 3508 | def testReplaceTooManyEntryPaths(self): |
| 3509 | """Test extracting some entries""" |
| 3510 | self._DoReadFileRealDtb('143_replace_all.dts') |
| 3511 | image_fname = tools.GetOutputFilename('image.bin') |
| 3512 | with self.assertRaises(ValueError) as e: |
| 3513 | control.ReplaceEntries(image_fname, 'fname', None, ['a', 'b']) |
| 3514 | self.assertIn('Must specify exactly one entry path to write with -f', |
| 3515 | str(e.exception)) |
| 3516 | |
Simon Glass | 0b074d6 | 2019-08-24 07:22:48 -0600 | [diff] [blame] | 3517 | def testPackReset16(self): |
| 3518 | """Test that an image with an x86 reset16 region can be created""" |
| 3519 | data = self._DoReadFile('144_x86_reset16.dts') |
| 3520 | self.assertEqual(X86_RESET16_DATA, data[:len(X86_RESET16_DATA)]) |
| 3521 | |
| 3522 | def testPackReset16Spl(self): |
| 3523 | """Test that an image with an x86 reset16-spl region can be created""" |
| 3524 | data = self._DoReadFile('145_x86_reset16_spl.dts') |
| 3525 | self.assertEqual(X86_RESET16_SPL_DATA, data[:len(X86_RESET16_SPL_DATA)]) |
| 3526 | |
| 3527 | def testPackReset16Tpl(self): |
| 3528 | """Test that an image with an x86 reset16-tpl region can be created""" |
| 3529 | data = self._DoReadFile('146_x86_reset16_tpl.dts') |
| 3530 | self.assertEqual(X86_RESET16_TPL_DATA, data[:len(X86_RESET16_TPL_DATA)]) |
| 3531 | |
Simon Glass | 232f90c | 2019-08-24 07:22:50 -0600 | [diff] [blame] | 3532 | def testPackIntelFit(self): |
| 3533 | """Test that an image with an Intel FIT and pointer can be created""" |
| 3534 | data = self._DoReadFile('147_intel_fit.dts') |
| 3535 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3536 | fit = data[16:32]; |
| 3537 | self.assertEqual(b'_FIT_ \x01\x00\x00\x00\x00\x01\x80}' , fit) |
| 3538 | ptr = struct.unpack('<i', data[0x40:0x44])[0] |
| 3539 | |
| 3540 | image = control.images['image'] |
| 3541 | entries = image.GetEntries() |
| 3542 | expected_ptr = entries['intel-fit'].image_pos - (1 << 32) |
| 3543 | self.assertEqual(expected_ptr, ptr) |
| 3544 | |
| 3545 | def testPackIntelFitMissing(self): |
| 3546 | """Test detection of a FIT pointer with not FIT region""" |
| 3547 | with self.assertRaises(ValueError) as e: |
| 3548 | self._DoReadFile('148_intel_fit_missing.dts') |
| 3549 | self.assertIn("'intel-fit-ptr' section must have an 'intel-fit' sibling", |
| 3550 | str(e.exception)) |
| 3551 | |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3552 | def _CheckSymbolsTplSection(self, dts, expected_vals): |
| 3553 | data = self._DoReadFile(dts) |
| 3554 | sym_values = struct.pack('<LQLL', *expected_vals) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3555 | upto1 = 4 + len(U_BOOT_SPL_DATA) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3556 | expected1 = tools.GetBytes(0xff, 4) + sym_values + U_BOOT_SPL_DATA[20:] |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3557 | self.assertEqual(expected1, data[:upto1]) |
| 3558 | |
| 3559 | upto2 = upto1 + 1 + len(U_BOOT_SPL_DATA) |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3560 | expected2 = tools.GetBytes(0xff, 1) + sym_values + U_BOOT_SPL_DATA[20:] |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3561 | self.assertEqual(expected2, data[upto1:upto2]) |
| 3562 | |
Simon Glass | 4e353e2 | 2019-08-24 07:23:04 -0600 | [diff] [blame] | 3563 | upto3 = 0x34 + len(U_BOOT_DATA) |
| 3564 | expected3 = tools.GetBytes(0xff, 1) + U_BOOT_DATA |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3565 | self.assertEqual(expected3, data[upto2:upto3]) |
| 3566 | |
Simon Glass | 3f8ff01 | 2019-08-24 07:23:05 -0600 | [diff] [blame] | 3567 | expected4 = sym_values + U_BOOT_TPL_DATA[20:] |
Simon Glass | 72555fa | 2019-11-06 17:22:44 -0700 | [diff] [blame] | 3568 | self.assertEqual(expected4, data[upto3:upto3 + len(U_BOOT_TPL_DATA)]) |
| 3569 | |
| 3570 | def testSymbolsTplSection(self): |
| 3571 | """Test binman can assign symbols embedded in U-Boot TPL in a section""" |
| 3572 | self._SetupSplElf('u_boot_binman_syms') |
| 3573 | self._SetupTplElf('u_boot_binman_syms') |
| 3574 | self._CheckSymbolsTplSection('149_symbols_tpl.dts', |
| 3575 | [0x04, 0x1c, 0x10 + 0x34, 0x04]) |
| 3576 | |
| 3577 | def testSymbolsTplSectionX86(self): |
| 3578 | """Test binman can assign symbols in a section with end-at-4gb""" |
| 3579 | self._SetupSplElf('u_boot_binman_syms_x86') |
| 3580 | self._SetupTplElf('u_boot_binman_syms_x86') |
| 3581 | self._CheckSymbolsTplSection('155_symbols_tpl_x86.dts', |
| 3582 | [0xffffff04, 0xffffff1c, 0xffffff34, |
| 3583 | 0x04]) |
Simon Glass | 3eb5b20 | 2019-08-24 07:23:00 -0600 | [diff] [blame] | 3584 | |
Simon Glass | 98c5957 | 2019-08-24 07:23:03 -0600 | [diff] [blame] | 3585 | def testPackX86RomIfwiSectiom(self): |
| 3586 | """Test that a section can be placed in an IFWI region""" |
| 3587 | self._SetupIfwi('fitimage.bin') |
| 3588 | data = self._DoReadFile('151_x86_rom_ifwi_section.dts') |
| 3589 | self._CheckIfwi(data) |
| 3590 | |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 3591 | def testPackFspM(self): |
| 3592 | """Test that an image with a FSP memory-init binary can be created""" |
| 3593 | data = self._DoReadFile('152_intel_fsp_m.dts') |
| 3594 | self.assertEqual(FSP_M_DATA, data[:len(FSP_M_DATA)]) |
| 3595 | |
Simon Glass | 4d9086d | 2019-10-20 21:31:35 -0600 | [diff] [blame] | 3596 | def testPackFspS(self): |
| 3597 | """Test that an image with a FSP silicon-init binary can be created""" |
| 3598 | data = self._DoReadFile('153_intel_fsp_s.dts') |
| 3599 | self.assertEqual(FSP_S_DATA, data[:len(FSP_S_DATA)]) |
Simon Glass | ba7985d | 2019-08-24 07:23:07 -0600 | [diff] [blame] | 3600 | |
Simon Glass | 9ea87b2 | 2019-10-20 21:31:36 -0600 | [diff] [blame] | 3601 | def testPackFspT(self): |
| 3602 | """Test that an image with a FSP temp-ram-init binary can be created""" |
| 3603 | data = self._DoReadFile('154_intel_fsp_t.dts') |
| 3604 | self.assertEqual(FSP_T_DATA, data[:len(FSP_T_DATA)]) |
| 3605 | |
Simon Glass | 48f3aad | 2020-07-09 18:39:31 -0600 | [diff] [blame] | 3606 | def testMkimage(self): |
| 3607 | """Test using mkimage to build an image""" |
| 3608 | data = self._DoReadFile('156_mkimage.dts') |
| 3609 | |
| 3610 | # Just check that the data appears in the file somewhere |
| 3611 | self.assertIn(U_BOOT_SPL_DATA, data) |
| 3612 | |
Simon Glass | 5e56018 | 2020-07-09 18:39:36 -0600 | [diff] [blame] | 3613 | def testExtblob(self): |
| 3614 | """Test an image with an external blob""" |
| 3615 | data = self._DoReadFile('157_blob_ext.dts') |
| 3616 | self.assertEqual(REFCODE_DATA, data) |
| 3617 | |
| 3618 | def testExtblobMissing(self): |
| 3619 | """Test an image with a missing external blob""" |
| 3620 | with self.assertRaises(ValueError) as e: |
| 3621 | self._DoReadFile('158_blob_ext_missing.dts') |
| 3622 | self.assertIn("Filename 'missing-file' not found in input path", |
| 3623 | str(e.exception)) |
| 3624 | |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 3625 | def testExtblobMissingOk(self): |
| 3626 | """Test an image with an missing external blob that is allowed""" |
Simon Glass | a003cd3 | 2020-07-09 18:39:40 -0600 | [diff] [blame] | 3627 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3628 | self._DoTestFile('158_blob_ext_missing.dts', allow_missing=True) |
| 3629 | err = stderr.getvalue() |
| 3630 | self.assertRegex(err, "Image 'main-section'.*missing.*: blob-ext") |
| 3631 | |
| 3632 | def testExtblobMissingOkSect(self): |
| 3633 | """Test an image with an missing external blob that is allowed""" |
| 3634 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3635 | self._DoTestFile('159_blob_ext_missing_sect.dts', |
| 3636 | allow_missing=True) |
| 3637 | err = stderr.getvalue() |
| 3638 | self.assertRegex(err, "Image 'main-section'.*missing.*: " |
| 3639 | "blob-ext blob-ext2") |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame] | 3640 | |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3641 | def testPackX86RomMeMissingDesc(self): |
| 3642 | """Test that an missing Intel descriptor entry is allowed""" |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3643 | with test_util.capture_sys_output() as (stdout, stderr): |
Simon Glass | 14c596c | 2020-07-25 15:11:19 -0600 | [diff] [blame] | 3644 | self._DoTestFile('164_x86_rom_me_missing.dts', allow_missing=True) |
Simon Glass | e88cef9 | 2020-07-09 18:39:41 -0600 | [diff] [blame] | 3645 | err = stderr.getvalue() |
| 3646 | self.assertRegex(err, |
| 3647 | "Image 'main-section'.*missing.*: intel-descriptor") |
| 3648 | |
| 3649 | def testPackX86RomMissingIfwi(self): |
| 3650 | """Test that an x86 ROM with Integrated Firmware Image can be created""" |
| 3651 | self._SetupIfwi('fitimage.bin') |
| 3652 | pathname = os.path.join(self._indir, 'fitimage.bin') |
| 3653 | os.remove(pathname) |
| 3654 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3655 | self._DoTestFile('111_x86_rom_ifwi.dts', allow_missing=True) |
| 3656 | err = stderr.getvalue() |
| 3657 | self.assertRegex(err, "Image 'main-section'.*missing.*: intel-ifwi") |
| 3658 | |
Simon Glass | d70829a | 2020-07-09 18:39:42 -0600 | [diff] [blame] | 3659 | def testPackOverlap(self): |
| 3660 | """Test that zero-size overlapping regions are ignored""" |
| 3661 | self._DoTestFile('160_pack_overlap_zero.dts') |
| 3662 | |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3663 | def testSimpleFit(self): |
| 3664 | """Test an image with a FIT inside""" |
| 3665 | data = self._DoReadFile('161_fit.dts') |
| 3666 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3667 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3668 | fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3669 | |
| 3670 | # The data should be inside the FIT |
| 3671 | dtb = fdt.Fdt.FromData(fit_data) |
| 3672 | dtb.Scan() |
| 3673 | fnode = dtb.GetNode('/images/kernel') |
| 3674 | self.assertIn('data', fnode.props) |
| 3675 | |
| 3676 | fname = os.path.join(self._indir, 'fit_data.fit') |
| 3677 | tools.WriteFile(fname, fit_data) |
| 3678 | out = tools.Run('dumpimage', '-l', fname) |
| 3679 | |
| 3680 | # Check a few features to make sure the plumbing works. We don't need |
| 3681 | # to test the operation of mkimage or dumpimage here. First convert the |
| 3682 | # output into a dict where the keys are the fields printed by dumpimage |
| 3683 | # and the values are a list of values for each field |
| 3684 | lines = out.splitlines() |
| 3685 | |
| 3686 | # Converts "Compression: gzip compressed" into two groups: |
| 3687 | # 'Compression' and 'gzip compressed' |
| 3688 | re_line = re.compile(r'^ *([^:]*)(?:: *(.*))?$') |
| 3689 | vals = collections.defaultdict(list) |
| 3690 | for line in lines: |
| 3691 | mat = re_line.match(line) |
| 3692 | vals[mat.group(1)].append(mat.group(2)) |
| 3693 | |
| 3694 | self.assertEquals('FIT description: test-desc', lines[0]) |
| 3695 | self.assertIn('Created:', lines[1]) |
| 3696 | self.assertIn('Image 0 (kernel)', vals) |
| 3697 | self.assertIn('Hash value', vals) |
| 3698 | data_sizes = vals.get('Data Size') |
| 3699 | self.assertIsNotNone(data_sizes) |
| 3700 | self.assertEqual(2, len(data_sizes)) |
| 3701 | # Format is "4 Bytes = 0.00 KiB = 0.00 MiB" so take the first word |
| 3702 | self.assertEqual(len(U_BOOT_DATA), int(data_sizes[0].split()[0])) |
| 3703 | self.assertEqual(len(U_BOOT_SPL_DTB_DATA), int(data_sizes[1].split()[0])) |
| 3704 | |
| 3705 | def testFitExternal(self): |
Simon Glass | 31ee50f | 2020-09-01 05:13:55 -0600 | [diff] [blame] | 3706 | """Test an image with an FIT with external images""" |
Simon Glass | 45d556d | 2020-07-09 18:39:45 -0600 | [diff] [blame] | 3707 | data = self._DoReadFile('162_fit_external.dts') |
| 3708 | fit_data = data[len(U_BOOT_DATA):-2] # _testing is 2 bytes |
| 3709 | |
| 3710 | # The data should be outside the FIT |
| 3711 | dtb = fdt.Fdt.FromData(fit_data) |
| 3712 | dtb.Scan() |
| 3713 | fnode = dtb.GetNode('/images/kernel') |
| 3714 | self.assertNotIn('data', fnode.props) |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 3715 | |
Alper Nebi Yasak | 6aae239 | 2020-08-31 12:58:18 +0300 | [diff] [blame] | 3716 | def testSectionIgnoreHashSignature(self): |
| 3717 | """Test that sections ignore hash, signature nodes for its data""" |
| 3718 | data = self._DoReadFile('165_section_ignore_hash_signature.dts') |
| 3719 | expected = (U_BOOT_DATA + U_BOOT_DATA) |
| 3720 | self.assertEqual(expected, data) |
| 3721 | |
Alper Nebi Yasak | c503060 | 2020-08-31 12:58:19 +0300 | [diff] [blame] | 3722 | def testPadInSections(self): |
| 3723 | """Test pad-before, pad-after for entries in sections""" |
Simon Glass | d12599d | 2020-10-26 17:40:09 -0600 | [diff] [blame] | 3724 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 3725 | '166_pad_in_sections.dts', update_dtb=True) |
Alper Nebi Yasak | c503060 | 2020-08-31 12:58:19 +0300 | [diff] [blame] | 3726 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 3727 | U_BOOT_DATA + tools.GetBytes(ord('!'), 6) + |
| 3728 | U_BOOT_DATA) |
| 3729 | self.assertEqual(expected, data) |
| 3730 | |
Simon Glass | d12599d | 2020-10-26 17:40:09 -0600 | [diff] [blame] | 3731 | dtb = fdt.Fdt(out_dtb_fname) |
| 3732 | dtb.Scan() |
| 3733 | props = self._GetPropTree(dtb, ['size', 'image-pos', 'offset']) |
| 3734 | expected = { |
| 3735 | 'image-pos': 0, |
| 3736 | 'offset': 0, |
| 3737 | 'size': 12 + 6 + 3 * len(U_BOOT_DATA), |
| 3738 | |
| 3739 | 'section:image-pos': 0, |
| 3740 | 'section:offset': 0, |
| 3741 | 'section:size': 12 + 6 + 3 * len(U_BOOT_DATA), |
| 3742 | |
| 3743 | 'section/before:image-pos': 0, |
| 3744 | 'section/before:offset': 0, |
| 3745 | 'section/before:size': len(U_BOOT_DATA), |
| 3746 | |
| 3747 | 'section/u-boot:image-pos': 4, |
| 3748 | 'section/u-boot:offset': 4, |
| 3749 | 'section/u-boot:size': 12 + len(U_BOOT_DATA) + 6, |
| 3750 | |
| 3751 | 'section/after:image-pos': 26, |
| 3752 | 'section/after:offset': 26, |
| 3753 | 'section/after:size': len(U_BOOT_DATA), |
| 3754 | } |
| 3755 | self.assertEqual(expected, props) |
| 3756 | |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 3757 | def testFitImageSubentryAlignment(self): |
| 3758 | """Test relative alignability of FIT image subentries""" |
| 3759 | entry_args = { |
| 3760 | 'test-id': TEXT_DATA, |
| 3761 | } |
| 3762 | data, _, _, _ = self._DoReadFileDtb('167_fit_image_subentry_alignment.dts', |
| 3763 | entry_args=entry_args) |
| 3764 | dtb = fdt.Fdt.FromData(data) |
| 3765 | dtb.Scan() |
| 3766 | |
| 3767 | node = dtb.GetNode('/images/kernel') |
| 3768 | data = dtb.GetProps(node)["data"].bytes |
| 3769 | align_pad = 0x10 - (len(U_BOOT_SPL_DATA) % 0x10) |
| 3770 | expected = (tools.GetBytes(0, 0x20) + U_BOOT_SPL_DATA + |
| 3771 | tools.GetBytes(0, align_pad) + U_BOOT_DATA) |
| 3772 | self.assertEqual(expected, data) |
| 3773 | |
| 3774 | node = dtb.GetNode('/images/fdt-1') |
| 3775 | data = dtb.GetProps(node)["data"].bytes |
| 3776 | expected = (U_BOOT_SPL_DTB_DATA + tools.GetBytes(0, 20) + |
| 3777 | tools.ToBytes(TEXT_DATA) + tools.GetBytes(0, 30) + |
| 3778 | U_BOOT_DTB_DATA) |
| 3779 | self.assertEqual(expected, data) |
| 3780 | |
| 3781 | def testFitExtblobMissingOk(self): |
| 3782 | """Test a FIT with a missing external blob that is allowed""" |
| 3783 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3784 | self._DoTestFile('168_fit_missing_blob.dts', |
| 3785 | allow_missing=True) |
| 3786 | err = stderr.getvalue() |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 3787 | self.assertRegex(err, "Image 'main-section'.*missing.*: atf-bl31") |
Alper Nebi Yasak | c261ec1 | 2020-08-31 12:58:20 +0300 | [diff] [blame] | 3788 | |
Simon Glass | 21db0ff | 2020-09-01 05:13:54 -0600 | [diff] [blame] | 3789 | def testBlobNamedByArgMissing(self): |
| 3790 | """Test handling of a missing entry arg""" |
| 3791 | with self.assertRaises(ValueError) as e: |
| 3792 | self._DoReadFile('068_blob_named_by_arg.dts') |
| 3793 | self.assertIn("Missing required properties/entry args: cros-ec-rw-path", |
| 3794 | str(e.exception)) |
| 3795 | |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 3796 | def testPackBl31(self): |
| 3797 | """Test that an image with an ATF BL31 binary can be created""" |
| 3798 | data = self._DoReadFile('169_atf_bl31.dts') |
| 3799 | self.assertEqual(ATF_BL31_DATA, data[:len(ATF_BL31_DATA)]) |
| 3800 | |
Samuel Holland | 9d8cc63 | 2020-10-21 21:12:15 -0500 | [diff] [blame] | 3801 | def testPackScp(self): |
| 3802 | """Test that an image with an SCP binary can be created""" |
| 3803 | data = self._DoReadFile('172_scp.dts') |
| 3804 | self.assertEqual(SCP_DATA, data[:len(SCP_DATA)]) |
| 3805 | |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3806 | def testFitFdt(self): |
| 3807 | """Test an image with an FIT with multiple FDT images""" |
| 3808 | def _CheckFdt(seq, expected_data): |
| 3809 | """Check the FDT nodes |
| 3810 | |
| 3811 | Args: |
| 3812 | seq: Sequence number to check (0 or 1) |
| 3813 | expected_data: Expected contents of 'data' property |
| 3814 | """ |
| 3815 | name = 'fdt-%d' % seq |
| 3816 | fnode = dtb.GetNode('/images/%s' % name) |
| 3817 | self.assertIsNotNone(fnode) |
| 3818 | self.assertEqual({'description','type', 'compression', 'data'}, |
| 3819 | set(fnode.props.keys())) |
| 3820 | self.assertEqual(expected_data, fnode.props['data'].bytes) |
| 3821 | self.assertEqual('fdt-test-fdt%d.dtb' % seq, |
| 3822 | fnode.props['description'].value) |
| 3823 | |
| 3824 | def _CheckConfig(seq, expected_data): |
| 3825 | """Check the configuration nodes |
| 3826 | |
| 3827 | Args: |
| 3828 | seq: Sequence number to check (0 or 1) |
| 3829 | expected_data: Expected contents of 'data' property |
| 3830 | """ |
| 3831 | cnode = dtb.GetNode('/configurations') |
| 3832 | self.assertIn('default', cnode.props) |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3833 | self.assertEqual('config-2', cnode.props['default'].value) |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3834 | |
| 3835 | name = 'config-%d' % seq |
| 3836 | fnode = dtb.GetNode('/configurations/%s' % name) |
| 3837 | self.assertIsNotNone(fnode) |
| 3838 | self.assertEqual({'description','firmware', 'loadables', 'fdt'}, |
| 3839 | set(fnode.props.keys())) |
| 3840 | self.assertEqual('conf-test-fdt%d.dtb' % seq, |
| 3841 | fnode.props['description'].value) |
| 3842 | self.assertEqual('fdt-%d' % seq, fnode.props['fdt'].value) |
| 3843 | |
| 3844 | entry_args = { |
| 3845 | 'of-list': 'test-fdt1 test-fdt2', |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3846 | 'default-dt': 'test-fdt2', |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3847 | } |
| 3848 | data = self._DoReadFileDtb( |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 3849 | '170_fit_fdt.dts', |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3850 | entry_args=entry_args, |
| 3851 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 3852 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3853 | fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3854 | |
| 3855 | dtb = fdt.Fdt.FromData(fit_data) |
| 3856 | dtb.Scan() |
| 3857 | fnode = dtb.GetNode('/images/kernel') |
| 3858 | self.assertIn('data', fnode.props) |
| 3859 | |
| 3860 | # Check all the properties in fdt-1 and fdt-2 |
| 3861 | _CheckFdt(1, TEST_FDT1_DATA) |
| 3862 | _CheckFdt(2, TEST_FDT2_DATA) |
| 3863 | |
| 3864 | # Check configurations |
| 3865 | _CheckConfig(1, TEST_FDT1_DATA) |
| 3866 | _CheckConfig(2, TEST_FDT2_DATA) |
| 3867 | |
| 3868 | def testFitFdtMissingList(self): |
| 3869 | """Test handling of a missing 'of-list' entry arg""" |
| 3870 | with self.assertRaises(ValueError) as e: |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 3871 | self._DoReadFile('170_fit_fdt.dts') |
Simon Glass | a435cd1 | 2020-09-01 05:13:59 -0600 | [diff] [blame] | 3872 | self.assertIn("Generator node requires 'of-list' entry argument", |
| 3873 | str(e.exception)) |
| 3874 | |
| 3875 | def testFitFdtEmptyList(self): |
| 3876 | """Test handling of an empty 'of-list' entry arg""" |
| 3877 | entry_args = { |
| 3878 | 'of-list': '', |
| 3879 | } |
| 3880 | data = self._DoReadFileDtb('170_fit_fdt.dts', entry_args=entry_args)[0] |
| 3881 | |
| 3882 | def testFitFdtMissingProp(self): |
| 3883 | """Test handling of a missing 'fit,fdt-list' property""" |
| 3884 | with self.assertRaises(ValueError) as e: |
| 3885 | self._DoReadFile('171_fit_fdt_missing_prop.dts') |
| 3886 | self.assertIn("Generator node requires 'fit,fdt-list' property", |
| 3887 | str(e.exception)) |
Simon Glass | 559c4de | 2020-09-01 05:13:58 -0600 | [diff] [blame] | 3888 | |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3889 | def testFitFdtEmptyList(self): |
| 3890 | """Test handling of an empty 'of-list' entry arg""" |
| 3891 | entry_args = { |
| 3892 | 'of-list': '', |
| 3893 | } |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 3894 | data = self._DoReadFileDtb('170_fit_fdt.dts', entry_args=entry_args)[0] |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3895 | |
| 3896 | def testFitFdtMissing(self): |
| 3897 | """Test handling of a missing 'default-dt' entry arg""" |
| 3898 | entry_args = { |
| 3899 | 'of-list': 'test-fdt1 test-fdt2', |
| 3900 | } |
| 3901 | with self.assertRaises(ValueError) as e: |
| 3902 | self._DoReadFileDtb( |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 3903 | '170_fit_fdt.dts', |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3904 | entry_args=entry_args, |
| 3905 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 3906 | self.assertIn("Generated 'default' node requires default-dt entry argument", |
| 3907 | str(e.exception)) |
| 3908 | |
| 3909 | def testFitFdtNotInList(self): |
| 3910 | """Test handling of a default-dt that is not in the of-list""" |
| 3911 | entry_args = { |
| 3912 | 'of-list': 'test-fdt1 test-fdt2', |
| 3913 | 'default-dt': 'test-fdt3', |
| 3914 | } |
| 3915 | with self.assertRaises(ValueError) as e: |
| 3916 | self._DoReadFileDtb( |
Bin Meng | 16cf566 | 2021-05-10 20:23:32 +0800 | [diff] [blame] | 3917 | '170_fit_fdt.dts', |
Simon Glass | 1032acc | 2020-09-06 10:39:08 -0600 | [diff] [blame] | 3918 | entry_args=entry_args, |
| 3919 | extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0] |
| 3920 | self.assertIn("default-dt entry argument 'test-fdt3' not found in fdt list: test-fdt1, test-fdt2", |
| 3921 | str(e.exception)) |
| 3922 | |
Simon Glass | a820af7 | 2020-09-06 10:39:09 -0600 | [diff] [blame] | 3923 | def testFitExtblobMissingHelp(self): |
| 3924 | """Test display of help messages when an external blob is missing""" |
| 3925 | control.missing_blob_help = control._ReadMissingBlobHelp() |
| 3926 | control.missing_blob_help['wibble'] = 'Wibble test' |
| 3927 | control.missing_blob_help['another'] = 'Another test' |
| 3928 | with test_util.capture_sys_output() as (stdout, stderr): |
| 3929 | self._DoTestFile('168_fit_missing_blob.dts', |
| 3930 | allow_missing=True) |
| 3931 | err = stderr.getvalue() |
| 3932 | |
| 3933 | # We can get the tag from the name, the type or the missing-msg |
| 3934 | # property. Check all three. |
| 3935 | self.assertIn('You may need to build ARM Trusted', err) |
| 3936 | self.assertIn('Wibble test', err) |
| 3937 | self.assertIn('Another test', err) |
| 3938 | |
Simon Glass | 6f1f4d4 | 2020-09-06 10:35:32 -0600 | [diff] [blame] | 3939 | def testMissingBlob(self): |
| 3940 | """Test handling of a blob containing a missing file""" |
| 3941 | with self.assertRaises(ValueError) as e: |
| 3942 | self._DoTestFile('173_missing_blob.dts', allow_missing=True) |
| 3943 | self.assertIn("Filename 'missing' not found in input path", |
| 3944 | str(e.exception)) |
| 3945 | |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 3946 | def testEnvironment(self): |
| 3947 | """Test adding a U-Boot environment""" |
| 3948 | data = self._DoReadFile('174_env.dts') |
| 3949 | self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)]) |
| 3950 | self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):]) |
| 3951 | env = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)] |
| 3952 | self.assertEqual(b'\x1b\x97\x22\x7c\x01var1=1\0var2="2"\0\0\xff\xff', |
| 3953 | env) |
| 3954 | |
| 3955 | def testEnvironmentNoSize(self): |
| 3956 | """Test that a missing 'size' property is detected""" |
| 3957 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8cdc08a | 2020-10-26 17:40:00 -0600 | [diff] [blame] | 3958 | self._DoTestFile('175_env_no_size.dts') |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 3959 | self.assertIn("'u-boot-env' entry must have a size property", |
| 3960 | str(e.exception)) |
| 3961 | |
| 3962 | def testEnvironmentTooSmall(self): |
| 3963 | """Test handling of an environment that does not fit""" |
| 3964 | with self.assertRaises(ValueError) as e: |
Simon Glass | 8cdc08a | 2020-10-26 17:40:00 -0600 | [diff] [blame] | 3965 | self._DoTestFile('176_env_too_small.dts') |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 3966 | |
| 3967 | # checksum, start byte, environment with \0 terminator, final \0 |
| 3968 | need = 4 + 1 + len(ENV_DATA) + 1 + 1 |
| 3969 | short = need - 0x8 |
| 3970 | self.assertIn("too small to hold data (need %#x more bytes)" % short, |
| 3971 | str(e.exception)) |
| 3972 | |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 3973 | def testSkipAtStart(self): |
| 3974 | """Test handling of skip-at-start section""" |
| 3975 | data = self._DoReadFile('177_skip_at_start.dts') |
| 3976 | self.assertEqual(U_BOOT_DATA, data) |
| 3977 | |
| 3978 | image = control.images['image'] |
| 3979 | entries = image.GetEntries() |
| 3980 | section = entries['section'] |
| 3981 | self.assertEqual(0, section.offset) |
| 3982 | self.assertEqual(len(U_BOOT_DATA), section.size) |
| 3983 | self.assertEqual(U_BOOT_DATA, section.GetData()) |
| 3984 | |
| 3985 | entry = section.GetEntries()['u-boot'] |
| 3986 | self.assertEqual(16, entry.offset) |
| 3987 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 3988 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 3989 | |
| 3990 | def testSkipAtStartPad(self): |
| 3991 | """Test handling of skip-at-start section with padded entry""" |
| 3992 | data = self._DoReadFile('178_skip_at_start_pad.dts') |
| 3993 | before = tools.GetBytes(0, 8) |
| 3994 | after = tools.GetBytes(0, 4) |
| 3995 | all = before + U_BOOT_DATA + after |
| 3996 | self.assertEqual(all, data) |
| 3997 | |
| 3998 | image = control.images['image'] |
| 3999 | entries = image.GetEntries() |
| 4000 | section = entries['section'] |
| 4001 | self.assertEqual(0, section.offset) |
| 4002 | self.assertEqual(len(all), section.size) |
| 4003 | self.assertEqual(all, section.GetData()) |
| 4004 | |
| 4005 | entry = section.GetEntries()['u-boot'] |
| 4006 | self.assertEqual(16, entry.offset) |
| 4007 | self.assertEqual(len(all), entry.size) |
| 4008 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 4009 | |
| 4010 | def testSkipAtStartSectionPad(self): |
| 4011 | """Test handling of skip-at-start section with padding""" |
| 4012 | data = self._DoReadFile('179_skip_at_start_section_pad.dts') |
| 4013 | before = tools.GetBytes(0, 8) |
| 4014 | after = tools.GetBytes(0, 4) |
| 4015 | all = before + U_BOOT_DATA + after |
Simon Glass | 510ef0f | 2020-10-26 17:40:13 -0600 | [diff] [blame] | 4016 | self.assertEqual(all, data) |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 4017 | |
| 4018 | image = control.images['image'] |
| 4019 | entries = image.GetEntries() |
| 4020 | section = entries['section'] |
| 4021 | self.assertEqual(0, section.offset) |
| 4022 | self.assertEqual(len(all), section.size) |
Simon Glass | 72eeff1 | 2020-10-26 17:40:16 -0600 | [diff] [blame] | 4023 | self.assertEqual(U_BOOT_DATA, section.data) |
Simon Glass | 510ef0f | 2020-10-26 17:40:13 -0600 | [diff] [blame] | 4024 | self.assertEqual(all, section.GetPaddedData()) |
Simon Glass | d1fdf75 | 2020-10-26 17:40:01 -0600 | [diff] [blame] | 4025 | |
| 4026 | entry = section.GetEntries()['u-boot'] |
| 4027 | self.assertEqual(16, entry.offset) |
| 4028 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 4029 | self.assertEqual(U_BOOT_DATA, entry.data) |
Simon Glass | a072950 | 2020-09-06 10:35:33 -0600 | [diff] [blame] | 4030 | |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4031 | def testSectionPad(self): |
| 4032 | """Testing padding with sections""" |
| 4033 | data = self._DoReadFile('180_section_pad.dts') |
| 4034 | expected = (tools.GetBytes(ord('&'), 3) + |
| 4035 | tools.GetBytes(ord('!'), 5) + |
| 4036 | U_BOOT_DATA + |
| 4037 | tools.GetBytes(ord('!'), 1) + |
| 4038 | tools.GetBytes(ord('&'), 2)) |
| 4039 | self.assertEqual(expected, data) |
| 4040 | |
| 4041 | def testSectionAlign(self): |
| 4042 | """Testing alignment with sections""" |
| 4043 | data = self._DoReadFileDtb('181_section_align.dts', map=True)[0] |
| 4044 | expected = (b'\0' + # fill section |
| 4045 | tools.GetBytes(ord('&'), 1) + # padding to section align |
| 4046 | b'\0' + # fill section |
| 4047 | tools.GetBytes(ord('!'), 3) + # padding to u-boot align |
| 4048 | U_BOOT_DATA + |
| 4049 | tools.GetBytes(ord('!'), 4) + # padding to u-boot size |
| 4050 | tools.GetBytes(ord('!'), 4)) # padding to section size |
| 4051 | self.assertEqual(expected, data) |
| 4052 | |
Simon Glass | d92c836 | 2020-10-26 17:40:25 -0600 | [diff] [blame] | 4053 | def testCompressImage(self): |
| 4054 | """Test compression of the entire image""" |
| 4055 | self._CheckLz4() |
| 4056 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4057 | '182_compress_image.dts', use_real_dtb=True, update_dtb=True) |
| 4058 | dtb = fdt.Fdt(out_dtb_fname) |
| 4059 | dtb.Scan() |
| 4060 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4061 | 'uncomp-size']) |
| 4062 | orig = self._decompress(data) |
| 4063 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4064 | |
| 4065 | # Do a sanity check on various fields |
| 4066 | image = control.images['image'] |
| 4067 | entries = image.GetEntries() |
| 4068 | self.assertEqual(2, len(entries)) |
| 4069 | |
| 4070 | entry = entries['blob'] |
| 4071 | self.assertEqual(COMPRESS_DATA, entry.data) |
| 4072 | self.assertEqual(len(COMPRESS_DATA), entry.size) |
| 4073 | |
| 4074 | entry = entries['u-boot'] |
| 4075 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 4076 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 4077 | |
| 4078 | self.assertEqual(len(data), image.size) |
| 4079 | self.assertEqual(COMPRESS_DATA + U_BOOT_DATA, image.uncomp_data) |
| 4080 | self.assertEqual(len(COMPRESS_DATA + U_BOOT_DATA), image.uncomp_size) |
| 4081 | orig = self._decompress(image.data) |
| 4082 | self.assertEqual(orig, image.uncomp_data) |
| 4083 | |
| 4084 | expected = { |
| 4085 | 'blob:offset': 0, |
| 4086 | 'blob:size': len(COMPRESS_DATA), |
| 4087 | 'u-boot:offset': len(COMPRESS_DATA), |
| 4088 | 'u-boot:size': len(U_BOOT_DATA), |
| 4089 | 'uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4090 | 'offset': 0, |
| 4091 | 'image-pos': 0, |
| 4092 | 'size': len(data), |
| 4093 | } |
| 4094 | self.assertEqual(expected, props) |
| 4095 | |
| 4096 | def testCompressImageLess(self): |
| 4097 | """Test compression where compression reduces the image size""" |
| 4098 | self._CheckLz4() |
| 4099 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4100 | '183_compress_image_less.dts', use_real_dtb=True, update_dtb=True) |
| 4101 | dtb = fdt.Fdt(out_dtb_fname) |
| 4102 | dtb.Scan() |
| 4103 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4104 | 'uncomp-size']) |
| 4105 | orig = self._decompress(data) |
| 4106 | |
| 4107 | self.assertEquals(COMPRESS_DATA + COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4108 | |
| 4109 | # Do a sanity check on various fields |
| 4110 | image = control.images['image'] |
| 4111 | entries = image.GetEntries() |
| 4112 | self.assertEqual(2, len(entries)) |
| 4113 | |
| 4114 | entry = entries['blob'] |
| 4115 | self.assertEqual(COMPRESS_DATA_BIG, entry.data) |
| 4116 | self.assertEqual(len(COMPRESS_DATA_BIG), entry.size) |
| 4117 | |
| 4118 | entry = entries['u-boot'] |
| 4119 | self.assertEqual(U_BOOT_DATA, entry.data) |
| 4120 | self.assertEqual(len(U_BOOT_DATA), entry.size) |
| 4121 | |
| 4122 | self.assertEqual(len(data), image.size) |
| 4123 | self.assertEqual(COMPRESS_DATA_BIG + U_BOOT_DATA, image.uncomp_data) |
| 4124 | self.assertEqual(len(COMPRESS_DATA_BIG + U_BOOT_DATA), |
| 4125 | image.uncomp_size) |
| 4126 | orig = self._decompress(image.data) |
| 4127 | self.assertEqual(orig, image.uncomp_data) |
| 4128 | |
| 4129 | expected = { |
| 4130 | 'blob:offset': 0, |
| 4131 | 'blob:size': len(COMPRESS_DATA_BIG), |
| 4132 | 'u-boot:offset': len(COMPRESS_DATA_BIG), |
| 4133 | 'u-boot:size': len(U_BOOT_DATA), |
| 4134 | 'uncomp-size': len(COMPRESS_DATA_BIG + U_BOOT_DATA), |
| 4135 | 'offset': 0, |
| 4136 | 'image-pos': 0, |
| 4137 | 'size': len(data), |
| 4138 | } |
| 4139 | self.assertEqual(expected, props) |
| 4140 | |
| 4141 | def testCompressSectionSize(self): |
| 4142 | """Test compression of a section with a fixed size""" |
| 4143 | self._CheckLz4() |
| 4144 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4145 | '184_compress_section_size.dts', use_real_dtb=True, update_dtb=True) |
| 4146 | dtb = fdt.Fdt(out_dtb_fname) |
| 4147 | dtb.Scan() |
| 4148 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4149 | 'uncomp-size']) |
| 4150 | orig = self._decompress(data) |
| 4151 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4152 | expected = { |
| 4153 | 'section/blob:offset': 0, |
| 4154 | 'section/blob:size': len(COMPRESS_DATA), |
| 4155 | 'section/u-boot:offset': len(COMPRESS_DATA), |
| 4156 | 'section/u-boot:size': len(U_BOOT_DATA), |
| 4157 | 'section:offset': 0, |
| 4158 | 'section:image-pos': 0, |
| 4159 | 'section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4160 | 'section:size': 0x30, |
| 4161 | 'offset': 0, |
| 4162 | 'image-pos': 0, |
| 4163 | 'size': 0x30, |
| 4164 | } |
| 4165 | self.assertEqual(expected, props) |
| 4166 | |
| 4167 | def testCompressSection(self): |
| 4168 | """Test compression of a section with no fixed size""" |
| 4169 | self._CheckLz4() |
| 4170 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4171 | '185_compress_section.dts', use_real_dtb=True, update_dtb=True) |
| 4172 | dtb = fdt.Fdt(out_dtb_fname) |
| 4173 | dtb.Scan() |
| 4174 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4175 | 'uncomp-size']) |
| 4176 | orig = self._decompress(data) |
| 4177 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig) |
| 4178 | expected = { |
| 4179 | 'section/blob:offset': 0, |
| 4180 | 'section/blob:size': len(COMPRESS_DATA), |
| 4181 | 'section/u-boot:offset': len(COMPRESS_DATA), |
| 4182 | 'section/u-boot:size': len(U_BOOT_DATA), |
| 4183 | 'section:offset': 0, |
| 4184 | 'section:image-pos': 0, |
| 4185 | 'section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4186 | 'section:size': len(data), |
| 4187 | 'offset': 0, |
| 4188 | 'image-pos': 0, |
| 4189 | 'size': len(data), |
| 4190 | } |
| 4191 | self.assertEqual(expected, props) |
| 4192 | |
| 4193 | def testCompressExtra(self): |
| 4194 | """Test compression of a section with no fixed size""" |
| 4195 | self._CheckLz4() |
| 4196 | data, _, _, out_dtb_fname = self._DoReadFileDtb( |
| 4197 | '186_compress_extra.dts', use_real_dtb=True, update_dtb=True) |
| 4198 | dtb = fdt.Fdt(out_dtb_fname) |
| 4199 | dtb.Scan() |
| 4200 | props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size', |
| 4201 | 'uncomp-size']) |
| 4202 | |
| 4203 | base = data[len(U_BOOT_DATA):] |
| 4204 | self.assertEquals(U_BOOT_DATA, base[:len(U_BOOT_DATA)]) |
| 4205 | rest = base[len(U_BOOT_DATA):] |
| 4206 | |
| 4207 | # Check compressed data |
| 4208 | section1 = self._decompress(rest) |
| 4209 | expect1 = tools.Compress(COMPRESS_DATA + U_BOOT_DATA, 'lz4') |
| 4210 | self.assertEquals(expect1, rest[:len(expect1)]) |
| 4211 | self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, section1) |
| 4212 | rest1 = rest[len(expect1):] |
| 4213 | |
| 4214 | section2 = self._decompress(rest1) |
| 4215 | expect2 = tools.Compress(COMPRESS_DATA + COMPRESS_DATA, 'lz4') |
| 4216 | self.assertEquals(expect2, rest1[:len(expect2)]) |
| 4217 | self.assertEquals(COMPRESS_DATA + COMPRESS_DATA, section2) |
| 4218 | rest2 = rest1[len(expect2):] |
| 4219 | |
| 4220 | expect_size = (len(U_BOOT_DATA) + len(U_BOOT_DATA) + len(expect1) + |
| 4221 | len(expect2) + len(U_BOOT_DATA)) |
| 4222 | #self.assertEquals(expect_size, len(data)) |
| 4223 | |
| 4224 | #self.assertEquals(U_BOOT_DATA, rest2) |
| 4225 | |
| 4226 | self.maxDiff = None |
| 4227 | expected = { |
| 4228 | 'u-boot:offset': 0, |
| 4229 | 'u-boot:image-pos': 0, |
| 4230 | 'u-boot:size': len(U_BOOT_DATA), |
| 4231 | |
| 4232 | 'base:offset': len(U_BOOT_DATA), |
| 4233 | 'base:image-pos': len(U_BOOT_DATA), |
| 4234 | 'base:size': len(data) - len(U_BOOT_DATA), |
| 4235 | 'base/u-boot:offset': 0, |
| 4236 | 'base/u-boot:image-pos': len(U_BOOT_DATA), |
| 4237 | 'base/u-boot:size': len(U_BOOT_DATA), |
| 4238 | 'base/u-boot2:offset': len(U_BOOT_DATA) + len(expect1) + |
| 4239 | len(expect2), |
| 4240 | 'base/u-boot2:image-pos': len(U_BOOT_DATA) * 2 + len(expect1) + |
| 4241 | len(expect2), |
| 4242 | 'base/u-boot2:size': len(U_BOOT_DATA), |
| 4243 | |
| 4244 | 'base/section:offset': len(U_BOOT_DATA), |
| 4245 | 'base/section:image-pos': len(U_BOOT_DATA) * 2, |
| 4246 | 'base/section:size': len(expect1), |
| 4247 | 'base/section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA), |
| 4248 | 'base/section/blob:offset': 0, |
| 4249 | 'base/section/blob:size': len(COMPRESS_DATA), |
| 4250 | 'base/section/u-boot:offset': len(COMPRESS_DATA), |
| 4251 | 'base/section/u-boot:size': len(U_BOOT_DATA), |
| 4252 | |
| 4253 | 'base/section2:offset': len(U_BOOT_DATA) + len(expect1), |
| 4254 | 'base/section2:image-pos': len(U_BOOT_DATA) * 2 + len(expect1), |
| 4255 | 'base/section2:size': len(expect2), |
| 4256 | 'base/section2:uncomp-size': len(COMPRESS_DATA + COMPRESS_DATA), |
| 4257 | 'base/section2/blob:offset': 0, |
| 4258 | 'base/section2/blob:size': len(COMPRESS_DATA), |
| 4259 | 'base/section2/blob2:offset': len(COMPRESS_DATA), |
| 4260 | 'base/section2/blob2:size': len(COMPRESS_DATA), |
| 4261 | |
| 4262 | 'offset': 0, |
| 4263 | 'image-pos': 0, |
| 4264 | 'size': len(data), |
| 4265 | } |
| 4266 | self.assertEqual(expected, props) |
| 4267 | |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 4268 | def testSymbolsSubsection(self): |
| 4269 | """Test binman can assign symbols from a subsection""" |
Simon Glass | 31e04cb | 2021-03-18 20:24:56 +1300 | [diff] [blame] | 4270 | self.checkSymbols('187_symbols_sub.dts', U_BOOT_SPL_DATA, 0x18) |
Simon Glass | ecbe473 | 2021-01-06 21:35:15 -0700 | [diff] [blame] | 4271 | |
Simon Glass | 3fb2540 | 2021-01-06 21:35:16 -0700 | [diff] [blame] | 4272 | def testReadImageEntryArg(self): |
| 4273 | """Test reading an image that would need an entry arg to generate""" |
| 4274 | entry_args = { |
| 4275 | 'cros-ec-rw-path': 'ecrw.bin', |
| 4276 | } |
| 4277 | data = self.data = self._DoReadFileDtb( |
| 4278 | '188_image_entryarg.dts',use_real_dtb=True, update_dtb=True, |
| 4279 | entry_args=entry_args) |
| 4280 | |
| 4281 | image_fname = tools.GetOutputFilename('image.bin') |
| 4282 | orig_image = control.images['image'] |
| 4283 | |
| 4284 | # This should not generate an error about the missing 'cros-ec-rw-path' |
| 4285 | # since we are reading the image from a file. Compare with |
| 4286 | # testEntryArgsRequired() |
| 4287 | image = Image.FromFile(image_fname) |
| 4288 | self.assertEqual(orig_image.GetEntries().keys(), |
| 4289 | image.GetEntries().keys()) |
| 4290 | |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 4291 | def testFilesAlign(self): |
| 4292 | """Test alignment with files""" |
| 4293 | data = self._DoReadFile('190_files_align.dts') |
| 4294 | |
| 4295 | # The first string is 15 bytes so will align to 16 |
| 4296 | expect = FILES_DATA[:15] + b'\0' + FILES_DATA[15:] |
| 4297 | self.assertEqual(expect, data) |
| 4298 | |
Simon Glass | db84b56 | 2021-01-06 21:35:19 -0700 | [diff] [blame] | 4299 | def testReadImageSkip(self): |
| 4300 | """Test reading an image and accessing its FDT map""" |
| 4301 | data = self.data = self._DoReadFileRealDtb('191_read_image_skip.dts') |
| 4302 | image_fname = tools.GetOutputFilename('image.bin') |
| 4303 | orig_image = control.images['image'] |
| 4304 | image = Image.FromFile(image_fname) |
| 4305 | self.assertEqual(orig_image.GetEntries().keys(), |
| 4306 | image.GetEntries().keys()) |
| 4307 | |
| 4308 | orig_entry = orig_image.GetEntries()['fdtmap'] |
| 4309 | entry = image.GetEntries()['fdtmap'] |
| 4310 | self.assertEqual(orig_entry.offset, entry.offset) |
| 4311 | self.assertEqual(orig_entry.size, entry.size) |
| 4312 | self.assertEqual(16, entry.image_pos) |
| 4313 | |
| 4314 | u_boot = image.GetEntries()['section'].GetEntries()['u-boot'] |
| 4315 | |
| 4316 | self.assertEquals(U_BOOT_DATA, u_boot.ReadData()) |
| 4317 | |
Simon Glass | c98de97 | 2021-03-18 20:24:57 +1300 | [diff] [blame] | 4318 | def testTplNoDtb(self): |
| 4319 | """Test that an image with tpl/u-boot-tpl-nodtb.bin can be created""" |
Simon Glass | 13089cc | 2021-04-25 08:39:32 +1200 | [diff] [blame] | 4320 | self._SetupTplElf() |
Simon Glass | c98de97 | 2021-03-18 20:24:57 +1300 | [diff] [blame] | 4321 | data = self._DoReadFile('192_u_boot_tpl_nodtb.dts') |
| 4322 | self.assertEqual(U_BOOT_TPL_NODTB_DATA, |
| 4323 | data[:len(U_BOOT_TPL_NODTB_DATA)]) |
| 4324 | |
Simon Glass | 63f41d4 | 2021-03-18 20:24:58 +1300 | [diff] [blame] | 4325 | def testTplBssPad(self): |
| 4326 | """Test that we can pad TPL's BSS with zeros""" |
| 4327 | # ELF file with a '__bss_size' symbol |
| 4328 | self._SetupTplElf() |
| 4329 | data = self._DoReadFile('193_tpl_bss_pad.dts') |
| 4330 | self.assertEqual(U_BOOT_TPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA, |
| 4331 | data) |
| 4332 | |
| 4333 | def testTplBssPadMissing(self): |
| 4334 | """Test that a missing symbol is detected""" |
| 4335 | self._SetupTplElf('u_boot_ucode_ptr') |
| 4336 | with self.assertRaises(ValueError) as e: |
| 4337 | self._DoReadFile('193_tpl_bss_pad.dts') |
| 4338 | self.assertIn('Expected __bss_size symbol in tpl/u-boot-tpl', |
| 4339 | str(e.exception)) |
| 4340 | |
Simon Glass | 718b529 | 2021-03-18 20:25:07 +1300 | [diff] [blame] | 4341 | def checkDtbSizes(self, data, pad_len, start): |
| 4342 | """Check the size arguments in a dtb embedded in an image |
| 4343 | |
| 4344 | Args: |
| 4345 | data: The image data |
| 4346 | pad_len: Length of the pad section in the image, in bytes |
| 4347 | start: Start offset of the devicetree to examine, within the image |
| 4348 | |
| 4349 | Returns: |
| 4350 | Size of the devicetree in bytes |
| 4351 | """ |
| 4352 | dtb_data = data[start:] |
| 4353 | dtb = fdt.Fdt.FromData(dtb_data) |
| 4354 | fdt_size = dtb.GetFdtObj().totalsize() |
| 4355 | dtb.Scan() |
| 4356 | props = self._GetPropTree(dtb, 'size') |
| 4357 | self.assertEqual({ |
| 4358 | 'size': len(data), |
| 4359 | 'u-boot-spl/u-boot-spl-bss-pad:size': pad_len, |
| 4360 | 'u-boot-spl/u-boot-spl-dtb:size': 801, |
| 4361 | 'u-boot-spl/u-boot-spl-nodtb:size': len(U_BOOT_SPL_NODTB_DATA), |
| 4362 | 'u-boot-spl:size': 860, |
| 4363 | 'u-boot-tpl:size': len(U_BOOT_TPL_DATA), |
| 4364 | 'u-boot/u-boot-dtb:size': 781, |
| 4365 | 'u-boot/u-boot-nodtb:size': len(U_BOOT_NODTB_DATA), |
| 4366 | 'u-boot:size': 827, |
| 4367 | }, props) |
| 4368 | return fdt_size |
| 4369 | |
| 4370 | def testExpanded(self): |
| 4371 | """Test that an expanded entry type is selected when needed""" |
| 4372 | self._SetupSplElf() |
| 4373 | self._SetupTplElf() |
| 4374 | |
| 4375 | # SPL has a devicetree, TPL does not |
| 4376 | entry_args = { |
| 4377 | 'spl-dtb': '1', |
| 4378 | 'spl-bss-pad': 'y', |
| 4379 | 'tpl-dtb': '', |
| 4380 | } |
| 4381 | self._DoReadFileDtb('194_fdt_incl.dts', use_expanded=True, |
| 4382 | entry_args=entry_args) |
| 4383 | image = control.images['image'] |
| 4384 | entries = image.GetEntries() |
| 4385 | self.assertEqual(3, len(entries)) |
| 4386 | |
| 4387 | # First, u-boot, which should be expanded into u-boot-nodtb and dtb |
| 4388 | self.assertIn('u-boot', entries) |
| 4389 | entry = entries['u-boot'] |
| 4390 | self.assertEqual('u-boot-expanded', entry.etype) |
| 4391 | subent = entry.GetEntries() |
| 4392 | self.assertEqual(2, len(subent)) |
| 4393 | self.assertIn('u-boot-nodtb', subent) |
| 4394 | self.assertIn('u-boot-dtb', subent) |
| 4395 | |
| 4396 | # Second, u-boot-spl, which should be expanded into three parts |
| 4397 | self.assertIn('u-boot-spl', entries) |
| 4398 | entry = entries['u-boot-spl'] |
| 4399 | self.assertEqual('u-boot-spl-expanded', entry.etype) |
| 4400 | subent = entry.GetEntries() |
| 4401 | self.assertEqual(3, len(subent)) |
| 4402 | self.assertIn('u-boot-spl-nodtb', subent) |
| 4403 | self.assertIn('u-boot-spl-bss-pad', subent) |
| 4404 | self.assertIn('u-boot-spl-dtb', subent) |
| 4405 | |
| 4406 | # Third, u-boot-tpl, which should be not be expanded, since TPL has no |
| 4407 | # devicetree |
| 4408 | self.assertIn('u-boot-tpl', entries) |
| 4409 | entry = entries['u-boot-tpl'] |
| 4410 | self.assertEqual('u-boot-tpl', entry.etype) |
| 4411 | self.assertEqual(None, entry.GetEntries()) |
| 4412 | |
| 4413 | def testExpandedTpl(self): |
| 4414 | """Test that an expanded entry type is selected for TPL when needed""" |
| 4415 | self._SetupTplElf() |
| 4416 | |
| 4417 | entry_args = { |
| 4418 | 'tpl-bss-pad': 'y', |
| 4419 | 'tpl-dtb': 'y', |
| 4420 | } |
| 4421 | self._DoReadFileDtb('195_fdt_incl_tpl.dts', use_expanded=True, |
| 4422 | entry_args=entry_args) |
| 4423 | image = control.images['image'] |
| 4424 | entries = image.GetEntries() |
| 4425 | self.assertEqual(1, len(entries)) |
| 4426 | |
| 4427 | # We only have u-boot-tpl, which be expanded |
| 4428 | self.assertIn('u-boot-tpl', entries) |
| 4429 | entry = entries['u-boot-tpl'] |
| 4430 | self.assertEqual('u-boot-tpl-expanded', entry.etype) |
| 4431 | subent = entry.GetEntries() |
| 4432 | self.assertEqual(3, len(subent)) |
| 4433 | self.assertIn('u-boot-tpl-nodtb', subent) |
| 4434 | self.assertIn('u-boot-tpl-bss-pad', subent) |
| 4435 | self.assertIn('u-boot-tpl-dtb', subent) |
| 4436 | |
| 4437 | def testExpandedNoPad(self): |
| 4438 | """Test an expanded entry without BSS pad enabled""" |
| 4439 | self._SetupSplElf() |
| 4440 | self._SetupTplElf() |
| 4441 | |
| 4442 | # SPL has a devicetree, TPL does not |
| 4443 | entry_args = { |
| 4444 | 'spl-dtb': 'something', |
| 4445 | 'spl-bss-pad': 'n', |
| 4446 | 'tpl-dtb': '', |
| 4447 | } |
| 4448 | self._DoReadFileDtb('194_fdt_incl.dts', use_expanded=True, |
| 4449 | entry_args=entry_args) |
| 4450 | image = control.images['image'] |
| 4451 | entries = image.GetEntries() |
| 4452 | |
| 4453 | # Just check u-boot-spl, which should be expanded into two parts |
| 4454 | self.assertIn('u-boot-spl', entries) |
| 4455 | entry = entries['u-boot-spl'] |
| 4456 | self.assertEqual('u-boot-spl-expanded', entry.etype) |
| 4457 | subent = entry.GetEntries() |
| 4458 | self.assertEqual(2, len(subent)) |
| 4459 | self.assertIn('u-boot-spl-nodtb', subent) |
| 4460 | self.assertIn('u-boot-spl-dtb', subent) |
| 4461 | |
| 4462 | def testExpandedTplNoPad(self): |
| 4463 | """Test that an expanded entry type with padding disabled in TPL""" |
| 4464 | self._SetupTplElf() |
| 4465 | |
| 4466 | entry_args = { |
| 4467 | 'tpl-bss-pad': '', |
| 4468 | 'tpl-dtb': 'y', |
| 4469 | } |
| 4470 | self._DoReadFileDtb('195_fdt_incl_tpl.dts', use_expanded=True, |
| 4471 | entry_args=entry_args) |
| 4472 | image = control.images['image'] |
| 4473 | entries = image.GetEntries() |
| 4474 | self.assertEqual(1, len(entries)) |
| 4475 | |
| 4476 | # We only have u-boot-tpl, which be expanded |
| 4477 | self.assertIn('u-boot-tpl', entries) |
| 4478 | entry = entries['u-boot-tpl'] |
| 4479 | self.assertEqual('u-boot-tpl-expanded', entry.etype) |
| 4480 | subent = entry.GetEntries() |
| 4481 | self.assertEqual(2, len(subent)) |
| 4482 | self.assertIn('u-boot-tpl-nodtb', subent) |
| 4483 | self.assertIn('u-boot-tpl-dtb', subent) |
| 4484 | |
| 4485 | def testFdtInclude(self): |
| 4486 | """Test that an Fdt is update within all binaries""" |
| 4487 | self._SetupSplElf() |
| 4488 | self._SetupTplElf() |
| 4489 | |
| 4490 | # SPL has a devicetree, TPL does not |
| 4491 | self.maxDiff = None |
| 4492 | entry_args = { |
| 4493 | 'spl-dtb': '1', |
| 4494 | 'spl-bss-pad': 'y', |
| 4495 | 'tpl-dtb': '', |
| 4496 | } |
| 4497 | # Build the image. It includes two separate devicetree binaries, each |
| 4498 | # with their own contents, but all contain the binman definition. |
| 4499 | data = self._DoReadFileDtb( |
| 4500 | '194_fdt_incl.dts', use_real_dtb=True, use_expanded=True, |
| 4501 | update_dtb=True, entry_args=entry_args)[0] |
| 4502 | pad_len = 10 |
| 4503 | |
| 4504 | # Check the U-Boot dtb |
| 4505 | start = len(U_BOOT_NODTB_DATA) |
| 4506 | fdt_size = self.checkDtbSizes(data, pad_len, start) |
| 4507 | |
| 4508 | # Now check SPL |
| 4509 | start += fdt_size + len(U_BOOT_SPL_NODTB_DATA) + pad_len |
| 4510 | fdt_size = self.checkDtbSizes(data, pad_len, start) |
| 4511 | |
| 4512 | # TPL has no devicetree |
| 4513 | start += fdt_size + len(U_BOOT_TPL_DATA) |
| 4514 | self.assertEqual(len(data), start) |
Simon Glass | bb39574 | 2020-10-26 17:40:14 -0600 | [diff] [blame] | 4515 | |
Simon Glass | 7098b7f | 2021-03-21 18:24:30 +1300 | [diff] [blame] | 4516 | def testSymbolsExpanded(self): |
| 4517 | """Test binman can assign symbols in expanded entries""" |
| 4518 | entry_args = { |
| 4519 | 'spl-dtb': '1', |
| 4520 | } |
| 4521 | self.checkSymbols('197_symbols_expand.dts', U_BOOT_SPL_NODTB_DATA + |
| 4522 | U_BOOT_SPL_DTB_DATA, 0x38, |
| 4523 | entry_args=entry_args, use_expanded=True) |
| 4524 | |
Simon Glass | e191578 | 2021-03-21 18:24:31 +1300 | [diff] [blame] | 4525 | def testCollection(self): |
| 4526 | """Test a collection""" |
| 4527 | data = self._DoReadFile('198_collection.dts') |
| 4528 | self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA + |
| 4529 | tools.GetBytes(0xff, 2) + U_BOOT_NODTB_DATA + |
| 4530 | tools.GetBytes(0xfe, 3) + U_BOOT_DTB_DATA, |
| 4531 | data) |
| 4532 | |
Simon Glass | 27a7f77 | 2021-03-21 18:24:32 +1300 | [diff] [blame] | 4533 | def testCollectionSection(self): |
| 4534 | """Test a collection where a section must be built first""" |
| 4535 | # Sections never have their contents when GetData() is called, but when |
| 4536 | # _BuildSectionData() is called with required=True, a section will force |
| 4537 | # building the contents, producing an error is anything is still |
| 4538 | # missing. |
| 4539 | data = self._DoReadFile('199_collection_section.dts') |
| 4540 | section = U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA |
| 4541 | self.assertEqual(section + U_BOOT_DATA + tools.GetBytes(0xff, 2) + |
| 4542 | section + tools.GetBytes(0xfe, 3) + U_BOOT_DATA, |
| 4543 | data) |
| 4544 | |
Simon Glass | f427c5f | 2021-03-21 18:24:33 +1300 | [diff] [blame] | 4545 | def testAlignDefault(self): |
| 4546 | """Test that default alignment works on sections""" |
| 4547 | data = self._DoReadFile('200_align_default.dts') |
| 4548 | expected = (U_BOOT_DATA + tools.GetBytes(0, 8 - len(U_BOOT_DATA)) + |
| 4549 | U_BOOT_DATA) |
| 4550 | # Special alignment for section |
| 4551 | expected += tools.GetBytes(0, 32 - len(expected)) |
| 4552 | # No alignment within the nested section |
| 4553 | expected += U_BOOT_DATA + U_BOOT_NODTB_DATA; |
| 4554 | # Now the final piece, which should be default-aligned |
| 4555 | expected += tools.GetBytes(0, 88 - len(expected)) + U_BOOT_NODTB_DATA |
| 4556 | self.assertEqual(expected, data) |
Simon Glass | 27a7f77 | 2021-03-21 18:24:32 +1300 | [diff] [blame] | 4557 | |
Bin Meng | c0b1574 | 2021-05-10 20:23:33 +0800 | [diff] [blame] | 4558 | def testPackOpenSBI(self): |
| 4559 | """Test that an image with an OpenSBI binary can be created""" |
| 4560 | data = self._DoReadFile('201_opensbi.dts') |
| 4561 | self.assertEqual(OPENSBI_DATA, data[:len(OPENSBI_DATA)]) |
| 4562 | |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 4563 | def testSectionsSingleThread(self): |
| 4564 | """Test sections without multithreading""" |
| 4565 | data = self._DoReadFileDtb('055_sections.dts', threads=0)[0] |
| 4566 | expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) + |
| 4567 | U_BOOT_DATA + tools.GetBytes(ord('a'), 12) + |
| 4568 | U_BOOT_DATA + tools.GetBytes(ord('&'), 4)) |
| 4569 | self.assertEqual(expected, data) |
| 4570 | |
| 4571 | def testThreadTimeout(self): |
| 4572 | """Test handling a thread that takes too long""" |
| 4573 | with self.assertRaises(ValueError) as e: |
| 4574 | self._DoTestFile('202_section_timeout.dts', |
| 4575 | test_section_timeout=True) |
Simon Glass | 2d59d15 | 2021-10-18 12:13:15 -0600 | [diff] [blame] | 4576 | self.assertIn("Timed out obtaining contents", str(e.exception)) |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 4577 | |
Simon Glass | 748a1d4 | 2021-07-06 10:36:41 -0600 | [diff] [blame] | 4578 | def testTiming(self): |
| 4579 | """Test output of timing information""" |
| 4580 | data = self._DoReadFile('055_sections.dts') |
| 4581 | with test_util.capture_sys_output() as (stdout, stderr): |
| 4582 | state.TimingShow() |
| 4583 | self.assertIn('read:', stdout.getvalue()) |
| 4584 | self.assertIn('compress:', stdout.getvalue()) |
| 4585 | |
Simon Glass | adfb849 | 2021-11-03 21:09:18 -0600 | [diff] [blame^] | 4586 | def testUpdateFdtInElf(self): |
| 4587 | """Test that we can update the devicetree in an ELF file""" |
| 4588 | infile = elf_fname = self.ElfTestFile('u_boot_binman_embed') |
| 4589 | outfile = os.path.join(self._indir, 'u-boot.out') |
| 4590 | begin_sym = 'dtb_embed_begin' |
| 4591 | end_sym = 'dtb_embed_end' |
| 4592 | retcode = self._DoTestFile( |
| 4593 | '060_fdt_update.dts', update_dtb=True, |
| 4594 | update_fdt_in_elf=','.join([infile,outfile,begin_sym,end_sym])) |
| 4595 | self.assertEqual(0, retcode) |
| 4596 | |
| 4597 | # Check that the output file does in fact contact a dtb with the binman |
| 4598 | # definition in the correct place |
| 4599 | syms = elf.GetSymbolFileOffset(infile, |
| 4600 | ['dtb_embed_begin', 'dtb_embed_end']) |
| 4601 | data = tools.ReadFile(outfile) |
| 4602 | dtb_data = data[syms['dtb_embed_begin'].offset: |
| 4603 | syms['dtb_embed_end'].offset] |
| 4604 | |
| 4605 | dtb = fdt.Fdt.FromData(dtb_data) |
| 4606 | dtb.Scan() |
| 4607 | props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS) |
| 4608 | self.assertEqual({ |
| 4609 | 'image-pos': 0, |
| 4610 | 'offset': 0, |
| 4611 | '_testing:offset': 32, |
| 4612 | '_testing:size': 2, |
| 4613 | '_testing:image-pos': 32, |
| 4614 | 'section@0/u-boot:offset': 0, |
| 4615 | 'section@0/u-boot:size': len(U_BOOT_DATA), |
| 4616 | 'section@0/u-boot:image-pos': 0, |
| 4617 | 'section@0:offset': 0, |
| 4618 | 'section@0:size': 16, |
| 4619 | 'section@0:image-pos': 0, |
| 4620 | |
| 4621 | 'section@1/u-boot:offset': 0, |
| 4622 | 'section@1/u-boot:size': len(U_BOOT_DATA), |
| 4623 | 'section@1/u-boot:image-pos': 16, |
| 4624 | 'section@1:offset': 16, |
| 4625 | 'section@1:size': 16, |
| 4626 | 'section@1:image-pos': 16, |
| 4627 | 'size': 40 |
| 4628 | }, props) |
| 4629 | |
| 4630 | def testUpdateFdtInElfInvalid(self): |
| 4631 | """Test that invalid args are detected with --update-fdt-in-elf""" |
| 4632 | with self.assertRaises(ValueError) as e: |
| 4633 | self._DoTestFile('060_fdt_update.dts', update_fdt_in_elf='fred') |
| 4634 | self.assertIn("Invalid args ['fred'] to --update-fdt-in-elf", |
| 4635 | str(e.exception)) |
| 4636 | |
| 4637 | def testUpdateFdtInElfNoSyms(self): |
| 4638 | """Test that missing symbols are detected with --update-fdt-in-elf""" |
| 4639 | infile = elf_fname = self.ElfTestFile('u_boot_binman_embed') |
| 4640 | outfile = '' |
| 4641 | begin_sym = 'wrong_begin' |
| 4642 | end_sym = 'wrong_end' |
| 4643 | with self.assertRaises(ValueError) as e: |
| 4644 | self._DoTestFile( |
| 4645 | '060_fdt_update.dts', |
| 4646 | update_fdt_in_elf=','.join([infile,outfile,begin_sym,end_sym])) |
| 4647 | self.assertIn("Expected two symbols 'wrong_begin' and 'wrong_end': got 0:", |
| 4648 | str(e.exception)) |
| 4649 | |
| 4650 | def testUpdateFdtInElfTooSmall(self): |
| 4651 | """Test that an over-large dtb is detected with --update-fdt-in-elf""" |
| 4652 | infile = elf_fname = self.ElfTestFile('u_boot_binman_embed_sm') |
| 4653 | outfile = os.path.join(self._indir, 'u-boot.out') |
| 4654 | begin_sym = 'dtb_embed_begin' |
| 4655 | end_sym = 'dtb_embed_end' |
| 4656 | with self.assertRaises(ValueError) as e: |
| 4657 | self._DoTestFile( |
| 4658 | '060_fdt_update.dts', update_dtb=True, |
| 4659 | update_fdt_in_elf=','.join([infile,outfile,begin_sym,end_sym])) |
| 4660 | self.assertRegex( |
| 4661 | str(e.exception), |
| 4662 | "Not enough space in '.*u_boot_binman_embed_sm' for data length.*") |
| 4663 | |
Simon Glass | 76f496d | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 4664 | |
Simon Glass | ac59991 | 2017-11-12 21:52:22 -0700 | [diff] [blame] | 4665 | if __name__ == "__main__": |
| 4666 | unittest.main() |