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