1#!/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"
24FECR_DEVICE_DIR="/data/local/tmp/ferrochrome"
25FECR_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)
29AOSP_PKG_NAME="com.android.virtualization.vmlauncher"
30SIGNED_PKG_NAME="com.google.android.virtualization.vmlauncher"
31
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() {
41  echo "ferochrome.sh: Launches ferrochrome image"
42  echo ""
43  echo "By default, this downloads ferrochrome image with version ${FECR_DEFAULT_VERSION},"
44  echo "launches, and waits for boot completed."
45  echo "When done, removes downloaded image."
46  echo ""
47  echo "Usage: ferrochrome.sh [options]"
48  echo ""
49  echo "Options"
50  echo "  --help or -h: This message"
51  echo "  --dir \${dir}: Use ferrochrome images at the dir instead of downloading"
52  echo "  --skip: Skipping downloading and/or pushing images"
53  echo "  --version \${version}: ferrochrome version to be downloaded"
54  echo "  --keep: Keep downloaded ferrochrome image"
55}
56
57
58fecr_version=""
59fecr_dir=""
60fecr_keep=""
61fecr_skip=""
62fecr_script_path=$(dirname ${0})
63
64# Parse parameters
65while (( "${#}" )); do
66  case "${1}" in
67    --version)
68      shift
69      fecr_version="${1}"
70      ;;
71    --dir)
72      shift
73      fecr_dir="${1}"
74      fecr_keep="true"
75      ;;
76    --keep)
77      fecr_keep="true"
78      ;;
79    --skip)
80      fecr_skip="true"
81      ;;
82    -h|--help)
83      print_usage
84      exit 0
85      ;;
86    *)
87      print_usage
88      exit 1
89      ;;
90  esac
91  shift
92done
93
94trap fecr_clean_up INT
95trap fecr_clean_up EXIT
96
97if [[ -z "${fecr_skip}" ]]; then
98  if [[ -z "${fecr_dir}" ]]; then
99    # Download fecr image archive, and extract necessary files
100    # DISCLAIMER: Image is too large (1.5G+ for compressed, 6.5G+ for uncompressed), so can't submit.
101    fecr_dir=$(mktemp -d)
102
103    echo "Downloading ferrochrome image to ${fecr_dir}"
104    fecr_version=${fecr_version:-${FECR_DEFAULT_VERSION}}
105    curl --output-dir ${fecr_dir} -O ${FECR_GS_URL}/${fecr_version}/image.zip
106  fi
107  if [[ ! -f "${fecr_dir}/chromiumos_test_image.bin" ]]; then
108    unzip ${fecr_dir}/image.zip chromiumos_test_image.bin boot_images/vmlinuz* -d ${fecr_dir} > /dev/null
109  fi
110
111  echo "Pushing ferrochrome image to ${FECR_DEVICE_DIR}"
112  adb shell mkdir -p ${FECR_DEVICE_DIR} > /dev/null || true
113  adb push ${fecr_dir}/chromiumos_test_image.bin ${FECR_DEVICE_DIR}
114  adb push ${fecr_dir}/boot_images/vmlinuz ${FECR_DEVICE_DIR}
115  adb push ${fecr_script_path}/assets/vm_config.json ${FECR_CONFIG_PATH}
116fi
117
118adb root > /dev/null
119adb shell pm list packages | grep ${AOSP_PKG_NAME} > /dev/null
120if [[ "${?}" == "0" ]]; then
121  pkg_name=${AOSP_PKG_NAME}
122else
123  pkg_name=${SIGNED_PKG_NAME}
124fi
125
126adb shell pm enable ${pkg_name}/${AOSP_PKG_NAME}.MainActivity > /dev/null
127adb shell pm grant ${pkg_name} android.permission.USE_CUSTOM_VIRTUAL_MACHINE > /dev/null
128adb shell pm clear ${pkg_name} > /dev/null
129
130echo "Starting ferrochrome"
131adb shell am start-activity ${pkg_name}/${AOSP_PKG_NAME}.MainActivity > /dev/null
132
133log_path="/data/data/${pkg_name}/files/console.log"
134fecr_start_time=${EPOCHSECONDS}
135
136while [[ $((EPOCHSECONDS - fecr_start_time)) -lt ${FECR_BOOT_TIMEOUT} ]]; do
137  adb shell grep -sF \""${FECR_BOOT_COMPLETED_LOG}"\" "${log_path}" && exit 0
138  sleep 10
139done
140
141echo "Ferrochrome failed to boot"
142exit 1
143