blob: 10ec7e582e042ed9b0148374b10167339f3612f0 [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
Simon Glass08631802024-09-01 16:26:14 -060055 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Richard Weinbergerd99fdc02024-11-21 15:32:10 -070056 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 Glassd2bc33ed2023-01-06 08:52:41 -060060def 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 Glassd3203bd2022-04-24 23:31:25 -060067
Simon Glassd77e8ae2024-11-21 15:32:11 -070068 script = '''# DO NOT EDIT THIS FILE
Simon Glassd2bc33ed2023-01-06 08:52:41 -060069#
70# Please edit /boot/armbianEnv.txt to set supported parameters
71#
72
73setenv load_addr "0x9000000"
74setenv overlay_error "false"
75# default values
76setenv rootdev "/dev/mmcblk%dp1"
77setenv verbosity "1"
78setenv console "both"
79setenv bootlogo "false"
80setenv rootfstype "ext4"
81setenv docker_optimizations "on"
82setenv earlycon "off"
83
84echo "Boot script loaded from ${devtype} ${devnum}"
85
86if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then
87 load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt
88 env import -t ${load_addr} ${filesize}
89fi
90
91if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi
92
93if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi
94if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi
95if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi
96if 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
99if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi
100
101setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}"
102
103if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi
104
105load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd
106load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image
107
108load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
109fdt addr ${fdt_addr_r}
110fdt resize 65536
111for 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
116done
117for 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
122done
123if test "${overlay_error}" = "true"; then
124 echo "Error applying DT overlays, restoring original DT"
125 load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile}
126else
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
136fi
137booti ${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 Glass08631802024-09-01 16:26:14 -0600141'''
Simon Glassd77e8ae2024-11-21 15:32:11 -0700142 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 Glassd2bc33ed2023-01-06 08:52:41 -0600148
Simon Glassd77e8ae2024-11-21 15:32:11 -0700149 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 Glassd2bc33ed2023-01-06 08:52:41 -0600155
Simon Glassefcc5662024-11-21 15:32:12 -0700156 mkimage = cons.config.build_dir + '/tools/mkimage'
Simon Glassd77e8ae2024-11-21 15:32:11 -0700157 u_boot_utils.run_and_log(
Simon Glassefcc5662024-11-21 15:32:12 -0700158 cons, f'{mkimage} -C none -A arm -T script -d {cmd_fname} {scr_fname}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600159
Simon Glassd77e8ae2024-11-21 15:32:11 -0700160 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 Glassd2bc33ed2023-01-06 08:52:41 -0600164
Simon Glassd77e8ae2024-11-21 15:32:11 -0700165 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 Glassd2bc33ed2023-01-06 08:52:41 -0600171
Simon Glassd77e8ae2024-11-21 15:32:11 -0700172 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 Glassd77e8ae2024-11-21 15:32:11 -0700176 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
177 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600178
179def setup_bootflow_image(cons):
180 """Create a 20MB disk image with a single FAT partition"""
181 mmc_dev = 1
Simon Glassf5e2df02023-01-17 10:47:41 -0700182 fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600183
Simon Glassd77e8ae2024-11-21 15:32:11 -0700184 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 Glassd3203bd2022-04-24 23:31:25 -0600188ui menu.c32
189menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options.
190menu title Fedora-Workstation-armhfp-31-1.9 Boot Options.
191menu hidden
192timeout 20
193totaltimeout 600
194
195label 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 Glassd77e8ae2024-11-21 15:32:11 -0700200 ext = os.path.join(mnt, 'extlinux')
201 mkdir_cond(ext)
Simon Glassd3203bd2022-04-24 23:31:25 -0600202
Simon Glassd77e8ae2024-11-21 15:32:11 -0700203 conf = os.path.join(ext, 'extlinux.conf')
204 with open(conf, 'w', encoding='ascii') as fd:
205 print(script, file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600206
Simon Glassd77e8ae2024-11-21 15:32:11 -0700207 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 Glassefcc5662024-11-21 15:32:12 -0700210 mkimage = cons.config.build_dir + '/tools/mkimage'
Simon Glassd77e8ae2024-11-21 15:32:11 -0700211 u_boot_utils.run_and_log(
Simon Glassefcc5662024-11-21 15:32:12 -0700212 cons, f'{mkimage} -f auto -d {inf} {os.path.join(mnt, vmlinux)}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600213
Simon Glassd77e8ae2024-11-21 15:32:11 -0700214 with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd:
215 print('initrd', file=fd)
Simon Glassd3203bd2022-04-24 23:31:25 -0600216
Simon Glassd77e8ae2024-11-21 15:32:11 -0700217 mkdir_cond(os.path.join(mnt, dtbdir))
Simon Glassd3203bd2022-04-24 23:31:25 -0600218
Simon Glassd77e8ae2024-11-21 15:32:11 -0700219 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 Glassd3203bd2022-04-24 23:31:25 -0600222
Simon Glassd77e8ae2024-11-21 15:32:11 -0700223 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 Glassd77e8ae2024-11-21 15:32:11 -0700228 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
229 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glassd3203bd2022-04-24 23:31:25 -0600230
Simon Glassfff928c2023-08-24 13:55:41 -0600231def 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 Glass08631802024-09-01 16:26:14 -0600249 kern_part = os.path.join(cons.config.result_dir,
250 f'kern-part-{arch}.bin')
Simon Glassfff928c2023-08-24 13:55:41 -0600251 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 Glass08631802024-09-01 16:26:14 -0600279 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Simon Glassfff928c2023-08-24 13:55:41 -0600280 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 Glass08631802024-09-01 16:26:14 -0600326
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 Glassfff928c2023-08-24 13:55:41 -0600340
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 Korpershoekd77f8152024-07-10 10:40:06 +0200369def 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 Glass08631802024-09-01 16:26:14 -0600391 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200392 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 Glass08631802024-09-01 16:26:14 -0600443 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200444
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100445 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 Korpershoekd77f8152024-07-10 10:40:06 +0200494 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600495
Simon Glassb8c26552023-06-01 10:23:03 -0600496def setup_cedit_file(cons):
Simon Glass08631802024-09-01 16:26:14 -0600497 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glassb8c26552023-06-01 10:23:03 -0600498 infname = os.path.join(cons.config.source_dir,
499 'test/boot/files/expo_layout.dts')
Simon Glassb1263bc2023-08-14 16:40:28 -0600500 inhname = os.path.join(cons.config.source_dir,
501 'test/boot/files/expo_ids.h')
Simon Glassb8c26552023-06-01 10:23:03 -0600502 expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py')
503 outfname = 'cedit.dtb'
504 u_boot_utils.run_and_log(
Simon Glassb1263bc2023-08-14 16:40:28 -0600505 cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600506
Stephen Warren770fe172016-02-08 14:44:16 -0700507@pytest.mark.buildconfigspec('ut_dm')
508def 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 Rini439ed3e2019-10-24 11:59:22 -0400513 data = b'this is a test'
514 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700515 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 Rini439ed3e2019-10-24 11:59:22 -0400520 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700521 with open(fn, 'wb') as fh:
522 fh.write(data)
523
Simon Glass509f32e2022-09-21 16:21:47 +0200524 # 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 Weinberger41eca322024-11-21 15:32:07 -0700533 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 Glass2c7b0e42022-10-29 19:47:19 -0600535
Alexander Gendin038cb022023-10-09 01:24:36 +0000536 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 Glass64c63252024-11-07 14:31:49 -0700542
543def 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 Glassd77e8ae2024-11-21 15:32:11 -0700550 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 Glassd77e8ae2024-11-21 15:32:11 -0700565 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
566 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glass64c63252024-11-07 14:31:49 -0700567
Simon Glassd3203bd2022-04-24 23:31:25 -0600568@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600569@pytest.mark.buildconfigspec('sandbox')
Simon Glassd3203bd2022-04-24 23:31:25 -0600570def test_ut_dm_init_bootstd(u_boot_console):
571 """Initialise data for bootflow tests"""
572
573 setup_bootflow_image(u_boot_console)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600574 setup_bootmenu_image(u_boot_console)
Simon Glassb8c26552023-06-01 10:23:03 -0600575 setup_cedit_file(u_boot_console)
Simon Glassfff928c2023-08-24 13:55:41 -0600576 setup_cros_image(u_boot_console)
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200577 setup_android_image(u_boot_console)
Simon Glass64c63252024-11-07 14:31:49 -0700578 setup_efi_image(u_boot_console)
Simon Glassd3203bd2022-04-24 23:31:25 -0600579
580 # Restart so that the new mmc1.img is picked up
581 u_boot_console.restart_uboot()
582
583
Stephen Warren770fe172016-02-08 14:44:16 -0700584def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200585 """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 Warren770fe172016-02-08 14:44:16 -0700601
Francis Laniel91ec8702023-12-22 22:02:24 +0100602 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 Glass08631802024-09-01 16:26:14 -0600606 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100607 else:
608 output = u_boot_console.run_command('ut ' + ut_subtest)
Stephen Warren770fe172016-02-08 14:44:16 -0700609 assert output.endswith('Failures: 0')