]> git.donarmstrong.com Git - tmk_firmware.git/blob - tool/mbed/lpc-vector-checksum.c
Merge branch 'merge_rn42'
[tmk_firmware.git] / tool / mbed / lpc-vector-checksum.c
1 /***************************************************************************
2 * https://github.com/dhylands/projects/blob/master/lpc/lpc-vector-checksum/lpc-vector-checksum.c
3 *
4 *     Copyright (c) 2012 by Dave Hylands
5 *           All Rights Reserved
6 *
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.
10 *
11 ***************************************************************************
12 *
13 *  This program calculates the vector checksum used in LPC17xx binary
14 *  images.
15 *
16 *  Usage:   lpc-vector-checksum file
17 *
18 ***************************************************************************/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <errno.h>
24 #include <string.h>
25
26 /***************************************************************************/
27 /**
28 *   update_vector_checksum
29 *  
30 *   The algorithim is to write the checksum such that the checksum of the
31 *   first 8 words is equal to zero.
32 *  
33 *   The LPC1768 uses little-endian, and this particular routine assumes
34 *   that it's running on a little-endian architecture.
35 */
36 static int update_vector_checksum( const char *filename )
37 {
38     uint32_t    sum;
39     uint32_t    header[8];
40     FILE       *fs;
41     int         i;
42
43     if (( fs = fopen( filename, "r+b" )) == NULL )
44     {
45         fprintf( stderr, "Unable to open '%s' for reading/writing (%d): %s\n", 
46                  filename, errno, strerror( errno ));
47         return 0;
48     }
49
50     if ( fread( header, sizeof( header ), 1, fs ) != 1 )
51     {
52         fprintf( stderr, "Failed to read header from '%s' (perhaps the file is too small?)",
53                  filename );
54         fclose( fs );
55         return 0;
56     }
57
58     sum = 0;
59     for ( i = 0; i < 7; i++ )
60     {
61         sum += header[i];
62     }
63     printf( "sum = 0x%08x, value to write = 0x%08x\n", sum, -sum );
64
65     /* write back the checksum to location 7
66      * http://sigalrm.blogspot.jp/2011/10/cortex-m3-exception-vector-checksum.html
67      */
68     fseek(fs, 0x1c, SEEK_SET);
69     sum = -sum;
70     fwrite(&sum, 4, 1, fs);
71
72     fclose( fs );
73
74     return 1;
75 }
76
77 /***************************************************************************/
78 /**
79 *   main
80 */
81 int main( int argc, char **argv )
82 {
83     int arg;
84
85     if ( argc < 2)
86     {
87         fprintf( stderr, "Usage: lpc-vector-checksum file ...\n" );
88         exit( 1 );
89     }
90
91     for ( arg = 1; arg < argc; arg++ )
92     {
93         update_vector_checksum( argv[ arg ]);
94     }
95
96     exit( 0 );
97     return 0;
98 }
99