[PATCH v2 10/15] powerpc/boot: add 64bit and little endian support to addnote

2014-04-24 Thread Cédric Le Goater
It could certainly be improved using Elf macros and byteswapping
routines, but the initial version of the code is organised to be a
single file program with limited dependencies. yaboot is the same.

Please scream if you want a total rewrite.

Signed-off-by: Cédric Le Goater 
---

Changes since RFC:

 - fixed the note creation which was done with the wrong endianess 
 - increased 'buf' as ELF structures are larger in 64bit

 arch/powerpc/boot/addnote.c |  128 ---
 1 file changed, 85 insertions(+), 43 deletions(-)

diff --git a/arch/powerpc/boot/addnote.c b/arch/powerpc/boot/addnote.c
index 349b5530d2c4..9d9f6f334d3c 100644
--- a/arch/powerpc/boot/addnote.c
+++ b/arch/powerpc/boot/addnote.c
@@ -6,6 +6,8 @@
  *
  * Copyright 2000 Paul Mackerras.
  *
+ * Adapted for 64 bit little endian images by Andrew Tauferner.
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version
@@ -55,36 +57,61 @@ unsigned int rpanote[N_RPA_DESCR] = {
 
 #define ROUNDUP(len)   (((len) + 3) & ~3)
 
-unsigned char buf[512];
+unsigned char buf[1024];
+#define ELFDATA2LSB 1
+#define ELFDATA2MSB 2
+static int e_data = ELFDATA2MSB;
+#define ELFCLASS32  1
+#define ELFCLASS64  2
+static int e_class = ELFCLASS32;
 
 #define GET_16BE(off)  ((buf[off] << 8) + (buf[(off)+1]))
-#define GET_32BE(off)  ((GET_16BE(off) << 16) + GET_16BE((off)+2))
-
-#define PUT_16BE(off, v)   (buf[off] = ((v) >> 8) & 0xff, \
-buf[(off) + 1] = (v) & 0xff)
-#define PUT_32BE(off, v)   (PUT_16BE((off), (v) >> 16), \
-PUT_16BE((off) + 2, (v)))
+#define GET_32BE(off)  ((GET_16BE(off) << 16U) + GET_16BE((off)+2U))
+#define GET_64BE(off)  unsigned long long)GET_32BE(off)) << 32ULL) + \
+   ((unsigned long long)GET_32BE((off)+4ULL)))
+#define PUT_16BE(off, v)(buf[off] = ((v) >> 8) & 0xff, \
+buf[(off) + 1] = (v) & 0xff)
+#define PUT_32BE(off, v)(PUT_16BE((off), (v) >> 16L), PUT_16BE((off) + 2, (v)))
+#define PUT_64BE(off, v)((PUT_32BE((off), (v) >> 32L), \
+ PUT_32BE((off) + 4, (v
+
+#define GET_16LE(off)  ((buf[off]) + (buf[(off)+1] << 8))
+#define GET_32LE(off)  (GET_16LE(off) + (GET_16LE((off)+2U) << 16U))
+#define GET_64LE(off)  ((unsigned long long)GET_32LE(off) + \
+   (((unsigned long long)GET_32LE((off)+4ULL)) << 32ULL))
+#define PUT_16LE(off, v) (buf[off] = (v) & 0xff, \
+ buf[(off) + 1] = ((v) >> 8) & 0xff)
+#define PUT_32LE(off, v) (PUT_16LE((off), (v)), PUT_16LE((off) + 2, (v) >> 
16L))
+#define PUT_64LE(off, v) (PUT_32LE((off), (v)), PUT_32LE((off) + 4, (v) >> 
32L))
+
+#define GET_16(off)(e_data == ELFDATA2MSB ? GET_16BE(off) : GET_16LE(off))
+#define GET_32(off)(e_data == ELFDATA2MSB ? GET_32BE(off) : GET_32LE(off))
+#define GET_64(off)(e_data == ELFDATA2MSB ? GET_64BE(off) : GET_64LE(off))
+#define PUT_16(off, v) (e_data == ELFDATA2MSB ? PUT_16BE(off, v) : \
+PUT_16LE(off, v))
+#define PUT_32(off, v)  (e_data == ELFDATA2MSB ? PUT_32BE(off, v) : \
+PUT_32LE(off, v))
+#define PUT_64(off, v)  (e_data == ELFDATA2MSB ? PUT_64BE(off, v) : \
+PUT_64LE(off, v))
 
 /* Structure of an ELF file */
 #define E_IDENT0   /* ELF header */
