478 lines
17 KiB
Bash
478 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 Compilation Module
|
|
#
|
|
# Purpose: WiFi driver compilation with Hook support and aml-comp integration
|
|
# Provides: WiFi 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
|
|
|
|
# Source ADLA build script if it exists
|
|
WIFI_COMM_SCRIPT="${SCRIPT_DIR}/build-wifi-comm.sh"
|
|
if [[ -f "${WIFI_COMM_SCRIPT}" ]]; then
|
|
display_alert "Loading wifi comm build script" "${WIFI_COMM_SCRIPT}" "info"
|
|
source "${WIFI_COMM_SCRIPT}"
|
|
else
|
|
display_alert "wifi comm build script not found" "${WIFI_COMM_SCRIPT}" "warn"
|
|
fi
|
|
|
|
# Source wap build script if it exists
|
|
WAP_SUPPLICANT_SCRIPT="${SCRIPT_DIR}/build-wpa-supplicant.sh"
|
|
if [[ -f "${WAP_SUPPLICANT_SCRIPT}" ]]; then
|
|
display_alert "Loading wpa supplicant build script" "${WAP_SUPPLICANT_SCRIPT}" "info"
|
|
source "${WAP_SUPPLICANT_SCRIPT}"
|
|
else
|
|
display_alert "wpa supplicant build script not found" "${WAP_SUPPLICANT_SCRIPT}" "warn"
|
|
fi
|
|
|
|
# ==============================================================================
|
|
# Shared Helper Functions
|
|
# ==============================================================================
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 1: Version and Package Information Definition
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Initialize package metadata for W2 WiFi driver
|
|
function _wifi_w2_init_package_info() {
|
|
PKG_MAINTAINER="Armbian Build <[email protected]>"
|
|
PKG_DESCRIPTION="Amlogic W2 WiFi driver"
|
|
PKG_NAME="aml-wifi-w2"
|
|
PKG_VERSION="1.0"
|
|
PKG_ARCH="${ARCH:-arm64}"
|
|
DRIVER_NAME="w2"
|
|
DRIVER_MODULE="${DRIVER_NAME}.ko"
|
|
COMMON_MODULE="${DRIVER_NAME}_comm.ko"
|
|
|
|
# Cross compilation settings
|
|
CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}"
|
|
}
|
|
|
|
# Initialize package metadata for W2L WiFi driver
|
|
function _wifi_w2l_init_package_info() {
|
|
PKG_MAINTAINER="Armbian Build <[email protected]>"
|
|
PKG_DESCRIPTION="Amlogic W2L WiFi driver"
|
|
PKG_NAME="aml-wifi-w2l"
|
|
PKG_VERSION="1.0"
|
|
PKG_ARCH="${ARCH:-arm64}"
|
|
DRIVER_NAME="w2l"
|
|
DRIVER_MODULE="${DRIVER_NAME}.ko"
|
|
COMMON_MODULE="${DRIVER_NAME}_comm.ko"
|
|
|
|
# Cross compilation settings
|
|
CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}"
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# 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_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 driver
|
|
# Parameters:
|
|
# $1 - driver type (w2 or w2l, affects firmware path)
|
|
function _wifi_create_install_dirs() {
|
|
local driver_type="$1"
|
|
|
|
# Module directory for extramodules
|
|
mkdir -p "${INSTALL_DIR}/lib/modules/extramodules/"
|
|
|
|
# Firmware directory
|
|
if [[ "${driver_type}" == "w2l" ]]; then
|
|
mkdir -p "${INSTALL_DIR}/lib/firmware/w2l/"
|
|
else
|
|
mkdir -p "${INSTALL_DIR}/lib/firmware/"
|
|
fi
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 3: 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_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 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}
|
|
EOF
|
|
}
|
|
|
|
# Create postinst script for WiFi driver
|
|
# Parameters:
|
|
# $1 - driver name (w2 or w2l)
|
|
# $2 - kernel version
|
|
function _wifi_create_postinst() {
|
|
local driver_name="$1"
|
|
local kernel_version="$2"
|
|
display_alert "COMPILE-WIFI Creating post-installation script DEBIAN/postinst driver_name ${driver_name} kernel_version ${kernel_version}" "info"
|
|
cat > "${DEBIAN_DIR}/postinst" << EOF
|
|
#!/bin/bash
|
|
set -e
|
|
|
|
if [ "\$1" = "configure" ]; then
|
|
# Run depmod to update module dependencies
|
|
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 ${driver_name} 2>/dev/null; then
|
|
echo "Amlogic ${driver_name} wifi module can be loaded with 'modprobe ${driver_name}'"
|
|
fi
|
|
fi
|
|
|
|
exit 0
|
|
EOF
|
|
chmod 755 "${DEBIAN_DIR}/postinst"
|
|
}
|
|
|
|
# Create prerm script for WiFi driver
|
|
# Parameters:
|
|
# $1 - driver name (w2 or w2l)
|
|
function _wifi_create_prerm() {
|
|
local driver_name="$1"
|
|
display_alert "COMPILE-WIFI Creating pre-removal script DEBIAN/prerm" "info"
|
|
cat > "${DEBIAN_DIR}/prerm" << EOF
|
|
#!/bin/bash
|
|
set -e
|
|
|
|
if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then
|
|
# Unload module if loaded
|
|
if lsmod | grep -q "${driver_name}"; then
|
|
modprobe -r ${driver_name} 2>/dev/null || true
|
|
fi
|
|
fi
|
|
|
|
exit 0
|
|
EOF
|
|
chmod 755 "${DEBIAN_DIR}/prerm"
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 4: Source/Vendor File Copying and Building
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Copy WiFi driver source files and build modules
|
|
# Parameters:
|
|
# $1 - driver type (w2 or w2l)
|
|
# $2 - source directory path
|
|
function _wifi_copy_and_build() {
|
|
local driver_type="$1"
|
|
local src_dir="$2"
|
|
|
|
display_alert "COMPILE-WIFI-${driver_type^^} Copying WiFi driver files" "info"
|
|
|
|
# Check if source exists
|
|
if [[ ! -d "${src_dir}" ]]; then
|
|
display_alert "COMPILE-WIFI-${driver_type^^} WiFi driver source code not found. Expected at: ${src_dir}" "error"
|
|
exit 1
|
|
fi
|
|
|
|
# Copy source to source directory
|
|
display_alert "COMPILE-WIFI-${driver_type^^} Copying source files to source directory" "info"
|
|
cp -r "${src_dir}/"* "${SOURCE_DIR}/"
|
|
|
|
# Kernel build directory
|
|
local KERNEL_BUILD_DIR="${BASE_DIR}/aml-comp/kernel/aml-6.12/build"
|
|
local STAGING_KERNEL_DIR="${BASE_DIR}/aml-comp/kernel/aml-6.12"
|
|
|
|
# Check for kernel headers
|
|
if [[ ! -d "${KERNEL_BUILD_DIR}" ]]; then
|
|
display_alert "COMPILE-WIFI-${driver_type^^} Kernel build directory not found at: ${KERNEL_BUILD_DIR}" "error"
|
|
exit 1
|
|
fi
|
|
|
|
# Change to source directory
|
|
cd "${SOURCE_DIR}"
|
|
|
|
# Build the module
|
|
display_alert "COMPILE-WIFI-${driver_type^^} Building ${driver_type} wifi kernel module" "info"
|
|
|
|
make -C "${KERNEL_BUILD_DIR}" \
|
|
M="${SOURCE_DIR}/aml_drv" \
|
|
CONFIG_BUILDROOT=y \
|
|
CONFIG_ANDROID_GKI=y \
|
|
CONFIG_AML_WOW_GOOGLE_CAST_EN=y \
|
|
CONFIG_AML_WOW_MAGIC_PACKET_EN=y \
|
|
KERNELDIR="${STAGING_KERNEL_DIR}" \
|
|
ARCH="${PKG_ARCH}" \
|
|
CROSS_COMPILE="${CROSS_COMPILE}"
|
|
|
|
# Check if modules were built
|
|
if [[ -f "${SOURCE_DIR}/aml_drv/fullmac/${DRIVER_MODULE}" ]] && [[ -f "${SOURCE_DIR}/aml_drv/fullmac/${COMMON_MODULE}" ]]; then
|
|
display_alert "COMPILE-WIFI-${driver_type^^} Modules built successfully: ${DRIVER_MODULE}, ${COMMON_MODULE}" "info"
|
|
|
|
# Install modules to INSTALL_DIR
|
|
local module_dir="${INSTALL_DIR}/lib/modules/extramodules/"
|
|
display_alert "COMPILE-WIFI-${driver_type^^} Installing modules to install directory ${module_dir}" "info"
|
|
install -m 0644 "${SOURCE_DIR}/aml_drv/fullmac/${DRIVER_MODULE}" "${module_dir}/"
|
|
install -m 0644 "${SOURCE_DIR}/aml_drv/fullmac/${COMMON_MODULE}" "${module_dir}/"
|
|
|
|
# Install firmware files to INSTALL_DIR
|
|
display_alert "COMPILE-WIFI-${driver_type^^} Installing firmware files to install directory" "info"
|
|
if [[ "${driver_type}" == "w2l" ]]; then
|
|
install -m 0644 ${SOURCE_DIR}/common/wifi_w2l_fw_*.bin "${INSTALL_DIR}/lib/firmware/w2l/" 2>/dev/null || true
|
|
install -m 0644 ${SOURCE_DIR}/common/aml_country_pwr_limit.txt "${INSTALL_DIR}/lib/firmware/w2l/" 2>/dev/null || true
|
|
else
|
|
install -m 0644 ${SOURCE_DIR}/common/wifi_w2_*fw_*.bin "${INSTALL_DIR}/lib/firmware/" 2>/dev/null || true
|
|
fi
|
|
else
|
|
display_alert "COMPILE-WIFI-${driver_type^^} Module build failed. Could not find ${DRIVER_MODULE} or ${COMMON_MODULE}" "err"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Step 5: Debian Package Building
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Build Debian package from install directory
|
|
# Parameters:
|
|
# $1 - log prefix (e.g. "COMPILE-WIFI-W2" or "COMPILE-WIFI-W2L")
|
|
# $2 - output deb filename (without path)
|
|
# Sets: OUTPUT_DEB
|
|
function _wifi_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_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)
|
|
# ==============================================================================
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# W2 WiFi Driver Build
|
|
# Entry point: amlbian_build_wifi_w2
|
|
# ------------------------------------------------------------------------------
|
|
|
|
function amlbian_build_wifi_w2() {
|
|
set -e
|
|
|
|
# ---- Step 1: Version and Package Information ----
|
|
_wifi_w2_init_package_info
|
|
|
|
# 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-W2 Building W2 wifi driver for kernel ${KERNEL_VERSION}" "info"
|
|
|
|
# ---- Step 2: Build Directory Setup ----
|
|
_wifi_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}"
|
|
_wifi_create_install_dirs "w2"
|
|
_wifi_display_build_info "COMPILE-WIFI-W2"
|
|
|
|
# ---- Step 3: Debian Packaging Scripts ----
|
|
_wifi_create_control_file \
|
|
"${PKG_NAME}" "${PKG_VERSION}" "${PKG_ARCH}" "${PKG_MAINTAINER}" \
|
|
"${PKG_DESCRIPTION}" "" "" "" "kernel"
|
|
|
|
_wifi_create_postinst "${DRIVER_NAME}" "${KERNEL_VERSION}"
|
|
_wifi_create_prerm "${DRIVER_NAME}"
|
|
|
|
# ---- Step 4: Copy Source/Vendor Files and Build ----
|
|
local SRC_DIR="${BASE_DIR}/aml-comp/hardware/wifi/amlogic/drivers/w2"
|
|
_wifi_copy_and_build "w2" "${SRC_DIR}"
|
|
|
|
# ---- Step 5: Build Debian Package ----
|
|
_wifi_build_deb_package "COMPILE-WIFI-W2" "${PKG_NAME}_${PKG_VERSION}_${KERNEL_VERSION}_${PKG_ARCH}.deb"
|
|
|
|
display_alert "COMPILE-WIFI-W2 W2 wifi driver build completed. Successfully built for ${PKG_ARCH} with kernel ${KERNEL_VERSION}" "info"
|
|
return 0
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# W2L WiFi Driver Build
|
|
# Entry point: amlbian_build_wifi_w2l
|
|
# ------------------------------------------------------------------------------
|
|
|
|
function amlbian_build_wifi_w2l() {
|
|
set -e
|
|
|
|
# ---- Step 1: Version and Package Information ----
|
|
_wifi_w2l_init_package_info
|
|
|
|
# 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-W2L Building W2L wifi driver for kernel ${KERNEL_VERSION}" "info"
|
|
|
|
# ---- Step 2: Build Directory Setup ----
|
|
_wifi_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}"
|
|
_wifi_create_install_dirs "w2l"
|
|
_wifi_display_build_info "COMPILE-WIFI-W2L"
|
|
|
|
# ---- Step 3: Debian Packaging Scripts ----
|
|
_wifi_create_control_file \
|
|
"${PKG_NAME}" "${PKG_VERSION}" "${PKG_ARCH}" "${PKG_MAINTAINER}" \
|
|
"${PKG_DESCRIPTION}" "" "" "" "kernel"
|
|
|
|
_wifi_create_postinst "${DRIVER_NAME}" "${KERNEL_VERSION}"
|
|
_wifi_create_prerm "${DRIVER_NAME}"
|
|
|
|
# ---- Step 4: Copy Source/Vendor Files and Build ----
|
|
local SRC_DIR="${BASE_DIR}/aml-comp/hardware/wifi/amlogic/drivers/w2l"
|
|
_wifi_copy_and_build "w2l" "${SRC_DIR}"
|
|
|
|
# ---- Step 5: Build Debian Package ----
|
|
_wifi_build_deb_package "COMPILE-WIFI-W2L" "${PKG_NAME}_${PKG_VERSION}_${KERNEL_VERSION}_${PKG_ARCH}.deb"
|
|
|
|
display_alert "COMPILE-WIFI-W2L W2L wifi driver build completed. Successfully built for ${PKG_ARCH} with kernel ${KERNEL_VERSION}" "info"
|
|
return 0
|
|
}
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Main WiFi Build Function
|
|
# ------------------------------------------------------------------------------
|
|
|
|
function amlbian_build_wifi() {
|
|
if ! is_unit_enabled "wifi"; then
|
|
echo "[SKIP] >> WiFi disable in BUILD_UNITS"
|
|
return
|
|
fi
|
|
|
|
# Build W2 WiFi driver
|
|
if [ "${WIFI_MODE:-}" = "w2" ]; then
|
|
display_alert "Building W2 WiFi driver" "W2 enabled in BUILD_UNITS" "info"
|
|
amlbian_build_wifi_w2
|
|
else
|
|
display_alert "Skipping W2 WiFi driver" "W2 disabled in BUILD_UNITS" "info"
|
|
fi
|
|
|
|
# Build W2L WiFi driver
|
|
if [ "${WIFI_MODE:-}" = "w2l" ]; then
|
|
display_alert "Building W2L WiFi driver" "W2L enabled in BUILD_UNITS" "info"
|
|
amlbian_build_wifi_w2l
|
|
else
|
|
display_alert "Skipping W2L WiFi driver" "W2L disabled in BUILD_UNITS" "info"
|
|
fi
|
|
|
|
if [ "${WIFI_COMM:-}" = "yes" ]; then
|
|
display_alert "Building wifi comm" "info"
|
|
amlbian_build_wifi_common
|
|
else
|
|
display_alert "Skipping wifi comm" "info"
|
|
fi
|
|
|
|
display_alert "Building wap supplicant" "info"
|
|
amlbian_build_wpa_supplicant
|
|
}
|
|
|
|
# ==============================================================================
|
|
# Main Execution
|
|
# ==============================================================================
|
|
|
|
display_alert "AMLbian WiFi Build Script" "Starting WiFi build" "info"
|
|
amlbian_build_wifi
|
|
display_alert "AMLOGIC: WiFi build completed" "Check output directory for packages" "info"
|