#!/usr/bin/env bash # # SPDX-License-Identifier: GPL-2.0 # # Copyright (c) 2026 AMLOGIC # # OPTEE video firmware build script for AMLbian # This script builds OPTEE video firmware components 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" MODULE_BUILD_DIR="${SRC}/output/.tmp" # ============================================================================== # Section 2: Default Configuration # ============================================================================== PLATFORM_TDK_VERSION="${PLATFORM_TDK_VERSION:-v3.18.0}" TDK_VERSION="${TDK_VERSION:-v3.8/dev/S905D5}" ARCH="${ARCH:-arm64}" BOARD="${BOARD:-s6-bq201}" # ============================================================================== # Section 4: OPTEE Video Firmware Build Functions # ============================================================================== # Map architecture to ARM_TARGET for OPTEE video firmware function __get_optee_video_arm_target() { local arch="${1}" case "${arch}" in "arm"|"armhf") echo "arm.aapcs-linux.hard" ;; "arm64"|"aarch64") echo "aarch64.lp64." ;; *) aml_log error "OPTEE-VIDEO-FIRMWARE" "Unsupported architecture: ${arch}" exit 1 ;; esac } # Validate TDK version function __validate_tdk_version() { local platform_tdk_version="${1}" local tdk_version="${2}" case "${platform_tdk_version}" in "v2.4.4"|"v3.8.0"|"v3.18.0"|"v4.4.0") ;; *) aml_log error "OPTEE-VIDEO-FIRMWARE" "Unsupported platform TDK version: ${platform_tdk_version}" aml_log info "OPTEE-VIDEO-FIRMWARE" "Supported versions: v2.4.4, v3.8.0, v3.18.0, v4.4.0" exit 1 ;; esac case "${tdk_version}" in "v2.4"|"v3.8"|"v3.8/dev/S905D5"|v3.8/dev/A311Y3) ;; *) aml_log error "OPTEE-VIDEO-FIRMWARE" "Unsupported TDK version: ${tdk_version}" aml_log info "OPTEE-VIDEO-FIRMWARE" "Supported versions: v2.4 v3.8 v3.8/dev/S905D5" exit 1 ;; esac } # Get video firmware source path function __get_video_firmware_source_path() { # 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 aml_log error "OPTEE-VIDEO-FIRMWARE" "aml-comp not found" exit 1 fi # Based on Yocto recipe: S = "${WORKDIR}/git/secfirmload/secloadbin" local firmware_path="${aml_comp_path}/multimedia/secfirmload/secloadbin" # Fallback: check if files are in ca_export directories if [[ ! -d "${firmware_path}" ]]; then aml_log error "OPTEE-VIDEO-FIRMWARE" "Video firmware directory not found: ${firmware_path}" exit 1 fi echo "${firmware_path}" } # Helper: Map architecture to ca_export directory name function __get_arm_export_target() { local arch="${1}" case "${arch}" in "arm"|"armhf") echo "ca_export_arm" ;; "arm64"|"aarch64") echo "ca_export_arm64" ;; *) echo "" ;; esac } # Main Installation Function function __optee_video_firmware_install() { local install_dir="${1}" local platform_tdk_version="${PLATFORM_TDK_VERSION}" local tdk_version="${TDK_VERSION}" local arch="${ARCH}" #__validate_tdk_version "${platform_tdk_version}" "${tdk_version}" # Determine ARM_TARGET for video firmware local arm_target="$(__get_optee_video_arm_target "${arch}")" aml_log info "OPTEE-VIDEO-FIRMWARE" "Installing OPTEE video firmware from TDK ${platform_tdk_version} ${tdk_version} (${arm_target})" # Get video firmware source path local firmware_source="$(__get_video_firmware_source_path)" aml_log info "OPTEE-VIDEO-FIRMWARE" "Firmware source: ${firmware_source}" # Create directory structure mkdir -p "${install_dir}/usr/bin" mkdir -p "${install_dir}/usr/lib" mkdir -p "${install_dir}/lib/optee_armtz" mkdir -p "${install_dir}/usr/share/optee-video-firmware" # TA target is always "noarch" for video firmware local ta_target="noarch" aml_log info "OPTEE-VIDEO-FIRMWARE" "Installing TA files..." # Install TA files local ta_dir="${firmware_source}/${ta_target}/ta/${tdk_version}" if [[ -d "${ta_dir}" ]]; then find "${ta_dir}" -name "*.ta" -type f -exec install -m 0644 {} "${install_dir}/lib/optee_armtz/" \; local ta_count=$(find "${ta_dir}" -name "*.ta" -type f | wc -l) aml_log debug "OPTEE-VIDEO-FIRMWARE" "Installed ${ta_count} TA files from ${ta_dir}" else aml_log warn "OPTEE-VIDEO-FIRMWARE" "TA directory not found: ${ta_dir}" fi aml_log info "OPTEE-VIDEO-FIRMWARE" "Installing library..." # Install library file local lib_file="${firmware_source}/${platform_tdk_version}/${arm_target}/libtee_preload_fw.so" if [[ -f "${lib_file}" ]]; then install -m 0644 "${lib_file}" "${install_dir}/usr/lib/" aml_log debug "OPTEE-VIDEO-FIRMWARE" "Installed libtee_preload_fw.so" else aml_log warn "OPTEE-VIDEO-FIRMWARE" "Library file not found: ${lib_file}" fi aml_log info "OPTEE-VIDEO-FIRMWARE" "Installing binary..." # Install binary file local bin_file="${firmware_source}/${platform_tdk_version}/${arm_target}/tee_preload_fw" if [[ -f "${bin_file}" ]]; then install -m 0755 "${bin_file}" "${install_dir}/usr/bin/" aml_log debug "OPTEE-VIDEO-FIRMWARE" "Installed tee_preload_fw" else aml_log warn "OPTEE-VIDEO-FIRMWARE" "Binary file not found: ${bin_file}" fi aml_log info "OPTEE-VIDEO-FIRMWARE RT_LINUX=${RT_LINUX}" "Installing system service files..." # Install systemd service file (if available) local service_file="${SCRIPT_DIR}/optee-video-firmware/videoFirmwarePreload.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/" if [ "${RT_LINUX}" = "yes" ]; then sed -i "/^ExecStart=\/usr\/bin\/tee_preload_fw/a ExecStart=/bin/sh -c 'echo 1 > /sys/class/firmware_codec/reload'" \ "${install_dir}/usr/lib/systemd/system/videoFirmwarePreload.service" fi aml_log debug "OPTEE-VIDEO-FIRMWARE" "Installed systemd service file" else aml_log info "OPTEE-VIDEO-FIRMWARE" "Systemd service file not found, skipping" fi display_alert "OPTEE-VIDEO-FIRMWARE" "Installation completed" "success" return 0 } # Debian Package Creation function __optee_video_firmware_create_deb() { local build_dir="${1}" local install_dir="${2}" local platform_tdk_version="${PLATFORM_TDK_VERSION}" local arch="${ARCH}" local pkg_arch="${arch}" # Package details local pkg_name="optee-video-firmware" local pkg_version="${platform_tdk_version#v}" # Remove 'v' prefix local pkg_maintainer="Amlbian Builder " local pkg_description="OPTEE video firmware components (TA files, preload library)" local pkg_description_long="This package provides OPTEE (Open Portable Trusted Execution Environment) video firmware components including Trusted Applications (TA), preload library, and system service for video firmware loading." aml_log info "OPTEE-VIDEO-FIRMWARE" "Creating Debian package ${pkg_name}_${pkg_version}_${pkg_arch}.deb" # 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 aml_log debug "OPTEE-VIDEO-FIRMWARE" "Copied installed files to package directory" else aml_log error "OPTEE-VIDEO-FIRMWARE" "No installed files found in ${install_dir}" return 1 fi # Create control file with dependency on optee-userspace cat > "${debian_dir}/control" < "${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/videoFirmwarePreload.service ]; then systemctl daemon-reload >/dev/null 2>&1 || true systemctl enable videoFirmwarePreload.service >/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) if command -v systemctl >/dev/null 2>&1 && [ -f /usr/lib/systemd/system/videoFirmwarePreload.service ]; then systemctl stop videoFirmwarePreload.service >/dev/null 2>&1 || true systemctl disable videoFirmwarePreload.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/videoFirmwarePreload.service ]; then rm -f /usr/lib/systemd/system/videoFirmwarePreload.service systemctl daemon-reload >/dev/null 2>&1 || true 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 "OPTEE-VIDEO-FIRMWARE" "Package created: ${output_dir}/linux-${pkg_name}-${BOARD}-amlbian_${pkg_version}_${pkg_arch}.deb" "success" return 0 else aml_log error "OPTEE-VIDEO-FIRMWARE" "Failed to build Debian package" return 1 fi } function amlbian_build_optee_video_firmware() { # Check if optee-video-firmware is enabled if ! is_unit_enabled "optee-video-firmware" && ! is_unit_enabled "optee"; then aml_log info "[SKIP] >> optee-video-firmware disabled (optee not enabled)" return fi aml_log info "AMLOGIC: Building OPTEE video firmware package ${PLATFORM_TDK_VERSION} ${ARCH} ${BOARD}" # Create build directory structure local build_root="${MODULE_BUILD_DIR}/optee-video-firmware-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}" 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" # Install OPTEE video firmware components if ! __optee_video_firmware_install "${install_dir}"; then aml_log error "AMLOGIC: OPTEE video firmware installation failed" return 1 fi # Create Debian package if ! __optee_video_firmware_create_deb "${build_dir}" "${install_dir}"; then aml_log error "AMLOGIC: Package creation failed" return 1 fi display_alert "AMLOGIC: OPTEE video firmware package build completed successfully" "success" return 0 } # ============================================================================== # Section 5: Main # ============================================================================== aml_log info "AMLIbian OPTEE Video Firmware Build Script" "TDK Version: ${PLATFORM_TDK_VERSION}, Arch: ${ARCH}, Board: ${BOARD}" amlbian_build_optee_video_firmware aml_log info "AMLOGIC: OPTEE video firmware build completed" "${BUILD_DIR}/output" # ============================================================================== # End of build-optee-video-firmware.sh # ==============================================================================