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