#! /bin/sh
set -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"

. "$pkgdatadir/grub-mkconfig_lib"

CLASS="--class gnu-linux --class gnu --class os --class acrn"

# read ACRN config (board/scenario) from debconf
ACRN_BOARD=$(echo "get acrn-hypervisor/board" | debconf-communicate || true)
if [ "$(echo "${ACRN_BOARD}" | awk '{print $1}')" != "0" ]; then
    gettext_printf "ACRN: Cannot access debconf acrn-hypervisor/board: %s" "${ACRN_BOARD}\n" >&2
    exit 0
fi
ACRN_BOARD="$(echo "${ACRN_BOARD}" | awk '{print $2}')"

ACRN_SCENARIO=$(echo "get acrn-hypervisor/scenario" | debconf-communicate || true)
if [ "$(echo "${ACRN_SCENARIO}" | awk '{print $1}')" != "0" ]; then
    gettext_printf "ACRN: Cannot access debconf acrn-hypervisor/:scenario %s" "${ACRN_SCENARIO}\n" >&2
    exit 0
fi
ACRN_SCENARIO="$(echo "${ACRN_SCENARIO}" | awk '{print $2}')"

# we obtain any data from scenario config
ACRN_SCENARIO_FILE=/usr/lib/x86_64-linux-gnu/acrn/${ACRN_BOARD}/${ACRN_SCENARIO}/scenario.xml
if [ ! -f ${ACRN_SCENARIO_FILE} ]; then
    gettext_printf "ACRN: Missing scenario config %s\n" "${ACRN_SCENARIO_FILE}" >&2
    exit 0
fi

# get list of vm ids from scenario config
ACRN_VM_IDS=$(xmllint --xpath '//vm/@id' ${ACRN_SCENARIO_FILE} 2>/dev/null | sed 's/\s*id="\([^"]*\)"/\1/g')
if [ -z "${ACRN_VM_IDS}" ]; then
    gettext_printf "ACRN: No VMs defined in scenario config %s" "${ACRN_SCENARIO_FILE}\n" >&2
    exit 0
fi

# get number of configured pre-launched VMs
ACRN_PRE_LAUNCHED_VM_COUNT=$(xmllint --xpath "count(//vm[load_order=\"PRE_LAUNCHED_VM\"])" ${ACRN_SCENARIO_FILE})

for id in ${ACRN_VM_IDS}; do
  # get grub relevant data from scenario configuration (ugly handling of pseudo array in conventional shell, sigh!)
  eval ACRN_LOAD_ORDER_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/load_order/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true)
  eval ACRN_NAME_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/name/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true)
  eval ACRN_KERN_MOD_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/os_config/kern_mod/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true)
  eval ACRN_RAMDISK_MOD_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/os_config/ramdisk_mod/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true)
  eval ACRN_ACPI_MOD_VM${id}=$(xmllint --xpath "//vm[@id=\"${id}\"]/os_config/acpi_mod_tag/text()" ${ACRN_SCENARIO_FILE} 2>/dev/null || true)

  # eventually store Service VM id
  [ "$(eval echo \${ACRN_LOAD_ORDER_VM${id}})" = "SERVICE_VM" ] && ACRN_SERVICE_VM_ID=${id}

  if [ "$(eval echo \${ACRN_LOAD_ORDER_VM${id}})" = "PRE_LAUNCHED_VM" ]; then
    # pre-launched VMs always need ACPI data, eventually set acpi_mod_tag to default
    if [ -z "$(eval echo \${ACRN_ACPI_MOD_VM${id}})" ]; then
        eval ACRN_ACPI_MOD_VM${id}="ACPI_VM${id}"
        eval GRUB_ACRN_MOD_ACPI_VM${id}="/boot/ACPI_VM${id}.bin"
    fi
  fi
done

# get performance policy parameter from scenario configuration
ACRN_CPU_PERF_POLICY=$(xmllint --xpath '//CPU_PERFORMANCE_POLICY/text()' ${ACRN_SCENARIO_FILE} 2>/dev/null || true)
if [ -z "${ACRN_CPU_PERF_POLICY}" ]; then
  ACRN_CPU_PERF_POLICY=Performance
fi
GRUB_CMDLINE_ACRN="cpu_perf_policy=${ACRN_CPU_PERF_POLICY} ${GRUB_CMDLINE_ACRN}"

if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
  OS=GNU/Linux
