/*-1* Copyright (c) 2000 Doug Rabson2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <bootstrap.h>27#include <efi.h>28#include <eficonsctl.h>29#include <efilib.h>30#include <stand.h>3132static EFI_PHYSICAL_ADDRESS heap;33static UINTN heapsize;3435void36efi_exit(EFI_STATUS exit_code)37{3839if (boot_services_active) {40BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));41BS->Exit(IH, exit_code, 0, NULL);42} else {43RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);44}45__unreachable();46}4748void49exit(int status)50{5152efi_exit(errno_to_efi_status(status));53__unreachable();54}5556static CHAR16 *57arg_skipsep(CHAR16 *argp)58{5960while (*argp == ' ' || *argp == '\t' || *argp == '\n')61argp++;62return (argp);63}6465static CHAR16 *66arg_skipword(CHAR16 *argp)67{6869while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n')70argp++;71return (argp);72}7374EFI_STATUS75efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)76{77static EFI_GUID image_protocol = LOADED_IMAGE_PROTOCOL;78static EFI_GUID console_control_protocol =79EFI_CONSOLE_CONTROL_PROTOCOL_GUID;80EFI_CONSOLE_CONTROL_PROTOCOL *console_control = NULL;81EFI_LOADED_IMAGE *img;82CHAR16 *argp, *args, **argv;83EFI_STATUS status;84int argc, addprog;8586IH = image_handle;87ST = system_table;88BS = ST->BootServices;89RS = ST->RuntimeServices;9091status = BS->LocateProtocol(&console_control_protocol, NULL,92(VOID **)&console_control);93if (status == EFI_SUCCESS)94(void)console_control->SetMode(console_control,95EfiConsoleControlScreenText);9697heapsize = 64 * 1024 * 1024;98status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,99EFI_SIZE_TO_PAGES(heapsize), &heap);100if (status != EFI_SUCCESS) {101ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"Failed to allocate memory for heap.\r\n");102BS->Exit(IH, status, 0, NULL);103}104105setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));106107/* Start tslog now that we have a heap.*/108tslog_init();109110/* Use efi_exit() from here on... */111112status = OpenProtocolByHandle(IH, &image_protocol, (void**)&img);113if (status != EFI_SUCCESS)114efi_exit(status);115116/*117* Pre-process the (optional) load options. If the option string118* is given as an ASCII string, we use a poor man's ASCII to119* Unicode-16 translation. The size of the option string as given120* to us includes the terminating null character. We assume the121* string is an ASCII string if strlen() plus the terminating122* '\0' is less than LoadOptionsSize. Even if all Unicode-16123* characters have the upper 8 bits non-zero, the terminating124* null character will cause a one-off.125* If the string is already in Unicode-16, we make a copy so that126* we know we can always modify the string.127*/128if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {129if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {130args = malloc(img->LoadOptionsSize << 1);131for (argc = 0; argc < (int)img->LoadOptionsSize; argc++)132args[argc] = ((char*)img->LoadOptions)[argc];133} else {134args = malloc(img->LoadOptionsSize);135memcpy(args, img->LoadOptions, img->LoadOptionsSize);136}137} else138args = NULL;139140/*141* Use a quick and dirty algorithm to build the argv vector. We142* first count the number of words. Then, after allocating the143* vector, we split the string up. We don't deal with quotes or144* other more advanced shell features.145* The EFI shell will pass the name of the image as the first146* word in the argument list. This does not happen if we're147* loaded by the boot manager. This is not so easy to figure148* out though. The ParentHandle is not always NULL, because149* there can be a function (=image) that will perform the task150* for the boot manager.151*/152/* Part 1: Figure out if we need to add our program name. */153addprog = (args == NULL || img->ParentHandle == NULL ||154img->FilePath == NULL) ? 1 : 0;155if (!addprog) {156addprog =157(DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||158DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||159DevicePathNodeLength(img->FilePath) <=160sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0;161if (!addprog) {162/* XXX todo. */163}164}165/* Part 2: count words. */166argc = (addprog) ? 1 : 0;167argp = args;168while (argp != NULL && *argp != 0) {169argp = arg_skipsep(argp);170if (*argp == 0)171break;172argc++;173argp = arg_skipword(argp);174}175/* Part 3: build vector. */176argv = malloc((argc + 1) * sizeof(CHAR16*));177argc = 0;178if (addprog)179argv[argc++] = (CHAR16 *)L"loader.efi";180argp = args;181while (argp != NULL && *argp != 0) {182argp = arg_skipsep(argp);183if (*argp == 0)184break;185argv[argc++] = argp;186argp = arg_skipword(argp);187/* Terminate the words. */188if (*argp != 0)189*argp++ = 0;190}191argv[argc] = NULL;192193status = main(argc, argv);194efi_exit(status);195return (status);196}197198199