本帖最后由 小恩GG 于 2021-10-15 10:02 编辑
【经验分享】RT1015 APP BEE加密烧录 一 文档说明 之前写过一篇RT1020使用MCUBootUtility工具实现BEE master key即fixed OTPMKSNVS key方式加密: 在实际使用中,也有很多的客户需要使用flexible User key方式实现BEE加密,并且有些客户还想使用命令行方式,然后用MFGTool工具去下载代码。所以本篇文章,为了大家能够方便使用BEE模式,在MIMXRT1015-EVK的开发板上,使用user key,分别演示使用MCUBootUtility工具以及命令行模式去实现BEE的加密。 本篇文章只做BEE加密,不做HAB签名。
二 相关准备 2.1 工具准备 MCUBootUtility下载: image_enc2.zip下载: 下载后放到NXP-MCUBootUtility-2.3.0\tools\image_enc2\win SDK下载:
2.2 app文件准备 本文以官方SDK_2.8.0_EVK-MIMXRT1015中的iled_blinky MCUXpresso IDE工程为例,生成不带BOOTHEADER的app文件: 图 1 生成evkmimxrt1015_igpio_led_output.s19原文件供后续使用。
三 MCUBootUtility BEE userkey加密 本章使用MCUBootUtility工具实现BEEuser key加密,并且不是能HAB认证。 3.1 MIMXRT1015-EVK原始fuse状态 读取未做加密的fuse状态,为了后续查看对比使用,使用MCUBootUtility工具的efuse operation utility可以把整个fuse map读取出来。 图 2 3.2 MCUbootutility BEE加密配置 图 3 由于本文只做BEE加密,不做HAB认证,所以,选择certificate 为no。 图 4 选择key storage region 为flexible user keys,即可变用户密钥。 这里保护区域只是从地址0X60001000开始,长度为0X2000,并没有把所有的app区域都做加密,这样可以对比生成的BEE加密文件,和原始文件从0X60003000开始,会是明文,前面区域0X60001000开始到0X60002FFF将是密文。 然后点击all in one action烧录代码: 图5 SW_GP2 region可以额外去烧录,点击burnDEK data即可。 烧完后,读取fuse map。 图6 可以看到cfg1已经改变,选择的BEE_KEY0_SEL来源为SW-GP2。 图7 对比BEE烧录后读出的文件和未加密的文件情况: 图8 图9 图10 图11 图12 可以发现,加密后的文件,确实是0X60001000到0X60002FFF被加密了。另外,在0X6000400区域添加了EKIB0, 0X6000480区域添加了EPRDB0。因为没有选择BEE engine1,只选择了BEE engine0,所以 0X60008000开始的EKIB1,EPRDB1区域没有有效数据。 到目前为止,已经实现了MCUBootUtility工具的BEE加密。退出serial download模式,配置MIMXRT1015-EVK板上sw8 1-ON,2-OFF,3-ON,4-OFF,复位开发板,可以看到小灯能够闪烁。
四 命令行模式 由于有些客户需要使用命令行模式实现BEE加密,并且选择MFGTool下载方式,所以本文给出如何使用SDK里SDK_2.8.0_EVK-MIMXRT1015\middleware\mcu-boot\bin\Tools去实现BEE命令行以及image_enc工具加密app,并且使用MFGTool烧录到RT1015中。
4.1 命令行文件准备 准备一个文件夹,把elftosb.exe,image_enc.exe,app文件 evkmimxrt1015_iled_blinky_0x60002000.s19,RemoveBinaryBytes.exe都拷贝进去。 RemoveBinaryBytes.exe是用来修改bin文件的,该文件会在附件中给出。 然后准备如下几个文件: imx-flexspinor-normal-unsigned.bd imxrt1015_app_flash_sb_gen.bd burn_fuse.bd
4.1.1imx-flexspinor-normal-unsigned.bd imx-flexspinor-normal-unsigned.bd 文件用于生成evkmimxrt1015_iled_blinky_0x60002000.s19对应的bootable .bin文件,即带有IVT头的文件: ivt_evkmimxrt1015_iled_blinky_0x60002000.bin ivt_evkmimxrt1015_iled_blinky_0x60002000_nopadding.bin bd内容为: - <p>options {
- flags = 0x00;
- startAddress = 0x60000000;
- ivtOffset = 0x1000;
- initialLoadSize = 0x2000;
- //DCDFilePath = "dcd.bin";
- # Note: This is required if the default entrypoint is not the Reset_Handler
- # Please set the entryPointAddress to Reset_Handler address
- // entryPointAddress = 0x60002000;
- }</p><p>sources {
- elfFile = extern(0);
- }</p><p>section (0)
- {
- }
- </p>
复制代码
4.1.2 imxrt1015_app_flash_sb_gen.bd 该文件用于配置外部QSPIflash,并且实现烧录功能,通常用该文件生成.sb文件,最后用MFGTool去下载.SB文件。 - sources {
- myBinFile = extern (0);
- }
- section (0) {
- load 0xc0000007 > 0x20202000;
- load 0x0 > 0x20202004;
- enable flexspinor 0x20202000;
- erase 0x60000000..0x60005000;
- load 0xf000000f > 0x20203000;
- enable flexspinor 0x20203000;
- load myBinFile > 0x60000400;
- }
复制代码
4.1.3 burn_fuse.bd BEE方式需要修改fuse数据,但是Fuse是只写一次的,所以这里将写fuse的操作分开,第一次BEE没有烧录Fuse的情况下,去烧录对应的fuse,否则后续更改app烧录,就不加fuse烧录步骤。Burn_fuse.bd主要用于配置需要烧录的fuse值到具体fuse区域,然后生成.sb文件,最后用MFGTool和烧录app一并烧录。
- # The source block assign file name to identifiers
- sources {
- }
- constants {
- }
- # !!!!!!!!!!!! WARNING !!!!!!!!!!!!
- # The section block specifies the sequence of boot commands to be written to the SB file
- # Note: this is just a template, please update it to actual values in users' project
- section (0) {
- # program SW_GP2
- load fuse 0x76543210 > 0x29;
- load fuse 0xfedcba98 > 0x2a;
- load fuse 0x89abcdef > 0x2b;
- load fuse 0x01234567 > 0x2c;
-
- # Program BEE_KEY0_SEL
- load fuse 0x00003000 > 0x6;
-
- }
复制代码
4.2 BEE命令行步骤 新建rt1015_bee_userkey_gp2.bat文件,内容为: - elftosb.exe -f imx -V -c imx-flexspinor-normal-unsigned.bd -o ivt_evkmimxrt1015_iled_blinky_0x60002000.bin evkmimxrt1015_iled_blinky_0x60002000.s19
- image_enc.exe hw_eng=bee ifile=ivt_evkmimxrt1015_iled_blinky_0x60002000.bin ofile=evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted.bin base_addr=0x60000000 region0_key=0123456789abcdeffedcba9876543210 region0_arg=1,[0x60001000,0x2000,0] region0_lock=0 use_zero_key=1 is_boot_image=1
- RemoveBinaryBytes.exe evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted.bin evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted_remove1K.bin 1024
- elftosb.exe -f kinetis -V -c program_imxrt1015_qspi_encrypt_sw_gp2.bd -o boot_image_encrypt.sb evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted_remove1K.bin
- elftosb.exe -f kinetis -V -c burn_fuse.bd -o burn_fuse.sb
- pause
复制代码 一共分为五步:
4.2.1 elftosb生成app带IVT文件 elftosb.exe -f imx -V -cimx-flexspinor-normal-unsigned.bd -oivt_evkmimxrt1015_iled_blinky_0x60002000.binevkmimxrt1015_iled_blinky_0x60002000.s19 该命令执行后会产生两个带IVT头的文件:ivt_evkmimxrt1015_iled_blinky_0x60002000.bin,ivt_evkmimxrt1015_iled_blinky_0x60002000_nopadding.bin 这里使用ivt_evkmimxrt1015_iled_blinky_0x60002000.bin
4.2.2 image_enc 生成app对应的BEE加密文件 image_enc.exe hw_eng=beeifile=ivt_evkmimxrt1015_iled_blinky_0x60002000.binofile=evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted.binbase_addr=0x60000000 region0_key=0123456789abcdeffedcba9876543210region0_arg=1,[0x60001000,0x2000,0] region0_lock=0 use_zero_key=1is_boot_image=1 关于image_enc中每一项关键字的含义,可以直接运行image_enc查看:
图13 该命令执行的结果将会和MCUBOOTutility的配置一样。加密区域从0X60001000开始,长度为0X2000。具体项目也可以参考图4. 运行之后获得文件: evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted.bin
4.2.3 RemoveBinaryBytes去掉BEE加密文件开头1024字节 RemoveBinaryBytes.exe evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted.binevkmimxrt1015_iled_blinky_0x60002000_bee_encrypted_remove1K.bin 1024 该命令用于去掉BEE加密文件开始的0X400长度的数据,这样修改后的加密文件将会直接从EKIB0开始。 运行之后,获得: evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted_remove1K.bin
4.2.4 elftosb生成BEE加密app对应的sb文件 elftosb.exe -f kinetis -V -cprogram_imxrt1015_qspi_encrypt_sw_gp2.bd -o boot_image_encrypt.sbevkmimxrt1015_iled_blinky_0x60002000_bee_encrypted_remove1K.bin 该命令用于把evkmimxrt1015_iled_blinky_0x60002000_bee_encrypted_remove1K.bin文件结合program_imxrt1015_qspi_encrypt_sw_gp2.bd生成可以使用MFGTool烧录到外部QSPI的sb文件。 运行之后获得: boot_image_encrypt.sb
4.2.5 elftosb生成fuse烧写对应的sb文件 elftosb.exe -f kinetis -V -cburn_fuse.bd -o burn_fuse.sb 该命令用于生成BEE需要烧写fuse的对应sb文件,后续将会和boot_image_encrypt.sb 一并使用MFGTool烧录。但是一旦fuse已经烧录之后,将不在需要这个sb文件,直接烧录对应的app加密文件即可。 运行之后获得: burn_fuse.sb
4.3 MFGTool烧录 MIMXRT1015-EVK开发板进入serial downloader模式,找两根USB分别接到J41,J9。 MFGTool可以在SDK_2.8.0_EVK-MIMXRT1015\middleware\mcu-boot\bin\Tools\mfgtools-rel找到。 如果要烧写burn_fuse.sb,需要修改ucl2.xml,路径: \SDK_2.8.0_EVK-MIMXRT1015\middleware\mcu-boot\bin\Tools\mfgtools-rel\Profiles\MXRT1015\OSFirmware 可以添加如下项目实现 - <LIST name="MXRT1015-beefuse_DevBoot" desc="Boot Flashloader">
- <!-- Stage 1, load and execute Flashloader -->
- <CMD state="BootStrap" type="boot" body="BootStrap" file="ivt_flashloader.bin" > Loading Flashloader. </CMD>
- <CMD state="BootStrap" type="jump" onError = "ignore"> Jumping to Flashloader. </CMD>
- <!-- Stage 2, burn BEE related fuse using Flashloader -->
- <CMD state="Blhost" type="blhost" body="get-property 1" > Get Property 1. </CMD> <!--Used to test if flashloader runs successfully-->
- <CMD state="Blhost" type="blhost" body="receive-sb-file "Profiles\\MXRT1015\\OS Firmware\\burn_fuse.sb"" > Program Boot Image. </CMD>
- <CMD state="Blhost" type="blhost" body="reset" > Reset. </CMD> <!--Reset device-->
- <!-- Stage 3, Program boot image into external memory using Flashloader -->
- <CMD state="Blhost" type="blhost" body="get-property 1" > Get Property 1. </CMD> <!--Used to test if flashloader runs successfully-->
- <CMD state="Blhost" type="blhost" timeout="15000" body="receive-sb-file "Profiles\\MXRT1015\\OS Firmware\\boot_image.sb"" > Program Boot Image. </CMD>
- <CMD state="Blhost" type="blhost" body="Update Completed!">Done</CMD>
- </list>
复制代码 如果已经烧录过Fuse,再更新app,只需要用MIMXRT1015-DevBoot- <LIST name="MXRT1015-DevBoot" desc="Boot Flashloader">
- <!-- Stage 1, load and execute Flashloader -->
- <CMD state="BootStrap" type="boot" body="BootStrap" file="ivt_flashloader.bin" > Loading Flashloader. </CMD>
- <CMD state="BootStrap" type="jump" onError = "ignore"> Jumping to Flashloader. </CMD>
- <!-- Stage 2, Program boot image into external memory using Flashloader -->
- <CMD state="Blhost" type="blhost" body="get-property 1" > Get Property 1. </CMD> <!--Used to test if flashloader runs successfully-->
- <CMD state="Blhost" type="blhost" timeout="15000" body="receive-sb-file "Profiles\\MXRT1015\\OS Firmware\\boot_image.sb"" > Program Boot Image. </CMD>
- <CMD state="Blhost" type="blhost" body="Update Completed!">Done</CMD>
- </list>
复制代码 具体选择哪个list,可以在cfg.ini中的name去配置:- [profiles]
- chip = MXRT1015
- [platform]
- board =
- [LIST]
- name = MXRT1015-DevBoot
复制代码 由于我这边已经用MCUBootUtility烧录过fuse,所以直接使用MXRT1015-DevBoot下载app.sb图14 可以看到,能够成功烧录。 点击stop,配置MIMXRT1015-EVK板上sw8 1-ON,2-OFF,3-ON,4-OFF,复位开发板,可以看到小灯能够闪烁。命令行也成完成BEE代码的烧录。 |