-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_pfh.pl
executable file
·194 lines (146 loc) · 4.22 KB
/
dump_pfh.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env perl
use warnings;
use strict;
#
# Load a phoenix bios image binary file, look for the $PFH and dump its contents
#
# Ref https://gist.github.com/skochinsky/181e6e338d90bb7f2693098dc43c6d54
#
use IO::File;
use UUID ':all';
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Quotekeys = 0;
sub usage() {
print("Dump the \$PFH information\n");
exit(1);
}
my $capsule_offset_hack = 0;
sub find_capsule {
my $fh = shift;
my $buf;
$fh->read($buf,16);
if ($buf eq "\xbd\x86\x66\x3b\x76\x0d\x30\x40\xb7\x0e\xb5\x51\x9e\x2f\xc5\xa0") {
# TODO
# - we should read the rest of the header
# - this header has some size details, but they do not match up with
# the magic offset we are using
# - Need to understand better why the resulting offset is 0x1d0
$capsule_offset_hack = 0x1d0;
return $capsule_offset_hack;
}
return undef;
}
sub find_pfh {
my $fh = shift;
my $offset = 0;
while(!$fh->eof()) {
$fh->seek($offset,SEEK_SET);
my $signature;
$fh->read($signature,4);
if ($signature eq '$PFH') {
return $offset;
}
$offset+=0x10;
}
return 0; # not found
}
sub hexify {
my $val = shift;
return sprintf("0x%02x",$val);
}
sub read_pfh_info {
my $db = shift;
my $fh = shift;
my $index = shift;
my $buf;
$fh->read($buf,4+4+8+4);
my @fields = qw(FileOffset Size FlashAddress NameOffset);
my @values = unpack("VVQV",$buf);
my %table = map { $fields[$_] => $values[$_] } (0..scalar(@fields)-1);
$table{FileOffset} = hexify($table{FileOffset});
$table{Size} = hexify($table{Size});
$table{FlashAddress} = hexify($table{FlashAddress});
$table{NameOffset} = hexify($table{NameOffset});
$table{_index} = $index;
push @{$db->{_PFH}{_part}},\%table;
return 1;
}
sub read_pfh_names {
my $db = shift;
my $fh = shift;
for my $entry (@{$db->{_PFH}{_part}}) {
$fh->seek(hex($entry->{NameOffset})+$capsule_offset_hack,SEEK_SET);
my $buf;
$fh->read($buf,32); # FIXME - just a guess
$entry->{_name} = unpack("Z*",$buf);
}
}
sub read_pfh {
my $db = shift;
my $fh = shift;
my $addr = shift;
$fh->seek($addr,SEEK_SET);
my $buf;
$fh->read($buf,4+4+4+2+4+2+4+4);
my @fields = qw(Signature Version HeaderSize HeaderChecksum TotalImageSize TotalImageChecksum NumberofImages ImageTableOffset);
my @values = unpack("a4VVvVvVV",$buf);
my %table = map { $fields[$_] => $values[$_] } (0..scalar(@fields)-1);
$table{Version} = hexify($table{Version});
$table{HeaderSize} = hexify($table{HeaderSize});
$table{HeaderChecksum} = hexify($table{HeaderChecksum});
$table{TotalImageSize} = hexify($table{TotalImageSize});
$table{TotalImageChecksum} = hexify($table{TotalImageChecksum});
$table{ImageTableOffset} = hexify($table{ImageTableOffset});
$table{_addr} = $addr;
$db->{_PFH} = \%table;
$fh->seek(hex($table{ImageTableOffset})+$capsule_offset_hack,SEEK_SET);
for my $i (0..$table{NumberofImages}-1) {
read_pfh_info($db,$fh,$i);
}
read_pfh_names($db,$fh);
return 1;
}
sub dump_partition_one {
my $part = shift;
printf("%10s %10s %10s %s\n",
$part->{FileOffset},
$part->{FlashAddress},
$part->{Size},
$part->{_name},
);
}
sub dump_partitions {
my $db = shift;
printf("%10s %10s %10s %s\n",
"fileOfs",
"flashAddr",
"Size",
"Name",
);
for my $part (@{$db->{_PFH}{_part}}) {
dump_partition_one($part);
}
}
sub main() {
my $binaryfile = shift @ARGV;
if (!defined($binaryfile)) {
usage();
}
my $fh = IO::File->new($binaryfile, O_RDONLY);
if (!defined($fh)) {
warn("Could not open $binaryfile\n");
exit(1);
}
my $db = {};
$db->{offset}{Capsule_Magic} = find_capsule($fh);
$db->{offset}{_PFH} = find_pfh($fh);
if (!$db->{offset}{_PFH}) {
die("Could not find FLASH MAP\n");
}
read_pfh($db,$fh,$db->{offset}{_PFH});
dump_partitions($db);
print Dumper($db);
}
main();