Skip to content

Commit

Permalink
Add EFI_RUNTIME_SERVICES and EFI_TIME (#15)
Browse files Browse the repository at this point in the history
It is now possible to get the current date and time with unsafe code using the GetTime function.
  • Loading branch information
JoshuaWierenga committed Feb 10, 2021
1 parent d936da7 commit 8325720
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
4 changes: 3 additions & 1 deletion EFISharp.CoreLib/EFISharp.CoreLib.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
Expand Down Expand Up @@ -33,7 +33,9 @@
</Target>

<ItemGroup>
<Compile Include="EfiSharp\EFI_RUNTIME_SERVICES.cs" />
<Compile Include="EfiSharp\EFI_BOOT_SERVICES.cs" />
<Compile Include="EfiSharp\EFI_TIME.cs" />
<Compile Include="EfiSharp\EFI_GUID.cs" />
<Compile Include="EfiSharp\EFI_EVENT.cs" />
<Compile Include="EfiSharp\EFI_HANDLE.cs" />
Expand Down
34 changes: 34 additions & 0 deletions EFISharp.CoreLib/EFISharp/EFI_RUNTIME_SERVICES.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Runtime.InteropServices;

namespace EfiSharp
{
[StructLayout(LayoutKind.Sequential)]
public readonly unsafe struct EFI_RUNTIME_SERVICES
{
private readonly EFI_TABLE_HEADER Hdr;

// Time Services
private readonly delegate*<EFI_TIME*, void*, EFI_STATUS> _getTime;

//TODO Add EFI_TIME_CAPABILITIES and support EFI_RT_PROPERTIES_TABLE configuration table, then update the summary and returns to indicate that.
/// <summary>
/// <para>This function returns a time that was valid sometime during the call to the function.</para>
/// <para>This function should take approximately the same amount of time to read the time each time it is called.</para>
/// </summary>
/// <param name="time">Out variable to receive a snapshot of the current time.</param>
/// <returns>
/// <para><see cref="EFI_STATUS.EFI_SUCCESS"/> if the operation completed successfully.</para>
/// <para><see cref="EFI_STATUS.EFI_INVALID_PARAMETER"/> if <paramref name="time"/> is null.</para>
/// <para><see cref="EFI_STATUS.EFI_DEVICE_ERROR"/> if the time could not be retrieved due to a hardware error.</para>
/// <para><see cref="EFI_STATUS.EFI_UNSUPPORTED"/> if this call is not supported by this platform at the time the call is made.</para>
/// </returns>
public EFI_STATUS GetTime(out EFI_TIME time)
{
fixed (EFI_TIME* pTime = &time)
{
return _getTime(pTime, null);
}
}
}
}
2 changes: 1 addition & 1 deletion EFISharp.CoreLib/EFISharp/EFI_SYSTEM_TABLE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public readonly unsafe struct EFI_SYSTEM_TABLE
internal readonly EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL* ConOut;
private readonly void* _pad2;
private readonly void* _pad3;
private readonly void* _pad4;
public readonly EFI_RUNTIME_SERVICES* RuntimeServices;
public readonly EFI_BOOT_SERVICES* BootServices;
}
}
18 changes: 18 additions & 0 deletions EFISharp.CoreLib/EFISharp/EFI_TIME.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Runtime.InteropServices;

namespace EfiSharp
{

[StructLayout(LayoutKind.Sequential)]
public readonly struct EFI_TIME
{
public readonly ushort Year;
public readonly byte Month;
public readonly byte Day;
public readonly byte Hour;
public readonly byte Minute;
public readonly byte Second;
public readonly byte Pad1;
public readonly uint Nanosecond;
}
}
22 changes: 22 additions & 0 deletions EfiSharp/Class1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static void ConsoleTest()
{
ConsolePrimitiveTests();
ConsoleFloatingPointTests();
ConsoleDateTimeTest();
ConsoleInputTest();
ConsoleInputExTest();
ConsoleKeyTest();
Expand Down Expand Up @@ -243,6 +244,27 @@ private static void ConsoleFloatingPointTests()
Console.WriteLine(-14141.000000000001d);
}

private static unsafe void ConsoleDateTimeTest()
{
UefiApplication.SystemTable->RuntimeServices->GetTime(out EFI_TIME currentTime);
Console.WriteLine("Date and Time Test:");
Console.Write("Date: ");
//ISO 8601 format
Console.Write(currentTime.Year);
Console.Write(currentTime.Month < 10 ? "-0" : "-");
Console.Write(currentTime.Month);
Console.Write(currentTime.Day < 10 ? "-0" : "-");
Console.Write(currentTime.Day);

Console.Write(", Time: ");
if (currentTime.Hour < 10) Console.Write('0');
Console.Write(currentTime.Hour);
Console.Write(currentTime.Minute < 10 ? ":0" : ":");
Console.Write(currentTime.Minute);
Console.Write(currentTime.Second < 10 ? ":0" : ":");
Console.WriteLine(currentTime.Second);
}

private static void ConsoleInputTest()
{
Console.Write("\r\nReadLine Input Test: ");
Expand Down

0 comments on commit 8325720

Please sign in to comment.