Skip to contentSkip to conversation

Fix offset calculation for multi-type legacy images.

PATCH New Nick Spence 2 participants 2 messages May 10, 2008 – May 11, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix offset calculation for multi-type legacy images.

Calculation of tail was incorrect when size % 4 == 0.

New code removes the conditional and does the same thing but with arithmetic

---

The minimum change is to re-insert the original check:

	if ((uimage_to_cpu (size[i]) % 4) != 0)
		tail += (4 - uimage_to_cpu (size[i]) % 4);

or a variation on this.

The proposed patch cleans up the code a little further by removing the condition entirely and achieving the same rounding-up effect a little more cleanly and clearly.

 common/image.c |   13 ++++---------
 1 files changed, 4 insertions(+), 9 deletions(-)
common/image.c +4 -9
index f7faf50..9e9e023 100644
@@ -23,7 +23,6 @@
  * MA 02111-1307 USA
  */
 
-
 #ifndef USE_HOSTCC
 #include <common.h>
 #include <watchdog.h>
@@ -242,7 +241,7 @@ void image_multi_getimg (image_header_t *hdr, ulong idx,
 {
 	int i;
 	uint32_t *size;
-	ulong offset, tail, count, img_data;
+	ulong offset, count, img_data;
 
 	/* get number of component */
 	count = image_multi_count (hdr);
@@ -258,19 +257,15 @@ void image_multi_getimg (image_header_t *hdr, ulong idx,
 	if (idx < count) {
 		*len = uimage_to_cpu (size[idx]);
 		offset = 0;
-		tail = 0;
 
 		/* go over all indices preceding requested component idx */
 		for (i = 0; i < idx; i++) {
-			/* add up i-th component size */
-			offset += uimage_to_cpu (size[i]);
-
-			/* add up alignment for i-th component */
-			tail += (4 - uimage_to_cpu (size[i]) % 4);
+			/* add up i-th component size, rounding up to 4 bytes */
+			offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
 		}
 
 		/* calculate idx-th component data address */
-		*data = img_data + offset + tail;
+		*data = img_data + offset;
 	} else {
 		*len = 0;
 		*data = 0;
Signature · 1 line
1.5.2.2
#1 lore raw

PATCH 1/1 Fix offset calculation for multi-type legacy images.

Quoted 12 lines · In message <[email protected]> you wrote:

Calculation of tail was incorrect when size % 4 == 0.

New code removes the conditional and does the same thing but with arithmetic

---

The minimum change is to re-insert the original check:

	if ((uimage_to_cpu (size[i]) % 4) != 0)
		tail += (4 - uimage_to_cpu (size[i]) % 4);
Quoted 8 lines

or a variation on this.

The proposed patch cleans up the code a little further by removing the condition entirely and achieving the same rounding-up effect a little more cleanly and clearly.

 common/image.c |   13 ++++---------
 1 files changed, 4 insertions(+), 9 deletions(-)

Applied, thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Schshschshchsch.
	-- The Gorn, "Arena", stardate 3046.2