blob: 210548a748680ea4930cda2773aeb011ac446f01 [file] [log] [blame]
Jaewan Kimb5fb9692024-06-10 14:18:12 +09001#!/bin/bash
2
3# Copyright 2024 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17## Booting tests for ferrochrome
18## Keep this file synced with docs/custom_vm.md
19
20set -e
21
22FECR_GS_URL="https://storage.googleapis.com/chromiumos-image-archive/ferrochrome-public"
23FECR_DEFAULT_VERSION="R127-15916.0.0"
Jaewan Kim5ebeb222024-07-05 19:14:53 +090024FECR_DEVICE_DIR="/data/local/tmp"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090025FECR_CONFIG_PATH="/data/local/tmp/vm_config.json" # hardcoded at VmLauncherApp
26FECR_CONSOLE_LOG_PATH="/data/data/\${pkg_name}/files/console.log"
27FECR_BOOT_COMPLETED_LOG="Have fun and send patches!"
28FECR_BOOT_TIMEOUT="300" # 5 minutes (300 seconds)
Jaewan Kim3ad58c32024-06-13 08:59:29 +000029ACTION_NAME="android.virtualization.VM_LAUNCHER"
Jaewan Kim920d4662024-07-09 05:02:36 +000030TRY_UNLOCK_MAX=10
Jaewan Kimb5fb9692024-06-10 14:18:12 +090031
32fecr_clean_up() {
33 trap - INT
34
35 if [[ -d ${fecr_dir} && -z ${fecr_keep} ]]; then
36 rm -rf ${fecr_dir}
37 fi
38}
39
40print_usage() {
Jaewan Kim3ad58c32024-06-13 08:59:29 +000041 echo "ferochrome: Launches ferrochrome image"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090042 echo ""
43 echo "By default, this downloads ferrochrome image with version ${FECR_DEFAULT_VERSION},"
44 echo "launches, and waits for boot completed."
Jaewan Kim3ad58c32024-06-13 08:59:29 +000045 echo "When done, removes downloaded image on host while keeping pushed image on device."
Jaewan Kimb5fb9692024-06-10 14:18:12 +090046 echo ""
Jaewan Kim3ad58c32024-06-13 08:59:29 +000047 echo "Usage: ferrochrome [options]"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090048 echo ""
49 echo "Options"
50 echo " --help or -h: This message"
Jaewan Kim3ad58c32024-06-13 08:59:29 +000051 echo " --dir DIR: Use ferrochrome images at the dir instead of downloading"
52 echo " --verbose: Verbose log message (set -x)"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090053 echo " --skip: Skipping downloading and/or pushing images"
54 echo " --version \${version}: ferrochrome version to be downloaded"
55 echo " --keep: Keep downloaded ferrochrome image"
56}
57
Jaewan Kimb5fb9692024-06-10 14:18:12 +090058fecr_version=""
59fecr_dir=""
60fecr_keep=""
61fecr_skip=""
62fecr_script_path=$(dirname ${0})
Jaewan Kim3ad58c32024-06-13 08:59:29 +000063fecr_verbose=""
Jaewan Kimb5fb9692024-06-10 14:18:12 +090064
65# Parse parameters
66while (( "${#}" )); do
67 case "${1}" in
Jaewan Kim3ad58c32024-06-13 08:59:29 +000068 --verbose)
69 fecr_verbose="true"
70 ;;
Jaewan Kimb5fb9692024-06-10 14:18:12 +090071 --version)
72 shift
73 fecr_version="${1}"
74 ;;
75 --dir)
76 shift
77 fecr_dir="${1}"
78 fecr_keep="true"
79 ;;
80 --keep)
81 fecr_keep="true"
82 ;;
83 --skip)
84 fecr_skip="true"
85 ;;
86 -h|--help)
87 print_usage
88 exit 0
89 ;;
90 *)
91 print_usage
92 exit 1
93 ;;
94 esac
95 shift
96done
97
98trap fecr_clean_up INT
99trap fecr_clean_up EXIT
100
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000101if [[ -n "${fecr_verbose}" ]]; then
102 set -x
103fi
104
105. "${fecr_script_path}/ferrochrome-precondition-checker.sh"
106
107resolved_activities=$(adb shell pm query-activities --components -a ${ACTION_NAME})
108
109if [[ "$(echo ${resolved_activities} | wc -l)" != "1" ]]; then
110 >&2 echo "Multiple VM launchers exists"
111 exit 1
112fi
113
114pkg_name=$(dirname ${resolved_activities})
115
116adb shell pm grant ${pkg_name} android.permission.USE_CUSTOM_VIRTUAL_MACHINE > /dev/null
117adb shell pm clear ${pkg_name} > /dev/null
118
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900119if [[ -z "${fecr_skip}" ]]; then
120 if [[ -z "${fecr_dir}" ]]; then
121 # Download fecr image archive, and extract necessary files
122 # DISCLAIMER: Image is too large (1.5G+ for compressed, 6.5G+ for uncompressed), so can't submit.
123 fecr_dir=$(mktemp -d)
124
Jaewan Kim94455ba2024-06-18 14:22:44 +0900125 echo "Downloading & extracting ferrochrome image to ${fecr_dir}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900126 fecr_version=${fecr_version:-${FECR_DEFAULT_VERSION}}
Jaewan Kim94455ba2024-06-18 14:22:44 +0900127 curl ${FECR_GS_URL}/${fecr_version}/chromiumos_test_image.tar.xz | tar xfJ - -C ${fecr_dir}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900128 fi
129
130 echo "Pushing ferrochrome image to ${FECR_DEVICE_DIR}"
131 adb shell mkdir -p ${FECR_DEVICE_DIR} > /dev/null || true
132 adb push ${fecr_dir}/chromiumos_test_image.bin ${FECR_DEVICE_DIR}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900133 adb push ${fecr_script_path}/assets/vm_config.json ${FECR_CONFIG_PATH}
134fi
135
Jaewan Kim920d4662024-07-09 05:02:36 +0000136echo "Ensure screen unlocked"
137
138try_unlock=0
139while [[ "${try_unlock}" -le "${TRY_UNLOCK_MAX}" ]]; do
140 screen_state=$(adb shell dumpsys nfc | sed -n 's/^mScreenState=\(.*\)$/\1/p')
141 case "${screen_state}" in
142 "ON_UNLOCKED")
143 break
144 ;;
145 "ON_LOCKED")
146 # Disclaimer: This can unlock phone only if unlock method is swipe (default after FDR)
147 adb shell input keyevent KEYCODE_MENU
148 ;;
149 "OFF_LOCKED"|"OFF_UNLOCKED")
150 adb shell input keyevent KEYCODE_WAKEUP
151 ;;
152 *)
153 echo "Unknown screen state. Continue to boot, but may fail"
154 break
155 ;;
156 esac
157 sleep 1
158 try_unlock=$((try_unlock+1))
159done
160if [[ "${try_unlock}" -gt "${TRY_UNLOCK_MAX}" ]]; then
161 >&2 echo "Failed to unlock screen. Try again after manual unlock"
162 exit 1
163fi
164
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900165echo "Starting ferrochrome"
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000166adb shell am start-activity -a ${ACTION_NAME} > /dev/null
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900167
Jaewan Kim56f38342024-07-03 07:54:15 +0000168if [[ $(adb shell getprop ro.fw.mu.headless_system_user) == "true" ]]; then
169 current_user=$(adb shell am get-current-user)
170 log_path="/data/user/${current_user}/${pkg_name}/files/console.log"
171else
172 log_path="/data/data/${pkg_name}/files/console.log"
173fi
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900174fecr_start_time=${EPOCHSECONDS}
175
176while [[ $((EPOCHSECONDS - fecr_start_time)) -lt ${FECR_BOOT_TIMEOUT} ]]; do
177 adb shell grep -sF \""${FECR_BOOT_COMPLETED_LOG}"\" "${log_path}" && exit 0
178 sleep 10
179done
180
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000181>&2 echo "Ferrochrome failed to boot. Dumping console log"
182>&2 adb shell cat ${log_path}
183
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900184exit 1