Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/firmware/ihex2fw: Simplify next record offset calculation

We can convert original expression for 'writelen" to use ALIGN as
follows:

(p->len + 9) & ~3 => (p->len + 6 + 3) & ~3 => ALIGN(p->len + 6, 4)

Now, subsituting "p->len + 6" with "p->len + sizeof(p->addr) +
sizeof(p->len)" we end up with the same expression as used by kernel
couterpart in linux/ihex.h:

ALIGN(p->len + sizeof(p->addr) + sizeof(p->len), 4)

That is a full size of the record, aligned to 4 bytes. No functional
change intended.

Cc: Chris Healy <cphealy@gmail.com>
Cc: Kyle McMartin <kyle@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel <linux-kernel@vger.kernel.org>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Andrey Smirnov and committed by
Greg Kroah-Hartman
2ef8179b 9fb4ab4d

+10 -1
+10 -1
tools/firmware/ihex2fw.c
··· 24 24 #include <getopt.h> 25 25 26 26 27 + #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask)) 28 + #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1) 29 + #define ALIGN(x, a) __ALIGN_KERNEL((x), (a)) 30 + 27 31 struct ihex_binrec { 28 32 struct ihex_binrec *next; /* not part of the real data structure */ 29 33 uint32_t addr; ··· 263 259 *p = record; 264 260 } 265 261 262 + static uint16_t ihex_binrec_size(struct ihex_binrec *p) 263 + { 264 + return p->len + sizeof(p->addr) + sizeof(p->len); 265 + } 266 + 266 267 static int output_records(int outfd) 267 268 { 268 269 unsigned char zeroes[6] = {0, 0, 0, 0, 0, 0}; 269 270 struct ihex_binrec *p = records; 270 271 271 272 while (p) { 272 - uint16_t writelen = (p->len + 9) & ~3; 273 + uint16_t writelen = ALIGN(ihex_binrec_size(p), 4); 273 274 274 275 p->addr = htonl(p->addr); 275 276 p->len = htons(p->len);