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