Files

339 lines
12 KiB
Bash

#!/usr/bin/env bash
# SPDX-License-Identifier: (GPL-2.0-only OR MIT)
# Copyright (C) 2026 Amlogic, Inc. All rights reserved
################################################################################
# Amlbian HCI Attach Compilation Package
#
# Purpose: Build aml_hciattach tool and bluetooth configuration package
# Provides: aml_hciattach binary to /usr/bin, configuration files to /lib/firmware/aml
# Based on Yocto recipe: amlogic-hciattach_git.bb
################################################################################
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: Package Information Definition
# ------------------------------------------------------------------------------
# Initialize package metadata for HCI Attach
function _hciattach_init_package_info() {
PKG_MAINTAINER="Armbian Build <build@armbian.com>"
PKG_DESCRIPTION="Amlogic HCI attach tool for bluetooth"
PKG_NAME="aml-hciattach"
PKG_VERSION="1.0"
PKG_ARCH="${ARCH:-arm64}"
COMPONENT="hciattach"
# Cross compilation settings
CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}"
# Source directory for hciattach
HCIATTACH_SRC_DIR="${BASE_DIR}/aml-comp/hardware/bluetooth/amlogic/aml_hciattach"
}
# ------------------------------------------------------------------------------
# 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, BIN_DIR, FIRMWARE_DIR
function _hciattach_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"
BIN_DIR="${INSTALL_DIR}/usr/bin/"
FIRMWARE_DIR="${INSTALL_DIR}/lib/firmware/aml/"
mkdir -p "${DEBIAN_DIR}"
mkdir -p "${BIN_DIR}"
mkdir -p "${FIRMWARE_DIR}"
}
# Display build directory info
# Parameters:
# $1 - log prefix
function _hciattach_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"
display_alert "${log_prefix} Bin dir: ${BIN_DIR}" "info"
display_alert "${log_prefix} Firmware dir: ${FIRMWARE_DIR}" "info"
}
# ------------------------------------------------------------------------------
# Step 3: Source File Validation and Copying
# ------------------------------------------------------------------------------
# Validate and copy source files
# Parameters:
# $1 - source directory path
# Sets: HCIATTACH_SRC_DIR (updated to SOURCE_DIR after copy)
function _hciattach_validate_and_copy_sources() {
local src_dir="$1"
# Check if source directory exists
if [[ ! -d "${src_dir}" ]]; then
display_alert "COMPILE-${COMPONENT} HCI attach source code not found" "${src_dir}" "error"
exit 1
fi
# Copy source to source directory
display_alert "COMPILE-${COMPONENT} Copying source files to source directory" "info"
cp -r "${src_dir}/"* "${SOURCE_DIR}/" 2>&1
# Update source directory to the copied location
HCIATTACH_SRC_DIR="${SOURCE_DIR}"
}
# ------------------------------------------------------------------------------
# Step 4: Build HCI Attach Tool
# ------------------------------------------------------------------------------
# Build aml_hciattach tool
function _hciattach_build_tool() {
display_alert "COMPILE-${COMPONENT} Building aml_hciattach" "info"
# Change to source directory
cd "${HCIATTACH_SRC_DIR}"
# Based on Yocto recipe: ${MAKE} -C ${S} CC='${CC}' OUT_DIR=${B}
make -C "${HCIATTACH_SRC_DIR}" CC="${CROSS_COMPILE}gcc" OUT_DIR="${BUILD_DIR}"
# Check if binary was built
if [[ ! -f "${BUILD_DIR}/aml_hciattach" ]]; then
display_alert "COMPILE-${COMPONENT} Build failed. Could not find aml_hciattach" "error"
exit 1
fi
display_alert "COMPILE-${COMPONENT} aml_hciattach built successfully" "info"
# 在BUILD_DIR中保存编译结果(备份)
display_alert "COMPILE-${COMPONENT} Saving build result to build directory" "info"
mkdir -p "${BUILD_DIR}/bin"
cp -f "${BUILD_DIR}/aml_hciattach" "${BUILD_DIR}/bin/"
}
# ------------------------------------------------------------------------------
# Step 5: File Installation
# ------------------------------------------------------------------------------
# Install binary and configuration files
function _hciattach_install_files() {
# Install binary to INSTALL_DIR(用于打包)
display_alert "COMPILE-${COMPONENT} Installing aml_hciattach to install directory ${BIN_DIR}" "info"
install -m 0755 "${BUILD_DIR}/aml_hciattach" "${BIN_DIR}/"
# Install configuration files to INSTALL_DIR
display_alert "COMPILE-${COMPONENT} Installing configuration files to install directory ${FIRMWARE_DIR}" "info"
# Install aml_bt.conf
if [[ -f "${HCIATTACH_SRC_DIR}/aml_bt.conf" ]]; then
install -m 0644 "${HCIATTACH_SRC_DIR}/aml_bt.conf" "${FIRMWARE_DIR}/"
fi
# Install a2dp_mode_cfg.txt
if [[ -f "${HCIATTACH_SRC_DIR}/a2dp_mode_cfg.txt" ]]; then
install -m 0644 "${HCIATTACH_SRC_DIR}/a2dp_mode_cfg.txt" "${FIRMWARE_DIR}/"
fi
# Install aml_bt_rf.txt
if [[ -f "${HCIATTACH_SRC_DIR}/aml_bt_rf.txt" ]]; then
install -m 0644 "${HCIATTACH_SRC_DIR}/aml_bt_rf.txt" "${FIRMWARE_DIR}/"
fi
# Special handling for w2 USB: change pinmux
# Based on Yocto recipe:
# if ${@bb.utils.contains('MACHINE_EXTRA_RRECOMMENDS', 'aml-bt-w2', 'true', 'false', d)}; then
# if [ "${AML_WIFI_BUS_TYPE}" = "usb" ]; then
# sed -i 's/^ChangePinMux.*/ChangePinMux=1/' ${FWDIR}/aml_bt.conf
# fi
# fi
if echo "$MACHINE_EXTRA_RRECOMMENDS" | grep -q -E '\b(aml-wifi-w2)\b'; then
if [[ -n "${AML_WIFI_BUS_TYPE}" ]] && [[ "${AML_WIFI_BUS_TYPE}" == "usb" ]]; then
if [[ -f "${FIRMWARE_DIR}/aml_bt.conf" ]]; then
display_alert "COMPILE-${COMPONENT} USB bus detected, enabling ChangePinMux" "info"
sed -i 's/^ChangePinMux.*/ChangePinMux=1/' "${FIRMWARE_DIR}/aml_bt.conf"
fi
fi
fi
}
# ------------------------------------------------------------------------------
# Step 6: Debian Packaging Scripts Creation
# ------------------------------------------------------------------------------
# Create Debian control file
function _hciattach_create_control_file() {
display_alert "COMPILE-${COMPONENT} Creating control file DEBIAN/control" "info"
cat > "${DEBIAN_DIR}/control" << EOF
Package: ${PKG_NAME}
Version: ${PKG_VERSION}
Architecture: ${PKG_ARCH}
Maintainer: ${PKG_MAINTAINER}
Section: utils
Priority: optional
Description: ${PKG_DESCRIPTION}
This package provides the aml_hciattach tool for initializing
Amlogic bluetooth chips. It includes configuration files for
bluetooth operation.
EOF
}
# Create post-installation script
function _hciattach_create_postinst() {
display_alert "COMPILE-${COMPONENT} Creating post-installation script: DEBIAN/postinst" "info"
cat > "${DEBIAN_DIR}/postinst" << EOF
#!/bin/bash
set -e
if [ "\$1" = "configure" ]; then
echo "Amlogic HCI attach tool installed successfully"
echo "Configuration files are located in /lib/firmware/aml/"
fi
exit 0
EOF
chmod 755 "${DEBIAN_DIR}/postinst"
}
# Create pre-removal script
function _hciattach_create_prerm() {
display_alert "COMPILE-${COMPONENT} Creating pre-removal script: DEBIAN/prerm" "info"
cat > "${DEBIAN_DIR}/prerm" << EOF
#!/bin/bash
set -e
if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then
# Stop any running bluetooth services if needed
echo "Removing amlogic-hciattach"
fi
exit 0
EOF
chmod 755 "${DEBIAN_DIR}/prerm"
}
# ------------------------------------------------------------------------------
# Step 7: Debian Package Building
# ------------------------------------------------------------------------------
# Build Debian package from install directory
# Parameters:
# $1 - log prefix (e.g. "COMPILE-HCIATTACH")
# $2 - output deb filename (without path)
# Sets: OUTPUT_DEB
function _hciattach_build_deb_package() {
local log_prefix="$1"
local output_deb_name="$2"
# 将install目录内容拷贝到deb目录
display_alert "${log_prefix} Copying install directory to deb package directory" "info"
cp -r "${INSTALL_DIR}/"* "${DEB_PACKAGE_DIR}/" 2>&1
# 基于deb目录打包
display_alert "${log_prefix} Building Debian package" "info"
dpkg-deb --build "${DEB_PACKAGE_DIR}" 2>&1
if [[ $? -ne 0 ]]; then
display_alert "${log_prefix} Package build failed" "error"
exit 1
fi
# 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"
}
# ==============================================================================
# Main Build Function (Entry Point)
# ==============================================================================
# Main HCI Attach package build function
function amlbian_build_hciattach() {
set -e
# ---- Step 1: Version and Package Information ----
_hciattach_init_package_info
display_alert "COMPILE-${COMPONENT} Building amlogic-hciattach package version ${PKG_VERSION} for ${PKG_ARCH}" "info"
# ---- Step 2: Build Directory Setup ----
_hciattach_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}"
_hciattach_display_build_info "COMPILE-${COMPONENT}"
# ---- Step 3: Source File Validation and Copying ----
_hciattach_validate_and_copy_sources "${HCIATTACH_SRC_DIR}"
# ---- Step 4: Build HCI Attach Tool ----
_hciattach_build_tool
# ---- Step 5: File Installation ----
_hciattach_install_files
# ---- Step 6: Debian Packaging Scripts ----
_hciattach_create_control_file
_hciattach_create_postinst
_hciattach_create_prerm
# ---- Step 7: Build Debian Package ----
_hciattach_build_deb_package "COMPILE-${COMPONENT}" "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}.deb"
display_alert "COMPILE-${COMPONENT} Amlogic HCI attach package build completed" "success"
display_alert "COMPILE-${COMPONENT} Build directories preserved:" "info"
display_alert "COMPILE-${COMPONENT} Source: ${SOURCE_DIR}" "info"
display_alert "COMPILE-${COMPONENT} Build: ${BUILD_DIR}" "info"
display_alert "COMPILE-${COMPONENT} Install: ${INSTALL_DIR}" "info"
display_alert "COMPILE-${COMPONENT} Patch: ${PATCH_DIR}" "info"
display_alert "COMPILE-${COMPONENT} Deb package: ${DEB_PACKAGE_DIR}" "info"
return 0
}