aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCédric Le Goater <clg@fr.ibm.com>2014-04-24 03:23:34 -0400
committerBenjamin Herrenschmidt <benh@kernel.crashing.org>2014-04-28 03:36:00 -0400
commit284b52c4c6e32870cacbd16872a7ed9e522cde0d (patch)
tree81f4e9b8f58c902c13a83f2195e3679816c402c9
parent98fd433aa6b02ba1612f9a9b73b8eb2f7e9d3419 (diff)
powerpc/boot: Add 64bit and little endian support to addnote
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 <clg@fr.ibm.com> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
-rw-r--r--arch/powerpc/boot/addnote.c128
1 files 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 @@
6 * 6 *
7 * Copyright 2000 Paul Mackerras. 7 * Copyright 2000 Paul Mackerras.
8 * 8 *
9 * Adapted for 64 bit little endian images by Andrew Tauferner.
10 *
9 * This program is free software; you can redistribute it and/or 11 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License 12 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 13 * as published by the Free Software Foundation; either version
@@ -55,36 +57,61 @@ unsigned int rpanote[N_RPA_DESCR] = {
55 57
56#define ROUNDUP(len) (((len) + 3) & ~3) 58#define ROUNDUP(len) (((len) + 3) & ~3)
57 59
58unsigned char buf[512]; 60unsigned char buf[1024];
61#define ELFDATA2LSB 1
62#define ELFDATA2MSB 2
63static int e_data = ELFDATA2MSB;
64#define ELFCLASS32 1
65#define ELFCLASS64 2
66static int e_class = ELFCLASS32;
59 67
60#define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1])) 68#define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
61#define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2)) 69#define GET_32BE(off) ((GET_16BE(off) << 16U) + GET_16BE((off)+2U))
62 70#define GET_64BE(off) ((((unsigned long long)GET_32BE(off)) << 32ULL) + \
63#define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \ 71 ((unsigned long long)GET_32BE((off)+4ULL)))
64 buf[(off) + 1] = (v) & 0xff) 72#define PUT_16BE(off, v)(buf[off] = ((v) >> 8) & 0xff, \
65#define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \ 73 buf[(off) + 1] = (v) & 0xff)
66 PUT_16BE((off) + 2, (v))) 74#define PUT_32BE(off, v)(PUT_16BE((off), (v) >> 16L), PUT_16BE((off) + 2, (v)))
75#define PUT_64BE(off, v)((PUT_32BE((off), (v) >> 32L), \
76 PUT_32BE((off) + 4, (v))))
77
78#define GET_16LE(off) ((buf[off]) + (buf[(off)+1] << 8))
79#define GET_32LE(off) (GET_16LE(off) + (GET_16LE((off)+2U) << 16U))
80#define GET_64LE(off) ((unsigned long long)GET_32LE(off) + \
81 (((unsigned long long)GET_32LE((off)+4ULL)) << 32ULL))
82#define PUT_16LE(off, v) (buf[off] = (v) & 0xff, \
83 buf[(off) + 1] = ((v) >> 8) & 0xff)
84#define PUT_32LE(off, v) (PUT_16LE((off), (v)), PUT_16LE((off) + 2, (v) >> 16L))
85#define PUT_64LE(off, v) (PUT_32LE((off), (v)), PUT_32LE((off) + 4, (v) >> 32L))
86
87#define GET_16(off) (e_data == ELFDATA2MSB ? GET_16BE(off) : GET_16LE(off))
88#define GET_32(off) (e_data == ELFDATA2MSB ? GET_32BE(off) : GET_32LE(off))
89#define GET_64(off) (e_data == ELFDATA2MSB ? GET_64BE(off) : GET_64LE(off))
90#define PUT_16(off, v) (e_data == ELFDATA2MSB ? PUT_16BE(off, v) : \
91 PUT_16LE(off, v))
92#define PUT_32(off, v) (e_data == ELFDATA2MSB ? PUT_32BE(off, v) : \
93 PUT_32LE(off, v))
94#define PUT_64(off, v) (e_data == ELFDATA2MSB ? PUT_64BE(off, v) : \
95 PUT_64LE(off, v))
67 96
68/* Structure of an ELF file */ 97/* Structure of an ELF file */
69#define E_IDENT 0 /* ELF header */ 98#define E_IDENT 0 /* ELF header */
70#define E_PHOFF 28 99#define E_PHOFF (e_class == ELFCLASS32 ? 28 : 32)
71#define E_PHENTSIZE 42 100#define E_PHENTSIZE (e_class == ELFCLASS32 ? 42 : 54)
72#define E_PHNUM 44 101#define E_PHNUM (e_class == ELFCLASS32 ? 44 : 56)
73#define E_HSIZE 52 /* size of ELF header */ 102#define E_HSIZE (e_class == ELFCLASS32 ? 52 : 64)
74 103
75#define EI_MAGIC 0 /* offsets in E_IDENT area */ 104#define EI_MAGIC 0 /* offsets in E_IDENT area */
76#define EI_CLASS 4 105#define EI_CLASS 4
77#define EI_DATA 5 106#define EI_DATA 5
78 107
79#define PH_TYPE 0 /* ELF program header */ 108#define PH_TYPE 0 /* ELF program header */
80#define PH_OFFSET 4 109#define PH_OFFSET (e_class == ELFCLASS32 ? 4 : 8)
81#define PH_FILESZ 16 110#define PH_FILESZ (e_class == ELFCLASS32 ? 16 : 32)
82#define PH_HSIZE 32 /* size of program header */ 111#define PH_HSIZE (e_class == ELFCLASS32 ? 32 : 56)
83 112
84#define PT_NOTE 4 /* Program header type = note */ 113#define PT_NOTE 4 /* Program header type = note */
85 114
86#define ELFCLASS32 1
87#define ELFDATA2MSB 2
88 115
89unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' }; 116unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
90 117
@@ -92,8 +119,8 @@ int
92main(int ac, char **av) 119main(int ac, char **av)
93{ 120{
94 int fd, n, i; 121 int fd, n, i;
95 int ph, ps, np; 122 unsigned long ph, ps, np;
96 int nnote, nnote2, ns; 123 long nnote, nnote2, ns;
97 124
98 if (ac != 2) { 125 if (ac != 2) {
99 fprintf(stderr, "Usage: %s elf-file\n", av[0]); 126 fprintf(stderr, "Usage: %s elf-file\n", av[0]);
@@ -114,26 +141,27 @@ main(int ac, char **av)
114 exit(1); 141 exit(1);
115 } 142 }
116 143
117 if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0) 144 if (memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
145 goto notelf;
146 e_class = buf[E_IDENT+EI_CLASS];
147 if (e_class != ELFCLASS32 && e_class != ELFCLASS64)
148 goto notelf;
149 e_data = buf[E_IDENT+EI_DATA];
150 if (e_data != ELFDATA2MSB && e_data != ELFDATA2LSB)
151 goto notelf;
152 if (n < E_HSIZE)
118 goto notelf; 153 goto notelf;
119 154
120 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32 155 ph = (e_class == ELFCLASS32 ? GET_32(E_PHOFF) : GET_64(E_PHOFF));
121 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) { 156 ps = GET_16(E_PHENTSIZE);
122 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n", 157 np = GET_16(E_PHNUM);
123 av[1]);
124 exit(1);
125 }
126
127 ph = GET_32BE(E_PHOFF);
128 ps = GET_16BE(E_PHENTSIZE);
129 np = GET_16BE(E_PHNUM);
130 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1) 158 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
131 goto notelf; 159 goto notelf;
132 if (ph + (np + 2) * ps + nnote + nnote2 > n) 160 if (ph + (np + 2) * ps + nnote + nnote2 > n)
133 goto nospace; 161 goto nospace;
134 162
135 for (i = 0; i < np; ++i) { 163 for (i = 0; i < np; ++i) {
136 if (GET_32BE(ph + PH_TYPE) == PT_NOTE) { 164 if (GET_32(ph + PH_TYPE) == PT_NOTE) {
137 fprintf(stderr, "%s already has a note entry\n", 165 fprintf(stderr, "%s already has a note entry\n",
138 av[1]); 166 av[1]);
139 exit(0); 167 exit(0);
@@ -148,15 +176,22 @@ main(int ac, char **av)
148 176
149 /* fill in the program header entry */ 177 /* fill in the program header entry */
150 ns = ph + 2 * ps; 178 ns = ph + 2 * ps;
151 PUT_32BE(ph + PH_TYPE, PT_NOTE); 179 PUT_32(ph + PH_TYPE, PT_NOTE);
152 PUT_32BE(ph + PH_OFFSET, ns); 180 if (e_class == ELFCLASS32)
153 PUT_32BE(ph + PH_FILESZ, nnote); 181 PUT_32(ph + PH_OFFSET, ns);
182 else
183 PUT_64(ph + PH_OFFSET, ns);
184
185 if (e_class == ELFCLASS32)
186 PUT_32(ph + PH_FILESZ, nnote);
187 else
188 PUT_64(ph + PH_FILESZ, nnote);
154 189
155 /* fill in the note area we point to */ 190 /* fill in the note area we point to */
156 /* XXX we should probably make this a proper section */ 191 /* XXX we should probably make this a proper section */
157 PUT_32BE(ns, strlen(arch) + 1); 192 PUT_32(ns, strlen(arch) + 1);
158 PUT_32BE(ns + 4, N_DESCR * 4); 193 PUT_32(ns + 4, N_DESCR * 4);
159 PUT_32BE(ns + 8, 0x1275); 194 PUT_32(ns + 8, 0x1275);
160 strcpy((char *) &buf[ns + 12], arch); 195 strcpy((char *) &buf[ns + 12], arch);
161 ns += 12 + strlen(arch) + 1; 196 ns += 12 + strlen(arch) + 1;
162 for (i = 0; i < N_DESCR; ++i, ns += 4) 197 for (i = 0; i < N_DESCR; ++i, ns += 4)
@@ -164,21 +199,28 @@ main(int ac, char **av)
164 199
165 /* fill in the second program header entry and the RPA note area */ 200 /* fill in the second program header entry and the RPA note area */
166 ph += ps; 201 ph += ps;
167 PUT_32BE(ph + PH_TYPE, PT_NOTE); 202 PUT_32(ph + PH_TYPE, PT_NOTE);
168 PUT_32BE(ph + PH_OFFSET, ns); 203 if (e_class == ELFCLASS32)
169 PUT_32BE(ph + PH_FILESZ, nnote2); 204 PUT_32(ph + PH_OFFSET, ns);
205 else
206 PUT_64(ph + PH_OFFSET, ns);
207
208 if (e_class == ELFCLASS32)
209 PUT_32(ph + PH_FILESZ, nnote);
210 else
211 PUT_64(ph + PH_FILESZ, nnote2);
170 212
171 /* fill in the note area we point to */ 213 /* fill in the note area we point to */
172 PUT_32BE(ns, strlen(rpaname) + 1); 214 PUT_32(ns, strlen(rpaname) + 1);
173 PUT_32BE(ns + 4, sizeof(rpanote)); 215 PUT_32(ns + 4, sizeof(rpanote));
174 PUT_32BE(ns + 8, 0x12759999); 216 PUT_32(ns + 8, 0x12759999);
175 strcpy((char *) &buf[ns + 12], rpaname); 217 strcpy((char *) &buf[ns + 12], rpaname);
176 ns += 12 + ROUNDUP(strlen(rpaname) + 1); 218 ns += 12 + ROUNDUP(strlen(rpaname) + 1);
177 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4) 219 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
178 PUT_32BE(ns, rpanote[i]); 220 PUT_32BE(ns, rpanote[i]);
179 221
180 /* Update the number of program headers */ 222 /* Update the number of program headers */
181 PUT_16BE(E_PHNUM, np + 2); 223 PUT_16(E_PHNUM, np + 2);
182 224
183 /* write back */ 225 /* write back */
184 lseek(fd, (long) 0, SEEK_SET); 226 lseek(fd, (long) 0, SEEK_SET);