#include <efi.h>
#include <efilib.h>
#include <efidevp.h>
#include <stand.h>
#include <Protocol/LoadedImage.h>
#include <bootstrap.h>
static EFI_PHYSICAL_ADDRESS heap;
static UINTN heapsize;
void
efi_exit(EFI_STATUS exit_code)
{
if (has_boot_services) {
BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
BS->Exit(IH, exit_code, 0, NULL);
} else {
RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
}
}
void
exit(int status __unused)
{
efi_exit(EFI_LOAD_ERROR);
}
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)
{
CHAR16 *argp, *args, **argv;
EFI_STATUS status;
int argc, addprog;
IH = image_handle;
ST = system_table;
BS = ST->BootServices;
RS = ST->RuntimeServices;
heapsize = 64 * 1024 * 1024;
heap = 0x0000000100000000;
status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData,
EFI_SIZE_TO_PAGES(heapsize), &heap);
if (status != EFI_SUCCESS)
BS->Exit(IH, status, 0, NULL);
setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
status = OpenProtocolByHandle(IH, &gEfiLoadedImageProtocolGuid,
(void **)&boot_img);
if (status != EFI_SUCCESS)
efi_exit(status);
if (boot_img->LoadOptionsSize > 0 && boot_img->LoadOptions != NULL) {
if (boot_img->LoadOptionsSize ==
strlen(boot_img->LoadOptions) + 1) {
args = malloc(boot_img->LoadOptionsSize << 1);
for (argc = 0; argc < (int)boot_img->LoadOptionsSize;
argc++)
args[argc] =
((char *)boot_img->LoadOptions)[argc];
} else {
args = malloc(boot_img->LoadOptionsSize);
memcpy(args, boot_img->LoadOptions,
boot_img->LoadOptionsSize);
}
} else
args = NULL;
addprog = (args == NULL || boot_img->ParentHandle == NULL ||
boot_img->FilePath == NULL) ? 1 : 0;
if (!addprog) {
addprog =
(DevicePathType(boot_img->FilePath) != MEDIA_DEVICE_PATH ||
DevicePathSubType(boot_img->FilePath) !=
MEDIA_FILEPATH_DP ||
DevicePathNodeLength(boot_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 *)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);
}
static int
command_heap(int argc __unused, char *argv[] __unused)
{
mallocstats();
printf("heap base at %p, top at %p, upper limit at %p\n",
(void *)(uintptr_t)heap, sbrk(0),
(void *)(uintptr_t)heap + heapsize);
return (CMD_OK);
}
COMMAND_SET(heap, "heap", "show heap usage", command_heap);