blob: 64133e96c57ed68f12694c4b5070a424435bd0b9 [file] [log] [blame]
Tristan Muntsinger82c10502019-10-15 14:49:19 -07001#!/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
17source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags"
18
Tristan Muntsinger34569112019-10-16 22:10:54 -070019DEFINE_boolean p1 \
20 false "Only generate/write the 1st partition (loader1)" "1"
21DEFINE_boolean p2 \
22 false "Only generate/write the 2nd partition (env)" "2"
23DEFINE_boolean p3 \
24 false "Only generate/write the 3rd partition (loader2)" "3"
25DEFINE_boolean p4 \
26 false "Only generate/write the 4th partition (trust)" "4"
27DEFINE_boolean p5 \
28 false "Only generate/write the 5th partition (rootfs)" "5"
29
30FLAGS_HELP="USAGE: $0 <KERNEL_DIR> [IMAGE] [flags]"
Tristan Muntsinger82c10502019-10-15 14:49:19 -070031
32FLAGS "$@" || exit $?
33eval set -- "${FLAGS_ARGV}"
34
Tristan Muntsinger34569112019-10-16 22:10:54 -070035if [ ${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}
45fi
46
Tristan Muntsinger82c10502019-10-15 14:49:19 -070047for 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
56done
57
Tristan Muntsingere4eeded2019-10-15 20:26:15 -070058USE_IMAGE=`[ -z "${IMAGE}" ] && echo "0" || echo "1"`
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -070059OVERWRITE=`[ -e "${IMAGE}" ] && echo "1" || echo "0"`
60if [ -z $KERNEL_DIR ]; then
Tristan Muntsinger82c10502019-10-15 14:49:19 -070061 flags_help
62 exit 1
63fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -070064if [ ! -e "${KERNEL_DIR}" ]; then
65 echo "error: can't find '${KERNEL_DIR}'. aborting..."
66 exit 1
67fi
68
69# escalate to superuser
70if [ $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}" ${@}
77fi
78
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -070079if [ $OVERWRITE -eq 1 ]; then
80 OVERWRITE_IMAGE=${IMAGE}
81 IMAGE=`mktemp`
82fi
83
Tristan Muntsingere4eeded2019-10-15 20:26:15 -070084if [ $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}"
117fi
118
Tristan Muntsinger34569112019-10-16 22:10:54 -0700119if [ ${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 -
124fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700125
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700126cd ${ANDROID_BUILD_TOP}/external/u-boot
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700127
Tristan Muntsinger34569112019-10-16 22:10:54 -0700128if [ ${FLAGS_p2} -eq ${FLAGS_TRUE} ]; then
129 tmpfile=`mktemp`
130 bootenv=`mktemp`
131 cat > ${tmpfile} << "EOF"
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700132bootdelay=2
133baudrate=1500000
134scriptaddr=0x00500000
135boot_targets=mmc1 mmc0
136bootcmd=run distro_bootcmd
137distro_bootcmd=for target in ${boot_targets}; do run bootcmd_${target}; done
138bootcmd_mmc0=devnum=0; run mmc_boot
139bootcmd_mmc1=devnum=1; run mmc_boot
140mmc_boot=if mmc dev ${devnum}; then ; run scan_for_boot_part; fi
Tristan Muntsinger3d6ee1c2019-11-23 17:01:35 -0800141scan_for_boot_part=part list mmc ${devnum} -bootable devplist; env exists devplist || setenv devplist 1; for distro_bootpart in ${devplist}; do if fstype mmc ${devnum}:${distro_bootpart} bootfstype; then run find_script; fi; done; setenv devplist;
142find_script=if test -e mmc ${devnum}:${distro_bootpart} /boot/boot.scr; then echo Found U-Boot script /boot/boot.scr; run run_scr; fi
143run_scr=load mmc ${devnum}:${distro_bootpart} ${scriptaddr} /boot/boot.scr; source ${scriptaddr}
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700144EOF
Tristan Muntsingera8e8b452019-11-25 18:33:59 -0800145 script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
146 echo "Sha=`${script_dir}/gen_sha.sh --kernel ${KERNEL_DIR}`" >> ${tmpfile}
Tristan Muntsinger34569112019-10-16 22:10:54 -0700147 ${ANDROID_HOST_OUT}/bin/mkenvimage -s 32768 -o ${bootenv} - < ${tmpfile}
148fi
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700149
Tristan Muntsinger34569112019-10-16 22:10:54 -0700150if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ] || [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
151 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig
152 if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
153 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
154 fi
155 if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
156 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- u-boot.itb
157 fi
158 if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
159 idbloader=`mktemp`
160 ${ANDROID_HOST_OUT}/bin/mkimage -n rk3399 -T rksd -d tpl/u-boot-tpl.bin ${idbloader}
161 cat spl/u-boot-spl.bin >> ${idbloader}
162 fi
163fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700164cd -
165
Tristan Muntsinger34569112019-10-16 22:10:54 -0700166if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
167 ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE}
168 if [ $? -ne 0 ]; then
169 echo "error: failed to build rootfs. exiting..."
170 exit 1
171 fi
172 truncate -s +3G ${IMAGE}
173 e2fsck -f ${IMAGE}
174 resize2fs ${IMAGE}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700175
Tristan Muntsinger34569112019-10-16 22:10:54 -0700176 mntdir=`mktemp -d`
177 mount ${IMAGE} ${mntdir}
178 if [ $? != 0 ]; then
179 echo "error: unable to mount ${IMAGE} ${mntdir}"
180 exit 1
181 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700182
Tristan Muntsinger34569112019-10-16 22:10:54 -0700183 cat > ${mntdir}/boot/boot.cmd << "EOF"
Tristan Muntsingerb9a73632020-01-03 02:38:47 -0800184setenv start_poe 'gpio set 150; gpio clear 146'
185run start_poe
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700186setenv bootcmd_dhcp '
187mw.b ${scriptaddr} 0 0x8000
188mmc dev 0 0
189mmc read ${scriptaddr} 0x1fc0 0x40
190env import -b ${scriptaddr} 0x8000
191mw.b ${scriptaddr} 0 0x8000
192if dhcp ${scriptaddr} manifest.txt; then
193 setenv OldSha ${Sha}
194 setenv Sha
195 env import -t ${scriptaddr} 0x8000 ManifestVersion
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800196 echo "Manifest version $ManifestVersion";
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700197 if test "$ManifestVersion" = "1"; then
198 run manifest1
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800199 elif test "$ManifestVersion" = "2"; then
200 run manifest2
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700201 else
202 run manifestX
203 fi
204fi'
205setenv manifestX 'echo "***** ERROR: Unknown manifest version! *****";'
206setenv manifest1 '
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700207env import -t ${scriptaddr} 0x8000
208if test "$Sha" != "$OldSha"; then
209 setenv serverip ${TftpServer}
210 setenv loadaddr 0x00200000
211 mmc dev 0 0;
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800212 setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg
213 setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb
214 setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg
215 setenv file $RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg
216 setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700217 mw.b ${scriptaddr} 0 0x8000
218 env export -b ${scriptaddr} 0x8000
219 mmc write ${scriptaddr} 0x1fc0 0x40
220else
221 echo "Already have ${Sha}. Booting..."
222fi'
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800223setenv manifest2 '
224env import -t ${scriptaddr} 0x8000
225if test "$DFUethaddr" = "$ethaddr" || test "$DFUethaddr" = ""; then
226 if test "$Sha" != "$OldSha"; then
227 setenv serverip ${TftpServer}
228 setenv loadaddr 0x00200000
229 mmc dev 0 0;
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800230 setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg
231 setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb
232 setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg
233 setenv file $RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg
234 setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800235 mw.b ${scriptaddr} 0 0x8000
236 env export -b ${scriptaddr} 0x8000
237 mmc write ${scriptaddr} 0x1fc0 0x40
238 else
239 echo "Already have ${Sha}. Booting..."
240 fi
241else
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800242 echo "Update ${Sha} is not for me. Booting..."
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800243fi'
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800244setenv tftpget1 '
245if test "$file" != ""; then
246 mw.b ${loadaddr} 0 0x400000
247 tftp ${file}
248 if test $? = 0; then
249 setenv isGz 0 && setexpr isGz sub .*\\.gz\$ 1 ${file}
250 if test $isGz = 1; then
251 if test ${file} = ${UbootEnv}; then
252 echo "** gzipped env unsupported **"
253 else
254 setexpr boffset ${offset} * 0x200
255 gzwrite mmc 0 ${loadaddr} 0x${filesize} 100000 ${boffset} && echo Updated: ${file}
256 fi
257 elif test ${file} = ${UbootEnv}; then
258 env import -b ${loadaddr} && echo Updated: ${file}
259 else
260 if test $size = 0; then
261 setexpr x $filesize - 1
262 setexpr x $x / 0x1000
263 setexpr x $x + 1
264 setexpr x $x * 0x1000
265 setexpr x $x / 0x200
266 size=0x${x}
267 fi
268 mmc write ${loadaddr} ${offset} ${size} && echo Updated: ${file}
269 fi
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700270 fi
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800271 if test $? != 0; then
272 echo ** UPDATE FAILED: ${file} **
273 fi
274fi'
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700275if mmc dev 1 0; then; else
276 run bootcmd_dhcp;
277fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700278load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image
279load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd
280load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb
281setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid"
282run finduuid
283setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000"
284booti 0x02080000 0x04000000 0x01f00000
285EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700286 ${ANDROID_HOST_OUT}/bin/mkimage \
287 -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700288
Tristan Muntsinger34569112019-10-16 22:10:54 -0700289 cd ${KERNEL_DIR}
290 export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH
291 export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH
292 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
293 CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig
294 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
295 CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc`
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700296
Tristan Muntsinger34569112019-10-16 22:10:54 -0700297 cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/
298 mkdir -p ${mntdir}/boot/dtb/rockchip/
299 cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/
300 cd -
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700301
Tristan Muntsinger34569112019-10-16 22:10:54 -0700302 mount -o bind /proc ${mntdir}/proc
303 mount -o bind /sys ${mntdir}/sys
304 mount -o bind /dev ${mntdir}/dev
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700305
Tristan Muntsinger34569112019-10-16 22:10:54 -0700306 echo "Installing required packages..."
307 chroot ${mntdir} /bin/bash <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700308apt-get update
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700309apt-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 Muntsinger82c10502019-10-15 14:49:19 -0700310EOF
311
Tristan Muntsinger34569112019-10-16 22:10:54 -0700312 echo "Turning on DHCP client..."
313 cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700314[Match]
315Name=en*
316
317[Network]
318DHCP=yes
319EOF
320
Tristan Muntsinger34569112019-10-16 22:10:54 -0700321 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700322echo "Adding user vsoc-01 and groups..."
323useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01
324echo -e "cuttlefish\ncuttlefish" | passwd
325echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01
326EOT
327
Tristan Muntsinger34569112019-10-16 22:10:54 -0700328 echo "Cloning android-cuttlefish..."
329 cd ${mntdir}/home/vsoc-01
330 git clone https://github.com/google/android-cuttlefish.git
331 cd -
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700332
Tristan Muntsingerb9a73632020-01-03 02:38:47 -0800333 echo "Creating PoE script..."
334 cat > ${mntdir}/usr/local/bin/poe << "EOF"
335#!/bin/bash
336
337if [ "$1" == "--start" ]; then
338 echo 146 > /sys/class/gpio/export
339 echo out > /sys/class/gpio/gpio146/direction
340 echo 0 > /sys/class/gpio/gpio146/value
341 echo 150 > /sys/class/gpio/export
342 echo out > /sys/class/gpio/gpio150/direction
343 echo 1 > /sys/class/gpio/gpio150/value
344 exit 0
345fi
346
347if [ "$1" == "--stop" ]; then
348 echo 0 > /sys/class/gpio/gpio146/value
349 echo 146 > /sys/class/gpio/unexport
350 echo 0 > /sys/class/gpio/gpio150/value
351 echo 150 > /sys/class/gpio/unexport
352 exit 0
353fi
354
355if [ ! -e /sys/class/gpio/gpio146/value ] || [ ! -e /sys/class/gpio/gpio150/value ]; then
356 echo "error: PoE service not initialized"
357 exit 1
358fi
359
360if [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then
361 echo 0 > /sys/class/gpio/gpio150/value
362 exit 0
363fi
364
365if [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then
366 echo 1 > /sys/class/gpio/gpio150/value
367 exit 0
368fi
369
370echo "usage: poe <0|1>"
371exit 1
372EOF
373 chown root:root ${mntdir}/usr/local/bin/poe
374 chmod 755 ${mntdir}/usr/local/bin/poe
375
376 echo "Creating PoE service..."
377 cat > ${mntdir}/etc/systemd/system/poe.service << EOF
378[Unit]
379 Description=PoE service
380 ConditionPathExists=/usr/local/bin/poe
381
382[Service]
383 Type=oneshot
384 ExecStart=/usr/local/bin/poe --start
385 ExecStop=/usr/local/bin/poe --stop
386 RemainAfterExit=true
387 StandardOutput=journal
388
389[Install]
390 WantedBy=multi-user.target
391EOF
392
Tristan Muntsinger34569112019-10-16 22:10:54 -0700393 echo "Creating led script..."
394 cat > ${mntdir}/usr/local/bin/led << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700395#!/bin/bash
396
397if [ "$1" == "--start" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700398 echo 125 > /sys/class/gpio/export
399 echo out > /sys/class/gpio/gpio125/direction
400 chmod 666 /sys/class/gpio/gpio125/value
401 echo 0 > /sys/class/gpio/gpio125/value
402 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700403fi
404
405if [ "$1" == "--stop" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700406 echo 0 > /sys/class/gpio/gpio125/value
407 echo 125 > /sys/class/gpio/unexport
408 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700409fi
410
411if [ ! -e /sys/class/gpio/gpio125/value ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700412 echo "error: led service not initialized"
413 exit 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700414fi
415
416if [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700417 echo 0 > /sys/class/gpio/gpio125/value
418 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700419fi
420
421if [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700422 echo 1 > /sys/class/gpio/gpio125/value
423 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700424fi
425
426echo "usage: led <0|1>"
427exit 1
428EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700429 chown root:root ${mntdir}/usr/local/bin/led
430 chmod 755 ${mntdir}/usr/local/bin/led
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700431
Tristan Muntsinger34569112019-10-16 22:10:54 -0700432 echo "Creating led service..."
433 cat > ${mntdir}/etc/systemd/system/led.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700434[Unit]
435 Description=led service
436 ConditionPathExists=/usr/local/bin/led
437
438[Service]
439 Type=oneshot
440 ExecStart=/usr/local/bin/led --start
441 ExecStop=/usr/local/bin/led --stop
442 RemainAfterExit=true
443 StandardOutput=journal
444
445[Install]
446 WantedBy=multi-user.target
447EOF
448
Tristan Muntsinger34569112019-10-16 22:10:54 -0700449 echo "Creating SD duplicator script..."
450 cat > ${mntdir}/usr/local/bin/sd-dupe << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700451#!/bin/bash
452led 0
453
454src_dev=mmcblk0
455dest_dev=mmcblk1
456part_num=p5
457
458if [ -e /dev/mmcblk0p5 ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700459 led 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700460
Tristan Muntsinger34569112019-10-16 22:10:54 -0700461 sgdisk -Z -a1 /dev/${dest_dev}
462 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 /dev/${dest_dev}
463 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env /dev/${dest_dev}
464 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 /dev/${dest_dev}
465 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust /dev/${dest_dev}
466 sgdisk -a1 -n:5:32768:- -A:5:set:2 -t:5:8305 -c:5:rootfs /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700467
Tristan Muntsinger34569112019-10-16 22:10:54 -0700468 src_block_count=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block count:" | sed 's/.*: *//'`
469 src_block_size=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block size:" | sed 's/.*: *//'`
470 src_fs_size=$(( src_block_count*src_block_size ))
471 src_fs_size_m=$(( src_fs_size / 1024 / 1024 + 1 ))
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700472
Tristan Muntsinger34569112019-10-16 22:10:54 -0700473 dd if=/dev/${src_dev}p1 of=/dev/${dest_dev}p1 conv=sync,noerror status=progress
474 dd if=/dev/${src_dev}p2 of=/dev/${dest_dev}p2 conv=sync,noerror status=progress
475 dd if=/dev/${src_dev}p3 of=/dev/${dest_dev}p3 conv=sync,noerror status=progress
476 dd if=/dev/${src_dev}p4 of=/dev/${dest_dev}p4 conv=sync,noerror status=progress
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700477
Tristan Muntsinger34569112019-10-16 22:10:54 -0700478 echo "Writing ${src_fs_size_m} MB: /dev/${src_dev} -> /dev/${dest_dev}..."
479 dd if=/dev/${src_dev}${part_num} of=/dev/${dest_dev}${part_num} bs=1M conv=sync,noerror status=progress
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700480
Tristan Muntsinger34569112019-10-16 22:10:54 -0700481 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
482 e2fsck -fy /dev/${dest_dev}${part_num}
483 resize2fs /dev/${dest_dev}${part_num}
484 tune2fs -O has_journal /dev/${dest_dev}${part_num}
485 e2fsck -fy /dev/${dest_dev}${part_num}
486 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700487
Tristan Muntsinger34569112019-10-16 22:10:54 -0700488 echo "Cleaning up..."
489 mount /dev/${dest_dev}${part_num} /media
490 chroot /media /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700491
Tristan Muntsinger34569112019-10-16 22:10:54 -0700492 if [ $? == 0 ]; then
493 echo "Successfully copied Rock Pi image!"
494 while true; do
495 led 1; sleep 0.5
496 led 0; sleep 0.5
497 done
498 else
499 echo "Error while copying Rock Pi image"
500 while true; do
501 led 1; sleep 0.1
502 led 0; sleep 0.1
503 done
504 fi
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700505else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700506 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
507 e2fsck -fy /dev/${dest_dev}${part_num}
508 resize2fs /dev/${dest_dev}${part_num}
509 tune2fs -O has_journal /dev/${dest_dev}${part_num}
510 e2fsck -fy /dev/${dest_dev}${part_num}
511 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700512
Tristan Muntsinger34569112019-10-16 22:10:54 -0700513 echo "Cleaning up..."
514 /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700515fi
516EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700517 chmod +x ${mntdir}/usr/local/bin/sd-dupe
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700518
Tristan Muntsinger34569112019-10-16 22:10:54 -0700519 echo "Creating SD duplicator service..."
520 cat > ${mntdir}/etc/systemd/system/sd-dupe.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700521[Unit]
522 Description=Duplicate SD card rootfs to eMMC on Rock Pi
523 ConditionPathExists=/usr/local/bin/sd-dupe
524 After=led.service
525
526[Service]
527 Type=simple
528 ExecStart=/usr/local/bin/sd-dupe
529 TimeoutSec=0
530 StandardOutput=tty
531
532[Install]
533 WantedBy=multi-user.target
534EOF
535
Tristan Muntsingere62646c2020-01-02 23:02:43 -0800536 umount ${mntdir}/sys
537 umount ${mntdir}/dev
538 umount ${mntdir}/proc
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700539
Tristan Muntsingere62646c2020-01-02 23:02:43 -0800540 chroot ${mntdir} /bin/bash << "EOT"
541echo "Installing cuttlefish-common package..."
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700542dpkg --add-architecture amd64
Tristan Muntsingere62646c2020-01-02 23:02:43 -0800543apt-get update
544apt-get install -y -f libc6:amd64 qemu-user-static
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700545cd /home/vsoc-01/android-cuttlefish
546dpkg-buildpackage -d -uc -us
547apt-get install -y -f ../cuttlefish-common_*_arm64.deb
548apt-get clean
Tristan Muntsingere62646c2020-01-02 23:02:43 -0800549
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700550usermod -aG cvdnetwork vsoc-01
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700551chmod 660 /dev/vhost-vsock
552chown root:cvdnetwork /dev/vhost-vsock
Tristan Muntsingere62646c2020-01-02 23:02:43 -0800553rm -rf /home/vsoc-01/*
554EOT
555
556 echo "Creating cleanup script..."
557 cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF"
558#!/bin/bash
559echo "nameserver 8.8.8.8" > /etc/resolv.conf
560MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/' | tr -d :`
561sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts
562sudo hostnamectl set-hostname "rockpi-${MAC}"
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700563
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700564rm /etc/machine-id
565rm /var/lib/dbus/machine-id
566dbus-uuidgen --ensure
567systemd-machine-id-setup
568
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700569systemctl disable sd-dupe
570rm /etc/systemd/system/sd-dupe.service
571rm /usr/local/bin/sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700572rm /usr/local/bin/install-cleanup
573EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700574 chmod +x ${mntdir}/usr/local/bin/install-cleanup
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700575
Tristan Muntsinger34569112019-10-16 22:10:54 -0700576 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700577echo "Enabling services..."
Tristan Muntsingerb9a73632020-01-03 02:38:47 -0800578systemctl enable poe
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700579systemctl enable led
580systemctl enable sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700581
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700582echo "Creating Initial Ramdisk..."
583update-initramfs -c -t -k "5.2.0"
584mkimage -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
585ln -s /boot/uInitrd-5.2.0 /boot/uInitrd
586EOT
587
Tristan Muntsinger34569112019-10-16 22:10:54 -0700588 umount ${mntdir}
589
590 # Turn on journaling
591 tune2fs -O ^has_journal ${IMAGE}
592 e2fsck -fy ${IMAGE} >/dev/null 2>&1
593fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700594
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700595if [ ${USE_IMAGE} -eq 0 ]; then
596 # 32GB eMMC size
Tristan Muntsinger34569112019-10-16 22:10:54 -0700597 end_sector=61071326
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700598 device=/dev/${mmc_dev}
599 devicep=${device}
600
601 sgdisk -Z -a1 ${device}
602 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${device}
603 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${device}
604 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${device}
605 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${device}
Tristan Muntsinger34569112019-10-16 22:10:54 -0700606 sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${device}
607 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
608 dd if=${IMAGE} of=${devicep}5 bs=1M
609 resize2fs ${devicep}5 >/dev/null 2>&1
610 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700611else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700612 device=$(losetup -f)
613 devicep=${device}p
614 if [ ${FLAGS_p5} -eq ${FLAGS_FALSE} ]; then
615 fs_end=3G
616 end_sector=-
617 fi
618 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
619 # Minimize rootfs filesystem
620 while true; do
621 out=`sudo resize2fs -M ${IMAGE} 2>&1`
622 if [[ $out =~ "Nothing to do" ]]; then
623 break
624 fi
625 done
626 # Minimize rootfs file size
627 block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'`
628 block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'`
629 sector_size=512
630 start_sector=32768
631 fs_size=$(( block_count*block_size ))
632 fs_sectors=$(( fs_size/sector_size ))
633 part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned
634 end_sector=$(( start_sector+part_sectors-1 ))
635 secondary_gpt_sectors=33
636 fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size ))
637 image_size=$(( part_sectors*sector_size ))
638 truncate -s ${image_size} ${IMAGE}
639 e2fsck -fy ${IMAGE} >/dev/null 2>&1
640 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700641
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700642 # Create final image
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -0700643 if [ $OVERWRITE -eq 1 ]; then
644 tmpimg=${OVERWRITE_IMAGE}
645 else
646 tmpimg=`mktemp`
647 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700648 truncate -s ${fs_end} ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700649
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700650 # Create GPT
651 sgdisk -Z -a1 ${tmpimg}
652 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg}
653 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg}
654 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg}
655 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg}
656 sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700657
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700658 losetup ${device} ${tmpimg}
659 partx -v --add ${device}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700660
Tristan Muntsinger34569112019-10-16 22:10:54 -0700661 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
662 dd if=${IMAGE} of=${devicep}5 bs=1M
663 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700664fi
Tristan Muntsinger34569112019-10-16 22:10:54 -0700665if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
666 dd if=${idbloader} of=${devicep}1
667fi
668if [ ${FLAGS_p2} -eq ${FLAGS_TRUE} ]; then
669 dd if=${bootenv} of=${devicep}2
670fi
671if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
672 dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3
673fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700674if [ ${USE_IMAGE} -eq 1 ]; then
675 chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -0700676 if [ $OVERWRITE -eq 0 ]; then
677 mv ${tmpimg} ${IMAGE}
678 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700679 partx -v --delete ${device}
680 losetup -d ${device}
681fi