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 <efi.h> 28 #include <efilib.h> 29 #include <efidevp.h> 30 #include <stand.h> 31 #include <Protocol/LoadedImage.h> 32 #include <bootstrap.h> 33 34 static EFI_PHYSICAL_ADDRESS heap; 35 static UINTN heapsize; 36 37 void 38 efi_exit(EFI_STATUS exit_code) 39 { 40 41 if (has_boot_services) { 42 BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize)); 43 BS->Exit(IH, exit_code, 0, NULL); 44 } else { 45 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); 46 } 47 } 48 49 void 50 exit(int status __unused) 51 { 52 53 efi_exit(EFI_LOAD_ERROR); 54 } 55 56 static CHAR16 * 57 arg_skipsep(CHAR16 *argp) 58 { 59 60 while (*argp == ' ' || *argp == '\t' || *argp == '\n') 61 argp++; 62 return (argp); 63 } 64 65 static CHAR16 * 66 arg_skipword(CHAR16 *argp) 67 { 68 69 while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n') 70 argp++; 71 return (argp); 72 } 73 74 EFI_STATUS 75 efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table) 76 { 77 CHAR16 *argp, *args, **argv; 78 EFI_STATUS status; 79 int argc, addprog; 80 81 IH = image_handle; 82 ST = system_table; 83 BS = ST->BootServices; 84 RS = ST->RuntimeServices; 85 86 heapsize = 64 * 1024 * 1024; 87 /* 4GB upper limit, try to leave some space from 1MB */ 88 heap = 0x0000000100000000; 89 status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData, 90 EFI_SIZE_TO_PAGES(heapsize), &heap); 91 if (status != EFI_SUCCESS) 92 BS->Exit(IH, status, 0, NULL); 93 94 setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize)); 95 96 /* Use efi_exit() from here on... */ 97 98 status = OpenProtocolByHandle(IH, &gEfiLoadedImageProtocolGuid, 99 (void **)&boot_img); 100 if (status != EFI_SUCCESS) 101 efi_exit(status); 102 103 /* 104 * Pre-process the (optional) load options. If the option string 105 * is given as an ASCII string, we use a poor man's ASCII to 106 * Unicode-16 translation. The size of the option string as given 107 * to us includes the terminating null character. We assume the 108 * string is an ASCII string if strlen() plus the terminating 109 * '\0' is less than LoadOptionsSize. Even if all Unicode-16 110 * characters have the upper 8 bits non-zero, the terminating 111 * null character will cause a one-off. 112 * If the string is already in Unicode-16, we make a copy so that 113 * we know we can always modify the string. 114 */ 115 if (boot_img->LoadOptionsSize > 0 && boot_img->LoadOptions != NULL) { 116 if (boot_img->LoadOptionsSize == 117 strlen(boot_img->LoadOptions) + 1) { 118 args = malloc(boot_img->LoadOptionsSize << 1); 119 for (argc = 0; argc < (int)boot_img->LoadOptionsSize; 120 argc++) 121 args[argc] = 122 ((char *)boot_img->LoadOptions)[argc]; 123 } else { 124 args = malloc(boot_img->LoadOptionsSize); 125 memcpy(args, boot_img->LoadOptions, 126 boot_img->LoadOptionsSize); 127 } 128 } else 129 args = NULL; 130 131 /* 132 * Use a quick and dirty algorithm to build the argv vector. We 133 * first count the number of words. Then, after allocating the 134 * vector, we split the string up. We don't deal with quotes or 135 * other more advanced shell features. 136 * The EFI shell will pass the name of the image as the first 137 * word in the argument list. This does not happen if we're 138 * loaded by the boot manager. This is not so easy to figure 139 * out though. The ParentHandle is not always NULL, because 140 * there can be a function (=image) that will perform the task 141 * for the boot manager. 142 */ 143 /* Part 1: Figure out if we need to add our program name. */ 144 addprog = (args == NULL || boot_img->ParentHandle == NULL || 145 boot_img->FilePath == NULL) ? 1 : 0; 146 if (!addprog) { 147 addprog = 148 (DevicePathType(boot_img->FilePath) != MEDIA_DEVICE_PATH || 149 DevicePathSubType(boot_img->FilePath) != 150 MEDIA_FILEPATH_DP || 151 DevicePathNodeLength(boot_img->FilePath) <= 152 sizeof (FILEPATH_DEVICE_PATH)) ? 1 : 0; 153 if (!addprog) { 154 /* XXX todo. */ 155 } 156 } 157 /* Part 2: count words. */ 158 argc = (addprog) ? 1 : 0; 159 argp = args; 160 while (argp != NULL && *argp != 0) { 161 argp = arg_skipsep(argp); 162 if (*argp == 0) 163 break; 164 argc++; 165 argp = arg_skipword(argp); 166 } 167 /* Part 3: build vector. */ 168 argv = malloc((argc + 1) * sizeof (CHAR16*)); 169 argc = 0; 170 if (addprog) 171 argv[argc++] = (CHAR16 *)LOADER_EFI; 172 argp = args; 173 while (argp != NULL && *argp != 0) { 174 argp = arg_skipsep(argp); 175 if (*argp == 0) 176 break; 177 argv[argc++] = argp; 178 argp = arg_skipword(argp); 179 /* Terminate the words. */ 180 if (*argp != 0) 181 *argp++ = 0; 182 } 183 argv[argc] = NULL; 184 185 status = main(argc, argv); 186 efi_exit(status); 187 return (status); 188 } 189 190 static int 191 command_heap(int argc __unused, char *argv[] __unused) 192 { 193 mallocstats(); 194 printf("heap base at %p, top at %p, upper limit at %p\n", 195 (void *)(uintptr_t)heap, sbrk(0), 196 (void *)(uintptr_t)heap + heapsize); 197 return (CMD_OK); 198 } 199 200 COMMAND_SET(heap, "heap", "show heap usage", command_heap); 201