Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 2 | """ |
| 3 | Unit-test runner |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 4 | |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 5 | Provides a test_ut() function which is used by conftest.py to run each unit |
| 6 | test one at a time, as well setting up some files needed by the tests. |
| 7 | |
| 8 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
| 9 | """ |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 10 | import collections |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 11 | import getpass |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 12 | import gzip |
| 13 | import os |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 14 | import os.path |
| 15 | import pytest |
| 16 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 17 | import u_boot_utils |
Tom Rini | 50ad019 | 2023-12-09 14:52:46 -0500 | [diff] [blame] | 18 | # pylint: disable=E0611 |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 19 | from tests import fs_helper |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 20 | from test_android import test_abootimg |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 21 | |
| 22 | def mkdir_cond(dirname): |
| 23 | """Create a directory if it doesn't already exist |
| 24 | |
| 25 | Args: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 26 | dirname (str): Name of directory to create |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 27 | """ |
| 28 | if not os.path.exists(dirname): |
| 29 | os.mkdir(dirname) |
| 30 | |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 31 | def setup_image(cons, mmc_dev, part_type, second_part=False): |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 32 | """Create a 20MB disk image with a single partition |
| 33 | |
| 34 | Args: |
| 35 | cons (ConsoleBase): Console to use |
| 36 | mmc_dev (int): MMC device number to use, e.g. 1 |
| 37 | part_type (int): Partition type, e.g. 0xc for FAT32 |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 38 | second_part (bool): True to contain a small second partition |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 39 | |
| 40 | Returns: |
| 41 | tuple: |
| 42 | str: Filename of MMC image |
| 43 | str: Directory name of 'mnt' directory |
| 44 | """ |
| 45 | fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 46 | mnt = os.path.join(cons.config.persistent_data_dir, 'mnt') |
| 47 | mkdir_cond(mnt) |
| 48 | |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 49 | spec = f'type={part_type:x}, size=18M, bootable' |
| 50 | if second_part: |
| 51 | spec += '\ntype=c' |
| 52 | |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 53 | u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') |
| 54 | u_boot_utils.run_and_log(cons, f'sudo sfdisk {fname}', |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 55 | stdin=spec.encode('utf-8')) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 56 | return fname, mnt |
| 57 | |
| 58 | def mount_image(cons, fname, mnt, fstype): |
| 59 | """Create a filesystem and mount it on partition 1 |
| 60 | |
| 61 | Args: |
| 62 | cons (ConsoleBase): Console to use |
| 63 | fname (str): Filename of MMC image |
| 64 | mnt (str): Directory name of 'mnt' directory |
| 65 | fstype (str): Filesystem type ('vfat' or 'ext4') |
| 66 | |
| 67 | Returns: |
| 68 | str: Name of loop device used |
| 69 | """ |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 70 | out = u_boot_utils.run_and_log(cons, f'sudo losetup --show -f -P {fname}') |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 71 | loop = out.strip() |
| 72 | part = f'{loop}p1' |
| 73 | u_boot_utils.run_and_log(cons, f'sudo mkfs.{fstype} {part}') |
| 74 | opts = '' |
| 75 | if fstype == 'vfat': |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 76 | opts += f' -o uid={os.getuid()},gid={os.getgid()}' |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 77 | u_boot_utils.run_and_log(cons, f'sudo mount -o loop {part} {mnt}{opts}') |
| 78 | u_boot_utils.run_and_log(cons, f'sudo chown {getpass.getuser()} {mnt}') |
| 79 | return loop |
| 80 | |
| 81 | def copy_prepared_image(cons, mmc_dev, fname): |
| 82 | """Use a prepared image since we cannot create one |
| 83 | |
| 84 | Args: |
| 85 | cons (ConsoleBase): Console touse |
| 86 | mmc_dev (int): MMC device number |
| 87 | fname (str): Filename of MMC image |
| 88 | """ |
| 89 | infname = os.path.join(cons.config.source_dir, |
| 90 | f'test/py/tests/bootstd/mmc{mmc_dev}.img.xz') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 91 | u_boot_utils.run_and_log(cons, ['sh', '-c', f'xz -dc {infname} >{fname}']) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 92 | |
| 93 | def setup_bootmenu_image(cons): |
| 94 | """Create a 20MB disk image with a single ext4 partition |
| 95 | |
| 96 | This is modelled on Armbian 22.08 Jammy |
| 97 | """ |
| 98 | mmc_dev = 4 |
| 99 | fname, mnt = setup_image(cons, mmc_dev, 0x83) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 100 | |
| 101 | loop = None |
| 102 | mounted = False |
| 103 | complete = False |
| 104 | try: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 105 | loop = mount_image(cons, fname, mnt, 'ext4') |
| 106 | mounted = True |
| 107 | |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 108 | script = '''# DO NOT EDIT THIS FILE |
| 109 | # |
| 110 | # Please edit /boot/armbianEnv.txt to set supported parameters |
| 111 | # |
| 112 | |
| 113 | setenv load_addr "0x9000000" |
| 114 | setenv overlay_error "false" |
| 115 | # default values |
| 116 | setenv rootdev "/dev/mmcblk%dp1" |
| 117 | setenv verbosity "1" |
| 118 | setenv console "both" |
| 119 | setenv bootlogo "false" |
| 120 | setenv rootfstype "ext4" |
| 121 | setenv docker_optimizations "on" |
| 122 | setenv earlycon "off" |
| 123 | |
| 124 | echo "Boot script loaded from ${devtype} ${devnum}" |
| 125 | |
| 126 | if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then |
| 127 | load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt |
| 128 | env import -t ${load_addr} ${filesize} |
| 129 | fi |
| 130 | |
| 131 | if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi |
| 132 | |
| 133 | if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi |
| 134 | if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi |
| 135 | if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi |
| 136 | if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi |
| 137 | |
| 138 | # get PARTUUID of first partition on SD/eMMC the boot script was loaded from |
| 139 | if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi |
| 140 | |
| 141 | setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}" |
| 142 | |
| 143 | if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi |
| 144 | |
| 145 | load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd |
| 146 | load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image |
| 147 | |
| 148 | load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile} |
| 149 | fdt addr ${fdt_addr_r} |
| 150 | fdt resize 65536 |
| 151 | for overlay_file in ${overlays}; do |
| 152 | if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then |
| 153 | echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo" |
| 154 | fdt apply ${load_addr} || setenv overlay_error "true" |
| 155 | fi |
| 156 | done |
| 157 | for overlay_file in ${user_overlays}; do |
| 158 | if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then |
| 159 | echo "Applying user provided DT overlay ${overlay_file}.dtbo" |
| 160 | fdt apply ${load_addr} || setenv overlay_error "true" |
| 161 | fi |
| 162 | done |
| 163 | if test "${overlay_error}" = "true"; then |
| 164 | echo "Error applying DT overlays, restoring original DT" |
| 165 | load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile} |
| 166 | else |
| 167 | if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then |
| 168 | echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)" |
| 169 | source ${load_addr} |
| 170 | fi |
| 171 | if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then |
| 172 | load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr |
| 173 | echo "Applying user provided fixup script (fixup.scr)" |
| 174 | source ${load_addr} |
| 175 | fi |
| 176 | fi |
| 177 | booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r} |
| 178 | |
| 179 | # Recompile with: |
| 180 | # mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 181 | ''' |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 182 | bootdir = os.path.join(mnt, 'boot') |
| 183 | mkdir_cond(bootdir) |
| 184 | cmd_fname = os.path.join(bootdir, 'boot.cmd') |
| 185 | scr_fname = os.path.join(bootdir, 'boot.scr') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 186 | with open(cmd_fname, 'w', encoding='ascii') as outf: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 187 | print(script, file=outf) |
| 188 | |
| 189 | infname = os.path.join(cons.config.source_dir, |
| 190 | 'test/py/tests/bootstd/armbian.bmp.xz') |
| 191 | bmp_file = os.path.join(bootdir, 'boot.bmp') |
| 192 | u_boot_utils.run_and_log( |
| 193 | cons, |
| 194 | ['sh', '-c', f'xz -dc {infname} >{bmp_file}']) |
| 195 | |
| 196 | u_boot_utils.run_and_log( |
| 197 | cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}') |
| 198 | |
| 199 | kernel = 'vmlinuz-5.15.63-rockchip64' |
| 200 | target = os.path.join(bootdir, kernel) |
| 201 | with open(target, 'wb') as outf: |
| 202 | print('kernel', outf) |
| 203 | |
| 204 | symlink = os.path.join(bootdir, 'Image') |
| 205 | if os.path.exists(symlink): |
| 206 | os.remove(symlink) |
| 207 | u_boot_utils.run_and_log( |
| 208 | cons, f'echo here {kernel} {symlink}') |
| 209 | os.symlink(kernel, symlink) |
| 210 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 211 | u_boot_utils.run_and_log( |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 212 | cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}') |
| 213 | complete = True |
| 214 | |
| 215 | except ValueError as exc: |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 216 | print(f'Falled to create image, failing back to prepared copy: {exc}') |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 217 | finally: |
| 218 | if mounted: |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 219 | u_boot_utils.run_and_log(cons, f'sudo umount --lazy {mnt}') |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 220 | if loop: |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 221 | u_boot_utils.run_and_log(cons, f'sudo losetup -d {loop}') |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 222 | |
| 223 | if not complete: |
| 224 | copy_prepared_image(cons, mmc_dev, fname) |
| 225 | |
| 226 | def setup_bootflow_image(cons): |
| 227 | """Create a 20MB disk image with a single FAT partition""" |
| 228 | mmc_dev = 1 |
Simon Glass | f5e2df0 | 2023-01-17 10:47:41 -0700 | [diff] [blame] | 229 | fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 230 | |
| 231 | loop = None |
| 232 | mounted = False |
| 233 | complete = False |
| 234 | try: |
| 235 | loop = mount_image(cons, fname, mnt, 'vfat') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 236 | mounted = True |
| 237 | |
| 238 | vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl' |
| 239 | initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img' |
| 240 | dtbdir = 'dtb-5.3.7-301.fc31.armv7hl' |
| 241 | script = '''# extlinux.conf generated by appliance-creator |
| 242 | ui menu.c32 |
| 243 | menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options. |
| 244 | menu title Fedora-Workstation-armhfp-31-1.9 Boot Options. |
| 245 | menu hidden |
| 246 | timeout 20 |
| 247 | totaltimeout 600 |
| 248 | |
| 249 | label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) |
| 250 | kernel /%s |
| 251 | append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB |
| 252 | fdtdir /%s/ |
| 253 | initrd /%s''' % (vmlinux, dtbdir, initrd) |
| 254 | ext = os.path.join(mnt, 'extlinux') |
| 255 | mkdir_cond(ext) |
| 256 | |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 257 | conf = os.path.join(ext, 'extlinux.conf') |
| 258 | with open(conf, 'w', encoding='ascii') as fd: |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 259 | print(script, file=fd) |
| 260 | |
| 261 | inf = os.path.join(cons.config.persistent_data_dir, 'inf') |
| 262 | with open(inf, 'wb') as fd: |
| 263 | fd.write(gzip.compress(b'vmlinux')) |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 264 | u_boot_utils.run_and_log( |
| 265 | cons, f'mkimage -f auto -d {inf} {os.path.join(mnt, vmlinux)}') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 266 | |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 267 | with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd: |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 268 | print('initrd', file=fd) |
| 269 | |
| 270 | mkdir_cond(os.path.join(mnt, dtbdir)) |
| 271 | |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 272 | dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 273 | u_boot_utils.run_and_log( |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 274 | cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 275 | complete = True |
| 276 | except ValueError as exc: |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 277 | print(f'Falled to create image, failing back to prepared copy: {exc}') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 278 | finally: |
| 279 | if mounted: |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 280 | u_boot_utils.run_and_log(cons, f'sudo umount --lazy {mnt}') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 281 | if loop: |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 282 | u_boot_utils.run_and_log(cons, f'sudo losetup -d {loop}') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 283 | |
| 284 | if not complete: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 285 | copy_prepared_image(cons, mmc_dev, fname) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 286 | |
| 287 | |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 288 | def setup_cros_image(cons): |
| 289 | """Create a 20MB disk image with ChromiumOS partitions""" |
| 290 | Partition = collections.namedtuple('part', 'start,size,name') |
| 291 | parts = {} |
| 292 | disk_data = None |
| 293 | |
| 294 | def pack_kernel(cons, arch, kern, dummy): |
| 295 | """Pack a kernel containing some fake data |
| 296 | |
| 297 | Args: |
| 298 | cons (ConsoleBase): Console to use |
| 299 | arch (str): Architecture to use ('x86' or 'arm') |
| 300 | kern (str): Filename containing kernel |
| 301 | dummy (str): Dummy filename to use for config and bootloader |
| 302 | |
| 303 | Return: |
| 304 | bytes: Packed-kernel data |
| 305 | """ |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 306 | kern_part = os.path.join(cons.config.result_dir, |
| 307 | f'kern-part-{arch}.bin') |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 308 | u_boot_utils.run_and_log( |
| 309 | cons, |
| 310 | f'futility vbutil_kernel --pack {kern_part} ' |
| 311 | '--keyblock doc/chromium/files/devkeys/kernel.keyblock ' |
| 312 | '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk ' |
| 313 | f'--version 1 --config {dummy} --bootloader {dummy} ' |
| 314 | f'--vmlinuz {kern}') |
| 315 | |
| 316 | with open(kern_part, 'rb') as inf: |
| 317 | kern_part_data = inf.read() |
| 318 | return kern_part_data |
| 319 | |
| 320 | def set_part_data(partnum, data): |
| 321 | """Set the contents of a disk partition |
| 322 | |
| 323 | This updates disk_data by putting data in the right place |
| 324 | |
| 325 | Args: |
| 326 | partnum (int): Partition number to set |
| 327 | data (bytes): Data for that partition |
| 328 | """ |
| 329 | nonlocal disk_data |
| 330 | |
| 331 | start = parts[partnum].start * sect_size |
| 332 | disk_data = disk_data[:start] + data + disk_data[start + len(data):] |
| 333 | |
| 334 | mmc_dev = 5 |
| 335 | fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 336 | u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 337 | #mnt = os.path.join(cons.config.persistent_data_dir, 'mnt') |
| 338 | #mkdir_cond(mnt) |
| 339 | u_boot_utils.run_and_log(cons, f'cgpt create {fname}') |
| 340 | |
| 341 | uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7' |
| 342 | uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309' |
| 343 | uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec' |
| 344 | uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3' |
| 345 | uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e' |
| 346 | uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b' |
| 347 | |
| 348 | ptr = 40 |
| 349 | |
| 350 | # Number of sectors in 1MB |
| 351 | sect_size = 512 |
| 352 | sect_1mb = (1 << 20) // sect_size |
| 353 | |
| 354 | required_parts = [ |
| 355 | {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'}, |
| 356 | {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'}, |
| 357 | {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'}, |
| 358 | {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'}, |
| 359 | {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'}, |
| 360 | |
| 361 | {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'}, |
| 362 | {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'}, |
| 363 | |
| 364 | {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'}, |
| 365 | {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'}, |
| 366 | |
| 367 | {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'}, |
| 368 | {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'}, |
| 369 | {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'}, |
| 370 | ] |
| 371 | |
| 372 | for part in required_parts: |
| 373 | size_str = part['size'] |
| 374 | if 'M' in size_str: |
| 375 | size = int(size_str[:-1]) * sect_1mb |
| 376 | else: |
| 377 | size = int(size_str) |
| 378 | u_boot_utils.run_and_log( |
| 379 | cons, |
| 380 | f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}") |
| 381 | ptr += size |
| 382 | |
| 383 | u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}') |
| 384 | out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 385 | |
| 386 | # We expect something like this: |
| 387 | # 8239 2048 1 Basic data |
| 388 | # 45 2048 2 ChromeOS kernel |
| 389 | # 8238 1 3 ChromeOS rootfs |
| 390 | # 2093 2048 4 ChromeOS kernel |
| 391 | # 8237 1 5 ChromeOS rootfs |
| 392 | # 41 1 6 ChromeOS kernel |
| 393 | # 42 1 7 ChromeOS rootfs |
| 394 | # 4141 2048 8 Basic data |
| 395 | # 43 1 9 ChromeOS reserved |
| 396 | # 44 1 10 ChromeOS reserved |
| 397 | # 40 1 11 ChromeOS firmware |
| 398 | # 6189 2048 12 EFI System Partition |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 399 | |
| 400 | # Create a dict (indexed by partition number) containing the above info |
| 401 | for line in out.splitlines(): |
| 402 | start, size, num, name = line.split(maxsplit=3) |
| 403 | parts[int(num)] = Partition(int(start), int(size), name) |
| 404 | |
| 405 | dummy = os.path.join(cons.config.result_dir, 'dummy.txt') |
| 406 | with open(dummy, 'wb') as outf: |
| 407 | outf.write(b'dummy\n') |
| 408 | |
| 409 | # For now we just use dummy kernels. This limits testing to just detecting |
| 410 | # a signed kernel. We could add support for the x86 data structures so that |
| 411 | # testing could cover getting the cmdline, setup.bin and other pieces. |
| 412 | kern = os.path.join(cons.config.result_dir, 'kern.bin') |
| 413 | with open(kern, 'wb') as outf: |
| 414 | outf.write(b'kernel\n') |
| 415 | |
| 416 | with open(fname, 'rb') as inf: |
| 417 | disk_data = inf.read() |
| 418 | |
| 419 | # put x86 kernel in partition 2 and arm one in partition 4 |
| 420 | set_part_data(2, pack_kernel(cons, 'x86', kern, dummy)) |
| 421 | set_part_data(4, pack_kernel(cons, 'arm', kern, dummy)) |
| 422 | |
| 423 | with open(fname, 'wb') as outf: |
| 424 | outf.write(disk_data) |
| 425 | |
| 426 | return fname |
| 427 | |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 428 | def setup_android_image(cons): |
| 429 | """Create a 20MB disk image with Android partitions""" |
| 430 | Partition = collections.namedtuple('part', 'start,size,name') |
| 431 | parts = {} |
| 432 | disk_data = None |
| 433 | |
| 434 | def set_part_data(partnum, data): |
| 435 | """Set the contents of a disk partition |
| 436 | |
| 437 | This updates disk_data by putting data in the right place |
| 438 | |
| 439 | Args: |
| 440 | partnum (int): Partition number to set |
| 441 | data (bytes): Data for that partition |
| 442 | """ |
| 443 | nonlocal disk_data |
| 444 | |
| 445 | start = parts[partnum].start * sect_size |
| 446 | disk_data = disk_data[:start] + data + disk_data[start + len(data):] |
| 447 | |
| 448 | mmc_dev = 7 |
| 449 | fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 450 | u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 451 | u_boot_utils.run_and_log(cons, f'cgpt create {fname}') |
| 452 | |
| 453 | ptr = 40 |
| 454 | |
| 455 | # Number of sectors in 1MB |
| 456 | sect_size = 512 |
| 457 | sect_1mb = (1 << 20) // sect_size |
| 458 | |
| 459 | required_parts = [ |
| 460 | {'num': 1, 'label':'misc', 'size': '1M'}, |
| 461 | {'num': 2, 'label':'boot_a', 'size': '4M'}, |
| 462 | {'num': 3, 'label':'boot_b', 'size': '4M'}, |
| 463 | {'num': 4, 'label':'vendor_boot_a', 'size': '4M'}, |
| 464 | {'num': 5, 'label':'vendor_boot_b', 'size': '4M'}, |
| 465 | ] |
| 466 | |
| 467 | for part in required_parts: |
| 468 | size_str = part['size'] |
| 469 | if 'M' in size_str: |
| 470 | size = int(size_str[:-1]) * sect_1mb |
| 471 | else: |
| 472 | size = int(size_str) |
| 473 | u_boot_utils.run_and_log( |
| 474 | cons, |
| 475 | f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}") |
| 476 | ptr += size |
| 477 | |
| 478 | u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}') |
| 479 | out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}') |
| 480 | |
| 481 | # Create a dict (indexed by partition number) containing the above info |
| 482 | for line in out.splitlines(): |
| 483 | start, size, num, name = line.split(maxsplit=3) |
| 484 | parts[int(num)] = Partition(int(start), int(size), name) |
| 485 | |
| 486 | with open(fname, 'rb') as inf: |
| 487 | disk_data = inf.read() |
| 488 | |
| 489 | test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex) |
| 490 | boot_img = os.path.join(cons.config.result_dir, 'bootv4.img') |
| 491 | with open(boot_img, 'rb') as inf: |
| 492 | set_part_data(2, inf.read()) |
| 493 | |
| 494 | test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex) |
| 495 | vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img') |
| 496 | with open(vendor_boot_img, 'rb') as inf: |
| 497 | set_part_data(4, inf.read()) |
| 498 | |
| 499 | with open(fname, 'wb') as outf: |
| 500 | outf.write(disk_data) |
| 501 | |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 502 | print(f'wrote to {fname}') |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 503 | |
| 504 | return fname |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 505 | |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 506 | def setup_cedit_file(cons): |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 507 | """Set up a .dtb file for use with testing expo and configuration editor""" |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 508 | infname = os.path.join(cons.config.source_dir, |
| 509 | 'test/boot/files/expo_layout.dts') |
Simon Glass | b1263bc | 2023-08-14 16:40:28 -0600 | [diff] [blame] | 510 | inhname = os.path.join(cons.config.source_dir, |
| 511 | 'test/boot/files/expo_ids.h') |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 512 | expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py') |
| 513 | outfname = 'cedit.dtb' |
| 514 | u_boot_utils.run_and_log( |
Simon Glass | b1263bc | 2023-08-14 16:40:28 -0600 | [diff] [blame] | 515 | cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}') |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 516 | |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 517 | @pytest.mark.buildconfigspec('ut_dm') |
| 518 | def test_ut_dm_init(u_boot_console): |
| 519 | """Initialize data for ut dm tests.""" |
| 520 | |
| 521 | fn = u_boot_console.config.source_dir + '/testflash.bin' |
| 522 | if not os.path.exists(fn): |
Tom Rini | 439ed3e | 2019-10-24 11:59:22 -0400 | [diff] [blame] | 523 | data = b'this is a test' |
| 524 | data += b'\x00' * ((4 * 1024 * 1024) - len(data)) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 525 | with open(fn, 'wb') as fh: |
| 526 | fh.write(data) |
| 527 | |
| 528 | fn = u_boot_console.config.source_dir + '/spi.bin' |
| 529 | if not os.path.exists(fn): |
Tom Rini | 439ed3e | 2019-10-24 11:59:22 -0400 | [diff] [blame] | 530 | data = b'\x00' * (2 * 1024 * 1024) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 531 | with open(fn, 'wb') as fh: |
| 532 | fh.write(data) |
| 533 | |
Simon Glass | 509f32e | 2022-09-21 16:21:47 +0200 | [diff] [blame] | 534 | # Create a file with a single partition |
| 535 | fn = u_boot_console.config.source_dir + '/scsi.img' |
| 536 | if not os.path.exists(fn): |
| 537 | data = b'\x00' * (2 * 1024 * 1024) |
| 538 | with open(fn, 'wb') as fh: |
| 539 | fh.write(data) |
| 540 | u_boot_utils.run_and_log( |
| 541 | u_boot_console, f'sfdisk {fn}', stdin=b'type=83') |
| 542 | |
Simon Glass | 0870921 | 2023-08-24 13:55:38 -0600 | [diff] [blame] | 543 | fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB') |
| 544 | fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB') |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 545 | |
Alexander Gendin | 038cb02 | 2023-10-09 01:24:36 +0000 | [diff] [blame] | 546 | mmc_dev = 6 |
| 547 | fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img') |
| 548 | data = b'\x00' * (12 * 1024 * 1024) |
| 549 | with open(fn, 'wb') as fh: |
| 550 | fh.write(data) |
| 551 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 552 | @pytest.mark.buildconfigspec('cmd_bootflow') |
Simon Glass | 84712cd | 2024-06-23 14:30:27 -0600 | [diff] [blame] | 553 | @pytest.mark.buildconfigspec('sandbox') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 554 | def test_ut_dm_init_bootstd(u_boot_console): |
| 555 | """Initialise data for bootflow tests""" |
| 556 | |
| 557 | setup_bootflow_image(u_boot_console) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame] | 558 | setup_bootmenu_image(u_boot_console) |
Simon Glass | b8c2655 | 2023-06-01 10:23:03 -0600 | [diff] [blame] | 559 | setup_cedit_file(u_boot_console) |
Simon Glass | fff928c | 2023-08-24 13:55:41 -0600 | [diff] [blame] | 560 | setup_cros_image(u_boot_console) |
Mattijs Korpershoek | d77f815 | 2024-07-10 10:40:06 +0200 | [diff] [blame] | 561 | setup_android_image(u_boot_console) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 562 | |
| 563 | # Restart so that the new mmc1.img is picked up |
| 564 | u_boot_console.restart_uboot() |
| 565 | |
| 566 | |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 567 | def test_ut(u_boot_console, ut_subtest): |
Heinrich Schuchardt | df6c36c | 2020-05-06 18:26:07 +0200 | [diff] [blame] | 568 | """Execute a "ut" subtest. |
| 569 | |
| 570 | The subtests are collected in function generate_ut_subtest() from linker |
| 571 | generated lists by applying a regular expression to the lines of file |
| 572 | u-boot.sym. The list entries are created using the C macro UNIT_TEST(). |
| 573 | |
| 574 | Strict naming conventions have to be followed to match the regular |
| 575 | expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in |
| 576 | test suite foo that can be executed via command 'ut foo bar' and is |
| 577 | implemented in C function foo_test_bar(). |
| 578 | |
| 579 | Args: |
| 580 | u_boot_console (ConsoleBase): U-Boot console |
| 581 | ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to |
| 582 | execute command 'ut foo bar' |
| 583 | """ |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 584 | |
Francis Laniel | 91ec870 | 2023-12-22 22:02:24 +0100 | [diff] [blame] | 585 | if ut_subtest == 'hush hush_test_simple_dollar': |
| 586 | # ut hush hush_test_simple_dollar prints "Unknown command" on purpose. |
| 587 | with u_boot_console.disable_check('unknown_command'): |
| 588 | output = u_boot_console.run_command('ut ' + ut_subtest) |
Simon Glass | 0863180 | 2024-09-01 16:26:14 -0600 | [diff] [blame] | 589 | assert 'Unknown command \'quux\' - try \'help\'' in output |
Francis Laniel | 91ec870 | 2023-12-22 22:02:24 +0100 | [diff] [blame] | 590 | else: |
| 591 | output = u_boot_console.run_command('ut ' + ut_subtest) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 592 | assert output.endswith('Failures: 0') |