README.md
1This document describes how to build and run an Android system image targeting
2the ARM Fixed Virtual Platform or QEMU.
3
4## New to Android?
5
6If you do not already have the ``repo`` tool, or a copy of the Android
7source tree, please follow the Android instructions for [downloading the
8source](https://source.android.com/setup/build/downloading).
9
10## Building the kernel
11
12```
13mkdir android-kernel-mainline
14cd android-kernel-mainline
15export FVP_KERNEL_PATH=$(pwd)
16repo init -u https://android.googlesource.com/kernel/manifest -b common-android-mainline
17repo sync
18repo start fvp-patches common -r 79f312ba371eeba2f3ab043b9b56823a459052c8
19```
20
21To support graphics on FVP, one additional cherry pick is required. This only
22applies to the ``fvp`` target, and not ``fvp_mini``, and it is also not required
23for QEMU.
24
25```
26repo download -c common 1463463
27```
28
29Then, build the kernel.
30
31```
32BUILD_CONFIG=common/build.config.gki.aarch64 build/build.sh -j$(nproc)
33BUILD_CONFIG=common-modules/virtual-device/build.config.virtual_device.aarch64 build/build.sh -j$(nproc)
34```
35
36## Building the firmware (ARM Trusted Firmware and U-Boot) (FVP only, not required on QEMU)
37
38First, install ``dtc``, the device tree compiler. On Debian, this is in the
39``device-tree-compiler`` package. Return to the top level directory (`cd ..`), and run:
40```
41mkdir platform
42cd platform
43export FVP_FIRMWARE_PATH=$(pwd)
44repo init -u https://git.linaro.org/landing-teams/working/arm/manifest.git -m pinned-uboot.xml -b 20.01
45repo sync
46
47# The included copy of U-Boot is incompatible with this version of AOSP, switch to a recent upstream checkout.
48cd u-boot
49git fetch https://gitlab.denx.de/u-boot/u-boot.git/ master
50git checkout 18b9c98024ec89e00a57707f07ff6ada06089d26
51cd ..
52
53mkdir -p tools/gcc
54cd tools/gcc
55wget https://releases.linaro.org/components/toolchain/binaries/6.2-2016.11/aarch64-linux-gnu/gcc-linaro-6.2.1-2016.11-x86_64_aarch64-linux-gnu.tar.xz
56tar -xJf gcc-linaro-6.2.1-2016.11-x86_64_aarch64-linux-gnu.tar.xz
57cd ../..
58
59build-scripts/build-test-uboot.sh -p fvp all
60```
61
62## Building userspace
63
64Follow the Android instructions to [download the
65source](https://source.android.com/setup/build/downloading), and run the
66following in the source directory.
67
68```
69. build/envsetup.sh
70lunch fvp-eng # or fvp-userdebug, fvp_mini-eng, fvp_mini-userdebug
71```
72
73The fvp-* lunch targets will build a full Android with UI support, while
74`fvp_mini-*` will build a small subset needed to boot to shell and support
75command line executables.
76
77Prepopulate the build directory with the kernel and firmware binaries. Normally,
78these are copied from the source tree as part of the build process, but not for
79this target yet.
80
81```
82mkdir -p $ANDROID_PRODUCT_OUT
83cp $FVP_KERNEL_PATH/out/android-mainline/dist/Image $ANDROID_PRODUCT_OUT/kernel
84cp $FVP_KERNEL_PATH/out/android-mainline/dist/{fvp-base-revc.dtb,initramfs.img} $ANDROID_PRODUCT_OUT/
85
86# FVP only! QEMU doesn't require custom-built firmware.
87cp $FVP_FIRMWARE_PATH/output/fvp/fvp-uboot/uboot/{bl1,fip}.bin $ANDROID_PRODUCT_OUT/
88```
89
90Set any additional build environment variables.
91* To enable MTE on all platform binaries (by default it is only enabled on a
92 small subset) add `export SANITIZE_TARGET=memtag_heap` for Async mode, or
93 `export SANITIZE_TARGET=memtag_heap SANITIZE_TARGET_DIAG=memtag_heap` for Sync
94 mode.
95* To disable 32 bit support in fvp_mini-* targets use
96 `export FVP_MULTILIB_BUILD=false`.
97
98Finally, build the userspace image with `m`.
99
100## Running
101
102The same image can be used with either ARM Fixed Virtual Platform simulator, or
103with QEMU. Slowdown from QEMU is roughly 10-20x, where ARM's FVP is 100-200x.
104
105### Running the image in FVP
106
107The model may be obtained from [ARM's
108website](https://developer.arm.com/tools-and-software/simulation-models/fixed-virtual-platforms/arm-ecosystem-models)
109(under "Armv-A Base RevC AEM FVP").
110
111From a lunched environment, first set the value of the ``MODEL_BIN`` environment
112variable to the path to the model executable (it should end with something like
113`FVP_Base_RevC-2xAEMv8A/models/Linux64_GCC-6.4/FVP_Base_RevC-2xAEMv8A`). Then
114run the following command to launch the model:
115```
116device/generic/goldfish/fvpbase/run_model
117```
118Additional model parameters may be passed by appending them to the
119``run_model`` command. Add the following to enable MTE support in the model:
120```
121-C cluster0.has_arm_v8-5=1 \
122-C cluster0.memory_tagging_support_level=2 \
123-C bp.dram_metadata.is_enabled=1
124```
125
126To terminate the model, press ``Ctrl-] Ctrl-D`` to terminate the telnet
127connection.
128
129### Running the image in QEMU
130
131As an alternative to using FVP, the image may also be run in QEMU.
132QEMU is generally much faster than FVP, but its support for the
133latest ARM architectural features is relatively new compared to FVP,
134so it may have more bugs.
135
136As of the time of writing, no released version of QEMU can successfully
137boot the system to GUI due to bugs in its MTE support, so a development
138version with bug fixes must be used. The instructions below check out a
139commit that has been successfully tested.
140
141Check [QEMU wiki](https://wiki.qemu.org/Hosts/Linux#Building_QEMU_for_Linux) for
142the list of build dependencies. Common missing packages include `ninja-build`,
143`libpixman-1-dev`, and `libgtk-3-dev` for GUI support.
144
145```
146git clone https://github.com/qemu/qemu
147cd qemu
148git checkout 5c6295a45b4fceac913c11abc62488c49c02b9fd
149mkdir build
150cd build
151../configure --target-list=aarch64-softmmu
152ninja
153export QEMU_BIN=$(pwd)/qemu-system-aarch64
154```
155
156Then run the following command in a lunched environment to start the emulator:
157```
158device/generic/goldfish/fvpbase/run_qemu
159```
160Additional QEMU arguments may be passed by appending them to the ``run_qemu``
161command. One useful argument is ``-nographic``, which disables the GUI, which
162may be useful when working with ``fvp_mini`` or if the GUI is not needed.
163
164To terminate the emulator, press ``Ctrl-A c q <Enter>`` or close the GUI
165window.
166
167### Accessing the model via adb
168
169To connect to the model on the host:
170```
171adb connect localhost:5555
172```
173