/*
 * Copyright (c) 2026, Chloe M.
 * Provided under the BSD-3 clause.
 *
 * Description:  Where your machine gets turned on~
 * Author:       Chloe M.
 */

#include <ex/trace.h>
#include <ex/pbi.h>
#include <ex/cmdline.h>
#include <ke/stos.h>
#include <ke/bpal.h>
#include <hal/serial.h>
#include <hal/kpcr.h>
#include <hal/board.h>
#include <ob/object.h>
#include <drivers/bootvid/fbio.h>
#include <drivers/acpi/acpi.h>
#include <mm/pmm.h>
#include <mm/vmm.h>
#include <stdef.h>

#define DTRACE(Fmt, ...) \
    TRACE("[ INIT ]: " Fmt, ##__VA_ARGS__)

/* Globals */
static KPCR BootstrapCore;

/*
 * Display a boot banner
 */
static VOID
BootBanner(VOID)
{
    TRACE("-- SystemPaw3 ~ %s --\n", ST_VERSION);
    DTRACE("booting SystemPaw3 !! <3\n");
}

VOID
KiKernelEntry(VOID)
{
    /* Initialize the serial driver */
    HalSerialInit();

    /* Write the boot banner */
    BootBanner();

    /* Initialize BPAL */
    KeBpalInit();

    /* Initialize boot video */
    BootVidInit();

    /* Initialize the command line parser */
    ExCmdLineInit();

    /* Phase 1 initialization the bootstrap core */
    HalKpcrP1Init(&BootstrapCore);

    /* Initialize the pre-boot image manager */
    ExPbiInit();

    /*
     * If the console is not enabled then we should show a
     * splash rather thann a black screen.
     */
    if (!BootVidConsEn()) {
        BootVidSplash();
    }

    /* Initialize physical memory */
    MmInitPmm();

    /* Initialize virtual memory */
    MmInitVmm();

    /* Initialize ACPI */
    AcpiInit();

    /* Phase 2 initialization of the bootstrap core */
    HalKpcrP2Init(&BootstrapCore);

    /* Initialize the object manager */
    ObInitManager();

    /* Initialize the board */
    HalBoardInit();

    /* Phase 3 initialization of the bootstrap core */
    HalKpcrP3Init(&BootstrapCore);
}
