Hi once again. I've done some more tests to see exactly what causes the issue to stop happening.
By default, I'm unable to authenticate encrypted files from U-Boot due to a CAAM error in the HAB. However, with these following changes in the imx-atf and imx-optee-os repos, I can get the authentication working (albeit with the CAAM being unusable in regular context, such as DEK blob creation).
imx-atf:
diff --git a/plat/imx/imx8m/imx8m_caam.c b/plat/imx/imx8m/imx8m_caam.c
index 478005e2e..aaeb00103 100644
--- a/plat/imx/imx8m/imx8m_caam.c
+++ b/plat/imx/imx8m/imx8m_caam.c
@@ -20,7 +20,7 @@ void imx8m_caam_init(void)
mmio_write_32(SM_CMD, sm_cmd);
/* config CAAM JRaMID set MID to Cortex A */
- mmio_write_32(CAAM_JR0MID, CAAM_NS_MID);
+ //mmio_write_32(CAAM_JR0MID, CAAM_NS_MID);
mmio_write_32(CAAM_JR1MID, CAAM_NS_MID);
mmio_write_32(CAAM_JR2MID, CAAM_NS_MID);
imx-optee-os:
diff --git a/core/drivers/crypto/caam/hal/common/hal_cfg.c b/core/drivers/crypto/caam/hal/common/hal_cfg.c
index 9ed36c52..8122bedf 100644
--- a/core/drivers/crypto/caam/hal/common/hal_cfg.c
+++ b/core/drivers/crypto/caam/hal/common/hal_cfg.c
@@ -97,9 +97,10 @@ void caam_hal_cfg_common_setup_nsjobring(struct caam_jrcfg *jrcfg)
if (jr_offset == jrcfg->offset)
continue;
#endif
- status = caam_hal_jr_setowner(jrcfg->base, jr_offset,
- JROWN_ARM_NS);
+ /* Skip the configuration of JR0 */
+ if (jrnum != 1)
+ status = caam_hal_jr_setowner(jrcfg->base, jr_offset,
+ JROWN_ARM_NS);
if (status == CAAM_NO_ERROR)
caam_hal_jr_prepare_backup(jrcfg->base, jr_offset);
}
Note that both changes are needed for the authentication to work, if only one of the patches is applied it will either hang or generate a HAB event.
Here's a rundown of the workflow I can observe when these patches aren't applied:
- The closed device successfully authenticates and boots an encrypted SPL U-Boot image. The CAAM's JR0MID register (offset 0x10) is configured either by the ROM code or by default with a value of 0x8011 (or at least that's the value I'm reading from the register while the SPL is running). From what I understand, this value means that JR0 is owned by the secure ARM and also that it's owned by Trustzone (bit JRxDID_MS_TZ_OWN in imx-optee-os).
- The SPL authenticates the encrypted FIT image successfully and jumps to the ATF. The ATF then writes the JR0MID register with a value of 0x1 (which means that JR0 is now owned by the non-secure Cortex A).
- The ATF jumps to the OP-TEE, which configures the JR0MID register once again with a new value of 0x80001 (JRxDID_MS_PRIM_CID and JRxDID_MS_PRIM_ICID bits representing the non-secure owner)
- U-Boot is launched, and any attempt at using the HAB to authenticate an encrypted file will result in a CAAM error. The rest of the CAAM functionality works fine.
When the patches are applied:
- Same as before
- The ATF no longer writes to the JR0MID register, causing its value to remain as 0x8011
- The OP-TEE no longer writes to the JR0MID register, so it still contains 0x8011
- With U-Boot launch, any regular usage of the CAAM doesn't work anymore, but if you use the HAB to authenticate an encrypted file, it will work.
To try to solve this, I tried momentarily changing the value of the JR0MID register before using the HAB to authenticate an encrypted file. If I try to modify the register directly from U-Boot, the new value doesn't persist (I write 0x8011 but the register contains 0x1). I implemented a custom PTA in OP-TEE to write the register from a more "trusted" context. The value of 0x8011 is written correctly, but the HAB authentication still doesn't work. I also tried de-allocating the 0 partition, writing the JR0MID register, re-allocating the partition and allocating pages 0 and 1 to said partition, but the result is still the same (even when done from OP-TEE via the PTA).
I get the feeling that once the JR0MID register is configured for non-secure world from the ATF, there's no going back to the secure configuration... is this true? Or is there a way to momentarily change the CAAM configuration so it can be used by the HAB once U-Boot has loaded?
I feel like I've run out of ideas and that the system is designed so that the HAB cannot decrypt any data once in non-secure world. In this case, we will have to drop the feature for the modules based on the i.MX8M family.
Best regards,
Gabriel