Enable CONFIG_TPM_AUTOSTART on sandbox and add two tests which call
tpm_autostart_init(): one checks that the first TPM is started, the
other that a missing TPM is skipped silently. Both check that nothing
is printed.

Add the new event spy to test_event_dump.py, which lists every spy in
the sandbox image.

Signed-off-by: Michael Haener <[email protected]>
---
Changes in v4:
- Call tpm_autostart_init() instead of sending EVT_LAST_STAGE_INIT
- Use the first TPM, as the handler does, and check that it is the v2 one
- Return -EAGAIN when CONFIG_TPM_AUTOSTART is disabled
- Check with UTF_CONSOLE that nothing is printed
- Add the new spy to test_event_dump.py

Changes in v3:
- New patch, split out of patch 1

 configs/sandbox_defconfig        |  1 +
 test/dm/tpm.c                    | 47 ++++++++++++++++++++++++++++++++
 test/py/tests/test_event_dump.py |  1 +
 3 files changed, 49 insertions(+)

diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index a2708e6fb33..3efbdc6fdf1 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -400,6 +400,7 @@ CONFIG_ECDSA=y
 CONFIG_ECDSA_VERIFY=y
 CONFIG_RSASSA_PSS=y
 CONFIG_TPM=y
+CONFIG_TPM_AUTOSTART=y
 CONFIG_ERRNO_STR=y
 CONFIG_GETOPT=y
 CONFIG_TEST_FDTDEC=y
diff --git a/test/dm/tpm.c b/test/dm/tpm.c
index 87c5c416daa..3969711bc3c 100644
--- a/test/dm/tpm.c
+++ b/test/dm/tpm.c
@@ -6,7 +6,9 @@
 
 #include <dm.h>
 #include <tpm_api.h>
+#include <dm/device-internal.h>
 #include <dm/test.h>
+#include <dm/uclass-internal.h>
 #include <test/test.h>
 #include <test/ut.h>
 
@@ -197,3 +199,48 @@ static int dm_test_tpm_autostart_reinit(struct 
unit_test_state *uts)
        return 0;
 }
 DM_TEST(dm_test_tpm_autostart_reinit, UTF_SCAN_FDT);
+
+/* Check that tpm_autostart_init() starts the first TPM */
+static int dm_test_tpm_autostart_init(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+       char buf[50];
+
+       if (!IS_ENABLED(CONFIG_TPM_AUTOSTART))
+               return -EAGAIN;
+
+       /* tpm_autostart_init() uses the first TPM, which is the v2 one */
+       ut_assertok(uclass_first_device_err(UCLASS_TPM, &dev));
+       ut_asserteq(TPM_V2, tpm_get_version(dev));
+
+       ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
+       ut_asserteq_str("init_done=0", buf);
+
+       ut_assertok(tpm_autostart_init());
+       ut_assert_console_end();
+
+       ut_assert(tpm_report_state(dev, buf, sizeof(buf)));
+       ut_asserteq_str("init_done=1", buf);
+
+       return 0;
+}
+DM_TEST(dm_test_tpm_autostart_init, UTF_SCAN_FDT | UTF_CONSOLE);
+
+/* Check that a missing TPM is skipped silently */
+static int dm_test_tpm_autostart_init_missing(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+
+       if (!IS_ENABLED(CONFIG_TPM_AUTOSTART))
+               return -EAGAIN;
+
+       while (!uclass_find_first_device(UCLASS_TPM, &dev) && dev)
+               ut_assertok(device_unbind(dev));
+
+       ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_TPM, &dev));
+       ut_assertok(tpm_autostart_init());
+       ut_assert_console_end();
+
+       return 0;
+}
+DM_TEST(dm_test_tpm_autostart_init_missing, UTF_SCAN_FDT | UTF_CONSOLE);
diff --git a/test/py/tests/test_event_dump.py b/test/py/tests/test_event_dump.py
index b9d48f54dc2..4b43f65908f 100644
--- a/test/py/tests/test_event_dump.py
+++ b/test/py/tests/test_event_dump.py
@@ -20,6 +20,7 @@ EVT_FT_FIXUP          bootmeth_vbe_simple_ft_fixup    
.*boot/vbe_simple_os.c:.*
 EVT_LAST_STAGE_INIT   alloc_write_acpi_tables         
.*lib/acpi/acpi_table.c:.*
 EVT_LAST_STAGE_INIT   efi_block_device_create         
.*lib/efi_driver/efi_block_device.c:.*
 EVT_LAST_STAGE_INIT   install_smbios_table            
.*lib/efi_loader/efi_smbios.c:.*
+EVT_LAST_STAGE_INIT   tpm_evt_autostart               .*lib/tpm_autostart.c:.*
 EVT_MISC_INIT_F       sandbox_early_getopt_check      
.*arch/sandbox/cpu/start.c:.*
 EVT_TEST              h_adder_simple                  .*test/common/event.c:'''
     assert re.match(expect, out, re.MULTILINE) is not None
-- 
2.55.0

Reply via email to