Skip to content

Commit

Permalink
add Utilities directory
Browse files Browse the repository at this point in the history
  • Loading branch information
m03315 committed May 1, 2021
1 parent edcde57 commit d79156f
Show file tree
Hide file tree
Showing 30 changed files with 1,414 additions and 0 deletions.
Binary file added OpenCore/Utilities/CreateVault/RsaTool
Binary file not shown.
70 changes: 70 additions & 0 deletions OpenCore/Utilities/CreateVault/create_vault.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

# create_vault.sh
#
#
# Created by Rodion Shingarev on 13.04.19.
#
OCPath="$1"

if [ "${OCPath}" = "" ]; then
echo "Usage ./create_vault.sh path/to/EFI/OC"
exit 1
fi

if [ ! -d "${OCPath}" ]; then
echo "Path $OCPath is missing!"
exit 1
fi

if [ ! -x /usr/bin/find ] || [ ! -x /bin/rm ] || [ ! -x /usr/bin/sed ] || [ ! -x /usr/bin/xxd ]; then
echo "Unix environment is broken!"
exit 1
fi

if [ ! -x /usr/libexec/PlistBuddy ]; then
echo "PlistBuddy is missing!"
exit 1
fi

if [ ! -x /usr/bin/shasum ]; then
echo "shasum is missing!"
exit 1
fi

abort() {
/bin/rm -rf vault.plist vault.sig /tmp/vault_hash
echo "Fatal error: ${1}!"
exit 1
}

echo "Chose ${OCPath} for hashing..."

cd "${OCPath}" || abort "Failed to reach ${OCPath}"
/bin/rm -rf vault.plist vault.sig || abort "Failed to cleanup"
/usr/libexec/PlistBuddy -c "Add Version integer 1" vault.plist || abort "Failed to set vault.plist version"

echo "Hashing files in ${OCPath}..."

/usr/bin/find . -not -path '*/\.*' -type f \
\( ! -iname ".*" \) \
\( ! -iname "vault.*" \) \
\( ! -iname "OpenCore.efi" \) | while read -r fname; do
fname="${fname#"./"}"
wname="${fname//\//\\\\}"
shasum=$(/usr/bin/shasum -a 256 "${fname}") || abort "Failed to hash ${fname}"
sha=$(echo "$shasum" | /usr/bin/sed 's/^\([a-f0-9]\{64\}\).*/\1/') || abort "Illegit hashsum"
if [ "${#sha}" != 64 ] || [ "$(echo "$sha"| /usr/bin/sed 's/^[a-f0-9]*$//')" ]; then
abort "Got invalid hash: ${sha}!"
fi

echo "${wname}: ${sha}"

echo "${sha}" | /usr/bin/xxd -r -p > /tmp/vault_hash || abort "Hashing failure"
/usr/libexec/PlistBuddy -c "Import Files:'${wname}' /tmp/vault_hash" vault.plist || abort "Failed to append vault.plist!"
done

/bin/rm -rf /tmp/vault_hash

echo "All done!"
exit 0
88 changes: 88 additions & 0 deletions OpenCore/Utilities/CreateVault/sign.command
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/sh

abort() {
echo "Fatal error: ${1}!"
exit 1
}

cleanup() {
echo "Cleaning up keys"
rm -rf "${KeyPath}"
}

if [ ! -x /usr/bin/dirname ] || [ ! -x /bin/chmod ] || [ ! -x /bin/mkdir ] || [ ! -x /usr/bin/openssl ] || [ ! -x /bin/rm ] || [ ! -x /usr/bin/strings ] || [ ! -x /usr/bin/grep ] || [ ! -x /usr/bin/cut ] || [ ! -x /bin/dd ] || [ ! -x /usr/bin/uuidgen ] ; then
abort "Unix environment is broken!"
fi

cd "$(/usr/bin/dirname "$0")" || abort "Failed to enter working directory!"

OCPath="$1"

if [ "$OCPath" = "" ]; then
OCPath=../../EFI/OC
fi

KeyPath="/tmp/Keys-$(/usr/bin/uuidgen)"
OCBin="${OCPath}/OpenCore.efi"
RootCA="${KeyPath}/ca.pem"
PrivKey="${KeyPath}/privatekey.cer"
PubKey="${KeyPath}/vault.pub"

if [ ! -d "${OCPath}" ]; then
abort "Path ${OCPath} is missing!"
fi

