#include <bootstrap.h>
#include <efi.h>
#include <eficonsctl.h>
#include <efilib.h>
#include <stand.h>
static EFI_PHYSICAL_ADDRESS heap;
static UINTN heapsize;
void
efi_exit(EFI_STATUS exit_code)
{
if (boot_services_active) {
BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
BS->Exit(IH, exit_code, 0, NULL);
} else {
RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
}
__unreachable();
}
void
exit(int status)
{
efi_exit(errno_to_efi_status(status));
__unreachable();
}
static CHAR16 *
arg_skipsep(CHAR16 *argp)
{
while (*argp == ' ' || *argp == '\t' || *argp == '\n')
argp++;
return (argp);
}
static CHAR16 *
arg_skipword(CHAR16 *argp)
{
while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n')
argp++;
return (argp);
}
EFI_STATUS
efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
{
static EFI_GUID image_protocol = LOADED_IMAGE_PROTOCOL;
static EFI_GUID console_control_protocol =
EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
EFI_CONSOLE_CONTROL_PROTOCOL *console_control = NULL;
EFI_LOADED_IMAGE *img;
CHAR16 *argp, *args, **argv;
EFI_STATUS status;
int argc, addprog;
IH = image_handle;
ST = system_table;
BS = ST->BootServices;
RS = ST->RuntimeServices;
status = BS->LocateProtocol(&console_control_protocol, NULL,
(VOID **)&console_control);
if (status == EFI_SUCCESS)
(void)console_control->SetMode(console_control,
EfiConsoleControlScreenText);
heapsize = 64 * 1024 * 1024;
status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
EFI_SIZE_TO_PAGES(heapsize), &heap);
if (status != EFI_SUCCESS) {
ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"Failed to allocate memory for heap.\r\n");
BS->Exit(IH, status, 0, NULL);
}
setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
tslog_init();
status = OpenProtocolByHandle(IH, &image_protocol, (void**)&img);
if (status != EFI_SUCCESS)
efi_exit(status);
if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {
if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {
args = malloc(img->LoadOptionsSize << 1);
for (argc = 0; argc < (int)img->LoadOptionsSize; argc++)
args[argc] = ((char*)img->LoadOptions)[argc];
} else {
args = malloc(img->LoadOptionsSize);
memcpy(args, img->LoadOptions, img->LoadOptionsSize);
}
} else
args = NULL;
addprog = (args == NULL || img->ParentHandle == NULL ||
img->FilePath == NULL) ? 1 : 0;
if (!addprog) {
addprog =
(DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||
DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||
DevicePathNodeLength(img->FilePath) <=
sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0;
if (!addprog) {
}
}
argc = (addprog) ? 1 : 0;
argp = args;
while (argp != NULL && *argp != 0) {
argp = arg_skipsep(argp);
if (*argp == 0)
break;
argc++;
argp = arg_skipword(argp);
}
argv = malloc((argc + 1) * sizeof(CHAR16*));
argc = 0;
if (addprog)
argv[argc++] = (CHAR16 *)L"loader.efi";
argp = args;
while (argp != NULL && *argp != 0) {
argp = arg_skipsep(argp);
if (*argp == 0)
break;
argv[argc++] = argp;
argp = arg_skipword(argp);
if (*argp != 0)
*argp++ = 0;
}
argv[argc] = NULL;
status = main(argc, argv);
efi_exit(status);
return (status);
}