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