forked from Mellanox/bfscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfbootmgr
executable file
·223 lines (196 loc) · 6.53 KB
/
bfbootmgr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/sh
# Copyright (c) 2017, Mellanox Technologies
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.
set -e
PROGNAME=$(basename "$0")
usage()
{
cat <<EOF
Usage: $PROGNAME COMMAND [PARAMS]
COMMAND list:
--help : print help.
--add <DISTRO> <KERNEL> <ARGS> : create a new boot entry.
--cleanall : delete all boot entires.
--force <shell|pxe|default> [p] : boot override; if 'p' is
used then the boot order
would be persistent across
reboots.
EOF
}
add=
cleanall=
force=
option=0
PARSED_OPTIONS=`getopt -n "$PROGNAME" -o h \
-l help,add,cleanall,force -- "$@"`
eval set -- "$PARSED_OPTIONS"
while true
do
case $1 in
-h | --help)
usage
exit 0
;;
-a | --add)
add=1
option=$(($option+1))
shift
;;
-c | --cleanall)
cleanall=1
option=$(($option+1))
shift
;;
-f | --force)
force=1
option=$(($option+1))
shift
;;
--)
shift
break
;;
esac
done
# Check arguments. For now, a single option is implemented at a time.
if [ $option -ne 1 ]; then
echo "Cannot support multiple options!"
usage
exit 1
fi
# This script requires that the kernel supports access to EFI non-volatile
# variables. The UEFI system requires several kernel configuration options.
# Since this script might be used after booting with initramfs only, we
# prefer to not enable those EFI configurations options. Alternatively, we
# use the efivarfs (EFI VARiable File System) interface (CONFIG_EFIVAR_FS)
# mounted using efivarfs kernel module at /sys/firmware/efi/efivars to
# remove EFI variables.
efivars=/sys/firmware/efi/efivars
boot_cleanall()
{
# First check whether boot order array variable is present and has
# boot options
boot_order=$(efibootmgr | grep BootOrder)
if [ "$(echo "$boot_order" | cut -f1 -d':')" = "BootOrder" ]; then
options=$(echo "$boot_order" | cut -f2 -d':' | sed -e 's/,/ /g')
if [ -n "$options" ]; then
# Clear boot options
for opt in $options; do
efibootmgr -b "$opt" -B
done
fi
fi
# Second round: there might be old boot entries not part of the
# boot order array. Those entries might be installed after firmware
# recovery. Boot entries are formatted as BootXXXX where XXXX refer
# to an hexadecimal number. Thus extract that number only.
options=$(efibootmgr | grep -F "*" | cut -f1 -d'*' | cut -c5-)
# Check whether extra boot entries exist.
if [ -n "$options" ]; then
# Clear boot options
for opt in $options; do
efibootmgr -b "$opt" -B
done
fi
}
boot_add()
{
# Read parameters.
distro="$1"
kernel="$2"
bootargs="$3"
# Set default parameters
disk="/dev/mmcblk0"
part="1"
# Create the boot entry.
echo "Distro : $distro"
echo "Kernel Image: $kernel"
echo "Command line: $bootargs"
efibootmgr -c -d $disk -p $part -l "$kernel" -L "$distro" -u "$bootargs"
}
boot_override()
{
cd $efivars
# Read parameters
select="$1"
persist="$2"
# Set the BlueField boot override variable.
# The efivarfs (EFI VARiable File System) contains files whose names are
# built from the UEFI variables name and GUID. All architecturally defined
# variables use the EFI_GLOBAL_VARIABLE GUID
# 0x8BE4DF61,0x93CA,0x11D2,0xAA,0xOD,0x00,0xE0,0x98,0x03,0x2B,0x8C.
var=BfBootOverride-8be4df61-93ca-11d2-aa0d-00e098032b8c
[[ -e "$var" ]] && chattr -i $var
if [ "$select" == "pxe" ]; then
data="01"
elif [ "$select" == "shell" ]; then
data="02"
elif [ "$select" == "default" ]; then
[[ -e "$var" ]] && (echo "Deleting $var"; rm $var)
exit 0
else
echo "Bad Boot Selection $select!"
return 1
fi
if [ -n "$persist" ]; then
data="$data\x01"
else
data="$data\x00"
fi
# Set the BlueField boot override variable.
printf "\x07\x00\x00\x00\x$data" > $var
}
# Linux kernel exposes EFI variables data to userspace via efivarfs
# interface. Thus mount it, if needed.
test "$(ls -A $efivars)" || mount -t efivarfs none $efivars
# Clean up boot options using 'efibootmgr'.
[[ -n "$cleanall" ]] && (boot_cleanall; exit $?)
# Add boot option
if [ -n "$add" ]; then
# Check arguments
if [ $# -lt 3 ]; then
echo "$PROGNAME: invalid operand(s)"
echo "Try: $PROGNAME --add <DISTRO> <KERNEL> <ARGS>"
exit 1
fi
boot_add $1 $2 $3
exit $?
fi
if [ -n "$force" ]; then
# Check arguments
if [ $# -lt 1 ]; then
echo "$PROGNAME: invalid operand(s)"
echo "Try: $PROGNAME --force <shell|pxe|default> [p]"
exit 1
fi
if [ -n "$2" ] && [ "$2" != "p" ]; then
echo "$PROGNAME: invalid option $2, did you mean 'p'?"
exit 1
fi
boot_override $1 $2
exit $?
fi