#!/usr/bin/env bash # SPDX-License-Identifier: (GPL-2.0-only OR MIT) # Copyright (C) 2026 Amlogic, Inc. All rights reserved ################################################################################ # Amlbian Modules Load Compilation Module # # Purpose: Compile and package modules load scripts for kernel modules # Provides: modules-load.sh and aml_wifi_load.sh installation ################################################################################ set -e # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" AMLBIAN_DIR="$(dirname "$(dirname "${SCRIPT_DIR}")")" BASE_DIR="$(dirname "$(dirname "${AMLBIAN_DIR}")")" MODULE_BUILD_DIR="${SRC}/output/.tmp" # ============================================================================== # Shared Helper Functions # ============================================================================== # ------------------------------------------------------------------------------ # Step 1: Package Information Definition # ------------------------------------------------------------------------------ # Initialize package metadata for Modules Load scripts function _modules_load_init_package_info() { PKG_MAINTAINER="Armbian Build " PKG_DESCRIPTION="Modules load scripts for Amlogic kernel modules" PKG_NAME="aml-modules-load" PKG_VERSION="1.0" PKG_ARCH="all" COMPONENT="MODULES-LOAD" # Source directory for modules load scripts MODULES_SRC_DIR="${SCRIPT_DIR}/files" } # ------------------------------------------------------------------------------ # Step 2: Build Directory Structure Setup # ------------------------------------------------------------------------------ # Create layered build directory structure # Parameters: # $1 - base name (e.g. "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}") # Sets: MODULE_BUILD_DIR, BASE_BUILD_DIR, SOURCE_DIR, BUILD_DIR, INSTALL_DIR, PATCH_DIR, # DEB_PACKAGE_DIR, DEBIAN_DIR function _modules_load_create_build_dirs() { local base_name="$1" mkdir -p "${MODULE_BUILD_DIR}" # 基础构建目录 BASE_BUILD_DIR="${MODULE_BUILD_DIR}/${base_name}" rm -rf ${BASE_BUILD_DIR} # 分层目录 SOURCE_DIR="${BASE_BUILD_DIR}/source" # 源码目录 BUILD_DIR="${BASE_BUILD_DIR}/build" # 编译目录 INSTALL_DIR="${BASE_BUILD_DIR}/install" # 安装目录 PATCH_DIR="${BASE_BUILD_DIR}/patch" # 补丁目录 # 创建所有目录 mkdir -p "${SOURCE_DIR}" mkdir -p "${BUILD_DIR}" mkdir -p "${INSTALL_DIR}" mkdir -p "${PATCH_DIR}" # 在BUILD_DIR下创建deb/包名目录 DEB_PACKAGE_DIR="${BUILD_DIR}/deb/${base_name}" mkdir -p "${DEB_PACKAGE_DIR}" # 安装目录的子目录 DEBIAN_DIR="${INSTALL_DIR}/DEBIAN" mkdir -p "${DEBIAN_DIR}" mkdir -p "${INSTALL_DIR}/etc" mkdir -p "${INSTALL_DIR}/usr/bin" mkdir -p "${INSTALL_DIR}/usr/chinese-fonts" mkdir -p "${INSTALL_DIR}/etc/systemd/system" } # Display build directory info # Parameters: # $1 - log prefix function _modules_load_display_build_info() { local log_prefix="$1" aml_log debug "${log_prefix} Source dir: ${SOURCE_DIR}" aml_log debug "${log_prefix} Build dir: ${BUILD_DIR}" aml_log debug "${log_prefix} Install dir: ${INSTALL_DIR}" aml_log debug "${log_prefix} Patch dir: ${PATCH_DIR}" aml_log debug "${log_prefix} Deb package dir: ${DEB_PACKAGE_DIR}" aml_log debug "${log_prefix} Modules source dir: ${MODULES_SRC_DIR}" } # ------------------------------------------------------------------------------ # Step 3: Source File Validation and Copying # ------------------------------------------------------------------------------ # Validate and copy modules-load.sh function _modules_load_copy_modules_load_sh() { aml_log info "COMPILE-${COMPONENT} Copying modules-load.sh from ${MODULES_SRC_DIR}" if [[ -f "${MODULES_SRC_DIR}/modules-load.sh" ]]; then cp "${MODULES_SRC_DIR}/modules-load.sh" "${INSTALL_DIR}/etc/" chmod 0755 "${INSTALL_DIR}/etc/modules-load.sh" aml_log info "COMPILE-${COMPONENT} Copied modules-load.sh" else aml_log error "COMPILE-${COMPONENT} modules-load.sh not found at ${MODULES_SRC_DIR}/modules-load.sh" exit 1 fi } # Validate and copy install_chinese_font.sh function _modules_load_copy_install_chinese_font_sh() { aml_log info "COMPILE-${COMPONENT} Copying install_chinese_font.sh from ${MODULES_SRC_DIR}" if [[ -f "${MODULES_SRC_DIR}/install_chinese_font.sh" ]]; then cp "${MODULES_SRC_DIR}/install_chinese_font.sh" "${INSTALL_DIR}/usr/chinese-fonts/" chmod 0755 "${INSTALL_DIR}/usr/chinese-fonts/install_chinese_font.sh" aml_log info "COMPILE-${COMPONENT} Copied install_chinese_font.sh" else aml_log error "COMPILE-${COMPONENT} install_chinese_font.sh not found at ${MODULES_SRC_DIR}/install_chinese_font.sh" exit 1 fi } # Validate and copy aml_wifi_load.sh function _modules_load_copy_aml_wifi_load_sh() { aml_log info "COMPILE-${COMPONENT} Copying aml_wifi_load.sh from ${MODULES_SRC_DIR}" if [[ -f "${MODULES_SRC_DIR}/aml_wifi_load.sh" ]]; then cp "${MODULES_SRC_DIR}/aml_wifi_load.sh" "${INSTALL_DIR}/usr/bin/" chmod 0755 "${INSTALL_DIR}/usr/bin/aml_wifi_load.sh" aml_log info "COMPILE-${COMPONENT} Copied aml_wifi_load.sh" else aml_log error "COMPILE-${COMPONENT} aml_wifi_load.sh not found at ${MODULES_SRC_DIR}/aml_wifi_load.sh" exit 1 fi } # Validate and copy set-usb-serial.sh function _modules_load_copy_aml_usb_serial_load_sh() { aml_log info "COMPILE-${COMPONENT} Copying set-usb-serial.sh from ${MODULES_SRC_DIR}" if [[ -f "${MODULES_SRC_DIR}/set-usb-serial.sh" ]]; then cp "${MODULES_SRC_DIR}/set-usb-serial.sh" "${INSTALL_DIR}/usr/bin/" chmod 0755 "${INSTALL_DIR}/usr/bin/set-usb-serial.sh" aml_log info "COMPILE-${COMPONENT} Copied set-usb-serial.sh" else aml_log error "COMPILE-${COMPONENT} set-usb-serial.sh not found at ${MODULES_SRC_DIR}/set-usb-serial.sh" exit 1 fi } # Validate and copy set_rt_conf.sh function _modules_load_copy_aml_rt_conf_sh() { if [ "$RT_LINUX" != "yes" ]; then return 0 fi mkdir -p "${INSTALL_DIR}/etc/kernel/realtime" aml_log info "COMPILE-${COMPONENT} Copying set_rt_conf.sh from ${MODULES_SRC_DIR}" if [[ -f "${MODULES_SRC_DIR}/set_rt_conf.sh" ]]; then cp "${MODULES_SRC_DIR}/set_rt_conf.sh" "${INSTALL_DIR}/usr/bin/" chmod 0755 "${INSTALL_DIR}/usr/bin/set_rt_conf.sh" cp "${MODULES_SRC_DIR}/set-rt-conf.service" "${INSTALL_DIR}/etc/systemd/system" aml_log info "COMPILE-${COMPONENT} Copied set_rt_conf.sh" else aml_log error "COMPILE-${COMPONENT} set_rt_conf.sh not found at ${MODULES_SRC_DIR}/set_rt_conf.sh" exit 1 fi } # ------------------------------------------------------------------------------ # Step 4: Debian Packaging Scripts Creation # ------------------------------------------------------------------------------ # Create Debian control file function _modules_load_create_control_file() { aml_log info "COMPILE-${COMPONENT} Creating control file DEBIAN/control" cat > "${DEBIAN_DIR}/control" < "${DEBIAN_DIR}/postinst" << EOF #!/bin/sh set -e if [ "\$1" = "configure" ]; then # Ensure scripts are executable chmod 0755 /etc/modules-load.sh 2>/dev/null || true chmod 0755 /usr/bin/aml_wifi_load.sh 2>/dev/null || true # Create symlink for backward compatibility if needed if [ ! -e /usr/sbin/aml_wifi_load.sh ] && [ -e /usr/bin/aml_wifi_load.sh ]; then ln -sf /usr/bin/aml_wifi_load.sh /usr/sbin/aml_wifi_load.sh 2>/dev/null || true fi echo "Modules load scripts installed successfully" echo "Main script: /etc/modules-load.sh" echo "WiFi loader: /usr/bin/aml_wifi_load.sh" EOF if [ "${RT_LINUX}" = "yes" ]; then cat >> "${DEBIAN_DIR}/postinst" <<'EOF' systemctl daemon-reload systemctl enable set-rt-conf.service EOF fi cat >> "${DEBIAN_DIR}/postinst" <<'EOF' fi exit 0 EOF chmod 755 "${DEBIAN_DIR}/postinst" } # Create pre-removal script function _modules_load_create_prerm() { aml_log info "COMPILE-${COMPONENT} Creating pre-removal script DEBIAN/prerm" cat > "${DEBIAN_DIR}/prerm" << EOF #!/bin/sh set -e if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then # Remove symlink if it exists if [ -L /usr/sbin/aml_wifi_load.sh ]; then rm -f /usr/sbin/aml_wifi_load.sh fi echo "Modules load scripts package being removed" fi exit 0 EOF chmod 755 "${DEBIAN_DIR}/prerm" } # ------------------------------------------------------------------------------ # Step 5: Debian Package Building # ------------------------------------------------------------------------------ # Build Debian package from install directory # Parameters: # $1 - log prefix (e.g. "COMPILE-MODULES-LOAD") # $2 - output deb filename (without path) # Sets: OUTPUT_DEB function _modules_load_build_deb_package() { local log_prefix="$1" local output_deb_name="$2" # 将install目录内容拷贝到deb目录 aml_log info "${log_prefix} Copying install directory to deb package directory" cp -r "${INSTALL_DIR}/"* "${DEB_PACKAGE_DIR}/" # 基于deb目录打包 aml_log info "${log_prefix} Building Debian package from deb directory" dpkg-deb --build "${DEB_PACKAGE_DIR}" # Move to output directory OUTPUT_DEB="${SRC}/output/${output_deb_name}" mkdir -p "$(dirname "${OUTPUT_DEB}")" mv "${DEB_PACKAGE_DIR}.deb" "${OUTPUT_DEB}" aml_log info "${log_prefix} Package built successfully ${output_deb_name}" aml_log info "${log_prefix} Build directory preserved ${BASE_BUILD_DIR}" } # ------------------------------------------------------------------------------ # Step 6: Display Installation Information # ------------------------------------------------------------------------------ # Display installation information function _modules_load_display_install_info() { aml_log debug "COMPILE-${COMPONENT} Modules load scripts will be installed to:" aml_log debug "COMPILE-${COMPONENT} - /etc/modules-load.sh" aml_log debug "COMPILE-${COMPONENT} - /usr/bin/aml_wifi_load.sh" } # ============================================================================== # Main Build Function (Entry Point) # ============================================================================== # Main Modules Load scripts package build function function amlbian_build_modules_load() { set -e if ! is_unit_enabled "modules-load"; then echo "[SKIP] >> modules-load disable in BUILD_UNITS" return fi # ---- Step 1: Package Information ---- _modules_load_init_package_info aml_log info "COMPILE-${COMPONENT} Building modules load scripts package version ${PKG_VERSION}" # ---- Step 2: Build Directory Setup ---- _modules_load_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}" _modules_load_display_build_info "COMPILE-${COMPONENT}" # ---- Step 3: Source File Validation and Copying ---- _modules_load_copy_modules_load_sh _modules_load_copy_aml_wifi_load_sh _modules_load_copy_aml_usb_serial_load_sh _modules_load_copy_install_chinese_font_sh _modules_load_copy_aml_rt_conf_sh # ---- Step 4: Debian Packaging Scripts ---- _modules_load_create_control_file _modules_load_create_postinst _modules_load_create_prerm # ---- Step 5: Build Debian Package ---- _modules_load_build_deb_package "COMPILE-${COMPONENT}" "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}.deb" # ---- Step 6: Display Installation Information ---- _modules_load_display_install_info display_alert "COMPILE-${COMPONENT} Modules load scripts package build completed" "success" return 0 } # ============================================================================== # Main Execution # ============================================================================== aml_log info "AMLbian Modules Load Build Script" "Starting modules load scripts build" amlbian_build_modules_load aml_log info "AMLOGIC: Modules load scripts build completed" "Check output directory for packages"