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