2 * Copyright (c) 2008-2010 Rui Paulo
3 * Copyright (c) 2006 Marcel Moolenaar
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
38 #include <bootstrap.h>
41 #include "loader_efi.h"
43 extern char bootprog_name[];
44 extern char bootprog_rev[];
45 extern char bootprog_date[];
46 extern char bootprog_maker[];
48 struct devdesc currdev; /* our current device */
49 struct arch_switch archsw; /* MI/MD interface boundary */
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;
64 main(int argc, CHAR16 *argv[])
67 EFI_LOADED_IMAGE *img;
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.
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.
85 for (i = 1; i < argc; i++) {
87 for (j = 0; argv[i][j] != 0; j++) {
88 if (j == sizeof(var)) {
92 if (j > 0 && argv[i][j] == '=')
94 var[j] = (char)argv[i][j];
102 if (efi_copy_init()) {
103 printf("failed to allocate staging area\n");
104 return (EFI_BUFFER_TOO_SMALL);
108 * March through the device switch probing for things.
110 for (i = 0; devsw[i] != NULL; i++)
111 if (devsw[i]->dv_init != NULL)
112 (devsw[i]->dv_init)();
114 /* Get our loaded image protocol interface structure. */
115 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
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);
127 printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
128 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
130 efi_handle_lookup(img->DeviceHandle, &currdev.d_dev, &currdev.d_unit);
131 currdev.d_type = currdev.d_dev->dv_type;
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...
142 BS->SetWatchdogTimer(0, 0, 0, NULL);
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,
149 setenv("LINES", "24", 1); /* optional */
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;
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);
165 interact(); /* doesn't return */
167 return (EFI_SUCCESS); /* keep compiler happy */
170 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
173 command_reboot(int argc, char *argv[])
177 for (i = 0; devsw[i] != NULL; ++i)
178 if (devsw[i]->dv_cleanup != NULL)
179 (devsw[i]->dv_cleanup)();
181 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23,
182 (CHAR16 *)"Reboot from the loader");
188 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
191 command_quit(int argc, char *argv[])
197 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
200 command_memmap(int argc, char *argv[])
203 EFI_MEMORY_DESCRIPTOR *map, *p;
208 static char *types[] = {
214 "RuntimeServicesCode",
215 "RuntimeServicesData",
216 "ConventionalMemory",
221 "MemoryMappedIOPortSpace",
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");
232 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
233 if (EFI_ERROR(status)) {
234 printf("Can't read memory map\n");
239 printf("%23s %12s %12s %8s %4s\n",
240 "Type", "Physical", "Virtual", "#Pages", "Attr");
242 for (i = 0, p = map; i < ndesc;
243 i++, p = NextMemoryDescriptor(p, dsz)) {
244 printf("%23s %012lx %012lx %08lx ",
249 if (p->Attribute & EFI_MEMORY_UC)
251 if (p->Attribute & EFI_MEMORY_WC)
253 if (p->Attribute & EFI_MEMORY_WT)
255 if (p->Attribute & EFI_MEMORY_WB)
257 if (p->Attribute & EFI_MEMORY_UCE)
259 if (p->Attribute & EFI_MEMORY_WP)
261 if (p->Attribute & EFI_MEMORY_RP)
263 if (p->Attribute & EFI_MEMORY_XP)
271 COMMAND_SET(configuration, "configuration",
272 "print configuration tables", command_configuration);
275 guid_to_string(EFI_GUID *guid)
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]);
287 command_configuration(int argc, char *argv[])
291 printf("NumberOfTableEntries=%ld\n", ST->NumberOfTableEntries);
292 for (i = 0; i < ST->NumberOfTableEntries; i++) {
296 guid = &ST->ConfigurationTable[i].VendorGuid;
297 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
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)))
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");
314 printf("Unknown Table (%s)", guid_to_string(guid));
315 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
322 COMMAND_SET(mode, "mode", "change or display text modes", command_mode);
325 command_mode(int argc, char *argv[])
333 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
334 extern void HO(void);
339 mode = strtol(argv[1], &cp, 0);
341 printf("Invalid mode\n");
344 status = conout->QueryMode(conout, mode, &cols, &rows);
345 if (EFI_ERROR(status)) {
346 printf("invalid mode %d\n", mode);
349 status = conout->SetMode(conout, mode);
350 if (EFI_ERROR(status)) {
351 printf("couldn't set mode %d\n", mode);
354 sprintf(rowenv, "%u", (unsigned)rows);
355 setenv("LINES", rowenv, 1);
356 HO(); /* set cursor */
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))
365 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
370 printf("Choose the mode with \"col <mode number>\"\n");
376 COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram);
379 command_nvram(int argc, char *argv[])
384 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
386 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
391 /* Initiate the search */
392 status = RS->GetNextVariableName(&varsz, NULL, NULL);
394 for (; status != EFI_NOT_FOUND; ) {
395 status = RS->GetNextVariableName(&varsz, var,
397 //if (EFI_ERROR(status))
400 conout->OutputString(conout, var);
403 status = RS->GetVariable(var, &varguid, NULL, &datasz,
405 /* XXX: check status */
406 data = malloc(datasz);
407 status = RS->GetVariable(var, &varguid, NULL, &datasz,
409 if (EFI_ERROR(status))
410 printf("<error retrieving variable>");
412 for (i = 0; i < datasz; i++) {
413 if (isalnum(data[i]) || isspace(data[i]))
414 printf("%c", data[i]);
416 printf("\\x%02x", data[i]);