Add lpcrc tool
authorAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
Fri, 14 Jan 2011 00:32:44 +0000 (00:32 +0000)
committerAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
Fri, 14 Jan 2011 00:32:44 +0000 (00:32 +0000)
Signed-off-by: Asbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>

tools/lpcrc/.gitignore [new file with mode: 0644]
tools/lpcrc/Makefile [new file with mode: 0644]
tools/lpcrc/lpcrc.c [new file with mode: 0644]
tools/lpcrc/lpcrc.sh [new file with mode: 0755]

diff --git a/tools/lpcrc/.gitignore b/tools/lpcrc/.gitignore
new file mode 100644 (file)
index 0000000..3719d13
--- /dev/null
@@ -0,0 +1 @@
+lpcrc
diff --git a/tools/lpcrc/Makefile b/tools/lpcrc/Makefile
new file mode 100644 (file)
index 0000000..6957146
--- /dev/null
@@ -0,0 +1,12 @@
+CC = gcc
+LD = gcc
+LDFLAGS = -Wall -O4 -std=c99
+EXES = lpcrc
+
+all: $(EXES)
+
+% : %.c
+       $(LD) $(LDFLAGS) -o $@ $<
+
+clean: 
+       rm -f $(EXES)
diff --git a/tools/lpcrc/lpcrc.c b/tools/lpcrc/lpcrc.c
new file mode 100644 (file)
index 0000000..75f7880
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Software License Agreement (BSD License)
+ *
+ * Copyright (c) 2010, Roel Verdult
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holders nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <assert.h>
+
+#define BLOCK_COUNT 7
+#define BLOCK_LENGTH 4
+#define BLOCK_TOTAL (BLOCK_COUNT*BLOCK_LENGTH)
+
+typedef unsigned char byte_t;
+
+int main(int argc, char *argv[])
+{
+       FILE* pf;
+       byte_t buf[BLOCK_TOTAL];
+       uint32_t crc = 0;
+       uint32_t block;
+       uint32_t cur_crc = 0;
+
+       // Check for required arguments
+       if (argc < 2)
+       {
+               printf("syntax: lpcrc <firmware.bin>\n");
+               return 1;
+       }
+
+       // Try to open the supplied firmware
+       if ((pf = fopen(argv[1],"rb")) == NULL)
+       {
+               printf("error: could not open file [%s]\n", argv[1]);
+               return 1;
+       }
+
+       // Read out the data blocks used for crc calculation
+       if (fread(buf,1,BLOCK_TOTAL,pf) != BLOCK_TOTAL)
+       {
+               printf("error: could not read required bytes\n");
+               fclose(pf);
+               return 1;
+       }
+
+       // Compute the crc value
+       for (block=0; block<BLOCK_COUNT; block++)
+       {
+               crc += *((uint32_t*)(buf+(block*BLOCK_LENGTH)));
+       }
+       crc = (~crc) + 1;
+
+       assert(fread(&cur_crc,1,BLOCK_LENGTH,pf) == BLOCK_LENGTH);
+       fclose(pf);
+
+       printf("cur_crc: %08x\n", cur_crc);
+       printf("new_crc: %08x\n", crc);
+       if (cur_crc != crc)
+               printf("CRC differs\n");
+
+       return 0;
+}
diff --git a/tools/lpcrc/lpcrc.sh b/tools/lpcrc/lpcrc.sh
new file mode 100755 (executable)
index 0000000..ee36dd5
--- /dev/null
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+binary=$1
+source=$2
+
+lpcrc=$(dirname $(readlink -f $0))/lpcrc
+
+crcoutput=$($lpcrc $1)
+
+echo "$crcoutput" | grep -q differs
+if [ $? -eq 0 ] ; then
+       crc=$(echo "$crcoutput" | grep new_crc | cut -d' ' -f2)
+       sed -i -e \
+         's/^\(\s\+0x\)[a-f0-9]\{8\}\(,\s\+\/\/\ Interrupt CRC\)/\1'$crc'\2/g' \
+         $source
+       make crc
+fi