blob: b0671f2b89c92efc37604d00ac39989d0f85abe0 [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 Muntsinger44f08172019-10-23 23:40:11 -0700132ethaddr=00:00:00:00:00:00
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700133bootdelay=2
134baudrate=1500000
135scriptaddr=0x00500000
136boot_targets=mmc1 mmc0
137bootcmd=run distro_bootcmd
138distro_bootcmd=for target in ${boot_targets}; do run bootcmd_${target}; done
139bootcmd_mmc0=devnum=0; run mmc_boot
140bootcmd_mmc1=devnum=1; run mmc_boot
141mmc_boot=if mmc dev ${devnum}; then ; run scan_for_boot_part; fi
Tristan Muntsingerf06455a2019-10-31 16:08:19 -0700142scan_for_boot_part=part list mmc ${devnum} -bootable devplist; env exists devplist || setenv devplist 1; if test $devnum = 1; then script_type=init; else script_type=boot; fi; for distro_bootpart in ${devplist}; do if fstype mmc ${devnum}:${distro_bootpart} bootfstype; then run find_script; fi; done; setenv devplist; setenv script_type;
143find_script=if test -e mmc ${devnum}:${distro_bootpart} /boot/$script_type.scr; then echo Found U-Boot script /boot/$script_type.scr; run run_scr; fi
144run_scr=load mmc ${devnum}:${distro_bootpart} ${scriptaddr} /boot/$script_type.scr; source ${scriptaddr}
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700145EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700146 ${ANDROID_HOST_OUT}/bin/mkenvimage -s 32768 -o ${bootenv} - < ${tmpfile}
147fi
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700148
Tristan Muntsinger34569112019-10-16 22:10:54 -0700149if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ] || [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
150 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig
151 if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
152 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
153 fi
154 if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
155 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- u-boot.itb
156 fi
157 if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
158 idbloader=`mktemp`
159 ${ANDROID_HOST_OUT}/bin/mkimage -n rk3399 -T rksd -d tpl/u-boot-tpl.bin ${idbloader}
160 cat spl/u-boot-spl.bin >> ${idbloader}
161 fi
162fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700163cd -
164
Tristan Muntsinger34569112019-10-16 22:10:54 -0700165if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
166 ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE}
167 if [ $? -ne 0 ]; then
168 echo "error: failed to build rootfs. exiting..."
169 exit 1
170 fi
171 truncate -s +3G ${IMAGE}
172 e2fsck -f ${IMAGE}
173 resize2fs ${IMAGE}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700174
Tristan Muntsinger34569112019-10-16 22:10:54 -0700175 mntdir=`mktemp -d`
176 mount ${IMAGE} ${mntdir}
177 if [ $? != 0 ]; then
178 echo "error: unable to mount ${IMAGE} ${mntdir}"
179 exit 1
180 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700181
Tristan Muntsinger34569112019-10-16 22:10:54 -0700182 cat > ${mntdir}/boot/init.cmd << "EOF"
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700183mmc dev 1 0; mmc read 0x02080000 0x1fc0 0x40;
184ethaddr=$ethaddr
185env default -a
186setenv ethaddr $ethaddr
187setenv boot_targets 'mmc1 mmc0 usb0 pxe'
188saveenv
189mmc dev 1 0; mmc read 0x04000000 0x1fc0 0x40;
190mmc dev 0 0; mmc write 0x04000000 0x1fc0 0x40;
191mmc dev 1 0; mmc write 0x02080000 0x1fc0 0x40;
192EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700193 ${ANDROID_BUILD_TOP}/external/u-boot/tools/mkimage \
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700194 -C none -A arm -T script -d ${mntdir}/boot/init.cmd ${mntdir}/boot/init.scr
195
Tristan Muntsinger34569112019-10-16 22:10:54 -0700196 cat > ${mntdir}/boot/boot.cmd << "EOF"
Tristan Muntsingerbcdc07b2019-10-16 22:56:19 -0700197setenv bootcmd_dhcp '
198mw.b ${scriptaddr} 0 0x8000
199mmc dev 0 0
200mmc read ${scriptaddr} 0x1fc0 0x40
201env import -b ${scriptaddr} 0x8000
202mw.b ${scriptaddr} 0 0x8000
203if dhcp ${scriptaddr} manifest.txt; then
204 setenv OldSha ${Sha}
205 setenv Sha
206 env import -t ${scriptaddr} 0x8000 ManifestVersion
207 if test "$ManifestVersion" = "1"; then
208 run manifest1
209 else
210 run manifestX
211 fi
212fi'
213setenv manifestX 'echo "***** ERROR: Unknown manifest version! *****";'
214setenv manifest1 '
215echo "Manifest version 1";
216env import -t ${scriptaddr} 0x8000
217if test "$Sha" != "$OldSha"; then
218 setenv serverip ${TftpServer}
219 setenv loadaddr 0x00200000
220 mmc dev 0 0;
221 file=$TplSplImg; offset=0x40; size=0x1f80; run tftpget1; setenv TplSplImg
222 file=$UbootItb; offset=0x4000; size=0x2000; run tftpget1; setenv UbootItb
223 file=$TrustImg; offset=0x6000; size=0x2000; run tftpget1; setenv TrustImg
224 file=$RootfsImg; offset=0x8000; size=0; run tftpget1; setenv RootfsImg
225 file=$UbootEnv; offset=0x1fc0; size=0x40; run tftpget1; setenv UbootEnv
226 mw.b ${scriptaddr} 0 0x8000
227 env export -b ${scriptaddr} 0x8000
228 mmc write ${scriptaddr} 0x1fc0 0x40
229else
230 echo "Already have ${Sha}. Booting..."
231fi'
232setenv tftpget1 "
233mw.b ${loadaddr} 0 0x400000
234&& tftp ${file}
235&& isGz=0 && setexpr isGz sub .*\\.gz\$ 1 ${file}
236&& if test $isGz = 1; then
237 setexpr boffset ${offset} * 0x200
238 && gzwrite mmc 0 ${loadaddr} 0x${filesize} 100000 ${boffset}
239 && echo Updated: ${bootfile}
240elif test ${file} = boot.env; then
241 env import -b ${loadaddr}
242 && echo Updated: boot.env
243else
244 && if test $size = 0; then
245 setexpr x $filesize - 1
246 && setexpr x $x / 0x1000
247 && setexpr x $x + 1
248 && setexpr x $x * 0x1000
249 && setexpr x $x / 0x200
250 && size=0x${x}
251 fi
252 && mmc write ${loadaddr} ${offset} ${size}
253 && echo Updated: ${bootfile}
254fi
255|| echo ** UPDATE FAILED: ${bootfile} **"
256if mmc dev 1 0; then; else
257 run bootcmd_dhcp;
258fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700259load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image
260load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd
261load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb
262setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid"
263run finduuid
264setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000"
265booti 0x02080000 0x04000000 0x01f00000
266EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700267 ${ANDROID_HOST_OUT}/bin/mkimage \
268 -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700269
Tristan Muntsinger34569112019-10-16 22:10:54 -0700270 cd ${KERNEL_DIR}
271 export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH
272 export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH
273 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
274 CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig
275 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
276 CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc`
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700277
Tristan Muntsinger34569112019-10-16 22:10:54 -0700278 cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/
279 mkdir -p ${mntdir}/boot/dtb/rockchip/
280 cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/
281 cd -
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700282
Tristan Muntsinger34569112019-10-16 22:10:54 -0700283 mount -o bind /proc ${mntdir}/proc
284 mount -o bind /sys ${mntdir}/sys
285 mount -o bind /dev ${mntdir}/dev
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700286
Tristan Muntsinger34569112019-10-16 22:10:54 -0700287 echo "Installing required packages..."
288 chroot ${mntdir} /bin/bash <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700289apt-get update
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700290apt-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 -0700291EOF
292
Tristan Muntsinger34569112019-10-16 22:10:54 -0700293 echo "Turning on DHCP client..."
294 cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700295[Match]
296Name=en*
297
298[Network]
299DHCP=yes
300EOF
301
Tristan Muntsinger34569112019-10-16 22:10:54 -0700302 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700303echo "Adding user vsoc-01 and groups..."
304useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01
305echo -e "cuttlefish\ncuttlefish" | passwd
306echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01
307EOT
308
Tristan Muntsinger34569112019-10-16 22:10:54 -0700309 echo "Cloning android-cuttlefish..."
310 cd ${mntdir}/home/vsoc-01
311 git clone https://github.com/google/android-cuttlefish.git
312 cd -
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700313
Tristan Muntsinger34569112019-10-16 22:10:54 -0700314 echo "Creating led script..."
315 cat > ${mntdir}/usr/local/bin/led << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700316#!/bin/bash
317
318if [ "$1" == "--start" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700319 echo 125 > /sys/class/gpio/export
320 echo out > /sys/class/gpio/gpio125/direction
321 chmod 666 /sys/class/gpio/gpio125/value
322 echo 0 > /sys/class/gpio/gpio125/value
323 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700324fi
325
326if [ "$1" == "--stop" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700327 echo 0 > /sys/class/gpio/gpio125/value
328 echo 125 > /sys/class/gpio/unexport
329 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700330fi
331
332if [ ! -e /sys/class/gpio/gpio125/value ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700333 echo "error: led service not initialized"
334 exit 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700335fi
336
337if [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700338 echo 0 > /sys/class/gpio/gpio125/value
339 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700340fi
341
342if [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700343 echo 1 > /sys/class/gpio/gpio125/value
344 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700345fi
346
347echo "usage: led <0|1>"
348exit 1
349EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700350 chown root:root ${mntdir}/usr/local/bin/led
351 chmod 755 ${mntdir}/usr/local/bin/led
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700352
Tristan Muntsinger34569112019-10-16 22:10:54 -0700353 echo "Creating led service..."
354 cat > ${mntdir}/etc/systemd/system/led.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700355[Unit]
356 Description=led service
357 ConditionPathExists=/usr/local/bin/led
358
359[Service]
360 Type=oneshot
361 ExecStart=/usr/local/bin/led --start
362 ExecStop=/usr/local/bin/led --stop
363 RemainAfterExit=true
364 StandardOutput=journal
365
366[Install]
367 WantedBy=multi-user.target
368EOF
369
Tristan Muntsinger34569112019-10-16 22:10:54 -0700370 echo "Creating SD duplicator script..."
371 cat > ${mntdir}/usr/local/bin/sd-dupe << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700372#!/bin/bash
373led 0
374
375src_dev=mmcblk0
376dest_dev=mmcblk1
377part_num=p5
378
379if [ -e /dev/mmcblk0p5 ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700380 led 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700381
Tristan Muntsinger34569112019-10-16 22:10:54 -0700382 sgdisk -Z -a1 /dev/${dest_dev}
383 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 /dev/${dest_dev}
384 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env /dev/${dest_dev}
385 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 /dev/${dest_dev}
386 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust /dev/${dest_dev}
387 sgdisk -a1 -n:5:32768:- -A:5:set:2 -t:5:8305 -c:5:rootfs /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700388
Tristan Muntsinger34569112019-10-16 22:10:54 -0700389 src_block_count=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block count:" | sed 's/.*: *//'`
390 src_block_size=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block size:" | sed 's/.*: *//'`
391 src_fs_size=$(( src_block_count*src_block_size ))
392 src_fs_size_m=$(( src_fs_size / 1024 / 1024 + 1 ))
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700393
Tristan Muntsinger34569112019-10-16 22:10:54 -0700394 dd if=/dev/${src_dev}p1 of=/dev/${dest_dev}p1 conv=sync,noerror status=progress
395 dd if=/dev/${src_dev}p2 of=/dev/${dest_dev}p2 conv=sync,noerror status=progress
396 dd if=/dev/${src_dev}p3 of=/dev/${dest_dev}p3 conv=sync,noerror status=progress
397 dd if=/dev/${src_dev}p4 of=/dev/${dest_dev}p4 conv=sync,noerror status=progress
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700398
Tristan Muntsinger34569112019-10-16 22:10:54 -0700399 echo "Writing ${src_fs_size_m} MB: /dev/${src_dev} -> /dev/${dest_dev}..."
400 dd if=/dev/${src_dev}${part_num} of=/dev/${dest_dev}${part_num} bs=1M conv=sync,noerror status=progress
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700401
Tristan Muntsinger34569112019-10-16 22:10:54 -0700402 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
403 e2fsck -fy /dev/${dest_dev}${part_num}
404 resize2fs /dev/${dest_dev}${part_num}
405 tune2fs -O has_journal /dev/${dest_dev}${part_num}
406 e2fsck -fy /dev/${dest_dev}${part_num}
407 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700408
Tristan Muntsinger34569112019-10-16 22:10:54 -0700409 echo "Cleaning up..."
410 mount /dev/${dest_dev}${part_num} /media
411 chroot /media /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700412
Tristan Muntsinger34569112019-10-16 22:10:54 -0700413 if [ $? == 0 ]; then
414 echo "Successfully copied Rock Pi image!"
415 while true; do
416 led 1; sleep 0.5
417 led 0; sleep 0.5
418 done
419 else
420 echo "Error while copying Rock Pi image"
421 while true; do
422 led 1; sleep 0.1
423 led 0; sleep 0.1
424 done
425 fi
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700426else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700427 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
428 e2fsck -fy /dev/${dest_dev}${part_num}
429 resize2fs /dev/${dest_dev}${part_num}
430 tune2fs -O has_journal /dev/${dest_dev}${part_num}
431 e2fsck -fy /dev/${dest_dev}${part_num}
432 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700433
Tristan Muntsinger34569112019-10-16 22:10:54 -0700434 echo "Cleaning up..."
435 /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700436fi
437EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700438 chmod +x ${mntdir}/usr/local/bin/sd-dupe
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700439
Tristan Muntsinger34569112019-10-16 22:10:54 -0700440 echo "Creating SD duplicator service..."
441 cat > ${mntdir}/etc/systemd/system/sd-dupe.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700442[Unit]
443 Description=Duplicate SD card rootfs to eMMC on Rock Pi
444 ConditionPathExists=/usr/local/bin/sd-dupe
445 After=led.service
446
447[Service]
448 Type=simple
449 ExecStart=/usr/local/bin/sd-dupe
450 TimeoutSec=0
451 StandardOutput=tty
452
453[Install]
454 WantedBy=multi-user.target
455EOF
456
Tristan Muntsinger34569112019-10-16 22:10:54 -0700457 echo "Creating cleanup script..."
458 cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700459#!/bin/bash
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700460echo "Installing cuttlefish-common package..."
461echo "nameserver 8.8.8.8" > /etc/resolv.conf
462MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/'`
463sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts
464sudo hostnamectl set-hostname "rockpi-${MAC}"
465
466dpkg --add-architecture amd64
467until ping -c1 ftp.debian.org; do sleep 1; done
468ntpdate time.google.com
469while true; do
470 apt-get -o Acquire::Check-Valid-Until=false update
471 if [ $? != 0 ]; then sleep 1; continue; fi
472 apt-get install -y -f libc6:amd64 qemu-user-static
473 if [ $? != 0 ]; then sleep 1; continue; fi
474 break
475done
476cd /home/vsoc-01/android-cuttlefish
477dpkg-buildpackage -d -uc -us
478apt-get install -y -f ../cuttlefish-common_*_arm64.deb
479apt-get clean
480usermod -aG cvdnetwork vsoc-01
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700481chmod 660 /dev/vhost-vsock
482chown root:cvdnetwork /dev/vhost-vsock
483
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700484rm /etc/machine-id
485rm /var/lib/dbus/machine-id
486dbus-uuidgen --ensure
487systemd-machine-id-setup
488
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700489systemctl disable sd-dupe
490rm /etc/systemd/system/sd-dupe.service
491rm /usr/local/bin/sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700492rm /usr/local/bin/install-cleanup
493EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700494 chmod +x ${mntdir}/usr/local/bin/install-cleanup
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700495
Tristan Muntsinger34569112019-10-16 22:10:54 -0700496 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700497echo "Enabling services..."
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700498systemctl enable led
499systemctl enable sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700500
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700501echo "Creating Initial Ramdisk..."
502update-initramfs -c -t -k "5.2.0"
503mkimage -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
504ln -s /boot/uInitrd-5.2.0 /boot/uInitrd
505EOT
506
Tristan Muntsinger34569112019-10-16 22:10:54 -0700507 umount ${mntdir}/sys
508 umount ${mntdir}/dev
509 umount ${mntdir}/proc
510 umount ${mntdir}
511
512 # Turn on journaling
513 tune2fs -O ^has_journal ${IMAGE}
514 e2fsck -fy ${IMAGE} >/dev/null 2>&1
515fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700516
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700517if [ ${USE_IMAGE} -eq 0 ]; then
518 # 32GB eMMC size
Tristan Muntsinger34569112019-10-16 22:10:54 -0700519 end_sector=61071326
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700520 device=/dev/${mmc_dev}
521 devicep=${device}
522
523 sgdisk -Z -a1 ${device}
524 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${device}
525 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${device}
526 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${device}
527 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${device}
Tristan Muntsinger34569112019-10-16 22:10:54 -0700528 sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${device}
529 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
530 dd if=${IMAGE} of=${devicep}5 bs=1M
531 resize2fs ${devicep}5 >/dev/null 2>&1
532 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700533else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700534 device=$(losetup -f)
535 devicep=${device}p
536 if [ ${FLAGS_p5} -eq ${FLAGS_FALSE} ]; then
537 fs_end=3G
538 end_sector=-
539 fi
540 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
541 # Minimize rootfs filesystem
542 while true; do
543 out=`sudo resize2fs -M ${IMAGE} 2>&1`
544 if [[ $out =~ "Nothing to do" ]]; then
545 break
546 fi
547 done
548 # Minimize rootfs file size
549 block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'`
550 block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'`
551 sector_size=512
552 start_sector=32768
553 fs_size=$(( block_count*block_size ))
554 fs_sectors=$(( fs_size/sector_size ))
555 part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned
556 end_sector=$(( start_sector+part_sectors-1 ))
557 secondary_gpt_sectors=33
558 fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size ))
559 image_size=$(( part_sectors*sector_size ))
560 truncate -s ${image_size} ${IMAGE}
561 e2fsck -fy ${IMAGE} >/dev/null 2>&1
562 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700563
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700564 # Create final image
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -0700565 if [ $OVERWRITE -eq 1 ]; then
566 tmpimg=${OVERWRITE_IMAGE}
567 else
568 tmpimg=`mktemp`
569 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700570 truncate -s ${fs_end} ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700571
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700572 # Create GPT
573 sgdisk -Z -a1 ${tmpimg}
574 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg}
575 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg}
576 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg}
577 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg}
578 sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700579
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700580 losetup ${device} ${tmpimg}
581 partx -v --add ${device}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700582
Tristan Muntsinger34569112019-10-16 22:10:54 -0700583 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
584 dd if=${IMAGE} of=${devicep}5 bs=1M
585 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700586fi
Tristan Muntsinger34569112019-10-16 22:10:54 -0700587if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
588 dd if=${idbloader} of=${devicep}1
589fi
590if [ ${FLAGS_p2} -eq ${FLAGS_TRUE} ]; then
591 dd if=${bootenv} of=${devicep}2
592fi
593if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
594 dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3
595fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700596if [ ${USE_IMAGE} -eq 1 ]; then
597 chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
Tristan Muntsinger1f28d1f2019-10-16 22:53:55 -0700598 if [ $OVERWRITE -eq 0 ]; then
599 mv ${tmpimg} ${IMAGE}
600 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700601 partx -v --delete ${device}
602 losetup -d ${device}
603fi