Skip to contentSkip to conversation

fdtdec: protect against another NULL phandlep in fdtdec_add_reserved_memory()

PATCH New Heiko Stuebner 3 participants 13 messages Oct 23, 2019 – Nov 14, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/4 fdtdec: protect against another NULL phandlep in fdtdec_add_reserved_memory()

From: Heiko Stuebner <[email protected]>

The change adding fdtdec_add_reserved_memory() already protected the added phandle against the phandlep being NULL - making the phandlep var optional.

But in the early code checking for an already existing carveout this check was not done and thus the phandle assignment could run into trouble, so add a check there as well, which makes the function still return successfully if a matching region is found, even though no-one wants to work with the phandle.

Fixes c9222a08b3f7 ("fdtdec: Implement fdtdec_add_reserved_memory()")

---
changes in v2:
- add comment about phandlep allowed to be NULL

 include/fdtdec.h | 1 +
 lib/fdtdec.c     | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/fdtdec.h +1 -0
index 635f53083b..54509a25ad 100644
@@ -1078,6 +1078,7 @@ static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle)
  * @param basename	base name of the node to create
  * @param carveout	information about the carveout region
  * @param phandlep	return location for the phandle of the carveout region
+ *			can be NULL
  * @return 0 on success or a negative error code on failure
  */
 int fdtdec_add_reserved_memory(void *blob, const char *basename,
lib/fdtdec.c +2 -1
index 74430c8b2f..d7c3684145 100644
@@ -1363,7 +1363,8 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
 		}
 
 		if (addr == carveout->start && (addr + size) == carveout->end) {
-			*phandlep = fdt_get_phandle(blob, node);
+			if (phandlep)
+				*phandlep = fdt_get_phandle(blob, node);
 			return 0;
 		}
 	}
Signature · 1 line
2.23.0
#1 lore raw

PATCH v3 1/4 fdtdec: protect against another NULL phandlep in fdtdec_add_reserved_memory()

Am Mittwoch, 23. Oktober 2019, 16:46:38 CET schrieb Heiko Stuebner:

Quoted 5 lines

From: Heiko Stuebner <[email protected]>

The change adding fdtdec_add_reserved_memory() already protected the added phandle against the phandlep being NULL - making the phandlep var optional.

Quoted 8 lines

But in the early code checking for an already existing carveout this check was not done and thus the phandle assignment could run into trouble, so add a check there as well, which makes the function still return successfully if a matching region is found, even though no-one wants to work with the phandle.

Fixes c9222a08b3f7 ("fdtdec: Implement fdtdec_add_reserved_memory()")

this patch already received in v2 a

#2 lore raw

PATCH v3 1/4 fdtdec: protect against another NULL phandlep in fdtdec_add_reserved_memory()

Am Mittwoch, 23. Oktober 2019, 16:46:38 CET schrieb Heiko Stuebner:

Quoted 5 lines

From: Heiko Stuebner <[email protected]>

The change adding fdtdec_add_reserved_memory() already protected the added phandle against the phandlep being NULL - making the phandlep var optional.

Quoted 8 lines

But in the early code checking for an already existing carveout this check was not done and thus the phandle assignment could run into trouble, so add a check there as well, which makes the function still return successfully if a matching region is found, even though no-one wants to work with the phandle.

Fixes c9222a08b3f7 ("fdtdec: Implement fdtdec_add_reserved_memory()")

this patch already received in v2 a

Applied to u-boot-dm, thanks!

#3 lore raw

PATCH v3 2/4 fdtdec: only create phandle if caller wants it in fdtdec_add_reserved_memory()

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

The phandlep pointer returning the phandle to the caller is optional and if it is not set when calling fdtdec_add_reserved_memory() it is highly likely that the caller is not interested in a phandle to the created reserved-memory area and really just wants that area added.

So just don't create a phandle in that case.

---
changes in v2:
- update function comment

 include/fdtdec.h |  2 +-
 lib/fdtdec.c     | 16 +++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)
