blob: 71a316875c511549573c115a37091528b132f584 [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 Muntsinger34569112019-10-16 22:10:54 -0700145 ${ANDROID_HOST_OUT}/bin/mkenvimage -s 32768 -o ${bootenv} - < ${tmpfile}
146fi
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700147
Tristan Muntsinger34569112019-10-16 22:10:54 -0700148if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ] || [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
149 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig
150 if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
151 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
152 fi
153 if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
154 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- u-boot.itb
155 fi
156 if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
157 idbloader=`mktemp`
158 ${ANDROID_HOST_OUT}/bin/mkimage -n rk3399 -T rksd -d tpl/u-boot-tpl.bin ${idbloader}
159 cat spl/u-boot-spl.bin >> ${idbloader}
160 fi
161fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700162cd -
163
Tristan Muntsinger34569112019-10-16 22:10:54 -0700164if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
165 ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE}
166 if [ $? -ne 0 ]; then
167 echo "error: failed to build rootfs. exiting..."
168 exit 1
169 fi
170 truncate -s +3G ${IMAGE}
171 e2fsck -f ${IMAGE}
172 resize2fs ${IMAGE}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700173
Tristan Muntsinger34569112019-10-16 22:10:54 -0700174 mntdir=`mktemp -d`
175 mount ${IMAGE} ${mntdir}
176 if [ $? != 0 ]; then
177 echo "error: unable to mount ${IMAGE} ${mntdir}"
178 exit 1
179 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700180
Tristan Muntsinger34569112019-10-16 22:10:54 -0700181 cat > ${mntdir}/boot/boot.cmd << "EOF"
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700182setenv bootcmd_dhcp '
183mw.b ${scriptaddr} 0 0x8000
184mmc dev 0 0
185mmc read ${scriptaddr} 0x1fc0 0x40
186env import -b ${scriptaddr} 0x8000
187mw.b ${scriptaddr} 0 0x8000
188if dhcp ${scriptaddr} manifest.txt; then
189 setenv OldSha ${Sha}
190 setenv Sha
191 env import -t ${scriptaddr} 0x8000 ManifestVersion
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800192 echo "Manifest version $ManifestVersion";
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700193 if test "$ManifestVersion" = "1"; then
194 run manifest1
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800195 elif test "$ManifestVersion" = "2"; then
196 run manifest2
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700197 else
198 run manifestX
199 fi
200fi'
201setenv manifestX 'echo "***** ERROR: Unknown manifest version! *****";'
202setenv manifest1 '
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700203env import -t ${scriptaddr} 0x8000
204if test "$Sha" != "$OldSha"; then
205 setenv serverip ${TftpServer}
206 setenv loadaddr 0x00200000
207 mmc dev 0 0;
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800208 setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg
209 setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb
210 setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg
211 setenv file $RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg
212 setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700213 mw.b ${scriptaddr} 0 0x8000
214 env export -b ${scriptaddr} 0x8000
215 mmc write ${scriptaddr} 0x1fc0 0x40
216else
217 echo "Already have ${Sha}. Booting..."
218fi'
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800219setenv manifest2 '
220env import -t ${scriptaddr} 0x8000
221if test "$DFUethaddr" = "$ethaddr" || test "$DFUethaddr" = ""; then
222 if test "$Sha" != "$OldSha"; then
223 setenv serverip ${TftpServer}
224 setenv loadaddr 0x00200000
225 mmc dev 0 0;
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800226 setenv file $TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg
227 setenv file $UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb
228 setenv file $TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg
229 setenv file $RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg
230 setenv file $UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800231 mw.b ${scriptaddr} 0 0x8000
232 env export -b ${scriptaddr} 0x8000
233 mmc write ${scriptaddr} 0x1fc0 0x40
234 else
235 echo "Already have ${Sha}. Booting..."
236 fi
237else
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800238 echo "Update ${Sha} is not for me. Booting..."
Tristan Muntsinger19c66ee2019-11-08 19:10:48 -0800239fi'
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800240setenv tftpget1 '
241if test "$file" != ""; then
242 mw.b ${loadaddr} 0 0x400000
243 tftp ${file}
244 if test $? = 0; then
245 setenv isGz 0 && setexpr isGz sub .*\\.gz\$ 1 ${file}
246 if test $isGz = 1; then
247 if test ${file} = ${UbootEnv}; then
248 echo "** gzipped env unsupported **"
249 else
250 setexpr boffset ${offset} * 0x200
251 gzwrite mmc 0 ${loadaddr} 0x${filesize} 100000 ${boffset} && echo Updated: ${file}
252 fi
253 elif test ${file} = ${UbootEnv}; then
254 env import -b ${loadaddr} && echo Updated: ${file}
255 else
256 if test $size = 0; then
257 setexpr x $filesize - 1
258 setexpr x $x / 0x1000
259 setexpr x $x + 1
260 setexpr x $x * 0x1000
261 setexpr x $x / 0x200
262 size=0x${x}
263 fi
264 mmc write ${loadaddr} ${offset} ${size} && echo Updated: ${file}
265 fi
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700266 fi
Tristan Muntsingerb0f74ea2019-11-19 20:39:10 -0800267 if test $? != 0; then
268 echo ** UPDATE FAILED: ${file} **
269 fi
270fi'
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700271if mmc dev 1 0; then; else
272 run bootcmd_dhcp;
273fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700274load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image
275load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd
276load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb
277setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid"
278run finduuid
279setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000"
280booti 0x02080000 0x04000000 0x01f00000
281EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700282 ${ANDROID_HOST_OUT}/bin/mkimage \
283 -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700284
Tristan Muntsinger34569112019-10-16 22:10:54 -0700285 cd ${KERNEL_DIR}
286 export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH
287 export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH
288 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
289 CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig
290 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
291 CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc`
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700292
Tristan Muntsinger34569112019-10-16 22:10:54 -0700293 cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/
294 mkdir -p ${mntdir}/boot/dtb/rockchip/
295 cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/
296 cd -
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700297
Tristan Muntsinger34569112019-10-16 22:10:54 -0700298 mount -o bind /proc ${mntdir}/proc
299 mount -o bind /sys ${mntdir}/sys
300 mount -o bind /dev ${mntdir}/dev
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700301
Tristan Muntsinger34569112019-10-16 22:10:54 -0700302 echo "Installing required packages..."
303 chroot ${mntdir} /bin/bash <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700304apt-get update
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700305apt-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 -0700306EOF
307
Tristan Muntsinger34569112019-10-16 22:10:54 -0700308 echo "Turning on DHCP client..."
309 cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700310[Match]
311Name=en*
312
313[Network]
314DHCP=yes
315EOF
316
Tristan Muntsinger34569112019-10-16 22:10:54 -0700317 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700318echo "Adding user vsoc-01 and groups..."
319useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01
320echo -e "cuttlefish\ncuttlefish" | passwd
321echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01
322EOT
323
Tristan Muntsinger34569112019-10-16 22:10:54 -0700324 echo "Cloning android-cuttlefish..."
325 cd ${mntdir}/home/vsoc-01
326 git clone https://github.com/google/android-cuttlefish.git
327 cd -
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700328
Tristan Muntsinger34569112019-10-16 22:10:54 -0700329 echo "Creating led script..."
330 cat > ${mntdir}/usr/local/bin/led << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700331#!/bin/bash
332
333if [ "$1" == "--start" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700334 echo 125 > /sys/class/gpio/export
335 echo out > /sys/class/gpio/gpio125/direction
336 chmod 666 /sys/class/gpio/gpio125/value
337 echo 0 > /sys/class/gpio/gpio125/value
338 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700339fi
340
341if [ "$1" == "--stop" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700342 echo 0 > /sys/class/gpio/gpio125/value
343 echo 125 > /sys/class/gpio/unexport
344 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700345fi
346
347if [ ! -e /sys/class/gpio/gpio125/value ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700348 echo "error: led service not initialized"
349 exit 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700350fi
351
352if [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700353 echo 0 > /sys/class/gpio/gpio125/value
354 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700355fi
356
357if [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700358 echo 1 > /sys/class/gpio/gpio125/value
359 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700360fi
361
362echo "usage: led <0|1>"
363exit 1
364EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700365 chown root:root ${mntdir}/usr/local/bin/led
366 chmod 755 ${mntdir}/usr/local/bin/led
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700367
Tristan Muntsinger34569112019-10-16 22:10:54 -0700368 echo "Creating led service..."
369 cat > ${mntdir}/etc/systemd/system/led.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700370[Unit]
371 Description=led service
372 ConditionPathExists=/usr/local/bin/led
373
374[Service]
375 Type=oneshot
376 ExecStart=/usr/local/bin/led --start
377 ExecStop=/usr/local/bin/led --stop
378 RemainAfterExit=true
379 StandardOutput=journal
380
381[Install]
382 WantedBy=multi-user.target
383EOF
384
Tristan Muntsinger34569112019-10-16 22:10:54 -0700385 echo "Creating SD duplicator script..."
386 cat > ${mntdir}/usr/local/bin/sd-dupe << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700387#!/bin/bash
388led 0
389
390src_dev=mmcblk0
391dest_dev=mmcblk1
392part_num=p5
393
394if [ -e /dev/mmcblk0p5 ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700395 led 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700396
Tristan Muntsinger34569112019-10-16 22:10:54 -0700397 sgdisk -Z -a1 /dev/${dest_dev}
398 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 /dev/${dest_dev}
399 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env /dev/${dest_dev}
400 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 /dev/${dest_dev}
401 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust /dev/${dest_dev}
402 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 -0700403
Tristan Muntsinger34569112019-10-16 22:10:54 -0700404 src_block_count=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block count:" | sed 's/.*: *//'`
405 src_block_size=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block size:" | sed 's/.*: *//'`
406 src_fs_size=$(( src_block_count*src_block_size ))
407 src_fs_size_m=$(( src_fs_size / 1024 / 1024 + 1 ))
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700408
Tristan Muntsinger34569112019-10-16 22:10:54 -0700409 dd if=/dev/${src_dev}p1 of=/dev/${dest_dev}p1 conv=sync,noerror status=progress
410 dd if=/dev/${src_dev}p2 of=/dev/${dest_dev}p2 conv=sync,noerror status=progress
411 dd if=/dev/${src_dev}p3 of=/dev/${dest_dev}p3 conv=sync,noerror status=progress
412 dd if=/dev/${src_dev}p4 of=/dev/${dest_dev}p4 conv=sync,noerror status=progress
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700413
Tristan Muntsinger34569112019-10-16 22:10:54 -0700414 echo "Writing ${src_fs_size_m} MB: /dev/${src_dev} -> /dev/${dest_dev}..."
415 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 -0700416
Tristan Muntsinger34569112019-10-16 22:10:54 -0700417 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
418 e2fsck -fy /dev/${dest_dev}${part_num}
419 resize2fs /dev/${dest_dev}${part_num}
420 tune2fs -O has_journal /dev/${dest_dev}${part_num}
421 e2fsck -fy /dev/${dest_dev}${part_num}
422 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700423
Tristan Muntsinger34569112019-10-16 22:10:54 -0700424 echo "Cleaning up..."
425 mount /dev/${dest_dev}${part_num} /media
426 chroot /media /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700427
Tristan Muntsinger34569112019-10-16 22:10:54 -0700428 if [ $? == 0 ]; then
429 echo "Successfully copied Rock Pi image!"
430 while true; do
431 led 1; sleep 0.5
432 led 0; sleep 0.5
433 done
434 else
435 echo "Error while copying Rock Pi image"
436 while true; do
437 led 1; sleep 0.1
438 led 0; sleep 0.1
439 done
440 fi
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700441else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700442 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
443 e2fsck -fy /dev/${dest_dev}${part_num}
444 resize2fs /dev/${dest_dev}${part_num}
445 tune2fs -O has_journal /dev/${dest_dev}${part_num}
446 e2fsck -fy /dev/${dest_dev}${part_num}
447 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700448
Tristan Muntsinger34569112019-10-16 22:10:54 -0700449 echo "Cleaning up..."
450 /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700451fi
452EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700453 chmod +x ${mntdir}/usr/local/bin/sd-dupe
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700454
Tristan Muntsinger34569112019-10-16 22:10:54 -0700455 echo "Creating SD duplicator service..."
456 cat > ${mntdir}/etc/systemd/system/sd-dupe.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700457[Unit]
458 Description=Duplicate SD card rootfs to eMMC on Rock Pi
459 ConditionPathExists=/usr/local/bin/sd-dupe
460 After=led.service
461
462[Service]
463 Type=simple
464 ExecStart=/usr/local/bin/sd-dupe
465 TimeoutSec=0
466 StandardOutput=tty
467
468[Install]
469 WantedBy=multi-user.target
470EOF
471
Tristan Muntsinger34569112019-10-16 22:10:54 -0700472 echo "Creating cleanup script..."
473 cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700474#!/bin/bash
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700475echo "Installing cuttlefish-common package..."
476echo "nameserver 8.8.8.8" > /etc/resolv.conf
Tristan Muntsinger052d1ff2019-11-19 16:43:43 -0800477MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/' | tr -d :`
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700478sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts
479sudo hostnamectl set-hostname "rockpi-${MAC}"
480
481dpkg --add-architecture amd64
482until ping -c1 ftp.debian.org; do sleep 1; done
483ntpdate time.google.com
484while true; do
485 apt-get -o Acquire::Check-Valid-Until=false update
486 if [ $? != 0 ]; then sleep 1; continue; fi
487 apt-get install -y -f libc6:amd64 qemu-user-static
488 if [ $? != 0 ]; then sleep 1; continue; fi
489 break
490done
491cd /home/vsoc-01/android-cuttlefish
492dpkg-buildpackage -d -uc -us
493apt-get install -y -f ../cuttlefish-common_*_arm64.deb
494apt-get clean
495usermod -aG cvdnetwork vsoc-01
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700496chmod 660 /dev/vhost-vsock
497chown root:cvdnetwork /dev/vhost-vsock
498
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700499rm /etc/machine-id
500rm /var/lib/dbus/machine-id
501dbus-uuidgen --ensure
502systemd-machine-id-setup
503
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700504systemctl disable sd-dupe
505rm /etc/systemd/system/sd-dupe.service
506rm /usr/local/bin/sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700507rm /usr/local/bin/install-cleanup
508EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700509 chmod +x ${mntdir}/usr/local/bin/install-cleanup
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700510
Tristan Muntsinger34569112019-10-16 22:10:54 -0700511 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700512echo "Enabling services..."
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700513systemctl enable led
514systemctl enable sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700515
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700516echo "Creating Initial Ramdisk..."
517update-initramfs -c -t -k "5.2.0"
518mkimage -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
519ln -s /boot/uInitrd-5.2.0 /boot/uInitrd
520EOT
521
Tristan Muntsinger34569112019-10-16 22:10:54 -0700522 umount ${mntdir}/sys
523 umount ${mntdir}/dev
524 umount ${mntdir}/proc
525 umount ${mntdir}
526
527 # Turn on journaling
528 tune2fs -O ^has_journal ${IMAGE}
529 e2fsck -fy ${IMAGE} >/dev/null 2>&1
530fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700531
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700532if [ ${USE_IMAGE} -eq 0 ]; then
533 # 32GB eMMC size
Tristan Muntsinger34569112019-10-16 22:10:54 -0700534 end_sector=61071326
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700535 device=/dev/${mmc_dev}
536 devicep=${device}
537
538 sgdisk -Z -a1 ${device}
539 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${device}
540 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${device}
541 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${device}
542 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${device}
Tristan Muntsinger34569112019-10-16 22:10:54 -0700543 sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${device}
544 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
545 dd if=${IMAGE} of=${devicep}5 bs=1M
546 resize2fs ${devicep}5 >/dev/null 2>&1
547 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700548else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700549 device=$(losetup -f)
550 devicep=${device}p
551 if [ ${FLAGS_p5} -eq ${FLAGS_FALSE} ]; then
552 fs_end=3G
553 end_sector=-
554 fi
555 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
556 # Minimize rootfs filesystem
557 while true; do
558 out=`sudo resize2fs -M ${IMAGE} 2>&1`
559 if [[ $out =~ "Nothing to do" ]]; then
560 break
561 fi
562 done
563 # Minimize rootfs file size
564 block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'`
565 block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'`
566 sector_size=512
567 start_sector=32768
568 fs_size=$(( block_count*block_size ))
569 fs_sectors=$(( fs_size/sector_size ))
570 part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned
571 end_sector=$(( start_sector+part_sectors-1 ))
572 secondary_gpt_sectors=33
573 fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size ))
574 image_size=$(( part_sectors*sector_size ))
575 truncate -s ${image_size} ${IMAGE}
576 e2fsck -fy ${IMAGE} >/dev/null 2>&1
577 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700578
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700579 # Create final image
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -0700580 if [ $OVERWRITE -eq 1 ]; then
581 tmpimg=${OVERWRITE_IMAGE}
582 else
583 tmpimg=`mktemp`
584 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700585 truncate -s ${fs_end} ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700586
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700587 # Create GPT
588 sgdisk -Z -a1 ${tmpimg}
589 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg}
590 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg}
591 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg}
592 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg}
593 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 -0700594
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700595 losetup ${device} ${tmpimg}
596 partx -v --add ${device}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700597
Tristan Muntsinger34569112019-10-16 22:10:54 -0700598 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
599 dd if=${IMAGE} of=${devicep}5 bs=1M
600 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700601fi
Tristan Muntsinger34569112019-10-16 22:10:54 -0700602if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
603 dd if=${idbloader} of=${devicep}1
604fi
605if [ ${FLAGS_p2} -eq ${FLAGS_TRUE} ]; then
606 dd if=${bootenv} of=${devicep}2
607fi
608if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
609 dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3
610fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700611if [ ${USE_IMAGE} -eq 1 ]; then
612 chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -0700613 if [ $OVERWRITE -eq 0 ]; then
614 mv ${tmpimg} ${IMAGE}
615 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700616 partx -v --delete ${device}
617 losetup -d ${device}
618fi