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 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 99 | cd ${ANDROID_BUILD_TOP}/external/u-boot |
Tristan Muntsinger | 5f26395 | 2019-10-16 21:12:06 -0700 | [diff] [blame^] | 100 | |
| 101 | tmpfile=`mktemp` |
| 102 | bootenv=`mktemp` |
| 103 | cat > ${tmpfile} << "EOF" |
| 104 | bootdelay=2 |
| 105 | baudrate=1500000 |
| 106 | scriptaddr=0x00500000 |
| 107 | boot_targets=mmc1 mmc0 |
| 108 | bootcmd=run distro_bootcmd |
| 109 | distro_bootcmd=for target in ${boot_targets}; do run bootcmd_${target}; done |
| 110 | bootcmd_mmc0=devnum=0; run mmc_boot |
| 111 | bootcmd_mmc1=devnum=1; run mmc_boot |
| 112 | mmc_boot=if mmc dev ${devnum}; then ; run scan_for_boot_part; fi |
| 113 | scan_for_boot_part=part list mmc ${devnum} -bootable devplist; env exists devplist || setenv devplist 1; for part in ${devplist}; do if fstype mmc ${devnum}:${part} bootfstype; then run find_init_script; fi; done; setenv devplist |
| 114 | find_init_script=if test -e mmc ${devnum}:${part} /boot/init.scr; then echo Found U-Boot script /boot/init.scr; run init_scr; fi |
| 115 | init_scr=load mmc ${devnum}:${part} ${scriptaddr} /boot/init.scr; source ${scriptaddr} |
| 116 | EOF |
| 117 | ${ANDROID_HOST_OUT}/bin/mkenvimage -s 32768 -o ${bootenv} - < ${tmpfile} |
| 118 | |
| 119 | idbloader=`mktemp` |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 120 | make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig |
| 121 | make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- -j`nproc` |
| 122 | ${ANDROID_HOST_OUT}/bin/mkimage -n rk3399 -T rksd -d tpl/u-boot-tpl.bin ${idbloader} |
| 123 | cat spl/u-boot-spl.bin >> ${idbloader} |
| 124 | cd - |
| 125 | |
| 126 | ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE} |
| 127 | if [ $? -ne 0 ]; then |
| 128 | echo "error: failed to build rootfs. exiting..." |
| 129 | exit 1 |
| 130 | fi |
| 131 | truncate -s +3G ${IMAGE} |
| 132 | e2fsck -f ${IMAGE} |
| 133 | resize2fs ${IMAGE} |
| 134 | |
| 135 | mntdir=`mktemp -d` |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 136 | mount ${IMAGE} ${mntdir} |
| 137 | if [ $? != 0 ]; then |
| 138 | echo "error: unable to mount ${IMAGE} ${mntdir}" |
| 139 | exit 1 |
| 140 | fi |
| 141 | |
Tristan Muntsinger | 5f26395 | 2019-10-16 21:12:06 -0700 | [diff] [blame^] | 142 | cat > ${mntdir}/boot/init.cmd << "EOF" |
| 143 | mmc dev 1 0; mmc read 0x02080000 0x1fc0 0x40; |
| 144 | ethaddr=$ethaddr |
| 145 | env default -a |
| 146 | setenv ethaddr $ethaddr |
| 147 | setenv boot_targets 'mmc1 mmc0 usb0 pxe' |
| 148 | saveenv |
| 149 | mmc dev 1 0; mmc read 0x04000000 0x1fc0 0x40; |
| 150 | mmc dev 0 0; mmc write 0x04000000 0x1fc0 0x40; |
| 151 | mmc dev 1 0; mmc write 0x02080000 0x1fc0 0x40; |
| 152 | EOF |
| 153 | ${ANDROID_BUILD_TOP}/external/u-boot/tools/mkimage \ |
| 154 | -C none -A arm -T script -d ${mntdir}/boot/init.cmd ${mntdir}/boot/init.scr |
| 155 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 156 | cat > ${mntdir}/boot/boot.cmd << "EOF" |
| 157 | load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image |
| 158 | load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd |
| 159 | load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb |
| 160 | setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid" |
| 161 | run finduuid |
| 162 | setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000" |
| 163 | booti 0x02080000 0x04000000 0x01f00000 |
| 164 | EOF |
| 165 | ${ANDROID_HOST_OUT}/bin/mkimage \ |
| 166 | -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr |
| 167 | |
| 168 | cd ${KERNEL_DIR} |
| 169 | export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH |
| 170 | export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH |
| 171 | make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \ |
| 172 | CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig |
| 173 | make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \ |
| 174 | CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc` |
| 175 | |
| 176 | cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/ |
| 177 | mkdir -p ${mntdir}/boot/dtb/rockchip/ |
| 178 | cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/ |
| 179 | cd - |
| 180 | |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 181 | mount -o bind /proc ${mntdir}/proc |
| 182 | mount -o bind /sys ${mntdir}/sys |
| 183 | mount -o bind /dev ${mntdir}/dev |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 184 | |
| 185 | echo "Installing required packages..." |
| 186 | chroot ${mntdir} /bin/bash <<EOF |
| 187 | apt-get update |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 188 | 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] | 189 | EOF |
| 190 | |
| 191 | echo "Turning on DHCP client..." |
| 192 | cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF |
| 193 | [Match] |
| 194 | Name=en* |
| 195 | |
| 196 | [Network] |
| 197 | DHCP=yes |
| 198 | EOF |
| 199 | |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 200 | chroot ${mntdir} /bin/bash << "EOT" |
| 201 | echo "Adding user vsoc-01 and groups..." |
| 202 | useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01 |
| 203 | echo -e "cuttlefish\ncuttlefish" | passwd |
| 204 | echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01 |
| 205 | EOT |
| 206 | |
| 207 | echo "Cloning android-cuttlefish..." |
| 208 | cd ${mntdir}/home/vsoc-01 |
| 209 | git clone https://github.com/google/android-cuttlefish.git |
| 210 | cd - |
| 211 | |
Tristan Muntsinger | ab89e8f | 2019-10-15 20:30:20 -0700 | [diff] [blame] | 212 | echo "Creating led script..." |
| 213 | cat > ${mntdir}/usr/local/bin/led << "EOF" |
| 214 | #!/bin/bash |
| 215 | |
| 216 | if [ "$1" == "--start" ]; then |
| 217 | echo 125 > /sys/class/gpio/export |
| 218 | echo out > /sys/class/gpio/gpio125/direction |
| 219 | chmod 666 /sys/class/gpio/gpio125/value |
| 220 | echo 0 > /sys/class/gpio/gpio125/value |
| 221 | exit 0 |
| 222 | fi |
| 223 | |
| 224 | if [ "$1" == "--stop" ]; then |
| 225 | echo 0 > /sys/class/gpio/gpio125/value |
| 226 | echo 125 > /sys/class/gpio/unexport |
| 227 | exit 0 |
| 228 | fi |
| 229 | |
| 230 | if [ ! -e /sys/class/gpio/gpio125/value ]; then |
| 231 | echo "error: led service not initialized" |
| 232 | exit 1 |
| 233 | fi |
| 234 | |
| 235 | if [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then |
| 236 | echo 0 > /sys/class/gpio/gpio125/value |
| 237 | exit 0 |
| 238 | fi |
| 239 | |
| 240 | if [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then |
| 241 | echo 1 > /sys/class/gpio/gpio125/value |
| 242 | exit 0 |
| 243 | fi |
| 244 | |
| 245 | echo "usage: led <0|1>" |
| 246 | exit 1 |
| 247 | EOF |
| 248 | chown root:root ${mntdir}/usr/local/bin/led |
| 249 | chmod 755 ${mntdir}/usr/local/bin/led |
| 250 | |
| 251 | echo "Creating led service..." |
| 252 | cat > ${mntdir}/etc/systemd/system/led.service << EOF |
| 253 | [Unit] |
| 254 | Description=led service |
| 255 | ConditionPathExists=/usr/local/bin/led |
| 256 | |
| 257 | [Service] |
| 258 | Type=oneshot |
| 259 | ExecStart=/usr/local/bin/led --start |
| 260 | ExecStop=/usr/local/bin/led --stop |
| 261 | RemainAfterExit=true |
| 262 | StandardOutput=journal |
| 263 | |
| 264 | [Install] |
| 265 | WantedBy=multi-user.target |
| 266 | EOF |
| 267 | |
| 268 | echo "Creating SD duplicator script..." |
| 269 | cat > ${mntdir}/usr/local/bin/sd-dupe << "EOF" |
| 270 | #!/bin/bash |
| 271 | led 0 |
| 272 | |
| 273 | src_dev=mmcblk0 |
| 274 | dest_dev=mmcblk1 |
| 275 | part_num=p5 |
| 276 | |
| 277 | if [ -e /dev/mmcblk0p5 ]; then |
| 278 | led 1 |
| 279 | |
| 280 | sgdisk -Z -a1 /dev/${dest_dev} |
| 281 | sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 /dev/${dest_dev} |
| 282 | sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env /dev/${dest_dev} |
| 283 | sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 /dev/${dest_dev} |
| 284 | sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust /dev/${dest_dev} |
| 285 | sgdisk -a1 -n:5:32768:- -A:5:set:2 -t:5:8305 -c:5:rootfs /dev/${dest_dev} |
| 286 | |
| 287 | src_block_count=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block count:" | sed 's/.*: *//'` |
| 288 | src_block_size=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block size:" | sed 's/.*: *//'` |
| 289 | src_fs_size=$(( src_block_count*src_block_size )) |
| 290 | src_fs_size_m=$(( src_fs_size / 1024 / 1024 + 1 )) |
| 291 | |
| 292 | dd if=/dev/${src_dev}p1 of=/dev/${dest_dev}p1 conv=sync,noerror status=progress |
| 293 | dd if=/dev/${src_dev}p2 of=/dev/${dest_dev}p2 conv=sync,noerror status=progress |
| 294 | dd if=/dev/${src_dev}p3 of=/dev/${dest_dev}p3 conv=sync,noerror status=progress |
| 295 | dd if=/dev/${src_dev}p4 of=/dev/${dest_dev}p4 conv=sync,noerror status=progress |
| 296 | |
| 297 | echo "Writing ${src_fs_size_m} MB: /dev/${src_dev} -> /dev/${dest_dev}..." |
| 298 | dd if=/dev/${src_dev}${part_num} of=/dev/${dest_dev}${part_num} bs=1M conv=sync,noerror status=progress |
| 299 | |
| 300 | echo "Expanding /dev/${dest_dev}${part_num} filesystem..." |
| 301 | e2fsck -fy /dev/${dest_dev}${part_num} |
| 302 | resize2fs /dev/${dest_dev}${part_num} |
| 303 | tune2fs -O has_journal /dev/${dest_dev}${part_num} |
| 304 | e2fsck -fy /dev/${dest_dev}${part_num} |
| 305 | sync /dev/${dest_dev} |
| 306 | |
| 307 | echo "Cleaning up..." |
| 308 | mount /dev/${dest_dev}${part_num} /media |
| 309 | chroot /media /usr/local/bin/install-cleanup |
| 310 | |
| 311 | if [ $? == 0 ]; then |
| 312 | echo "Successfully copied Rock Pi image!" |
| 313 | while true; do |
| 314 | led 1; sleep 0.5 |
| 315 | led 0; sleep 0.5 |
| 316 | done |
| 317 | else |
| 318 | echo "Error while copying Rock Pi image" |
| 319 | while true; do |
| 320 | led 1; sleep 0.1 |
| 321 | led 0; sleep 0.1 |
| 322 | done |
| 323 | fi |
| 324 | else |
| 325 | echo "Expanding /dev/${dest_dev}${part_num} filesystem..." |
| 326 | e2fsck -fy /dev/${dest_dev}${part_num} |
| 327 | resize2fs /dev/${dest_dev}${part_num} |
| 328 | tune2fs -O has_journal /dev/${dest_dev}${part_num} |
| 329 | e2fsck -fy /dev/${dest_dev}${part_num} |
| 330 | sync /dev/${dest_dev} |
| 331 | |
| 332 | echo "Cleaning up..." |
| 333 | /usr/local/bin/install-cleanup |
| 334 | fi |
| 335 | EOF |
| 336 | chmod +x ${mntdir}/usr/local/bin/sd-dupe |
| 337 | |
| 338 | echo "Creating SD duplicator service..." |
| 339 | cat > ${mntdir}/etc/systemd/system/sd-dupe.service << EOF |
| 340 | [Unit] |
| 341 | Description=Duplicate SD card rootfs to eMMC on Rock Pi |
| 342 | ConditionPathExists=/usr/local/bin/sd-dupe |
| 343 | After=led.service |
| 344 | |
| 345 | [Service] |
| 346 | Type=simple |
| 347 | ExecStart=/usr/local/bin/sd-dupe |
| 348 | TimeoutSec=0 |
| 349 | StandardOutput=tty |
| 350 | |
| 351 | [Install] |
| 352 | WantedBy=multi-user.target |
| 353 | EOF |
| 354 | |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 355 | echo "Creating cleanup script..." |
| 356 | cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF" |
| 357 | #!/bin/bash |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 358 | echo "Installing cuttlefish-common package..." |
| 359 | echo "nameserver 8.8.8.8" > /etc/resolv.conf |
| 360 | MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/'` |
| 361 | sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts |
| 362 | sudo hostnamectl set-hostname "rockpi-${MAC}" |
| 363 | |
| 364 | dpkg --add-architecture amd64 |
| 365 | until ping -c1 ftp.debian.org; do sleep 1; done |
| 366 | ntpdate time.google.com |
| 367 | while true; do |
| 368 | apt-get -o Acquire::Check-Valid-Until=false update |
| 369 | if [ $? != 0 ]; then sleep 1; continue; fi |
| 370 | apt-get install -y -f libc6:amd64 qemu-user-static |
| 371 | if [ $? != 0 ]; then sleep 1; continue; fi |
| 372 | break |
| 373 | done |
| 374 | cd /home/vsoc-01/android-cuttlefish |
| 375 | dpkg-buildpackage -d -uc -us |
| 376 | apt-get install -y -f ../cuttlefish-common_*_arm64.deb |
| 377 | apt-get clean |
| 378 | usermod -aG cvdnetwork vsoc-01 |
Tristan Muntsinger | 5ea7c6a | 2019-10-15 16:53:08 -0700 | [diff] [blame] | 379 | chmod 660 /dev/vhost-vsock |
| 380 | chown root:cvdnetwork /dev/vhost-vsock |
| 381 | |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 382 | rm /etc/machine-id |
| 383 | rm /var/lib/dbus/machine-id |
| 384 | dbus-uuidgen --ensure |
| 385 | systemd-machine-id-setup |
| 386 | |
Tristan Muntsinger | ab89e8f | 2019-10-15 20:30:20 -0700 | [diff] [blame] | 387 | systemctl disable sd-dupe |
| 388 | rm /etc/systemd/system/sd-dupe.service |
| 389 | rm /usr/local/bin/sd-dupe |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 390 | rm /usr/local/bin/install-cleanup |
| 391 | EOF |
| 392 | chmod +x ${mntdir}/usr/local/bin/install-cleanup |
| 393 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 394 | chroot ${mntdir} /bin/bash << "EOT" |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 395 | echo "Enabling services..." |
Tristan Muntsinger | ab89e8f | 2019-10-15 20:30:20 -0700 | [diff] [blame] | 396 | systemctl enable led |
| 397 | systemctl enable sd-dupe |
Tristan Muntsinger | d32ee2b | 2019-10-15 15:07:00 -0700 | [diff] [blame] | 398 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 399 | echo "Creating Initial Ramdisk..." |
| 400 | update-initramfs -c -t -k "5.2.0" |
| 401 | 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 |
| 402 | ln -s /boot/uInitrd-5.2.0 /boot/uInitrd |
| 403 | EOT |
| 404 | |
| 405 | umount ${mntdir}/sys |
| 406 | umount ${mntdir}/dev |
| 407 | umount ${mntdir}/proc |
| 408 | umount ${mntdir} |
| 409 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 410 | if [ ${USE_IMAGE} -eq 0 ]; then |
| 411 | # 32GB eMMC size |
| 412 | last_sector=61071326 |
| 413 | |
| 414 | device=/dev/${mmc_dev} |
| 415 | devicep=${device} |
| 416 | |
| 417 | sgdisk -Z -a1 ${device} |
| 418 | sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${device} |
| 419 | sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${device} |
| 420 | sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${device} |
| 421 | sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${device} |
| 422 | sgdisk -a1 -n:5:32768:${last_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${device} |
| 423 | fi |
| 424 | |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 425 | # Turn on journaling |
| 426 | tune2fs -O ^has_journal ${IMAGE} |
| 427 | e2fsck -fy ${IMAGE} >/dev/null 2>&1 |
| 428 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 429 | if [ ${USE_IMAGE} -eq 0 ]; then |
| 430 | dd if=${IMAGE} of=${devicep}5 bs=1M |
| 431 | resize2fs ${devicep}5 >/dev/null 2>&1 |
| 432 | else |
| 433 | # Minimize rootfs filesystem |
| 434 | while true; do |
| 435 | out=`sudo resize2fs -M ${IMAGE} 2>&1` |
| 436 | if [[ $out =~ "Nothing to do" ]]; then |
| 437 | break |
| 438 | fi |
| 439 | done |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 440 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 441 | # Minimize rootfs file size |
| 442 | block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'` |
| 443 | block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'` |
| 444 | sector_size=512 |
| 445 | start_sector=32768 |
| 446 | fs_size=$(( block_count*block_size )) |
| 447 | fs_sectors=$(( fs_size/sector_size )) |
| 448 | part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned |
| 449 | end_sector=$(( start_sector+part_sectors-1 )) |
| 450 | secondary_gpt_sectors=33 |
| 451 | fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size )) |
| 452 | image_size=$(( part_sectors*sector_size )) |
| 453 | truncate -s ${image_size} ${IMAGE} |
| 454 | e2fsck -fy ${IMAGE} >/dev/null 2>&1 |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 455 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 456 | # Create final image |
| 457 | tmpimg=`mktemp` |
| 458 | truncate -s ${fs_end} ${tmpimg} |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 459 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 460 | # Create GPT |
| 461 | sgdisk -Z -a1 ${tmpimg} |
| 462 | sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg} |
| 463 | sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg} |
| 464 | sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg} |
| 465 | sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg} |
| 466 | 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] | 467 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 468 | device=$(losetup -f) |
| 469 | devicep=${device}p |
| 470 | losetup ${device} ${tmpimg} |
| 471 | partx -v --add ${device} |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 472 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 473 | # copy over data |
| 474 | dd if=${IMAGE} of=${devicep}5 bs=1M |
| 475 | fi |
Tristan Muntsinger | 82c1050 | 2019-10-15 14:49:19 -0700 | [diff] [blame] | 476 | |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 477 | dd if=${idbloader} of=${devicep}1 |
Tristan Muntsinger | 5f26395 | 2019-10-16 21:12:06 -0700 | [diff] [blame^] | 478 | dd if=${bootenv} of=${devicep}2 |
Tristan Muntsinger | e4eeded | 2019-10-15 20:26:15 -0700 | [diff] [blame] | 479 | dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3 |
| 480 | |
| 481 | if [ ${USE_IMAGE} -eq 1 ]; then |
| 482 | chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg} |
| 483 | mv ${tmpimg} ${IMAGE} |
| 484 | partx -v --delete ${device} |
| 485 | losetup -d ${device} |
| 486 | fi |