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 | |
| 136 | chroot ${mntdir} /bin/bash << "EOT" |
| 137 | echo "Adding user vsoc-01 and groups..." |
| 138 | useradd -m -G sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01 |
| 139 | echo -e "cuttlefish\ncuttlefish" | passwd |
| 140 | echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01 |
| 141 | |
| 142 | echo "Creating Initial Ramdisk..." |
| 143 | update-initramfs -c -t -k "5.2.0" |
| 144 | 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 |
| 145 | ln -s /boot/uInitrd-5.2.0 /boot/uInitrd |
| 146 | EOT |
| 147 | |
| 148 | umount ${mntdir}/sys |
| 149 | umount ${mntdir}/dev |
| 150 | umount ${mntdir}/proc |
| 151 | umount ${mntdir} |
| 152 | |
| 153 | # Turn on journaling |
| 154 | tune2fs -O ^has_journal ${IMAGE} |
| 155 | e2fsck -fy ${IMAGE} >/dev/null 2>&1 |
| 156 | |
| 157 | # Minimize rootfs filesystem |
| 158 | while true; do |
| 159 | out=`sudo resize2fs -M ${IMAGE} 2>&1` |
| 160 | if [[ $out =~ "Nothing to do" ]]; then |
| 161 | break |
| 162 | fi |
| 163 | done |
| 164 | |
| 165 | # Minimize rootfs file size |
| 166 | block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'` |
| 167 | block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'` |
| 168 | sector_size=512 |
| 169 | start_sector=32768 |
| 170 | fs_size=$(( block_count*block_size )) |
| 171 | fs_sectors=$(( fs_size/sector_size )) |
| 172 | part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned |
| 173 | end_sector=$(( start_sector+part_sectors-1 )) |
| 174 | secondary_gpt_sectors=33 |
| 175 | fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size )) |
| 176 | image_size=$(( part_sectors*sector_size )) |
| 177 | truncate -s ${image_size} ${IMAGE} |
| 178 | e2fsck -fy ${IMAGE} >/dev/null 2>&1 |
| 179 | |
| 180 | # Create final image |
| 181 | tmpimg=`mktemp` |
| 182 | truncate -s ${fs_end} ${tmpimg} |
| 183 | mknod -m640 /dev/loop100 b 7 100 |
| 184 | chown root:disk /dev/loop100 |
| 185 | |
| 186 | # Create GPT |
| 187 | sgdisk -Z -a1 ${tmpimg} |
| 188 | sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg} |
| 189 | sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg} |
| 190 | sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg} |
| 191 | sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg} |
| 192 | sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${tmpimg} |
| 193 | |
| 194 | device=$(losetup -f) |
| 195 | losetup ${device} ${tmpimg} |
| 196 | partx -v --add ${device} |
| 197 | |
| 198 | # copy over data |
| 199 | dd if=${idbloader} of=${device}p1 |
| 200 | dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${device}p3 |
| 201 | dd if=${IMAGE} of=${device}p5 bs=1M |
| 202 | |
| 203 | chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg} |
| 204 | mv ${tmpimg} ${IMAGE} |
| 205 | partx -v --delete ${device} |
| 206 | losetup -d ${device} |