include/fdtdec.h +1 -1
index 54509a25ad..fe8e7770ec 100644
@@ -1078,7 +1078,7 @@ static inline int fdtdec_set_phandle(void *blob, int node, uint32_t phandle)
  * @param basename	base name of the node to create
  * @param carveout	information about the carveout region
  * @param phandlep	return location for the phandle of the carveout region
- *			can be NULL
+ *			can be NULL if no phandle should be added
  * @return 0 on success or a negative error code on failure
  */
 int fdtdec_add_reserved_memory(void *blob, const char *basename,
lib/fdtdec.c +9 -7
index d7c3684145..c841aafa2a 100644
@@ -1393,13 +1393,15 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
 	if (node < 0)
 		return node;
 
-	err = fdt_generate_phandle(blob, &phandle);
-	if (err < 0)
-		return err;
-
-	err = fdtdec_set_phandle(blob, node, phandle);
-	if (err < 0)
-		return err;
+	if (phandlep) {
+		err = fdt_generate_phandle(blob, &phandle);
+		if (err < 0)
+			return err;
+
+		err = fdtdec_set_phandle(blob, node, phandle);
+		if (err < 0)
+			return err;
+	}
 
 	/* store one or two address cells */
 	if (na > 1)
Signature · 1 line
2.23.0
#4 lore raw

PATCH v3 2/4 fdtdec: only create phandle if caller wants it in fdtdec_add_reserved_memory()

Am Mittwoch, 23. Oktober 2019, 16:46:39 CET schrieb Heiko Stuebner:

Quoted 10 lines

From: Heiko Stuebner <[email protected]>

The phandlep pointer returning the phandle to the caller is optional and if it is not set when calling fdtdec_add_reserved_memory() it is highly likely that the caller is not interested in a phandle to the created reserved-memory area and really just wants that area added.

So just don't create a phandle in that case.

this patch already received in v2 a

#5 lore raw

PATCH v3 2/4 fdtdec: only create phandle if caller wants it in fdtdec_add_reserved_memory()

Am Mittwoch, 23. Oktober 2019, 16:46:39 CET schrieb Heiko Stuebner:

Quoted 10 lines

From: Heiko Stuebner <[email protected]>

The phandlep pointer returning the phandle to the caller is optional and if it is not set when calling fdtdec_add_reserved_memory() it is highly likely that the caller is not interested in a phandle to the created reserved-memory area and really just wants that area added.

So just don't create a phandle in that case.

this patch already received in v2 a

Applied to u-boot-dm, thanks!

#6 lore raw

PATCH v3 2/4 fdtdec: only create phandle if caller wants it in fdtdec_add_reserved_memory()

Am Mittwoch, 23. Oktober 2019, 16:46:39 CET schrieb Heiko Stuebner:

Quoted 10 lines

From: Heiko Stuebner <[email protected]>

The phandlep pointer returning the phandle to the caller is optional and if it is not set when calling fdtdec_add_reserved_memory() it is highly likely that the caller is not interested in a phandle to the created reserved-memory area and really just wants that area added.

So just don't create a phandle in that case.

this patch already received in v2 a

Applied to u-boot-dm, thanks!
Applied to u-boot-dm, thanks!

#7 lore raw

PATCH v3 3/4 image: fdt: copy possible optee nodes to a loaded devicetree

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

The loading convention for optee or any other tee on arm64 is as bl32 parameter to the trusted-firmware. So TF-A gets invoked with the TEE as bl32 and main u-boot as bl33. Once it has done its startup TF-A jumps into the bl32 for the TEE startup, returns to TF-A and then jumps to bl33.

All of them get passed a devicetree as parameter and all components often get loaded from a FIT image.

OP-TEE will create additional nodes in that devicetree namely a firmware node and possibly multiple reserved-memory nodes.

While this devicetree is used in main u-boot, in most cases it won't be the one passed to the actual kernel. Instead most boot commands will load a new devicetree from somewhere like mass storage of the network, so if that happens u-boot should transfer the optee nodes to that new devicetree.

To make that happen introduce optee_copy_fdt_nodes() called from the dt setup function in image-fdt which after checking for the optee presence in the u-boot dt will make sure a optee node is present in the kernel dt and transfer any reserved-memory regions it can find.

---
changes in v2:
- don't create a new optee firmware-node, but instead copy the compatible+method properties from the old fdt blob.
changes in v3:
- move check for pre-existing optee node into main function If the kernel-dts already contains optee nodes, we probably want to leave it _completely_ alone

 common/image-fdt.c  |   8 +++
 include/tee/optee.h |   9 +++
 lib/optee/optee.c   | 140 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 157 insertions(+)
common/image-fdt.c +8 -0
index 4247dcee0c..48388488d9 100644
@@ -17,6 +17,7 @@
 #include <linux/libfdt.h>
 #include <mapmem.h>
 #include <asm/io.h>
+#include <tee/optee.h>
 
 #ifndef CONFIG_SYS_FDT_PAD
 #define CONFIG_SYS_FDT_PAD 0x3000
@@ -561,6 +562,13 @@ int image_setup_libfdt(bootm_headers_t *images, void *blob,
 		}
 	}
 
