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