bootinfo.c revision 329145
12862Swollman/*-
22862Swollman * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
32862Swollman * All rights reserved.
42862Swollman *
52862Swollman * Redistribution and use in source and binary forms, with or without
62862Swollman * modification, are permitted provided that the following conditions
72862Swollman * are met:
82862Swollman * 1. Redistributions of source code must retain the above copyright
92862Swollman *    notice, this list of conditions and the following disclaimer.
102862Swollman * 2. Redistributions in binary form must reproduce the above copyright
112862Swollman *    notice, this list of conditions and the following disclaimer in the
122862Swollman *    documentation and/or other materials provided with the distribution.
132862Swollman *
142862Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152862Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162862Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172862Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182862Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192862Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202862Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212862Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222862Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232862Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242862Swollman * SUCH DAMAGE.
252862Swollman */
262862Swollman
272862Swollman#include <sys/cdefs.h>
282862Swollman__FBSDID("$FreeBSD: stable/11/stand/userboot/userboot/bootinfo.c 263005 2014-03-11 10:13:06Z royger $");
292862Swollman
302862Swollman#include <stand.h>
312862Swollman#include <sys/param.h>
322862Swollman#include <sys/reboot.h>
332862Swollman#include <sys/linker.h>
342862Swollman#include <sys/boot.h>
3520420Ssteve
362862Swollman#include "bootstrap.h"
372862Swollman#include "libuserboot.h"
382862Swollman
392862Swollmanint
402862Swollmanbi_getboothowto(char *kargs)
4136009Scharnier{
4220420Ssteve    char	*cp;
4336009Scharnier    char	*curpos, *next, *string;
442862Swollman    int		howto;
4599109Sobrien    int		active;
4699109Sobrien    int		i;
472862Swollman    int		vidconsole;
482862Swollman
492862Swollman    /* Parse kargs */
502862Swollman    howto = 0;
512862Swollman    if (kargs  != NULL) {
522862Swollman	cp = kargs;
532862Swollman	active = 0;
542862Swollman	while (*cp != 0) {
552862Swollman	    if (!active && (*cp == '-')) {
5690108Simp		active = 1;
5726402Scharnier	    } else if (active)
582862Swollman		switch (*cp) {
5990108Simp		case 'a':
602862Swollman		    howto |= RB_ASKNAME;
6126402Scharnier		    break;
627165Sjoerg		case 'C':
632862Swollman		    howto |= RB_CDROM;
6426402Scharnier		    break;
6526402Scharnier		case 'd':
6691080Smarkm		    howto |= RB_KDB;
6791080Smarkm		    break;
6826402Scharnier		case 'D':
6926402Scharnier		    howto |= RB_MULTIPLE;
7026402Scharnier		    break;
7126402Scharnier		case 'm':
7226402Scharnier		    howto |= RB_MUTE;
732862Swollman		    break;
7426402Scharnier		case 'g':
7526402Scharnier		    howto |= RB_GDB;
7626402Scharnier		    break;
772862Swollman		case 'h':
7876874Skris		    howto |= RB_SERIAL;
792862Swollman		    break;
802862Swollman		case 'p':
8176874Skris		    howto |= RB_PAUSE;
822862Swollman		    break;
832862Swollman		case 'r':
842862Swollman		    howto |= RB_DFLTROOT;
852862Swollman		    break;
862862Swollman		case 's':
8726402Scharnier		    howto |= RB_SINGLE;
8890108Simp		    break;
8990108Simp		case 'v':
9026402Scharnier		    howto |= RB_VERBOSE;
9126434Scharnier		    break;
9226402Scharnier		default:
9326402Scharnier		    active = 0;
94		    break;
95		}
96	    cp++;
97	}
98    }
99    /* get equivalents from the environment */
100    for (i = 0; howto_names[i].ev != NULL; i++)
101	if (getenv(howto_names[i].ev) != NULL)
102	    howto |= howto_names[i].mask;
103
104    /* Enable selected consoles */
105    string = next = strdup(getenv("console"));
106    vidconsole = 0;
107    while (next != NULL) {
108	curpos = strsep(&next, " ,");
109	if (*curpos == '\0')
110		continue;
111	if (!strcmp(curpos, "vidconsole"))
112	    vidconsole = 1;
113	else if (!strcmp(curpos, "comconsole"))
114	    howto |= RB_SERIAL;
115	else if (!strcmp(curpos, "nullconsole"))
116	    howto |= RB_MUTE;
117    }
118
119    if (vidconsole && (howto & RB_SERIAL))
120	howto |= RB_MULTIPLE;
121
122    /*
123     * XXX: Note that until the kernel is ready to respect multiple consoles
124     * for the boot messages, the first named console is the primary console
125     */
126    if (!strcmp(string, "vidconsole"))
127	howto &= ~RB_SERIAL;
128
129    free(string);
130
131    return(howto);
132}
133
134void
135bi_setboothowto(int howto)
136{
137    int		i;
138
139    for (i = 0; howto_names[i].ev != NULL; i++)
140	if (howto & howto_names[i].mask)
141	    setenv(howto_names[i].ev, "YES", 1);
142}
143
144/*
145 * Copy the environment into the load area starting at (addr).
146 * Each variable is formatted as <name>=<value>, with a single nul
147 * separating each variable, and a double nul terminating the environment.
148 */
149vm_offset_t
150bi_copyenv(vm_offset_t addr)
151{
152    struct env_var	*ep;
153
154    /* traverse the environment */
155    for (ep = environ; ep != NULL; ep = ep->ev_next) {
156        CALLBACK(copyin, ep->ev_name, addr, strlen(ep->ev_name));
157	addr += strlen(ep->ev_name);
158	CALLBACK(copyin, "=", addr, 1);
159	addr++;
160	if (ep->ev_value != NULL) {
161            CALLBACK(copyin, ep->ev_value, addr, strlen(ep->ev_value));
162	    addr += strlen(ep->ev_value);
163	}
164	CALLBACK(copyin, "", addr, 1);
165	addr++;
166    }
167    CALLBACK(copyin, "", addr, 1);
168    addr++;
169    return(addr);
170}
171