+	fdt_ret = optee_copy_fdt_nodes(gd->fdt_blob, blob);
+	if (fdt_ret) {
+		printf("ERROR: transfer of optee nodes to new fdt failed: %s\n",
+		       fdt_strerror(fdt_ret));
+		goto err;
+	}
+
 	/* Delete the old LMB reservation */
 	if (lmb)
 		lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
include/tee/optee.h +9 -0
index 9446928fd4..121b30a303 100644
@@ -67,4 +67,13 @@ static inline int optee_verify_bootm_image(unsigned long image_addr,
 }
 #endif
 
+#if defined(CONFIG_OPTEE) && defined(CONFIG_OF_LIBFDT)
+int optee_copy_fdt_nodes(const void *old_blob, void *new_blob);
+#else
+static inline int optee_copy_fdt_nodes(const void *old_blob, void *new_blob)
+{
+	return 0;
+}
+#endif
+
 #endif /* _OPTEE_H */
lib/optee/optee.c +140 -0

149 lines not on this page · raw message

Signature · 1 line
2.23.0
#8 lore raw

PATCH v3 3/4 image: fdt: copy possible optee nodes to a loaded devicetree

Quoted 35 lines · Heiko Stuebner

From: Heiko Stuebner <[email protected]>

The loading convention for optee or any other tee on arm64 is as bl32 parameter to the trusted-firmware. So TF-A gets invoked with the TEE as bl32 and main u-boot as bl33. Once it has done its startup TF-A jumps into the bl32 for the TEE startup, returns to TF-A and then jumps to bl33.

All of them get passed a devicetree as parameter and all components often get loaded from a FIT image.

OP-TEE will create additional nodes in that devicetree namely a firmware node and possibly multiple reserved-memory nodes.

While this devicetree is used in main u-boot, in most cases it won't be the one passed to the actual kernel. Instead most boot commands will load a new devicetree from somewhere like mass storage of the network, so if that happens u-boot should transfer the optee nodes to that new devicetree.

To make that happen introduce optee_copy_fdt_nodes() called from the dt setup function in image-fdt which after checking for the optee presence in the u-boot dt will make sure a optee node is present in the kernel dt and transfer any reserved-memory regions it can find.

---
changes in v2:
- don't create a new optee firmware-node, but instead copy the compatible+method properties from the old fdt blob.
changes in v3:
- move check for pre-existing optee node into main function If the kernel-dts already contains optee nodes, we probably want to leave it _completely_ alone

Quoted 5 lines
 common/image-fdt.c  |   8 +++
 include/tee/optee.h |   9 +++
 lib/optee/optee.c   | 140 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 157 insertions(+)

Applied to u-boot-dm, thanks!

#9 lore raw

PATCH v3 4/4 tests: add OP-TEE test suite

In reply to Heiko Stuebner

From: Heiko Stuebner <[email protected]>

OP-TEE can get supplied with a devicetree and will then insert its firmware node and reserved-memory sections into it. As this devicetree often is not the one supplied to a later loaded kernel, a previous commit added functionality to transfer these nodes onto that new devicetree.

To make sure this functionality stays intact, also add a test for the transfer functionality.

