xref: /aosp_15_r20/kernel/tests/tools/flash_device.sh (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker#!/usr/bin/env bash
2*2f2c4c7aSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0
3*2f2c4c7aSAndroid Build Coastguard Worker
4*2f2c4c7aSAndroid Build Coastguard Worker# A handy tool to flash device with local build or remote build.
5*2f2c4c7aSAndroid Build Coastguard Worker
6*2f2c4c7aSAndroid Build Coastguard Worker# Constants
7*2f2c4c7aSAndroid Build Coastguard WorkerFETCH_SCRIPT="fetch_artifact.sh"
8*2f2c4c7aSAndroid Build Coastguard Worker# Please see go/cl_flashstation
9*2f2c4c7aSAndroid Build Coastguard WorkerFLASH_CLI=/google/bin/releases/android/flashstation/cl_flashstation
10*2f2c4c7aSAndroid Build Coastguard WorkerLOCAL_FLASH_CLI=/google/bin/releases/android/flashstation/local_flashstation
11*2f2c4c7aSAndroid Build Coastguard WorkerREMOTE_MIX_SCRIPT_PATH="DATA/local/tmp/build_mixed_kernels_ramdisk"
12*2f2c4c7aSAndroid Build Coastguard WorkerFETCH_SCRIPT="kernel/tests/tools/fetch_artifact.sh"
13*2f2c4c7aSAndroid Build Coastguard WorkerDOWNLOAD_PATH="/tmp/downloaded_images"
14*2f2c4c7aSAndroid Build Coastguard WorkerKERNEL_TF_PREBUILT=prebuilts/tradefed/filegroups/tradefed/tradefed.sh
15*2f2c4c7aSAndroid Build Coastguard WorkerPLATFORM_TF_PREBUILT=tools/tradefederation/prebuilts/filegroups/tradefed/tradefed.sh
16*2f2c4c7aSAndroid Build Coastguard WorkerKERNEL_JDK_PATH=prebuilts/jdk/jdk11/linux-x86
17*2f2c4c7aSAndroid Build Coastguard WorkerPLATFORM_JDK_PATH=prebuilts/jdk/jdk21/linux-x86
18*2f2c4c7aSAndroid Build Coastguard WorkerLOCAL_JDK_PATH=/usr/local/buildtools/java/jdk11
19*2f2c4c7aSAndroid Build Coastguard WorkerLOG_DIR=$PWD/out/test_logs/$(date +%Y%m%d_%H%M%S)
20*2f2c4c7aSAndroid Build Coastguard Worker# Color constants
21*2f2c4c7aSAndroid Build Coastguard WorkerBOLD="$(tput bold)"
22*2f2c4c7aSAndroid Build Coastguard WorkerEND="$(tput sgr0)"
23*2f2c4c7aSAndroid Build Coastguard WorkerGREEN="$(tput setaf 2)"
24*2f2c4c7aSAndroid Build Coastguard WorkerRED="$(tput setaf 198)"
25*2f2c4c7aSAndroid Build Coastguard WorkerYELLOW="$(tput setaf 3)"
26*2f2c4c7aSAndroid Build Coastguard WorkerORANGE="$(tput setaf 208)"
27*2f2c4c7aSAndroid Build Coastguard WorkerBLUE=$(tput setaf 4)
28*2f2c4c7aSAndroid Build Coastguard Worker
29*2f2c4c7aSAndroid Build Coastguard WorkerSKIP_BUILD=false
30*2f2c4c7aSAndroid Build Coastguard WorkerGCOV=false
31*2f2c4c7aSAndroid Build Coastguard WorkerDEBUG=false
32*2f2c4c7aSAndroid Build Coastguard WorkerKASAN=false
33*2f2c4c7aSAndroid Build Coastguard WorkerEXTRA_OPTIONS=()
34*2f2c4c7aSAndroid Build Coastguard WorkerLOCAL_REPO=
35*2f2c4c7aSAndroid Build Coastguard WorkerDEVICE_VARIANT="userdebug"
36*2f2c4c7aSAndroid Build Coastguard Worker
37*2f2c4c7aSAndroid Build Coastguard WorkerBOARD=
38*2f2c4c7aSAndroid Build Coastguard WorkerABI=
39*2f2c4c7aSAndroid Build Coastguard WorkerPRODUCT=
40*2f2c4c7aSAndroid Build Coastguard WorkerBUILD_TYPE=
41*2f2c4c7aSAndroid Build Coastguard WorkerDEVICE_KERNEL_STRING=
42*2f2c4c7aSAndroid Build Coastguard WorkerDEVICE_KERNEL_VERSION=
43*2f2c4c7aSAndroid Build Coastguard WorkerSYSTEM_DLKM_INFO=
44*2f2c4c7aSAndroid Build Coastguard Worker
45*2f2c4c7aSAndroid Build Coastguard Workerfunction print_help() {
46*2f2c4c7aSAndroid Build Coastguard Worker    echo "Usage: $0 [OPTIONS]"
47*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
48*2f2c4c7aSAndroid Build Coastguard Worker    echo "This script will build images and flash a physical device."
49*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
50*2f2c4c7aSAndroid Build Coastguard Worker    echo "Available options:"
51*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -s <serial_number>, --serial=<serial_number>"
52*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        [Mandatory] The serial number for device to be flashed with."
53*2f2c4c7aSAndroid Build Coastguard Worker    echo "  --skip-build          [Optional] Skip the image build step. Will build by default if in repo."
54*2f2c4c7aSAndroid Build Coastguard Worker    echo "  --gcov                [Optional] Build gcov enabled kernel"
55*2f2c4c7aSAndroid Build Coastguard Worker    echo "  --debug               [Optional] Build debug enabled kernel"
56*2f2c4c7aSAndroid Build Coastguard Worker    echo "  --kasan               [Optional] Build kasan enabled kernel"
57*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -pb <platform_build>, --platform-build=<platform_build>"
58*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        [Optional] The platform build path. Can be a local path or a remote build"
59*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        as ab://<branch>/<build_target>/<build_id>."
60*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If not specified and the script is running from a platform repo,"
61*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        it will use the platform build in the local repo."
62*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If string 'None' is set, no platform build will be flashed,"
63*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -sb <system_build>, --system-build=<system_build>"
64*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        [Optional] The system build path for GSI testing. Can be a local path or"
65*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        remote build as ab://<branch>/<build_target>/<build_id>."
66*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If not specified, no system build will be used."
67*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -kb <kernel_build>, --kernel-build=<kernel_build>"
68*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        [Optional] The kernel build path. Can be a local path or a remote build"
69*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        as ab://<branch>/<build_target>/<build_id>."
70*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If not specified and the script is running from an Android common kernel repo,"
71*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        it will use the kernel in the local repo."
72*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -vkb <vendor_kernel_build>, --vendor-kernel-build=<vendor_kernel_build>"
73*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        [Optional] The vendor kernel build path. Can be a local path or a remote build"
74*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        as ab://<branch>/<build_target>/<build_id>."
75*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If not specified, and the script is running from a vendor kernel repo, "
76*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        it will use the kernel in the local repo."
77*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -vkbt <vendor_kernel_build_target>, --vendor-kernel-build-target=<vendor_kernel_build_target>"
78*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        [Optional] The vendor kernel build target to be used to build vendor kernel."
79*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If not specified, and the script is running from a vendor kernel repo, "
80*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        it will try to find a local build target in the local repo."
81*2f2c4c7aSAndroid Build Coastguard Worker    echo "  --device-variant=<device_variant>"
82*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        [Optional] Device variant such as userdebug, user, or eng."
83*2f2c4c7aSAndroid Build Coastguard Worker    echo "                        If not specified, will be userdebug by default."
84*2f2c4c7aSAndroid Build Coastguard Worker    echo "  -h, --help            Display this help message and exit"
85*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
86*2f2c4c7aSAndroid Build Coastguard Worker    echo "Examples:"
87*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0"
88*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 1C141FDEE003FH"
89*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 1C141FDEE003FH -pb ab://git_main/raven-userdebug/latest"
90*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 1C141FDEE003FH -pb ~/aosp-main"
91*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 1C141FDEE003FH -vkb ~/pixel-mainline -pb ab://git_main/raven-trunk_staging-userdebug/latest"
92*2f2c4c7aSAndroid Build Coastguard Worker    echo "$0 -s 1C141FDEE003FH -vkb ab://kernel-android-gs-pixel-mainline/kernel_raviole_kleaf/latest \
93*2f2c4c7aSAndroid Build Coastguard Worker-pb ab://git_trunk_pixel_kernel_61-release/raven-userdebug/latest \
94*2f2c4c7aSAndroid Build Coastguard Worker-kb ab://aosp_kernel-common-android-mainline/kernel_aarch64/latest"
95*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
96*2f2c4c7aSAndroid Build Coastguard Worker    exit 0
97*2f2c4c7aSAndroid Build Coastguard Worker}
98*2f2c4c7aSAndroid Build Coastguard Worker
99*2f2c4c7aSAndroid Build Coastguard Workerfunction parse_arg() {
100*2f2c4c7aSAndroid Build Coastguard Worker    while test $# -gt 0; do
101*2f2c4c7aSAndroid Build Coastguard Worker        case "$1" in
102*2f2c4c7aSAndroid Build Coastguard Worker            -h|--help)
103*2f2c4c7aSAndroid Build Coastguard Worker                print_help
104*2f2c4c7aSAndroid Build Coastguard Worker                ;;
105*2f2c4c7aSAndroid Build Coastguard Worker            -s)
106*2f2c4c7aSAndroid Build Coastguard Worker                shift
107*2f2c4c7aSAndroid Build Coastguard Worker                if test $# -gt 0; then
108*2f2c4c7aSAndroid Build Coastguard Worker                    SERIAL_NUMBER=$1
109*2f2c4c7aSAndroid Build Coastguard Worker                else
110*2f2c4c7aSAndroid Build Coastguard Worker                    print_error "device serial is not specified"
111*2f2c4c7aSAndroid Build Coastguard Worker                fi
112*2f2c4c7aSAndroid Build Coastguard Worker                shift
113*2f2c4c7aSAndroid Build Coastguard Worker                ;;
114*2f2c4c7aSAndroid Build Coastguard Worker            --serial*)
115*2f2c4c7aSAndroid Build Coastguard Worker                SERIAL_NUMBER=$(echo $1 | sed -e "s/^[^=]*=//g")
116*2f2c4c7aSAndroid Build Coastguard Worker                shift
117*2f2c4c7aSAndroid Build Coastguard Worker                ;;
118*2f2c4c7aSAndroid Build Coastguard Worker            --skip-build)
119*2f2c4c7aSAndroid Build Coastguard Worker                SKIP_BUILD=true
120*2f2c4c7aSAndroid Build Coastguard Worker                shift
121*2f2c4c7aSAndroid Build Coastguard Worker                ;;
122*2f2c4c7aSAndroid Build Coastguard Worker            -pb)
123*2f2c4c7aSAndroid Build Coastguard Worker                shift
124*2f2c4c7aSAndroid Build Coastguard Worker                if test $# -gt 0; then
125*2f2c4c7aSAndroid Build Coastguard Worker                    PLATFORM_BUILD=$1
126*2f2c4c7aSAndroid Build Coastguard Worker                else
127*2f2c4c7aSAndroid Build Coastguard Worker                    print_error "platform build is not specified"
128*2f2c4c7aSAndroid Build Coastguard Worker                fi
129*2f2c4c7aSAndroid Build Coastguard Worker                shift
130*2f2c4c7aSAndroid Build Coastguard Worker                ;;
131*2f2c4c7aSAndroid Build Coastguard Worker            --platform-build=*)
132*2f2c4c7aSAndroid Build Coastguard Worker                PLATFORM_BUILD=$(echo $1 | sed -e "s/^[^=]*=//g")
133*2f2c4c7aSAndroid Build Coastguard Worker                shift
134*2f2c4c7aSAndroid Build Coastguard Worker                ;;
135*2f2c4c7aSAndroid Build Coastguard Worker            -sb)
136*2f2c4c7aSAndroid Build Coastguard Worker                shift
137*2f2c4c7aSAndroid Build Coastguard Worker                if test $# -gt 0; then
138*2f2c4c7aSAndroid Build Coastguard Worker                    SYSTEM_BUILD=$1
139*2f2c4c7aSAndroid Build Coastguard Worker                else
140*2f2c4c7aSAndroid Build Coastguard Worker                    print_error "system build is not specified"
141*2f2c4c7aSAndroid Build Coastguard Worker                fi
142*2f2c4c7aSAndroid Build Coastguard Worker                shift
143*2f2c4c7aSAndroid Build Coastguard Worker                ;;
144*2f2c4c7aSAndroid Build Coastguard Worker            --system-build=*)
145*2f2c4c7aSAndroid Build Coastguard Worker                SYSTEM_BUILD=$(echo $1 | sed -e "s/^[^=]*=//g")
146*2f2c4c7aSAndroid Build Coastguard Worker                shift
147*2f2c4c7aSAndroid Build Coastguard Worker                ;;
148*2f2c4c7aSAndroid Build Coastguard Worker            -kb)
149*2f2c4c7aSAndroid Build Coastguard Worker                shift
150*2f2c4c7aSAndroid Build Coastguard Worker                if test $# -gt 0; then
151*2f2c4c7aSAndroid Build Coastguard Worker                    KERNEL_BUILD=$1
152*2f2c4c7aSAndroid Build Coastguard Worker                else
153*2f2c4c7aSAndroid Build Coastguard Worker                    print_error "kernel build path is not specified"
154*2f2c4c7aSAndroid Build Coastguard Worker                fi
155*2f2c4c7aSAndroid Build Coastguard Worker                shift
156*2f2c4c7aSAndroid Build Coastguard Worker                ;;
157*2f2c4c7aSAndroid Build Coastguard Worker            --kernel-build=*)
158*2f2c4c7aSAndroid Build Coastguard Worker                KERNEL_BUILD=$(echo $1 | sed -e "s/^[^=]*=//g")
159*2f2c4c7aSAndroid Build Coastguard Worker                shift
160*2f2c4c7aSAndroid Build Coastguard Worker                ;;
161*2f2c4c7aSAndroid Build Coastguard Worker            -vkb)
162*2f2c4c7aSAndroid Build Coastguard Worker                shift
163*2f2c4c7aSAndroid Build Coastguard Worker                if test $# -gt 0; then
164*2f2c4c7aSAndroid Build Coastguard Worker                    VENDOR_KERNEL_BUILD=$1
165*2f2c4c7aSAndroid Build Coastguard Worker                else
166*2f2c4c7aSAndroid Build Coastguard Worker                    print_error "vendor kernel build path is not specified"
167*2f2c4c7aSAndroid Build Coastguard Worker                fi
168*2f2c4c7aSAndroid Build Coastguard Worker                shift
169*2f2c4c7aSAndroid Build Coastguard Worker                ;;
170*2f2c4c7aSAndroid Build Coastguard Worker            --vendor-kernel-build=*)
171*2f2c4c7aSAndroid Build Coastguard Worker                VENDOR_KERNEL_BUILD=$(echo $1 | sed -e "s/^[^=]*=//g")
172*2f2c4c7aSAndroid Build Coastguard Worker                shift
173*2f2c4c7aSAndroid Build Coastguard Worker                ;;
174*2f2c4c7aSAndroid Build Coastguard Worker            -vkbt)
175*2f2c4c7aSAndroid Build Coastguard Worker                shift
176*2f2c4c7aSAndroid Build Coastguard Worker                if test $# -gt 0; then
177*2f2c4c7aSAndroid Build Coastguard Worker                    VENDOR_KERNEL_BUILD_TARGET=$1
178*2f2c4c7aSAndroid Build Coastguard Worker                else
179*2f2c4c7aSAndroid Build Coastguard Worker                    print_error "vendor kernel build target is not specified"
180*2f2c4c7aSAndroid Build Coastguard Worker                fi
181*2f2c4c7aSAndroid Build Coastguard Worker                shift
182*2f2c4c7aSAndroid Build Coastguard Worker                ;;
183*2f2c4c7aSAndroid Build Coastguard Worker            --vendor-kernel-build-target=*)
184*2f2c4c7aSAndroid Build Coastguard Worker                VENDOR_KERNEL_BUILD_TARGET=$(echo $1 | sed -e "s/^[^=]*=//g")
185*2f2c4c7aSAndroid Build Coastguard Worker                shift
186*2f2c4c7aSAndroid Build Coastguard Worker                ;;
187*2f2c4c7aSAndroid Build Coastguard Worker            --device-variant=*)
188*2f2c4c7aSAndroid Build Coastguard Worker                DEVICE_VARIANT=$(echo $1 | sed -e "s/^[^=]*=//g")
189*2f2c4c7aSAndroid Build Coastguard Worker                shift
190*2f2c4c7aSAndroid Build Coastguard Worker                ;;
191*2f2c4c7aSAndroid Build Coastguard Worker            --gcov)
192*2f2c4c7aSAndroid Build Coastguard Worker                GCOV=true
193*2f2c4c7aSAndroid Build Coastguard Worker                shift
194*2f2c4c7aSAndroid Build Coastguard Worker                ;;
195*2f2c4c7aSAndroid Build Coastguard Worker            --debug)
196*2f2c4c7aSAndroid Build Coastguard Worker                DEBUG=true
197*2f2c4c7aSAndroid Build Coastguard Worker                shift
198*2f2c4c7aSAndroid Build Coastguard Worker                ;;
199*2f2c4c7aSAndroid Build Coastguard Worker            --kasan)
200*2f2c4c7aSAndroid Build Coastguard Worker                KASAN=true
201*2f2c4c7aSAndroid Build Coastguard Worker                shift
202*2f2c4c7aSAndroid Build Coastguard Worker                ;;
203*2f2c4c7aSAndroid Build Coastguard Worker            *)
204*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Unsupported flag: $1" >&2
205*2f2c4c7aSAndroid Build Coastguard Worker                shift
206*2f2c4c7aSAndroid Build Coastguard Worker                ;;
207*2f2c4c7aSAndroid Build Coastguard Worker        esac
208*2f2c4c7aSAndroid Build Coastguard Worker    done
209*2f2c4c7aSAndroid Build Coastguard Worker}
210*2f2c4c7aSAndroid Build Coastguard Worker
211*2f2c4c7aSAndroid Build Coastguard Workerfunction adb_checker() {
212*2f2c4c7aSAndroid Build Coastguard Worker    if ! which adb &> /dev/null; then
213*2f2c4c7aSAndroid Build Coastguard Worker        print_error "adb not found!"
214*2f2c4c7aSAndroid Build Coastguard Worker    fi
215*2f2c4c7aSAndroid Build Coastguard Worker}
216*2f2c4c7aSAndroid Build Coastguard Worker
217*2f2c4c7aSAndroid Build Coastguard Workerfunction go_to_repo_root() {
218*2f2c4c7aSAndroid Build Coastguard Worker    current_dir="$1"
219*2f2c4c7aSAndroid Build Coastguard Worker    while [ ! -d ".repo" ] && [ "$current_dir" != "/" ]; do
220*2f2c4c7aSAndroid Build Coastguard Worker        current_dir=$(dirname "$current_dir")  # Go up one directory
221*2f2c4c7aSAndroid Build Coastguard Worker        cd "$current_dir"
222*2f2c4c7aSAndroid Build Coastguard Worker    done
223*2f2c4c7aSAndroid Build Coastguard Worker}
224*2f2c4c7aSAndroid Build Coastguard Worker
225*2f2c4c7aSAndroid Build Coastguard Workerfunction print_info() {
226*2f2c4c7aSAndroid Build Coastguard Worker    local log_prompt=$MY_NAME
227*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$2" ]; then
228*2f2c4c7aSAndroid Build Coastguard Worker        log_prompt+=" line $2"
229*2f2c4c7aSAndroid Build Coastguard Worker    fi
230*2f2c4c7aSAndroid Build Coastguard Worker    echo "[$log_prompt]: ${GREEN}$1${END}"
231*2f2c4c7aSAndroid Build Coastguard Worker}
232*2f2c4c7aSAndroid Build Coastguard Worker
233*2f2c4c7aSAndroid Build Coastguard Workerfunction print_warn() {
234*2f2c4c7aSAndroid Build Coastguard Worker    local log_prompt=$MY_NAME
235*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$2" ]; then
236*2f2c4c7aSAndroid Build Coastguard Worker        log_prompt+=" line $2"
237*2f2c4c7aSAndroid Build Coastguard Worker    fi
238*2f2c4c7aSAndroid Build Coastguard Worker    echo "[$log_prompt]: ${ORANGE}$1${END}"
239*2f2c4c7aSAndroid Build Coastguard Worker}
240*2f2c4c7aSAndroid Build Coastguard Worker
241*2f2c4c7aSAndroid Build Coastguard Workerfunction print_error() {
242*2f2c4c7aSAndroid Build Coastguard Worker    local log_prompt=$MY_NAME
243*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$2" ]; then
244*2f2c4c7aSAndroid Build Coastguard Worker        log_prompt+=" line $2"
245*2f2c4c7aSAndroid Build Coastguard Worker    fi
246*2f2c4c7aSAndroid Build Coastguard Worker    echo -e "[$log_prompt]: ${RED}$1${END}"
247*2f2c4c7aSAndroid Build Coastguard Worker    cd $OLD_PWD
248*2f2c4c7aSAndroid Build Coastguard Worker    exit 1
249*2f2c4c7aSAndroid Build Coastguard Worker}
250*2f2c4c7aSAndroid Build Coastguard Worker
251*2f2c4c7aSAndroid Build Coastguard Workerfunction set_platform_repo () {
252*2f2c4c7aSAndroid Build Coastguard Worker    print_warn "Build environment target product '${TARGET_PRODUCT}' does not match expected $1. \
253*2f2c4c7aSAndroid Build Coastguard Worker    Reset build environment" "$LINENO"
254*2f2c4c7aSAndroid Build Coastguard Worker    local lunch_cli="source build/envsetup.sh && lunch $1"
255*2f2c4c7aSAndroid Build Coastguard Worker    if [ -f "build/release/release_configs/trunk_staging.textproto" ]; then
256*2f2c4c7aSAndroid Build Coastguard Worker        lunch_cli+="-trunk_staging-$DEVICE_VARIANT"
257*2f2c4c7aSAndroid Build Coastguard Worker    else
258*2f2c4c7aSAndroid Build Coastguard Worker        lunch_cli+="-$DEVICE_VARIANT"
259*2f2c4c7aSAndroid Build Coastguard Worker    fi
260*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Setup build environment with: $lunch_cli" "$LINENO"
261*2f2c4c7aSAndroid Build Coastguard Worker    eval "$lunch_cli"
262*2f2c4c7aSAndroid Build Coastguard Worker    exit_code=$?
263*2f2c4c7aSAndroid Build Coastguard Worker    if [ $exit_code -eq 0 ]; then
264*2f2c4c7aSAndroid Build Coastguard Worker        print_info "$lunch_cli succeeded" "$LINENO"
265*2f2c4c7aSAndroid Build Coastguard Worker    else
266*2f2c4c7aSAndroid Build Coastguard Worker        print_error "$lunch_cli failed" "$LINENO"
267*2f2c4c7aSAndroid Build Coastguard Worker    fi
268*2f2c4c7aSAndroid Build Coastguard Worker}
269*2f2c4c7aSAndroid Build Coastguard Worker
270*2f2c4c7aSAndroid Build Coastguard Workerfunction find_repo () {
271*2f2c4c7aSAndroid Build Coastguard Worker    manifest_output=$(grep -e "superproject" -e "gs-pixel" -e "kernel/private/devices/google/common" \
272*2f2c4c7aSAndroid Build Coastguard Worker     -e "private/google-modules/soc/gs" -e "kernel/common" -e "common-modules/virtual-device" \
273*2f2c4c7aSAndroid Build Coastguard Worker     .repo/manifests/default.xml)
274*2f2c4c7aSAndroid Build Coastguard Worker    case "$manifest_output" in
275*2f2c4c7aSAndroid Build Coastguard Worker        *platform/superproject*)
276*2f2c4c7aSAndroid Build Coastguard Worker            PLATFORM_REPO_ROOT="$PWD"
277*2f2c4c7aSAndroid Build Coastguard Worker            PLATFORM_VERSION=$(grep -e "platform/superproject" .repo/manifests/default.xml | \
278*2f2c4c7aSAndroid Build Coastguard Worker            grep -oP 'revision="\K[^"]*')
279*2f2c4c7aSAndroid Build Coastguard Worker            print_info "PLATFORM_REPO_ROOT=$PLATFORM_REPO_ROOT, PLATFORM_VERSION=$PLATFORM_VERSION" "$LINENO"
280*2f2c4c7aSAndroid Build Coastguard Worker            if [ -z "$PLATFORM_BUILD" ]; then
281*2f2c4c7aSAndroid Build Coastguard Worker                PLATFORM_BUILD="$PLATFORM_REPO_ROOT"
282*2f2c4c7aSAndroid Build Coastguard Worker            elif [[ "$PLATFORM_BUILD" == "None" ]]; then
283*2f2c4c7aSAndroid Build Coastguard Worker                PLATFORM_BUILD=
284*2f2c4c7aSAndroid Build Coastguard Worker            fi
285*2f2c4c7aSAndroid Build Coastguard Worker            ;;
286*2f2c4c7aSAndroid Build Coastguard Worker        *kernel/private/devices/google/common*|*private/google-modules/soc/gs*)
287*2f2c4c7aSAndroid Build Coastguard Worker            VENDOR_KERNEL_REPO_ROOT="$PWD"
288*2f2c4c7aSAndroid Build Coastguard Worker            VENDOR_KERNEL_VERSION=$(grep -e "default revision" .repo/manifests/default.xml | \
289*2f2c4c7aSAndroid Build Coastguard Worker            grep -oP 'revision="\K[^"]*')
290*2f2c4c7aSAndroid Build Coastguard Worker            print_info "VENDOR_KERNEL_REPO_ROOT=$VENDOR_KERNEL_REPO_ROOT" "$LINENO"
291*2f2c4c7aSAndroid Build Coastguard Worker            print_info "VENDOR_KERNEL_VERSION=$VENDOR_KERNEL_VERSION" "$LINENO"
292*2f2c4c7aSAndroid Build Coastguard Worker            if [ -z "$VENDOR_KERNEL_BUILD" ]; then
293*2f2c4c7aSAndroid Build Coastguard Worker                VENDOR_KERNEL_BUILD="$VENDOR_KERNEL_REPO_ROOT"
294*2f2c4c7aSAndroid Build Coastguard Worker            fi
295*2f2c4c7aSAndroid Build Coastguard Worker            ;;
296*2f2c4c7aSAndroid Build Coastguard Worker        *common-modules/virtual-device*)
297*2f2c4c7aSAndroid Build Coastguard Worker            KERNEL_REPO_ROOT="$PWD"
298*2f2c4c7aSAndroid Build Coastguard Worker            KERNEL_VERSION=$(grep -e "kernel/superproject" \
299*2f2c4c7aSAndroid Build Coastguard Worker            .repo/manifests/default.xml | grep -oP 'revision="common-\K[^"]*')
300*2f2c4c7aSAndroid Build Coastguard Worker            print_info "KERNEL_REPO_ROOT=$KERNEL_REPO_ROOT, KERNEL_VERSION=$KERNEL_VERSION" "$LINENO"
301*2f2c4c7aSAndroid Build Coastguard Worker            if [ -z "$KERNEL_BUILD" ]; then
302*2f2c4c7aSAndroid Build Coastguard Worker                KERNEL_BUILD="$KERNEL_REPO_ROOT"
303*2f2c4c7aSAndroid Build Coastguard Worker            fi
304*2f2c4c7aSAndroid Build Coastguard Worker            ;;
305*2f2c4c7aSAndroid Build Coastguard Worker        *)
306*2f2c4c7aSAndroid Build Coastguard Worker            print_warn "Unknown manifest output. Could not determine repository type." "$LINENO"
307*2f2c4c7aSAndroid Build Coastguard Worker            ;;
308*2f2c4c7aSAndroid Build Coastguard Worker    esac
309*2f2c4c7aSAndroid Build Coastguard Worker}
310*2f2c4c7aSAndroid Build Coastguard Worker
311*2f2c4c7aSAndroid Build Coastguard Workerfunction build_platform () {
312*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$SKIP_BUILD" = true ]]; then
313*2f2c4c7aSAndroid Build Coastguard Worker        print_warn "--skip-build is set. Do not rebuild platform build" "$LINENO"
314*2f2c4c7aSAndroid Build Coastguard Worker        return
315*2f2c4c7aSAndroid Build Coastguard Worker    fi
316*2f2c4c7aSAndroid Build Coastguard Worker    build_cmd="m -j12 ; make otatools -j12 ; make dist -j12"
317*2f2c4c7aSAndroid Build Coastguard Worker    print_warn "Flag --skip-build is not set. Rebuilt images at $PWD with: $build_cmd" "$LINENO"
318*2f2c4c7aSAndroid Build Coastguard Worker    eval $build_cmd
319*2f2c4c7aSAndroid Build Coastguard Worker    exit_code=$?
320*2f2c4c7aSAndroid Build Coastguard Worker    if [ $exit_code -eq 1 ]; then
321*2f2c4c7aSAndroid Build Coastguard Worker        print_warn "$build_cmd returned exit_code $exit_code" "$LINENO"
322*2f2c4c7aSAndroid Build Coastguard Worker        print_error "$build_cmd failed" "$LINENO"
323*2f2c4c7aSAndroid Build Coastguard Worker    else
324*2f2c4c7aSAndroid Build Coastguard Worker        if [ -f "${ANDROID_PRODUCT_OUT}/system.img" ]; then
325*2f2c4c7aSAndroid Build Coastguard Worker            print_info "${ANDROID_PRODUCT_OUT}/system.img exist" "$LINENO"
326*2f2c4c7aSAndroid Build Coastguard Worker        else
327*2f2c4c7aSAndroid Build Coastguard Worker            print_error "${ANDROID_PRODUCT_OUT}/system.img doesn't exist" "$LINENO"
328*2f2c4c7aSAndroid Build Coastguard Worker        fi
329*2f2c4c7aSAndroid Build Coastguard Worker    fi
330*2f2c4c7aSAndroid Build Coastguard Worker}
331*2f2c4c7aSAndroid Build Coastguard Worker
332*2f2c4c7aSAndroid Build Coastguard Workerfunction build_ack () {
333*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$SKIP_BUILD" = true ]]; then
334*2f2c4c7aSAndroid Build Coastguard Worker        print_warn "--skip-build is set. Do not rebuild kernel" "$LINENO"
335*2f2c4c7aSAndroid Build Coastguard Worker        return
336*2f2c4c7aSAndroid Build Coastguard Worker    fi
337*2f2c4c7aSAndroid Build Coastguard Worker    build_cmd="tools/bazel run --config=fast"
338*2f2c4c7aSAndroid Build Coastguard Worker    if [ "$GCOV" = true ]; then
339*2f2c4c7aSAndroid Build Coastguard Worker        build_cmd+=" --gcov"
340*2f2c4c7aSAndroid Build Coastguard Worker    fi
341*2f2c4c7aSAndroid Build Coastguard Worker    if [ "$DEBUG" = true ]; then
342*2f2c4c7aSAndroid Build Coastguard Worker        build_cmd+=" --debug"
343*2f2c4c7aSAndroid Build Coastguard Worker    fi
344*2f2c4c7aSAndroid Build Coastguard Worker    if [ "$KASAN" = true ]; then
345*2f2c4c7aSAndroid Build Coastguard Worker        build_cmd+=" --kasan"
346*2f2c4c7aSAndroid Build Coastguard Worker    fi
347*2f2c4c7aSAndroid Build Coastguard Worker    build_cmd+=" //common:kernel_aarch64_dist"
348*2f2c4c7aSAndroid Build Coastguard Worker    print_warn "Flag --skip-build is not set. Rebuild the kernel with: $build_cmd." "$LINENO"
349*2f2c4c7aSAndroid Build Coastguard Worker    eval $build_cmd
350*2f2c4c7aSAndroid Build Coastguard Worker    exit_code=$?
351*2f2c4c7aSAndroid Build Coastguard Worker    if [ $exit_code -eq 0 ]; then
352*2f2c4c7aSAndroid Build Coastguard Worker        print_info "$build_cmd succeeded" "$LINENO"
353*2f2c4c7aSAndroid Build Coastguard Worker    else
354*2f2c4c7aSAndroid Build Coastguard Worker        print_error "$build_cmd failed" "$LINENO"
355*2f2c4c7aSAndroid Build Coastguard Worker    fi
356*2f2c4c7aSAndroid Build Coastguard Worker}
357*2f2c4c7aSAndroid Build Coastguard Worker
358*2f2c4c7aSAndroid Build Coastguard Workerfunction download_platform_build() {
359*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Downloading $1 to $PWD" "$LINENO"
360*2f2c4c7aSAndroid Build Coastguard Worker    local build_info="$1"
361*2f2c4c7aSAndroid Build Coastguard Worker    local file_patterns=("*$PRODUCT-img-*.zip" "bootloader.img" "radio.img" "misc_info.txt" "otatools.zip")
362*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$1" == *"user/"* ]]; then
363*2f2c4c7aSAndroid Build Coastguard Worker        file_patterns+=("vendor_ramdisk-debug.img")
364*2f2c4c7aSAndroid Build Coastguard Worker    else
365*2f2c4c7aSAndroid Build Coastguard Worker        file_patterns+=("vendor_ramdisk.img")
366*2f2c4c7aSAndroid Build Coastguard Worker    fi
367*2f2c4c7aSAndroid Build Coastguard Worker
368*2f2c4c7aSAndroid Build Coastguard Worker    echo "Downloading ${file_patterns[@]} from $build_info"
369*2f2c4c7aSAndroid Build Coastguard Worker    for pattern in "${file_patterns[@]}"; do
370*2f2c4c7aSAndroid Build Coastguard Worker        download_file_name="$build_info/$pattern"
371*2f2c4c7aSAndroid Build Coastguard Worker        eval "$FETCH_SCRIPT $download_file_name"
372*2f2c4c7aSAndroid Build Coastguard Worker        exit_code=$?
373*2f2c4c7aSAndroid Build Coastguard Worker        if [ $exit_code -eq 0 ]; then
374*2f2c4c7aSAndroid Build Coastguard Worker            print_info "Download $download_file_name succeeded" "$LINENO"
375*2f2c4c7aSAndroid Build Coastguard Worker        else
376*2f2c4c7aSAndroid Build Coastguard Worker            print_error "Download $download_file_name failed" "$LINENO"
377*2f2c4c7aSAndroid Build Coastguard Worker        fi
378*2f2c4c7aSAndroid Build Coastguard Worker        if [[ "$pattern" == "vendor_ramdisk-debug.img" ]]; then
379*2f2c4c7aSAndroid Build Coastguard Worker            cp vendor_ramdisk-debug.img vendor_ramdisk.img
380*2f2c4c7aSAndroid Build Coastguard Worker        fi
381*2f2c4c7aSAndroid Build Coastguard Worker    done
382*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
383*2f2c4c7aSAndroid Build Coastguard Worker}
384*2f2c4c7aSAndroid Build Coastguard Worker
385*2f2c4c7aSAndroid Build Coastguard Workerfunction download_gki_build() {
386*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Downloading $1 to $PWD" "$LINENO"
387*2f2c4c7aSAndroid Build Coastguard Worker    local build_info="$1"
388*2f2c4c7aSAndroid Build Coastguard Worker    local file_patterns=("Image.lz4" "boot-lz4.img" "system_dlkm_staging_archive.tar.gz" "system_dlkm.flatten.ext4.img" "system_dlkm.flatten.erofs.img")
389*2f2c4c7aSAndroid Build Coastguard Worker
390*2f2c4c7aSAndroid Build Coastguard Worker    echo "Downloading ${file_patterns[@]} from $build_info"
391*2f2c4c7aSAndroid Build Coastguard Worker    for pattern in "${file_patterns[@]}"; do
392*2f2c4c7aSAndroid Build Coastguard Worker        download_file_name="$build_info/$pattern"
393*2f2c4c7aSAndroid Build Coastguard Worker        eval "$FETCH_SCRIPT $download_file_name"
394*2f2c4c7aSAndroid Build Coastguard Worker        exit_code=$?
395*2f2c4c7aSAndroid Build Coastguard Worker        if [ $exit_code -eq 0 ]; then
396*2f2c4c7aSAndroid Build Coastguard Worker            print_info "Download $download_file_name succeeded" "$LINENO"
397*2f2c4c7aSAndroid Build Coastguard Worker        else
398*2f2c4c7aSAndroid Build Coastguard Worker            print_error "Download $download_file_name failed" "$LINENO"
399*2f2c4c7aSAndroid Build Coastguard Worker        fi
400*2f2c4c7aSAndroid Build Coastguard Worker    done
401*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
402*2f2c4c7aSAndroid Build Coastguard Worker}
403*2f2c4c7aSAndroid Build Coastguard Worker
404*2f2c4c7aSAndroid Build Coastguard Workerfunction download_vendor_kernel_build() {
405*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Downloading $1 to $PWD" "$LINENO"
406*2f2c4c7aSAndroid Build Coastguard Worker    local build_info="$1"
407*2f2c4c7aSAndroid Build Coastguard Worker    local file_patterns=("vendor_dlkm_staging_archive.tar.gz" "Image.lz4" "dtbo.img" \
408*2f2c4c7aSAndroid Build Coastguard Worker    "initramfs.img" "vendor_dlkm.img" "boot.img" "vendor_dlkm.modules.blocklist" "vendor_dlkm.modules.load" )
409*2f2c4c7aSAndroid Build Coastguard Worker
410*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$VENDOR_KERNEL_VERSION" == *"6.6" ]]; then
411*2f2c4c7aSAndroid Build Coastguard Worker        file_patterns+=("*vendor_dev_nodes_fragment.img")
412*2f2c4c7aSAndroid Build Coastguard Worker    fi
413*2f2c4c7aSAndroid Build Coastguard Worker
414*2f2c4c7aSAndroid Build Coastguard Worker    case "$PRODUCT" in
415*2f2c4c7aSAndroid Build Coastguard Worker        oriole | raven | bluejay)
416*2f2c4c7aSAndroid Build Coastguard Worker            file_patterns+=( "gs101-a0.dtb" "gs101-b0.dtb")
417*2f2c4c7aSAndroid Build Coastguard Worker            ;;
418*2f2c4c7aSAndroid Build Coastguard Worker        *)
419*2f2c4c7aSAndroid Build Coastguard Worker            ;;
420*2f2c4c7aSAndroid Build Coastguard Worker    esac
421*2f2c4c7aSAndroid Build Coastguard Worker
422*2f2c4c7aSAndroid Build Coastguard Worker    echo "Downloading ${file_patterns[@]} from $build_info"
423*2f2c4c7aSAndroid Build Coastguard Worker    for pattern in "${file_patterns[@]}"; do
424*2f2c4c7aSAndroid Build Coastguard Worker        download_file_name="$build_info/$pattern"
425*2f2c4c7aSAndroid Build Coastguard Worker        eval "$FETCH_SCRIPT $download_file_name"
426*2f2c4c7aSAndroid Build Coastguard Worker        exit_code=$?
427*2f2c4c7aSAndroid Build Coastguard Worker        if [ $exit_code -eq 0 ]; then
428*2f2c4c7aSAndroid Build Coastguard Worker            print_info "Download $download_file_name succeeded" "$LINENO"
429*2f2c4c7aSAndroid Build Coastguard Worker        else
430*2f2c4c7aSAndroid Build Coastguard Worker            print_error "Download $download_file_name failed" "$LINENO"
431*2f2c4c7aSAndroid Build Coastguard Worker        fi
432*2f2c4c7aSAndroid Build Coastguard Worker    done
433*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
434*2f2c4c7aSAndroid Build Coastguard Worker}
435*2f2c4c7aSAndroid Build Coastguard Worker
436*2f2c4c7aSAndroid Build Coastguard Workerfunction flash_gki_build() {
437*2f2c4c7aSAndroid Build Coastguard Worker    local boot_image_name
438*2f2c4c7aSAndroid Build Coastguard Worker    local system_dlkm_image_name
439*2f2c4c7aSAndroid Build Coastguard Worker
440*2f2c4c7aSAndroid Build Coastguard Worker    case "$PRODUCT" in
441*2f2c4c7aSAndroid Build Coastguard Worker        oriole | raven | bluejay)
442*2f2c4c7aSAndroid Build Coastguard Worker            boot_image_name="boot-lz4.img"
443*2f2c4c7aSAndroid Build Coastguard Worker            # no system_dlkm partition
444*2f2c4c7aSAndroid Build Coastguard Worker            ;;
445*2f2c4c7aSAndroid Build Coastguard Worker        eos | aurora | full_erd8835 | betty | kirkwood)
446*2f2c4c7aSAndroid Build Coastguard Worker            boot_image_name="boot.img"
447*2f2c4c7aSAndroid Build Coastguard Worker            if [[ "$PRODUCT" == "kirkwood" ]] && [[ ! "$KERNEL_VERSION" =~ ^android13 ]]; then  # Check if NOT android13
448*2f2c4c7aSAndroid Build Coastguard Worker                system_dlkm_image_name="system_dlkm.flatten.erofs.img"
449*2f2c4c7aSAndroid Build Coastguard Worker            # no system_dlkm for android12 & android13
450*2f2c4c7aSAndroid Build Coastguard Worker            elif [[ ! "$KERNEL_VERSION" =~ ^android12 ]] && [[ ! "$KERNEL_VERSION" =~ ^android13 ]]; then  # Check if NOT android12 AND NOT android13
451*2f2c4c7aSAndroid Build Coastguard Worker                system_dlkm_image_name="system_dlkm.flatten.erofs.img"
452*2f2c4c7aSAndroid Build Coastguard Worker            fi
453*2f2c4c7aSAndroid Build Coastguard Worker            ;;
454*2f2c4c7aSAndroid Build Coastguard Worker        k6985v1 | k6989v1)
455*2f2c4c7aSAndroid Build Coastguard Worker            boot_image_name="boot-gz.img"
456*2f2c4c7aSAndroid Build Coastguard Worker            # no system_dlkm for android12 & android13
457*2f2c4c7aSAndroid Build Coastguard Worker            if [[ ! "$KERNEL_VERSION" =~ ^android12 ]] && [[ ! "$KERNEL_VERSION" =~ ^android13 ]]; then  # Check if NOT android12 AND NOT android13
458*2f2c4c7aSAndroid Build Coastguard Worker                system_dlkm_image_name="system_dlkm.flatten.ext4.img"
459*2f2c4c7aSAndroid Build Coastguard Worker            fi
460*2f2c4c7aSAndroid Build Coastguard Worker            ;;
461*2f2c4c7aSAndroid Build Coastguard Worker        *)
462*2f2c4c7aSAndroid Build Coastguard Worker            boot_image_name="boot-lz4.img"
463*2f2c4c7aSAndroid Build Coastguard Worker            # no system_dlkm for android12 & android13
464*2f2c4c7aSAndroid Build Coastguard Worker            if [[ ! "$KERNEL_VERSION" =~ ^android12 ]] && [[ ! "$KERNEL_VERSION" =~ ^android13 ]]; then # Check if NOT android12 AND NOT android13
465*2f2c4c7aSAndroid Build Coastguard Worker                system_dlkm_image_name="system_dlkm.flatten.ext4.img"
466*2f2c4c7aSAndroid Build Coastguard Worker            fi
467*2f2c4c7aSAndroid Build Coastguard Worker            ;;
468*2f2c4c7aSAndroid Build Coastguard Worker    esac
469*2f2c4c7aSAndroid Build Coastguard Worker
470*2f2c4c7aSAndroid Build Coastguard Worker    if [ -z "$TRADEFED" ]; then
471*2f2c4c7aSAndroid Build Coastguard Worker        find_tradefed_bin
472*2f2c4c7aSAndroid Build Coastguard Worker    fi
473*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d "$DOWNLOAD_PATH/tf_gki_kernel_dir" ]; then
474*2f2c4c7aSAndroid Build Coastguard Worker        rm -rf "$DOWNLOAD_PATH/tf_gki_kernel_dir"
475*2f2c4c7aSAndroid Build Coastguard Worker    fi
476*2f2c4c7aSAndroid Build Coastguard Worker    local kernel_dir="$DOWNLOAD_PATH/tf_gki_kernel_dir"
477*2f2c4c7aSAndroid Build Coastguard Worker    mkdir -p "$kernel_dir"
478*2f2c4c7aSAndroid Build Coastguard Worker    cd "$vendor_kernel_dir" || $(print_error "Fail to go to $gki_kernel_dir" "$LINENO")
479*2f2c4c7aSAndroid Build Coastguard Worker    cp "$KERNEL_BUILD/$boot_image_name" "$kernel_dir" || $(print_error "Fail to copy $KERNEL_BUILD/$boot_image_name" "$LINENO")
480*2f2c4c7aSAndroid Build Coastguard Worker    tf_cli="$TRADEFED \
481*2f2c4c7aSAndroid Build Coastguard Worker    run commandAndExit template/local_min --log-level-display VERBOSE \
482*2f2c4c7aSAndroid Build Coastguard Worker    --log-file-path=$LOG_DIR -s $SERIAL_NUMBER --disable-verity \
483*2f2c4c7aSAndroid Build Coastguard Worker    --template:map test=example/reboot --num-of-reboots 1 \
484*2f2c4c7aSAndroid Build Coastguard Worker    --template:map preparers=template/preparers/gki-device-flash-preparer \
485*2f2c4c7aSAndroid Build Coastguard Worker    --extra-file gki_boot.img=$kernel_dir/$boot_image_name"
486*2f2c4c7aSAndroid Build Coastguard Worker
487*2f2c4c7aSAndroid Build Coastguard Worker    # Check if system_dlkm_image_name is set before adding it to the command
488*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$system_dlkm_image_name" ]; then
489*2f2c4c7aSAndroid Build Coastguard Worker        cp "$KERNEL_BUILD/$system_dlkm_image_name" "$kernel_dir" || $(print_error "Fail to copy $KERNEL_BUILD/$system_dlkm_image_name" "$LINENO")
490*2f2c4c7aSAndroid Build Coastguard Worker        tf_cli+=" --extra-file system_dlkm.img=$kernel_dir/$system_dlkm_image_name"
491*2f2c4c7aSAndroid Build Coastguard Worker    fi
492*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Run $tf_cli" "$LINENO"
493*2f2c4c7aSAndroid Build Coastguard Worker    eval "$tf_cli" # Quote the variable expansion
494*2f2c4c7aSAndroid Build Coastguard Worker}
495*2f2c4c7aSAndroid Build Coastguard Worker
496*2f2c4c7aSAndroid Build Coastguard Workerfunction flash_vendor_kernel_build() {
497*2f2c4c7aSAndroid Build Coastguard Worker    if [ -z "$TRADEFED" ]; then
498*2f2c4c7aSAndroid Build Coastguard Worker        find_tradefed_bin
499*2f2c4c7aSAndroid Build Coastguard Worker    fi
500*2f2c4c7aSAndroid Build Coastguard Worker    local tf_cli="$TRADEFED run commandAndExit template/local_min --log-level-display VERBOSE \
501*2f2c4c7aSAndroid Build Coastguard Worker    --log-file-path=$LOG_DIR -s $SERIAL_NUMBER --disable-verity \
502*2f2c4c7aSAndroid Build Coastguard Worker    --template:map test=example/reboot --num-of-reboots 1 \
503*2f2c4c7aSAndroid Build Coastguard Worker    --template:map preparers=template/preparers/gki-device-flash-preparer"
504*2f2c4c7aSAndroid Build Coastguard Worker
505*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d "$DOWNLOAD_PATH/tf_vendor_kernel_dir" ]; then
506*2f2c4c7aSAndroid Build Coastguard Worker        rm -rf "$DOWNLOAD_PATH/tf_vendor_kernel_dir"
507*2f2c4c7aSAndroid Build Coastguard Worker    fi
508*2f2c4c7aSAndroid Build Coastguard Worker    local vendor_kernel_dir="$DOWNLOAD_PATH/tf_vendor_kernel_dir"
509*2f2c4c7aSAndroid Build Coastguard Worker    mkdir -p "$vendor_kernel_dir"
510*2f2c4c7aSAndroid Build Coastguard Worker    local file_patterns=("boot.img" "initramfs.img" "dtbo.img" "vendor_dlkm.img")
511*2f2c4c7aSAndroid Build Coastguard Worker    for pattern in "${file_patterns[@]}"; do
512*2f2c4c7aSAndroid Build Coastguard Worker        if [ ! -f "$VENDOR_KERNEL_BUILD/$pattern" ]; then
513*2f2c4c7aSAndroid Build Coastguard Worker            print_error "$VENDOR_KERNEL_BUILD/$pattern doesn't exist" "$LINENO"
514*2f2c4c7aSAndroid Build Coastguard Worker        fi
515*2f2c4c7aSAndroid Build Coastguard Worker        cp "$VENDOR_KERNEL_BUILD/$pattern" "$vendor_kernel_dir"
516*2f2c4c7aSAndroid Build Coastguard Worker        if [[ "$pattern" == "boot.img" ]]; then
517*2f2c4c7aSAndroid Build Coastguard Worker            tf_cli+=" --extra-file gki_boot.img=$vendor_kernel_dir/boot.img"
518*2f2c4c7aSAndroid Build Coastguard Worker        else
519*2f2c4c7aSAndroid Build Coastguard Worker            tf_cli+=" --extra-file $pattern=$vendor_kernel_dir/$pattern"
520*2f2c4c7aSAndroid Build Coastguard Worker        fi
521*2f2c4c7aSAndroid Build Coastguard Worker    done
522*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Run $tf_cli" "$LINENO"
523*2f2c4c7aSAndroid Build Coastguard Worker    eval $tf_cli
524*2f2c4c7aSAndroid Build Coastguard Worker}
525*2f2c4c7aSAndroid Build Coastguard Worker
526*2f2c4c7aSAndroid Build Coastguard Worker# Function to check and wait for an ADB device
527*2f2c4c7aSAndroid Build Coastguard Workerfunction wait_for_adb_device() {
528*2f2c4c7aSAndroid Build Coastguard Worker  local serial_number="$1"  # Optional serial number
529*2f2c4c7aSAndroid Build Coastguard Worker  local timeout_seconds="${2:-300}"  # Timeout in seconds (default 5 minutes)
530*2f2c4c7aSAndroid Build Coastguard Worker
531*2f2c4c7aSAndroid Build Coastguard Worker  local start_time=$(date +%s)
532*2f2c4c7aSAndroid Build Coastguard Worker  local end_time=$((start_time + timeout_seconds))
533*2f2c4c7aSAndroid Build Coastguard Worker
534*2f2c4c7aSAndroid Build Coastguard Worker  while (( $(date +%s) < end_time )); do
535*2f2c4c7aSAndroid Build Coastguard Worker    devices=$(adb devices | grep "$SERIAL_NUMBER" | wc -l)
536*2f2c4c7aSAndroid Build Coastguard Worker
537*2f2c4c7aSAndroid Build Coastguard Worker    if (( devices > 0 )); then
538*2f2c4c7aSAndroid Build Coastguard Worker      print_info "Device $SERIAL_NUMBER is connected with adb" "$LINENO"
539*2f2c4c7aSAndroid Build Coastguard Worker      return 0  # Success
540*2f2c4c7aSAndroid Build Coastguard Worker    fi
541*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Waiting for device $SERIAL_NUMBER in adb devies" "$LINENO"
542*2f2c4c7aSAndroid Build Coastguard Worker    sleep 1
543*2f2c4c7aSAndroid Build Coastguard Worker  done
544*2f2c4c7aSAndroid Build Coastguard Worker
545*2f2c4c7aSAndroid Build Coastguard Worker  print_error "Timeout waiting for $SERIAL_NUMBER in adb devices" "$LINENO"
546*2f2c4c7aSAndroid Build Coastguard Worker}
547*2f2c4c7aSAndroid Build Coastguard Worker
548*2f2c4c7aSAndroid Build Coastguard Workerfunction flash_platform_build() {
549*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$PLATFORM_BUILD" == ab://* ]] && [ -x "$FLASH_CLI" ]; then
550*2f2c4c7aSAndroid Build Coastguard Worker        local flash_cmd="$FLASH_CLI --nointeractive --force_flash_partitions --disable_verity -w -s $SERIAL_NUMBER "
551*2f2c4c7aSAndroid Build Coastguard Worker        IFS='/' read -ra array <<< "$PLATFORM_BUILD"
552*2f2c4c7aSAndroid Build Coastguard Worker        if [ ! -z "${array[3]}" ]; then
553*2f2c4c7aSAndroid Build Coastguard Worker            local _build_type="${array[3]#*-}"
554*2f2c4c7aSAndroid Build Coastguard Worker            if [[ "$_build_type" == *userdebug ]]; then
555*2f2c4c7aSAndroid Build Coastguard Worker                flash_cmd+=" -t $_build_type"
556*2f2c4c7aSAndroid Build Coastguard Worker            elif [[ "$_build_type" == *user ]]; then
557*2f2c4c7aSAndroid Build Coastguard Worker                flash_cmd+=" -t $_build_type --force_debuggable"
558*2f2c4c7aSAndroid Build Coastguard Worker            fi
559*2f2c4c7aSAndroid Build Coastguard Worker        fi
560*2f2c4c7aSAndroid Build Coastguard Worker        if [ ! -z "${array[4]}" ] && [[ "${array[4]}" != latest* ]]; then
561*2f2c4c7aSAndroid Build Coastguard Worker            echo "Flash $SERIAL_NUMBER with platform build from branch $PLATFORM_BUILD..."
562*2f2c4c7aSAndroid Build Coastguard Worker            flash_cmd+=" --bid ${array[4]}"
563*2f2c4c7aSAndroid Build Coastguard Worker        else
564*2f2c4c7aSAndroid Build Coastguard Worker            echo "Flash $SERIAL_NUMBER with platform build $PLATFORM_BUILD..."
565*2f2c4c7aSAndroid Build Coastguard Worker            flash_cmd+=" -l ${array[2]}"
566*2f2c4c7aSAndroid Build Coastguard Worker        fi
567*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Flash $SERIAL_NUMBER with flash station cli by: $flash_cmd" "$LINENO"
568*2f2c4c7aSAndroid Build Coastguard Worker        eval "$flash_cmd"
569*2f2c4c7aSAndroid Build Coastguard Worker        exit_code=$?
570*2f2c4c7aSAndroid Build Coastguard Worker        if [ $exit_code -eq 0 ]; then
571*2f2c4c7aSAndroid Build Coastguard Worker            echo "Flash platform succeeded"
572*2f2c4c7aSAndroid Build Coastguard Worker            wait_for_adb_device
573*2f2c4c7aSAndroid Build Coastguard Worker            return
574*2f2c4c7aSAndroid Build Coastguard Worker        else
575*2f2c4c7aSAndroid Build Coastguard Worker            echo "Flash platform build failed with exit code $exit_code"
576*2f2c4c7aSAndroid Build Coastguard Worker            exit 1
577*2f2c4c7aSAndroid Build Coastguard Worker        fi
578*2f2c4c7aSAndroid Build Coastguard Worker    fi
579*2f2c4c7aSAndroid Build Coastguard Worker
580*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$PLATFORM_REPO_ROOT" ] && [[ "$PLATFORM_BUILD" == "$PLATFORM_REPO_ROOT/out/target/product/$PRODUCT" ]] && \
581*2f2c4c7aSAndroid Build Coastguard Worker    [ -x "$PLATFORM_REPO_ROOT/vendor/google/tools/flashall" ]; then
582*2f2c4c7aSAndroid Build Coastguard Worker        cd "$PLATFORM_REPO_ROOT"
583*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Flash with vendor/google/tools/flashall" "$LINENO"
584*2f2c4c7aSAndroid Build Coastguard Worker        if [ -z "${TARGET_PRODUCT}" ] || [[ "${TARGET_PRODUCT}" != *"$PRODUCT" ]]; then
585*2f2c4c7aSAndroid Build Coastguard Worker            if [[ "$PLATFORM_VERSION" == aosp-* ]]; then
586*2f2c4c7aSAndroid Build Coastguard Worker                set_platform_repo "aosp_$PRODUCT"
587*2f2c4c7aSAndroid Build Coastguard Worker            else
588*2f2c4c7aSAndroid Build Coastguard Worker                set_platform_repo "$PRODUCT"
589*2f2c4c7aSAndroid Build Coastguard Worker            fi
590*2f2c4c7aSAndroid Build Coastguard Worker        fi
591*2f2c4c7aSAndroid Build Coastguard Worker        eval "vendor/google/tools/flashall  --nointeractive -w -s $SERIAL_NUMBER"
592*2f2c4c7aSAndroid Build Coastguard Worker        return
593*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -x "${ANDROID_HOST_OUT}/bin/local_flashstation" ] || [ -x "$LOCAL_FLASH_CLI" ]; then
594*2f2c4c7aSAndroid Build Coastguard Worker        if [ -z "${TARGET_PRODUCT}" ]; then
595*2f2c4c7aSAndroid Build Coastguard Worker            export TARGET_PRODUCT="$PRODUCT"
596*2f2c4c7aSAndroid Build Coastguard Worker        fi
597*2f2c4c7aSAndroid Build Coastguard Worker        if [ -z "${TARGET_BUILD_VARIANT}" ]; then
598*2f2c4c7aSAndroid Build Coastguard Worker            export TARGET_BUILD_VARIANT="$DEVICE_VARIANT"
599*2f2c4c7aSAndroid Build Coastguard Worker        fi
600*2f2c4c7aSAndroid Build Coastguard Worker        if [ -z "${ANDROID_PRODUCT_OUT}" ] || [[ "${ANDROID_PRODUCT_OUT}" != "$PLATFORM_BUILD" ]] ; then
601*2f2c4c7aSAndroid Build Coastguard Worker            export ANDROID_PRODUCT_OUT="$PLATFORM_BUILD"
602*2f2c4c7aSAndroid Build Coastguard Worker        fi
603*2f2c4c7aSAndroid Build Coastguard Worker        if [ -z "${ANDROID_HOST_OUT}" ]; then
604*2f2c4c7aSAndroid Build Coastguard Worker            export ANDROID_HOST_OUT="$PLATFORM_BUILD"
605*2f2c4c7aSAndroid Build Coastguard Worker        fi
606*2f2c4c7aSAndroid Build Coastguard Worker        if [ ! -f "$PLATFORM_BUILD/system.img" ]; then
607*2f2c4c7aSAndroid Build Coastguard Worker            local device_image=$(find "$PLATFORM_BUILD" -maxdepth 1 -type f -name *-img*.zip)
608*2f2c4c7aSAndroid Build Coastguard Worker            unzip -j "$device_image" -d "$PLATFORM_BUILD"
609*2f2c4c7aSAndroid Build Coastguard Worker        fi
610*2f2c4c7aSAndroid Build Coastguard Worker
611*2f2c4c7aSAndroid Build Coastguard Worker        awk '! /baseband/' "$PLATFORM_BUILD"/android-info.txt > temp && mv temp "$PLATFORM_BUILD"/android-info.txt
612*2f2c4c7aSAndroid Build Coastguard Worker        awk '! /bootloader/' "$PLATFORM_BUILD"/android-info.txt > temp && mv temp "$PLATFORM_BUILD"/android-info.txt
613*2f2c4c7aSAndroid Build Coastguard Worker
614*2f2c4c7aSAndroid Build Coastguard Worker        flash_cmd="$LOCAL_FLASH_CLI"
615*2f2c4c7aSAndroid Build Coastguard Worker
616*2f2c4c7aSAndroid Build Coastguard Worker        if [ ! -x "$LOCAL_FLASH_CLI" ]; then
617*2f2c4c7aSAndroid Build Coastguard Worker            flash_cmd="${ANDROID_HOST_OUT}/bin/local_flashstation"
618*2f2c4c7aSAndroid Build Coastguard Worker        fi
619*2f2c4c7aSAndroid Build Coastguard Worker
620*2f2c4c7aSAndroid Build Coastguard Worker        flash_cmd+=" --nointeractive --force_flash_partitions --disable_verity --disable_verification  -w -s $SERIAL_NUMBER"
621*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Flash device with: $flash_cmd" "$LINENO"
622*2f2c4c7aSAndroid Build Coastguard Worker        eval "$flash_cmd"
623*2f2c4c7aSAndroid Build Coastguard Worker        exit_code=$?
624*2f2c4c7aSAndroid Build Coastguard Worker        if [ $exit_code -eq 0 ]; then
625*2f2c4c7aSAndroid Build Coastguard Worker            echo "Flash platform succeeded"
626*2f2c4c7aSAndroid Build Coastguard Worker            wait_for_adb_device
627*2f2c4c7aSAndroid Build Coastguard Worker            return
628*2f2c4c7aSAndroid Build Coastguard Worker        else
629*2f2c4c7aSAndroid Build Coastguard Worker            echo "Flash platform build failed with exit code $exit_code"
630*2f2c4c7aSAndroid Build Coastguard Worker            exit 1
631*2f2c4c7aSAndroid Build Coastguard Worker        fi
632*2f2c4c7aSAndroid Build Coastguard Worker    fi
633*2f2c4c7aSAndroid Build Coastguard Worker
634*2f2c4c7aSAndroid Build Coastguard Worker}
635*2f2c4c7aSAndroid Build Coastguard Worker
636*2f2c4c7aSAndroid Build Coastguard Workerfunction get_mix_ramdisk_script() {
637*2f2c4c7aSAndroid Build Coastguard Worker    download_file_name="ab://git_main/aosp_cf_x86_64_only_phone-trunk_staging-userdebug/latest/*-tests-*.zip"
638*2f2c4c7aSAndroid Build Coastguard Worker    eval "$FETCH_SCRIPT $download_file_name"
639*2f2c4c7aSAndroid Build Coastguard Worker    exit_code=$?
640*2f2c4c7aSAndroid Build Coastguard Worker    if [ $exit_code -eq 0 ]; then
641*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Download $download_file_name succeeded" "$LINENO"
642*2f2c4c7aSAndroid Build Coastguard Worker    else
643*2f2c4c7aSAndroid Build Coastguard Worker        print_error "Download $download_file_name failed" "$LINENO" "$LINENO"
644*2f2c4c7aSAndroid Build Coastguard Worker    fi
645*2f2c4c7aSAndroid Build Coastguard Worker    eval "unzip -j *-tests-* DATA/local/tmp/build_mixed_kernels_ramdisk"
646*2f2c4c7aSAndroid Build Coastguard Worker    echo ""
647*2f2c4c7aSAndroid Build Coastguard Worker}
648*2f2c4c7aSAndroid Build Coastguard Worker
649*2f2c4c7aSAndroid Build Coastguard Workerfunction mixing_build() {
650*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z ${PLATFORM_REPO_ROOT_PATH} ] && [ -f "$PLATFORM_REPO_ROOT_PATH/vendor/google/tools/build_mixed_kernels_ramdisk"]; then
651*2f2c4c7aSAndroid Build Coastguard Worker        mix_kernel_cmd="$PLATFORM_REPO_ROOT_PATH/vendor/google/tools/build_mixed_kernels_ramdisk"
652*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -f "$DOWNLOAD_PATH/build_mixed_kernels_ramdisk" ]; then
653*2f2c4c7aSAndroid Build Coastguard Worker        mix_kernel_cmd="$DOWNLOAD_PATH/build_mixed_kernels_ramdisk"
654*2f2c4c7aSAndroid Build Coastguard Worker    else
655*2f2c4c7aSAndroid Build Coastguard Worker        cd "$DOWNLOAD_PATH"
656*2f2c4c7aSAndroid Build Coastguard Worker        get_mix_ramdisk_script
657*2f2c4c7aSAndroid Build Coastguard Worker        mix_kernel_cmd="$PWD/build_mixed_kernels_ramdisk"
658*2f2c4c7aSAndroid Build Coastguard Worker    fi
659*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -f "$mix_kernel_cmd" ]; then
660*2f2c4c7aSAndroid Build Coastguard Worker        print_error "$mix_kernel_cmd doesn't exist or is not executable" "$LINENO"
661*2f2c4c7aSAndroid Build Coastguard Worker    elif [ ! -x "$mix_kernel_cmd" ]; then
662*2f2c4c7aSAndroid Build Coastguard Worker        print_error "$mix_kernel_cmd is not executable" "$LINENO"
663*2f2c4c7aSAndroid Build Coastguard Worker    fi
664*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$PLATFORM_BUILD" == ab://* ]]; then
665*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Download platform build $PLATFORM_BUILD" "$LINENO"
666*2f2c4c7aSAndroid Build Coastguard Worker        if [ -d "$DOWNLOAD_PATH/device_dir" ]; then
667*2f2c4c7aSAndroid Build Coastguard Worker            rm -rf "$DOWNLOAD_PATH/device_dir"
668*2f2c4c7aSAndroid Build Coastguard Worker        fi
669*2f2c4c7aSAndroid Build Coastguard Worker        PLATFORM_DIR="$DOWNLOAD_PATH/device_dir"
670*2f2c4c7aSAndroid Build Coastguard Worker        mkdir -p "$PLATFORM_DIR"
671*2f2c4c7aSAndroid Build Coastguard Worker        cd "$PLATFORM_DIR" || $(print_error "Fail to go to $PLATFORM_DIR" "$LINENO")
672*2f2c4c7aSAndroid Build Coastguard Worker        download_platform_build "$PLATFORM_BUILD"
673*2f2c4c7aSAndroid Build Coastguard Worker        PLATFORM_BUILD="$PLATFORM_DIR"
674*2f2c4c7aSAndroid Build Coastguard Worker    elif [ ! -z "$PLATFORM_REPO_ROOT" ] && [[ "$PLATFORM_BUILD" == "$PLATFORM_REPO_ROOT"* ]]; then
675*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Copy platform build $PLATFORM_BUILD to $DOWNLOAD_PATH/device_dir" "$LINENO"
676*2f2c4c7aSAndroid Build Coastguard Worker        PLATFORM_DIR="$DOWNLOAD_PATH/device_dir"
677*2f2c4c7aSAndroid Build Coastguard Worker        mkdir -p "$PLATFORM_DIR"
678*2f2c4c7aSAndroid Build Coastguard Worker        cd "$PLATFORM_DIR" || $(print_error "Fail to go to $PLATFORM_DIR" "$LINENO")
679*2f2c4c7aSAndroid Build Coastguard Worker        local device_image=$(find "$PLATFORM_BUILD" -maxdepth 1 -type f -name *-img.zip)
680*2f2c4c7aSAndroid Build Coastguard Worker        if [ ! -z "device_image" ]; then
681*2f2c4c7aSAndroid Build Coastguard Worker            cp "$device_image $PLATFORM_DIR/$PRODUCT-img-0.zip" "$PLATFORM_DIR"
682*2f2c4c7aSAndroid Build Coastguard Worker        else
683*2f2c4c7aSAndroid Build Coastguard Worker            device_image=$(find "$PLATFORM_BUILD" -maxdepth 1 -type f -name *-img-*.zip)
684*2f2c4c7aSAndroid Build Coastguard Worker            if [ ! -z "device_image" ]; then
685*2f2c4c7aSAndroid Build Coastguard Worker                cp "$device_image $PLATFORM_DIR/$PRODUCT-img-0.zip" "$PLATFORM_DIR"
686*2f2c4c7aSAndroid Build Coastguard Worker            else
687*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Can't find $RPODUCT-img-*.zip in $PLATFORM_BUILD"
688*2f2c4c7aSAndroid Build Coastguard Worker            fi
689*2f2c4c7aSAndroid Build Coastguard Worker        fi
690*2f2c4c7aSAndroid Build Coastguard Worker        local file_patterns=("bootloader.img" "radio.img" "vendor_ramdisk.img" "misc_info.txt" "otatools.zip")
691*2f2c4c7aSAndroid Build Coastguard Worker        for pattern in "${file_patterns[@]}"; do
692*2f2c4c7aSAndroid Build Coastguard Worker            cp "$PLATFORM_BUILD/$pattern" "$PLATFORM_DIR/$pattern"
693*2f2c4c7aSAndroid Build Coastguard Worker            exit_code=$?
694*2f2c4c7aSAndroid Build Coastguard Worker            if [ $exit_code -eq 0 ]; then
695*2f2c4c7aSAndroid Build Coastguard Worker                print_info "Copied $PLATFORM_BUILD/$pattern to $PLATFORM_DIR" "$LINENO"
696*2f2c4c7aSAndroid Build Coastguard Worker            else
697*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Failed to copy $PLATFORM_BUILD/$pattern to $PLATFORM_DIR" "$LINENO"
698*2f2c4c7aSAndroid Build Coastguard Worker            fi
699*2f2c4c7aSAndroid Build Coastguard Worker        done
700*2f2c4c7aSAndroid Build Coastguard Worker        PLATFORM_BUILD="$PLATFORM_DIR"
701*2f2c4c7aSAndroid Build Coastguard Worker    fi
702*2f2c4c7aSAndroid Build Coastguard Worker
703*2f2c4c7aSAndroid Build Coastguard Worker    local new_device_dir="$DOWNLOAD_PATH/new_device_dir"
704*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d "$new_device_dir" ]; then
705*2f2c4c7aSAndroid Build Coastguard Worker        rm -rf "$new_device_dir"
706*2f2c4c7aSAndroid Build Coastguard Worker    fi
707*2f2c4c7aSAndroid Build Coastguard Worker    mkdir -p "$new_device_dir"
708*2f2c4c7aSAndroid Build Coastguard Worker    local mixed_build_cmd="$mix_kernel_cmd"
709*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d "${KERNEL_BUILD}" ]; then
710*2f2c4c7aSAndroid Build Coastguard Worker        mixed_build_cmd+=" --gki_dir $KERNEL_BUILD"
711*2f2c4c7aSAndroid Build Coastguard Worker    fi
712*2f2c4c7aSAndroid Build Coastguard Worker    mixed_build_cmd+=" $PLATFORM_BUILD $VENDOR_KERNEL_BUILD $new_device_dir"
713*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Run: $mixed_build_cmd" "$LINENO"
714*2f2c4c7aSAndroid Build Coastguard Worker    eval $mixed_build_cmd
715*2f2c4c7aSAndroid Build Coastguard Worker    device_image=$(ls $new_device_dir/*$PRODUCT-img*.zip)
716*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -f "$device_image" ]; then
717*2f2c4c7aSAndroid Build Coastguard Worker        print_error "New device image is not created in $new_device_dir" "$LINENO"
718*2f2c4c7aSAndroid Build Coastguard Worker    fi
719*2f2c4c7aSAndroid Build Coastguard Worker    cp "$PLATFORM_BUILD"/bootloader.img $new_device_dir/.
720*2f2c4c7aSAndroid Build Coastguard Worker    cp "$PLATFORM_BUILD"/radio.img $new_device_dir/.
721*2f2c4c7aSAndroid Build Coastguard Worker    PLATFORM_BUILD="$new_device_dir"
722*2f2c4c7aSAndroid Build Coastguard Worker}
723*2f2c4c7aSAndroid Build Coastguard Worker
724*2f2c4c7aSAndroid Build Coastguard Workerget_kernel_version_from_boot_image() {
725*2f2c4c7aSAndroid Build Coastguard Worker    local boot_image_path="$1"
726*2f2c4c7aSAndroid Build Coastguard Worker    local version_output
727*2f2c4c7aSAndroid Build Coastguard Worker
728*2f2c4c7aSAndroid Build Coastguard Worker    # Check for mainline kernel
729*2f2c4c7aSAndroid Build Coastguard Worker    version_output=$(strings "$boot_image_path" | grep mainline)
730*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$version_output" ]; then
731*2f2c4c7aSAndroid Build Coastguard Worker        KERNEL_VERSION="android-mainline"
732*2f2c4c7aSAndroid Build Coastguard Worker        return  # Exit the function early if a match is found
733*2f2c4c7aSAndroid Build Coastguard Worker    fi
734*2f2c4c7aSAndroid Build Coastguard Worker
735*2f2c4c7aSAndroid Build Coastguard Worker    # Check for Android 15 6.6 kernel
736*2f2c4c7aSAndroid Build Coastguard Worker    version_output=$(strings "$boot_image_path" | grep "android15" | grep "6.6")
737*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$version_output" ]; then
738*2f2c4c7aSAndroid Build Coastguard Worker        KERNEL_VERSION="android15-6.6"
739*2f2c4c7aSAndroid Build Coastguard Worker        return
740*2f2c4c7aSAndroid Build Coastguard Worker    fi
741*2f2c4c7aSAndroid Build Coastguard Worker
742*2f2c4c7aSAndroid Build Coastguard Worker    # Check for Android 14 6.1 kernel
743*2f2c4c7aSAndroid Build Coastguard Worker    version_output=$(strings "$boot_image_path" | grep "android14" | grep "6.1")
744*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$version_output" ]; then
745*2f2c4c7aSAndroid Build Coastguard Worker        KERNEL_VERSION="android14-6.1"
746*2f2c4c7aSAndroid Build Coastguard Worker        return
747*2f2c4c7aSAndroid Build Coastguard Worker    fi
748*2f2c4c7aSAndroid Build Coastguard Worker
749*2f2c4c7aSAndroid Build Coastguard Worker    # Check for Android 14 5.15 kernel
750*2f2c4c7aSAndroid Build Coastguard Worker    version_output=$(strings "$boot_image_path" | grep "android14" | grep "5.15")
751*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$version_output" ]; then
752*2f2c4c7aSAndroid Build Coastguard Worker        KERNEL_VERSION="android14-5.15"
753*2f2c4c7aSAndroid Build Coastguard Worker        return
754*2f2c4c7aSAndroid Build Coastguard Worker    fi
755*2f2c4c7aSAndroid Build Coastguard Worker
756*2f2c4c7aSAndroid Build Coastguard Worker    # Check for Android 13 5.15 kernel
757*2f2c4c7aSAndroid Build Coastguard Worker    version_output=$(strings "$boot_image_path" | grep "android13" | grep "5.15")
758*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$version_output" ]; then
759*2f2c4c7aSAndroid Build Coastguard Worker        KERNEL_VERSION="android13-5.15"
760*2f2c4c7aSAndroid Build Coastguard Worker        return
761*2f2c4c7aSAndroid Build Coastguard Worker    fi
762*2f2c4c7aSAndroid Build Coastguard Worker
763*2f2c4c7aSAndroid Build Coastguard Worker    # Check for Android 13 5.10 kernel
764*2f2c4c7aSAndroid Build Coastguard Worker    version_output=$(strings "$boot_image_path" | grep "android13" | grep "5.10")
765*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$version_output" ]; then
766*2f2c4c7aSAndroid Build Coastguard Worker        KERNEL_VERSION="android13-5.10"
767*2f2c4c7aSAndroid Build Coastguard Worker        return
768*2f2c4c7aSAndroid Build Coastguard Worker    fi
769*2f2c4c7aSAndroid Build Coastguard Worker
770*2f2c4c7aSAndroid Build Coastguard Worker    # Check for Android 12 5.10 kernel
771*2f2c4c7aSAndroid Build Coastguard Worker    version_output=$(strings "$boot_image_path" | grep "android12" | grep "5.10")
772*2f2c4c7aSAndroid Build Coastguard Worker    if [ ! -z "$version_output" ]; then
773*2f2c4c7aSAndroid Build Coastguard Worker        KERNEL_VERSION="android12-5.10"
774*2f2c4c7aSAndroid Build Coastguard Worker        return
775*2f2c4c7aSAndroid Build Coastguard Worker    fi
776*2f2c4c7aSAndroid Build Coastguard Worker}
777*2f2c4c7aSAndroid Build Coastguard Worker
778*2f2c4c7aSAndroid Build Coastguard Workerfunction gki_build_only_operation {
779*2f2c4c7aSAndroid Build Coastguard Worker    IFS='-' read -ra array <<< "$KERNEL_VERSION"
780*2f2c4c7aSAndroid Build Coastguard Worker    case "$KERNEL_VERSION" in
781*2f2c4c7aSAndroid Build Coastguard Worker        android-mainline | android15-6.6* | android14-6.1* | android14-5.15* )
782*2f2c4c7aSAndroid Build Coastguard Worker            if [[ "$KERNEL_VERSION" == "$DEVICE_KERNEL_VERSION"* ]] && [ ! -z "$SYSTEM_DLKM_INFO" ]; then
783*2f2c4c7aSAndroid Build Coastguard Worker                print_info "Device $SERIAL_NUMBER is with $KERNEL_VERSION kernel. Flash GKI directly" "$LINENO"
784*2f2c4c7aSAndroid Build Coastguard Worker                flash_gki_build
785*2f2c4c7aSAndroid Build Coastguard Worker            elif [ -z "$SYSTEM_DLKM_INFO" ]; then
786*2f2c4c7aSAndroid Build Coastguard Worker                print_warn "Device $SERIAL_NUMBER is $PRODUCT that doesn't have system_dlkm partition. Can't flash GKI directly. \
787*2f2c4c7aSAndroid Build Coastguard WorkerPlease add vendor kernel build for example by flag -vkb ab://kernel-${array[0]}-gs-pixel-${array[1]}/<kernel_target>/latest" "$LINENO"
788*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Can not flash GKI to SERIAL_NUMBER without -vkb <vendor_kernel_build> been specified." "$LINENO"
789*2f2c4c7aSAndroid Build Coastguard Worker            elif [[ "$KERNEL_VERSION" != "$DEVICE_KERNEL_VERSION"* ]]; then
790*2f2c4c7aSAndroid Build Coastguard Worker                print_warn "Device $PRODUCT $SERIAL_NUMBER comes with $DEVICE_KERNEL_STRING kernel. Can't flash GKI directly. \
791*2f2c4c7aSAndroid Build Coastguard WorkerPlease add a platform build with $KERNEL_VERSION kernel or add vendor kernel build for example by flag \
792*2f2c4c7aSAndroid Build Coastguard Worker-vkb ab://kernel-${array[0]}-gs-pixel-${array[1]}/<kernel_target>/latest" "$LINENO"
793*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Cannot flash $KERNEL_VERSION GKI to device directly $SERIAL_NUMBER." "$LINENO"
794*2f2c4c7aSAndroid Build Coastguard Worker            fi
795*2f2c4c7aSAndroid Build Coastguard Worker            ;;
796*2f2c4c7aSAndroid Build Coastguard Worker        android13-5.15* | android13-5.10* | android12-5.10* | android12-5.4* )
797*2f2c4c7aSAndroid Build Coastguard Worker            if [[ "$KERNEL_VERSION" == "$EVICE_KERNEL_VERSION"* ]]; then
798*2f2c4c7aSAndroid Build Coastguard Worker                print_info "Device $SERIAL_NUMBER is with android13-5.15 kernel. Flash GKI directly." "$LINENO"
799*2f2c4c7aSAndroid Build Coastguard Worker                flash_gki_build
800*2f2c4c7aSAndroid Build Coastguard Worker            else
801*2f2c4c7aSAndroid Build Coastguard Worker                print_warn "Device $SERIAL_NUMBER is $PRODUCT comes with $DEVICE_KERNEL_STRING kernel. Can't flash GKI directly. \
802*2f2c4c7aSAndroid Build Coastguard WorkerPlease add a platform build with $KERNEL_VERSION kernel or add vendor kernel build for example by flag \
803*2f2c4c7aSAndroid Build Coastguard Worker-vkb ab://kernel-${array[0]}-gs-pixel-${array[1]}/<kernel_target>/latest" "$LINENO"
804*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Cannot flash $KERNEL_VERSION GKI to device directly $SERIAL_NUMBER." "$LINENO"
805*2f2c4c7aSAndroid Build Coastguard Worker            fi
806*2f2c4c7aSAndroid Build Coastguard Worker            ;;
807*2f2c4c7aSAndroid Build Coastguard Worker        *)
808*2f2c4c7aSAndroid Build Coastguard Worker            print_error "Unsupported KERNEL_VERSION: $KERNEL_VERSION" "$LINENO" "$LINENO"
809*2f2c4c7aSAndroid Build Coastguard Worker            ;;
810*2f2c4c7aSAndroid Build Coastguard Worker    esac
811*2f2c4c7aSAndroid Build Coastguard Worker}
812*2f2c4c7aSAndroid Build Coastguard Worker
813*2f2c4c7aSAndroid Build Coastguard Workerfunction extract_device_kernel_version() {
814*2f2c4c7aSAndroid Build Coastguard Worker    local kernel_string="$1"
815*2f2c4c7aSAndroid Build Coastguard Worker    # Check if the string contains '-android'
816*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$kernel_string" == *"-mainline"* ]]; then
817*2f2c4c7aSAndroid Build Coastguard Worker        DEVICE_KERNEL_VERSION="android-mainline"
818*2f2c4c7aSAndroid Build Coastguard Worker    elif [[ "$kernel_string" == *"-android"* ]]; then
819*2f2c4c7aSAndroid Build Coastguard Worker        # Extract the substring between the first hyphen and the second hyphen
820*2f2c4c7aSAndroid Build Coastguard Worker        DEVICE_KERNEL_VERSION=$(echo "$kernel_string" | awk -F '-' '{print $2"-"$1}' | cut -d '.' -f -2)
821*2f2c4c7aSAndroid Build Coastguard Worker    else
822*2f2c4c7aSAndroid Build Coastguard Worker       print_warn "Can not parse $kernel_string into kernel version" "$LINENO"
823*2f2c4c7aSAndroid Build Coastguard Worker    fi
824*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Device kernel version is $DEVICE_KERNEL_VERSION" "$LINENO"
825*2f2c4c7aSAndroid Build Coastguard Worker}
826*2f2c4c7aSAndroid Build Coastguard Worker
827*2f2c4c7aSAndroid Build Coastguard Workerfunction get_device_info {
828*2f2c4c7aSAndroid Build Coastguard Worker    adb_count=$(adb devices | grep "$SERIAL_NUMBER" | wc -l)
829*2f2c4c7aSAndroid Build Coastguard Worker    if (( adb_count > 0 )); then
830*2f2c4c7aSAndroid Build Coastguard Worker        BOARD=$(adb -s "$SERIAL_NUMBER" shell getprop ro.product.board)
831*2f2c4c7aSAndroid Build Coastguard Worker        ABI=$(adb -s "$SERIAL_NUMBER" shell getprop ro.product.cpu.abi)
832*2f2c4c7aSAndroid Build Coastguard Worker        PRODUCT=$(adb -s "$SERIAL_NUMBER" shell getprop ro.build.product)
833*2f2c4c7aSAndroid Build Coastguard Worker        BUILD_TYPE=$(adb -s "$SERIAL_NUMBER" shell getprop ro.build.type)
834*2f2c4c7aSAndroid Build Coastguard Worker        DEVICE_KERNEL_STRING=$(adb -s "$SERIAL_NUMBER" shell uname -r)
835*2f2c4c7aSAndroid Build Coastguard Worker        extract_device_kernel_version "$DEVICE_KERNEL_STRING"
836*2f2c4c7aSAndroid Build Coastguard Worker        SYSTEM_DLKM_INFO=$(adb -s "$SERIAL_NUMBER" shell getprop dev.mnt.blk.system_dlkm)
837*2f2c4c7aSAndroid Build Coastguard Worker        print_info "device info: BOARD=$BOARD, ABI=$ABI, PRODUCT=$PRODUCT, BUILD_TYPE=$BUILD_TYPE" "$LINENO"
838*2f2c4c7aSAndroid Build Coastguard Worker        print_info "device info: SYSTEM_DLKM_INFO=$SYSTEM_DLKM_INFO, DEVICE_KERNEL_STRING=$DEVICE_KERNEL_STRING" "$LINENO"
839*2f2c4c7aSAndroid Build Coastguard Worker        return 0
840*2f2c4c7aSAndroid Build Coastguard Worker    fi
841*2f2c4c7aSAndroid Build Coastguard Worker    fastboot_count=$(fastboot devices | grep "$SERIAL_NUMBER" | wc -l)
842*2f2c4c7aSAndroid Build Coastguard Worker    if (( fastboot_count > 0 )); then
843*2f2c4c7aSAndroid Build Coastguard Worker        # try get product by fastboot command
844*2f2c4c7aSAndroid Build Coastguard Worker        local output=$(fastboot -s "$SERIAL_NUMBER" getvar product 2>&1)
845*2f2c4c7aSAndroid Build Coastguard Worker        PRODUCT=$(echo "$output" | grep -oP '^product:\s*\K.*' | cut -d' ' -f1)
846*2f2c4c7aSAndroid Build Coastguard Worker        print_info "$SERIAL_NUMBER is in fastboot with device info: PRODUCT=$PRODUCT" "$LINENO"
847*2f2c4c7aSAndroid Build Coastguard Worker        return 0
848*2f2c4c7aSAndroid Build Coastguard Worker    fi
849*2f2c4c7aSAndroid Build Coastguard Worker    print_error "$SERIAL_NUMBER is not connected with adb or fastboot"
850*2f2c4c7aSAndroid Build Coastguard Worker}
851*2f2c4c7aSAndroid Build Coastguard Worker
852*2f2c4c7aSAndroid Build Coastguard Workerfunction find_tradefed_bin {
853*2f2c4c7aSAndroid Build Coastguard Worker    cd "$REPO_ROOT_PATH"
854*2f2c4c7aSAndroid Build Coastguard Worker    if [ -f "${ANDROID_HOST_OUT}/bin/tradefed.sh" ] ; then
855*2f2c4c7aSAndroid Build Coastguard Worker        TRADEFED="${ANDROID_HOST_OUT}/bin/tradefed.sh"
856*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Use the tradefed from the local built path $TRADEFED" "$LINENO"
857*2f2c4c7aSAndroid Build Coastguard Worker        return
858*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -f "$PLATFORM_TF_PREBUILT" ]; then
859*2f2c4c7aSAndroid Build Coastguard Worker        TF_BIN="$PLATFORM_TF_PREBUILT"
860*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Local Tradefed is not built yet. Use the prebuilt from $PLATFORM_TF_PREBUILT" "$LINENO"
861*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -f "$KERNEL_TF_PREBUILT" ]; then
862*2f2c4c7aSAndroid Build Coastguard Worker        TF_BIN="$KERNEL_TF_PREBUILT"
863*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -f "/tmp/tradefed/tradefed.sh" ]; then
864*2f2c4c7aSAndroid Build Coastguard Worker        TF_BIN=/tmp/tradefed/tradefed.sh
865*2f2c4c7aSAndroid Build Coastguard Worker    # No Tradefed found
866*2f2c4c7aSAndroid Build Coastguard Worker    else
867*2f2c4c7aSAndroid Build Coastguard Worker        mkdir -p "/tmp/tradefed"
868*2f2c4c7aSAndroid Build Coastguard Worker        cd /tmp/tradefed
869*2f2c4c7aSAndroid Build Coastguard Worker        eval "$FETCH_SCRIPT ab://tradefed/tradefed/latest/google-tradefed.zip"
870*2f2c4c7aSAndroid Build Coastguard Worker        exit_code=$?
871*2f2c4c7aSAndroid Build Coastguard Worker        if [ $exit_code -eq 0 ]; then
872*2f2c4c7aSAndroid Build Coastguard Worker            print_info "Download tradefed succeeded" "$LINENO"
873*2f2c4c7aSAndroid Build Coastguard Worker        else
874*2f2c4c7aSAndroid Build Coastguard Worker            print_error "Download tradefed failed" "$LINENO"
875*2f2c4c7aSAndroid Build Coastguard Worker        fi
876*2f2c4c7aSAndroid Build Coastguard Worker        echo ""
877*2f2c4c7aSAndroid Build Coastguard Worker        eval "unzip -oq google-tradefed.zip"
878*2f2c4c7aSAndroid Build Coastguard Worker        TF_BIN=/tmp/tradefed/tradefed.sh
879*2f2c4c7aSAndroid Build Coastguard Worker        cd "$REPO_ROOT_PATH"
880*2f2c4c7aSAndroid Build Coastguard Worker    fi
881*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d "${ANDROID_JAVA_HOME}" ] ; then
882*2f2c4c7aSAndroid Build Coastguard Worker        JDK_PATH="${ANDROID_JAVA_HOME}"
883*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -d "$PLATFORM_JDK_PATH" ] ; then
884*2f2c4c7aSAndroid Build Coastguard Worker        JDK_PATH="$PLATFORM_JDK_PATH"
885*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -d "$KERNEL_JDK_PATH" ] ; then
886*2f2c4c7aSAndroid Build Coastguard Worker        JDK_PATH="$KERNEL_JDK_PATH"
887*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -d "$LOCAL_JDK_PATH" ] ; then
888*2f2c4c7aSAndroid Build Coastguard Worker        JDK_PATH="$LOCAL_JDK_PATH"
889*2f2c4c7aSAndroid Build Coastguard Worker    else
890*2f2c4c7aSAndroid Build Coastguard Worker        print_error "Can't find JAVA JDK path" "$LINENO"
891*2f2c4c7aSAndroid Build Coastguard Worker    fi
892*2f2c4c7aSAndroid Build Coastguard Worker    TRADEFED="JAVA_HOME=$JDK_PATH PATH=$JDK_PATH/bin:$PATH $TF_BIN"
893*2f2c4c7aSAndroid Build Coastguard Worker}
894*2f2c4c7aSAndroid Build Coastguard Worker
895*2f2c4c7aSAndroid Build Coastguard Workeradb_checker
896*2f2c4c7aSAndroid Build Coastguard Worker
897*2f2c4c7aSAndroid Build Coastguard WorkerLOCAL_REPO=
898*2f2c4c7aSAndroid Build Coastguard Worker
899*2f2c4c7aSAndroid Build Coastguard WorkerOLD_PWD=$PWD
900*2f2c4c7aSAndroid Build Coastguard WorkerMY_NAME=$0
901*2f2c4c7aSAndroid Build Coastguard Worker
902*2f2c4c7aSAndroid Build Coastguard Workerparse_arg "$@"
903*2f2c4c7aSAndroid Build Coastguard Worker
904*2f2c4c7aSAndroid Build Coastguard Workerif [ -z "$SERIAL_NUMBER" ]; then
905*2f2c4c7aSAndroid Build Coastguard Worker    print_error "Device serial is not provided with flag -s <serial_number>." "$LINENO"
906*2f2c4c7aSAndroid Build Coastguard Worker    exit 1
907*2f2c4c7aSAndroid Build Coastguard Workerfi
908*2f2c4c7aSAndroid Build Coastguard Worker
909*2f2c4c7aSAndroid Build Coastguard Workerget_device_info
910*2f2c4c7aSAndroid Build Coastguard Worker
911*2f2c4c7aSAndroid Build Coastguard WorkerFULL_COMMAND_PATH=$(dirname "$PWD/$0")
912*2f2c4c7aSAndroid Build Coastguard WorkerREPO_LIST_OUT=$(repo list 2>&1)
913*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$REPO_LIST_OUT" == "error"* ]]; then
914*2f2c4c7aSAndroid Build Coastguard Worker    print_error "Current path $PWD is not in an Android repo. Change path to repo root." "$LINENO"
915*2f2c4c7aSAndroid Build Coastguard Worker    go_to_repo_root "$FULL_COMMAND_PATH"
916*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Changed path to $PWD" "$LINENO"
917*2f2c4c7aSAndroid Build Coastguard Workerelse
918*2f2c4c7aSAndroid Build Coastguard Worker    go_to_repo_root "$PWD"
919*2f2c4c7aSAndroid Build Coastguard Workerfi
920*2f2c4c7aSAndroid Build Coastguard Worker
921*2f2c4c7aSAndroid Build Coastguard WorkerREPO_ROOT_PATH="$PWD"
922*2f2c4c7aSAndroid Build Coastguard WorkerFETCH_SCRIPT="$REPO_ROOT_PATH/$FETCH_SCRIPT"
923*2f2c4c7aSAndroid Build Coastguard Worker
924*2f2c4c7aSAndroid Build Coastguard Workerfind_repo
925*2f2c4c7aSAndroid Build Coastguard Worker
926*2f2c4c7aSAndroid Build Coastguard Workerif [ ! -d "$DOWNLOAD_PATH" ]; then
927*2f2c4c7aSAndroid Build Coastguard Worker    mkdir -p "$DOWNLOAD_PATH" || $(print_error "Fail to create directory $DOWNLOAD_PATH" "$LINENO")
928*2f2c4c7aSAndroid Build Coastguard Workerfi
929*2f2c4c7aSAndroid Build Coastguard Worker
930*2f2c4c7aSAndroid Build Coastguard Workerif [ ! -z "$PLATFORM_BUILD" ] && [[ "$PLATFORM_BUILD" != ab://* ]] && [ -d "$PLATFORM_BUILD" ]; then
931*2f2c4c7aSAndroid Build Coastguard Worker    # Check if PLATFORM_BUILD is an Android platform repo
932*2f2c4c7aSAndroid Build Coastguard Worker    cd "$PLATFORM_BUILD"
933*2f2c4c7aSAndroid Build Coastguard Worker    PLATFORM_REPO_LIST_OUT=$(repo list 2>&1)
934*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$PLATFORM_REPO_LIST_OUT" != "error"* ]]; then
935*2f2c4c7aSAndroid Build Coastguard Worker        go_to_repo_root "$PWD"
936*2f2c4c7aSAndroid Build Coastguard Worker        if [[ "$PWD" != "$REPO_ROOT_PATH" ]]; then
937*2f2c4c7aSAndroid Build Coastguard Worker            find_repo
938*2f2c4c7aSAndroid Build Coastguard Worker        fi
939*2f2c4c7aSAndroid Build Coastguard Worker        if [ "$SKIP_BUILD" = false ] && [[ "$PLATFORM_BUILD" != "ab://"* ]] && [[ ! -z "$PLATFORM_BUILD" ]]; then
940*2f2c4c7aSAndroid Build Coastguard Worker            if [ -z "${TARGET_PRODUCT}" ] || [[ "${TARGET_PRODUCT}" != *"$PRODUCT" ]]; then
941*2f2c4c7aSAndroid Build Coastguard Worker                if [[ "$PLATFORM_VERSION" == aosp-* ]]; then
942*2f2c4c7aSAndroid Build Coastguard Worker                    set_platform_repo "aosp_$PRODUCT"
943*2f2c4c7aSAndroid Build Coastguard Worker                else
944*2f2c4c7aSAndroid Build Coastguard Worker                    set_platform_repo "$PRODUCT"
945*2f2c4c7aSAndroid Build Coastguard Worker                fi
946*2f2c4c7aSAndroid Build Coastguard Worker            elif [[ "${TARGET_PRODUCT}" == *"$PRODUCT" ]]; then
947*2f2c4c7aSAndroid Build Coastguard Worker                echo "TARGET_PRODUCT=${TARGET_PRODUCT}, ANDROID_PRODUCT_OUT=${ANDROID_PRODUCT_OUT}"
948*2f2c4c7aSAndroid Build Coastguard Worker            fi
949*2f2c4c7aSAndroid Build Coastguard Worker            if [[ "${TARGET_PRODUCT}" == *"$PRODUCT" ]]; then
950*2f2c4c7aSAndroid Build Coastguard Worker                build_platform
951*2f2c4c7aSAndroid Build Coastguard Worker            else
952*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Can not build platform build due to lunch build target failure" "$LINENO"
953*2f2c4c7aSAndroid Build Coastguard Worker            fi
954*2f2c4c7aSAndroid Build Coastguard Worker        fi
955*2f2c4c7aSAndroid Build Coastguard Worker        if [ -d "${PLATFORM_REPO_ROOT}" ] && [ -f "$PLATFORM_REPO_ROOT/out/target/product/$PRODUCT/otatools.zip" ]; then
956*2f2c4c7aSAndroid Build Coastguard Worker            PLATFORM_BUILD=$PLATFORM_REPO_ROOT/out/target/product/$PRODUCT
957*2f2c4c7aSAndroid Build Coastguard Worker        elif [ -d "${ANDROID_PRODUCT_OUT}" ] && [ -f "${ANDROID_PRODUCT_OUT}/otatools.zip" ]; then
958*2f2c4c7aSAndroid Build Coastguard Worker            PLATFORM_BUILD="${ANDROID_PRODUCT_OUT}"
959*2f2c4c7aSAndroid Build Coastguard Worker        else
960*2f2c4c7aSAndroid Build Coastguard Worker            PLATFORM_BUILD=
961*2f2c4c7aSAndroid Build Coastguard Worker        fi
962*2f2c4c7aSAndroid Build Coastguard Worker    fi
963*2f2c4c7aSAndroid Build Coastguard Workerfi
964*2f2c4c7aSAndroid Build Coastguard Worker
965*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$SYSTEM_BUILD" == ab://* ]]; then
966*2f2c4c7aSAndroid Build Coastguard Worker    print_warn "System build is not supoort yet" "$LINENO"
967*2f2c4c7aSAndroid Build Coastguard Workerelif [ ! -z "$SYSTEM_BUILD" ] && [ -d "$SYSTEM_BUILD" ]; then
968*2f2c4c7aSAndroid Build Coastguard Worker    print_warn "System build is not supoort yet" "$LINENO"
969*2f2c4c7aSAndroid Build Coastguard Worker    # Get GSI build
970*2f2c4c7aSAndroid Build Coastguard Worker    cd "$SYSTEM_BUILD"
971*2f2c4c7aSAndroid Build Coastguard Worker    SYSTEM_REPO_LIST_OUT=$(repo list 2>&1)
972*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$SYSTEM_REPO_LIST_OUT" != "error"* ]]; then
973*2f2c4c7aSAndroid Build Coastguard Worker        go_to_repo_root "$PWD"
974*2f2c4c7aSAndroid Build Coastguard Worker        if [[ "$PWD" != "$REPO_ROOT_PATH" ]]; then
975*2f2c4c7aSAndroid Build Coastguard Worker            find_repo
976*2f2c4c7aSAndroid Build Coastguard Worker        fi
977*2f2c4c7aSAndroid Build Coastguard Worker        if [ -z "${TARGET_PRODUCT}" ] || [[ "${TARGET_PRODUCT}" != "_arm64" ]]; then
978*2f2c4c7aSAndroid Build Coastguard Worker            set_platform_repo "aosp_arm64"
979*2f2c4c7aSAndroid Build Coastguard Worker            if [ "$SKIP_BUILD" = false ] ; then
980*2f2c4c7aSAndroid Build Coastguard Worker                build_platform
981*2f2c4c7aSAndroid Build Coastguard Worker            fi
982*2f2c4c7aSAndroid Build Coastguard Worker            SYSTEM_BUILD="${ANDROID_PRODUCT_OUT}/system.img"
983*2f2c4c7aSAndroid Build Coastguard Worker        fi
984*2f2c4c7aSAndroid Build Coastguard Worker    fi
985*2f2c4c7aSAndroid Build Coastguard Workerfi
986*2f2c4c7aSAndroid Build Coastguard Worker
987*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$KERNEL_BUILD" == ab://* ]]; then
988*2f2c4c7aSAndroid Build Coastguard Worker    IFS='/' read -ra array <<< "$KERNEL_BUILD"
989*2f2c4c7aSAndroid Build Coastguard Worker    KERNEL_VERSION=$(echo "${array[2]}" | sed "s/aosp_kernel-common-//g")
990*2f2c4c7aSAndroid Build Coastguard Worker    IFS='-' read -ra array <<< "$KERNEL_VERSION"
991*2f2c4c7aSAndroid Build Coastguard Worker    KERNEL_VERSION="${array[0]}-${array[1]}"
992*2f2c4c7aSAndroid Build Coastguard Worker    print_info "$KERNEL_BUILD is KERNEL_VERSION $KERNEL_VERSION" "$LINENO"
993*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$KERNEL_VERSION" != "$DEVICE_KERNEL_VERSION"* ]] && [ -z "$PLATFORM_BUILD" ] && [ -z "$VENDOR_KERNEL_BUILD" ]; then
994*2f2c4c7aSAndroid Build Coastguard Worker        print_warn "Device $PRODUCT $SERIAL_NUMBER comes with $DEVICE_KERNEL_STRING $DEVICE_KERNEL_VERSION kernel. \
995*2f2c4c7aSAndroid Build Coastguard WorkerCan't flash $KERNEL_VERSION GKI directly. Please use a platform build with the $KERNEL_VERSION kernel \
996*2f2c4c7aSAndroid Build Coastguard Workeror use a vendor kernel build by flag -vkb, for example -vkb -vkb ab://kernel-${array[0]}-gs-pixel-${array[1]}/<kernel_target>/latest" "$LINENO"
997*2f2c4c7aSAndroid Build Coastguard Worker        print_error "Cannot flash $KERNEL_VERSION GKI to device $SERIAL_NUMBER directly." "$LINENO"
998*2f2c4c7aSAndroid Build Coastguard Worker    fi
999*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Download kernel build $KERNEL_BUILD" "$LINENO"
1000*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d "$DOWNLOAD_PATH/gki_dir" ]; then
1001*2f2c4c7aSAndroid Build Coastguard Worker        rm -rf "$DOWNLOAD_PATH/gki_dir"
1002*2f2c4c7aSAndroid Build Coastguard Worker    fi
1003*2f2c4c7aSAndroid Build Coastguard Worker    GKI_DIR="$DOWNLOAD_PATH/gki_dir"
1004*2f2c4c7aSAndroid Build Coastguard Worker    mkdir -p "$GKI_DIR"
1005*2f2c4c7aSAndroid Build Coastguard Worker    cd "$GKI_DIR" || $(print_error "Fail to go to $GKI_DIR" "$LINENO")
1006*2f2c4c7aSAndroid Build Coastguard Worker    download_gki_build $KERNEL_BUILD
1007*2f2c4c7aSAndroid Build Coastguard Worker    KERNEL_BUILD="$GKI_DIR"
1008*2f2c4c7aSAndroid Build Coastguard Workerelif [ ! -z "$KERNEL_BUILD" ] && [ -d "$KERNEL_BUILD" ]; then
1009*2f2c4c7aSAndroid Build Coastguard Worker    # Check if kernel repo is provided
1010*2f2c4c7aSAndroid Build Coastguard Worker    cd "$KERNEL_BUILD"
1011*2f2c4c7aSAndroid Build Coastguard Worker    KERNEL_REPO_LIST_OUT=$(repo list 2>&1)
1012*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$KERNEL_REPO_LIST_OUT" != "error"* ]]; then
1013*2f2c4c7aSAndroid Build Coastguard Worker        go_to_repo_root "$PWD"
1014*2f2c4c7aSAndroid Build Coastguard Worker        if [[ "$PWD" != "$REPO_ROOT_PATH" ]]; then
1015*2f2c4c7aSAndroid Build Coastguard Worker            find_repo
1016*2f2c4c7aSAndroid Build Coastguard Worker        fi
1017*2f2c4c7aSAndroid Build Coastguard Worker        if [ "$SKIP_BUILD" = false ] ; then
1018*2f2c4c7aSAndroid Build Coastguard Worker            if [ ! -f "common/BUILD.bazel" ]; then
1019*2f2c4c7aSAndroid Build Coastguard Worker                # TODO: Add build support to android12 and earlier kernels
1020*2f2c4c7aSAndroid Build Coastguard Worker                print_error "bazel build is not supported in $PWD" "$LINENO"
1021*2f2c4c7aSAndroid Build Coastguard Worker            else
1022*2f2c4c7aSAndroid Build Coastguard Worker                build_ack
1023*2f2c4c7aSAndroid Build Coastguard Worker            fi
1024*2f2c4c7aSAndroid Build Coastguard Worker        fi
1025*2f2c4c7aSAndroid Build Coastguard Worker        KERNEL_BUILD="$PWD/out/kernel_aarch64/dist"
1026*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -f "$KERNEL_BUILD/boot*.img" ]; then
1027*2f2c4c7aSAndroid Build Coastguard Worker        get_kernel_version_from_boot_image "$KERNEL_BUILD/boot*.img"
1028*2f2c4c7aSAndroid Build Coastguard Worker    fi
1029*2f2c4c7aSAndroid Build Coastguard Workerfi
1030*2f2c4c7aSAndroid Build Coastguard Worker
1031*2f2c4c7aSAndroid Build Coastguard Workerif [[ "$VENDOR_KERNEL_BUILD" == ab://* ]]; then
1032*2f2c4c7aSAndroid Build Coastguard Worker    print_info "Download vendor kernel build $VENDOR_KERNEL_BUILD" "$LINENO"
1033*2f2c4c7aSAndroid Build Coastguard Worker    if [ -d "$DOWNLOAD_PATH/vendor_kernel_dir" ]; then
1034*2f2c4c7aSAndroid Build Coastguard Worker        rm -rf "$DOWNLOAD_PATH/vendor_kernel_dir"
1035*2f2c4c7aSAndroid Build Coastguard Worker    fi
1036*2f2c4c7aSAndroid Build Coastguard Worker    VENDOR_KERNEL_DIR="$DOWNLOAD_PATH/vendor_kernel_dir"
1037*2f2c4c7aSAndroid Build Coastguard Worker    mkdir -p "$VENDOR_KERNEL_DIR"
1038*2f2c4c7aSAndroid Build Coastguard Worker    cd "$VENDOR_KERNEL_DIR" || $(print_error "Fail to go to $VENDOR_KERNEL_DIR" "$LINENO")
1039*2f2c4c7aSAndroid Build Coastguard Worker    download_vendor_kernel_build $VENDOR_KERNEL_BUILD
1040*2f2c4c7aSAndroid Build Coastguard Worker    VENDOR_KERNEL_BUILD="$VENDOR_KERNEL_DIR"
1041*2f2c4c7aSAndroid Build Coastguard Workerelif [ ! -z "$VENDOR_KERNEL_BUILD" ] && [ -d "$VENDOR_KERNEL_BUILD" ]; then
1042*2f2c4c7aSAndroid Build Coastguard Worker    # Check if vendor kernel repo is provided
1043*2f2c4c7aSAndroid Build Coastguard Worker    cd "$VENDOR_KERNEL_BUILD"
1044*2f2c4c7aSAndroid Build Coastguard Worker    VENDOR_KERNEL_REPO_LIST_OUT=$(repo list 2>&1)
1045*2f2c4c7aSAndroid Build Coastguard Worker    if [[ "$VENDOR_KERNEL_REPO_LIST_OUT" != "error"* ]]; then
1046*2f2c4c7aSAndroid Build Coastguard Worker        go_to_repo_root "$PWD"
1047*2f2c4c7aSAndroid Build Coastguard Worker        if [[ "$PWD" != "$REPO_ROOT_PATH" ]]; then
1048*2f2c4c7aSAndroid Build Coastguard Worker            find_repo
1049*2f2c4c7aSAndroid Build Coastguard Worker        fi
1050*2f2c4c7aSAndroid Build Coastguard Worker        if [ -z "$VENDOR_KERNEL_BUILD_TARGET" ]; then
1051*2f2c4c7aSAndroid Build Coastguard Worker            kernel_build_target_count=$(ls build_*.sh | wc -w)
1052*2f2c4c7aSAndroid Build Coastguard Worker            if (( kernel_build_target_count == 1 )); then
1053*2f2c4c7aSAndroid Build Coastguard Worker                VENDOR_KERNEL_BUILD_TARGET=$(echo $(ls build_*.sh) | sed 's/build_\(.*\)\.sh/\1/')
1054*2f2c4c7aSAndroid Build Coastguard Worker            elif (( kernel_build_target_count > 1 )); then
1055*2f2c4c7aSAndroid Build Coastguard Worker                print_warn "There are multiple build_*.sh scripts in $PWD, Can't decide vendor kernel build target" "$LINENO"
1056*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Please use -vkbt <value> or --vendor-kernel-build-target=<value> to specify a kernel build target" "$LINENO"
1057*2f2c4c7aSAndroid Build Coastguard Worker            else
1058*2f2c4c7aSAndroid Build Coastguard Worker                # TODO: Add build support to android12 and earlier kernels
1059*2f2c4c7aSAndroid Build Coastguard Worker                print_error "There is no build_*.sh script in $PWD" "$LINENO"
1060*2f2c4c7aSAndroid Build Coastguard Worker            fi
1061*2f2c4c7aSAndroid Build Coastguard Worker        fi
1062*2f2c4c7aSAndroid Build Coastguard Worker        if [ "$SKIP_BUILD" = false ] ; then
1063*2f2c4c7aSAndroid Build Coastguard Worker            build_cmd="./build_$VENDOR_KERNEL_BUILD_TARGET.sh"
1064*2f2c4c7aSAndroid Build Coastguard Worker            if [ "$GCOV" = true ]; then
1065*2f2c4c7aSAndroid Build Coastguard Worker                build_cmd+=" --gcov"
1066*2f2c4c7aSAndroid Build Coastguard Worker            fi
1067*2f2c4c7aSAndroid Build Coastguard Worker            if [ "$DEBUG" = true ]; then
1068*2f2c4c7aSAndroid Build Coastguard Worker                build_cmd+=" --debug"
1069*2f2c4c7aSAndroid Build Coastguard Worker            fi
1070*2f2c4c7aSAndroid Build Coastguard Worker            if [ "$KASAN" = true ]; then
1071*2f2c4c7aSAndroid Build Coastguard Worker                build_cmd+=" --kasan"
1072*2f2c4c7aSAndroid Build Coastguard Worker            fi
1073*2f2c4c7aSAndroid Build Coastguard Worker            print_info "Build vendor kernel with $build_cmd"
1074*2f2c4c7aSAndroid Build Coastguard Worker            eval "$build_cmd"
1075*2f2c4c7aSAndroid Build Coastguard Worker            exit_code=$?
1076*2f2c4c7aSAndroid Build Coastguard Worker            if [ $exit_code -eq 0 ]; then
1077*2f2c4c7aSAndroid Build Coastguard Worker                print_info "Build vendor kernel succeeded"
1078*2f2c4c7aSAndroid Build Coastguard Worker            else
1079*2f2c4c7aSAndroid Build Coastguard Worker                print_error "Build vendor kernel failed with exit code $exit_code"
1080*2f2c4c7aSAndroid Build Coastguard Worker                exit 1
1081*2f2c4c7aSAndroid Build Coastguard Worker            fi
1082*2f2c4c7aSAndroid Build Coastguard Worker        fi
1083*2f2c4c7aSAndroid Build Coastguard Worker        VENDOR_KERNEL_BUILD="$PWD/out/$VENDOR_KERNEL_BUILD_TARGET/dist"
1084*2f2c4c7aSAndroid Build Coastguard Worker    fi
1085*2f2c4c7aSAndroid Build Coastguard Workerfi
1086*2f2c4c7aSAndroid Build Coastguard Worker
1087*2f2c4c7aSAndroid Build Coastguard Workerif [ -z "$PLATFORM_BUILD" ]; then  # No platform build provided
1088*2f2c4c7aSAndroid Build Coastguard Worker    if [ -z "$KERNEL_BUILD" ] && [ -z "$VENDOR_KERNEL_BUILD" ]; then  # No kernel or vendor kernel build
1089*2f2c4c7aSAndroid Build Coastguard Worker        print_info "KERNEL_BUILD=$KERNEL_BUILD VENDOR_KERNEL_BUILD=$VENDOR_KERNEL_BUILD" "$LINENO"
1090*2f2c4c7aSAndroid Build Coastguard Worker        print_error "Nothing to flash" "$LINENO"
1091*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -z "$KERNEL_BUILD" ] && [ ! -z "$VENDOR_KERNEL_BUILD" ]; then  # Only vendor kernel build
1092*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Flash kernel from $VENDOR_KERNEL_BUILD" "$LINENO"
1093*2f2c4c7aSAndroid Build Coastguard Worker        flash_vendor_kernel_build
1094*2f2c4c7aSAndroid Build Coastguard Worker    elif [ ! -z "$KERNEL_BUILD" ] && [ ! -z "$VENDOR_KERNEL_BUILD" ]; then  # Both kernel and vendor kernel builds
1095*2f2c4c7aSAndroid Build Coastguard Worker        print_error "Mixing only GKI build & vendor kernel build is not supported. \
1096*2f2c4c7aSAndroid Build Coastguard WorkerPlease add platform build for example -pb ab://git_main/$PRODUCT-trunk_staging-userdebug/latest." "$LINENO"
1097*2f2c4c7aSAndroid Build Coastguard Worker    elif [ ! -z "$KERNEL_BUILD" ] && [ -z "$VENDOR_KERNEL_BUILD" ]; then  # Only GKI build
1098*2f2c4c7aSAndroid Build Coastguard Worker        gki_build_only_operation
1099*2f2c4c7aSAndroid Build Coastguard Worker    fi
1100*2f2c4c7aSAndroid Build Coastguard Workerelse  # Platform build provided
1101*2f2c4c7aSAndroid Build Coastguard Worker    if [ -z "$KERNEL_BUILD" ] && [ -z "$VENDOR_KERNEL_BUILD" ]; then  # No kernel or vendor kernel build
1102*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Flash platform build only"
1103*2f2c4c7aSAndroid Build Coastguard Worker        flash_platform_build
1104*2f2c4c7aSAndroid Build Coastguard Worker    elif [ -z "$KERNEL_BUILD" ] && [ ! -z "$VENDOR_KERNEL_BUILD" ]; then  # Vendor kernel build and platform build
1105*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Mix vendor kernel and platform build"
1106*2f2c4c7aSAndroid Build Coastguard Worker        mixing_build
1107*2f2c4c7aSAndroid Build Coastguard Worker        flash_platform_build
1108*2f2c4c7aSAndroid Build Coastguard Worker    elif [ ! -z "$KERNEL_BUILD" ] && [ -z "$VENDOR_KERNEL_BUILD" ]; then # GKI build and platform build
1109*2f2c4c7aSAndroid Build Coastguard Worker        flash_platform_build
1110*2f2c4c7aSAndroid Build Coastguard Worker        get_device_info
1111*2f2c4c7aSAndroid Build Coastguard Worker        gki_build_only_operation
1112*2f2c4c7aSAndroid Build Coastguard Worker    elif [ ! -z "$KERNEL_BUILD" ] && [ ! -z "$VENDOR_KERNEL_BUILD" ]; then  # All three builds provided
1113*2f2c4c7aSAndroid Build Coastguard Worker        print_info "Mix GKI kernel, vendor kernel and platform build" "$LINENO"
1114*2f2c4c7aSAndroid Build Coastguard Worker        mixing_build
1115*2f2c4c7aSAndroid Build Coastguard Worker        flash_platform_build
1116*2f2c4c7aSAndroid Build Coastguard Worker    fi
1117*2f2c4c7aSAndroid Build Coastguard Workerfi
1118