199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 2006 Semihalf, Piotr Kruszynski <ppk@semihalf.com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
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 <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/reboot.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/linker.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/boot.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/elf.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/metadata.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "api_public.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "glue.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LOADER_FDT_SUPPORT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <fdt_platform.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomemd_getboothowto(char *kargs)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int howto;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int active;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Parse kargs */
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kargs != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = kargs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome active = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*cp != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!active && (*cp == '-'))
199767f8919635c4928607450d9e0abb932109ceToomas Soome active = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (active)
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (*cp) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'a':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_ASKNAME;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'C':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_CDROM;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'd':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_KDB;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'D':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_MULTIPLE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'm':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_MUTE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'g':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_GDB;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'h':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_SERIAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'p':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_PAUSE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'r':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_DFLTROOT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 's':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_SINGLE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'v':
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_VERBOSE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome active = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* get equivalents from the environment */
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 if ((p = getenv("console"))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(p, "comconsole"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_SERIAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(p, "nullconsole"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_MUTE;
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 Soomemd_copyenv(vm_offset_t addr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct env_var *ep;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* traverse the environment */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (ep = environ; ep != NULL; ep = ep->ev_next) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += strlen(ep->ev_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin("=", addr, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ep->ev_value != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin(ep->ev_value, addr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome strlen(ep->ev_value));
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += strlen(ep->ev_value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin("", addr, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome archsw.arch_copyin("", addr, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(addr);
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 u_int32_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 Soomemd_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 vm_offset_t a;
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
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_NAME(addr, fp->f_name, c); /* this field must be 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 a = fp->f_addr - __elfN(relocation_offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_ADDR(addr, a, c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_SIZE(addr, fp->f_size, 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 }
199767f8919635c4928607450d9e0abb932109ceToomas Soome MOD_END(addr, c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Load the information expected by a kernel.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The 'boothowto' argument is constructed
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - The 'bootdev' argument is constructed
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 Soomemd_load(char *args, vm_offset_t *modulep)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *kfp, *bfp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct preloaded_file *xp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct file_metadata *md;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct bootinfo *bip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t kernend;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t envp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t vaddr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LOADER_FDT_SUPPORT)
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t dtbp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int dtb_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *rootdevname;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int howto;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto = md_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 if (rootdevname == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootdevname = getenv("currdev");
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try reading the /etc/fstab file to select the root device */
199767f8919635c4928607450d9e0abb932109ceToomas Soome getrootmount(rootdevname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find the last module in the chain */
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 /* 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 = md_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 kernend = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome kfp = file_findfile(NULL, "elf32 kernel");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kfp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome kfp = file_findfile(NULL, "elf kernel");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (kfp == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("can't find kernel file");
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Figure out the size and location of the metadata */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *modulep = addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = md_copymodules(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome kernend = roundup(addr + size, PAGE_SIZE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Provide MODINFOMD_KERNEND */
199767f8919635c4928607450d9e0abb932109ceToomas Soome md = file_findmetadata(kfp, MODINFOMD_KERNEND);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&kernend, md->md_data, sizeof kernend);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Convert addresses to the final VA */
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
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Only now copy actual modules and metadata */
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void)md_copymodules(addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}