Skip to content

Commit e8c604d

Browse files
committed
Add comments and separate long commands.
1 parent 1c6716f commit e8c604d

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

bcd-sys.sh

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RED='\033[1;31m'
1717
BGTYELLOW='\033[1;93m'
1818
NC='\033[0m' # No Color
1919

20+
# Show basic usage.
2021
usage () {
2122
echo "Usage: $(basename $0) <source> [options] <system>"
2223
echo
@@ -27,6 +28,7 @@ echo "-h, --help Display full usage information."
2728
exit 1
2829
}
2930

31+
# Show full help.
3032
help () {
3133
echo "Usage: $(basename $0) <source> [options] <system>"
3234
echo
@@ -35,6 +37,7 @@ echo
3537
exit
3638
}
3739

40+
# Remove existing boot loader and resume entries for matching device (11000001).
3841
remove_duplicates () {
3942
if [[ "$3" == "uefi" ]]; then
4043
bcdpath="$2/EFI/Microsoft/Boot/BCD"
@@ -74,6 +77,7 @@ do
7477
done
7578
}
7679

80+
# Build main and recovery stores using blank BCD-NEW file and reg templates.
7781
build_stores () {
7882
if [[ "$verbose" == "true" ]]; then echo "Build the main BCD store..."; fi
7983
$resdir/winload.sh "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}" "${11}" "${12}" > $tmpdir/winload.txt
@@ -95,6 +99,7 @@ elif [[ "$3" == "bios" ]]; then
9599
fi
96100
}
97101

102+
# Build new Windows entry and update existing BCD stores.
98103
update_winload () {
99104
remove_duplicates "$1" "$2" "$3" "${11}" "${12}"
100105
if [[ "$verbose" == "true" ]]; then echo "Update main BCD hive with new entries..."; fi
@@ -109,6 +114,7 @@ elif [[ "$3" == "bios" ]]; then
109114
fi
110115
}
111116

117+
# Copy the WBM files for the specified firmware and set the file attributes the same as bcdboot.
112118
copy_bootmgr () {
113119
if [[ "$3" == "uefi" ]]; then
114120
if [[ "$verbose" == "true" ]]; then echo "Copy the EFI boot files to the ESP..."; fi
@@ -137,6 +143,8 @@ elif [[ "$3" == "bios" ]]; then
137143
fi
138144
}
139145

146+
# Used when no syspath is specified in the arguments.
147+
# Look for an ESP or active primary partition on the Windows disk or the first disk (/dev/sda).
140148
get_syspath () {
141149
firmware="$1"
142150
windisk="$2"
@@ -196,6 +204,8 @@ elif [[ "$firmware" == "bios" || "$firmware" == "both" ]]; then
196204
fi
197205
}
198206

207+
# Used when a syspath is provided in the arguments.
208+
# Find the block device (and active partition if BIOS/BOTH) for the specified mount point.
199209
get_device () {
200210
firmware="$1"
201211
syspath="$2"
@@ -223,6 +233,7 @@ elif [[ "$firmware" == "bios" || "$firmware" == "both" ]]; then
223233
fi
224234
}
225235

236+
# Load the NBD module if needed then find a free block device to use.
226237
attach_vdisk () {
227238
errormsg=$(modinfo nbd 2>&1>/dev/null)
228239
if [[ -z $(command -v qemu-nbd) ]]; then missing+=" qemu-utils"; fi
@@ -243,6 +254,7 @@ done
243254
sudo qemu-nbd -c "$vrtdisk" "$imgpath"
244255
}
245256

257+
# Display partitions on virtual disk and mount the specified volume.
246258
mount_vpart () {
247259
vrtpath="/mnt/virtwin"
248260
mntopts="rw,nosuid,nodev,relatime,uid=$(id -u),gid=$(id -g),iocharset=utf8,windows_names"
@@ -258,6 +270,7 @@ done
258270
sudo mkdir -p "$vrtpath" && sudo mount -t ntfs3 -o"$mntopts" /dev/$vtwinpart "$vrtpath"
259271
}
260272

273+
# Unmount volume and detach virtual disk then unload NBD module if no longer needed.
261274
umount_vpart () {
262275
if [[ "$verbose" == "true" ]]; then echo "Removing temporary VHDX mount point..."; fi
263276
sudo umount "$vrtpath" && sudo rm -rf "$vrtpath"
@@ -268,16 +281,19 @@ if [[ "$unloadnbd" == "true" ]]; then
268281
fi
269282
}
270283

