2 * Copyright (c) 2004, 2006 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
36 #include <machine/bootinfo.h>
38 #define EFI_INTEL_FPSWA \
39 {0xc41b6531,0x97b9,0x11d3,{0x9a,0x29,0x00,0x90,0x27,0x3f,0xc1,0x4d}}
41 static EFI_GUID fpswa_guid = EFI_INTEL_FPSWA;
43 /* DIG64 Headless Console & Debug Port Table. */
44 #define HCDP_TABLE_GUID \
45 {0xf951938d,0x620b,0x42ef,{0x82,0x79,0xa8,0x4b,0x79,0x61,0x78,0x98}}
47 static EFI_GUID hcdp_guid = HCDP_TABLE_GUID;
52 ldr_alloc(vm_offset_t va)
59 ldr_bootinfo(struct bootinfo *bi, uint64_t *bi_addr)
62 EFI_MEMORY_DESCRIPTOR *mm;
63 EFI_PHYSICAL_ADDRESS addr;
67 UINTN mmsz, pages, sz;
70 bi->bi_systab = (uint64_t)ST;
71 bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp_guid);
73 sz = sizeof(EFI_HANDLE);
74 status = BS->LocateHandle(ByProtocol, &fpswa_guid, 0, &sz, &handle);
76 status = BS->HandleProtocol(handle, &fpswa_guid, &fpswa);
77 bi->bi_fpswa = (status == 0) ? (uint64_t)fpswa : 0;
79 bisz = (sizeof(struct bootinfo) + 0x0f) & ~0x0f;
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.
91 BS->GetMemoryMap(&sz, NULL, &mapkey, &mmsz, &mmver);
94 pages = EFI_SIZE_TO_PAGES(sz + bisz);
95 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
97 if (EFI_ERROR(status)) {
98 printf("%s: AllocatePages() returned 0x%lx\n", __func__,
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
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__,
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;
122 bcopy(bi, (void *)(*bi_addr), sizeof(*bi));
127 ldr_enter(const char *kernel)
131 status = BS->ExitBootServices(IH, mapkey);
132 if (EFI_ERROR(status)) {
133 printf("%s: ExitBootServices() returned 0x%lx\n", __func__,