Skip to content

Instantly share code, notes, and snippets.

@cecekpawon
Last active July 5, 2019 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecekpawon/e38bda384ff115f09e638482849deb2d to your computer and use it in GitHub Desktop.
Save cecekpawon/e38bda384ff115f09e638482849deb2d to your computer and use it in GitHub Desktop.
Apple EFI/FFS Extractor
#!/usr/bin/php
<?php
/*
---
Apple EFI/FFS Extractor
---
- chmod+x.script.
- Place FirmwareFile ($FF) & UEFIExtract into same directory as script.
- UEFIExtract: https://github.com/LongSoft/UEFITool/releases
---
@cecekpawon Tue Mar 12 08:53:12 2018
---
*/
$FF = "MBP143.fd";
$UEFIExtract = "UEFIExtract";
$aUEFIExtract = array (
array ("2ECED69B-2793-4388-BA3C-823040EBCCD2", "EfiOSInfo"),
array ("35628CFC-3CFF-444F-99C1-D5F06A069914", "EfiDevicePathPropertyDatabase"),
array ("43B93232-AFBE-11D4-BD0F-0080C73C8881", "EdkPartition"),
array ("44883EC1-C77C-1749-B73D-30C7B468B556", "ExFatDxe"),
array ("7064F130-846E-4CE2-83C1-4BBCBCBF1AE5", "AppleBootPolicy"),
array ("961578FE-B6B7-44C3-AF35-6BC705CD2B1F", "Fat"),
array ("AE4C11C8-1D6C-F24E-A183-E1CA36D1A8A9", "HfsPlus"),
array ("BC468182-0C0B-D645-A8AC-FB5D81076AE8", "UserInterfaceThemeDriver"),
array ("CD3BAFB6-50FB-4FE8-8E4E-AB74D2C1A600", "EnglishDxe"),
array ("CFFB32F4-C2A8-48BB-A0EB-6C3CCA3FE847", "ApfsJumpStart")
);
$cdir = __DIR__;
function rsearch ($folder, $pattern) {
$iti = new RecursiveDirectoryIterator ($folder);
foreach (new RecursiveIteratorIterator ($iti) as $file) {
if (strpos ($file, $pattern) !== false) {
return $file->__toString ();
}
}
return false;
}
chdir ("$cdir");
if (!file_exists ("$FF")) {
echo "! $FF not exists!\n";
exit;
}
if (file_exists ("$cdir/$UEFIExtract")) {
passthru ("cd \"$cdir\";");
$count = count ($aUEFIExtract);
for ($i=0; $i < $count; $i++) {
$guid = $aUEFIExtract[$i][0];
$name = $aUEFIExtract[$i][1];
passthru ("\"$cdir\"/$UEFIExtract $FF $guid > /dev/null;");
print ("Get $name ($guid)\n");
$dump = "$cdir/$FF.dump";
if (file_exists ("$dump")) {
$ffs = $efi = FALSE;
$filepath = rsearch ($dump, "body.bin");
if (file_exists ("$filepath")) {
$f = file_get_contents ("$filepath");
$a = unpack ("H*", $f)[1];
if (preg_match ("#^([a-f0-9]{8})4D5A#i", $a, $c)) {
print ("- Found $name.ffs\n");
$ddir = dirname ($filepath);
passthru ("cat \"$ddir/header.bin\" \"$filepath\" > \"$cdir/$name.ffs\";");
$files = glob ("$ddir/*", GLOB_BRACE);
foreach ($files as $key => $value) {
if (!is_dir ($value)) continue;
$filepath = rsearch ($value, "body.bin");
$f = file_get_contents ("$filepath");
$a = unpack ("H*", $f)[1];
if (preg_match ("#^4D5A#i", $a, $c)) {
print ("- Found $name.efi\n");
passthru ("cp \"$filepath\" $cdir/$name.efi;");
break;
}
}
}
passthru ("rm -rf \"$dump\";");
} else {
print ("! No Bin\n");
}
} else {
print ("! Extracting failed\n");
}
print ("\n");
}
} else {
print ("! Download UEFIExtract (https://github.com/LongSoft/UEFITool/releases)\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment