c0865a183badcd8b3b0483f04ee2b6a79fee706f
[openwrt/staging/dedeckeh.git] / tools / firmware-utils / src / bcmalgo.c
1 #include <stdlib.h>
2 #include <sys/types.h>
3 #include <stdio.h>
4 #include <inttypes.h>
5 #include <string.h>
6 #include <getopt.h>
7 #include <unistd.h>
8 #include <sys/time.h>
9 #include <sys/stat.h>
10 #include "bcmalgo.h"
11
12
13 #define UTIL_VERSION "0.1"
14 #define ENDIAN_REVERSE_NEEDED
15
16 uint32_t reverse_endian32 ( uint32_t data )
17 {
18 #ifdef ENDIAN_REVERSE_NEEDED
19 return 0 | ( data & 0x000000ff ) << 24
20 | ( data & 0x0000ff00 ) << 8
21 | ( data & 0x00ff0000 ) >> 8
22 | ( data & 0xff000000 ) >> 24;
23 #else
24 return data;
25 #endif
26 }
27
28 uint16_t reverse_endian16 ( uint16_t data )
29 {
30 #ifdef ENDIAN_REVERSE_NEEDED
31 return 0 | ( data & 0x00ff ) << 8
32 | ( data & 0xff00 ) >> 8;
33 #else
34 return data;
35 #endif
36 }
37
38
39
40 uint32_t get_buffer_crc ( char* filebuffer,size_t size )
41 {
42
43 long crc=0xffffffffL;
44 long crcxor = 0xffffffffL;
45 long num4 = 0xffffffffL;
46 long num5 = size;
47 long num6 = 0x4c11db7L;
48 long num7 = 0x80000000L;
49 int i;
50 long j;
51 for ( i = 0; i < ( num5 ); i++ )
52 {
53 long num2 = filebuffer[i];
54 for ( j = 0x80L; j != 0L; j = j >> 1 )
55 {
56 long num3 = crc & num7;
57 crc = crc << 1;
58 if ( ( num2 & j ) != 0L )
59 {
60 num3 ^= num7;
61 }
62 if ( num3 != 0L )
63 {
64 crc ^= num6;
65 }
66 }
67 }
68 crc ^= crcxor;
69 crc &= num4;
70
71 uint8_t b1 = ( uint8_t ) ( ( crc & -16777216L ) >> 0x18 );
72 uint8_t b2 = ( uint8_t ) ( ( crc & 0xff0000L ) >> 0x10 );
73 uint8_t b3 = ( uint8_t ) ( ( crc & 0xff00L ) >> 8 );
74 uint8_t b4 = ( uint8_t ) ( crc & 0xffL );
75 int32_t crc_result = ( b1 | b2 << 8| b3 << 16| b4 <<24 );
76 return reverse_endian32 ( crc_result );
77 }
78
79 //Thnx to Vector for the algo.
80 uint32_t get_file_crc ( char* filename )
81 {
82 struct stat buf;
83 stat ( filename,&buf );
84 char* filebuffer = malloc ( buf.st_size+10 );
85 FILE* fd = fopen ( filename,"r" );
86 fread ( filebuffer, 1, buf.st_size,fd );
87 fclose ( fd );
88 uint32_t crc = get_buffer_crc ( filebuffer,buf.st_size );
89 free ( filebuffer );
90 return crc;
91 }
92
93
94
95 uint16_t get_hcs ( ldr_header_t* hd )
96 {
97 uint8_t* head = ( uint8_t* ) hd;
98 uint8_t hcs_minor;
99 uint8_t hcs_major;
100 uint16_t n = 0xffff;
101 int state = 0;
102 int i,j;
103 for ( i = 0; i < 0x54; i++ )
104 {
105 uint16_t m = head[i];
106 m = m << 8;
107 for ( j = 0; j < 8; j++ )
108 {
109 if ( ( ( n ^ m ) & 0x8000 ) == 0 )
110 {
111 state = 0;
112 }
113 else
114 {
115 state = 1;
116 }
117 n = n << 1;
118 if ( state )
119 {
120 n ^= 0x1021;
121 }
122 m = m << 1;
123 }
124 n &= 0xffff;
125 }
126 n ^= 0xffff;
127 hcs_major = ( uint8_t ) ( ( n & 0xff00 ) >> 8 );
128 hcs_minor = ( uint8_t ) ( n & 0xff );
129 uint16_t hcs = hcs_major <<8 | hcs_minor;
130 return hcs;
131 }
132
133 ldr_header_t* construct_header ( uint32_t magic, uint16_t rev_maj,uint16_t rev_min, uint32_t build_date, uint32_t filelen, uint32_t ldaddress, const char* filename, uint32_t crc_data )
134 {
135 ldr_header_t* hd = malloc ( sizeof ( ldr_header_t ) );
136 hd->magic=reverse_endian16 ( magic );
137 hd->control=0; //FixMe: Make use of it once compression is around
138 hd->rev_min = reverse_endian16 ( rev_min );
139 hd->rev_maj = reverse_endian16 ( rev_maj );
140 hd->build_date = reverse_endian32 ( build_date );
141 hd->filelen = reverse_endian32 ( filelen );
142 hd->ldaddress = reverse_endian32 ( ldaddress );
143 printf ( "Creating header for %s...\n", filename );
144 if ( strlen ( filename ) >63 )
145 {
146 printf ( "[!] Filename too long - stripping it to 63 bytes.\n" );
147 strncpy ( ( char* ) &hd->filename, filename, 63 );
148 hd->filename[63]=0x00;
149 }
150 else
151 {
152 strcpy ( ( char* ) &hd->filename, filename );
153 }
154 hd->crc=reverse_endian32 ( crc_data );
155 hd->hcs = reverse_endian16 ( get_hcs ( hd ) );
156 return hd;
157 }
158
159 static char control_unc[] = "Uncompressed";
160 static char control_lz[] = "LZRW1/KH";
161 static char control_mlzo[] = "mini-LZO";
162 static char control_nrv[] = "NRV2D99 [Bootloader?]";
163 static char control_nstdlzma[] = "(non-standard) LZMA";
164 static char control_unk[] = "Unknown";
165 char* get_control_info ( uint16_t control )
166 {
167 control = reverse_endian16 ( control );
168 switch ( control )
169 {
170 case 0:
171 return control_unc;
172 break;
173 case 1:
174 return control_lz;
175 break;
176 case 2:
177 return control_mlzo;
178 break;
179 case 3:
180 return control_unc;
181 break;
182 case 4:
183 return control_nrv;
184 break;
185 case 5:
186 return control_nstdlzma;
187 break;
188 case 6:
189 return control_unc;
190 break;
191 case 7:
192 return control_unc;
193 break;
194 default:
195 return control_unk;
196 break;
197 }
198
199 }
200
201 int dump_header ( ldr_header_t* hd )
202 {
203 printf ( "=== Header Information ===\n" );
204 printf ( "Header magic:\t0x%04X\n",reverse_endian16 ( hd->magic ) );
205 printf ( "Control:\t0x%04X (%s)\n",reverse_endian16 ( hd->control ), get_control_info ( hd->control ) );
206 printf ( "Major rev. :\t0x%04X\n",reverse_endian16 ( hd->rev_maj ) );
207 printf ( "Minor rev. :\t0x%04X\n",reverse_endian16 ( hd->rev_min ) );
208 printf ( "File name :\t%s\n", ( char* ) &hd->filename );
209 printf ( "File length:\t%d bytes\n", reverse_endian32 ( hd->filelen ) );
210 printf ( "Build time:\t0x%08X //FixMe: print in human-readable form\n", reverse_endian32 ( hd->build_date ) ); //FixMe:
211 printf ( "HCS:\t\t0x%04X ",reverse_endian16 ( hd->hcs ) );
212 uint16_t hcs = get_hcs ( hd );
213 int ret=0;
214 if ( hcs ==reverse_endian16 ( hd->hcs ) )
215 {
216 printf ( "(OK!)\n" );
217 }
218 else
219 {
220 printf ( "(ERROR! expected 0x%04X)\n",hcs );
221 ret=1;
222 }
223 //printf("HCS:\t0x%02X",reverse_endian32(hd->hcs));
224 printf ( "Load address:\t0x%08X\n", reverse_endian32 ( hd->ldaddress ) ); //FixMe:
225 printf ( "HNW:\t\t0x%04X\n",reverse_endian16 ( hd->her_znaet_chto ) ); //Hell knows what
226 printf ( "CRC:\t\t0x%08X\n",reverse_endian32 ( hd->crc ) );
227 printf ( "=== Binary Header Dump===\n" );
228 int i;
229 uint8_t* head = ( uint8_t* ) hd;
230 for ( i=0;i<=sizeof ( ldr_header_t );i++ )
231 {
232 if ( i % 8==0 )
233 printf ( "\n" );
234 printf ( "0x%02x ",head[i] );
235 }
236 printf ( "\n\n== End Of Header dump ==\n" );
237 return ret;
238 }
239
240
241 void print_copyright()
242 {
243 printf ( "Part of bcm-utils package ver. " UTIL_VERSION " \n" );
244 printf ( "Copyright (C) 2009 Andrew 'Necromant' Andrianov\n"
245 "This is free software, and you are welcome to redistribute it\n"
246 "under certain conditions. See COPYING for details\n" );
247 }