Skip to content

Reference Bootloader Hikey

Ricardo Salveti de Araujo edited this page Dec 22, 2015 · 10 revisions

Reference Software – Bootloader – HiKey

Boot components for HiKey:

l-loader

Used to switch from aarch32 to aarch64 and boot. First image to be flashed when in recovery mode.

Tool specific to HiKey, and can be found at https://github.com/96boards/l-loader

As part of the build process, l-loader incorporates bl1.bin, and binary is named as l-loader.bin.

ARM Trusted Firmware

Reference implementation of the ARM Trusted Firmware, based on the open source ARM Trusted Firmware project.

Since the changes required for HiKey are not yet upstream, a specific git tree is provided as part of the 96Boards github project, and can be found at https://github.com/96boards/arm-trusted-firmware.

The ARM TF tree is meant to be built together with EDK2, and the binary containing the ARM TF code is bl1.bin.

Tianocore EDK2 – UEFI

EDK2 is a modern, feature-rich, cross-platform firmware development environment for the UEFI and PI specifications.

Since the changes required for HiKey are not yet upstream, a specific git tree is provided as part of the 96Boards github project, and can be found at https://github.com/96boards/edk2.

The binary containing EDK2 is fip.bin, and flashed with the fastboot identifier.

GRUB2

GRUB is a portable, powerful bootloader, commonly used by desktop and servers.

Since no extra change is required for GRUB2, the Debian package is used as reference.

GRUB2 is provided as an UEFI application, with binary named as grubaa64.efi (for ARM 64).

Fastboot

Reference implementation of the Fastboot protocol, available as an UEFI application.

Binary file AndroidFastbootApp.efi.

Installing from the pre-built binaries:

Latest UEFI snapshot build is published at http://builds.96boards.org/snapshots/reference-platform/components/uefi/latest/release/hikey/

In order to flash from scratch (recovery mode), download the following files:

The main difference between Debian and AOSP is the partition table that gets used, so you need to pick the right partition table (ptable*.img) and the right boot image for the distribution you want to use.

For the boot files (from the reference builds):

For example, to download the latest UEFI build and Debian boot file:

wget http://builds.96boards.org/snapshots/reference-platform/components/uefi/latest/release/hikey/l-loader.bin
wget http://builds.96boards.org/snapshots/reference-platform/components/uefi/latest/release/hikey/fip.bin
wget http://builds.96boards.org/snapshots/reference-platform/components/uefi/latest/release/hikey/ptable-linux.img
wget https://raw.githubusercontent.com/96boards/burn-boot/master/hisi-idt.py
wget http://builds.96boards.org/snapshots/reference-platform/debian/latest/hikey/hikey-boot-linux-*.uefi.img.gz
gunzip hikey-boot-linux-*.uefi.img.gz

Enabling recovery mode

In order to be able to flash all the files listed above, it’s required to boot HiKey in a special mode, called recovery mode.

Steps to booting into recovery mode:

  • Turn off the HiKey board
  • Make sure pin1-pin2 and pin3-pin4 on J15 are linked (recovery mode)
  • Connect HiKey micro-USB to PC with USB cable
  • Power the HiKey board

Once powered on in recovery mode, for 90 seconds, a new USB serial interface should show up at your host machine. If you are running Linux as your host, you should see the following at your kernel log (dmesg):

usb 1-2.1: new full-speed USB device number 46 using xhci_hcd
usb 1-2.1: New USB device found, idVendor=12d1, idProduct=3609
usb 1-2.1: New USB device strings: Mfr=1, Product=4, SerialNumber=0
usb 1-2.1: Product: ㄰㌲㔴㜶㤸
usb 1-2.1: Manufacturer: 䕇䕎䥎
option 1-2.1:1.0: GSM modem (1-port) converter detected
usb 1-2.1: GSM modem (1-port) converter now attached to ttyUSB0

Now the board is ready to be flashed with the HiKey recovery tool (hisi-idt.py). You have 90 seconds to flash l-loader in the next step.

Flashing l-loader

The only binary that is required to be flashed with the HiKey recovery tool is l-loader.bin. Since l-loader.bin also implements the fastboot protocol, the remaining components can be easily flashed with fastboot instead.

In order to flash l-loader, run the following (replace ttyUSB0 with the correct device name showing in the kernel log above):

sudo python hisi-idt.py --img1=l-loader.bin -d /dev/ttyUSB0

You should get the following output:

+----------------------+
 Serial:  /dev/ttyUSB0
 Image1:  l-loader.bin
 Image2:  
+----------------------+
Sending l-loader.bin ...
Done

Once the flashing is completed, the board will automatically boot into fastboot mode. Once again, in case you are running Linux at your host, you should see the following:

usb 1-2.1: new high-speed USB device number 48 using xhci_hcd
usb 1-2.1: New USB device found, idVendor=18d1, idProduct=d00d
usb 1-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-2.1: Product: Android 2.0
usb 1-2.1: Manufacturer: Androi
usb 1-2.1: SerialNumber: 0123456789ABCDEF

Flashing the remaining files

Since fastboot is supported by l-loader, to flash the remaining files just use fastboot directly:

