forked from Mellanox/bfscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfrec
executable file
·317 lines (267 loc) · 9.28 KB
/
bfrec
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/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
# To debug set run=echo
run=
PROGNAME=$(basename "$0")
usage()
{
cat <<EOF
Usage: $PROGNAME [--help]
[--default [FILE]] (deprecated)
[--bootctl [FILE]]
[--capsule [FILE]]
Description:
--help : print help.
--default [FILE] : update the boot partition via the
kernel path (deprecated). If FILE
isn't specified, then use default.
--bootctl [FILE] : update the boot partition via the
kernel path. This is intended to
replace '--default'. If FILE isn't
specified, then use default.
--capsule [FILE] : update the boot partition via the
capsule path. If FILE isn't specified,
then use default.
EOF
exit 0
}
bootctl_mode=
capsule_mode=
option_name=
PARSED_OPTIONS=`getopt -n "$PROGNAME" -o h \
-l help,default,bootctl,capsule -- "$@"`
eval set -- "$PARSED_OPTIONS"
while true
do
case $1 in
-h | --help)
usage
;;
--default)
bootctl_mode=1
option_name="default"
shift
;;
--bootctl)
bootctl_mode=1
option_name="bootctl"
shift
;;
--capsule)
capsule_mode=1
shift
;;
--)
shift
break
;;
esac
done
if [ -n "$bootctl_mode" ] && [ -n "$capsule_mode" ]; then
cat <<EOF
ERROR: Use either options '--bootctl' or '--capsule'
EOF
exit 1
fi
bfb_location=/lib/firmware/mellanox/boot
capsule_location=/lib/firmware/mellanox/boot/capsule
exit_failure()
{
action=$1
path=$2
if [ -n "$action" ]; then
echo "use $path path to update boot partitions."
$action
exit $?
fi
exit 2
}
uefi_capsule_update()
{
# Set firmware update application variables
updater_efi=FirmwareUpdate.efi
updater_desc="FirmwareUpdater"
# Set EFI System partition mountpoint
mount_efi=/mnt/efi_system_partition
# Set capsule update file variable name
capsule_efi=MmcBootCap
device_efi=/dev/mmcblk0p1
# First, check whether UEFI supports capsule update. Note that UEFI
# Capsule update is supported in UEFI version 2.0 or greater.
bfver=/opt/mlnx/scripts/bfver
uefi_ver=$($bfver | grep UEFI | cut -d':' -f2 | sed -e 's/[[:space:]]//')
uefi_ver_major=$(echo $uefi_ver | cut -d'.' -f1)
if [ $((uefi_ver_major)) -lt 2 ]; then
echo "Capsule update isn't supported in UEFI version $uefi_ver"
exit_failure $1 $2
fi
# UEFI will read the capsule image from the EFI System Partition.
# Check whether an EFI System partition is present. If not, then
# the capsule update is not supported;
if [ ! "$(fdisk -l | grep "EFI System")" ]; then
echo "cannot find EFI System Partition"
exit_failure $1 $2
fi
if [ ! -f "$capsule_location/$updater_efi" ]; then
echo "cannot find EFI updater application"
exit_failure $1 $2
fi
if [ ! -f "$capsule_file" ]; then
capsule_file=$capsule_location/$capsule_efi
fi
if [ ! -f "$capsule_file" ]; then
echo "cannot find Capsule $capsule_file"
# Use the default.bfb file as capsule file. This might be
# reviewed once the UEFI capsule format is used.
capsule_file=$bfb_location/default.bfb
if [ -f "$capsule_file" ]; then
echo "use $capsule_file as Capsule file"
else
echo "no Capsule update file found"
exit_failure $1 $2
fi
fi
# Check whether the EFI System Partition is mounted. If not mounted,
# then create the mountpoint and mount the disk partition to the
# default location in order to copy the update files.
esp_dir=$(mount | grep $device_efi | cut -f 3 -d' ')
if [ -z "$esp_dir" ]; then
if [ ! -d $mount_efi ]; then
$run mkdir $mount_efi
fi
$run mount $device_efi $mount_efi
else
mount_efi=$esp_dir
fi
# Copy the capsule file into the EFI System Partition. Rename the file
# to match the expected pathname in UEFI, if needed.
$run cp $capsule_file $mount_efi/$capsule_efi
$run cp $capsule_location/$updater_efi $mount_efi/$updater_efi
$run sync
# Doing some cleanup, if needed
if [ -z "$esp_dir" ]; then
$run umount $mount_efi
$run rmdir $mount_efi
# Wait until eMMC internal cache is fully flushed
$run sleep 3
fi
# Create a boot entry to process the capsule update.
efivars=/sys/firmware/efi/efivars
test "$(ls -A $efivars)" || mount -t efivarfs efivarfs $efivars
# The UEFI boot device selector must boot the EFI updater application
# to enter the system firmware update mode.
if [ "$(efibootmgr | grep $updater_desc)" ]; then
option=$(efibootmgr | grep $updater_desc | cut -f1 -d'*' | cut -c5-)
$run efibootmgr -b "$option" -B
fi
disk=$(echo $device_efi | cut -f 1 -d'p')
part=$(echo $device_efi | cut -f 2 -d'p')
$run efibootmgr -c -d $disk -p $part \
-L "$updater_desc" -l "$updater_efi" 2>&1 >/dev/null
cat <<EOF
***********************************************************************
*** ***
*** Reboot the system to process the platform firmware updates ***
*** ***
***********************************************************************
EOF
# Enable card-reset
$run /sbin/mlxbf-bootctl -e
## Ask if the user wishes to reboot?
#read -p 'Reboot now (Y/n) [n]: ' answer
#if [ "$answer" == 'Y' ]; then
# $run reboot
#fi
return 0
}
kernel_bootctl_update()
{
bfb_device=/dev/mmcblk0
if [ ! $(which mlxbf-bootctl) ]; then
echo "mlxbf-bootctl program is not supported"
exit_failure $1 $2
fi
if [ ! -b "$bfb_device" ]; then
echo "bad block device $bfb_device"
exit_failure $1 $2
fi
if [ -z "$bfb_file" ]; then
bfb_file=$bfb_location/default.bfb
fi
if [ ! -f "$bfb_file" ]; then
echo "cannot find file $bfb_file"
exit_failure $1 $2
fi
$run /sbin/mlxbf-bootctl -s -d $bfb_device -b $bfb_file
$run /sbin/mlxbf-bootctl -s -d $bfb_device -b $bfb_file
$run sync
cat <<EOF
***********************************************************************
*** ***
*** Platform firmware updates complete. ***
*** ***
***********************************************************************
EOF
return 0
}
#
# Parse command arguments
#
if [ -n "$bootctl_mode" ] || [ -n "$capsule_mode" ]; then
bfb_file=
capsule_file=
if [ $# -eq 1 ]; then
if [ ! -f $1 ]; then
echo "cannot find file $1"
exit 1
fi
bfb_file=$1
capsule_file=$1
fi
fi
if [ -n "$bootctl_mode" ]; then
if [ $# -ge 2 ]; then
echo "too many arguments with option '--$option_name'"
exit 1
fi
# Update the boot partitions.
kernel_bootctl_update; exit $?
elif [ -n "$capsule_mode" ]; then
if [ $# -ge 2 ]; then
echo "too many arguments with option '--capsule'"
exit 1
fi
# Initiate UEFI capsule update.
uefi_capsule_update; exit $?
fi
# If we reach here, then try both modes. Start with capsule update path
# otherwise use the bootctl tool.
next_update_action=kernel_bootctl_update
next_update_path=bootctl
uefi_capsule_update $next_update_action $next_update_path