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