-#defineE_PHOFF 28
-#define E_PHENTSIZE42
-#define E_PHNUM44
-#define E_HSIZE52  /* size of ELF header */
+#defineE_PHOFF (e_class == ELFCLASS32 ? 28 : 32)
+#define E_PHENTSIZE(e_class == ELFCLASS32 ? 42 : 54)
+#define E_PHNUM(e_class == ELFCLASS32 ? 44 : 56)
+#define E_HSIZE(e_class == ELFCLASS32 ? 52 : 64)
 
 #define EI_MAGIC   0   /* offsets in E_IDENT area */
 #define EI_CLASS   4
 #define EI_DATA5
 
 #define PH_TYPE0   /* ELF program header */
-#define PH_OFFSET  4
-#define PH_FILESZ  16
-#define PH_HSIZE   32  /* size of program header */
+#define PH_OFFSET  (e_class == ELFCLASS32 ? 4 : 8)
+#define PH_FILESZ  (e_class == ELFCLASS32 ? 16 : 32)
+#define PH_HSIZE   (e_class == ELFCLASS32 ? 32 : 56)
 
 #define PT_NOTE4   /* Program header type = note */
 
-#define ELFCLASS32 1
-#define ELFDATA2MSB2
 
 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
 
@@ -92,8 +119,8 @@ int
 main(int ac, char **av)
 {
int fd, n, i;
-   int ph, ps, np;
-   int nnote, nnote2, ns;
+   unsigned long ph, ps, np;
+   long nnote, nnote2, ns;
 
if (ac != 2) {
fprintf(stderr, "Usage: %s elf-file\n", av[0]);
@@ -114,26 +141,27 @@ main(int ac, char **av)
exit(1);
}
 
-   if (n < E_HSIZE || memc

[PATCH v2 10/15] powerpc/boot: add 64bit and little endian support to addnote

2014-04-14 Thread Cédric Le Goater
It could certainly be improved using Elf macros and byteswapping
routines, but the initial version of the code is organised to be a
single file program with limited dependencies. yaboot is the same.

Please scream if you want a total rewrite.

Signed-off-by: Cédric Le Goater 
---

Changes since RFC:

 - fixed the note creation which was done with the wrong endianess 
 - increased 'buf' as ELF structures are larger in 64bit

 arch/powerpc/boot/addnote.c |  128 ---
 1 file changed, 85 insertions(+), 43 deletions(-)

diff --git a/arch/powerpc/boot/addnote.c b/arch/powerpc/boot/addnote.c
index 349b5530d2c4..9d9f6f334d3c 100644
--- a/arch/powerpc/boot/addnote.c
+++ b/arch/powerpc/boot/addnote.c
@@ -6,6 +6,8 @@
  *
  * Copyright 2000 Paul Mackerras.
  *
+ * Adapted for 64 bit little endian images by Andrew Tauferner.
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version
@@ -55,36 +57,61 @@ unsigned int rpanote[N_RPA_DESCR] = {
 
 #define ROUNDUP(len)   (((len) + 3) & ~3)
 
-unsigned char buf[512];
+unsigned char buf[1024];
+#define ELFDATA2LSB 1
+#define ELFDATA2MSB 2
+static int e_data = ELFDATA2MSB;
+#define ELFCLASS32  1
+#define ELFCLASS64  2
+static int e_class = ELFCLASS32;
 
 #define GET_16BE(off)  ((buf[off] << 8) + (buf[(off)+1]))
-#define GET_32BE(off)  ((GET_16BE(off) << 16) + GET_16BE((off)+2))
-
-#define PUT_16BE(off, v)   (buf[off] = ((v) >> 8) & 0xff, \
-buf[(off) + 1] = (v) & 0xff)
-#define PUT_32BE(off, v)   (PUT_16BE((off), (v) >> 16), \
-PUT_16BE((off) + 2, (v)))
+#define GET_32BE(off)  ((GET_16BE(off) << 16U) + GET_16BE((off)+2U))
+#define GET_64BE(off)  unsigned long long)GET_32BE(off)) << 32ULL) + \
+   ((unsigned long long)GET_32BE((off)+4ULL)))
+#define PUT_16BE(off, v)(buf[off] = ((v) >> 8) & 0xff, \
+buf[(off) + 1] = (v) & 0xff)
+#define PUT_32BE(off, v)(PUT_16BE((off), (v) >> 16L), PUT_16BE((off) + 2, (v)))
+#define PUT_64BE(off, v)((PUT_32BE((off), (v) >> 32L), \
+ PUT_32BE((off) + 4, (v
+
+#define GET_16LE(off)  ((buf[off]) + (buf[(off)+1] << 8))
+#define GET_32LE(off)  (GET_16LE(off) + (GET_16LE((off)+2U) << 16U))
+#define GET_64LE(off)  ((unsigned long long)GET_32LE(off) + \
+   (((unsigned long long)GET_32LE((off)+4ULL)) << 32ULL))
+#define PUT_16LE(off, v) (buf[off] = (v) & 0xff, \
+ buf[(off) + 1] = ((v) >> 8) & 0xff)
+#define PUT_32LE(off, v) (PUT_16LE((off), (v)), PUT_16LE((off) + 2, (v) >> 
16L))
+#define PUT_64LE(off, v) (PUT_32LE((off), (v)), PUT_32LE((off) + 4, (v) >> 
32L))
+
+#define GET_16(off)(e_data == ELFDATA2MSB ? GET_16BE(off) : GET_16LE(off))
+#define GET_32(off)(e_data == ELFDATA2MSB ? GET_32BE(off) : GET_32LE(off))
+#define GET_64(off)(e_data == ELFDATA2MSB ? GET_64BE(off) : GET_64LE(off))
+#define PUT_16(off, v) (e_data == ELFDATA2MSB ? PUT_16BE(off, v) : \
+PUT_16LE(off, v))
+#define PUT_32(off, v)  (e_data == ELFDATA2MSB ? PUT_32BE(off, v) : \
+PUT_32LE(off, v))
+#define PUT_64(off, v)  (e_data == ELFDATA2MSB ? PUT_64BE(off, v) : \
+PUT_64LE(off, v))
 
 /* Structure of an ELF file */
 #define E_IDENT0   /* ELF header */
