2 * Software License Agreement (BSD License)
\r
4 * Copyright (c) 2010, Roel Verdult
\r
5 * All rights reserved.
\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
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
34 #define BLOCK_COUNT 7
\r
35 #define BLOCK_LENGTH 4
\r
36 #define BLOCK_TOTAL (BLOCK_COUNT*BLOCK_LENGTH)
\r
38 typedef unsigned char byte_t;
\r
40 int main(int argc, char *argv[])
\r
43 byte_t buf[BLOCK_TOTAL];
\r
47 // Check for required arguments
\r
50 printf("syntax: lpcrc <firmware.bin>\n");
\r
54 // Try to open the supplied firmware
\r
55 if ((pf = fopen(argv[1],"rb+")) == NULL)
\r
57 printf("error: could not open file [%s] with write access\n",argv[1]);
\r
61 // Read out the data blocks used for crc calculation
\r
62 if (fread(buf,1,BLOCK_TOTAL,pf) != BLOCK_TOTAL)
\r
64 printf("error: could not read required bytes\n");
\r
69 // Compute the crc value
\r
70 for (block=0; block<BLOCK_COUNT; block++)
\r
72 crc += *((uint32_t*)(buf+(block*BLOCK_LENGTH)));
\r
76 // Reposition the file stream indicator to switch between read and write
\r
77 if (fseek(pf,0,SEEK_CUR) != 0)
\r
79 printf("error: could not switch from read to write mode\n");
\r
84 // Write the crc back to the file
\r
85 if (fwrite((byte_t*)&crc,1,BLOCK_LENGTH,pf) != BLOCK_LENGTH)
\r
87 printf("error: could not write crc back to file\n");
\r
92 printf("succesfully updated crc to: %08x\n",crc);
\r