Files
2026-08-24 10:03:24 +08:00

414 lines
16 KiB
Bash

#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# OPTEE userspace build script for AMLbian
# This script builds OPTEE userspace components for different Amlogic chipsets
set -e
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
AMLBIAN_DIR="$(dirname "${SCRIPT_DIR}")"
AMLBIAN_DIR="$(dirname "${AMLBIAN_DIR}")" # Go up one more level from multimedia/tee
AMLBIAN_DIR="$(dirname "${AMLBIAN_DIR}")" # Go up one more level from scripts
BUILD_DIR="${AMLBIAN_DIR}/../build"
# Source common functions
if [[ -f "${AMLBIAN_DIR}/lib/function-common.sh" ]]; then
source "${AMLBIAN_DIR}/lib/function-common.sh"
fi
# ==============================================================================
# Section 2: Default Configuration
# ==============================================================================
PLATFORM_TDK_VERSION="${PLATFORM_TDK_VERSION:-v3.18.0}"
ARCH="${ARCH:-arm64}"
BOARD="${BOARD:-s6-bq201}"
DEB_VERSION="5.15"
# ==============================================================================
# Section 4: OPTEE Userspace Build Functions
# ==============================================================================
# Map architecture to ARM_TARGET directory name
function __get_arm_target() {
local arch="${1}"
case "${arch}" in
"arm"|"armhf")
echo "ca_export_arm"
;;
"arm64"|"aarch64")
echo "ca_export_arm64"
;;
*)
display_alert "OPTEE-USERSPACE" "Unsupported architecture: ${arch}" "err"
exit 1
;;
esac
}
# Validate TDK version
function __validate_tdk_version() {
local version="${1}"
case "${version}" in
"v2.4.4"|"v3.8.0"|"v3.18.0"|"v4.4.0")
return 0
;;
*)
display_alert "OPTEE-USERSPACE" "Unsupported TDK version: ${version}" "err"
display_alert "OPTEE-USERSPACE" "Supported versions: v2.4.4, v3.8.0, v3.18.0, v4.4.0" "info"
exit 1
;;
esac
}
# Get TDK source path
function __get_tdk_source_path() {
local tdk_version="${1}"
local arm_target="${2}"
# Try to find TDK in aml-comp
local aml_comp_path="$(amlbian_get_aml_comp_dir)"
if [[ -z "${aml_comp_path}" ]] || [[ ! -d "${aml_comp_path}" ]]; then
display_alert "OPTEE-USERSPACE" "aml-comp not found" "err"
exit 1
fi
local tdk_path="${aml_comp_path}/vendor/amlogic/tdk/${tdk_version}/${arm_target}"
if [[ ! -d "${tdk_path}" ]]; then
display_alert "OPTEE-USERSPACE" "TDK directory not found: ${tdk_path}" "err"
exit 1
fi
echo "${tdk_path}"
}
# Create library symlinks based on TDK version
function __create_library_symlinks() {
local lib_dir="${1}"
local tdk_version="${2}"
display_alert "OPTEE-USERSPACE" "Creating library symlinks for TDK ${tdk_version}" "info"
case "${tdk_version}" in
"v2.4.4"|"v3.8.0")
# libteec.so.1.0 -> libteec.so.1 -> libteec.so
if [[ -f "${lib_dir}/libteec.so.1.0" ]]; then
ln -sf libteec.so.1.0 "${lib_dir}/libteec.so.1"
ln -sf libteec.so.1 "${lib_dir}/libteec.so"
display_alert "OPTEE-USERSPACE" "Created symlinks for libteec.so (v2.4.4/v3.8.0)" "debug"
fi
;;
"v3.18.0")
# libckteec.so.0.1.0 -> libckteec.so.0.1 -> libckteec.so.0 -> libckteec.so
if [[ -f "${lib_dir}/libckteec.so.0.1.0" ]]; then
ln -sf libckteec.so.0.1.0 "${lib_dir}/libckteec.so.0.1"
ln -sf libckteec.so.0.1 "${lib_dir}/libckteec.so.0"
ln -sf libckteec.so.0 "${lib_dir}/libckteec.so"
display_alert "OPTEE-USERSPACE" "Created symlinks for libckteec.so" "debug"
fi
# libseteec.so.0.1.0 -> libseteec.so.0.1 -> libseteec.so.0 -> libseteec.so
if [[ -f "${lib_dir}/libseteec.so.0.1.0" ]]; then
ln -sf libseteec.so.0.1.0 "${lib_dir}/libseteec.so.0.1"
ln -sf libseteec.so.0.1 "${lib_dir}/libseteec.so.0"
ln -sf libseteec.so.0 "${lib_dir}/libseteec.so"
display_alert "OPTEE-USERSPACE" "Created symlinks for libseteec.so" "debug"
fi
# libteec.so.1.0.0 -> libteec.so.1.0 -> libteec.so.1 -> libteec.so
if [[ -f "${lib_dir}/libteec.so.1.0.0" ]]; then
ln -sf libteec.so.1.0.0 "${lib_dir}/libteec.so.1.0"
ln -sf libteec.so.1.0 "${lib_dir}/libteec.so.1"
ln -sf libteec.so.1 "${lib_dir}/libteec.so"
display_alert "OPTEE-USERSPACE" "Created symlinks for libteec.so (v3.18.0)" "debug"
fi
;;
"v4.4.0")
# libckteec.so.0.1.0 -> libckteec.so.0.1 -> libckteec.so.0 -> libckteec.so
if [[ -f "${lib_dir}/libckteec.so.0.1.0" ]]; then
ln -sf libckteec.so.0.1.0 "${lib_dir}/libckteec.so.0.1"
ln -sf libckteec.so.0.1 "${lib_dir}/libckteec.so.0"
ln -sf libckteec.so.0 "${lib_dir}/libckteec.so"
display_alert "OPTEE-USERSPACE" "Created symlinks for libckteec.so" "debug"
fi
# libseteec.so.0.1.0 -> libseteec.so.0.1 -> libseteec.so.0 -> libseteec.so
if [[ -f "${lib_dir}/libseteec.so.0.1.0" ]]; then
ln -sf libseteec.so.0.1.0 "${lib_dir}/libseteec.so.0.1"
ln -sf libseteec.so.0.1 "${lib_dir}/libseteec.so.0"
ln -sf libseteec.so.0 "${lib_dir}/libseteec.so"
display_alert "OPTEE-USERSPACE" "Created symlinks for libseteec.so" "debug"
fi
# libteec.so.2.0.0 -> libteec.so.2.0 -> libteec.so.2 -> libteec.so
if [[ -f "${lib_dir}/libteec.so.2.0.0" ]]; then
ln -sf libteec.so.2.0.0 "${lib_dir}/libteec.so.2.0"
ln -sf libteec.so.2.0 "${lib_dir}/libteec.so.2"
ln -sf libteec.so.2 "${lib_dir}/libteec.so"
display_alert "OPTEE-USERSPACE" "Created symlinks for libteec.so (v4.4.0)" "debug"
fi
;;
esac
}
function __optee_userspace_install() {
local install_dir="${1}"
__validate_tdk_version "${PLATFORM_TDK_VERSION}"
# Determine ARM_TARGET based on architecture
local arm_target="$(__get_arm_target "${ARCH}")"
display_alert "OPTEE-USERSPACE" "Installing OPTEE userspace components from TDK ${PLATFORM_TDK_VERSION} (${arm_target})" "info"
# Get TDK source path
local tdk_source="$(__get_tdk_source_path "${PLATFORM_TDK_VERSION}" "${arm_target}")"
display_alert "OPTEE-USERSPACE" "TDK source: ${tdk_source}" "info"
# Create directory structure
mkdir -p "${install_dir}/usr/bin"
mkdir -p "${install_dir}/usr/lib"
mkdir -p "${install_dir}/usr/include"
mkdir -p "${install_dir}/usr/share/tdk"
# Install binaries
display_alert "OPTEE-USERSPACE" "Installing binaries..." "info"
if [[ -f "${tdk_source}/bin/tee-supplicant" ]]; then
install -m 0755 "${tdk_source}/bin/tee-supplicant" "${install_dir}/usr/bin/"
display_alert "OPTEE-USERSPACE" "Installed tee-supplicant" "debug"
else
display_alert "OPTEE-USERSPACE" "tee-supplicant not found in ${tdk_source}/bin/" "warn"
fi
if [[ -f "${tdk_source}/bin/tee_stest" ]]; then
install -m 0755 "${tdk_source}/bin/tee_stest" "${install_dir}/usr/bin/"
display_alert "OPTEE-USERSPACE" "Installed tee_stest" "debug"
else
display_alert "OPTEE-USERSPACE" "tee_stest not found in ${tdk_source}/bin/" "warn"
fi
# Install libraries
display_alert "OPTEE-USERSPACE" "Installing libraries..." "info"
if [[ -d "${tdk_source}/lib" ]]; then
# Copy library files based on TDK version
case "${PLATFORM_TDK_VERSION}" in
"v2.4.4"|"v3.8.0")
# Copy libteec.so.1.0
if [[ -f "${tdk_source}/lib/libteec.so.1.0" ]]; then
install -m 0644 "${tdk_source}/lib/libteec.so.1.0" "${install_dir}/usr/lib/"
display_alert "OPTEE-USERSPACE" "Installed libteec.so.1.0" "debug"
fi
;;
"v3.18.0")
# Copy libckteec.so.0.1.0, libseteec.so.0.1.0, libteec.so.1.0.0
if [[ -f "${tdk_source}/lib/libckteec.so.0.1.0" ]]; then
install -m 0644 "${tdk_source}/lib/libckteec.so.0.1.0" "${install_dir}/usr/lib/"
display_alert "OPTEE-USERSPACE" "Installed libckteec.so.0.1.0" "debug"
fi
if [[ -f "${tdk_source}/lib/libseteec.so.0.1.0" ]]; then
install -m 0644 "${tdk_source}/lib/libseteec.so.0.1.0" "${install_dir}/usr/lib/"
display_alert "OPTEE-USERSPACE" "Installed libseteec.so.0.1.0" "debug"
fi
if [[ -f "${tdk_source}/lib/libteec.so.1.0.0" ]]; then
install -m 0644 "${tdk_source}/lib/libteec.so.1.0.0" "${install_dir}/usr/lib/"
display_alert "OPTEE-USERSPACE" "Installed libteec.so.1.0.0" "debug"
fi
;;
"v4.4.0")
# Copy libckteec.so.0.1.0, libseteec.so.0.1.0, libteec.so.2.0.0
if [[ -f "${tdk_source}/lib/libckteec.so.0.1.0" ]]; then
install -m 0644 "${tdk_source}/lib/libckteec.so.0.1.0" "${install_dir}/usr/lib/"
display_alert "OPTEE-USERSPACE" "Installed libckteec.so.0.1.0" "debug"
fi
if [[ -f "${tdk_source}/lib/libseteec.so.0.1.0" ]]; then
install -m 0644 "${tdk_source}/lib/libseteec.so.0.1.0" "${install_dir}/usr/lib/"
display_alert "OPTEE-USERSPACE" "Installed libseteec.so.0.1.0" "debug"
fi
if [[ -f "${tdk_source}/lib/libteec.so.2.0.0" ]]; then
install -m 0644 "${tdk_source}/lib/libteec.so.2.0.0" "${install_dir}/usr/lib/"
display_alert "OPTEE-USERSPACE" "Installed libteec.so.2.0.0" "debug"
fi
;;
esac
# Also copy any other .so files that might be needed (like .a static libraries)
find "${tdk_source}/lib" -type f -name "*.a" -exec install -m 0644 {} "${install_dir}/usr/lib/" \;
# Create symlinks
__create_library_symlinks "${install_dir}/usr/lib" "${PLATFORM_TDK_VERSION}"
else
display_alert "OPTEE-USERSPACE" "Library directory not found: ${tdk_source}/lib" "warn"
fi
# Install headers
display_alert "OPTEE-USERSPACE" "Installing headers..." "info"
if [[ -d "${tdk_source}/include" ]]; then
install -m 0644 "${tdk_source}/include/"*.h "${install_dir}/usr/include/" 2>/dev/null || true
display_alert "OPTEE-USERSPACE" "Installed headers from ${tdk_source}/include/" "debug"
else
display_alert "OPTEE-USERSPACE" "Include directory not found: ${tdk_source}/include" "warn"
fi
# Install secure OS components (only for v2.4.4)
if [[ "${PLATFORM_TDK_VERSION}" == "v2.4.4" ]]; then
display_alert "OPTEE-USERSPACE" "Installing secure OS components..." "info"
if [[ -d "${tdk_source}/../secureos" ]]; then
mkdir -p "${install_dir}/usr/share/tdk/secureos"
cp -r "${tdk_source}/../secureos/"* "${install_dir}/usr/share/tdk/secureos/" 2>/dev/null || true
display_alert "OPTEE-USERSPACE" "Copied secure OS components" "debug"
else
display_alert "OPTEE-USERSPACE" "Secure OS directory not found: ${tdk_source}/../secureos" "warn"
fi
fi
display_alert "OPTEE-USERSPACE" "Installation completed" "success"
}
function __optee_userspace_create_deb() {
local build_dir="${1}"
local install_dir="${2}"
local pkg_arch="${ARCH}"
# Package details (defined as local variables)
local pkg_name="optee-userspace"
local pkg_version="${PLATFORM_TDK_VERSION#v}" # Remove 'v' prefix
local pkg_maintainer="Amlbian Builder <build@amlbian.com>"
local pkg_description="OPTEE userspace components (TEE supplicant, libraries)"
local pkg_description_long="This package provides OPTEE (Open Portable Trusted Execution Environment) userspace components including TEE supplicant daemon, client libraries, and test utilities."
display_alert "OPTEE-USERSPACE" "Creating Debian package ${pkg_name}_${pkg_version}_${pkg_arch}.deb" "info"
# Create package directory structure
local deb_dir="${build_dir}/deb/${pkg_name}-${pkg_version}"
local debian_dir="${deb_dir}/DEBIAN"
mkdir -p "${debian_dir}"
# Copy installed files to package directory
if [[ -d "${install_dir}" ]] && [[ "$(ls -A "${install_dir}")" ]]; then
cp -r "${install_dir}"/* "${deb_dir}/" 2>/dev/null || true
display_alert "OPTEE-USERSPACE" "Copied installed files to package directory" "debug"
else
display_alert "OPTEE-USERSPACE" "No installed files found in ${install_dir}" "error"
return 1
fi
# Create control file
cat > "${debian_dir}/control" <<EOF
Package: ${pkg_name}
Version: ${pkg_version}
Architecture: ${pkg_arch}
Maintainer: ${pkg_maintainer}
Description: ${pkg_description}
${pkg_description_long}
Depends: libc6
Section: libs
Priority: optional
EOF
# Create postinst script for ldconfig
cat > "${debian_dir}/postinst" << 'EOF'
#!/bin/sh
set -e
if [ "$1" = "configure" ]; then
# Update ldconfig cache
if command -v ldconfig >/dev/null 2>&1; then
ldconfig
fi
fi
exit 0
EOF
chmod 755 "${debian_dir}/postinst"
# Create prerm script
cat > "${debian_dir}/prerm" << 'EOF'
#!/bin/sh
set -e
if [ "$1" = "remove" ] || [ "$1" = "deconfigure" ]; then
# Update ldconfig
if command -v ldconfig >/dev/null 2>&1; then
ldconfig
fi
fi
exit 0
EOF
chmod 755 "${debian_dir}/prerm"
# Build Debian package
cd "${build_dir}/deb"
dpkg-deb --build "${pkg_name}-${pkg_version}" >/dev/null 2>&1
local deb_file="${pkg_name}-${pkg_version}.deb"
if [[ -f "${deb_file}" ]]; then
# Move to output directory
local output_dir="${BUILD_DIR}/output"
mkdir -p "${output_dir}"
mv "${deb_file}" "${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${pkg_version}_${pkg_arch}.deb"
display_alert "OPTEE-USERSPACE" "Package created: ${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${pkg_version}_${pkg_arch}.deb" "success"
return 0
else
display_alert "OPTEE-USERSPACE" "Failed to build Debian package" "error"
return 1
fi
}
function amlbian_build_optee_userspace() {
if ! is_unit_enabled "optee-userspace" && ! is_unit_enabled "optee"; then
display_alert "[SKIP] >> optee-userspace disable in BUILD_UNITS" "" "info"
return
fi
display_alert "AMLOGIC: Building OPTEE userspace ${PLATFORM_TDK_VERSION} ${ARCH} ${BOARD}" "info"
# Create build directory structure
local build_root="${BUILD_DIR}/.tmp/optee-userspace-build-${ARCH}"
local build_dir="${build_root}/build"
local install_dir="${build_root}/install"
rm -rf "${build_root}"
# Create directories
mkdir -p "${build_dir}" "${install_dir}"
display_alert "AMLOGIC: Build directory: ${build_dir}" "info"
display_alert "AMLOGIC: Install directory: ${install_dir}" "info"
display_alert "AMLOGIC: output_dir ${BUILD_DIR}/output" "info"
# Install OPTEE userspace components
if ! __optee_userspace_install "${install_dir}"; then
display_alert "AMLOGIC: OPTEE userspace installation failed" "error"
return 1
fi
# Create Debian package
if ! __optee_userspace_create_deb "${build_dir}" "${install_dir}"; then
display_alert "AMLOGIC: Package creation failed" "error"
return 1
fi
display_alert "AMLOGIC: OPTEE userspace build completed successfully" "success"
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
display_alert "AMLIbian OPTEE Userspace Build Script" "TDK Version: ${PLATFORM_TDK_VERSION}, Arch: ${ARCH}, Board: ${BOARD}" "info"
amlbian_build_optee_userspace
display_alert "AMLOGIC: OPTEE userspace build completed" "${OUTPUT_DIR}" "info"
# ==============================================================================
# End of build-optee-userspace.sh
# ==============================================================================