1#!/usr/bin/env bash
2
3# Copyright 2017 The Fuchsia Authors
4#
5# Use of this source code is governed by a MIT-style
6# license that can be found in the LICENSE file or at
7# https://opensource.org/licenses/MIT
8
9set -eo pipefail
10
11CMDLINE=
12EXTRA_ARGS=()
13
14SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
15ZIRCON_DIR="${SCRIPTS_DIR}/.."
16
17FIRMWARE_DIR=/tmp/hikey/hikey-firmware
18FIRMWARE_GIT=https://android.googlesource.com/device/linaro/hikey
19FIRMWARE_TAG=android-o-iot-preview-5
20
21help() {
22    echo "usage: ${0} [options]"
23    echo "  -b [build-dir]  Use specified build directory."
24    echo "                  Defaults to build-hikey960/."
25    echo "  -d [ramdisk]    Use specified ramdisk file."
26    echo "                  Defaults to BUILD_DIR/bootdata.bin."
27    echo "  -f              Download and flash firmware."
28    echo "  -m              Add mexec option to kernel command line to enable netboot."
29    echo "  -h              Show this help message."
30    echo
31    echo "See docs/targets/hikey960.md for more details."
32    exit 1
33}
34
35help_fastboot() {
36    echo
37    echo "Check that the device is in fastboot mode:"
38    echo "  Auto Power up(Switch 1)   closed/ON"
39    echo "  Recovery(Switch 2)        open/OFF"
40    echo "  Fastboot(Switch 3)        closed/ON"
41
42    read -p "Proceed (y|n)? " -n 1 -r
43    echo
44    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
45        exit 1
46    fi
47}
48
49git_clone() {
50    git clone --depth 1 $@
51}
52
53flash_firmware() {
54    if [[ ! -d $FIRMWARE_DIR ]]; then
55        git_clone -b $FIRMWARE_TAG $FIRMWARE_GIT $FIRMWARE_DIR
56    fi
57
58    help_fastboot
59    fastboot flash ptable ${FIRMWARE_DIR}/installer/hikey960/ptable.img
60    fastboot flash xloader ${FIRMWARE_DIR}/installer/hikey960/sec_xloader.img
61    fastboot flash fastboot ${FIRMWARE_DIR}/installer/hikey960/fastboot.img
62    fastboot flash nvme ${FIRMWARE_DIR}/installer/hikey960/nvme.img
63    fastboot flash fw_lpm3 ${FIRMWARE_DIR}/installer/hikey960/lpm3.img
64    fastboot flash trustfirmware ${FIRMWARE_DIR}/installer/hikey960/bl31.bin
65}
66
67flash_kernel() {
68    "${ZIRCON_DIR}/kernel/target/arm64/board/hikey960/package-image.sh" -B "${BUILD_DIR}" "${EXTRA_ARGS[@]}"
69
70    fastboot flash boot $OUT_IMAGE
71    # Can't guarantee that the target has written image to flash before the
72    # fastboot command completes, so short delay here before reboot.
73    sleep 1
74    fastboot reboot
75}
76
77while getopts "b:d:fmnp:ruh" FLAG; do
78    case $FLAG in
79    b) BUILD_DIR="${OPTARG}";;
80    d) RAMDISK="${OPTARG}";;
81    f) FLAG_FIRMWARE=true;;
82    m) CMDLINE+=" netsvc.netboot=true";;
83    *) help;;
84    esac
85done
86shift $((OPTIND-1))
87
88BUILD_DIR="${BUILD_DIR:-build-arm64}"
89RAMDISK="${RAMDISK:-${BUILD_DIR}/hikey960-bootdata.bin}"
90OUT_IMAGE="${BUILD_DIR}/hikey960-boot.img"
91
92if [[ $FLAG_FIRMWARE ]]; then
93    flash_firmware
94    exit 0
95fi
96
97if [[ -n "${CMDLINE}" ]]; then
98    EXTRA_ARGS+=(-c "${CMDLINE}")
99fi
100
101flash_kernel
102