# SPDX-License-Identifier: UPL-1.0
# shellcheck shell=bash
# coding: utf-8
#
# Copyright (c) 2025, Oracle and/or its affiliates.
#
# Configuration file for Confidential Computing (CoCo) scripts
# with definitions and default values.
#

export PKG_CMD="dnf"

export COCO_PKG_LIST="grub* kernel* *ovmf* qemu*"

export SEV_HOST=0
export SEV_GUEST=0
export SEV_ES_HOST=0
export SEV_ES_GUEST=0
export SEV_SNP_HOST=0
export SEV_SNP_GUEST=0
export TDX_HOST=0
export TDX_GUEST=0

detect_coco_hardware() {
    if [[ "$(cat /sys/module/kvm_intel/parameters/tdx 2> /dev/null)" == "Y" ]]; then
        export TDX_HOST=1
	export COCO_MACHINE_ARGS=",confidential-guest-support=tdx"
    elif [[ -c /dev/tdx_guest ]]; then
        export TDX_GUEST=1
    elif [[ "$(cat /sys/module/kvm_amd/parameters/sev_snp 2> /dev/null)" == "Y" ]]; then
        export SEV_SNP_HOST=1
	export COCO_PKG_LIST="$COCO_PKG_LIST veraison*"
	export COCO_MACHINE_ARGS=",confidential-guest-support=sev-snp-guest"
    elif [[ "$(cat /sys/module/kvm_amd/parameters/sev_es 2> /dev/null)" == "Y" ]]; then
        export SEV_ES_HOST=1
    elif [[ "$(cat /sys/module/kvm_amd/parameters/sev 2> /dev/null)" == "Y" ]]; then
        export SEV_HOST=1
	export COCO_MACHINE_ARGS=",confidential-guest-support=sev-guest"
    elif [[ "$(dmesg | grep 'SEV: Status:')" =~ "SEV-SNP" ]]; then
        export SEV_SNP_GUEST=1
    elif [[ "$(dmesg | grep 'SEV: Status:')" =~ "SEV-ES" ]]; then
        export SEV_ES_GUEST=1
    elif [[ -c /dev/sev_guest ]]; then
        export SEV_GUEST=1
    fi
}

# unit-test functions if called directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
	detect_coco_hardware
fi
