xref: /illumos-gate/usr/src/boot/i386/libi386/bootinfo.c (revision 15f90b02bdacbf0ae47fa105944f15b6596f9748)
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 
29 #include <stand.h>
30 #include <sys/param.h>
31 #include <sys/reboot.h>
32 #include <sys/linker.h>
33 #include <sys/boot.h>
34 #include "bootstrap.h"
35 #include "libi386.h"
36 #include "btxv86.h"
37 
38 int
39 bi_getboothowto(char *kargs)
40 {
41     char	*cp;
42     char	*curpos, *next, *string;
43     int		howto;
44     int		active;
45     int		i;
46     int		vidconsole;
47 
48     /* Parse kargs */
49     howto = 0;
50     if (kargs  != NULL) {
51 	cp = kargs;
52 	active = 0;
53 	while (*cp != 0) {
54 	    if (!active && (*cp == '-')) {
55 		active = 1;
56 	    } else if (active)
57 		switch (*cp) {
58 		case 'a':
59 		    howto |= RB_ASKNAME;
60 		    break;
61 		case 'C':
62 		    howto |= RB_CDROM;
63 		    break;
64 		case 'd':
65 		    howto |= RB_KDB;
66 		    break;
67 		case 'D':
68 		    howto |= RB_MULTIPLE;
69 		    break;
70 		case 'm':
71 		    howto |= RB_MUTE;
72 		    break;
73 		case 'g':
74 		    howto |= RB_GDB;
75 		    break;
76 		case 'h':
77 		    howto |= RB_SERIAL;
78 		    break;
79 		case 'p':
80 		    howto |= RB_PAUSE;
81 		    break;
82 		case 'r':
83 		    howto |= RB_DFLTROOT;
84 		    break;
85 		case 's':
86 		    howto |= RB_SINGLE;
87 		    break;
88 		case 'v':
89 		    howto |= RB_VERBOSE;
90 		    break;
91 		default:
92 		    active = 0;
93 		    break;
94 		}
95 	    cp++;
96 	}
97     }
98     /* get equivalents from the environment */
99     for (i = 0; howto_names[i].ev != NULL; i++)
100 	if (getenv(howto_names[i].ev) != NULL)
101 	    howto |= howto_names[i].mask;
102 
103     /* Enable selected consoles */
104     string = next = strdup(getenv("console"));
105     vidconsole = 0;
106     while (next != NULL) {
107 	curpos = strsep(&next, " ,");
108 	if (*curpos == '\0')
109 		continue;
110 	if (!strcmp(curpos, "text"))
111 	    vidconsole = 1;
112 	else if (!strcmp(curpos, "ttya"))
113 	    howto |= RB_SERIAL;
114 	else if (!strcmp(curpos, "ttyb"))
115 	    howto |= RB_SERIAL;
116 	else if (!strcmp(curpos, "ttyc"))
117 	    howto |= RB_SERIAL;
118 	else if (!strcmp(curpos, "ttyd"))
119 	    howto |= RB_SERIAL;
120 	else if (!strcmp(curpos, "null"))
121 	    howto |= RB_MUTE;
122     }
123 
124     if (vidconsole && (howto & RB_SERIAL))
125 	howto |= RB_MULTIPLE;
126 
127     /*
128      * XXX: Note that until the kernel is ready to respect multiple consoles
129      * for the boot messages, the first named console is the primary console
130      */
131     if (!strcmp(string, "text"))
132 	howto &= ~RB_SERIAL;
133 
134     free(string);
135 
136     return(howto);
137 }
138 
139 void
140 bi_setboothowto(int howto)
141 {
142     int		i;
143 
144     for (i = 0; howto_names[i].ev != NULL; i++)
145 	if (howto & howto_names[i].mask)
146 	    setenv(howto_names[i].ev, "YES", 1);
147 }
148 
149 /*
150  * Copy the environment into the load area starting at (addr).
151  * Each variable is formatted as <name>=<value>, with a single nul
152  * separating each variable, and a double nul terminating the environment.
153  */
154 vm_offset_t
155 bi_copyenv(vm_offset_t addr)
156 {
157     struct env_var	*ep;
158 
159     /* traverse the environment */
160     for (ep = environ; ep != NULL; ep = ep->ev_next) {
161 	i386_copyin(ep->ev_name, addr, strlen(ep->ev_name));
162 	addr += strlen(ep->ev_name);
163 	i386_copyin("=", addr, 1);
164 	addr++;
165 	if (ep->ev_value != NULL) {
166 	    i386_copyin(ep->ev_value, addr, strlen(ep->ev_value));
167 	    addr += strlen(ep->ev_value);
168 	}
169 	i386_copyin("", addr, 1);
170 	addr++;
171     }
172     i386_copyin("", addr, 1);
173     addr++;
174     return(addr);
175 }
176