diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2015-05-28 15:53:55 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2015-05-28 15:53:55 +0000 |
commit | 22d36e63991bddd8da64eb6b01af03ed9a468b48 (patch) | |
tree | a134261c5dacae64104d318e6bf521e1e91c6807 /scripts | |
parent | 607e0e6f17deba566df58eba20d96bf986cac085 (diff) | |
download | master-187ad058-22d36e63991bddd8da64eb6b01af03ed9a468b48.tar.gz master-187ad058-22d36e63991bddd8da64eb6b01af03ed9a468b48.tar.bz2 master-187ad058-22d36e63991bddd8da64eb6b01af03ed9a468b48.zip |
scripts: add srecimage.pl, a script to convert binaries to SREC format
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@45808 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/srecimage.pl | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/scripts/srecimage.pl b/scripts/srecimage.pl new file mode 100755 index 0000000000..a68aa61f5b --- /dev/null +++ b/scripts/srecimage.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +# +# srecimage.pl - script to convert a binary image into srec +# Copyright (c) 2015 - Jo-Philipp Wich <jow@openwrt.org> +# +# This script is in the public domain. + +use strict; + +my ($input, $output, $offset) = @ARGV; + +if (!defined($input) || !-f $input || !defined($output) || + !defined($offset) || $offset !~ /^(0x)?[a-fA-F0-9]+$/) { + die "Usage: $0 <input file> <output file> <load address>\n"; +} + +sub srec +{ + my ($type, $addr, $data, $len) = @_; + my @addrtypes = qw(%04X %04X %06X %08X %08X %04X %06X %08X %06X %04X); + my $addrstr = sprintf $addrtypes[$type], $addr; + + $len = length($data) if ($len <= 0); + $len += 1 + (length($addrstr) / 2); + + my $sum = $len; + + foreach my $byte (unpack('C*', pack('H*', $addrstr)), unpack('C*', $data)) + { + $sum += $byte; + } + + return sprintf "S%d%02X%s%s%02X\r\n", + $type, $len, $addrstr, uc(unpack('H*', $data)), ~($sum & 0xFF) & 0xFF; +} + + +open(IN, '<:raw', $input) || die "Unable to open $input: $!\n"; +open(OUT, '>:raw', $output) || die "Unable to open $output: $!\n"; + +my ($basename) = $output =~ m!([^/]+)$!; + +print OUT srec(0, 0, $basename, 0); + +my $off = hex($offset); +my $len; + +while (defined($len = read(IN, my $buf, 16)) && $len > 0) +{ + print OUT srec(3, $off, $buf, $len); + $off += $len; +} + +print OUT srec(7, hex($offset), "", 0); + +close OUT; +close IN; |