284+
# Check for the WBM in the current firmware boot options.
271285
get_wbmoption () {
272286
wbmoptnum=$(efibootmgr | grep "Windows Boot Manager" | awk '{print $1}' | sed 's/Boot//;s/\*//')
273287
}
274288

289+
# Update the WBM firmware option and device data in the BCD entry.
275290
create_wbmfwvar () {
276291
if [[ "$verbose" == "true" ]]; then echo "Update main BCD with current WBM firware variable..."; fi
277292
$resdir/wbmfwvar.sh $1 > $tmpdir/wbmfwvar.txt
278293
sudo hivexsh -w "$2/EFI/Microsoft/Boot/BCD" -f $tmpdir/wbmfwvar.txt
279294
}
280295

296+
# Remove hivexsh scripts and BCD files created during the build process.
281297
cleanup () {
282298
if [[ "$verbose" == "true" ]]; then echo "Clean up temporary files..."; fi
283299
rm -f $tmpdir/winload.txt $tmpdir/recovery.txt $tmpdir/wbmfwvar.txt $tmpdir/BCD-Windows $tmpdir/BCD-Recovery
@@ -290,12 +306,14 @@ if [[ ! -z "$missing" ]]; then
290306
fi
291307
}
292308

309+
# Script starts here.
293310
if [[ $(uname) != "Linux" ]]; then echo "Unsupported platform detected."; exit 1; fi
294311

