e811c45cfde962f7b3248a59afbc61932ce0b78c
[rapper.git] / bitbucket_lpc1768 / lpcrc / lpcrc.c
1 /*\r
2  * Software License Agreement (BSD License)\r
3  *\r
4  * Copyright (c) 2010, Roel Verdult\r
5  * All rights reserved.\r
6  *\r
7  * Redistribution and use in source and binary forms, with or without\r
8  * modification, are permitted provided that the following conditions are met:\r
9  * 1. Redistributions of source code must retain the above copyright\r
10  * notice, this list of conditions and the following disclaimer.\r
11  * 2. Redistributions in binary form must reproduce the above copyright\r
12  * notice, this list of conditions and the following disclaimer in the\r
13  * documentation and/or other materials provided with the distribution.\r
14  * 3. Neither the name of the copyright holders nor the\r
15  * names of its contributors may be used to endorse or promote products\r
16  * derived from this software without specific prior written permission.\r
17  *\r
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
24  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28  *\r
29  */\r
30 \r
31 #include <stdio.h>\r
32 #include <stdint.h>\r
33 \r
34 #define BLOCK_COUNT 7\r
35 #define BLOCK_LENGTH 4\r
36 #define BLOCK_TOTAL (BLOCK_COUNT*BLOCK_LENGTH)\r
37 \r
38 typedef unsigned char byte_t;\r
39 \r
40 int main(int argc, char *argv[])\r
41 {\r
42   FILE* pf;\r
43   byte_t buf[BLOCK_TOTAL];\r
44   uint32_t crc = 0;\r
45   uint32_t block;\r
46   \r
47   // Check for required arguments\r
48   if (argc < 2)\r
49   {\r
50     printf("syntax: lpcrc <firmware.bin>\n");\r
51     return 1;\r
52   }\r
53   \r
54   // Try to open the supplied firmware\r
55   if ((pf = fopen(argv[1],"rb+")) == NULL)\r
56   {\r
57     printf("error: could not open file [%s] with write access\n",argv[1]);\r
58     return 1;\r
59   }\r
60 \r
61   // Read out the data blocks used for crc calculation\r
62   if (fread(buf,1,BLOCK_TOTAL,pf) != BLOCK_TOTAL)\r
63   {\r
64     printf("error: could not read required bytes\n");\r
65     fclose(pf);\r
66     return 1;\r
67   }\r
68 \r
69   // Compute the crc value\r
70   for (block=0; block<BLOCK_COUNT; block++)\r
71   {\r
72     crc += *((uint32_t*)(buf+(block*BLOCK_LENGTH)));\r
73   }\r
74   crc = (~crc) + 1;\r
75   \r
76   // Reposition the file stream indicator to switch between read and write\r
77   if (fseek(pf,0,SEEK_CUR) != 0)\r
78   {\r
79     printf("error: could not switch from read to write mode\n");\r
80     fclose(pf);\r
81     return 1;\r
82   }\r
83   \r
84   // Write the crc back to the file\r
85   if (fwrite((byte_t*)&crc,1,BLOCK_LENGTH,pf) != BLOCK_LENGTH)\r
86   {\r
87     printf("error: could not write crc back to file\n");\r
88     fclose(pf);\r
89     return 1;\r
90   }\r
91 \r
92   printf("succesfully updated crc to: %08x\n",crc);\r
93   fclose(pf);\r
94   \r
95   return 0;\r
96 }\r
97 \r