2026-08-13 11:28:40 +08:00
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# Kernel build script for AMLbian
# This script builds kernel for different Amlogic chipsets
set -e
# Get script directory
SCRIPT_DIR = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) "
AMLBIAN_DIR = " $( dirname " ${ SCRIPT_DIR } " ) "
# ==============================================================================
# Section 1: Help Information
# ==============================================================================
function show_help() {
cat << EOF
Usage: $(basename "$0") [OPTIONS]
Build kernel for Amlogic boards
Options:
--kernel-version VERSION Kernel version (6.12, etc.)
--board BOARD Board name (s6-bq201, a9-by401)
--source-dir PATH Local kernel source directory
--output-dir PATH Output directory for build artifacts
--defconfig CONFIG Kernel defconfig to use
--clean Clean before build
--help Show this help message
Examples:
$(basename "$0") --kernel-version 6.12 --board s6-bq201
$(basename "$0") --source-dir /path/to/kernel --clean
EOF
}
# ==============================================================================
# Section 2: Default Configuration
# ==============================================================================
KERNEL_VERSION = " ${ KERNEL_VERSION :- 6 .12 } "
BOARD = " ${ BOARD :- s6 -bq201 } "
SOURCE_DIR = " ${ LOCAL_KERNEL_PATH :- ${ AMLBIAN_DIR } /../../aml-comp/kernel } "
#OUTPUT_DIR="${AMLBIAN_DIR}/../../aml-comp/output"
OUTPUT_DIR = " ${ AMLBIAN_DIR } /../output/kernel"
DEFCONFIG = "s6_bq201_defconfig"
CLEAN_BUILD = "no"
# ==============================================================================
# Section 3: Parse Arguments
# ==============================================================================
while [[ $# -gt 0 ]] ; do
case " $1 " in
--kernel-version)
KERNEL_VERSION = " $2 "
shift 2
;;
--board)
BOARD = " $2 "
shift 2
;;
--source-dir)
SOURCE_DIR = " $2 "
shift 2
;;
--output-dir)
OUTPUT_DIR = " $2 "
shift 2
;;
--defconfig)
DEFCONFIG = " $2 "
shift 2
;;
--clean)
CLEAN_BUILD = "yes"
shift
;;
--help)
show_help
exit 0
;;
*)
2026-11-11 08:50:43 +08:00
aml_log error "Unknown option: $1 "
2026-08-13 11:28:40 +08:00
show_help
exit 1
;;
esac
done
# ==============================================================================
# Section 4: Build Kernel
# ==============================================================================
function prepare_linux_config() {
Kernel_config = "final_defconfig"
local KERNEL_SOURCE_DIR = " $1 "
local FINAL_DEFCONFIG_PATH = " ${ KERNEL_SOURCE_DIR } /common_drivers/arch/arm64/configs"
local GKI_DEFCONFIG_PATH = " ${ KERNEL_SOURCE_DIR } /arch/arm64/configs"
local NFS_BOOT = ""
KERNEL_CONFIG_DIR = " ${ AMLBIAN_DIR } /patches/linux_defconf"
rm -f ${ GKI_DEFCONFIG_PATH } /${ Kernel_config }
2026-11-11 08:50:43 +08:00
aml_log info "[LINUX]prepare kernel config start... ${ KERNEL_CONFIG_DIR } "
2026-08-13 11:28:40 +08:00
# Print all variables used in this function
2026-11-11 08:50:43 +08:00
aml_log debug "[LINUX] Variables in prepare_linux_config:"
aml_log debug " KERNEL_SOURCE_DIR: ${ KERNEL_SOURCE_DIR } "
aml_log debug " AMLBIAN_DIR: ${ AMLBIAN_DIR } "
aml_log debug " FINAL_DEFCONFIG_PATH: ${ FINAL_DEFCONFIG_PATH } "
aml_log debug " GKI_DEFCONFIG_PATH: ${ GKI_DEFCONFIG_PATH } "
aml_log debug " KERNEL_CONFIG_DIR: ${ KERNEL_CONFIG_DIR } "
aml_log debug " Kernel_config: ${ Kernel_config } "
aml_log debug " LINUX_DEFCONFIG: ${ LINUX_DEFCONFIG :- not -set } "
2026-08-13 11:28:40 +08:00
# Check if critical paths exist
2026-11-11 08:50:43 +08:00
aml_log info "[LINUX] Checking critical paths..."
2026-08-13 11:28:40 +08:00
# Check KERNEL_SOURCE_DIR
if [[ -z " ${ KERNEL_SOURCE_DIR } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: KERNEL_SOURCE_DIR is empty"
return 1
2026-08-13 11:28:40 +08:00
elif [[ ! -d " ${ KERNEL_SOURCE_DIR } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: KERNEL_SOURCE_DIR does not exist: ${ KERNEL_SOURCE_DIR } "
return 1
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log debug " ✓ KERNEL_SOURCE_DIR exists: ${ KERNEL_SOURCE_DIR } "
2026-08-13 11:28:40 +08:00
fi
# Check AMLBIAN_DIR
if [[ -z " ${ AMLBIAN_DIR } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: AMLBIAN_DIR is empty"
return 1
2026-08-13 11:28:40 +08:00
elif [[ ! -d " ${ AMLBIAN_DIR } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: AMLBIAN_DIR does not exist: ${ AMLBIAN_DIR } "
return 1
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log debug " ✓ AMLBIAN_DIR exists: ${ AMLBIAN_DIR } "
2026-08-13 11:28:40 +08:00
fi
# Check FINAL_DEFCONFIG_PATH
if [[ ! -d " ${ FINAL_DEFCONFIG_PATH } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: FINAL_DEFCONFIG_PATH does not exist: ${ FINAL_DEFCONFIG_PATH } "
return 1
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log debug " ✓ FINAL_DEFCONFIG_PATH exists: ${ FINAL_DEFCONFIG_PATH } "
2026-08-13 11:28:40 +08:00
fi
# Check GKI_DEFCONFIG_PATH
if [[ ! -d " ${ GKI_DEFCONFIG_PATH } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: GKI_DEFCONFIG_PATH does not exist: ${ GKI_DEFCONFIG_PATH } "
return 1
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log debug " ✓ GKI_DEFCONFIG_PATH exists: ${ GKI_DEFCONFIG_PATH } "
2026-08-13 11:28:40 +08:00
fi
# Check KERNEL_CONFIG_DIR
if [[ ! -d " ${ KERNEL_CONFIG_DIR } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: KERNEL_CONFIG_DIR does not exist: ${ KERNEL_CONFIG_DIR } "
return 1
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log debug " ✓ KERNEL_CONFIG_DIR exists: ${ KERNEL_CONFIG_DIR } "
2026-08-13 11:28:40 +08:00
# Check for required files in KERNEL_CONFIG_DIR
if [[ ! -f " ${ KERNEL_CONFIG_DIR } /common.cfg" ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: common.cfg not found in ${ KERNEL_CONFIG_DIR } "
return 1
2026-08-13 11:28:40 +08:00
fi
if [[ -n " ${ LINUX_DEFCONFIG } " && ! -f " ${ KERNEL_CONFIG_DIR } / ${ LINUX_DEFCONFIG } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error " ⚠ ERROR: ${ LINUX_DEFCONFIG } not found in ${ KERNEL_CONFIG_DIR } "
return 1
2026-08-13 11:28:40 +08:00
fi
fi
# separate base config
EXCLUDED_FILES =( "common.cfg" " ${ LINUX_DEFCONFIG } " )
EXT_CONFIG = ""
for file in " $KERNEL_CONFIG_DIR " /*; do
filename = $( basename " $file " )
if [[ ! " ${ EXCLUDED_FILES [@] } " = ~ " $filename " ]] ; then
EXT_CONFIG += " ${ KERNEL_CONFIG_DIR } / $filename "
fi
done
2026-11-11 08:50:43 +08:00
if [ " ${ RT_LINUX } " = "yes" ] ; then
RT_CONFIG = " ${ KERNEL_SOURCE_DIR } /common_drivers/arch/arm64/configs/preempt_rt.fragment"
else
RT_CONFIG = ""
fi
KCONFIG_CONFIG = ${ FINAL_DEFCONFIG_PATH } /${ Kernel_config } \
${ KERNEL_SOURCE_DIR } /scripts/kconfig/merge_config.sh -m -r \
${ GKI_DEFCONFIG_PATH } /gki_defconfig \
${ KERNEL_SOURCE_DIR } /common_drivers/arch/arm64/configs/amlogic_gki.fragment \
${ KERNEL_SOURCE_DIR } /common_drivers/arch/arm64/configs/amlogic_gki.10 \
${ KERNEL_SOURCE_DIR } /common_drivers/arch/arm64/configs/amlogic_gki.debug \
${ KERNEL_SOURCE_DIR } /common_drivers/arch/arm64/configs/amlogic_gcc64_defconfig \
${ KERNEL_CONFIG_DIR } /common.cfg \
${ RT_CONFIG } \
${ KERNEL_CONFIG_DIR } /${ LINUX_DEFCONFIG } \
$EXT_CONFIG
2026-08-13 11:28:40 +08:00
cp ${ FINAL_DEFCONFIG_PATH } /${ Kernel_config } ${ GKI_DEFCONFIG_PATH } /${ Kernel_config }
export LINUX_DEFCONFIG = ${ Kernel_config }
2026-11-11 08:50:43 +08:00
aml_log info "[LINUX]prepare kernel config done..."
2026-08-13 11:28:40 +08:00
}
# Get kernel version from kernel source .config file
function get_kernel_version() {
local kernel_source_dir = " ${ 1 :- ${ KERNEL_SOURCE_DIR }} "
if [[ -z " ${ kernel_source_dir } " || ! -d " ${ kernel_source_dir } " ]] ; then
kernel_source_dir = " ${ KERNEL_SRC_DIR } "
if [[ -z " ${ kernel_source_dir } " || ! -d " ${ kernel_source_dir } " ]] ; then
echo ""
return 1
fi
fi
local config_file = " ${ kernel_source_dir } /build/.config"
if [[ ! -f " ${ config_file } " ]] ; then
echo ""
return 1
fi
# Extract kernel version from config file
# Look for line like: "# Linux/arm64 6.12.38 Kernel Configuration"
local version_line = $( grep -E "^# Linux/[a-z0-9_]+ [0-9]+\.[0-9]+\.[0-9]+ Kernel Configuration $" " ${ config_file } " | head -1)
if [[ -z " ${ version_line } " ]] ; then
echo ""
return 1
fi
# Extract version number (e.g., 6.12.38 from "# Linux/arm64 6.12.38 Kernel Configuration")
local base_version = $( echo " ${ version_line } " | sed -E 's/^# Linux\/[a-z0-9_]+ ([0-9]+\.[0-9]+\.[0-9]+) Kernel Configuration$/\1/' )
if [[ -z " ${ base_version } " ]] ; then
echo ""
return 1
fi
# Add amlogic extension
local extended_version = " ${ base_version } -amlogic"
# Add git commit hash if available
# if [[ -n "${BUILD_REPOSITORY_COMMIT}" ]]; then
# extended_version="${extended_version}-${BUILD_REPOSITORY_COMMIT}"
# else
# # Try to get git commit from kernel source directory
# if command -v git >/dev/null 2>&1 && [[ -d "${kernel_source_dir}" ]]; then
# if git -C "${kernel_source_dir}" rev-parse --git-dir >/dev/null 2>&1; then
# local git_commit="$(git -C "${kernel_source_dir}" rev-parse --short=7 HEAD 2>/dev/null || true)"
# if [[ -n "${git_commit}" ]]; then
# extended_version="${extended_version}-${git_commit}"
# fi
# fi
# fi
# fi
echo " ${ extended_version } "
return 0
}
function compile_kernel() {
local aml_comp_kernel_path = $1
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC : Kernel start compile ${ aml_comp_kernel_path } "
2026-08-13 11:28:40 +08:00
# For now, use Armbian's native kernel build
cd " ${ aml_comp_kernel_path } "
if [[ -n " ${ aml_comp_kernel_path } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-KERNEL Found aml-comp Kernel at: ${ aml_comp_kernel_path } "
2026-08-13 11:28:40 +08:00
# If aml-comp kernel has a Makefile, attempt a local build under amlbian
if [[ -f " ${ aml_comp_kernel_path } /Makefile" ]] ; then
export KERNEL_SOURCE_DIR = " ${ aml_comp_kernel_path } "
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Building aml-comp Kernel locally: ${ KERNEL_SOURCE_DIR } "
2026-08-13 11:28:40 +08:00
local original_dir = " $( pwd ) "
cd " ${ aml_comp_kernel_path } " || {
2026-11-11 08:50:43 +08:00
aml_log error "COMPILE-KERNEL Failed to change to aml-comp kernel directory"
2026-08-13 11:28:40 +08:00
cd " ${ original_dir } "
2026-11-11 08:50:43 +08:00
exit 1
2026-08-13 11:28:40 +08:00
}
# Try an out-of-tree build if supported, otherwise in-tree
local build_dir = " ${ aml_comp_kernel_path } /build"
mkdir -p " ${ build_dir } "
# Check default gcc version before switching toolchain
local need_new_toolchain = 1
local default_gcc_version = ""
# Try to get default gcc version
if command -v gcc & > /dev/null; then
default_gcc_version = $( ${ CC } --version | head -n1 | grep -oP '\d+\.\d+\.\d+' | head -n1)
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-KERNEL Default gcc version: ${ default_gcc_version } "
2026-08-13 11:28:40 +08:00
# Check if version is >= 5.1.0
if [[ -n " ${ default_gcc_version } " ]] ; then
# Simple version comparison
local major_version = $( echo " ${ default_gcc_version } " | cut -d. -f1)
local minor_version = $( echo " ${ default_gcc_version } " | cut -d. -f2)
if [[ ${ major_version } -gt 5 ]] || ([[ ${ major_version } -eq 5 ]] && [[ ${ minor_version } -ge 1 ]]) ; then
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-KERNEL Default gcc version ${ default_gcc_version } >= 5.1.0, no need for new toolchain"
2026-08-13 11:28:40 +08:00
need_new_toolchain = 0
else
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-KERNEL Default gcc version ${ default_gcc_version } < 5.1.0, will try new toolchain"
2026-08-13 11:28:40 +08:00
fi
fi
else
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL gcc not found in PATH"
2026-08-13 11:28:40 +08:00
fi
# Only switch to new toolchain if needed
if [[ ${ need_new_toolchain } -eq 1 ]] ; then
# local new_toolchain_path="/opt/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin"
local new_toolchain_path = "/opt/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin"
if [[ -d " ${ new_toolchain_path } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Using new toolchain from: ${ new_toolchain_path } "
2026-08-13 11:28:40 +08:00
export PATH = " ${ new_toolchain_path } : ${ PATH } "
# Check if gcc exists in the new toolchain
if [[ -f " ${ new_toolchain_path } /aarch64-linux-gnu-gcc" ]] ; then
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Found cross-compiler: ${ new_toolchain_path } /aarch64-linux-gnu-gcc"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL Cross-compiler not found in new toolchain, using default"
2026-08-13 11:28:40 +08:00
fi
else
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL New toolchain directory not found: ${ new_toolchain_path } "
2026-08-13 11:28:40 +08:00
fi
fi
cd " ${ aml_comp_kernel_path } "
# Ensure helpers that rely on KERNEL_SOURCE_DIR see the aml-comp tree
export KERNEL_SOURCE_DIR = " ${ aml_comp_kernel_path } "
COMMON_DRV_PATH = " ${ aml_comp_kernel_path } /common_drivers"
export COMMON_DRIVERS_DIR = ${ COMMON_DRV_PATH }
# Keep source tree clean for out-of-tree builds. This prevents
# "source tree is not clean" errors during make.
if [[ " ${ KERNEL_FORCE_MRPROPER :- yes } " == "yes" ]] ; then
if [[ -f " ${ aml_comp_kernel_path } /.config" \
|| -f " ${ aml_comp_kernel_path } /include/config/auto.conf" \
|| -f " ${ aml_comp_kernel_path } /include/generated/autoconf.h" ]] ; then
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Cleaning kernel source tree (mrproper)"
2026-08-13 11:28:40 +08:00
make ARCH = arm64 mrproper 2>& 1 | while read -r line; do
display_alert "COMPILE-KERNEL mrproper: ${ line } "
done
fi
fi
setup_git_safe_dir ${ aml_comp_kernel_path }
#apply patches to kernel from common drivers if any
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Applying common driver patches to aml-comp kernel..."
2026-08-13 11:28:40 +08:00
KERNEL_DIR = ${ S } COMMON_DRIVERS_DIR = " ${ COMMON_DRV_PATH } " ${ COMMON_DRV_PATH } /auto_patch/auto_patch.sh "common16-6.12" 2>& 1 | while read -r line; do
display_alert "COMPILE-KERNEL apply patch: ${ line } "
done
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Running mrproper on aml-comp kernel tree"
2026-08-13 11:28:40 +08:00
make -C " ${ aml_comp_kernel_path } " ARCH = arm64 mrproper >/dev/null 2>& 1 || true
prepare_linux_config ${ aml_comp_kernel_path }
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Running: make -j $( nproc) ARCH=arm64 CROSS_COMPILE= ${ CROSS_COMPILE :- aarch64 -none-linux-gnu- } O= ${ build_dir } ${ FULL_EXTRA_ARGS } "
2026-08-13 11:28:40 +08:00
make V = 1 -j${ nproc } ARCH = arm64 CROSS_COMPILE = ${ CROSS_COMPILE :- aarch64 -none-linux-gnu- } O = " ${ build_dir } " ${ LINUX_DEFCONFIG :- defconfig } ${ FULL_EXTRA_ARGS } 2>& 1 | while read -r line; do
display_alert "COMPILE-KERNEL make conf: ${ line } "
done
#example: make -j8 ARCH=arm64 CROSS_COMPILE=ccache aarch64-none-linux-gnu- Image t7_a311d2_an400_linux.dtb t7c_a311d2_an400_linux.dtb modules
#make -j${nproc} ARCH=${DISTRIB_ARCH} CROSS_COMPILE="${CCACHE} ${KERNEL_COMPILER}" Image ${LINUX_DTB[@]} modules $EXTRA_MAKE_ARGS
#make -j${nproc} ARCH=arm64 CROSS_COMPILE=${CROSS_COMPILE:-aarch64-none-linux-gnu-} O="${build_dir}" Image ${KERNEL_DEVICETREE} modules ${FULL_EXTRA_ARGS} 2>&1 | while read -r line; do
#done
local make_exit = 0
if ! make -j" $( nproc) " -C " ${ aml_comp_kernel_path } " ARCH = arm64 CROSS_COMPILE = ${ CROSS_COMPILE :- aarch64 -none-linux-gnu- } O = " ${ build_dir } " Image ${ KERNEL_DEVICETREE } modules ${ FULL_EXTRA_ARGS } ; then
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL Image+ ${ KERNEL_DEVICETREE } build failed, retrying with generic dtbs target"
2026-08-13 11:28:40 +08:00
if ! make -j" $( nproc) " ARCH = arm64 CROSS_COMPILE = ${ CROSS_COMPILE :- aarch64 -none-linux-gnu- } O = " ${ build_dir } " Image modules dtbs ${ FULL_EXTRA_ARGS } ; then
make_exit = 1
fi
fi
# Install modules into a deterministic staging path so packaging can
# pick them up instead of relying on scattered .ko locations.
local modules_staging_dir = " ${ build_dir } /modules-staging"
local dtb_staging_dir = " ${ build_dir } /dtb-staging"
local kernel_release = ""
if [[ ${ make_exit } -eq 0 ]] ; then
kernel_release = " $( make -s ARCH = arm64 CROSS_COMPILE = ${ CROSS_COMPILE :- aarch64 -none-linux-gnu- } O = " ${ build_dir } " kernelrelease 2>/dev/null || true ) "
[[ -z " ${ kernel_release } " ]] && kernel_release = " ${ KERNEL_VER :- } "
[[ -z " ${ kernel_release } " ]] && kernel_release = " $( basename " $( find " ${ build_dir } /lib/modules" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | head -1) " ) "
if [[ -n " ${ kernel_release } " ]] ; then
rm -rf " ${ modules_staging_dir } "
mkdir -p " ${ modules_staging_dir } "
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-KERNEL Installing modules to staging: ${ modules_staging_dir } "
2026-08-13 11:28:40 +08:00
make -j" $( nproc) " ARCH = arm64 CROSS_COMPILE = ${ CROSS_COMPILE :- aarch64 -none-linux-gnu- } O = " ${ build_dir } " \
INSTALL_MOD_PATH = " ${ modules_staging_dir } " DEPMOD = true modules_install ${ FULL_EXTRA_ARGS } || true
local modules_install_exit = 0
[[ -d " ${ modules_staging_dir } /lib/modules/ ${ kernel_release } " ]] || modules_install_exit = 1
if [[ ${ modules_install_exit } -ne 0 ]] ; then
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL modules_install failed; trying fallback .ko collection"
2026-08-13 11:28:40 +08:00
fi
local staged_mod_dir = " ${ modules_staging_dir } /lib/modules/ ${ kernel_release } "
local staged_mod_count = 0
if [[ -d " ${ staged_mod_dir } " ]] ; then
staged_mod_count = $( find " ${ staged_mod_dir } " -type f -name "*.ko*" 2>/dev/null | wc -l)
fi
if [[ ${ staged_mod_count } -eq 0 ]] ; then
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL No modules in staging; collecting scattered .ko as fallback"
2026-08-13 11:28:40 +08:00
mkdir -p " ${ staged_mod_dir } /extra"
find " ${ build_dir } " " ${ aml_comp_kernel_path } " -type f -name "*.ko" 2>/dev/null | while read -r ko; do
cp -f " ${ ko } " " ${ staged_mod_dir } /extra/" 2>/dev/null || true
done
staged_mod_count = $( find " ${ staged_mod_dir } " -type f -name "*.ko*" 2>/dev/null | wc -l)
fi
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Staged kernel modules: ${ staged_mod_count } "
2026-08-13 11:28:40 +08:00
export AMLBIAN_KERNEL_MODULES_DIR = " ${ staged_mod_dir } "
export AMLBIAN_KERNEL_VERSION = " ${ kernel_release } "
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-KERNEL AMLBIAN_KERNEL_MODULES_DIR= ${ AMLBIAN_KERNEL_MODULES_DIR } "
2026-08-13 11:28:40 +08:00
depmod -a -b ${ modules_staging_dir } ${ kernel_release }
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-KERNEL after depmod ... $( ls -l ${ modules_staging_dir } ) "
2026-08-13 11:28:40 +08:00
# Stage DTBs deterministically for packaging.
rm -rf " ${ dtb_staging_dir } "
mkdir -p " ${ dtb_staging_dir } "
2026-11-11 08:50:43 +08:00
local dtb_found = 0 dtb_target dtb_name dtb_path dtb_output_name
2026-08-13 11:28:40 +08:00
for dtb_target in ${ KERNEL_DEVICETREE } ; do
dtb_name = " $( basename " ${ dtb_target } " ) "
dtb_path = " $( find " ${ build_dir } /arch/arm64/boot/dts" \
" ${ build_dir } /common_drivers/arch/arm64/boot/dts" \
" ${ aml_comp_kernel_path } /arch/arm64/boot/dts" -type f -name " ${ dtb_name } " 2>/dev/null | head -1) "
2026-11-11 08:50:43 +08:00
2026-08-13 11:28:40 +08:00
if [[ -n " ${ dtb_path } " ]] ; then
2026-11-11 08:50:43 +08:00
dtb_output_name = " ${ dtb_name /_rt.dtb/.dtb } "
cp -f " ${ dtb_path } " " ${ dtb_staging_dir } / ${ dtb_output_name } "
2026-08-13 11:28:40 +08:00
dtb_found = 1
fi
done
if [[ ${ dtb_found } -eq 0 ]] ; then
find " ${ build_dir } /common_drivers/arch/arm64/boot/dts/amlogic" \
" ${ build_dir } /arch/arm64/boot/dts/amlogic" -type f -name "*.dtb" 2>/dev/null | while read -r one_dtb; do
cp -f " ${ one_dtb } " " ${ dtb_staging_dir } /" 2>/dev/null || true
done
fi
local dtb_count
dtb_count = $( find " ${ dtb_staging_dir } " -type f -name "*.dtb" 2>/dev/null | wc -l)
if [[ ${ dtb_count } -gt 0 ]] ; then
export AMLBIAN_KERNEL_DTB_DIR = " ${ dtb_staging_dir } "
display_alert "COMPILE-KERNEL AMLBIAN_KERNEL_DTB_DIR= ${ AMLBIAN_KERNEL_DTB_DIR } (count= ${ dtb_count } )"
2026-11-11 08:50:43 +08:00
echo "SOURCE_DIR= ${ SOURCE_DIR } "
echo "build_dir= ${ build_dir } "
echo "dtb_staging_dir= ${ dtb_staging_dir } "
if [ $( echo " ${ KERNEL_DEVICETREE } " | wc -w ) -gt 1 ] ; then
${ SOURCE_DIR } /../prebuilt/hosttools/kernel/dtbTool -o ${ dtb_staging_dir } /multi.dtb -p ${ build_dir } /scripts/dtc/ ${ dtb_staging_dir } /
else
install -m 0644 ${ dtb_staging_dir } /${ KERNEL_DEVICETREE } ${ dtb_staging_dir } /multi.dtb
fi
aml_log info "COMPILE-KERNEL multi.dtb will be compressing"
mv ${ dtb_staging_dir } /multi.dtb ${ dtb_staging_dir } /multi.dtb.orig
gzip -c ${ dtb_staging_dir } /multi.dtb.orig > ${ dtb_staging_dir } /multi.dtb
rm -fv ${ dtb_staging_dir } /multi.dtb.orig
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL No DTBs staged for packaging"
2026-08-13 11:28:40 +08:00
fi
else
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL Could not determine kernel release; modules staging skipped"
2026-08-13 11:28:40 +08:00
fi
fi
2026-11-11 08:50:43 +08:00
aml_log debug "COMPILE-KERNEL make img: ${ LINENO } "
2026-08-13 11:28:40 +08:00
# local make_exit=${PIPESTATUS[0]}
# cd "${original_dir}"
# if [[ ${make_exit} -eq 0 ]]; then
# used_aml_comp_kernel=1
# export kernel_build_dir="${build_dir}"
2026-11-11 08:50:43 +08:00
# aml_log info "COMPILE-KERNEL aml-comp kernel built: ${kernel_build_dir} success"
2026-08-13 11:28:40 +08:00
# else
2026-11-11 08:50:43 +08:00
# aml_log warn "COMPILE-KERNEL aml-comp kernel build failed or not supported; will fallback to Armbian build"
2026-08-13 11:28:40 +08:00
# used_aml_comp_kernel=0
# fi
else
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL aml-comp Kernel not buildable here; using Armbian kernel"
2026-08-13 11:28:40 +08:00
fi
else
2026-11-11 08:50:43 +08:00
aml_log warn "COMPILE-KERNEL aml-comp Kernel not available, using Armbian kernel"
2026-08-13 11:28:40 +08:00
fi
}
function package_kernel() {
2026-11-11 08:50:43 +08:00
aml_log info "PACKAGE-KERNEL start package kernel"
2026-08-13 11:28:40 +08:00
# Get kernel version for packaging
local kernel_version = $( get_kernel_version " ${ KERNEL_SRC_DIR } " )
if [[ -z " ${ kernel_version } " ]] ; then
kernel_version = "unknown- $( date +%Y%m%d) "
2026-11-11 08:50:43 +08:00
aml_log warn "AMLOGIC : get kernel version failed, set as ${ kernel_version } ${ BOARD } "
2026-08-13 11:28:40 +08:00
fi
# Package kernel image DEB
if package_kernel_deb_amlbian_main " ${ KERNEL_SRC_DIR } " " ${ kernel_version } " " ${ LINUX_DEFCONFIG :- defconfig } " ; then
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Kernel Image DEB package created successfully"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "COMPILE-KERNEL Failed to create Kernel Image DEB package"
exit 1
2026-08-13 11:28:40 +08:00
fi
# Package kernel modules DEB
if package_kmod_deb_amlbian_main " ${ KERNEL_SRC_DIR } " " ${ kernel_version } " ; then
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL Kernel Modules DEB package created successfully"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "COMPILE-KERNEL Failed to create Kernel Modules DEB package"
exit 1
2026-08-13 11:28:40 +08:00
fi
# Package DTB DEB
if package_dtb_deb_amlbian_main " ${ KERNEL_SRC_DIR } " " ${ kernel_version } " ; then
2026-11-11 08:50:43 +08:00
aml_log info "COMPILE-KERNEL DTB DEB package created successfully"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "COMPILE-KERNEL Failed to create DTB DEB package"
exit 1
2026-08-13 11:28:40 +08:00
fi
}
function prepare_modules() {
# bash ${WORKDIR}/modules_install.sh ${D} ${STAGING_KERNEL_DIR}/${COMMON_DRV_PATH}/ ${KERNEL_VERSION} ${NAND_FLAG} ${SOC}
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: prepare_modules bash ${ AMLBIAN_DIR } /scripts/modules_install.sh ${ KERNEL_SOURCE_DIR } /build ${ COMMON_DRIVERS_DIR } ${ AMLBIAN_KERNEL_VERSION } false"
2026-08-13 11:28:40 +08:00
bash ${ AMLBIAN_DIR } /scripts/modules_install.sh ${ KERNEL_SOURCE_DIR } /build/modules-staging ${ COMMON_DRIVERS_DIR } ${ AMLBIAN_KERNEL_VERSION } false
}
function amlbian_build_kernel() {
if ! is_unit_enabled "kernel" ; then
2026-11-11 08:50:43 +08:00
aml_log info "[SKIP] >> kernel disable in BUILD_UNITS"
2026-08-13 11:28:40 +08:00
return
fi
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Building kernel ${ KERNEL_VERSION } ${ BOARD } "
aml_log debug "AMLOGIC: Source directory ${ SOURCE_DIR } "
aml_log debug "AMLOGIC: Output directory ${ OUTPUT_DIR } "
aml_log debug "AMLOGIC: Defconfig ${ DEFCONFIG } "
2026-08-13 11:28:40 +08:00
local aml_comp_kernel_path = ${ SOURCE_DIR } /${ KERNEL_VER }
export KERNEL_SRC_DIR = ${ aml_comp_kernel_path }
# Check if source directory exists
if [[ ! -d " ${ aml_comp_kernel_path } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: Kernel source not found ${ aml_comp_kernel_path } "
2026-08-13 11:28:40 +08:00
return 1
fi
# Create output directory
mkdir -p " ${ OUTPUT_DIR } "
# Clean if requested
if [[ " ${ CLEAN_BUILD } " == "yes" ]] ; then
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Cleaning build directory ${ aml_comp_kernel_path } "
2026-08-13 11:28:40 +08:00
cd " ${ aml_comp_kernel_path } "
make clean 2>/dev/null || true
make mrproper 2>/dev/null || true
fi
# AMLOGIC: Uncomment below when ready to build
# make "${DEFCONFIG}"
# make -j$(nproc) Image
# make -j$(nproc) modules
# make -j$(nproc) dtbs
compile_kernel ${ aml_comp_kernel_path }
prepare_modules
package_kernel ${ aml_comp_kernel_path }
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
2026-11-11 08:50:43 +08:00
aml_log info "AMLIbian Kernel Build Script" "Version: ${ KERNEL_VERSION } , Board: ${ BOARD } "
2026-08-13 11:28:40 +08:00
amlbian_build_kernel
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Kernel build completed" " ${ OUTPUT_DIR } "
2026-08-13 11:28:40 +08:00
# ==============================================================================
# End of build-kernel.sh
2026-11-11 08:50:43 +08:00
# ==============================================================================