blob: b8adb597e11bb9eb9ed563c9f0dd0e3ed0a3928c [file] [log] [blame]
Stephen Warren770fe172016-02-08 14:44:16 -07001# SPDX-License-Identifier: GPL-2.0
Simon Glass08631802024-09-01 16:26:14 -06002"""
3Unit-test runner
Stephen Warren770fe172016-02-08 14:44:16 -07004
Simon Glass08631802024-09-01 16:26:14 -06005Provides a test_ut() function which is used by conftest.py to run each unit
6test 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 Glassfff928c2023-08-24 13:55:41 -060010import collections
Simon Glassd3203bd2022-04-24 23:31:25 -060011import gzip
12import os
Stephen Warren770fe172016-02-08 14:44:16 -070013import os.path
14import pytest
15
Simon Glassfb916372025-02-09 09:07:15 -070016import utils
Tom Rini50ad0192023-12-09 14:52:46 -050017# pylint: disable=E0611
Simon Glass2c7b0e42022-10-29 19:47:19 -060018from tests import fs_helper
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +020019from test_android import test_abootimg
Simon Glassd3203bd2022-04-24 23:31:25 -060020
21def mkdir_cond(dirname):
22 """Create a directory if it doesn't already exist
23
24 Args:
Simon Glassd2bc33ed2023-01-06 08:52:41 -060025 dirname (str): Name of directory to create
Simon Glassd3203bd2022-04-24 23:31:25 -060026 """
27 if not os.path.exists(dirname):
28 os.mkdir(dirname)
29
Simon Glass32701112025-02-09 09:07:17 -070030def setup_image(ubman, devnum, part_type, img_size=20, second_part=False,
Simon Glassd157d3f2024-11-21 15:32:09 -070031 basename='mmc'):
32 """Create a disk image with a single partition
Simon Glassd2bc33ed2023-01-06 08:52:41 -060033
34 Args:
Simon Glass32701112025-02-09 09:07:17 -070035 ubman (ConsoleBase): Console to use
Simon Glass64c63252024-11-07 14:31:49 -070036 devnum (int): Device number to use, e.g. 1
Simon Glassd2bc33ed2023-01-06 08:52:41 -060037 part_type (int): Partition type, e.g. 0xc for FAT32
Simon Glassd157d3f2024-11-21 15:32:09 -070038 img_size (int): Image size in MiB
Simon Glassf5e2df02023-01-17 10:47:41 -070039 second_part (bool): True to contain a small second partition
Simon Glass64c63252024-11-07 14:31:49 -070040 basename (str): Base name to use in the filename, e.g. 'mmc'
Simon Glassd2bc33ed2023-01-06 08:52:41 -060041
42 Returns:
43 tuple:
44 str: Filename of MMC image
Richard Weinbergerd99fdc02024-11-21 15:32:10 -070045 str: Directory name of scratch directory
Simon Glassd2bc33ed2023-01-06 08:52:41 -060046 """
Simon Glass32701112025-02-09 09:07:17 -070047 fname = os.path.join(ubman.config.source_dir, f'{basename}{devnum}.img')
48 mnt = os.path.join(ubman.config.persistent_data_dir, 'scratch')
Simon Glassd3203bd2022-04-24 23:31:25 -060049 mkdir_cond(mnt)
50
Simon Glassd157d3f2024-11-21 15:32:09 -070051 spec = f'type={part_type:x}, size={img_size - 2}M, start=1M, bootable'
Simon Glassf5e2df02023-01-17 10:47:41 -070052 if second_part:
53 spec += '\ntype=c'
54
Simon Glass32701112025-02-09 09:07:17 -070055 utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
56 utils.run_and_log(ubman, f'sfdisk {fname}',
Simon Glassf5e2df02023-01-17 10:47:41 -070057 stdin=spec.encode('utf-8'))
Simon Glassd2bc33ed2023-01-06 08:52:41 -060058 return fname, mnt
59
Simon Glass170eb322025-03-15 14:25:49 +000060def copy_partition(ubman, fsfile, outname):
61 """Copy a partition into a disk iamge
62
63 Args:
64 ubman (ConsoleBase): U-Boot fixture
65 fsfile (str): Name of partition file
66 outname (str): Name of full-disk file to update
67 """
68 utils.run_and_log(ubman,
69 f'dd if={fsfile} of={outname} bs=1M seek=1 conv=notrunc')
70
Simon Glass32701112025-02-09 09:07:17 -070071def setup_bootmenu_image(ubman):
Simon Glassd2bc33ed2023-01-06 08:52:41 -060072 """Create a 20MB disk image with a single ext4 partition
73
74 This is modelled on Armbian 22.08 Jammy
75 """
76 mmc_dev = 4
Simon Glass32701112025-02-09 09:07:17 -070077 fname, mnt = setup_image(ubman, mmc_dev, 0x83)
Simon Glassd3203bd2022-04-24 23:31:25 -060078
Simon Glassd77e8ae2024-11-21 15:32:11 -070079 script = '''# DO NOT EDIT THIS FILE
Simon Glassd2bc33ed2023-01-06 08:52:41 -060080#
81# Please edit /boot/armbianEnv.txt to set supported parameters
82#
83
84setenv load_addr "0x9000000"
85setenv overlay_error "false"
86# default values
87setenv rootdev "/dev/mmcblk%dp1"
88setenv verbosity "1"
89setenv console "both"
90setenv bootlogo "false"
91setenv rootfstype "ext4"
92setenv docker_optimizations "on"
93setenv earlycon "off"
94
95echo "Boot script loaded from ${devtype} ${devnum}"
96
97if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then
98 load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt
99 env import -t ${load_addr} ${filesize}
100fi
101
102if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi
103
104if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi
105if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi
106if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi
107if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi
108
109# get PARTUUID of first partition on SD/eMMC the boot script was loaded from
110if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi
111
112setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}"
113
114if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi
115
116load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd
117load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image
118
119load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
120fdt addr ${fdt_addr_r}
121fdt resize 65536
122for overlay_file in ${overlays}; do
123 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then
124 echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo"
125 fdt apply ${load_addr} || setenv overlay_error "true"
126 fi
127done
128for overlay_file in ${user_overlays}; do
129 if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then
130 echo "Applying user provided DT overlay ${overlay_file}.dtbo"
131 fdt apply ${load_addr} || setenv overlay_error "true"
132 fi
133done
134if test "${overlay_error}" = "true"; then
135 echo "Error applying DT overlays, restoring original DT"
136 load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
137else
138 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then
139 echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)"
140 source ${load_addr}
141 fi
142 if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then
143 load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr
144 echo "Applying user provided fixup script (fixup.scr)"
145 source ${load_addr}
146 fi
147fi
148booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
149
150# Recompile with:
151# mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
Simon Glass08631802024-09-01 16:26:14 -0600152'''
Simon Glassd77e8ae2024-11-21 15:32:11 -0700153 bootdir = os.path.join(mnt, 'boot')
154 mkdir_cond(bootdir)
155 cmd_fname = os.path.join(bootdir, 'boot.cmd')
156 scr_fname = os.path.join(bootdir, 'boot.scr')
157 with open(cmd_fname, 'w', encoding='ascii') as outf:
158 print(script, file=outf)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600159
Simon Glass32701112025-02-09 09:07:17 -0700160 infname = os.path.join(ubman.config.source_dir,
Simon Glassd77e8ae2024-11-21 15:32:11 -0700161 'test/py/tests/bootstd/armbian.bmp.xz')
162 bmp_file = os.path.join(bootdir, 'boot.bmp')
Simon Glassfb916372025-02-09 09:07:15 -0700163 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700164 ubman,
Simon Glassd77e8ae2024-11-21 15:32:11 -0700165 ['sh', '-c', f'xz -dc {infname} >{bmp_file}'])
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600166
Simon Glass32701112025-02-09 09:07:17 -0700167 mkimage = ubman.config.build_dir + '/tools/mkimage'
Simon Glassfb916372025-02-09 09:07:15 -0700168 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700169 ubman, f'{mkimage} -C none -A arm -T script -d {cmd_fname} {scr_fname}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600170
Simon Glassd77e8ae2024-11-21 15:32:11 -0700171 kernel = 'vmlinuz-5.15.63-rockchip64'
172 target = os.path.join(bootdir, kernel)
173 with open(target, 'wb') as outf:
174 print('kernel', outf)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600175
Simon Glassd77e8ae2024-11-21 15:32:11 -0700176 symlink = os.path.join(bootdir, 'Image')
177 if os.path.exists(symlink):
178 os.remove(symlink)
Simon Glassfb916372025-02-09 09:07:15 -0700179 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700180 ubman, f'echo here {kernel} {symlink}')
Simon Glassd77e8ae2024-11-21 15:32:11 -0700181 os.symlink(kernel, symlink)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600182
Simon Glassd77e8ae2024-11-21 15:32:11 -0700183 fsfile = 'ext18M.img'
Simon Glass32701112025-02-09 09:07:17 -0700184 utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}')
185 utils.run_and_log(ubman, f'mkfs.ext4 {fsfile} -d {mnt}')
Simon Glass170eb322025-03-15 14:25:49 +0000186 copy_partition(ubman, fsfile, fname)
Simon Glass32701112025-02-09 09:07:17 -0700187 utils.run_and_log(ubman, f'rm -rf {mnt}')
188 utils.run_and_log(ubman, f'rm -f {fsfile}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600189
Simon Glass32701112025-02-09 09:07:17 -0700190def setup_bootflow_image(ubman):
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600191 """Create a 20MB disk image with a single FAT partition"""
192 mmc_dev = 1
Simon Glass32701112025-02-09 09:07:17 -0700193 fname, mnt = setup_image(ubman, mmc_dev, 0xc, second_part=True)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600194
Simon Glassd77e8ae2024-11-21 15:32:11 -0700195 vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
196 initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
197 dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
198 script = '''# extlinux.conf generated by appliance-creator
Simon Glassd3203bd2022-04-24 23:31:25 -0600199ui menu.c32
200menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
201menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
202menu hidden
203timeout 20
204totaltimeout 600
205
206label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
207 kernel /%s
208 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
209 fdtdir /%s/
210 initrd /%s''' % (vmlinux, dtbdir, initrd)
Simon Glassd77e8ae2024-11-21 15:32:11 -0700211 ext = os.path.join(mnt, 'extlinux')
212 mkdir_cond(ext)
Simon Glassd3203bd2022-04-24 23:31:25 -0600213
Simon Glassd77e8ae2024-11-21 15:32:11 -0700214 conf = os.path.join(ext, 'extlinux.conf')
215 with open(conf, 'w', encoding='ascii') as fd:
216 print(script, file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600217
Simon Glass32701112025-02-09 09:07:17 -0700218 inf = os.path.join(ubman.config.persistent_data_dir, 'inf')
Simon Glassd77e8ae2024-11-21 15:32:11 -0700219 with open(inf, 'wb') as fd:
220 fd.write(gzip.compress(b'vmlinux'))
Simon Glass32701112025-02-09 09:07:17 -0700221 mkimage = ubman.config.build_dir + '/tools/mkimage'
Simon Glassfb916372025-02-09 09:07:15 -0700222 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700223 ubman, f'{mkimage} -f auto -d {inf} {os.path.join(mnt, vmlinux)}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600224
Simon Glassd77e8ae2024-11-21 15:32:11 -0700225 with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd:
226 print('initrd', file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600227
Simon Glassd77e8ae2024-11-21 15:32:11 -0700228 mkdir_cond(os.path.join(mnt, dtbdir))
Simon Glassd3203bd2022-04-24 23:31:25 -0600229
Simon Glassd77e8ae2024-11-21 15:32:11 -0700230 dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb')
Simon Glassfb916372025-02-09 09:07:15 -0700231 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700232 ubman, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};')
Simon Glassd3203bd2022-04-24 23:31:25 -0600233
Simon Glassd77e8ae2024-11-21 15:32:11 -0700234 fsfile = 'vfat18M.img'
Simon Glass32701112025-02-09 09:07:17 -0700235 utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}')
236 utils.run_and_log(ubman, f'mkfs.vfat {fsfile}')
237 utils.run_and_log(ubman, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/'])
Simon Glass170eb322025-03-15 14:25:49 +0000238 copy_partition(ubman, fsfile, fname)
Simon Glass32701112025-02-09 09:07:17 -0700239 utils.run_and_log(ubman, f'rm -rf {mnt}')
240 utils.run_and_log(ubman, f'rm -f {fsfile}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600241
Simon Glass32701112025-02-09 09:07:17 -0700242def setup_cros_image(ubman):
Simon Glassfff928c2023-08-24 13:55:41 -0600243 """Create a 20MB disk image with ChromiumOS partitions"""
244 Partition = collections.namedtuple('part', 'start,size,name')
245 parts = {}
246 disk_data = None
247
Simon Glass32701112025-02-09 09:07:17 -0700248 def pack_kernel(ubman, arch, kern, dummy):
Simon Glassfff928c2023-08-24 13:55:41 -0600249 """Pack a kernel containing some fake data
250
251 Args:
Simon Glass32701112025-02-09 09:07:17 -0700252 ubman (ConsoleBase): Console to use
Simon Glassfff928c2023-08-24 13:55:41 -0600253 arch (str): Architecture to use ('x86' or 'arm')
254 kern (str): Filename containing kernel
255 dummy (str): Dummy filename to use for config and bootloader
256
257 Return:
258 bytes: Packed-kernel data
259 """
Simon Glass32701112025-02-09 09:07:17 -0700260 kern_part = os.path.join(ubman.config.result_dir,
Simon Glass08631802024-09-01 16:26:14 -0600261 f'kern-part-{arch}.bin')
Simon Glassfb916372025-02-09 09:07:15 -0700262 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700263 ubman,
Simon Glassfff928c2023-08-24 13:55:41 -0600264 f'futility vbutil_kernel --pack {kern_part} '
265 '--keyblock doc/chromium/files/devkeys/kernel.keyblock '
266 '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk '
267 f'--version 1 --config {dummy} --bootloader {dummy} '
268 f'--vmlinuz {kern}')
269
270 with open(kern_part, 'rb') as inf:
271 kern_part_data = inf.read()
272 return kern_part_data
273
274 def set_part_data(partnum, data):
275 """Set the contents of a disk partition
276
277 This updates disk_data by putting data in the right place
278
279 Args:
280 partnum (int): Partition number to set
281 data (bytes): Data for that partition
282 """
283 nonlocal disk_data
284
285 start = parts[partnum].start * sect_size
286 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
287
288 mmc_dev = 5
Simon Glass32701112025-02-09 09:07:17 -0700289 fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
290 utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
291 utils.run_and_log(ubman, f'cgpt create {fname}')
Simon Glassfff928c2023-08-24 13:55:41 -0600292
293 uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
294 uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309'
295 uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
296 uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3'
297 uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e'
298 uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
299
300 ptr = 40
301
302 # Number of sectors in 1MB
303 sect_size = 512
304 sect_1mb = (1 << 20) // sect_size
305
306 required_parts = [
307 {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'},
308 {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'},
309 {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'},
310 {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
311 {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
312
313 {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'},
314 {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'},
315
316 {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'},
317 {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'},
318
319 {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'},
320 {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'},
321 {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'},
322 ]
323
324 for part in required_parts:
325 size_str = part['size']
326 if 'M' in size_str:
327 size = int(size_str[:-1]) * sect_1mb
328 else:
329 size = int(size_str)
Simon Glassfb916372025-02-09 09:07:15 -0700330 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700331 ubman,
Simon Glassfff928c2023-08-24 13:55:41 -0600332 f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}")
333 ptr += size
334
Simon Glass32701112025-02-09 09:07:17 -0700335 utils.run_and_log(ubman, f'cgpt boot -p {fname}')
336 out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
Simon Glass08631802024-09-01 16:26:14 -0600337
338 # We expect something like this:
339 # 8239 2048 1 Basic data
340 # 45 2048 2 ChromeOS kernel
341 # 8238 1 3 ChromeOS rootfs
342 # 2093 2048 4 ChromeOS kernel
343 # 8237 1 5 ChromeOS rootfs
344 # 41 1 6 ChromeOS kernel
345 # 42 1 7 ChromeOS rootfs
346 # 4141 2048 8 Basic data
347 # 43 1 9 ChromeOS reserved
348 # 44 1 10 ChromeOS reserved
349 # 40 1 11 ChromeOS firmware
350 # 6189 2048 12 EFI System Partition
Simon Glassfff928c2023-08-24 13:55:41 -0600351
352 # Create a dict (indexed by partition number) containing the above info
353 for line in out.splitlines():
354 start, size, num, name = line.split(maxsplit=3)
355 parts[int(num)] = Partition(int(start), int(size), name)
356
Simon Glassc6a52e72024-11-15 16:19:23 -0700357 # Set up the kernel command-line
Simon Glass32701112025-02-09 09:07:17 -0700358 dummy = os.path.join(ubman.config.result_dir, 'dummy.txt')
Simon Glassfff928c2023-08-24 13:55:41 -0600359 with open(dummy, 'wb') as outf:
Simon Glassc6a52e72024-11-15 16:19:23 -0700360 outf.write(b'BOOT_IMAGE=/vmlinuz-5.15.0-121-generic root=/dev/nvme0n1p1 ro quiet splash vt.handoff=7')
Simon Glassfff928c2023-08-24 13:55:41 -0600361
362 # For now we just use dummy kernels. This limits testing to just detecting
363 # a signed kernel. We could add support for the x86 data structures so that
364 # testing could cover getting the cmdline, setup.bin and other pieces.
Simon Glass32701112025-02-09 09:07:17 -0700365 kern = os.path.join(ubman.config.result_dir, 'kern.bin')
Simon Glassfff928c2023-08-24 13:55:41 -0600366 with open(kern, 'wb') as outf:
367 outf.write(b'kernel\n')
368
369 with open(fname, 'rb') as inf:
370 disk_data = inf.read()
371
372 # put x86 kernel in partition 2 and arm one in partition 4
Simon Glass32701112025-02-09 09:07:17 -0700373 set_part_data(2, pack_kernel(ubman, 'x86', kern, dummy))
374 set_part_data(4, pack_kernel(ubman, 'arm', kern, dummy))
Simon Glassfff928c2023-08-24 13:55:41 -0600375
376 with open(fname, 'wb') as outf:
377 outf.write(disk_data)
378
379 return fname
380
Simon Glass32701112025-02-09 09:07:17 -0700381def setup_android_image(ubman):
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200382 """Create a 20MB disk image with Android partitions"""
383 Partition = collections.namedtuple('part', 'start,size,name')
384 parts = {}
385 disk_data = None
386
387 def set_part_data(partnum, data):
388 """Set the contents of a disk partition
389
390 This updates disk_data by putting data in the right place
391
392 Args:
393 partnum (int): Partition number to set
394 data (bytes): Data for that partition
395 """
396 nonlocal disk_data
397
398 start = parts[partnum].start * sect_size
399 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
400
401 mmc_dev = 7
Simon Glass32701112025-02-09 09:07:17 -0700402 fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
403 utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
404 utils.run_and_log(ubman, f'cgpt create {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200405
406 ptr = 40
407
408 # Number of sectors in 1MB
409 sect_size = 512
410 sect_1mb = (1 << 20) // sect_size
411
412 required_parts = [
413 {'num': 1, 'label':'misc', 'size': '1M'},
414 {'num': 2, 'label':'boot_a', 'size': '4M'},
415 {'num': 3, 'label':'boot_b', 'size': '4M'},
416 {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
417 {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
418 ]
419
420 for part in required_parts:
421 size_str = part['size']
422 if 'M' in size_str:
423 size = int(size_str[:-1]) * sect_1mb
424 else:
425 size = int(size_str)
Simon Glassfb916372025-02-09 09:07:15 -0700426 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700427 ubman,
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200428 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
429 ptr += size
430
Simon Glass32701112025-02-09 09:07:17 -0700431 utils.run_and_log(ubman, f'cgpt boot -p {fname}')
432 out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200433
434 # Create a dict (indexed by partition number) containing the above info
435 for line in out.splitlines():
436 start, size, num, name = line.split(maxsplit=3)
437 parts[int(num)] = Partition(int(start), int(size), name)
438
439 with open(fname, 'rb') as inf:
440 disk_data = inf.read()
441
Simon Glass32701112025-02-09 09:07:17 -0700442 test_abootimg.AbootimgTestDiskImage(ubman, 'bootv4.img', test_abootimg.boot_img_hex)
443 boot_img = os.path.join(ubman.config.result_dir, 'bootv4.img')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200444 with open(boot_img, 'rb') as inf:
445 set_part_data(2, inf.read())
446
Simon Glass32701112025-02-09 09:07:17 -0700447 test_abootimg.AbootimgTestDiskImage(ubman, 'vendor_boot.img', test_abootimg.vboot_img_hex)
448 vendor_boot_img = os.path.join(ubman.config.result_dir, 'vendor_boot.img')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200449 with open(vendor_boot_img, 'rb') as inf:
450 set_part_data(4, inf.read())
451
452 with open(fname, 'wb') as outf:
453 outf.write(disk_data)
454
Simon Glass08631802024-09-01 16:26:14 -0600455 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200456
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100457 mmc_dev = 8
Simon Glass32701112025-02-09 09:07:17 -0700458 fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
459 utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
460 utils.run_and_log(ubman, f'cgpt create {fname}')
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100461
462 ptr = 40
463
464 # Number of sectors in 1MB
465 sect_size = 512
466 sect_1mb = (1 << 20) // sect_size
467
468 required_parts = [
469 {'num': 1, 'label':'misc', 'size': '1M'},
470 {'num': 2, 'label':'boot_a', 'size': '4M'},
471 {'num': 3, 'label':'boot_b', 'size': '4M'},
472 ]
473
474 for part in required_parts:
475 size_str = part['size']
476 if 'M' in size_str:
477 size = int(size_str[:-1]) * sect_1mb
478 else:
479 size = int(size_str)
Simon Glassfb916372025-02-09 09:07:15 -0700480 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700481 ubman,
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100482 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
483 ptr += size
484
Simon Glass32701112025-02-09 09:07:17 -0700485 utils.run_and_log(ubman, f'cgpt boot -p {fname}')
486 out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100487
488 # Create a dict (indexed by partition number) containing the above info
489 for line in out.splitlines():
490 start, size, num, name = line.split(maxsplit=3)
491 parts[int(num)] = Partition(int(start), int(size), name)
492
493 with open(fname, 'rb') as inf:
494 disk_data = inf.read()
495
Simon Glass32701112025-02-09 09:07:17 -0700496 test_abootimg.AbootimgTestDiskImage(ubman, 'boot.img', test_abootimg.img_hex)
497 boot_img = os.path.join(ubman.config.result_dir, 'boot.img')
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100498 with open(boot_img, 'rb') as inf:
499 set_part_data(2, inf.read())
500
501 with open(fname, 'wb') as outf:
502 outf.write(disk_data)
503
504 print(f'wrote to {fname}')
505
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200506 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600507
Simon Glass32701112025-02-09 09:07:17 -0700508def setup_cedit_file(ubman):
Simon Glass08631802024-09-01 16:26:14 -0600509 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glass32701112025-02-09 09:07:17 -0700510 infname = os.path.join(ubman.config.source_dir,
Simon Glassb8c26552023-06-01 10:23:03 -0600511 'test/boot/files/expo_layout.dts')
Simon Glass32701112025-02-09 09:07:17 -0700512 inhname = os.path.join(ubman.config.source_dir,
Simon Glassb1263bc2023-08-14 16:40:28 -0600513 'test/boot/files/expo_ids.h')
Simon Glass32701112025-02-09 09:07:17 -0700514 expo_tool = os.path.join(ubman.config.source_dir, 'tools/expo.py')
Simon Glassb8c26552023-06-01 10:23:03 -0600515 outfname = 'cedit.dtb'
Simon Glassfb916372025-02-09 09:07:15 -0700516 utils.run_and_log(
Simon Glass32701112025-02-09 09:07:17 -0700517 ubman, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600518
Stephen Warren770fe172016-02-08 14:44:16 -0700519@pytest.mark.buildconfigspec('ut_dm')
Simon Glassddba5202025-02-09 09:07:14 -0700520def test_ut_dm_init(ubman):
Stephen Warren770fe172016-02-08 14:44:16 -0700521 """Initialize data for ut dm tests."""
522
Simon Glassddba5202025-02-09 09:07:14 -0700523 fn = ubman.config.source_dir + '/testflash.bin'
Stephen Warren770fe172016-02-08 14:44:16 -0700524 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400525 data = b'this is a test'
526 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700527 with open(fn, 'wb') as fh:
528 fh.write(data)
529
Simon Glassddba5202025-02-09 09:07:14 -0700530 fn = ubman.config.source_dir + '/spi.bin'
Stephen Warren770fe172016-02-08 14:44:16 -0700531 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400532 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700533 with open(fn, 'wb') as fh:
534 fh.write(data)
535
Simon Glass509f32e2022-09-21 16:21:47 +0200536 # Create a file with a single partition
Simon Glassddba5202025-02-09 09:07:14 -0700537 fn = ubman.config.source_dir + '/scsi.img'
Simon Glass509f32e2022-09-21 16:21:47 +0200538 if not os.path.exists(fn):
539 data = b'\x00' * (2 * 1024 * 1024)
540 with open(fn, 'wb') as fh:
541 fh.write(data)
Simon Glassfb916372025-02-09 09:07:15 -0700542 utils.run_and_log(
Simon Glassddba5202025-02-09 09:07:14 -0700543 ubman, f'sfdisk {fn}', stdin=b'type=83')
Simon Glass509f32e2022-09-21 16:21:47 +0200544
Simon Glassddba5202025-02-09 09:07:14 -0700545 fs_helper.mk_fs(ubman.config, 'ext2', 0x200000, '2MB', None)
546 fs_helper.mk_fs(ubman.config, 'fat32', 0x100000, '1MB', None)
Simon Glass2c7b0e42022-10-29 19:47:19 -0600547
Alexander Gendin038cb022023-10-09 01:24:36 +0000548 mmc_dev = 6
Simon Glassddba5202025-02-09 09:07:14 -0700549 fn = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
Alexander Gendin038cb022023-10-09 01:24:36 +0000550 data = b'\x00' * (12 * 1024 * 1024)
551 with open(fn, 'wb') as fh:
552 fh.write(data)
553
Simon Glass64c63252024-11-07 14:31:49 -0700554
Simon Glass32701112025-02-09 09:07:17 -0700555def setup_efi_image(ubman):
Simon Glass64c63252024-11-07 14:31:49 -0700556 """Create a 20MB disk image with an EFI app on it"""
557 devnum = 1
558 basename = 'flash'
Simon Glass32701112025-02-09 09:07:17 -0700559 fname, mnt = setup_image(ubman, devnum, 0xc, second_part=True,
Simon Glass64c63252024-11-07 14:31:49 -0700560 basename=basename)
561
Simon Glassd77e8ae2024-11-21 15:32:11 -0700562 efi_dir = os.path.join(mnt, 'EFI')
563 mkdir_cond(efi_dir)
564 bootdir = os.path.join(efi_dir, 'BOOT')
565 mkdir_cond(bootdir)
Simon Glass32701112025-02-09 09:07:17 -0700566 efi_src = os.path.join(ubman.config.build_dir,
Simon Glassd77e8ae2024-11-21 15:32:11 -0700567 'lib/efi_loader/testapp.efi')
568 efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI')
569 with open(efi_src, 'rb') as inf:
570 with open(efi_dst, 'wb') as outf:
571 outf.write(inf.read())
572 fsfile = 'vfat18M.img'
Simon Glass32701112025-02-09 09:07:17 -0700573 utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}')
574 utils.run_and_log(ubman, f'mkfs.vfat {fsfile}')
575 utils.run_and_log(ubman, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/'])
Simon Glass170eb322025-03-15 14:25:49 +0000576 copy_partition(ubman, fsfile, fname)
Simon Glass32701112025-02-09 09:07:17 -0700577 utils.run_and_log(ubman, f'rm -rf {mnt}')
578 utils.run_and_log(ubman, f'rm -f {fsfile}')
Simon Glass64c63252024-11-07 14:31:49 -0700579
Simon Glassd3203bd2022-04-24 23:31:25 -0600580@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600581@pytest.mark.buildconfigspec('sandbox')
Simon Glassddba5202025-02-09 09:07:14 -0700582def test_ut_dm_init_bootstd(ubman):
Simon Glassd3203bd2022-04-24 23:31:25 -0600583 """Initialise data for bootflow tests"""
584
Simon Glassddba5202025-02-09 09:07:14 -0700585 setup_bootflow_image(ubman)
586 setup_bootmenu_image(ubman)
587 setup_cedit_file(ubman)
588 setup_cros_image(ubman)
589 setup_android_image(ubman)
590 setup_efi_image(ubman)
Simon Glassd3203bd2022-04-24 23:31:25 -0600591
592 # Restart so that the new mmc1.img is picked up
Simon Glassddba5202025-02-09 09:07:14 -0700593 ubman.restart_uboot()
Simon Glassd3203bd2022-04-24 23:31:25 -0600594
595
Simon Glassddba5202025-02-09 09:07:14 -0700596def test_ut(ubman, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200597 """Execute a "ut" subtest.
598
599 The subtests are collected in function generate_ut_subtest() from linker
600 generated lists by applying a regular expression to the lines of file
601 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
602
603 Strict naming conventions have to be followed to match the regular
604 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
605 test suite foo that can be executed via command 'ut foo bar' and is
606 implemented in C function foo_test_bar().
607
608 Args:
Simon Glassddba5202025-02-09 09:07:14 -0700609 ubman (ConsoleBase): U-Boot console
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200610 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
611 execute command 'ut foo bar'
612 """
Stephen Warren770fe172016-02-08 14:44:16 -0700613
Francis Laniel91ec8702023-12-22 22:02:24 +0100614 if ut_subtest == 'hush hush_test_simple_dollar':
615 # ut hush hush_test_simple_dollar prints "Unknown command" on purpose.
Simon Glassddba5202025-02-09 09:07:14 -0700616 with ubman.disable_check('unknown_command'):
617 output = ubman.run_command('ut ' + ut_subtest)
Simon Glass08631802024-09-01 16:26:14 -0600618 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100619 else:
Simon Glassddba5202025-02-09 09:07:14 -0700620 output = ubman.run_command('ut ' + ut_subtest)
Simon Glass1f271dc2025-01-20 14:26:01 -0700621 assert output.endswith('failures: 0')