Skip to contentSkip to conversation

imx: deal with the RNG self test of the i.MX6 boot ROM

PATCH Superseded Heiko Schocher 2 participants 8 messages Sep 22 – Sep 28

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover imx: deal with the RNG self test of the i.MX6 boot ROM

The boot ROM of a few i.MX6 revisions runs its RNG self test incorrectly, which leaves a HAB warning behind on every boot and says nothing about the state of the RNG.

The first patch runs the test correctly, before anything else uses the RNG. The second one makes hab_status name the leftover event, so that the warning stops sending people looking for a fault in their own image.

Azure build:
https://dev.azure.com/hs0298/hs/_build/results?buildId=214&view=results
is fine.

Heiko Schocher (2):
crypto: fsl: run the CAAM RNG self test on affected i.MX6 chips
imx: hab: name the event left behind by the boot ROM RNG self test

 arch/arm/include/asm/arch-imx/cpu.h |   2 +
 arch/arm/mach-imx/hab.c             |  16 +++
 drivers/crypto/fsl/Kconfig          |  22 ++++
 drivers/crypto/fsl/Makefile         |   1 +
 drivers/crypto/fsl/jr.c             |   3 +
 drivers/crypto/fsl/jr.h             |   1 +
 drivers/crypto/fsl/rng_self_test.c  | 157 ++++++++++++++++++++++++++++
 7 files changed, 202 insertions(+)
 create mode 100644 drivers/crypto/fsl/rng_self_test.c

---
base-commit: 75df64103873fac09642860cedadaffb8cf6b2c7
branch: hab-rng-v1

Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 crypto: fsl: run the CAAM RNG self test on affected i.MX6 chips

The boot ROM of a few i.MX6 revisions runs the RNG self test incorrectly. It leaves a HAB event behind on every boot:

    STS = HAB_WARNING (0x69)   RSN = HAB_ENG_FAIL (0x30)
    CTX = HAB_CTX_ENTRY (0xE1) ENG = HAB_ENG_CAAM (0x1D)

NXP recommends to run a rng self test before any RNG related crypto implementations are done.

Affected are i.MX6Solo, i.MX6DL and i.MX6SX revision 1.4, i.MX6Q revision 1.6 and i.MX6QP revision 1.1, and no other revision of those chips. Both chip and revision are read at runtime and have to match.

Run this test on affected chips and stop if test fails.

Part of the code is taken from u-boot-imx NXP tree, from the commit linked below, and adapted to mainline.

---
Rework against NXP base:
- adapt the chip detection
- checkpatch cleanup
- Kconfig symbol renamed and set a default

It may be worth discussing whether triggering a panic() and bringing the entire system to a halt is appropriate in this case.

 arch/arm/include/asm/arch-imx/cpu.h |   2 +
 drivers/crypto/fsl/Kconfig          |  22 ++++
 drivers/crypto/fsl/Makefile         |   1 +
 drivers/crypto/fsl/jr.c             |   3 +
 drivers/crypto/fsl/jr.h             |   1 +
 drivers/crypto/fsl/rng_self_test.c  | 157 ++++++++++++++++++++++++++++
 6 files changed, 186 insertions(+)
 create mode 100644 drivers/crypto/fsl/rng_self_test.c
arch/arm/include/asm/arch-imx/cpu.h +2 -0
index bbc4b421a02..f2225df9519 100644
@@ -95,7 +95,9 @@
 #define CHIP_REV_1_1            0x11
 #define CHIP_REV_1_2            0x12
 #define CHIP_REV_1_3            0x13
+#define CHIP_REV_1_4            0x14
 #define CHIP_REV_1_5            0x15
+#define CHIP_REV_1_6            0x16
 #define CHIP_REV_2_0            0x20
 #define CHIP_REV_2_1            0x21
 #define CHIP_REV_2_2            0x22
drivers/crypto/fsl/Kconfig +22 -0
index 244a9bd905d..41de402c530 100644
@@ -100,4 +100,26 @@ config FSL_DCP_RNG
 	  and a Pseudo-Random Number Generator (PRNG) to achieve a true
 	  randomness and cryptographic strength.
 
+config FSL_CAAM_RNG_SELF_TEST
+	bool "Run the CAAM RNG self test on affected i.MX6 chips"
+	depends on FSL_CAAM
+	default y if MX6S || MX6DL || MX6Q || MX6QDL || MX6SX
+	help
+	  The boot ROM of a few i.MX6 revisions runs the RNG self test
+	  incorrectly and leaves a HAB warning behind, which reads
+
+	    STS = HAB_WARNING (0x69)   RSN = HAB_ENG_FAIL (0x30)
+	    CTX = HAB_CTX_ENTRY (0xE1) ENG = HAB_ENG_CAAM (0x1D)
+
+	  The warning itself is harmless and this option does not make it
+	  go away. What it does is run the self test correctly, before
+	  anything else uses the RNG, so that a genuinely broken RNG is
+	  found rather than assumed to be the known warning. The board is
+	  stopped if the test fails.
+
+	  Affected are i.MX6Solo, i.MX6DL and i.MX6SX revision 1.4, i.MX6Q
+	  revision 1.6 and i.MX6QP revision 1.1, and no other revision of
+	  those chips. Chip and revision are read at runtime, so a build
+	  covering several of them behaves correctly on each.
+
 endif
