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