]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
test: Add sandbox TPM boot measurement
authorEddie James <eajames@linux.ibm.com>
Tue, 24 Oct 2023 15:43:51 +0000 (10:43 -0500)
committerIlias Apalodimas <ilias.apalodimas@linaro.org>
Fri, 27 Oct 2023 10:17:21 +0000 (13:17 +0300)
Use the sandbox TPM driver to measure some boot images in a unit
test case.

Signed-off-by: Eddie James <eajames@linux.ibm.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Acked-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
arch/sandbox/dts/sandbox.dtsi
arch/sandbox/dts/test.dts
configs/sandbox_defconfig
include/test/suites.h
test/boot/Makefile
test/boot/measurement.c [new file with mode: 0644]
test/cmd_ut.c

index ff7e5584c5580c861e9e177e3ea59a2e503e3f5e..241f397ba6e711f6dd9c8c0d58f9bc66127caf04 100644 (file)
@@ -4,11 +4,23 @@
  * and sandbox64 builds.
  */
 
+#include <config.h>
 #include <dt-bindings/input/input.h>
 
 #define USB_CLASS_HUB                  9
 
 / {
+       reserved-memory {
+               #address-cells = <1>;
+               #size-cells = <1>;
+               ranges;
+
+               event_log: tcg_event_log {
+                       no-map;
+                       reg = <(CFG_SYS_SDRAM_SIZE - 0x2000) 0x2000>;
+               };
+       };
+
        binman {
        };
 
 
        tpm2 {
                compatible = "sandbox,tpm2";
+               memory-region = <&event_log>;
        };
 
        triangle {
index 9a863ea732ffdc728cbff6914e61776d978f89a6..bb2ddd9bf29500ba53dd0f5507f553c18852e45a 100644 (file)
@@ -9,6 +9,7 @@
 
 /dts-v1/;
 
+#include <config.h>
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/gpio/sandbox-gpio.h>
 #include <dt-bindings/input/input.h>
                osd0 = "/osd";
        };
 
+       reserved-memory {
+               #address-cells = <1>;
+               #size-cells = <1>;
+               ranges;
+
+               event_log: tcg_event_log {
+                       no-map;
+                       reg = <(CFG_SYS_SDRAM_SIZE - 0x2000) 0x2000>;
+               };
+       };
+
        binman: binman {
        };
 
 
        tpm2 {
                compatible = "sandbox,tpm2";
+               memory-region = <&event_log>;
        };
 
        tpm {
index d667cb9ae47fc4ad2ce9988cff8d481041661a44..6e32649bc7ad587dc1a8d0a3d9970b7e9aa6f4f1 100644 (file)
@@ -348,4 +348,5 @@ CONFIG_TEST_FDTDEC=y
 CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
+CONFIG_MEASURED_BOOT=y
 CONFIG_ARM_FFA_TRANSPORT=y
index 1c7dc65966a1c1be57160f48fbe1af7f4770e2de..48ed549c13f65814fc175f5fb371886b8eb91504 100644 (file)
@@ -45,6 +45,7 @@ int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
+int do_ut_measurement(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc,
index 52947580ae6012b8a2eeb2f34a3a7def42be1dfa..068522cb9e010d3fa09455dcef52fe03443fb8c6 100644 (file)
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_BOOTSTD) += bootdev.o bootstd_common.o bootflow.o bootmeth.o
 obj-$(CONFIG_FIT) += image.o
+obj-$(CONFIG_MEASURED_BOOT) += measurement.o
 
 obj-$(CONFIG_EXPO) += expo.o
 obj-$(CONFIG_CEDIT) += cedit.o
diff --git a/test/boot/measurement.c b/test/boot/measurement.c
new file mode 100644 (file)
index 0000000..9db2ed3
--- /dev/null
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for measured boot functions
+ *
+ * Copyright 2023 IBM Corp.
+ * Written by Eddie James <eajames@linux.ibm.com>
+ */
+
+#include <common.h>
+#include <bootm.h>
+#include <malloc.h>
+#include <test/suites.h>
+#include <test/test.h>
+#include <test/ut.h>
+#include <asm/io.h>
+
+#define MEASUREMENT_TEST(_name, _flags)        \
+       UNIT_TEST(_name, _flags, measurement_test)
+
+static int measure(struct unit_test_state *uts)
+{
+       struct bootm_headers images;
+       const size_t size = 1024;
+       u8 *kernel;
+       u8 *initrd;
+       size_t i;
+
+       kernel = malloc(size);
+       initrd = malloc(size);
+
+       images.os.image_start = map_to_sysmem(kernel);
+       images.os.image_len = size;
+
+       images.rd_start = map_to_sysmem(initrd);
+       images.rd_end = images.rd_start + size;
+
+       images.ft_addr = malloc(size);
+       images.ft_len = size;
+
+       env_set("bootargs", "measurement testing");
+
+       for (i = 0; i < size; ++i) {
+               kernel[i] = 0xf0 | (i & 0xf);
+               initrd[i] = (i & 0xf0) | 0xf;
+               images.ft_addr[i] = i & 0xff;
+       }
+
+       ut_assertok(bootm_measure(&images));
+
+       free(images.ft_addr);
+       free(initrd);
+       free(kernel);
+
+       return 0;
+}
+MEASUREMENT_TEST(measure, 0);
+
+int do_ut_measurement(struct cmd_tbl *cmdtp, int flag, int argc,
+                     char *const argv[])
+{
+       struct unit_test *tests = UNIT_TEST_SUITE_START(measurement_test);
+       const int n_ents = UNIT_TEST_SUITE_COUNT(measurement_test);
+
+       return cmd_ut_category("measurement", "measurement_test_", tests,
+                              n_ents, argc, argv);
+}
index 0f56409e8031449e49109e29793a24deb0432906..e87adcb71e2e3e24b30342f5c83517d2f7dee099 100644 (file)
@@ -99,6 +99,10 @@ static struct cmd_tbl cmd_ut_sub[] = {
 #if CONFIG_IS_ENABLED(UT_UNICODE) && !defined(API_BUILD)
        U_BOOT_CMD_MKENT(unicode, CONFIG_SYS_MAXARGS, 1, do_ut_unicode, "", ""),
 #endif
+#ifdef CONFIG_MEASURED_BOOT
+       U_BOOT_CMD_MKENT(measurement, CONFIG_SYS_MAXARGS, 1, do_ut_measurement,
+                        "", ""),
+#endif
 #ifdef CONFIG_SANDBOX
        U_BOOT_CMD_MKENT(compression, CONFIG_SYS_MAXARGS, 1, do_ut_compression,
                         "", ""),