2026-08-13 11:28:40 +08:00
#!/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"
2026-11-11 08:50:43 +08:00
MODULE_BUILD_DIR = " ${ SRC } /output/.tmp"
2026-08-13 11:28:40 +08:00
# ==============================================================================
# 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"
;;
*)
2026-11-11 08:50:43 +08:00
aml_log error "OPTEE-USERSPACE" "Unsupported architecture: ${ arch } "
2026-08-13 11:28:40 +08:00
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
;;
*)
2026-11-11 08:50:43 +08:00
aml_log error "OPTEE-USERSPACE" "Unsupported TDK version: ${ version } "
aml_log info "OPTEE-USERSPACE" "Supported versions: v2.4.4, v3.8.0, v3.18.0, v4.4.0"
2026-08-13 11:28:40 +08:00
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
2026-11-11 08:50:43 +08:00
aml_log error "OPTEE-USERSPACE" "aml-comp not found"
2026-08-13 11:28:40 +08:00
exit 1
fi
local tdk_path = " ${ aml_comp_path } /vendor/amlogic/tdk/ ${ tdk_version } / ${ arm_target } "
if [[ ! -d " ${ tdk_path } " ]] ; then
2026-11-11 08:50:43 +08:00
aml_log error "OPTEE-USERSPACE" "TDK directory not found: ${ tdk_path } "
2026-08-13 11:28:40 +08:00
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 } "
2026-11-11 08:50:43 +08:00
aml_log info "OPTEE-USERSPACE" "Creating library symlinks for TDK ${ tdk_version } "
2026-08-13 11:28:40 +08:00
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"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Created symlinks for libteec.so (v2.4.4/v3.8.0)"
2026-08-13 11:28:40 +08:00
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"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Created symlinks for libckteec.so"
2026-08-13 11:28:40 +08:00
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"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Created symlinks for libseteec.so"
2026-08-13 11:28:40 +08:00
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"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Created symlinks for libteec.so (v3.18.0)"
2026-08-13 11:28:40 +08:00
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"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Created symlinks for libckteec.so"
2026-08-13 11:28:40 +08:00
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"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Created symlinks for libseteec.so"
2026-08-13 11:28:40 +08:00
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"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Created symlinks for libteec.so (v4.4.0)"
2026-08-13 11:28:40 +08:00
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 } " ) "
2026-11-11 08:50:43 +08:00
aml_log info "OPTEE-USERSPACE" "Installing OPTEE userspace components from TDK ${ PLATFORM_TDK_VERSION } ( ${ arm_target } )"
2026-08-13 11:28:40 +08:00
# Get TDK source path
local tdk_source = " $( __get_tdk_source_path " ${ PLATFORM_TDK_VERSION } " " ${ arm_target } " ) "
2026-11-11 08:50:43 +08:00
aml_log info "OPTEE-USERSPACE" "TDK source: ${ tdk_source } "
2026-08-13 11:28:40 +08:00
# 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
2026-11-11 08:50:43 +08:00
aml_log info "OPTEE-USERSPACE" "Installing binaries..."
2026-08-13 11:28:40 +08:00
if [[ -f " ${ tdk_source } /bin/tee-supplicant" ]] ; then
install -m 0755 " ${ tdk_source } /bin/tee-supplicant" " ${ install_dir } /usr/bin/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed tee-supplicant"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "OPTEE-USERSPACE" "tee-supplicant not found in ${ tdk_source } /bin/"
2026-08-13 11:28:40 +08:00
fi
if [[ -f " ${ tdk_source } /bin/tee_stest" ]] ; then
install -m 0755 " ${ tdk_source } /bin/tee_stest" " ${ install_dir } /usr/bin/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed tee_stest"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "OPTEE-USERSPACE" "tee_stest not found in ${ tdk_source } /bin/"
2026-08-13 11:28:40 +08:00
fi
# Install libraries
2026-11-11 08:50:43 +08:00
aml_log info "OPTEE-USERSPACE" "Installing libraries..."
2026-08-13 11:28:40 +08:00
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/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed libteec.so.1.0"
2026-08-13 11:28:40 +08:00
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/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed libckteec.so.0.1.0"
2026-08-13 11:28:40 +08:00
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/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed libseteec.so.0.1.0"
2026-08-13 11:28:40 +08:00
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/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed libteec.so.1.0.0"
2026-08-13 11:28:40 +08:00
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/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed libckteec.so.0.1.0"
2026-08-13 11:28:40 +08:00
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/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed libseteec.so.0.1.0"
2026-08-13 11:28:40 +08:00
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/"
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed libteec.so.2.0.0"
2026-08-13 11:28:40 +08:00
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
2026-11-11 08:50:43 +08:00
aml_log warn "OPTEE-USERSPACE" "Library directory not found: ${ tdk_source } /lib"
2026-08-13 11:28:40 +08:00
fi
# Install headers
2026-11-11 08:50:43 +08:00
aml_log info "OPTEE-USERSPACE" "Installing headers..."
2026-08-13 11:28:40 +08:00
if [[ -d " ${ tdk_source } /include" ]] ; then
install -m 0644 " ${ tdk_source } /include/" *.h " ${ install_dir } /usr/include/" 2>/dev/null || true
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Installed headers from ${ tdk_source } /include/"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "OPTEE-USERSPACE" "Include directory not found: ${ tdk_source } /include"
2026-08-13 11:28:40 +08:00
fi
# Install secure OS components (only for v2.4.4)
if [[ " ${ PLATFORM_TDK_VERSION } " == "v2.4.4" ]] ; then
2026-11-11 08:50:43 +08:00
aml_log info "OPTEE-USERSPACE" "Installing secure OS components..."
2026-08-13 11:28:40 +08:00
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
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Copied secure OS components"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log warn "OPTEE-USERSPACE" "Secure OS directory not found: ${ tdk_source } /../secureos"
2026-08-13 11:28:40 +08:00
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."
2026-11-11 08:50:43 +08:00
aml_log info "OPTEE-USERSPACE" "Creating Debian package ${ pkg_name } _ ${ pkg_version } _ ${ pkg_arch } .deb"
2026-08-13 11:28:40 +08:00
# 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
2026-11-11 08:50:43 +08:00
aml_log debug "OPTEE-USERSPACE" "Copied installed files to package directory"
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "OPTEE-USERSPACE" "No installed files found in ${ install_dir } "
2026-08-13 11:28:40 +08:00
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
2026-11-11 08:50:43 +08:00
aml_log error "OPTEE-USERSPACE" "Failed to build Debian package"
2026-08-13 11:28:40 +08:00
return 1
fi
}
function amlbian_build_optee_userspace() {
if ! is_unit_enabled "optee-userspace" && ! is_unit_enabled "optee" ; then
2026-11-11 08:50:43 +08:00
aml_log info "[SKIP] >> optee-userspace disable in BUILD_UNITS"
2026-08-13 11:28:40 +08:00
return
fi
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Building OPTEE userspace ${ PLATFORM_TDK_VERSION } ${ ARCH } ${ BOARD } "
2026-08-13 11:28:40 +08:00
# Create build directory structure
2026-11-11 08:50:43 +08:00
local build_root = " ${ MODULE_BUILD_DIR } /optee-userspace-build- ${ ARCH } "
2026-08-13 11:28:40 +08:00
local build_dir = " ${ build_root } /build"
local install_dir = " ${ build_root } /install"
rm -rf " ${ build_root } "
# Create directories
mkdir -p " ${ build_dir } " " ${ install_dir } "
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Build directory: ${ build_dir } "
aml_log info "AMLOGIC: Install directory: ${ install_dir } "
aml_log info "AMLOGIC: output_dir ${ BUILD_DIR } /output"
2026-08-13 11:28:40 +08:00
# Install OPTEE userspace components
if ! __optee_userspace_install " ${ install_dir } " ; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: OPTEE userspace installation failed"
2026-08-13 11:28:40 +08:00
return 1
fi
# Create Debian package
if ! __optee_userspace_create_deb " ${ build_dir } " " ${ install_dir } " ; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: Package creation failed"
2026-08-13 11:28:40 +08:00
return 1
fi
display_alert "AMLOGIC: OPTEE userspace build completed successfully" "success"
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
2026-11-11 08:50:43 +08:00
aml_log info "AMLIbian OPTEE Userspace Build Script" "TDK Version: ${ PLATFORM_TDK_VERSION } , Arch: ${ ARCH } , Board: ${ BOARD } "
2026-08-13 11:28:40 +08:00
amlbian_build_optee_userspace
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: OPTEE userspace build completed" " ${ OUTPUT_DIR } "
2026-08-13 11:28:40 +08:00
# ==============================================================================
# End of build-optee-userspace.sh
2026-11-11 08:50:43 +08:00
# ==============================================================================