blob: cacf11f7c0a111ce7bc822f16529152e75cc453a [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
Simon Glassc6a52e72024-11-15 16:19:23 -0700346 # Set up the kernel command-line
Simon Glassfff928c2023-08-24 13:55:41 -0600347 dummy = os.path.join(cons.config.result_dir, 'dummy.txt')
348 with open(dummy, 'wb') as outf:
Simon Glassc6a52e72024-11-15 16:19:23 -0700349 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 -0600350
351 # For now we just use dummy kernels. This limits testing to just detecting
352 # a signed kernel. We could add support for the x86 data structures so that
353 # testing could cover getting the cmdline, setup.bin and other pieces.
354 kern = os.path.join(cons.config.result_dir, 'kern.bin')
355 with open(kern, 'wb') as outf:
356 outf.write(b'kernel\n')
357
358 with open(fname, 'rb') as inf:
359 disk_data = inf.read()
360
361 # put x86 kernel in partition 2 and arm one in partition 4
362 set_part_data(2, pack_kernel(cons, 'x86', kern, dummy))
363 set_part_data(4, pack_kernel(cons, 'arm', kern, dummy))
364
365 with open(fname, 'wb') as outf:
366 outf.write(disk_data)
367
368 return fname
369
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200370def setup_android_image(cons):
371 """Create a 20MB disk image with Android partitions"""
372 Partition = collections.namedtuple('part', 'start,size,name')
373 parts = {}
374 disk_data = None
375
376 def set_part_data(partnum, data):
377 """Set the contents of a disk partition
378
379 This updates disk_data by putting data in the right place
380
381 Args:
382 partnum (int): Partition number to set
383 data (bytes): Data for that partition
384 """
385 nonlocal disk_data
386
387 start = parts[partnum].start * sect_size
388 disk_data = disk_data[:start] + data + disk_data[start + len(data):]
389
390 mmc_dev = 7
391 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
Simon Glass08631802024-09-01 16:26:14 -0600392 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200393 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
394
395 ptr = 40
396
397 # Number of sectors in 1MB
398 sect_size = 512
399 sect_1mb = (1 << 20) // sect_size
400
401 required_parts = [
402 {'num': 1, 'label':'misc', 'size': '1M'},
403 {'num': 2, 'label':'boot_a', 'size': '4M'},
404 {'num': 3, 'label':'boot_b', 'size': '4M'},
405 {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
406 {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
407 ]
408
409 for part in required_parts:
410 size_str = part['size']
411 if 'M' in size_str:
412 size = int(size_str[:-1]) * sect_1mb
413 else:
414 size = int(size_str)
415 u_boot_utils.run_and_log(
416 cons,
417 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
418 ptr += size
419
420 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
421 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
422
423 # Create a dict (indexed by partition number) containing the above info
424 for line in out.splitlines():
425 start, size, num, name = line.split(maxsplit=3)
426 parts[int(num)] = Partition(int(start), int(size), name)
427
428 with open(fname, 'rb') as inf:
429 disk_data = inf.read()
430
431 test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex)
432 boot_img = os.path.join(cons.config.result_dir, 'bootv4.img')
433 with open(boot_img, 'rb') as inf:
434 set_part_data(2, inf.read())
435
436 test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex)
437 vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img')
438 with open(vendor_boot_img, 'rb') as inf:
439 set_part_data(4, inf.read())
440
441 with open(fname, 'wb') as outf:
442 outf.write(disk_data)
443
Simon Glass08631802024-09-01 16:26:14 -0600444 print(f'wrote to {fname}')
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200445
Guillaume La Roque368ad9e2024-11-26 09:06:13 +0100446 mmc_dev = 8
447 fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
448 u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M')
449 u_boot_utils.run_and_log(cons, f'cgpt create {fname}')
450
451 ptr = 40
452
453 # Number of sectors in 1MB
454 sect_size = 512
455 sect_1mb = (1 << 20) // sect_size
456
457 required_parts = [
458 {'num': 1, 'label':'misc', 'size': '1M'},
459 {'num': 2, 'label':'boot_a', 'size': '4M'},
460 {'num': 3, 'label':'boot_b', 'size': '4M'},
461 ]
462
463 for part in required_parts:
464 size_str = part['size']
465 if 'M' in size_str:
466 size = int(size_str[:-1]) * sect_1mb
467 else:
468 size = int(size_str)
469 u_boot_utils.run_and_log(
470 cons,
471 f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
472 ptr += size
473
474 u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}')
475 out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}')
476
477 # Create a dict (indexed by partition number) containing the above info
478 for line in out.splitlines():
479 start, size, num, name = line.split(maxsplit=3)
480 parts[int(num)] = Partition(int(start), int(size), name)
481
482 with open(fname, 'rb') as inf:
483 disk_data = inf.read()
484
485 test_abootimg.AbootimgTestDiskImage(cons, 'boot.img', test_abootimg.img_hex)
486 boot_img = os.path.join(cons.config.result_dir, 'boot.img')
487 with open(boot_img, 'rb') as inf:
488 set_part_data(2, inf.read())
489
490 with open(fname, 'wb') as outf:
491 outf.write(disk_data)
492
493 print(f'wrote to {fname}')
494
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200495 return fname
Simon Glassfff928c2023-08-24 13:55:41 -0600496
Simon Glassb8c26552023-06-01 10:23:03 -0600497def setup_cedit_file(cons):
Simon Glass08631802024-09-01 16:26:14 -0600498 """Set up a .dtb file for use with testing expo and configuration editor"""
Simon Glassb8c26552023-06-01 10:23:03 -0600499 infname = os.path.join(cons.config.source_dir,
500 'test/boot/files/expo_layout.dts')
Simon Glassb1263bc2023-08-14 16:40:28 -0600501 inhname = os.path.join(cons.config.source_dir,
502 'test/boot/files/expo_ids.h')
Simon Glassb8c26552023-06-01 10:23:03 -0600503 expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py')
504 outfname = 'cedit.dtb'
505 u_boot_utils.run_and_log(
Simon Glassb1263bc2023-08-14 16:40:28 -0600506 cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}')
Simon Glassb8c26552023-06-01 10:23:03 -0600507
Stephen Warren770fe172016-02-08 14:44:16 -0700508@pytest.mark.buildconfigspec('ut_dm')
509def test_ut_dm_init(u_boot_console):
510 """Initialize data for ut dm tests."""
511
512 fn = u_boot_console.config.source_dir + '/testflash.bin'
513 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400514 data = b'this is a test'
515 data += b'\x00' * ((4 * 1024 * 1024) - len(data))
Stephen Warren770fe172016-02-08 14:44:16 -0700516 with open(fn, 'wb') as fh:
517 fh.write(data)
518
519 fn = u_boot_console.config.source_dir + '/spi.bin'
520 if not os.path.exists(fn):
Tom Rini439ed3e2019-10-24 11:59:22 -0400521 data = b'\x00' * (2 * 1024 * 1024)
Stephen Warren770fe172016-02-08 14:44:16 -0700522 with open(fn, 'wb') as fh:
523 fh.write(data)
524
Simon Glass509f32e2022-09-21 16:21:47 +0200525 # Create a file with a single partition
526 fn = u_boot_console.config.source_dir + '/scsi.img'
527 if not os.path.exists(fn):
528 data = b'\x00' * (2 * 1024 * 1024)
529 with open(fn, 'wb') as fh:
530 fh.write(data)
531 u_boot_utils.run_and_log(
532 u_boot_console, f'sfdisk {fn}', stdin=b'type=83')
533
Richard Weinberger41eca322024-11-21 15:32:07 -0700534 fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', None)
535 fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', None)
Simon Glass2c7b0e42022-10-29 19:47:19 -0600536
Alexander Gendin038cb022023-10-09 01:24:36 +0000537 mmc_dev = 6
538 fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img')
539 data = b'\x00' * (12 * 1024 * 1024)
540 with open(fn, 'wb') as fh:
541 fh.write(data)
542
Simon Glass64c63252024-11-07 14:31:49 -0700543
544def setup_efi_image(cons):
545 """Create a 20MB disk image with an EFI app on it"""
546 devnum = 1
547 basename = 'flash'
548 fname, mnt = setup_image(cons, devnum, 0xc, second_part=True,
549 basename=basename)
550
Simon Glassd77e8ae2024-11-21 15:32:11 -0700551 efi_dir = os.path.join(mnt, 'EFI')
552 mkdir_cond(efi_dir)
553 bootdir = os.path.join(efi_dir, 'BOOT')
554 mkdir_cond(bootdir)
555 efi_src = os.path.join(cons.config.build_dir,
556 'lib/efi_loader/testapp.efi')
557 efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI')
558 with open(efi_src, 'rb') as inf:
559 with open(efi_dst, 'wb') as outf:
560 outf.write(inf.read())
561 fsfile = 'vfat18M.img'
562 u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}')
563 u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}')
564 u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/'])
565 u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1')
Simon Glassd77e8ae2024-11-21 15:32:11 -0700566 u_boot_utils.run_and_log(cons, f'rm -rf {mnt}')
567 u_boot_utils.run_and_log(cons, f'rm -f {fsfile}')
Simon Glass64c63252024-11-07 14:31:49 -0700568
Simon Glassd3203bd2022-04-24 23:31:25 -0600569@pytest.mark.buildconfigspec('cmd_bootflow')
Simon Glass84712cd2024-06-23 14:30:27 -0600570@pytest.mark.buildconfigspec('sandbox')
Simon Glassd3203bd2022-04-24 23:31:25 -0600571def test_ut_dm_init_bootstd(u_boot_console):
572 """Initialise data for bootflow tests"""
573
574 setup_bootflow_image(u_boot_console)
Simon Glassd2bc33ed2023-01-06 08:52:41 -0600575 setup_bootmenu_image(u_boot_console)
Simon Glassb8c26552023-06-01 10:23:03 -0600576 setup_cedit_file(u_boot_console)
Simon Glassfff928c2023-08-24 13:55:41 -0600577 setup_cros_image(u_boot_console)
Mattijs Korpershoekd77f8152024-07-10 10:40:06 +0200578 setup_android_image(u_boot_console)
Simon Glass64c63252024-11-07 14:31:49 -0700579 setup_efi_image(u_boot_console)
Simon Glassd3203bd2022-04-24 23:31:25 -0600580
581 # Restart so that the new mmc1.img is picked up
582 u_boot_console.restart_uboot()
583
584
Stephen Warren770fe172016-02-08 14:44:16 -0700585def test_ut(u_boot_console, ut_subtest):
Heinrich Schuchardtdf6c36c2020-05-06 18:26:07 +0200586 """Execute a "ut" subtest.
587
588 The subtests are collected in function generate_ut_subtest() from linker
589 generated lists by applying a regular expression to the lines of file
590 u-boot.sym. The list entries are created using the C macro UNIT_TEST().
591
592 Strict naming conventions have to be followed to match the regular
593 expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in
594 test suite foo that can be executed via command 'ut foo bar' and is
595 implemented in C function foo_test_bar().
596
597 Args:
598 u_boot_console (ConsoleBase): U-Boot console
599 ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to
600 execute command 'ut foo bar'
601 """
Stephen Warren770fe172016-02-08 14:44:16 -0700602
Francis Laniel91ec8702023-12-22 22:02:24 +0100603 if ut_subtest == 'hush hush_test_simple_dollar':
604 # ut hush hush_test_simple_dollar prints "Unknown command" on purpose.
605 with u_boot_console.disable_check('unknown_command'):
606 output = u_boot_console.run_command('ut ' + ut_subtest)
Simon Glass08631802024-09-01 16:26:14 -0600607 assert 'Unknown command \'quux\' - try \'help\'' in output
Francis Laniel91ec8702023-12-22 22:02:24 +0100608 else:
609 output = u_boot_console.run_command('ut ' + ut_subtest)
Stephen Warren770fe172016-02-08 14:44:16 -0700610 assert output.endswith('Failures: 0')