main.c revision 293303
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: stable/10/sys/boot/efi/loader/main.c 293303 2016-01-07 03:28:56Z emaste $"); 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 43extern char bootprog_name[]; 44extern char bootprog_rev[]; 45extern char bootprog_date[]; 46extern char bootprog_maker[]; 47 48struct devdesc currdev; /* our current device */ 49struct arch_switch archsw; /* MI/MD interface boundary */ 50 51EFI_GUID acpi = ACPI_TABLE_GUID; 52EFI_GUID acpi20 = ACPI_20_TABLE_GUID; 53EFI_GUID devid = DEVICE_PATH_PROTOCOL; 54EFI_GUID imgid = LOADED_IMAGE_PROTOCOL; 55EFI_GUID mps = MPS_TABLE_GUID; 56EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL; 57EFI_GUID smbios = SMBIOS_TABLE_GUID; 58EFI_GUID dxe = DXE_SERVICES_TABLE_GUID; 59EFI_GUID hoblist = HOB_LIST_TABLE_GUID; 60EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID; 61EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID; 62 63EFI_STATUS 64main(int argc, CHAR16 *argv[]) 65{ 66 char vendor[128]; 67 EFI_LOADED_IMAGE *img; 68 EFI_GUID *guid; 69 int i; 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 if (efi_copy_init()) { 80 printf("failed to allocate staging area\n"); 81 return (EFI_BUFFER_TOO_SMALL); 82 } 83 84 /* 85 * March through the device switch probing for things. 86 */ 87 for (i = 0; devsw[i] != NULL; i++) 88 if (devsw[i]->dv_init != NULL) 89 (devsw[i]->dv_init)(); 90 91 /* Get our loaded image protocol interface structure. */ 92 BS->HandleProtocol(IH, &imgid, (VOID**)&img); 93 94 printf("Image base: 0x%lx\n", (u_long)img->ImageBase); 95 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16, 96 ST->Hdr.Revision & 0xffff); 97 printf("EFI Firmware: "); 98 /* printf doesn't understand EFI Unicode */ 99 ST->ConOut->OutputString(ST->ConOut, ST->FirmwareVendor); 100 printf(" (rev %d.%02d)\n", ST->FirmwareRevision >> 16, 101 ST->FirmwareRevision & 0xffff); 102 103 printf("\n"); 104 printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 105 printf("(%s, %s)\n", bootprog_maker, bootprog_date); 106 107 efi_handle_lookup(img->DeviceHandle, &currdev.d_dev, &currdev.d_unit); 108 currdev.d_type = currdev.d_dev->dv_type; 109 110 /* 111 * Disable the watchdog timer. By default the boot manager sets 112 * the timer to 5 minutes before invoking a boot option. If we 113 * want to return to the boot manager, we have to disable the 114 * watchdog timer and since we're an interactive program, we don't 115 * want to wait until the user types "quit". The timer may have 116 * fired by then. We don't care if this fails. It does not prevent 117 * normal functioning in any way... 118 */ 119 BS->SetWatchdogTimer(0, 0, 0, NULL); 120 121 env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev), 122 efi_setcurrdev, env_nounset); 123 env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset, 124 env_nounset); 125 126 setenv("LINES", "24", 1); /* optional */ 127 128 archsw.arch_autoload = efi_autoload; 129 archsw.arch_getdev = efi_getdev; 130 archsw.arch_copyin = efi_copyin; 131 archsw.arch_copyout = efi_copyout; 132 archsw.arch_readin = efi_readin; 133 134 for (i = 0; i < ST->NumberOfTableEntries; i++) { 135 guid = &ST->ConfigurationTable[i].VendorGuid; 136 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) { 137 smbios_detect(ST->ConfigurationTable[i].VendorTable); 138 break; 139 } 140 } 141 142 interact(); /* doesn't return */ 143 144 return (EFI_SUCCESS); /* keep compiler happy */ 145} 146 147COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 148 149static int 150command_reboot(int argc, char *argv[]) 151{ 152 int i; 153 154 for (i = 0; devsw[i] != NULL; ++i) 155 if (devsw[i]->dv_cleanup != NULL) 156 (devsw[i]->dv_cleanup)(); 157 158 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23, 159 (CHAR16 *)"Reboot from the loader"); 160 161 /* NOTREACHED */ 162 return (CMD_ERROR); 163} 164 165COMMAND_SET(quit, "quit", "exit the loader", command_quit); 166 167static int 168command_quit(int argc, char *argv[]) 169{ 170 exit(0); 171 return (CMD_OK); 172} 173 174COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 175 176static int 177command_memmap(int argc, char *argv[]) 178{ 179 UINTN sz; 180 EFI_MEMORY_DESCRIPTOR *map, *p; 181 UINTN key, dsz; 182 UINT32 dver; 183 EFI_STATUS status; 184 int i, ndesc; 185 static char *types[] = { 186 "Reserved", 187 "LoaderCode", 188 "LoaderData", 189 "BootServicesCode", 190 "BootServicesData", 191 "RuntimeServicesCode", 192 "RuntimeServicesData", 193 "ConventionalMemory", 194 "UnusableMemory", 195 "ACPIReclaimMemory", 196 "ACPIMemoryNVS", 197 "MemoryMappedIO", 198 "MemoryMappedIOPortSpace", 199 "PalCode" 200 }; 201 202 sz = 0; 203 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); 204 if (status != EFI_BUFFER_TOO_SMALL) { 205 printf("Can't determine memory map size\n"); 206 return CMD_ERROR; 207 } 208 map = malloc(sz); 209 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); 210 if (EFI_ERROR(status)) { 211 printf("Can't read memory map\n"); 212 return CMD_ERROR; 213 } 214 215 ndesc = sz / dsz; 216 printf("%23s %12s %12s %8s %4s\n", 217 "Type", "Physical", "Virtual", "#Pages", "Attr"); 218 219 for (i = 0, p = map; i < ndesc; 220 i++, p = NextMemoryDescriptor(p, dsz)) { 221 printf("%23s %012lx %012lx %08lx ", 222 types[p->Type], 223 p->PhysicalStart, 224 p->VirtualStart, 225 p->NumberOfPages); 226 if (p->Attribute & EFI_MEMORY_UC) 227 printf("UC "); 228 if (p->Attribute & EFI_MEMORY_WC) 229 printf("WC "); 230 if (p->Attribute & EFI_MEMORY_WT) 231 printf("WT "); 232 if (p->Attribute & EFI_MEMORY_WB) 233 printf("WB "); 234 if (p->Attribute & EFI_MEMORY_UCE) 235 printf("UCE "); 236 if (p->Attribute & EFI_MEMORY_WP) 237 printf("WP "); 238 if (p->Attribute & EFI_MEMORY_RP) 239 printf("RP "); 240 if (p->Attribute & EFI_MEMORY_XP) 241 printf("XP "); 242 printf("\n"); 243 } 244 245 return CMD_OK; 246} 247 248COMMAND_SET(configuration, "configuration", 249 "print configuration tables", command_configuration); 250 251static const char * 252guid_to_string(EFI_GUID *guid) 253{ 254 static char buf[40]; 255 256 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 257 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], 258 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4], 259 guid->Data4[5], guid->Data4[6], guid->Data4[7]); 260 return (buf); 261} 262 263static int 264command_configuration(int argc, char *argv[]) 265{ 266 int i; 267 268 printf("NumberOfTableEntries=%ld\n", ST->NumberOfTableEntries); 269 for (i = 0; i < ST->NumberOfTableEntries; i++) { 270 EFI_GUID *guid; 271 272 printf(" "); 273 guid = &ST->ConfigurationTable[i].VendorGuid; 274 if (!memcmp(guid, &mps, sizeof(EFI_GUID))) 275 printf("MPS Table"); 276 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID))) 277 printf("ACPI Table"); 278 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID))) 279 printf("ACPI 2.0 Table"); 280 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) 281 printf("SMBIOS Table"); 282 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID))) 283 printf("DXE Table"); 284 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID))) 285 printf("HOB List Table"); 286 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID))) 287 printf("Memory Type Information Table"); 288 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID))) 289 printf("Debug Image Info Table"); 290 else 291 printf("Unknown Table (%s)", guid_to_string(guid)); 292 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable); 293 } 294 295 return CMD_OK; 296} 297 298 299COMMAND_SET(mode, "mode", "change or display text modes", command_mode); 300 301static int 302command_mode(int argc, char *argv[]) 303{ 304 UINTN cols, rows; 305 unsigned int mode; 306 int i; 307 char *cp; 308 char rowenv[8]; 309 EFI_STATUS status; 310 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 311 312 conout = ST->ConOut; 313 314 if (argc > 1) { 315 mode = strtol(argv[1], &cp, 0); 316 if (cp[0] != '\0') { 317 printf("Invalid mode\n"); 318 return (CMD_ERROR); 319 } 320 status = conout->QueryMode(conout, mode, &cols, &rows); 321 if (EFI_ERROR(status)) { 322 printf("invalid mode %d\n", mode); 323 return (CMD_ERROR); 324 } 325 status = conout->SetMode(conout, mode); 326 if (EFI_ERROR(status)) { 327 printf("couldn't set mode %d\n", mode); 328 return (CMD_ERROR); 329 } 330 sprintf(rowenv, "%u", (unsigned)rows); 331 setenv("LINES", rowenv, 1); 332 333 return (CMD_OK); 334 } 335 336 for (i = 0; ; i++) { 337 status = conout->QueryMode(conout, i, &cols, &rows); 338 if (EFI_ERROR(status)) 339 break; 340 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols, 341 (unsigned)rows); 342 } 343 344 if (i != 0) 345 printf("Choose the mode with \"col <mode number>\"\n"); 346 347 return (CMD_OK); 348} 349 350 351COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram); 352 353static int 354command_nvram(int argc, char *argv[]) 355{ 356 CHAR16 var[128]; 357 CHAR16 *data; 358 EFI_STATUS status; 359 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} }; 360 UINTN varsz, datasz; 361 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 362 int i; 363 364 conout = ST->ConOut; 365 366 /* Initiate the search */ 367 status = RS->GetNextVariableName(&varsz, NULL, NULL); 368 369 for (; status != EFI_NOT_FOUND; ) { 370 status = RS->GetNextVariableName(&varsz, var, 371 &varguid); 372 //if (EFI_ERROR(status)) 373 //break; 374 375 conout->OutputString(conout, var); 376 printf("="); 377 datasz = 0; 378 status = RS->GetVariable(var, &varguid, NULL, &datasz, 379 NULL); 380 /* XXX: check status */ 381 data = malloc(datasz); 382 status = RS->GetVariable(var, &varguid, NULL, &datasz, 383 data); 384 if (EFI_ERROR(status)) 385 printf("<error retrieving variable>"); 386 else { 387 for (i = 0; i < datasz; i++) { 388 if (isalnum(data[i]) || isspace(data[i])) 389 printf("%c", data[i]); 390 else 391 printf("\\x%02x", data[i]); 392 } 393 } 394 /* XXX */ 395 pager_output("\n"); 396 free(data); 397 } 398 399 return (CMD_OK); 400} 401