295312
if [[ $# -eq 0 ]]; then
296313
usage
297314
fi
298315

316+
# Check for required packages that are missing.
299317
if [[ -z $(command -v hivexsh) ]]; then missing+=" hivex"; fi
300318
if [[ -z $(command -v hivexregedit) ]]; then missing+=" hivexregedit"; fi
301319
if [[ -z $(command -v setfattr) ]]; then missing+=" attr/setfattr"; fi
@@ -357,13 +375,16 @@ while (( "$#" )); do
357375
done
358376
shopt -u nocasematch
359377

378+
# Check if source is a virtual disk file.
360379
if [[ "$winpath" == *".vhdx"* && -f "$winpath" ]];then
361380
virtual="true"
362381
imgpath="$winpath"
363382
attach_vdisk
364383
sleep 1 && mount_vpart
365384
fi
366385

386+
# Check source for path to the WBM files then get the block device.
387+
# Get the mount point, file path and block device that contains the virtual disk file.
367388
if [[ -d "$winpath/Windows/Boot" ]]; then
368389
windisk=$(lsblk -o path,mountpoint | grep "$winpath" | awk '{print $1}' | sed 's/[0-9]\+$//')
369390
elif [[ "$virtual" == "true" && -d "$vrtpath/Windows/Boot" ]]; then
@@ -377,6 +398,12 @@ else
377398
exit 1
378399
fi
379400

401+
# Perform the actions appropriate for the specified firmware type.
402+
# Compare the WBM product versions of the source and system volumes.
403+
# Copy the boot files if missing or older than the source version.
404+
# Create new BCD stores or update existing ones with new entries.
405+
# Create a WBM firmware entry in the first or last position when needed.
406+
# Unmount the virtual disk and remove the temporary hive scripts/files.
380407
if [[ "$firmware" != "uefi" && "$firmware" != "bios" && "$firmware" != "both" ]]; then
381408
echo -e "${RED}Unsupport firmware: Only UEFI, BIOS or BOTH.${NC}"
382409
exit 1
@@ -443,14 +470,16 @@ else
443470
else
444471
copy_bootmgr "$winpath" "$syspath" "$fwmode"
445472
fi
446-
build_stores "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" "$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
473+
build_stores "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" \
474+
"$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
447475
if [[ "$setfwmod" == "false" && "$efibootvars" == "true" ]]; then
448476
if [[ ! -z "$wbmoptnum" ]]; then
449477
if [[ "$verbose" == "true" ]]; then echo "Remove the current Windows Boot Manager option..."; fi
450478
sudo efibootmgr -b "$wbmoptnum" -B > /dev/null
451479
fi
452480
if [[ "$verbose" == "true" ]]; then echo "Add the Windows Boot Manager to the firmware..."; fi
453-
sudo efibootmgr -c -d "$efidisk" -p "$efinum" -l "$wbmefipath" -L "Windows Boot Manager" -@ $resdir/Templates/wbmoptdata.bin > /dev/null
481+
sudo efibootmgr -c -d "$efidisk" -p "$efinum" -l "$wbmefipath" -L "Windows Boot Manager" \
482+
-@ $resdir/Templates/wbmoptdata.bin > /dev/null
454483
get_wbmoption && create_wbmfwvar "$wbmoptnum" "$syspath"
455484
if [[ "$setwbmlast" == "true" ]]; then
456485
bootorder=$(efibootmgr | grep BootOrder: | awk '{print $2}' | sed "s/$wbmoptnum,//")
@@ -473,11 +502,13 @@ else
473502
copy_bootmgr "$winpath" "$syspath" "$fwmode"
474503
fi
475504
fi
476-
build_stores "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" "$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
505+
build_stores "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" \
506+
"$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
477507
if [[ "$setfwmod" == "false" && "$efibootvars" == "true" ]]; then
478508
if [[ -z "$wbmoptnum" ]]; then
479509
if [[ "$verbose" == "true" ]]; then echo "Add the Windows Boot Manager to the firmware..."; fi
480-
sudo efibootmgr -c -d "$efidisk" -p "$efinum" -l "$wbmefipath" -L "Windows Boot Manager" -@ $resdir/Templates/wbmoptdata.bin > /dev/null
510+
sudo efibootmgr -c -d "$efidisk" -p "$efinum" -l "$wbmefipath" -L "Windows Boot Manager" \
511+
-@ $resdir/Templates/wbmoptdata.bin > /dev/null
481512
get_wbmoption
482513
if [[ "$setwbmlast" == "true" ]]; then
483514
bootorder=$(efibootmgr | grep BootOrder: | awk '{print $2}' | sed "s/$wbmoptnum,//")
@@ -504,10 +535,12 @@ else
504535
sudo mv "$syspath/EFI/BCD-BOOT" "$syspath/EFI/Microsoft/Boot/BCD"
505536
sudo mv "$syspath/EFI/BCD-RECOVERY" "$syspath/EFI/Microsoft/Recovery/BCD"
506537
fi
507-
update_winload "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" "$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
538+
update_winload "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" \
539+
"$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
508540
if [[ "$setfwmod" == "false" && "$efibootvars" == "true" && -z "$wbmoptnum" ]]; then
509541
if [[ "$verbose" == "true" ]]; then echo "Add the Windows Boot Manager to the firmware..."; fi
510-
sudo efibootmgr -c -d "$efidisk" -p "$efinum" -l "$wbmefipath" -L "Windows Boot Manager" -@ $resdir/Templates/wbmoptdata.bin > /dev/null
542+
sudo efibootmgr -c -d "$efidisk" -p "$efinum" -l "$wbmefipath" -L "Windows Boot Manager" \
543+
-@ $resdir/Templates/wbmoptdata.bin > /dev/null
511544
get_wbmoption && create_wbmfwvar "$wbmoptnum" "$syspath"
512545
if [[ "$setwbmlast" == "true" ]]; then
513546
bootorder=$(efibootmgr | grep BootOrder: | awk '{print $2}' | sed "s/$wbmoptnum,//")
@@ -585,7 +618,8 @@ else
585618
else
586619
copy_bootmgr "$winpath" "$syspath" "$fwmode" "$sysfstype"
587620
fi
588-
build_stores "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" "$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
621+
build_stores "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" \
622+
"$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
589623
elif [[ "$sysbtmgr" == "true" && "$clean" == "true" ]]; then
590624
if [[ "$verbose" == "true" ]]; then echo "Remove current BCD store..."; fi
591625
sudo rm -f "$syspath"/Boot/BCD
@@ -600,7 +634,8 @@ else
600634
fi
601635
fi
602636
fi
603-
build_stores "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" "$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
637+
build_stores "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" \
638+
"$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
604639
else
605640
createbcd="false"
606641
if [[ "$sysuwfver" != "$localuwfver" || "$sysvhdver" != "$localvhdver" ]]; then
@@ -618,7 +653,8 @@ else
618653
sudo mv "$syspath/BCD" "$syspath/Boot"
619654
fi
620655
fi
621-
update_winload "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" "$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
656+
update_winload "$winpath" "$syspath" "$fwmode" "$setfwmod" "$createbcd" "$prewbmdef" \
657+
"$prodname" "$locale" "$verbose" "$virtual" "$vrtpath" "$imgstring"
622658
fi
623659
if [[ "$syspath" == "/mnt/winsys" ]]; then
624660
if [[ "$verbose" == "true" ]]; then echo "Removing temporary system mount point..."; fi

0 commit comments

Comments
 (0)