1#!/bin/bash
2
3INSTALLER_DIR="`dirname ${0}`"
4ECHO_PREFIX="=== "
5
6# for cases that don't run "lunch hikey960-userdebug"
7if [ -z "${ANDROID_BUILD_TOP}" ]; then
8    ANDROID_BUILD_TOP=$(cd ${INSTALLER_DIR}/../../../../../; pwd)
9    ANDROID_PRODUCT_OUT="${ANDROID_BUILD_TOP}/out/target/product/hikey960"
10fi
11
12if [ ! -d "${ANDROID_PRODUCT_OUT}" ]; then
13    echo ${ECHO_PREFIX}"error in locating out directory, check if it exist"
14    exit
15fi
16
17echo ${ECHO_PREFIX}"android out dir:${ANDROID_PRODUCT_OUT}"
18
19. "${ANDROID_BUILD_TOP}/device/linaro/hikey/vendor-package-ver.sh"
20
21VENDOR_DIR=$ANDROID_BUILD_TOP/vendor/linaro/hikey960/${EXPECTED_LINARO_VENDOR_VERSION}/
22
23# TODO: Pull one-time recovery/qdl path out of standard install
24# Flash bootloader firmware files
25if [ ! -d "${VENDOR_DIR}/" ]; then
26    echo "FLASH-ALL-AOSP: Missing vendor firmware package?"
27    echo "                Make sure the vendor binaries have been downloaded from"
28    echo "                ${VND_PKG_URL}"
29    echo "                and extracted to $ANDROID_BUILD_TOP."
30    exit
31fi
32
33pushd $VENDOR_DIR/bootloader/
34
35function check_partition_table_version () {
36	fastboot erase reserved
37	if [ $? -eq 0 ]
38	then
39		IS_PTABLE_1MB_ALIGNED=true
40	else
41		IS_PTABLE_1MB_ALIGNED=false
42	fi
43}
44
45function flashing_atf_uefi () {
46	fastboot flash ptable prm_ptable.img
47	fastboot flash xloader hisi-sec_xloader.img
48	fastboot reboot-bootloader
49
50	fastboot flash fastboot l-loader.bin
51	fastboot flash fip fip.bin
52	fastboot flash nvme hisi-nvme.img
53	fastboot flash fw_lpm3 hisi-lpm3.img
54	fastboot flash trustfirmware hisi-bl31.bin
55	fastboot reboot-bootloader
56
57	fastboot flash ptable prm_ptable.img
58	fastboot flash xloader hisi-sec_xloader.img
59	fastboot flash fastboot l-loader.bin
60	fastboot flash fip fip.bin
61
62	fastboot flash boot "${ANDROID_PRODUCT_OUT}"/boot.img
63	fastboot flash super "${ANDROID_PRODUCT_OUT}"/super.img
64	fastboot flash userdata "${ANDROID_PRODUCT_OUT}"/userdata.img
65	fastboot format cache
66}
67
68function upgrading_ptable_1mb_aligned () {
69	fastboot flash xloader hisi-sec_xloader.img
70	fastboot flash ptable hisi-ptable.img
71	fastboot flash fastboot hisi-fastboot.img
72	fastboot reboot-bootloader
73}
74
75echo ${ECHO_PREFIX}"Checking partition table version..."
76check_partition_table_version
77
78if [ "${IS_PTABLE_1MB_ALIGNED}" == "true" ]
79then
80	echo ${ECHO_PREFIX}"Partition table is 1MB aligned. Flashing ATF/UEFI..."
81	flashing_atf_uefi
82else
83	echo ${ECHO_PREFIX}"Partition table is 512KB aligned."
84	echo ${ECHO_PREFIX}"Upgrading to 1MB aligned version..."
85	upgrading_ptable_1mb_aligned
86	echo ${ECHO_PREFIX}"Flashing ATF/UEFI..."
87	flashing_atf_uefi
88	echo ${ECHO_PREFIX}"Done"
89fi
90
91fastboot reboot
92popd
93