Marek Vasut | 78ad5b4 | 2022-07-12 17:04:52 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # 0) Generate keys |
| 4 | # |
| 5 | # WARNING: ECDSA keys are only supported by HAB 4.5 and newer (i.e. i.MX8M Plus) |
| 6 | # |
| 7 | # cd /path/to/cst-3.3.1/keys/ |
| 8 | # ./hab4_pki_tree.sh -existing-ca n -use-ecc n -kl 4096 -duration 10 -num-srk 4 -srk-ca y |
| 9 | # cd /path/to/cst-3.3.1/crts/ |
| 10 | # ../linux64/bin/srktool -h 4 -t SRK_1_2_3_4_table.bin -e SRK_1_2_3_4_fuse.bin -d sha256 -c ./SRK1_sha256_4096_65537_v3_ca_crt.pem,./SRK2_sha256_4096_65537_v3_ca_crt.pem,./SRK3_sha256_4096_65537_v3_ca_crt.pem,./SRK4_sha256_4096_65537_v3_ca_crt.pem -f 1 |
| 11 | |
| 12 | # 1) Build U-Boot (e.g. for i.MX8MM) |
| 13 | # |
| 14 | # export ATF_LOAD_ADDR=0x920000 |
| 15 | # cp -Lv /path/to/arm-trusted-firmware/build/imx8mm/release/bl31.bin . |
| 16 | # cp -Lv /path/to/firmware-imx-8.14/firmware/ddr/synopsys/ddr3* . |
| 17 | # make -j imx8mm_board_defconfig |
| 18 | # make -j`nproc` flash.bin |
| 19 | |
| 20 | # 2) Sign SPL and DRAM blobs |
| 21 | |
| 22 | cp doc/imx/habv4/csf_examples/mx8m/csf_spl.txt csf_spl.tmp |
| 23 | cp doc/imx/habv4/csf_examples/mx8m/csf_fit.txt csf_fit.tmp |
| 24 | |
| 25 | spl_block_base=$(printf "0x%x" $(( $(sed -n "/CONFIG_SPL_TEXT_BASE=/ s@.*=@@p" .config) - 0x40)) ) |
| 26 | spl_block_size=$(printf "0x%x" $(stat -tc %s u-boot-spl-ddr.bin)) |
| 27 | sed -i "/Blocks = / s@.*@ Blocks = $spl_block_base 0x0 $spl_block_size \"flash.bin\"@" csf_spl.tmp |
| 28 | |
| 29 | # Generate CSF blob |
| 30 | cst -i csf_spl.tmp -o csf_spl.bin |
| 31 | |
| 32 | # Patch CSF blob into flash.bin |
| 33 | spl_csf_offset=$(xxd -s 24 -l 4 -e flash.bin | cut -d " " -f 2 | sed "s@^@0x@") |
| 34 | spl_bin_offset=$(xxd -s 4 -l 4 -e flash.bin | cut -d " " -f 2 | sed "s@^@0x@") |
| 35 | spl_dd_offset=$((${spl_csf_offset} - ${spl_bin_offset} + 0x40)) |
| 36 | dd if=csf_spl.bin of=flash.bin bs=1 seek=${spl_dd_offset} conv=notrunc |
| 37 | |
| 38 | # 3) Sign u-boot.itb |
| 39 | |
| 40 | # fitImage tree |
Simon Glass | 72cc538 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 41 | fit_block_base=$(printf "0x%x" $(( $(sed -n "/CONFIG_TEXT_BASE=/ s@.*=@@p" .config) - $(sed -n "/CONFIG_FIT_EXTERNAL_OFFSET=/ s@.*=@@p" .config) - 0x200 - 0x40)) ) |
Marek Vasut | 78ad5b4 | 2022-07-12 17:04:52 +0200 | [diff] [blame] | 42 | fit_block_offset=$(printf "0x%s" $(fdtget -t x u-boot.dtb /binman/imx-boot/uboot offset)) |
Fabio Estevam | c0a2836 | 2022-08-13 09:03:10 -0300 | [diff] [blame] | 43 | fit_block_size=$(printf "0x%x" $(( ( ($(fdtdump u-boot.itb 2>/dev/null | sed -n "/^...totalsize:/ s@.*\(0x[0-9a-f]\+\).*@\1@p") + 0x1000 - 0x1 ) & ~(0x1000 - 0x1)) + 0x20 )) ) |
Marek Vasut | 78ad5b4 | 2022-07-12 17:04:52 +0200 | [diff] [blame] | 44 | sed -i "/Blocks = / s@.*@ Blocks = $fit_block_base $fit_block_offset $fit_block_size \"flash.bin\", \\\\@" csf_fit.tmp |
| 45 | |
| 46 | # U-Boot |
| 47 | uboot_block_base=$(printf "0x%s" $(fdtget -t x u-boot.itb /images/uboot load)) |
| 48 | uboot_block_offset=$(printf "0x%x" $(( $(printf "0x%s" $(fdtget -t x u-boot.itb /images/uboot data-position)) + ${fit_block_offset} ))) |
| 49 | uboot_block_size=$(printf "0x%s" $(fdtget -t x u-boot.itb /images/uboot data-size)) |
| 50 | sed -i "/0xuuuu/ s@.*@ $uboot_block_base $uboot_block_offset $uboot_block_size \"flash.bin\", \\\\@" csf_fit.tmp |
| 51 | |
| 52 | # ATF |
| 53 | atf_block_base=$(printf "0x%s" $(fdtget -t x u-boot.itb /images/atf load)) |
| 54 | atf_block_offset=$(printf "0x%x" $(( $(printf "0x%s" $(fdtget -t x u-boot.itb /images/atf data-position)) + ${fit_block_offset} ))) |
| 55 | atf_block_size=$(printf "0x%s" $(fdtget -t x u-boot.itb /images/atf data-size)) |
| 56 | sed -i "/0xaaaa/ s@.*@ $atf_block_base $atf_block_offset $atf_block_size \"flash.bin\", \\\\@" csf_fit.tmp |
| 57 | |
| 58 | # DTB |
| 59 | dtb_block_base=$(printf "0x%x" $(( ${uboot_block_base} + ${uboot_block_size} ))) |
| 60 | dtb_block_offset=$(printf "0x%x" $(( $(printf "0x%s" $(fdtget -t x u-boot.itb /images/fdt-1 data-position)) + ${fit_block_offset} ))) |
| 61 | dtb_block_size=$(printf "0x%s" $(fdtget -t x u-boot.itb /images/fdt-1 data-size)) |
| 62 | sed -i "/0xdddd/ s@.*@ $dtb_block_base $dtb_block_offset $dtb_block_size \"flash.bin\"@" csf_fit.tmp |
| 63 | |
| 64 | # IVT |
| 65 | ivt_ptr_base=$(printf "%08x" ${fit_block_base} | sed "s@\(..\)\(..\)\(..\)\(..\)@0x\4\3\2\1@") |
| 66 | ivt_block_base=$(printf "%08x" $(( ${fit_block_base} + ${fit_block_size} - 0x20 )) | sed "s@\(..\)\(..\)\(..\)\(..\)@0x\4\3\2\1@") |
| 67 | csf_block_base=$(printf "%08x" $(( ${fit_block_base} + ${fit_block_size} )) | sed "s@\(..\)\(..\)\(..\)\(..\)@0x\4\3\2\1@") |
| 68 | ivt_block_offset=$((${fit_block_offset} + ${fit_block_size} - 0x20)) |
| 69 | csf_block_offset=$((${ivt_block_offset} + 0x20)) |
| 70 | |
| 71 | echo "0xd1002041 ${ivt_ptr_base} 0x00000000 0x00000000 0x00000000 ${ivt_block_base} ${csf_block_base} 0x00000000" | xxd -r -p > ivt.bin |
| 72 | dd if=ivt.bin of=flash.bin bs=1 seek=${ivt_block_offset} conv=notrunc |
| 73 | |
| 74 | # Generate CSF blob |
| 75 | cst -i csf_fit.tmp -o csf_fit.bin |
| 76 | # Patch CSF blob into flash.bin |
| 77 | dd if=csf_fit.bin of=flash.bin bs=1 seek=${csf_block_offset} conv=notrunc |