1#!/bin/bash 2 3# Copyright 2022 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 17 18set -x 19set -u 20# comment out -e because partx has error message 21# set -o errexit 22# set -e 23 24# get script directory 25script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 26 27if [ -z $ANDROID_BUILD_TOP ]; then 28 echo "error: run script after 'lunch'" 29 exit 1 30fi 31 32source "${ANDROID_BUILD_TOP}/external/shflags/shflags" 33 34# prepare pre-requested files, including kernel and uboot 35# get temp directory 36tmpdir=`echo $RANDOM | md5sum | head -c 20` 37mkdir "/tmp/$tmpdir" 38UBOOT_REPO="/tmp/$tmpdir/uboot" 39KERNEL_REPO="/tmp/$tmpdir/kernel" 40IMAGE="/tmp/$tmpdir/test_image" 41ARCH= 42FLAGS_HELP="USAGE: $0 [ARCH]" 43mkdir $UBOOT_REPO 44mkdir $KERNEL_REPO 45cd $KERNEL_REPO 46repo init -u persistent-https://android.git.corp.google.com/kernel/manifest -b common-android13-5.15 && repo sync 47 48# parse input parameters 49FLAGS "$@" || exit $? 50eval set -- "${FLAGS_ARGV}" 51 52for arg in "$@" ; do 53 if [ -z $ARCH ]; then 54 ARCH=$arg 55 else 56 flags_help 57 exit 1 58 fi 59done 60 61if [[ "$ARCH" == "arm" ]]; then 62 cd $UBOOT_REPO 63 repo init -u persistent-https://android.git.corp.google.com/kernel/manifest -b u-boot-mainline && repo sync 64fi 65 66if [ -z $KERNEL_REPO -o -z $UBOOT_REPO ]; then 67 flags_help 68 exit 1 69fi 70if [ ! -e "${UBOOT_REPO}" ]; then 71 echo "error: can't find '${UBOOT_REPO}'. aborting..." 72 exit 1 73fi 74if [ ! -e "${KERNEL_REPO}" ]; then 75 echo "error: can't find '${KERNEL_REPO}'. aborting..." 76 exit 1 77fi 78 79tmpfile=`mktemp` 80bootenv=`mktemp` 81cat > ${tmpfile} << "EOF" 82bootdelay=2 83baudrate=1500000 84scriptaddr=0x00500000 85boot_targets=mmc1 mmc0 86bootcmd=run distro_bootcmd 87distro_bootcmd=for target in ${boot_targets}; do run bootcmd_${target}; done 88bootcmd_mmc0=devnum=0; run mmc_boot 89bootcmd_mmc1=devnum=1; run mmc_boot 90mmc_boot=if mmc dev ${devnum}; then ; run scan_for_boot_part; fi 91scan_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; 92find_script=if test -e mmc ${devnum}:${distro_bootpart} /boot/boot.scr; then echo Found U-Boot script /boot/boot.scr; run run_scr; fi 93run_scr=load mmc ${devnum}:${distro_bootpart} ${scriptaddr} /boot/boot.scr; source ${scriptaddr} 94EOF 95echo "Sha=`${script_dir}/gen_sha.sh --uboot ${UBOOT_REPO} --kernel ${KERNEL_REPO}`" >> ${tmpfile} 96${ANDROID_BUILD_TOP}/device/google/cuttlefish_prebuilts/uboot_tools/mkenvimage -s 32768 -o ${bootenv} - < ${tmpfile} 97 98# build uboot based on architecture 99cd ${UBOOT_REPO} 100if [[ "$ARCH" == "arm" ]]; then 101 BUILD_CONFIG=u-boot/build.config.rockpi4 build/build.sh -j1 102fi 103cd - 104 105# build kernel based on architecture 106cd ${KERNEL_REPO} 107rm -rf out 108if [[ "$ARCH" == "arm" ]]; then 109 BUILD_CONFIG=common/build.config.rockpi4 build/build.sh -j`nproc` 110else 111 BUILD_CONFIG=common/build.config.gce.x86_64 build/build.sh -j`nproc` 112fi 113cd - 114 115dist_dir=$(echo ${KERNEL_REPO}/out*/dist) 116 117# build rootfs/host images 118if [[ "$ARCH" == "arm" ]]; then 119 ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh \ 120 -a arm64 -s bullseye-rockpi -n ${IMAGE} -r ${IMAGE}.initrd -e \ 121 -k ${dist_dir}/Image -i ${dist_dir}/initramfs.img \ 122 -d ${dist_dir}/rk3399-rock-pi-4b.dtb:rockchip 123else 124 ${ANDROID_BUILD_TOP}/kernel/tests/net/test/build_rootfs.sh \ 125 -a amd64 -s bullseye-server -n ${IMAGE} -r ${IMAGE}.initrd -e \ 126 -k ${dist_dir}/bzImage -i ${dist_dir}/initramfs.img -g 127fi 128 129if [ $? -ne 0 ]; then 130 echo "error: failed to build rootfs. exiting..." 131 exit 1 132fi 133 134rm -f ${IMAGE}.initrd 135rm -rf ${ANDROID_BUILD_TOP}/disk.raw 136cp ${IMAGE} ${ANDROID_BUILD_TOP}/disk.raw 137cd ${ANDROID_BUILD_TOP} 138rm -rf disk_${USER}.raw.tar.gz 139tar Szcvf disk_${USER}.raw.tar.gz disk.raw 140