#!/bin/bash

# SPDX-FileCopyrightText: Copyright (c) 2020-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.

# This script runs on target pre installation of nvidia-core debian package.
set -e

BUILD="generic"

if [ -e "/opt/nvidia/l4t-packages/.nv-l4t-disable-boot-fw-update-in-preinstall" ]; then
	echo "Pre-installing... skip compatibility checking."
	exit 0
fi

# Check platform compatibility
compatible="yes"
CHIP="$(tr '\0' ' ' < /proc/device-tree/compatible)"
if [[ ${CHIP} =~ "tegra234" ]]; then
	if [ "${BUILD}" != "generic" ]; then
		compatible="no"
	fi
else
	compatible="no"
fi

if [ "${compatible}" = "no" ]; then
	echo "Target SOC: ${CHIP}, package build: ${BUILD}"
	echo "The package is incompatible with this platform, quit."
	exit 1
fi

# Get and check the partlabel of the partitions on active storage:
# if the partlabel is APP_b, rootfs A/B is enabled, or rootfs is disabled.
rootfs_ab="false"
mount_dev_name=$( findmnt -n -o SOURCE --target / )
active_storage=$( lsblk -sl "${mount_dev_name}" -o NAME,TYPE | grep "disk" | awk '{print $1}' )
active_dev_node="/dev/${active_storage}"
num_of_partitions=$( partx -g "${active_dev_node}" | wc -l )

# shellcheck disable=SC2086
for index in $(seq 1 ${num_of_partitions});
do
	partlabel=$( sfdisk --part-label "${active_dev_node}" "${index}" )
	if [ "${partlabel}" = "APP_b" ]; then
		rootfs_ab="true"
		break
	fi
done

# Check rootfs A/B, if NOT enabled, go on Debian update;
# or print warning message and exit
if [ "${rootfs_ab}" = "true" ]; then
	echo "WARNING: rootfs A/B is enabled, can not update using Debian OTA, quit."
	exit 1
else
	echo "INFO: rootfs A/B is not enabled."
fi
