xref: /illumos-gate/usr/src/boot/efi/loader/arch/i386/efimd.c (revision 69764de32bf29a19d2a8b8832adafc4bd4f934f4)
1 /*-
2  * Copyright (c) 2004, 2006 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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 ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <stand.h>
31 
32 #include <efi.h>
33 #include <efilib.h>
34 
35 #include <libi386.h>
36 #include <machine/bootinfo.h>
37 
38 #define EFI_INTEL_FPSWA		\
39     {0xc41b6531,0x97b9,0x11d3,{0x9a,0x29,0x00,0x90,0x27,0x3f,0xc1,0x4d}}
40 
41 static EFI_GUID fpswa_guid = EFI_INTEL_FPSWA;
42 
43 /* DIG64 Headless Console & Debug Port Table. */
44 #define	HCDP_TABLE_GUID		\
45     {0xf951938d,0x620b,0x42ef,{0x82,0x79,0xa8,0x4b,0x79,0x61,0x78,0x98}}
46 
47 static EFI_GUID hcdp_guid = HCDP_TABLE_GUID;
48 
49 static UINTN mapkey;
50 
51 uint64_t
52 ldr_alloc(vm_offset_t va)
53 {
54 
55 	return (0);
56 }
57 
58 int
59 ldr_bootinfo(struct bootinfo *bi, uint64_t *bi_addr)
60 {
61 	VOID *fpswa;
62 	EFI_MEMORY_DESCRIPTOR *mm;
63 	EFI_PHYSICAL_ADDRESS addr;
64 	EFI_HANDLE *handles;
65 	EFI_STATUS status;
66 	size_t bisz;
67 	UINTN mmsz, pages, sz;
68 	UINT32 mmver;
69 	uint_t nhandles;
70 
71 	bi->bi_systab = (uint64_t)ST;
72 	bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp_guid);
73 
74 	status = efi_get_protocol_handles(&fpswa_guid, &nhandles, &handles);
75 	if (status == 0)
76 		status = OpenProtocolByHandle(*handles, &fpswa_guid, &fpswa);
77 	bi->bi_fpswa = (status == 0) ? (uint64_t)fpswa : 0;
78 
79 	bisz = (sizeof(struct bootinfo) + 0x0f) & ~0x0f;
80 
81 	/*
82 	 * Allocate enough pages to hold the bootinfo block and the memory
83 	 * map EFI will return to us. The memory map has an unknown size,
84 	 * so we have to determine that first. Note that the AllocatePages
85 	 * call can itself modify the memory map, so we have to take that
86 	 * into account as well. The changes to the memory map are caused
87 	 * by splitting a range of free memory into two (AFAICT), so that
88 	 * one is marked as being loader data.
89 	 */
90 	sz = 0;
91 	BS->GetMemoryMap(&sz, NULL, &mapkey, &mmsz, &mmver);
92 	sz += mmsz;
93 	sz = (sz + 15) & ~15;
94 	pages = EFI_SIZE_TO_PAGES(sz + bisz);
95 	status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
96 	    &addr);
97 	if (EFI_ERROR(status)) {
98 		printf("%s: AllocatePages() returned 0x%lx\n", __func__,
99 		    (long)status);
100 		return (ENOMEM);
101 	}
102 
103 	/*
104 	 * Read the memory map and stash it after bootinfo. Align the
105 	 * memory map on a 16-byte boundary (the bootinfo block is page
106 	 * aligned).
107 	 */
108 	*bi_addr = addr;
109 	mm = (void *)(addr + bisz);
110 	sz = (EFI_PAGE_SIZE * pages) - bisz;
111 	status = BS->GetMemoryMap(&sz, mm, &mapkey, &mmsz, &mmver);
112 	if (EFI_ERROR(status)) {
113 		printf("%s: GetMemoryMap() returned 0x%lx\n", __func__,
114 		    (long)status);
115 		return (EINVAL);
116 	}
117 	bi->bi_memmap = (uint64_t)mm;
118 	bi->bi_memmap_size = sz;
119 	bi->bi_memdesc_size = mmsz;
120 	bi->bi_memdesc_version = mmver;
121 
122 	bcopy(bi, (void *)(*bi_addr), sizeof(*bi));
123 	return (0);
124 }
125 
126 int
127 ldr_enter(const char *kernel)
128 {
129 	EFI_STATUS status;
130 
131 	status = BS->ExitBootServices(IH, mapkey);
132 	if (EFI_ERROR(status)) {
133 		printf("%s: ExitBootServices() returned 0x%lx\n", __func__,
134 		    (long)status);
135 		return (EINVAL);
136 	}
137 
138 	return (0);
139 }
140