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>
39 #include "../libi386/libi386.h"
41 extern char bootprog_name[];
42 extern char bootprog_rev[];
43 extern char bootprog_date[];
44 extern char bootprog_maker[];
46 struct devdesc currdev; /* our current device */
47 struct arch_switch archsw; /* MI/MD interface boundary */
49 EFI_GUID acpi = ACPI_TABLE_GUID;
50 EFI_GUID acpi20 = ACPI_20_TABLE_GUID;
51 EFI_GUID devid = DEVICE_PATH_PROTOCOL;
52 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
53 EFI_GUID mps = MPS_TABLE_GUID;
54 EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
55 EFI_GUID smbios = SMBIOS_TABLE_GUID;
58 main(int argc, CHAR16 *argv[])
61 EFI_LOADED_IMAGE *img;
65 * XXX Chicken-and-egg problem; we want to have console output
66 * early, but some console attributes may depend on reading from
67 * eg. the boot device, which we can't do yet. We can use
68 * printf() etc. once this is done.
73 * March through the device switch probing for things.
75 for (i = 0; devsw[i] != NULL; i++)
76 if (devsw[i]->dv_init != NULL)
77 (devsw[i]->dv_init)();
79 /* Get our loaded image protocol interface structure. */
80 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
82 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
83 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
84 ST->Hdr.Revision & 0xffff);
85 printf("EFI Firmware: ");
86 /* printf doesn't understand EFI Unicode */
87 ST->ConOut->OutputString(ST->ConOut, ST->FirmwareVendor);
88 printf(" (rev %d.%02d)\n", ST->FirmwareRevision >> 16,
89 ST->FirmwareRevision & 0xffff);
92 printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
93 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
95 efi_handle_lookup(img->DeviceHandle, &currdev.d_dev, &currdev.d_unit);
96 currdev.d_type = currdev.d_dev->dv_type;
99 * Disable the watchdog timer. By default the boot manager sets
100 * the timer to 5 minutes before invoking a boot option. If we
101 * want to return to the boot manager, we have to disable the
102 * watchdog timer and since we're an interactive program, we don't
103 * want to wait until the user types "quit". The timer may have
104 * fired by then. We don't care if this fails. It does not prevent
105 * normal functioning in any way...
107 BS->SetWatchdogTimer(0, 0, 0, NULL);
109 env_setenv("currdev", EV_VOLATILE, i386_fmtdev(&currdev),
110 i386_setcurrdev, env_nounset);
111 env_setenv("loaddev", EV_VOLATILE, i386_fmtdev(&currdev), env_noset,
114 setenv("LINES", "24", 1); /* optional */
116 archsw.arch_autoload = i386_autoload;
117 archsw.arch_getdev = i386_getdev;
118 archsw.arch_copyin = i386_copyin;
119 archsw.arch_copyout = i386_copyout;
120 archsw.arch_readin = i386_readin;
122 interact(); /* doesn't return */
124 return (EFI_SUCCESS); /* keep compiler happy */
127 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
130 command_reboot(int argc, char *argv[])
134 for (i = 0; devsw[i] != NULL; ++i)
135 if (devsw[i]->dv_cleanup != NULL)
136 (devsw[i]->dv_cleanup)();
138 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23,
139 (CHAR16 *)"Reboot from the loader");
145 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
148 command_quit(int argc, char *argv[])
154 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
157 command_memmap(int argc, char *argv[])
160 EFI_MEMORY_DESCRIPTOR *map, *p;
165 static char *types[] = {
171 "RuntimeServicesCode",
172 "RuntimeServicesData",
173 "ConventionalMemory",
178 "MemoryMappedIOPortSpace",
183 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
184 if (status != EFI_BUFFER_TOO_SMALL) {
185 printf("Can't determine memory map size\n");
189 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
190 if (EFI_ERROR(status)) {
191 printf("Can't read memory map\n");
196 printf("%23s %12s %12s %8s %4s\n",
197 "Type", "Physical", "Virtual", "#Pages", "Attr");
199 for (i = 0, p = map; i < ndesc;
200 i++, p = NextMemoryDescriptor(p, dsz)) {
201 printf("%23s %012lx %012lx %08lx ",
206 if (p->Attribute & EFI_MEMORY_UC)
208 if (p->Attribute & EFI_MEMORY_WC)
210 if (p->Attribute & EFI_MEMORY_WT)
212 if (p->Attribute & EFI_MEMORY_WB)
214 if (p->Attribute & EFI_MEMORY_UCE)
216 if (p->Attribute & EFI_MEMORY_WP)
218 if (p->Attribute & EFI_MEMORY_RP)
220 if (p->Attribute & EFI_MEMORY_XP)
228 COMMAND_SET(configuration, "configuration",
229 "print configuration tables", command_configuration);
232 guid_to_string(EFI_GUID *guid)
236 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
237 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
238 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
239 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
244 command_configuration(int argc, char *argv[])
248 printf("NumberOfTableEntries=%ld\n", ST->NumberOfTableEntries);
249 for (i = 0; i < ST->NumberOfTableEntries; i++) {
253 guid = &ST->ConfigurationTable[i].VendorGuid;
254 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
256 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
257 printf("ACPI Table");
258 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
259 printf("ACPI 2.0 Table");
260 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
261 printf("SMBIOS Table");
263 printf("Unknown Table (%s)", guid_to_string(guid));
264 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
271 COMMAND_SET(mode, "mode", "change or display text modes", command_mode);
274 command_mode(int argc, char *argv[])
276 unsigned int cols, rows, mode;
281 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
286 mode = strtol(argv[1], &cp, 0);
288 printf("Invalid mode\n");
291 status = conout->QueryMode(conout, mode, &cols, &rows);
292 if (EFI_ERROR(status)) {
293 printf("invalid mode %d\n", mode);
296 status = conout->SetMode(conout, mode);
297 if (EFI_ERROR(status)) {
298 printf("couldn't set mode %d\n", mode);
301 sprintf(rowenv, "%d", rows);
302 setenv("LINES", rowenv, 1);
308 status = conout->QueryMode(conout, i, &cols, &rows);
309 if (EFI_ERROR(status))
311 printf("Mode %d: %d columns, %d rows\n", i, cols, rows);
315 printf("Choose the mode with \"col <mode number>\"\n");
321 COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram);
324 command_nvram(int argc, char *argv[])
329 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
332 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
337 /* Initiate the search */
338 status = RS->GetNextVariableName(&varsz, NULL, NULL);
340 for (; status != EFI_NOT_FOUND; ) {
341 status = RS->GetNextVariableName(&varsz, var,
343 //if (EFI_ERROR(status))
346 conout->OutputString(conout, var);
349 status = RS->GetVariable(var, &varguid, NULL, &datasz,
351 /* XXX: check status */
352 data = malloc(datasz);
353 status = RS->GetVariable(var, &varguid, NULL, &datasz,
355 if (EFI_ERROR(status))
356 printf("<error retrieving variable>");
358 for (i = 0; i < datasz; i++) {
359 if (isalnum(data[i]) || isspace(data[i]))
360 printf("%c", data[i]);
362 printf("\\x%02x", data[i]);