#!/bin/sh #=============================================================================== # # Copyright (C) 2022-2024 by Digi International Inc. # All rights reserved. # # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License version 2 as published by # the Free Software Foundation. # # # Description: # Script will be called by swupdate to install a new u-boot within linux. #=============================================================================== UBOOT_NAME="$1" UBOOT_ENC="$2" UBOOT_SEEK_KB="$3" UBOOT_REDUNDANT="$4" UBOOT_TFA_NAME="$5" UBOOT_TFA_FILE="/tmp/${UBOOT_TFA_NAME}" UBOOT_FILE="/tmp/${UBOOT_NAME}" UBOOT_NAND_DUMP="/tmp/u-boot-dump.hex" UBOOT_ENCRYPTED_DEK="/tmp/u-boot-encrypted-with-dek.imx" DEK_FILE="/tmp/dek.bin" DEK_KEY_SIZE="32" DEK_BLOB_SIZE="$((DEK_KEY_SIZE + 56))" # DEK blob has an overhead of 56 bytes: header (8 bytes) + random AES-256 key (32 bytes) + MAC (16 bytes). DEK_BLOB_HEADER="8100584" # The last byte lacks one digit on purpose, to match 40, 41 and 42; all HAB versions. PLATFORM="$(tr -d '\0' "${DEK_FILE}" local rc=$? if [ "${rc}" -ne 0 ]; then exit_error "## ERROR: DEK dump to file failed." "${rc}" fi # Validate the DEK blob. if ! dd if="${DEK_FILE}" bs=1 count=4 2>/dev/null | hexdump -ve '1/1 "%.2X"' | grep -q "${DEK_BLOB_HEADER}"; then exit_error "## ERROR: Could not find DEK blob." fi } dump_dek () { case "${PLATFORM}" in ccimx6ul*) dump_dek_ccimx6ul ;; *) exit_error "## ERROR: Device not supported ${PLATFORM}." ;; esac } append_dek_ccimx6ul () { cat "${UBOOT_FILE}" "${DEK_FILE}" > "${UBOOT_ENCRYPTED_DEK}" local rc=$? if [ "${rc}" -ne 0 ]; then exit_error "## ERROR: Merging DEK with U-Boot image failed." "${rc}" fi } append_dek () { dump_dek case "${PLATFORM}" in ccimx6ul*) append_dek_ccimx6ul ;; *) exit_error "## ERROR: Device not supported: ${PLATFORM}." ;; esac UBOOT_FILE="${UBOOT_ENCRYPTED_DEK}" } write_file_to_nand () { local FLASH_DEV="$1" local FW_FILE="$2" # Sanity check. if [ ! -c "${FLASH_DEV}" ]; then exit_error "## ERROR: Invalid MTD partition: ${FLASH_DEV}." fi # Clean MTD partition. flash_eraseall "${FLASH_DEV}" local rc=$? if [ "${rc}" -ne 0 ]; then exit_error "## ERROR: Could not erase ${FLASH_DEV} partition." "${rc}" fi # Write file to NAND. nandwrite -p "${FLASH_DEV}" "${FW_FILE}" local rc=$? if [ "${rc}" -ne 0 ]; then exit_error "## ERROR: Could not write file to NAND." "${rc}" fi } get_mtd_number_from_partition () { local PARTITION_NAME="$1" local MTD_NUM="$(sed -ne "/${PARTITION_NAME}/s,^mtd\([0-9]\+\).*,\1,g;T;p" /proc/mtd)" echo "${MTD_NUM}" } # If U-Boot is encrypted, the DEK key blob needs to be extracted from existing U-Boot # and appended to the new U-Boot before writing it. if [ "${UBOOT_ENC}" = "enc" ]; then append_dek fi # Write U-Boot if expr "${PLATFORM}" : "ccmp1.*" >/dev/null; then # Install TFA file in fsbl1 partition. write_file_to_nand "/dev/mtd$(get_mtd_number_from_partition fsbl1)" "${UBOOT_TFA_FILE}" # Install U-Boot FIP file in fip-a partition. write_file_to_nand "/dev/mtd$(get_mtd_number_from_partition fip-a)" "${UBOOT_FILE}" # Check if redundant U-Boot update is requested. if [ "${UBOOT_REDUNDANT}" = "redundant" ]; then # Install TFA file in fsbl2 partition (redundant). write_file_to_nand "/dev/mtd$(get_mtd_number_from_partition fsbl2)" "${UBOOT_TFA_FILE}" # Install U-Boot FIP file in fip-b partition (redundant). write_file_to_nand "/dev/mtd$(get_mtd_number_from_partition fip-b)" "${UBOOT_FILE}" fi else # Mount debug file system to remove some kobs-ng warnings. if ! grep -qs debugfs /proc/mounts; then mount -t debugfs debugfs /sys/kernel/debug/ fi # Install U-Boot onto the Nand Flash using kobs-ng. kobs-ng init -x -v "${UBOOT_FILE}" rc=$? if [ "${rc}" -ne 0 ]; then exit_error "## ERROR: Could not write file to NAND." "${rc}" fi fi # Clean intermediate artifacts. clean_artifacts exit 0