Merge "Adding GSI build capability into Auto emulators" into rvc-qpr-dev am: abd557cabb
Original change: https://googleplex-android-review.googlesource.com/c/device/generic/car/+/12990459
Change-Id: I399f0004bd5c5e2ac718da6997b0fe69521d91f3
diff --git a/METADATA b/METADATA
new file mode 100644
index 0000000..d97975c
--- /dev/null
+++ b/METADATA
@@ -0,0 +1,3 @@
+third_party {
+ license_type: NOTICE
+}
diff --git a/tools/README.md b/tools/README.md
new file mode 100644
index 0000000..b78ec1e
--- /dev/null
+++ b/tools/README.md
@@ -0,0 +1,70 @@
+# AAOS car AVD tools
+This folder contains scripts to help you to build, run and share car AVD images. We intentailly keep this on the AOSP main trunk for vairous branches from Android 10.
+
+## Download the tools
+- To clone the tool only: git_clone_projects.sh
+- Link it into an Android source tree, e.g.:
+```
+ln -s $HOME/avd/car/tools $ANDROID_BUILD_TOP/device/generic/car/tools
+```
+
+## Build an AVD image
+This builds AOSP car x86_64 userdebug AVD.
+```
+. device/generic/car/tools/mk_car_avd.sh
+```
+
+## Pack an AVD image
+If you plan to share the AVD with others or run on other host via Android Emulator directly, you can pack necessary image files to $HOME/Downloads/x86_64
+```
+device/generic/car/tools/test_pack_avd_img.sh
+```
+* You can use it as an example to create your own script for an AVD target by pack_avd_img.sh.
+* pack_avd_img.sh contains brief descriptions of each file needed.
+
+## Start an AVD
+
+### New AVD config
+To create a clean local AVD config and run with the latest Android Emulator engine from the SDK.
+```
+device/generic/car/tools/test_run_local_avd.sh
+```
+
+To run the existing AVD config.
+```
+device/generic/car/tools/run_local_avd.sh
+```
+
+### Multiple AVDs
+run_local_avd.sh allows you to setup and run multiple AVDs. You can crease a script for each of them. e.g.
+```
+AVD_IMAGE_DIR=$HOME/avd/aosp_car_x86_64/x86_64 \
+ WORKDIR=$HOME/avd/aosp_car_x86_64 \
+ ABI="x86_64" \
+ $SCRIPT_DIR/tools/run_local_avd.sh
+```
+* Deleting the WORKDIR will let the script create it from scratch next time as the 1st run.
+
+### Change the config
+The default AVD config.ini are created as the following default settings at the 1st run. You can also change them at the 1st run, e.g.
+```
+ANDROID_SDK_ROOT="/Users/$USER/Library/Android/sdk" \
+ WORKDIR="$HOME/avd/aosp_car_x86_64" \
+ AVD_IMAGE_DIR="$HOME/avd/aosp_car_x86_64/x86_64" \
+ ABI="x86_64" \
+ DISPLAY_DENSITY=213 \
+ DISPLAY_WIDTH=1920 \
+ DISPLAY_HEIGHT=1080 \
+ RAM_MB=3584 \
+ ./run_local_avd.sh
+```
+
+The AVD can also be changed by editing the AVD config.ini directily, e.g. at:
+```
+$WORKDIR/.android/avd/my_car_avd_x86_64.avd/config.ini
+```
+
+### Android Emulator startup options
+You can append [Android Emulator Command-line startup options](https://developer.android.com/studio/run/emulator-commandline#common) as needed. E.g.
+ * to wipe user data: ./run_local_avd.sh -wipe-data
+ * to cold boot: ./run_local_avd.sh -no-snapshot-load
diff --git a/tools/create_avd_config.sh b/tools/create_avd_config.sh
new file mode 100755
index 0000000..f1f672f
--- /dev/null
+++ b/tools/create_avd_config.sh
@@ -0,0 +1,96 @@
+#!/bin/bash
+
+# Copyright (C) 2020 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Creates an car AVD configuration
+#
+# Arguments:
+# $1: (AVD_NAME) Name of the AVD
+# $2: (WORKDIR) where to store the avd config
+# $3: (AVD_IMAGE_DIR) AVD System image dir
+# $4: (DISPLAY_DENSITY) Display density
+# $5: (DISPLAY_WIDTH) Display width px
+# $6: (DISPLAY_HEIGHT) Display height px
+# $7: (RAM_MB) AVD RAM size in MB
+# $8: (ABD) AVD ABI, e.g. x86 or x86_64
+
+AVD_NAME=$1
+WORKDIR=$2
+AVD_IMAGE_DIR=$3
+DISPLAY_DENSITY=$4
+DISPLAY_WIDTH=$5
+DISPLAY_HEIGHT=$6
+RAM_MB=$7
+ABI=$8
+
+mkdir -p ${WORKDIR}/.android/avd/${AVD_NAME}.avd
+
+# avd_name.ini
+AVD_INI_FILE=${WORKDIR}/.android/avd/${AVD_NAME}.ini
+echo Creating $AVD_INI_FILE
+echo "avd.ini.encoding=UTF-8" >> $AVD_INI_FILE
+echo "path=${WORKDIR}/.android/avd/${AVD_NAME}.avd" >> $AVD_INI_FILE
+echo "path.rel=avd/${AVD_NAME}.avd" >> $AVD_INI_FILE
+
+# avd_name.avd/config.ini
+AVD_CONFIG_INI=${WORKDIR}/.android/avd/${AVD_NAME}.avd/config.ini
+echo Creating $AVD_CONFIG_INI $DISPLAY_WIDTH x $DISPLAY_HEIGHT @ $DISPLAY_DENSITY with $RAM_MB
+echo AVD Img: $AVD_IMAGE_DIR
+cat <<EOT >> $AVD_CONFIG_INI
+
+image.sysdir.1 = ${AVD_IMAGE_DIR}
+hw.lcd.density = ${DISPLAY_DENSITY}
+hw.lcd.width = ${DISPLAY_WIDTH}
+hw.lcd.height = ${DISPLAY_HEIGHT}
+AvdId = ${AVD_NAME}
+avd.ini.displayname = ${AVD_NAME}
+hw.ramSize = ${RAM_MB}
+abi.type = ${ABI}
+
+tag.display = Automotive
+tag.id = android-automotive
+hw.device.manufacturer = google
+hw.device.name = hawk
+avd.ini.encoding = UTF-8
+disk.dataPartition.size = 6442450944
+fastboot.chosenSnapshotFile =
+fastboot.forceChosenSnapshotBoot = no
+fastboot.forceColdBoot = no
+fastboot.forceFastBoot = yes
+hw.accelerometer = no
+hw.arc = false
+hw.audioInput = yes
+hw.battery = no
+hw.camera.back = None
+hw.camera.front = None
+hw.cpu.arch = x86_64
+hw.cpu.ncore = 4
+hw.dPad = no
+hw.device.hash2 = MD5:1fdb01985c7b4d7c19ec309cc238b0f9
+hw.gps = yes
+hw.gpu.enabled = yes
+hw.gpu.mode = auto
+hw.initialOrientation = landscape
+hw.keyboard = yes
+hw.keyboard.charmap = qwerty2
+hw.keyboard.lid = false
+hw.mainKeys = no
+hw.sdCard = no
+hw.sensors.orientation = no
+hw.sensors.proximity = no
+hw.trackBall = no
+runtime.network.latency = none
+runtime.network.speed = full
+EOT
diff --git a/tools/git_clone_projects.sh b/tools/git_clone_projects.sh
new file mode 100755
index 0000000..13a9efc
--- /dev/null
+++ b/tools/git_clone_projects.sh
@@ -0,0 +1,62 @@
+#!/bin/bash
+
+# Copyright (C) 2020 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+echo "An example to clone minimal car/tools project to operate AVDs."
+
+if [[ -z $GIT_REPO_URL ]]; then
+ GIT_REPO_URL="https://android.googlesource.com"
+fi
+echo "GIT_REPO_URL=$GIT_REPO_URL"
+
+if [[ -z $BRANCH ]]; then
+ echo 'You may set BRANCH="target-branch"'
+fi
+echo "BRANCH=$BRANCH"
+
+if [[ -z $WORK_DIR ]]; then
+ export WORK_DIR="$PWD"
+fi
+echo "WORK_DIR=$WORK_DIR"
+
+mkdir -p $WORK_DIR
+cd $WORK_DIR
+
+echo "git clone https://android.googlesource.com/device/generic/car"
+PROJECTS=0
+SECONDS=0
+PROJECT_PATH="device/generic/car"
+if [[ -z $BRANCH ]]; then
+ git clone "$GIT_REPO_URL/$PROJECT_PATH"
+else
+ git clone -b $BRANCH "$GIT_REPO_URL/$PROJECT_PATH"
+fi
+let "PROJECTS++"
+cd "$WORK_DIR/car"
+f=`git rev-parse --git-dir`/hooks/commit-msg ; mkdir -p $(dirname $f) ; curl -Lo $f https://gerrit-review.googlesource.com/tools/hooks/commit-msg ; chmod +x $f
+cd "$WORK_DIR"
+echo
+
+ls -l "$WORK_DIR"
+
+echo "
+
+Cloning $PROJECTS projects takes: $SECONDS sec.
+
+Do your magic and then get the change pushed for review, e.g.:
+git add -u
+git commit
+git push origin HEAD:refs/for/BRANCH
+"
diff --git a/tools/mk_car_avd.sh b/tools/mk_car_avd.sh
new file mode 100755
index 0000000..fd94423
--- /dev/null
+++ b/tools/mk_car_avd.sh
@@ -0,0 +1 @@
+. build/envsetup.sh ; lunch aosp_car_x86_64-userdebug; m -j32
\ No newline at end of file
diff --git a/tools/pack_avd_img.sh b/tools/pack_avd_img.sh
new file mode 100755
index 0000000..02ce6e9
--- /dev/null
+++ b/tools/pack_avd_img.sh
@@ -0,0 +1,100 @@
+#!/bin/bash
+
+# Copyright (C) 2020 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+echo pack_avd_img.sh packs AVD images you built to be used on other hosts.
+echo It copies necessary files from ANDROID_PRODUCT_OUT to OUT_DIR.
+
+if [[ -z $ANDROID_PRODUCT_OUT ]]; then
+ echo "err: please set ANDROID_PRODUCT_OUT='/android/out/target/product/generic_x86_64'"
+ exit
+fi
+
+if [[ -z $ANDROID_BUILD_TOP ]]; then
+ echo "err: please set ANDROID_BUILD_TOP='/android'"
+ exit
+fi
+
+if [[ -z $OUT_DIR ]]; then
+ echo "err: please set OUT_DIR='/my/out/dir/x86_64'"
+ exit
+fi
+
+echo Packing AVD images from $ANDROID_PRODUCT_OUT to $OUT_DIR
+
+if [[ ! -d $OUT_DIR ]]; then
+ mkdir $OUT_DIR
+fi
+
+echo System, qemu verion
+cp $ANDROID_PRODUCT_OUT/system-qemu.img $OUT_DIR/system.img
+
+echo Vendor, qemu verion
+cp $ANDROID_PRODUCT_OUT/vendor-qemu.img $OUT_DIR/vendor.img
+
+
+if [[ -f $ANDROID_PRODUCT_OUT/kernel-ranchu-64 ]]; then
+ echo Kernel: 64-bit, prebuilt
+ cp $ANDROID_PRODUCT_OUT/kernel-ranchu-64 $OUT_DIR/kernel-ranchu-64
+else
+ echo Kernel: 64-bit for 32-bit userspaces, prebuilt
+ cp $ANDROID_PRODUCT_OUT/kernel-ranchu $OUT_DIR/kernel-ranchu
+fi
+
+echo Ramdisk, prebuilt
+cp $ANDROID_PRODUCT_OUT/ramdisk-qemu.img $OUT_DIR/ramdisk.img
+
+echo Encryptionkey, prebuilt
+cp $ANDROID_PRODUCT_OUT/encryptionkey.img $OUT_DIR/encryptionkey.img
+
+echo Userdata, prebuilt
+# take prebuilt userdata.img
+#Ref:https://cs.android.com/android/platform/superproject/+/master:development/build/sdk.atree?q=userdata.img&ss=android%2Fplatform%2Fsuperproject:development%2Fbuild%2F
+cp $ANDROID_BUILD_TOP/device/generic/goldfish/data/etc/userdata.img $OUT_DIR/userdata.img
+
+echo User data
+#Todo: will this replace the need of userdata.img?
+cp -r $ANDROID_PRODUCT_OUT/data $OUT_DIR/data
+
+echo build properties
+cp $ANDROID_PRODUCT_OUT/system/build.prop $OUT_DIR/build.prop
+
+echo Verified Boot Parameters
+cp $ANDROID_PRODUCT_OUT/VerifiedBootParams.textproto $OUT_DIR/VerifiedBootParams.textproto
+
+echo Default AVD config.ini
+cp $ANDROID_PRODUCT_OUT/config.ini $OUT_DIR/config.ini
+
+echo Android Emulator Adviced Feature settings
+cp $ANDROID_PRODUCT_OUT/advancedFeatures.ini $OUT_DIR/advancedFeatures.ini
+
+echo
+echo In $OUT_DIR:
+ls -l $OUT_DIR
+
+if [[ -n $AVD_IMAGE_ZIP ]]; then
+
+ rm $AVD_IMAGE_ZIP
+
+ echo Zipping $OUT_DIR to $AVD_IMAGE_ZIP
+ # to perserve ABI in the zip, assuming OUT_DIR always ends with ABI
+ # e.g. /path/to/x86_64
+ ABI=${OUT_DIR##*/} # = "x86_64"
+ PATH_TO_IMG_DIR=${OUT_DIR%/$ABI} # = "/path/to"
+ cd $PATH_TO_IMG_DIR
+ zip -r $AVD_IMAGE_ZIP $ABI
+ cd -
+ unzip -l $AVD_IMAGE_ZIP
+fi
diff --git a/tools/remount.sh b/tools/remount.sh
new file mode 100755
index 0000000..4ae04f9
--- /dev/null
+++ b/tools/remount.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# Copyright (C) 2020 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+echo "To remount the system partition to be writable:"
+echo "The AVD has to be 1. rootable & 2. started with -writable-system option"
+
+echo "Disabling Verified Build"
+adb root
+adb disable-verity
+adb shell avbctl disable-verification
+
+echo "Rebooting, rooting & remouting"
+adb reboot; adb wait-for-device
+echo "Need to wait long enough for adb to be ready to remount"
+sleep 10
+adb root
+adb remount
\ No newline at end of file
diff --git a/tools/run_local_avd.sh b/tools/run_local_avd.sh
new file mode 100755
index 0000000..dee99aa
--- /dev/null
+++ b/tools/run_local_avd.sh
@@ -0,0 +1,109 @@
+#!/bin/bash
+
+# Copyright (C) 2020 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# See test_run_local_avd.sh for examples
+
+echo Run a local AVD config in WORKDIR by Android Emulator engine from the SDK
+
+OS="$(uname -s)"
+echo Running on $OS
+if [[ $OS == "Linux" ]]; then
+ DEFAULT_ANDROID_SDK_ROOT="$HOME/Android/Sdk"
+elif [[ $OS == "Darwin" ]]; then
+ DEFAULT_ANDROID_SDK_ROOT="/Users/$USER/Library/Android/sdk"
+else
+ echo Sorry, this does not work on $OS
+ exit
+fi
+
+if [[ -z $ANDROID_SDK_ROOT ]]; then
+ ANDROID_SDK_ROOT="$DEFAULT_ANDROID_SDK_ROOT"
+fi
+
+if [[ -z $WORKDIR ]]; then
+ WORKDIR="$HOME/workdir"
+fi
+
+if [[ -z $ANDROID_AVD_HOME ]]; then
+ ANDROID_AVD_HOME="$WORKDIR/.android/avd"
+fi
+
+if [[ -z $ABI ]]; then
+ ABI="x86_64"
+fi
+
+if [[ -z $AVD_IMAGE_DIR ]]; then
+ AVD_IMAGE_DIR="$WORKDIR/$ABI"
+fi
+
+if [[ -z $AVD_NAME ]]; then
+ AVD_NAME="my_car_avd_$ABI"
+fi
+
+if [[ -z $DISPLAY_DENSITY ]]; then
+ DISPLAY_DENSITY=213
+fi
+
+if [[ -z $DISPLAY_WIDTH ]]; then
+ DISPLAY_WIDTH=1920
+fi
+
+if [[ -z $DISPLAY_HEIGHT ]]; then
+ DISPLAY_HEIGHT=1080
+fi
+
+if [[ -z $RAM_MB ]]; then
+ # 3.5 GB as x86 AVD is limited to 4g
+ RAM_MB=3584
+fi
+
+# Get the script dir
+MY_NAME=$0
+MY_FILENAME=${MY_NAME##*/} # = "name.sh"
+MY_DIR=${MY_NAME%/$MY_FILENAME} # = "/path/to"
+
+if ! [[ -f "$ANDROID_AVD_HOME/$AVD_NAME.avd/config.ini" ]]; then
+ # Create the AVD config as there is no one
+ $MY_DIR/create_avd_config.sh "$AVD_NAME" "$WORKDIR" "$AVD_IMAGE_DIR" \
+ "$DISPLAY_DENSITY" "$DISPLAY_WIDTH" "$DISPLAY_HEIGHT" "$RAM_MB" "$ABI"
+fi
+
+echo Running "$ANDROID_AVD_HOME/$AVD_NAME.avd" by $ANDROID_SDK_ROOT
+# Tell emu where to find my car AVD & SDK
+ANDROID_AVD_HOME=$ANDROID_AVD_HOME \
+ ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT \
+ $ANDROID_SDK_ROOT/emulator/emulator \
+ -avd $AVD_NAME \
+ $@ &
+
+echo
+sleep 30
+echo a. Supported features
+adb shell pm list features
+
+echo
+echo b. GAS versions
+adb shell pm list packages --show-versioncode | grep google
+adb shell pm list packages --show-versioncode | grep vending
+
+echo
+echo c. AVD Memory Info
+adb shell "cat /proc/meminfo"
+
+echo
+echo d. AVD Prop and processes
+adb shell getprop
+adb shell ps
diff --git a/tools/test_pack_avd_img.sh b/tools/test_pack_avd_img.sh
new file mode 100755
index 0000000..c063ffa
--- /dev/null
+++ b/tools/test_pack_avd_img.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+
+# Copyright (C) 2020 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+echo Pack AVD images from ANDROID_PRODUCT_OUT to $HOME/Downloads/x86_64
+
+if [[ -z $ANDROID_PRODUCT_OUT ]]; then
+ echo "err: please set ANDROID_PRODUCT_OUT='/android/out/target/product/generic_x86_64'"
+ exit
+fi
+
+if [[ -z $ANDROID_BUILD_TOP ]]; then
+ echo "err: please set ANDROID_BUILD_TOP='/android'"
+ exit
+fi
+
+# Defaults
+if [[ -z $OUT_DIR ]]; then
+ OUT_DIR="$HOME/Downloads/x86_64"
+fi
+
+if [[ -z $AVD_IMAGE_ZIP ]]; then
+ AVD_IMAGE_ZIP="$HOME/Downloads/my_aosp_car_x86_64-qt-v123456.zip"
+fi
+
+echo Delete $OUT_DIR
+rm -rf $OUT_DIR
+
+echo Packing AVD images
+OUT_DIR=$OUT_DIR \
+ AVD_IMAGE_ZIP=$AVD_IMAGE_ZIP \
+ $ANDROID_BUILD_TOP/device/generic/car/tools/pack_avd_img.sh
diff --git a/tools/test_run_local_avd.sh b/tools/test_run_local_avd.sh
new file mode 100755
index 0000000..2be5840
--- /dev/null
+++ b/tools/test_run_local_avd.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+# Copyright (C) 2020 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+echo Setup a clean local AVD with the Img in $HOME/Downloads/x86_64 and run
+
+MY_NAME=$0
+MY_FILENAME=${MY_NAME##*/} # = "test_run_local_avd.sh"
+MY_DIR=${MY_NAME%/$MY_FILENAME} # = "/path/to"
+echo running from $MY_DIR
+
+WORKDIR="$HOME/workdir"
+echo Setup a clean $WORKDIR
+rm -rf $WORKDIR
+mkdir $WORKDIR
+
+ABI="x86_64"
+AVD_IMAGE_DIR="$HOME/Downloads/$ABI"
+
+echo link $AVD_IMAGE_DIR to "$WORKDIR/$ABI"
+ln -s $AVD_IMAGE_DIR "$WORKDIR/$ABI"
+
+WORKDIR=$WORKDIR \
+ ABI=$ABI \
+ AVD_IMAGE_DIR=$AVD_IMAGE_DIR \
+ $MY_DIR/run_local_avd.sh -verbose -show-kernel -debug init $@