XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Runtime.InteropServices;

namespace XFESystem.Kernel;

/// <summary>Small, allocation-free firmware-framebuffer diagnostics until desktop ownership.</summary>
internal static class NativeBootProgress
{
    private static bool visible;
    private static string stage = "A00";
    internal static int StageCount { get; private set; }
    internal static bool IsVisible => visible;

    [DllImport("*", EntryPoint = "XfeBootConsoleSetEnabled")]
    [SuppressGCTransition]
    private static extern void SetEnabled(int enabled);

    [DllImport("*", EntryPoint = "XfeBootConsoleWriteChar")]
    [SuppressGCTransition]
    private static extern void WriteChar(int character);

    [DllImport("*", EntryPoint = "XfeRuntimeSerialAbsentCount")]
    [SuppressGCTransition]
    internal static extern ulong SerialAbsentCount();
    [DllImport("*", EntryPoint = "XfeRuntimeSerialTimeoutCount")]
    [SuppressGCTransition]
    internal static extern ulong SerialTimeoutCount();
    [DllImport("*", EntryPoint = "XfeRuntimeSerialDroppedCount")]
    [SuppressGCTransition]
    internal static extern ulong SerialDroppedCount();

    [DllImport("*", EntryPoint = "XfeRuntimePhysicalCpuCount")]
    [SuppressGCTransition]
    internal static extern uint PhysicalCpuCount();
    [DllImport("*", EntryPoint = "XfeRuntimeSchedulerCpuCount")]
    [SuppressGCTransition]
    internal static extern uint SchedulerCpuCount();

    internal static void Begin()
    {
        SetEnabled(1);
        visible = true;
        StageCount = 0;
        Write("\nXFESystem startup diagnostics\n");
        Stage("A00", "Kernel lifecycle entered");
    }

    internal static void Resume(string code, string description)
    {
        // The base lifecycle disables its early mirror after OnBoot.
        SetEnabled(1);
        visible = true;
        Stage(code, description);
    }

    internal static void Stage(string code, string description)
    {
        stage = code;
        StageCount++;
        Write("[XFE "); Write(code); Write("] "); Write(description); Write("\n");
    }

    internal static void Failed(string reason)
    {
        if (!visible) { SetEnabled(1); visible = true; }
        Write("\n[XFE BOOT FAILED] Stage "); Write(stage); Write("\n");
        Write(reason); Write("\nPlease photograph this screen. No disk install was performed.\n");
    }

    internal static void DesktopOwnsDisplay()
    {
        // Disable the early mirror before the first desktop presentation so
        // runtime logging can never paint over a completed desktop frame.
        SetEnabled(0);
        visible = false;
    }

    private static void Write(string text)
    {
        if (!visible) return;
        foreach (char character in text) WriteChar(character);
    }
}