blob: 6c3b360d5fddbd1c3864c7f62102dc3f6e16f3a3 [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 Glassd3203bd2022-04-24 23:31:25 -060016import u_boot_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 Glassd157d3f2024-11-21 15:32:09 -070030def 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 Glassd2bc33ed2023-01-06 08:52:41 -060033
34 Args:
35 cons (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 Glass64c63252024-11-07 14:31:49 -070047 fname = os.path.join(cons.config.source_dir, f'{basename}{devnum}.img')
Richard Weinbergerd99fdc02024-11-21 15:32:10 -070048 mnt = os.path.join(cons.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
Richard Weinbergerd99fdc02024-11-21 15:32:10 -070055 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
56 u_boot_utils.run_and_log(cons, 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 Glass64c63252024-11-07 14:31:49 -070060def copy_prepared_image(cons, devnum, fname, basename='mmc'):
Simon Glassd2bc33ed2023-01-06 08:52:41 -060061 """Use a prepared image since we cannot create one
62
63 Args:
64 cons (ConsoleBase): Console touse
Simon Glass64c63252024-11-07 14:31:49 -070065 devnum (int): device number
Simon Glassd2bc33ed2023-01-06 08:52:41 -060066 fname (str): Filename of MMC image
Simon Glass64c63252024-11-07 14:31:49 -070067 basename (str): Base name to use in the filename, e.g. 'mmc'
Simon Glassd2bc33ed2023-01-06 08:52:41 -060068 """
69 infname = os.path.join(cons.config.source_dir,
Simon Glass64c63252024-11-07 14:31:49 -070070 f'test/py/tests/bootstd/{basename}{devnum}.img.xz')
Simon Glass08631802024-09-01 16:26:14 -060071 u_boot_utils.run_and_log(cons, ['sh', '-c', f'xz -dc {infname} >{fname}'])
Simon Glassd2bc33ed2023-01-06 08:52:41 -060072
73def setup_bootmenu_image(cons):
74 """Create a 20MB disk image with a single ext4 partition
75
76 This is modelled on Armbian 22.08 Jammy
77 """
78 mmc_dev = 4
79 fname, mnt = setup_image(cons, mmc_dev, 0x83)
Simon Glassd3203bd2022-04-24 23:31:25 -060080
Simon Glassd3203bd2022-04-24 23:31:25 -060081 complete = False
Simon Glassd77e8ae2024-11-21 15:32:11 -070082 script = '''# DO NOT EDIT THIS FILE
Simon Glassd2bc33ed2023-01-06 08:52:41 -060083#
84# Please edit /boot/armbianEnv.txt to set supported parameters
85#
86
87setenv load_addr "0x9000000"
88setenv overlay_error "false"
89# default values
90setenv rootdev "/dev/mmcblk%dp1"
91setenv verbosity "1"
92setenv console "both"
93setenv bootlogo "false"
94setenv rootfstype "ext4"
95setenv docker_optimizations "on"
96setenv earlycon "off"
97
98echo "Boot script loaded from ${devtype} ${devnum}"
99
100if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then
101 load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt
102 env import -t ${load_addr} ${filesize}
103fi
104
105if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi
106
107if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi
108if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi
109if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi
110if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi
111
112# get PARTUUID of first partition on SD/eMMC the boot script was loaded from
113if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi
114
115setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}"
116
117if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi
118
119load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd
120load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image
121
122load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
123fdt addr ${fdt_addr_r}
124fdt resize 65536
125for overlay_file in ${overlays}; do
126 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then
127 echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo"
128 fdt apply ${load_addr} || setenv overlay_error "true"
129 fi
130done
131for overlay_file in ${user_overlays}; do
132 if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then
133 echo "Applying user provided DT overlay ${overlay_file}.dtbo"
134 fdt apply ${load_addr} || setenv overlay_error "true"
135 fi
136done
137if test "${overlay_error}" = "true"; then
138 echo "Error applying DT overlays, restoring original DT"
139 load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
140else
141 if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then
142 echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)"
143 source ${load_addr}
144 fi
145 if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then
146 load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr
147 echo "Applying user provided fixup script (fixup.scr)"
148 source ${load_addr}
149 fi
150fi
151booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
152
153# Recompile with:
154# mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr
Simon Glass08631802024-09-01 16:26:14 -0600155'''
Simon Glassd77e8ae2024-11-21 15:32:11 -0700156 bootdir = os.path.join(mnt, 'boot')
157 mkdir_cond(bootdir)
158 cmd_fname = os.path.join(bootdir, 'boot.cmd')
159 scr_fname = os.path.join(bootdir, 'boot.scr')
160 with open(cmd_fname, 'w', encoding='ascii') as outf:
161 print(script, file=outf)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600162
Simon Glassd77e8ae2024-11-21 15:32:11 -0700163 infname = os.path.join(cons.config.source_dir,
164 'test/py/tests/bootstd/armbian.bmp.xz')
165 bmp_file = os.path.join(bootdir, 'boot.bmp')
166 u_boot_utils.run_and_log(
167 cons,
168 ['sh', '-c', f'xz -dc {infname} >{bmp_file}'])
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600169
Simon Glassefcc5662024-11-21 15:32:12 -0700170 mkimage = cons.config.build_dir + '/tools/mkimage'
Simon Glassd77e8ae2024-11-21 15:32:11 -0700171 u_boot_utils.run_and_log(
Simon Glassefcc5662024-11-21 15:32:12 -0700172 cons, f'{mkimage} -C none -A arm -T script -d {cmd_fname} {scr_fname}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600173
Simon Glassd77e8ae2024-11-21 15:32:11 -0700174 kernel = 'vmlinuz-5.15.63-rockchip64'
175 target = os.path.join(bootdir, kernel)
176 with open(target, 'wb') as outf:
177 print('kernel', outf)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600178
Simon Glassd77e8ae2024-11-21 15:32:11 -0700179 symlink = os.path.join(bootdir, 'Image')
180 if os.path.exists(symlink):
181 os.remove(symlink)
182 u_boot_utils.run_and_log(
183 cons, f'echo here {kernel} {symlink}')
184 os.symlink(kernel, symlink)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600185
Simon Glassd77e8ae2024-11-21 15:32:11 -0700186 fsfile = 'ext18M.img'
187 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
188 u_boot_utils.run_and_log(cons, f'mkfs.ext4 {fsfile} -d {mnt}')
189 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
190 complete = True
191 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
192 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600193
194 if not complete:
195 copy_prepared_image(cons, mmc_dev, fname)
196
197def setup_bootflow_image(cons):
198 """Create a 20MB disk image with a single FAT partition"""
199 mmc_dev = 1
Simon Glassf5e2df02023-01-17 10:47:41 -0700200 fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600201
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600202 complete = False
Simon Glassd77e8ae2024-11-21 15:32:11 -0700203 vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
204 initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
205 dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
206 script = '''# extlinux.conf generated by appliance-creator
Simon Glassd3203bd2022-04-24 23:31:25 -0600207ui menu.c32
208menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
209menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
210menu hidden
211timeout 20
212totaltimeout 600
213
214label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
215 kernel /%s
216 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
217 fdtdir /%s/
218 initrd /%s''' % (vmlinux, dtbdir, initrd)
Simon Glassd77e8ae2024-11-21 15:32:11 -0700219 ext = os.path.join(mnt, 'extlinux')
220 mkdir_cond(ext)
Simon Glassd3203bd2022-04-24 23:31:25 -0600221
Simon Glassd77e8ae2024-11-21 15:32:11 -0700222 conf = os.path.join(ext, 'extlinux.conf')
223 with open(conf, 'w', encoding='ascii') as fd:
224 print(script, file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600225
Simon Glassd77e8ae2024-11-21 15:32:11 -0700226 inf = os.path.join(cons.config.persistent_data_dir, 'inf')
227 with open(inf, 'wb') as fd:
228 fd.write(gzip.compress(b'vmlinux'))
Simon Glassefcc5662024-11-21 15:32:12 -0700229 mkimage = cons.config.build_dir + '/tools/mkimage'
Simon Glassd77e8ae2024-11-21 15:32:11 -0700230 u_boot_utils.run_and_log(
Simon Glassefcc5662024-11-21 15:32:12 -0700231 cons, f'{mkimage} -f auto -d {inf} {os.path.join(mnt, vmlinux)}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600232
Simon Glassd77e8ae2024-11-21 15:32:11 -0700233 with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd:
234 print('initrd', file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600235
Simon Glassd77e8ae2024-11-21 15:32:11 -0700236 mkdir_cond(os.path.join(mnt, dtbdir))
Simon Glassd3203bd2022-04-24 23:31:25 -0600237
Simon Glassd77e8ae2024-11-21 15:32:11 -0700238 dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb')
239 u_boot_utils.run_and_log(
240 cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};')
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700241
Simon Glassd77e8ae2024-11-21 15:32:11 -0700242 fsfile = 'vfat18M.img'
243 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
244 u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
245 u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/'])
246 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
247 complete = True
248 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
249 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600250 if not complete:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600251 copy_prepared_image(cons, mmc_dev, fname)
Simon Glassd3203bd2022-04-24 23:31:25 -0600252
Simon Glassfff928c2023-08-24 13:55:41 -0600253def setup_cros_image(cons):
254 """Create a 20MB disk image with ChromiumOS partitions"""
255 Partition = collections.namedtuple('part', 'start,size,name')
256 parts = {}
257 disk_data = None
258
259 def pack_kernel(cons, arch, kern, dummy):
260 """Pack a kernel containing some fake data
261
262 Args:
263 cons (ConsoleBase): Console to use
264 arch (str): Architecture to use ('x86' or 'arm')
265 kern (str): Filename containing kernel
266 dummy (str): Dummy filename to use for config and bootloader
267
268 Return:
269 bytes: Packed-kernel data
270 """
Simon Glass08631802024-09-01 16:26:14 -0600271 kern_part = os.path.join(cons.config.result_dir,
272 f'kern-part-{arch}.bin')
Simon Glassfff928c2023-08-24 13:55:41 -0600273 u_boot_utils.run_and_log(
274 cons,
275 f'futility vbutil_kernel --pack {kern_part} '
276 '--keyblock doc/chromium/files/devkeys/kernel.keyblock '
277 '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk '
278 f'--version 1 --config {dummy} --bootloader {dummy} '
279 f'--vmlinuz {kern}')
280
281 with open(kern_part, 'rb') as inf:
282 kern_part_data = inf.read()
283 return kern_part_data
284
285 def set_part_data(partnum, data):
286 """Set the contents of a disk partition
287
288 This updates disk_data by putting data in the right place
289
290 Args:
291 partnum (int): Partition number to set
292 data (bytes): Data for that partition
293 """
294 nonlocal disk_data
295
296 start = parts[partnum].start * sect_size
297 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
298
299 mmc_dev = 5
300 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600301 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Simon Glassfff928c2023-08-24 13:55:41 -0600302 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
303
304 uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
305 uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309'
306 uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
307 uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3'
308 uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e'
309 uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
310
311 ptr = 40
312
313 # Number of sectors in 1MB
314 sect_size = 512
315 sect_1mb = (1 << 20) // sect_size
316
317 required_parts = [
318 {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'},
319 {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'},
320 {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'},
321 {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
322 {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
323
324 {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'},
325 {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'},
326
327 {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'},
328 {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'},
329
330 {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'},
331 {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'},
332 {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'},
333 ]
334
335 for part in required_parts:
336 size_str = part['size']
337 if 'M' in size_str:
338 size = int(size_str[:-1]) * sect_1mb
339 else:
340 size = int(size_str)
341 u_boot_utils.run_and_log(
342 cons,
343 f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}")
344 ptr += size
345
346 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
347 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
Simon Glass08631802024-09-01 16:26:14 -0600348
349 # We expect something like this:
350 # 8239 2048 1 Basic data
351 # 45 2048 2 ChromeOS kernel
352 # 8238 1 3 ChromeOS rootfs
353 # 2093 2048 4 ChromeOS kernel
354 # 8237 1 5 ChromeOS rootfs
355 # 41 1 6 ChromeOS kernel
356 # 42 1 7 ChromeOS rootfs
357 # 4141 2048 8 Basic data
358 # 43 1 9 ChromeOS reserved
359 # 44 1 10 ChromeOS reserved
360 # 40 1 11 ChromeOS firmware
361 # 6189 2048 12 EFI System Partition
Simon Glassfff928c2023-08-24 13:55:41 -0600362
363 # Create a dict (indexed by partition number) containing the above info
364 for line in out.splitlines():
365 start, size, num, name = line.split(maxsplit=3)
366 parts[int(num)] = Partition(int(start), int(size), name)
367
368 dummy = os.path.join(cons.config.result_dir, 'dummy.txt')
369 with open(dummy, 'wb') as outf:
370 outf.write(b'dummy\n')
371
372 # For now we just use dummy kernels. This limits testing to just detecting
373 # a signed kernel. We could add support for the x86 data structures so that
374 # testing could cover getting the cmdline, setup.bin and other pieces.
375 kern = os.path.join(cons.config.result_dir, 'kern.bin')
376 with open(kern, 'wb') as outf:
377 outf.write(b'kernel\n')
378
379 with open(fname, 'rb') as inf:
380 disk_data = inf.read()
381
382 # put x86 kernel in partition 2 and arm one in partition 4
383 set_part_data(2, pack_kernel(cons, 'x86', kern, dummy))
384 set_part_data(4, pack_kernel(cons, 'arm', kern, dummy))
385
386 with open(fname, 'wb') as outf:
387 outf.write(disk_data)
388
389 return fname
390
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200391def setup_android_image(cons):
392 """Create a 20MB disk image with Android partitions"""
393 Partition = collections.namedtuple('part', 'start,size,name')
394 parts = {}
395 disk_data = None
396
397 def set_part_data(partnum, data):
398 """Set the contents of a disk partition
399
400 This updates disk_data by putting data in the right place
401
402 Args:
403 partnum (int): Partition number to set
404 data (bytes): Data for that partition
405 """
406 nonlocal disk_data
407
408 start = parts[partnum].start * sect_size
409 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
410
411 mmc_dev = 7
412 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600413 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200414 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
415
416 ptr = 40
417
418 # Number of sectors in 1MB
419 sect_size = 512
420 sect_1mb = (1 << 20) // sect_size
421
422 required_parts = [
423 {'num': 1, 'label':'misc', 'size': '1M'},
424 {'num': 2, 'label':'boot_a', 'size': '4M'},
425 {'num': 3, 'label':'boot_b', 'size': '4M'},
426 {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
427 {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
428 ]
429
430 for part in required_parts:
431 size_str = part['size']
432 if 'M' in size_str:
433 size = int(size_str[:-1]) * sect_1mb
434 else:
435 size = int(size_str)
436 u_boot_utils.run_and_log(
437 cons,
438 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
439 ptr += size
440
441 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
442 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
443
444 # Create a dict (indexed by partition number) containing the above info
445 for line in out.splitlines():
446 start, size, num, name = line.split(maxsplit=3)
447 parts[int(num)] = Partition(int(start), int(size), name)
448
449 with open(fname, 'rb') as inf:
450 disk_data = inf.read()
451
452 test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex)
453 boot_img = os.path.join(cons.config.result_dir, 'bootv4.img')
454 with open(boot_img, 'rb') as inf:
455 set_part_data(2, inf.read())
456
457 test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex)
458 vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img')
459 with open(vendor_boot_img, 'rb') as inf:
460 set_part_data(4, inf.read())
461
462 with open(fname, 'wb') as outf:
463 outf.write(disk_data)
464
Simon Glass08631802024-09-01 16:26:14 -0600465 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200466
467 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600468
Simon Glassb8c26552023-06-01 10:23:03 -0600469def setup_cedit_file(cons):
Simon Glass08631802024-09-01 16:26:14 -0600470 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glassb8c26552023-06-01 10:23:03 -0600471 infname = os.path.join(cons.config.source_dir,
472 'test/boot/files/expo_layout.dts')
Simon Glassb1263bc2023-08-14 16:40:28 -0600473 inhname = os.path.join(cons.config.source_dir,
474 'test/boot/files/expo_ids.h')
Simon Glassb8c26552023-06-01 10:23:03 -0600475 expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py')
476 outfname = 'cedit.dtb'
477 u_boot_utils.run_and_log(
Simon Glassb1263bc2023-08-14 16:40:28 -0600478 cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600479
Stephen Warren770fe172016-02-08 14:44:16 -0700480@pytest.mark.buildconfigspec('ut_dm')
481def test_ut_dm_init(u_boot_console):
482 """Initialize data for ut dm tests."""
483
484 fn = u_boot_console.config.source_dir + '/testflash.bin'
485 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400486 data = b'this is a test'
487 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700488 with open(fn, 'wb') as fh:
489 fh.write(data)
490
491 fn = u_boot_console.config.source_dir + '/spi.bin'
492 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400493 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700494 with open(fn, 'wb') as fh:
495 fh.write(data)
496
Simon Glass509f32e2022-09-21 16:21:47 +0200497 # Create a file with a single partition
498 fn = u_boot_console.config.source_dir + '/scsi.img'
499 if not os.path.exists(fn):
500 data = b'\x00' * (2 * 1024 * 1024)
501 with open(fn, 'wb') as fh:
502 fh.write(data)
503 u_boot_utils.run_and_log(
504 u_boot_console, f'sfdisk {fn}', stdin=b'type=83')
505
Richard Weinberger41eca322024-11-21 15:32:07 -0700506 fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', None)
507 fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', None)
Simon Glass2c7b0e42022-10-29 19:47:19 -0600508
Alexander Gendin038cb022023-10-09 01:24:36 +0000509 mmc_dev = 6
510 fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img')
511 data = b'\x00' * (12 * 1024 * 1024)
512 with open(fn, 'wb') as fh:
513 fh.write(data)
514
Simon Glass64c63252024-11-07 14:31:49 -0700515
516def setup_efi_image(cons):
517 """Create a 20MB disk image with an EFI app on it"""
518 devnum = 1
519 basename = 'flash'
520 fname, mnt = setup_image(cons, devnum, 0xc, second_part=True,
521 basename=basename)
522
Simon Glass64c63252024-11-07 14:31:49 -0700523 complete = False
Simon Glassd77e8ae2024-11-21 15:32:11 -0700524 efi_dir = os.path.join(mnt, 'EFI')
525 mkdir_cond(efi_dir)
526 bootdir = os.path.join(efi_dir, 'BOOT')
527 mkdir_cond(bootdir)
528 efi_src = os.path.join(cons.config.build_dir,
529 'lib/efi_loader/testapp.efi')
530 efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI')
531 with open(efi_src, 'rb') as inf:
532 with open(efi_dst, 'wb') as outf:
533 outf.write(inf.read())
534 fsfile = 'vfat18M.img'
535 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
536 u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
537 u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/'])
538 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
539 complete = True
540 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
541 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glass64c63252024-11-07 14:31:49 -0700542
543 if not complete:
544 copy_prepared_image(cons, devnum, fname, basename)
545
546
Simon Glassd3203bd2022-04-24 23:31:25 -0600547@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600548@pytest.mark.buildconfigspec('sandbox')
Simon Glassd3203bd2022-04-24 23:31:25 -0600549def test_ut_dm_init_bootstd(u_boot_console):
550 """Initialise data for bootflow tests"""
551
552 setup_bootflow_image(u_boot_console)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600553 setup_bootmenu_image(u_boot_console)
Simon Glassb8c26552023-06-01 10:23:03 -0600554 setup_cedit_file(u_boot_console)
Simon Glassfff928c2023-08-24 13:55:41 -0600555 setup_cros_image(u_boot_console)
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200556 setup_android_image(u_boot_console)
Simon Glass64c63252024-11-07 14:31:49 -0700557 setup_efi_image(u_boot_console)
Simon Glassd3203bd2022-04-24 23:31:25 -0600558
559 # Restart so that the new mmc1.img is picked up
560 u_boot_console.restart_uboot()
561
562
Stephen Warren770fe172016-02-08 14:44:16 -0700563def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200564 """Execute a "ut" subtest.
565
566 The subtests are collected in function generate_ut_subtest() from linker
567 generated lists by applying a regular expression to the lines of file
568 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
569
570 Strict naming conventions have to be followed to match the regular
571 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
572 test suite foo that can be executed via command 'ut foo bar' and is
573 implemented in C function foo_test_bar().
574
575 Args:
576 u_boot_console (ConsoleBase): U-Boot console
577 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
578 execute command 'ut foo bar'
579 """
Stephen Warren770fe172016-02-08 14:44:16 -0700580
Francis Laniel91ec8702023-12-22 22:02:24 +0100581 if ut_subtest == 'hush hush_test_simple_dollar':
582 # ut hush hush_test_simple_dollar prints "Unknown command" on purpose.
583 with u_boot_console.disable_check('unknown_command'):
584 output = u_boot_console.run_command('ut ' + ut_subtest)
Simon Glass08631802024-09-01 16:26:14 -0600585 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100586 else:
587 output = u_boot_console.run_command('ut ' + ut_subtest)
Stephen Warren770fe172016-02-08 14:44:16 -0700588 assert output.endswith('Failures: 0')