/*
    * Boot.cpp
    * Kernel-side acquisition and validation of the Montauk Boot Contract
    * Copyright (c) 2026 Daniel Hammer
*/

#include "Boot.hpp"
#include "BootProtocol.hpp"

namespace montauk::boot {

    // The contract value lives in kernel .bss so it survives the bootloader
    // reclaiming its own structures.  Note: the *arrays* it points at do not
    // (see the lifetime model in BootInfo.hpp) -- consumers must read them
    // during early boot.
    static BootInfo g_info{};

    const BootInfo& Info() {
        return g_info;
    }

    bool Initialize() {
        // Translate the active bootloader's native handoff into the contract.
        if (!Acquire(g_info)) {
            // The environment is unusable, or the protocol is unsupported.
            // We cannot log (no console yet) -- the caller halts.
            return false;
        }

        // The adapter must speak the same contract revision we compiled
        // against.  A mismatch means struct layouts disagree; continuing
        // would read garbage.  We cannot trust the framebuffer either, so
        // signal the caller to halt.
        if (g_info.contractVersion != ContractVersion) {
            return false;
        }

        // A console-capable framebuffer is required to report anything at
        // all, so its absence must also be a silent halt.  Every other
        // required field (e.g. the memory map) is validated by its consumer
        // once the console is up, so those failures are visible.
        if (!g_info.has(FeatureFramebuffer)
         || g_info.framebuffer.address == 0) {
            return false;
        }

        return true;
    }

}