---
changes in v2:
- added test
changes in v3:
- remove unused variables
- added test for copying optee nodes into prepopulated fdt

 Makefile                           |   1 +
 include/test/optee.h               |  14 +++
 include/test/suites.h              |   1 +
 test/Kconfig                       |   1 +
 test/cmd_ut.c                      |   6 ++
 test/optee/Kconfig                 |   7 ++
 test/optee/Makefile                |  13 +++
 test/optee/cmd_ut_optee.c          | 149 +++++++++++++++++++++++++++++
 test/optee/test-optee-base.dts     |  13 +++
 test/optee/test-optee-no-optee.dts |  13 +++
 test/optee/test-optee-optee.dts    |  32 +++++++
 11 files changed, 250 insertions(+)
 create mode 100644 include/test/optee.h
 create mode 100644 test/optee/Kconfig
 create mode 100644 test/optee/Makefile
 create mode 100644 test/optee/cmd_ut_optee.c
 create mode 100644 test/optee/test-optee-base.dts
 create mode 100644 test/optee/test-optee-no-optee.dts
 create mode 100644 test/optee/test-optee-optee.dts
Makefile +1 -0
index 54da5cd51c..dc30dc8d93 100644
@@ -746,6 +746,7 @@ libs-$(CONFIG_API) += api/
 libs-$(CONFIG_HAS_POST) += post/
 libs-$(CONFIG_UNIT_TEST) += test/ test/dm/
 libs-$(CONFIG_UT_ENV) += test/env/
+libs-$(CONFIG_UT_OPTEE) += test/optee/
 libs-$(CONFIG_UT_OVERLAY) += test/overlay/
 
 libs-y += $(if $(BOARDDIR),board/$(BOARDDIR)/)
