blob: 9ecaf444577c896141d5460dff8629c2fe909ff5 [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 Muntsingerab89e8f2019-10-15 20:30:20 -0700179echo "Creating led script..."
180cat > ${mntdir}/usr/local/bin/led << "EOF"
181#!/bin/bash
182
183if [ "$1" == "--start" ]; then
184 echo 125 > /sys/class/gpio/export
185 echo out > /sys/class/gpio/gpio125/direction
186 chmod 666 /sys/class/gpio/gpio125/value
187 echo 0 > /sys/class/gpio/gpio125/value
188 exit 0
189fi
190
191if [ "$1" == "--stop" ]; then
192 echo 0 > /sys/class/gpio/gpio125/value
193 echo 125 > /sys/class/gpio/unexport
194 exit 0
195fi
196
197if [ ! -e /sys/class/gpio/gpio125/value ]; then
198 echo "error: led service not initialized"
199 exit 1
200fi
201
202if [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then
203 echo 0 > /sys/class/gpio/gpio125/value
204 exit 0
205fi
206
207if [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then
208 echo 1 > /sys/class/gpio/gpio125/value
209 exit 0
210fi
211
212echo "usage: led <0|1>"
213exit 1
214EOF
215chown root:root ${mntdir}/usr/local/bin/led
216chmod 755 ${mntdir}/usr/local/bin/led
217
218echo "Creating led service..."
219cat > ${mntdir}/etc/systemd/system/led.service << EOF
220[Unit]
221 Description=led service
222 ConditionPathExists=/usr/local/bin/led
223
224[Service]
225 Type=oneshot
226 ExecStart=/usr/local/bin/led --start
227 ExecStop=/usr/local/bin/led --stop
228 RemainAfterExit=true
229 StandardOutput=journal
230
231[Install]
232 WantedBy=multi-user.target
233EOF
234
235echo "Creating SD duplicator script..."
236cat > ${mntdir}/usr/local/bin/sd-dupe << "EOF"
237#!/bin/bash
238led 0
239
240src_dev=mmcblk0
241dest_dev=mmcblk1
242part_num=p5
243
244if [ -e /dev/mmcblk0p5 ]; then
245 led 1
246
247 sgdisk -Z -a1 /dev/${dest_dev}
248 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 /dev/${dest_dev}
249 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env /dev/${dest_dev}
250 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 /dev/${dest_dev}
251 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust /dev/${dest_dev}
252 sgdisk -a1 -n:5:32768:- -A:5:set:2 -t:5:8305 -c:5:rootfs /dev/${dest_dev}
253
254 src_block_count=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block count:" | sed 's/.*: *//'`
255 src_block_size=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block size:" | sed 's/.*: *//'`
256 src_fs_size=$(( src_block_count*src_block_size ))
257 src_fs_size_m=$(( src_fs_size / 1024 / 1024 + 1 ))
258
259 dd if=/dev/${src_dev}p1 of=/dev/${dest_dev}p1 conv=sync,noerror status=progress
260 dd if=/dev/${src_dev}p2 of=/dev/${dest_dev}p2 conv=sync,noerror status=progress
261 dd if=/dev/${src_dev}p3 of=/dev/${dest_dev}p3 conv=sync,noerror status=progress
262 dd if=/dev/${src_dev}p4 of=/dev/${dest_dev}p4 conv=sync,noerror status=progress
263
264 echo "Writing ${src_fs_size_m} MB: /dev/${src_dev} -> /dev/${dest_dev}..."
265 dd if=/dev/${src_dev}${part_num} of=/dev/${dest_dev}${part_num} bs=1M conv=sync,noerror status=progress
266
267 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
268 e2fsck -fy /dev/${dest_dev}${part_num}
269 resize2fs /dev/${dest_dev}${part_num}
270 tune2fs -O has_journal /dev/${dest_dev}${part_num}
271 e2fsck -fy /dev/${dest_dev}${part_num}
272 sync /dev/${dest_dev}
273
274 echo "Cleaning up..."
275 mount /dev/${dest_dev}${part_num} /media
276 chroot /media /usr/local/bin/install-cleanup
277
278 if [ $? == 0 ]; then
279 echo "Successfully copied Rock Pi image!"
280 while true; do
281 led 1; sleep 0.5
282 led 0; sleep 0.5
283 done
284 else
285 echo "Error while copying Rock Pi image"
286 while true; do
287 led 1; sleep 0.1
288 led 0; sleep 0.1
289 done
290 fi
291else
292 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
293 e2fsck -fy /dev/${dest_dev}${part_num}
294 resize2fs /dev/${dest_dev}${part_num}
295 tune2fs -O has_journal /dev/${dest_dev}${part_num}
296 e2fsck -fy /dev/${dest_dev}${part_num}
297 sync /dev/${dest_dev}
298
299 echo "Cleaning up..."
300 /usr/local/bin/install-cleanup
301fi
302EOF
303chmod +x ${mntdir}/usr/local/bin/sd-dupe
304
305echo "Creating SD duplicator service..."
306cat > ${mntdir}/etc/systemd/system/sd-dupe.service << EOF
307[Unit]
308 Description=Duplicate SD card rootfs to eMMC on Rock Pi
309 ConditionPathExists=/usr/local/bin/sd-dupe
310 After=led.service
311
312[Service]
313 Type=simple
314 ExecStart=/usr/local/bin/sd-dupe
315 TimeoutSec=0
316 StandardOutput=tty
317
318[Install]
319 WantedBy=multi-user.target
320EOF
321
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700322echo "Creating cleanup script..."
323cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF"
324#!/bin/bash
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700325echo "Installing cuttlefish-common package..."
326echo "nameserver 8.8.8.8" > /etc/resolv.conf
327MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/'`
328sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts
329sudo hostnamectl set-hostname "rockpi-${MAC}"
330
331dpkg --add-architecture amd64
332until ping -c1 ftp.debian.org; do sleep 1; done
333ntpdate time.google.com
334while true; do
335 apt-get -o Acquire::Check-Valid-Until=false update
336 if [ $? != 0 ]; then sleep 1; continue; fi
337 apt-get install -y -f libc6:amd64 qemu-user-static
338 if [ $? != 0 ]; then sleep 1; continue; fi
339 break
340done
341cd /home/vsoc-01/android-cuttlefish
342dpkg-buildpackage -d -uc -us
343apt-get install -y -f ../cuttlefish-common_*_arm64.deb
344apt-get clean
345usermod -aG cvdnetwork vsoc-01
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700346chmod 660 /dev/vhost-vsock
347chown root:cvdnetwork /dev/vhost-vsock
348
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700349rm /etc/machine-id
350rm /var/lib/dbus/machine-id
351dbus-uuidgen --ensure
352systemd-machine-id-setup
353
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700354systemctl disable sd-dupe
355rm /etc/systemd/system/sd-dupe.service
356rm /usr/local/bin/sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700357rm /usr/local/bin/install-cleanup
358EOF
359chmod +x ${mntdir}/usr/local/bin/install-cleanup
360
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700361chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700362echo "Enabling services..."
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700363systemctl enable led
364systemctl enable sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700365
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700366echo "Creating Initial Ramdisk..."
367update-initramfs -c -t -k "5.2.0"
368mkimage -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
369ln -s /boot/uInitrd-5.2.0 /boot/uInitrd
370EOT
371
372umount ${mntdir}/sys
373umount ${mntdir}/dev
374umount ${mntdir}/proc
375umount ${mntdir}
376
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700377if [ ${USE_IMAGE} -eq 0 ]; then
378 # 32GB eMMC size
379 last_sector=61071326
380
381 device=/dev/${mmc_dev}
382 devicep=${device}
383
384 sgdisk -Z -a1 ${device}
385 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${device}
386 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${device}
387 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${device}
388 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${device}
389 sgdisk -a1 -n:5:32768:${last_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${device}
390fi
391
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700392# Turn on journaling
393tune2fs -O ^has_journal ${IMAGE}
394e2fsck -fy ${IMAGE} >/dev/null 2>&1
395
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700396if [ ${USE_IMAGE} -eq 0 ]; then
397 dd if=${IMAGE} of=${devicep}5 bs=1M
398 resize2fs ${devicep}5 >/dev/null 2>&1
399else
400 # Minimize rootfs filesystem
401 while true; do
402 out=`sudo resize2fs -M ${IMAGE} 2>&1`
403 if [[ $out =~ "Nothing to do" ]]; then
404 break
405 fi
406 done
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700407
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700408 # Minimize rootfs file size
409 block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'`
410 block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'`
411 sector_size=512
412 start_sector=32768
413 fs_size=$(( block_count*block_size ))
414 fs_sectors=$(( fs_size/sector_size ))
415 part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned
416 end_sector=$(( start_sector+part_sectors-1 ))
417 secondary_gpt_sectors=33
418 fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size ))
419 image_size=$(( part_sectors*sector_size ))
420 truncate -s ${image_size} ${IMAGE}
421 e2fsck -fy ${IMAGE} >/dev/null 2>&1
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700422
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700423 # Create final image
424 tmpimg=`mktemp`
425 truncate -s ${fs_end} ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700426
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700427 # Create GPT
428 sgdisk -Z -a1 ${tmpimg}
429 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg}
430 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg}
431 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg}
432 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg}
433 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 -0700434
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700435 device=$(losetup -f)
436 devicep=${device}p
437 losetup ${device} ${tmpimg}
438 partx -v --add ${device}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700439
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700440 # copy over data
441 dd if=${IMAGE} of=${devicep}5 bs=1M
442fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700443
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700444dd if=${idbloader} of=${devicep}1
445dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3
446
447if [ ${USE_IMAGE} -eq 1 ]; then
448 chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
449 mv ${tmpimg} ${IMAGE}
450 partx -v --delete ${device}
451 losetup -d ${device}
452fi