if [ ! -f "${OCBin}" ]; then
abort "OpenCore.efi is missing!"
fi

if [ ! -x ./RsaTool ] || [ ! -x ./create_vault.sh ]; then
if [ -f ./RsaTool ]; then
/bin/chmod a+x ./RsaTool || abort "Failed to set permission for RsaTool"
else
abort "Failed to find RsaTool!"
fi

if [ -f ./create_vault.sh ]; then
/bin/chmod a+x ./create_vault.sh || abort "Failed to set permission for create_vault.sh"
else
abort "Failed to find create_vault.sh!"
fi
fi

trap cleanup EXIT INT TERM

if [ ! -d "${KeyPath}" ]; then
/bin/mkdir -p "${KeyPath}" || abort "Failed to create path ${KeyPath}"
fi

./create_vault.sh "${OCPath}" || abort "create_vault.sh returns errors!"

if [ ! -f "${RootCA}" ]; then
/usr/bin/openssl genrsa -out "${RootCA}" 2048 || abort "Failed to generate CA"
if [ -f "${PrivKey}" ]; then
echo "WARNING: Private key exists without CA"
fi
fi

/bin/rm -fP "${PrivKey}" || abort "Failed to remove ${PrivKey}"
echo "Issuing a new private key..."
/usr/bin/openssl req -new -x509 -key "${RootCA}" -out "${PrivKey}" -days 1825 -subj "/C=WO/L=127.0.0.1/O=Acidanthera/OU=Acidanthera OpenCore/CN=Greetings from Acidanthera and WWHC" || abort "Failed to issue private key!"

/bin/rm -fP "${PubKey}" || abort "Failed to remove ${PubKey}"
echo "Getting public key based off private key..."
./RsaTool -cert "${PrivKey}" > "${PubKey}" || abort "Failed to get public key"

echo "Signing ${OCBin}..."
./RsaTool -sign "${OCPath}/vault.plist" "${OCPath}/vault.sig" "${PubKey}" || abort "Failed to patch ${PubKey}"

echo "Bin-patching ${OCBin}..."
off=$(($(/usr/bin/strings -a -t d "${OCBin}" | /usr/bin/grep "=BEGIN OC VAULT=" | /usr/bin/cut -f1 -d' ') + 16))
if [ "${off}" -le 16 ]; then
abort "${OCBin} is borked"
fi

/bin/dd of="${OCBin}" if="${PubKey}" bs=1 seek="${off}" count=528 conv=notrunc || abort "Failed to bin-patch ${OCBin}"

echo "All done!"
exit 0
54 changes: 54 additions & 0 deletions OpenCore/Utilities/LegacyBoot/BootInstallBase.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# Install booter on physical disk.

cd "$(dirname "$0")" || exit 1

if [ ! -f "boot${ARCHS}" ] || [ ! -f boot0 ] || [ ! -f boot1f32 ]; then
echo "Boot files are missing from this package!"
echo "You probably forgot to build DuetPkg first."
exit 1
fi

diskutil list
echo "Enter disk number to install to:"
read -r N

if [[ ! $(diskutil info disk"${N}" | sed -n 's/.*Device Node: *//p') ]]
then
echo Disk "$N" not found
exit 1
fi

FS=$(diskutil info disk"${N}"s1 | sed -n 's/.*File System Personality: *//p')
echo "$FS"

if [ "$FS" != "MS-DOS FAT32" ]
then
echo "No FAT32 partition to install"
exit 1
fi

# Write MBR
sudo fdisk -f boot0 -u /dev/rdisk"${N}"

diskutil umount disk"${N}"s1
sudo dd if=/dev/rdisk"${N}"s1 count=1 of=origbs
cp -v boot1f32 newbs
sudo dd if=origbs of=newbs skip=3 seek=3 bs=1 count=87 conv=notrunc
dd if=/dev/random of=newbs skip=496 seek=496 bs=1 count=14 conv=notrunc
sudo dd if=newbs of=/dev/rdisk"${N}"s1
diskutil mount disk"${N}"s1

cp -v "boot${ARCHS}" "$(diskutil info disk"${N}"s1 | sed -n 's/.*Mount Point: *//p')/boot"

if [ "$(diskutil info disk"${N}" | sed -n 's/.*Content (IOContent): *//p')" == "FDisk_partition_scheme" ]
then
sudo fdisk -e /dev/rdisk"$N" <<-MAKEACTIVE
p
f 1
w
y
q
MAKEACTIVE
fi
5 changes: 5 additions & 0 deletions OpenCore/Utilities/LegacyBoot/BootInstall_IA32.tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

