main.c revision 294981
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 294981 2016-01-28 12:11:42Z smh $"); 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 UINTN k; 71 72 /* 73 * XXX Chicken-and-egg problem; we want to have console output 74 * early, but some console attributes may depend on reading from 75 * eg. the boot device, which we can't do yet. We can use 76 * printf() etc. once this is done. 77 */ 78 cons_probe(); 79 80 /* 81 * Loop through the args, and for each one that contains an '=' that is 82 * not the first character, add it to the environment. This allows 83 * loader and kernel env vars to be passed on the command line. Convert 84 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied. 85 */ 86 for (i = 1; i < argc; i++) { 87 vargood = 0; 88 for (j = 0; argv[i][j] != 0; j++) { 89 if (j == sizeof(var)) { 90 vargood = 0; 91 break; 92 } 93 if (j > 0 && argv[i][j] == '=') 94 vargood = 1; 95 var[j] = (char)argv[i][j]; 96 } 97 if (vargood) { 98 var[j] = 0; 99 putenv(var); 100 } 101 } 102 103 if (efi_copy_init()) { 104 printf("failed to allocate staging area\n"); 105 return (EFI_BUFFER_TOO_SMALL); 106 } 107 108 /* 109 * March through the device switch probing for things. 110 */ 111 for (i = 0; devsw[i] != NULL; i++) 112 if (devsw[i]->dv_init != NULL) 113 (devsw[i]->dv_init)(); 114 115 /* Get our loaded image protocol interface structure. */ 116 BS->HandleProtocol(IH, &imgid, (VOID**)&img); 117 118 printf("Image base: 0x%lx\n", (u_long)img->ImageBase); 119 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16, 120 ST->Hdr.Revision & 0xffff); 121 printf("EFI Firmware: "); 122 /* printf doesn't understand EFI Unicode */ 123 ST->ConOut->OutputString(ST->ConOut, ST->FirmwareVendor); 124 printf(" (rev %d.%02d)\n", ST->FirmwareRevision >> 16, 125 ST->FirmwareRevision & 0xffff); 126 127 printf("\n"); 128 printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 129 printf("(%s, %s)\n", bootprog_maker, bootprog_date); 130 131 efi_handle_lookup(img->DeviceHandle, &currdev.d_dev, &currdev.d_unit); 132 currdev.d_type = currdev.d_dev->dv_type; 133 134 /* 135 * Disable the watchdog timer. By default the boot manager sets 136 * the timer to 5 minutes before invoking a boot option. If we 137 * want to return to the boot manager, we have to disable the 138 * watchdog timer and since we're an interactive program, we don't 139 * want to wait until the user types "quit". The timer may have 140 * fired by then. We don't care if this fails. It does not prevent 141 * normal functioning in any way... 142 */ 143 BS->SetWatchdogTimer(0, 0, 0, NULL); 144 145 env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev), 146 efi_setcurrdev, env_nounset); 147 env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset, 148 env_nounset); 149 150 setenv("LINES", "24", 1); /* optional */ 151 152 archsw.arch_autoload = efi_autoload; 153 archsw.arch_getdev = efi_getdev; 154 archsw.arch_copyin = efi_copyin; 155 archsw.arch_copyout = efi_copyout; 156 archsw.arch_readin = efi_readin; 157 158 for (k = 0; k < ST->NumberOfTableEntries; k++) { 159 guid = &ST->ConfigurationTable[k].VendorGuid; 160 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) { 161 smbios_detect(ST->ConfigurationTable[k].VendorTable); 162 break; 163 } 164 } 165 166 interact(); /* doesn't return */ 167 168 return (EFI_SUCCESS); /* keep compiler happy */ 169} 170 171COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 172 173static int 174command_reboot(int argc, char *argv[]) 175{ 176 int i; 177 178 for (i = 0; devsw[i] != NULL; ++i) 179 if (devsw[i]->dv_cleanup != NULL) 180 (devsw[i]->dv_cleanup)(); 181 182 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23, 183 (CHAR16 *)"Reboot from the loader"); 184 185 /* NOTREACHED */ 186 return (CMD_ERROR); 187} 188 189COMMAND_SET(quit, "quit", "exit the loader", command_quit); 190 191static int 192command_quit(int argc, char *argv[]) 193{ 194 exit(0); 195 return (CMD_OK); 196} 197 198COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 199 200static int 201command_memmap(int argc, char *argv[]) 202{ 203 UINTN sz; 204 EFI_MEMORY_DESCRIPTOR *map, *p; 205 UINTN key, dsz; 206 UINT32 dver; 207 EFI_STATUS status; 208 int i, ndesc; 209 static char *types[] = { 210 "Reserved", 211 "LoaderCode", 212 "LoaderData", 213 "BootServicesCode", 214 "BootServicesData", 215 "RuntimeServicesCode", 216 "RuntimeServicesData", 217 "ConventionalMemory", 218 "UnusableMemory", 219 "ACPIReclaimMemory", 220 "ACPIMemoryNVS", 221 "MemoryMappedIO", 222 "MemoryMappedIOPortSpace", 223 "PalCode" 224 }; 225 226 sz = 0; 227 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); 228 if (status != EFI_BUFFER_TOO_SMALL) { 229 printf("Can't determine memory map size\n"); 230 return (CMD_ERROR); 231 } 232 map = malloc(sz); 233 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); 234 if (EFI_ERROR(status)) { 235 printf("Can't read memory map\n"); 236 return (CMD_ERROR); 237 } 238 239 ndesc = sz / dsz; 240 printf("%23s %12s %12s %8s %4s\n", 241 "Type", "Physical", "Virtual", "#Pages", "Attr"); 242 243 for (i = 0, p = map; i < ndesc; 244 i++, p = NextMemoryDescriptor(p, dsz)) { 245 printf("%23s %012jx %012jx %08jx ", types[p->Type], 246 (uintmax_t)p->PhysicalStart, (uintmax_t)p->VirtualStart, 247 (uintmax_t)p->NumberOfPages); 248 if (p->Attribute & EFI_MEMORY_UC) 249 printf("UC "); 250 if (p->Attribute & EFI_MEMORY_WC) 251 printf("WC "); 252 if (p->Attribute & EFI_MEMORY_WT) 253 printf("WT "); 254 if (p->Attribute & EFI_MEMORY_WB) 255 printf("WB "); 256 if (p->Attribute & EFI_MEMORY_UCE) 257 printf("UCE "); 258 if (p->Attribute & EFI_MEMORY_WP) 259 printf("WP "); 260 if (p->Attribute & EFI_MEMORY_RP) 261 printf("RP "); 262 if (p->Attribute & EFI_MEMORY_XP) 263 printf("XP "); 264 printf("\n"); 265 } 266 267 return (CMD_OK); 268} 269 270COMMAND_SET(configuration, "configuration", "print configuration tables", 271 command_configuration); 272 273static const char * 274guid_to_string(EFI_GUID *guid) 275{ 276 static char buf[40]; 277 278 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 279 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], 280 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4], 281 guid->Data4[5], guid->Data4[6], guid->Data4[7]); 282 return (buf); 283} 284 285static int 286command_configuration(int argc, char *argv[]) 287{ 288 UINTN i; 289 290 printf("NumberOfTableEntries=%lu\n", 291 (unsigned long)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 322COMMAND_SET(mode, "mode", "change or display text modes", command_mode); 323 324static int 325command_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 376COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram); 377 378static int 379command_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, i; 386 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 387 388 conout = ST->ConOut; 389 390 /* Initiate the search */ 391 status = RS->GetNextVariableName(&varsz, NULL, NULL); 392 393 for (; status != EFI_NOT_FOUND; ) { 394 status = RS->GetNextVariableName(&varsz, var, &varguid); 395 //if (EFI_ERROR(status)) 396 //break; 397 398 conout->OutputString(conout, var); 399 printf("="); 400 datasz = 0; 401 status = RS->GetVariable(var, &varguid, NULL, &datasz, NULL); 402 /* XXX: check status */ 403 data = malloc(datasz); 404 status = RS->GetVariable(var, &varguid, NULL, &datasz, data); 405 if (EFI_ERROR(status)) 406 printf("<error retrieving variable>"); 407 else { 408 for (i = 0; i < datasz; i++) { 409 if (isalnum(data[i]) || isspace(data[i])) 410 printf("%c", data[i]); 411 else 412 printf("\\x%02x", data[i]); 413 } 414 } 415 /* XXX */ 416 pager_output("\n"); 417 free(data); 418 } 419 420 return (CMD_OK); 421} 422