2 * Copyright (c) 2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <bootstrap.h>
32 #include <eficonsctl.h>
36 static EFI_PHYSICAL_ADDRESS heap;
37 static UINTN heapsize;
40 efi_exit(EFI_STATUS exit_code)
43 if (boot_services_active) {
44 BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
45 BS->Exit(IH, exit_code, 0, NULL);
47 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
55 efi_exit(EFI_LOAD_ERROR);
59 arg_skipsep(CHAR16 *argp)
62 while (*argp == ' ' || *argp == '\t' || *argp == '\n')
68 arg_skipword(CHAR16 *argp)
71 while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n')
77 efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
79 static EFI_GUID image_protocol = LOADED_IMAGE_PROTOCOL;
80 static EFI_GUID console_control_protocol =
81 EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
82 EFI_CONSOLE_CONTROL_PROTOCOL *console_control = NULL;
83 EFI_LOADED_IMAGE *img;
84 CHAR16 *argp, *args, **argv;
90 BS = ST->BootServices;
91 RS = ST->RuntimeServices;
93 status = BS->LocateProtocol(&console_control_protocol, NULL,
94 (VOID **)&console_control);
95 if (status == EFI_SUCCESS)
96 (void)console_control->SetMode(console_control,
97 EfiConsoleControlScreenText);
99 heapsize = 64 * 1024 * 1024;
100 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
101 EFI_SIZE_TO_PAGES(heapsize), &heap);
102 if (status != EFI_SUCCESS) {
103 ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"Failed to allocate memory for heap.\r\n");
104 BS->Exit(IH, status, 0, NULL);
107 setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
109 /* Start tslog now that we have a heap.*/
112 /* Use efi_exit() from here on... */
114 status = OpenProtocolByHandle(IH, &image_protocol, (void**)&img);
115 if (status != EFI_SUCCESS)
119 * Pre-process the (optional) load options. If the option string
120 * is given as an ASCII string, we use a poor man's ASCII to
121 * Unicode-16 translation. The size of the option string as given
122 * to us includes the terminating null character. We assume the
123 * string is an ASCII string if strlen() plus the terminating
124 * '\0' is less than LoadOptionsSize. Even if all Unicode-16
125 * characters have the upper 8 bits non-zero, the terminating
126 * null character will cause a one-off.
127 * If the string is already in Unicode-16, we make a copy so that
128 * we know we can always modify the string.
130 if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {
131 if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {
132 args = malloc(img->LoadOptionsSize << 1);
133 for (argc = 0; argc < (int)img->LoadOptionsSize; argc++)
134 args[argc] = ((char*)img->LoadOptions)[argc];
136 args = malloc(img->LoadOptionsSize);
137 memcpy(args, img->LoadOptions, img->LoadOptionsSize);
143 * Use a quick and dirty algorithm to build the argv vector. We
144 * first count the number of words. Then, after allocating the
145 * vector, we split the string up. We don't deal with quotes or
146 * other more advanced shell features.
147 * The EFI shell will pass the name of the image as the first
148 * word in the argument list. This does not happen if we're
149 * loaded by the boot manager. This is not so easy to figure
150 * out though. The ParentHandle is not always NULL, because
151 * there can be a function (=image) that will perform the task
152 * for the boot manager.
154 /* Part 1: Figure out if we need to add our program name. */
155 addprog = (args == NULL || img->ParentHandle == NULL ||
156 img->FilePath == NULL) ? 1 : 0;
159 (DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||
160 DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||
161 DevicePathNodeLength(img->FilePath) <=
162 sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0;
167 /* Part 2: count words. */
168 argc = (addprog) ? 1 : 0;
170 while (argp != NULL && *argp != 0) {
171 argp = arg_skipsep(argp);
175 argp = arg_skipword(argp);
177 /* Part 3: build vector. */
178 argv = malloc((argc + 1) * sizeof(CHAR16*));
181 argv[argc++] = (CHAR16 *)L"loader.efi";
183 while (argp != NULL && *argp != 0) {
184 argp = arg_skipsep(argp);
188 argp = arg_skipword(argp);
189 /* Terminate the words. */
195 status = main(argc, argv);