Skip to content

Commit

Permalink
Pull more code out into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpenedblade committed Jun 16, 2024
1 parent b810a45 commit 0e0be05
Showing 1 changed file with 66 additions and 57 deletions.
123 changes: 66 additions & 57 deletions docs/tools/firmware.sh
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,61 @@ create_arch_pkg () {
EOF
}

download_recovery_image() {
output_file="$1"
echo -e "\nDownloading macOS Recovery Image"
if [[ ${verbose} = "-v" ]]
then
curl -O https://raw.githubusercontent.com/kholia/OSX-KVM/master/fetch-macOS-v2.py
else
curl -s -O https://raw.githubusercontent.com/kholia/OSX-KVM/master/fetch-macOS-v2.py
fi
echo -e "\nNote: In order to get complete firmware files, download macOS Monterey or later.\n"
python3 fetch-macOS-v2.py
echo -e "\nConverting image from .dmg to .img"
if [[ ${verbose} = "-v" ]]
then
dmg2img -v BaseSystem.dmg "$output_file"
else
dmg2img -s BaseSystem.dmg "$output_file"
fi
}

mount_firmware_vol() {
echo -e "\nMounting the macOS volume"
macosvol=/dev/$(lsblk -o NAME,FSTYPE | grep nvme0n1 | grep apfs | head -1 | awk '{print $1}' | rev | cut -c -9 | rev)
mnt_dir="$(mktemp -d)"
cd "$mnt_dir"
for i in 0 1 2 3 4 5
do
mkdir -p "vol${i}"
if [[ ${verbose} = "-v" ]]
then
sudo mount -v -o vol=${i} "${macosvol}" "vol${i}" || true
else
sudo mount -o vol=${i} "${macosvol}" "vol${i}" 2>/dev/null || true
fi

if [ -d "vol${i}/usr/share/firmware" ]
then
echo "${mnt_dir}/vol${i}"
return 0
else
if [[ ${verbose} = "-v" ]]
then
sudo umount -v "vol${i}" || true
rmdir -v "vol${i}"
else
sudo umount "vol${i}" 2>/dev/null || true
rmdir "vol${i}"
fi
fi
done
echo -e "Could not find location of firmware. Aborting!"
rmdir "$mnt_dir"
exit 1
}

os=$(uname -s)
case "$os" in
(Darwin)
Expand Down Expand Up @@ -864,46 +919,19 @@ case "$os" in
sudo modprobe ${verbose} apfs 2>/dev/null || install_package linux-apfs-rw
unmount_macos_and_cleanup () {
sudo rm -r ${verbose} "${workdir}" || true
for i in 0 1 2 3 4 5
do
if [[ ${verbose} = "-v" ]]
then
sudo umount -v "${macosdir}/vol${i}" || true
else
sudo umount "${macosdir}/vol${i}" 2>/dev/null || true
fi
done
sudo rm -r ${verbose} "${macosdir}" || true
}

echo -e "\nMounting the macOS volume"
workdir=$(mktemp -d)
macosdir=$(mktemp -d)
macosvol=/dev/$(lsblk -o NAME,FSTYPE | grep nvme0n1 | grep apfs | head -1 | awk '{print $1}' | rev | cut -c -9 | rev)
fwlocation=""
for i in 0 1 2 3 4 5
do
mkdir -p "${macosdir}/vol${i}"
if [[ ${verbose} = "-v" ]]
then
sudo mount -v -o vol=${i} "${macosvol}" "${macosdir}/vol${i}" || true
sudo umount -v "${macosdir}/vol${i}" || true
else
sudo mount -o vol=${i} "${macosvol}" "${macosdir}/vol${i}" 2>/dev/null || true
fi

if [ -d "${macosdir}/vol${i}/usr/share/firmware" ]
then
fwlocation="${macosdir}/vol${i}/usr/share/firmware"
sudo umount "${macosdir}/vol${i}" 2>/dev/null || true
fi
done
sudo rm -r ${verbose} "${macosdir}" || true
}

workdir=$(mktemp -d)
macosdir="$(mount_firmware_vol)"
echo "Getting firmware"
if [[ ${fwlocation} = "" ]]
then
echo -e "Could not find location of firmware. Aborting!"
unmount_macos_and_cleanup
exit 1
fi
create_firmware_archive "${fwlocation}" "${workdir}/firmware-renamed.tar" "${verbose}" || (echo -e "\nCouldn't extract firmware. Try running the script again. If error still persists, try restarting your Mac and then run the script again, or choose some other method." && unmount_macos_and_cleanup && exit 1)
create_firmware_archive "${macosdir}/usr/share/firmware" "${workdir}/firmware-renamed.tar" "${verbose}" || (echo -e "\nCouldn't extract firmware. Try running the script again. If error still persists, try restarting your Mac and then run the script again, or choose some other method." && unmount_macos_and_cleanup && exit 1)
sudo tar ${verbose} -xC /lib/firmware/brcm -f "${workdir}/firmware-renamed.tar"
reload_kernel_modules
echo "Cleaning up"
Expand All @@ -917,37 +945,18 @@ case "$os" in
dmg2img >/dev/null 2>&1 || install_package dmg2img
cleanup_dmg () {
sudo rm -r ${verbose} "${workdir}"
sudo umount ${verbose} "${loopdevice}"
sudo umount ${verbose} "${loopdev}"
sudo rm -r ${verbose} "${imgdir}"
sudo losetup -d /dev/"${loopdev}"
}

echo -e "\nDownloading macOS Recovery Image"
workdir=$(mktemp -d)
imgdir=$(mktemp -d)
cd "${workdir}"
if [[ ${verbose} = "-v" ]]
then
curl -O https://raw.githubusercontent.com/kholia/OSX-KVM/master/fetch-macOS-v2.py
else
curl -s -O https://raw.githubusercontent.com/kholia/OSX-KVM/master/fetch-macOS-v2.py
fi
echo -e "\nNote: In order to get complete firmware files, download macOS Monterey or later.\n"
python3 fetch-macOS-v2.py
echo -e "\nConverting image from .dmg to .img"
if [[ ${verbose} = "-v" ]]
then
dmg2img -v BaseSystem.dmg fw.img
else
dmg2img -s BaseSystem.dmg fw.img
fi
download_recovery_image "${workdir}/fw.img"
echo "Mounting image"
loopdev=$(losetup -f | cut -d "/" -f 3)
sudo losetup -P "${loopdev}" fw.img
loopdevice=/dev/$(lsblk -o KNAME,TYPE,MOUNTPOINT -n | grep "${loopdev}" | tail -1 | awk '{print $1}')
sudo mount ${verbose} "${loopdevice}" "${imgdir}"
sudo mount ${verbose} "${loopdev}" "${imgdir}"
echo "Getting firmware"
cd - >/dev/null
create_firmware_archive "${imgdir}/usr/share/firmware" "${workdir}/firmware-renamed.tar" ${verbose} || (echo -e "\nCouldn't extract firmware. Try choosing some other macOS version (should be Monterey or later). If error still persists, try restarting your Mac and then run the script again." && cleanup_dmg && exit 1)
sudo tar ${verbose} -xC /lib/firmware/brcm -f "${workdir}/firmware-renamed.tar"
reload_kernel_modules
Expand Down

0 comments on commit 0e0be05

Please sign in to comment.