drivers/crypto/fsl/Makefile +1 -0
index 965c4938c8c..14b53db7844 100644
@@ -9,3 +9,4 @@ obj-$(CONFIG_RSA_FREESCALE_EXP) += fsl_rsa.o
 obj-$(CONFIG_$(PHASE_)FSL_CAAM_RNG) += rng.o
 obj-$(CONFIG_FSL_DCP_RNG) += dcp_rng.o
 obj-$(CONFIG_FSL_MFGPROT) += fsl_mfgprot.o
+obj-$(CONFIG_FSL_CAAM_RNG_SELF_TEST) += rng_self_test.o
drivers/crypto/fsl/jr.c +3 -0
index 07d336c074a..3a5032bcaf6 100644
@@ -804,6 +804,9 @@ init:
 	pamu_enable();
 #endif
 
+	if (IS_ENABLED(CONFIG_FSL_CAAM_RNG_SELF_TEST))
+		rng_self_test();
+
 	if (get_rng_vid(caam->sec) >= 4) {
 		if (rng_init(sec_idx, caam->sec) < 0) {
 			printf("SEC%u:  RNG instantiation failed\n", sec_idx);
drivers/crypto/fsl/jr.h +1 -0
index b136cd8d05a..d8bd0797331 100644
@@ -132,4 +132,5 @@ struct caam_regs {
 void caam_jr_strstatus(u32 status);
 int run_descriptor_jr(uint32_t *desc);
 
+void rng_self_test(void);
 #endif
drivers/crypto/fsl/rng_self_test.c new file +157 -0

157 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/2 imx: hab: name the event left behind by the boot ROM RNG self test

In reply to Heiko Schocher

The boot ROM of a few i.MX6 revisions runs its RNG self test incorrectly and logs an event for it on every boot. An event that reads HAB_ENG_FAIL on the CAAM and nothing further sends the reader looking for a fault in their own image, which is not where it is.

Print from where that event comes.

---

 arch/arm/mach-imx/hab.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/arm/mach-imx/hab.c +16 -0
index f92f77670a8..35c497619d6 100644
@@ -421,6 +421,19 @@ static inline u32 get_idx(u8 *list, u8 tgt, u32 size)
 	return idx;
 }
 
+/*
+ * The boot ROM of a few i.MX6 revisions runs the RNG self test
+ * incorrectly and leaves this event behind on every boot. Say so, so
+ * that the reader does not go looking for a fault that is not there.
+ */
+static bool is_rng_self_test_event(struct record *rec)
+{
+	return rec->contents[0] == HAB_WARNING &&
+	       rec->contents[1] == HAB_ENG_FAIL &&
+	       rec->contents[2] == HAB_CTX_ENTRY &&
+	       rec->contents[3] == HAB_ENG_CAAM;
+}
+
 static void process_event_record(uint8_t *event_data, size_t bytes)
 {
 	struct record *rec = (struct record *)event_data;
@@ -433,6 +446,9 @@ static void process_event_record(uint8_t *event_data, size_t bytes)
 	       ARRAY_SIZE(hab_contexts))]);
 	printf("%s", eng_str[get_idx(hab_engines, rec->contents[3],
 	       ARRAY_SIZE(hab_engines))]);
+
+	if (is_rng_self_test_event(rec))
+		puts("\nknown to come from the RNG self test of the boot ROM, harmless\n");
 }
 
 static void display_event(uint8_t *event_data, size_t bytes)
Signature · 1 line
2.55.0
reviewer
#3 lore raw
In reply to Heiko Schocher

Hi Heiko,

Quoted 5 lines · Heiko Schocher

The boot ROM of a few i.MX6 revisions runs its RNG self test incorrectly, which leaves a HAB warning behind on every boot and says nothing about the state of the RNG.

Quoted 8 lines

The first patch runs the test correctly, before anything else uses the RNG. The second one makes hab_status name the leftover event, so that the warning stops sending people looking for a fault in their own image.

Azure build:
https://dev.azure.com/hs0298/hs/_build/results?buildId=214&view=results
is fine.

Not really; Azure reports an error:

+ tools/buildman/buildman -o /tmp -seP mx6 -x engicam,phytec,toradex Summary of current source for 86 boards (2 threads, 1 job per thread)

       arm:  w+   tbs2910 +   imx6dl_mamoj
