blob: 29b24d31a50fdf3dc83a4dfb2f68322b01aec193 [file] [log] [blame]
Tristan Muntsinger82c10502019-10-15 14:49:19 -07001#!/bin/bash
2
3# Copyright 2019 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
17source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags"
18
19FLAGS_HELP="USAGE: $0 <KERNEL_DIR> [IMAGE]"
20
21FLAGS "$@" || exit $?
22eval set -- "${FLAGS_ARGV}"
23
24for arg in "$@" ; do
25 if [ -z $KERNEL_DIR ]; then
26 KERNEL_DIR=$arg
27 elif [ -z $IMAGE ]; then
28 IMAGE=$arg
29 else
30 flags_help
31 exit 1
32 fi
33done
34
Tristan Muntsingere4eeded2019-10-15 20:26:15 -070035USE_IMAGE=`[ -z "${IMAGE}" ] && echo "0" || echo "1"`
Tristan Muntsinger82c10502019-10-15 14:49:19 -070036if [ -z $KERNEL_DIR ] || [ -z $IMAGE ]; then
37 flags_help
38 exit 1
39fi
40if [ -e "${IMAGE}" ]; then
41 echo "error: ${IMAGE} already exists"
42 exit 1
43fi
44if [ ! -e "${KERNEL_DIR}" ]; then
45 echo "error: can't find '${KERNEL_DIR}'. aborting..."
46 exit 1
47fi
48
49# escalate to superuser
50if [ $UID -ne 0 ]; then
51 cd ${ANDROID_BUILD_TOP}
52 . ./build/envsetup.sh
53 lunch ${TARGET_PRODUCT}-${TARGET_BUILD_VARIANT}
54 mmma external/u-boot
55 cd -
56 exec sudo -E "${0}" ${@}
57fi
58
Tristan Muntsingere4eeded2019-10-15 20:26:15 -070059if [ $USE_IMAGE -eq 0 ]; then
60 init_devs=`lsblk --nodeps -oNAME -n`
61 echo "Reinsert device (to write to) into PC"
62 while true; do
63 devs=`lsblk --nodeps -oNAME -n`
64 new_devs="$(echo -e "${init_devs}\n${devs}" | sort | uniq -u | awk 'NF')"
65 num_devs=`echo "${new_devs}" | wc -l`
66 if [[ "${new_devs}" == "" ]]; then
67 num_devs=0
68 fi
69 if [[ ${num_devs} -gt 1 ]]; then
70 echo "error: too many new devices detected! aborting..."
71 exit 1
72 fi
73 if [[ ${num_devs} -eq 1 ]]; then
74 if [[ "${new_devs}" != "${mmc_dev}" ]]; then
75 if [[ "${mmc_dev}" != "" ]]; then
76 echo "error: block device name mismatch ${new_devs} != ${mmc_dev}"
77 echo "Reinsert device (to write to) into PC"
78 fi
79 mmc_dev=${new_devs}
80 continue
81 fi
82 echo "${init_devs}" | grep "${mmc_dev}" >/dev/null
83 if [[ $? -eq 0 ]]; then
84 init_devs="${devs}"
85 continue
86 fi
87 break
88 fi
89 done
90 # now inform the user
91 echo "Detected device at /dev/${mmc_dev}"
92fi
93
Tristan Muntsinger82c10502019-10-15 14:49:19 -070094cd ${ANDROID_BUILD_TOP}/external/arm-trusted-firmware
95CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rk3399 DEBUG=0 ERROR_DEPRECATED=1 bl31
96export BL31="${ANDROID_BUILD_TOP}/external/arm-trusted-firmware/build/rk3399/release/bl31/bl31.elf"
97cd -
98
99idbloader=`mktemp`
100cd ${ANDROID_BUILD_TOP}/external/u-boot
101make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig
102make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
103${ANDROID_HOST_OUT}/bin/mkimage -n rk3399 -T rksd -d tpl/u-boot-tpl.bin ${idbloader}
104cat spl/u-boot-spl.bin >> ${idbloader}
105cd -
106
107${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE}
108if [ $? -ne 0 ]; then
109 echo "error: failed to build rootfs. exiting..."
110 exit 1
111fi
112truncate -s +3G ${IMAGE}
113e2fsck -f ${IMAGE}
114resize2fs ${IMAGE}
115
116mntdir=`mktemp -d`
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700117mount ${IMAGE} ${mntdir}
118if [ $? != 0 ]; then
119 echo "error: unable to mount ${IMAGE} ${mntdir}"
120 exit 1
121fi
122
123cat > ${mntdir}/boot/boot.cmd << "EOF"
124load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image
125load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd
126load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb
127setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid"
128run finduuid
129setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000"
130booti 0x02080000 0x04000000 0x01f00000
131EOF
132${ANDROID_HOST_OUT}/bin/mkimage \
133 -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr
134
135cd ${KERNEL_DIR}
136export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH
137export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH
138make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
139 CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig
140make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
141 CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc`
142
143cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/
144mkdir -p ${mntdir}/boot/dtb/rockchip/
145cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/
146cd -
147
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700148mount -o bind /proc ${mntdir}/proc
149mount -o bind /sys ${mntdir}/sys
150mount -o bind /dev ${mntdir}/dev
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700151
152echo "Installing required packages..."
153chroot ${mntdir} /bin/bash <<EOF
154apt-get update
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700155apt-get install -y -f initramfs-tools u-boot-tools network-manager openssh-server sudo man-db vim git dpkg-dev cdbs debhelper config-package-dev gdisk eject lzop binfmt-support ntpdate
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700156EOF
157
158echo "Turning on DHCP client..."
159cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF
160[Match]
161Name=en*
162
163[Network]
164DHCP=yes
165EOF
166
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700167chroot ${mntdir} /bin/bash << "EOT"
168echo "Adding user vsoc-01 and groups..."
169useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01
170echo -e "cuttlefish\ncuttlefish" | passwd
171echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01
172EOT
173
174echo "Cloning android-cuttlefish..."
175cd ${mntdir}/home/vsoc-01
176git clone https://github.com/google/android-cuttlefish.git
177cd -
178
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700179echo "Creating cleanup script..."
180cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF"
181#!/bin/bash
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700182echo "Installing cuttlefish-common package..."
183echo "nameserver 8.8.8.8" > /etc/resolv.conf
184MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/'`
185sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts
186sudo hostnamectl set-hostname "rockpi-${MAC}"
187
188dpkg --add-architecture amd64
189until ping -c1 ftp.debian.org; do sleep 1; done
190ntpdate time.google.com
191while true; do
192 apt-get -o Acquire::Check-Valid-Until=false update
193 if [ $? != 0 ]; then sleep 1; continue; fi
194 apt-get install -y -f libc6:amd64 qemu-user-static
195 if [ $? != 0 ]; then sleep 1; continue; fi
196 break
197done
198cd /home/vsoc-01/android-cuttlefish
199dpkg-buildpackage -d -uc -us
200apt-get install -y -f ../cuttlefish-common_*_arm64.deb
201apt-get clean
202usermod -aG cvdnetwork vsoc-01
203chown -R vsoc-01:vsoc-01 /home/vsoc-01
204chmod 660 /dev/vhost-vsock
205chown root:cvdnetwork /dev/vhost-vsock
206
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700207rm /etc/machine-id
208rm /var/lib/dbus/machine-id
209dbus-uuidgen --ensure
210systemd-machine-id-setup
211
212systemctl disable cleanup
213rm /usr/local/bin/install-cleanup
214EOF
215chmod +x ${mntdir}/usr/local/bin/install-cleanup
216
217echo "Creating cleanup service..."
218cat > ${mntdir}/etc/systemd/system/cleanup.service << EOF
219[Unit]
220 Description=cleanup service
221 ConditionPathExists=/usr/local/bin/install-cleanup
222
223[Service]
224 Type=simple
225 ExecStart=/usr/local/bin/install-cleanup
226 TimeoutSec=0
227 StandardOutput=tty
228
229[Install]
230 WantedBy=multi-user.target
231EOF
232
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700233chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700234echo "Enabling services..."
235systemctl enable cleanup
236
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700237echo "Creating Initial Ramdisk..."
238update-initramfs -c -t -k "5.2.0"
239mkimage -A arm -O linux -T ramdisk -C none -a 0 -e 0 -n uInitrd -d /boot/initrd.img-5.2.0 /boot/uInitrd-5.2.0
240ln -s /boot/uInitrd-5.2.0 /boot/uInitrd
241EOT
242
243umount ${mntdir}/sys
244umount ${mntdir}/dev
245umount ${mntdir}/proc
246umount ${mntdir}
247
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700248if [ ${USE_IMAGE} -eq 0 ]; then
249 # 32GB eMMC size
250 last_sector=61071326
251
252 device=/dev/${mmc_dev}
253 devicep=${device}
254
255 sgdisk -Z -a1 ${device}
256 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${device}
257 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${device}
258 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${device}
259 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${device}
260 sgdisk -a1 -n:5:32768:${last_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${device}
261fi
262
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700263# Turn on journaling
264tune2fs -O ^has_journal ${IMAGE}
265e2fsck -fy ${IMAGE} >/dev/null 2>&1
266
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700267if [ ${USE_IMAGE} -eq 0 ]; then
268 dd if=${IMAGE} of=${devicep}5 bs=1M
269 resize2fs ${devicep}5 >/dev/null 2>&1
270else
271 # Minimize rootfs filesystem
272 while true; do
273 out=`sudo resize2fs -M ${IMAGE} 2>&1`
274 if [[ $out =~ "Nothing to do" ]]; then
275 break
276 fi
277 done
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700278
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700279 # Minimize rootfs file size
280 block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'`
281 block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'`
282 sector_size=512
283 start_sector=32768
284 fs_size=$(( block_count*block_size ))
285 fs_sectors=$(( fs_size/sector_size ))
286 part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned
287 end_sector=$(( start_sector+part_sectors-1 ))
288 secondary_gpt_sectors=33
289 fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size ))
290 image_size=$(( part_sectors*sector_size ))
291 truncate -s ${image_size} ${IMAGE}
292 e2fsck -fy ${IMAGE} >/dev/null 2>&1
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700293
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700294 # Create final image
295 tmpimg=`mktemp`
296 truncate -s ${fs_end} ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700297
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700298 # Create GPT
299 sgdisk -Z -a1 ${tmpimg}
300 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg}
301 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg}
302 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg}
303 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg}
304 sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700305
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700306 device=$(losetup -f)
307 devicep=${device}p
308 losetup ${device} ${tmpimg}
309 partx -v --add ${device}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700310
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700311 # copy over data
312 dd if=${IMAGE} of=${devicep}5 bs=1M
313fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700314
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700315dd if=${idbloader} of=${devicep}1
316dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3
317
318if [ ${USE_IMAGE} -eq 1 ]; then
319 chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
320 mv ${tmpimg} ${IMAGE}
321 partx -v --delete ${device}
322 losetup -d ${device}
323fi