Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 1 | #!/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 | |
| 17 | source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags" |
| 18 | |
| 19 | FLAGS_HELP="USAGE: $0 <KERNEL_DIR> [IMAGE]" |
| 20 | |
| 21 | FLAGS "$@" || exit $? |
| 22 | eval set -- "${FLAGS_ARGV}" |
| 23 | |
| 24 | for 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 |
| 33 | done |
| 34 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 35 | USE_IMAGE=`[ -z "${IMAGE}" ] && echo "0" || echo "1"` |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 36 | if [ -z $KERNEL_DIR ] || [ -z $IMAGE ]; then |
| 37 | flags_help |
| 38 | exit 1 |
| 39 | fi |
| 40 | if [ -e "${IMAGE}" ]; then |
| 41 | echo "error: ${IMAGE} already exists" |
| 42 | exit 1 |
| 43 | fi |
| 44 | if [ ! -e "${KERNEL_DIR}" ]; then |
| 45 | echo "error: can't find '${KERNEL_DIR}'. aborting..." |
| 46 | exit 1 |
| 47 | fi |
| 48 | |
| 49 | # escalate to superuser |
| 50 | if [ $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}" ${@} |
| 57 | fi |
| 58 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 59 | if [ $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}" |
| 92 | fi |
| 93 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 94 | cd ${ANDROID_BUILD_TOP}/external/arm-trusted-firmware |
| 95 | CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rk3399 DEBUG=0 ERROR_DEPRECATED=1 bl31 |
| 96 | export BL31="${ANDROID_BUILD_TOP}/external/arm-trusted-firmware/build/rk3399/release/bl31/bl31.elf" |
| 97 | cd - |
| 98 | |
| 99 | idbloader=`mktemp` |
| 100 | cd ${ANDROID_BUILD_TOP}/external/u-boot |
| 101 | make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig |
| 102 | make 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} |
| 104 | cat spl/u-boot-spl.bin >> ${idbloader} |
| 105 | cd - |
| 106 | |
| 107 | ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE} |
| 108 | if [ $? -ne 0 ]; then |
| 109 | echo "error: failed to build rootfs. exiting..." |
| 110 | exit 1 |
| 111 | fi |
| 112 | truncate -s +3G ${IMAGE} |
| 113 | e2fsck -f ${IMAGE} |
| 114 | resize2fs ${IMAGE} |
| 115 | |
| 116 | mntdir=`mktemp -d` |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 117 | mount ${IMAGE} ${mntdir} |
| 118 | if [ $? != 0 ]; then |
| 119 | echo "error: unable to mount ${IMAGE} ${mntdir}" |
| 120 | exit 1 |
| 121 | fi |
| 122 | |
| 123 | cat > ${mntdir}/boot/boot.cmd << "EOF" |
| 124 | load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image |
| 125 | load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd |
| 126 | load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb |
| 127 | setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid" |
| 128 | run finduuid |
| 129 | setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000" |
| 130 | booti 0x02080000 0x04000000 0x01f00000 |
| 131 | EOF |
| 132 | ${ANDROID_HOST_OUT}/bin/mkimage \ |
| 133 | -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr |
| 134 | |
| 135 | cd ${KERNEL_DIR} |
| 136 | export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH |
| 137 | export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH |
| 138 | make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \ |
| 139 | CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig |
| 140 | make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \ |
| 141 | CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc` |
| 142 | |
| 143 | cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/ |
| 144 | mkdir -p ${mntdir}/boot/dtb/rockchip/ |
| 145 | cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/ |
| 146 | cd - |
| 147 | |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 148 | mount -o bind /proc ${mntdir}/proc |
| 149 | mount -o bind /sys ${mntdir}/sys |
| 150 | mount -o bind /dev ${mntdir}/dev |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 151 | |
| 152 | echo "Installing required packages..." |
| 153 | chroot ${mntdir} /bin/bash <<EOF |
| 154 | apt-get update |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 155 | apt-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 Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 156 | EOF |
| 157 | |
| 158 | echo "Turning on DHCP client..." |
| 159 | cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF |
| 160 | [Match] |
| 161 | Name=en* |
| 162 | |
| 163 | [Network] |
| 164 | DHCP=yes |
| 165 | EOF |
| 166 | |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 167 | chroot ${mntdir} /bin/bash << "EOT" |
| 168 | echo "Adding user vsoc-01 and groups..." |
| 169 | useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01 |
| 170 | echo -e "cuttlefish\ncuttlefish" | passwd |
| 171 | echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01 |
| 172 | EOT |
| 173 | |
| 174 | echo "Cloning android-cuttlefish..." |
| 175 | cd ${mntdir}/home/vsoc-01 |
| 176 | git clone https://github.com/google/android-cuttlefish.git |
| 177 | cd - |
| 178 | |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 179 | echo "Creating cleanup script..." |
| 180 | cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF" |
| 181 | #!/bin/bash |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 182 | echo "Installing cuttlefish-common package..." |
| 183 | echo "nameserver 8.8.8.8" > /etc/resolv.conf |
| 184 | MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/'` |
| 185 | sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts |
| 186 | sudo hostnamectl set-hostname "rockpi-${MAC}" |
| 187 | |
| 188 | dpkg --add-architecture amd64 |
| 189 | until ping -c1 ftp.debian.org; do sleep 1; done |
| 190 | ntpdate time.google.com |
| 191 | while 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 |
| 197 | done |
| 198 | cd /home/vsoc-01/android-cuttlefish |
| 199 | dpkg-buildpackage -d -uc -us |
| 200 | apt-get install -y -f ../cuttlefish-common_*_arm64.deb |
| 201 | apt-get clean |
| 202 | usermod -aG cvdnetwork vsoc-01 |
| 203 | chown -R vsoc-01:vsoc-01 /home/vsoc-01 |
| 204 | chmod 660 /dev/vhost-vsock |
| 205 | chown root:cvdnetwork /dev/vhost-vsock |
| 206 | |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 207 | rm /etc/machine-id |
| 208 | rm /var/lib/dbus/machine-id |
| 209 | dbus-uuidgen --ensure |
| 210 | systemd-machine-id-setup |
| 211 | |
| 212 | systemctl disable cleanup |
| 213 | rm /usr/local/bin/install-cleanup |
| 214 | EOF |
| 215 | chmod +x ${mntdir}/usr/local/bin/install-cleanup |
| 216 | |
| 217 | echo "Creating cleanup service..." |
| 218 | cat > ${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 |
| 231 | EOF |
| 232 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 233 | chroot ${mntdir} /bin/bash << "EOT" |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 234 | echo "Enabling services..." |
| 235 | systemctl enable cleanup |
| 236 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 237 | echo "Creating Initial Ramdisk..." |
| 238 | update-initramfs -c -t -k "5.2.0" |
| 239 | mkimage -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 |
| 240 | ln -s /boot/uInitrd-5.2.0 /boot/uInitrd |
| 241 | EOT |
| 242 | |
| 243 | umount ${mntdir}/sys |
| 244 | umount ${mntdir}/dev |
| 245 | umount ${mntdir}/proc |
| 246 | umount ${mntdir} |
| 247 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 248 | if [ ${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} |
| 261 | fi |
| 262 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 263 | # Turn on journaling |
| 264 | tune2fs -O ^has_journal ${IMAGE} |
| 265 | e2fsck -fy ${IMAGE} >/dev/null 2>&1 |
| 266 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 267 | if [ ${USE_IMAGE} -eq 0 ]; then |
| 268 | dd if=${IMAGE} of=${devicep}5 bs=1M |
| 269 | resize2fs ${devicep}5 >/dev/null 2>&1 |
| 270 | else |
| 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 Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 278 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 279 | # 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 Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 293 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 294 | # Create final image |
| 295 | tmpimg=`mktemp` |
| 296 | truncate -s ${fs_end} ${tmpimg} |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 297 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 298 | # 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 Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 305 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 306 | device=$(losetup -f) |
| 307 | devicep=${device}p |
| 308 | losetup ${device} ${tmpimg} |
| 309 | partx -v --add ${device} |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 310 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 311 | # copy over data |
| 312 | dd if=${IMAGE} of=${devicep}5 bs=1M |
| 313 | fi |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 314 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame^] | 315 | dd if=${idbloader} of=${devicep}1 |
| 316 | dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3 |
| 317 | |
| 318 | if [ ${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} |
| 323 | fi |