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