/*
 * @author  xamidev <xamidev@riseup.net>
 * @brief   Miscellaneous debug features
 * @license GPL-3.0-only
 */

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

extern struct boot_context boot_ctx;

/*
 * memmap_display - displays a memory map
 * @response: Limine memory map response
 *
 * Displays the memory map we get from Limine
 * to see different regions, their sizes, and
 * how the memory is laid out at handoff.
 */
void memmap_display(struct limine_memmap_response* response)
{
	DEBUG("Got memory map from Limine: revision %u, %u entries", response->revision, response->entry_count);

	for (size_t i=0; i<response->entry_count; i++) {
		struct limine_memmap_entry* entry = response->entries[i];
		char type[32] = {0};
		switch(entry->type) {
			case LIMINE_MEMMAP_USABLE:
				strcpy(type, "USABLE");
				break;
			case LIMINE_MEMMAP_RESERVED:
				strcpy(type, "RESERVED");
				break;
			case LIMINE_MEMMAP_ACPI_RECLAIMABLE:
				strcpy(type, "ACPI_RECLAIMABLE");
				break;
			case LIMINE_MEMMAP_ACPI_NVS:
				strcpy(type, "ACPI_NVS");
				break;
			case LIMINE_MEMMAP_BAD_MEMORY:
				strcpy(type, "BAD_MEMORY");
				break;
			case LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE:
				strcpy(type, "BOOTLOADER_RECLAIMABLE");
				break;
			case LIMINE_MEMMAP_KERNEL_AND_MODULES:
				strcpy(type, "KERNEL_AND_MODULES");
				break;
			case LIMINE_MEMMAP_FRAMEBUFFER:
				strcpy(type, "FRAMEBUFFER");
				break;
			default:
				strcpy(type, "UNKNOWN");
				break;
		}
		DEBUG("Entry %02u: [0x%016x | %016u bytes] - %s", i, entry->base, entry->length, type);
	}
}

/*
 * hhdm_display - displays the HHDM offset
 * @hhdm: Limine HHDM offset response
 */
void hhdm_display(struct limine_hhdm_response* hhdm)
{
	DEBUG("Got HHDM revision=%u offset=0x%p", hhdm->revision, hhdm->offset);
}

/*
 * boot_mem_display - displays all memory info
 */
void boot_mem_display()
{
	memmap_display(boot_ctx.mmap);
	hhdm_display(boot_ctx.hhdm);
	DEBUG("Kernel is at phys_base=0x%p virt_base=0x%p", boot_ctx.kaddr->physical_base, boot_ctx.kaddr->virtual_base);
}