Add a third sandbox fw_images[] entry (image_index 3) that aliases
onto the same DFU alt setting as image_index 1 through a strong
efi_firmware_get_dfu_alt_num() override, plus the matching capsule
GUID and test case that applies it via ESRT and checks the write
landed. Without the override this would fail, since dfu_alt_info has
no alt number 2 (the default image_index - 1) defined.

Signed-off-by: Balaji Selvanathan <[email protected]>
---
 arch/sandbox/dts/sandbox_capsule.dtsi              | 12 ++++++
 board/sandbox/sandbox.c                            | 18 +++++++++
 include/sandbox_efi_capsule.h                      |  1 +
 .../test_efi_capsule/test_capsule_firmware_raw.py  | 45 ++++++++++++++++++++++
 4 files changed, 76 insertions(+)

diff --git a/arch/sandbox/dts/sandbox_capsule.dtsi 
b/arch/sandbox/dts/sandbox_capsule.dtsi
index 34d29916b30..e5464df1715 100644
--- a/arch/sandbox/dts/sandbox_capsule.dtsi
+++ b/arch/sandbox/dts/sandbox_capsule.dtsi
@@ -166,4 +166,16 @@
                        };
                };
        };
+
+       capsule12 {
+               filename = "Test06";
+               efi-capsule {
+                       image-index = <0x3>;
+                       image-guid = SANDBOX_ALTIMG_IMAGE_GUID;
+
+                       text {
+                               text = "u-boot:New";
+                       };
+               };
+       };
 };
diff --git a/board/sandbox/sandbox.c b/board/sandbox/sandbox.c
index 13006a0ffc2..8ae7c244a09 100644
--- a/board/sandbox/sandbox.c
+++ b/board/sandbox/sandbox.c
@@ -42,6 +42,16 @@ struct efi_fw_image fw_images[] = {
                .fw_name = u"SANDBOX-UBOOT-ENV",
                .image_index = 2,
        },
+       {
+               /*
+                * Aliases image_index 3 onto the same DFU alt setting (0) as
+                * SANDBOX-UBOOT above, to exercise the
+                * efi_firmware_get_dfu_alt_num() override below and prove it
+                * is consulted instead of the default (image_index - 1 = 2).
+                */
+               .fw_name = u"SANDBOX-ALTIMG",
+               .image_index = 3,
+       },
 #elif defined(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)
        {
                .fw_name = u"SANDBOX-FIT",
@@ -57,6 +67,14 @@ struct efi_capsule_update_info update_info = {
        .images = fw_images,
 };
 
+u8 efi_firmware_get_dfu_alt_num(u8 image_index)
+{
+       if (image_index == 3)
+               return 0;
+
+       return image_index - 1;
+}
+
 #endif /* EFI_HAVE_CAPSULE_SUPPORT */
 
 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
diff --git a/include/sandbox_efi_capsule.h b/include/sandbox_efi_capsule.h
index 84d45ec5cfd..848f1bdc4aa 100644
--- a/include/sandbox_efi_capsule.h
+++ b/include/sandbox_efi_capsule.h
@@ -10,6 +10,7 @@
 #define SANDBOX_UBOOT_ENV_IMAGE_GUID   "9e339473-c2eb-530a-a69b-0cd6bbbed40e"
 #define SANDBOX_FIT_IMAGE_GUID         "46610520-469e-59dc-a8dd-c11832b877ea"
 #define SANDBOX_INCORRECT_GUID         "058b7d83-50d5-4c47-a195-60d86ad341c4"
+#define SANDBOX_ALTIMG_IMAGE_GUID      "2d137324-092d-5f04-a62c-a7b2442cb0ee"
 
 #define UBOOT_FIT_IMAGE                        "u-boot_bin_env.itb"
 
diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py 
b/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
index b8cb483b380..6c1cc3c6cc8 100644
--- a/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
+++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
@@ -238,3 +238,48 @@ class TestEfiCapsuleFirmwareRaw:
             check_file_removed(ubman, disk_img, capsule_files)
 
             verify_content(ubman, '100000', 'u-boot:Old')
+
+    def test_efi_capsule_fw6(
+            self, u_boot_config, ubman, efi_capsule_data):
+        """ Test Case 6
+        Update U-Boot on SPI Flash via an image_index whose DFU alt number
+        is resolved through board/sandbox/sandbox.c's
+        efi_firmware_get_dfu_alt_num() override rather than the default
+        (image_index - 1), proving the override is honored.
+        0x100000-0x150000: U-Boot binary (but dummy)
+        """
+        disk_img = efi_capsule_data
+        capsule_files = ['Test06']
+        with ubman.log.section('Test Case 6-a, before reboot'):
+            capsule_setup(ubman, disk_img, '0x0000000000000004')
+            init_content(ubman, '100000', 'u-boot.bin.old', 'Old')
+            place_capsule_file(ubman, capsule_files)
+
+        capsule_early = u_boot_config.buildconfig.get(
+            'config_efi_capsule_on_disk_early')
+        capsule_auth = u_boot_config.buildconfig.get(
+            'config_efi_capsule_authenticate')
+
+        # reboot
+        ubman.restart_uboot(expect_reset = capsule_early)
+
+        with ubman.log.section('Test Case 6-b, after reboot'):
+            if not capsule_early:
+                exec_manual_update(ubman, disk_img, capsule_files)
+
+            # make sure the dfu_alt_info exists because it is required for 
making ESRT.
+            output = ubman.run_command_list([
+                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 
0x50000;u-boot-env raw 0x150000 0x200000"',
+                'efidebug capsule esrt'])
+
+            # ensure that SANDBOX_ALTIMG_IMAGE_GUID is in the ESRT.
+            assert '2D137324-092D-5F04-A62C-A7B2442CB0EE' in ''.join(output)
+
+            check_file_removed(ubman, disk_img, capsule_files)
+
+            # the override maps image_index 3 to dfu_alt_num 0, the same
+            # alt as SANDBOX-UBOOT (image_index 1). Without the override,
+            # the default (image_index - 1 = 2) does not exist in
+            # dfu_alt_info and the write would fail, leaving content 'Old'.
+            expected = 'u-boot:Old' if capsule_auth else 'u-boot:New'
+            verify_content(ubman, '100000', expected)

-- 
2.34.1

Reply via email to