Backup restore, workflow #3 massflash jetson orin nano problem

Hi,

#!/bin/bash

# SPDX-FileCopyrightText: Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Contributor License Agreement (CLA):
# https://github.com/NVIDIA/DALI_extra/blob/main/NVIDIA_CLA_v1.0.1.docx

L4T_BACKUP_PARTITIONS_SCRIPT="nvbackup_partitions.sh"
L4T_RESTORE_PARTITIONS_SCRIPT="nvrestore_partitions.sh"


function is_tar_archive
{
	[[ "${1}" =~ \.tar\.gz ]]
}

addentry() {
	if [ "$#" -ne 7 ]; then
		print_message "addentry function needs 6 parameters when adding an entry to partition map."
		return 1
	fi
	# shellcheck disable=SC2001
	echo "${@}" | sed s/" "/,/g
}

convert_backup_image_to_initrd_flash()
{
	local source="${1}"
	local destination="${2}"
	local line_index=0
	local internal_text
	local external_text=""
	local mmc_text=""
	local mmcboot_text=""
	local qspi_text=""
	local APPcount=0

	rm -rf "${destination:?}"/*

	pushd "${source}"

	mkdir -p "${destination}/internal"

	local external_images="$(ls | grep -E 'nvme0n1*|sda*|mmcblk1*')"
	if [[ -n "${external_images}" ]]; then
		mkdir -p "${destination}/external"
		ls | grep -v -E 'gpt*|nvme0n1*|sda*|mmcblk1*' | xargs cp -r -t "${destination}/internal/"
		ls | grep -E 'gpt*|nvme0n1*|sda*|mmcblk1*' | xargs cp -r -t "${destination}/external/"
	else
		cp  -r * "${destination}/internal/"
	fi

	while read -r p; do
		if [ "${line_index}" = "0" ]; then
			line_index=$((line_index + 1))
			continue
		fi
		declare -a FIELDS
		for part in {1..6}; do
			FIELDS[part]=$(echo "$p" | awk -F, -v part="${part}" '{print $part}')
		done

		if [ "${FIELDS[2]}" = "gpt_1" ]; then
			local sha1_chksum_gen=
			sha1_chksum_gen=$(sha1sum "${FIELDS[1]}" | cut -d\  -f 1)
			local size_file=
			size_file=$(stat -c%s "${FIELDS[1]}")
			if [[ -n "$external_images" ]]; then
				external_text+="$(addentry "9:0:primary_gpt" "$((FIELDS[3] * 512))" "$((FIELDS[4] * 512))" \
					"${FIELDS[1]}" "${size_file}" "fixed-<reserved>-0" "${sha1_chksum_gen}")"
				external_text+="\n"
			else
				mmc_text+="$(addentry "1:3:primary_gpt" "$((FIELDS[3] * 512))" "$((FIELDS[4] * 512))" \
					"${FIELDS[1]}" "${size_file}" "fixed-<reserved>-0" "${sha1_chksum_gen}")"
				mmc_text+="\n"
			fi
		elif [ "${FIELDS[2]}" = "gpt_2" ]; then
			local sha1_chksum_gen=
			sha1_chksum_gen=$(sha1sum "${FIELDS[1]}" | cut -d\  -f 1)
			local size_file=
			size_file=$(stat -c%s "${FIELDS[1]}")
			if [[ -n "$external_images" ]]; then
				external_text+="$(addentry "9:0:secondary_gpt" "$((FIELDS[3] * 512))" "$((FIELDS[4] * 512))" \
					"${FIELDS[1]}" "${size_file}" "fixed-<reserved>-0" "${sha1_chksum_gen}")"
				external_text+="\n"
			else
				mmc_text+="$(addentry "1:3:secondary_gpt" "$((FIELDS[3] * 512))" "$((FIELDS[4] * 512))" \
					"${FIELDS[1]}" "${size_file}" "fixed-<reserved>-0" "${sha1_chksum_gen}")"
				mmc_text+="\n"
			fi
		elif [[ "${FIELDS[2]}" =~ mmcblk0boot ]]; then
			local sha1_chksum_gen=
			sha1_chksum_gen=$(sha1sum "${FIELDS[1]}" | cut -d\  -f 1)
			local size_file=
			size_file=$(stat -c%s "${FIELDS[1]}")
			local partition_index
			partition_index=$([[ ${FIELDS[2]} =~ mmcblk0boot([[:digit:]]+) ]] && echo "${BASH_REMATCH[1]}")
			mmcboot_text+="$(addentry "0:3:${FIELDS[2]}" "$(((FIELDS[3] + FIELDS[4] * partition_index) * 512))" "$((FIELDS[4] * 512 * (partition_index + 1)))" \
				"${FIELDS[1]}" "${size_file}" "fixed-<reserved>-0" "${sha1_chksum_gen}")"
			mmcboot_text+="\n"
		elif [[ "${FIELDS[2]}" =~ mmcblk0 ]]; then
			local partition_name
			local sha1_chksum_gen=
			sha1_chksum_gen=$(sha1sum "${FIELDS[1]}" | cut -d\  -f 1)
			local size_file=
			local partition_index
			partition_index=$([[ ${FIELDS[1]} =~ mmcblk0p([[:digit:]]+) ]] && echo "${BASH_REMATCH[1]}")
			if is_tar_archive "${FIELDS[1]}"; then
				partition_name="APP"
				APPcount=$((APPcount+1))
				if [ ${APPcount} -eq 2 ]; then
					partition_name="APP_b"
				fi
				APP="${FIELDS[1]}"

				echo "${partition_name}=${APP}" >> "${destination}/internal/flash.cfg"
				pushd "${destination}/internal"
				gzip -vd "${APP}"
				mv "${APP/%.gz/}" "${APP}"
				sha1sum "${APP}" > "${destination}/internal/${APP}.sha1sum"
				popd
			else
				partition_name="mmcblk0p${partition_index}"
				pushd "${destination}/internal"
				gzip -S img -vd "${FIELDS[1]}"
				mv "${FIELDS[1]/%img/}" "${FIELDS[1]}"
				popd
			fi
			mmc_text+="$(addentry "1:3:${partition_name}" "$((FIELDS[3] * 512))" "$((FIELDS[4] * 512))" \
				"${FIELDS[1]}" "$((FIELDS[4] * 512))" "fixed-<reserved>-${partition_index}" "${sha1_chksum_gen}")"
			mmc_text+="\n"
		elif [[ "${FIELDS[2]}" =~ nvme0n1 || "${FIELDS[2]}" =~ sda || "${FIELDS[2]}" =~ mmcblk1 ]]; then
			echo "${FIELDS[1]}"
			local partition_name
			local sha1_chksum_gen=
			sha1_chksum_gen=$(sha1sum "${FIELDS[1]}" | cut -d\  -f 1)
			local size_file=
			local partition_index
			if [[ "${FIELDS[2]}" =~ nvme0n1 ]]; then
				partition_index=$([[ ${FIELDS[1]} =~ nvme0n1p([[:digit:]]+) ]] && echo "${BASH_REMATCH[1]}")
			elif [[ "${FIELDS[2]}" =~ sda ]]; then
				partition_index=$([[ ${FIELDS[1]} =~ sda([[:digit:]]+) ]] && echo "${BASH_REMATCH[1]}")
			else
				partition_index=$([[ ${FIELDS[1]} =~ mmcblk1([[:digit:]]+) ]] && echo "${BASH_REMATCH[1]}")
			fi
			if is_tar_archive "${FIELDS[1]}"; then
				partition_name="APP"
				APPcount=$((APPcount+1))
				if [ ${APPcount} -eq 2 ]; then
					partition_name="APP_b"
				fi
				APP="${FIELDS[1]}"

                external_device=${FIELDS[2]%[0-9]}
                external_device=${external_device%p}

				echo "external_device=${external_device}" >> "${destination}/internal/flash.cfg"
				echo "${partition_name}_ext=${APP}" >> "${destination}/external/flash.cfg"
				echo "external_device=${external_device}" >> "${destination}/external/flash.cfg"
				pushd "${destination}/external"
				gzip -vd "${APP}"
				mv "${APP/%.gz/}" "${APP}"
				sha1sum "${APP}" > "${destination}/external/${APP}.sha1sum"
				popd
			else
				if [ "${FIELDS[2]}" = nvme0n1 ]; then
					partition_name="nvme0n1p${partition_index}"
				elif [ "${FIELDS[2]}" = sda ]; then
					partition_name="sda${partition_index}"
				else
					partition_name="mmcblk1p${partition_index}"
				fi
				pushd "${destination}/external"
				gzip -S img -vd "${FIELDS[1]}"
				mv "${FIELDS[1]/%img/}" "${FIELDS[1]}"
				popd
			fi
			external_text+="$(addentry "9:0:${partition_name}" "$((FIELDS[3] * 512))" "$((FIELDS[4] * 512))" \
				"${FIELDS[1]}" "$((FIELDS[4] * 512))" "fixed-<reserved>-${partition_index}" "${sha1_chksum_gen}")"
			external_text+="\n"
		elif [[ "${FIELDS[2]}" =~ qspi ]]; then
			local sha1_chksum_gen=
			sha1_chksum_gen=$(sha1sum "${FIELDS[1]}" | cut -d\  -f 1)
			local size_file=
			size_file=$(stat -c%s "${FIELDS[1]}")
			local partition_index
			qspi_text+="$(addentry "3:0:${FIELDS[2]}" "$((FIELDS[3] * 512))" "$((FIELDS[4] * 512))" \
				"${FIELDS[1]}" "${size_file}" "fixed-<reserved>-0" "${sha1_chksum_gen}")"
			qspi_text+="\n"
		fi
		line_index=$((line_index + 1))
	done < "nvpartitionmap.txt"

	internal_text="${mmcboot_text}${qspi_text}${mmc_text}"
	line_index=0
	internal_text="$(echo -en "${internal_text}" | while read -r line; do
		echo "${line_index}, ${line}"; line_index=$((line_index + 1))
		done
		)"
	echo -e "${internal_text}" > "${destination}/internal/flash.idx"

	if [ -n "${external_text}" ]; then
		line_index=0
		external_text="$(echo -en "${external_text}" | while read -r line; do
			echo "${line_index}, ${line}"; line_index=$((line_index + 1))
			done
			)"
		echo -e "${external_text}" > "${destination}/external/flash.idx"
	fi

	cp "${destination}/../l4t_flash_from_kernel.sh" "${destination}"
	popd
}

This is the full script.
I built this patch on top of 35.4.1, and I’m sure it’s working fine on 35.4.1.
I didn’t verify on 35.3.1, so maybe there are something different between these two versions leading to the error.

Sure. Just run the l4t_backup_restore.func script directly.
You need some trick to set the folder path and move the code out of the function block.

I don’t see why you cannot.