else
  case ${GRUB_DISTRIBUTOR} in
    Ubuntu|Kubuntu)
      OS="${GRUB_DISTRIBUTOR}"
      ;;
    *)
      OS="${GRUB_DISTRIBUTOR} GNU/Linux"
      ;;
  esac
  CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
fi

# loop-AES arranges things so that /dev/loop/X can be our root device, but
# the initrds that Linux uses don't like that.
case ${GRUB_DEVICE} in
  /dev/loop/*|/dev/loop[0-9])
    GRUB_DEVICE=$(losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/")
    # We can't cope with devices loop-mounted from files here.
    case ${GRUB_DEVICE} in
      /dev/*) ;;
      *) exit 0 ;;
    esac
  ;;
esac

# btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
# and mounting btrfs requires user space scanning, so force UUID in this case.
if [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
    || ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
    || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
  LINUX_ROOT_DEVICE=${GRUB_DEVICE}
else
  LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi

case x"$GRUB_FS" in
    xbtrfs)
        rootsubvol="$(make_system_path_relative_to_its_root /)"
        rootsubvol="${rootsubvol#/}"
        if [ "x${rootsubvol}" != x ]; then
            GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
        fi;;
    xzfs)
        rpool=$(${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true)
        bootfs="$(make_system_path_relative_to_its_root / | sed -e "s,@$,,")"
        LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
        ;;
esac

# add_mod_tag: Add entry for a module tag
# add_mod_tag <tagtype> <tagname> <vmid>
# tagtype: Type of tag: kernel, ramdisk, acpi
# tagname: Name of the tag (from config)
# vmid: ID of the repsective VM
add_mod_tag()
{
    local tagtype=$1
    local tagname=$2
    local vmid=$3

    # silently skip empty tagnames
    if [ -z "${tagname}" ]; then
        return
    fi

    local tagvalue="$(eval echo \${GRUB_ACRN_MOD_${tagname}})"
    if [ -n "${tagvalue}" ]; then
        local message="$(gettext_printf "Loading ACRN %s %s for %s" "VM${vmid}" "${tagtype}" "$(eval echo \${ACRN_NAME_VM${vmid}})")"
        local basename=$(basename ${tagvalue})
        local dirname=$(dirname ${tagvalue})
        local rel_dirname=$(make_system_path_relative_to_its_root $dirname)
        local modparams
        local loaderparams

        # kernel might add a command line
        [ "${tagtype}" = "kernel" ] && modparams="$(eval echo \${GRUB_ACRN_MOD_CMDLINE_${tagname}})"
        # ramdisk will not be unzipped
        [ "${tagtype}" = "ramdisk" ] && loaderparams="--nounzip"
        cat << EOF
	echo	'$(echo "$message" | grub_quote)'
	${module_loader} ${loaderparams}	${rel_dirname}/${basename} ${tagname} ${modparams}
EOF
    else
        gettext_printf "ACRN: GRUB_ACRN_MOD_${tagname} not set, skipping\n" >&2
    fi
}


add_prelaunched_vms()
{
    # eventually also load data for pre-launched VMs
    for id in ${ACRN_VM_IDS}; do
        # we just care for pre-launched VMs
        [ "$(eval echo \${ACRN_LOAD_ORDER_VM${id}})" != "PRE_LAUNCHED_VM" ] && continue
        # start with kernel mod tag
        tagname="$(eval echo \${ACRN_KERN_MOD_VM${id}})"
        if [ -z "${tagname}" ]; then
           gettext_printf "ACRN: No kernel module tag set for %s(%s), skipping\n" "$(eval echo \${ACRN_NAME_VM${id}})" "VM${id}" >&2
           continue
        fi
        if [ -z "$(eval echo \${GRUB_ACRN_MOD_${tagname}})" ]; then
           gettext_printf "ACRN: GRUB_ACRN_MOD_${tagname} not set, skipping %s(%s)\n" "$(eval echo \${ACRN_NAME_VM${id}})" "VM${id}" >&2
           continue
        fi
        gettext_printf "Found ACRN pre-launched %s %s: %s\n" "VM${id}" "$(eval echo \${ACRN_NAME_VM${id}})" "$(eval echo \${GRUB_ACRN_MOD_${tagname}})">&2
        add_mod_tag kernel ${tagname} ${id}
        add_mod_tag acpi "$(eval echo \${ACRN_ACPI_MOD_VM${id}})" ${id}
        add_mod_tag ramdisk "$(eval echo \${ACRN_RAMDISK_MOD_VM${id}})" ${id}
    done
}

linux_entry ()
{
  os="$1"
  version="$2"
  acrn_version="$3"
  args="$4"
  acrn_args="$5"
  if [ -z "$boot_device_id" ]; then
      boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  fi
  title="$(gettext_printf "%s with ACRN hypervisor" "${os}")"
  echo "menuentry '$(echo "$title, with Linux ${version} (ACRN ${acrn_version})" | grub_quote)' ${CLASS} \$menuentry_id_option 'acrn-gnulinux-$boot_device_id' {"

  if [ -z "${prepare_boot_cache}" ]; then
    prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
  fi
  printf '%s\n' "${prepare_boot_cache}"
  message="$(gettext_printf "Loading ACRN hypervisor %s ..." ${acrn_version})"
  cat << EOF
	echo	'$(echo "$message" | grub_quote)'
	${acrn_loader}	${rel_acrn_dirname}/${acrn_basename} ${acrn_args} root=${linux_root_device_thisversion} ro ${args}
EOF
  message="$(gettext_printf "Loading ACRN Service VM Linux kernel %s ..." ${version})"
  ktagname="$(eval echo \${ACRN_KERN_MOD_VM${ACRN_SERVICE_VM_ID}})"
  cat << EOF
	echo	'$(echo "$message" | grub_quote)'
	${module_loader}	${rel_dirname}/${basename} ${ktagname} $(eval echo \${GRUB_ACRN_MOD_CMDLINE_${ktagname}})
EOF
  if test -n "${initrd}"; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    message="$(gettext_printf "Loading ACRN Service VM initial ramdisk ...")"
    rtagname="$(eval echo \${ACRN_RAMDISK_MOD_VM${ACRN_SERVICE_VM_ID}})"
    cat << EOF
	echo	'$(echo "$message" | grub_quote)'
	${module_loader} --nounzip	${rel_dirname}/${initrd} ${rtagname}
EOF
  fi
  add_prelaunched_vms
  cat << EOF
}
EOF
}

machine=$(uname -m)
# ACRN only on x86_64 machines
if [ "x${machine}" != "xx86_64" ]; then
    exit 0
fi
GENKERNEL_ARCH="x86"

# ServiceVM: Gather all Linux images with ACRN support, i.e. CONFIG_ACRN_HSM is set
linux_list=
for i in /boot/vmlinu[xz]-* /vmlinu[xz]-* /boot/kernel-*; do
    if grub_file_is_not_garbage "$i"; then
        basename=$(basename $i)
        version=$(echo $basename | sed -e "s,^[^0-9]*-,,g")
        alt_version=$(echo $version | sed -e "s,\.old$,,g")
        dirname=$(dirname $i)
        config=
        for j in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
            if test -e "${j}" ; then
                config="${j}"
                break
            fi
        done
        if grep -Eqx "^CONFIG_ACRN_HSM=(y|m)" "${config}" 2> /dev/null; then
            linux_list="$linux_list $i"
        fi
    fi
done

file_is_not_sym () {
    case "$1" in
        */acrn-syms-*)
        return 1;;
    *)
        return 0;;
    esac
}

