blob: 79c841d9751b71ae5dede680d1ad9ceceb7619c9 [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
35if [ -z $KERNEL_DIR ] || [ -z $IMAGE ]; then
36 flags_help
37 exit 1
38fi
39if [ -e "${IMAGE}" ]; then
40 echo "error: ${IMAGE} already exists"
41 exit 1
42fi
43if [ ! -e "${KERNEL_DIR}" ]; then
44 echo "error: can't find '${KERNEL_DIR}'. aborting..."
45 exit 1
46fi
47
48# escalate to superuser
49if [ $UID -ne 0 ]; then
50 cd ${ANDROID_BUILD_TOP}
51 . ./build/envsetup.sh
52 lunch ${TARGET_PRODUCT}-${TARGET_BUILD_VARIANT}
53 mmma external/u-boot
54 cd -
55 exec sudo -E "${0}" ${@}
56fi
57
58cd ${ANDROID_BUILD_TOP}/external/arm-trusted-firmware
59CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rk3399 DEBUG=0 ERROR_DEPRECATED=1 bl31
60export BL31="${ANDROID_BUILD_TOP}/external/arm-trusted-firmware/build/rk3399/release/bl31/bl31.elf"
61cd -
62
63idbloader=`mktemp`
64cd ${ANDROID_BUILD_TOP}/external/u-boot
65make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig
66make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
67${ANDROID_HOST_OUT}/bin/mkimage -n rk3399 -T rksd -d tpl/u-boot-tpl.bin ${idbloader}
68cat spl/u-boot-spl.bin >> ${idbloader}
69cd -
70
71${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE}
72if [ $? -ne 0 ]; then
73 echo "error: failed to build rootfs. exiting..."
74 exit 1
75fi
76truncate -s +3G ${IMAGE}
77e2fsck -f ${IMAGE}
78resize2fs ${IMAGE}
79
80mntdir=`mktemp -d`
81if [ $? -ne 0 ]; then
82 echo "error: failed to mkdir tmp. exiting..."
83 exit 1
84fi
85mount ${IMAGE} ${mntdir}
86if [ $? != 0 ]; then
87 echo "error: unable to mount ${IMAGE} ${mntdir}"
88 exit 1
89fi
90
91cat > ${mntdir}/boot/boot.cmd << "EOF"
92load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image
93load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd
94load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb
95setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid"
96run finduuid
97setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000"
98booti 0x02080000 0x04000000 0x01f00000
99EOF
100${ANDROID_HOST_OUT}/bin/mkimage \
101 -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr
102
103cd ${KERNEL_DIR}
104export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH
105export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH
106make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
107 CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig
108make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
109 CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc`
110
111cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/
112mkdir -p ${mntdir}/boot/dtb/rockchip/
113cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/
114cd -
115
116sudo mount -o bind /proc ${mntdir}/proc
117sudo mount -o bind /sys ${mntdir}/sys
118sudo mount -o bind /dev ${mntdir}/dev
119
120echo "Installing required packages..."
121chroot ${mntdir} /bin/bash <<EOF
122apt-get update
123apt-get install -y -f initramfs-tools u-boot-tools network-manager openssh-server sudo
124apt-get clean
125EOF
126
127echo "Turning on DHCP client..."
128cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF
129[Match]
130Name=en*
131
132[Network]
133DHCP=yes
134EOF
135
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700136echo "Creating cleanup script..."
137cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF"
138#!/bin/bash
139rm /etc/machine-id
140rm /var/lib/dbus/machine-id
141dbus-uuidgen --ensure
142systemd-machine-id-setup
143
144systemctl disable cleanup
145rm /usr/local/bin/install-cleanup
146EOF
147chmod +x ${mntdir}/usr/local/bin/install-cleanup
148
149echo "Creating cleanup service..."
150cat > ${mntdir}/etc/systemd/system/cleanup.service << EOF
151[Unit]
152 Description=cleanup service
153 ConditionPathExists=/usr/local/bin/install-cleanup
154
155[Service]
156 Type=simple
157 ExecStart=/usr/local/bin/install-cleanup
158 TimeoutSec=0
159 StandardOutput=tty
160
161[Install]
162 WantedBy=multi-user.target
163EOF
164
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700165chroot ${mntdir} /bin/bash << "EOT"
166echo "Adding user vsoc-01 and groups..."
167useradd -m -G sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01
168echo -e "cuttlefish\ncuttlefish" | passwd
169echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01
170
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700171echo "Enabling services..."
172systemctl enable cleanup
173
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700174echo "Creating Initial Ramdisk..."
175update-initramfs -c -t -k "5.2.0"
176mkimage -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
177ln -s /boot/uInitrd-5.2.0 /boot/uInitrd
178EOT
179
180umount ${mntdir}/sys
181umount ${mntdir}/dev
182umount ${mntdir}/proc
183umount ${mntdir}
184
185# Turn on journaling
186tune2fs -O ^has_journal ${IMAGE}
187e2fsck -fy ${IMAGE} >/dev/null 2>&1
188
189# Minimize rootfs filesystem
190while true; do
191 out=`sudo resize2fs -M ${IMAGE} 2>&1`
192 if [[ $out =~ "Nothing to do" ]]; then
193 break
194 fi
195done
196
197# Minimize rootfs file size
198block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'`
199block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'`
200sector_size=512
201start_sector=32768
202fs_size=$(( block_count*block_size ))
203fs_sectors=$(( fs_size/sector_size ))
204part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned
205end_sector=$(( start_sector+part_sectors-1 ))
206secondary_gpt_sectors=33
207fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size ))
208image_size=$(( part_sectors*sector_size ))
209truncate -s ${image_size} ${IMAGE}
210e2fsck -fy ${IMAGE} >/dev/null 2>&1
211
212# Create final image
213tmpimg=`mktemp`
214truncate -s ${fs_end} ${tmpimg}
215mknod -m640 /dev/loop100 b 7 100
216chown root:disk /dev/loop100
217
218# Create GPT
219sgdisk -Z -a1 ${tmpimg}
220sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg}
221sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg}
222sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg}
223sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg}
224sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${tmpimg}
225
226device=$(losetup -f)
227losetup ${device} ${tmpimg}
228partx -v --add ${device}
229
230# copy over data
231dd if=${idbloader} of=${device}p1
232dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${device}p3
233dd if=${IMAGE} of=${device}p5 bs=1M
234
235chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
236mv ${tmpimg} ${IMAGE}
237partx -v --delete ${device}
238losetup -d ${device}