]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/boot/efi/loader/main.c
MFC r293233: loader.efi: add terminal emulation support
[FreeBSD/stable/10.git] / sys / boot / efi / loader / main.c
1 /*-
2  * Copyright (c) 2008-2010 Rui Paulo
3  * Copyright (c) 2006 Marcel Moolenaar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <stand.h>
32 #include <string.h>
33 #include <setjmp.h>
34
35 #include <efi.h>
36 #include <efilib.h>
37
38 #include <bootstrap.h>
39 #include <smbios.h>
40
41 #include "loader_efi.h"
42
43 extern char bootprog_name[];
44 extern char bootprog_rev[];
45 extern char bootprog_date[];
46 extern char bootprog_maker[];
47
48 struct devdesc currdev;         /* our current device */
49 struct arch_switch archsw;      /* MI/MD interface boundary */
50
51 EFI_GUID acpi = ACPI_TABLE_GUID;
52 EFI_GUID acpi20 = ACPI_20_TABLE_GUID;
53 EFI_GUID devid = DEVICE_PATH_PROTOCOL;
54 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
55 EFI_GUID mps = MPS_TABLE_GUID;
56 EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
57 EFI_GUID smbios = SMBIOS_TABLE_GUID;
58 EFI_GUID dxe = DXE_SERVICES_TABLE_GUID;
59 EFI_GUID hoblist = HOB_LIST_TABLE_GUID;
60 EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID;
61 EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID;
62
63 EFI_STATUS
64 main(int argc, CHAR16 *argv[])
65 {
66         char var[128];
67         EFI_LOADED_IMAGE *img;
68         EFI_GUID *guid;
69         int i, j, vargood;
70
71         /*
72          * XXX Chicken-and-egg problem; we want to have console output
73          * early, but some console attributes may depend on reading from
74          * eg. the boot device, which we can't do yet.  We can use
75          * printf() etc. once this is done.
76          */
77         cons_probe();
78
79         /*
80          * Loop through the args, and for each one that contains an '=' that is
81          * not the first character, add it to the environment.  This allows
82          * loader and kernel env vars to be passed on the command line.  Convert
83          * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
84          */
85         for (i = 1; i < argc; i++) {
86                 vargood = 0;
87                 for (j = 0; argv[i][j] != 0; j++) {
88                         if (j == sizeof(var)) {
89                                 vargood = 0;
90                                 break;
91                         }
92                         if (j > 0 && argv[i][j] == '=')
93                                 vargood = 1;
94                         var[j] = (char)argv[i][j];
95                 }
96                 if (vargood) {
97                         var[j] = 0;
98                         putenv(var);
99                 }
100         }
101
102         if (efi_copy_init()) {
103                 printf("failed to allocate staging area\n");
104                 return (EFI_BUFFER_TOO_SMALL);
105         }
106
107         /*
108          * March through the device switch probing for things.
109          */
110         for (i = 0; devsw[i] != NULL; i++)
111                 if (devsw[i]->dv_init != NULL)
112                         (devsw[i]->dv_init)();
113
114         /* Get our loaded image protocol interface structure. */
115         BS->HandleProtocol(IH, &imgid, (VOID**)&img);
116
117         printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
118         printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
119             ST->Hdr.Revision & 0xffff);
120         printf("EFI Firmware: ");
121         /* printf doesn't understand EFI Unicode */
122         ST->ConOut->OutputString(ST->ConOut, ST->FirmwareVendor);
123         printf(" (rev %d.%02d)\n", ST->FirmwareRevision >> 16,
124             ST->FirmwareRevision & 0xffff);
125
126         printf("\n");
127         printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
128         printf("(%s, %s)\n", bootprog_maker, bootprog_date);
129
130         efi_handle_lookup(img->DeviceHandle, &currdev.d_dev, &currdev.d_unit);
131         currdev.d_type = currdev.d_dev->dv_type;
132
133         /*
134          * Disable the watchdog timer. By default the boot manager sets
135          * the timer to 5 minutes before invoking a boot option. If we
136          * want to return to the boot manager, we have to disable the
137          * watchdog timer and since we're an interactive program, we don't
138          * want to wait until the user types "quit". The timer may have
139          * fired by then. We don't care if this fails. It does not prevent
140          * normal functioning in any way...
141          */
142         BS->SetWatchdogTimer(0, 0, 0, NULL);
143
144         env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
145             efi_setcurrdev, env_nounset);
146         env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
147             env_nounset);
148
149         setenv("LINES", "24", 1);       /* optional */
150
151         archsw.arch_autoload = efi_autoload;
152         archsw.arch_getdev = efi_getdev;
153         archsw.arch_copyin = efi_copyin;
154         archsw.arch_copyout = efi_copyout;
155         archsw.arch_readin = efi_readin;
156
157         for (i = 0; i < ST->NumberOfTableEntries; i++) {
158                 guid = &ST->ConfigurationTable[i].VendorGuid;
159                 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) {
160                         smbios_detect(ST->ConfigurationTable[i].VendorTable);
161                         break;
162                 }
163         }
164
165         interact();                     /* doesn't return */
166
167         return (EFI_SUCCESS);           /* keep compiler happy */
168 }
169
170 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
171
172 static int
173 command_reboot(int argc, char *argv[])
174 {
175         int i;
176
177         for (i = 0; devsw[i] != NULL; ++i)
178                 if (devsw[i]->dv_cleanup != NULL)
179                         (devsw[i]->dv_cleanup)();
180
181         RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23,
182             (CHAR16 *)"Reboot from the loader");
183
184         /* NOTREACHED */
185         return (CMD_ERROR);
186 }
187
188 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
189
190 static int
191 command_quit(int argc, char *argv[])
192 {
193         exit(0);
194         return (CMD_OK);
195 }
196
197 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
198
199 static int
200 command_memmap(int argc, char *argv[])
201 {
202         UINTN sz;
203         EFI_MEMORY_DESCRIPTOR *map, *p;
204         UINTN key, dsz;
205         UINT32 dver;
206         EFI_STATUS status;
207         int i, ndesc;
208         static char *types[] = {
209             "Reserved",
210             "LoaderCode",
211             "LoaderData",
212             "BootServicesCode",
213             "BootServicesData",
214             "RuntimeServicesCode",
215             "RuntimeServicesData",
216             "ConventionalMemory",
217             "UnusableMemory",
218             "ACPIReclaimMemory",
219             "ACPIMemoryNVS",
220             "MemoryMappedIO",
221             "MemoryMappedIOPortSpace",
222             "PalCode"
223         };
224
225         sz = 0;
226         status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
227         if (status != EFI_BUFFER_TOO_SMALL) {
228                 printf("Can't determine memory map size\n");
229                 return CMD_ERROR;
230         }
231         map = malloc(sz);
232         status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
233         if (EFI_ERROR(status)) {
234                 printf("Can't read memory map\n");
235                 return CMD_ERROR;
236         }
237
238         ndesc = sz / dsz;
239         printf("%23s %12s %12s %8s %4s\n",
240                "Type", "Physical", "Virtual", "#Pages", "Attr");
241
242         for (i = 0, p = map; i < ndesc;
243              i++, p = NextMemoryDescriptor(p, dsz)) {
244             printf("%23s %012lx %012lx %08lx ",
245                    types[p->Type],
246                    p->PhysicalStart,
247                    p->VirtualStart,
248                    p->NumberOfPages);
249             if (p->Attribute & EFI_MEMORY_UC)
250                 printf("UC ");
251             if (p->Attribute & EFI_MEMORY_WC)
252                 printf("WC ");
253             if (p->Attribute & EFI_MEMORY_WT)
254                 printf("WT ");
255             if (p->Attribute & EFI_MEMORY_WB)
256                 printf("WB ");
257             if (p->Attribute & EFI_MEMORY_UCE)
258                 printf("UCE ");
259             if (p->Attribute & EFI_MEMORY_WP)
260                 printf("WP ");
261             if (p->Attribute & EFI_MEMORY_RP)
262                 printf("RP ");
263             if (p->Attribute & EFI_MEMORY_XP)
264                 printf("XP ");
265             printf("\n");
266         }
267
268         return CMD_OK;
269 }
270
271 COMMAND_SET(configuration, "configuration",
272             "print configuration tables", command_configuration);
273
274 static const char *
275 guid_to_string(EFI_GUID *guid)
276 {
277         static char buf[40];
278
279         sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
280             guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
281             guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
282             guid->Data4[5], guid->Data4[6], guid->Data4[7]);
283         return (buf);
284 }
285
286 static int
287 command_configuration(int argc, char *argv[])
288 {
289         int i;
290
291         printf("NumberOfTableEntries=%ld\n", ST->NumberOfTableEntries);
292         for (i = 0; i < ST->NumberOfTableEntries; i++) {
293                 EFI_GUID *guid;
294
295                 printf("  ");
296                 guid = &ST->ConfigurationTable[i].VendorGuid;
297                 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
298                         printf("MPS Table");
299                 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
300                         printf("ACPI Table");
301                 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
302                         printf("ACPI 2.0 Table");
303                 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
304                         printf("SMBIOS Table");
305                 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID)))
306                         printf("DXE Table");
307                 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID)))
308                         printf("HOB List Table");
309                 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID)))
310                         printf("Memory Type Information Table");
311                 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID)))
312                         printf("Debug Image Info Table");
313                 else
314                         printf("Unknown Table (%s)", guid_to_string(guid));
315                 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
316         }
317
318         return CMD_OK;
319 }
320
321
322 COMMAND_SET(mode, "mode", "change or display text modes", command_mode);
323
324 static int
325 command_mode(int argc, char *argv[])
326 {
327         UINTN cols, rows;
328         unsigned int mode;
329         int i;
330         char *cp;
331         char rowenv[8];
332         EFI_STATUS status;
333         SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
334         extern void HO(void);
335
336         conout = ST->ConOut;
337
338         if (argc > 1) {
339                 mode = strtol(argv[1], &cp, 0);
340                 if (cp[0] != '\0') {
341                         printf("Invalid mode\n");
342                         return (CMD_ERROR);
343                 }
344                 status = conout->QueryMode(conout, mode, &cols, &rows);
345                 if (EFI_ERROR(status)) {
346                         printf("invalid mode %d\n", mode);
347                         return (CMD_ERROR);
348                 }
349                 status = conout->SetMode(conout, mode);
350                 if (EFI_ERROR(status)) {
351                         printf("couldn't set mode %d\n", mode);
352                         return (CMD_ERROR);
353                 }
354                 sprintf(rowenv, "%u", (unsigned)rows);
355                 setenv("LINES", rowenv, 1);
356                 HO();           /* set cursor */
357                 return (CMD_OK);
358         }
359
360         printf("Current mode: %d\n", conout->Mode->Mode);
361         for (i = 0; i <= conout->Mode->MaxMode; i++) {
362                 status = conout->QueryMode(conout, i, &cols, &rows);
363                 if (EFI_ERROR(status))
364                         continue;
365                 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
366                     (unsigned)rows);
367         }
368
369         if (i != 0)
370                 printf("Choose the mode with \"col <mode number>\"\n");
371
372         return (CMD_OK);
373 }
374
375
376 COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram);
377
378 static int
379 command_nvram(int argc, char *argv[])
380 {
381         CHAR16 var[128];
382         CHAR16 *data;
383         EFI_STATUS status;
384         EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
385         UINTN varsz, datasz;
386         SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
387         int i;
388
389         conout = ST->ConOut;
390
391         /* Initiate the search */
392         status = RS->GetNextVariableName(&varsz, NULL, NULL);
393
394         for (; status != EFI_NOT_FOUND; ) {
395                 status = RS->GetNextVariableName(&varsz, var,
396                     &varguid);
397                 //if (EFI_ERROR(status))
398                         //break;
399
400                 conout->OutputString(conout, var);
401                 printf("=");
402                 datasz = 0;
403                 status = RS->GetVariable(var, &varguid, NULL, &datasz,
404                     NULL);
405                 /* XXX: check status */
406                 data = malloc(datasz);
407                 status = RS->GetVariable(var, &varguid, NULL, &datasz,
408                     data);
409                 if (EFI_ERROR(status))
410                         printf("<error retrieving variable>");
411                 else {
412                         for (i = 0; i < datasz; i++) {
413                                 if (isalnum(data[i]) || isspace(data[i]))
414                                         printf("%c", data[i]);
415                                 else
416                                         printf("\\x%02x", data[i]);
417                         }
418                 }
419                 /* XXX */
420                 pager_output("\n");
421                 free(data);
422         }
423
424         return (CMD_OK);
425 }