Back to home page

OSCL-LXR

 
 

    


0001 #! /usr/bin/perl -w
0002 # SPDX-License-Identifier: GPL-2.0
0003 
0004 
0005 # convert an Intel HEX file into a set of C records usable by the firmware
0006 # loading code in usb-serial.c (or others)
0007 
0008 # accepts the .hex file(s) on stdin, a basename (to name the initialized
0009 # array) as an argument, and prints the .h file to stdout. Typical usage:
0010 #  perl ezusb_convert.pl foo <foo.hex >fw_foo.h
0011 
0012 
0013 my $basename = $ARGV[0];
0014 die "no base name specified" unless $basename;
0015 
0016 while (<STDIN>) {
0017     # ':' <len> <addr> <type> <len-data> <crc> '\r'
0018     #  len, type, crc are 2-char hex, addr is 4-char hex. type is 00 for
0019     # normal records, 01 for EOF
0020     my($lenstring, $addrstring, $typestring, $reststring, $doscrap) =
0021       /^:(\w\w)(\w\w\w\w)(\w\w)(\w+)(\r?)$/;
0022     die "malformed line: $_" unless $reststring;
0023     last if $typestring eq '01';
0024     my($len) = hex($lenstring);
0025     my($addr) = hex($addrstring);
0026     my(@bytes) = unpack("C*", pack("H".(2*$len), $reststring));
0027     #pop(@bytes); # last byte is a CRC
0028     push(@records, [$addr, \@bytes]);
0029 }
0030 
0031 @sorted_records = sort { $a->[0] <=> $b->[0] } @records;
0032 
0033 print <<"EOF";
0034 /*
0035  * ${basename}_fw.h
0036  *
0037  * Generated from ${basename}.s by ezusb_convert.pl
0038  * This file is presumed to be under the same copyright as the source file
0039  * from which it was derived.
0040  */
0041 
0042 EOF
0043 
0044 print "static const struct ezusb_hex_record ${basename}_firmware[] = {\n";
0045 foreach $r (@sorted_records) {
0046     printf("{ 0x%04x,\t%d,\t{", $r->[0], scalar(@{$r->[1]}));
0047     print join(", ", map {sprintf('0x%02x', $_);} @{$r->[1]});
0048     print "} },\n";
0049 }
0050 print "{ 0xffff,\t0,\t{0x00} }\n";
0051 print "};\n";