# vim: filetype=sh

BIOSBOOT_PARTITION_SIZE_KB=1024
FAT_OVERHEAD_PERCENT=10

# partitioning and formatting
# ---------------------------

partition_image()
{
    device=$1
    efi_partition_size_kb=$(get_efi_partition_size_kb)
    quiet sgdisk \
            -n 1:0:+${efi_partition_size_kb}K -t 1:ef00 \
            -n 2:0:+${BIOSBOOT_PARTITION_SIZE_KB}K -t 2:ef02 \
            -n 3:0:0 -t 3:8e00 $device
}

format_partition()
{
    partnum=$1
    partdevice=$2

    if [ $partnum -eq 1 ]
    then
        # format the efi partition
        quiet mkfs.vfat -n DBSTCK_EFI $partdevice
        return 0
    else
        # partition 2 needs no filesystem
        # partition 3 is the root fs => managed by debootstick core
        return 1
    fi
}

get_efi_partition_size_kb()
{
    set -- $(uefi_binary_image_info)
    uefi_binary_name=$2
    efi_image_size_bytes=$(stat -c "%s" $uefi_binary_name)
    efi_partition_size_kb=$(apply_overhead_percent \
                $((efi_image_size_bytes/1024)) $FAT_OVERHEAD_PERCENT)
    echo $efi_partition_size_kb
}
