/*
 * @author  xamidev <xamidev@riseup.net>
 * @brief   Limine requests for boot
 * @description
 * The kernel makes a few requests to the Limine bootloader
 * in order to get precious information about the system.
 * We get a framebuffer, a memory map, the address of the
 * kernel in memory, and the Higher Half Direct Map offset.
 * @license GPL-3.0-only
 */

#include <kernel.h>
#include <limine.h>
#include <stddef.h>

__attribute__((used, section(".limine_requests_start")))
volatile LIMINE_REQUESTS_START_MARKER;

__attribute__((used, section(".limine_requests")))
volatile struct limine_framebuffer_request framebuffer_request = {
	.id = LIMINE_FRAMEBUFFER_REQUEST,
	.revision = 0
};

__attribute__((used, section(".limine_requests")))
volatile struct limine_memmap_request memmap_request = {
	.id = LIMINE_MEMMAP_REQUEST,
	.revision = 0
};

__attribute__((used, section(".limine_requests")))
volatile struct limine_hhdm_request hhdm_request = {
	.id = LIMINE_HHDM_REQUEST,
	.revision = 0
};

__attribute__((used, section(".limine_requests")))
volatile struct limine_kernel_address_request kerneladdr_request = {
	.id = LIMINE_KERNEL_ADDRESS_REQUEST,
	.revision = 0
};

__attribute__((used, section(".limine_requests")))
volatile struct limine_boot_time_request date_request = {
	.id = LIMINE_BOOT_TIME_REQUEST,
	.revision = 0
};

__attribute__((used, section(".limine_requests")))
volatile struct limine_module_request module_request = {
	.id = LIMINE_MODULE_REQUEST,
	.revision = 0
};

__attribute__((used, section(".limine_requests_end")))
volatile LIMINE_REQUESTS_END_MARKER;

void populate_boot_context(struct boot_context* ctx)
{
	// Populate boot context
	// This stays valid only if the BOOTLOADER_RECLAIMABLE regions are preserved
 	ctx->fb = framebuffer_request.response ? framebuffer_request.response->framebuffers[0] : NULL;
	ctx->mmap = memmap_request.response ? memmap_request.response : NULL;
	ctx->hhdm = hhdm_request.response ? hhdm_request.response : NULL;
	ctx->kaddr = kerneladdr_request.response ? kerneladdr_request.response : NULL;
	ctx->bootdate = date_request.response ? date_request.response : NULL;
	ctx->module = module_request.response ? module_request.response : NULL;
}