479 lines
17 KiB
Bash
479 lines
17 KiB
Bash
#!/usr/bin/env bash
|
|
# SPDX-License-Identifier: (GPL-2.0-only OR MIT)
|
|
# Copyright (C) 2026 Amlogic, Inc. All rights reserved
|
|
################################################################################
|
|
# Amlbian WiFi Common Compilation Module
|
|
#
|
|
# Purpose: WiFi common driver compilation with Hook support and aml-comp integration
|
|
# Provides: WiFi common driver compilation, firmware 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}")")"
|
|
|
|
# Source common functions
|
|
if [[ -f "${AMLBIAN_DIR}/lib/function-common.sh" ]]; then
|
|
source "${AMLBIAN_DIR}/lib/function-common.sh"
|
|
fi
|
|
|
|
# ==============================================================================
|
|
# Shared Helper Functions
|
|
# ==============================================================================
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 1: Version and Package Information Definition
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Initialize package metadata for WiFi common driver
|
|
function _wifi_common_init_package_info() {
|
|
PKG_MAINTAINER="Armbian Build <[email protected]>"
|
|
PKG_DESCRIPTION="Amlogic WiFi common driver"
|
|
PKG_NAME="aml-wifi-common"
|
|
PKG_VERSION="1.0"
|
|
PKG_ARCH="${ARCH:-arm64}"
|
|
DRIVER_NAME="wifi_comm"
|
|
DRIVER_MODULE="${DRIVER_NAME}.ko"
|
|
|
|
# Cross compilation settings
|
|
CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}"
|
|
|
|
# Board configuration
|
|
BOARD="${BOARD:-s6-bq201}"
|
|
KERNEL_VER="${KERNEL_VER:-aml-6.12}"
|
|
|
|
# WiFi modules supported
|
|
WIFI_MODULES=("w1" "w1u" "w2" "w2l")
|
|
|
|
# Configuration overrides
|
|
if [ "${BOARD:-}" = "s6-bq201" ]; then
|
|
OVERRIDES="${OVERRIDES:-mesons6-bq201}"
|
|
elif [ "${BOARD:-}" = "a9-by401" ]; then
|
|
OVERRIDES="${OVERRIDES:-mesona9-by401}"
|
|
elif [ "${BOARD:-}" = "s6-bl201" ]; then
|
|
OVERRIDES="${OVERRIDES:-mesons6-bl201}"
|
|
fi
|
|
|
|
display_alert "COMPILE-WIFI-COMMON" "BOARD: ${BOARD} OVERRIDES: ${OVERRIDES}" "info"
|
|
|
|
#MACHINE_EXTRA_RRECOMMENDS="${MACHINE_EXTRA_RRECOMMENDS:-aml-wifi-w2l}"
|
|
#DISTRO_FEATURES="${DISTRO_FEATURES:-aml-wifi-w2l}"
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 2: Build Directory Structure Setup
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Create layered build directory structure
|
|
# Parameters:
|
|
# $1 - base name (e.g. "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}")
|
|
# Sets: TMP_DIR, BASE_BUILD_DIR, SOURCE_DIR, BUILD_DIR, INSTALL_DIR, PATCH_DIR,
|
|
# DEB_PACKAGE_DIR, DEBIAN_DIR
|
|
function _wifi_common_create_build_dirs() {
|
|
local base_name="$1"
|
|
|
|
TMP_DIR="${SRC}/.tmp"
|
|
mkdir -p "${TMP_DIR}"
|
|
|
|
# 基础构建目录
|
|
BASE_BUILD_DIR="${TMP_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}"
|
|
}
|
|
|
|
# Create standard install subdirectories for WiFi common driver
|
|
function _wifi_common_create_install_dirs() {
|
|
local kernel_version="$1"
|
|
|
|
mkdir -p "${INSTALL_DIR}/lib/modules/${kernel_version}/kernel/drivers/amlogic/wifi/"
|
|
mkdir -p "${INSTALL_DIR}/lib/firmware/"
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 3: Helper Functions
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Extract board information from OVERRIDES
|
|
function _wifi_common_extract_board_info() {
|
|
local overrides="${OVERRIDES:-}"
|
|
local meson_override=""
|
|
local board_info=""
|
|
|
|
# Split OVERRIDES string
|
|
IFS=':' read -ra overrides_list <<< "$overrides"
|
|
|
|
# Find override starting with "meson"
|
|
for override in "${overrides_list[@]}"; do
|
|
if [[ "$override" == meson* ]]; then
|
|
meson_override="$override"
|
|
break
|
|
fi
|
|
done
|
|
|
|
# Extract board information
|
|
if [[ -n "$meson_override" ]]; then
|
|
IFS='-' read -ra parts <<< "$meson_override"
|
|
if [[ ${#parts[@]} -ge 2 ]]; then
|
|
board_info="${parts[1]}"
|
|
fi
|
|
fi
|
|
|
|
echo "$board_info"
|
|
}
|
|
|
|
# Check if should compile based on MACHINE_EXTRA_RRECOMMENDS
|
|
function _wifi_common_should_compile() {
|
|
local machine_extra_rrecommends="${MACHINE_EXTRA_RRECOMMENDS:-}"
|
|
local patterns=("aml-wifi-w2" "aml-wifi-w1" "aml-wifi-w1u")
|
|
|
|
# Check if contains any specified pattern
|
|
for pattern in "${patterns[@]}"; do
|
|
if [[ "$machine_extra_rrecommends" == *"$pattern"* ]]; then
|
|
return 0 # should compile
|
|
fi
|
|
done
|
|
|
|
return 1 # should not compile
|
|
}
|
|
|
|
# Check if aml-wifi feature is enabled in DISTRO_FEATURES
|
|
function _wifi_common_is_aml_wifi_enabled() {
|
|
local distro_features="${DISTRO_FEATURES:-}"
|
|
display_alert "COMPILE-WIFI-COMMON" "DISTRO_FEATURES: ${distro_features}" "info"
|
|
|
|
if [[ "$distro_features" == *"aml-wifi"* ]]; then
|
|
return 0 # enabled
|
|
else
|
|
return 1 # not enabled
|
|
fi
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 4: Debian Packaging Scripts Creation
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Create Debian control file
|
|
# Parameters:
|
|
# $1 - package name
|
|
# $2 - package version
|
|
# $3 - package architecture
|
|
# $4 - maintainer
|
|
# $5 - description
|
|
# $6 - dependencies (optional)
|
|
# $7 - conflicts (optional)
|
|
# $8 - provides (optional)
|
|
# $9 - section (optional, default "kernel")
|
|
function _wifi_common_create_control_file() {
|
|
local pkg_name="$1"
|
|
local pkg_version="$2"
|
|
local pkg_arch="$3"
|
|
local maintainer="$4"
|
|
local description="$5"
|
|
local depends="${6:-}"
|
|
local conflicts="${7:-}"
|
|
local provides="${8:-}"
|
|
local section="${9:-kernel}"
|
|
|
|
display_alert "COMPILE-WIFI-COMMON Creating control file DEBIAN/control" "info"
|
|
cat > "${DEBIAN_DIR}/control" <<EOF
|
|
Package: ${pkg_name}
|
|
Version: ${pkg_version}
|
|
Architecture: ${pkg_arch}
|
|
Maintainer: ${maintainer}
|
|
Depends: ${depends}
|
|
Conflicts: ${conflicts}
|
|
Provides: ${provides}
|
|
Section: ${section}
|
|
Priority: optional
|
|
Description: ${description}
|
|
This package provides the common kernel module for Amlogic wifi drivers.
|
|
It includes the wifi_comm module for common wifi functionality.
|
|
EOF
|
|
}
|
|
|
|
# Create postinst script for WiFi common driver
|
|
function _wifi_common_create_postinst() {
|
|
local kernel_version="$1"
|
|
display_alert "COMPILE-WIFI-COMMON Creating post-installation script DEBIAN/postinst" "info"
|
|
cat > "${DEBIAN_DIR}/postinst" << EOF
|
|
#!/bin/sh
|
|
set -e
|
|
|
|
if [ "\$1" = "configure" ]; then
|
|
# run depmod only if /lib/modules/${kernel_version} exists
|
|
if [ -d "/lib/modules/${kernel_version}" ]; then
|
|
depmod -a ${kernel_version}
|
|
else
|
|
echo "Warning: /lib/modules/${kernel_version} not found, skipping depmod"
|
|
fi
|
|
|
|
# try to load module but don't error
|
|
if modprobe -n wifi_comm 2>/dev/null; then
|
|
echo "Amlogic wifi common module can be loaded with 'modprobe wifi_comm'"
|
|
fi
|
|
fi
|
|
|
|
exit 0
|
|
EOF
|
|
chmod 755 "${DEBIAN_DIR}/postinst"
|
|
}
|
|
|
|
# Create prerm script for WiFi common driver
|
|
function _wifi_common_create_prerm() {
|
|
display_alert "COMPILE-WIFI-COMMON Creating pre-removal script DEBIAN/prerm" "info"
|
|
cat > "${DEBIAN_DIR}/prerm" << EOF
|
|
#!/bin/sh
|
|
set -e
|
|
|
|
if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then
|
|
# Unload module if loaded
|
|
if lsmod | grep -q "wifi_comm"; then
|
|
modprobe -r wifi_comm 2>/dev/null || true
|
|
fi
|
|
fi
|
|
|
|
exit 0
|
|
EOF
|
|
chmod 755 "${DEBIAN_DIR}/prerm"
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 5: Source/Vendor File Copying and Building
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Copy WiFi common driver source files and build module
|
|
function _wifi_common_copy_and_build() {
|
|
display_alert "COMPILE-WIFI-COMMON Copying WiFi common driver files" "info"
|
|
|
|
# Source directory
|
|
local SRC_DIR="${BASE_DIR}/aml-comp/hardware/wifi/amlogic/drivers/wifi_comm"
|
|
|
|
# Check if source exists
|
|
if [[ ! -d "${SRC_DIR}" ]]; then
|
|
display_alert "COMPILE-WIFI-COMMON WiFi common driver source code not found. Expected at: ${SRC_DIR}" "error"
|
|
exit 1
|
|
fi
|
|
|
|
# Copy source to source directory
|
|
display_alert "COMPILE-WIFI-COMMON Copying source files to source directory" "info"
|
|
cp -r "${SRC_DIR}/"* "${SOURCE_DIR}/"
|
|
|
|
# Kernel headers directory
|
|
local KERNEL_HEADERS="${BASE_DIR}/aml-comp/kernel/${KERNEL_VER}"
|
|
|
|
# Check for kernel headers
|
|
if [[ ! -d "${KERNEL_HEADERS}" ]]; then
|
|
display_alert "COMPILE-WIFI-COMMON Kernel headers directory not found at: ${KERNEL_HEADERS}" "error"
|
|
exit 1
|
|
fi
|
|
|
|
# Change to source directory
|
|
cd "${SOURCE_DIR}"
|
|
|
|
# Build the module
|
|
if echo "$MACHINE_EXTRA_RRECOMMENDS" | grep -q -E '\b(aml-wifi-w2|aml-wifi-w1|aml-wifi-w1u)\b'; then
|
|
display_alert "COMPILE-WIFI-COMMON Building wifi_comm.ko kernel module $MACHINE_EXTRA_RRECOMMENDS" "info"
|
|
make -C "${KERNEL_HEADERS}/build" \
|
|
M="${SOURCE_DIR}" \
|
|
KERNELDIR="${KERNEL_HEADERS}" \
|
|
ARCH="${PKG_ARCH}" \
|
|
CROSS_COMPILE="${CROSS_COMPILE}"
|
|
fi
|
|
|
|
# Check if module was built
|
|
if [[ -f "${SOURCE_DIR}/${DRIVER_MODULE}" ]]; then
|
|
display_alert "COMPILE-WIFI-COMMON Module built successfully: ${DRIVER_MODULE}" "info"
|
|
|
|
# Install module to INSTALL_DIR
|
|
local module_dir="${INSTALL_DIR}/lib/modules/${KERNEL_VERSION}/kernel/drivers/amlogic/wifi/"
|
|
display_alert "COMPILE-WIFI-COMMON Installing module to install directory ${module_dir}" "info"
|
|
install -m 0644 "${SOURCE_DIR}/${DRIVER_MODULE}" "${module_dir}/"
|
|
else
|
|
rm -rf "${INSTALL_DIR}/lib/modules"
|
|
display_alert "COMPILE-WIFI-COMMON Module build failed. Could not find ${DRIVER_MODULE}" "info"
|
|
fi
|
|
}
|
|
|
|
# Install firmware files based on board configuration
|
|
function _wifi_common_install_firmware() {
|
|
local board_info="$1"
|
|
local kernel_version="$2"
|
|
|
|
display_alert "COMPILE-WIFI-COMMON Installing firmware files" "info"
|
|
|
|
# Check if aml-wifi feature is enabled
|
|
if ! _wifi_common_is_aml_wifi_enabled; then
|
|
display_alert "COMPILE-WIFI-COMMON Aml-wifi feature not enabled, skipping firmware installation" "info"
|
|
return 0
|
|
fi
|
|
|
|
display_alert "COMPILE-WIFI-COMMON Aml-wifi feature enabled, installing firmware files" "info"
|
|
|
|
# Install firmware files for each WiFi module
|
|
for WIFI_MODULE in "${WIFI_MODULES[@]}"; do
|
|
# Check if this module is in machine recommended list
|
|
local machine_extra_rrecommends="${MACHINE_EXTRA_RRECOMMENDS:-}"
|
|
if echo "${machine_extra_rrecommends}" | grep -wq "aml-wifi-${WIFI_MODULE}"; then
|
|
display_alert "COMPILE-WIFI-COMMON Installing firmware for module: ${WIFI_MODULE}, board: ${board_info}" "info"
|
|
|
|
local FILE_PATH="${SOURCE_DIR}/aml_txt"
|
|
local WIFI_PATH="${FILE_PATH}/${WIFI_MODULE}"
|
|
|
|
# Determine firmware source directory
|
|
local FIRMWARE_SRC_PATH=""
|
|
|
|
# Prioritize board-specific firmware directory
|
|
if [[ -n "$board_info" && -d "${WIFI_PATH}/${board_info}" ]]; then
|
|
FIRMWARE_SRC_PATH="${WIFI_PATH}/${board_info}"
|
|
# Then use firmware files in module directory
|
|
elif [[ -n "$(ls ${WIFI_PATH}/aml_*.txt 2>/dev/null)" ]]; then
|
|
FIRMWARE_SRC_PATH="${WIFI_PATH}"
|
|
else
|
|
continue
|
|
fi
|
|
|
|
display_alert "COMPILE-WIFI-COMMON Firmware source path: ${FIRMWARE_SRC_PATH}" "info"
|
|
|
|
# Determine firmware destination directory
|
|
local FIRMWARE_DST=""
|
|
if [[ "${WIFI_MODULE}" == "w2" ]]; then
|
|
FIRMWARE_DST="${INSTALL_DIR}/lib/firmware/"
|
|
else
|
|
FIRMWARE_DST="${INSTALL_DIR}/lib/firmware/${WIFI_MODULE}/"
|
|
fi
|
|
|
|
mkdir -p "${FIRMWARE_DST}"
|
|
|
|
# Copy firmware files to INSTALL_DIR
|
|
for f in "${FIRMWARE_SRC_PATH}"/aml_*.txt; do
|
|
if [[ -f "$f" ]]; then
|
|
install -m 0644 "$f" "${FIRMWARE_DST}"
|
|
fi
|
|
done
|
|
fi
|
|
done
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 6: Debian Package Building
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Build Debian package from install directory
|
|
# Parameters:
|
|
# $1 - log prefix (e.g. "COMPILE-WIFI-COMMON")
|
|
# $2 - output deb filename (without path)
|
|
# Sets: OUTPUT_DEB
|
|
function _wifi_common_build_deb_package() {
|
|
local log_prefix="$1"
|
|
local output_deb_name="$2"
|
|
|
|
# 将install目录内容拷贝到deb目录
|
|
display_alert "Copying install directory to deb package directory ${log_prefix}" "info"
|
|
cp -r "${INSTALL_DIR}/"* "${DEB_PACKAGE_DIR}/" 2>&1
|
|
|
|
# 基于deb目录打包
|
|
display_alert "Building Debian package from deb directory ${log_prefix}" "info"
|
|
dpkg-deb --build "${DEB_PACKAGE_DIR}" 2>&1
|
|
|
|
# Move to output directory
|
|
OUTPUT_DEB="${SRC}/output/${output_deb_name}"
|
|
mkdir -p "$(dirname "${OUTPUT_DEB}")"
|
|
mv "${DEB_PACKAGE_DIR}.deb" "${OUTPUT_DEB}"
|
|
|
|
display_alert "${log_prefix} Package built successfully ${output_deb_name}" "info"
|
|
display_alert "${log_prefix} Build directory preserved ${BASE_BUILD_DIR}" "info"
|
|
}
|
|
|
|
# Display build directory info
|
|
# Parameters:
|
|
# $1 - log prefix
|
|
function _wifi_common_display_build_info() {
|
|
local log_prefix="$1"
|
|
display_alert "${log_prefix} Source dir: ${SOURCE_DIR}" "info"
|
|
display_alert "${log_prefix} Build dir: ${BUILD_DIR}" "info"
|
|
display_alert "${log_prefix} Install dir: ${INSTALL_DIR}" "info"
|
|
display_alert "${log_prefix} Patch dir: ${PATCH_DIR}" "info"
|
|
display_alert "${log_prefix} Deb package dir: ${DEB_PACKAGE_DIR}" "info"
|
|
}
|
|
|
|
# ==============================================================================
|
|
# Main Build Functions (Entry Points)
|
|
# ==============================================================================
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# WiFi Common Driver Build
|
|
# Entry point: amlbian_build_wifi_common
|
|
# ------------------------------------------------------------------------------
|
|
|
|
function amlbian_build_wifi_common() {
|
|
set -e
|
|
|
|
# ---- Step 1: Version and Package Information ----
|
|
_wifi_common_init_package_info
|
|
|
|
# Check compile condition
|
|
if ! _wifi_common_should_compile; then
|
|
display_alert "COMPILE-WIFI-COMMON" "Skipping compilation for this machine" "info"
|
|
return 0
|
|
fi
|
|
|
|
# Get kernel version for packaging
|
|
local KERNEL_VERSION=$(make -s -C "${BASE_DIR}/aml-comp/kernel/aml-6.12/build" kernelrelease 2>/dev/null || true)
|
|
if [[ -z "${KERNEL_VERSION}" ]]; then
|
|
KERNEL_VERSION="unknown-$(date +%Y%m%d)"
|
|
fi
|
|
display_alert "COMPILE-WIFI-COMMON Building WiFi common driver for kernel ${KERNEL_VERSION}" "info"
|
|
|
|
# ---- Step 2: Build Directory Setup ----
|
|
_wifi_common_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}"
|
|
_wifi_common_create_install_dirs "${KERNEL_VERSION}"
|
|
_wifi_common_display_build_info "COMPILE-WIFI-COMMON"
|
|
|
|
# ---- Step 3: Debian Packaging Scripts ----
|
|
_wifi_common_create_control_file \
|
|
"${PKG_NAME}" "${PKG_VERSION}" "${PKG_ARCH}" "${PKG_MAINTAINER}" \
|
|
"${PKG_DESCRIPTION}" "" "" "" "kernel"
|
|
|
|
_wifi_common_create_postinst "${KERNEL_VERSION}"
|
|
_wifi_common_create_prerm
|
|
|
|
# ---- Step 4: Copy Source/Vendor Files and Build ----
|
|
_wifi_common_copy_and_build
|
|
|
|
# ---- Step 5: Install Firmware Files ----
|
|
local board_info=$(_wifi_common_extract_board_info)
|
|
display_alert "COMPILE-WIFI-COMMON Board info: ${board_info:-none}" "info"
|
|
_wifi_common_install_firmware "${board_info}" "${KERNEL_VERSION}"
|
|
|
|
# ---- Step 6: Build Debian Package ----
|
|
_wifi_common_build_deb_package "COMPILE-WIFI-COMMON" "${PKG_NAME}_${PKG_VERSION}_${KERNEL_VERSION}_${PKG_ARCH}.deb"
|
|
|
|
display_alert "COMPILE-WIFI-COMMON WiFi common driver build completed. Successfully built for ${PKG_ARCH} with kernel ${KERNEL_VERSION}" "info"
|
|
return 0
|
|
}
|
|
|
|
export amlbian_build_wifi_common
|