blob: bae7a04bcef5edcb14e2cecea36f613b00f4ff4e [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 Muntsinger82c10502019-10-15 14:49:19 -070059if [ -z $KERNEL_DIR ] || [ -z $IMAGE ]; then
60 flags_help
61 exit 1
62fi
63if [ -e "${IMAGE}" ]; then
64 echo "error: ${IMAGE} already exists"
65 exit 1
66fi
67if [ ! -e "${KERNEL_DIR}" ]; then
68 echo "error: can't find '${KERNEL_DIR}'. aborting..."
69 exit 1
70fi
71
72# escalate to superuser
73if [ $UID -ne 0 ]; then
74 cd ${ANDROID_BUILD_TOP}
75 . ./build/envsetup.sh
76 lunch ${TARGET_PRODUCT}-${TARGET_BUILD_VARIANT}
77 mmma external/u-boot
78 cd -
79 exec sudo -E "${0}" ${@}
80fi
81
Tristan Muntsingere4eeded2019-10-15 20:26:15 -070082if [ $USE_IMAGE -eq 0 ]; then
83 init_devs=`lsblk --nodeps -oNAME -n`
84 echo "Reinsert device (to write to) into PC"
85 while true; do
86 devs=`lsblk --nodeps -oNAME -n`
87 new_devs="$(echo -e "${init_devs}\n${devs}" | sort | uniq -u | awk 'NF')"
88 num_devs=`echo "${new_devs}" | wc -l`
89 if [[ "${new_devs}" == "" ]]; then
90 num_devs=0
91 fi
92 if [[ ${num_devs} -gt 1 ]]; then
93 echo "error: too many new devices detected! aborting..."
94 exit 1
95 fi
96 if [[ ${num_devs} -eq 1 ]]; then
97 if [[ "${new_devs}" != "${mmc_dev}" ]]; then
98 if [[ "${mmc_dev}" != "" ]]; then
99 echo "error: block device name mismatch ${new_devs} != ${mmc_dev}"
100 echo "Reinsert device (to write to) into PC"
101 fi
102 mmc_dev=${new_devs}
103 continue
104 fi
105 echo "${init_devs}" | grep "${mmc_dev}" >/dev/null
106 if [[ $? -eq 0 ]]; then
107 init_devs="${devs}"
108 continue
109 fi
110 break
111 fi
112 done
113 # now inform the user
114 echo "Detected device at /dev/${mmc_dev}"
115fi
116
Tristan Muntsinger34569112019-10-16 22:10:54 -0700117if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
118 cd ${ANDROID_BUILD_TOP}/external/arm-trusted-firmware
119 CROSS_COMPILE=aarch64-linux-gnu- make PLAT=rk3399 DEBUG=0 ERROR_DEPRECATED=1 bl31
120 export BL31="${ANDROID_BUILD_TOP}/external/arm-trusted-firmware/build/rk3399/release/bl31/bl31.elf"
121 cd -
122fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700123
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700124cd ${ANDROID_BUILD_TOP}/external/u-boot
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700125
Tristan Muntsinger34569112019-10-16 22:10:54 -0700126if [ ${FLAGS_p2} -eq ${FLAGS_TRUE} ]; then
127 tmpfile=`mktemp`
128 bootenv=`mktemp`
129 cat > ${tmpfile} << "EOF"
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700130bootdelay=2
131baudrate=1500000
132scriptaddr=0x00500000
133boot_targets=mmc1 mmc0
134bootcmd=run distro_bootcmd
135distro_bootcmd=for target in ${boot_targets}; do run bootcmd_${target}; done
136bootcmd_mmc0=devnum=0; run mmc_boot
137bootcmd_mmc1=devnum=1; run mmc_boot
138mmc_boot=if mmc dev ${devnum}; then ; run scan_for_boot_part; fi
139scan_for_boot_part=part list mmc ${devnum} -bootable devplist; env exists devplist || setenv devplist 1; for part in ${devplist}; do if fstype mmc ${devnum}:${part} bootfstype; then run find_init_script; fi; done; setenv devplist
140find_init_script=if test -e mmc ${devnum}:${part} /boot/init.scr; then echo Found U-Boot script /boot/init.scr; run init_scr; fi
141init_scr=load mmc ${devnum}:${part} ${scriptaddr} /boot/init.scr; source ${scriptaddr}
142EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700143 ${ANDROID_HOST_OUT}/bin/mkenvimage -s 32768 -o ${bootenv} - < ${tmpfile}
144fi
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700145
Tristan Muntsinger34569112019-10-16 22:10:54 -0700146if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ] || [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
147 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rock-pi-4-rk3399_defconfig
148 if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
149 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- -j`nproc`
150 fi
151 if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
152 make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- u-boot.itb
153 fi
154 if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
155 idbloader=`mktemp`
156 ${ANDROID_HOST_OUT}/bin/mkimage -n rk3399 -T rksd -d tpl/u-boot-tpl.bin ${idbloader}
157 cat spl/u-boot-spl.bin >> ${idbloader}
158 fi
159fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700160cd -
161
Tristan Muntsinger34569112019-10-16 22:10:54 -0700162if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
163 ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh -a arm64 -s buster -n ${IMAGE}
164 if [ $? -ne 0 ]; then
165 echo "error: failed to build rootfs. exiting..."
166 exit 1
167 fi
168 truncate -s +3G ${IMAGE}
169 e2fsck -f ${IMAGE}
170 resize2fs ${IMAGE}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700171
Tristan Muntsinger34569112019-10-16 22:10:54 -0700172 mntdir=`mktemp -d`
173 mount ${IMAGE} ${mntdir}
174 if [ $? != 0 ]; then
175 echo "error: unable to mount ${IMAGE} ${mntdir}"
176 exit 1
177 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700178
Tristan Muntsinger34569112019-10-16 22:10:54 -0700179 cat > ${mntdir}/boot/init.cmd << "EOF"
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700180mmc dev 1 0; mmc read 0x02080000 0x1fc0 0x40;
181ethaddr=$ethaddr
182env default -a
183setenv ethaddr $ethaddr
184setenv boot_targets 'mmc1 mmc0 usb0 pxe'
185saveenv
186mmc dev 1 0; mmc read 0x04000000 0x1fc0 0x40;
187mmc dev 0 0; mmc write 0x04000000 0x1fc0 0x40;
188mmc dev 1 0; mmc write 0x02080000 0x1fc0 0x40;
189EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700190 ${ANDROID_BUILD_TOP}/external/u-boot/tools/mkimage \
Tristan Muntsinger5f263952019-10-16 21:12:06 -0700191 -C none -A arm -T script -d ${mntdir}/boot/init.cmd ${mntdir}/boot/init.scr
192
Tristan Muntsinger34569112019-10-16 22:10:54 -0700193 cat > ${mntdir}/boot/boot.cmd << "EOF"
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700194load mmc ${devnum}:${distro_bootpart} 0x02080000 /boot/Image
195load mmc ${devnum}:${distro_bootpart} 0x04000000 /boot/uInitrd
196load mmc ${devnum}:${distro_bootpart} 0x01f00000 /boot/dtb/rockchip/rk3399-rock-pi-4.dtb
197setenv finduuid "part uuid mmc ${devnum}:${distro_bootpart} uuid"
198run finduuid
199setenv bootargs "earlycon=uart8250,mmio32,0xff1a0000 console=ttyS2,1500000n8 loglevel=7 root=PARTUUID=${uuid} rootwait rootfstype=ext4 sdhci.debug_quirks=0x20000000"
200booti 0x02080000 0x04000000 0x01f00000
201EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700202 ${ANDROID_HOST_OUT}/bin/mkimage \
203 -C none -A arm -T script -d ${mntdir}/boot/boot.cmd ${mntdir}/boot/boot.scr
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700204
Tristan Muntsinger34569112019-10-16 22:10:54 -0700205 cd ${KERNEL_DIR}
206 export PATH=${ANDROID_BUILD_TOP}/prebuilts/clang/host/linux-x86/clang-r353983c/bin:$PATH
207 export PATH=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin:$PATH
208 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
209 CLANG_TRIPLE=aarch64-linux-gnu- rockpi4_defconfig
210 make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-androidkernel- \
211 CLANG_TRIPLE=aarch64-linux-gnu- -j`nproc`
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700212
Tristan Muntsinger34569112019-10-16 22:10:54 -0700213 cp ${KERNEL_DIR}/arch/arm64/boot/Image ${mntdir}/boot/
214 mkdir -p ${mntdir}/boot/dtb/rockchip/
215 cp ${KERNEL_DIR}/arch/arm64/boot/dts/rockchip/rk3399-rock-pi-4.dtb ${mntdir}/boot/dtb/rockchip/
216 cd -
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700217
Tristan Muntsinger34569112019-10-16 22:10:54 -0700218 mount -o bind /proc ${mntdir}/proc
219 mount -o bind /sys ${mntdir}/sys
220 mount -o bind /dev ${mntdir}/dev
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700221
Tristan Muntsinger34569112019-10-16 22:10:54 -0700222 echo "Installing required packages..."
223 chroot ${mntdir} /bin/bash <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700224apt-get update
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700225apt-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 -0700226EOF
227
Tristan Muntsinger34569112019-10-16 22:10:54 -0700228 echo "Turning on DHCP client..."
229 cat >${mntdir}/etc/systemd/network/dhcp.network <<EOF
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700230[Match]
231Name=en*
232
233[Network]
234DHCP=yes
235EOF
236
Tristan Muntsinger34569112019-10-16 22:10:54 -0700237 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700238echo "Adding user vsoc-01 and groups..."
239useradd -m -G kvm,sudo -d /home/vsoc-01 --shell /bin/bash vsoc-01
240echo -e "cuttlefish\ncuttlefish" | passwd
241echo -e "cuttlefish\ncuttlefish" | passwd vsoc-01
242EOT
243
Tristan Muntsinger34569112019-10-16 22:10:54 -0700244 echo "Cloning android-cuttlefish..."
245 cd ${mntdir}/home/vsoc-01
246 git clone https://github.com/google/android-cuttlefish.git
247 cd -
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700248
Tristan Muntsinger34569112019-10-16 22:10:54 -0700249 echo "Creating led script..."
250 cat > ${mntdir}/usr/local/bin/led << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700251#!/bin/bash
252
253if [ "$1" == "--start" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700254 echo 125 > /sys/class/gpio/export
255 echo out > /sys/class/gpio/gpio125/direction
256 chmod 666 /sys/class/gpio/gpio125/value
257 echo 0 > /sys/class/gpio/gpio125/value
258 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700259fi
260
261if [ "$1" == "--stop" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700262 echo 0 > /sys/class/gpio/gpio125/value
263 echo 125 > /sys/class/gpio/unexport
264 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700265fi
266
267if [ ! -e /sys/class/gpio/gpio125/value ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700268 echo "error: led service not initialized"
269 exit 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700270fi
271
272if [ "$1" == "0" ] || [ "$1" == "off" ] || [ "$1" == "OFF" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700273 echo 0 > /sys/class/gpio/gpio125/value
274 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700275fi
276
277if [ "$1" == "1" ] || [ "$1" == "on" ] || [ "$1" == "ON" ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700278 echo 1 > /sys/class/gpio/gpio125/value
279 exit 0
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700280fi
281
282echo "usage: led <0|1>"
283exit 1
284EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700285 chown root:root ${mntdir}/usr/local/bin/led
286 chmod 755 ${mntdir}/usr/local/bin/led
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700287
Tristan Muntsinger34569112019-10-16 22:10:54 -0700288 echo "Creating led service..."
289 cat > ${mntdir}/etc/systemd/system/led.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700290[Unit]
291 Description=led service
292 ConditionPathExists=/usr/local/bin/led
293
294[Service]
295 Type=oneshot
296 ExecStart=/usr/local/bin/led --start
297 ExecStop=/usr/local/bin/led --stop
298 RemainAfterExit=true
299 StandardOutput=journal
300
301[Install]
302 WantedBy=multi-user.target
303EOF
304
Tristan Muntsinger34569112019-10-16 22:10:54 -0700305 echo "Creating SD duplicator script..."
306 cat > ${mntdir}/usr/local/bin/sd-dupe << "EOF"
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700307#!/bin/bash
308led 0
309
310src_dev=mmcblk0
311dest_dev=mmcblk1
312part_num=p5
313
314if [ -e /dev/mmcblk0p5 ]; then
Tristan Muntsinger34569112019-10-16 22:10:54 -0700315 led 1
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700316
Tristan Muntsinger34569112019-10-16 22:10:54 -0700317 sgdisk -Z -a1 /dev/${dest_dev}
318 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 /dev/${dest_dev}
319 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env /dev/${dest_dev}
320 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 /dev/${dest_dev}
321 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust /dev/${dest_dev}
322 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 -0700323
Tristan Muntsinger34569112019-10-16 22:10:54 -0700324 src_block_count=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block count:" | sed 's/.*: *//'`
325 src_block_size=`tune2fs -l /dev/${src_dev}${part_num} | grep "Block size:" | sed 's/.*: *//'`
326 src_fs_size=$(( src_block_count*src_block_size ))
327 src_fs_size_m=$(( src_fs_size / 1024 / 1024 + 1 ))
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700328
Tristan Muntsinger34569112019-10-16 22:10:54 -0700329 dd if=/dev/${src_dev}p1 of=/dev/${dest_dev}p1 conv=sync,noerror status=progress
330 dd if=/dev/${src_dev}p2 of=/dev/${dest_dev}p2 conv=sync,noerror status=progress
331 dd if=/dev/${src_dev}p3 of=/dev/${dest_dev}p3 conv=sync,noerror status=progress
332 dd if=/dev/${src_dev}p4 of=/dev/${dest_dev}p4 conv=sync,noerror status=progress
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700333
Tristan Muntsinger34569112019-10-16 22:10:54 -0700334 echo "Writing ${src_fs_size_m} MB: /dev/${src_dev} -> /dev/${dest_dev}..."
335 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 -0700336
Tristan Muntsinger34569112019-10-16 22:10:54 -0700337 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
338 e2fsck -fy /dev/${dest_dev}${part_num}
339 resize2fs /dev/${dest_dev}${part_num}
340 tune2fs -O has_journal /dev/${dest_dev}${part_num}
341 e2fsck -fy /dev/${dest_dev}${part_num}
342 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700343
Tristan Muntsinger34569112019-10-16 22:10:54 -0700344 echo "Cleaning up..."
345 mount /dev/${dest_dev}${part_num} /media
346 chroot /media /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700347
Tristan Muntsinger34569112019-10-16 22:10:54 -0700348 if [ $? == 0 ]; then
349 echo "Successfully copied Rock Pi image!"
350 while true; do
351 led 1; sleep 0.5
352 led 0; sleep 0.5
353 done
354 else
355 echo "Error while copying Rock Pi image"
356 while true; do
357 led 1; sleep 0.1
358 led 0; sleep 0.1
359 done
360 fi
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700361else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700362 echo "Expanding /dev/${dest_dev}${part_num} filesystem..."
363 e2fsck -fy /dev/${dest_dev}${part_num}
364 resize2fs /dev/${dest_dev}${part_num}
365 tune2fs -O has_journal /dev/${dest_dev}${part_num}
366 e2fsck -fy /dev/${dest_dev}${part_num}
367 sync /dev/${dest_dev}
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700368
Tristan Muntsinger34569112019-10-16 22:10:54 -0700369 echo "Cleaning up..."
370 /usr/local/bin/install-cleanup
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700371fi
372EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700373 chmod +x ${mntdir}/usr/local/bin/sd-dupe
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700374
Tristan Muntsinger34569112019-10-16 22:10:54 -0700375 echo "Creating SD duplicator service..."
376 cat > ${mntdir}/etc/systemd/system/sd-dupe.service << EOF
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700377[Unit]
378 Description=Duplicate SD card rootfs to eMMC on Rock Pi
379 ConditionPathExists=/usr/local/bin/sd-dupe
380 After=led.service
381
382[Service]
383 Type=simple
384 ExecStart=/usr/local/bin/sd-dupe
385 TimeoutSec=0
386 StandardOutput=tty
387
388[Install]
389 WantedBy=multi-user.target
390EOF
391
Tristan Muntsinger34569112019-10-16 22:10:54 -0700392 echo "Creating cleanup script..."
393 cat > ${mntdir}/usr/local/bin/install-cleanup << "EOF"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700394#!/bin/bash
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700395echo "Installing cuttlefish-common package..."
396echo "nameserver 8.8.8.8" > /etc/resolv.conf
397MAC=`ip link | grep eth0 -A1 | grep ether | sed 's/.*\(..:..:..:..:..:..\) .*/\1/'`
398sed -i " 1 s/.*/& rockpi-${MAC}/" /etc/hosts
399sudo hostnamectl set-hostname "rockpi-${MAC}"
400
401dpkg --add-architecture amd64
402until ping -c1 ftp.debian.org; do sleep 1; done
403ntpdate time.google.com
404while true; do
405 apt-get -o Acquire::Check-Valid-Until=false update
406 if [ $? != 0 ]; then sleep 1; continue; fi
407 apt-get install -y -f libc6:amd64 qemu-user-static
408 if [ $? != 0 ]; then sleep 1; continue; fi
409 break
410done
411cd /home/vsoc-01/android-cuttlefish
412dpkg-buildpackage -d -uc -us
413apt-get install -y -f ../cuttlefish-common_*_arm64.deb
414apt-get clean
415usermod -aG cvdnetwork vsoc-01
Tristan Muntsinger5ea7c6a2019-10-15 16:53:08 -0700416chmod 660 /dev/vhost-vsock
417chown root:cvdnetwork /dev/vhost-vsock
418
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700419rm /etc/machine-id
420rm /var/lib/dbus/machine-id
421dbus-uuidgen --ensure
422systemd-machine-id-setup
423
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700424systemctl disable sd-dupe
425rm /etc/systemd/system/sd-dupe.service
426rm /usr/local/bin/sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700427rm /usr/local/bin/install-cleanup
428EOF
Tristan Muntsinger34569112019-10-16 22:10:54 -0700429 chmod +x ${mntdir}/usr/local/bin/install-cleanup
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700430
Tristan Muntsinger34569112019-10-16 22:10:54 -0700431 chroot ${mntdir} /bin/bash << "EOT"
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700432echo "Enabling services..."
Tristan Muntsingerab89e8f2019-10-15 20:30:20 -0700433systemctl enable led
434systemctl enable sd-dupe
Tristan Muntsingerd32ee2b2019-10-15 15:07:00 -0700435
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700436echo "Creating Initial Ramdisk..."
437update-initramfs -c -t -k "5.2.0"
438mkimage -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
439ln -s /boot/uInitrd-5.2.0 /boot/uInitrd
440EOT
441
Tristan Muntsinger34569112019-10-16 22:10:54 -0700442 umount ${mntdir}/sys
443 umount ${mntdir}/dev
444 umount ${mntdir}/proc
445 umount ${mntdir}
446
447 # Turn on journaling
448 tune2fs -O ^has_journal ${IMAGE}
449 e2fsck -fy ${IMAGE} >/dev/null 2>&1
450fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700451
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700452if [ ${USE_IMAGE} -eq 0 ]; then
453 # 32GB eMMC size
Tristan Muntsinger34569112019-10-16 22:10:54 -0700454 end_sector=61071326
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700455 device=/dev/${mmc_dev}
456 devicep=${device}
457
458 sgdisk -Z -a1 ${device}
459 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${device}
460 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${device}
461 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${device}
462 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${device}
Tristan Muntsinger34569112019-10-16 22:10:54 -0700463 sgdisk -a1 -n:5:32768:${end_sector} -A:5:set:2 -t:5:8305 -c:5:rootfs ${device}
464 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
465 dd if=${IMAGE} of=${devicep}5 bs=1M
466 resize2fs ${devicep}5 >/dev/null 2>&1
467 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700468else
Tristan Muntsinger34569112019-10-16 22:10:54 -0700469 device=$(losetup -f)
470 devicep=${device}p
471 if [ ${FLAGS_p5} -eq ${FLAGS_FALSE} ]; then
472 fs_end=3G
473 end_sector=-
474 fi
475 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
476 # Minimize rootfs filesystem
477 while true; do
478 out=`sudo resize2fs -M ${IMAGE} 2>&1`
479 if [[ $out =~ "Nothing to do" ]]; then
480 break
481 fi
482 done
483 # Minimize rootfs file size
484 block_count=`sudo tune2fs -l ${IMAGE} | grep "Block count:" | sed 's/.*: *//'`
485 block_size=`sudo tune2fs -l ${IMAGE} | grep "Block size:" | sed 's/.*: *//'`
486 sector_size=512
487 start_sector=32768
488 fs_size=$(( block_count*block_size ))
489 fs_sectors=$(( fs_size/sector_size ))
490 part_sectors=$(( ((fs_sectors-1)/2048+1)*2048 )) # 1MB-aligned
491 end_sector=$(( start_sector+part_sectors-1 ))
492 secondary_gpt_sectors=33
493 fs_end=$(( (end_sector+secondary_gpt_sectors+1)*sector_size ))
494 image_size=$(( part_sectors*sector_size ))
495 truncate -s ${image_size} ${IMAGE}
496 e2fsck -fy ${IMAGE} >/dev/null 2>&1
497 fi
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700498
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700499 # Create final image
500 tmpimg=`mktemp`
501 truncate -s ${fs_end} ${tmpimg}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700502
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700503 # Create GPT
504 sgdisk -Z -a1 ${tmpimg}
505 sgdisk -a1 -n:1:64:8127 -t:1:8301 -c:1:loader1 ${tmpimg}
506 sgdisk -a1 -n:2:8128:8191 -t:2:8301 -c:2:env ${tmpimg}
507 sgdisk -a1 -n:3:16384:24575 -t:3:8301 -c:3:loader2 ${tmpimg}
508 sgdisk -a1 -n:4:24576:32767 -t:4:8301 -c:4:trust ${tmpimg}
509 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 -0700510
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700511 losetup ${device} ${tmpimg}
512 partx -v --add ${device}
Tristan Muntsinger82c10502019-10-15 14:49:19 -0700513
Tristan Muntsinger34569112019-10-16 22:10:54 -0700514 if [ ${FLAGS_p5} -eq ${FLAGS_TRUE} ]; then
515 dd if=${IMAGE} of=${devicep}5 bs=1M
516 fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700517fi
Tristan Muntsinger34569112019-10-16 22:10:54 -0700518if [ ${FLAGS_p1} -eq ${FLAGS_TRUE} ]; then
519 dd if=${idbloader} of=${devicep}1
520fi
521if [ ${FLAGS_p2} -eq ${FLAGS_TRUE} ]; then
522 dd if=${bootenv} of=${devicep}2
523fi
524if [ ${FLAGS_p3} -eq ${FLAGS_TRUE} ]; then
525 dd if=${ANDROID_BUILD_TOP}/external/u-boot/u-boot.itb of=${devicep}3
526fi
Tristan Muntsingere4eeded2019-10-15 20:26:15 -0700527if [ ${USE_IMAGE} -eq 1 ]; then
528 chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
529 mv ${tmpimg} ${IMAGE}
530 partx -v --delete ${device}
531 losetup -d ${device}
532fi