-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommands.c
More file actions
194 lines (150 loc) · 5.63 KB
/
Copy pathcommands.c
File metadata and controls
194 lines (150 loc) · 5.63 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "commands.h"
#include "utils.h"
#include "filesystem.h"
void print_header(EFI_SYSTEM_TABLE *SystemTable) {
SetColor(SystemTable, COLOR_CYAN);
Print(L"\n HOBBYOS V0.1 UEFI - (X64 Bare Metal Version)\n");
Print(L" (c) Portal Micilini - All Rights Reserved.\n\n");
SetColor(SystemTable, COLOR_RESET);
}
void cmd_ver(EFI_SYSTEM_TABLE *SystemTable) {
SetColor(SystemTable, COLOR_CYAN);
Print(L"\n HobbyOS v0.1 UEFI Edition - (X64 Bare Metal Version)\n");
Print(L" Development by: Micilini\n");
Print(L" Architecture: x64 Bare Metal\n\n");
SetColor(SystemTable, COLOR_RESET);
}
void cmd_shutdown(EFI_SYSTEM_TABLE *SystemTable) {
SetColor(SystemTable, COLOR_RED);
Print(L"\n Shutdown the system...\n");
uefi_call_wrapper(SystemTable->BootServices->Stall, 1, 1000000);
uefi_call_wrapper((void*)SystemTable->RuntimeServices->ResetSystem, 4, EfiResetShutdown, EFI_SUCCESS, 0, NULL);
}
void cmd_restart(EFI_SYSTEM_TABLE *SystemTable) {
SetColor(SystemTable, COLOR_YELLOW);
Print(L"\n Restarting...\n");
uefi_call_wrapper(SystemTable->BootServices->Stall, 1, 1000000);
uefi_call_wrapper((void*)SystemTable->RuntimeServices->ResetSystem, 4, EfiResetWarm, EFI_SUCCESS, 0, NULL);
}
void cmd_date(EFI_SYSTEM_TABLE *SystemTable) {
EFI_TIME Time;
EFI_STATUS Status;
Status = uefi_call_wrapper(SystemTable->RuntimeServices->GetTime, 2, &Time, NULL);
if (EFI_ERROR(Status)) {
Print(L"Error when reading clock (RTC).\n");
return;
}
SetColor(SystemTable, COLOR_CYAN);
Print(L" Date: %02d/%02d/%04d Hour: %02d:%02d:%02d\n",
Time.Day, Time.Month, Time.Year,
Time.Hour, Time.Minute, Time.Second);
SetColor(SystemTable, COLOR_WHITE);
}
void cmd_mem(EFI_SYSTEM_TABLE *SystemTable) {
EFI_STATUS Status;
UINTN MemoryMapSize = 0;
EFI_MEMORY_DESCRIPTOR *MemoryMap = NULL;
UINTN MapKey;
UINTN DescriptorSize;
UINT32 DescriptorVersion;
Status = uefi_call_wrapper(SystemTable->BootServices->GetMemoryMap, 5,
&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);
if (Status != EFI_BUFFER_TOO_SMALL) {
Print(L"Error querying RAM..\n");
return;
}
MemoryMapSize += 2 * DescriptorSize;
Status = uefi_call_wrapper(SystemTable->BootServices->AllocatePool, 3,
EfiLoaderData, MemoryMapSize, (void**)&MemoryMap);
if (EFI_ERROR(Status)) {
Print(L"Allocation error for memory map.\n");
return;
}
Status = uefi_call_wrapper(SystemTable->BootServices->GetMemoryMap, 5,
&MemoryMapSize, MemoryMap, &MapKey, &DescriptorSize, &DescriptorVersion);
if (EFI_ERROR(Status)) {
Print(L"Error retrieving map.\n");
uefi_call_wrapper(SystemTable->BootServices->FreePool, 1, MemoryMap);
return;
}
UINT64 TotalRam = 0;
UINT64 FreeRam = 0;
UINT8 *Ptr = (UINT8*)MemoryMap;
UINT8 *End = Ptr + MemoryMapSize;
while (Ptr < End) {
EFI_MEMORY_DESCRIPTOR *Desc = (EFI_MEMORY_DESCRIPTOR*)Ptr;
UINT64 SizeInBytes = Desc->NumberOfPages * 4096;
TotalRam += SizeInBytes;
if (Desc->Type == EfiConventionalMemory) {
FreeRam += SizeInBytes;
}
Ptr += DescriptorSize;
}
uefi_call_wrapper(SystemTable->BootServices->FreePool, 1, MemoryMap);
SetColor(SystemTable, COLOR_YELLOW);
Print(L" Total RAM: %ld MB\n", TotalRam / (1024 * 1024));
SetColor(SystemTable, COLOR_GREEN);
Print(L" Free RAM: %ld MB\n", FreeRam / (1024 * 1024));
SetColor(SystemTable, COLOR_WHITE);
}
void cmd_hexdump(EFI_SYSTEM_TABLE *SystemTable, CHAR16* FileName) {
if (FileName[0] == 0) {
Print(L"Usage: hexdump <file>\n");
return;
}
EFI_FILE_PROTOCOL *Root = fs_get_root();
if (!Root) {
Print(L"Error: File system not initialized.\n");
return;
}
CHAR16 FullPath[256];
ResolvePath(FileName, FullPath);
EFI_FILE_PROTOCOL *File;
EFI_STATUS Status = uefi_call_wrapper(Root->Open, 5, Root, &File, FullPath, EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(Status)) {
Print(L"File not found: %s\n", FullPath);
return;
}
UINT8 Buffer[16];
UINTN ReadSize = sizeof(Buffer);
UINTN Offset = 0;
Print(L"\nOFFSET 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ASCII\n");
Print(L"-------- ----------------------------------------------- ----------------\n");
BOOLEAN Stop = FALSE;
while (!Stop) {
ReadSize = sizeof(Buffer);
Status = uefi_call_wrapper(File->Read, 3, File, &ReadSize, Buffer);
if (EFI_ERROR(Status) || ReadSize == 0) break;
SetColor(SystemTable, COLOR_CYAN);
Print(L"%08x ", Offset);
SetColor(SystemTable, COLOR_WHITE);
for (UINTN i = 0; i < 16; i++) {
if (i < ReadSize) {
if (Buffer[i] < 0x10) Print(L"0");
Print(L"%x ", Buffer[i]);
} else {
Print(L" ");
}
}
Print(L" ");
SetColor(SystemTable, COLOR_YELLOW);
for (UINTN i = 0; i < ReadSize; i++) {
CHAR16 c = (CHAR16)Buffer[i];
if (c >= 32 && c <= 126) {
CHAR16 Str[2] = {c, 0};
Print(Str);
} else {
Print(L".");
}
}
SetColor(SystemTable, COLOR_WHITE);
Print(L"\n");
Offset += ReadSize;
if (Offset > 2048) {
Print(L"\n... (Very large file, showing only the first 2KB.) ...\n");
Stop = TRUE;
}
}
Print(L"\n");
uefi_call_wrapper(File->Close, 1, File);
}