include/test/optee.h new file +14 -0
new file mode 100644
index 0000000000..a8c6e6395f
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2019, Theobroma Systems Design und Consulting GmbH
+ */
+
+#ifndef __TEST_OPTEE_H__
+#define __TEST_OPTEE_H__
+
+#include <test/test.h>
+
+/* Declare a new environment test */
+#define OPTEE_TEST(_name, _flags)	UNIT_TEST(_name, _flags, optee_test)
+
+#endif /* __TEST_OPTEE_H__ */
include/test/suites.h +1 -0
index 01bee09346..20970f08d6 100644
@@ -28,6 +28,7 @@ int do_ut_compression(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_lib(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+int do_ut_optee(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_unicode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
test/Kconfig +1 -0
index 48a0e501f8..f53629aac5 100644
@@ -33,4 +33,5 @@ config UT_UNICODE
 
 source "test/dm/Kconfig"
 source "test/env/Kconfig"
+source "test/optee/Kconfig"
 source "test/overlay/Kconfig"
test/cmd_ut.c +6 -0
index e3b89504e7..2781f8bd56 100644
@@ -43,6 +43,9 @@ static cmd_tbl_t cmd_ut_sub[] = {
 #if defined(CONFIG_UT_ENV)
 	U_BOOT_CMD_MKENT(env, CONFIG_SYS_MAXARGS, 1, do_ut_env, "", ""),
 #endif
+#ifdef CONFIG_UT_OPTEE
+	U_BOOT_CMD_MKENT(optee, CONFIG_SYS_MAXARGS, 1, do_ut_optee, "", ""),
+#endif
 #ifdef CONFIG_UT_OVERLAY
 	U_BOOT_CMD_MKENT(overlay, CONFIG_SYS_MAXARGS, 1, do_ut_overlay, "", ""),
 #endif
@@ -114,6 +117,9 @@ static char ut_help_text[] =
 #ifdef CONFIG_UT_LIB
 	"ut lib [test-name] - test library functions\n"
 #endif
+#ifdef CONFIG_UT_OPTEE
+	"ut optee [test-name]\n"
+#endif
 #ifdef CONFIG_UT_OVERLAY
 	"ut overlay [test-name]\n"
 #endif
test/optee/Kconfig new file +7 -0
new file mode 100644
index 0000000000..2f6834aa3b
@@ -0,0 +1,7 @@
+config UT_OPTEE
+	bool "Enable OP-TEE Unit Tests"
+	depends on UNIT_TEST && OF_CONTROL && OPTEE
+	default y
+	help
+	  This enables the 'ut optee' command which runs a series of unit
+	  tests on the optee library code..
test/optee/Makefile new file +13 -0
new file mode 100644
index 0000000000..8793fd7ad6
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2019, Theobroma Systems Design und Consulting GmbH
+
+# Test files
+obj-y += cmd_ut_optee.o
+
+DTC_FLAGS += -@
+
+# DT overlays
+obj-y += test-optee-base.dtb.o
+obj-y += test-optee-optee.dtb.o
+obj-y += test-optee-no-optee.dtb.o
test/optee/cmd_ut_optee.c new file +149 -0

149 lines not on this page · raw message

test/optee/test-optee-base.dts new file +13 -0
new file mode 100644
index 0000000000..3c1d0c60ef
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2019, Theobroma Systems Design und Consulting GmbH
+ */
+
+/dts-v1/;
+
+/ {
+	#address-cells = <2>;
+	#size-cells = <2>;
+};
+
+
test/optee/test-optee-no-optee.dts new file +13 -0
new file mode 100644
index 0000000000..3c1d0c60ef
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2019, Theobroma Systems Design und Consulting GmbH
+ */
+
+/dts-v1/;
+
+/ {
+	#address-cells = <2>;
+	#size-cells = <2>;
+};
+
+
test/optee/test-optee-optee.dts new file +32 -0
new file mode 100644
index 0000000000..11e26a2728
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2019, Theobroma Systems Design und Consulting GmbH
+ */
+
+/dts-v1/;
+
+/ {
+	#address-cells = <2>;
+	#size-cells = <2>;
+
+	firmware {
+		optee {
+			compatible = "linaro,optee-tz";
+			method = "smc";
+		};
+	};
+
+	reserved-memory {
+		#address-cells = <2>;
+		#size-cells = <2>;
+		ranges;
+
+		optee_shm at 32000000 {
+			reg = <0x00 0x32000000 0x00 0x400000>;
+		};
+
+		optee_core at 30000000 {
+			reg = <0x00 0x30000000 0x00 0x2000000>;
+		};
+	};
+};
Signature · 1 line
2.23.0
#10 lore raw

PATCH v3 4/4 tests: add OP-TEE test suite

Am Mittwoch, 23. Oktober 2019, 16:46:41 CET schrieb Heiko Stuebner:

Quoted 8 lines

From: Heiko Stuebner <[email protected]>

OP-TEE can get supplied with a devicetree and will then insert its firmware node and reserved-memory sections into it. As this devicetree often is not the one supplied to a later loaded kernel, a previous commit added functionality to transfer these nodes onto that new devicetree.

Quoted 4 lines

To make sure this functionality stays intact, also add a test for the transfer functionality.

this patch already received in v2 a

#11 lore raw

PATCH v3 4/4 tests: add OP-TEE test suite

Am Mittwoch, 23. Oktober 2019, 16:46:41 CET schrieb Heiko Stuebner:

Quoted 8 lines

From: Heiko Stuebner <[email protected]>

OP-TEE can get supplied with a devicetree and will then insert its firmware node and reserved-memory sections into it. As this devicetree often is not the one supplied to a later loaded kernel, a previous commit added functionality to transfer these nodes onto that new devicetree.

Quoted 4 lines

To make sure this functionality stays intact, also add a test for the transfer functionality.

this patch already received in v2 a

Applied to u-boot-dm, thanks!

#12 lore raw

PATCH v3 4/4 tests: add OP-TEE test suite

Am Mittwoch, 23. Oktober 2019, 16:46:41 CET schrieb Heiko Stuebner:

Quoted 8 lines

From: Heiko Stuebner <[email protected]>

OP-TEE can get supplied with a devicetree and will then insert its firmware node and reserved-memory sections into it. As this devicetree often is not the one supplied to a later loaded kernel, a previous commit added functionality to transfer these nodes onto that new devicetree.

Quoted 4 lines

To make sure this functionality stays intact, also add a test for the transfer functionality.

this patch already received in v2 a

Applied to u-boot-dm, thanks!
Applied to u-boot-dm, thanks!