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