A suggestion that would be the direct way to automate the update, as optee is the whole reason for this additional step. I dont know if you have ever managed a production line, counting minutes each with a cost price.
But as we already have to run and handle 2 YOCTO builds with one line of configuration being different, affecting 10 lines of code - we’ll just put the value of TDX_OPTEE_FS_RPMB_MODE into a file and install it in the image.
Just some quick things to note here. Apologies if these points were already obvious, but I figured I’d write it down just in case:
The device must be booted HAB-closed for RPMB fusing to occur. This is because the HUK is only available after the device is closed, and the HUK is used in key derivation.
Simply testing whether the image on the device is the factory image or the production image won’t tell you if the device is closed or not.
OP-TEE implements a function called plat_rpmb_key_is_ready() to determine whether the device’s HUK is available or not, and will not program the RPMB key if there’s no HUK. This is a safety measure; if you were to write the RPMB key before the device is closed, it would be derived based on the development HUK, which is a fake HUK that is not actually unique to the device.
You will need to reboot after programming the HAB keys and setting the HAB mode to closed. After the reboot the RPMB key will be automatically fused into the eMMC.
It is only after the reboot in HAB-closed mode that you can safely update to the production image. So rather than testing for the TDX_OPTEE_FS_RPMB_MODE, it would be safer to check if the device is HAB-closed, or that the RPMB key has been programmed. You can check HAB status with the hab_status u-boot command, or with linux userspace utilities to read the appropriate fuse, if they are included in your factory image. To test for whether the RPMB key is programed properly from linux space, meta-toradex-security recommends reading the mmc’s RPMB counter, performing a write, and then verifying that the counter has incremented. Note that if this test passes, it also necessarily implies that the device is HAB-closed.
Are you 100% sure the device is actually closed, and not just fused with HAB/AHAB keys? That is, has the final fusing command generated in the fuse-cmds.txt file actually been run, and then the device powercycled? I’m referring to the one at the end of this block:
# After the device successfully boots a signed image without generating
# any HAB events, it is safe to secure, or 'close', the device. This is
# the last step in the process. Once the fuse is blown, the chip does
# not load an image that has not been signed using the correct PKI tree.
# Be careful! This is again a One-Time Programmable e-fuse. Once you
# write it you can't go back, so get it right the first time. If
# anything in the previous steps wasn't done correctly, after writing
# this bit, the SOM will not boot anymore!
fuse prog <ACTUAL FUSE CMD INTENTIONALLY OMITTED>