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