Navigation

    全志在线开发者论坛

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • 在线文档
    • 社区主页

    【FAQ】全志V系列芯片 在TinaLinux如何判断安全固件与rotpk.bin是否对应?

    V Series
    v853 v851s faq 技术支持
    1
    1
    979
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • q1215200171
      budbool LV 9 last edited by

      1.主题

      TinaLinux如何判断安全固件与rotpk.bin是否对应

      2.问题背景

      客户在开发过程中,经常遇到安全固件烧录后,启动失败并不停烧录的问题。

      这个问题有可能有多种原因,最常见的一种是安全固件与烧录到开发板efuse中的rotpk不一致。

      如果可以从安全固件中提取rotpk的话,将提取的rotpk与efuse中烧录的rotpk(或一组签名密钥对中的rotpk.bin文件)对比,即可判断是否对应。

      那么如何从安全固件中提取rotpk?

      3.问题分析

      提取思路:

      安全固件中包含了各个镜像的签名证书,其中TOC1中包含了使用根密钥签名的证书。

      全志安全启动使用的证书是X509格式,通过openssl可以解析根密钥签名的证书内容,提取证书中的公钥。

      将公钥按照特定组织方式,再次计算sha256,即可获得安全固件中的根密钥公钥的hash值,即rotpk。

      4.解决办法

      这里提供一个genrotpkfromfw.sh的脚本,使用方法:

      ./genrotpkfromfw.sh <secure firmware>
      

      执行完后,会在当前目录生成rotpk_rsa.bin,并打印其中的内容。

      user@pc:$ ./getrotpkfromfw.sh tina_xxx_secure_v0.img
      Successfully get rotpk file: rotpk_rsa.bin from tina_xxx_secure_v0.img
      rotpk content:
      00000000  90 fa 80 f1 54 49 51 2a  8a 04 23 97 06 6f 5f 78  |....TIQ*..#..o_x|
      00000010  0b 6c 8f 89 21 98 e8 d1  ba a4 2e b6 ce d1 76 f3  |.l..!.........v.|
      00000020
      

      脚本内容如下:

      #!/bin/bash
      
      function help_usage()
      {
              echo "Get rotpk from secure firmware"
              echo "USAGE: $0 <secure firmware>"
      }
      
      if [ -z "$1" ]; then
              help_usage
              exit 1
      fi
      
      
      FW=$1
      TOC_STR="|sunxi-secure"
      CERT=.cert_tmp
      GENPUBKEY_RSA=key_rsa_pub.bin
      ROTPK_RSA=rotpk_rsa.bin
      
      rm -f $CERT $GENPUBKEY_RSA $ROTPK_RSA
      
      
      #############################
      # 1. get cert               #
      #############################
      toc_addr=`hexdump -C $FW | grep $TOC_STR | awk '{print strtonum("0x"$1)}'`
      cert_offset=`hexdump -s $(($toc_addr + 128)) -n 4 $FW | head -1|  awk '{print strtonum("0x"$5$4$3$2)}'`
      cert_addr=$(($toc_addr+$cert_offset))
      dd if=$FW of=$CERT bs=1 count=4k skip=$cert_addr 2> /dev/null
      
      if [ ! -f $CERT ]; then
              echo "Error: generate $CERT error"
              exit 1
      fi
      
      #############################
      # 2. get root public key    #
      #############################
      openssl x509 -modulus -in $CERT -inform der -noout | awk -F = '{print $2}' | xxd -r -ps > $GENPUBKEY_RSA
      openssl x509 -in $CERT -inform der -text -noout | grep 65537 > /dev/null
      if [ $? -eq 0 ]; then
                      echo 010001 | xxd -r -ps >> $GENPUBKEY_RSA
      else
                      echo "Error: RSA Exponent is not 65537"
                      rm -f $CERT $GENPUBKEY_RSA $ROTPK_RSA
                      exit 1
      fi
      dd if=/dev/zero of=temp.bin bs=1 count=253 > /dev/null 2>&1
      cat temp.bin | tr "\000" "\221" >> $GENPUBKEY_RSA
      rm -f temp.bin
      
      #############################
      # 3. get rotpk              #
      #############################
      # generate $ROTPK_RSA based on $GENPUBKEY_RSA
      openssl dgst -sha256 -binary -out $ROTPK_RSA $GENPUBKEY_RSA
      rm -f $CERT $GENPUBKEY_RSA
      
      echo "Successfully get rotpk file: $ROTPK_RSA from $FW"
      echo "rotpk content:"
      hexdump -C $ROTPK_RSA
      
      1 Reply Last reply Reply Quote Share 3
      • Referenced by  q1215200171 q1215200171 
      • 1 / 1
      • First post
        Last post

      Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号

      行为准则 | 用户协议 | 隐私权政策