aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/quantum.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/quantum.c')
0 files changed, 0 insertions, 0 deletions
'n44' href='#n44'>44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "bcmalgo.h"


#define UTIL_VERSION "0.1"
#define ENDIAN_REVERSE_NEEDED

uint32_t reverse_endian32 ( uint32_t data )
{
#ifdef ENDIAN_REVERSE_NEEDED
	return 0 | ( data & 0x000000ff ) << 24
	       | ( data & 0x0000ff00 ) << 8
	       | ( data & 0x00ff0000 ) >> 8
	       | ( data & 0xff000000 ) >> 24;
#else
	return data;
#endif
}

uint16_t reverse_endian16 ( uint16_t data )
{
#ifdef ENDIAN_REVERSE_NEEDED
	return 0 | ( data & 0x00ff ) << 8
	       | ( data & 0xff00 ) >> 8;
#else
	return data;
#endif
}



uint32_t get_buffer_crc ( char* filebuffer,size_t size )
{

	long crc=0xffffffffL;
	long crcxor = 0xffffffffL;
	long num4 = 0xffffffffL;
	long num5 = size;
	long num6 = 0x4c11db7L;
	long num7 = 0x80000000L;
	int i;
	long j;
	for ( i = 0; i < ( num5 ); i++ )
	{
		long num2 = filebuffer[i];
		for ( j = 0x80L; j != 0L; j = j >> 1 )
		{
			long num3 = crc & num7;
			crc = crc << 1;
			if ( ( num2 & j ) != 0L )
			{
				num3 ^= num7;
			}
			if ( num3 != 0L )
			{
				crc ^= num6;
			}
		}
	}
	crc ^= crcxor;
	crc &= num4;

	uint8_t b1 = ( uint8_t ) ( ( crc & -16777216L ) >> 0x18 );
	uint8_t b2 = ( uint8_t ) ( ( crc & 0xff0000L ) >> 0x10 );
	uint8_t b3 = ( uint8_t ) ( ( crc & 0xff00L ) >> 8 );
	uint8_t b4 = ( uint8_t ) ( crc & 0xffL );
	int32_t crc_result = ( b1 | b2 << 8| b3 << 16| b4 <<24 );
	return reverse_endian32 ( crc_result );
}

//Thnx to Vector for the algo.
uint32_t get_file_crc ( char* filename )
{
	struct stat buf;
	stat ( filename,&buf );
	char* filebuffer = malloc ( buf.st_size+10 );
	FILE* fd = fopen ( filename,"r" );
	fread ( filebuffer, 1, buf.st_size,fd );
	fclose ( fd );
	uint32_t crc = get_buffer_crc ( filebuffer,buf.st_size );
	free ( filebuffer );
	return crc;
}



uint16_t get_hcs ( ldr_header_t* hd )
{
	uint8_t* head = ( uint8_t* ) hd;
	uint8_t hcs_minor;
	uint8_t hcs_major;
	uint16_t n = 0xffff;
	uint16_t m = 0;
	int state = 0;
	int i,j;
	for ( i = 0; i < 0x54; i++ )
	{
		uint16_t m = head[i];
		m = m << 8;
		for ( j = 0; j < 8; j++ )
		{
			if ( ( ( n ^ m ) & 0x8000 ) == 0 )
			{
				state = 0;
			}
			else
			{
				state = 1;
			}
			n = n << 1;
			if ( state )
			{
				n ^= 0x1021;
			}
			m = m << 1;
		}
		n &= 0xffff;
	}
	n ^= 0xffff;
	hcs_major = ( uint8_t ) ( ( n & 0xff00 ) >> 8 );
	hcs_minor = ( uint8_t ) ( n & 0xff );
	uint16_t hcs = hcs_major <<8 | hcs_minor;
	return hcs;
}

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 )
{
	ldr_header_t* hd = malloc ( sizeof ( ldr_header_t ) );
	hd->magic=reverse_endian16 ( magic );
	hd->control=0; //FixMe: Make use of it once compression is around
	hd->rev_min = reverse_endian16 ( rev_min );
	hd->rev_maj = reverse_endian16 ( rev_maj );
	hd->build_date = reverse_endian32 ( build_date );
	hd->filelen = reverse_endian32 ( filelen );
	hd->ldaddress = reverse_endian32 ( ldaddress );
	printf ( "Creating header for %s...\n", filename );