blob: b65600ccfd2c5aa0f29414999e4a12f284b27b9b [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 Muntsingerbcdc07b2019-10-16 22:56:19 -0700184setenv bootcmd_dhcp '
185mw.b ${scriptaddr} 0 0x8000
186mmc dev 0 0
187mmc read ${scriptaddr} 0x1fc0 0x40
188env import -b ${scriptaddr} 0x8000
189mw.b ${scriptaddr} 0 0x8000
190if dhcp ${scriptaddr} manifest.txt; then
191 setenv OldSha ${Sha}
192 setenv Sha
193 env import -t ${scriptaddr} 0x8000 ManifestVersion
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800194 echo "Manifest version $ManifestVersion";
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700195 if test "$ManifestVersion" = "1"; then
196 run manifest1
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800197 elif test "$ManifestVersion" = "2"; then
198 run manifest2
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700199 else
200 run manifestX
201 fi
202fi'
203setenv manifestX 'echo "***** ERROR: Unknown manifest version! *****";'
204setenv manifest1 '
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700205env import -t ${scriptaddr} 0x8000
206if test "$Sha" != "$OldSha"; then
207 setenv serverip ${TftpServer}
208 setenv loadaddr 0x00200000
209 mmc dev 0 0;
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800210 setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg
211 setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb
212 setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg
213 setenv file $RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg
214 setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700215 mw.b ${scriptaddr} 0 0x8000
216 env export -b ${scriptaddr} 0x8000
217 mmc write ${scriptaddr} 0x1fc0 0x40
218else
219 echo "Already have ${Sha}. Booting..."
220fi'
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800221setenv manifest2 '
222env import -t ${scriptaddr} 0x8000
223if test "$DFUethaddr" = "$ethaddr" || test "$DFUethaddr" = ""; then
224 if test "$Sha" != "$OldSha"; then
225 setenv serverip ${TftpServer}
226 setenv loadaddr 0x00200000
227 mmc dev 0 0;
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800228 setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg
229 setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb
230 setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg
231 setenv file $RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg
232 setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800233 mw.b ${scriptaddr} 0 0x8000
234 env export -b ${scriptaddr} 0x8000
235 mmc write ${scriptaddr} 0x1fc0 0x40
236 else
237 echo "Already have ${Sha}. Booting..."
238 fi
239else
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800240 echo "Update ${Sha} is not for me. Booting..."
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800241fi'
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800242setenv tftpget1 '
243if test "$file" != ""; then
244 mw.b ${loadaddr} 0 0x400000
245 tftp ${file}
246 if test $? = 0; then
247 setenv isGz 0 && setexpr isGz sub .*\\.gz\$ 1 ${file}
248 if test $isGz = 1; then
249 if test ${file} = ${UbootEnv}; then
250 echo "** gzipped env unsupported **"
251 else
252 setexpr boffset ${offset} * 0x200
253 gzwrite mmc 0 ${loadaddr} 0x${filesize} 100000 ${boffset} && echo Updated: ${file}
254 fi
255 elif test ${file} = ${UbootEnv}; then
256 env import -b ${loadaddr} && echo Updated: ${file}
257 else
258 if test $size = 0; then
259 setexpr x $filesize - 1
260 setexpr x $x / 0x1000
261 setexpr x $x + 1
262 setexpr x $x * 0x1000
263 setexpr x $x / 0x200
264 size=0x${x}
265 fi
266 mmc write ${loadaddr} ${offset} ${size} && echo Updated: ${file}
267 fi
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700268 fi
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800269 if test $? != 0; then
270 echo ** UPDATE FAILED: ${file} **
271 fi
272fi'
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700273if mmc dev 1 0; then; else
274 run bootcmd_dhcp;
275fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700276load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image
277load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd
278load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb
279setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid"
280run finduuid
281setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000"
282booti 0x02080000 0x04000000 0x01f00000
283EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700284 ${ANDROID_HOST_OUT}/bin/mkimage \
285 -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700286
Tristan Muntsinger34569112019-10-16 22:10:54 -0700287 cd ${KERNEL_DIR}
288 export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH
289 export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH
290 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
291 CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig
292 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
293 CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc`
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700294
Tristan Muntsinger34569112019-10-16 22:10:54 -0700295 cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/
296 mkdir -p ${mntdir}/boot/dtb/rockchip/
297 cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/
298 cd -
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700299
Tristan Muntsinger34569112019-10-16 22:10:54 -0700300 mount -o bind /proc ${mntdir}/proc
301 mount -o bind /sys ${mntdir}/sys
302 mount -o bind /dev ${mntdir}/dev
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700303
Tristan Muntsinger34569112019-10-16 22:10:54 -0700304 echo "Installing required packages..."
305 chroot ${mntdir} /bin/bash <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700306apt-get update
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700307apt-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 -0700308EOF
309
Tristan Muntsinger34569112019-10-16 22:10:54 -0700310 echo "Turning on DHCP client..."
311 cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700312[Match]
313Name=en*
314
315[Network]
316DHCP=yes
317EOF
318
Tristan Muntsinger34569112019-10-16 22:10:54 -0700319 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700320echo "Adding user vsoc-01 and groups..."
321useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01
322echo -e "cuttlefish\ncuttlefish" | passwd
323echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01
324EOT
325
Tristan Muntsinger34569112019-10-16 22:10:54 -0700326 echo "Cloning android-cuttlefish..."
327 cd ${mntdir}/home/vsoc-01
328 git clone https://github.com/google/android-cuttlefish.git
329 cd -
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700330
Tristan Muntsinger34569112019-10-16 22:10:54 -0700331 echo "Creating led script..."
332 cat > ${mntdir}/usr/local/bin/led << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700333#!/bin/bash
334
335if [ "$1" == "--start" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700336 echo 125 > /sys/class/gpio/export
337 echo out > /sys/class/gpio/gpio125/direction
338 chmod 666 /sys/class/gpio/gpio125/value
339 echo 0 > /sys/class/gpio/gpio125/value
340 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700341fi
342
343if [ "$1" == "--stop" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700344 echo 0 > /sys/class/gpio/gpio125/value
345 echo 125 > /sys/class/gpio/unexport
346 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700347fi
348
349if [ ! -e /sys/class/gpio/gpio125/value ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700350 echo "error: led service not initialized"
351 exit 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700352fi
353
354if [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700355 echo 0 > /sys/class/gpio/gpio125/value
356 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700357fi
358
359if [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700360 echo 1 > /sys/class/gpio/gpio125/value
361 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700362fi
363
364echo "usage: led <0|1>"
365exit 1
366EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700367 chown root:root ${mntdir}/usr/local/bin/led
368 chmod 755 ${mntdir}/usr/local/bin/led
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700369
Tristan Muntsinger34569112019-10-16 22:10:54 -0700370 echo "Creating led service..."
371 cat > ${mntdir}/etc/systemd/system/led.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700372[Unit]
373 Description=led service
374 ConditionPathExists=/usr/local/bin/led
375
376[Service]
377 Type=oneshot
378 ExecStart=/usr/local/bin/led --start
379 ExecStop=/usr/local/bin/led --stop
380 RemainAfterExit=true
381 StandardOutput=journal
382
383[Install]
384 WantedBy=multi-user.target
385EOF
386
Tristan Muntsinger34569112019-10-16 22:10:54 -0700387 echo "Creating SD duplicator script..."
388 cat > ${mntdir}/usr/local/bin/sd-dupe << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700389#!/bin/bash
390led 0
391
392src_dev=mmcblk0
393dest_dev=mmcblk1
394part_num=p5
395
396if [ -e /dev/mmcblk0p5 ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700397 led 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700398
Tristan Muntsinger34569112019-10-16 22:10:54 -0700399 sgdisk -Z -a1 /dev/${dest_dev}
400 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 /dev/${dest_dev}
401 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env /dev/${dest_dev}
402 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 /dev/${dest_dev}
403 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust /dev/${dest_dev}
404 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 -0700405
Tristan Muntsinger34569112019-10-16 22:10:54 -0700406 src_block_count=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block count:" | sed 's/.*: *//'`
407 src_block_size=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block size:" | sed 's/.*: *//'`
408 src_fs_size=$(( src_block_count*src_block_size ))
409 src_fs_size_m=$(( src_fs_size / 1024 / 1024 + 1 ))
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700410
Tristan Muntsinger34569112019-10-16 22:10:54 -0700411 dd if=/dev/${src_dev}p1 of=/dev/${dest_dev}p1 conv=sync,noerror status=progress
412 dd if=/dev/${src_dev}p2 of=/dev/${dest_dev}p2 conv=sync,noerror status=progress
413 dd if=/dev/${src_dev}p3 of=/dev/${dest_dev}p3 conv=sync,noerror status=progress
414 dd if=/dev/${src_dev}p4 of=/dev/${dest_dev}p4 conv=sync,noerror status=progress
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700415
Tristan Muntsinger34569112019-10-16 22:10:54 -0700416 echo "Writing ${src_fs_size_m} MB: /dev/${src_dev} -> /dev/${dest_dev}..."
417 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 -0700418
Tristan Muntsinger34569112019-10-16 22:10:54 -0700419 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
420 e2fsck -fy /dev/${dest_dev}${part_num}
421 resize2fs /dev/${dest_dev}${part_num}
422 tune2fs -O has_journal /dev/${dest_dev}${part_num}
423 e2fsck -fy /dev/${dest_dev}${part_num}
424 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700425
Tristan Muntsinger34569112019-10-16 22:10:54 -0700426 echo "Cleaning up..."
427 mount /dev/${dest_dev}${part_num} /media
428 chroot /media /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700429
Tristan Muntsinger34569112019-10-16 22:10:54 -0700430 if [ $? == 0 ]; then
431 echo "Successfully copied Rock Pi image!"
432 while true; do
433 led 1; sleep 0.5
434 led 0; sleep 0.5
435 done
436 else
437 echo "Error while copying Rock Pi image"
438 while true; do
439 led 1; sleep 0.1
440 led 0; sleep 0.1
441 done
442 fi
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700443else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700444 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
445 e2fsck -fy /dev/${dest_dev}${part_num}
446 resize2fs /dev/${dest_dev}${part_num}
447 tune2fs -O has_journal /dev/${dest_dev}${part_num}
448 e2fsck -fy /dev/${dest_dev}${part_num}
449 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700450
Tristan Muntsinger34569112019-10-16 22:10:54 -0700451 echo "Cleaning up..."
452 /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700453fi
454EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700455 chmod +x ${mntdir}/usr/local/bin/sd-dupe
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700456
Tristan Muntsinger34569112019-10-16 22:10:54 -0700457 echo "Creating SD duplicator service..."
458 cat > ${mntdir}/etc/systemd/system/sd-dupe.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700459[Unit]
460 Description=Duplicate SD card rootfs to eMMC on Rock Pi
461 ConditionPathExists=/usr/local/bin/sd-dupe
462 After=led.service
463
464[Service]
465 Type=simple
466 ExecStart=/usr/local/bin/sd-dupe
467 TimeoutSec=0
468 StandardOutput=tty
469
470[Install]
471 WantedBy=multi-user.target
472EOF
473
Tristan Muntsinger34569112019-10-16 22:10:54 -0700474 echo "Creating cleanup script..."
475 cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700476#!/bin/bash
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700477echo "Installing cuttlefish-common package..."
478echo "nameserver 8.8.8.8" > /etc/resolv.conf
Tristan Muntsinger052d1ff2019-11-19 16:43:43 -0800479MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/' | tr -d :`
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700480sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts
481sudo hostnamectl set-hostname "rockpi-${MAC}"
482
483dpkg --add-architecture amd64
484until ping -c1 ftp.debian.org; do sleep 1; done
485ntpdate time.google.com
486while true; do
487 apt-get -o Acquire::Check-Valid-Until=false update
488 if [ $? != 0 ]; then sleep 1; continue; fi
489 apt-get install -y -f libc6:amd64 qemu-user-static
490 if [ $? != 0 ]; then sleep 1; continue; fi
491 break
492done
493cd /home/vsoc-01/android-cuttlefish
494dpkg-buildpackage -d -uc -us
495apt-get install -y -f ../cuttlefish-common_*_arm64.deb
496apt-get clean
497usermod -aG cvdnetwork vsoc-01
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700498chmod 660 /dev/vhost-vsock
499chown root:cvdnetwork /dev/vhost-vsock
500
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700501rm /etc/machine-id
502rm /var/lib/dbus/machine-id
503dbus-uuidgen --ensure
504systemd-machine-id-setup
505
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700506systemctl disable sd-dupe
507rm /etc/systemd/system/sd-dupe.service
508rm /usr/local/bin/sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700509rm /usr/local/bin/install-cleanup
510EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700511 chmod +x ${mntdir}/usr/local/bin/install-cleanup
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700512
Tristan Muntsinger34569112019-10-16 22:10:54 -0700513 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700514echo "Enabling services..."
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700515systemctl enable led
516systemctl enable sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700517
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700518echo "Creating Initial Ramdisk..."
519update-initramfs -c -t -k "5.2.0"
520mkimage -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
521ln -s /boot/uInitrd-5.2.0 /boot/uInitrd
522EOT
523
Tristan Muntsinger34569112019-10-16 22:10:54 -0700524 umount ${mntdir}/sys
525 umount ${mntdir}/dev
526 umount ${mntdir}/proc
527 umount ${mntdir}
528
529 # Turn on journaling
530 tune2fs -O ^has_journal ${IMAGE}
531 e2fsck -fy ${IMAGE} >/dev/null 2>&1
532fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700533
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700534if [ ${USE_IMAGE} -eq 0 ]; then
535 # 32GB eMMC size
Tristan Muntsinger34569112019-10-16 22:10:54 -0700536 end_sector=61071326
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700537 device=/dev/${mmc_dev}
538 devicep=${device}
539
540 sgdisk -Z -a1 ${device}
541 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${device}
542 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${device}
543 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${device}
544 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${device}
Tristan Muntsinger34569112019-10-16 22:10:54 -0700545 sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${device}
546 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
547 dd if=${IMAGE} of=${devicep}5 bs=1M
548 resize2fs ${devicep}5 >/dev/null 2>&1
549 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700550else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700551 device=$(losetup -f)
552 devicep=${device}p
553 if [ ${FLAGS_p5} -eq ${FLAGS_FALSE} ]; then
554 fs_end=3G
555 end_sector=-
556 fi
557 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
558 # Minimize rootfs filesystem
559 while true; do
560 out=`sudo resize2fs -M ${IMAGE} 2>&1`
561 if [[ $out =~ "Nothing to do" ]]; then
562 break
563 fi
564 done
565 # Minimize rootfs file size
566 block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'`
567 block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'`
568 sector_size=512
569 start_sector=32768
570 fs_size=$(( block_count*block_size ))
571 fs_sectors=$(( fs_size/sector_size ))
572 part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned
573 end_sector=$(( start_sector+part_sectors-1 ))
574 secondary_gpt_sectors=33
575 fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size ))
576 image_size=$(( part_sectors*sector_size ))
577 truncate -s ${image_size} ${IMAGE}
578 e2fsck -fy ${IMAGE} >/dev/null 2>&1
579 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700580
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700581 # Create final image
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -0700582 if [ $OVERWRITE -eq 1 ]; then
583 tmpimg=${OVERWRITE_IMAGE}
584 else
585 tmpimg=`mktemp`
586 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700587 truncate -s ${fs_end} ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700588
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700589 # Create GPT
590 sgdisk -Z -a1 ${tmpimg}
591 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg}
592 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg}
593 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg}
594 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg}
595 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 -0700596
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700597 losetup ${device} ${tmpimg}
598 partx -v --add ${device}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700599
Tristan Muntsinger34569112019-10-16 22:10:54 -0700600 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
601 dd if=${IMAGE} of=${devicep}5 bs=1M
602 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700603fi
Tristan Muntsinger34569112019-10-16 22:10:54 -0700604if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
605 dd if=${idbloader} of=${devicep}1
606fi
607if [ ${FLAGS_p2} -eq ${FLAGS_TRUE} ]; then
608 dd if=${bootenv} of=${devicep}2
609fi
610if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
611 dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3
612fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700613if [ ${USE_IMAGE} -eq 1 ]; then
614 chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -0700615 if [ $OVERWRITE -eq 0 ]; then
616 mv ${tmpimg} ${IMAGE}
617 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700618 partx -v --delete ${device}
619 losetup -d ${device}
620fi