[coreboot] New patch to review for coreboot: 97f7138 cbfstool: make endian detection code more robust

Mathias Krause (minipli@googlemail.com) gerrit at coreboot.org
Tue Jul 17 21:21:31 CEST 2012

Mathias Krause (minipli at googlemail.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/1234


commit 97f71387c02b23fd66b90fe43a4096d36d6dad47
Author: Mathias Krause <minipli at googlemail.com>
Date:   Tue Jul 17 20:52:17 2012 +0200

    cbfstool: make endian detection code more robust
    Accessing the memory of a char array through a uint32_t pointer breaks
    strict-aliasing rules as it dereferences memory with lower alignment
    requirements than the type of the pointer requires. It's no problem on
    x86 as the architecture is able to handle unaligned memory access but
    other architectures are not.
    Fix this by doing the test the other way around -- accessing the first
    byte of a uint32_t variable though a uint8_t pointer.
    Change-Id: Id340b406597014232741c98a4fd0b7c159f164c2
    Signed-off-by: Mathias Krause <minipli at googlemail.com>
 util/cbfstool/cbfstool.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c
index 9dbdc1c..721bf33 100644
--- a/util/cbfstool/cbfstool.c
+++ b/util/cbfstool/cbfstool.c
@@ -339,9 +339,9 @@ int host_bigendian = 0;
 static void which_endian(void)
-	char test[4] = "1234";
-	uint32_t inttest = *(uint32_t *) test;
-	if (inttest == 0x31323334) {
+	static const uint32_t inttest = 0x12345678;
+	uint8_t inttest_lsb = *(uint8_t *)&inttest;
+	if (inttest_lsb == 0x12) {
 		host_bigendian = 1;

More information about the coreboot mailing list