Skip to content

Instantly share code, notes, and snippets.

@onitake
Created March 6, 2016 12:22
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 onitake/5b5e4f1d46c28931b92c to your computer and use it in GitHub Desktop.
Save onitake/5b5e4f1d46c28931b92c to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use IO::File;
sub usage() {
print STDERR "Usage: scantscf GSL_TS_CFG.h\n";
print STDERR "Dumps the configuration block of Silead firmware in header format.\n";
-1;
}
my $tscffile = $ARGV[0] or exit usage;
my $in = IO::File->new($tscffile, 'r') or die "Can't open $tscffile: $!";
my $cfg = 0;
while (my $line = <$in>) {
if ($cfg and $line =~ /};/) {
$cfg = 0;
} elsif ($line =~ /gsl_config_data_id/) {
$cfg = 1;
} elsif ($cfg) {
$line =~ s/\s//g;
for my $code (split(/,/, $line)) {
my $value;
if ($code =~ /0[xX]([0-9a-f]+)/) {
$value = hex($1) & 0xffffffff;
} elsif ($code =~ /([0-9]+)/) {
$value = int($1) & 0xffffffff;
} else {
$value = 0;
}
my @bytes;
my $ascii = '';
for my $i (0, 8, 16, 24) {
my $byte = ($value >> $i) & 0xff;
if ($byte >= 0x20 && $byte <= 0x7e) {
$ascii .= chr($byte);
} else {
$ascii .= '.';
}
push(@bytes, $byte);
}
my @shorts = ($value & 0xffff, ($value >> 16) & 0xffff);
printf("%10d %5d %5d %3d %3d %3d %3d 0x%08x 0x%04x 0x%04x 0x%02x 0x%02x 0x%02x 0x%02x %s\n", $value, @shorts, @bytes, $value, @shorts, @bytes, $ascii);
}
}
}
$in->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment