Skip to content

Commit

Permalink
Update ACPI.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Epiczhul authored May 25, 2024
1 parent f33c25e commit 7cb4a9b
Showing 1 changed file with 174 additions and 41 deletions.
215 changes: 174 additions & 41 deletions source/Cosmos.Core/ACPI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.InteropServices;

namespace Cosmos.Core
Expand All @@ -12,67 +12,156 @@ public unsafe class ACPI
/// RSD table struct.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct RSDPtr
public unsafe struct RSDPtr
{
/// <summary>
/// Signature.
/// </summary>
public fixed byte Signature[8];
/// <summary>
/// CheckSum
/// </summary>
public byte CheckSum;
/// <summary>
/// OemID
/// </summary>
public fixed byte OemID[6];
/// <summary>
/// Revision
/// </summary>
public byte Revision;
public uint RsdtAddress;
/// <summary>
/// RSDT Address
/// </summary>
public int RsdtAddress;
};

// New Port I/O
/// <summary>
/// RSDT table struct.
/// IO port.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct RSDT
{
public fixed byte Signature[4];
public uint Length;
public byte Revision;
public byte Checksum;
public fixed byte OemID[6];
public fixed byte OemTableID[8];
public uint OemRevision;
public uint CreatorID;
public uint CreatorRevision;
public fixed uint Entry[1]; // Placeholder for the start of the array of pointers to other tables
}

// New Port I/O
private static ushort smiIO, pm1aIO, pm1bIO;

// ACPI variables
/// <summary>
/// SMI CMD.
/// </summary>
private static int* SMI_CMD;
/// <summary>
/// ACPI ENABLE.
/// </summary>
private static byte ACPI_ENABLE;
/// <summary>
/// ACPI DISABLE.
/// </summary>
private static byte ACPI_DISABLE;
/// <summary>
/// PM1a CNT
/// </summary>
private static int* PM1a_CNT;
/// <summary>
/// PM1b CNT
/// </summary>
private static int* PM1b_CNT;
/// <summary>
/// SLP TYPa
/// </summary>
private static short SLP_TYPa;
/// <summary>
/// SLP TYPb
/// </summary>
private static short SLP_TYPb;
/// <summary>
/// SLP EN.
/// </summary>
private static short SLP_EN;
/// <summary>
/// PM1 CNT LEN1
/// </summary>
private static byte PM1_CNT_LEN;

private static byte* Facp = null;
/// <summary>
/// Check ACPI header.
/// </summary>
/// <param name="ptr"></param>
/// <param name="sig"></param>
/// <returns></returns>
static int acpiCheckHeader(byte* ptr, string sig)
{
return Compare(sig, ptr);
}

/// <summary>
/// FACP.
/// </summary>
private static byte* Facp = null;
/// <summary>
/// FACP struct.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct FACP
{
/// <summary>
/// Signature.
/// </summary>
public fixed byte Signature[4];
/// <summary>
/// Length.
/// </summary>
public int Length;

/// <summary>
/// Unused.
/// </summary>
public fixed byte unneded1[40 - 8];
/// <summary>
/// DSDT.
/// </summary>
public int* DSDT;
/// <summary>
/// Unused.
/// </summary>
public fixed byte unneded2[48 - 44];
/// <summary>
/// SMI CMD.
/// </summary>
public int* SMI_CMD;
/// <summary>
/// ACPI ENABLE.
/// </summary>
public byte ACPI_ENABLE;
/// <summary>
/// ACPI DISABLE.
/// </summary>
public byte ACPI_DISABLE;
/// <summary>
/// Unused.
/// </summary>
public fixed byte unneded3[64 - 54];
/// <summary>
/// PM1a CNT BLK.
/// </summary>
public int* PM1a_CNT_BLK;
/// <summary>
/// PM1b CNT BLK.
/// </summary>
public int* PM1b_CNT_BLK;
/// <summary>
/// Unused.
/// </summary>
public fixed byte unneded4[89 - 72];
/// <summary>
/// PM1 CNT LEN.
/// </summary>
public byte PM1_CNT_LEN;
};

/// <summary>
/// Compare string to byte array.
/// </summary>
/// <param name="c1">String.</param>
/// <param name="c2">Pointer to the head of the byte array.</param>
/// <returns>0 - identical, -1 different.</returns>
static int Compare(string c1, byte* c2)
{
for (int i = 0; i < c1.Length; i++)
Expand All @@ -82,6 +171,11 @@ static int Compare(string c1, byte* c2)
return 0;
}

