[U-Boot-Users] [PATCH v2] tools/envcrc.c: use the target sizeof rather than build

Mike Frysinger vapier at gentoo.org
Mon Mar 31 03:42:18 CEST 2008


The envcrc.c does sizeof(unsigned long) when calculating the crc, but this
is done with the build toolchain instead of the target toolchain, so if
the build is a 64bit system but the target is 32bits, the size will
obviously be wrong.  This introduces a sizeof.sh script to calculate the
required sizeof() value of any type with any compiler.

Signed-off-by: Mike Frysinger <vapier at gentoo.org>
---
Changes:
 - adds copyright/license
 - increase limit to 512
 - increment by 4 bytes to find "32bit naturals" faster:
(old/new # = number of times gcc is called)
type	| sizeof	| old	| new
char	| 1	| 1	| 5
short	| 2	| 2	| 4
int	| 4	| 4	| 2
long	| 8	| 8	| 3
double	| 8	| 8	| 3
---
 tools/Makefile  |    2 +-
 tools/envcrc.c  |    4 ++--
 tools/sizeof.sh |   45 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 3 deletions(-)
 create mode 100755 tools/sizeof.sh

diff --git a/tools/Makefile b/tools/Makefile
index 8784a6d..7351f28 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -168,7 +168,7 @@ $(obj)mpc86x_clk$(SFX):	$(obj)mpc86x_clk.o
 		$(STRIP) $@
 
 $(obj)envcrc.o:	$(src)envcrc.c
-		$(CC) -g $(CFLAGS) -c -o $@ $<
+		$(CC) -g $(CFLAGS) -c -o $@ $< -DTARGET_SIZEOF_ULONG=$(shell $(CONFIG_SHELL) $(src)sizeof.sh unsigned long)
 
 $(obj)ubsha1.o:	$(src)ubsha1.c
 		$(CC) -g $(CFLAGS) -c -o $@ $<
diff --git a/tools/envcrc.c b/tools/envcrc.c
index 7b77183..afde912 100644
--- a/tools/envcrc.c
+++ b/tools/envcrc.c
@@ -58,9 +58,9 @@
 #endif	/* CFG_ENV_IS_IN_FLASH */
 
 #ifdef CFG_REDUNDAND_ENVIRONMENT
-# define ENV_HEADER_SIZE	(sizeof(unsigned long) + 1)
+# define ENV_HEADER_SIZE	(TARGET_SIZEOF_ULONG + 1)
 #else
-# define ENV_HEADER_SIZE	(sizeof(unsigned long))
+# define ENV_HEADER_SIZE	(TARGET_SIZEOF_ULONG)
 #endif
 
 #define ENV_SIZE (CFG_ENV_SIZE - ENV_HEADER_SIZE)
diff --git a/tools/sizeof.sh b/tools/sizeof.sh
new file mode 100755
index 0000000..4eadb3c
--- /dev/null
+++ b/tools/sizeof.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+# Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006 Free Software
+# Foundation, Inc.
+# Licensed under the GPL-2 or later.
+
+#
+# Get the sizeof(type) for a compiler
+#
+
+type="$*"
+CC=${CC:-gcc}
+
+if [ -z "$type" ] ; then
+	echo "Usage: sizeof.sh <type>" 1>&2
+	exit 1
+fi
+
+test_size() {
+	cat <<-EOF > sizeof.c
+	typedef $type ac__type_sizeof_;
+	int main() {
+		static int test_array [1 - 2 * !(((long int) (sizeof (ac__type_sizeof_))) <= $size)];
+		test_array [0] = 0;
+		return 0;
+	}
+	EOF
+	${CC} -c sizeof.c >/dev/null 2>&1
+	ret=$?
+	rm -f sizeof.[co]
+	return $ret
+}
+
+size=4
+while [ ${size} -lt 512 ] ; do
+	if test_size ${size} ; then
+		((size-=1))
+		while test_size ${size} ; do
+			((size-=1))
+		done
+		echo $((size+1))
+		exit 0
+	fi
+	((size+=4))
+done
+exit 1
-- 
1.5.4.4





More information about the U-Boot mailing list