199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
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 "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libuserboot.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomebi_getboothowto(char *kargs)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *curpos, *next, *string;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int howto;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int active;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int vidconsole;
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 /* 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 /* Enable selected consoles */
199767f8919635c4928607450d9e0abb932109ceToomas Soome string = next = strdup(getenv("console"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome vidconsole = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (next != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome curpos = strsep(&next, " ,");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*curpos == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(curpos, "vidconsole"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome vidconsole = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (!strcmp(curpos, "comconsole"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_SERIAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (!strcmp(curpos, "nullconsole"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_MUTE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (vidconsole && (howto & RB_SERIAL))
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto |= RB_MULTIPLE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX: Note that until the kernel is ready to respect multiple consoles
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for the boot messages, the first named console is the primary console
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(string, "vidconsole"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome howto &= ~RB_SERIAL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(string);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(howto);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomebi_setboothowto(int howto)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; howto_names[i].ev != NULL; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (howto & howto_names[i].mask)
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv(howto_names[i].ev, "YES", 1);
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 Soomevm_offset_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomebi_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 CALLBACK(copyin, ep->ev_name, addr, strlen(ep->ev_name));
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += strlen(ep->ev_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome CALLBACK(copyin, "=", addr, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ep->ev_value != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome CALLBACK(copyin, ep->ev_value, addr, strlen(ep->ev_value));
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr += strlen(ep->ev_value);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome CALLBACK(copyin, "", addr, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome CALLBACK(copyin, "", addr, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(addr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}