How to build the firmware for hikey and hikey960 boards - liuyq/android-issues GitHub Wiki

  1. There 3 build methods

    1. Linaro CI Build scripts
      1. 96boards-reference-uefi-staging.yaml
      2. rpb-uefi/staging/builders.sh
      3. 96boards-hikey-uefi-openplatformpkg.yaml
    2. John Stultz docker method
      1. https://github.com/johnstultz-work/dockerstuff/blob/master/Dockerfile.uefi about outdated, needs a trivial update
      2. https://github.com/johnstultz-work/dockerstuff/blob/master/Dockerfile.uefi960
    3. hikey960-firmware manifest
      1. https://github.com/liuyq/manifest/blob/hikey960-firmware/default.xml
  2. build with linaro ci method

    1. Repositories

      Repository URL Hikey Hikey960
      arm-trusted-firmware https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git origin/integration origin/integration
      edk2 https://github.com/96boards-hikey/edk2.git origin/testing/hikey960_v2.5 origin/testing/hikey960_v2.5
      l-loader https://github.com/96boards-hikey/l-loader.git testing/hikey960_v1.2 testing/hikey960_v1.2
      optee_os https://github.com/OP-TEE/optee_os.git 3.0.0 3.0.0
      OpenPlatformPkg https://github.com/96boards-hikey/OpenPlatformPkg.git testing/hikey960_v1.3.4 testing/hikey960_v1.3.4
      uefi-tools https://git.linaro.org/uefi/uefi-tools.git master master
    2. Clone Repositories

      $ git clone --depth 1 https://github.com/96boards-hikey/atf-fastboot.git
      $ git clone https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git arm-trusted-firmware && \
             cd arm-trusted-firmware && git checkout -b stable-baseline origin/integration && cd -
      $ git clone https://github.com/96boards-hikey/edk2.git edk2 && \
             cd edk2 && git checkout -b stable-baseline origin/testing/hikey960_v2.5 && git submodule update --init --recursive && cd -
      $ git clone --depth 1 -b testing/hikey960_v1.2 https://github.com/96boards-hikey/l-loader.git l-loader
      $ git clone https://github.com/OP-TEE/optee_os.git optee_os && \
             cd optee_os && git checkout -b stable-baseline 3.0.0 && cd -
      $ git clone -b testing/hikey960_v1.3.4 https://github.com/96boards-hikey/OpenPlatformPkg.git OpenPlatformPkg && \
             cd edk2 && rm -rf OpenPlatformPkg && ln -s ../OpenPlatformPkg && cd -
      $ git clone -b master https://git.linaro.org/uefi/uefi-tools.git uefi-tools
      
    3. Build with linaro-ci method

       $ export BUILD_PATH=${PWD}/
       $ export EDK2_DIR=${BUILD_PATH}/edk2
       $ export OPP_DIR=${BUILD_PATH}/OpenPlatformPkg
       $ export ATF_DIR=${BUILD_PATH}/arm-trusted-firmware
       $ export OPTEE_OS_DIR=${BUILD_PATH}/optee_os
       $ export UEFI_TOOLS_DIR=${BUILD_PATH}/uefi-tools
       $ export LOADER_DIR=${BUILD_PATH}/l-loader
      
       $ cd ${BUILD_PATH}//edk2 && ln -sf ../OpenPlatformPkg && cd -
       $ cd ${BUILD_PATH}/l-loader
       $ sed -i s/#GENERATE_PTABLE=1/GENERATE_PTABLE=1/g build_uefi.sh
       $ sed -i s/BUILD_OPTION=DEBUG/BUILD_OPTION=RELEASE/g build_uefi.sh
       $ ./build_uefi.sh hikey
      
  3. Build with hikey960-firmware manifest

    # Need to install the toolchains first
    $ mkdir -p /opt/toolchain/ && cd /opt/toolchain/
    $ wget http://releases.linaro.org/components/toolchain/binaries/7.2-2017.11/aarch64-linux-gnu/gcc-linaro-7.2.1-2017.11-x86_64_aarch64-linux-gnu.tar.xz
    $ tar -xf gcc-linaro-7.2.1-2017.11-x86_64_aarch64-linux-gnu.tar.xz
    $ wget http://releases.linaro.org/components/toolchain/binaries/4.9-2017.01/arm-linux-gnueabihf/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz 
    $ tar -xf gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz
    $ cd -
    
    # repo sync repositories
    $ mkdir hikey-uefi && cd hikey-uefi
    # To work around using the default repo from google
    # export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo'
    $ repo init -b hikey960-firmware https://github.com/liuyq/manifest.git
    $ repo sync $(nproc)
    
    # update l-loader/build_uefi.sh to generate the ptable file
    $ sed -i s/#GENERATE_PTABLE=1/GENERATE_PTABLE=1/g l-loader/build_uefi.sh
    # update l-loader/build_uefi.sh to generate the release version firmware
    $ sed -i s/BUILD_OPTION=DEBUG/BUILD_OPTION=RELEASE/g l-loader/build_uefi.sh
    
    # build firmwares for hikey or hikey960
    # be sure to clean the repositories before running the scripts below
    # otherwise the files might not work.
    $ ./build_uefi.sh hikey960
    $ ./build_uefi.sh hikey
    
    # output files for hikey firmwares
    $ ll l-loader/recovery.bin l-loader/fip.bin l-loader/l-loader.bin l-loader/prm_ptable.img
    lrwxrwxrwx 1 liuyq liuyq     51 2月   6 14:41 l-loader/fip.bin -> ../arm-trusted-firmware/build/hikey/release/fip.bin
    -rw-rw-r-- 1 liuyq liuyq  44544 2月   6 14:41 l-loader/l-loader.bin # BL2
    -rw-rw-r-- 1 liuyq liuyq  17408 2月   6 14:42 l-loader/prm_ptable.img # needs to be renamed to ptable-aosp-8g.img
    -rw-rw-r-- 1 liuyq liuyq 199680 2月   6 14:41 l-loader/recovery.bin # BL1 and NS_BL1U
    $ wget https://raw.githubusercontent.com/96boards/burn-boot/master/hisi-idt.py -O l-loader/hisi-idt.py
    
    # output for hikey960: TODO
    
  4. deployments/update on firmware

    NOTE:

    • The instructions in the following description is a bit outdated, as the recovery.img is used now.
      The last commit that has the installer/hikey files is [f505336c6f1c47788d3beb9a0ed4de7aac049233](https://android.googlesource.com/device/linaro/hikey/+/f505336c6f1c47788d3beb9a0ed4de7aac049233/installer/hikey/) as the next commit [da0c896d94d54e9267e18dd3201a2d98126a282f](https://android.googlesource.com/device/linaro/hikey/+/da0c896d94d54e9267e18dd3201a2d98126a282f) which is after it, the files were removed. [installer/hikey/README](https://android.googlesource.com/device/linaro/hikey/+/f505336c6f1c47788d3beb9a0ed4de7aac049233/installer/hikey/README) and [installer/hikey/flash-all.sh](https://android.googlesource.com/device/linaro/hikey/+/f505336c6f1c47788d3beb9a0ed4de7aac049233/installer/hikey/flash-all.sh) should be read and used for the firmware update.
      
    • Please check the instructions from here: https://www.96boards.org/documentation/consumer/hikey/hikey620/installation/board-recovery.md.html
    • Here are the available images, not the latest version though.

    INSTRUCTIONS IN A SCRIPT:

    #!/bin/bash -ex
    
    firmware_dir="${1}"
    firmware_dir="82"
    firmware_dir="$(cd ${firmware_dir}; pwd)"
    serial_dev="/dev/ttyUSB1"
    
    if [ -z "${firmware_dir}" ]; then
        exit 0
    fi
    
    #if ! [ -e "${serial_dev}" ]; then
    #    echo "${serial_dev} does not exist"
    #    exit 1
    #fi
    ptable_name="ptable-aosp-8g.img"
    if echo "$@" |grep -q 4g; then
        ptable_name="ptable-aosp-4g.img"
    fi
    
    # workaround to use python2
    dir_virenv=/tmp/work/hikey/
    virtualenv --python=python2 ${dir_virenv}
    source ${dir_virenv}/bin/activate
    pip install pyserial
    
    sudo ${dir_virenv}/bin/python2 ${firmware_dir}/hisi-idt.py -d ${serial_dev} --img1 ${firmware_dir}/recovery.bin
    #python2 ${firmware_dir}/hisi-idt.py -d ${serial_dev} --img1 ${firmware_dir}/recovery.bin
    fastboot getvar partition-size:ptable
    fastboot flash ptable ${firmware_dir}/${ptable_name}
    fastboot flash loader ${firmware_dir}/l-loader.bin
    fastboot flash fastboot ${firmware_dir}/fip.bin
    
  5. issues with Ubuntu 22.04

    1. build error reported on __builtin_strncpy
      In file included from /usr/include/string.h:495,
                       from ../Common/FvLib.h:21,
                       from GenVtf.c:19:
      In function ‘strncpy’,
          inlined from ‘ConvertVersionInfo’ at GenVtf.c:132:7:
      /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation]
        106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
            |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      GenVtf.c: In function ‘ConvertVersionInfo’:
      GenVtf.c:130:14: note: length computed here
        130 |     Length = strlen(Str);
            |              ^~~~~~~~~~~
      In file included from /usr/include/string.h:495,
                       from ../Common/FvLib.h:21,
                       from GenVtf.c:19:
      In function ‘strncpy’,
          inlined from ‘CreateFitTableAndInitialize’ at GenVtf.c:1532:3:
      /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: error: ‘__builtin_strncpy’ output truncated before terminating nul copying 8 bytes from a string of the same length [-Werror=stringop-truncation]
        106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
            |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      cc1: all warnings being treated as errors
      make[2]: *** [../Makefiles/footer.makefile:27: GenVtf.o] Err
      
      could be worked around with the following patch on l-loader/build-uefi.sh
      $ git diff
      diff --git a/build_uefi.sh b/build_uefi.sh
      index 15db58b9ff7c..05123fbe7257 100755
      --- a/build_uefi.sh
      +++ b/build_uefi.sh
      @@ -137,7 +137,7 @@ cd ${EDK2_DIR}/BaseTools
       make clean
       rm -fr ${BUILD_PATH}/Build/
       rm -fr ${EDK2_DIR}/Build/
      -rm -f ${EDK2_DIR}/Conf/.cache
      +rm -fr ${EDK2_DIR}/Conf/.cache # is a directory now
       rm -f ${EDK2_DIR}/Conf/build_rule.txt
       rm -f ${EDK2_DIR}/Conf/target.txt
       rm -f ${EDK2_DIR}/Conf/tools_def.txt
      @@ -232,7 +232,8 @@ function do_build()
                      esac
              fi
              source edk2/edksetup.sh
      -       make -C edk2/BaseTools
      +       # https://review.trustedfirmware.org/plugins/gitiles/OP-TEE/build/+/f342db9fdc99a36252f4fda9ffbd949c84624ab2%5E%21/#F0
      +       make -C edk2/BaseTools BUILD_CC="gcc -Wno-error=stringop-truncation"
              if [ $? != 0 ]; then
                      echo "Fail to build EDKII BaseTools ($?)"
                      exit
      $
      
    2. ModuleNotFoundError: No module named 'Crypto'
      + make PLATFORM=hikey-hikey960 CFG_ARM64_core=y DEBUG=0
        GEN     out/arm-plat-hikey/core/ta_pub_key.c
      Traceback (most recent call last):
        File "scripts/pem_to_pub_c.py", line 57, in <module>
          main()
        File "scripts/pem_to_pub_c.py", line 23, in main
          from Crypto.PublicKey import RSA
      ModuleNotFoundError: No module named 'Crypto'
      make: *** [mk/subdir.mk:153: out/arm-plat-hikey/core/ta_pub_key.c] Error 1
      
      could be resolved with the following methods
      $ sudo pip install pycryptodome
      $ sudo apt -y install python3-pycryptodome # For Ubuntu 22.04
      
    3. optee_os scripts/gen_hashed_bin.py reports "struct.error: required argument is not an integer"
      Traceback (most recent call last):
        File "./scripts/gen_hashed_bin.py", line 176, in <module>
          main()
        File "./scripts/gen_hashed_bin.py", line 151, in main
          write_header_v1(outf, init_size, args, paged_size)
        File "./scripts/gen_hashed_bin.py", line 20, in write_header_v1
          outf.write(struct.pack('<IBBHIIIII', \
      struct.error: required argument is not an integer
      make: *** [core/arch/arm/kernel/link.mk:227: out/arm-plat-hikey/core/tee.bin] Error 1
      
      could be worked around with use python2 for the scripts:
      $ git diff
      diff --git a/scripts/gen_hashed_bin.py b/scripts/gen_hashed_bin.py
      index 32350a47efb8..ab12c0dac3f0 100755
      --- a/scripts/gen_hashed_bin.py
      +++ b/scripts/gen_hashed_bin.py
      @@ -1,4 +1,4 @@
      -#!/usr/bin/env python
      +#!/usr/bin/env python2
       # SPDX-License-Identifier: BSD-2-Clause
       #
       # Copyright (c) 2014-2017, Linaro Limited
      diff --git a/scripts/gen_ld_sects.py b/scripts/gen_ld_sects.py
      index c5dc3a7b6c1a..f14db9ec5d9c 100755
      --- a/scripts/gen_ld_sects.py
      +++ b/scripts/gen_ld_sects.py
      @@ -1,4 +1,4 @@
      -#!/usr/bin/env python
      +#!/usr/bin/env python2
       # SPDX-License-Identifier: BSD-2-Clause
       #
       # Copyright (c) 2017, Linaro Limited
      $
      
    4. hisi-idt.py TypeError: ord() expected string of length 1, but int found
      $ sudo python hisi-idt.py -d /dev/ttyUSB1 --img1 recovery.bin
      +----------------------+
       Serial:  /dev/ttyUSB1
       Image1:  recovery.bin
       Image2:  
      +----------------------+
      
      Sending recovery.bin ...
      hisi-idt.py:169: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
        self.sendheadframe(length,address)
      Traceback (most recent call last):
        File "hisi-idt.py", line 272, in <module>
          main(sys.argv[1:])
        File "hisi-idt.py", line 269, in main
          burnboot('hi3716cv200', dev, img1, img2)
        File "hisi-idt.py", line 201, in burnboot
          downloader.download(filename1, filename2)
        File "hisi-idt.py", line 186, in download
          self.senddata(data,self.bootheadaddress[self.chip])
        File "hisi-idt.py", line 169, in senddata
          self.sendheadframe(length,address)
        File "hisi-idt.py", line 135, in sendheadframe
          crc = self.calc_crc(data)
        File "hisi-idt.py", line 91, in calc_crc
          crc = ((crc << 8) | ord(char)) ^ self.crctable[(crc >> 8) & 0xff]
      TypeError: ord() expected string of length 1, but int found
      $
      
      this could be worked around with running hisi-idt.py with python2 version
      $ virtualenv --python=python2 /tmp/python2/
      $ source /tmp/python2/bin/activate
      $ /tmp/python2/bin/python ./hisi-idt.py  -d /dev/ttyUSB1 --img1 recovery.bin
      
    5. hisi-idt.py ImportError: No module named serial
      (python2) $ ./hisi-idt.py -d /dev/ttyUSB1 --img1 recovery.bin
      Traceback (most recent call last):
        File "./hisi-idt.py", line 6, in <module>
          import serial, time
      ImportError: No module named serial
      (python2) $
      
      This could be resolved with pyserial module
      $ pip install pyserial
      
    6. fastboot flash ptable invalid partition
      + fastboot flash ptable /data/android13/prebuilts/hikey-uefi/82-lab/ptable-aosp-8g.img
      < waiting for any device >
      Sending 'ptable' (17 KB)                           FAILED (remote: 'invalid partition')
      fastboot: error: Command failed
      $
      
      This will be resolved with the fastboot getvar partition-size:ptable command:
      $ fastboot getvar partition-size:ptable
      
⚠️ **GitHub.com Fallback** ⚠️