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, \
40 {0x9a, 0x29, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
41
42 static EFI_GUID fpswa_guid = EFI_INTEL_FPSWA;
43
44 /* DIG64 Headless Console & Debug Port Table. */
45 #define HCDP_TABLE_GUID \
46 {0xf951938d, 0x620b, 0x42ef, \
47 {0x82, 0x79, 0xa8, 0x4b, 0x79, 0x61, 0x78, 0x98}}
48
49 static EFI_GUID hcdp_guid = HCDP_TABLE_GUID;
50
51 static UINTN mapkey;
52
53 uint64_t
ldr_alloc(vm_offset_t va)54 ldr_alloc(vm_offset_t va)
55 {
56
57 return (0);
58 }
59
60 int
ldr_bootinfo(struct bootinfo * bi,uint64_t * bi_addr)61 ldr_bootinfo(struct bootinfo *bi, uint64_t *bi_addr)
62 {
63 void *fpswa;
64 EFI_MEMORY_DESCRIPTOR *mm;
65 EFI_PHYSICAL_ADDRESS addr;
66 EFI_HANDLE *handles;
67 EFI_STATUS status;
68 size_t bisz;
69 UINTN mmsz, pages, sz;
70 UINT32 mmver;
71 uint_t nhandles;
72
73 bi->bi_systab = (uint64_t)ST;
74 bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp_guid);
75
76 status = efi_get_protocol_handles(&fpswa_guid, &nhandles, &handles);
77 if (status == 0)
78 status = OpenProtocolByHandle(*handles, &fpswa_guid, &fpswa);
79 bi->bi_fpswa = (status == 0) ? (uint64_t)fpswa : 0;
80
81 bisz = (sizeof (struct bootinfo) + 0x0f) & ~0x0f;
82
83 /*
84 * Allocate enough pages to hold the bootinfo block and the memory
85 * map EFI will return to us. The memory map has an unknown size,
86 * so we have to determine that first. Note that the AllocatePages
87 * call can itself modify the memory map, so we have to take that
88 * into account as well. The changes to the memory map are caused
89 * by splitting a range of free memory into two (AFAICT), so that
90 * one is marked as being loader data.
91 */
92 sz = 0;
93 BS->GetMemoryMap(&sz, NULL, &mapkey, &mmsz, &mmver);
94 sz += mmsz;
95 sz = (sz + 15) & ~15;
96 pages = EFI_SIZE_TO_PAGES(sz + bisz);
97 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
98 &addr);
99 if (EFI_ERROR(status)) {
100 printf("%s: AllocatePages() returned 0x%lx\n", __func__,
101 (long)status);
102 return (ENOMEM);
103 }
104
105 /*
106 * Read the memory map and stash it after bootinfo. Align the
107 * memory map on a 16-byte boundary (the bootinfo block is page
108 * aligned).
109 */
110 *bi_addr = addr;
111 mm = (void *)(addr + bisz);
112 sz = (EFI_PAGE_SIZE * pages) - bisz;
113 status = BS->GetMemoryMap(&sz, mm, &mapkey, &mmsz, &mmver);
114 if (EFI_ERROR(status)) {
115 printf("%s: GetMemoryMap() returned 0x%lx\n", __func__,
116 (long)status);
117 return (EINVAL);
118 }
119 bi->bi_memmap = (uint64_t)mm;
120 bi->bi_memmap_size = sz;
121 bi->bi_memdesc_size = mmsz;
122 bi->bi_memdesc_version = mmver;
123
124 bcopy(bi, (void *)(*bi_addr), sizeof (*bi));
125 return (0);
126 }
127
128 int
ldr_enter(const char * kernel)129 ldr_enter(const char *kernel)
130 {
131 EFI_STATUS status;
132
133 status = BS->ExitBootServices(IH, mapkey);
134 if (EFI_ERROR(status)) {
135 printf("%s: ExitBootServices() returned 0x%lx\n", __func__,
136 (long)status);
137 return (EINVAL);
138 }
139
140 return (0);
141 }
142