Versions of these two scripts that I've been using on my machines for many years.

They have never been consistently kept in any revision control history, so
the revision control stuff I have before today is probably not all that
helpful either since it's so sporadic.
This commit is contained in:
Bradley M. Kuhn 2011-04-21 12:46:36 -04:00
commit 5c750d7c58
3 changed files with 558 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*~

337
find-firmware.pl Executable file
View file

@ -0,0 +1,337 @@
#!/usr/bin/perl
#By Michael Holzt, DL3KJU, kju@fqdn.org, kju@debian.org, kju@IRCNet
#edited by novalis
# impoved by bkuhn over a period of 2005-2010.
use strict;
my ($fn, $onefile, $ofarg) = @ARGV;
if ($onefile ne '-o') {
$onefile = -1;
} elsif ($ofarg) {
$onefile = $ofarg;
}
die "Usage: splitgzip.pl <imagefile>\n" if ( $fn eq '' );
print "Reading $fn...\n";
open(IN,"<$fn") or die "Can't open: $fn\n";
my $image;
my $imglen = read(IN,$image, -s $fn);
die("did not get all the bytes, got $imglen expected " . -s $fn)
if $imglen != -s $fn;
close(IN);
print "Analysing $fn...\n";
my $offset = 0;
my $output = 0;
my $jffs = 0;
my $filenum = 0;
for ( $offset=0; $offset<$imglen; $offset++ )
{
if (ord(substr($image,$offset,1)) == 0x45 &&
ord(substr($image,$offset+1,1)) == 0x3d &&
ord(substr($image,$offset+2,1)) == 0xcd &&
ord(substr($image,$offset+3,1)) == 0x28 )
{
print "Found cramfs-Header at $offset... ";
close(OUT) if ( $output );
$fn = "morx$filenum"; #fixme, get name from file if possible
#hm, magic says this should work, but it didn't on the one i tried
#$fn = '';
#$i = 48;
#while (substr ($image, $offset+$i, 1) ne "\0") {
# $fn .= substr ($image, $offset+$i, 1);
# $i ++;
#}
$filenum++;
print "Filename $fn.cramfs... writing...\n";
$fn .= $filenum if -e "$fn.cramfs";
open(OUT,">$fn.cramfs") or die "Can't open: $fn\n";
$output = 1;
if ($onefile >= 0) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
}
if (substr($image,$offset,4) eq "ISc(") {
close(OUT) if ( $output );
$fn = "data$filenum";
$filenum++;
print "Filename $fn.cab... writing... (don't forget the data1.hdr thing)\n";
$fn .= $filenum if -e "$fn.cramfs";
open(OUT,">$fn.cab") or die "Can't open: $fn\n";
$output = 1;
if ($onefile >= 0) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
}
if (ord(substr($image,$offset,1)) == 0x5D &&
ord(substr($image,$offset+1,1)) == 0x00 &&
ord(substr($image,$offset+2,1)) == 0x00 &&
ord(substr($image,$offset+3,1)) == 0x80 &&
ord(substr($image,$offset+4,1)) == 0x00)
{
print "Found LZMA-Header at $offset\n";
close(OUT) if ( $output );
$fn = '';
my $x = 10;
while ( ord(substr($image,$offset+$x,1)) != 0 )
{
$fn .= substr($image,$offset+$x,1);
$x++;
}
$fn = substr ($fn, 0, 10);
if ($fn !~ /^[\w-.]+$/) {
$fn = "morx$filenum";
}
$filenum ++;
$fn .= $filenum if -e "$fn.lzma";
print "Filename $fn.lzma... writing...\n";
open(OUT,">$fn.lzma") or die "Can't open: $fn\n";
if ($onefile) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
$output = 1;
}
if (ord(substr($image,$offset,1)) == 0x1F &&
ord(substr($image,$offset+1,1)) == 0x8B &&
ord(substr($image,$offset+2,1)) == 0x08 )
{
#fixme
#if ($onefile && $output) {
# next;
#}
print "Found GZIP-Header at $offset\n";
close(OUT) if ( $output );
$fn = '';
my $x = 10;
while ( ord(substr($image,$offset+$x,1)) != 0 )
{
$fn .= substr($image,$offset+$x,1);
$x++;
}
$fn = substr ($fn, 0, 10);
if ($fn !~ /^[\w-.]+$/) {
$fn = "morx$filenum";
}
$filenum ++;
$fn .= $filenum if -e "$fn.gz";
print "Filename $fn.gz... writing...\n";
open(OUT,">$fn.gz") or die "Can't open: $fn\n";
if ($onefile) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
$output = 1;
}
if (substr($image,$offset,3) eq "BZh") {
#fixme
#if ($onefile && $output) {
# next;
#}
print "Found Bzip-Header at $offset\n";
close(OUT) if ( $output );
$fn = '';
my $x = 0;
while ( ord(substr($image,$offset+$x,1)) != 0 )
{
$fn .= substr($image,$offset+$x,1);
$x++;
}
$fn = substr ($fn, 0, 10);
if ($fn !~ /^[\w-.]+$/) {
$fn = "morx$filenum";
}
$filenum ++;
$fn .= $filenum if -e "$fn.bz2";
print "Filename $fn.bz2... writing...\n";
open(OUT,">$fn.bz2") or die "Can't open: $fn\n";
if ($onefile) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
$output = 1;
}
if (substr($image,$offset,4) eq "sqsh" or
substr($image,$offset,4) eq "hsqs") {
#fixme
#if ($onefile && $output) {
# next;
#}
my $endianIs = (substr($image,$offset,4) eq "sqsh") ? "big" : "little";
print "Found squashfs filesystem, $endianIs endian at $offset\n";
close(OUT) if ( $output );
$fn = "morx$filenum";
$filenum ++;
$fn .= $filenum if -e "$fn.squash";
print "Filename $fn.squash... writing...\n";
open(OUT,">$fn.squash") or die "Can't open: $fn\n";
if ($onefile) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
$output = 1;
}
if (substr($image,$offset,3) eq "Rar") {
#fixme
#if ($onefile && $output) {
# next;
#}
print "Found Rar at $offset (this hasn't been tested yet, might be saving wrong stuff)\n";
close(OUT) if ( $output );
$fn = '';
my $x = 0;
while ( ord(substr($image,$offset+$x,1)) != 0 )
{
$fn .= substr($image,$offset+$x,1);
$x++;
}
$fn = substr ($fn, 0, 10);
if ($fn !~ /^[\w-.]+$/) {
$fn = "morx$filenum";
}
$filenum ++;
$fn .= $filenum if -e "$fn.rar";
print "Filename $fn.rar... writing...\n";
open(OUT,">$fn.rar") or die "Can't open: $fn\n";
if ($onefile) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
$output = 1;
}
if (ord(substr($image,$offset+1,1)) == 0x39 &&
ord(substr($image,$offset,1)) == 0x31 &&
ord(substr($image,$offset+3,1)) == 0x34 &&
ord(substr($image,$offset+2,1)) == 0x38)
{
$jffs = 1;
print "Found JFFS Magic Bitmask at $offset\n";
close(OUT) if ( $output );
$fn = "morx$filenum";
$filenum ++;
$fn .= $filenum if -e "$fn.jffs";
print "Filename $fn.jffs... writing...\n";
open(OUT,">$fn.jffs") or die "Can't open: $fn\n";
if ($onefile) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
$output = 1;
}
# This Ext2 code doesn't work. I googled around looking or a way to read it
# but could not find one.
# if (ord(substr($image,$offset,1)) == 0x53 &&
# ord(substr($image,$offset+1,1)) == 0xef &&
# ord(substr($image,$offset+2,1)) == 0x01 &&
# ord(substr($image,$offset+3,1)) == 0x00)
# {
# print "Found EXT2/3 Magic Bitmask at $offset\n";
# close(OUT) if ( $output );
# $fn = "morx$filenum";
# $filenum ++;
# $fn .= $filenum if -e "$fn.ext";
# print "Filename $fn.ext... writing...\n";
# open(OUT,">$fn.ext") or die "Can't open: $fn\n";
# if ($onefile) {
# print OUT substr($image, $offset, 1+$imglen - $offset);
# close(OUT);
# $onefile --;
# }
# if (!$onefile) {
# exit (0);
# }
# $output = 1;
# }
if (ord(substr($image,$offset,1)) == 0x85 &&
ord(substr($image,$offset+1,1)) == 0x19 &&
ord(substr($image,$offset+2,1)) == 0x03 &&
ord(substr($image,$offset+3,1)) == 0x20)
{
$jffs = 1;
print "Found JFFS2 Magic Bitmask at $offset\n";
close(OUT) if ( $output );
$fn = "morx$filenum";
$filenum ++;
$fn .= $filenum if -e "$fn.jffs2";
print "Filename $fn.jffs2... writing...\n";
open(OUT,">$fn.jffs2") or die "Can't open: $fn\n";
if ($onefile) {
print OUT substr($image, $offset, 1+$imglen - $offset);
close(OUT);
$onefile --;
}
if (!$onefile) {
exit (0);
}
$output = 1;
}
print OUT substr($image,$offset,1) if ( $output );
}
close(OUT) if ( $output );

