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