summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2010-01-16 17:15:11 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2010-01-16 17:15:11 +0000
commit4875ff436a689ea4c86aefaca174cc00ced40525 (patch)
tree295d5b68a77bf085f510ee03a4d39e5d43d4b428
parentb7cd2ccf64a21dc25dfa2974182d4ebb3c6272f2 (diff)
downloadnuttx-4875ff436a689ea4c86aefaca174cc00ced40525.tar.gz
nuttx-4875ff436a689ea4c86aefaca174cc00ced40525.tar.bz2
nuttx-4875ff436a689ea4c86aefaca174cc00ced40525.zip
Need to handle padding at the end of the binary
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2507 42af7a65-404d-4744-a932-0658087f49c3
-rwxr-xr-xnuttx/configs/ea3131/tools/lpchdr.c55
-rwxr-xr-xnuttx/configs/ea3131/tools/mklpc.sh60
2 files changed, 103 insertions, 12 deletions
diff --git a/nuttx/configs/ea3131/tools/lpchdr.c b/nuttx/configs/ea3131/tools/lpchdr.c
index 919c16865..da9533847 100755
--- a/nuttx/configs/ea3131/tools/lpchdr.c
+++ b/nuttx/configs/ea3131/tools/lpchdr.c
@@ -49,9 +49,13 @@
#include "lpchdr.h"
/************************************************************************************
- * Definitions
+ * Pre-processor Definitions
************************************************************************************/
+#define IO_BUF_SIZE 1024
+#define HDR_SIZE 0x80
+#define HDR_CRC_SIZE 0x6c
+
/************************************************************************************
* Private Data
************************************************************************************/
@@ -108,10 +112,10 @@ static void parse_args(int argc, char **argv)
}
}
-static uint32_t infilecrc32(int infd, size_t len)
+static inline uint32_t infilecrc32(int infd, size_t len, size_t padlen)
{
off_t offset;
- uint8_t buffer[1024];
+ uint8_t buffer[IO_BUF_SIZE];
ssize_t nbytes;
size_t bytesread;
uint32_t crc;
@@ -126,7 +130,7 @@ static uint32_t infilecrc32(int infd, size_t len)
crc = 0;
for (bytesread = 0; bytesread < len; bytesread += nbytes)
{
- nbytes = read(infd, buffer, 1024);
+ nbytes = read(infd, buffer, IO_BUF_SIZE);
if (nbytes < 0)
{
fprintf(stderr, "read failed: %s\n", strerror(errno));
@@ -142,13 +146,17 @@ static uint32_t infilecrc32(int infd, size_t len)
crc = crc32part(buffer, nbytes, crc);
}
}
- return crc;
+
+ /* Add the zero-padding at the end of the binary in the CRC */
+
+ memset(buffer, 0, IO_BUF_SIZE);
+ return crc32part(buffer, padlen, crc);
}
-static void writefile(int infd, int outfd, size_t len)
+static inline void writefile(int infd, int outfd, size_t len, size_t padlen)
{
off_t offset;
- uint8_t buffer[1024];
+ uint8_t buffer[IO_BUF_SIZE];
ssize_t nbytesread;
ssize_t nbyteswritten;
size_t totalread;
@@ -162,7 +170,7 @@ static void writefile(int infd, int outfd, size_t len)
for (totalread = 0; totalread < len; totalread += nbytesread)
{
- nbytesread = read(infd, buffer, 1024);
+ nbytesread = read(infd, buffer, IO_BUF_SIZE);
if (nbytesread < 0)
{
fprintf(stderr, "read failed: %s\n", strerror(errno));
@@ -188,6 +196,21 @@ static void writefile(int infd, int outfd, size_t len)
}
}
}
+
+ /* Write the zero-padding at the end of the binary */
+
+ memset(buffer, 0, IO_BUF_SIZE);
+ nbyteswritten = write(outfd, buffer, padlen);
+ if (nbyteswritten < 0)
+ {
+ fprintf(stderr, "write failed: %s\n", strerror(errno));
+ exit(4);
+ }
+ else if (nbyteswritten != padlen)
+ {
+ fprintf(stderr, "Short writes not handled\n");
+ exit(4);
+ }
}
/************************************************************************************
@@ -199,6 +222,7 @@ int main(int argc, char **argv, char **envp)
struct lpc313x_header_s g_hdr;
struct stat buf;
ssize_t nbytes;
+ size_t padlen;
int infd;
int outfd;
int ret;
@@ -239,12 +263,19 @@ int main(int argc, char **argv, char **envp)
g_hdr.magic = 0x41676d69;
g_hdr.imageType = 0x0000000b;
g_hdr.imageLength = (buf.st_size + sizeof(struct lpc313x_header_s) + 511) & ~0x1ff;
- g_hdr.execution_crc32 = infilecrc32(infd, buf.st_size);
- g_hdr.header_crc32 = crc32((const uint8_t*)&g_hdr, 0x6c);
+
+ /* This is how much we must pad at the end of the binary image. */
+
+ padlen = g_hdr.imageLength - buf.st_size;
+
+ /* Calculate CRCs */
+
+ g_hdr.execution_crc32 = infilecrc32(infd, buf.st_size, padlen);
+ g_hdr.header_crc32 = crc32((const uint8_t*)&g_hdr, HDR_CRC_SIZE);
/* Write the header */
- nbytes = write(outfd, &g_hdr, 0x80);
+ nbytes = write(outfd, &g_hdr, HDR_SIZE);
if (nbytes != 0x80)
{
fprintf(stderr, "write of header to of %s failed: %s\n", g_outfile, strerror(errno));
@@ -253,7 +284,7 @@ int main(int argc, char **argv, char **envp)
/* Copy the input file to the output */
- writefile(infd, outfd, buf.st_size);
+ writefile(infd, outfd, buf.st_size, padlen);
close(infd);
close(outfd);
return 0;
diff --git a/nuttx/configs/ea3131/tools/mklpc.sh b/nuttx/configs/ea3131/tools/mklpc.sh
new file mode 100755
index 000000000..8a1ca3be7
--- /dev/null
+++ b/nuttx/configs/ea3131/tools/mklpc.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+# This script lies in sub-directory configs/ea3131/tools but make be executed
+# from either that directory or TOPDIR
+
+MYNAME=`basename $0`
+if [ -x "$PWD/$MYNAME" ]; then
+ TOPDIR="$PWD/../../.."
+else
+ if [ -x "$PWD/configs/ea3131/tools/$MYNAME" ]; then
+ TOPDIR="$PWD"
+ else
+ echo "This script must be executed from a known director"
+ exit 1
+ fi
+fi
+echo "TOOLDIR: $TOOLDIR"
+
+# The lpchdr could be named lpchdr.exe if we are running under Cygwin or
+# just lpchdr under Linux
+
+TOOLDIR=$TOPDIR/configs/ea3131/tools
+
+if [ ! -d "$TOOLDIR" ]; then
+ echo "Tool directory $TOOLDIR does not exist"
+ exit 1
+fi
+
+if [ -x "$TOOLDIR/lpchdr.exe" ]; then
+ LPCHDR="$TOOLDIR/lpchdr.exe"
+else
+ if [ -x "$TOOLDIR/lpchdr" ]; then
+ LPCHDR="$TOOLDIR/lpchdr"
+ else
+ echo "lpchdr executable does not exist in $TOODIR"
+ echo " - cd $TOOLDIR"
+ echo " - make"
+ fi
+fi
+echo "LPCHDR: $LPCHDR"
+
+# Now get the path to the NuttX executable
+
+NUTTXPATH="$TOPDIR/nuttx"
+
+if [ ! -f "$NUTTXPATH" ]; then
+ echo "NuttX binary does not exist at $NUTTXPATH"
+ echo " - cd $TOPDIR"
+ echo " - make"
+ exit 1
+fi
+echo "NUTTXPATH: $NUTTXPATH"
+
+# Create the binary
+
+echo "COMMAND: $LPCHDR -o $TOPDIR/nuttx.lpc $NUTTXPATH"
+"$LPCHDR" -o "$TOPDIR/nuttx.lpc" "$NUTTXPATH" || \
+ { echo "$LPCHDR failed" ; exit 1 ; }
+echo "Successfully created binary"
+