From 59e9080dcb136e2f4227a65140010aa442206dda Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Fri, 3 Nov 2017 13:54:16 -0700 Subject: endian: Fix bebitenc() to actually encode big-endian bebitenc() just runs a downward loop over the same body as lebitenc(). That doesn't give you a byte-swapped result, it gives you the same final value, just starting from the other side to fill it in. (Also, it confused i++ and i--, so it really gives you a compiler error.) The correct code needs to have the array index inverted relative to the bit shift index to produce a big endian result. Change-Id: I5c2da3a196334844ce23468bd0124bbe2f378c46 Signed-off-by: Julius Werner Reviewed-on: https://review.coreboot.org/22322 Tested-by: build bot (Jenkins) Reviewed-by: Aaron Durbin --- payloads/libpayload/include/endian.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'payloads/libpayload') diff --git a/payloads/libpayload/include/endian.h b/payloads/libpayload/include/endian.h index 2655634c93..2dcfa1f8da 100644 --- a/payloads/libpayload/include/endian.h +++ b/payloads/libpayload/include/endian.h @@ -132,8 +132,8 @@ static inline void bebitenc(void *pp, uint32_t u, uint8_t b) uint8_t *p = (uint8_t *)pp; int i; - for (i = (b - 1); i >= 0; i++) - p[i] = (u >> i*8) & 0xFF; + for (i = 0; i < b; i++) + p[(b - 1) - i] = (u >> i*8) & 0xFF; } static inline void be16enc(void *pp, uint16_t u) -- cgit v1.2.3