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