2 * Program to hack in a PT_NOTE program header entry in an ELF file.
3 * This is needed for OF on RS/6000s to load an image correctly.
4 * Note that OF needs a program header entry for the note, not an
7 * Copyright 2000 Paul Mackerras.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 * Usage: addnote zImage [note.elf]
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).
27 /* CHRP note section */
28 char arch[] = "PowerPC";
31 unsigned int descr[N_DESCR] = {
32 0xffffffff, /* real-mode = true */
33 0x02000000, /* real-base, i.e. where we expect OF to be */
34 0xffffffff, /* real-size */
35 0xffffffff, /* virt-base */
36 0xffffffff, /* virt-size */
37 0x4000, /* load-base */
40 /* RPA note section */
41 char rpaname[] = "IBM,RPA-Client-Config";
44 * Note: setting ignore_my_client_config *should* mean that OF ignores
45 * all the other fields, but there is a firmware bug which means that
46 * it looks at the splpar field at least. So these values need to be
50 unsigned int rpanote[N_RPA_DESCR] = {
52 128, /* min_rmo_size */
53 0, /* min_rmo_percent */
54 46, /* max_pft_size */
58 0, /* ignore_my_client_config */
61 #define ROUNDUP(len) (((len) + 3) & ~3)
63 unsigned char buf[512];
64 unsigned char notebuf[512];
66 #define GET_16BE(b, off) (((b)[off] << 8) + ((b)[(off)+1]))
67 #define GET_32BE(b, off) ((GET_16BE((b), (off)) << 16) + \
68 GET_16BE((b), (off)+2))
70 #define PUT_16BE(b, off, v) ((b)[off] = ((v) >> 8) & 0xff, \
71 (b)[(off) + 1] = (v) & 0xff)
72 #define PUT_32BE(b, off, v) (PUT_16BE((b), (off), (v) >> 16), \
73 PUT_16BE((b), (off) + 2, (v)))
75 /* Structure of an ELF file */
76 #define E_IDENT 0 /* ELF header */
78 #define E_PHENTSIZE 42
80 #define E_HSIZE 52 /* size of ELF header */
82 #define EI_MAGIC 0 /* offsets in E_IDENT area */
86 #define PH_TYPE 0 /* ELF program header */
89 #define PH_HSIZE 32 /* size of program header */
91 #define PT_NOTE 4 /* Program header type = note */
96 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
98 unsigned char *read_rpanote(const char *fname, int *nnp)
104 notefd = open(fname, O_RDONLY);
109 nr = read(notefd, notebuf, sizeof(notebuf));
114 if (nr == 0) /* empty file */
117 memcmp(¬ebuf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0 ||
118 notebuf[E_IDENT+EI_CLASS] != ELFCLASS32 ||
119 notebuf[E_IDENT+EI_DATA] != ELFDATA2MSB)
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)
130 for (i = 0; i < np; ++i, ph += ps) {
131 if (GET_32BE(notebuf, ph + PH_TYPE) != PT_NOTE)
133 note = GET_32BE(notebuf, ph + PH_OFFSET);
134 notesize = GET_32BE(notebuf, ph + PH_FILESZ);
135 if (notesize < 34 || note + notesize > nr)
137 if (GET_32BE(notebuf, note) != strlen(rpaname) + 1 ||
138 GET_32BE(notebuf, note + 8) != 0x12759999 ||
139 strcmp((char *)¬ebuf[note + 12], rpaname) != 0)
141 /* looks like an RPA note, return it */
143 return ¬ebuf[note];
145 /* no RPA note found */
149 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n", fname);
154 main(int ac, char **av)
158 int nnote, nnote2, ns;
161 if (ac != 2 && ac != 3) {
162 fprintf(stderr, "Usage: %s elf-file [rpanote.elf]\n", av[0]);
165 fd = open(av[1], O_RDWR);
171 nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
172 nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
175 n = read(fd, buf, sizeof(buf));
181 if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
184 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
185 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
186 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
192 rpap = read_rpanote(av[2], &nnote2);
194 ph = GET_32BE(buf, E_PHOFF);
195 ps = GET_16BE(buf, E_PHENTSIZE);
196 np = GET_16BE(buf, E_PHNUM);
197 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
199 if (ph + (np + 2) * ps + nnote + nnote2 > n)
202 for (i = 0; i < np; ++i) {
203 if (GET_32BE(buf, ph + PH_TYPE) == PT_NOTE) {
204 fprintf(stderr, "%s already has a note entry\n",
211 /* XXX check that the area we want to use is all zeroes */
212 for (i = 0; i < 2 * ps + nnote + nnote2; ++i)
213 if (buf[ph + i] != 0)
216 /* fill in the program header entry */
218 PUT_32BE(buf, ph + PH_TYPE, PT_NOTE);
219 PUT_32BE(buf, ph + PH_OFFSET, ns);
220 PUT_32BE(buf, ph + PH_FILESZ, nnote);
222 /* fill in the note area we point to */
223 /* XXX we should probably make this a proper section */
224 PUT_32BE(buf, ns, strlen(arch) + 1);
225 PUT_32BE(buf, ns + 4, N_DESCR * 4);
226 PUT_32BE(buf, ns + 8, 0x1275);
227 strcpy((char *) &buf[ns + 12], arch);
228 ns += 12 + strlen(arch) + 1;
229 for (i = 0; i < N_DESCR; ++i, ns += 4)
230 PUT_32BE(buf, ns, descr[i]);
232 /* fill in the second program header entry and the RPA note area */
234 PUT_32BE(buf, ph + PH_TYPE, PT_NOTE);
235 PUT_32BE(buf, ph + PH_OFFSET, ns);
236 PUT_32BE(buf, ph + PH_FILESZ, nnote2);
238 /* fill in the note area we point to */
240 /* RPA note supplied in file, just copy the whole thing over */
241 memcpy(buf + ns, rpap, nnote2);
243 PUT_32BE(buf, ns, strlen(rpaname) + 1);
244 PUT_32BE(buf, ns + 4, sizeof(rpanote));
245 PUT_32BE(buf, ns + 8, 0x12759999);
246 strcpy((char *) &buf[ns + 12], rpaname);
247 ns += 12 + ROUNDUP(strlen(rpaname) + 1);
248 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
249 PUT_32BE(buf, ns, rpanote[i]);
252 /* Update the number of program headers */
253 PUT_16BE(buf, E_PHNUM, np + 2);
256 lseek(fd, (long) 0, SEEK_SET);
257 i = write(fd, buf, n);
263 fprintf(stderr, "%s: write truncated\n", av[1]);
270 fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
274 fprintf(stderr, "sorry, I can't find space in %s to put the note\n",