/// <summary>
/// Check RSD checksum.
/// </summary>
/// <param name="address">Address to check.</param>
/// <returns>True if RSDT table checksum is good.</returns>
static bool Check_RSD(uint address)
{
byte sum = 0;
Expand All @@ -95,6 +189,11 @@ static bool Check_RSD(uint address)
return sum == 0;
}

/// <summary>
/// Start the ACPI.
/// </summary>
/// <param name="initialize">Initialize the ACPI. (default = true)</param>
/// <param name="enable">Enable the ACPI. (default = true)</param>
public static void Start(bool initialize = true, bool enable = true)
{
if (initialize)
Expand All @@ -108,6 +207,10 @@ public static void Start(bool initialize = true, bool enable = true)
}
}

/// <summary>
/// Shutdown the ACPI.
/// </summary>
/// <exception cref="System.IO.IOException">Thrown on IO error.</exception>
public static void Shutdown()
{
Console.Clear();
Expand All @@ -126,11 +229,20 @@ public static void Shutdown()
CPU.Halt();
}

/// <summary>
/// Reboot ACPI.
/// Not implemented.
/// </summary>
/// <exception cref="NotImplementedException">Thrown always.</exception>
public static void Reboot()
{
throw new NotImplementedException("ACPI Reset not implemented yet.");
throw new NotImplementedException("ACPI Reset not implemented yet."); //TODO
}

/// <summary>
/// Initialize the ACPI.
/// </summary>
/// <returns>true on success, false on failure.</returns>
private static bool Init()
{
byte* ptr = (byte*)RSDPAddress();
Expand Down Expand Up @@ -227,16 +339,26 @@ private static bool Init()
return false;
}

/// <summary>
/// Enable ACPI.
/// </summary>
public static void Enable()
{
smiIO = ACPI_ENABLE;
}

/// <summary>
/// Disable ACPI.
/// </summary>
public static void Disable()
{
smiIO = ACPI_DISABLE;
}

/// <summary>
/// Get the RSDP address.
/// </summary>
/// <returns>uint value.</returns>
private static unsafe uint RSDPAddress()
{
for (uint addr = 0xE0000; addr < 0x100000; addr += 4)
Expand Down Expand Up @@ -264,6 +386,11 @@ private static unsafe uint RSDPAddress()
return 0;
}

/// <summary>
/// Check RSDT table
/// </summary>
/// <param name="ptr">A pointer to the RSDT</param>
/// <returns>RSDT table address</returns>
private static uint* acpiCheckRSDPtr(uint* ptr)
{
string sig = "RSD PTR ";
Expand Down Expand Up @@ -297,6 +424,18 @@ private static unsafe uint RSDPAddress()
return null;
}

/// <summary>
/// Get data from the FACP table.
/// </summary>
/// <param name="number">Index number of the data to get.
/// <list type="bullet">
/// <item>0 - ACPI ENABLE</item>
/// <item>1 - ACPI DISABLE</item>
/// <item>2 - PM1 CNT LEN</item>
/// <item>other - 0</item>
/// </list>
/// </param>
/// <returns>byte value.</returns>
private static byte facpbget(int number)
{
switch (number)
Expand All @@ -312,6 +451,19 @@ private static byte facpbget(int number)
}
}

/// <summary>
/// Get pointer to the data on the FACP.
/// </summary>
/// <param name="number">Index number of the data to get.
/// <list type="bullet">
/// <item>0 - DSDT</item>
/// <item>1 - SMI CMD</item>
/// <item>2 - PM1a</item>
/// <item>3 - PM1b</item>
/// <item>other - null</item>
/// </list>
/// </param>
/// <returns>int pointer.</returns>
private static int* facpget(int number)
{
switch (number)
Expand All @@ -328,24 +480,5 @@ private static byte facpbget(int number)
return null;
}
}

public static RSDT* FindRSDT()
{
uint rsdpAddress = RSDPAddress();
if (rsdpAddress == 0)
{
return null;
}

RSDPtr* rsdp = (RSDPtr*)rsdpAddress;
uint rsdtAddress = rsdp->RsdtAddress;

if (rsdtAddress == 0)
{
return null;
}

return (RSDT*)rsdtAddress;
}
}
}

0 comments on commit 7cb4a9b

Please sign in to comment.