blob: e897eee6ae2955f0e8d6aa7b8ecf5029fd9604a1 [file]
# SPDX-License-Identifier: (GPL-2.0+ OR MIT)
#
# Copyright (c) 2019 Amlogic, Inc. All rights reserved.
#
#!/bin/bash
exec_name=$0
set -o errtrace
trap 'echo Fatal error: script ${exec_name} aborting at line $LINENO,\
command \"$BASH_COMMAND\" returned $?; exit 1' ERR
readonly kernel_dir=$(dirname "$(dirname "$(realpath "$0")")")
export PATH=${kernel_dir}/../prebuilt/toolchain/aarch64/bin:$PATH
arch=arm64
cross_compile=${kernel_dir}/../prebuilt/toolchain/aarch64/bin/aarch64-cros-linux-gnu-
clang_triple=${kernel_dir}/../prebuilt/toolchain/aarch64/bin/aarch64-cros-linux-gnu-
cc_clang=${kernel_dir}/../prebuilt/toolchain/aarch64/bin/aarch64-cros-linux-gnu-clang
cpu_num=$(grep -c processor /proc/cpuinfo)
readonly fctname="fct"
readonly bootdir=${kernel_dir}/arch/arm64/boot
function usage(){
echo "Usage: ${exec_name} <board> [workspace path] [-d]"
echo "supported boards: swift-p2 swift-b1 swift-b3 swift-b4"
echo
echo "-d : Build deterministicly. Use git revision and timestamp and"
echo " generic user and host for /proc/version"
}
function deterministic_build(){
local deterministic_build=false
if [[ "$1" == "-d" ]]; then
deterministic_build=true
shift
fi
# Set variables for deterministic kernel builds
# These correspond to scripts/mkcompile_h
if [[ "$deterministic_build" == true ]]; then
export KBUILD_BUILD_VERSION=0
export KBUILD_BUILD_TIMESTAMP="$(git log -1 --format=%cd \
--date=format:'%a %b %d %H:%M:%S %Y') \
($(git rev-parse HEAD | head -c 8))"
export KBUILD_BUILD_USER=user
export KBUILD_BUILD_HOST=host
else
# Add git hash to date. This corresponds to scripts/mkcompile_h
# This shows up in /proc/version
export KBUILD_BUILD_TIMESTAMP="$(date) ($(git rev-parse HEAD | head -c 8))"
fi
}
function run_kernel_make(){
echo "***** building $4 *****"
make PAHOLE= CLANG_TRIPLE=$clang_triple CC=$cc_clang CROSS_COMPILE=$1 ARCH=$3 -j$2 $4
echo "***** completed building $4 *****"
}
function build_kernel(){
local kernel_path=$(readlink -f $1)
local defconfig_file_name=$2
# If it is b series board, use bx defconfig.
local board_type=`echo ${defconfig_file_name} | cut -d "-" -f2 | cut -c 1`
if [[ "$board_type" == "b" ]]; then
local product_surname=`echo ${defconfig_file_name} | cut -d "-" -f1`
defconfig_file_name=${product_surname}-bx_defconfig
fi
pushd $kernel_path > /dev/null
# Make a clean build and check .config and defconfig if different then abort.
# make clean
cp -r common_drivers/arch/arm64/configs/${defconfig_file_name} \
arch/arm64/configs/${defconfig_file_name}
run_kernel_make $cross_compile $cpu_num $arch $defconfig_file_name
diff .config common_drivers/arch/arm64/configs/${defconfig_file_name}
run_kernel_make $cross_compile $cpu_num $arch all
rm -f arch/arm64/configs/${defconfig_file_name}
popd > /dev/null
}
function build_dtb(){
local kernel_path=$(readlink -f $1)
local dtb_file_name=$2
pushd $kernel_path > /dev/null
run_kernel_make $cross_compile $cpu_num $arch $dtb_file_name
popd > /dev/null
}
# Append $1 which is an integer value to the file at path $2. $1 is appended as
# a binary little endian integer.
append_uint32_le() {
local val=$1
local file=$2
printf "0: %.8x" ${val} | sed -E 's/0: (..)(..)(..)(..)/0: \4\3\2\1/' \
| xxd -r -g0 >> ${file}
}
# Pack the kernel along with its dtb file
# The format is [header][compressed kernel][dtb file]
#
# header is little endian and consists of
# struct {
# char magic[KDTB_MAGIC_SZ];
# uint32_t kernel_size;
# uint32_t dtb_size;
# };
pack_kernel() {
local dtb_file=$1
local product=$2
local board_name=$3
local factory=$4
#local compressed_kernel=${bootdir}/Image.lz4
local compressed_kernel=${bootdir}/Image.gz
local magic="KDTB"
if [ ! -z ${factory} ]; then
local packed_kernel=${bootdir}/${factory}_kernel.${product}.gz-dtb.\
${board_name}
else
local packed_kernel=${bootdir}/kernel.${product}.gz-dtb.${board_name}
fi
echo -n ${magic} > ${packed_kernel}
append_uint32_le $(stat -c %s ${compressed_kernel}) ${packed_kernel}
append_uint32_le $(stat -c %s ${dtb_file}) ${packed_kernel}
cat ${compressed_kernel} ${dtb_file} >> ${packed_kernel}
}
copy_ko_to_path() {
ko_list=$1
dest_dir=$2
board=$3
# rm -rf $dest_dir || true
mkdir -p "$dest_dir" || true
echo "Copy ko to ${dest_dir}..."
while IFS= read -r ko_file; do
ko_path=$(echo "$ko_file" | awk '{gsub(/^ +| +$/, ""); print}')
if [ -f "$kernel_dir/$ko_path" ]; then
cp "$kernel_dir/$ko_path" "$dest_dir/$(basename "$ko_path" .ko).$board.ko"
else
echo "File not found: $ko_file"
fi
done < "${ko_list}"
if ! [ $? -eq 0 ]; then
exit 1
else
${cross_compile}strip --strip-debug ${dest_dir}/*
fi
}
if (( $# < 1 ))
then
usage
exit 2
fi
board=$1
readonly workspace_path=$2
deterministic_build $3
product=`echo ${board} | cut -d "-" -f1`
case $product in
swift)
pushd ${kernel_dir} > /dev/null
FULL_KERNEL_VERSION=$(grep -rn 'BRANCH=' ${kernel_dir}/build.config.common)
FULL_KERNEL_VERSION="common${FULL_KERNEL_VERSION##*android}"
echo "./common_drivers/auto_patch/auto_patch.sh ${FULL_KERNEL_VERSION}"
./common_drivers/auto_patch/auto_patch.sh ${FULL_KERNEL_VERSION}
build_kernel ${kernel_dir} ${board}_defconfig
dtb_file_name=${board}.dtb
path_to_dtb_file=${kernel_dir}/common_drivers/arch/arm64/boot/dts/amlogic/${dtb_file_name}
build_dtb ${kernel_dir} ${dtb_file_name}
cp common_drivers/arch/arm64/boot/dts/amlogic/${dtb_file_name} arch/arm64/boot
# build factory kernel
fct_dtb_file_name=${fctname}_${dtb_file_name}
path_to_fct_dtb_file=${kernel_dir}/common_drivers/arch/arm64/boot/dts/amlogic/${fct_dtb_file_name}
build_dtb ${kernel_dir} ${fct_dtb_file_name}
cp common_drivers/arch/arm64/boot/dts/amlogic/${fct_dtb_file_name} arch/arm64/boot
pack_kernel $path_to_dtb_file ${product} ${board}
pack_kernel $path_to_fct_dtb_file ${product} ${board} ${fctname}
popd > /dev/null
if [ ! -z "$workspace_path" ] && [ -d "$workspace_path" ]; then
prebuilt_path=${workspace_path}/vendor/amlogic/${product}/prebuilt
kernel_path=${prebuilt_path}/kernel_6.6
mkdir -p ${kernel_path}
fct_kernel_path=${prebuilt_path}/factory/kernel_6.6
mkdir -p ${fct_kernel_path}
cp ${bootdir}/kernel.${product}.gz-dtb.${board} ${kernel_path}
cp ${path_to_dtb_file} ${kernel_path}/${board}.dtb
# Factory directory
cp ${bootdir}/${fctname}_kernel.${product}.gz-dtb.${board} \
${fct_kernel_path}/kernel.${product}.gz-dtb.${board}
# copy all ko
kernel_modules=${kernel_path}/lib/modules
copy_ko_to_path ${kernel_dir}/common_drivers/ko_list ${kernel_modules} ${board}
fi
;;
*)
echo "unknown board: $board"
exit 1
esac