Merge branch 'master' into sim-target-tree
[kugel-rb.git] / tools / ucl2src.pl
blob54cda5b3c0e8ba1d2fa2f3eb68ebde22e2014669
1 #!/usr/bin/env perl
2 ############################################################################
3 # __________ __ ___.
4 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 # \/ \/ \/ \/ \/
9 # $Id$
11 # Copyright (C) 2005 by Jens Arnold
13 # All files in this archive are subject to the GNU General Public License.
14 # See the file COPYING in the source tree root for full license agreement.
16 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 # KIND, either express or implied.
19 ############################################################################
21 if (!$ARGV[0])
23 print <<HERE
24 Usage: ucl2src [-p=<prefix>] <ucl file>
26 Check & strip header from an .ucl file and generate <prefix>.c and
27 <prefix>.h from it.
28 HERE
30 exit;
33 my $prefix = $p;
34 if(!$prefix) {
35 $prefix="uclimage";
38 my $input = $ARGV[0];
39 my $buffer;
40 my $insize;
41 my $readsize = 0;
43 open(INF, "<$input") or die "Can't open $input";
44 binmode INF;
46 # check UCL header
48 # magic header
49 read(INF, $buffer, 8);
50 if ($buffer ne pack("C8", 0x00, 0xe9, 0x55, 0x43, 0x4c, 0xff, 0x01, 0x1a))
52 die "Not an UCL file.";
54 read(INF, $buffer, 4);
56 # method
57 read(INF, $buffer, 1);
58 if (ord($buffer) != 0x2E)
60 die sprintf("Wrong compression method (expected 0x2E, found 0x%02X)",
61 ord($buffer));
64 read(INF, $buffer, 9);
66 # file size
67 read(INF, $buffer, 4);
68 $insize = unpack("N", $buffer) + 8;
70 open(OUTF, ">$prefix.c") or die "Can't open $prefix.c";
72 print OUTF <<HERE
73 /* This file was automatically generated using ucl2src.pl */
75 /* Data compressed with UCL method 0x2e follows */
76 const unsigned char image[] = {
77 HERE
80 while (read(INF, $buffer, 1))
82 $readsize++;
83 printf OUTF ("0x%02x,", ord($buffer));
84 if (!($readsize % 16))
86 print OUTF "\n";
90 close(INF);
92 if ($readsize != $insize)
94 die "Input file truncated, got $readsize of $insize bytes."
97 print OUTF <<HERE
99 /* end of compressed image */
100 HERE
102 close(OUTF);
104 open(OUTF, ">$prefix.h") or die "Can't open $prefix.h";
106 print OUTF "/* This file was automatically generated using ucl2src.pl */\n";
107 print OUTF "extern const unsigned char image[".$insize."];\n";
109 close(OUTF);