2018-01-26 18:07:56 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Extract all of the files from a UEFI firmware image.
|
|
|
|
#
|
|
|
|
# This is a simple replacement for uefi-firmware-parser
|
|
|
|
# with fewer features and more suited for reconstituting the
|
|
|
|
# firmware later.
|
|
|
|
#
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use FindBin;
|
|
|
|
use lib "$FindBin::Bin/../lib";
|
|
|
|
use EFI;
|
|
|
|
use Getopt::Long;
|
|
|
|
use File::Basename;
|
2018-01-26 20:08:48 +00:00
|
|
|
use File::Temp 'tempfile';
|
2018-01-26 18:51:17 +00:00
|
|
|
use Data::Dumper;
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-02-02 13:40:42 +00:00
|
|
|
my $usage = <<"END";
|
2018-01-26 20:54:46 +00:00
|
|
|
Usage:
|
|
|
|
$0 [options] firmware.rom | tee firmware.txt
|
|
|
|
Options:
|
2018-02-02 13:40:42 +00:00
|
|
|
-h | -? | --help this usage
|
2018-01-26 20:54:46 +00:00
|
|
|
-v | --version Print per-section logs
|
|
|
|
-o | --output-dir DIR Write the output to a different base directory
|
|
|
|
-s | --start 0xX Starting offset for partial extraction
|
2018-02-02 13:40:42 +00:00
|
|
|
-n | --length 0xX Number of bytes to extract for partial extraction
|
2018-03-22 20:08:59 +00:00
|
|
|
-r | --repack Unpack LZMA compressed file sections
|
2018-02-02 13:40:42 +00:00
|
|
|
|
|
|
|
END
|
2018-01-26 18:07:56 +00:00
|
|
|
|
|
|
|
my $start_offset = 0; # 0x02c00000;
|
2018-02-02 13:40:42 +00:00
|
|
|
my $extract_length;
|
2018-01-26 18:51:17 +00:00
|
|
|
my $verbose = 0;
|
2018-01-26 20:47:53 +00:00
|
|
|
my $base_dir = '.';
|
2018-03-22 20:08:59 +00:00
|
|
|
my $repack;
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 20:54:46 +00:00
|
|
|
GetOptions(
|
2018-02-02 13:40:42 +00:00
|
|
|
"h|?|help" => sub { print $usage; exit 0 },
|
2018-01-26 20:54:46 +00:00
|
|
|
"v|verbose+" => \$verbose,
|
|
|
|
"o|output-dir=s" => \$base_dir,
|
|
|
|
"s|start=o" => \$start_offset,
|
2018-02-02 13:40:42 +00:00
|
|
|
"n|length=o" => \$extract_length,
|
2018-03-22 20:08:59 +00:00
|
|
|
"r|repack+" => \$repack,
|
2018-01-26 20:54:46 +00:00
|
|
|
) or die $usage;
|
|
|
|
|
2018-01-26 18:07:56 +00:00
|
|
|
local $/;
|
|
|
|
while(<>)
|
|
|
|
{
|
2018-01-26 20:54:46 +00:00
|
|
|
process_region($base_dir, $_);
|
2018-01-26 18:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process_region
|
|
|
|
{
|
|
|
|
my $base = shift;
|
|
|
|
my $data = shift;
|
|
|
|
my $length = length($data);
|
|
|
|
|
2018-01-26 20:47:53 +00:00
|
|
|
printf "%s: length 0x%x\n", $base, $length
|
2018-01-26 20:08:48 +00:00
|
|
|
if $verbose;
|
|
|
|
|
2018-01-26 18:07:56 +00:00
|
|
|
my $start_unknown;
|
|
|
|
|
|
|
|
# Search for the start of firmware volumes,
|
|
|
|
# identified by their '_FVH' in the structure
|
2018-01-26 20:08:48 +00:00
|
|
|
my $step = 256;
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 20:54:46 +00:00
|
|
|
# Adjust the end offset if they ask for too much
|
2018-02-02 13:40:42 +00:00
|
|
|
my $end_offset = $length - 0x30;
|
|
|
|
if (defined $extract_length)
|
|
|
|
{
|
|
|
|
my $new_end_offset = $start_offset + $extract_length;
|
|
|
|
$end_offset = $new_end_offset
|
|
|
|
if $new_end_offset < $end_offset;
|
|
|
|
}
|
2018-01-26 20:54:46 +00:00
|
|
|
|
|
|
|
for(my $offset = $start_offset ; $offset < $end_offset ; $offset += $step)
|
2018-01-26 18:07:56 +00:00
|
|
|
{
|
2018-01-27 15:33:42 +00:00
|
|
|
# Look for a flash region descriptor
|
|
|
|
my $ifd_sig = unpack("N", substr($_, $offset + 0x10, 4));
|
|
|
|
if ($ifd_sig == 0x5AA5F00F)
|
|
|
|
{
|
|
|
|
printf "%s/0x%08x.ifd: Flash Descriptor\n",
|
|
|
|
$base,
|
|
|
|
$offset,
|
|
|
|
;
|
|
|
|
|
2018-01-31 22:22:46 +00:00
|
|
|
my $ifd_len = 0x10000;
|
2018-01-27 15:33:42 +00:00
|
|
|
my $data = substr($_, $offset, $ifd_len);
|
|
|
|
|
|
|
|
output(sprintf("%s/0x%08x.ifd", $base, $offset), $data);
|
|
|
|
|
|
|
|
$offset += $ifd_len - $step;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $fv_sig = substr($_, $offset + 0x28, 4);
|
2018-01-26 18:07:56 +00:00
|
|
|
my $fv_length = EFI::read64($_, $offset + 0x20);
|
|
|
|
|
2018-01-27 15:33:42 +00:00
|
|
|
if ($fv_sig ne '_FVH' or $offset + $fv_length > $length)
|
2018-01-26 18:07:56 +00:00
|
|
|
{
|
|
|
|
# likely not a filesystem; report an unknown region
|
|
|
|
# if we have started processing filesystems
|
2018-01-26 20:08:48 +00:00
|
|
|
$start_unknown = $offset
|
|
|
|
unless defined $start_unknown;
|
2018-01-26 18:07:56 +00:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $start_unknown)
|
|
|
|
{
|
|
|
|
# We have a unknown region to write out
|
|
|
|
my $len = $offset - $start_unknown;
|
|
|
|
my $data = substr($_, $start_unknown, $len);
|
2018-01-26 20:47:53 +00:00
|
|
|
printf "%s/0x%08x.bin: UNKNOWN length 0x%x \n",
|
2018-01-26 20:08:48 +00:00
|
|
|
$base, $start_unknown, $len;
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-27 03:04:53 +00:00
|
|
|
output(sprintf("%s/0x%08x.bin", $base, $start_unknown), $data, 1);
|
2018-01-26 18:07:56 +00:00
|
|
|
undef $start_unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $fv = substr($data, $offset, $fv_length);
|
|
|
|
|
|
|
|
process_fv(sprintf("%s/0x%08x", $base, $offset), $fv);
|
|
|
|
|
|
|
|
# skip to the end of the filesystem
|
|
|
|
# should we care if this FV is not processed?
|
2018-01-26 20:08:48 +00:00
|
|
|
$offset += $fv_length - $step;
|
2018-01-26 18:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub process_fv
|
|
|
|
{
|
|
|
|
my $base = shift;
|
|
|
|
my $fv = shift;
|
|
|
|
my $fv_length = length $fv;
|
2018-01-26 20:08:48 +00:00
|
|
|
my $guid = EFI::read_guid($fv, 16);
|
|
|
|
|
2018-01-26 20:47:53 +00:00
|
|
|
output("$base.fv", $fv);
|
|
|
|
printf "%s.fv: FV %s length 0x%x\n",
|
|
|
|
$base,
|
|
|
|
$guid,
|
|
|
|
$fv_length,
|
|
|
|
;
|
|
|
|
|
2018-01-26 20:08:48 +00:00
|
|
|
if ($guid ne '8c8ce578-8a3d-4f1c-9935-896185c32dd3'
|
|
|
|
and $guid ne '5473c07a-3dcb-4dca-bd6f-1e9689e7349a'
|
|
|
|
) {
|
|
|
|
# we can only process normal firmware volumes
|
|
|
|
return;
|
|
|
|
}
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 20:08:48 +00:00
|
|
|
# read the start of data offset from the header
|
2018-01-26 18:07:56 +00:00
|
|
|
my $offset = EFI::read16($fv, 0x30);
|
|
|
|
if ($offset >= $fv_length)
|
|
|
|
{
|
2018-01-26 20:47:53 +00:00
|
|
|
die sprintf "%s: FV invalid data offset 0x%04x\n",
|
2018-01-26 18:07:56 +00:00
|
|
|
$base, $offset;
|
|
|
|
}
|
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
|
2018-01-26 18:07:56 +00:00
|
|
|
while($offset < $fv_length - 0x20)
|
|
|
|
{
|
|
|
|
my $len = EFI::read24($fv, $offset + 0x14);
|
|
|
|
my $data_offset = 0x18;
|
|
|
|
|
|
|
|
if ($len == 0xFFFFFF)
|
|
|
|
{
|
|
|
|
# Version 2 header with extended length
|
|
|
|
$len = EFI::read64($fv, $offset + 0x18);
|
|
|
|
|
|
|
|
# If we have an all-0xFF length, which indicates
|
|
|
|
# the start of free space
|
|
|
|
return 1 if ~$len == 0;
|
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
## Looks good, adjust the starting offset
|
2018-01-26 18:07:56 +00:00
|
|
|
$data_offset += 0x8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($len == 0x0)
|
|
|
|
{
|
2018-01-26 20:47:53 +00:00
|
|
|
die sprintf "%s: 0x%08x file has zero length?\n",
|
2018-01-26 18:07:56 +00:00
|
|
|
$base,
|
|
|
|
$offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($len + $offset > $fv_length)
|
|
|
|
{
|
2018-01-27 20:33:56 +00:00
|
|
|
warn sprintf "%s: 0x%08x file len 0x%x exceeds FV len\n",
|
2018-01-26 18:07:56 +00:00
|
|
|
$base,
|
|
|
|
$offset,
|
|
|
|
$len;
|
2018-01-27 20:33:56 +00:00
|
|
|
return;
|
2018-01-26 18:07:56 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 21:59:25 +00:00
|
|
|
|
2018-01-26 18:07:56 +00:00
|
|
|
my $data = substr($fv, $offset, $len);
|
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
process_ffs($base, $data, $data_offset);
|
2018-01-26 18:07:56 +00:00
|
|
|
|
|
|
|
$offset += $len;
|
|
|
|
|
|
|
|
# align it
|
|
|
|
$offset = ($offset + 7) & ~7;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub process_ffs
|
|
|
|
{
|
|
|
|
my $base = shift;
|
|
|
|
my $ffs = shift;
|
2018-01-26 18:51:17 +00:00
|
|
|
my $data_offset = shift; # might be 0x18 or 0x20
|
2018-01-26 18:07:56 +00:00
|
|
|
my $len = length($ffs);
|
|
|
|
|
|
|
|
my $guid = EFI::read_guid($ffs, 0x00);
|
2018-01-26 20:08:48 +00:00
|
|
|
my $type = ord(substr($ffs, 0x12, 1));
|
2018-01-26 18:07:56 +00:00
|
|
|
|
|
|
|
if ($guid eq 'ffffffff-ffff-ffff-ffff-ffffffffffff')
|
|
|
|
{
|
|
|
|
# padding: do not output it
|
|
|
|
# should check that everything is 0xFF first
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
output("$base/$guid.ffs", $ffs);
|
2018-01-26 20:08:48 +00:00
|
|
|
my $data = substr($ffs, $data_offset);
|
2018-03-22 20:08:59 +00:00
|
|
|
my $type_str = EFI::file_type_lookup($type);
|
|
|
|
my $name = $type_str;
|
2018-01-26 20:08:48 +00:00
|
|
|
|
2018-01-27 04:09:57 +00:00
|
|
|
if ($name eq 'FFS_PAD' or $name eq 'RAW')
|
2018-01-26 20:08:48 +00:00
|
|
|
{
|
|
|
|
# we are done here
|
2018-01-27 04:09:57 +00:00
|
|
|
printf "%s/%s.ffs: $name length 0x%x \n", $base, $guid, $len;
|
2018-01-26 20:08:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-03-22 20:08:59 +00:00
|
|
|
# if we are re-packing, recursively expand the sections
|
|
|
|
# and regenerate the section file
|
|
|
|
my @sections = process_sections("$base/$guid", $data);
|
|
|
|
my @new_sections = map { EFI::section($_->[0], $_->[1]) } @sections;
|
|
|
|
|
|
|
|
if ($repack)
|
|
|
|
{
|
|
|
|
output("$base/$guid.ffs", EFI::ffs(
|
|
|
|
$type_str,
|
|
|
|
EFI::guid($guid),
|
|
|
|
@new_sections
|
|
|
|
));
|
|
|
|
}
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
# if this file has a UI section, extract its name
|
2018-01-26 20:08:48 +00:00
|
|
|
$name = EFI::read_ucs16($_->[1], 0)
|
|
|
|
for grep { $_->[0] eq 'USER_INTERFACE'} @sections;
|
|
|
|
|
2018-01-26 20:47:53 +00:00
|
|
|
print "$base/$guid.ffs: $name\n";
|
2018-01-26 20:08:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub process_sections
|
|
|
|
{
|
|
|
|
my $base = shift;
|
|
|
|
my $data = shift;
|
2018-03-22 20:08:59 +00:00
|
|
|
my @rc;
|
2018-01-26 20:08:48 +00:00
|
|
|
|
|
|
|
for (ffs_sections($base, $data))
|
|
|
|
{
|
2018-03-22 20:08:59 +00:00
|
|
|
my @sec = process_section($base, @$_);
|
|
|
|
push @rc, @sec if @sec;
|
2018-01-26 20:08:48 +00:00
|
|
|
}
|
2018-03-22 20:08:59 +00:00
|
|
|
|
|
|
|
return @rc;
|
2018-01-26 20:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process_section
|
|
|
|
{
|
|
|
|
my $base = shift;
|
|
|
|
my $type = shift;
|
|
|
|
my $sec = shift;
|
|
|
|
my $number = shift;
|
|
|
|
my $offset = shift;
|
|
|
|
|
2018-03-22 20:08:59 +00:00
|
|
|
if ($type eq 'USER_INTERFACE')
|
|
|
|
{
|
|
|
|
# we should record that we've seen a UI section
|
|
|
|
#printf "Name: %s\n", EFI::read_ucs16($sec, 0);
|
|
|
|
}
|
|
|
|
|
2018-01-26 20:08:48 +00:00
|
|
|
# Look for GUID defined sections that encode LZMA compressed data
|
|
|
|
if ($type eq 'GUID_DEFINED')
|
2018-01-26 18:51:17 +00:00
|
|
|
{
|
2018-01-26 20:08:48 +00:00
|
|
|
my $guid = EFI::read_guid($sec, 0);
|
|
|
|
return unless $guid eq $EFI::lzma_guid;
|
|
|
|
|
|
|
|
my $lz_data = substr($sec, 0x14, length($sec) - 0x14);
|
|
|
|
|
|
|
|
my ($fh,$filename) = tempfile();
|
|
|
|
print $fh $lz_data;
|
|
|
|
close $fh;
|
|
|
|
|
2018-03-23 21:59:25 +00:00
|
|
|
my $data = `lzma --decompress --stdout - < $filename`;
|
2018-01-26 20:47:53 +00:00
|
|
|
printf "%s.ffs: length 0x%x (0x%x compressed)\n",
|
|
|
|
$base,
|
|
|
|
length($data),
|
|
|
|
length($lz_data),
|
|
|
|
;
|
2018-01-26 20:08:48 +00:00
|
|
|
|
|
|
|
# recursively process this data
|
2018-03-22 20:08:59 +00:00
|
|
|
return process_sections($base, $data);
|
2018-01-26 18:51:17 +00:00
|
|
|
}
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 20:08:48 +00:00
|
|
|
if ($type eq 'FIRMWARE_VOLUME_IMAGE')
|
|
|
|
{
|
|
|
|
process_fv(sprintf("%s/%d", $base, $number), $sec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-27 04:09:57 +00:00
|
|
|
if ($type eq 'RAW'
|
|
|
|
or $type eq 'TIANO_COMPRESSED'
|
|
|
|
) {
|
2018-03-22 20:08:59 +00:00
|
|
|
#return;
|
2018-01-26 20:08:48 +00:00
|
|
|
}
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 20:47:53 +00:00
|
|
|
warn "$base/$number: $type unknown\n"
|
|
|
|
if $type =~ /^0x/;
|
2018-03-22 20:08:59 +00:00
|
|
|
|
|
|
|
return ([ $type, $sec, $number, $offset ]);
|
2018-01-26 18:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
sub ffs_sections
|
2018-01-26 18:07:56 +00:00
|
|
|
{
|
2018-01-26 18:51:17 +00:00
|
|
|
my $base = shift;
|
|
|
|
my $ffs = shift;
|
|
|
|
my $ffs_len = length($ffs);
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
my @sections;
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-03-23 21:59:25 +00:00
|
|
|
# find each section inside
|
2018-01-26 20:08:48 +00:00
|
|
|
my $number = 0;
|
|
|
|
my $offset = 0;
|
2018-01-26 18:51:17 +00:00
|
|
|
while($offset < $ffs_len - 8)
|
|
|
|
{
|
|
|
|
my $len = EFI::read24($ffs, $offset);
|
|
|
|
my $data_offset = 0x4;
|
|
|
|
if ($len == 0xFFFFFF)
|
|
|
|
{
|
|
|
|
# FFSv3 section
|
|
|
|
$len = EFI::read32($ffs, $offset + $data_offset);
|
|
|
|
$data_offset += 4;
|
|
|
|
}
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
if ($len < $data_offset)
|
|
|
|
{
|
2018-01-30 15:52:02 +00:00
|
|
|
warn sprintf "%s: 0x%x Section length %x invalid\n", $base, $offset, $len;
|
|
|
|
return;
|
2018-01-26 18:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset + $len > $ffs_len)
|
|
|
|
{
|
|
|
|
die sprintf "%s: Section length %x exceeds FFS len %x\n",
|
|
|
|
$base,
|
|
|
|
$len,
|
|
|
|
$ffs_len,
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $sec = substr($ffs, $offset, $len);
|
|
|
|
|
|
|
|
# move to the next section, keeping a 4-byte alignment
|
|
|
|
$offset = ($offset + $len + 3) & ~3;
|
|
|
|
|
|
|
|
my $sec_type = ord(substr($sec, 3, 1));
|
|
|
|
my $sec_data = substr($sec, $data_offset, $len - $data_offset);
|
|
|
|
|
|
|
|
my $sec_type_name = $EFI::section_types_lookup{$sec_type};
|
|
|
|
$sec_type_name ||= sprintf "0x%02x", $sec_type;
|
|
|
|
|
2018-01-26 20:47:53 +00:00
|
|
|
printf "%s.ffs: %s len %x\n", $base, $sec_type_name, $len
|
2018-01-26 18:51:17 +00:00
|
|
|
if $verbose;
|
|
|
|
|
2018-01-26 20:08:48 +00:00
|
|
|
# ignore RAW sections that are all padding
|
|
|
|
next if $sec_type_name eq 'RAW' and empty_data($sec_data);
|
|
|
|
|
|
|
|
push @sections, [ $sec_type_name, $sec_data, $number, $offset ];
|
|
|
|
$number++;
|
2018-01-26 18:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return @sections;
|
2018-01-26 18:07:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
|
2018-01-26 20:08:48 +00:00
|
|
|
sub empty_data
|
|
|
|
{
|
|
|
|
my $data = shift;
|
|
|
|
my $byte = substr($data, 0, 1);
|
|
|
|
for(my $i = 0 ; $i < length $data ; $i++)
|
|
|
|
{
|
|
|
|
return 0 if substr($data, $i, 1) ne $byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
sub output
|
2018-01-26 18:07:56 +00:00
|
|
|
{
|
2018-01-26 18:51:17 +00:00
|
|
|
my $name = shift,
|
2018-01-26 18:07:56 +00:00
|
|
|
my $data = shift;
|
2018-01-27 03:04:53 +00:00
|
|
|
my $force = shift;
|
2018-01-26 18:07:56 +00:00
|
|
|
|
2018-01-26 20:08:48 +00:00
|
|
|
# check for an empty region (all the same value)
|
2018-01-27 03:04:53 +00:00
|
|
|
return if not $force and empty_data($data);
|
2018-01-26 20:08:48 +00:00
|
|
|
|
2018-01-26 18:51:17 +00:00
|
|
|
my $dir = dirname($name);
|
|
|
|
system(mkdir => -p => $dir)
|
|
|
|
and die "$name: Unable to create directory\n";
|
|
|
|
open FILE, '>', "$name"
|
|
|
|
or die "$name: Unable to create output file: $!\n";
|
|
|
|
print FILE $data;
|
|
|
|
close FILE;
|
2018-01-26 18:07:56 +00:00
|
|
|
}
|