199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2000 Doug Rabson
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efi.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <eficonsctl.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efilib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeEFI_HANDLE IH;
199767f8919635c4928607450d9e0abb932109ceToomas SoomeEFI_SYSTEM_TABLE *ST;
199767f8919635c4928607450d9e0abb932109ceToomas SoomeEFI_BOOT_SERVICES *BS;
199767f8919635c4928607450d9e0abb932109ceToomas SoomeEFI_RUNTIME_SERVICES *RS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_PHYSICAL_ADDRESS heap;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic UINTN heapsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic CHAR16 *
199767f8919635c4928607450d9e0abb932109ceToomas Soomearg_skipsep(CHAR16 *argp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*argp == ' ' || *argp == '\t' || *argp == '\n')
199767f8919635c4928607450d9e0abb932109ceToomas Soome argp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (argp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic CHAR16 *
199767f8919635c4928607450d9e0abb932109ceToomas Soomearg_skipword(CHAR16 *argp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n')
199767f8919635c4928607450d9e0abb932109ceToomas Soome argp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (argp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid *
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefi_get_table(EFI_GUID *tbl)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_GUID *id;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < ST->NumberOfTableEntries; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome id = &ST->ConfigurationTable[i].VendorGuid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!memcmp(id, tbl, sizeof(EFI_GUID)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ST->ConfigurationTable[i].VendorTable);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid exit(EFI_STATUS exit_code)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
199767f8919635c4928607450d9e0abb932109ceToomas Soome BS->Exit(IH, exit_code, 0, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomeefi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static EFI_GUID image_protocol = LOADED_IMAGE_PROTOCOL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome static EFI_GUID console_control_protocol =
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_CONSOLE_CONTROL_PROTOCOL *console_control = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_LOADED_IMAGE *img;
199767f8919635c4928607450d9e0abb932109ceToomas Soome CHAR16 *argp, *args, **argv;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int argc, addprog;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome IH = image_handle;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ST = system_table;
199767f8919635c4928607450d9e0abb932109ceToomas Soome BS = ST->BootServices;
199767f8919635c4928607450d9e0abb932109ceToomas Soome RS = ST->RuntimeServices;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateProtocol(&console_control_protocol, NULL,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (VOID **)&console_control);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status == EFI_SUCCESS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)console_control->SetMode(console_control,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EfiConsoleControlScreenText);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome heapsize = 64 * 1024 * 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* 4GB upper limit, try to leave some space from 1MB */
199767f8919635c4928607450d9e0abb932109ceToomas Soome heap = 0x0000000100000000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_SIZE_TO_PAGES(heapsize), &heap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status != EFI_SUCCESS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome BS->Exit(IH, status, 0, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Use exit() from here on... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->HandleProtocol(IH, &image_protocol, (VOID**)&img);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status != EFI_SUCCESS)
199767f8919635c4928607450d9e0abb932109ceToomas Soome exit(status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Pre-process the (optional) load options. If the option string
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is given as an ASCII string, we use a poor man's ASCII to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Unicode-16 translation. The size of the option string as given
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to us includes the terminating null character. We assume the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * string is an ASCII string if strlen() plus the terminating
199767f8919635c4928607450d9e0abb932109ceToomas Soome * '\0' is less than LoadOptionsSize. Even if all Unicode-16
199767f8919635c4928607450d9e0abb932109ceToomas Soome * characters have the upper 8 bits non-zero, the terminating
199767f8919635c4928607450d9e0abb932109ceToomas Soome * null character will cause a one-off.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If the string is already in Unicode-16, we make a copy so that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * we know we can always modify the string.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = malloc(img->LoadOptionsSize << 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (argc = 0; argc < img->LoadOptionsSize; argc++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome args[argc] = ((char*)img->LoadOptions)[argc];
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = malloc(img->LoadOptionsSize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(args, img->LoadOptions, img->LoadOptionsSize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Use a quick and dirty algorithm to build the argv vector. We
199767f8919635c4928607450d9e0abb932109ceToomas Soome * first count the number of words. Then, after allocating the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * vector, we split the string up. We don't deal with quotes or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * other more advanced shell features.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The EFI shell will pass the name of the image as the first
199767f8919635c4928607450d9e0abb932109ceToomas Soome * word in the argument list. This does not happen if we're
199767f8919635c4928607450d9e0abb932109ceToomas Soome * loaded by the boot manager. This is not so easy to figure
199767f8919635c4928607450d9e0abb932109ceToomas Soome * out though. The ParentHandle is not always NULL, because
199767f8919635c4928607450d9e0abb932109ceToomas Soome * there can be a function (=image) that will perform the task
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for the boot manager.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Part 1: Figure out if we need to add our program name. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome addprog = (args == NULL || img->ParentHandle == NULL ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome img->FilePath == NULL) ? 1 : 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!addprog) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome addprog =
199767f8919635c4928607450d9e0abb932109ceToomas Soome (DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome DevicePathNodeLength(img->FilePath) <=
199767f8919635c4928607450d9e0abb932109ceToomas Soome sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!addprog) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX todo. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Part 2: count words. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome argc = (addprog) ? 1 : 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome argp = args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (argp != NULL && *argp != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome argp = arg_skipsep(argp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*argp == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome argc++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome argp = arg_skipword(argp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Part 3: build vector. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv = malloc((argc + 1) * sizeof(CHAR16*));
199767f8919635c4928607450d9e0abb932109ceToomas Soome argc = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (addprog)
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv[argc++] = (CHAR16 *)L"loader.efi";
199767f8919635c4928607450d9e0abb932109ceToomas Soome argp = args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (argp != NULL && *argp != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome argp = arg_skipsep(argp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*argp == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv[argc++] = argp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome argp = arg_skipword(argp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Terminate the words. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*argp != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *argp++ = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv[argc] = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = main(argc, argv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome exit(status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}