199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2004, 2006 Marcel Moolenaar
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2014 The FreeBSD Foundation
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 * 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 AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 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#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/reboot.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/linker.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/boot.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/cpufunc.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/elf.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/metadata.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/psl.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efi.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <efilib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "loader_efi.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__amd64__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/specialreg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "framebuffer.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LOADER_FDT_SUPPORT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <fdt_platform.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern EFI_SYSTEM_TABLE *ST;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic const char howto_switches[] = "aCdrgDmphsv";
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int howto_masks[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomebi_getboothowto(char *kargs)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *sw;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *opts;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *console;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int howto, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Get the boot options from the environment first. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; howto_names[i].ev != NULL; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (getenv(howto_names[i].ev) != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= howto_names[i].mask;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome console = getenv("console");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (console != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(console, "comconsole") == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_SERIAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (strcmp(console, "nullconsole") == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_MUTE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Parse kargs */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kargs == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (howto);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome opts = strchr(kargs, '-');
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (opts != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*(++opts) != '\0') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sw = strchr(howto_switches, *opts);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sw == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= howto_masks[sw - howto_switches];
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome opts = strchr(opts, '-');
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (howto);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copy the environment into the load area starting at (addr).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Each variable is formatted as <name>=<value>, with a single nul
199767f8919635c4928607450d9e0abb932109ceToomas Soome * separating each variable, and a double nul terminating the environment.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vm_offset_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomebi_copyenv(vm_offset_t start)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct env_var *ep;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t addr, last;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = last = start;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Traverse the environment. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (ep = environ; ep != NULL; ep = ep->ev_next) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(ep->ev_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (archsw.arch_copyin("=", addr, 1) != 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ep->ev_value != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(ep->ev_value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (archsw.arch_copyin("", addr, 1) != 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome last = ++addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (archsw.arch_copyin("", last++, 1) != 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome last = start;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(last);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copy module-related data into the load area, where it can be
199767f8919635c4928607450d9e0abb932109ceToomas Soome * used as a directory for loaded modules.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Module data is presented in a self-describing format. Each datum
199767f8919635c4928607450d9e0abb932109ceToomas Soome * is preceded by a 32-bit identifier and a 32-bit size field.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Currently, the following data are saved:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MOD_NAME (variable) module name (string)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MOD_TYPE (variable) module type (string)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MOD_ARGS (variable) module parameters (string)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MOD_ADDR sizeof(vm_offset_t) module load address
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MOD_SIZE sizeof(size_t) module size
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MOD_METADATA (variable) type-specific metadata
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define COPY32(v, a, c) { \
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t x = (v); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(&x, a, sizeof(x)); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome a += sizeof(x); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_STR(t, a, s, c) { \
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY32(t, a, c); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY32(strlen(s) + 1, a, c); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(s, a, strlen(s) + 1); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome a += roundup(strlen(s) + 1, sizeof(u_long)); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_VAR(t, a, s, c) { \
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY32(t, a, c); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY32(sizeof(s), a, c); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(&s, a, sizeof(s)); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome a += roundup(sizeof(s), sizeof(u_long)); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_METADATA(a, mm, c) { \
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY32(MODINFO_METADATA | mm->md_type, a, c); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY32(mm->md_size, a, c); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(mm->md_data, a, mm->md_size); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome a += roundup(mm->md_size, sizeof(u_long)); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MOD_END(a, c) { \
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY32(MODINFO_END, a, c); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome COPY32(0, a, c); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic vm_offset_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomebi_copymodules(vm_offset_t addr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *fp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file_metadata *md;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t v;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = addr != 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Start with the first module on the list, should be the kernel. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_NAME(addr, fp->f_name, c); /* This must come first. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_TYPE(addr, fp->f_type, c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fp->f_args)
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_ARGS(addr, fp->f_args, c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v = fp->f_addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__arm__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome v -= __elfN(relocation_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_ADDR(addr, v, c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v = fp->f_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_SIZE(addr, v, c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (md = fp->f_metadata; md != NULL; md = md->md_next)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(md->md_type & MODINFOMD_NOCOPY))
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_METADATA(addr, md, c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_END(addr, c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomebi_load_efi_data(struct preloaded_file *kfp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_MEMORY_DESCRIPTOR *mm;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_PHYSICAL_ADDRESS addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_STATUS status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t efisz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome UINTN efi_mapkey;
199767f8919635c4928607450d9e0abb932109ceToomas Soome UINTN mmsz, pages, retry, sz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome UINT32 mmver;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct efi_map_header *efihdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__amd64__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct efi_fb efifb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (efi_find_framebuffer(&efifb) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("EFI framebuffer information:\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("addr, size 0x%lx, 0x%lx\n", efifb.fb_addr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb.fb_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("dimensions %d x %d\n", efifb.fb_width,
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb.fb_height);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("stride %d\n", efifb.fb_stride);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
199767f8919635c4928607450d9e0abb932109ceToomas Soome efifb.fb_mask_reserved);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * It is possible that the first call to ExitBootServices may change
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the map key. Fetch a new map key and retry ExitBootServices in that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * case.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (retry = 2; retry > 0; retry--) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Allocate enough pages to hold the bootinfo block and the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * memory map EFI will return to us. The memory map has an
199767f8919635c4928607450d9e0abb932109ceToomas Soome * unknown size, so we have to determine that first. Note that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the AllocatePages call can itself modify the memory map, so
199767f8919635c4928607450d9e0abb932109ceToomas Soome * we have to take that into account as well. The changes to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the memory map are caused by splitting a range of free
199767f8919635c4928607450d9e0abb932109ceToomas Soome * memory into two (AFAICT), so that one is marked as being
199767f8919635c4928607450d9e0abb932109ceToomas Soome * loader data.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz += mmsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = (sz + 0xf) & ~0xf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pages = EFI_SIZE_TO_PAGES(sz + efisz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pages, &addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: AllocatePages error %lu\n", __func__,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_ERROR_CODE(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 efihdr = (struct efi_map_header *)addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome mm = (void *)((uint8_t *)efihdr + efisz);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = (EFI_PAGE_SIZE * pages) - efisz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: GetMemoryMap error %lu\n", __func__,
199767f8919635c4928607450d9e0abb932109ceToomas Soome EFI_ERROR_CODE(status));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome status = BS->ExitBootServices(IH, efi_mapkey);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (EFI_ERROR(status) == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome efihdr->memory_size = sz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efihdr->descriptor_size = mmsz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome efihdr->descriptor_version = mmver;
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
199767f8919635c4928607450d9e0abb932109ceToomas Soome efihdr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome BS->FreePages(addr, pages);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Load the information expected by an amd64 kernel.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The 'boothowto' argument is constructed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The 'bootdev' argument is constructed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The 'bootinfo' struct is constructed, and copied into the kernel space.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The kernel environment is copied into kernel space.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - Module metadata are formatted and placed in kernel space.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomebi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *xp, *kfp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct devdesc *rootdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file_metadata *md;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t kernend;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t envp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *rootdevname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int howto;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LOADER_FDT_SUPPORT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t dtbp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int dtb_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__arm__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t vaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * These metadata addreses must be converted for kernel after
199767f8919635c4928607450d9e0abb932109ceToomas Soome * relocation.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t mdt[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
199767f8919635c4928607450d9e0abb932109ceToomas Soome MODINFOMD_ENVP,
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LOADER_FDT_SUPPORT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome MODINFOMD_DTBP
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome };
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto = bi_getboothowto(args);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Allow the environment variable 'rootdev' to override the supplied
199767f8919635c4928607450d9e0abb932109ceToomas Soome * device. This should perhaps go to MI code and/or have $rootdev
199767f8919635c4928607450d9e0abb932109ceToomas Soome * tested/set by MI code before launching the kernel.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootdevname = getenv("rootdev");
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootdev == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Can't determine root device.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try reading the /etc/fstab file to select the root device */
199767f8919635c4928607450d9e0abb932109ceToomas Soome getrootmount(efi_fmtdev((void *)rootdev));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (addr < (xp->f_addr + xp->f_size))
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = xp->f_addr + xp->f_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Pad to a page boundary. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = roundup(addr, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Copy our environment. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome envp = addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = bi_copyenv(addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Pad to a page boundary. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = roundup(addr, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LOADER_FDT_SUPPORT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Handle device tree blob */
199767f8919635c4928607450d9e0abb932109ceToomas Soome dtbp = addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome dtb_size = fdt_copy(addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Pad to a page boundary */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dtb_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += roundup(dtb_size, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome kfp = file_findfile(NULL, "elf kernel");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kfp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome kfp = file_findfile(NULL, "elf64 kernel");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kfp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("can't find kernel file");
199767f8919635c4928607450d9e0abb932109ceToomas Soome kernend = 0; /* fill it in later */
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LOADER_FDT_SUPPORT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dtb_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output("WARNING! Trying to fire up the kernel, but no "
199767f8919635c4928607450d9e0abb932109ceToomas Soome "device tree blob found!\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bi_load_efi_data(kfp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Figure out the size and location of the metadata. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *modulep = addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = bi_copymodules(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome kernend = roundup(addr + size, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *kernendp = kernend;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* patch MODINFOMD_KERNEND */
199767f8919635c4928607450d9e0abb932109ceToomas Soome md = file_findmetadata(kfp, MODINFOMD_KERNEND);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&kernend, md->md_data, sizeof kernend);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__arm__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *modulep -= __elfN(relocation_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Do relocation fixup on metadata of each module. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < sizeof mdt / sizeof mdt[0]; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome md = file_findmetadata(xp, mdt[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (md) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(md->md_data, &vaddr, sizeof vaddr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome vaddr -= __elfN(relocation_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&vaddr, md->md_data, sizeof vaddr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Copy module list and metadata. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)bi_copymodules(addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}