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