Skip to content

Commit

Permalink
Add subcommand parsing and automation
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpenedblade committed Jun 15, 2024
1 parent 5466a26 commit 02b6d45
Showing 1 changed file with 112 additions and 42 deletions.
154 changes: 112 additions & 42 deletions docs/tools/firmware.sh
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,108 @@ EOF
set -euo pipefail

verbose=""
while getopts "vhxp" option; do
subcmd=""
args=""
while getopts "vhx" option; do
case $option in
v) verbose="-v" ;;
h) echo "usage: $0 [-vhxp] [fw_dir_path output_archive_path]"; exit 0 ;;
h)
cat <<- EOF
usage: $0 [-vhx] subcommand [subcmd args]
Subcommands:
rename_only /path/to/firmware archive.tar
copy_to_efi
create_archive
create_package
get_from_efi
get_from_macos
get_from_online
EOF
exit 0 ;;
x) set -x;;
p) rename_firmware "${@:2:2}" ${verbose} && exit 0 ;;
?) exit 1 ;;
esac
done

if [[ "${1-}" == -* ]]; then
subcmd="${2-}"
args=( "${@:3}" )
else
subcmd="${1-}"
args=( "${@:2}" )
fi

if [[ "$subcmd" = "" ]]; then
case "$(uname -s)" in
(Darwin)
echo "Detected macOS"
cat <<- EOF
How do you want to copy the firmware to Linux?
1. Copy the firmware to the EFI partition and run the same script on Linux to retrieve it.
2. Create a tarball of the firmware and extract it to Linux.
3. Create a Linux specific package which can be installed using a package manager.
Note: Option 2 and 3 require additional software like python3 and tools specific for your package manager. Requirements will be told as you proceed further.
EOF
read -r choice
case ${choice} in
(1) subcmd="copy_to_efi" ;;
(2) subcmd="create_archive" ;;
(3) subcmd="create_package"
echo -e "\nWhat package manager does your Linux distribution use?\n"
echo "1. apt"
echo "2. dnf"
echo "3. pacman"
read -r target_pkg_manager
case ${target_pkg_manager} in
(1) target_pkg_manager="apt" ;;
(2) target_pkg_manager="rpm" ;;
(3) target_pkg_manager="pacman" ;;
(*) echo -e "\nError: Invalid option!" && exit 1 ;;
esac
;;
(*) echo -e "\nError: Invalid option!" && exit 1 ;;
esac
;;
(Linux)
echo "Detected Linux"
cat <<- EOF
How do you want to copy the firmware to Linux?
1. Retrieve the firmware from the EFI partition.
2. Retrieve the firmware directly from macOS.
3. Download a macOS Recovery Image from Apple and extract the firmware from there.
Note: If you are choosing Option 1, then make sure you have run the same script on macOS before and chose Option 1 (Copy the firmware to the EFI partition and run the same script on Linux to retrieve it) there.
EOF
read -r choice
case ${choice} in
(1)
subcmd="get_from_efi" ;;
(2)
subcmd="get_from_macos" ;;
(3)
subcmd="get_from_online" ;;
(*)
echo -e "\nError: Invalid option!"
exit 1
;;
esac
;;
(*)
echo "Error: unsupported platform"
;;
esac
fi

if [[ "$subcmd" = "" ]]; then
exit 1
fi

aur_install() {
local aur_package=$1
dir=$(mktemp -d)
Expand Down Expand Up @@ -686,6 +778,11 @@ create_arch_pkg () {
EOF
}

if [[ ${subcmd} = "rename_only" ]]; then
rename_firmware "${args[@]}" ${verbose}
exit 0
fi

os=$(uname -s)
case "$os" in
(Darwin)
Expand All @@ -707,19 +804,8 @@ case "$os" in
EOF
fi
cat <<- EOF
How do you want to copy the firmware to Linux?
1. Copy the firmware to the EFI partition and run the same script on Linux to retrieve it.
2. Create a tarball of the firmware and extract it to Linux.
3. Create a Linux specific package which can be installed using a package manager.
Note: Option 2 and 3 require additional software like python3 and tools specific for your package manager. Requirements will be told as you proceed further.
EOF
read -r choice
case ${choice} in
(1)
case ${subcmd} in
("copy_to_efi")
echo -e "\nMounting the EFI partition"
EFILABEL=$(diskutil info disk0s1 | grep "Volume Name" | cut -d ":" -f 2 | xargs)
sudo diskutil mount disk0s1
Expand Down Expand Up @@ -757,7 +843,7 @@ case "$os" in
sudo umount /tmp/apple-wifi-efi
EOF
;;
(2)
("create_archive")

echo -e "\nCreating a tarball of the firmware"
create_firmware_archive /usr/share/firmware "$HOME/Downloads/firmware.tar" ${verbose}
Expand All @@ -773,20 +859,15 @@ case "$os" in
sudo modprobe hci_bcm4377
EOF
;;
(3)
echo -e "\nWhat package manager does your Linux distribution use?\n"
echo "1. apt"
echo "2. dnf"
echo "3. pacman"
read -r package
case ${package} in
(1)
("create_package")
case ${target_pkg_manager} in
("apt")
create_deb
;;
(2)
("dnf")
create_rpm
;;
(3)
("pacman")
create_arch_pkg
;;
(*)
Expand All @@ -813,19 +894,8 @@ case "$os" in
EOF
exit 1
fi
cat <<- EOF
How do you want to copy the firmware to Linux?
1. Retrieve the firmware from the EFI partition.
2. Retrieve the firmware directly from macOS.
3. Download a macOS Recovery Image from Apple and extract the firmware from there.
Note: If you are choosing Option 1, then make sure you have run the same script on macOS before and chose Option 1 (Copy the firmware to the EFI partition and run the same script on Linux to retrieve it) there.
EOF
read -r choice
case ${choice} in
(1)
case ${subcmd} in
("get_from_efi")
echo -e "\nRe-mounting the EFI partition"
mountpoint=$(mktemp -d)
workdir=$(mktemp -d)
Expand Down Expand Up @@ -865,7 +935,7 @@ case "$os" in
sudo rmdir "$mountpoint"
echo -e "\nDone!"
;;
(2)
("get_from_macos")
echo -e "\nChecking for missing dependencies"
# Load the apfs driver, and install if missing
sudo modprobe ${verbose} apfs 2>/dev/null || install_package linux-apfs-rw
Expand Down Expand Up @@ -917,7 +987,7 @@ case "$os" in
unmount_macos_and_cleanup
echo "Done!"
;;
(3)
("get_from_online")
# Detect whether curl and dmg2img are installed
echo -e "\nChecking for missing dependencies"
curl --version >/dev/null 2>&1 || install_package curl
Expand Down

0 comments on commit 02b6d45

Please sign in to comment.