1 /***************************************************************************
2 * https://github.com/dhylands/projects/blob/master/lpc/lpc-vector-checksum/lpc-vector-checksum.c
4 * Copyright (c) 2012 by Dave Hylands
7 * Permission is granted to any individual or institution to use, copy,
8 * modify, or redistribute this file so long as it is not sold for profit,
9 * and that this copyright notice is retained.
11 ***************************************************************************
13 * This program calculates the vector checksum used in LPC17xx binary
16 * Usage: lpc-vector-checksum file
18 ***************************************************************************/
26 /***************************************************************************/
28 * update_vector_checksum
30 * The algorithim is to write the checksum such that the checksum of the
31 * first 8 words is equal to zero.
33 * The LPC1768 uses little-endian, and this particular routine assumes
34 * that it's running on a little-endian architecture.
36 static int update_vector_checksum( const char *filename )
43 if (( fs = fopen( filename, "r+b" )) == NULL )
45 fprintf( stderr, "Unable to open '%s' for reading/writing (%d): %s\n",
46 filename, errno, strerror( errno ));
50 if ( fread( header, sizeof( header ), 1, fs ) != 1 )
52 fprintf( stderr, "Failed to read header from '%s' (perhaps the file is too small?)",
59 for ( i = 0; i < 7; i++ )
63 printf( "sum = 0x%08x, value to write = 0x%08x\n", sum, -sum );
65 /* write back the checksum to location 7
66 * http://sigalrm.blogspot.jp/2011/10/cortex-m3-exception-vector-checksum.html
68 fseek(fs, 0x1c, SEEK_SET);
70 fwrite(&sum, 4, 1, fs);
77 /***************************************************************************/
81 int main( int argc, char **argv )
87 fprintf( stderr, "Usage: lpc-vector-checksum file ...\n" );
91 for ( arg = 1; arg < argc; arg++ )
93 update_vector_checksum( argv[ arg ]);