blob: 54cda5b3c0e8ba1d2fa2f3eb68ebde22e2014669 [file] [log] [blame]
Jens Arnold4c385142005-11-30 00:05:40 +00001#!/usr/bin/env perl
2############################################################################
3# __________ __ ___.
4# Open \______ \ ____ ____ | | _\_ |__ _______ ___
5# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8# \/ \/ \/ \/ \/
9# $Id$
10#
11# Copyright (C) 2005 by Jens Arnold
12#
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.
15#
16# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17# KIND, either express or implied.
18#
19############################################################################
20
21if (!$ARGV[0])
22{
23 print <<HERE
24Usage: ucl2src [-p=<prefix>] <ucl file>
25
26Check & strip header from an .ucl file and generate <prefix>.c and
27<prefix>.h from it.
28HERE
29;
30 exit;
31}
32
33my $prefix = $p;
34if(!$prefix) {
35 $prefix="uclimage";
36}
37
38my $input = $ARGV[0];
39my $buffer;
40my $insize;
41my $readsize = 0;
42
43open(INF, "<$input") or die "Can't open $input";
44binmode INF;
45
46# check UCL header
47
48# magic header
49read(INF, $buffer, 8);
50if ($buffer ne pack("C8", 0x00, 0xe9, 0x55, 0x43, 0x4c, 0xff, 0x01, 0x1a))
51{
52 die "Not an UCL file.";
53}
54read(INF, $buffer, 4);
55
56# method
57read(INF, $buffer, 1);
58if (ord($buffer) != 0x2E)
59{
60 die sprintf("Wrong compression method (expected 0x2E, found 0x%02X)",
61 ord($buffer));
62}
63
64read(INF, $buffer, 9);
65
66# file size
67read(INF, $buffer, 4);
68$insize = unpack("N", $buffer) + 8;
69
70open(OUTF, ">$prefix.c") or die "Can't open $prefix.c";
71
72print OUTF <<HERE
73/* This file was automatically generated using ucl2src.pl */
74
75/* Data compressed with UCL method 0x2e follows */
76const unsigned char image[] = {
77HERE
78 ;
79
80while (read(INF, $buffer, 1))
81{
82 $readsize++;
83 printf OUTF ("0x%02x,", ord($buffer));
84 if (!($readsize % 16))
85 {
86 print OUTF "\n";
87 }
88}
89
90close(INF);
91
92if ($readsize != $insize)
93{
94 die "Input file truncated, got $readsize of $insize bytes."
95}
96
97print OUTF <<HERE
98};
99/* end of compressed image */
100HERE
101 ;
102close(OUTF);
103
104open(OUTF, ">$prefix.h") or die "Can't open $prefix.h";
105
106print OUTF "/* This file was automatically generated using ucl2src.pl */\n";
107print OUTF "extern const unsigned char image[".$insize."];\n";
108
109close(OUTF);
110