199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2004, 2006 Marcel Moolenaar
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efi.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efilib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <libi386.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/bootinfo.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define EFI_INTEL_FPSWA \
199767f8919635c4928607450d9e0abb932109ceToomas Soome {0xc41b6531,0x97b9,0x11d3,{0x9a,0x29,0x00,0x90,0x27,0x3f,0xc1,0x4d}}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_GUID fpswa_guid = EFI_INTEL_FPSWA;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* DIG64 Headless Console & Debug Port Table. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define HCDP_TABLE_GUID \
199767f8919635c4928607450d9e0abb932109ceToomas Soome {0xf951938d,0x620b,0x42ef,{0x82,0x79,0xa8,0x4b,0x79,0x61,0x78,0x98}}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic EFI_GUID hcdp_guid = HCDP_TABLE_GUID;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic UINTN mapkey;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuint64_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomeldr_alloc(vm_offset_t va)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeldr_bootinfo(struct bootinfo *bi, uint64_t *bi_addr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome VOID *fpswa;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_MEMORY_DESCRIPTOR *mm;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PHYSICAL_ADDRESS addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_HANDLE handle;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t bisz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome UINTN mmsz, pages, sz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome UINT32 mmver;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi->bi_systab = (uint64_t)ST;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp_guid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = sizeof(EFI_HANDLE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->LocateHandle(ByProtocol, &fpswa_guid, 0, &sz, &handle);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (status == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->HandleProtocol(handle, &fpswa_guid, &fpswa);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi->bi_fpswa = (status == 0) ? (uint64_t)fpswa : 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bisz = (sizeof(struct bootinfo) + 0x0f) & ~0x0f;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Allocate enough pages to hold the bootinfo block and the memory
199767f8919635c4928607450d9e0abb932109ceToomas Soome * map EFI will return to us. The memory map has an unknown size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * so we have to determine that first. Note that the AllocatePages
199767f8919635c4928607450d9e0abb932109ceToomas Soome * call can itself modify the memory map, so we have to take that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * into account as well. The changes to the memory map are caused
199767f8919635c4928607450d9e0abb932109ceToomas Soome * by splitting a range of free memory into two (AFAICT), so that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * one is marked as being loader data.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome BS->GetMemoryMap(&sz, NULL, &mapkey, &mmsz, &mmver);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz += mmsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = (sz + 15) & ~15;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pages = EFI_SIZE_TO_PAGES(sz + bisz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
199767f8919635c4928607450d9e0abb932109ceToomas Soome &addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: AllocatePages() returned 0x%lx\n", __func__,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOMEM);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read the memory map and stash it after bootinfo. Align the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * memory map on a 16-byte boundary (the bootinfo block is page
199767f8919635c4928607450d9e0abb932109ceToomas Soome * aligned).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *bi_addr = addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome mm = (void *)(addr + bisz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = (EFI_PAGE_SIZE * pages) - bisz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->GetMemoryMap(&sz, mm, &mapkey, &mmsz, &mmver);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: GetMemoryMap() returned 0x%lx\n", __func__,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi->bi_memmap = (uint64_t)mm;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi->bi_memmap_size = sz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi->bi_memdesc_size = mmsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi->bi_memdesc_version = mmver;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(bi, (void *)(*bi_addr), sizeof(*bi));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeldr_enter(const char *kernel)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->ExitBootServices(IH, mapkey);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: ExitBootServices() returned 0x%lx\n", __func__,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (long)status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}