+spl/u-boot-spl.bin exceeds file size limit:
+  limit:  0xefa0 bytes
+  actual: 0xf091 bytes
+  excess: 0xf1 bytes

+make[1]: *** [Makefile:2453: spl/u-boot-spl.bin] Error 1 +make[1]: *** Deleting file 'spl/u-boot-spl.bin' +make: *** [Makefile:189: __sub-make] Error 2

https://dev.azure.com/hs0298/hs/_build/results?buildId=214&view=logs&j=3024cb51-9d6b-5eff-1dde-683017b794fa&t=58ec3b56-872b-5fc2-ee03-8e48280ded87

I got the same error in GitLab CI.

Please fix and resend.

Thanks

#4 lore raw

Hello Fabio,

Quoted 30 lines · Fabio Estevam

Hi Heiko,

Quoted 5 lines · Heiko Schocher

The boot ROM of a few i.MX6 revisions runs its RNG self test incorrectly, which leaves a HAB warning behind on every boot and says nothing about the state of the RNG.

Quoted 8 lines

The first patch runs the test correctly, before anything else uses the RNG. The second one makes hab_status name the leftover event, so that the warning stops sending people looking for a fault in their own image.

Azure build:
https://dev.azure.com/hs0298/hs/_build/results?buildId=214&view=results
is fine.

Not really; Azure reports an error:

+ tools/buildman/buildman -o /tmp -seP mx6 -x engicam,phytec,toradex Summary of current source for 86 boards (2 threads, 1 job per thread)

        arm:  w+   tbs2910 +   imx6dl_mamoj
(quoted diff) +7 -0
+spl/u-boot-spl.bin exceeds file size limit:
+  limit:  0xefa0 bytes
+  actual: 0xf091 bytes
+  excess: 0xf1 bytes
+make[1]: *** [Makefile:2453: spl/u-boot-spl.bin] Error 1
+make[1]: *** Deleting file 'spl/u-boot-spl.bin'
+make: *** [Makefile:189: __sub-make] Error 2
Quoted 5 lines

https://dev.azure.com/hs0298/hs/_build/results?buildId=214&view=logs&j=3024cb51-9d6b-5eff-1dde-683017b794fa&t=58ec3b56-872b-5fc2-ee03-8e48280ded87

I got the same error in GitLab CI.

Please fix and resend.

Ouch, I missed the gitlab Email... sorry!

Okay, SPL gets to big for board imx6dl_mamoj

What can we do? Simply disable the new Kconfig option for this board?

I added the board Maintainers to cc, may they can help us?

bye,
Heiko

Quoted 3 lines

Thanks

Signature · 5 lines
Nabla Software Engineering
HRB 40522 Augsburg
Phone: +49 821 45592596
E-Mail: [email protected]
Geschäftsführer : Stefano Babic
reviewer
#5 lore raw

Hi Heiko,

Quoted 1 line · Heiko Schocher

Ouch, I missed the gitlab Email... sorry!

No problem.

Quoted 3 lines

Okay, SPL gets to big for board imx6dl_mamoj

What can we do? Simply disable the new Kconfig option for this board?

Does enabling LTO help for this board?

Regards,

Fabio Estevam

#6 lore raw

Hello Fabio,

Quoted 6 lines · Fabio Estevam

Hi Heiko,

Quoted 1 line · Heiko Schocher

Ouch, I missed the gitlab Email... sorry!

Quoted 7 lines

No problem.

Quoted 3 lines

Okay, SPL gets to big for board imx6dl_mamoj

What can we do? Simply disable the new Kconfig option for this board?

Does enabling LTO help for this board?

LTO is already active for that board

configs/imx6dl_mamoj_defconfig
15 CONFIG_LTO=y

builded without it locally, and LTO saves 3328 bytes.

I builded locally with another toolchain (Fedora-provided ARM Linux cross-toolchain), no size problem.

I see only 2 options:

make the new Kconfig option not default on, or disable it for this board in configs/imx6dl_mamoj_defconfig

bye,
Heiko

Quoted 5 lines

Regards,

Fabio Estevam

Signature · 5 lines
Nabla Software Engineering
HRB 40522 Augsburg
Phone: +49 821 45592596
E-Mail: [email protected]
Geschäftsführer : Stefano Babic
reviewer
#7 lore raw

Hi Heiko,

Quoted 7 lines · Heiko Schocher

LTO is already active for that board

configs/imx6dl_mamoj_defconfig
15 CONFIG_LTO=y

builded without it locally, and LTO saves 3328 bytes.

Quoted 7 lines

I builded locally with another toolchain (Fedora-provided ARM Linux cross-toolchain), no size problem.

I see only 2 options:

make the new Kconfig option not default on, or disable it for this board in configs/imx6dl_mamoj_defconfig

Let's disable it for imx6dl_mamoj_defconfig.

Regards,

Fabio Estevam