13 #define UTIL_VERSION "0.1"
14 #define ENDIAN_REVERSE_NEEDED
16 uint32_t reverse_endian32 ( uint32_t data )
18 #ifdef ENDIAN_REVERSE_NEEDED
19 return 0 | ( data & 0x000000ff ) << 24
20 | ( data & 0x0000ff00 ) << 8
21 | ( data & 0x00ff0000 ) >> 8
22 | ( data & 0xff000000 ) >> 24;
28 uint16_t reverse_endian16 ( uint16_t data )
30 #ifdef ENDIAN_REVERSE_NEEDED
31 return 0 | ( data & 0x00ff ) << 8
32 | ( data & 0xff00 ) >> 8;
40 uint32_t get_buffer_crc ( char* filebuffer,size_t size )
44 long crcxor = 0xffffffffL;
45 long num4 = 0xffffffffL;
47 long num6 = 0x4c11db7L;
48 long num7 = 0x80000000L;
51 for ( i = 0; i < ( num5 ); i++ )
53 long num2 = filebuffer[i];
54 for ( j = 0x80L; j != 0L; j = j >> 1 )
56 long num3 = crc & num7;
58 if ( ( num2 & j ) != 0L )
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 );
79 //Thnx to Vector for the algo.
80 uint32_t get_file_crc ( char* filename )
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 );
88 uint32_t crc = get_buffer_crc ( filebuffer,buf.st_size );
95 uint16_t get_hcs ( ldr_header_t* hd )
97 uint8_t* head = ( uint8_t* ) hd;
104 for ( i = 0; i < 0x54; i++ )
106 uint16_t m = head[i];
108 for ( j = 0; j < 8; j++ )
110 if ( ( ( n ^ m ) & 0x8000 ) == 0 )
128 hcs_major = ( uint8_t ) ( ( n & 0xff00 ) >> 8 );
129 hcs_minor = ( uint8_t ) ( n & 0xff );
130 uint16_t hcs = hcs_major <<8 | hcs_minor;
134 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 )
136 ldr_header_t* hd = malloc ( sizeof ( ldr_header_t ) );
137 hd->magic=reverse_endian16 ( magic );
138 hd->control=0; //FixMe: Make use of it once compression is around
139 hd->rev_min = reverse_endian16 ( rev_min );
140 hd->rev_maj = reverse_endian16 ( rev_maj );
141 hd->build_date = reverse_endian32 ( build_date );
142 hd->filelen = reverse_endian32 ( filelen );
143 hd->ldaddress = reverse_endian32 ( ldaddress );
144 printf ( "Creating header for %s...\n", filename );
145 if ( strlen ( filename ) >63 )
147 printf ( "[!] Filename too long - stripping it to 63 bytes.\n" );
148 strncpy ( ( char* ) &hd->filename, filename, 63 );
149 hd->filename[63]=0x00;
153 strcpy ( ( char* ) &hd->filename, filename );
155 hd->crc=reverse_endian32 ( crc_data );
156 hd->hcs = reverse_endian16 ( get_hcs ( hd ) );
160 static char control_unc[] = "Uncompressed";
161 static char control_lz[] = "LZRW1/KH";
162 static char control_mlzo[] = "mini-LZO";
163 static char control_nrv[] = "NRV2D99 [Bootloader?]";
164 static char control_nstdlzma[] = "(non-standard) LZMA";
165 static char control_unk[] = "Unknown";
166 char* get_control_info ( uint16_t control )
168 control = reverse_endian16 ( control );
187 return control_nstdlzma;
202 int dump_header ( ldr_header_t* hd )
204 printf ( "=== Header Information ===\n" );
205 printf ( "Header magic:\t0x%04X\n",reverse_endian16 ( hd->magic ) );
206 printf ( "Control:\t0x%04X (%s)\n",reverse_endian16 ( hd->control ), get_control_info ( hd->control ) );
207 printf ( "Major rev. :\t0x%04X\n",reverse_endian16 ( hd->rev_maj ) );
208 printf ( "Minor rev. :\t0x%04X\n",reverse_endian16 ( hd->rev_min ) );
209 printf ( "File name :\t%s\n", ( char* ) &hd->filename );
210 printf ( "File length:\t%d bytes\n", reverse_endian32 ( hd->filelen ) );
211 printf ( "Build time:\t0x%08X //FixMe: print in human-readable form\n", reverse_endian32 ( hd->build_date ) ); //FixMe:
212 printf ( "HCS:\t\t0x%04X ",reverse_endian16 ( hd->hcs ) );
213 uint16_t hcs = get_hcs ( hd );
215 if ( hcs ==reverse_endian16 ( hd->hcs ) )
217 printf ( "(OK!)\n" );
221 printf ( "(ERROR! expected 0x%04X)\n",hcs );
224 //printf("HCS:\t0x%02X",reverse_endian32(hd->hcs));
225 printf ( "Load address:\t0x%08X\n", reverse_endian32 ( hd->ldaddress ) ); //FixMe:
226 printf ( "HNW:\t\t0x%04X\n",reverse_endian16 ( hd->her_znaet_chto ) ); //Hell knows what
227 printf ( "CRC:\t\t0x%08X\n",reverse_endian32 ( hd->crc ) );
228 printf ( "=== Binary Header Dump===\n" );
230 uint8_t* head = ( uint8_t* ) hd;
231 for ( i=0;i<=sizeof ( ldr_header_t );i++ )
235 printf ( "0x%02x ",head[i] );
237 printf ( "\n\n== End Of Header dump ==\n" );
242 void print_copyright()
244 printf ( "Part of bcm-utils package ver. " UTIL_VERSION " \n" );
245 printf ( "Copyright (C) 2009 Andrew 'Necromant' Andrianov\n"
246 "This is free software, and you are welcome to redistribute it\n"
247 "under certain conditions. See COPYING for details\n" );