#!/usr/bin/env bash # # SPDX-License-Identifier: GPL-2.0 # # Copyright (c) 2026 AMLOGIC # # AMLOGIC Overlay Extension for Armbian Build Framework # # This extension implements all hook points for mesons6-bq201 board # with local source integration and custom packaging. # 在加载任何 hook 函数之前,先禁用可能有冲突的基础函数 extension_hook_opt_out compile_atf extension_hook_opt_out compile_crust extension_hook_opt_out config_pre_main extension_hook_opt_out customize_image function host_pre_docker_launch__amlogic_pass_build_units() { display_alert "AMLOGIC: Preparing BUILD_UNITS for Docker" "${BUILD_UNITS:-not set}" "info" # 如果 BUILD_UNITS 已设置 if [[ -n "${BUILD_UNITS}" ]]; then # 添加环境变量到 Docker DOCKER_EXTRA_ARGS+=("--env" "BUILD_UNITS=${BUILD_UNITS[*]}") display_alert "AMLOGIC: Set BUILD_UNITS environment variable" "${BUILD_UNITS}" "debug" else display_alert "AMLOGIC: BUILD_UNITS not set, using defaults in container" "" "warn" fi if [[ -n "${RELEASE_SPIN}" ]]; then # 添加环境变量到 Docker DOCKER_EXTRA_ARGS+=("--env" "RELEASE_SPIN=${RELEASE_SPIN}") display_alert "AMLOGIC: Set RELEASE_SPIN environment variable" "${RELEASE_SPIN}" "debug" else display_alert "AMLOGIC: RELEASE_SPIN not set, using defaults in container" "" "warn" fi if [[ -n "${RELEASE_VERSION}" ]]; then # 添加环境变量到 Docker DOCKER_EXTRA_ARGS+=("--env" "RELEASE_VERSION=${RELEASE_VERSION}") display_alert "AMLOGIC: Set RELEASE_VERSION environment variable" "${RELEASE_VERSION}" "debug" else display_alert "AMLOGIC: RELEASE_VERSION not set, using defaults in container" "" "warn" fi if [[ -n "${DATETIME}" ]]; then # 添加环境变量到 Docker DOCKER_EXTRA_ARGS+=("--env" "DATETIME=${DATETIME}") display_alert "AMLOGIC: Set DATETIME environment variable" "${DATETIME}" "debug" else display_alert "AMLOGIC: DATETIME not set, using defaults in container" "" "warn" fi } ### Phase 1: Initialization & Configuration ### function config_pre_main__amlogic_overlay() { display_alert "AMLOGIC TODO" "config_pre_main hook called for ${BOARD}" "info" # Placeholder for custom configuration before main config loading } function extension_prepare_config__amlogic_overlay() { display_alert "AMLOGIC Overlay Extension" "Loaded for ${BOARD}" "info" # 这里可以添加其他配置 } # 将 package 字符串解析后追加到指定数组,并自动去重 # 用法: # local packages=() # append_packages packages "${PACKAGE_LIST_AML}" # function append_packages() { local -n _target_array=$1 local package_str="$2" local line pkg local -A seen=() # 记录已有元素,避免重复 for pkg in "${_target_array[@]}"; do seen["$pkg"]=1 done while IFS= read -r line; do [[ -z "${line}" ]] && continue for pkg in $line; do [[ -z "${pkg}" ]] && continue if [[ -z "${seen[$pkg]}" ]]; then _target_array+=("${pkg}") seen["$pkg"]=1 fi done done <<< "$(echo "${package_str}" | tr '\\' ' ')" } function extension_finish_config__aml_packages() { display_alert "Adding Amlogic packages to rootfs" "${EXTENSION}" "info" # 验证全局变量是否存在 if [[ -z "${PACKAGE_LIST_AML}" ]]; then display_alert "PACKAGE_LIST_AML is empty!" "${EXTENSION}" "warn" return 0 fi if [[ "${SHOW_DEBUG}" == "yes" ]]; then display_alert "Old package list" "${EXTRA_PACKAGES_ROOTFS}" "debug" fi # 清除可能存在的缓存引用问题 unset EXTRA_PACKAGES_ROOTFS unset EXTRA_PACKAGES_ROOTFS_REFS append_packages packages "${COMMON_PACKAGE_LIST_ADDITIONAL}" append_packages packages "${PACKAGE_LIST_AML}" # 检查数组是否为空 if [[ ${#packages[@]} -eq 0 ]]; then display_alert "No packages found in PACKAGE_LIST_AML" "${EXTENSION}" "warn" return 0 fi # 安全地获取文件路径和行号 local file_path="" local line_number="" if [[ -n "${EXTENSION_FILE}" && -f "${EXTENSION_FILE}" ]]; then file_path="${EXTENSION_FILE}" # 使用更安全的方式获取行号 line_number=$(grep -n "^function extension_finish_config__aml_packages" "${file_path}" 2>/dev/null | head -1 | cut -d: -f1) fi # 设置默认值 : "${file_path:=unknown}" : "${line_number:=0}" # 逐个添加包和引用 for pkg in "${packages[@]}"; do if [[ -n "${pkg}" ]]; then EXTRA_PACKAGES_ROOTFS="${EXTRA_PACKAGES_ROOTFS} ${pkg}" EXTRA_PACKAGES_ROOTFS_REFS="${EXTRA_PACKAGES_ROOTFS_REFS} ${pkg}:${file_path}:${line_number}" fi done # 清理空格 EXTRA_PACKAGES_ROOTFS=$(echo "${EXTRA_PACKAGES_ROOTFS}" | xargs) EXTRA_PACKAGES_ROOTFS_REFS=$(echo "${EXTRA_PACKAGES_ROOTFS_REFS}" | xargs) display_alert "Added ${#packages[@]} packages to rootfs cache" "${EXTENSION}" "info" if [[ "${SHOW_DEBUG}" == "yes" ]]; then display_alert "Current package list" "${EXTRA_PACKAGES_ROOTFS}" "debug" fi return 0 } # 注意: 在进入docker分别生效, 对应路径不过 function load_local_source() { source "${EXTENSION_DIR}/../lib/function-env.sh" source "${EXTENSION_DIR}/../lib/function-common.sh" source "${EXTENSION_DIR}/../lib/function-package.sh" source "${EXTENSION_DIR}/../lib/function-initrd.sh" source "${EXTENSION_DIR}/../lib/function-img.sh" #直接执行,规避需要mount非必要的依赖进入docker if [[ "${ARMBIAN_RUNNING_IN_CONTAINER}" == "yes" ]]; then display_alert "AMLOGIC" "debs had built, check it " "info" # if ! check_files_exist ${EXTENSION_DIR}/../output/linux-u-boot-*.deb ${SHOW_DEBUG};then # display_alert "AMLOGIC" "NO uboot deb!!!" "err" # exit 1 # fi check_files_exist ${EXTENSION_DIR}/../../output/linux-u-boot-*.deb ${SHOW_DEBUG} check_files_exist ${EXTENSION_DIR}/../../output/linux-image-*.deb ${SHOW_DEBUG} check_files_exist ${EXTENSION_DIR}/../../output/linux-dtb-*.deb ${SHOW_DEBUG} check_files_exist ${EXTENSION_DIR}/../../output/linux-modules-*.deb ${SHOW_DEBUG} # gst-aml-vendor build runs during install_common (docker phase, needs # rootfs as cross-compile sysroot); bring its function into scope. source "${EXTENSION_DIR}/../scripts/multimedia/gst/build-gst-aml-vendor.sh" else amlbian_init_source_paths display_alert "AMLOGIC" ">> Start build uboot " "info" source "${EXTENSION_DIR}/../scripts/build-uboot.sh" display_alert "AMLOGIC" ">> Start build kernel " "info" source "${EXTENSION_DIR}/../scripts/build-kernel.sh" display_alert "AMLOGIC" ">> Start modules-load" "info" source "${EXTENSION_DIR}/../scripts/modules-load/build-modules-load.sh" display_alert "AMLOGIC" ">> Start udev" "info" source "${EXTENSION_DIR}/../scripts/udev/build-udev.sh" display_alert "AMLOGIC" ">> Start Gpu" "info" source "${EXTENSION_DIR}/../scripts/gpu/build-gpu.sh" display_alert "AMLOGIC" ">> Start Npu" "info" source "${EXTENSION_DIR}/../scripts/npu/build-npu.sh" display_alert "AMLOGIC" ">> Start wifi" "info" source "${EXTENSION_DIR}/../scripts/wifi/build-wifi.sh" display_alert "AMLOGIC" ">> Start isp" "info" source "${EXTENSION_DIR}/../scripts/isp/build-isp.sh" display_alert "AMLOGIC" ">> Start bt" "info" source "${EXTENSION_DIR}/../scripts/bt/build-bt.sh" display_alert "AMLOGIC" ">> Start build media-modules " "info" source "${EXTENSION_DIR}/../scripts/build-media-modules.sh" source "${EXTENSION_DIR}/../scripts/build-tahoe.sh" display_alert "AMLOGIC" ">> Start build optee" "info" source "${EXTENSION_DIR}/../scripts/multimedia/tee/build-optee-userspace.sh" source "${EXTENSION_DIR}/../scripts/multimedia/tee/build-tee-supplicant.sh" source "${EXTENSION_DIR}/../scripts/multimedia/tee/build-optee-video-firmware.sh" display_alert "AMLOGIC" ">> Start build libion " "info" source "${EXTENSION_DIR}/../scripts/build-libion.sh" display_alert "AMLOGIC" ">> Start build libge2d " "info" source "${EXTENSION_DIR}/../scripts/build-libge2d.sh" display_alert "AMLOGIC" ">> Start build liblog " "info" source "${EXTENSION_DIR}/../scripts/build-liblog.sh" display_alert "AMLOGIC" ">> Start build libencoder " "info" source "${EXTENSION_DIR}/../scripts/multimedia/libencoder/build-libversenc-prebuilt.sh" source "${EXTENSION_DIR}/../scripts/multimedia/libencoder/build-libamvenc.sh" source "${EXTENSION_DIR}/../scripts/multimedia/libencoder/build_libtahoevenc.sh" source "${EXTENSION_DIR}/../scripts/multimedia/libencoder/build_libamljpegenc.sh" display_alert "AMLOGIC" ">> Start build dma-allocator " "info" source "${EXTENSION_DIR}/../scripts/multimedia/build-dma-allocator.sh" display_alert "AMLOGIC" ">> Start build amaudioutils " "info" source "${EXTENSION_DIR}/../scripts/multimedia/build-aml-amaudioutils.sh" display_alert "AMLOGIC" ">> Start build mediahal-sdk " "info" source "${EXTENSION_DIR}/../scripts/multimedia/build-aml-mediahal-sdk.sh" display_alert "AMLOGIC" ">> Start build vendor-files " "info" source "${EXTENSION_DIR}/../scripts/multimedia/tvserver/build-vendor-files.sh" # source "${EXTENSION_DIR}/../scripts/multimedia/tvserver/build-aml-tvserver.sh" source "${EXTENSION_DIR}/../scripts/multimedia/tvserver/build-aml-ubootenv.sh" display_alert "AMLOGIC" ">> Start build linux-uapi-headers " "info" source "${EXTENSION_DIR}/../scripts/build-linux-uapi-headers.sh" display_alert "AMLOGIC" ">> Start build logcat " "info" source "${EXTENSION_DIR}/../scripts/devtools/android-tools-logcat/build-logcat.sh" if ! is_unit_enabled "rootfs"; then echo "rootfs NOT in BUILD_UNITS, exit immediately" exit 1 fi fi display_alert "AMLOGIC" "Loading functions done" "info" } ### Phase 2: Host Preparation ### function add_host_dependencies__amlogic_overlay() { display_alert "AMLOGIC TODO" "add_host_dependencies hook called" "info" # EXTRA_BUILD_DEPS+=" custom-package" if [[ "${ARMBIAN_RUNNING_IN_CONTAINER}" == "yes" ]]; then display_alert "AMLOGIC" "TODO>> Check enivornment is ready? " "info" # 检查 crc32 工具是否存在 for uboot build support # if ! command -v crc32 &> /dev/null; then # display_alert "AMLOGIC" "crc32 tool not found, adding libarchive-zip-perl" "warn" # # 在 Docker 容器内需要安装,support multilib # # apt-get install -y libc6:i386 libstdc++6:i386 # dpkg --add-architecture i386 # apt-get update # apt-get install -y libc6:i386 libstdc++6:i386 zlib1g:i386 # apt install -y --allow-downgrades --allow-remove-essential \ # -o Dpkg::Options::="--force-depends" \ # libarchive-zip-perl zlib1g cmake ninja-build doxygen xxd graphviz # else # display_alert "AMLOGIC" "crc32 tool already available" "debug" # fi else config_source_board_file fi load_local_source # EXTRA_BUILD_DEPS+=" custom-package" if [[ "${ARMBIAN_RUNNING_IN_CONTAINER}" == "yes" ]]; then print_all_info_for_debug # display_alert "AMLOGIC" "TODO>> Start build uboot " "info" # display_alert "AMLOGIC" "TODO>> Start build kernel " "info" else display_alert "AMLOGIC" "Ignore host dependencies in host env............" "warn" fi } ### Phase 3: Artifact Building - U-Boot ### function fetch_custom_uboot__amlogic_overlay() { display_alert "AMLOGIC TODO" "fetch_custom_uboot hook called - using pre-fetched local U-Boot source at ${LOCAL_UBOOT_PATH:-/not/set}" "info" # Skip git fetch and use local pre-fetched U-Boot source if [[ "${USE_LOCAL_SOURCES}" == "yes" ]]; then display_alert "AMLOGIC" "Skipping U-Boot git fetch, using local pre-fetched source" "info" # Set environment variables to tell Armbian to skip git operations export USE_LOCAL_UBOOT_SOURCE="yes" export SKIP_GIT_FETCH="yes" # Copy local source to build directory if path exists if [[ -d "${LOCAL_UBOOT_PATH}" ]]; then local uboot_dir="${SRC}/cache/sources/u-boot-${BOARD}" display_alert "AMLOGIC" "Copying pre-fetched U-Boot from ${LOCAL_UBOOT_PATH} to ${uboot_dir}" "info" # Clean target directory if exists rm -rf "${uboot_dir}" mkdir -p "${uboot_dir}" # Copy entire directory including .git folder # Using cp -r with trailing dot to copy hidden files cp -r "${LOCAL_UBOOT_PATH}/." "${uboot_dir}/" 2>/dev/null || { display_alert "AMLOGIC ERROR" "Failed to copy U-Boot source from ${LOCAL_UBOOT_PATH}" "err" return 1 } # Verify .git directory exists if [[ -d "${uboot_dir}/.git" ]]; then display_alert "AMLOGIC" "Local U-Boot source contains .git directory, git info will be available" "info" else display_alert "AMLOGIC WARNING" "Local U-Boot source missing .git directory" "warn" fi # Set additional variables to ensure Armbian uses this source export BOOTSOURCE="${uboot_dir}" export BOOTBRANCH="local" export BOOTPATCHDIR="none" display_alert "AMLOGIC" "U-Boot source prepared at ${uboot_dir}, git fetch skipped" "info" return 0 # Success, skip further git fetch operations else display_alert "AMLOGIC ERROR" "Local U-Boot path not found: ${LOCAL_UBOOT_PATH}" "err" return 1 fi fi # Fallback to default behavior if not using local sources display_alert "AMLOGIC" "Falling back to remote U-Boot source fetching" "warn" return 1 # Return non-zero to let Armbian perform default git fetch } function build_custom_uboot__amlogic_overlay() { display_alert "AMLOGIC TODO" "build_custom_uboot hook called" "info" # Custom U-Boot build flags and steps # make AMLOGIC_CUSTOM_FLAG=1 } function post_uboot_custom_postprocess__amlogic_overlay() { display_alert "AMLOGIC TODO" "post_uboot_custom_postprocess hook called with arg: ${1}" "info" # Post-process U-Boot binaries (signing, custom headers, etc.) } function pre_config_uboot_target__amlogic_overlay() { display_alert "AMLOGIC TODO" "pre_config_uboot_target hook called" "info" # Modify U-Boot config before configuration } function post_config_uboot_target__amlogic_overlay() { display_alert "AMLOGIC TODO" "post_config_uboot_target hook called" "info" # Modify U-Boot config after configuration # scripts/config --enable CONFIG_AMLOGIC_FEATURE } ### Phase 3: Artifact Building - Kernel ### function fetch_custom_kernel__amlogic_overlay() { display_alert "AMLOGIC TODO" "fetch_custom_kernel hook called - using pre-fetched local kernel source at ${LOCAL_KERNEL_PATH:-/not/set}" "info" # Skip git fetch and use local pre-fetched kernel source if [[ "${USE_LOCAL_SOURCES}" == "yes" ]]; then display_alert "AMLOGIC" "Skipping kernel git fetch, using local pre-fetched source" "info" # Set environment variables to tell Armbian to skip git operations export USE_LOCAL_KERNEL_SOURCE="yes" export SKIP_GIT_FETCH_KERNEL="yes" # Copy local source to build directory if path exists if [[ -d "${LOCAL_KERNEL_PATH}" ]]; then local kernel_dir="${SRC}/cache/sources/linux-${BOARD}" display_alert "AMLOGIC" "Copying pre-fetched kernel from ${LOCAL_KERNEL_PATH} to ${kernel_dir}" "info" # Clean target directory if exists rm -rf "${kernel_dir}" mkdir -p "${kernel_dir}" # Copy entire directory including .git folder # Using cp -r with trailing dot to copy hidden files cp -r "${LOCAL_KERNEL_PATH}/." "${kernel_dir}/" 2>/dev/null || { display_alert "AMLOGIC ERROR" "Failed to copy kernel source from ${LOCAL_KERNEL_PATH}" "err" return 1 } # Verify .git directory exists if [[ -d "${kernel_dir}/.git" ]]; then display_alert "AMLOGIC" "Local kernel source contains .git directory, git info will be available" "info" else display_alert "AMLOGIC WARNING" "Local kernel source missing .git directory" "warn" fi # Set additional variables to ensure Armbian uses this source export KERNELSOURCE="${kernel_dir}" export KERNELBRANCH="local" export KERNELPATCHDIR="none" display_alert "AMLOGIC" "Kernel source prepared at ${kernel_dir}, git fetch skipped" "info" return 0 # Success, skip further git fetch operations else display_alert "AMLOGIC ERROR" "Local kernel path not found: ${LOCAL_KERNEL_PATH}" "err" return 1 fi fi # Fallback to default behavior if not using local sources display_alert "AMLOGIC" "Falling back to remote kernel source fetching" "warn" return 1 # Return non-zero to let Armbian perform default git fetch } function build_custom_kernel__amlogic_overlay() { display_alert "AMLOGIC TODO" "build_custom_kernel hook called" "info" # Custom kernel build steps } function post_kernel_custom_postprocess__amlogic_overlay() { display_alert "AMLOGIC TODO" "post_kernel_custom_postprocess hook called with arg: ${1}" "info" # Post-process kernel image and modules } ### Phase 3: Artifact Building - Firmware ### function post_firmware_custom_postprocess__amlogic_overlay() { display_alert "AMLOGIC TODO" "post_firmware_custom_postprocess hook called" "info" # Add custom firmware files for AMLOGIC # cp "${EXTENSION_DIR}/firmware/"* "${destination}/lib/firmware/" } ### Phase 4: Root Filesystem Creation ### function pre_install_distribution_specific__amlogic_overlay() { display_alert "AMLOGIC TODO" "pre_install_distribution_specific hook called" "info" # Run before distribution packages are installed } function customize_image__amlogic_overlay() { display_alert "AMLOGIC TODO" "customize_image hook called for ${BOARD}" "info" # Main image customization hook # Example customizations: # chroot_sdcard echo "mesons6-bq201" > /etc/hostname # chroot_sdcard echo "AMLOGIC_CUSTOM=1" >> /etc/environment # Install custom packages # install_deb_chroot "${EXTENSION_DIR}/packages/"*.deb } function post_armbian_repo_customize_image__amlogic_overlay() { display_alert "AMLOGIC TODO" "post_armbian_repo_customize_image hook called" "info" # Customizations after Armbian repository is enabled } function post_repo_customize_image__amlogic_overlay() { display_alert "AMLOGIC TODO" "post_repo_customize_image hook called" "info" # Final customizations before image creation } ### Phase 5: Image Assembly ### function write_uboot_platform__amlogic_overlay() { display_alert "AMLOGIC TODO" "write_uboot_platform hook called with args: $@" "info" # Custom U-Boot installation for AMLOGIC boards # Example for SPI flash: # if [[ "${1}" == "spi" ]]; then # dd if="${2}/u-boot-spi.bin" of="${LOOP}" conv=fsync bs=1 count=444 status=none # dd if="${2}/u-boot-spi.bin" of="${LOOP}" conv=fsync bs=512 skip=1 seek=1 status=none # fi } function write_uboot_platform_mtd__amlogic_overlay() { display_alert "AMLOGIC TODO" "write_uboot_platform_mtd hook called with args: $@" "info" # U-Boot installation for MTD devices } function setup_write_uboot_platform__amlogic_overlay() { display_alert "AMLOGIC TODO" "setup_write_uboot_platform hook called" "info" # Setup for U-Boot installation } function post_image_creation__amlogic_overlay() { display_alert "AMLOGIC TODO" "post_image_creation hook called" "info" # Post-process the final image # Example custom packaging: # local image_file="${DESTIMG}/${BOARD}.img" # if [[ -f "${image_file}" ]]; then # display_alert "AMLOGIC custom packaging" "Processing ${image_file}" "info" # # Sign image # # Compress with custom options # # Generate metadata # fi } ### Additional hooks for complete coverage ### function extension_metadata_ready__amlogic_overlay() { display_alert "AMLOGIC TODO" "extension_metadata_ready hook called" "info" # Process extension metadata after build } function run_after_build__amlogic_overlay() { display_alert "AMLOGIC TODO" "run_after_build hook called" "info" # Run after build completes successfully # Package img final... amlbian_package_img if [[ -n "${SET_OWNER_TO_UID}" ]]; then display_alert "AMLOGIC" "Resetting cache owners" "info" reset_uid_owner "${SRC}/cache" fi } function uboot_custom_postprocess__amlogic_overlay() { display_alert "AMLOGIC TODO" "uboot_custom_postprocess hook called with arg: ${1}" "info" # Legacy U-Boot postprocess hook } function compile_atf__amlogic_overlay() { display_alert "AMLOGIC TODO" "compile_atf hook called" "info" # Custom ATF compilation for AMLOGIC } function compile_crust__amlogic_overlay() { display_alert "AMLOGIC TODO" "compile_crust hook called" "info" # Custom crust compilation for AMLOGIC } function pre_package_uboot_image__amlogic_overlay() { display_alert "AMLOGIC TODO" "pre_package_uboot_image hook called" "info" # Custom processing before U-Boot image packaging } ### Board-specific initialization ### function family_config__amlogic_overlay() { display_alert "AMLOGIC TODO" "family_config hook called for ${BOARD_FAMILY}" "info" # Board family configuration } function board_config__amlogic_overlay() { display_alert "AMLOGIC TODO" "board_config hook called for ${BOARD}" "info" # Board-specific configuration } ### Helper functions ### function display_amlogic_info() { display_alert "AMLOGIC Overlay Info" "Board: ${BOARD}, Family: ${BOARD_FAMILY}, Branch: ${BRANCH}" "info" display_alert "AMLOGIC Local Sources" "UBOOT: ${LOCAL_UBOOT_PATH:-not set}, KERNEL: ${LOCAL_KERNEL_PATH:-not set}" "info" } function host_pre_docker_launch__amlogic_overlay() { display_alert "AMLOGIC DOINIG" "Adding local source mount for Docker: ${AML_COMP_DIR}" "info" print_all_info_for_debug env_mount_to_docker } # Enable the extension automatically when sourced display_alert "AMLOGIC Overlay Extension" "Loaded for ${BOARD:-unknown board}" "info"