2026-08-13 11:28:40 +08:00
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2026 AMLOGIC
#
# TEE supplicant service package build script for AMLbian
# This script builds tee-supplicant systemd service and init script package
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
# ==============================================================================
ARCH = " ${ ARCH :- arm64 } "
BOARD = " ${ BOARD :- s6 -bq201 } "
# ==============================================================================
# Section 4: TEE Supplicant Service Build Functions
# ==============================================================================
# Main Installation Function
function __tee_supplicant_install() {
local install_dir = " ${ 1 } "
2026-11-11 08:50:43 +08:00
aml_log info "TEE-SUPP" "Installing tee-supplicant service files"
2026-08-13 11:28:40 +08:00
# Service files are expected to be in amlbian/lib/multimedia/optee-userspace
local service_dir = " ${ AMLBIAN_DIR } /scripts/multimedia/tee/optee-userspace"
mkdir -p " ${ install_dir } /usr/bin"
mkdir -p " ${ install_dir } /usr/lib"
# Install systemd service file
local service_file = " ${ service_dir } /tee-supplicant.service"
if [[ -f " ${ service_file } " ]] ; then
mkdir -p " ${ install_dir } /usr/lib/systemd/system"
install -m 0644 " ${ service_file } " " ${ install_dir } /usr/lib/systemd/system/"
2026-11-11 08:50:43 +08:00
aml_log debug "TEE-SUPP" "Installed systemd service file: ${ service_file } "
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "TEE-SUPP" "Systemd service file not found: ${ service_file } "
2026-08-13 11:28:40 +08:00
fi
# Install init script
local init_script = " ${ service_dir } /tee-supplicant.init"
if [[ -f " ${ init_script } " ]] ; then
mkdir -p " ${ install_dir } /etc/init.d"
install -m 0755 " ${ init_script } " " ${ install_dir } /etc/init.d/tee-supplicant"
2026-11-11 08:50:43 +08:00
aml_log debug "TEE-SUPP" "Installed init script: ${ init_script } "
2026-08-13 11:28:40 +08:00
else
2026-11-11 08:50:43 +08:00
aml_log error "TEE-SUPP" "Init script not found: ${ init_script } "
2026-08-13 11:28:40 +08:00
fi
display_alert "TEE-SUPP" "Service files installed to ${ install_dir } " "success"
return 0
}
# Debian Package Creation
function __tee_supplicant_create_deb() {
local build_dir = " ${ 1 } "
local install_dir = " ${ 2 } "
local pkg_arch = " ${ ARCH } "
# Package details
local pkg_name = "tee-supplicant"
local pkg_version = "1.0.0"
local pkg_maintainer = "Amlbian Builder <build@amlbian.com>"
local pkg_description = "tee-supplicant systemd service and init script"
local pkg_description_long = "This package provides systemd service and SysV init script for tee-supplicant daemon (part of OPTEE userspace). It depends on optee-userspace package which provides the actual binary."
2026-11-11 08:50:43 +08:00
aml_log info "TEE-SUPP" "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 "TEE-SUPP" "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 "TEE-SUPP" "No installed files found in ${ install_dir } "
2026-08-13 11:28:40 +08:00
return 1
fi
# Create control file with dependency on optee-userspace
cat > " ${ debian_dir } /control" <<EOF
Package: ${pkg_name}
Version: ${pkg_version}
Architecture: ${pkg_arch}
Maintainer: ${pkg_maintainer}
Depends: optee-userspace
Description: ${pkg_description}
${pkg_description_long}
Section: admin
Priority: optional
EOF
# Create postinst script for systemd service
cat > " ${ debian_dir } /postinst" << 'EOF'
#!/bin/sh
set -e
case "$1" in
configure)
# Update ldconfig cache
if command -v ldconfig >/dev/null 2>&1; then
ldconfig
fi
# Enable systemd service if systemd is available
if command -v systemctl >/dev/null 2>&1 && [ -f /usr/lib/systemd/system/tee-supplicant.service ]; then
systemctl daemon-reload >/dev/null 2>&1 || true
systemctl enable tee-supplicant.service >/dev/null 2>&1 || true
fi
# Update init.d scripts if systemd is not used
if [ ! -x /bin/systemctl ] && [ -f /etc/init.d/tee-supplicant ]; then
update-rc.d tee-supplicant defaults >/dev/null 2>&1 || true
fi
;;
*)
;;
esac
exit 0
EOF
chmod 755 " ${ debian_dir } /postinst"
# Create prerm script
cat > " ${ debian_dir } /prerm" << 'EOF'
#!/bin/sh
set -e
case "$1" in
remove|deconfigure)
# Stop service if running
if [ -f /etc/init.d/tee-supplicant ]; then
/etc/init.d/tee-supplicant stop >/dev/null 2>&1 || true
fi
if command -v systemctl >/dev/null 2>&1 && [ -f /usr/lib/systemd/system/tee-supplicant.service ]; then
systemctl stop tee-supplicant.service >/dev/null 2>&1 || true
systemctl disable tee-supplicant.service >/dev/null 2>&1 || true
fi
;;
*)
;;
esac
exit 0
EOF
chmod 755 " ${ debian_dir } /prerm"
# Create postrm script
cat > " ${ debian_dir } /postrm" << 'EOF'
#!/bin/sh
set -e
case "$1" in
purge|remove)
# Update ldconfig
if command -v ldconfig >/dev/null 2>&1; then
ldconfig
fi
# Clean up systemd service
if [ -f /usr/lib/systemd/system/tee-supplicant.service ]; then
rm -f /usr/lib/systemd/system/tee-supplicant.service
systemctl daemon-reload >/dev/null 2>&1 || true
fi
# Clean up init.d scripts
if [ -f /etc/init.d/tee-supplicant ]; then
update-rc.d -f tee-supplicant remove >/dev/null 2>&1 || true
rm -f /etc/init.d/tee-supplicant
fi
;;
*)
;;
esac
exit 0
EOF
chmod 755 " ${ debian_dir } /postrm"
# 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 "TEE-SUPP" "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 "TEE-SUPP" "Failed to build Debian package"
2026-08-13 11:28:40 +08:00
return 1
fi
}
function amlbian_build_tee_supplicant() {
# Check if optee-userspace is enabled
if ! is_unit_enabled "tee-supplicant" && ! is_unit_enabled "optee" ; then
2026-11-11 08:50:43 +08:00
aml_log info "[SKIP] >> tee-supplicant disabled (optee-userspace not enabled)"
2026-08-13 11:28:40 +08:00
return
fi
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: Building tee-supplicant service package ${ 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 } /tee-supplicant-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 tee-supplicant service files
if ! __tee_supplicant_install " ${ install_dir } " ; then
2026-11-11 08:50:43 +08:00
aml_log error "AMLOGIC: tee-supplicant installation failed"
2026-08-13 11:28:40 +08:00
return 1
fi
# Create Debian package
if ! __tee_supplicant_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: tee-supplicant service package build completed successfully" "success"
return 0
}
# ==============================================================================
# Section 5: Main
# ==============================================================================
2026-11-11 08:50:43 +08:00
aml_log info "AMLIbian TEE Supplicant Service Build Script" "Arch: ${ ARCH } , Board: ${ BOARD } "
2026-08-13 11:28:40 +08:00
amlbian_build_tee_supplicant
2026-11-11 08:50:43 +08:00
aml_log info "AMLOGIC: tee-supplicant service package build completed" " ${ BUILD_DIR } /output"
2026-08-13 11:28:40 +08:00
# ==============================================================================
# End of build-tee-supplicant.sh
2026-11-11 08:50:43 +08:00
# ==============================================================================