blob: 42d400864c5364cd79f357121833efd102683703 [file] [log] [blame]
Dave Chapman530f31d2006-08-30 23:17:04 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 Dave Chapman
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24/*
25 * CRC32 implementation taken from:
26 *
27 * efone - Distributed internet phone system.
28 *
29 * (c) 1999,2000 Krzysztof Dabrowski
30 * (c) 1999,2000 ElysiuM deeZine
31 *
32 * This program is free software; you can redistribute it and/or
33 * modify it under the terms of the GNU General Public License
34 * as published by the Free Software Foundation; either version
35 * 2 of the License, or (at your option) any later version.
36 *
37 */
38
39/* based on implementation by Finn Yannick Jacobs */
40
41#include <stdio.h>
42#include <stdlib.h>
43
44/* crc_tab[] -- this crcTable is being build by chksum_crc32GenTab().
45 * so make sure, you call it before using the other
46 * functions!
47 */
48static unsigned int crc_tab[256];
49
50/* chksum_crc() -- to a given block, this one calculates the
51 * crc32-checksum until the length is
52 * reached. the crc32-checksum will be
53 * the result.
54 */
55static unsigned int chksum_crc32 (unsigned char *block, unsigned int length)
56{
57 register unsigned long crc;
58 unsigned long i;
59
60 crc = 0;
61 for (i = 0; i < length; i++)
62 {
63 crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
64 }
65 return (crc);
66}
67
68/* chksum_crc32gentab() -- to a global crc_tab[256], this one will
69 * calculate the crcTable for crc32-checksums.
70 * it is generated to the polynom [..]
71 */
72
73static void chksum_crc32gentab (void)
74{
75 unsigned long crc, poly;
76 int i, j;
77
78 poly = 0xEDB88320L;
79 for (i = 0; i < 256; i++)
80 {
81 crc = i;
82 for (j = 8; j > 0; j--)
83 {
84 if (crc & 1)
85 {
86 crc = (crc >> 1) ^ poly;
87 }
88 else
89 {
90 crc >>= 1;
91 }
92 }
93 crc_tab[i] = crc;
94 }
95}
96
97static void int2le(unsigned int val, unsigned char* addr)
98{
99 addr[0] = val & 0xFF;
100 addr[1] = (val >> 8) & 0xff;
101 addr[2] = (val >> 16) & 0xff;
102 addr[3] = (val >> 24) & 0xff;
103}
104
Barry Wardell12df7742007-03-14 13:51:05 +0000105int mi4_encode(char *iname, char *oname, int version, int magic,
106 char *model, char *type)
Dave Chapman530f31d2006-08-30 23:17:04 +0000107{
108 size_t len;
109 int length;
110 int mi4length;
111 FILE *file;
112 unsigned int crc = 0;
113 unsigned char *outbuf;
114
115 file = fopen(iname, "rb");
116 if (!file) {
117 perror(iname);
118 return -1;
119 }
120 fseek(file,0,SEEK_END);
121 length = ftell(file);
122
123 fseek(file,0,SEEK_SET);
124
125 /* Add 4 bytes to length (for magic), the 0x200 byte header and
126 then round to an even 0x400 bytes
127 */
128 mi4length = (length+4+0x200+0x3ff)&~0x3ff;
129
130 outbuf = malloc(mi4length);
131
132 if ( !outbuf ) {
133 printf("out of memory!\n");
134 return -1;
135 }
136
137 /* Clear the buffer to zero */
138 memset(outbuf, 0, mi4length);
139
140 len = fread(outbuf+0x200, 1, length, file);
Daniel Stenbergafc39fb2008-04-09 13:18:59 +0000141 if(len < (size_t)length) {
Dave Chapman530f31d2006-08-30 23:17:04 +0000142 perror(iname);
143 return -2;
144 }
145 fclose(file);
146
147 /* We need to write some data into the actual image - before calculating
148 the CRC. */
149 int2le(0x00000100, &outbuf[0x2e0]); /* magic */
Daniel Stenberg34080492007-02-23 22:45:00 +0000150 int2le(magic, &outbuf[0x2e4]); /* magic */
Dave Chapman530f31d2006-08-30 23:17:04 +0000151 int2le(length+4, &outbuf[0x2e8]); /* length plus 0xaa55aa55 */
152
153 int2le(0xaa55aa55, &outbuf[0x200+length]); /* More Magic */
Barry Wardell12df7742007-03-14 13:51:05 +0000154
Barry Wardell84ff46a2007-03-15 13:04:38 +0000155 strncpy((char *)outbuf+0x1f8, type, 4); /* type of binary (RBBL, RBOS) */
156 strncpy((char *)outbuf+0x1fc, model, 4); /* 4 character model id */
Barry Wardell12df7742007-03-14 13:51:05 +0000157
Dave Chapman530f31d2006-08-30 23:17:04 +0000158 /* Calculate CRC32 checksum */
159 chksum_crc32gentab ();
Barry Wardell84ff46a2007-03-15 13:04:38 +0000160 crc = chksum_crc32 (outbuf+0x200,mi4length-0x200);
Dave Chapman530f31d2006-08-30 23:17:04 +0000161
162 strncpy((char *)outbuf, "PPOS", 4); /* Magic */
163 int2le(version, &outbuf[0x04]); /* .mi4 version */
164 int2le(length+4, &outbuf[0x08]); /* Length of firmware plus magic */
165 int2le(crc, &outbuf[0x0c]); /* CRC32 of mi4 file */
166 int2le(0x00000002, &outbuf[0x10]); /* Encryption type: 2 = TEA */
167 int2le(mi4length, &outbuf[0x14]); /* Total .mi4 length */
168 int2le(mi4length-0x200, &outbuf[0x18]); /* Length of plaintext part */
169
170 /* v3 files require a dummy DSA signature */
171 if (version == 0x00010301) {
172 outbuf[0x2f]=0x01;
173 }
174
175 file = fopen(oname, "wb");
176 if (!file) {
177 perror(oname);
178 return -3;
179 }
180
181 len = fwrite(outbuf, 1, mi4length, file);
Daniel Stenbergafc39fb2008-04-09 13:18:59 +0000182 if(len < (size_t)length) {
Dave Chapman530f31d2006-08-30 23:17:04 +0000183 perror(oname);
184 return -4;
185 }
186
187 fclose(file);
188
189 fprintf(stderr, "File encoded successfully\n" );
190
191 return 0;
192}