Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 2 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 3 | |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame^] | 4 | import getpass |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 5 | import gzip |
| 6 | import os |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 7 | import os.path |
| 8 | import pytest |
| 9 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 10 | import u_boot_utils |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 11 | from tests import fs_helper |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 12 | |
| 13 | def mkdir_cond(dirname): |
| 14 | """Create a directory if it doesn't already exist |
| 15 | |
| 16 | Args: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame^] | 17 | dirname (str): Name of directory to create |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 18 | """ |
| 19 | if not os.path.exists(dirname): |
| 20 | os.mkdir(dirname) |
| 21 | |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame^] | 22 | def setup_image(cons, mmc_dev, part_type): |
| 23 | """Create a 20MB disk image with a single partition |
| 24 | |
| 25 | Args: |
| 26 | cons (ConsoleBase): Console to use |
| 27 | mmc_dev (int): MMC device number to use, e.g. 1 |
| 28 | part_type (int): Partition type, e.g. 0xc for FAT32 |
| 29 | |
| 30 | Returns: |
| 31 | tuple: |
| 32 | str: Filename of MMC image |
| 33 | str: Directory name of 'mnt' directory |
| 34 | """ |
| 35 | fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 36 | mnt = os.path.join(cons.config.persistent_data_dir, 'mnt') |
| 37 | mkdir_cond(mnt) |
| 38 | |
| 39 | u_boot_utils.run_and_log(cons, 'qemu-img create %s 20M' % fname) |
| 40 | u_boot_utils.run_and_log(cons, 'sudo sfdisk %s' % fname, |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame^] | 41 | stdin=f'type={part_type:x}'.encode('utf-8')) |
| 42 | return fname, mnt |
| 43 | |
| 44 | def mount_image(cons, fname, mnt, fstype): |
| 45 | """Create a filesystem and mount it on partition 1 |
| 46 | |
| 47 | Args: |
| 48 | cons (ConsoleBase): Console to use |
| 49 | fname (str): Filename of MMC image |
| 50 | mnt (str): Directory name of 'mnt' directory |
| 51 | fstype (str): Filesystem type ('vfat' or 'ext4') |
| 52 | |
| 53 | Returns: |
| 54 | str: Name of loop device used |
| 55 | """ |
| 56 | out = u_boot_utils.run_and_log(cons, 'sudo losetup --show -f -P %s' % fname) |
| 57 | loop = out.strip() |
| 58 | part = f'{loop}p1' |
| 59 | u_boot_utils.run_and_log(cons, f'sudo mkfs.{fstype} {part}') |
| 60 | opts = '' |
| 61 | if fstype == 'vfat': |
| 62 | opts += ' -o uid={os.getuid()},gid={os.getgid()}' |
| 63 | u_boot_utils.run_and_log(cons, f'sudo mount -o loop {part} {mnt}{opts}') |
| 64 | u_boot_utils.run_and_log(cons, f'sudo chown {getpass.getuser()} {mnt}') |
| 65 | return loop |
| 66 | |
| 67 | def copy_prepared_image(cons, mmc_dev, fname): |
| 68 | """Use a prepared image since we cannot create one |
| 69 | |
| 70 | Args: |
| 71 | cons (ConsoleBase): Console touse |
| 72 | mmc_dev (int): MMC device number |
| 73 | fname (str): Filename of MMC image |
| 74 | """ |
| 75 | infname = os.path.join(cons.config.source_dir, |
| 76 | f'test/py/tests/bootstd/mmc{mmc_dev}.img.xz') |
| 77 | u_boot_utils.run_and_log( |
| 78 | cons, |
| 79 | ['sh', '-c', 'xz -dc %s >%s' % (infname, fname)]) |
| 80 | |
| 81 | def setup_bootmenu_image(cons): |
| 82 | """Create a 20MB disk image with a single ext4 partition |
| 83 | |
| 84 | This is modelled on Armbian 22.08 Jammy |
| 85 | """ |
| 86 | mmc_dev = 4 |
| 87 | fname, mnt = setup_image(cons, mmc_dev, 0x83) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 88 | |
| 89 | loop = None |
| 90 | mounted = False |
| 91 | complete = False |
| 92 | try: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame^] | 93 | loop = mount_image(cons, fname, mnt, 'ext4') |
| 94 | mounted = True |
| 95 | |
| 96 | vmlinux = 'Image' |
| 97 | initrd = 'uInitrd' |
| 98 | dtbdir = 'dtb' |
| 99 | script = '''# DO NOT EDIT THIS FILE |
| 100 | # |
| 101 | # Please edit /boot/armbianEnv.txt to set supported parameters |
| 102 | # |
| 103 | |
| 104 | setenv load_addr "0x9000000" |
| 105 | setenv overlay_error "false" |
| 106 | # default values |
| 107 | setenv rootdev "/dev/mmcblk%dp1" |
| 108 | setenv verbosity "1" |
| 109 | setenv console "both" |
| 110 | setenv bootlogo "false" |
| 111 | setenv rootfstype "ext4" |
| 112 | setenv docker_optimizations "on" |
| 113 | setenv earlycon "off" |
| 114 | |
| 115 | echo "Boot script loaded from ${devtype} ${devnum}" |
| 116 | |
| 117 | if test -e ${devtype} ${devnum} ${prefix}armbianEnv.txt; then |
| 118 | load ${devtype} ${devnum} ${load_addr} ${prefix}armbianEnv.txt |
| 119 | env import -t ${load_addr} ${filesize} |
| 120 | fi |
| 121 | |
| 122 | if test "${logo}" = "disabled"; then setenv logo "logo.nologo"; fi |
| 123 | |
| 124 | if test "${console}" = "display" || test "${console}" = "both"; then setenv consoleargs "console=tty1"; fi |
| 125 | if test "${console}" = "serial" || test "${console}" = "both"; then setenv consoleargs "console=ttyS2,1500000 ${consoleargs}"; fi |
| 126 | if test "${earlycon}" = "on"; then setenv consoleargs "earlycon ${consoleargs}"; fi |
| 127 | if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi |
| 128 | |
| 129 | # get PARTUUID of first partition on SD/eMMC the boot script was loaded from |
| 130 | if test "${devtype}" = "mmc"; then part uuid mmc ${devnum}:1 partuuid; fi |
| 131 | |
| 132 | setenv bootargs "root=${rootdev} rootwait rootfstype=${rootfstype} ${consoleargs} consoleblank=0 loglevel=${verbosity} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}" |
| 133 | |
| 134 | if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory swapaccount=1"; fi |
| 135 | |
| 136 | load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd |
| 137 | load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image |
| 138 | |
| 139 | load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile} |
| 140 | fdt addr ${fdt_addr_r} |
| 141 | fdt resize 65536 |
| 142 | for overlay_file in ${overlays}; do |
| 143 | if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-${overlay_file}.dtbo; then |
| 144 | echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo" |
| 145 | fdt apply ${load_addr} || setenv overlay_error "true" |
| 146 | fi |
| 147 | done |
| 148 | for overlay_file in ${user_overlays}; do |
| 149 | if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then |
| 150 | echo "Applying user provided DT overlay ${overlay_file}.dtbo" |
| 151 | fdt apply ${load_addr} || setenv overlay_error "true" |
| 152 | fi |
| 153 | done |
| 154 | if test "${overlay_error}" = "true"; then |
| 155 | echo "Error applying DT overlays, restoring original DT" |
| 156 | load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}dtb/${fdtfile} |
| 157 | else |
| 158 | if load ${devtype} ${devnum} ${load_addr} ${prefix}dtb/rockchip/overlay/${overlay_prefix}-fixup.scr; then |
| 159 | echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)" |
| 160 | source ${load_addr} |
| 161 | fi |
| 162 | if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then |
| 163 | load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr |
| 164 | echo "Applying user provided fixup script (fixup.scr)" |
| 165 | source ${load_addr} |
| 166 | fi |
| 167 | fi |
| 168 | booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r} |
| 169 | |
| 170 | # Recompile with: |
| 171 | # mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr |
| 172 | ''' % (mmc_dev) |
| 173 | bootdir = os.path.join(mnt, 'boot') |
| 174 | mkdir_cond(bootdir) |
| 175 | cmd_fname = os.path.join(bootdir, 'boot.cmd') |
| 176 | scr_fname = os.path.join(bootdir, 'boot.scr') |
| 177 | with open(cmd_fname, 'w') as outf: |
| 178 | print(script, file=outf) |
| 179 | |
| 180 | infname = os.path.join(cons.config.source_dir, |
| 181 | 'test/py/tests/bootstd/armbian.bmp.xz') |
| 182 | bmp_file = os.path.join(bootdir, 'boot.bmp') |
| 183 | u_boot_utils.run_and_log( |
| 184 | cons, |
| 185 | ['sh', '-c', f'xz -dc {infname} >{bmp_file}']) |
| 186 | |
| 187 | u_boot_utils.run_and_log( |
| 188 | cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}') |
| 189 | |
| 190 | kernel = 'vmlinuz-5.15.63-rockchip64' |
| 191 | target = os.path.join(bootdir, kernel) |
| 192 | with open(target, 'wb') as outf: |
| 193 | print('kernel', outf) |
| 194 | |
| 195 | symlink = os.path.join(bootdir, 'Image') |
| 196 | if os.path.exists(symlink): |
| 197 | os.remove(symlink) |
| 198 | u_boot_utils.run_and_log( |
| 199 | cons, f'echo here {kernel} {symlink}') |
| 200 | os.symlink(kernel, symlink) |
| 201 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 202 | u_boot_utils.run_and_log( |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame^] | 203 | cons, f'mkimage -C none -A arm -T script -d {cmd_fname} {scr_fname}') |
| 204 | complete = True |
| 205 | |
| 206 | except ValueError as exc: |
| 207 | print('Falled to create image, failing back to prepared copy: %s', |
| 208 | str(exc)) |
| 209 | finally: |
| 210 | if mounted: |
| 211 | u_boot_utils.run_and_log(cons, 'sudo umount %s' % mnt) |
| 212 | if loop: |
| 213 | u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop) |
| 214 | |
| 215 | if not complete: |
| 216 | copy_prepared_image(cons, mmc_dev, fname) |
| 217 | |
| 218 | def setup_bootflow_image(cons): |
| 219 | """Create a 20MB disk image with a single FAT partition""" |
| 220 | mmc_dev = 1 |
| 221 | fname, mnt = setup_image(cons, mmc_dev, 0xc) |
| 222 | |
| 223 | loop = None |
| 224 | mounted = False |
| 225 | complete = False |
| 226 | try: |
| 227 | loop = mount_image(cons, fname, mnt, 'vfat') |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 228 | mounted = True |
| 229 | |
| 230 | vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl' |
| 231 | initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img' |
| 232 | dtbdir = 'dtb-5.3.7-301.fc31.armv7hl' |
| 233 | script = '''# extlinux.conf generated by appliance-creator |
| 234 | ui menu.c32 |
| 235 | menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options. |
| 236 | menu title Fedora-Workstation-armhfp-31-1.9 Boot Options. |
| 237 | menu hidden |
| 238 | timeout 20 |
| 239 | totaltimeout 600 |
| 240 | |
| 241 | label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) |
| 242 | kernel /%s |
| 243 | append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB |
| 244 | fdtdir /%s/ |
| 245 | initrd /%s''' % (vmlinux, dtbdir, initrd) |
| 246 | ext = os.path.join(mnt, 'extlinux') |
| 247 | mkdir_cond(ext) |
| 248 | |
| 249 | with open(os.path.join(ext, 'extlinux.conf'), 'w') as fd: |
| 250 | print(script, file=fd) |
| 251 | |
| 252 | inf = os.path.join(cons.config.persistent_data_dir, 'inf') |
| 253 | with open(inf, 'wb') as fd: |
| 254 | fd.write(gzip.compress(b'vmlinux')) |
| 255 | u_boot_utils.run_and_log(cons, 'mkimage -f auto -d %s %s' % |
| 256 | (inf, os.path.join(mnt, vmlinux))) |
| 257 | |
| 258 | with open(os.path.join(mnt, initrd), 'w') as fd: |
| 259 | print('initrd', file=fd) |
| 260 | |
| 261 | mkdir_cond(os.path.join(mnt, dtbdir)) |
| 262 | |
| 263 | dtb_file = os.path.join(mnt, '%s/sandbox.dtb' % dtbdir) |
| 264 | u_boot_utils.run_and_log( |
| 265 | cons, 'dtc -o %s' % dtb_file, stdin=b'/dts-v1/; / {};') |
| 266 | complete = True |
| 267 | except ValueError as exc: |
| 268 | print('Falled to create image, failing back to prepared copy: %s', |
| 269 | str(exc)) |
| 270 | finally: |
| 271 | if mounted: |
| 272 | u_boot_utils.run_and_log(cons, 'sudo umount %s' % mnt) |
| 273 | if loop: |
| 274 | u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop) |
| 275 | |
| 276 | if not complete: |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame^] | 277 | copy_prepared_image(cons, mmc_dev, fname) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 278 | |
| 279 | |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 280 | @pytest.mark.buildconfigspec('ut_dm') |
| 281 | def test_ut_dm_init(u_boot_console): |
| 282 | """Initialize data for ut dm tests.""" |
| 283 | |
| 284 | fn = u_boot_console.config.source_dir + '/testflash.bin' |
| 285 | if not os.path.exists(fn): |
Tom Rini | 439ed3e | 2019-10-24 11:59:22 -0400 | [diff] [blame] | 286 | data = b'this is a test' |
| 287 | data += b'\x00' * ((4 * 1024 * 1024) - len(data)) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 288 | with open(fn, 'wb') as fh: |
| 289 | fh.write(data) |
| 290 | |
| 291 | fn = u_boot_console.config.source_dir + '/spi.bin' |
| 292 | if not os.path.exists(fn): |
Tom Rini | 439ed3e | 2019-10-24 11:59:22 -0400 | [diff] [blame] | 293 | data = b'\x00' * (2 * 1024 * 1024) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 294 | with open(fn, 'wb') as fh: |
| 295 | fh.write(data) |
| 296 | |
Simon Glass | 509f32e | 2022-09-21 16:21:47 +0200 | [diff] [blame] | 297 | # Create a file with a single partition |
| 298 | fn = u_boot_console.config.source_dir + '/scsi.img' |
| 299 | if not os.path.exists(fn): |
| 300 | data = b'\x00' * (2 * 1024 * 1024) |
| 301 | with open(fn, 'wb') as fh: |
| 302 | fh.write(data) |
| 303 | u_boot_utils.run_and_log( |
| 304 | u_boot_console, f'sfdisk {fn}', stdin=b'type=83') |
| 305 | |
Simon Glass | 2c7b0e4 | 2022-10-29 19:47:19 -0600 | [diff] [blame] | 306 | fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', |
| 307 | use_src_dir=True) |
| 308 | fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', |
| 309 | use_src_dir=True) |
| 310 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 311 | @pytest.mark.buildconfigspec('cmd_bootflow') |
| 312 | def test_ut_dm_init_bootstd(u_boot_console): |
| 313 | """Initialise data for bootflow tests""" |
| 314 | |
| 315 | setup_bootflow_image(u_boot_console) |
Simon Glass | d2bc33ed | 2023-01-06 08:52:41 -0600 | [diff] [blame^] | 316 | setup_bootmenu_image(u_boot_console) |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 317 | |
| 318 | # Restart so that the new mmc1.img is picked up |
| 319 | u_boot_console.restart_uboot() |
| 320 | |
| 321 | |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 322 | def test_ut(u_boot_console, ut_subtest): |
Heinrich Schuchardt | df6c36c | 2020-05-06 18:26:07 +0200 | [diff] [blame] | 323 | """Execute a "ut" subtest. |
| 324 | |
| 325 | The subtests are collected in function generate_ut_subtest() from linker |
| 326 | generated lists by applying a regular expression to the lines of file |
| 327 | u-boot.sym. The list entries are created using the C macro UNIT_TEST(). |
| 328 | |
| 329 | Strict naming conventions have to be followed to match the regular |
| 330 | expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in |
| 331 | test suite foo that can be executed via command 'ut foo bar' and is |
| 332 | implemented in C function foo_test_bar(). |
| 333 | |
| 334 | Args: |
| 335 | u_boot_console (ConsoleBase): U-Boot console |
| 336 | ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to |
| 337 | execute command 'ut foo bar' |
| 338 | """ |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 339 | |
| 340 | output = u_boot_console.run_command('ut ' + ut_subtest) |
| 341 | assert output.endswith('Failures: 0') |