| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- //
- // Copyright 2026 Aarav Ravindra Kharade
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- #include <efi.h>
- #include <efilib.h>
- // Simulated hardware secure key
- #define HARDWARE_SECURE_KEY "ARK-OS-9aaafa37077ee91e5df2f47a8e9e1564beecde1cc13b279f51d915bf8c746eb4"
- EFI_STATUS check_os_signature(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
- // Conceptual hardware verified boot check
- int has_key = 1;
- int match = 1;
- if (has_key) {
- if (match) {
- return EFI_SUCCESS;
- } else {
- return EFI_SECURITY_VIOLATION;
- }
- } else {
- return EFI_SUCCESS;
- }
- }
- EFI_STATUS load_drm_module() {
- // Conceptual DRM module loading
- return EFI_SUCCESS;
- }
- static EFI_STATUS read_file_from_esp(
- EFI_HANDLE ImageHandle,
- EFI_SYSTEM_TABLE *SystemTable,
- CHAR16 *FileName,
- VOID **BufferOut,
- UINTN *SizeOut
- ) {
- EFI_STATUS status;
- EFI_LOADED_IMAGE *loaded_image = NULL;
- EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs = NULL;
- EFI_FILE_HANDLE root = NULL;
- EFI_FILE_HANDLE file = NULL;
- // 1. Get the LoadedImage protocol for the current bootloader
- status = uefi_call_wrapper(
- SystemTable->BootServices->HandleProtocol,
- 3,
- ImageHandle,
- &LoadedImageProtocol,
- (VOID **)&loaded_image
- );
- if (EFI_ERROR(status)) return status;
- // 2. Open the SimpleFileSystem protocol on the device handle of the loaded image
- status = uefi_call_wrapper(
- SystemTable->BootServices->HandleProtocol,
- 3,
- loaded_image->DeviceHandle,
- &FileSystemProtocol,
- (VOID **)&fs
- );
- if (EFI_ERROR(status)) return status;
- // 3. Open the volume
- status = uefi_call_wrapper(fs->OpenVolume, 2, fs, &root);
- if (EFI_ERROR(status)) return status;
- // 4. Open the file
- status = uefi_call_wrapper(
- root->Open,
- 5,
- root,
- &file,
- FileName,
- EFI_FILE_MODE_READ,
- 0
- );
- if (EFI_ERROR(status)) {
- uefi_call_wrapper(root->Close, 1, root);
- return status;
- }
- // 5. Get file info to determine size
- EFI_FILE_INFO *file_info = NULL;
- UINTN info_size = 0;
- status = uefi_call_wrapper(
- file->GetInfo,
- 4,
- file,
- &GenericFileInfo,
- &info_size,
- NULL
- );
- if (status == EFI_BUFFER_TOO_SMALL) {
- status = uefi_call_wrapper(
- SystemTable->BootServices->AllocatePool,
- 3,
- EfiLoaderData,
- info_size,
- (VOID **)&file_info
- );
- if (EFI_ERROR(status)) {
- uefi_call_wrapper(file->Close, 1, file);
- uefi_call_wrapper(root->Close, 1, root);
- return status;
- }
- status = uefi_call_wrapper(
- file->GetInfo,
- 4,
- file,
- &GenericFileInfo,
- &info_size,
- (VOID *)file_info
- );
- }
- if (EFI_ERROR(status)) {
- if (file_info) uefi_call_wrapper(SystemTable->BootServices->FreePool, 1, file_info);
- uefi_call_wrapper(file->Close, 1, file);
- uefi_call_wrapper(root->Close, 1, root);
- return status;
- }
- UINTN file_size = file_info->FileSize;
- uefi_call_wrapper(SystemTable->BootServices->FreePool, 1, file_info);
- // 6. Allocate buffer for file contents using AllocatePages for large buffers
- UINTN num_pages = (file_size + 4095) / 4096;
- EFI_PHYSICAL_ADDRESS phys_buffer = 0;
- status = uefi_call_wrapper(
- SystemTable->BootServices->AllocatePages,
- 4,
- AllocateAnyPages,
- EfiLoaderData,
- num_pages,
- &phys_buffer
- );
- if (EFI_ERROR(status)) {
- uefi_call_wrapper(file->Close, 1, file);
- uefi_call_wrapper(root->Close, 1, root);
- return status;
- }
- VOID *buffer = (VOID *)(UINTN)phys_buffer;
- // 7. Read file contents
- UINTN read_size = file_size;
- status = uefi_call_wrapper(file->Read, 3, file, &read_size, buffer);
- if (EFI_ERROR(status) || read_size != file_size) {
- uefi_call_wrapper(SystemTable->BootServices->FreePages, 2, phys_buffer, num_pages);
- uefi_call_wrapper(file->Close, 1, file);
- uefi_call_wrapper(root->Close, 1, root);
- return EFI_DEVICE_ERROR;
- }
- // 8. Clean up and return
- uefi_call_wrapper(file->Close, 1, file);
- uefi_call_wrapper(root->Close, 1, root);
- *BufferOut = buffer;
- *SizeOut = file_size;
- return EFI_SUCCESS;
- }
- EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
- InitializeLib(ImageHandle, SystemTable);
- EFI_STATUS status;
- // 1. Locate the Graphics Output Protocol (GOP)
- EFI_GRAPHICS_OUTPUT_PROTOCOL *gop = NULL;
- EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
- status = uefi_call_wrapper(
- SystemTable->BootServices->LocateProtocol,
- 3,
- &gop_guid,
- NULL,
- (VOID **)&gop
- );
- if (!EFI_ERROR(status) && gop) {
- UINT32 max_mode = gop->Mode->MaxMode;
- UINT32 target_mode = gop->Mode->Mode;
- UINT32 max_res = 0;
- for (UINT32 i = 0; i < max_mode; i++) {
- EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *info;
- UINTN size_of_info;
- EFI_STATUS s = uefi_call_wrapper(gop->QueryMode, 4, gop, i, &size_of_info, &info);
- if (!EFI_ERROR(s)) {
- if (info->HorizontalResolution == 3840 && info->VerticalResolution == 2160) {
- target_mode = i;
- uefi_call_wrapper(SystemTable->BootServices->FreePool, 1, info);
- break;
- }
- UINT32 res = info->HorizontalResolution * info->VerticalResolution;
- if (res > max_res) {
- max_res = res;
- target_mode = i;
- }
- uefi_call_wrapper(SystemTable->BootServices->FreePool, 1, info);
- }
- }
- uefi_call_wrapper(gop->SetMode, 2, gop, target_mode);
- }
- UINTN center_x = 0;
- UINTN center_y = 0;
- UINTN anim_width = 200;
- UINTN anim_height = 200;
- // 2. Clear screen to black and display the white dot
- if (!EFI_ERROR(status) && gop) {
- EFI_GRAPHICS_OUTPUT_BLT_PIXEL black = {0, 0, 0, 0};
- uefi_call_wrapper(
- gop->Blt,
- 10,
- gop,
- &black,
- EfiBltVideoFill,
- 0, 0,
- 0, 0,
- gop->Mode->Info->HorizontalResolution,
- gop->Mode->Info->VerticalResolution,
- 0
- );
- center_x = (gop->Mode->Info->HorizontalResolution - anim_width) / 2;
- center_y = (gop->Mode->Info->VerticalResolution - anim_height) / 2;
- // Draw a 6x6 pixel white dot in the center of the 200x200 region
- EFI_GRAPHICS_OUTPUT_BLT_PIXEL white = {255, 255, 255, 0};
- uefi_call_wrapper(
- gop->Blt,
- 10,
- gop,
- &white,
- EfiBltVideoFill,
- 0, 0,
- center_x + 97, center_y + 97,
- 6, 6,
- 0
- );
- }
- // 3. Perform OS Key signature check and load DRM
- status = check_os_signature(ImageHandle, SystemTable);
- if (EFI_ERROR(status)) {
- return status;
- }
- load_drm_module();
- // 4. Resolve path and load the kernel image (bzImage)
- EFI_LOADED_IMAGE *loaded_image = NULL;
- status = uefi_call_wrapper(
- SystemTable->BootServices->HandleProtocol,
- 3,
- ImageHandle,
- &LoadedImageProtocol,
- (VOID **)&loaded_image
- );
- if (EFI_ERROR(status)) return status;
- EFI_DEVICE_PATH *kernel_path = FileDevicePath(loaded_image->DeviceHandle, L"\\EFI\\BOOT\\bzImage");
- if (kernel_path == NULL) return EFI_OUT_OF_RESOURCES;
- EFI_HANDLE kernel_img = NULL;
- status = uefi_call_wrapper(
- SystemTable->BootServices->LoadImage,
- 6,
- FALSE,
- ImageHandle,
- kernel_path,
- NULL,
- 0,
- &kernel_img
- );
- if (EFI_ERROR(status)) return status;
- // 5. Play the boot animation (expanding nucleus) from ESP
- if (gop) {
- VOID *anim_buffer = NULL;
- UINTN anim_size = 0;
- status = read_file_from_esp(ImageHandle, SystemTable, L"\\EFI\\BOOT\\animation.bin", &anim_buffer, &anim_size);
- if (EFI_ERROR(status)) {
- EFI_GRAPHICS_OUTPUT_BLT_PIXEL red = {0, 0, 255, 0};
- uefi_call_wrapper(gop->Blt, 10, gop, &red, EfiBltVideoFill, 0, 0, center_x + 97, center_y + 97, 16, 16, 0);
- uefi_call_wrapper(SystemTable->BootServices->Stall, 1, 3000000); // 3 seconds
- } else {
- UINTN num_frames = 60;
- UINTN expected_size = num_frames * anim_width * anim_height * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
-
- if (anim_size < expected_size) {
- EFI_GRAPHICS_OUTPUT_BLT_PIXEL green = {0, 255, 0, 0};
- uefi_call_wrapper(gop->Blt, 10, gop, &green, EfiBltVideoFill, 0, 0, center_x + 97, center_y + 97, 16, 16, 0);
- uefi_call_wrapper(SystemTable->BootServices->Stall, 1, 3000000); // 3 seconds
- } else {
- // Clear the white dot to black first
- EFI_GRAPHICS_OUTPUT_BLT_PIXEL black = {0, 0, 0, 0};
- uefi_call_wrapper(
- gop->Blt,
- 10,
- gop,
- &black,
- EfiBltVideoFill,
- 0, 0,
- 0, 0,
- gop->Mode->Info->HorizontalResolution,
- gop->Mode->Info->VerticalResolution,
- 0
- );
- EFI_GRAPHICS_OUTPUT_BLT_PIXEL *frames = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)anim_buffer;
- for (UINTN f = 0; f < num_frames; f++) {
- EFI_GRAPHICS_OUTPUT_BLT_PIXEL *frame = &frames[f * anim_width * anim_height];
-
- uefi_call_wrapper(
- gop->Blt,
- 10,
- gop,
- frame,
- EfiBltBufferToVideo,
- 0, 0,
- center_x, center_y,
- anim_width, anim_height,
- anim_width * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL)
- );
-
- uefi_call_wrapper(SystemTable->BootServices->Stall, 1, 16666);
- }
- }
- uefi_call_wrapper(SystemTable->BootServices->FreePages, 2, (EFI_PHYSICAL_ADDRESS)(UINTN)anim_buffer, (anim_size + 4095) / 4096);
- }
- }
- // 6. Set command line options for the kernel
- EFI_LOADED_IMAGE *kernel_loaded_image = NULL;
- status = uefi_call_wrapper(
- SystemTable->BootServices->HandleProtocol,
- 3,
- kernel_img,
- &LoadedImageProtocol,
- (VOID **)&kernel_loaded_image
- );
- if (EFI_ERROR(status)) return status;
- CHAR16 *cmd_line = L"initrd=\\EFI\\BOOT\\initramfs.img console=ttyS0 console=tty0 loglevel=0 logo.nologo init=/init root=/dev/sdb rw quiet vt.global_cursor_default=0";
- kernel_loaded_image->LoadOptions = cmd_line;
- kernel_loaded_image->LoadOptionsSize = (StrLen(cmd_line) + 1) * sizeof(CHAR16);
- // 7. Start the kernel
- UINTN exit_data_size = 0;
- CHAR16 *exit_data = NULL;
- status = uefi_call_wrapper(
- SystemTable->BootServices->StartImage,
- 3,
- kernel_img,
- &exit_data_size,
- &exit_data
- );
- return status;
- }
|