/*-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}4647void48exit(int status)49{5051efi_exit(EFI_LOAD_ERROR);52}5354static CHAR16 *55arg_skipsep(CHAR16 *argp)56{5758while (*argp == ' ' || *argp == '\t' || *argp == '\n')59argp++;60return (argp);61}6263static CHAR16 *64arg_skipword(CHAR16 *argp)65{6667while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n')68argp++;69return (argp);70}7172EFI_STATUS73efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)74{75static EFI_GUID image_protocol = LOADED_IMAGE_PROTOCOL;76static EFI_GUID console_control_protocol =77EFI_CONSOLE_CONTROL_PROTOCOL_GUID;78EFI_CONSOLE_CONTROL_PROTOCOL *console_control = NULL;79EFI_LOADED_IMAGE *img;80CHAR16 *argp, *args, **argv;81EFI_STATUS status;82int argc, addprog;8384IH = image_handle;85ST = system_table;86BS = ST->BootServices;87RS = ST->RuntimeServices;8889status = BS->LocateProtocol(&console_control_protocol, NULL,90(VOID **)&console_control);91if (status == EFI_SUCCESS)92(void)console_control->SetMode(console_control,93EfiConsoleControlScreenText);9495heapsize = 64 * 1024 * 1024;96status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,97EFI_SIZE_TO_PAGES(heapsize), &heap);98if (status != EFI_SUCCESS) {99ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"Failed to allocate memory for heap.\r\n");100BS->Exit(IH, status, 0, NULL);101}102103setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));104105/* Start tslog now that we have a heap.*/106tslog_init();107108/* Use efi_exit() from here on... */109110status = OpenProtocolByHandle(IH, &image_protocol, (void**)&img);111if (status != EFI_SUCCESS)112efi_exit(status);113114/*115* Pre-process the (optional) load options. If the option string116* is given as an ASCII string, we use a poor man's ASCII to117* Unicode-16 translation. The size of the option string as given118* to us includes the terminating null character. We assume the119* string is an ASCII string if strlen() plus the terminating120* '\0' is less than LoadOptionsSize. Even if all Unicode-16121* characters have the upper 8 bits non-zero, the terminating122* null character will cause a one-off.123* If the string is already in Unicode-16, we make a copy so that124* we know we can always modify the string.125*/126if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {127if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {128args = malloc(img->LoadOptionsSize << 1);129for (argc = 0; argc < (int)img->LoadOptionsSize; argc++)130args[argc] = ((char*)img->LoadOptions)[argc];131} else {132args = malloc(img->LoadOptionsSize);133memcpy(args, img->LoadOptions, img->LoadOptionsSize);134}135} else136args = NULL;137138/*139* Use a quick and dirty algorithm to build the argv vector. We140* first count the number of words. Then, after allocating the141* vector, we split the string up. We don't deal with quotes or142* other more advanced shell features.143* The EFI shell will pass the name of the image as the first144* word in the argument list. This does not happen if we're145* loaded by the boot manager. This is not so easy to figure146* out though. The ParentHandle is not always NULL, because147* there can be a function (=image) that will perform the task148* for the boot manager.149*/150/* Part 1: Figure out if we need to add our program name. */151addprog = (args == NULL || img->ParentHandle == NULL ||152img->FilePath == NULL) ? 1 : 0;153if (!addprog) {154addprog =155(DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||156DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||157DevicePathNodeLength(img->FilePath) <=158sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0;159if (!addprog) {160/* XXX todo. */161}162}163/* Part 2: count words. */164argc = (addprog) ? 1 : 0;165argp = args;166while (argp != NULL && *argp != 0) {167argp = arg_skipsep(argp);168if (*argp == 0)169break;170argc++;171argp = arg_skipword(argp);172}173/* Part 3: build vector. */174argv = malloc((argc + 1) * sizeof(CHAR16*));175argc = 0;176if (addprog)177argv[argc++] = (CHAR16 *)L"loader.efi";178argp = args;179while (argp != NULL && *argp != 0) {180argp = arg_skipsep(argp);181if (*argp == 0)182break;183argv[argc++] = argp;184argp = arg_skipword(argp);185/* Terminate the words. */186if (*argp != 0)187*argp++ = 0;188}189argv[argc] = NULL;190191status = main(argc, argv);192efi_exit(status);193return (status);194}195196197