forked from Mellanox/bfscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfcfg
executable file
·622 lines (549 loc) · 17.2 KB
/
bfcfg
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/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.
# shellcheck disable=SC2059
true
bfcfg_version=0.2
cfg_file=/etc/bf.cfg
log_file=/tmp/bfcfg.log
dump_mode=0
efivars=/sys/firmware/efi/efivars
efi_global_var_guid=8be4df61-93ca-11d2-aa0d-00e098032b8c
efi_vlan_var_guid=9e23d768-d2f3-4366-9fc3-3a7aba864374
rshim_efi_mac_sysfs=${efivars}/RshimMacAddr-${efi_global_var_guid}
oob_mac_sysfs=/sys/bus/platform/drivers/mlx-bootctl/oob_mac
log_msg()
{
echo "$@" >> ${log_file}
}
mfg_cfg()
{
local tmp_file=/tmp/.bfcfg-mfg-data
local opn_str_sysfs=/sys/bus/platform/drivers/mlx-bootctl/opn_str
local oob_efi_mac_sysfs=${efivars}/OobMacAddr-${efi_global_var_guid}
local mac_err opn_err mac
if [ $dump_mode -eq 1 ]; then
[ -e "${oob_mac_sysfs}" ] && echo "mfg: MFG_OOB_MAC=$(cat ${oob_mac_sysfs} 2>/dev/null)"
[ -e "${opn_str_sysfs}" ] && echo "mfg: MFG_OPN_STR=$(cat ${opn_str_sysfs} 2>/dev/null)"
return
fi
if [ -n "${MFG_OOB_MAC}" ] && [ -e "${oob_mac_sysfs}" ] && [ -n "${MFG_OPN_STR}" ] && [ -e "${opn_str_sysfs}" ]; then
log_msg "mfg: OOB_MAC=${MFG_OOB_MAC}"
echo "${MFG_OOB_MAC}" > ${oob_mac_sysfs} 2>>${log_file}
mac_err=$?
log_msg "mfg: OPN_STR=${MFG_OPN_STR}"
echo "${MFG_OPN_STR}" > ${opn_str_sysfs} 2>>${log_file}
opn_err=$?
if [ ${mac_err} -eq 0 ] && [ ${opn_err} -eq 0 ]; then
log_msg "mfg: lock the partition"
echo 1 > /sys/bus/platform/drivers/mlx-bootctl/mfg_lock 2>>${log_file}
else
log_msg "mfg: mac_err=${mac_err} opn_err=${opn_err}"
fi
# Update the MAC address in UEFI variable.
# Somehow 'printf' into the sysfs file doesn't always work, probably
# due to length check of each write. A temporary file is used here
# to workaround such issue.
mac="${MFG_OOB_MAC//:/\\x}"
mac="\\x07\\x00\\x00\\x00\\x${mac}"
printf "${mac}" > ${tmp_file}
chattr -i ${oob_efi_mac_sysfs} 2>/dev/null
cp ${tmp_file} ${oob_efi_mac_sysfs}
rm -f ${tmp_file}
else
log_msg "mfg: skip"
fi
}
#
# Set a value at the specified offset in a file.
#
sys_cfg_one_byte()
{
local tmp_file=$1
local name=$2
local offset=$3
local value=$4
local bin_value
if [ ${dump_mode} -eq 1 ]; then
value=$(hexdump -s "${offset}" -n 1 -e '/1 "%d" "\n"' "${tmp_file}")
echo "sys: ${name}=${value}"
elif [ -n "${value}" ]; then
bin_value=$(echo "${value}" | tr '[:lower:]' '[:upper:]')
if [ ."$value" = ."TRUE" ]; then
bin_value='\x01'
else
bin_value='\x00'
fi
printf "${bin_value}" | dd of="${tmp_file}" seek="${offset}" bs=1 count=1 conv=notrunc 2> /dev/null
has_change=1
log_msg "sys: ${name}=${value}"
fi
}
#
# This function writes to the BfSysCfg UEFI variable directly.
# Below are the offsets defined in UEFI which is 4(fixed header) plus the offset
# of the variable within the BfSysCfg struct. These offsets are not supposed to
# change in order to be backward compatible with previous releases.
# VARIABLE(Name) OFFSET(Byte) SIZE(Byte)
# SYS_ENABLE_SMMU 24 1
# SYS_DISABLE_SPMI 25 1
# SYS_ENABLE_2ND_EMMC 26 1
# SYS_BOOT_PROTECT 27 1
# SYS_ENABLE_SPCR 32 1
# SYS_DISABLE_PCIE 33 1
# SYS_ENABLE_OPTEE 34 1
# SYS_ENABLE_I2C0 36 1
#
sys_cfg()
{
local tmp_file=/tmp/.bfcfg-sysfs-data
local sys_cfg_sysfs=${efivars}/BfSysCfg-9c759c02-e5a3-45e4-acfc-c34a500628a6
if [ $dump_mode -eq 0 ] && [ ! -e "${sys_cfg_sysfs}" ]; then
log_msg "sys: failed to find the EFI variable"
return
fi
# shellcheck disable=SC2216
yes | cp -f ${sys_cfg_sysfs} ${tmp_file}
has_change=0
sys_cfg_one_byte ${tmp_file} "ENABLE_SMMU" 24 "${SYS_ENABLE_SMMU}"
sys_cfg_one_byte ${tmp_file} "DISABLE_SPMI" 25 "${SYS_DISABLE_SPMI}"
sys_cfg_one_byte ${tmp_file} "ENABLE_2ND_EMMC" 26 "${SYS_ENABLE_2ND_EMMC}"
sys_cfg_one_byte ${tmp_file} "BOOT_PROTECT" 27 "${SYS_BOOT_PROTECT}"
sys_cfg_one_byte ${tmp_file} "ENABLE_SPCR" 32 "${SYS_ENABLE_SPCR}"
sys_cfg_one_byte ${tmp_file} "DISABLE_PCIE" 33 "${SYS_DISABLE_PCIE}"
sys_cfg_one_byte ${tmp_file} "ENABLE_OPTEE" 34 "${SYS_ENABLE_OPTEE}"
sys_cfg_one_byte ${tmp_file} "ENABLE_I2C0" 36 "${SYS_ENABLE_I2C0}"
if [ ${has_change} -eq 1 ]; then
chattr -i ${sys_cfg_sysfs}
cp ${tmp_file} ${sys_cfg_sysfs}
sync
fi
rm -f ${tmp_file}
}
misc_cfg()
{
# Rshim MAC address.
local mac
local tmp_file=/tmp/.bfcfg-misc-data
if [ $dump_mode -eq 1 ]; then
mac=$(hexdump -v -e '/1 "%02x"' ${rshim_efi_mac_sysfs})
mac="${mac:8:2}:${mac:10:2}:${mac:12:2}:${mac:14:2}:${mac:16:2}:${mac:18:2}"
echo "misc: NET_RSHIM_MAC=${mac}"
elif [ -n "${NET_RSHIM_MAC}" ]; then
mac="${NET_RSHIM_MAC//:/\\x}"
mac="\\x07\\x00\\x00\\x00\\x${mac}"
printf "${mac}" > ${tmp_file}
chattr -i ${rshim_efi_mac_sysfs}
cp ${tmp_file} ${rshim_efi_mac_sysfs}
rm -f ${tmp_file}
log_msg "misc: NET_RSHIM_MAC=${NET_RSHIM_MAC}"
fi
}
#
# Parse the partition configuration and export $EFI_PART, $ROOT_PART and
# $PERSIST_PART
#
part_info()
{
local disk part value
for disk in {0..15}; do
eval "disk_name=\${DISK${disk}_NAME}"
# shellcheck disable=SC2154
[ -z "${disk_name}" ] && continue
for part in {0..15}; do
eval "value=\${DISK${disk}_PART${part}_MOUNT}"
if [ ."${value}" = ."/" ]; then
echo ROOT_PART="${disk_name}p${part}"
fi
eval "value=\${DISK${disk}_PART${part}_TYPE}"
if [ ."${value}" = ."EFI" ]; then
echo EFI_PART="${disk_name}p${part}"
fi
eval "value=\${DISK${disk}_PART${part}_PERSIST}"
if [ -n "${value}" ]; then
echo PERSIST_PART="${disk_name}p${part}"
fi
done
done
}
#
# Add header into a boot entry
# $1: output file
#
boot_cfg_add_header()
{
printf "\\x07\\x00\\x00\\x00\\x01\\x00\\x00\\x00" >> "$1"
}
#
# Add length into a boot entry
# $1: output file
# $2: length
#
boot_cfg_add_len()
{
len=$2
len=$(printf '%04x' "${len}")
printf "\\x${len:2:2}\\x${len:0:2}" >> "$1"
}
#
# Add tail into a boot entry
# $1: output file
#
boot_cfg_add_tail()
{
printf "\\x7f\\xff\\x04\\x00" >> "$1"
}
#
# Add name into a boot entry
# $1: output file
# $2: name
#
boot_cfg_add_name()
{
local idx name=$2
for ((idx=0; idx<${#name}; idx++)); do
printf "${name:$idx:1}" >> "$1"
printf "\\x00" >> "$1"
done
printf "\\x00\\x00" >> "$1"
}
#
# Add PCIe info into a boot entry
# $1: output file
# $2: port name
#
# Note: The quoting here is intentional, spaces need to be kept out
# shellcheck disable=SC2140
boot_cfg_add_pcie()
{
printf \
"\\x02\\x01\\x0c\\x00\\xd0\\x41\\x03\\x0a\\x00\\x00\\x00\\x00"\
"\\x01\\x01\\x06\\x00\\x00\\x00\\x01\\x01\\x06\\x00\\x00\\x00"\
"\\x01\\x01\\x06\\x00\\x00\\x02\\x01\\x01\\x06\\x00" >> "$1"
if [ "$2" = "NIC_P0" ]; then
printf "\\x00" >> "$1"
else
printf "\\x01" >> "$1"
fi
printf "\\x00" >> "$1"
}
#
# Add Eth/MAC info into a boot entry
# $1: output file
# $2: MAC address (xx:xx:xx:xx:xx:xx)
#
boot_cfg_add_eth()
{
local idx mac=$2
printf "\\x03\\x0b\\x25\\x00" >> "$1"
mac="\\x${mac//:/\\x}"
printf "${mac}" >> "$1"
for idx in {1..26}; do
printf "\\x00" >> "$1"
done
printf "\\x01" >> "$1"
}
#
# Add VLAN info into a boot entry
# $1: output file
# $2: decimal VLAN id
#
boot_cfg_add_vlan()
{
local vlan_id=$2
vlan_id=$(printf '%04x' "${vlan_id}")
printf "\\x03\\x14\\x06\\x00\\x${vlan_id:2:2}\\x${vlan_id:0:2}" >> "$1"
}
#
# Add IPv4 info into a boot entry
# $1: output file
#
boot_cfg_add_ipv4()
{
local idx
printf "\\x03\\x0c\\x1b\\x00" >> "$1"
for idx in {1..23}; do
printf "\\x00" >> "$1"
done
}
#
# Add IPv6 info into a boot entry
# $1: output file
#
boot_cfg_add_ipv6()
{
local idx
printf "\\x03\\x0d\\x3c\\x00" >> "$1"
for idx in {1..39}; do
printf "\\x00" >> "$1"
done
printf "\\x40" >> "$1"
for idx in {1..16}; do
printf "\\x00" >> "$1"
done
}
get_hca_p0_mac()
{
local dev devmac devid p0mac
p0mac="feffffffffff"
for dev in /sys/class/net/*; do
[ ! -f ${dev}/device/device ] && continue
devid=$(cat ${dev}/device/device)
[ ."${devid}" != ."0xa2d2" -a ."${devid}" != ."0xa2d6" ] && continue
devmac=$(cat ${dev}/address | sed 's/://g' | tr '[:upper:]' '[:lower:]')
if [ "${devmac}" \< "${p0mac}" ]; then
p0mac=${devmac}
fi
done
echo "0x${p0mac}"
}
#
# Boot Entry configuration
# Each entry BOOT<N> could have the following format:
# PXE:
# BOOT<N> = NET-<NIC_P0 | NIC_P1 | OOB | RSHIM>[.<vlan-id>]-<IPV4 | IPV6>
# UEFI Shell:
# BOOT<N> = UEFI_SHELL
# DISK: boot entries created during OS installation.
# BOOT<N> = DISK
# Example:
# BOOT0 = NET-NIC_P1-IPV4
# BOOT1 = DISK
#
boot_cfg()
{
local i tmp idx entry ifname proto mac vlan vlan_len disk_entry_idx
local shell_entry disk_entries boot_order
local tmp_dir=/tmp/.boot_cfg
local tmp_file=${tmp_dir}/boot
local tmp_vlan_file=${tmp_dir}/vlan
local value tmp_entry old_boot_order
[ $dump_mode -eq 1 ] && return
rm -rf ${tmp_dir} 2>/dev/null
mkdir -p ${tmp_dir}
old_boot_order=$(efibootmgr 2>/dev/null | grep BootOrder | awk '{print $2}' | tr ',' ' ')
# Check whether to preserve booting entries from disk.
for i in {0..32}; do
eval "entry=\${BOOT${i}}"
entry=$(echo "${entry}" | tr '[:lower:]' '[:upper:]')
[ -n "${entry}" ] && tmp=${entry}
if [ "${entry}" = "DISK" ]; then
for tmp_entry in ${old_boot_order}; do
value=$(efibootmgr -v 2>/dev/null | grep "^Boot${tmp_entry}\*" | grep -w HD)
if [ -n "${value}" ]; then
disk_entries="${disk_entries} ${tmp_entry}"
fi
done
break
elif [ "${entry}" = "UEFI_SHELL" ]; then
# Save the UEFI shell option
shell_entry=$(efibootmgr 2>/dev/null | grep "EFI Internal Shell" | cut -c5-8)
cp -f ${efivars}/Boot"${shell_entry}"* ${tmp_dir}/shell
fi
done
# Don't continue if nothing configured.
[ -z "${tmp}" ] && return
# Save disk entries
for i in ${disk_entries}; do
cp ${efivars}/Boot"${i}"* ${tmp_dir}/
done
# Remove all existing entries
rm -f ${efivars}/Boot00*
# Scan it again to add boot entries
idx=0
for i in {0..32}; do
eval "entry=\${BOOT${i}}"
[ -z "${entry}" ] && continue
rm -f ${tmp_file} 2>/dev/null
entry=$(echo "${entry}" | tr '[:lower:]' '[:upper:]')
# BOOT<N> = DISK
if [ ."${entry}" = ."DISK" ]; then
disk_entry_idx=64
for j in $disk_entries; do
tmp=$(printf '%04x' ${disk_entry_idx})
cp "${tmp_dir}/Boot${j}-${efi_global_var_guid}" "${efivars}/Boot${tmp}-${efi_global_var_guid}"
[ -n "${boot_order}" ] && boot_order="${boot_order},"
boot_order="${boot_order}${tmp}"
disk_entry_idx=$((disk_entry_idx + 1))
done
continue
fi
# BOOT<N> = UEFI_SHELL
if [ ."${entry}" = ."UEFI_SHELL" ]; then
if [ ! -e "${tmp_dir}/shell" ]; then
log_msg "boot: UEFI shell entry not found"
continue
fi
cp -f ${tmp_dir}/shell ${tmp_file}
else
# BOOT<N> = NET-<NIC_P0 | NIC_P1 | OOB | RSHIM>[.<vlan-id>]-<IPV4 | IPV6>
ifname=$(echo "${entry}" | cut -d '-' -f 2)
proto=$(echo "${entry}" | cut -d '-' -f 3)
vlan=""
vlan_len=0
if [[ "${ifname}" = *"."* ]]; then
vlan=$(echo "${ifname}" | cut -d '.' -f 2)
ifname=$(echo "${ifname}" | cut -d '.' -f 1)
vlan_len=6
fi
if [ -z "${ifname}" ] || [ -z "${proto}" ]; then
log_msg "boot: invalid format ${entry}"
continue
fi
if [ "${proto}" != "IPV4" ] && [ "${proto}" != "IPV6" ]; then
log_msg "boot: invalid format ${entry}, need IPV4 or IPV6"
continue
fi
boot_cfg_add_header "${tmp_file}"
case "${ifname}" in
OOB)
mac=$(cat ${oob_mac_sysfs} 2>/dev/null)
if [ -z "${mac}" ]; then
log_msg "boot: failed to get MAC for ${entry}"
continue
fi
if [ "${proto}" = "IPV4" ]; then
boot_cfg_add_len "${tmp_file}" $((68 + vlan_len))
else
boot_cfg_add_len "${tmp_file}" $((101 + vlan_len))
fi
boot_cfg_add_name "${tmp_file}" "${entry}"
;;
RSHIM)
mac=$(hexdump -v -e '/1 " %02x"' ${rshim_efi_mac_sysfs} 2>/dev/null)
if [ -z "${mac}" ]; then
log_msg "boot: failed to get MAC for ${entry}"
continue
fi
mac="${mac// /:}"
mac=${mac: -17}
if [ "${proto}" = "IPV4" ]; then
boot_cfg_add_len "${tmp_file}" $((68 + vlan_len))
else
boot_cfg_add_len "${tmp_file}" $((101 + vlan_len))
fi
boot_cfg_add_name "${tmp_file}" "${entry}"
;;
NIC_P0|NIC_P1)
mac=$(get_hca_p0_mac)
if [ -z "${mac}" ] || [ ."${mac}" = ."N/A" ]; then
log_msg "boot: failed to get MAC for ${entry}"
continue
fi
if [ "${ifname}" = "NIC_P1" ]; then
mac=$((mac + 1))
fi
mac=$(printf '%012x' ${mac})
# shellcheck disable=SC2116,SC2096,SC2086
mac=$(echo ${mac:0:2}:${mac:2:2}:${mac:4:2}:${mac:6:2}:${mac:8:2}:${mac:10:2})
if [ "${proto}" = "IPV4" ]; then
boot_cfg_add_len "${tmp_file}" $((104 + vlan_len))
else
boot_cfg_add_len "${tmp_file}" $((137 + vlan_len))
fi
boot_cfg_add_name ${tmp_file} "${entry}"
boot_cfg_add_pcie "${tmp_file}" "${ifname}"
;;
*)
continue
;;
esac
boot_cfg_add_eth "${tmp_file}" "${mac}"
# Remove old VLAN configuration
mac=$(echo "${mac}" | tr '[:lower:]' '[:upper:]')
mac="${mac//:/}"
eval "tmp=\${${ifname}_VLAN_SET}"
if [ -z "${tmp}" ]; then
eval "${ifname}_VLAN_SET=1"
chattr -i "${efivars}/${mac}-${efi_vlan_var_guid}" 2>/dev/null
rm -f "${efivars}/${mac}-${efi_vlan_var_guid}" 2>/dev/null
fi
# Add new VLAN if specified
if [ -n "${vlan}" ]; then
tmp=$(printf '%04x' "${vlan}")
if [ ! -e "${efivars}/${mac}-${efi_vlan_var_guid}" ]; then
printf "\\x07\\x00\\x00\\x00\\x${tmp:2:2}\\x${tmp:0:2}" > \
"${efivars}/${mac}-${efi_vlan_var_guid}"
else
chattr -i "${efivars}/${mac}-${efi_vlan_var_guid}"
cp "${efivars}/${mac}-${efi_vlan_var_guid}" ${tmp_vlan_file}
printf "\\x${tmp:2:2}\\x${tmp:0:2}" >> ${tmp_vlan_file}
cp ${tmp_vlan_file} "${efivars}/${mac}-${efi_vlan_var_guid}"
fi
boot_cfg_add_vlan "${tmp_file}" "${vlan}"
fi
if [ "${proto}" = "IPV4" ]; then
boot_cfg_add_ipv4 "${tmp_file}"
else
boot_cfg_add_ipv6 "${tmp_file}"
fi
boot_cfg_add_tail "${tmp_file}"
fi
if [ -e "${tmp_file}" ]; then
tmp=$(printf '%04x' ${idx})
cp ${tmp_file} "${efivars}/Boot${tmp}-${efi_global_var_guid}"
log_msg "boot: add Boot${tmp}(${entry})"
[ -n "${boot_order}" ] && boot_order="${boot_order},"
boot_order="${boot_order}${tmp}"
idx=$((idx + 1))
fi
done
efibootmgr -o "${boot_order}" >/dev/null
log_msg "boot: set boot order ${boot_order}"
rm -rf ${tmp_dir} 2>/dev/null
}
usage()
{
echo "syntax: bfcfg [--help|-h] [--dump|-d] [--part-info|-p]"
}
# Source the configuration if exists.
# shellcheck source=/dev/null
[ -e "${cfg_file}" ] && . ${cfg_file}
# Parse the arguments.
options=$(getopt -n bfcfg -o dhp -l dump,help,part-info -- "$@")
eval set -- "$options"
while [ "$1" != -- ]; do
case $1 in
--dump|-d) dump_mode=1 ;;
--help|-h) usage; exit 0 ;;
--part-info|-p) part_info; exit 0 ;;
esac
shift
done
shift
# Start a new log file.
rm -f ${log_file} >/dev/null
log_msg "bfcfg (ver ${bfcfg_version})"
log_msg "$(date)"
log_msg
# Mount the efi variables.
test "$(ls -A ${efivars})" || mount -t efivarfs none ${efivars}
mfg_cfg
sys_cfg
misc_cfg
boot_cfg
sync