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