1*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash 2*2f2c4c7aSAndroid Build Coastguard Worker# 3*2f2c4c7aSAndroid Build Coastguard Worker# Copyright (C) 2021 The Android Open Source Project 4*2f2c4c7aSAndroid Build Coastguard Worker# 5*2f2c4c7aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*2f2c4c7aSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*2f2c4c7aSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*2f2c4c7aSAndroid Build Coastguard Worker# 9*2f2c4c7aSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*2f2c4c7aSAndroid Build Coastguard Worker# 11*2f2c4c7aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*2f2c4c7aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*2f2c4c7aSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*2f2c4c7aSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*2f2c4c7aSAndroid Build Coastguard Worker# limitations under the License. 16*2f2c4c7aSAndroid Build Coastguard Worker# 17*2f2c4c7aSAndroid Build Coastguard Worker 18*2f2c4c7aSAndroid Build Coastguard Workerset -e 19*2f2c4c7aSAndroid Build Coastguard Workerset -u 20*2f2c4c7aSAndroid Build Coastguard Worker 21*2f2c4c7aSAndroid Build Coastguard WorkerSCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P) 22*2f2c4c7aSAndroid Build Coastguard Worker 23*2f2c4c7aSAndroid Build Coastguard Worker. $SCRIPT_DIR/bullseye-common.sh 24*2f2c4c7aSAndroid Build Coastguard Worker 25*2f2c4c7aSAndroid Build Coastguard Workersed -i "s,debian,rockpi," /etc/hosts 26*2f2c4c7aSAndroid Build Coastguard Workersed -i "s,debian,rockpi," /etc/hostname 27*2f2c4c7aSAndroid Build Coastguard Worker 28*2f2c4c7aSAndroid Build Coastguard Worker# Build U-Boot FIT based on the Debian initrd 29*2f2c4c7aSAndroid Build Coastguard Workerif [[ "${embed_kernel_initrd_dtb}" = "1" ]]; then 30*2f2c4c7aSAndroid Build Coastguard Worker mkimage -f auto -A arm64 -O linux -T kernel -C none -a 0x02080000 \ 31*2f2c4c7aSAndroid Build Coastguard Worker -d /boot/vmlinuz-$(uname -r) -i /boot/initrd.img-$(uname -r) \ 32*2f2c4c7aSAndroid Build Coastguard Worker -b /boot/dtb/rockchip/rk3399-rock-pi-4b.dtb /boot/boot.fit 33*2f2c4c7aSAndroid Build Coastguard Workerfi 34*2f2c4c7aSAndroid Build Coastguard Worker 35*2f2c4c7aSAndroid Build Coastguard Worker# Write U-Boot script to /boot 36*2f2c4c7aSAndroid Build Coastguard Workercat >/boot/boot.cmd <<"EOF" 37*2f2c4c7aSAndroid Build Coastguard Workersetenv start_poe 'gpio set 150; gpio clear 146' 38*2f2c4c7aSAndroid Build Coastguard Workerrun start_poe 39*2f2c4c7aSAndroid Build Coastguard Workersetenv bootcmd_dhcp ' 40*2f2c4c7aSAndroid Build Coastguard Workermw.b ${scriptaddr} 0 0x8000 41*2f2c4c7aSAndroid Build Coastguard Workermmc dev 0 0 42*2f2c4c7aSAndroid Build Coastguard Workermmc read ${scriptaddr} 0x1fc0 0x40 43*2f2c4c7aSAndroid Build Coastguard Workerenv import -b ${scriptaddr} 0x8000 44*2f2c4c7aSAndroid Build Coastguard Workermw.b ${scriptaddr} 0 0x8000 45*2f2c4c7aSAndroid Build Coastguard Workerif dhcp ${scriptaddr} manifest.txt; then 46*2f2c4c7aSAndroid Build Coastguard Worker setenv OldSha ${Sha} 47*2f2c4c7aSAndroid Build Coastguard Worker setenv Sha 48*2f2c4c7aSAndroid Build Coastguard Worker env import -t ${scriptaddr} 0x8000 ManifestVersion 49*2f2c4c7aSAndroid Build Coastguard Worker echo "Manifest version $ManifestVersion"; 50*2f2c4c7aSAndroid Build Coastguard Worker if test "$ManifestVersion" = "1"; then 51*2f2c4c7aSAndroid Build Coastguard Worker run manifest1 52*2f2c4c7aSAndroid Build Coastguard Worker elif test "$ManifestVersion" = "2"; then 53*2f2c4c7aSAndroid Build Coastguard Worker run manifest2 54*2f2c4c7aSAndroid Build Coastguard Worker elif test "$ManifestVersion" = "3"; then 55*2f2c4c7aSAndroid Build Coastguard Worker run manifest3 56*2f2c4c7aSAndroid Build Coastguard Worker else 57*2f2c4c7aSAndroid Build Coastguard Worker run manifestX 58*2f2c4c7aSAndroid Build Coastguard Worker fi 59*2f2c4c7aSAndroid Build Coastguard Workerfi' 60*2f2c4c7aSAndroid Build Coastguard Workersetenv manifestX 'echo "***** ERROR: Unknown manifest version! *****";' 61*2f2c4c7aSAndroid Build Coastguard Workersetenv manifest1 ' 62*2f2c4c7aSAndroid Build Coastguard Workerenv import -t ${scriptaddr} 0x8000 63*2f2c4c7aSAndroid Build Coastguard Workerif test "$Sha" != "$OldSha"; then 64*2f2c4c7aSAndroid Build Coastguard Worker setenv serverip ${TftpServer} 65*2f2c4c7aSAndroid Build Coastguard Worker setenv loadaddr 0x00200000 66*2f2c4c7aSAndroid Build Coastguard Worker mmc dev 0 0; 67*2f2c4c7aSAndroid Build Coastguard Worker setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg 68*2f2c4c7aSAndroid Build Coastguard Worker setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb 69*2f2c4c7aSAndroid Build Coastguard Worker setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg 70*2f2c4c7aSAndroid Build Coastguard Worker setenv file $RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg 71*2f2c4c7aSAndroid Build Coastguard Worker setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv 72*2f2c4c7aSAndroid Build Coastguard Worker mw.b ${scriptaddr} 0 0x8000 73*2f2c4c7aSAndroid Build Coastguard Worker env export -b ${scriptaddr} 0x8000 74*2f2c4c7aSAndroid Build Coastguard Worker mmc write ${scriptaddr} 0x1fc0 0x40 75*2f2c4c7aSAndroid Build Coastguard Workerelse 76*2f2c4c7aSAndroid Build Coastguard Worker echo "Already have ${Sha}. Booting..." 77*2f2c4c7aSAndroid Build Coastguard Workerfi' 78*2f2c4c7aSAndroid Build Coastguard Workersetenv manifest2 ' 79*2f2c4c7aSAndroid Build Coastguard Workerenv import -t ${scriptaddr} 0x8000 80*2f2c4c7aSAndroid Build Coastguard Workerif test "$DFUethaddr" = "$ethaddr" || test "$DFUethaddr" = ""; then 81*2f2c4c7aSAndroid Build Coastguard Worker if test "$Sha" != "$OldSha"; then 82*2f2c4c7aSAndroid Build Coastguard Worker setenv serverip ${TftpServer} 83*2f2c4c7aSAndroid Build Coastguard Worker setenv loadaddr 0x00200000 84*2f2c4c7aSAndroid Build Coastguard Worker mmc dev 0 0; 85*2f2c4c7aSAndroid Build Coastguard Worker setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg 86*2f2c4c7aSAndroid Build Coastguard Worker setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb 87*2f2c4c7aSAndroid Build Coastguard Worker setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg 88*2f2c4c7aSAndroid Build Coastguard Worker setenv file $RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg 89*2f2c4c7aSAndroid Build Coastguard Worker setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv 90*2f2c4c7aSAndroid Build Coastguard Worker mw.b ${scriptaddr} 0 0x8000 91*2f2c4c7aSAndroid Build Coastguard Worker env export -b ${scriptaddr} 0x8000 92*2f2c4c7aSAndroid Build Coastguard Worker mmc write ${scriptaddr} 0x1fc0 0x40 93*2f2c4c7aSAndroid Build Coastguard Worker else 94*2f2c4c7aSAndroid Build Coastguard Worker echo "Already have ${Sha}. Booting..." 95*2f2c4c7aSAndroid Build Coastguard Worker fi 96*2f2c4c7aSAndroid Build Coastguard Workerelse 97*2f2c4c7aSAndroid Build Coastguard Worker echo "Update ${Sha} is not for me. Booting..." 98*2f2c4c7aSAndroid Build Coastguard Workerfi' 99*2f2c4c7aSAndroid Build Coastguard Workersetenv manifest3 ' 100*2f2c4c7aSAndroid Build Coastguard Workerenv import -t ${scriptaddr} 0x8000 101*2f2c4c7aSAndroid Build Coastguard Workerif test "$DFUethaddr" = "$ethaddr" || test "$DFUethaddr" = ""; then 102*2f2c4c7aSAndroid Build Coastguard Worker if test "$Sha" != "$OldSha"; then 103*2f2c4c7aSAndroid Build Coastguard Worker setenv serverip ${TftpServer} 104*2f2c4c7aSAndroid Build Coastguard Worker setenv loadaddr 0x00200000 105*2f2c4c7aSAndroid Build Coastguard Worker mmc dev 0 0; 106*2f2c4c7aSAndroid Build Coastguard Worker setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg 107*2f2c4c7aSAndroid Build Coastguard Worker setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv 108*2f2c4c7aSAndroid Build Coastguard Worker setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb 109*2f2c4c7aSAndroid Build Coastguard Worker setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg 110*2f2c4c7aSAndroid Build Coastguard Worker setenv file $EspImg; offset=0x8000; size=0x40000; run tftpget1; setenv EspImg 111*2f2c4c7aSAndroid Build Coastguard Worker setenv file $RootfsImg; offset=0x48000; size=0; run tftpget1; setenv RootfsImg 112*2f2c4c7aSAndroid Build Coastguard Worker mw.b ${scriptaddr} 0 0x8000 113*2f2c4c7aSAndroid Build Coastguard Worker env export -b ${scriptaddr} 0x8000 114*2f2c4c7aSAndroid Build Coastguard Worker mmc write ${scriptaddr} 0x1fc0 0x40 115*2f2c4c7aSAndroid Build Coastguard Worker else 116*2f2c4c7aSAndroid Build Coastguard Worker echo "Already have ${Sha}. Booting..." 117*2f2c4c7aSAndroid Build Coastguard Worker fi 118*2f2c4c7aSAndroid Build Coastguard Workerelse 119*2f2c4c7aSAndroid Build Coastguard Worker echo "Update ${Sha} is not for me. Booting..." 120*2f2c4c7aSAndroid Build Coastguard Workerfi' 121*2f2c4c7aSAndroid Build Coastguard Workersetenv tftpget1 ' 122*2f2c4c7aSAndroid Build Coastguard Workerif test "$file" != ""; then 123*2f2c4c7aSAndroid Build Coastguard Worker mw.b ${loadaddr} 0 0x400000 124*2f2c4c7aSAndroid Build Coastguard Worker tftp ${file} 125*2f2c4c7aSAndroid Build Coastguard Worker if test $? = 0; then 126*2f2c4c7aSAndroid Build Coastguard Worker setenv isGz 0 && setexpr isGz sub .*\\.gz\$ 1 ${file} 127*2f2c4c7aSAndroid Build Coastguard Worker if test $isGz = 1; then 128*2f2c4c7aSAndroid Build Coastguard Worker if test ${file} = ${UbootEnv}; then 129*2f2c4c7aSAndroid Build Coastguard Worker echo "** gzipped env unsupported **" 130*2f2c4c7aSAndroid Build Coastguard Worker else 131*2f2c4c7aSAndroid Build Coastguard Worker setexpr boffset ${offset} * 0x200 132*2f2c4c7aSAndroid Build Coastguard Worker gzwrite mmc 0 ${loadaddr} 0x${filesize} 100000 ${boffset} && echo Updated: ${file} 133*2f2c4c7aSAndroid Build Coastguard Worker fi 134*2f2c4c7aSAndroid Build Coastguard Worker elif test ${file} = ${UbootEnv}; then 135*2f2c4c7aSAndroid Build Coastguard Worker env import -b ${loadaddr} && echo Updated: ${file} 136*2f2c4c7aSAndroid Build Coastguard Worker else 137*2f2c4c7aSAndroid Build Coastguard Worker if test $size = 0; then 138*2f2c4c7aSAndroid Build Coastguard Worker setexpr x $filesize - 1 139*2f2c4c7aSAndroid Build Coastguard Worker setexpr x $x / 0x1000 140*2f2c4c7aSAndroid Build Coastguard Worker setexpr x $x + 1 141*2f2c4c7aSAndroid Build Coastguard Worker setexpr x $x * 0x1000 142*2f2c4c7aSAndroid Build Coastguard Worker setexpr x $x / 0x200 143*2f2c4c7aSAndroid Build Coastguard Worker size=0x${x} 144*2f2c4c7aSAndroid Build Coastguard Worker fi 145*2f2c4c7aSAndroid Build Coastguard Worker mmc write ${loadaddr} ${offset} ${size} && echo Updated: ${file} 146*2f2c4c7aSAndroid Build Coastguard Worker fi 147*2f2c4c7aSAndroid Build Coastguard Worker fi 148*2f2c4c7aSAndroid Build Coastguard Worker if test $? != 0; then 149*2f2c4c7aSAndroid Build Coastguard Worker echo ** UPDATE FAILED: ${file} ** 150*2f2c4c7aSAndroid Build Coastguard Worker fi 151*2f2c4c7aSAndroid Build Coastguard Workerfi' 152*2f2c4c7aSAndroid Build Coastguard Workerif mmc dev 1 0; then; else 153*2f2c4c7aSAndroid Build Coastguard Worker run bootcmd_dhcp; 154*2f2c4c7aSAndroid Build Coastguard Workerfi 155*2f2c4c7aSAndroid Build Coastguard Workerif bcb load 0 misc; then 156*2f2c4c7aSAndroid Build Coastguard Worker # valid BCB found 157*2f2c4c7aSAndroid Build Coastguard Worker if bcb test command = bootonce-bootloader; then 158*2f2c4c7aSAndroid Build Coastguard Worker bcb clear command; bcb store 159*2f2c4c7aSAndroid Build Coastguard Worker setenv autoload no; dhcp 160*2f2c4c7aSAndroid Build Coastguard Worker fastboot udp 161*2f2c4c7aSAndroid Build Coastguard Worker reset 162*2f2c4c7aSAndroid Build Coastguard Worker elif bcb test command = boot-recovery; then 163*2f2c4c7aSAndroid Build Coastguard Worker bcb clear command; bcb store 164*2f2c4c7aSAndroid Build Coastguard Worker # we don't have recovery, reboot. 165*2f2c4c7aSAndroid Build Coastguard Worker reset 166*2f2c4c7aSAndroid Build Coastguard Worker fi 167*2f2c4c7aSAndroid Build Coastguard Workerfi 168*2f2c4c7aSAndroid Build Coastguard Workerif test -e mmc ${devnum}:${distro_bootpart} /boot/rootfs.gz; then 169*2f2c4c7aSAndroid Build Coastguard Worker setenv loadaddr 0x00200000 170*2f2c4c7aSAndroid Build Coastguard Worker mw.b ${loadaddr} 0 0x400000 171*2f2c4c7aSAndroid Build Coastguard Worker load mmc ${devnum}:${distro_bootpart} ${loadaddr} /boot/rootfs.gz 172*2f2c4c7aSAndroid Build Coastguard Worker gzwrite mmc ${devnum} ${loadaddr} 0x${filesize} 100000 0x9100000 173*2f2c4c7aSAndroid Build Coastguard Workerfi 174*2f2c4c7aSAndroid Build Coastguard Workerload mmc ${devnum}:${distro_bootpart} 0x06080000 /boot/boot.fit 175*2f2c4c7aSAndroid Build Coastguard Workersetenv bootargs "net.ifnames=0 8250.nr_uarts=4 earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 kvm-arm.mode=nvhe sdhci.debug_quirks=0x20000000 root=LABEL=ROOT" 176*2f2c4c7aSAndroid Build Coastguard Workerbootm 0x06080000 177*2f2c4c7aSAndroid Build Coastguard WorkerEOF 178*2f2c4c7aSAndroid Build Coastguard Workermkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr 179*2f2c4c7aSAndroid Build Coastguard Worker 180*2f2c4c7aSAndroid Build Coastguard Worker# Write control script for PoE hat 181*2f2c4c7aSAndroid Build Coastguard Workercat >/usr/local/bin/poe <<"EOF" 182*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash 183*2f2c4c7aSAndroid Build Coastguard Worker 184*2f2c4c7aSAndroid Build Coastguard Workerif [ "$1" == "--start" ]; then 185*2f2c4c7aSAndroid Build Coastguard Worker echo 146 > /sys/class/gpio/export 186*2f2c4c7aSAndroid Build Coastguard Worker echo out > /sys/class/gpio/gpio146/direction 187*2f2c4c7aSAndroid Build Coastguard Worker echo 0 > /sys/class/gpio/gpio146/value 188*2f2c4c7aSAndroid Build Coastguard Worker echo 150 > /sys/class/gpio/export 189*2f2c4c7aSAndroid Build Coastguard Worker echo out > /sys/class/gpio/gpio150/direction 190*2f2c4c7aSAndroid Build Coastguard Worker echo 1 > /sys/class/gpio/gpio150/value 191*2f2c4c7aSAndroid Build Coastguard Worker exit 0 192*2f2c4c7aSAndroid Build Coastguard Workerfi 193*2f2c4c7aSAndroid Build Coastguard Worker 194*2f2c4c7aSAndroid Build Coastguard Workerif [ "$1" == "--stop" ]; then 195*2f2c4c7aSAndroid Build Coastguard Worker echo 0 > /sys/class/gpio/gpio146/value 196*2f2c4c7aSAndroid Build Coastguard Worker echo 146 > /sys/class/gpio/unexport 197*2f2c4c7aSAndroid Build Coastguard Worker echo 0 > /sys/class/gpio/gpio150/value 198*2f2c4c7aSAndroid Build Coastguard Worker echo 150 > /sys/class/gpio/unexport 199*2f2c4c7aSAndroid Build Coastguard Worker exit 0 200*2f2c4c7aSAndroid Build Coastguard Workerfi 201*2f2c4c7aSAndroid Build Coastguard Worker 202*2f2c4c7aSAndroid Build Coastguard Workerif [ ! -e /sys/class/gpio/gpio146/value ] || [ ! -e /sys/class/gpio/gpio150/value ]; then 203*2f2c4c7aSAndroid Build Coastguard Worker echo "error: PoE service not initialized" 204*2f2c4c7aSAndroid Build Coastguard Worker exit 1 205*2f2c4c7aSAndroid Build Coastguard Workerfi 206*2f2c4c7aSAndroid Build Coastguard Worker 207*2f2c4c7aSAndroid Build Coastguard Workerif [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then 208*2f2c4c7aSAndroid Build Coastguard Worker echo 0 > /sys/class/gpio/gpio150/value 209*2f2c4c7aSAndroid Build Coastguard Worker exit 0 210*2f2c4c7aSAndroid Build Coastguard Workerfi 211*2f2c4c7aSAndroid Build Coastguard Worker 212*2f2c4c7aSAndroid Build Coastguard Workerif [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then 213*2f2c4c7aSAndroid Build Coastguard Worker echo 1 > /sys/class/gpio/gpio150/value 214*2f2c4c7aSAndroid Build Coastguard Worker exit 0 215*2f2c4c7aSAndroid Build Coastguard Workerfi 216*2f2c4c7aSAndroid Build Coastguard Worker 217*2f2c4c7aSAndroid Build Coastguard Workerecho "usage: poe <0|1>" 218*2f2c4c7aSAndroid Build Coastguard Workerexit 1 219*2f2c4c7aSAndroid Build Coastguard WorkerEOF 220*2f2c4c7aSAndroid Build Coastguard Workerchmod a+x /usr/local/bin/poe 221*2f2c4c7aSAndroid Build Coastguard Worker 222*2f2c4c7aSAndroid Build Coastguard Worker# Write service to start PoE control script 223*2f2c4c7aSAndroid Build Coastguard Workercat >/etc/systemd/system/poe.service <<EOF 224*2f2c4c7aSAndroid Build Coastguard Worker[Unit] 225*2f2c4c7aSAndroid Build Coastguard WorkerDescription=PoE service 226*2f2c4c7aSAndroid Build Coastguard WorkerConditionPathExists=/usr/local/bin/poe 227*2f2c4c7aSAndroid Build Coastguard Worker 228*2f2c4c7aSAndroid Build Coastguard Worker[Service] 229*2f2c4c7aSAndroid Build Coastguard WorkerType=oneshot 230*2f2c4c7aSAndroid Build Coastguard WorkerExecStart=/usr/local/bin/poe --start 231*2f2c4c7aSAndroid Build Coastguard WorkerExecStop=/usr/local/bin/poe --stop 232*2f2c4c7aSAndroid Build Coastguard WorkerRemainAfterExit=true 233*2f2c4c7aSAndroid Build Coastguard WorkerStandardOutput=journal 234*2f2c4c7aSAndroid Build Coastguard Worker 235*2f2c4c7aSAndroid Build Coastguard Worker[Install] 236*2f2c4c7aSAndroid Build Coastguard WorkerWantedBy=multi-user.target 237*2f2c4c7aSAndroid Build Coastguard WorkerEOF 238*2f2c4c7aSAndroid Build Coastguard Worker 239*2f2c4c7aSAndroid Build Coastguard Worker# Write control script for status LEDs 240*2f2c4c7aSAndroid Build Coastguard Workercat >/usr/local/bin/led <<"EOF" 241*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash 242*2f2c4c7aSAndroid Build Coastguard Worker 243*2f2c4c7aSAndroid Build Coastguard Workerif [ "$1" == "--start" ]; then 244*2f2c4c7aSAndroid Build Coastguard Worker echo 125 > /sys/class/gpio/export 245*2f2c4c7aSAndroid Build Coastguard Worker echo out > /sys/class/gpio/gpio125/direction 246*2f2c4c7aSAndroid Build Coastguard Worker chmod 666 /sys/class/gpio/gpio125/value 247*2f2c4c7aSAndroid Build Coastguard Worker echo 0 > /sys/class/gpio/gpio125/value 248*2f2c4c7aSAndroid Build Coastguard Worker exit 0 249*2f2c4c7aSAndroid Build Coastguard Workerfi 250*2f2c4c7aSAndroid Build Coastguard Worker 251*2f2c4c7aSAndroid Build Coastguard Workerif [ "$1" == "--stop" ]; then 252*2f2c4c7aSAndroid Build Coastguard Worker echo 0 > /sys/class/gpio/gpio125/value 253*2f2c4c7aSAndroid Build Coastguard Worker echo 125 > /sys/class/gpio/unexport 254*2f2c4c7aSAndroid Build Coastguard Worker exit 0 255*2f2c4c7aSAndroid Build Coastguard Workerfi 256*2f2c4c7aSAndroid Build Coastguard Worker 257*2f2c4c7aSAndroid Build Coastguard Workerif [ ! -e /sys/class/gpio/gpio125/value ]; then 258*2f2c4c7aSAndroid Build Coastguard Worker echo "error: led service not initialized" 259*2f2c4c7aSAndroid Build Coastguard Worker exit 1 260*2f2c4c7aSAndroid Build Coastguard Workerfi 261*2f2c4c7aSAndroid Build Coastguard Worker 262*2f2c4c7aSAndroid Build Coastguard Workerif [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then 263*2f2c4c7aSAndroid Build Coastguard Worker echo 0 > /sys/class/gpio/gpio125/value 264*2f2c4c7aSAndroid Build Coastguard Worker exit 0 265*2f2c4c7aSAndroid Build Coastguard Workerfi 266*2f2c4c7aSAndroid Build Coastguard Worker 267*2f2c4c7aSAndroid Build Coastguard Workerif [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then 268*2f2c4c7aSAndroid Build Coastguard Worker echo 1 > /sys/class/gpio/gpio125/value 269*2f2c4c7aSAndroid Build Coastguard Worker exit 0 270*2f2c4c7aSAndroid Build Coastguard Workerfi 271*2f2c4c7aSAndroid Build Coastguard Worker 272*2f2c4c7aSAndroid Build Coastguard Workerecho "usage: led <0|1>" 273*2f2c4c7aSAndroid Build Coastguard Workerexit 1 274*2f2c4c7aSAndroid Build Coastguard WorkerEOF 275*2f2c4c7aSAndroid Build Coastguard Workerchmod a+x /usr/local/bin/led 276*2f2c4c7aSAndroid Build Coastguard Worker 277*2f2c4c7aSAndroid Build Coastguard Worker# Write service to start LED control script 278*2f2c4c7aSAndroid Build Coastguard Workercat >/etc/systemd/system/led.service <<EOF 279*2f2c4c7aSAndroid Build Coastguard Worker[Unit] 280*2f2c4c7aSAndroid Build Coastguard WorkerDescription=led service 281*2f2c4c7aSAndroid Build Coastguard WorkerConditionPathExists=/usr/local/bin/led 282*2f2c4c7aSAndroid Build Coastguard Worker 283*2f2c4c7aSAndroid Build Coastguard Worker[Service] 284*2f2c4c7aSAndroid Build Coastguard WorkerType=oneshot 285*2f2c4c7aSAndroid Build Coastguard WorkerExecStart=/usr/local/bin/led --start 286*2f2c4c7aSAndroid Build Coastguard WorkerExecStop=/usr/local/bin/led --stop 287*2f2c4c7aSAndroid Build Coastguard WorkerRemainAfterExit=true 288*2f2c4c7aSAndroid Build Coastguard WorkerStandardOutput=journal 289*2f2c4c7aSAndroid Build Coastguard Worker 290*2f2c4c7aSAndroid Build Coastguard Worker[Install] 291*2f2c4c7aSAndroid Build Coastguard WorkerWantedBy=multi-user.target 292*2f2c4c7aSAndroid Build Coastguard WorkerEOF 293*2f2c4c7aSAndroid Build Coastguard Worker 294*2f2c4c7aSAndroid Build Coastguard Worker# Write control script for one-time SD-Card->eMMC duplication 295*2f2c4c7aSAndroid Build Coastguard Workercat >/usr/local/bin/sd-dupe <<"EOF" 296*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash 297*2f2c4c7aSAndroid Build Coastguard Workerled 0 298*2f2c4c7aSAndroid Build Coastguard Worker 299*2f2c4c7aSAndroid Build Coastguard Workersrc_dev=mmcblk0 300*2f2c4c7aSAndroid Build Coastguard Workerdest_dev=mmcblk1 301*2f2c4c7aSAndroid Build Coastguard Workerpart_num=p7 302*2f2c4c7aSAndroid Build Coastguard Worker 303*2f2c4c7aSAndroid Build Coastguard Workerif [ -e "/dev/${src_dev}" ] && [ -e "/dev/${dest_dev}" ]; then 304*2f2c4c7aSAndroid Build Coastguard Worker led 1 305*2f2c4c7aSAndroid Build Coastguard Worker 306*2f2c4c7aSAndroid Build Coastguard Worker sgdisk -Z /dev/${dest_dev} 307*2f2c4c7aSAndroid Build Coastguard Worker 308*2f2c4c7aSAndroid Build Coastguard Worker sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:idbloader /dev/${dest_dev} 309*2f2c4c7aSAndroid Build Coastguard Worker sgdisk -a1 -n:2:8128:+64 -t:2:8301 -c:2:uboot_env /dev/${dest_dev} 310*2f2c4c7aSAndroid Build Coastguard Worker sgdisk -n:3:8M:+4M -t:3:8301 -c:3:uboot /dev/${dest_dev} 311*2f2c4c7aSAndroid Build Coastguard Worker sgdisk -n:4:12M:+4M -t:4:8301 -c:4:trust /dev/${dest_dev} 312*2f2c4c7aSAndroid Build Coastguard Worker sgdisk -n:5:16M:+1M -t:5:8301 -c:5:misc /dev/${dest_dev} 313*2f2c4c7aSAndroid Build Coastguard Worker sgdisk -n:6:17M:+128M -t:6:ef00 -c:6:esp -A:6:set:0 /dev/${dest_dev} 314*2f2c4c7aSAndroid Build Coastguard Worker sgdisk -n:7:145M:0 -t:7:8305 -c:7:rootfs -A:7:set:2 /dev/${dest_dev} 315*2f2c4c7aSAndroid Build Coastguard Worker 316*2f2c4c7aSAndroid Build Coastguard Worker src_block_count=$(tune2fs -l /dev/${src_dev}${part_num} | grep "Block count:" | sed 's/.*: *//') 317*2f2c4c7aSAndroid Build Coastguard Worker src_block_size=$(tune2fs -l /dev/${src_dev}${part_num} | grep "Block size:" | sed 's/.*: *//') 318*2f2c4c7aSAndroid Build Coastguard Worker src_fs_size=$(( src_block_count*src_block_size )) 319*2f2c4c7aSAndroid Build Coastguard Worker src_fs_size_m=$(( src_fs_size / 1024 / 1024 + 1 )) 320*2f2c4c7aSAndroid Build Coastguard Worker 321*2f2c4c7aSAndroid Build Coastguard Worker dd if=/dev/${src_dev}p1 of=/dev/${dest_dev}p1 conv=sync,noerror status=progress 322*2f2c4c7aSAndroid Build Coastguard Worker dd if=/dev/${src_dev}p2 of=/dev/${dest_dev}p2 conv=sync,noerror status=progress 323*2f2c4c7aSAndroid Build Coastguard Worker dd if=/dev/${src_dev}p3 of=/dev/${dest_dev}p3 conv=sync,noerror status=progress 324*2f2c4c7aSAndroid Build Coastguard Worker dd if=/dev/${src_dev}p4 of=/dev/${dest_dev}p4 conv=sync,noerror status=progress 325*2f2c4c7aSAndroid Build Coastguard Worker dd if=/dev/${src_dev}p5 of=/dev/${dest_dev}p5 conv=sync,noerror status=progress 326*2f2c4c7aSAndroid Build Coastguard Worker 327*2f2c4c7aSAndroid Build Coastguard Worker echo "Writing ${src_fs_size_m} MB: /dev/${src_dev} -> /dev/${dest_dev}..." 328*2f2c4c7aSAndroid Build Coastguard Worker dd if=/dev/${src_dev}${part_num} of=/dev/${dest_dev}${part_num} bs=1M conv=sync,noerror status=progress 329*2f2c4c7aSAndroid Build Coastguard Worker 330*2f2c4c7aSAndroid Build Coastguard Worker echo "Expanding /dev/${dest_dev}${part_num} filesystem..." 331*2f2c4c7aSAndroid Build Coastguard Worker e2fsck -fy /dev/${dest_dev}${part_num} 332*2f2c4c7aSAndroid Build Coastguard Worker resize2fs /dev/${dest_dev}${part_num} 333*2f2c4c7aSAndroid Build Coastguard Worker tune2fs -O has_journal /dev/${dest_dev}${part_num} 334*2f2c4c7aSAndroid Build Coastguard Worker e2fsck -fy /dev/${dest_dev}${part_num} 335*2f2c4c7aSAndroid Build Coastguard Worker sync /dev/${dest_dev} 336*2f2c4c7aSAndroid Build Coastguard Worker 337*2f2c4c7aSAndroid Build Coastguard Worker echo "Cleaning up..." 338*2f2c4c7aSAndroid Build Coastguard Worker mount /dev/${dest_dev}${part_num} /media 339*2f2c4c7aSAndroid Build Coastguard Worker chroot /media /usr/local/bin/install-cleanup 340*2f2c4c7aSAndroid Build Coastguard Worker 341*2f2c4c7aSAndroid Build Coastguard Worker if [ $? == 0 ]; then 342*2f2c4c7aSAndroid Build Coastguard Worker echo "Successfully copied Rock Pi image!" 343*2f2c4c7aSAndroid Build Coastguard Worker while true; do 344*2f2c4c7aSAndroid Build Coastguard Worker led 1; sleep 0.5 345*2f2c4c7aSAndroid Build Coastguard Worker led 0; sleep 0.5 346*2f2c4c7aSAndroid Build Coastguard Worker done 347*2f2c4c7aSAndroid Build Coastguard Worker else 348*2f2c4c7aSAndroid Build Coastguard Worker echo "Error while copying Rock Pi image" 349*2f2c4c7aSAndroid Build Coastguard Worker while true; do 350*2f2c4c7aSAndroid Build Coastguard Worker led 1; sleep 0.1 351*2f2c4c7aSAndroid Build Coastguard Worker led 0; sleep 0.1 352*2f2c4c7aSAndroid Build Coastguard Worker done 353*2f2c4c7aSAndroid Build Coastguard Worker fi 354*2f2c4c7aSAndroid Build Coastguard Workerelse 355*2f2c4c7aSAndroid Build Coastguard Worker echo "Expanding /dev/${dest_dev}${part_num} filesystem..." 356*2f2c4c7aSAndroid Build Coastguard Worker e2fsck -fy /dev/${dest_dev}${part_num} 357*2f2c4c7aSAndroid Build Coastguard Worker resize2fs /dev/${dest_dev}${part_num} 358*2f2c4c7aSAndroid Build Coastguard Worker tune2fs -O has_journal /dev/${dest_dev}${part_num} 359*2f2c4c7aSAndroid Build Coastguard Worker e2fsck -fy /dev/${dest_dev}${part_num} 360*2f2c4c7aSAndroid Build Coastguard Worker sync /dev/${dest_dev} 361*2f2c4c7aSAndroid Build Coastguard Worker 362*2f2c4c7aSAndroid Build Coastguard Worker echo "Cleaning up..." 363*2f2c4c7aSAndroid Build Coastguard Worker /usr/local/bin/install-cleanup 364*2f2c4c7aSAndroid Build Coastguard Workerfi 365*2f2c4c7aSAndroid Build Coastguard WorkerEOF 366*2f2c4c7aSAndroid Build Coastguard Workerchmod a+x /usr/local/bin/sd-dupe 367*2f2c4c7aSAndroid Build Coastguard Worker 368*2f2c4c7aSAndroid Build Coastguard Worker# Write one-shot service for SDCard->eMMC duplication 369*2f2c4c7aSAndroid Build Coastguard Workercat >/etc/systemd/system/sd-dupe.service <<EOF 370*2f2c4c7aSAndroid Build Coastguard Worker[Unit] 371*2f2c4c7aSAndroid Build Coastguard WorkerDescription=Duplicate SD card rootfs to eMMC on Rock Pi 372*2f2c4c7aSAndroid Build Coastguard WorkerConditionPathExists=/usr/local/bin/sd-dupe 373*2f2c4c7aSAndroid Build Coastguard WorkerAfter=led.service 374*2f2c4c7aSAndroid Build Coastguard Worker 375*2f2c4c7aSAndroid Build Coastguard Worker[Service] 376*2f2c4c7aSAndroid Build Coastguard WorkerType=simple 377*2f2c4c7aSAndroid Build Coastguard WorkerExecStart=/usr/local/bin/sd-dupe 378*2f2c4c7aSAndroid Build Coastguard WorkerTimeoutSec=0 379*2f2c4c7aSAndroid Build Coastguard WorkerStandardOutput=tty 380*2f2c4c7aSAndroid Build Coastguard Worker 381*2f2c4c7aSAndroid Build Coastguard Worker[Install] 382*2f2c4c7aSAndroid Build Coastguard WorkerWantedBy=multi-user.target 383*2f2c4c7aSAndroid Build Coastguard WorkerEOF 384*2f2c4c7aSAndroid Build Coastguard Worker 385*2f2c4c7aSAndroid Build Coastguard Worker# Write cleanup script for eMMC (after duplication) 386*2f2c4c7aSAndroid Build Coastguard Workercat >/usr/local/bin/install-cleanup <<"EOF" 387*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash 388*2f2c4c7aSAndroid Build Coastguard WorkerMAC=$(ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/' | tr -d :) 389*2f2c4c7aSAndroid Build Coastguard Workersed -i "s,rockpi,rockpi-${MAC}," /etc/hosts 390*2f2c4c7aSAndroid Build Coastguard Workersudo hostnamectl set-hostname "rockpi-${MAC}" 391*2f2c4c7aSAndroid Build Coastguard Worker 392*2f2c4c7aSAndroid Build Coastguard Workerrm -f /etc/machine-id 393*2f2c4c7aSAndroid Build Coastguard Workerrm -f /var/lib/dbus/machine-id 394*2f2c4c7aSAndroid Build Coastguard Workerdbus-uuidgen --ensure 395*2f2c4c7aSAndroid Build Coastguard Workersystemd-machine-id-setup 396*2f2c4c7aSAndroid Build Coastguard Worker 397*2f2c4c7aSAndroid Build Coastguard Workersystemctl disable sd-dupe 398*2f2c4c7aSAndroid Build Coastguard Workerrm -f /etc/systemd/system/sd-dupe.service 399*2f2c4c7aSAndroid Build Coastguard Workerrm -f /usr/local/bin/sd-dupe 400*2f2c4c7aSAndroid Build Coastguard Workerrm -f /usr/local/bin/install-cleanup 401*2f2c4c7aSAndroid Build Coastguard WorkerEOF 402*2f2c4c7aSAndroid Build Coastguard Workerchmod a+x /usr/local/bin/install-cleanup 403*2f2c4c7aSAndroid Build Coastguard Worker 404*2f2c4c7aSAndroid Build Coastguard Workersystemctl enable poe 405*2f2c4c7aSAndroid Build Coastguard Workersystemctl enable led 406*2f2c4c7aSAndroid Build Coastguard Workersystemctl enable sd-dupe 407*2f2c4c7aSAndroid Build Coastguard Worker 408*2f2c4c7aSAndroid Build Coastguard Workersetup_dynamic_networking "eth0" "" 409*2f2c4c7aSAndroid Build Coastguard Worker 410*2f2c4c7aSAndroid Build Coastguard Workerupdate_apt_sources bullseye "" 411*2f2c4c7aSAndroid Build Coastguard Worker 412*2f2c4c7aSAndroid Build Coastguard Workersetup_cuttlefish_user 413*2f2c4c7aSAndroid Build Coastguard Worker 414*2f2c4c7aSAndroid Build Coastguard Workersetup_and_build_cuttlefish 415*2f2c4c7aSAndroid Build Coastguard Workersetup_and_build_iptables 416*2f2c4c7aSAndroid Build Coastguard Worker 417*2f2c4c7aSAndroid Build Coastguard Workerinstall_and_cleanup_cuttlefish 418*2f2c4c7aSAndroid Build Coastguard Workerinstall_and_cleanup_iptables 419*2f2c4c7aSAndroid Build Coastguard Worker 420*2f2c4c7aSAndroid Build Coastguard Workercreate_systemd_getty_symlinks ttyS2 421*2f2c4c7aSAndroid Build Coastguard Worker 422*2f2c4c7aSAndroid Build Coastguard Workersetup_grub "net.ifnames=0 8250.nr_uarts=4 earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 kvm-arm.mode=nvhe sdhci.debug_quirks=0x20000000" 423*2f2c4c7aSAndroid Build Coastguard Worker 424*2f2c4c7aSAndroid Build Coastguard Workerapt-get purge -y vim-tiny 425*2f2c4c7aSAndroid Build Coastguard Workerrm -f /etc/network/interfaces.d/eth0.conf 426*2f2c4c7aSAndroid Build Coastguard Workerbullseye_cleanup 427