xref: /illumos-gate/usr/src/boot/efi/loader/efi_main.c (revision f334afcf)
165f62d6eSToomas Soome /*
265f62d6eSToomas Soome  * Copyright (c) 2000 Doug Rabson
365f62d6eSToomas Soome  * All rights reserved.
465f62d6eSToomas Soome  *
565f62d6eSToomas Soome  * Redistribution and use in source and binary forms, with or without
665f62d6eSToomas Soome  * modification, are permitted provided that the following conditions
765f62d6eSToomas Soome  * are met:
865f62d6eSToomas Soome  * 1. Redistributions of source code must retain the above copyright
965f62d6eSToomas Soome  *    notice, this list of conditions and the following disclaimer.
1065f62d6eSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
1165f62d6eSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
1265f62d6eSToomas Soome  *    documentation and/or other materials provided with the distribution.
1365f62d6eSToomas Soome  *
1465f62d6eSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1565f62d6eSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1665f62d6eSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1765f62d6eSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1865f62d6eSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1965f62d6eSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2065f62d6eSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2165f62d6eSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2265f62d6eSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2365f62d6eSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2465f62d6eSToomas Soome  * SUCH DAMAGE.
2565f62d6eSToomas Soome  */
2665f62d6eSToomas Soome 
2765f62d6eSToomas Soome #include <efi.h>
2865f62d6eSToomas Soome #include <efilib.h>
29*f334afcfSToomas Soome #include <efidevp.h>
3065f62d6eSToomas Soome #include <stand.h>
31*f334afcfSToomas Soome #include <Protocol/LoadedImage.h>
3265f62d6eSToomas Soome 
3365f62d6eSToomas Soome static EFI_PHYSICAL_ADDRESS heap;
3465f62d6eSToomas Soome static UINTN heapsize;
3565f62d6eSToomas Soome 
3665f62d6eSToomas Soome void
efi_exit(EFI_STATUS exit_code)3765f62d6eSToomas Soome efi_exit(EFI_STATUS exit_code)
3865f62d6eSToomas Soome {
3965f62d6eSToomas Soome 
40190f051bSToomas Soome 	if (has_boot_services) {
41190f051bSToomas Soome 		BS->FreePages(heap, EFI_SIZE_TO_PAGES(heapsize));
42190f051bSToomas Soome 		BS->Exit(IH, exit_code, 0, NULL);
43190f051bSToomas Soome 	} else {
44190f051bSToomas Soome 		RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
45190f051bSToomas Soome 	}
4665f62d6eSToomas Soome }
4765f62d6eSToomas Soome 
4865f62d6eSToomas Soome void
exit(int status __unused)498eef2ab6SToomas Soome exit(int status __unused)
5065f62d6eSToomas Soome {
5165f62d6eSToomas Soome 
5265f62d6eSToomas Soome 	efi_exit(EFI_LOAD_ERROR);
5365f62d6eSToomas Soome }
5465f62d6eSToomas Soome 
5565f62d6eSToomas Soome static CHAR16 *
arg_skipsep(CHAR16 * argp)5665f62d6eSToomas Soome arg_skipsep(CHAR16 *argp)
5765f62d6eSToomas Soome {
5865f62d6eSToomas Soome 
5965f62d6eSToomas Soome 	while (*argp == ' ' || *argp == '\t' || *argp == '\n')
6065f62d6eSToomas Soome 		argp++;
6165f62d6eSToomas Soome 	return (argp);
6265f62d6eSToomas Soome }
6365f62d6eSToomas Soome 
6465f62d6eSToomas Soome static CHAR16 *
arg_skipword(CHAR16 * argp)6565f62d6eSToomas Soome arg_skipword(CHAR16 *argp)
6665f62d6eSToomas Soome {
6765f62d6eSToomas Soome 
6865f62d6eSToomas Soome 	while (*argp && *argp != ' ' && *argp != '\t' && *argp != '\n')
6965f62d6eSToomas Soome 		argp++;
7065f62d6eSToomas Soome 	return (argp);
7165f62d6eSToomas Soome }
7265f62d6eSToomas Soome 
7365f62d6eSToomas Soome EFI_STATUS
efi_main(EFI_HANDLE image_handle,EFI_SYSTEM_TABLE * system_table)7465f62d6eSToomas Soome efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
7565f62d6eSToomas Soome {
76*f334afcfSToomas Soome 	EFI_LOADED_IMAGE_PROTOCOL *img;
7765f62d6eSToomas Soome 	CHAR16 *argp, *args, **argv;
7865f62d6eSToomas Soome 	EFI_STATUS status;
7965f62d6eSToomas Soome 	int argc, addprog;
8065f62d6eSToomas Soome 
8165f62d6eSToomas Soome 	IH = image_handle;
8265f62d6eSToomas Soome 	ST = system_table;
8365f62d6eSToomas Soome 	BS = ST->BootServices;
8465f62d6eSToomas Soome 	RS = ST->RuntimeServices;
8565f62d6eSToomas Soome 
8665f62d6eSToomas Soome 	heapsize = 64 * 1024 * 1024;
8765f62d6eSToomas Soome 	/* 4GB upper limit, try to leave some space from 1MB */
8865f62d6eSToomas Soome 	heap = 0x0000000100000000;
8965f62d6eSToomas Soome 	status = BS->AllocatePages(AllocateMaxAddress, EfiLoaderData,
9065f62d6eSToomas Soome 	    EFI_SIZE_TO_PAGES(heapsize), &heap);
9165f62d6eSToomas Soome 	if (status != EFI_SUCCESS)
9265f62d6eSToomas Soome 		BS->Exit(IH, status, 0, NULL);
9365f62d6eSToomas Soome 
9465f62d6eSToomas Soome 	setheap((void *)(uintptr_t)heap, (void *)(uintptr_t)(heap + heapsize));
9565f62d6eSToomas Soome 
9665f62d6eSToomas Soome 	/* Use efi_exit() from here on... */
9765f62d6eSToomas Soome 
98*f334afcfSToomas Soome 	status = OpenProtocolByHandle(IH, &gEfiLoadedImageProtocolGuid,
99*f334afcfSToomas Soome 	    (void **)&img);
10065f62d6eSToomas Soome 	if (status != EFI_SUCCESS)
10165f62d6eSToomas Soome 		efi_exit(status);
10265f62d6eSToomas Soome 
10365f62d6eSToomas Soome 	/*
10465f62d6eSToomas Soome 	 * Pre-process the (optional) load options. If the option string
10565f62d6eSToomas Soome 	 * is given as an ASCII string, we use a poor man's ASCII to
10665f62d6eSToomas Soome 	 * Unicode-16 translation. The size of the option string as given
10765f62d6eSToomas Soome 	 * to us includes the terminating null character. We assume the
10865f62d6eSToomas Soome 	 * string is an ASCII string if strlen() plus the terminating
10965f62d6eSToomas Soome 	 * '\0' is less than LoadOptionsSize. Even if all Unicode-16
11065f62d6eSToomas Soome 	 * characters have the upper 8 bits non-zero, the terminating
11165f62d6eSToomas Soome 	 * null character will cause a one-off.
11265f62d6eSToomas Soome 	 * If the string is already in Unicode-16, we make a copy so that
11365f62d6eSToomas Soome 	 * we know we can always modify the string.
11465f62d6eSToomas Soome 	 */
11565f62d6eSToomas Soome 	if (img->LoadOptionsSize > 0 && img->LoadOptions != NULL) {
11665f62d6eSToomas Soome 		if (img->LoadOptionsSize == strlen(img->LoadOptions) + 1) {
11765f62d6eSToomas Soome 			args = malloc(img->LoadOptionsSize << 1);
11865f62d6eSToomas Soome 			for (argc = 0; argc < (int)img->LoadOptionsSize; argc++)
119*f334afcfSToomas Soome 				args[argc] = ((char *)img->LoadOptions)[argc];
12065f62d6eSToomas Soome 		} else {
12165f62d6eSToomas Soome 			args = malloc(img->LoadOptionsSize);
12265f62d6eSToomas Soome 			memcpy(args, img->LoadOptions, img->LoadOptionsSize);
12365f62d6eSToomas Soome 		}
12465f62d6eSToomas Soome 	} else
12565f62d6eSToomas Soome 		args = NULL;
12665f62d6eSToomas Soome 
12765f62d6eSToomas Soome 	/*
12865f62d6eSToomas Soome 	 * Use a quick and dirty algorithm to build the argv vector. We
12965f62d6eSToomas Soome 	 * first count the number of words. Then, after allocating the
13065f62d6eSToomas Soome 	 * vector, we split the string up. We don't deal with quotes or
13165f62d6eSToomas Soome 	 * other more advanced shell features.
13265f62d6eSToomas Soome 	 * The EFI shell will pass the name of the image as the first
13365f62d6eSToomas Soome 	 * word in the argument list. This does not happen if we're
13465f62d6eSToomas Soome 	 * loaded by the boot manager. This is not so easy to figure
13565f62d6eSToomas Soome 	 * out though. The ParentHandle is not always NULL, because
13665f62d6eSToomas Soome 	 * there can be a function (=image) that will perform the task
13765f62d6eSToomas Soome 	 * for the boot manager.
13865f62d6eSToomas Soome 	 */
13965f62d6eSToomas Soome 	/* Part 1: Figure out if we need to add our program name. */
14065f62d6eSToomas Soome 	addprog = (args == NULL || img->ParentHandle == NULL ||
14165f62d6eSToomas Soome 	    img->FilePath == NULL) ? 1 : 0;
14265f62d6eSToomas Soome 	if (!addprog) {
14365f62d6eSToomas Soome 		addprog =
14465f62d6eSToomas Soome 		    (DevicePathType(img->FilePath) != MEDIA_DEVICE_PATH ||
145*f334afcfSToomas Soome 		    DevicePathSubType(img->FilePath) != MEDIA_FILEPATH_DP ||
146*f334afcfSToomas Soome 		    DevicePathNodeLength(img->FilePath) <=
147*f334afcfSToomas Soome 		    sizeof (FILEPATH_DEVICE_PATH)) ? 1 : 0;
14865f62d6eSToomas Soome 		if (!addprog) {
14965f62d6eSToomas Soome 			/* XXX todo. */
15065f62d6eSToomas Soome 		}
15165f62d6eSToomas Soome 	}
15265f62d6eSToomas Soome 	/* Part 2: count words. */
15365f62d6eSToomas Soome 	argc = (addprog) ? 1 : 0;
15465f62d6eSToomas Soome 	argp = args;
15565f62d6eSToomas Soome 	while (argp != NULL && *argp != 0) {
15665f62d6eSToomas Soome 		argp = arg_skipsep(argp);
15765f62d6eSToomas Soome 		if (*argp == 0)
15865f62d6eSToomas Soome 			break;
15965f62d6eSToomas Soome 		argc++;
16065f62d6eSToomas Soome 		argp = arg_skipword(argp);
16165f62d6eSToomas Soome 	}
16265f62d6eSToomas Soome 	/* Part 3: build vector. */
163*f334afcfSToomas Soome 	argv = malloc((argc + 1) * sizeof (CHAR16*));
16465f62d6eSToomas Soome 	argc = 0;
16565f62d6eSToomas Soome 	if (addprog)
16683b4671eSToomas Soome 		argv[argc++] = (CHAR16 *)LOADER_EFI;
16765f62d6eSToomas Soome 	argp = args;
16865f62d6eSToomas Soome 	while (argp != NULL && *argp != 0) {
16965f62d6eSToomas Soome 		argp = arg_skipsep(argp);
17065f62d6eSToomas Soome 		if (*argp == 0)
17165f62d6eSToomas Soome 			break;
17265f62d6eSToomas Soome 		argv[argc++] = argp;
17365f62d6eSToomas Soome 		argp = arg_skipword(argp);
17465f62d6eSToomas Soome 		/* Terminate the words. */
17565f62d6eSToomas Soome 		if (*argp != 0)
17665f62d6eSToomas Soome 			*argp++ = 0;
17765f62d6eSToomas Soome 	}
17865f62d6eSToomas Soome 	argv[argc] = NULL;
17965f62d6eSToomas Soome 
18065f62d6eSToomas Soome 	status = main(argc, argv);
18165f62d6eSToomas Soome 	efi_exit(status);
18265f62d6eSToomas Soome 	return (status);
18365f62d6eSToomas Soome }
184