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