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