main.c revision 83216
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 1998,2000 Doug Rabson <dfr@freebsd.org>
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 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#ifndef lint
29static const char rcsid[] =
30  "$FreeBSD: head/sys/boot/ia64/efi/main.c 83216 2001-09-08 12:32:12Z dfr $";
31#endif /* not lint */
32
33#include <stand.h>
34#include <string.h>
35#include <setjmp.h>
36
37#include <efi.h>
38#include <efilib.h>
39
40#include "bootstrap.h"
41#include "efiboot.h"
42
43extern char bootprog_name[];
44extern char bootprog_rev[];
45extern char bootprog_date[];
46extern char bootprog_maker[];
47
48struct efi_devdesc	currdev;	/* our current device */
49struct arch_switch	archsw;		/* MI/MD interface boundary */
50
51EFI_STATUS
52efi_main (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *system_table)
53{
54	int i;
55	EFI_PHYSICAL_ADDRESS mem;
56
57	efi_init(image_handle, system_table);
58
59	/*
60	 * Initialise the heap as early as possible.  Once this is done,
61	 * alloc() is usable. The stack is buried inside us, so this is
62	 * safe.
63	 */
64	BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
65			  512*1024/4096, &mem);
66	setheap((void *)mem, (void *)(mem + 512*1024));
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	/*
77	 * Initialise the block cache
78	 */
79	bcache_init(32, 512);		/* 16k XXX tune this */
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	printf("\n");
89	printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
90	printf("(%s, %s)\n", bootprog_maker, bootprog_date);
91#if 0
92	printf("Memory: %ld k\n", memsize() / 1024);
93#endif
94
95	/* XXX presumes that biosdisk is first in devsw */
96	currdev.d_dev = devsw[0];
97	currdev.d_type = currdev.d_dev->dv_type;
98	currdev.d_kind.efidisk.unit = 0;
99	/* XXX should be able to detect this, default to autoprobe */
100	currdev.d_kind.efidisk.slice = -1;
101	/* default to 'a' */
102	currdev.d_kind.efidisk.partition = 0;
103
104#if 0
105	/* Create arc-specific variables */
106	bootfile = GetEnvironmentVariable(ARCENV_BOOTFILE);
107	if (bootfile)
108		setenv("bootfile", bootfile, 1);
109#endif
110
111	env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
112	    efi_setcurrdev, env_nounset);
113	env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
114	    env_nounset);
115
116	setenv("LINES", "24", 1);	/* optional */
117
118	archsw.arch_autoload = efi_autoload;
119	archsw.arch_getdev = efi_getdev;
120	archsw.arch_copyin = efi_copyin;
121	archsw.arch_copyout = efi_copyout;
122	archsw.arch_readin = efi_readin;
123
124	interact();			/* doesn't return */
125
126	return (EFI_SUCCESS);		/* keep compiler happy */
127}
128
129COMMAND_SET(quit, "quit", "exit the loader", command_quit);
130
131static int
132command_quit(int argc, char *argv[])
133{
134	exit(0);
135	return (CMD_OK);
136}
137
138COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
139
140static int
141command_memmap(int argc, char *argv[])
142{
143	UINTN sz;
144	EFI_MEMORY_DESCRIPTOR *map, *p;
145	UINTN key, dsz;
146	UINT32 dver;
147	EFI_STATUS status;
148	int i, ndesc;
149	static char *types[] = {
150	    "Reserved",
151	    "LoaderCode",
152	    "LoaderData",
153	    "BootServicesCode",
154	    "BootServicesData",
155	    "RuntimeServicesCode",
156	    "RuntimeServicesData",
157	    "ConventionalMemory",
158	    "UnusableMemory",
159	    "ACPIReclaimMemory",
160	    "ACPIMemoryNVS",
161	    "MemoryMappedIO",
162	    "MemoryMappedIOPortSpace",
163	    "PalCode"
164	};
165
166	sz = 0;
167	status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
168	if (status != EFI_BUFFER_TOO_SMALL) {
169		printf("Can't determine memory map size\n");
170		return;
171	}
172	map = malloc(sz);
173	status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
174	if (EFI_ERROR(status)) {
175		printf("Can't read memory map\n");
176		return;
177	}
178
179	ndesc = sz / dsz;
180	printf("%23s %12s %12s %8s %4s\n",
181	       "Type", "Physical", "Virtual", "#Pages", "Attr");
182
183	for (i = 0, p = map; i < ndesc;
184	     i++, p = NextMemoryDescriptor(p, dsz)) {
185	    printf("%23s %012lx %012lx %08lx ",
186		   types[p->Type],
187		   p->PhysicalStart,
188		   p->VirtualStart,
189		   p->NumberOfPages);
190	    if (p->Attribute & EFI_MEMORY_UC)
191		printf("UC ");
192	    if (p->Attribute & EFI_MEMORY_WC)
193		printf("WC ");
194	    if (p->Attribute & EFI_MEMORY_WT)
195		printf("WT ");
196	    if (p->Attribute & EFI_MEMORY_WB)
197		printf("WB ");
198	    if (p->Attribute & EFI_MEMORY_UCE)
199		printf("UCE ");
200	    if (p->Attribute & EFI_MEMORY_WP)
201		printf("WP ");
202	    if (p->Attribute & EFI_MEMORY_RP)
203		printf("RP ");
204	    if (p->Attribute & EFI_MEMORY_XP)
205		printf("XP ");
206	    if (p->Attribute & EFI_MEMORY_RUNTIME)
207		printf("RUNTIME");
208	    printf("\n");
209	}
210
211	return (CMD_OK);
212}
213