sudo fastboot flash ptable ptable-linux.img
sudo fastboot flash fastboot fip.bin
sudo fastboot flash boot hikey-boot-linux-*.uefi.img

Remember to always flash ptable first, unless you already know the partition table that was flashed last.

Rebooting into UEFI

  • Turn off HiKey board
  • Remove the jumper of pin3-pin4 on J15
  • Turn on HiKey board

In case you want to force UEFI to always load the fastboot application first, make sure that jumper of pin5-pin6 on J15 are connected.

Additional resources

Using the UEFI Shell

To access the UEFI shell, simply interrupt the boot process after powering up your board (make sure you have UART access), and select the Shell option.

[2] Shell

And to load fastboot from the shell:
bc. Shell> fastboot
Android Fastboot mode – version 0.4. Press any key to quit

Controlling Boot Order

If auto boot feature is enabled, a predefined boot entry will be selected by default.

Boot entries:

  1. Android Fastboot App
  2. Grub on eMMC
  3. Grub on SD

By default, the predefined boot entry is 2. If jumper on pin5-6 of J15 is connect, the predefined boot entry is 1.

To change boot order for the case jumper not on pin5-6:

sudo fastboot oem bootdevice [emmc|sd]

Control Auto Boot

By default, auto boot feature is enabled, which means UEFI will automatically search and boot Grub 2. If you want to be able to interrupt UEFI booting (to use the UEFI shell), disable autoboot and it should give 10 seconds before booting.

To disable auto boot:

sudo fastboot oem autoboot 0

To enable auto boot:

sudo fastboot oem autoboot 1

Set serial number in recovery mode

Generate new serial number:

sudo fastboot oem serialno

By default, random serial number is generated in recovery mode. If user wants to generate new serial number, run the above command when hikey is in recovery mode. Then power off. In the next boot, fastboot will use the new generated serial number for transmission.

Known Issues

  • Flashing l-loader.bin to the pseudopartitions is not enabled

TODO

  • Explaining how to manually create the boot partition, and showing that the device tree file is critical for the boot process
  • Describing how to build everything from sources

Building from source

The source code is available from:

Since GRUB2 is currently consumed directly from the Debian package, debian package rebuild instructions applies.

Build instructions

Prerequisites:

Installing pre-built toolchain(s)

mkdir arm-tc arm64-tc
tar --strip-components=1 -C ${PWD}/arm-tc -xf gcc-linaro-arm-linux-gnueabihf-4.9-*_linux.tar.xz
tar --strip-components=1 -C ${PWD}/arm64-tc -xf gcc-linaro-aarch64-linux-gnu-4.9-*_linux.tar.xz
export PATH="${PWD}/arm-tc/bin:${PWD}/arm64-tc/bin:$PATH"

Getting the source code

git clone -b reference --depth 1 https://github.com/96boards/edk2.git linaro-edk2
git clone -b reference --depth 1 https://github.com/96boards/arm-trusted-firmware.git
git clone --depth 1 https://github.com/96boards/l-loader.git
git clone git://git.linaro.org/uefi/uefi-tools.git

Building EDK2/UEFI for HiKey

Building EDK2/UEFI is simple if built with the uefi-tools.sh script, since it already incorporates the platform specific configs and binaries.

To build EDK2/UEFI:

export AARCH64_TOOLCHAIN=GCC49
export EDK2_DIR=${PWD}/linaro-edk2
export UEFI_TOOLS_DIR=${PWD}/uefi-tools
cd ${EDK2_DIR}
${UEFI_TOOLS_DIR}/uefi-build.sh -b RELEASE -a ../arm-trusted-firmware hikey

And bl1.bin with l-loader (ptable files are also created as part of the l-loader Makefile):

cd ../l-loader
ln -s ${EDK2_DIR}/Build/HiKey/RELEASE_GCC49/FV/bl1.bin
ln -s ${EDK2_DIR}/Build/HiKey/RELEASE_GCC49/FV/fip.bin
make # requires sudo for creating the partition tables

The files fip.bin, l-loader.bin and ptable-linux.img are now built. All the image files are in $BUILD/l-loader directory. The Fastboot App is at edk2/Build/HiKey/RELEASE_GCC49/AARCH64/AndroidFastbootApp.efi.

EFI boot partition

The boot partition is a 64MB FAT partition only contains fastboot.efi, GRUB2 and the dtb file used (same one provided by the kernel).

wget https://builds.96boards.org/snapshots/reference-platform/components/grub/latest/grubaa64.efi -O grubaa64.efi
mkdir boot-fat
dd if=/dev/zero of=boot-fat.uefi.img bs=512 count=131072
sudo mkfs.fat -n "BOOT IMG" boot-fat.uefi.img
sudo mount -o loop,rw,sync boot-fat.uefi.img boot-fat
sudo cp hi6220-hikey.dtb boot-fat/hi6220-hikey.dtb
sudo cp AndroidFastbootApp.efi boot-fat/fastboot.efi
sudo cp grubaa64.efi boot-fat/grubaa64.efi
sudo umount boot-fat
sudo mv boot-fat.uefi.img hikey-boot-linux-VERSION.uefi.img
rm -rf boot-fat

Now just flash the recently created hikey-boot-linux-VERSION.uefi.img with the same instructions as used with the pre-built binaries.