bootinfo.c revision 335755
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/11/stand/userboot/userboot/bootinfo.c 335755 2018-06-28 01:32:37Z kevans $");
29
30#include <stand.h>
31#include <sys/param.h>
32#include <sys/reboot.h>
33#include <sys/linker.h>
34
35#include "bootstrap.h"
36#include "libuserboot.h"
37
38int
39bi_getboothowto(char *kargs)
40{
41    char	*cp;
42    char	*curpos, *next, *string;
43    int		howto;
44    int		active;
45    int		vidconsole;
46
47    /* Parse kargs */
48    howto = 0;
49    if (kargs  != NULL) {
50	cp = kargs;
51	active = 0;
52	while (*cp != 0) {
53	    if (!active && (*cp == '-')) {
54		active = 1;
55	    } else if (active)
56		switch (*cp) {
57		case 'a':
58		    howto |= RB_ASKNAME;
59		    break;
60		case 'C':
61		    howto |= RB_CDROM;
62		    break;
63		case 'd':
64		    howto |= RB_KDB;
65		    break;
66		case 'D':
67		    howto |= RB_MULTIPLE;
68		    break;
69		case 'm':
70		    howto |= RB_MUTE;
71		    break;
72		case 'g':
73		    howto |= RB_GDB;
74		    break;
75		case 'h':
76		    howto |= RB_SERIAL;
77		    break;
78		case 'p':
79		    howto |= RB_PAUSE;
80		    break;
81		case 'r':
82		    howto |= RB_DFLTROOT;
83		    break;
84		case 's':
85		    howto |= RB_SINGLE;
86		    break;
87		case 'v':
88		    howto |= RB_VERBOSE;
89		    break;
90		default:
91		    active = 0;
92		    break;
93		}
94	    cp++;
95	}
96    }
97
98    howto |= bootenv_flags();
99
100    /* Enable selected consoles */
101    string = next = strdup(getenv("console"));
102    vidconsole = 0;
103    while (next != NULL) {
104	curpos = strsep(&next, " ,");
105	if (*curpos == '\0')
106		continue;
107	if (!strcmp(curpos, "vidconsole"))
108	    vidconsole = 1;
109	else if (!strcmp(curpos, "comconsole"))
110	    howto |= RB_SERIAL;
111	else if (!strcmp(curpos, "nullconsole"))
112	    howto |= RB_MUTE;
113    }
114
115    if (vidconsole && (howto & RB_SERIAL))
116	howto |= RB_MULTIPLE;
117
118    /*
119     * XXX: Note that until the kernel is ready to respect multiple consoles
120     * for the boot messages, the first named console is the primary console
121     */
122    if (!strcmp(string, "vidconsole"))
123	howto &= ~RB_SERIAL;
124
125    free(string);
126
127    return(howto);
128}
129
130void
131bi_setboothowto(int howto)
132{
133
134    bootenv_set(howto);
135}
136
137/*
138 * Copy the environment into the load area starting at (addr).
139 * Each variable is formatted as <name>=<value>, with a single nul
140 * separating each variable, and a double nul terminating the environment.
141 */
142vm_offset_t
143bi_copyenv(vm_offset_t addr)
144{
145    struct env_var	*ep;
146
147    /* traverse the environment */
148    for (ep = environ; ep != NULL; ep = ep->ev_next) {
149        CALLBACK(copyin, ep->ev_name, addr, strlen(ep->ev_name));
150	addr += strlen(ep->ev_name);
151	CALLBACK(copyin, "=", addr, 1);
152	addr++;
153	if (ep->ev_value != NULL) {
154            CALLBACK(copyin, ep->ev_value, addr, strlen(ep->ev_value));
155	    addr += strlen(ep->ev_value);
156	}
157	CALLBACK(copyin, "", addr, 1);
158	addr++;
159    }
160    CALLBACK(copyin, "", addr, 1);
161    addr++;
162    return(addr);
163}
164