ezusb_convert.pl 1.6 KB

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