1223695Sdfr/*-
2223695Sdfr * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3223695Sdfr * All rights reserved.
4223695Sdfr *
5223695Sdfr * Redistribution and use in source and binary forms, with or without
6223695Sdfr * modification, are permitted provided that the following conditions
7223695Sdfr * are met:
8223695Sdfr * 1. Redistributions of source code must retain the above copyright
9223695Sdfr *    notice, this list of conditions and the following disclaimer.
10223695Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11223695Sdfr *    notice, this list of conditions and the following disclaimer in the
12223695Sdfr *    documentation and/or other materials provided with the distribution.
13223695Sdfr *
14223695Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15223695Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16223695Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17223695Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18223695Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19223695Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20223695Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21223695Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22223695Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23223695Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24223695Sdfr * SUCH DAMAGE.
25223695Sdfr */
26223695Sdfr
27223695Sdfr#include <sys/cdefs.h>
28223695Sdfr__FBSDID("$FreeBSD: stable/11/stand/userboot/userboot/bootinfo.c 344378 2019-02-20 19:19:24Z kevans $");
29223695Sdfr
30223695Sdfr#include <stand.h>
31223695Sdfr#include <sys/param.h>
32223695Sdfr#include <sys/reboot.h>
33344378Skevans#include <sys/boot.h>
34223695Sdfr#include <sys/linker.h>
35223695Sdfr
36223695Sdfr#include "bootstrap.h"
37223695Sdfr#include "libuserboot.h"
38223695Sdfr
39223695Sdfrint
40223695Sdfrbi_getboothowto(char *kargs)
41223695Sdfr{
42223695Sdfr    char	*curpos, *next, *string;
43223695Sdfr    int		howto;
44223695Sdfr    int		vidconsole;
45223695Sdfr
46344378Skevans    howto = boot_parse_cmdline(kargs);
47344378Skevans    howto |= boot_env_to_howto();
48223695Sdfr
49223695Sdfr    /* Enable selected consoles */
50223695Sdfr    string = next = strdup(getenv("console"));
51223695Sdfr    vidconsole = 0;
52223695Sdfr    while (next != NULL) {
53223695Sdfr	curpos = strsep(&next, " ,");
54223695Sdfr	if (*curpos == '\0')
55223695Sdfr		continue;
56223695Sdfr	if (!strcmp(curpos, "vidconsole"))
57223695Sdfr	    vidconsole = 1;
58223695Sdfr	else if (!strcmp(curpos, "comconsole"))
59223695Sdfr	    howto |= RB_SERIAL;
60223695Sdfr	else if (!strcmp(curpos, "nullconsole"))
61223695Sdfr	    howto |= RB_MUTE;
62223695Sdfr    }
63223695Sdfr
64223695Sdfr    if (vidconsole && (howto & RB_SERIAL))
65223695Sdfr	howto |= RB_MULTIPLE;
66223695Sdfr
67223695Sdfr    /*
68223695Sdfr     * XXX: Note that until the kernel is ready to respect multiple consoles
69344378Skevans     * for the messages from /etc/rc, the first named console is the primary
70344378Skevans     * console
71223695Sdfr     */
72223695Sdfr    if (!strcmp(string, "vidconsole"))
73223695Sdfr	howto &= ~RB_SERIAL;
74223695Sdfr
75223695Sdfr    free(string);
76223695Sdfr
77223695Sdfr    return(howto);
78223695Sdfr}
79223695Sdfr
80223695Sdfrvoid
81223695Sdfrbi_setboothowto(int howto)
82223695Sdfr{
83223695Sdfr
84344378Skevans    boot_howto_to_env(howto);
85223695Sdfr}
86223695Sdfr
87223695Sdfr/*
88223695Sdfr * Copy the environment into the load area starting at (addr).
89223695Sdfr * Each variable is formatted as <name>=<value>, with a single nul
90223695Sdfr * separating each variable, and a double nul terminating the environment.
91223695Sdfr */
92223695Sdfrvm_offset_t
93223695Sdfrbi_copyenv(vm_offset_t addr)
94223695Sdfr{
95223695Sdfr    struct env_var	*ep;
96223695Sdfr
97223695Sdfr    /* traverse the environment */
98223695Sdfr    for (ep = environ; ep != NULL; ep = ep->ev_next) {
99223695Sdfr        CALLBACK(copyin, ep->ev_name, addr, strlen(ep->ev_name));
100223695Sdfr	addr += strlen(ep->ev_name);
101223695Sdfr	CALLBACK(copyin, "=", addr, 1);
102223695Sdfr	addr++;
103223695Sdfr	if (ep->ev_value != NULL) {
104223695Sdfr            CALLBACK(copyin, ep->ev_value, addr, strlen(ep->ev_value));
105223695Sdfr	    addr += strlen(ep->ev_value);
106223695Sdfr	}
107223695Sdfr	CALLBACK(copyin, "", addr, 1);
108223695Sdfr	addr++;
109223695Sdfr    }
110223695Sdfr    CALLBACK(copyin, "", addr, 1);
111223695Sdfr    addr++;
112223695Sdfr    return(addr);
113223695Sdfr}
114