177943Sdfr/*-
277943Sdfr * Copyright (c) 2000 Doug Rabson
377943Sdfr * All rights reserved.
477943Sdfr *
577943Sdfr * Redistribution and use in source and binary forms, with or without
677943Sdfr * modification, are permitted provided that the following conditions
777943Sdfr * are met:
877943Sdfr * 1. Redistributions of source code must retain the above copyright
977943Sdfr *    notice, this list of conditions and the following disclaimer.
1077943Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1177943Sdfr *    notice, this list of conditions and the following disclaimer in the
1277943Sdfr *    documentation and/or other materials provided with the distribution.
1377943Sdfr *
1477943Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1577943Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1677943Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1777943Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1877943Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1977943Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2077943Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2177943Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2277943Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2377943Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2477943Sdfr * SUCH DAMAGE.
2577943Sdfr */
2677943Sdfr
27113038Sobrien#include <sys/cdefs.h>
28113038Sobrien__FBSDID("$FreeBSD: releng/10.2/sys/boot/efi/libefi/libefi.c 281323 2015-04-09 19:51:55Z jhb $");
2978327Sobrien
3077943Sdfr#include <efi.h>
31271996Semaste#include <eficonsctl.h>
3277943Sdfr#include <efilib.h>
33107723Smarcel#include <stand.h>
3477943Sdfr
3577943SdfrEFI_HANDLE		IH;
3677943SdfrEFI_SYSTEM_TABLE	*ST;
3777943SdfrEFI_BOOT_SERVICES	*BS;
3877943SdfrEFI_RUNTIME_SERVICES	*RS;
3977943Sdfr
40107723Smarcelstatic EFI_PHYSICAL_ADDRESS heap;
41107723Smarcelstatic UINTN heapsize;
42107723Smarcel
43107723Smarcelstatic CHAR16 *
44107723Smarcelarg_skipsep(CHAR16 *argp)
45107723Smarcel{
46107723Smarcel
47107723Smarcel	while (*argp == ' ' || *argp == '\t')
48107723Smarcel		argp++;
49107723Smarcel	return (argp);
50107723Smarcel}
51107723Smarcel
52107723Smarcelstatic CHAR16 *
53107723Smarcelarg_skipword(CHAR16 *argp)
54107723Smarcel{
55107723Smarcel
56107723Smarcel	while (*argp && *argp != ' ' && *argp != '\t')
57107723Smarcel		argp++;
58107723Smarcel	return (argp);
59107723Smarcel}
60107723Smarcel
61107733Smarcelvoid *
62107733Smarcelefi_get_table(EFI_GUID *tbl)
63107733Smarcel{
64107733Smarcel	EFI_GUID *id;
65107733Smarcel	int i;
66107733Smarcel
67107733Smarcel	for (i = 0; i < ST->NumberOfTableEntries; i++) {
68107733Smarcel		id = &ST->ConfigurationTable[i].VendorGuid;
69107733Smarcel		if (!memcmp(id, tbl, sizeof(EFI_GUID)))
70107733Smarcel			return (ST->ConfigurationTable[i].VendorTable);
71107733Smarcel	}
72107733Smarcel	return (NULL);
73107733Smarcel}
74107733Smarcel
75107723Smarcelvoid exit(EFI_STATUS exit_code)
76107723Smarcel{
77107723Smarcel
78107723Smarcel	BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
79107723Smarcel	BS->Exit(IH, exit_code, 0, NULL);
80107723Smarcel}
81107723Smarcel
8277943Sdfrvoid
83107723Smarcelefi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
8477943Sdfr{
85107723Smarcel	static EFI_GUID image_protocol = LOADED_IMAGE_PROTOCOL;
86271996Semaste	static EFI_GUID console_control_protocol =
87271996Semaste	    EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
88271996Semaste	EFI_CONSOLE_CONTROL_PROTOCOL *console_control = NULL;
89107723Smarcel	EFI_LOADED_IMAGE *img;
90107723Smarcel	CHAR16 *argp, *args, **argv;
91107723Smarcel	EFI_STATUS status;
92107723Smarcel	int argc, addprog;
93107723Smarcel
9477943Sdfr	IH = image_handle;
9577943Sdfr	ST = system_table;
9677943Sdfr	BS = ST->BootServices;
9777943Sdfr	RS = ST->RuntimeServices;
98107723Smarcel
99271996Semaste	status = BS->LocateProtocol(&console_control_protocol, NULL,
100271996Semaste	    (VOID **)&console_control);
101271996Semaste	if (status == EFI_SUCCESS)
102271996Semaste		(void)console_control->SetMode(console_control,
103271996Semaste		    EfiConsoleControlScreenText);
104271996Semaste
105281323Sjhb	heapsize = 3 * 1024 * 1024;
106107723Smarcel	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
107107723Smarcel	    EFI_SIZE_TO_PAGES(heapsize), &heap);
108107723Smarcel	if (status != EFI_SUCCESS)
109107723Smarcel		BS->Exit(IH, status, 0, NULL);
110107723Smarcel
111163929Smarcel	setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
112107723Smarcel
113107723Smarcel	/* Use exit() from here on... */
114107723Smarcel
115107723Smarcel	status = BS->HandleProtocol(IH, &image_protocol, (VOID**)&img);
116107723Smarcel	if (status != EFI_SUCCESS)
117107723Smarcel		exit(status);
118107723Smarcel
119107723Smarcel	/*
120107723Smarcel	 * Pre-process the (optional) load options. If the option string
121107723Smarcel	 * is given as an ASCII string, we use a poor man's ASCII to
122107723Smarcel	 * Unicode-16 translation. The size of the option string as given
123107723Smarcel	 * to us includes the terminating null character. We assume the
124107723Smarcel	 * string is an ASCII string if strlen() plus the terminating
125107723Smarcel	 * '\0' is less than LoadOptionsSize. Even if all Unicode-16
126107723Smarcel	 * characters have the upper 8 bits non-zero, the terminating
127107723Smarcel	 * null character will cause a one-off.
128107723Smarcel	 * If the string is already in Unicode-16, we make a copy so that
129107723Smarcel	 * we know we can always modify the string.
130107723Smarcel	 */
131111692Smarcel	if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {
132107723Smarcel		if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {
133107723Smarcel			args = malloc(img->LoadOptionsSize << 1);
134107723Smarcel			for (argc = 0; argc < img->LoadOptionsSize; argc++)
135107723Smarcel				args[argc] = ((char*)img->LoadOptions)[argc];
136107723Smarcel		} else {
137107723Smarcel			args = malloc(img->LoadOptionsSize);
138107723Smarcel			memcpy(args, img->LoadOptions, img->LoadOptionsSize);
139107723Smarcel		}
140107723Smarcel	} else
141107723Smarcel		args = NULL;
142107723Smarcel
143107723Smarcel	/*
144107723Smarcel	 * Use a quick and dirty algorithm to build the argv vector. We
145107723Smarcel	 * first count the number of words. Then, after allocating the
146107723Smarcel	 * vector, we split the string up. We don't deal with quotes or
147107723Smarcel	 * other more advanced shell features.
148243978Srpaulo	 * The EFI shell will pass the name of the image as the first
149107723Smarcel	 * word in the argument list. This does not happen if we're
150107723Smarcel	 * loaded by the boot manager. This is not so easy to figure
151107723Smarcel	 * out though. The ParentHandle is not always NULL, because
152107723Smarcel	 * there can be a function (=image) that will perform the task
153107723Smarcel	 * for the boot manager.
154107723Smarcel	 */
155107723Smarcel	/* Part 1: Figure out if we need to add our program name. */
156107723Smarcel	addprog = (args == NULL || img->ParentHandle == NULL ||
157107723Smarcel	    img->FilePath == NULL) ? 1 : 0;
158107723Smarcel	if (!addprog) {
159107723Smarcel		addprog =
160107723Smarcel		    (DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||
161107723Smarcel		     DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||
162107723Smarcel		     DevicePathNodeLength(img->FilePath) <=
163107723Smarcel			sizeof(FILEPATH_DEVICE_PATH)) ? 1 : 0;
164107723Smarcel		if (!addprog) {
165107723Smarcel			/* XXX todo. */
166107723Smarcel		}
167107723Smarcel	}
168107723Smarcel	/* Part 2: count words. */
169107723Smarcel	argc = (addprog) ? 1 : 0;
170107723Smarcel	argp = args;
171107723Smarcel	while (argp != NULL && *argp != 0) {
172107723Smarcel		argp = arg_skipsep(argp);
173107723Smarcel		if (*argp == 0)
174107723Smarcel			break;
175107723Smarcel		argc++;
176107723Smarcel		argp = arg_skipword(argp);
177107723Smarcel	}
178107723Smarcel	/* Part 3: build vector. */
179107723Smarcel	argv = malloc((argc + 1) * sizeof(CHAR16*));
180107723Smarcel	argc = 0;
181107723Smarcel	if (addprog)
182107723Smarcel		argv[argc++] = L"loader.efi";
183107723Smarcel	argp = args;
184107723Smarcel	while (argp != NULL && *argp != 0) {
185107723Smarcel		argp = arg_skipsep(argp);
186107723Smarcel		if (*argp == 0)
187107723Smarcel			break;
188107723Smarcel		argv[argc++] = argp;
189107723Smarcel		argp = arg_skipword(argp);
190107723Smarcel		/* Terminate the words. */
191107723Smarcel		if (*argp != 0)
192107723Smarcel			*argp++ = 0;
193107723Smarcel	}
194107723Smarcel	argv[argc] = NULL;
195107723Smarcel
196107723Smarcel	status = main(argc, argv);
197107723Smarcel	exit(status);
19877943Sdfr}
199