forked from Mellanox/bfscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfrec
executable file
·286 lines (242 loc) · 8.17 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
#!/bin/bash
# Copyright (c) 2020, 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]
[--bootctl [FILE]]
[--capsule [FILE]]
Description:
--help : print help.
--bootctl [FILE] : update the boot partition via the
kernel path. If FILE isn't specified,
/lib/firmware/mellanox/boot/default.bfb
will be used by default.
--capsule [FILE] : update the boot partition via the
capsule path. If FILE isn't specified,
/lib/firmware/mellanox/boot/capsule/MmcBootCap
will be used by default.
EOF
exit 0
}
bootctl_mode=
capsule_mode=
PARSED_OPTIONS=$(getopt -n "$PROGNAME" -o h -l help,bootctl,capsule -- "$@")
eval set -- "$PARSED_OPTIONS"
while true
do
case $1 in
-h | --help)
usage
;;
--bootctl)
bootctl_mode=1
shift
;;
--capsule)
capsule_mode=1
shift
;;
--)
shift
break
;;
esac
done
# Mount efivars sysfs if not already
efivars=/sys/firmware/efi/efivars
test "$(ls -A $efivars)" || mount -t efivarfs efivarfs $efivars
# Check secure boot
secure_boot=
if [ -e "$efivars/PK-8be4df61-93ca-11d2-aa0d-00e098032b8c" ]; then
secure_boot=1
fi
# Only allow capsule when secure boot is enabled.
if [ -n "$bootctl_mode" ]; then
if [ -n "$secure_boot" ]; then
echo "ERROR: need to use capsule in secure boot mode"
exit 1
fi
if [ -n "$capsule_mode" ]; then
echo "ERROR: Use either options '--bootctl' or '--capsule'"
exit 1
fi
elif [ -z "$capsule_mode" ]; then
if [ -n "$secure_boot" ]; then
capsule_mode=1
else
bootctl_mode=1
fi
fi
#
# Fall back to bootctl mode if capsule is not supported, which is
# needed to be compatible with old boot-image versions.
#
os_cap_var=OsIndicationsSupported-8be4df61-93ca-11d2-aa0d-00e098032b8c
os_var=OsIndications-8be4df61-93ca-11d2-aa0d-00e098032b8c
if [ ! -e "${efivars}/${os_cap_var}" ]; then
capsule_mode=
bootctl_mode=1
else
# Check the EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED bit.
cp -f ${efivars}/${os_cap_var} /tmp/
os_cap=$(hexdump -s 4 -n 1 -e '/1 "%d" "\n"' /tmp/${os_cap_var})
os_cap=$((os_cap & 4))
if [ $os_cap -eq 0 ]; then
capsule_mode=
bootctl_mode=1
fi
fi
bfb_location=/lib/firmware/mellanox/boot
capsule_location=/lib/firmware/mellanox/boot/capsule
uefi_capsule_update()
{
# Set EFI System partition mountpoint
mount_efi=/mnt/efi_system_partition
# Set capsule update file variable name
capsule_efi=MmcBootCap
device_efi=/dev/mmcblk0p1
# 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 -q "EFI System" >/dev/null; then
echo "cannot find EFI System Partition"
exit 2
fi
if [ ! -f "$capsule_file" ]; then
capsule_file=$capsule_location/$capsule_efi
fi
if [ ! -f "$capsule_file" ]; then
echo "Capsule file not found"
exit 2
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.
# "EFI/UpdateCapsule" is the standard directory defined by UEFI spec.
# Files in this directory will processed as capsules in alphabetical
# order once the capsule flag is set in the 'OsIndications' variable
# which is done by the printf command below.
$run mkdir -p "$mount_efi/EFI/UpdateCapsule" 2>/dev/null
$run cp $capsule_file "$mount_efi/EFI/UpdateCapsule/$capsule_efi"
$run printf "\x07\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00" > \
"${efivars}/${os_var}"
$run sync
# Doing some cleanup, if needed
if [ -z "$esp_dir" ]; then
$run umount "$mount_efi"
$run rmdir "$mount_efi"
$run sync
# Wait until eMMC internal cache is fully flushed
$run sleep 3
fi
cat <<EOF
***********************************************************************
*** ***
*** Reboot the system to process the platform firmware updates ***
*** ***
***********************************************************************
EOF
# Enable card-reset
$run /sbin/mlxbf-bootctl -e
return 0
}
kernel_bootctl_update()
{
bfb_device=/dev/mmcblk0
if ! command -v mlxbf-bootctl >/dev/null; then
echo "mlxbf-bootctl program is not supported"
exit 2
fi
if [ ! -b "$bfb_device" ]; then
echo "bad block device $bfb_device"
exit 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 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"
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