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