cd "$(dirname "$0")" || exit 1
export ARCHS=IA32
source BootInstallBase.sh
5 changes: 5 additions & 0 deletions OpenCore/Utilities/LegacyBoot/BootInstall_X64.tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

cd "$(dirname "$0")" || exit 1
export ARCHS=X64
source BootInstallBase.sh
80 changes: 80 additions & 0 deletions OpenCore/Utilities/LegacyBoot/QemuBuild.command
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

# Build QEMU image, example:
# qemu-system-x86_64 -drive file=$QEMU_IMAGE/OpenCore.RO.raw -serial stdio \
# -usb -device usb-kbd -device usb-mouse -s -m 8192

cd "$(dirname "$0")" || exit 1

if [ ! -f boot ] || [ ! -f boot0 ] || [ ! -f boot1f32 ]; then
echo "Boot files are missing from this package!"
echo "You probably forgot to build DuetPkg first."
exit 1
fi

if [ "$(which qemu-img)" = "" ]; then
echo "QEMU installation missing"
exit 1
fi

if [ ! -d ROOT ]; then
echo "No ROOT directory with ESP partition contents"
exit 1
fi

rm -f OpenCore.dmg.sparseimage OpenCore.RO.raw OpenCore.RO.dmg
hdiutil create -size 200m -layout "UNIVERSAL HD" -type SPARSE -o OpenCore.dmg
newDevice=$(hdiutil attach -nomount OpenCore.dmg.sparseimage |head -n 1 | awk '{print $1}')
echo newdevice "$newDevice"

diskutil partitionDisk "${newDevice}" 1 MBR fat32 TEST R

# boot install script
diskutil list
N=$(echo "$newDevice" | tr -dc '0-9')
echo "Will be installed to Disk ${N}"


if [[ ! $(diskutil info disk"${N}" | sed -n 's/.*Device Node: *//p') ]]
then
echo Disk "$N" not found
exit 1
fi

FS=$(diskutil info disk"${N}"s1 | sed -n 's/.*File System Personality: *//p')
echo "$FS"

if [ "$FS" != "MS-DOS FAT32" ]
then
echo "No FAT32 partition to install"
exit 1
fi

# Write MBR
sudo fdisk -f boot0 -u /dev/rdisk"${N}"

diskutil umount disk"${N}"s1
sudo dd if=/dev/rdisk"${N}"s1 count=1 of=origbs
cp -v boot1f32 newbs
sudo dd if=origbs of=newbs skip=3 seek=3 bs=1 count=87 conv=notrunc
dd if=/dev/random of=newbs skip=496 seek=496 bs=1 count=14 conv=notrunc
sudo dd if=newbs of=/dev/rdisk"${N}"s1
diskutil mount disk"${N}"s1

cp -v boot "$(diskutil info disk"${N}"s1 | sed -n 's/.*Mount Point: *//p')"
cp -rv ROOT/* "$(diskutil info disk"${N}"s1 | sed -n 's/.*Mount Point: *//p')"

if [ "$(diskutil info disk"${N}" | sed -n 's/.*Content (IOContent): *//p')" == "FDisk_partition_scheme" ]
then
sudo fdisk -e /dev/rdisk"$N" <<-MAKEACTIVE
p
f 1
w
y
q
MAKEACTIVE
fi

hdiutil detach "$newDevice"
hdiutil convert -format UDRO OpenCore.dmg.sparseimage -o OpenCore.RO.dmg
qemu-img convert -f dmg -O raw OpenCore.RO.dmg OpenCore.RO.raw
7 changes: 7 additions & 0 deletions OpenCore/Utilities/LegacyBoot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BootInstall
===========

This tool installs legacy DuetPkg environment on GPT-formatted disk
to enable UEFI environment on BIOS-based systems.

Source code: https://github.com/acidanthera/DuetPkg
Binary file added OpenCore/Utilities/LegacyBoot/boot0
Binary file not shown.
Binary file added OpenCore/Utilities/LegacyBoot/boot1f32
Binary file not shown.
Binary file added OpenCore/Utilities/LegacyBoot/bootIA32
Binary file not shown.
Binary file added OpenCore/Utilities/LegacyBoot/bootX64
Binary file not shown.
Loading

0 comments on commit d79156f

Please sign in to comment.