# use ELF *.out files for multiboot
acrn_out_list=
for i in /boot/acrn*.out; do
    if grub_file_is_not_garbage "$i" && file_is_not_sym "$i" && grub-file --is-x86-multiboot "$i"; then
        acrn_out_list="$acrn_out_list $i"
    fi
done
# use raw binary *.bin files for multiboot2
acrn_bin_list=
for i in /boot/acrn*.bin; do
    if grub_file_is_not_garbage "$i" && file_is_not_sym "$i" && grub-file --is-x86-multiboot2 "$i"; then
        acrn_bin_list="$acrn_bin_list $i"
    fi
done
# we prefer multiboot2
if [ "x${acrn_bin_list}" != "x" ]; then
    acrn_list="${acrn_bin_list}"
    acrn_loader="multiboot2"
    module_loader="module2"
else
    acrn_list="${acrn_out_list}"
    acrn_loader="multiboot --quirk-modules-after-kernel"
    module_loader="module"
fi
# no ACRN binary found
if [ "x${acrn_list}" = "x" ] ; then
    exit 0
fi

prepare_boot_cache=
boot_device_id=
acrn_first_entry=

while [ "x${acrn_list}" != "x" ] ; do
    current_acrn=$(echo ${acrn_list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | LC_ALL=C sort -V -r | sed -e 's/ 1$/.old/; s/ 2$//' | head -n 1)
    acrn_basename=$(basename ${current_acrn})
    acrn_dirname=$(dirname ${current_acrn})
    rel_acrn_dirname=$(make_system_path_relative_to_its_root $acrn_dirname)
    acrn_version=$(echo $acrn_basename | sed -e "s,.out$,,g;s,.bin$,,g;s,^acrn-,,g")

    list="${linux_list}" # this is the list of possible ServiceVM kernels

    # no ACRN capable linux kernel, eventually add ACRN entry for partitioned system
    if ([ "x$list" = "x" ] || [ "x${ACRN_SERVICE_VM_ID}" = "x" ]) && [ "${ACRN_PRE_LAUNCHED_VM_COUNT}" != "0" ]; then
        if [ -z "${acrn_first_entry}" ]; then
            title="$(gettext_printf "%s with ACRN hypervisor" "${OS}")"
            acrn_first_entry="false"
        else
            title="$(gettext_printf "%s with ACRN hypervisor %s" "${OS}" "${acrn_version}")"
        fi
        echo "menuentry '$(echo "$title, with Linux ${version} (ACRN ${acrn_version})" | grub_quote)' ${CLASS} \$menuentry_id_option 'acrn-gnulinux-partitioned-${acrn_version}' {"
        message="$(gettext_printf "Loading ACRN hypervisor %s ..." ${acrn_version})"
        cat << EOF
	echo	'$(echo "$message" | grub_quote)'
	${acrn_loader}	${rel_acrn_dirname}/${acrn_basename} ${GRUB_CMDLINE_ACRN}
EOF
        add_prelaunched_vms
        echo "}"
    fi
    if [ -z "$boot_device_id" ]; then
        boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
    fi

    # only if we have at least one ACRN capable kernel and a Service VM entry defined
    while [ "x$list" != "x" ] && [ "x${ACRN_SERVICE_VM_ID}" != "x" ] ; do
        linux=$(echo ${list} | tr ' ' '\n' | sed -e 's/\.old$/ 1/; / 1$/! s/$/ 2/' | LC_ALL=C sort -V -r | sed -e 's/ 1$/.old/; s/ 2$//' | head -n 1)
        gettext_printf "Found ACRN linux image: %s\n" "$linux" >&2
        basename=$(basename $linux)
        dirname=$(dirname $linux)
        rel_dirname=$(make_system_path_relative_to_its_root $dirname)
        version=$(echo $basename | sed -e "s,^[^0-9]*-,,g")
        alt_version=$(echo $version | sed -e "s,\.old$,,g")
        linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"

        initrd=
        for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
           "initrd-${version}" "initramfs-${version}.img" \
           "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
           "initrd-${alt_version}" "initramfs-${alt_version}.img" \
           "initramfs-genkernel-${version}" \
           "initramfs-genkernel-${alt_version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}" ; do
            if test -e "${dirname}/${i}" ; then
                initrd="$i"
                break
            fi
        done
        if [ -n "${initrd}" ]; then
            if [ -z "$(eval echo \${ACRN_RAMDISK_MOD_VM${ACRN_SERVICE_VM_ID}})" ]; then
                # Use the found ramdisk despite we don't have a respective module tag
                # in ACRN configuration. This makes the UUID magic work and avoid the
                # problem of duplicated boot menu entries detected by os-prober when
                # using GRUB_DEVICE directly and running grub-mkconfig on such a system.
                gettext_printf "WARNING: Using ${initrd} despite ramdisk module tag of ACRN Service VM is not set.\n" >&2
            else
                gettext_printf "Found ACRN initrd image: %s\n" "${dirname}/${initrd}" >&2
            fi
        else
            gettext_printf "ACRN: No initrd image for ${ACRN_BOARD}:${ACRN_SCENARIO}\n" >&2
            # "UUID=" magic is parsed by initrds.  Since there's no initrd, it can't work here.
            linux_root_device_thisversion=${GRUB_DEVICE}
        fi

        linux_entry "${OS}" "${version}" "${acrn_version}" \
            "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" "${GRUB_CMDLINE_ACRN}"

        if [ -z "$boot_device_id" ]; then
            boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
        fi

        list=$(echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' ')
    done
    acrn_list=$(echo $acrn_list | tr ' ' '\n' | fgrep -vx "$current_acrn" | tr '\n' ' ')
done

echo ""
