initial import
[patches.git] / 0001-tools-envcrc.c-use-the-target-sizeof-rather-than-bu.patch
1 From d7603745eb4cd64397f741746afaeba230e69bc4 Mon Sep 17 00:00:00 2001
2 From: Mike Frysinger <vapier@gentoo.org>
3 Date: Sun, 30 Mar 2008 21:36:07 -0400
4 Subject: [PATCH] tools/envcrc.c: use the target sizeof rather than build
5
6 The envcrc.c does sizeof(unsigned long) when calculating the crc, but this
7 is done with the build toolchain instead of the target toolchain, so if
8 the build is a 64bit system but the target is 32bits, the size will
9 obviously be wrong. This introduces a sizeof.sh script to calculate the
10 required sizeof() value of any type with any compiler.
11
12 Signed-off-by: Mike Frysinger <vapier@gentoo.org>
13 ---
14 tools/Makefile | 2 +-
15 tools/envcrc.c | 4 ++--
16 tools/sizeof.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++
17 3 files changed, 48 insertions(+), 3 deletions(-)
18 create mode 100755 tools/sizeof.sh
19
20 diff --git a/tools/Makefile b/tools/Makefile
21 index 8784a6d..7351f28 100644
22 --- a/tools/Makefile
23 +++ b/tools/Makefile
24 @@ -168,7 +168,7 @@ $(obj)mpc86x_clk$(SFX): $(obj)mpc86x_clk.o
25 $(STRIP) $@
26
27 $(obj)envcrc.o: $(src)envcrc.c
28 - $(CC) -g $(CFLAGS) -c -o $@ $<
29 + $(CC) -g $(CFLAGS) -c -o $@ $< -DTARGET_SIZEOF_ULONG=$(shell $(CONFIG_SHELL) $(src)sizeof.sh unsigned long)
30
31 $(obj)ubsha1.o: $(src)ubsha1.c
32 $(CC) -g $(CFLAGS) -c -o $@ $<
33 diff --git a/tools/envcrc.c b/tools/envcrc.c
34 index 7b77183..afde912 100644
35 --- a/tools/envcrc.c
36 +++ b/tools/envcrc.c
37 @@ -58,9 +58,9 @@
38 #endif /* CFG_ENV_IS_IN_FLASH */
39
40 #ifdef CFG_REDUNDAND_ENVIRONMENT
41 -# define ENV_HEADER_SIZE (sizeof(unsigned long) + 1)
42 +# define ENV_HEADER_SIZE (TARGET_SIZEOF_ULONG + 1)
43 #else
44 -# define ENV_HEADER_SIZE (sizeof(unsigned long))
45 +# define ENV_HEADER_SIZE (TARGET_SIZEOF_ULONG)
46 #endif
47
48 #define ENV_SIZE (CFG_ENV_SIZE - ENV_HEADER_SIZE)
49 diff --git a/tools/sizeof.sh b/tools/sizeof.sh
50 new file mode 100755
51 index 0000000..4eadb3c
52 --- /dev/null
53 +++ b/tools/sizeof.sh
54 @@ -0,0 +1,45 @@
55 +#!/bin/sh
56 +# Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006 Free Software
57 +# Foundation, Inc.
58 +# Licensed under the GPL-2 or later.
59 +
60 +#
61 +# Get the sizeof(type) for a compiler
62 +#
63 +
64 +type="$*"
65 +CC=${CC:-gcc}
66 +
67 +if [ -z "$type" ] ; then
68 + echo "Usage: sizeof.sh <type>" 1>&2
69 + exit 1
70 +fi
71 +
72 +test_size() {
73 + cat <<-EOF > sizeof.c
74 + typedef $type ac__type_sizeof_;
75 + int main() {
76 + static int test_array [1 - 2 * !(((long int) (sizeof (ac__type_sizeof_))) <= $size)];
77 + test_array [0] = 0;
78 + return 0;
79 + }
80 + EOF
81 + ${CC} -c sizeof.c >/dev/null 2>&1
82 + ret=$?
83 + rm -f sizeof.[co]
84 + return $ret
85 +}
86 +
87 +size=4
88 +while [ ${size} -lt 512 ] ; do
89 + if test_size ${size} ; then
90 + ((size-=1))
91 + while test_size ${size} ; do
92 + ((size-=1))
93 + done
94 + echo $((size+1))
95 + exit 0
96 + fi
97 + ((size+=4))
98 +done
99 +exit 1
100 --
101 1.5.4.4
102