-#defineE_PHOFF 28
-#define E_PHENTSIZE42
-#define E_PHNUM44
-#define E_HSIZE52  /* size of ELF header */
+#defineE_PHOFF (e_class == ELFCLASS32 ? 28 : 32)
+#define E_PHENTSIZE(e_class == ELFCLASS32 ? 42 : 54)
+#define E_PHNUM(e_class == ELFCLASS32 ? 44 : 56)
+#define E_HSIZE(e_class == ELFCLASS32 ? 52 : 64)
 
 #define EI_MAGIC   0   /* offsets in E_IDENT area */
 #define EI_CLASS   4
 #define EI_DATA5
 
 #define PH_TYPE0   /* ELF program header */
-#define PH_OFFSET  4
-#define PH_FILESZ  16
-#define PH_HSIZE   32  /* size of program header */
+#define PH_OFFSET  (e_class == ELFCLASS32 ? 4 : 8)
+#define PH_FILESZ  (e_class == ELFCLASS32 ? 16 : 32)
+#define PH_HSIZE   (e_class == ELFCLASS32 ? 32 : 56)
 
 #define PT_NOTE4   /* Program header type = note */
 
-#define ELFCLASS32 1
-#define ELFDATA2MSB2
 
 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
 
@@ -92,8 +119,8 @@ int
 main(int ac, char **av)
 {
int fd, n, i;
-   int ph, ps, np;
-   int nnote, nnote2, ns;
+   unsigned long ph, ps, np;
+   long nnote, nnote2, ns;
 
if (ac != 2) {
fprintf(stderr, "Usage: %s elf-file\n", av[0]);
@@ -114,26 +141,27 @@ main(int ac, char **av)
exit(1);
}
 
-   if (n < E_HSIZE || memc