aboutsummaryrefslogtreecommitdiff
path: root/tool/mbed/lpc-vector-checksum.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2014-11-24 13:50:33 +0900
committertmk <nobody@nowhere>2014-11-24 13:50:33 +0900
commit363950982a291c3bfa03ac6362061b1d37dc06b0 (patch)
treec46fc53fe00137ced3c8edd3d0766ee844f77516 /tool/mbed/lpc-vector-checksum.c
parenteb90ed6238426db9367e294abfaefb5de07564f5 (diff)
parent60096e11c77980ca6b54674c5b68248e8aa15d8d (diff)
downloadqmk_firmware-363950982a291c3bfa03ac6362061b1d37dc06b0.tar.gz
qmk_firmware-363950982a291c3bfa03ac6362061b1d37dc06b0.zip
Merge branch 'rn42' into merge_rn42
Conflicts: .gitignore common.mk common/debug_config.h common/print.h
Diffstat (limited to 'tool/mbed/lpc-vector-checksum.c')
-rw-r--r--tool/mbed/lpc-vector-checksum.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/tool/mbed/lpc-vector-checksum.c b/tool/mbed/lpc-vector-checksum.c
new file mode 100644
index 000000000..316a1253a
--- /dev/null
+++ b/tool/mbed/lpc-vector-checksum.c
@@ -0,0 +1,99 @@
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*/
36static 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*/
81int 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