xref: /freebsd/stand/efi/loader/efi_main.c (revision 3e15b01d6914c927e37d1699645783acf286655c)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 2000 Doug Rabson
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh  * are met:
8ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh  *
14ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh  * SUCH DAMAGE.
25ca987d46SWarner Losh  */
26ca987d46SWarner Losh 
2719e4f2f2SColin Percival #include <bootstrap.h>
28ca987d46SWarner Losh #include <efi.h>
29ca987d46SWarner Losh #include <eficonsctl.h>
30ca987d46SWarner Losh #include <efilib.h>
31ca987d46SWarner Losh #include <stand.h>
32ca987d46SWarner Losh 
33ca987d46SWarner Losh static EFI_PHYSICAL_ADDRESS heap;
34ca987d46SWarner Losh static UINTN heapsize;
35ca987d46SWarner Losh 
36ca987d46SWarner Losh void
efi_exit(EFI_STATUS exit_code)37ca987d46SWarner Losh efi_exit(EFI_STATUS exit_code)
38ca987d46SWarner Losh {
39ca987d46SWarner Losh 
40*a2e02d9dSToomas Soome 	if (boot_services_active) {
41ca987d46SWarner Losh 		BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
42ca987d46SWarner Losh 		BS->Exit(IH, exit_code, 0, NULL);
43*a2e02d9dSToomas Soome 	} else {
44*a2e02d9dSToomas Soome 		RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
45*a2e02d9dSToomas Soome 	}
46ca987d46SWarner Losh }
47ca987d46SWarner Losh 
48ca987d46SWarner Losh void
exit(int status)49ca987d46SWarner Losh exit(int status)
50ca987d46SWarner Losh {
51ca987d46SWarner Losh 
52ca987d46SWarner Losh 	efi_exit(EFI_LOAD_ERROR);
53ca987d46SWarner Losh }
54ca987d46SWarner Losh 
55ca987d46SWarner Losh static CHAR16 *
arg_skipsep(CHAR16 * argp)56ca987d46SWarner Losh arg_skipsep(CHAR16 *argp)
57ca987d46SWarner Losh {
58ca987d46SWarner Losh 
59ca987d46SWarner Losh 	while (*argp == ' ' || *argp == '\t' || *argp == '\n')
60ca987d46SWarner Losh 		argp++;
61ca987d46SWarner Losh 	return (argp);
62ca987d46SWarner Losh }
63ca987d46SWarner Losh 
64ca987d46SWarner Losh static CHAR16 *
arg_skipword(CHAR16 * argp)65ca987d46SWarner Losh arg_skipword(CHAR16 *argp)
66ca987d46SWarner Losh {
67ca987d46SWarner Losh 
68ca987d46SWarner Losh 	while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n')
69ca987d46SWarner Losh 		argp++;
70ca987d46SWarner Losh 	return (argp);
71ca987d46SWarner Losh }
72ca987d46SWarner Losh 
73ca987d46SWarner Losh EFI_STATUS
efi_main(EFI_HANDLE image_handle,EFI_SYSTEM_TABLE * system_table)74ca987d46SWarner Losh efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
75ca987d46SWarner Losh {
76ca987d46SWarner Losh 	static EFI_GUID image_protocol = LOADED_IMAGE_PROTOCOL;
77ca987d46SWarner Losh 	static EFI_GUID console_control_protocol =
78ca987d46SWarner Losh 	    EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
79ca987d46SWarner Losh 	EFI_CONSOLE_CONTROL_PROTOCOL *console_control = NULL;
80ca987d46SWarner Losh 	EFI_LOADED_IMAGE *img;
81ca987d46SWarner Losh 	CHAR16 *argp, *args, **argv;
82ca987d46SWarner Losh 	EFI_STATUS status;
83ca987d46SWarner Losh 	int argc, addprog;
84ca987d46SWarner Losh 
85ca987d46SWarner Losh 	IH = image_handle;
86ca987d46SWarner Losh 	ST = system_table;
87ca987d46SWarner Losh 	BS = ST->BootServices;
88ca987d46SWarner Losh 	RS = ST->RuntimeServices;
89ca987d46SWarner Losh 
90ca987d46SWarner Losh 	status = BS->LocateProtocol(&console_control_protocol, NULL,
91ca987d46SWarner Losh 	    (VOID **)&console_control);
92ca987d46SWarner Losh 	if (status == EFI_SUCCESS)
93ca987d46SWarner Losh 		(void)console_control->SetMode(console_control,
94ca987d46SWarner Losh 		    EfiConsoleControlScreenText);
95ca987d46SWarner Losh 
96ca987d46SWarner Losh 	heapsize = 64 * 1024 * 1024;
97ca987d46SWarner Losh 	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
98ca987d46SWarner Losh 	    EFI_SIZE_TO_PAGES(heapsize), &heap);
99974df0a9SRebecca Cran 	if (status != EFI_SUCCESS) {
100122229deSRebecca Cran 		ST->ConOut->OutputString(ST->ConOut, (CHAR16 *)L"Failed to allocate memory for heap.\r\n");
101ca987d46SWarner Losh 		BS->Exit(IH, status, 0, NULL);
102974df0a9SRebecca Cran 	}
103ca987d46SWarner Losh 
104ca987d46SWarner Losh 	setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
105ca987d46SWarner Losh 
10619e4f2f2SColin Percival 	/* Start tslog now that we have a heap.*/
10719e4f2f2SColin Percival 	tslog_init();
10819e4f2f2SColin Percival 
109ca987d46SWarner Losh 	/* Use efi_exit() from here on... */
110ca987d46SWarner Losh 
111110d56cbSToomas Soome 	status = OpenProtocolByHandle(IH, &image_protocol, (void**)&img);
112ca987d46SWarner Losh 	if (status != EFI_SUCCESS)
113ca987d46SWarner Losh 		efi_exit(status);
114ca987d46SWarner Losh 
115ca987d46SWarner Losh 	/*
116ca987d46SWarner Losh 	 * Pre-process the (optional) load options. If the option string
117ca987d46SWarner Losh 	 * is given as an ASCII string, we use a poor man's ASCII to
118ca987d46SWarner Losh 	 * Unicode-16 translation. The size of the option string as given
119ca987d46SWarner Losh 	 * to us includes the terminating null character. We assume the
120ca987d46SWarner Losh 	 * string is an ASCII string if strlen() plus the terminating
121ca987d46SWarner Losh 	 * '\0' is less than LoadOptionsSize. Even if all Unicode-16
122ca987d46SWarner Losh 	 * characters have the upper 8 bits non-zero, the terminating
123ca987d46SWarner Losh 	 * null character will cause a one-off.
124ca987d46SWarner Losh 	 * If the string is already in Unicode-16, we make a copy so that
125ca987d46SWarner Losh 	 * we know we can always modify the string.
126ca987d46SWarner Losh 	 */
127ca987d46SWarner Losh 	if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {
128ca987d46SWarner Losh 		if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {
129ca987d46SWarner Losh 			args = malloc(img->LoadOptionsSize << 1);
130ca987d46SWarner Losh 			for (argc = 0; argc < (int)img->LoadOptionsSize; argc++)
131ca987d46SWarner Losh 				args[argc] = ((char*)img->LoadOptions)[argc];
132ca987d46SWarner Losh 		} else {
133ca987d46SWarner Losh 			args = malloc(img->LoadOptionsSize);
134ca987d46SWarner Losh 			memcpy(args, img->LoadOptions, img->LoadOptionsSize);
135ca987d46SWarner Losh 		}
136ca987d46SWarner Losh 	} else
137ca987d46SWarner Losh 		args = NULL;
138ca987d46SWarner Losh 
139ca987d46SWarner Losh 	/*
140ca987d46SWarner Losh 	 * Use a quick and dirty algorithm to build the argv vector. We
141ca987d46SWarner Losh 	 * first count the number of words. Then, after allocating the
142ca987d46SWarner Losh 	 * vector, we split the string up. We don't deal with quotes or
143ca987d46SWarner Losh 	 * other more advanced shell features.
144ca987d46SWarner Losh 	 * The EFI shell will pass the name of the image as the first
145ca987d46SWarner Losh 	 * word in the argument list. This does not happen if we're
146ca987d46SWarner Losh 	 * loaded by the boot manager. This is not so easy to figure
147ca987d46SWarner Losh 	 * out though. The ParentHandle is not always NULL, because
148ca987d46SWarner Losh 	 * there can be a function (=image) that will perform the task
149ca987d46SWarner Losh 	 * for the boot manager.
150ca987d46SWarner Losh 	 */
151ca987d46SWarner Losh 	/* Part 1: Figure out if we need to add our program name. */
152ca987d46SWarner Losh 	addprog = (args == NULL || img->ParentHandle == NULL ||
153ca987d46SWarner Losh 	    img->FilePath == NULL) ? 1 : 0;
154ca987d46SWarner Losh 	if (!addprog) {
155ca987d46SWarner Losh 		addprog =
156ca987d46SWarner Losh 		    (DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||
157ca987d46SWarner Losh 		     DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||
158ca987d46SWarner Losh 		     DevicePathNodeLength(img->FilePath) <=
159ca987d46SWarner Losh 			sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0;
160ca987d46SWarner Losh 		if (!addprog) {
161ca987d46SWarner Losh 			/* XXX todo. */
162ca987d46SWarner Losh 		}
163ca987d46SWarner Losh 	}
164ca987d46SWarner Losh 	/* Part 2: count words. */
165ca987d46SWarner Losh 	argc = (addprog) ? 1 : 0;
166ca987d46SWarner Losh 	argp = args;
167ca987d46SWarner Losh 	while (argp != NULL && *argp != 0) {
168ca987d46SWarner Losh 		argp = arg_skipsep(argp);
169ca987d46SWarner Losh 		if (*argp == 0)
170ca987d46SWarner Losh 			break;
171ca987d46SWarner Losh 		argc++;
172ca987d46SWarner Losh 		argp = arg_skipword(argp);
173ca987d46SWarner Losh 	}
174ca987d46SWarner Losh 	/* Part 3: build vector. */
175ca987d46SWarner Losh 	argv = malloc((argc + 1) * sizeof(CHAR16*));
176ca987d46SWarner Losh 	argc = 0;
177ca987d46SWarner Losh 	if (addprog)
178ca987d46SWarner Losh 		argv[argc++] = (CHAR16 *)L"loader.efi";
179ca987d46SWarner Losh 	argp = args;
180ca987d46SWarner Losh 	while (argp != NULL && *argp != 0) {
181ca987d46SWarner Losh 		argp = arg_skipsep(argp);
182ca987d46SWarner Losh 		if (*argp == 0)
183ca987d46SWarner Losh 			break;
184ca987d46SWarner Losh 		argv[argc++] = argp;
185ca987d46SWarner Losh 		argp = arg_skipword(argp);
186ca987d46SWarner Losh 		/* Terminate the words. */
187ca987d46SWarner Losh 		if (*argp != 0)
188ca987d46SWarner Losh 			*argp++ = 0;
189ca987d46SWarner Losh 	}
190ca987d46SWarner Losh 	argv[argc] = NULL;
191ca987d46SWarner Losh 
192ca987d46SWarner Losh 	status = main(argc, argv);
193ca987d46SWarner Losh 	efi_exit(status);
194ca987d46SWarner Losh 	return (status);
195ca987d46SWarner Losh }
196