Jens Arnold | 4c38514 | 2005-11-30 00:05:40 +0000 | [diff] [blame] | 1 | #!/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 | |
| 21 | if (!$ARGV[0]) |
| 22 | { |
| 23 | print <<HERE |
| 24 | Usage: ucl2src [-p=<prefix>] <ucl file> |
| 25 | |
| 26 | Check & strip header from an .ucl file and generate <prefix>.c and |
| 27 | <prefix>.h from it. |
| 28 | HERE |
| 29 | ; |
| 30 | exit; |
| 31 | } |
| 32 | |
| 33 | my $prefix = $p; |
| 34 | if(!$prefix) { |
| 35 | $prefix="uclimage"; |
| 36 | } |
| 37 | |
| 38 | my $input = $ARGV[0]; |
| 39 | my $buffer; |
| 40 | my $insize; |
| 41 | my $readsize = 0; |
| 42 | |
| 43 | open(INF, "<$input") or die "Can't open $input"; |
| 44 | binmode INF; |
| 45 | |
| 46 | # check UCL header |
| 47 | |
| 48 | # magic header |
| 49 | read(INF, $buffer, 8); |
| 50 | if ($buffer ne pack("C8", 0x00, 0xe9, 0x55, 0x43, 0x4c, 0xff, 0x01, 0x1a)) |
| 51 | { |
| 52 | die "Not an UCL file."; |
| 53 | } |
| 54 | read(INF, $buffer, 4); |
| 55 | |
| 56 | # method |
| 57 | read(INF, $buffer, 1); |
| 58 | if (ord($buffer) != 0x2E) |
| 59 | { |
| 60 | die sprintf("Wrong compression method (expected 0x2E, found 0x%02X)", |
| 61 | ord($buffer)); |
| 62 | } |
| 63 | |
| 64 | read(INF, $buffer, 9); |
| 65 | |
| 66 | # file size |
| 67 | read(INF, $buffer, 4); |
| 68 | $insize = unpack("N", $buffer) + 8; |
| 69 | |
| 70 | open(OUTF, ">$prefix.c") or die "Can't open $prefix.c"; |
| 71 | |
| 72 | print OUTF <<HERE |
| 73 | /* This file was automatically generated using ucl2src.pl */ |
| 74 | |
| 75 | /* Data compressed with UCL method 0x2e follows */ |
| 76 | const unsigned char image[] = { |
| 77 | HERE |
| 78 | ; |
| 79 | |
| 80 | while (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 | |
| 90 | close(INF); |
| 91 | |
| 92 | if ($readsize != $insize) |
| 93 | { |
| 94 | die "Input file truncated, got $readsize of $insize bytes." |
| 95 | } |
| 96 | |
| 97 | print OUTF <<HERE |
| 98 | }; |
| 99 | /* end of compressed image */ |
| 100 | HERE |
| 101 | ; |
| 102 | close(OUTF); |
| 103 | |
| 104 | open(OUTF, ">$prefix.h") or die "Can't open $prefix.h"; |
| 105 | |
| 106 | print OUTF "/* This file was automatically generated using ucl2src.pl */\n"; |
| 107 | print OUTF "extern const unsigned char image[".$insize."];\n"; |
| 108 | |
| 109 | close(OUTF); |
| 110 | |