#!/usr/bin/env bash # # SPDX-License-Identifier: GPL-2.0 # # Copyright (c) 2026 AMLOGIC # # Packaging functions for AMLbian # ============================================================================== # Section 1: U-Boot Packaging # ============================================================================== # Package U-Boot binaries function amlbian_package_uboot() { display_alert "AMLOGIC TODO: Packaging U-Boot" "${BOARD}" "info" local uboot_dir="${1}" local output_dir="${2}" # AMLOGIC TODO: Implement custom U-Boot packaging # This function should handle: # 1. Signing U-Boot binaries if required # 2. Creating final U-Boot package display_alert "AMLOGIC TODO: Using Armbian native U-Boot packaging" "${BOARD}" "info" return 0 } # ============================================================================== # Section 2: Kernel Packaging # ============================================================================== # ============================================================================== # Kernel Image DEB Packaging Main Function (without modules and dtbs) # ============================================================================== function package_kernel_deb_amlbian_main() { local kernel_build_dir="${1}" local kernel_version="${2}" local kernel_config="${3}" display_alert "PACKAGE-KERNEL Image DEB Packaging" # Check if kernel build directory exists if [[ -z "${kernel_build_dir}" || ! -d "${kernel_build_dir}" ]]; then display_alert "PACKAGE-KERNEL Kernel build directory not found: ${kernel_build_dir}" display_alert "PACKAGE-KERNEL Skipping Kernel DEB packaging" return 0 fi # Check for kernel artifacts local kernel_image="${kernel_build_dir}/arch/arm64/boot/Image" if [[ ! -f "${kernel_image}" ]]; then # Try alternative locations kernel_image=$(find "${kernel_build_dir}" -name "Image" -o -name "vmlinuz" -o -name "zImage" 2>/dev/null | head -1) if [[ -z "${kernel_image}" || ! -f "${kernel_image}" ]]; then display_alert "PACKAGE-KERNEL Kernel image not found in: ${kernel_build_dir}" display_alert "PACKAGE-KERNEL Cannot create DEB package without kernel image" return 0 fi fi # Get kernel version if not provided if [[ -z "${kernel_version}" ]]; then kernel_version=$(get_kernel_version "${kernel_build_dir}") if [[ -z "${kernel_version}" ]]; then display_alert "PACKAGE-KERNEL Could not determine kernel version" kernel_version="unknown-$(date +%Y%m%d)" fi fi display_alert "PACKAGE-KERNEL Creating Kernel Image DEB package..." display_alert "PACKAGE-KERNEL Build Directory: ${kernel_build_dir}" display_alert "PACKAGE-KERNEL Kernel Version: ${kernel_version}" display_alert "PACKAGE-KERNEL Kernel Config: ${kernel_config}" # If BUILD_REPOSITORY_COMMIT is not provided, try to derive it from kernel source if [[ -z "${BUILD_REPOSITORY_COMMIT:-}" ]]; then local kernel_source_dir="${KERNEL_SOURCE_DIR:-}" if command -v git >/dev/null 2>&1 && [[ -n "${kernel_source_dir}" && -d "${kernel_source_dir}" ]]; then if git -C "${kernel_source_dir}" rev-parse --git-dir >/dev/null 2>&1; then BUILD_REPOSITORY_COMMIT="$(git -C "${kernel_source_dir}" rev-parse --short=7 HEAD 2>/dev/null || true)" export BUILD_REPOSITORY_COMMIT display_alert "PACKAGE-KERNEL Derived BUILD_REPOSITORY_COMMIT=${BUILD_REPOSITORY_COMMIT} from kernel source" fi fi fi # ==================================================================== # Step 1: Prepare DEB Package Structure # ==================================================================== local package_name="linux-image-${BOARD}-amlbian" local package_version="${kernel_version}-${BUILD_REPOSITORY_COMMIT:-0}" local package_arch="arm64" # Create temporary package directory local temp_dir="${SRC}/output/temp-kernel-deb" rm -rf "${temp_dir}" mkdir -p "${temp_dir}/${package_name}" # Create DEBIAN control directory local control_dir="${temp_dir}/${package_name}/DEBIAN" mkdir -p "${control_dir}" # ==================================================================== # Step 2: Create Control File # ==================================================================== cat > "${control_dir}/control" << EOF Package: ${package_name} Version: ${package_version} Architecture: ${package_arch} Maintainer: Amlbian Build System Installed-Size: $(du -sk "${kernel_build_dir}" | cut -f1) Section: kernel Priority: optional Provides: linux-image, linux-image-${BOARD} Conflicts: linux-image-${BOARD}-armbian Description: Custom Linux Kernel Image for ${BOARD} (Amlbian) This package contains a custom Linux kernel image built from local sources for ${BOARD}. It includes only the kernel image and essential boot files. . Features: - Custom kernel ${kernel_version} - Kernel image optimized for ${BOARD} performance - Built with Amlbian build system EOF # ==================================================================== # Step 3: Copy Kernel Files # ==================================================================== # Create directories in package local boot_dir="${temp_dir}/${package_name}/boot" local usr_lib_kernel_dir="${temp_dir}/${package_name}/usr/lib/${package_name}" mkdir -p "${boot_dir}" "${usr_lib_kernel_dir}" # Copy kernel image display_alert "PACKAGE-KERNEL Copying kernel image: $(basename ${kernel_image})" cp "${kernel_image}" "${boot_dir}/Image-${kernel_version}" # Create symlink ln -sf "Image-${kernel_version}" "${boot_dir}/Image" # O= out-of-tree build keeps .config / System.map under build/ subdir. # Without /boot/config-, postinst's update-initramfs greps empty # file for CONFIG_RD_GZIP and aborts. local kernel_config_file="" system_map_file="" for cand in "${kernel_build_dir}/build/.config" "${kernel_build_dir}/.config"; do [[ -f "${cand}" ]] && { kernel_config_file="${cand}"; break; } done for cand in "${kernel_build_dir}/build/System.map" "${kernel_build_dir}/System.map"; do [[ -f "${cand}" ]] && { system_map_file="${cand}"; break; } done [[ -n "${kernel_config_file}" ]] && cp "${kernel_config_file}" "${boot_dir}/config-${kernel_version}" [[ -n "${system_map_file}" ]] && cp "${system_map_file}" "${boot_dir}/System.map-${kernel_version}" # ==================================================================== # Step 4: Create Maintainer Scripts # ==================================================================== # Create postinst script cat > "${control_dir}/postinst" << 'EOF' #!/bin/bash set -e kernel_version="${kernel_version}" package_name="${package_name}" echo "[AMLBIAN] Installing custom kernel image for ${BOARD}..." # Check if this is a real installation if [ "$1" = "configure" ]; then echo " Kernel image installed to /boot/Image-\${kernel_version}" # Update kernel symlinks if [ -f "/boot/Image-\${kernel_version}" ]; then ln -sf "Image-\${kernel_version}" "/boot/Image" 2>/dev/null || true ln -sf "Image-\${kernel_version}" "/boot/vmlinuz" 2>/dev/null || true ln -sf "Image-\${kernel_version}" "/boot/vmlinuz-\${kernel_version}" 2>/dev/null || true fi fi exit 0 EOF chmod 755 "${control_dir}/postinst" # Create prerm script cat > "${control_dir}/prerm" << 'EOF' #!/bin/bash set -e kernel_version="${kernel_version}" echo "[AMLBIAN] Removing custom kernel image for ${BOARD}..." if [ "$1" = "remove" ]; then echo " Removing kernel image..." # Remove kernel image rm -f "/boot/Image-\${kernel_version}" "/boot/Image" "/boot/vmlinuz-\${kernel_version}" "/boot/vmlinuz" # Remove config and System.map rm -f "/boot/config-\${kernel_version}" "/boot/System.map-\${kernel_version}" fi exit 0 EOF chmod 755 "${control_dir}/prerm" # ==================================================================== # Step 5: Build DEB Package # ==================================================================== local output_dir="${SRC}/output" mkdir -p "${output_dir}" display_alert "PACKAGE-KERNEL Building DEB package..." # Change to temp directory and build package cd "${temp_dir}" || { display_alert "PACKAGE-KERNEL Failed to change to temp directory" "err" return 1 } dpkg-deb --build "${package_name}" 2>&1 | while read line; do display_alert "PACKAGE-KERNEL dpkg-deb: ${line}" done local exit_code=${PIPESTATUS[0]} cd - > /dev/null if [[ ${exit_code} -ne 0 ]]; then display_alert "PACKAGE-KERNEL Failed to build DEB package" "err" rm -rf "${temp_dir}" return ${exit_code} fi # Move package to output directory local deb_file="${package_name}_${package_version}_${package_arch}.deb" mv "${temp_dir}/${package_name}.deb" "${output_dir}/${deb_file}" # Clean up rm -rf "${temp_dir}" # ==================================================================== # Step 6: Verify Package # ==================================================================== if [[ -f "${output_dir}/${deb_file}" ]]; then display_alert "PACKAGE-KERNEL DEB package created: ${deb_file} success" display_alert "PACKAGE-KERNEL Size: $(du -h "${output_dir}/${deb_file}" | cut -f1)" # Show package info display_alert "PACKAGE-KERNEL Package contents:" dpkg-deb -c "${output_dir}/${deb_file}" 2>/dev/null | head -10 | while read line; do display_alert "PACKAGE-KERNEL ${LINENO}" done # Create package manifest create_package_manifest "${output_dir}" "${deb_file}" "Kernel-Image" return 0 else display_alert "PACKAGE-KERNEL DEB package not created" "err" return 1 fi } # ============================================================================== # Kernel Modules DEB Packaging Main Function # ============================================================================== function package_kmod_deb_amlbian_main() { local kernel_build_dir="${1}" local kernel_version="${2}" display_alert "PACKAGE-KERNEL Modules DEB Packaging" # Check if kernel build directory exists if [[ -z "${kernel_build_dir}" || ! -d "${kernel_build_dir}" ]]; then amlbian_log "warn" "PACKAGE-KMOD" "Kernel build directory not found: ${kernel_build_dir}" display_alert "PACKAGE-KMOD Skipping Kernel Modules DEB packaging" return 0 fi # Get kernel version if not provided if [[ -z "${kernel_version}" ]]; then kernel_version=$(get_kernel_version "${kernel_build_dir}") if [[ -z "${kernel_version}" ]]; then amlbian_log "warn" "PACKAGE-KMOD" "Could not determine kernel version" kernel_version="unknown-$(date +%Y%m%d)" fi fi display_alert "PACKAGE-KMOD Creating Kernel Modules DEB package..." display_alert "PACKAGE-KMOD Build Directory: ${kernel_build_dir}" display_alert "PACKAGE-KMOD Kernel Version: ${kernel_version}" # If BUILD_REPOSITORY_COMMIT is not provided, try to derive it from kernel source if [[ -z "${BUILD_REPOSITORY_COMMIT:-}" ]]; then local kernel_source_dir="${KERNEL_SOURCE_DIR:-}" if command -v git >/dev/null 2>&1 && [[ -n "${kernel_source_dir}" && -d "${kernel_source_dir}" ]]; then if git -C "${kernel_source_dir}" rev-parse --git-dir >/dev/null 2>&1; then BUILD_REPOSITORY_COMMIT="$(git -C "${kernel_source_dir}" rev-parse --short=7 HEAD 2>/dev/null || true)" export BUILD_REPOSITORY_COMMIT display_alert "PACKAGE-KMOD Derived BUILD_REPOSITORY_COMMIT=${BUILD_REPOSITORY_COMMIT} from kernel source" fi fi fi # ==================================================================== # Step 1: Prepare DEB Package Structure # ==================================================================== local package_name="linux-modules-${BOARD}-amlbian" local package_version="${kernel_version}-${BUILD_REPOSITORY_COMMIT:-0}" local package_arch="arm64" # Create temporary package directory local temp_dir="${SRC}/output/temp-kernel-modules-deb" rm -rf "${temp_dir}" mkdir -p "${temp_dir}/${package_name}" # Create DEBIAN control directory local control_dir="${temp_dir}/${package_name}/DEBIAN" mkdir -p "${control_dir}" # ==================================================================== # Step 2: Create Control File # ==================================================================== cat > "${control_dir}/control" << EOF Package: ${package_name} Version: ${package_version} Architecture: ${package_arch} Maintainer: Amlbian Build System Installed-Size: $(du -sk "${kernel_build_dir}" | cut -f1) Section: kernel Priority: optional Provides: linux-modules, linux-modules-${BOARD} Conflicts: linux-modules-${BOARD}-armbian Description: Custom Linux Kernel Modules for ${BOARD} (Amlbian) This package contains kernel modules for a custom Linux kernel built from local sources for ${BOARD}. . Features: - Kernel modules for kernel ${kernel_version} - Built with Amlbian build system Depends: kmod EOF # ==================================================================== # Step 3: Copy Kernel Modules # ==================================================================== # Create directories in package local lib_modules_dir="${temp_dir}/${package_name}/lib/modules/${kernel_version}" mkdir -p "${lib_modules_dir}" # Copy kernel modules. Prefer explicit staging path exported by compile-kernel. local modules_source_dir="" if [[ -n "${AMLBIAN_KERNEL_MODULES_DIR:-}" && -d "${AMLBIAN_KERNEL_MODULES_DIR}" ]]; then modules_source_dir="${AMLBIAN_KERNEL_MODULES_DIR}" elif [[ -d "${kernel_build_dir}/lib/modules/${kernel_version}" ]]; then modules_source_dir="${kernel_build_dir}/lib/modules/${kernel_version}" elif [[ -d "${kernel_build_dir}/modules-staging/lib/modules/${kernel_version}" ]]; then modules_source_dir="${kernel_build_dir}/modules-staging/lib/modules/${kernel_version}" fi if [[ -n "${modules_source_dir}" ]]; then display_alert "PACKAGE-KMOD Copying kernel modules from: ${modules_source_dir}" # Create target directory structure first mkdir -p "${lib_modules_dir}" # Copy only runtime kernel modules and essential files, not build artifacts # We want: *.ko, modules.*, modules.dep*, modules.alias, modules.symbols, etc. # We don't want: *.o, *.cmd, *.d, built-in.a, .tmp_*, and other build artifacts # Copy all .ko files find "${modules_source_dir}" -name "*.ko" -type f 2>/dev/null | while read -r ko_file; do local rel_path="${ko_file#${modules_source_dir}/}" local target_dir="${lib_modules_dir}/$(dirname "${rel_path}")" mkdir -p "${target_dir}" cp -f "${ko_file}" "${target_dir}/" 2>/dev/null || true done if [ -d "${modules_source_dir}/../../../modules" ]; then cp -rf ${modules_source_dir}/../../../modules ${lib_modules_dir} fi # Copy essential module info files local essential_files=( "modules.order" "modules.builtin" "modules.builtin.modinfo" "modules.dep" "modules.dep.bin" "modules.alias" "modules.alias.bin" "modules.symbols" "modules.symbols.bin" "modules.devname" "modules.softdep" "modules.builtin.alias.bin" "modules.builtin.bin" ) for essential_file in "${essential_files[@]}"; do if [[ -f "${modules_source_dir}/${essential_file}" ]]; then cp -f "${modules_source_dir}/${essential_file}" "${lib_modules_dir}/" 2>/dev/null || true fi done # Also copy any .bin files that might be needed find "${modules_source_dir}" -maxdepth 1 -name "*.bin" -type f 2>/dev/null | while read -r bin_file; do cp -f "${bin_file}" "${lib_modules_dir}/" 2>/dev/null || true done # Copy module.* files (module metadata) find "${modules_source_dir}" -maxdepth 1 -name "modules.*" -type f 2>/dev/null | while read -r mod_file; do cp -f "${mod_file}" "${lib_modules_dir}/" 2>/dev/null || true done # Copy System.map if exists (needed for module dependencies) if [[ -f "${modules_source_dir}/../System.map" ]]; then cp -f "${modules_source_dir}/../System.map" "${lib_modules_dir}/" 2>/dev/null || true fi # Count how many .ko files were copied local ko_count=$(find "${lib_modules_dir}" -name "*.ko" -type f 2>/dev/null | wc -l) display_alert "PACKAGE-KMOD Copied ${ko_count} kernel module files (.ko)" # Count total files copied for logging local total_files=$(find "${lib_modules_dir}" -type f 2>/dev/null | wc -l) display_alert "PACKAGE-KMOD Total ${total_files} files in module package (optimized for size)" # Create symlinks for build and source (not needed for runtime, but kept for compatibility) if [[ -d "${kernel_build_dir}" ]]; then ln -sf "${kernel_build_dir}" "${lib_modules_dir}/build" 2>/dev/null || true fi if [[ -n "${KERNEL_SOURCE_DIR}" && -d "${KERNEL_SOURCE_DIR}" ]]; then ln -sf "${KERNEL_SOURCE_DIR}" "${lib_modules_dir}/source" 2>/dev/null || true fi else amlbian_log "warn" "PACKAGE-KMOD" "Kernel modules source directory not found" fi # postinst — first heredoc (unquoted) bakes kernel_version literal at # write time; second heredoc (quoted) keeps $1 etc. for dpkg. cat > "${control_dir}/postinst" << EOF #!/bin/bash set -e kernel_version="${kernel_version}" EOF cat >> "${control_dir}/postinst" << 'EOF' [ "$1" = "configure" ] || exit 0 # depmod first so update-initramfs can resolve module names. command -v depmod >/dev/null 2>&1 && depmod -a "${kernel_version}" 2>/dev/null || true # Create initramfs (reads /etc/initramfs-tools/conf.d/amlbian-most and modules # list written by build framework). -c for first install; -u fallback. if command -v update-initramfs >/dev/null 2>&1; then update-initramfs -c -k "${kernel_version}" 2>&1 || \ update-initramfs -u -k "${kernel_version}" 2>&1 || true fi # /boot/initrd.img symlink for U-Boot CONFIG_BOOTCOMMAND (kver-independent name). [ -f "/boot/initrd.img-${kernel_version}" ] && \ ln -sf "initrd.img-${kernel_version}" /boot/initrd.img 2>/dev/null || true exit 0 EOF chmod 755 "${control_dir}/postinst" # Create prerm script cat > "${control_dir}/prerm" << 'EOF' #!/bin/bash set -e kernel_version="${kernel_version}" echo "[AMLBIAN] Removing custom kernel modules for ${BOARD}..." if [ "$1" = "remove" ]; then echo " Removing kernel modules..." # Remove modules directory if [ -d "/lib/modules/\${kernel_version}" ]; then rm -rf "/lib/modules/\${kernel_version}" fi # Remove initramfs if command -v update-initramfs >/dev/null 2>&1; then update-initramfs -d -k "\${kernel_version}" 2>/dev/null || true fi fi exit 0 EOF chmod 755 "${control_dir}/prerm" # ==================================================================== # Step 5: Build DEB Package # ==================================================================== local output_dir="${SRC}/output" mkdir -p "${output_dir}" display_alert "PACKAGE-KMOD Building DEB package..." # Change to temp directory and build package cd "${temp_dir}" || { display_alert "PACKAGE-KMOD Failed to change to temp directory" "err" return 1 } dpkg-deb --build "${package_name}" 2>&1 | while read line; do display_alert "PACKAGE-KERNEL PACKAGE-KMOD" "dpkg-deb: ${line}" done local exit_code=${PIPESTATUS[0]} cd - > /dev/null if [[ ${exit_code} -ne 0 ]]; then display_alert "PACKAGE-KMOD Failed to build DEB package" rm -rf "${temp_dir}" return ${exit_code} fi # Move package to output directory local deb_file="${package_name}_${package_version}_${package_arch}.deb" mv "${temp_dir}/${package_name}.deb" "${output_dir}/${deb_file}" # Clean up rm -rf "${temp_dir}" # ==================================================================== # Step 6: Verify Package # ==================================================================== if [[ -f "${output_dir}/${deb_file}" ]]; then display_alert "PACKAGE-KMOD DEB package created: ${deb_file}" display_alert "PACKAGE-KMOD Size: $(du -h "${output_dir}/${deb_file}" | cut -f1)" # Show package info display_alert "PACKAGE-KMOD Package contents:" dpkg-deb -c "${output_dir}/${deb_file}" 2>/dev/null | head -10 | while read line; do display_alert "PACKAGE-KERNEL PACKAGE-KMOD" " ${line}" done # Create package manifest create_package_manifest "${output_dir}" "${deb_file}" "Kernel-Modules" return 0 else display_alert "PACKAGE-KMOD DEB package not created" return 1 fi } # ============================================================================== # DTB DEB Packaging Main Function # ============================================================================== function package_dtb_deb_amlbian_main() { local kernel_build_dir="${1}" local kernel_version="${2}" display_alert "PACKAGE-KERNEL DTB DEB Packaging" # Check if kernel build directory exists if [[ -z "${kernel_build_dir}" || ! -d "${kernel_build_dir}" ]]; then display_alert "PACKAGE-DTB Kernel build directory not found: ${kernel_build_dir}" display_alert "PACKAGE-DTB Skipping DTB DEB packaging" return 0 fi # Get kernel version if not provided if [[ -z "${kernel_version}" ]]; then kernel_version=$(get_kernel_version "${kernel_build_dir}") if [[ -z "${kernel_version}" ]]; then display_alert "PACKAGE-DTB Could not determine kernel version" kernel_version="unknown-$(date +%Y%m%d)" fi fi display_alert "PACKAGE-DTB Creating DTB DEB package..." display_alert "PACKAGE-DTB Build Directory: ${kernel_build_dir}" display_alert "PACKAGE-DTB Kernel Version: ${kernel_version}" # If BUILD_REPOSITORY_COMMIT is not provided, try to derive it from kernel source if [[ -z "${BUILD_REPOSITORY_COMMIT:-}" ]]; then local kernel_source_dir="${KERNEL_SOURCE_DIR:-}" if command -v git >/dev/null 2>&1 && [[ -n "${kernel_source_dir}" && -d "${kernel_source_dir}" ]]; then if git -C "${kernel_source_dir}" rev-parse --git-dir >/dev/null 2>&1; then BUILD_REPOSITORY_COMMIT="$(git -C "${kernel_source_dir}" rev-parse --short=7 HEAD 2>/dev/null || true)" export BUILD_REPOSITORY_COMMIT display_alert "PACKAGE-DTB Derived BUILD_REPOSITORY_COMMIT=${BUILD_REPOSITORY_COMMIT} from kernel source" fi fi fi # ==================================================================== # Step 1: Prepare DEB Package Structure # ==================================================================== local package_name="linux-dtb-${BOARD}-amlbian" local package_version="${kernel_version}-${BUILD_REPOSITORY_COMMIT:-0}" local package_arch="arm64" # Create temporary package directory local temp_dir="${SRC}/output/temp-kernel-dtb-deb" rm -rf "${temp_dir}" mkdir -p "${temp_dir}/${package_name}" # Create DEBIAN control directory local control_dir="${temp_dir}/${package_name}/DEBIAN" mkdir -p "${control_dir}" # ==================================================================== # Step 2: Create Control File # ==================================================================== cat > "${control_dir}/control" << EOF Package: ${package_name} Version: ${package_version} Architecture: ${package_arch} Maintainer: Amlbian Build System Installed-Size: $(du -sk "${kernel_build_dir}" | cut -f1) Section: kernel Priority: optional Provides: linux-dtb, linux-dtb-${BOARD} Conflicts: linux-dtb-${BOARD}-armbian Description: Custom Linux DTBs for ${BOARD} (Amlbian) This package contains device tree blobs for a custom Linux kernel built from local sources for ${BOARD}. . Features: - DTBs for kernel ${kernel_version} - Optimized for Amlogic devices - Built with Amlbian build system EOF # ==================================================================== # Step 3: Copy DTB Files # ==================================================================== # Create directories in package local boot_dir="${temp_dir}/${package_name}/boot" local usr_lib_kernel_dir="${temp_dir}/${package_name}/usr/lib/${package_name}" mkdir -p "${boot_dir}" "${usr_lib_kernel_dir}" # Copy device tree blobs (dtbs) local dtb_source_dir="" if [[ -n "${AMLBIAN_KERNEL_DTB_DIR:-}" && -d "${AMLBIAN_KERNEL_DTB_DIR}" ]]; then dtb_source_dir="${AMLBIAN_KERNEL_DTB_DIR}" elif [[ -d "${kernel_build_dir}/dtb-staging" ]]; then dtb_source_dir="${kernel_build_dir}/dtb-staging" elif [[ -d "${kernel_build_dir}/arch/arm64/boot/dts/amlogic" ]]; then dtb_source_dir="${kernel_build_dir}/arch/arm64/boot/dts/amlogic" fi if [[ -n "${dtb_source_dir}" ]]; then display_alert "PACKAGE-DTB Copying device tree blobs from: ${dtb_source_dir}" find "${dtb_source_dir}" -name "*.dtb" -type f 2>/dev/null | while read -r dtb; do cp "${dtb}" "${usr_lib_kernel_dir}/" 2>/dev/null || true done # Also copy to boot directory for compatibility with Armbian standard find "${dtb_source_dir}" -name "*.dtb" -type f 2>/dev/null | while read -r dtb; do cp "${dtb}" "${boot_dir}/" 2>/dev/null || true done # Create dtb directory in boot for Armbian compatibility local dtb_count=$(find "${usr_lib_kernel_dir}" -name "*.dtb" -type f 2>/dev/null | wc -l) display_alert "PACKAGE-DTB Copied ${dtb_count} DTB files" else display_alert "PACKAGE-DTB No DTB source directory found" return 0 fi # ==================================================================== # Step 4: Create Maintainer Scripts # ==================================================================== # Create postinst script cat > "${control_dir}/postinst" << 'EOF' #!/bin/bash set -e kernel_version="${kernel_version}" package_name="${package_name}" echo "[AMLBIAN] Installing custom DTBs for ${BOARD}..." # Check if this is a real installation if [ "$1" = "configure" ]; then echo " DTBs installed to /usr/lib/\${package_name}/" # Deploy DTBs to boot partition for compatibility if [ -d "/usr/lib/\${package_name}" ]; then mkdir -p "/boot/dtb-\${kernel_version}" cp -a /usr/lib/\${package_name}/*.dtb "/boot/dtb-\${kernel_version}/" 2>/dev/null || true ln -sfn "dtb-\${kernel_version}" "/boot/dtb" 2>/dev/null || true fi fi exit 0 EOF chmod 755 "${control_dir}/postinst" # Create prerm script cat > "${control_dir}/prerm" << 'EOF' #!/bin/bash set -e kernel_version="${kernel_version}" echo "[AMLBIAN] Removing custom DTBs for ${BOARD}..." if [ "$1" = "remove" ]; then echo " Removing DTBs..." # Remove dtb directories rm -rf "/boot/dtb-\${kernel_version}" "/boot/dtb" fi exit 0 EOF chmod 755 "${control_dir}/prerm" # ==================================================================== # Step 5: Build DEB Package # ==================================================================== local output_dir="${SRC}/output" mkdir -p "${output_dir}" display_alert "PACKAGE-DTB Building DEB package..." # Change to temp directory and build package cd "${temp_dir}" || { amlbian_log "error" "PACKAGE-DTB" "Failed to change to temp directory" return 1 } dpkg-deb --build "${package_name}" 2>&1 | while read line; do display_alert "PACKAGE-KERNEL PACKAGE-DTB" "dpkg-deb: ${line}" done local exit_code=${PIPESTATUS[0]} cd - > /dev/null if [[ ${exit_code} -ne 0 ]]; then amlbian_log "error" "PACKAGE-DTB" "Failed to build DEB package" rm -rf "${temp_dir}" return ${exit_code} fi # Move package to output directory local deb_file="${package_name}_${package_version}_${package_arch}.deb" mv "${temp_dir}/${package_name}.deb" "${output_dir}/${deb_file}" # Clean up rm -rf "${temp_dir}" # ==================================================================== # Step 6: Verify Package # ==================================================================== if [[ -f "${output_dir}/${deb_file}" ]]; then display_alert "PACKAGE-DTB DEB package created: ${deb_file}" display_alert "PACKAGE-DTB Size: $(du -h "${output_dir}/${deb_file}" | cut -f1)" # Show package info display_alert "PACKAGE-DTB Package contents:" dpkg-deb -c "${output_dir}/${deb_file}" 2>/dev/null | head -10 | while read line; do display_alert "PACKAGE-KERNEL PACKAGE-DTB" " ${line}" done # Create package manifest create_package_manifest "${output_dir}" "${deb_file}" "Kernel-DTB" return 0 else amlbian_log "error" "PACKAGE-DTB" "DEB package not created" return 1 fi } # ============================================================================== # Section 3: Image Packaging # ============================================================================== # Package final image function amlbian_package_image() { display_alert "AMLOGIC TODO: Packaging final image" "${BOARD}" "info" local image_file="${DESTIMG}/${BOARD}.img" # AMLOGIC TODO: Implement custom image packaging # This function should handle: # 1. Custom partition layout if needed # 2. Image signing # 3. Custom compression # 4. Metadata generation if [[ -f "${image_file}" ]]; then display_alert "AMLOGIC TODO: Processing image" "${image_file}" "info" # AMLOGIC TODO: Add custom packaging steps here # Example: # amlbian_sign_image "${image_file}" # amlbian_compress_image "${image_file}" display_alert "AMLOGIC TODO: Custom image packaging complete" "${image_file}" "info" else display_alert "AMLOGIC TODO: Image file not found" "${image_file}" "warn" fi return 0 } # ============================================================================== # Section 4: BSP Packaging # ============================================================================== # Package BSP (Board Support Package) function amlbian_package_bsp() { display_alert "AMLOGIC TODO: Packaging BSP" "${BOARD}" "info" # AMLOGIC TODO: Implement BSP packaging # This should include: # 1. GPU drivers # 2. NPU firmware # 3. WiFi firmware # 4. Custom device tree overlays display_alert "AMLOGIC TODO: Using Armbian native BSP packaging" "${BOARD}" "info" return 0 } # ============================================================================== # Section 5: Firmware Packaging # ============================================================================== # Package firmware files function amlbian_package_firmware() { display_alert "AMLOGIC TODO: Packaging firmware" "${BOARD}" "info" local fw_source="${AMLBIAN_DIR}/firmware" local fw_dest="${destination}/lib/firmware" # AMLOGIC TODO: Package custom firmware files if [[ -d "${fw_source}" ]]; then display_alert "AMLOGIC TODO: Copying custom firmware" "${fw_source} -> ${fw_dest}" "info" mkdir -p "${fw_dest}" cp -r "${fw_source}/." "${fw_dest}/" 2>/dev/null || true fi return 0 } # ============================================================================== # Section 6: Custom Image Hook # ============================================================================== # Custom image creation hook function amlbian_custom_image_packaging() { display_alert "AMLOGIC TODO: Custom image packaging hook" "${BOARD}" "info" # This is called during image creation # Use this to add any final customizations # AMLOGIC TODO: Add post-image-creation customizations return 0 } # ============================================================================== # Section 7: Image Post-Processing # ============================================================================== # Post-process image after creation function amlbian_post_image_creation() { display_alert "AMLOGIC TODO: Post-image-creation processing" "${BOARD}" "info" local image_file="${DESTIMG}/${IMAGE输出_NAME:-${BOARD}.img}" if [[ -f "${image_file}" ]]; then # AMLOGIC TODO: Add post-creation processing # Example: Generate checksums # md5sum "${image_file}" > "${image_file}.md5" display_alert "AMLOGIC TODO: Image post-processing complete" "${image_file}" "info" fi return 0 } # ============================================================================== # End of function-package.sh # ==============================================================================