#!/bin/sh

ARCH=x86_64-unknown-uefi
PROFILE=debug
O=target/${ARCH}/${PROFILE}

BIOS=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd
IMAGE=${O}/image.fat32

set -e

mkdir -p ${O}/image

cargo build

dd if=/dev/zero of=${IMAGE} bs=1M count=64
mkfs.vfat -F32 ${IMAGE}
mcopy -i ${IMAGE} ${O}/yboot.efi ::yboot.efi

if [ "${KERNEL_FILE}" != "" ]; then
    mcopy -i ${IMAGE} ${KERNEL_FILE} ::kernel.elf
fi

qemu-system-x86_64 \
    -s \
    -serial mon:stdio \
    -m 256 \
    -drive format=raw,file=${BIOS},readonly=on,if=pflash \
    -drive format=raw,file=${IMAGE} \
    -net none \
    -enable-kvm \
    -M q35 \
    -cpu host
