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