1#!/bin/bash
2
3build_kernel() {
4    echo "==========Building kernel image=========="
5    cd $ANDROID_BUILD_TOP
6    source build/envsetup.sh
7    lunch walleye-userdebug
8    cd $LOCAL_KERNEL_HOME
9    . ./build.config
10    make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} ${DEFCONFIG}
11    make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -j32
12    cp $LOCAL_KERNEL_HOME/arch/arm64/boot/Image.lz4-dtb arch/arm64/boot/dtbo.img `find . -name '*.ko'` $ANDROID_BUILD_TOP/device/google/wahoo-kernel
13}
14
15build_image() {
16    cd $ANDROID_BUILD_TOP
17    source build/envsetup.sh
18    lunch walleye-userdebug
19    if [ "$1" = "bootimage" ]; then
20        echo "==========Building bootimage=========="
21        make -j32 vendorimage-nodeps
22        make -j32 vbmetaimage-nodeps
23        make -j32 bootimage
24    else
25        echo "==========Building complete image=========="
26        make -j32
27    fi
28}
29
30wait_for_fastboot() {
31    # wait for device to enter fastboot, max wait is 200secs
32    local i=0
33    while [ $i -lt 100 ]
34    do
35        if [ -n "`fastboot devices`"  ]; then
36            break
37        else
38            sleep 2
39            i=$((i+1))
40        fi
41    done
42}
43
44flash_android() {
45    # reboot the device if it's online
46    if [ "`adb devices`" != "List of devices attached" ]; then
47        echo "==========Rebooting the device into fastboot=========="
48        adb reboot bootloader
49    fi
50
51    echo "==========Waiting for device to enter fastboot=========="
52    wait_for_fastboot
53
54    if [ -z "`fastboot devices`" ]; then
55        echo "==========Device failed to enter fastboot=========="
56        exit
57    fi
58
59    # flash the device
60    if [ "$1" = "bootimage" ]; then
61        echo "==========Flashing bootimage=========="
62        fastboot flash vbmeta
63        fastboot flash vendor
64        fastboot flash boot
65        fastboot reboot
66    else
67        echo "==========Flashing complete image=========="
68        fastboot flashall
69    fi
70
71    echo "==========Waiting for device to come online=========="
72    # wait for device to boot
73    adb wait-for-device
74}
75
76# check input parameters
77if [ "$1" != "kernel" ] && [ "$1" != "all" ]; then
78    echo "First parameter \"$1\" is invalid. Should be \"kernel\" or \"all\"."
79    exit
80fi
81
82if [ "$2" != "build" ] && [ "$2" != "flash" ]; then
83    echo "Second parameter \"$2\" is invalid. Should be \"build\" or \"flash\"."
84    exit
85fi
86
87if [ -z "$ANDROID_BUILD_TOP" ]; then
88    echo "ANDROID_BUILD_TOP environment variable is not set."
89    exit
90fi
91
92if [ -z "$LOCAL_KERNEL_HOME" ]; then
93    echo "LOCAL_KERNEL_HOME environment variable is not set."
94    exit
95fi
96
97if [ "$2" = "build" ]; then
98    build_kernel
99    if [ "$1" = "kernel" ]; then
100        build_image bootimage
101    else
102        build_image
103    fi
104else
105    if [ "$1" = "kernel" ]; then
106        flash_android bootimage
107    else
108        flash_android
109    fi
110fi
111
112