-
Notifications
You must be signed in to change notification settings - Fork 0
/
UefiMain.c
133 lines (116 loc) · 3.04 KB
/
UefiMain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <Uefi.h>
#include <Library/UefiLib.h>
// To call gBS (Global Boot Services)
#include <Library/UefiBootServicesTableLib.h>
#include "time.h"
#include "screenIO.h"
#include "string.h"
int GetCommandVerb(IN CHAR16 *source, OUT CHAR16 *verb, int searchLen)
{
for (int i = 0; i < searchLen; i++)
{
if (source[i] == L' ')
{
*(verb + i) = 0;
return 1;
}
*(verb + i) = source[i];
}
return 0;
}
// This return unix time too
unsigned long getTime(int printToo)
{
EFI_TIME time;
EFI_STATUS status = gST->RuntimeServices->GetTime(&time, NULL);
if (status != EFI_SUCCESS)
{
Print(L"Error getting time\n");
return 0;
}
if (printToo)
{
Print(L"%d-%d-%d %d:%d:%d\n", time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second);
}
return (unsigned long)getUnixTime(&time);
}
void HandleVerb(IN CHAR16 *verb, IN CHAR16 *args)
{
if (strcmp(verb, L"exit"))
{
gBS->Exit(gImageHandle, 0, 0, NULL);
}
else if (strcmp(verb, L"reset"))
{
Print(L"Ok.");
gST->RuntimeServices->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
}
else if (strcmp(verb, L"reinitialize"))
{
Print(L"Ok.");
gST->RuntimeServices->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, NULL);
}
else if (strcmp(verb, L"shutdown"))
{
Print(L"Ok.");
gST->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
}
else if (strcmp(verb, L"time"))
{
unsigned int unixtime = getTime(1);
CHAR16 unixtimestr[30];
inttostr((int)unixtime, unixtimestr);
Print(unixtimestr);
}
else if (strcmp(verb, L"timeset"))
{
EFI_TIME time;
CHAR16 strtmp[9];
gST->RuntimeServices->GetTime(&time, NULL);
Print(L"YEAR [1900-9999]: ");
readLine(strtmp, 9);
time.Year = strtoint(strtmp);
Print(L"MONTH [1-12]: ");
readLine(strtmp, 9);
time.Month = strtoint(strtmp);
Print(L"DAY [1-31]: ");
readLine(strtmp, 9);
time.Day = strtoint(strtmp);
Print(L"HOUR [0-23]: ");
readLine(strtmp, 9);
time.Hour = strtoint(strtmp);
Print(L"MINUTE [0-59]: ");
readLine(strtmp, 9);
time.Minute = strtoint(strtmp);
Print(L"SECOND [0-59]: ");
readLine(strtmp, 9);
time.Second = strtoint(strtmp);
gST->RuntimeServices->SetTime(&time);
getTime(1);
}
else
{
Print(L"Unknown command");
}
}
EFI_STATUS
EFIAPI
UefiMain(
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable)
{
SystemTable->BootServices->SetWatchdogTimer(0, 0, 0, NULL);
SystemTable->ConOut->ClearScreen(SystemTable->ConOut);
Print(L"MinSys\n");
while (1)
{
CHAR16 input[255];
CHAR16 verb[16];
Print(L"> ");
readLine(input, 255);
GetCommandVerb(input, verb, 16);
HandleVerb(verb, input);
Print(L"\n");
}
return EFI_SUCCESS;
}