220
hierarchy-comparison Executable file
View file

@ -0,0 +1,220 @@
#!/usr/bin/perl -w
# hierarchy-comparison -*- Perl -*-
# Possible bug: only -type f and -type d are checked
# Copyright (C) 2001, 2002, 2003, 2004, 2008 Bradley M. Kuhn <bkuhn@ebb.org>
#
# This software's license gives you freedom; you can copy, convey,
# propogate, redistribute and/or modify this program under the terms of
# the GNU General Public License (GPL) as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version of the GPL published by the FSF.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file in the toplevel directory called
# "GPLv3". If not, see <http://www.gnu.org/licenses/>.
#
use strict;
use POSIX qw(tmpnam);
use Cwd;
my $VERSION = '1.1';
my $DO_DIFF = 1;
######################################################################
sub FindAndSortOutput {
use File::Find;
my($type, $dir, $output, $ignoreRegex, $filterRewrite) = @_;
my @files;
my $buildList = sub {
my $val = $_;
chomp $val;
$val =~ s/$filterRewrite// if defined $filterRewrite;
if ($type eq "NON-REGULAR") {
push(@files, $val) unless -f $_;
} elsif ($type eq "FILES") {
push(@files, $val) if -f $_;
} elsif ($type eq "DIRECTORY") {
push(@files, $val) if -d $_;
} else {
die "Unknown type requested: $type";
}
};
find({ wanted => $buildList, no_chdir => 1}, $dir);
open(FILE_OUTPUT, ">$output") or
die "$0: unablet open temporary output file, $output: $!";
my @sortedChompedFiles;
foreach my $file (sort {$a cmp $b } @files) {
chomp $file;
next if defined $ignoreRegex and $file =~ /$ignoreRegex/;
push(@sortedChompedFiles, $file);
print FILE_OUTPUT "$file\n";
}
close FILE_OUTPUT;
return @sortedChompedFiles;
}
######################################################################
sub MD5SumFiles {
my($dir, $fileListRef, $outputFile) = @_;
my $curdir = getcwd();
chdir $dir or die "unable to change to $dir: $0";
# open(FILE_OUTPUT, "|/usr/bin/xargs /usr/bin/md5sum >$outputFile 2>&1")
open(FILE_OUTPUT, "|/usr/bin/xargs -0 /usr/bin/sha1sum >$outputFile")
or die "unable to write to $outputFile: $!";
foreach my $file (@{$fileListRef}) {
print FILE_OUTPUT "$file\000";
}
close FILE_OUTPUT;
die "$0: error running xargs to md5sum command; $!" unless ($? == 0);
open(FILE_INPUT, "<", $outputFile) or die "unable to read back in $outputFile: $!";
my %files2sha1;
while (my $line = <FILE_INPUT>) {
chomp $line;
die "$outputFile has a strange line, \"$line\""
unless $line =~ /^(\S+)\s+(.+)$/;
$files2sha1{$2} = $1;
}
close FILE_INPUT;
chdir $curdir or die "$0: cannot go back into $curdir: $!";
return \%files2sha1;
}
######################################################################
if (@ARGV != 3 and @ARGV != 4) {
print "usage: $0 <ORIG_DIRECTORY> ",
"<COMPARED_DIRECTORY> <DIFF_OUTPUT_FILE> [<IGNORED_FILES_REGEX>]\n";
exit 2;
}
my($origDir, $comparedDir, $diffOutputFile, $ignoreRegex) = @ARGV;
$origDir =~ s%/\s*$%%;
$comparedDir =~ s%/\s*$%%;
my $origTempFile = POSIX::tmpnam();
my $comparedTempFile = POSIX::tmpnam();
# First, look for directory differences
print "Doing directory comparision: ";
my(@orgNonRegular) = FindAndSortOutput("NON-REGULAR", $origDir, $origTempFile,
$ignoreRegex, "^/?($origDir|$comparedDir)/?");
my(@comparedNonRegular) = FindAndSortOutput("NON-REGULAR", $comparedDir,
$comparedTempFile, $ignoreRegex,
"^/?($origDir|$comparedDir)/?");
system(
"/usr/bin/diff -u $origTempFile $comparedTempFile >> $diffOutputFile 2>&1");
my $diffExitCode = $?;
unlink($origTempFile, $comparedTempFile);
if ($diffExitCode == 2) {
print "\n";
die "$0: error trying to diff files: $!";
} elsif ($diffExitCode == 0) {
print "directory structures match.\n";
} else {
print "differences found in directory structures.\n",
"See $diffOutputFile for more information.\n";
}
# Now, see if the files are all the same
print "Doing file hierarchy comparision: ";
my(@orgFiles) = FindAndSortOutput("FILES", $origDir, $origTempFile,
$ignoreRegex, "^/?($origDir|$comparedDir)/?");
my(@comparedFiles) = FindAndSortOutput("FILES", $comparedDir,
$comparedTempFile, $ignoreRegex, "^/?($origDir|$comparedDir)/?");
system(
"/usr/bin/diff -u $origTempFile $comparedTempFile >> $diffOutputFile 2>&1");
$diffExitCode = $?;
if ($diffExitCode == 2) {
print "\n";
die "$0: error trying to diff files: $!";
} elsif ($diffExitCode == 0) {
print "both contain the same list of files.\n";
} else {
print "differences found in file hierarchies.\n",
"See $diffOutputFile for more information.\n";
}
# Now, diff the md5sums of the files.
print "Doing file contents comparisons: ";
# Assume that the two lists were the same, and come down to the list of
# shared files.
my(%origH, %comparedH);
@origH{@orgFiles} = @orgFiles;
@comparedH{@comparedFiles} = @comparedFiles;
my %final;
foreach my $ii (@orgFiles, @comparedFiles) {
$final{$ii} = $ii
if defined $origH{$ii} and defined $comparedH{$ii};
}
my(@o, @c);
@o = keys %final;
@c = keys %final;
my $origFiles2sha1 = MD5SumFiles($origDir, \@o, $origTempFile);
my $comparedFiles2sha1 = MD5SumFiles($comparedDir, \@c, $comparedTempFile);
system(
"/usr/bin/diff -u $origTempFile $comparedTempFile >> $diffOutputFile 2>&1");
$diffExitCode = $?;
unlink($origTempFile, $comparedTempFile);
if ($DO_DIFF) {
foreach my $file (keys %final) {
if ($origFiles2sha1->{$file} ne $comparedFiles2sha1->{$file}) {
system(
"/usr/bin/diff -u \"$origDir/$file\" \"$comparedDir/$file\" >> $diffOutputFile 2>&1");
}
}
}
if ($diffExitCode == 2) {
print "\n";
die "$0: error trying to diff files: $!";
} elsif ($diffExitCode == 0) {
print "all files match.\n";
} else {
print "differences found in some files.\n",
"See $diffOutputFile for more information.\n";
exit 1;
}
exit 0;
#
# Local variables:
# compile-command: "perl -c hierarchy-comparison"
# End: