xref: /illumos-gate/usr/src/boot/efi/loader/efi_main.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 
29 #include <efi.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 (has_boot_services) {
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 __unused)
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 	EFI_LOADED_IMAGE *img;
78 	CHAR16 *argp, *args, **argv;
79 	EFI_STATUS status;
80 	int argc, addprog;
81 
82 	IH = image_handle;
83 	ST = system_table;
84 	BS = ST->BootServices;
85 	RS = ST->RuntimeServices;
86 
87 	heapsize = 64 * 1024 * 1024;
88 	/* 4GB upper limit, try to leave some space from 1MB */
89 	heap = 0x0000000100000000;
90 	status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData,
91 	    EFI_SIZE_TO_PAGES(heapsize), &heap);
92 	if (status != EFI_SUCCESS)
93 		BS->Exit(IH, status, 0, NULL);
94 
95 	setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
96 
97 	/* Use efi_exit() from here on... */
98 
99 	status = OpenProtocolByHandle(IH, &image_protocol, (void **)&img);
100 	if (status != EFI_SUCCESS)
101 		efi_exit(status);
102 
103 	/*
104 	 * Pre-process the (optional) load options. If the option string
105 	 * is given as an ASCII string, we use a poor man's ASCII to
106 	 * Unicode-16 translation. The size of the option string as given
107 	 * to us includes the terminating null character. We assume the
108 	 * string is an ASCII string if strlen() plus the terminating
109 	 * '\0' is less than LoadOptionsSize. Even if all Unicode-16
110 	 * characters have the upper 8 bits non-zero, the terminating
111 	 * null character will cause a one-off.
112 	 * If the string is already in Unicode-16, we make a copy so that
113 	 * we know we can always modify the string.
114 	 */
115 	if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {
116 		if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {
117 			args = malloc(img->LoadOptionsSize << 1);
118 			for (argc = 0; argc < (int)img->LoadOptionsSize; argc++)
119 				args[argc] = ((char*)img->LoadOptions)[argc];
120 		} else {
121 			args = malloc(img->LoadOptionsSize);
122 			memcpy(args, img->LoadOptions, img->LoadOptionsSize);
123 		}
124 	} else
125 		args = NULL;
126 
127 	/*
128 	 * Use a quick and dirty algorithm to build the argv vector. We
129 	 * first count the number of words. Then, after allocating the
130 	 * vector, we split the string up. We don't deal with quotes or
131 	 * other more advanced shell features.
132 	 * The EFI shell will pass the name of the image as the first
133 	 * word in the argument list. This does not happen if we're
134 	 * loaded by the boot manager. This is not so easy to figure
135 	 * out though. The ParentHandle is not always NULL, because
136 	 * there can be a function (=image) that will perform the task
137 	 * for the boot manager.
138 	 */
139 	/* Part 1: Figure out if we need to add our program name. */
140 	addprog = (args == NULL || img->ParentHandle == NULL ||
141 	    img->FilePath == NULL) ? 1 : 0;
142 	if (!addprog) {
143 		addprog =
144 		    (DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||
145 		     DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||
146 		     DevicePathNodeLength(img->FilePath) <=
147 			sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0;
148 		if (!addprog) {
149 			/* XXX todo. */
150 		}
151 	}
152 	/* Part 2: count words. */
153 	argc = (addprog) ? 1 : 0;
154 	argp = args;
155 	while (argp != NULL && *argp != 0) {
156 		argp = arg_skipsep(argp);
157 		if (*argp == 0)
158 			break;
159 		argc++;
160 		argp = arg_skipword(argp);
161 	}
162 	/* Part 3: build vector. */
163 	argv = malloc((argc + 1) * sizeof(CHAR16*));
164 	argc = 0;
165 	if (addprog)
166 		argv[argc++] = (CHAR16 *)LOADER_EFI;
167 	argp = args;
168 	while (argp != NULL && *argp != 0) {
169 		argp = arg_skipsep(argp);
170 		if (*argp == 0)
171 			break;
172 		argv[argc++] = argp;
173 		argp = arg_skipword(argp);
174 		/* Terminate the words. */
175 		if (*argp != 0)
176 			*argp++ = 0;
177 	}
178 	argv[argc] = NULL;
179 
180 	status = main(argc, argv);
181 	efi_exit(status);
182 	return (status);
183 }
184