#!/usr/bin/ash
# SPDX-License-Identifier: MIT

run_earlyhook() {
    if [ -e /vendorfw ]; then
        msg ":: Asahi: Vendor firmware was loaded by the bootloader"
        return 0
    fi

    if [ ! -e /proc/device-tree/chosen/asahi,efi-system-partition ]; then
        msg ":: Asahi: Missing asahi,efi-system-partition variable, firmware will not be loaded!"
        return 1
    fi

    msg ":: Asahi: Triggering early load of NVMe modules..."
    modprobe apple-mailbox
    modprobe nvme-apple
    modprobe xhci-plat-hcd

    for i in $(seq 0 50); do
        [ -e /sys/bus/platform/drivers/nvme-apple/*.nvme/nvme/nvme*/nvme*n1/ ] && break
        sleep 0.1
    done

    if [ ! -e /sys/bus/platform/drivers/nvme-apple/*.nvme/nvme/nvme*/nvme*n1/ ]; then
        err "Timed out waiting for NVMe device"
        return 1
    fi

    # If the above exists, hopefully the /dev device exists and this will work

    msg ":: Asahi: Unpacking vendor firmware into initramfs..."

    VENDORFW="/run/.system-efi/vendorfw/"

    (
        . /usr/share/asahi-scripts/functions.sh
        mount_sys_esp /run/.system-efi
    )

    if [ ! -e "$VENDORFW/firmware.cpio" ]; then
        msg ":: Asahi: Vendor firmware not available in ESP!"
        umount /run/.system-efi
        return 1
    fi

    ( cd /; cpio -i < "$VENDORFW/firmware.cpio" )
    umount /run/.system-efi

    msg ":: Asahi: Vendor firmware unpacked successfully"
}

run_latehook() {
    [ -e /vendorfw ] || return
    msg ":: Asahi: Copying vendor firmware to tmpfs under root filesystem..."
    mkdir -p /new_root/lib/firmware/vendor
    mount -t tmpfs -o mode=0755 vendorfw /new_root/lib/firmware/vendor
    cp -r /vendorfw/* /vendorfw/.vendorfw.manifest /new_root/lib/firmware/vendor
}
