main.c revision 294345
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 294345 2016-01-19 21:39:21Z ian $");
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 ",
245		   types[p->Type],
246		   p->PhysicalStart,
247		   p->VirtualStart,
248		   p->NumberOfPages);
249	    if (p->Attribute & EFI_MEMORY_UC)
250		printf("UC ");
251	    if (p->Attribute & EFI_MEMORY_WC)
252		printf("WC ");
253	    if (p->Attribute & EFI_MEMORY_WT)
254		printf("WT ");
255	    if (p->Attribute & EFI_MEMORY_WB)
256		printf("WB ");
257	    if (p->Attribute & EFI_MEMORY_UCE)
258		printf("UCE ");
259	    if (p->Attribute & EFI_MEMORY_WP)
260		printf("WP ");
261	    if (p->Attribute & EFI_MEMORY_RP)
262		printf("RP ");
263	    if (p->Attribute & EFI_MEMORY_XP)
264		printf("XP ");
265	    printf("\n");
266	}
267
268	return CMD_OK;
269}
270
271COMMAND_SET(configuration, "configuration",
272	    "print configuration tables", command_configuration);
273
274static const char *
275guid_to_string(EFI_GUID *guid)
276{
277	static char buf[40];
278
279	sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
280	    guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
281	    guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
282	    guid->Data4[5], guid->Data4[6], guid->Data4[7]);
283	return (buf);
284}
285
286static int
287command_configuration(int argc, char *argv[])
288{
289	int i;
290
291	printf("NumberOfTableEntries=%ld\n", 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
335	conout = ST->ConOut;
336
337	if (argc > 1) {
338		mode = strtol(argv[1], &cp, 0);
339		if (cp[0] != '\0') {
340			printf("Invalid mode\n");
341			return (CMD_ERROR);
342		}
343		status = conout->QueryMode(conout, mode, &cols, &rows);
344		if (EFI_ERROR(status)) {
345			printf("invalid mode %d\n", mode);
346			return (CMD_ERROR);
347		}
348		status = conout->SetMode(conout, mode);
349		if (EFI_ERROR(status)) {
350			printf("couldn't set mode %d\n", mode);
351			return (CMD_ERROR);
352		}
353		sprintf(rowenv, "%u", (unsigned)rows);
354		setenv("LINES", rowenv, 1);
355
356		return (CMD_OK);
357	}
358
359	printf("Current mode: %d\n", conout->Mode->Mode);
360	for (i = 0; i <= conout->Mode->MaxMode; i++) {
361		status = conout->QueryMode(conout, i, &cols, &rows);
362		if (EFI_ERROR(status))
363			continue;
364		printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
365		    (unsigned)rows);
366	}
367
368	if (i != 0)
369		printf("Choose the mode with \"col <mode number>\"\n");
370
371	return (CMD_OK);
372}
373
374
375COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram);
376
377static int
378command_nvram(int argc, char *argv[])
379{
380	CHAR16 var[128];
381	CHAR16 *data;
382	EFI_STATUS status;
383	EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
384	UINTN varsz, datasz;
385	SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
386	int i;
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,
395		    &varguid);
396		//if (EFI_ERROR(status))
397			//break;
398
399		conout->OutputString(conout, var);
400		printf("=");
401		datasz = 0;
402		status = RS->GetVariable(var, &varguid, NULL, &datasz,
403		    NULL);
404		/* XXX: check status */
405		data = malloc(datasz);
406		status = RS->GetVariable(var, &varguid, NULL, &datasz,
407		    data);
408		if (EFI_ERROR(status))
409			printf("<error retrieving variable>");
410		else {
411			for (i = 0; i < datasz; i++) {
412				if (isalnum(data[i]) || isspace(data[i]))
413					printf("%c", data[i]);
414				else
415					printf("\\x%02x", data[i]);
416			}
417		}
418		/* XXX */
419		pager_output("\n");
420		free(data);
421	}
422
423	return (CMD_OK);
424}
425