main.c revision 294449
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 294449 2016-01-20 17:38:21Z 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 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 170COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 171 172static int 173command_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 188COMMAND_SET(quit, "quit", "exit the loader", command_quit); 189 190static int 191command_quit(int argc, char *argv[]) 192{ 193 exit(0); 194 return (CMD_OK); 195} 196 197COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 198 199static int 200command_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 ", types[p->Type], 245 p->PhysicalStart, p->VirtualStart, p->NumberOfPages); 246 if (p->Attribute & EFI_MEMORY_UC) 247 printf("UC "); 248 if (p->Attribute & EFI_MEMORY_WC) 249 printf("WC "); 250 if (p->Attribute & EFI_MEMORY_WT) 251 printf("WT "); 252 if (p->Attribute & EFI_MEMORY_WB) 253 printf("WB "); 254 if (p->Attribute & EFI_MEMORY_UCE) 255 printf("UCE "); 256 if (p->Attribute & EFI_MEMORY_WP) 257 printf("WP "); 258 if (p->Attribute & EFI_MEMORY_RP) 259 printf("RP "); 260 if (p->Attribute & EFI_MEMORY_XP) 261 printf("XP "); 262 printf("\n"); 263 } 264 265 return (CMD_OK); 266} 267 268COMMAND_SET(configuration, "configuration", "print configuration tables", 269 command_configuration); 270 271static const char * 272guid_to_string(EFI_GUID *guid) 273{ 274 static char buf[40]; 275 276 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 277 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], 278 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4], 279 guid->Data4[5], guid->Data4[6], guid->Data4[7]); 280 return (buf); 281} 282 283static int 284command_configuration(int argc, char *argv[]) 285{ 286 int i; 287 288 printf("NumberOfTableEntries=%ld\n", ST->NumberOfTableEntries); 289 for (i = 0; i < ST->NumberOfTableEntries; i++) { 290 EFI_GUID *guid; 291 292 printf(" "); 293 guid = &ST->ConfigurationTable[i].VendorGuid; 294 if (!memcmp(guid, &mps, sizeof(EFI_GUID))) 295 printf("MPS Table"); 296 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID))) 297 printf("ACPI Table"); 298 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID))) 299 printf("ACPI 2.0 Table"); 300 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) 301 printf("SMBIOS Table"); 302 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID))) 303 printf("DXE Table"); 304 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID))) 305 printf("HOB List Table"); 306 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID))) 307 printf("Memory Type Information Table"); 308 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID))) 309 printf("Debug Image Info Table"); 310 else 311 printf("Unknown Table (%s)", guid_to_string(guid)); 312 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable); 313 } 314 315 return (CMD_OK); 316} 317 318 319COMMAND_SET(mode, "mode", "change or display text modes", command_mode); 320 321static int 322command_mode(int argc, char *argv[]) 323{ 324 UINTN cols, rows; 325 unsigned int mode; 326 int i; 327 char *cp; 328 char rowenv[8]; 329 EFI_STATUS status; 330 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 331 extern void HO(void); 332 333 conout = ST->ConOut; 334 335 if (argc > 1) { 336 mode = strtol(argv[1], &cp, 0); 337 if (cp[0] != '\0') { 338 printf("Invalid mode\n"); 339 return (CMD_ERROR); 340 } 341 status = conout->QueryMode(conout, mode, &cols, &rows); 342 if (EFI_ERROR(status)) { 343 printf("invalid mode %d\n", mode); 344 return (CMD_ERROR); 345 } 346 status = conout->SetMode(conout, mode); 347 if (EFI_ERROR(status)) { 348 printf("couldn't set mode %d\n", mode); 349 return (CMD_ERROR); 350 } 351 sprintf(rowenv, "%u", (unsigned)rows); 352 setenv("LINES", rowenv, 1); 353 HO(); /* set cursor */ 354 return (CMD_OK); 355 } 356 357 printf("Current mode: %d\n", conout->Mode->Mode); 358 for (i = 0; i <= conout->Mode->MaxMode; i++) { 359 status = conout->QueryMode(conout, i, &cols, &rows); 360 if (EFI_ERROR(status)) 361 continue; 362 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols, 363 (unsigned)rows); 364 } 365 366 if (i != 0) 367 printf("Choose the mode with \"col <mode number>\"\n"); 368 369 return (CMD_OK); 370} 371 372 373COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram); 374 375static int 376command_nvram(int argc, char *argv[]) 377{ 378 CHAR16 var[128]; 379 CHAR16 *data; 380 EFI_STATUS status; 381 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} }; 382 UINTN varsz, datasz; 383 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 384 int i; 385 386 conout = ST->ConOut; 387 388 /* Initiate the search */ 389 status = RS->GetNextVariableName(&varsz, NULL, NULL); 390 391 for (; status != EFI_NOT_FOUND; ) { 392 status = RS->GetNextVariableName(&varsz, var, &varguid); 393 //if (EFI_ERROR(status)) 394 //break; 395 396 conout->OutputString(conout, var); 397 printf("="); 398 datasz = 0; 399 status = RS->GetVariable(var, &varguid, NULL, &datasz, NULL); 400 /* XXX: check status */ 401 data = malloc(datasz); 402 status = RS->GetVariable(var, &varguid, NULL, &datasz, data); 403 if (EFI_ERROR(status)) 404 printf("<error retrieving variable>"); 405 else { 406 for (i = 0; i < datasz; i++) { 407 if (isalnum(data[i]) || isspace(data[i])) 408 printf("%c", data[i]); 409 else 410 printf("\\x%02x", data[i]); 411 } 412 } 413 /* XXX */ 414 pager_output("\n"); 415 free(data); 416 } 417 418 return (CMD_OK); 419} 420