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