]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
test: efi: boot: Set up an image suitable for EFI testing
authorSimon Glass <sjg@chromium.org>
Thu, 7 Nov 2024 21:31:49 +0000 (14:31 -0700)
committerHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Sat, 9 Nov 2024 09:01:47 +0000 (10:01 +0100)
Create a new disk for use with tests, which contains the new 'testapp'
EFI app specifically intended for testing the EFI loader.

Attach it to the USB device, since most testing is currently done with
mmc.

Initially this image will be used to test the EFI bootmeth.

Fix a stale comment in prep_mmc_bootdev() while we are here.

For now this uses sudo and a compressed fallback file, like all the
other bootstd tests. Once this series is in, the patch which moves
this to use user-space tools will be cleaned up and re-submitted.

Signed-off-by: Simon Glass <sjg@chromium.org>
arch/sandbox/dts/test.dts
test/boot/bootdev.c
test/boot/bootflow.c
test/py/tests/bootstd/flash1.img.xz [new file with mode: 0644]
test/py/tests/test_ut.py

index 3017b33d67b1de72243b442fc180b434ab93a14a..dee280184b1bb8fc9010fa4c72aead6dbec8f1a5 100644 (file)
                                flash-stick@1 {
                                        reg = <1>;
                                        compatible = "sandbox,usb-flash";
-                                       sandbox,filepath = "testflash1.bin";
+                                       sandbox,filepath = "flash1.img";
                                };
 
                                flash-stick@2 {
index 369c611d924b5c07378bdcc8ad97f929beb76c02..8c44afd929710845737eae75b5bbee139ec908ab 100644 (file)
@@ -221,6 +221,10 @@ static int bootdev_test_order(struct unit_test_state *uts)
        /* Use the environment variable to override it */
        ut_assertok(env_set("boot_targets", "mmc1 mmc2 usb"));
        ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
+
+       /* get the usb device which has a backing file (flash1.img) */
+       ut_asserteq(0, bootflow_scan_next(&iter, &bflow));
+
        ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
        ut_asserteq(5, iter.num_devs);
        ut_asserteq_str("mmc1.bootdev", iter.dev_used[0]->name);
@@ -260,7 +264,11 @@ static int bootdev_test_order(struct unit_test_state *uts)
        ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
        ut_asserteq(2, iter.num_devs);
 
-       /* Now scan past mmc1 and make sure that the 3 USB devices show up */
+       /*
+        * Now scan past mmc1 and make sure that the 3 USB devices show up. The
+        * first one has a backing file so returns success
+        */
+       ut_asserteq(0, bootflow_scan_next(&iter, &bflow));
        ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
        ut_asserteq(6, iter.num_devs);
        ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
@@ -322,6 +330,10 @@ static int bootdev_test_prio(struct unit_test_state *uts)
 
        /* 3 MMC and 3 USB bootdevs: MMC should come before USB */
        ut_assertok(bootflow_scan_first(NULL, NULL, &iter, 0, &bflow));
+
+       /* get the usb device which has a backing file (flash1.img) */
+       ut_asserteq(0, bootflow_scan_next(&iter, &bflow));
+
        ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
        ut_asserteq(6, iter.num_devs);
        ut_asserteq_str("mmc2.bootdev", iter.dev_used[0]->name);
@@ -339,6 +351,10 @@ static int bootdev_test_prio(struct unit_test_state *uts)
        bootflow_iter_uninit(&iter);
        ut_assertok(bootflow_scan_first(NULL, NULL, &iter, BOOTFLOWIF_HUNT,
                                        &bflow));
+
+       /* get the usb device which has a backing file (flash1.img) */
+       ut_asserteq(0, bootflow_scan_next(&iter, &bflow));
+
        ut_asserteq(-ENODEV, bootflow_scan_next(&iter, &bflow));
        ut_asserteq(7, iter.num_devs);
        ut_asserteq_str("usb_mass_storage.lun0.bootdev",
index 372bbab6b8db3b7b672e1153dbcde12fb83f9eaa..ac548e78cebed98baf7d73948b4b55c08aeb056d 100644 (file)
@@ -534,7 +534,7 @@ static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev,
 
        order[2] = mmc_dev;
 
-       /* Enable the mmc4 node since we need a second bootflow */
+       /* Enable the requested mmc node since we need a second bootflow */
        root = oftree_root(oftree_default());
        node = ofnode_find_subnode(root, mmc_dev);
        ut_assert(ofnode_valid(node));
diff --git a/test/py/tests/bootstd/flash1.img.xz b/test/py/tests/bootstd/flash1.img.xz
new file mode 100644 (file)
index 0000000..29b78c6
Binary files /dev/null and b/test/py/tests/bootstd/flash1.img.xz differ
index 9166c8f6b6e4fb5a1935452872f4000eda28ea57..6d44191976bbe72f777373044d70bdbfc7efd841 100644 (file)
@@ -28,21 +28,22 @@ def mkdir_cond(dirname):
     if not os.path.exists(dirname):
         os.mkdir(dirname)
 
-def setup_image(cons, mmc_dev, part_type, second_part=False):
+def setup_image(cons, devnum, part_type, second_part=False, basename='mmc'):
     """Create a 20MB disk image with a single partition
 
     Args:
         cons (ConsoleBase): Console to use
-        mmc_dev (int): MMC device number to use, e.g. 1
+        devnum (int): Device number to use, e.g. 1
         part_type (int): Partition type, e.g. 0xc for FAT32
         second_part (bool): True to contain a small second partition
+        basename (str): Base name to use in the filename, e.g. 'mmc'
 
     Returns:
         tuple:
             str: Filename of MMC image
             str: Directory name of 'mnt' directory
     """
-    fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img')
+    fname = os.path.join(cons.config.source_dir, f'{basename}{devnum}.img')
     mnt = os.path.join(cons.config.persistent_data_dir, 'mnt')
     mkdir_cond(mnt)
 
@@ -78,16 +79,17 @@ def mount_image(cons, fname, mnt, fstype):
     u_boot_utils.run_and_log(cons, f'sudo chown {getpass.getuser()} {mnt}')
     return loop
 
-def copy_prepared_image(cons, mmc_dev, fname):
+def copy_prepared_image(cons, devnum, fname, basename='mmc'):
     """Use a prepared image since we cannot create one
 
     Args:
         cons (ConsoleBase): Console touse
-        mmc_dev (int): MMC device number
+        devnum (int): device number
         fname (str): Filename of MMC image
+        basename (str): Base name to use in the filename, e.g. 'mmc'
     """
     infname = os.path.join(cons.config.source_dir,
-                           f'test/py/tests/bootstd/mmc{mmc_dev}.img.xz')
+                           f'test/py/tests/bootstd/{basename}{devnum}.img.xz')
     u_boot_utils.run_and_log(cons, ['sh', '-c', f'xz -dc {infname} >{fname}'])
 
 def setup_bootmenu_image(cons):
@@ -547,6 +549,44 @@ def test_ut_dm_init(u_boot_console):
     with open(fn, 'wb') as fh:
         fh.write(data)
 
+
+def setup_efi_image(cons):
+    """Create a 20MB disk image with an EFI app on it"""
+    devnum = 1
+    basename = 'flash'
+    fname, mnt = setup_image(cons, devnum, 0xc, second_part=True,
+                             basename=basename)
+
+    loop = None
+    mounted = False
+    complete = False
+    try:
+        loop = mount_image(cons, fname, mnt, 'ext4')
+        mounted = True
+        efi_dir = os.path.join(mnt, 'EFI')
+        mkdir_cond(efi_dir)
+        bootdir = os.path.join(efi_dir, 'BOOT')
+        mkdir_cond(bootdir)
+        efi_src = os.path.join(cons.config.build_dir,
+                               f'lib/efi_loader/testapp.efi')
+        efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI')
+        with open(efi_src, 'rb') as inf:
+            with open(efi_dst, 'wb') as outf:
+                outf.write(inf.read())
+        complete = True
+    except ValueError as exc:
+        print(f'Falled to create image, failing back to prepared copy: {exc}')
+
+    finally:
+        if mounted:
+            u_boot_utils.run_and_log(cons, 'sudo umount --lazy %s' % mnt)
+        if loop:
+            u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop)
+
+    if not complete:
+        copy_prepared_image(cons, devnum, fname, basename)
+
+
 @pytest.mark.buildconfigspec('cmd_bootflow')
 @pytest.mark.buildconfigspec('sandbox')
 def test_ut_dm_init_bootstd(u_boot_console):
@@ -557,6 +597,7 @@ def test_ut_dm_init_bootstd(u_boot_console):
     setup_cedit_file(u_boot_console)
     setup_cros_image(u_boot_console)
     setup_android_image(u_boot_console)
+    setup_efi_image(u_boot_console)
 
     # Restart so that the new mmc1.img is picked up
     u_boot_console.restart_uboot()