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