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