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