aboutsummaryrefslogtreecommitdiffstats
path: root/arch/powerpc/boot/addnote.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/powerpc/boot/addnote.c')
-rw-r--r--arch/powerpc/boot/addnote.c175
1 files changed, 45 insertions, 130 deletions
diff --git a/arch/powerpc/boot/addnote.c b/arch/powerpc/boot/addnote.c
index 3091d1d21aef..b1e5611b2ab1 100644
--- a/arch/powerpc/boot/addnote.c
+++ b/arch/powerpc/boot/addnote.c
@@ -11,12 +11,7 @@
11 * as published by the Free Software Foundation; either version 11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version. 12 * 2 of the License, or (at your option) any later version.
13 * 13 *
14 * Usage: addnote [-r realbase] zImage [note.elf] 14 * Usage: addnote zImage
15 *
16 * If note.elf is supplied, it is the name of an ELF file that contains
17 * an RPA note to use instead of the built-in one. Alternatively, the
18 * note.elf file may be empty, in which case the built-in RPA note is
19 * used (this is to simplify how this is invoked from the wrapper script).
20 */ 15 */
21#include <stdio.h> 16#include <stdio.h>
22#include <stdlib.h> 17#include <stdlib.h>
@@ -48,29 +43,27 @@ char rpaname[] = "IBM,RPA-Client-Config";
48 */ 43 */
49#define N_RPA_DESCR 8 44#define N_RPA_DESCR 8
50unsigned int rpanote[N_RPA_DESCR] = { 45unsigned int rpanote[N_RPA_DESCR] = {
51 1, /* lparaffinity */ 46 0, /* lparaffinity */
52 128, /* min_rmo_size */ 47 64, /* min_rmo_size */
53 0, /* min_rmo_percent */ 48 0, /* min_rmo_percent */
54 46, /* max_pft_size */ 49 40, /* max_pft_size */
55 1, /* splpar */ 50 1, /* splpar */
56 -1, /* min_load */ 51 -1, /* min_load */
57 1, /* new_mem_def */ 52 0, /* new_mem_def */
58 0, /* ignore_my_client_config */ 53 1, /* ignore_my_client_config */
59}; 54};
60 55
61#define ROUNDUP(len) (((len) + 3) & ~3) 56#define ROUNDUP(len) (((len) + 3) & ~3)
62 57
63unsigned char buf[512]; 58unsigned char buf[512];
64unsigned char notebuf[512];
65 59
66#define GET_16BE(b, off) (((b)[off] << 8) + ((b)[(off)+1])) 60#define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
67#define GET_32BE(b, off) ((GET_16BE((b), (off)) << 16) + \ 61#define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2))
68 GET_16BE((b), (off)+2))
69 62
70#define PUT_16BE(b, off, v) ((b)[off] = ((v) >> 8) & 0xff, \ 63#define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \
71 (b)[(off) + 1] = (v) & 0xff) 64 buf[(off) + 1] = (v) & 0xff)
72#define PUT_32BE(b, off, v) (PUT_16BE((b), (off), (v) >> 16), \ 65#define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \
73 PUT_16BE((b), (off) + 2, (v))) 66 PUT_16BE((off) + 2, (v)))
74 67
75/* Structure of an ELF file */ 68/* Structure of an ELF file */
76#define E_IDENT 0 /* ELF header */ 69#define E_IDENT 0 /* ELF header */
@@ -95,95 +88,25 @@ unsigned char notebuf[512];
95 88
96unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' }; 89unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
97 90
98unsigned char *read_rpanote(const char *fname, int *nnp)
99{
100 int notefd, nr, i;
101 int ph, ps, np;
102 int note, notesize;
103
104 notefd = open(fname, O_RDONLY);
105 if (notefd < 0) {
106 perror(fname);
107 exit(1);
108 }
109 nr = read(notefd, notebuf, sizeof(notebuf));
110 if (nr < 0) {
111 perror("read note");
112 exit(1);
113 }
114 if (nr == 0) /* empty file */
115 return NULL;
116 if (nr < E_HSIZE ||
117 memcmp(&notebuf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0 ||
118 notebuf[E_IDENT+EI_CLASS] != ELFCLASS32 ||
119 notebuf[E_IDENT+EI_DATA] != ELFDATA2MSB)
120 goto notelf;
121 close(notefd);
122
123 /* now look for the RPA-note */
124 ph = GET_32BE(notebuf, E_PHOFF);
125 ps = GET_16BE(notebuf, E_PHENTSIZE);
126 np = GET_16BE(notebuf, E_PHNUM);
127 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
128 goto notelf;
129
130 for (i = 0; i < np; ++i, ph += ps) {
131 if (GET_32BE(notebuf, ph + PH_TYPE) != PT_NOTE)
132 continue;
133 note = GET_32BE(notebuf, ph + PH_OFFSET);
134 notesize = GET_32BE(notebuf, ph + PH_FILESZ);
135 if (notesize < 34 || note + notesize > nr)
136 continue;
137 if (GET_32BE(notebuf, note) != strlen(rpaname) + 1 ||
138 GET_32BE(notebuf, note + 8) != 0x12759999 ||
139 strcmp((char *)&notebuf[note + 12], rpaname) != 0)
140 continue;
141 /* looks like an RPA note, return it */
142 *nnp = notesize;
143 return &notebuf[note];
144 }
145 /* no RPA note found */
146 return NULL;
147
148 notelf:
149 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n", fname);
150 exit(1);
151}
152
153int 91int
154main(int ac, char **av) 92main(int ac, char **av)
155{ 93{
156 int fd, n, i, ai; 94 int fd, n, i;
157 int ph, ps, np; 95 int ph, ps, np;
158 int nnote, nnote2, ns; 96 int nnote, nnote2, ns;
159 unsigned char *rpap; 97
160 char *p, *endp; 98 if (ac != 2) {
161 99 fprintf(stderr, "Usage: %s elf-file\n", av[0]);
162 ai = 1;
163 if (ac >= ai + 2 && strcmp(av[ai], "-r") == 0) {
164 /* process -r realbase */
165 p = av[ai + 1];
166 descr[1] = strtol(p, &endp, 16);
167 if (endp == p || *endp != 0) {
168 fprintf(stderr, "Can't parse -r argument '%s' as hex\n",
169 p);
170 exit(1);
171 }
172 ai += 2;
173 }
174 if (ac != ai + 1 && ac != ai + 2) {
175 fprintf(stderr, "Usage: %s [-r realbase] elf-file [rpanote.elf]\n", av[0]);
176 exit(1); 100 exit(1);
177 } 101 }
178 fd = open(av[ai], O_RDWR); 102 fd = open(av[1], O_RDWR);
179 if (fd < 0) { 103 if (fd < 0) {
180 perror(av[ai]); 104 perror(av[1]);
181 exit(1); 105 exit(1);
182 } 106 }
183 107
184 nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr); 108 nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
185 nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote); 109 nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
186 rpap = NULL;
187 110
188 n = read(fd, buf, sizeof(buf)); 111 n = read(fd, buf, sizeof(buf));
189 if (n < 0) { 112 if (n < 0) {
@@ -197,25 +120,22 @@ main(int ac, char **av)
197 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32 120 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
198 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) { 121 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
199 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n", 122 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
200 av[ai]); 123 av[1]);
201 exit(1); 124 exit(1);
202 } 125 }
203 126
204 if (ac == ai + 2) 127 ph = GET_32BE(E_PHOFF);
205 rpap = read_rpanote(av[ai + 1], &nnote2); 128 ps = GET_16BE(E_PHENTSIZE);
206 129 np = GET_16BE(E_PHNUM);
207 ph = GET_32BE(buf, E_PHOFF);
208 ps = GET_16BE(buf, E_PHENTSIZE);
209 np = GET_16BE(buf, E_PHNUM);
210 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1) 130 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
211 goto notelf; 131 goto notelf;
212 if (ph + (np + 2) * ps + nnote + nnote2 > n) 132 if (ph + (np + 2) * ps + nnote + nnote2 > n)
213 goto nospace; 133 goto nospace;
214 134
215 for (i = 0; i < np; ++i) { 135 for (i = 0; i < np; ++i) {
216 if (GET_32BE(buf, ph + PH_TYPE) == PT_NOTE) { 136 if (GET_32BE(ph + PH_TYPE) == PT_NOTE) {
217 fprintf(stderr, "%s already has a note entry\n", 137 fprintf(stderr, "%s already has a note entry\n",
218 av[ai]); 138 av[1]);
219 exit(0); 139 exit(0);
220 } 140 }
221 ph += ps; 141 ph += ps;
@@ -228,42 +148,37 @@ main(int ac, char **av)
228 148
229 /* fill in the program header entry */ 149 /* fill in the program header entry */
230 ns = ph + 2 * ps; 150 ns = ph + 2 * ps;
231 PUT_32BE(buf, ph + PH_TYPE, PT_NOTE); 151 PUT_32BE(ph + PH_TYPE, PT_NOTE);
232 PUT_32BE(buf, ph + PH_OFFSET, ns); 152 PUT_32BE(ph + PH_OFFSET, ns);
233 PUT_32BE(buf, ph + PH_FILESZ, nnote); 153 PUT_32BE(ph + PH_FILESZ, nnote);
234 154
235 /* fill in the note area we point to */ 155 /* fill in the note area we point to */
236 /* XXX we should probably make this a proper section */ 156 /* XXX we should probably make this a proper section */
237 PUT_32BE(buf, ns, strlen(arch) + 1); 157 PUT_32BE(ns, strlen(arch) + 1);
238 PUT_32BE(buf, ns + 4, N_DESCR * 4); 158 PUT_32BE(ns + 4, N_DESCR * 4);
239 PUT_32BE(buf, ns + 8, 0x1275); 159 PUT_32BE(ns + 8, 0x1275);
240 strcpy((char *) &buf[ns + 12], arch); 160 strcpy((char *) &buf[ns + 12], arch);
241 ns += 12 + strlen(arch) + 1; 161 ns += 12 + strlen(arch) + 1;
242 for (i = 0; i < N_DESCR; ++i, ns += 4) 162 for (i = 0; i < N_DESCR; ++i, ns += 4)
243 PUT_32BE(buf, ns, descr[i]); 163 PUT_32BE(ns, descr[i]);
244 164
245 /* fill in the second program header entry and the RPA note area */ 165 /* fill in the second program header entry and the RPA note area */
246 ph += ps; 166 ph += ps;
247 PUT_32BE(buf, ph + PH_TYPE, PT_NOTE); 167 PUT_32BE(ph + PH_TYPE, PT_NOTE);
248 PUT_32BE(buf, ph + PH_OFFSET, ns); 168 PUT_32BE(ph + PH_OFFSET, ns);
249 PUT_32BE(buf, ph + PH_FILESZ, nnote2); 169 PUT_32BE(ph + PH_FILESZ, nnote2);
250 170
251 /* fill in the note area we point to */ 171 /* fill in the note area we point to */
252 if (rpap) { 172 PUT_32BE(ns, strlen(rpaname) + 1);
253 /* RPA note supplied in file, just copy the whole thing over */ 173 PUT_32BE(ns + 4, sizeof(rpanote));
254 memcpy(buf + ns, rpap, nnote2); 174 PUT_32BE(ns + 8, 0x12759999);
255 } else { 175 strcpy((char *) &buf[ns + 12], rpaname);
256 PUT_32BE(buf, ns, strlen(rpaname) + 1); 176 ns += 12 + ROUNDUP(strlen(rpaname) + 1);
257 PUT_32BE(buf, ns + 4, sizeof(rpanote)); 177 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
258 PUT_32BE(buf, ns + 8, 0x12759999); 178 PUT_32BE(ns, rpanote[i]);
259 strcpy((char *) &buf[ns + 12], rpaname);
260 ns += 12 + ROUNDUP(strlen(rpaname) + 1);
261 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
262 PUT_32BE(buf, ns, rpanote[i]);
263 }
264 179
265 /* Update the number of program headers */ 180 /* Update the number of program headers */
266 PUT_16BE(buf, E_PHNUM, np + 2); 181 PUT_16BE(E_PHNUM, np + 2);
267 182
268 /* write back */ 183 /* write back */
269 lseek(fd, (long) 0, SEEK_SET); 184 lseek(fd, (long) 0, SEEK_SET);
@@ -273,18 +188,18 @@ main(int ac, char **av)
273 exit(1); 188 exit(1);
274 } 189 }
275 if (i < n) { 190 if (i < n) {
276 fprintf(stderr, "%s: write truncated\n", av[ai]); 191 fprintf(stderr, "%s: write truncated\n", av[1]);
277 exit(1); 192 exit(1);
278 } 193 }
279 194
280 exit(0); 195 exit(0);
281 196
282 notelf: 197 notelf:
283 fprintf(stderr, "%s does not appear to be an ELF file\n", av[ai]); 198 fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
284 exit(1); 199 exit(1);
285 200
286 nospace: 201 nospace:
287 fprintf(stderr, "sorry, I can't find space in %s to put the note\n", 202 fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
288 av[ai]); 203 av[1]);
289 exit(1); 204 exit(1);
290} 205}