1*ca987d46SWarner Losh /*- 2*ca987d46SWarner Losh * Copyright (c) 2000 Doug Rabson 3*ca987d46SWarner Losh * All rights reserved. 4*ca987d46SWarner Losh * 5*ca987d46SWarner Losh * Redistribution and use in source and binary forms, with or without 6*ca987d46SWarner Losh * modification, are permitted provided that the following conditions 7*ca987d46SWarner Losh * are met: 8*ca987d46SWarner Losh * 1. Redistributions of source code must retain the above copyright 9*ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer. 10*ca987d46SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 11*ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer in the 12*ca987d46SWarner Losh * documentation and/or other materials provided with the distribution. 13*ca987d46SWarner Losh * 14*ca987d46SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15*ca987d46SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16*ca987d46SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17*ca987d46SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18*ca987d46SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19*ca987d46SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20*ca987d46SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21*ca987d46SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22*ca987d46SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23*ca987d46SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24*ca987d46SWarner Losh * SUCH DAMAGE. 25*ca987d46SWarner Losh */ 26*ca987d46SWarner Losh 27*ca987d46SWarner Losh #include <sys/cdefs.h> 28*ca987d46SWarner Losh __FBSDID("$FreeBSD$"); 29*ca987d46SWarner Losh 30*ca987d46SWarner Losh #include <efi.h> 31*ca987d46SWarner Losh #include <eficonsctl.h> 32*ca987d46SWarner Losh #include <efilib.h> 33*ca987d46SWarner Losh #include <stand.h> 34*ca987d46SWarner Losh 35*ca987d46SWarner Losh static EFI_PHYSICAL_ADDRESS heap; 36*ca987d46SWarner Losh static UINTN heapsize; 37*ca987d46SWarner Losh 38*ca987d46SWarner Losh void 39*ca987d46SWarner Losh efi_exit(EFI_STATUS exit_code) 40*ca987d46SWarner Losh { 41*ca987d46SWarner Losh 42*ca987d46SWarner Losh BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize)); 43*ca987d46SWarner Losh BS->Exit(IH, exit_code, 0, NULL); 44*ca987d46SWarner Losh } 45*ca987d46SWarner Losh 46*ca987d46SWarner Losh void 47*ca987d46SWarner Losh exit(int status) 48*ca987d46SWarner Losh { 49*ca987d46SWarner Losh 50*ca987d46SWarner Losh efi_exit(EFI_LOAD_ERROR); 51*ca987d46SWarner Losh } 52*ca987d46SWarner Losh 53*ca987d46SWarner Losh static CHAR16 * 54*ca987d46SWarner Losh arg_skipsep(CHAR16 *argp) 55*ca987d46SWarner Losh { 56*ca987d46SWarner Losh 57*ca987d46SWarner Losh while (*argp == ' ' || *argp == '\t' || *argp == '\n') 58*ca987d46SWarner Losh argp++; 59*ca987d46SWarner Losh return (argp); 60*ca987d46SWarner Losh } 61*ca987d46SWarner Losh 62*ca987d46SWarner Losh static CHAR16 * 63*ca987d46SWarner Losh arg_skipword(CHAR16 *argp) 64*ca987d46SWarner Losh { 65*ca987d46SWarner Losh 66*ca987d46SWarner Losh while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n') 67*ca987d46SWarner Losh argp++; 68*ca987d46SWarner Losh return (argp); 69*ca987d46SWarner Losh } 70*ca987d46SWarner Losh 71*ca987d46SWarner Losh EFI_STATUS 72*ca987d46SWarner Losh efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) 73*ca987d46SWarner Losh { 74*ca987d46SWarner Losh static EFI_GUID image_protocol = LOADED_IMAGE_PROTOCOL; 75*ca987d46SWarner Losh static EFI_GUID console_control_protocol = 76*ca987d46SWarner Losh EFI_CONSOLE_CONTROL_PROTOCOL_GUID; 77*ca987d46SWarner Losh EFI_CONSOLE_CONTROL_PROTOCOL *console_control = NULL; 78*ca987d46SWarner Losh EFI_LOADED_IMAGE *img; 79*ca987d46SWarner Losh CHAR16 *argp, *args, **argv; 80*ca987d46SWarner Losh EFI_STATUS status; 81*ca987d46SWarner Losh int argc, addprog; 82*ca987d46SWarner Losh 83*ca987d46SWarner Losh IH = image_handle; 84*ca987d46SWarner Losh ST = system_table; 85*ca987d46SWarner Losh BS = ST->BootServices; 86*ca987d46SWarner Losh RS = ST->RuntimeServices; 87*ca987d46SWarner Losh 88*ca987d46SWarner Losh status = BS->LocateProtocol(&console_control_protocol, NULL, 89*ca987d46SWarner Losh (VOID **)&console_control); 90*ca987d46SWarner Losh if (status == EFI_SUCCESS) 91*ca987d46SWarner Losh (void)console_control->SetMode(console_control, 92*ca987d46SWarner Losh EfiConsoleControlScreenText); 93*ca987d46SWarner Losh 94*ca987d46SWarner Losh heapsize = 64 * 1024 * 1024; 95*ca987d46SWarner Losh status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, 96*ca987d46SWarner Losh EFI_SIZE_TO_PAGES(heapsize), &heap); 97*ca987d46SWarner Losh if (status != EFI_SUCCESS) 98*ca987d46SWarner Losh BS->Exit(IH, status, 0, NULL); 99*ca987d46SWarner Losh 100*ca987d46SWarner Losh setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize)); 101*ca987d46SWarner Losh 102*ca987d46SWarner Losh /* Use efi_exit() from here on... */ 103*ca987d46SWarner Losh 104*ca987d46SWarner Losh status = BS->HandleProtocol(IH, &image_protocol, (VOID**)&img); 105*ca987d46SWarner Losh if (status != EFI_SUCCESS) 106*ca987d46SWarner Losh efi_exit(status); 107*ca987d46SWarner Losh 108*ca987d46SWarner Losh /* 109*ca987d46SWarner Losh * Pre-process the (optional) load options. If the option string 110*ca987d46SWarner Losh * is given as an ASCII string, we use a poor man's ASCII to 111*ca987d46SWarner Losh * Unicode-16 translation. The size of the option string as given 112*ca987d46SWarner Losh * to us includes the terminating null character. We assume the 113*ca987d46SWarner Losh * string is an ASCII string if strlen() plus the terminating 114*ca987d46SWarner Losh * '\0' is less than LoadOptionsSize. Even if all Unicode-16 115*ca987d46SWarner Losh * characters have the upper 8 bits non-zero, the terminating 116*ca987d46SWarner Losh * null character will cause a one-off. 117*ca987d46SWarner Losh * If the string is already in Unicode-16, we make a copy so that 118*ca987d46SWarner Losh * we know we can always modify the string. 119*ca987d46SWarner Losh */ 120*ca987d46SWarner Losh if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) { 121*ca987d46SWarner Losh if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) { 122*ca987d46SWarner Losh args = malloc(img->LoadOptionsSize << 1); 123*ca987d46SWarner Losh for (argc = 0; argc < (int)img->LoadOptionsSize; argc++) 124*ca987d46SWarner Losh args[argc] = ((char*)img->LoadOptions)[argc]; 125*ca987d46SWarner Losh } else { 126*ca987d46SWarner Losh args = malloc(img->LoadOptionsSize); 127*ca987d46SWarner Losh memcpy(args, img->LoadOptions, img->LoadOptionsSize); 128*ca987d46SWarner Losh } 129*ca987d46SWarner Losh } else 130*ca987d46SWarner Losh args = NULL; 131*ca987d46SWarner Losh 132*ca987d46SWarner Losh /* 133*ca987d46SWarner Losh * Use a quick and dirty algorithm to build the argv vector. We 134*ca987d46SWarner Losh * first count the number of words. Then, after allocating the 135*ca987d46SWarner Losh * vector, we split the string up. We don't deal with quotes or 136*ca987d46SWarner Losh * other more advanced shell features. 137*ca987d46SWarner Losh * The EFI shell will pass the name of the image as the first 138*ca987d46SWarner Losh * word in the argument list. This does not happen if we're 139*ca987d46SWarner Losh * loaded by the boot manager. This is not so easy to figure 140*ca987d46SWarner Losh * out though. The ParentHandle is not always NULL, because 141*ca987d46SWarner Losh * there can be a function (=image) that will perform the task 142*ca987d46SWarner Losh * for the boot manager. 143*ca987d46SWarner Losh */ 144*ca987d46SWarner Losh /* Part 1: Figure out if we need to add our program name. */ 145*ca987d46SWarner Losh addprog = (args == NULL || img->ParentHandle == NULL || 146*ca987d46SWarner Losh img->FilePath == NULL) ? 1 : 0; 147*ca987d46SWarner Losh if (!addprog) { 148*ca987d46SWarner Losh addprog = 149*ca987d46SWarner Losh (DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH || 150*ca987d46SWarner Losh DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP || 151*ca987d46SWarner Losh DevicePathNodeLength(img->FilePath) <= 152*ca987d46SWarner Losh sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0; 153*ca987d46SWarner Losh if (!addprog) { 154*ca987d46SWarner Losh /* XXX todo. */ 155*ca987d46SWarner Losh } 156*ca987d46SWarner Losh } 157*ca987d46SWarner Losh /* Part 2: count words. */ 158*ca987d46SWarner Losh argc = (addprog) ? 1 : 0; 159*ca987d46SWarner Losh argp = args; 160*ca987d46SWarner Losh while (argp != NULL && *argp != 0) { 161*ca987d46SWarner Losh argp = arg_skipsep(argp); 162*ca987d46SWarner Losh if (*argp == 0) 163*ca987d46SWarner Losh break; 164*ca987d46SWarner Losh argc++; 165*ca987d46SWarner Losh argp = arg_skipword(argp); 166*ca987d46SWarner Losh } 167*ca987d46SWarner Losh /* Part 3: build vector. */ 168*ca987d46SWarner Losh argv = malloc((argc + 1) * sizeof(CHAR16*)); 169*ca987d46SWarner Losh argc = 0; 170*ca987d46SWarner Losh if (addprog) 171*ca987d46SWarner Losh argv[argc++] = (CHAR16 *)L"loader.efi"; 172*ca987d46SWarner Losh argp = args; 173*ca987d46SWarner Losh while (argp != NULL && *argp != 0) { 174*ca987d46SWarner Losh argp = arg_skipsep(argp); 175*ca987d46SWarner Losh if (*argp == 0) 176*ca987d46SWarner Losh break; 177*ca987d46SWarner Losh argv[argc++] = argp; 178*ca987d46SWarner Losh argp = arg_skipword(argp); 179*ca987d46SWarner Losh /* Terminate the words. */ 180*ca987d46SWarner Losh if (*argp != 0) 181*ca987d46SWarner Losh *argp++ = 0; 182*ca987d46SWarner Losh } 183*ca987d46SWarner Losh argv[argc] = NULL; 184*ca987d46SWarner Losh 185*ca987d46SWarner Losh status = main(argc, argv); 186*ca987d46SWarner Losh efi_exit(status); 187*ca987d46SWarner Losh return (status); 188*ca987d46SWarner Losh } 189