blob: 82cedbef03945fd83397781480dc34ec9f8a201f [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 Glassd77e8ae2024-11-21 15:32:11 -0700170 u_boot_utils.run_and_log(
171 cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600172
Simon Glassd77e8ae2024-11-21 15:32:11 -0700173 kernel = 'vmlinuz-5.15.63-rockchip64'
174 target = os.path.join(bootdir, kernel)
175 with open(target, 'wb') as outf:
176 print('kernel', outf)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600177
Simon Glassd77e8ae2024-11-21 15:32:11 -0700178 symlink = os.path.join(bootdir, 'Image')
179 if os.path.exists(symlink):
180 os.remove(symlink)
181 u_boot_utils.run_and_log(
182 cons, f'echo here {kernel} {symlink}')
183 os.symlink(kernel, symlink)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600184
Simon Glassd77e8ae2024-11-21 15:32:11 -0700185 fsfile = 'ext18M.img'
186 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
187 u_boot_utils.run_and_log(cons, f'mkfs.ext4 {fsfile} -d {mnt}')
188 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
189 complete = True
190 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
191 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600192
193 if not complete:
194 copy_prepared_image(cons, mmc_dev, fname)
195
196def setup_bootflow_image(cons):
197 """Create a 20MB disk image with a single FAT partition"""
198 mmc_dev = 1
Simon Glassf5e2df02023-01-17 10:47:41 -0700199 fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600200
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600201 complete = False
Simon Glassd77e8ae2024-11-21 15:32:11 -0700202 vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl'
203 initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img'
204 dtbdir = 'dtb-5.3.7-301.fc31.armv7hl'
205 script = '''# extlinux.conf generated by appliance-creator
Simon Glassd3203bd2022-04-24 23:31:25 -0600206ui menu.c32
207menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
208menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
209menu hidden
210timeout 20
211totaltimeout 600
212
213label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
214 kernel /%s
215 append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB
216 fdtdir /%s/
217 initrd /%s''' % (vmlinux, dtbdir, initrd)
Simon Glassd77e8ae2024-11-21 15:32:11 -0700218 ext = os.path.join(mnt, 'extlinux')
219 mkdir_cond(ext)
Simon Glassd3203bd2022-04-24 23:31:25 -0600220
Simon Glassd77e8ae2024-11-21 15:32:11 -0700221 conf = os.path.join(ext, 'extlinux.conf')
222 with open(conf, 'w', encoding='ascii') as fd:
223 print(script, file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600224
Simon Glassd77e8ae2024-11-21 15:32:11 -0700225 inf = os.path.join(cons.config.persistent_data_dir, 'inf')
226 with open(inf, 'wb') as fd:
227 fd.write(gzip.compress(b'vmlinux'))
228 u_boot_utils.run_and_log(
229 cons, f'mkimage -f auto -d {inf} {os.path.join(mnt, vmlinux)}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600230
Simon Glassd77e8ae2024-11-21 15:32:11 -0700231 with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd:
232 print('initrd', file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600233
Simon Glassd77e8ae2024-11-21 15:32:11 -0700234 mkdir_cond(os.path.join(mnt, dtbdir))
Simon Glassd3203bd2022-04-24 23:31:25 -0600235
Simon Glassd77e8ae2024-11-21 15:32:11 -0700236 dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb')
237 u_boot_utils.run_and_log(
238 cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};')
Richard Weinbergerd99fdc02024-11-21 15:32:10 -0700239
Simon Glassd77e8ae2024-11-21 15:32:11 -0700240 fsfile = 'vfat18M.img'
241 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
242 u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
243 u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/'])
244 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
245 complete = True
246 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
247 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600248 if not complete:
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600249 copy_prepared_image(cons, mmc_dev, fname)
Simon Glassd3203bd2022-04-24 23:31:25 -0600250
Simon Glassfff928c2023-08-24 13:55:41 -0600251def setup_cros_image(cons):
252 """Create a 20MB disk image with ChromiumOS partitions"""
253 Partition = collections.namedtuple('part', 'start,size,name')
254 parts = {}
255 disk_data = None
256
257 def pack_kernel(cons, arch, kern, dummy):
258 """Pack a kernel containing some fake data
259
260 Args:
261 cons (ConsoleBase): Console to use
262 arch (str): Architecture to use ('x86' or 'arm')
263 kern (str): Filename containing kernel
264 dummy (str): Dummy filename to use for config and bootloader
265
266 Return:
267 bytes: Packed-kernel data
268 """
Simon Glass08631802024-09-01 16:26:14 -0600269 kern_part = os.path.join(cons.config.result_dir,
270 f'kern-part-{arch}.bin')
Simon Glassfff928c2023-08-24 13:55:41 -0600271 u_boot_utils.run_and_log(
272 cons,
273 f'futility vbutil_kernel --pack {kern_part} '
274 '--keyblock doc/chromium/files/devkeys/kernel.keyblock '
275 '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk '
276 f'--version 1 --config {dummy} --bootloader {dummy} '
277 f'--vmlinuz {kern}')
278
279 with open(kern_part, 'rb') as inf:
280 kern_part_data = inf.read()
281 return kern_part_data
282
283 def set_part_data(partnum, data):
284 """Set the contents of a disk partition
285
286 This updates disk_data by putting data in the right place
287
288 Args:
289 partnum (int): Partition number to set
290 data (bytes): Data for that partition
291 """
292 nonlocal disk_data
293
294 start = parts[partnum].start * sect_size
295 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
296
297 mmc_dev = 5
298 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600299 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Simon Glassfff928c2023-08-24 13:55:41 -0600300 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
301
302 uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
303 uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309'
304 uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
305 uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3'
306 uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e'
307 uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
308
309 ptr = 40
310
311 # Number of sectors in 1MB
312 sect_size = 512
313 sect_1mb = (1 << 20) // sect_size
314
315 required_parts = [
316 {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'},
317 {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'},
318 {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'},
319 {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
320 {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'},
321
322 {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'},
323 {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'},
324
325 {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'},
326 {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'},
327
328 {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'},
329 {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'},
330 {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'},
331 ]
332
333 for part in required_parts:
334 size_str = part['size']
335 if 'M' in size_str:
336 size = int(size_str[:-1]) * sect_1mb
337 else:
338 size = int(size_str)
339 u_boot_utils.run_and_log(
340 cons,
341 f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}")
342 ptr += size
343
344 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
345 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
Simon Glass08631802024-09-01 16:26:14 -0600346
347 # We expect something like this:
348 # 8239 2048 1 Basic data
349 # 45 2048 2 ChromeOS kernel
350 # 8238 1 3 ChromeOS rootfs
351 # 2093 2048 4 ChromeOS kernel
352 # 8237 1 5 ChromeOS rootfs
353 # 41 1 6 ChromeOS kernel
354 # 42 1 7 ChromeOS rootfs
355 # 4141 2048 8 Basic data
356 # 43 1 9 ChromeOS reserved
357 # 44 1 10 ChromeOS reserved
358 # 40 1 11 ChromeOS firmware
359 # 6189 2048 12 EFI System Partition
Simon Glassfff928c2023-08-24 13:55:41 -0600360
361 # Create a dict (indexed by partition number) containing the above info
362 for line in out.splitlines():
363 start, size, num, name = line.split(maxsplit=3)
364 parts[int(num)] = Partition(int(start), int(size), name)
365
366 dummy = os.path.join(cons.config.result_dir, 'dummy.txt')
367 with open(dummy, 'wb') as outf:
368 outf.write(b'dummy\n')
369
370 # For now we just use dummy kernels. This limits testing to just detecting
371 # a signed kernel. We could add support for the x86 data structures so that
372 # testing could cover getting the cmdline, setup.bin and other pieces.
373 kern = os.path.join(cons.config.result_dir, 'kern.bin')
374 with open(kern, 'wb') as outf:
375 outf.write(b'kernel\n')
376
377 with open(fname, 'rb') as inf:
378 disk_data = inf.read()
379
380 # put x86 kernel in partition 2 and arm one in partition 4
381 set_part_data(2, pack_kernel(cons, 'x86', kern, dummy))
382 set_part_data(4, pack_kernel(cons, 'arm', kern, dummy))
383
384 with open(fname, 'wb') as outf:
385 outf.write(disk_data)
386
387 return fname
388
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200389def setup_android_image(cons):
390 """Create a 20MB disk image with Android partitions"""
391 Partition = collections.namedtuple('part', 'start,size,name')
392 parts = {}
393 disk_data = None
394
395 def set_part_data(partnum, data):
396 """Set the contents of a disk partition
397
398 This updates disk_data by putting data in the right place
399
400 Args:
401 partnum (int): Partition number to set
402 data (bytes): Data for that partition
403 """
404 nonlocal disk_data
405
406 start = parts[partnum].start * sect_size
407 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
408
409 mmc_dev = 7
410 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600411 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200412 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
413
414 ptr = 40
415
416 # Number of sectors in 1MB
417 sect_size = 512
418 sect_1mb = (1 << 20) // sect_size
419
420 required_parts = [
421 {'num': 1, 'label':'misc', 'size': '1M'},
422 {'num': 2, 'label':'boot_a', 'size': '4M'},
423 {'num': 3, 'label':'boot_b', 'size': '4M'},
424 {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
425 {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
426 ]
427
428 for part in required_parts:
429 size_str = part['size']
430 if 'M' in size_str:
431 size = int(size_str[:-1]) * sect_1mb
432 else:
433 size = int(size_str)
434 u_boot_utils.run_and_log(
435 cons,
436 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
437 ptr += size
438
439 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
440 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
441
442 # Create a dict (indexed by partition number) containing the above info
443 for line in out.splitlines():
444 start, size, num, name = line.split(maxsplit=3)
445 parts[int(num)] = Partition(int(start), int(size), name)
446
447 with open(fname, 'rb') as inf:
448 disk_data = inf.read()
449
450 test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex)
451 boot_img = os.path.join(cons.config.result_dir, 'bootv4.img')
452 with open(boot_img, 'rb') as inf:
453 set_part_data(2, inf.read())
454
455 test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex)
456 vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img')
457 with open(vendor_boot_img, 'rb') as inf:
458 set_part_data(4, inf.read())
459
460 with open(fname, 'wb') as outf:
461 outf.write(disk_data)
462
Simon Glass08631802024-09-01 16:26:14 -0600463 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200464
465 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600466
Simon Glassb8c26552023-06-01 10:23:03 -0600467def setup_cedit_file(cons):
Simon Glass08631802024-09-01 16:26:14 -0600468 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glassb8c26552023-06-01 10:23:03 -0600469 infname = os.path.join(cons.config.source_dir,
470 'test/boot/files/expo_layout.dts')
Simon Glassb1263bc2023-08-14 16:40:28 -0600471 inhname = os.path.join(cons.config.source_dir,
472 'test/boot/files/expo_ids.h')
Simon Glassb8c26552023-06-01 10:23:03 -0600473 expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py')
474 outfname = 'cedit.dtb'
475 u_boot_utils.run_and_log(
Simon Glassb1263bc2023-08-14 16:40:28 -0600476 cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600477
Stephen Warren770fe172016-02-08 14:44:16 -0700478@pytest.mark.buildconfigspec('ut_dm')
479def test_ut_dm_init(u_boot_console):
480 """Initialize data for ut dm tests."""
481
482 fn = u_boot_console.config.source_dir + '/testflash.bin'
483 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400484 data = b'this is a test'
485 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700486 with open(fn, 'wb') as fh:
487 fh.write(data)
488
489 fn = u_boot_console.config.source_dir + '/spi.bin'
490 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400491 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700492 with open(fn, 'wb') as fh:
493 fh.write(data)
494
Simon Glass509f32e2022-09-21 16:21:47 +0200495 # Create a file with a single partition
496 fn = u_boot_console.config.source_dir + '/scsi.img'
497 if not os.path.exists(fn):
498 data = b'\x00' * (2 * 1024 * 1024)
499 with open(fn, 'wb') as fh:
500 fh.write(data)
501 u_boot_utils.run_and_log(
502 u_boot_console, f'sfdisk {fn}', stdin=b'type=83')
503
Richard Weinberger41eca322024-11-21 15:32:07 -0700504 fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', None)
505 fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', None)
Simon Glass2c7b0e42022-10-29 19:47:19 -0600506
Alexander Gendin038cb022023-10-09 01:24:36 +0000507 mmc_dev = 6
508 fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img')
509 data = b'\x00' * (12 * 1024 * 1024)
510 with open(fn, 'wb') as fh:
511 fh.write(data)
512
Simon Glass64c63252024-11-07 14:31:49 -0700513
514def setup_efi_image(cons):
515 """Create a 20MB disk image with an EFI app on it"""
516 devnum = 1
517 basename = 'flash'
518 fname, mnt = setup_image(cons, devnum, 0xc, second_part=True,
519 basename=basename)
520
Simon Glass64c63252024-11-07 14:31:49 -0700521 complete = False
Simon Glassd77e8ae2024-11-21 15:32:11 -0700522 efi_dir = os.path.join(mnt, 'EFI')
523 mkdir_cond(efi_dir)
524 bootdir = os.path.join(efi_dir, 'BOOT')
525 mkdir_cond(bootdir)
526 efi_src = os.path.join(cons.config.build_dir,
527 'lib/efi_loader/testapp.efi')
528 efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI')
529 with open(efi_src, 'rb') as inf:
530 with open(efi_dst, 'wb') as outf:
531 outf.write(inf.read())
532 fsfile = 'vfat18M.img'
533 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
534 u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
535 u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/'])
536 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
537 complete = True
538 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
539 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glass64c63252024-11-07 14:31:49 -0700540
541 if not complete:
542 copy_prepared_image(cons, devnum, fname, basename)
543
544
Simon Glassd3203bd2022-04-24 23:31:25 -0600545@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600546@pytest.mark.buildconfigspec('sandbox')
Simon Glassd3203bd2022-04-24 23:31:25 -0600547def test_ut_dm_init_bootstd(u_boot_console):
548 """Initialise data for bootflow tests"""
549
550 setup_bootflow_image(u_boot_console)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600551 setup_bootmenu_image(u_boot_console)
Simon Glassb8c26552023-06-01 10:23:03 -0600552 setup_cedit_file(u_boot_console)
Simon Glassfff928c2023-08-24 13:55:41 -0600553 setup_cros_image(u_boot_console)
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200554 setup_android_image(u_boot_console)
Simon Glass64c63252024-11-07 14:31:49 -0700555 setup_efi_image(u_boot_console)
Simon Glassd3203bd2022-04-24 23:31:25 -0600556
557 # Restart so that the new mmc1.img is picked up
558 u_boot_console.restart_uboot()
559
560
Stephen Warren770fe172016-02-08 14:44:16 -0700561def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200562 """Execute a "ut" subtest.
563
564 The subtests are collected in function generate_ut_subtest() from linker
565 generated lists by applying a regular expression to the lines of file
566 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
567
568 Strict naming conventions have to be followed to match the regular
569 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
570 test suite foo that can be executed via command 'ut foo bar' and is
571 implemented in C function foo_test_bar().
572
573 Args:
574 u_boot_console (ConsoleBase): U-Boot console
575 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
576 execute command 'ut foo bar'
577 """
Stephen Warren770fe172016-02-08 14:44:16 -0700578
Francis Laniel91ec8702023-12-22 22:02:24 +0100579 if ut_subtest == 'hush hush_test_simple_dollar':
580 # ut hush hush_test_simple_dollar prints "Unknown command" on purpose.
581 with u_boot_console.disable_check('unknown_command'):
582 output = u_boot_console.run_command('ut ' + ut_subtest)
Simon Glass08631802024-09-01 16:26:14 -0600583 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100584 else:
585 output = u_boot_console.run_command('ut ' + ut_subtest)
Stephen Warren770fe172016-02-08 14:44:16 -0700586 assert output.endswith('Failures: 0')