]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_boot.c
zfs: merge openzfs/zfs@86e115e21
[FreeBSD/FreeBSD.git] / sys / kern / subr_boot.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5  * All Rights Reserved.
6  * Copyright (c) 1998 Robert Nordier
7  * All Rights Reserved.
8  * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
9  * All rights reserved.
10  * Copyright (c) 2014 Roger Pau MonnĂ© <roger.pau@citrix.com>
11  * All Rights Reserved.
12  * Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
13  * Copyright (c) 2018 Netflix, Inc.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer
20  *    in this position and unchanged.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 #include <sys/cdefs.h>
39 /* Note: This is compiled in both the kernel and boot loader contexts */
40
41 #include <sys/param.h>
42 #ifdef _KERNEL
43 #include <sys/systm.h>
44 #else
45 #include <stand.h>
46 #endif
47 #include <sys/reboot.h>
48 #include <sys/boot.h>
49 #include <sys/tslog.h>
50
51 #ifdef _KERNEL
52 #define SETENV(k, v)    kern_setenv(k, v)
53 #define GETENV(k)       kern_getenv(k)
54 #define FREE(v)         freeenv(v)
55 #else   /* Boot loader */
56 #define SETENV(k, v)    setenv(k, v, 1)
57 #define GETENV(k)       getenv(k)
58 #define FREE(v)
59 #endif
60
61 static struct
62 {
63         const char      *ev;
64         int             mask;
65 } howto_names[] = {
66         { "boot_askname",       RB_ASKNAME},
67         { "boot_cdrom",         RB_CDROM},
68         { "boot_ddb",           RB_KDB},
69         { "boot_dfltroot",      RB_DFLTROOT},
70         { "boot_gdb",           RB_GDB},
71         { "boot_multicons",     RB_MULTIPLE},
72         { "boot_mute",          RB_MUTE},
73         { "boot_pause",         RB_PAUSE},
74         { "boot_serial",        RB_SERIAL},
75         { "boot_single",        RB_SINGLE},
76         { "boot_verbose",       RB_VERBOSE},
77         { NULL, 0}
78 };
79
80 /*
81  * In the boot environment, we often parse a command line and have to throw away
82  * its contents. As we do so, we set environment variables that correspond to
83  * the flags we encounter. Later, to get a howto mask, we grovel through these
84  * to reconstruct it. This also allows users in their loader.conf to set them
85  * and have the kernel see them.
86  */
87
88 /**
89  * @brief convert the env vars in howto_names into a howto mask
90  */
91 int
92 boot_env_to_howto(void)
93 {
94         int i, howto;
95         char *val;
96
97         TSENTER();
98         for (howto = 0, i = 0; howto_names[i].ev != NULL; i++) {
99                 val = GETENV(howto_names[i].ev);
100                 if (val != NULL && strcasecmp(val, "no") != 0)
101                         howto |= howto_names[i].mask;
102                 FREE(val);
103         }
104         TSEXIT();
105         return (howto);
106 }
107
108 /**
109  * @brief Set env vars from howto_names based on howto passed in
110  */
111 void
112 boot_howto_to_env(int howto)
113 {
114         int i;
115
116         for (i = 0; howto_names[i].ev != NULL; i++)
117                 if (howto & howto_names[i].mask)
118                         SETENV(howto_names[i].ev, "YES");
119 }
120
121 /**
122  * @brief Helper routine to parse a single arg and return its mask
123  *
124  * Parse all the - options to create a mask (or a serial speed in the
125  * case of -S). If the arg doesn't start with '-' assume it's an env
126  * variable and set that instead.
127  */
128 int
129 boot_parse_arg(const char *v)
130 {
131         char *n;
132         int howto;
133
134 #if 0
135 /* Need to see if this is better or worse than the meat of the #else */
136 static const char howto_switches[] = "aCdrgDmphsv";
137 static int howto_masks[] = {
138         RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
139         RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
140 };
141
142         opts = strchr(kargs, '-');
143         while (opts != NULL) {
144                 while (*(++opts) != '\0') {
145                         sw = strchr(howto_switches, *opts);
146                         if (sw == NULL)
147                                 break;
148                         howto |= howto_masks[sw - howto_switches];
149                 }
150                 opts = strchr(opts, '-');
151         }
152 #else
153         howto = 0;
154         if (*v == '-') {
155                 while (*v != '\0') {
156                         v++;
157                         switch (*v) {
158                         case 'a': howto |= RB_ASKNAME; break;
159                         case 'C': howto |= RB_CDROM; break;
160                         case 'd': howto |= RB_KDB; break;
161                         case 'D': howto |= RB_MULTIPLE; break;
162                         case 'm': howto |= RB_MUTE; break;
163                         case 'g': howto |= RB_GDB; break;
164                         case 'h': howto |= RB_SERIAL; break;
165                         case 'p': howto |= RB_PAUSE; break;
166                         case 'P': howto |= RB_PROBE; break;
167                         case 'r': howto |= RB_DFLTROOT; break;
168                         case 's': howto |= RB_SINGLE; break;
169                         case 'S': SETENV("comconsole_speed", v + 1); v += strlen(v); break;
170                         case 'v': howto |= RB_VERBOSE; break;
171                         }
172                 }
173         } else {
174                 char buf[128];
175                 char *vv = buf;
176
177                 strlcpy(buf, v, sizeof(buf));
178                 n = strsep(&vv, "=");
179                 if (vv == NULL)
180                         SETENV(n, "1");
181                 else
182                         SETENV(n, vv);
183         }
184 #endif
185         return (howto);
186 }
187
188 /**
189  * @brief breakup the command line into args, and pass to boot_parse_arg
190  */
191 int
192 boot_parse_cmdline_delim(char *cmdline, const char *delim)
193 {
194         char *v;
195         int howto;
196
197         TSENTER();
198         howto = 0;
199         while ((v = strsep(&cmdline, delim)) != NULL) {
200                 if (*v == '\0')
201                         continue;
202                 howto |= boot_parse_arg(v);
203         }
204         TSEXIT();
205         return (howto);
206 }
207
208 /**
209  * @brief Simplified interface for common 'space or tab separated' args
210  */
211 int
212 boot_parse_cmdline(char *cmdline)
213 {
214
215         return (boot_parse_cmdline_delim(cmdline, " \t\n"));
216 }
217
218 /**
219  * @brief Pass a vector of strings to boot_parse_arg
220  */
221 int
222 boot_parse_args(int argc, char *argv[])
223 {
224         int i, howto;
225
226         howto = 0;
227         for (i = 1; i < argc; i++)
228                 howto |= boot_parse_arg(argv[i]);
229         return (howto);
230 }