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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#!/usr/bin/perl -w
#
# Quick Perl program to decode and display details about
# tagged images created by mknbi
# -e extracts directory to `nbidir' and segments to `segmentN'
# N = 0, 1, 2, ...
#
# Added code to dump vendor tag in hex (for DOS disk parameters)
#
# Ken Yap, August 1999
#
use strict;
use vars qw($imagefile $data $curoffset $dirfile @seglengths $vendordata
$extract $status $i);
sub getvendordata ($)
{
my ($flags) = @_;
my $vendordata = '';
my $vendorlen = ($flags & 0xff) >> 4;
if ($vendorlen > 0) {
$vendorlen *= 4;
$vendordata = unpack("a$vendorlen", substr($data, $curoffset));
$curoffset += $vendorlen;
}
return ($vendordata);
}
sub decodesegmentflags ($)
{
my ($flags) = @_;
my ($type);
$flags >>= 24;
$flags &= 0x3;
($flags == 0) and $type = "Absolute";
($flags == 1) and $type = "Follows last segment";
($flags == 2) and $type = "Below end of memory";
($flags == 3) and $type = "Below last segment loaded";
return ($type);
}
sub one_nbi_segment ($)
{
my ($segnum) = @_;
my ($type, $vendordata, @vdata, $i);
my ($flags, $loadaddr, $imagelen, $memlength) = unpack('V4', substr($data, $curoffset));
$curoffset += 16;
printf "Segment number %d\n", $segnum;
printf "Load address:\t\t%08x\n", $loadaddr;
printf "Image length:\t\t%d\n", $imagelen;
printf "Memory length:\t\t%d\n", $memlength;
$type = &decodesegmentflags($flags);
print "Position:\t\t$type\n";
printf "Vendor tag:\t\t%d\n", ($flags >> 8) & 0xff;
if (($vendordata = &getvendordata($flags)) ne '') {
print "Vendor data:\t\t\"", $vendordata, "\"\n";
@vdata = unpack('C*', $vendordata);
print "Vendor data in hex:\t";
foreach $i (0..$#vdata) {
printf "%02x ", $vdata[$i];
}
print "\n";
}
print "\n";
push (@seglengths, $imagelen);
return (($flags >> 26) & 1);
}
sub decode_nbi
{
my ($magic, $flags, $bx, $ds, $ip, $cs) = unpack('a4Vv4', substr($data, 0, 16));
$curoffset = 16;
# Decode the header
printf "Type: NBI\nHeader location:\t%04x:%04x\n", $ds, $bx;
if (($flags >> 31) & 1) {
printf "Start address:\t\t%04x%04x (flat)\n", $cs, $ip;
} else {
printf "Start address:\t\t%04x:%04x\n", $cs, $ip;
}
print "Flags:\n";
print "\tReturn to loader after execution (extension)\n" if (($flags >> 8) & 1);
if (($vendordata = &getvendordata($flags)) ne '') {
print "Vendor data:\t\t", $vendordata, "\n";
}
print "\n";
# Now decode each segment record
my $segnum = 0;
do {
$i = &one_nbi_segment($segnum);
++$segnum;
} while (!$i);
}
sub one_elf_segment ($$)
{
my ($segnum, $curoffset) = @_;
my ($offset, $vaddr, $paddr, $filesz, $memsz, $flags,
$align) = unpack('@4V6', substr($data, $curoffset));
printf "Segment number %d\n", $segnum;
printf "Load address:\t\t%08x\n", $vaddr;
printf "Image length:\t\t%d\n", $filesz;
printf "Memory length:\t\t%d\n", $memsz;
print "\n";
push (@seglengths, $filesz);
}
sub decode_elf
{
my ($entry, $phoff, $shoff, $flags, $ehsize, $phentsize,
$phnum) = unpack('@24V4v3', $data);
printf "Type: ELF\nStart address:\t\t%08x\n", $entry;
print "Flags:\n";
print "\tReturn to loader after execution (extension)\n" if ($flags & 0x8000000);
print "\n";
$curoffset = $phoff;
foreach $i (0..$phnum-1) {
&one_elf_segment($i, $curoffset);
$curoffset += $phentsize;
}
}
$extract = 0;
@seglengths = ();
$#ARGV >= 0 or die "Usage: disnbi [-e] Etherboot-image-file\n";
if ($ARGV[0] eq '-e') {
$extract = 1; shift
}
$#ARGV >= 0 or die "Usage: disnbi [-e] Etherboot-image-file\n";
$imagefile= $ARGV[0];
open(I, $ARGV[0]) or die "$imagefile: $!\n";
binmode(I);
(defined($status = sysread(I, $data, 512)) and $status == 512)
or die "$imagefile: Cannot read header\n";
my ($magic) = unpack('a4', substr($data, 0, 4));
if ($magic eq "\x36\x13\x03\x1B") {
&decode_nbi();
$dirfile = 'nbidir';
} elsif ($magic eq "\x7FELF") {
&decode_elf();
$dirfile = 'elfdir';
} else {
die "$imagefile: Not a tagged or ELF image file\n";
}
exit(0) if ($extract == 0);
print "Dumping directory to `$dirfile'...\n";
open(O, ">$dirfile") or die "$dirfile: $!\n";
binmode(O);
print O $data;
close(O);
$data = '';
foreach $i (0..$#seglengths) {
print "Extracting segment $i to `segment$i'...\n";
open(O, ">segment$i") or die "segment$i: $!\n";
binmode(O);
(defined($status = sysread(I, $data, $seglengths[$i]))
and $status = $seglengths[$i])
or die "$imagefile: Cannot read data\n";
print O $data;
close(O);
}
print "Done\n";
exit(0);
__END__
=head1 NAME
disnbi - display Etherboot image
=head1 SYNOPSIS
B<disnbi> [C<-e>] I<Etherboot-file>
=head1 DESCRIPTION
B<disnbl> is a program that to display in symbolic form the contents
of a Etherboot image created by mknbi or mkelf. Detection of image type
is automatic.
B<-e> Extract contents of image as well. The directory will be written
to C<nbidir> or C<elfdir> and the segments to C<segment>I<n> where I<n>
is 0, 1, 2, etc.
=head1 BUGS
Please report all bugs to the author.
=head1 SEE ALSO
Etherboot tutorial at C<http://etherboot.sourceforge.net/>
=head1 COPYRIGHT
B<disnbl> is under the GNU Public License
=head1 AUTHOR
Ken Yap (C<ken_yap@users.sourceforge.net>)
=head1 DATE
Version 1.4 December 2002
|