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 | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 4 | import gzip |
| 5 | import os |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 6 | import os.path |
| 7 | import pytest |
| 8 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 9 | import u_boot_utils |
| 10 | |
| 11 | def mkdir_cond(dirname): |
| 12 | """Create a directory if it doesn't already exist |
| 13 | |
| 14 | Args: |
| 15 | dirname: Name of directory to create |
| 16 | """ |
| 17 | if not os.path.exists(dirname): |
| 18 | os.mkdir(dirname) |
| 19 | |
| 20 | def setup_bootflow_image(u_boot_console): |
| 21 | """Create a 20MB disk image with a single FAT partition""" |
| 22 | cons = u_boot_console |
| 23 | fname = os.path.join(cons.config.source_dir, 'mmc1.img') |
| 24 | mnt = os.path.join(cons.config.persistent_data_dir, 'mnt') |
| 25 | mkdir_cond(mnt) |
| 26 | |
| 27 | u_boot_utils.run_and_log(cons, 'qemu-img create %s 20M' % fname) |
| 28 | u_boot_utils.run_and_log(cons, 'sudo sfdisk %s' % fname, |
| 29 | stdin=b'type=c') |
| 30 | |
| 31 | loop = None |
| 32 | mounted = False |
| 33 | complete = False |
| 34 | try: |
| 35 | out = u_boot_utils.run_and_log(cons, |
| 36 | 'sudo losetup --show -f -P %s' % fname) |
| 37 | loop = out.strip() |
| 38 | fatpart = '%sp1' % loop |
| 39 | u_boot_utils.run_and_log(cons, 'sudo mkfs.vfat %s' % fatpart) |
| 40 | u_boot_utils.run_and_log( |
| 41 | cons, 'sudo mount -o loop %s %s -o uid=%d,gid=%d' % |
| 42 | (fatpart, mnt, os.getuid(), os.getgid())) |
| 43 | mounted = True |
| 44 | |
| 45 | vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl' |
| 46 | initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img' |
| 47 | dtbdir = 'dtb-5.3.7-301.fc31.armv7hl' |
| 48 | script = '''# extlinux.conf generated by appliance-creator |
| 49 | ui menu.c32 |
| 50 | menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options. |
| 51 | menu title Fedora-Workstation-armhfp-31-1.9 Boot Options. |
| 52 | menu hidden |
| 53 | timeout 20 |
| 54 | totaltimeout 600 |
| 55 | |
| 56 | label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) |
| 57 | kernel /%s |
| 58 | append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB |
| 59 | fdtdir /%s/ |
| 60 | initrd /%s''' % (vmlinux, dtbdir, initrd) |
| 61 | ext = os.path.join(mnt, 'extlinux') |
| 62 | mkdir_cond(ext) |
| 63 | |
| 64 | with open(os.path.join(ext, 'extlinux.conf'), 'w') as fd: |
| 65 | print(script, file=fd) |
| 66 | |
| 67 | inf = os.path.join(cons.config.persistent_data_dir, 'inf') |
| 68 | with open(inf, 'wb') as fd: |
| 69 | fd.write(gzip.compress(b'vmlinux')) |
| 70 | u_boot_utils.run_and_log(cons, 'mkimage -f auto -d %s %s' % |
| 71 | (inf, os.path.join(mnt, vmlinux))) |
| 72 | |
| 73 | with open(os.path.join(mnt, initrd), 'w') as fd: |
| 74 | print('initrd', file=fd) |
| 75 | |
| 76 | mkdir_cond(os.path.join(mnt, dtbdir)) |
| 77 | |
| 78 | dtb_file = os.path.join(mnt, '%s/sandbox.dtb' % dtbdir) |
| 79 | u_boot_utils.run_and_log( |
| 80 | cons, 'dtc -o %s' % dtb_file, stdin=b'/dts-v1/; / {};') |
| 81 | complete = True |
| 82 | except ValueError as exc: |
| 83 | print('Falled to create image, failing back to prepared copy: %s', |
| 84 | str(exc)) |
| 85 | finally: |
| 86 | if mounted: |
| 87 | u_boot_utils.run_and_log(cons, 'sudo umount %s' % mnt) |
| 88 | if loop: |
| 89 | u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop) |
| 90 | |
| 91 | if not complete: |
| 92 | # Use a prepared image since we cannot create one |
| 93 | infname = os.path.join(cons.config.source_dir, |
| 94 | 'test/py/tests/bootstd/mmc1.img.xz') |
| 95 | u_boot_utils.run_and_log( |
| 96 | cons, |
| 97 | ['sh', '-c', 'xz -dc %s >%s' % (infname, fname)]) |
| 98 | |
| 99 | |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 100 | @pytest.mark.buildconfigspec('ut_dm') |
| 101 | def test_ut_dm_init(u_boot_console): |
| 102 | """Initialize data for ut dm tests.""" |
| 103 | |
| 104 | fn = u_boot_console.config.source_dir + '/testflash.bin' |
| 105 | if not os.path.exists(fn): |
Tom Rini | 439ed3e | 2019-10-24 11:59:22 -0400 | [diff] [blame] | 106 | data = b'this is a test' |
| 107 | data += b'\x00' * ((4 * 1024 * 1024) - len(data)) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 108 | with open(fn, 'wb') as fh: |
| 109 | fh.write(data) |
| 110 | |
| 111 | fn = u_boot_console.config.source_dir + '/spi.bin' |
| 112 | if not os.path.exists(fn): |
Tom Rini | 439ed3e | 2019-10-24 11:59:22 -0400 | [diff] [blame] | 113 | data = b'\x00' * (2 * 1024 * 1024) |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 114 | with open(fn, 'wb') as fh: |
| 115 | fh.write(data) |
| 116 | |
Simon Glass | d3203bd | 2022-04-24 23:31:25 -0600 | [diff] [blame] | 117 | @pytest.mark.buildconfigspec('cmd_bootflow') |
| 118 | def test_ut_dm_init_bootstd(u_boot_console): |
| 119 | """Initialise data for bootflow tests""" |
| 120 | |
| 121 | setup_bootflow_image(u_boot_console) |
| 122 | |
| 123 | # Restart so that the new mmc1.img is picked up |
| 124 | u_boot_console.restart_uboot() |
| 125 | |
| 126 | |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 127 | def test_ut(u_boot_console, ut_subtest): |
Heinrich Schuchardt | df6c36c | 2020-05-06 18:26:07 +0200 | [diff] [blame] | 128 | """Execute a "ut" subtest. |
| 129 | |
| 130 | The subtests are collected in function generate_ut_subtest() from linker |
| 131 | generated lists by applying a regular expression to the lines of file |
| 132 | u-boot.sym. The list entries are created using the C macro UNIT_TEST(). |
| 133 | |
| 134 | Strict naming conventions have to be followed to match the regular |
| 135 | expression. Use UNIT_TEST(foo_test_bar, _flags, foo_test) for a test bar in |
| 136 | test suite foo that can be executed via command 'ut foo bar' and is |
| 137 | implemented in C function foo_test_bar(). |
| 138 | |
| 139 | Args: |
| 140 | u_boot_console (ConsoleBase): U-Boot console |
| 141 | ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to |
| 142 | execute command 'ut foo bar' |
| 143 | """ |
Stephen Warren | 770fe17 | 2016-02-08 14:44:16 -0700 | [diff] [blame] | 144 | |
| 145 | output = u_boot_console.run_command('ut ' + ut_subtest) |
| 146 | assert output.endswith('Failures: 0') |