-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViOS.c
More file actions
387 lines (328 loc) · 11.6 KB
/
ViOS.c
File metadata and controls
387 lines (328 loc) · 11.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/** @file
This sample application bases on HelloWorld PCD setting
to print "UEFI Hello World!" to the UEFI Console.
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "./ViOS64Bit/src/config.h"
#include <Guid/FileInfo.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PcdLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Protocol/LoadedImage.h>
#include <Protocol/SimpleFileSystem.h>
#include <Uefi.h>
typedef struct __attribute__((packed)) E820Entry
{
UINT64 base_addr;
UINT64 length;
UINT32 type;
UINT32 extended_attr;
} E820Entry;
typedef struct __attribute__((packed)) E820Entries
{
UINT64 count;
E820Entry entries[];
} E820Entries;
EFI_HANDLE imageHandle = NULL;
EFI_SYSTEM_TABLE *systemTable = NULL;
EFI_STATUS SetupMemoryMaps()
{
EFI_STATUS status;
UINTN memoryMapSize = 0;
EFI_MEMORY_DESCRIPTOR *memoryMap = NULL;
UINTN mapKey;
UINTN descriptorSize;
UINT32 descriptorVersion;
status = gBS->GetMemoryMap(&memoryMapSize, NULL, &mapKey, &descriptorSize,
&descriptorVersion);
if (status != EFI_BUFFER_TOO_SMALL && EFI_ERROR(status))
{
Print(L"Error retreving initial memory map size: %r\n", status);
return status;
}
// Calculate memory map size
memoryMapSize += descriptorSize * 10;
// Allocate a buffer for the memory map
memoryMap = AllocatePool(memoryMapSize);
if (memoryMap == NULL)
{
Print(L"Error allocating memory for memory map\n");
return EFI_OUT_OF_RESOURCES;
}
status = gBS->GetMemoryMap(&memoryMapSize, memoryMap, &mapKey,
&descriptorSize, &descriptorVersion);
if (EFI_ERROR(status))
{
Print(L"Error getting memory map: %r\n", status);
FreePool(memoryMap);
return status;
}
UINTN descriptorCount = memoryMapSize / descriptorSize;
EFI_MEMORY_DESCRIPTOR *desc = memoryMap;
UINTN totalConventionalDescriptors = 0;
for (UINTN i = 0; i < descriptorCount; ++i)
{
if (desc->Type == EfiConventionalMemory)
{
totalConventionalDescriptors++;
}
desc = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)desc + descriptorSize);
}
EFI_PHYSICAL_ADDRESS MemoryMapLocationE820 =
VIOS_MEMORY_MAP_TOTAL_ENTRIES_LOCATION;
UINTN MemoryMapSizeE820 =
(sizeof(E820Entry) * totalConventionalDescriptors) + sizeof(UINT64);
status = gBS->AllocatePages(AllocateAddress, EfiLoaderData,
EFI_SIZE_TO_PAGES(MemoryMapSizeE820),
&MemoryMapLocationE820);
if (EFI_ERROR(status))
{
Print(L"Error allocating memory for the E820 Entries: %r\n", status);
return status;
}
E820Entries *e820Entries = (E820Entries *)MemoryMapLocationE820;
UINTN ConventionalMemoryIndex = 0;
desc = memoryMap;
for (UINTN i = 0; i < descriptorCount; ++i)
{
if (desc->Type == EfiConventionalMemory)
{
E820Entry *e820Entry = &e820Entries->entries[ConventionalMemoryIndex];
e820Entry->base_addr = desc->PhysicalStart;
e820Entry->length = desc->NumberOfPages * 4096;
e820Entry->type = 1; // Usuable memory
e820Entry->extended_attr = 0;
Print(L"e820Entry=%p\n", e820Entry);
ConventionalMemoryIndex++;
}
desc = (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)desc + descriptorSize);
}
e820Entries->count = totalConventionalDescriptors;
FreePool(memoryMap);
return EFI_SUCCESS;
}
EFI_STATUS ReadFileFromCurrentFilesystem(CHAR16 *FileName, VOID **Buffer_Out,
UINTN *BufferSize_Out)
{
EFI_STATUS Status = 0;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImageProtocol = NULL;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem = NULL;
EFI_FILE_PROTOCOL *Root = NULL;
EFI_FILE_PROTOCOL *File = NULL;
UINTN FileInfoSize = 0;
*Buffer_Out = NULL;
*BufferSize_Out = 0;
Status = gBS->HandleProtocol(imageHandle, &gEfiLoadedImageProtocolGuid,
(VOID **)&LoadedImageProtocol);
if (EFI_ERROR(Status))
{
Print(L"Error accessing LoadedImageProtocol: %r\n", Status);
return Status;
}
Status = gBS->HandleProtocol(LoadedImageProtocol->DeviceHandle,
&gEfiSimpleFileSystemProtocolGuid,
(VOID **)&SimpleFileSystem);
if (EFI_ERROR(Status))
{
Print(L"Error accessing SimpleFIleSystem: %r\n", Status);
return Status;
}
Status = SimpleFileSystem->OpenVolume(SimpleFileSystem, &Root);
if (EFI_ERROR(Status))
{
Print(L"Error opening root directory: %r\n", Status);
return Status;
}
// Open the file in the root directory
Status = Root->Open(Root, &File, FileName, EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(Status))
{
Print(L"Error opening file %s: %r\n", FileName, Status);
return Status;
}
// Retrieve file information to determine file size
FileInfoSize = OFFSET_OF(EFI_FILE_INFO, FileName) + 256 * sizeof(CHAR16);
VOID *FileInfoBuffer = AllocatePool(FileInfoSize);
if (FileInfoBuffer == NULL)
{
Print(L"Error allocating buffer for file info\n");
File->Close(File);
return EFI_OUT_OF_RESOURCES;
}
EFI_FILE_INFO *FileInfo = (EFI_FILE_INFO *)FileInfoBuffer;
Status = File->GetInfo(File, &gEfiFileInfoGuid, &FileInfoSize, FileInfo);
if (EFI_ERROR(Status))
{
Print(L"Error getting file info for %s %r\n", FileName, Status);
FreePool(FileInfoBuffer);
File->Close(File);
return Status;
}
UINTN BufferSize = FileInfo->FileSize;
FreePool(FileInfoBuffer);
FileInfoBuffer = NULL;
// Allocate the memory for the file content
VOID *Buffer = AllocatePool(BufferSize);
if (Buffer == NULL)
{
Print(L"Error allocating buffer for file %s\n", FileName);
File->Close(File);
return EFI_OUT_OF_RESOURCES;
}
Status = File->Read(File, &BufferSize, Buffer);
if (EFI_ERROR(Status))
{
Print(L"Error reading file %s %r", FileName, Status);
FreePool(Buffer);
File->Close(File);
return Status;
}
*Buffer_Out = Buffer;
*BufferSize_Out = BufferSize;
Print(L"Read %d bytes from file %s\n", BufferSize, FileName);
// Close the file
File->Close(File);
return EFI_SUCCESS;
}
EFI_STATUS GetFrameBufferInfo(EFI_GRAPHICS_OUTPUT_PROTOCOL **GraphicsOutput)
{
EFI_STATUS Status;
// Locate the graphics output protocol
Status = gBS->LocateProtocol(&gEfiGraphicsOutputProtocolGuid, NULL,
(VOID **)GraphicsOutput);
if (EFI_ERROR(Status))
{
Print(L"Error: unable to locate GOP: %r", Status);
return Status;
}
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
UINTN SizeOfInfo;
// Get the current mode
Status = (*GraphicsOutput)
->QueryMode(*GraphicsOutput, (*GraphicsOutput)->Mode->Mode,
&SizeOfInfo, &Info);
if (EFI_ERROR(Status))
{
Print(L"Unable to query mode %%r\n", Status);
return Status;
}
Print(L"Framebuffer base address: %p\n",
(*GraphicsOutput)->Mode->FrameBufferBase);
Print(L"Framebuffer size: %lu bytes\n",
(*GraphicsOutput)->Mode->FrameBufferSize);
Print(L"Screen resolution: %u x %u\n", Info->HorizontalResolution,
Info->VerticalResolution);
Print(L"Pixels per scan line: %u\n", Info->PixelsPerScanLine);
return EFI_SUCCESS;
}
/*
The user Entry Point for Application. The user code starts with this function
as the real entry point for the application.
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The entry point is executed successfully.
@retval other Some error occurs when executing this entry point.
**/
EFI_STATUS
EFIAPI
UefiMain(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
{
imageHandle = ImageHandle;
systemTable = SystemTable;
EFI_STATUS Status = 0;
Print(L"ViOS OS UEFI bootloader.");
// Setup and load E820 Entries
SetupMemoryMaps();
VOID *KernelBuffer = NULL;
UINTN KernelBufferSize = 0;
Status = ReadFileFromCurrentFilesystem(L"kernel.bin", &KernelBuffer,
&KernelBufferSize);
if (EFI_ERROR(Status))
{
Print(L"Error reading kernel: %r\n", Status);
return Status;
}
Print(L"Kernel file loaded successfully at: %p\n", KernelBuffer);
// The kernel must be mapped at 0x100000
EFI_PHYSICAL_ADDRESS KernelBase = VIOS_KERNEL_LOCATION;
Status = gBS->AllocatePages(AllocateAddress, EfiLoaderData,
EFI_SIZE_TO_PAGES(KernelBufferSize), &KernelBase);
if (EFI_ERROR(Status))
{
Print(L"Error allocating memory for kernel %r\n", Status);
return Status;
}
// Copy the kernel to the allocated memory
CopyMem((VOID *)KernelBase, KernelBuffer, KernelBufferSize);
Print(L"Kernel copied to memory at: %p\n", KernelBase);
EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
// Lets get the frame buffers
Status = GetFrameBufferInfo(&GraphicsOutput);
if (EFI_ERROR(Status))
{
Print(L"Error getting frame buffer info: %r\n", Status);
return Status;
}
// Draw the entire screen green
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *FrameBuffer =
(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)GraphicsOutput->Mode->FrameBufferBase;
// There can be padding so we must use pixels per scan line
UINTN PixelsPerScanLine = GraphicsOutput->Mode->Info->PixelsPerScanLine;
UINTN HoriziontalResolution =
GraphicsOutput->Mode->Info->HorizontalResolution;
UINTN VerticalResoltuion = GraphicsOutput->Mode->Info->VerticalResolution;
for (UINTN y = 0; y < VerticalResoltuion; y++)
{
for (UINTN x = 0; x < HoriziontalResolution; x++)
{
FrameBuffer[y * PixelsPerScanLine + x].Red = 0x00;
FrameBuffer[y * PixelsPerScanLine + x].Green = 0x00;
FrameBuffer[y * PixelsPerScanLine + x].Blue = 0x00;
FrameBuffer[y * PixelsPerScanLine + x].Reserved = 0x00;
}
}
// End the UEFI services and jump to the kernel
UINTN memoryMapSize = 0;
EFI_MEMORY_DESCRIPTOR *memoryMap = NULL;
UINTN mapKey;
UINTN descriptorSize;
UINT32 descriptorVersion;
Status = gBS->GetMemoryMap(&memoryMapSize, NULL, &mapKey, &descriptorSize,
&descriptorVersion);
memoryMapSize += descriptorSize * 10;
memoryMap = AllocatePool(memoryMapSize);
if (memoryMap == NULL)
{
return EFI_OUT_OF_RESOURCES;
}
Status = gBS->GetMemoryMap(&memoryMapSize, memoryMap, &mapKey,
&descriptorSize, &descriptorVersion);
if (EFI_ERROR(Status))
{
FreePool(memoryMap);
return Status;
}
Status = gBS->ExitBootServices(ImageHandle, mapKey);
if (EFI_ERROR(Status))
{
FreePool(memoryMap);
return Status;
}
__asm__ __volatile__(
"movq %0, %%rdi\n\t" // Frame buffer base
"movq %1, %%rdx\n\t" // horiziontal resolution
"movq %2, %%rcx\n\t" // vertical resolution
"movq %3, %%rsi\n\t" // pixels per scan line
:
: "r"((UINT64)FrameBuffer), "r"((UINT64)HoriziontalResolution),
"r"((UINT64)VerticalResoltuion), "r"((UINT64)PixelsPerScanLine)
: "rdi", "rsi", "rdx", "rcx");
__asm__("jmp *%0" : : "r"(KernelBase));
// Will never get run because of the jump above.
return EFI_SUCCESS;
}