1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2002 Gordon Tetlow. All rights reserved.
6# Copyright (c) 2012 Sandvine Incorporated. All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29# $FreeBSD$
30
31append="NO"
32delete="NO"
33kenv=
34force="NO"
35nextboot_file="/boot/nextboot.conf"
36
37add_kenv()
38{
39	local var value
40
41	var=$1
42	# strip literal quotes if passed in
43	value=${2%\"*}
44	value=${value#*\"}
45
46	if [ -n "${kenv}" ]; then
47		kenv="${kenv}
48"
49	fi
50	kenv="${kenv}${var}=\"${value}\""
51}
52
53display_usage() {
54	cat <<-EOF
55	Usage: nextboot [-af] [-e variable=value] [-k kernel] [-o options]
56	       nextboot -D
57	EOF
58}
59
60while getopts "aDe:fk:o:" argument ; do
61	case "${argument}" in
62	a)
63		append="YES"
64		;;
65	D)
66		delete="YES"
67		;;
68	e)
69		var=${OPTARG%%=*}
70		value=${OPTARG#*=}
71		if [ -z "$var" -o -z "$value" ]; then
72			display_usage
73			exit 1
74		fi
75		add_kenv "$var" "$value"
76		;;
77	f)
78		force="YES"
79		;;
80	k)
81		kernel="${OPTARG}"
82		add_kenv kernel "$kernel"
83		;;
84	o)
85		add_kenv kernel_options "${OPTARG}"
86		;;
87	*)
88		display_usage
89		exit 1
90		;;
91	esac
92done
93
94if [ ${delete} = "YES" ]; then
95	rm -f ${nextboot_file}
96	exit 0
97fi
98
99if [ -z "${kenv}" ]; then
100	display_usage
101	exit 1
102fi
103
104if [ -n "${kernel}" -a ${force} = "NO" -a ! -d /boot/${kernel} ]; then
105	echo "Error: /boot/${kernel} doesn't exist. Use -f to override."
106	exit 1
107fi
108
109df -Tn "/boot/" 2>/dev/null | while read _fs _type _other ; do
110	[ "zfs" = "${_type}" ] || continue
111	cat 1>&2 <<-EOF
112		WARNING: loader(8) has only R/O support for ZFS
113		nextboot.conf will NOT be reset in case of kernel boot failure
114	EOF
115done
116
117set -e
118
119nextboot_tmp=$(mktemp $(dirname ${nextboot_file})/nextboot.XXXXXX)
120
121if [ ${append} = "YES" -a -f ${nextboot_file} ]; then
122	cp -f ${nextboot_file} ${nextboot_tmp}
123fi
124
125cat >> ${nextboot_tmp} << EOF
126nextboot_enable="YES"
127$kenv
128EOF
129
130fsync ${nextboot_tmp}
131
132mv ${nextboot_tmp} ${nextboot_file}
133