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