ezusb_convert.pl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #! /usr/bin/perl -w
  2. # convert an Intel HEX file into a set of C records usable by the firmware
  3. # loading code in usb-serial.c (or others)
  4. # accepts the .hex file(s) on stdin, a basename (to name the initialized
  5. # array) as an argument, and prints the .h file to stdout. Typical usage:
  6. # perl ezusb_convert.pl foo <foo.hex >fw_foo.h
  7. my $basename = $ARGV[0];
  8. die "no base name specified" unless $basename;
  9. while (<STDIN>) {
  10. # ':' <len> <addr> <type> <len-data> <crc> '\r'
  11. # len, type, crc are 2-char hex, addr is 4-char hex. type is 00 for
  12. # normal records, 01 for EOF
  13. my($lenstring, $addrstring, $typestring, $reststring, $doscrap) =
  14. /^:(\w\w)(\w\w\w\w)(\w\w)(\w+)(\r?)$/;
  15. die "malformed line: $_" unless $reststring;
  16. last if $typestring eq '01';
  17. my($len) = hex($lenstring);
  18. my($addr) = hex($addrstring);
  19. my(@bytes) = unpack("C*", pack("H".(2*$len), $reststring));
  20. #pop(@bytes); # last byte is a CRC
  21. push(@records, [$addr, \@bytes]);
  22. }
  23. @sorted_records = sort { $a->[0] <=> $b->[0] } @records;
  24. print <<"EOF";
  25. /*
  26. * ${basename}_fw.h
  27. *
  28. * Generated from ${basename}.s by ezusb_convert.pl
  29. * This file is presumed to be under the same copyright as the source file
  30. * from which it was derived.
  31. */
  32. EOF
  33. print "static const struct ezusb_hex_record ${basename}_firmware[] = {\n";
  34. foreach $r (@sorted_records) {
  35. printf("{ 0x%04x,\t%d,\t{", $r->[0], scalar(@{$r->[1]}));
  36. print join(", ", map {sprintf('0x%02x', $_);} @{$r->[1]});
  37. print "} },\n";
  38. }
  39. print "{ 0xffff,\t0,\t{0x00} }\n";
  40. print "};\n";