2 * addnote.c - 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 modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 /* Usage: addnote zImage */
31 /* CHRP note section */
32 char arch[] = "PowerPC";
35 unsigned int descr[N_DESCR] = {
36 0xffffffff, /* real-mode = true */
37 0x00c00000, /* real-base, i.e. where we expect OF to be */
38 0xffffffff, /* real-size */
39 0xffffffff, /* virt-base */
40 0xffffffff, /* virt-size */
41 0x4000, /* load-base */
44 /* RPA note section */
45 char rpaname[] = "IBM,RPA-Client-Config";
48 * Note: setting ignore_my_client_config *should* mean that OF ignores
49 * all the other fields, but there is a firmware bug which means that
50 * it looks at the splpar field at least. So these values need to be
54 unsigned int rpanote[N_RPA_DESCR] = {
56 64, /* min_rmo_size */
57 0, /* min_rmo_percent */
58 40, /* max_pft_size */
62 1, /* ignore_my_client_config */
65 #define ROUNDUP(len) (((len) + 3) & ~3)
67 unsigned char buf[512];
69 #define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
70 #define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2))
71 #define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \
72 buf[(off) + 1] = (v) & 0xff)
73 #define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \
74 PUT_16BE((off) + 2, (v)))
76 /* Structure of an ELF file */
77 #define E_IDENT 0 /* ELF header */
79 #define E_PHENTSIZE 42
81 #define E_HSIZE 52 /* size of ELF header */
83 #define EI_MAGIC 0 /* offsets in E_IDENT area */
87 #define PH_TYPE 0 /* ELF program header */
90 #define PH_HSIZE 32 /* size of program header */
92 #define PT_NOTE 4 /* Program header type = note */
97 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
100 main(int ac, char **av)
104 int nnote, nnote2, ns;
107 fprintf(stderr, "Usage: %s elf-file\n", av[0]);
110 fd = open(av[1], O_RDWR);
116 nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
117 nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
119 n = read(fd, buf, sizeof(buf));
125 if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
128 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
129 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
130 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
135 ph = GET_32BE(E_PHOFF);
136 ps = GET_16BE(E_PHENTSIZE);
137 np = GET_16BE(E_PHNUM);
138 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
140 if (ph + (np + 2) * ps + nnote + nnote2 > n)
143 for (i = 0; i < np; ++i) {
144 if (GET_32BE(ph + PH_TYPE) == PT_NOTE) {
145 fprintf(stderr, "%s already has a note entry\n",
152 /* XXX check that the area we want to use is all zeroes */
153 for (i = 0; i < 2 * ps + nnote + nnote2; ++i)
154 if (buf[ph + i] != 0)
157 /* fill in the program header entry */
159 PUT_32BE(ph + PH_TYPE, PT_NOTE);
160 PUT_32BE(ph + PH_OFFSET, ns);
161 PUT_32BE(ph + PH_FILESZ, nnote);
163 /* fill in the note area we point to */
164 /* XXX we should probably make this a proper section */
165 PUT_32BE(ns, strlen(arch) + 1);
166 PUT_32BE(ns + 4, N_DESCR * 4);
167 PUT_32BE(ns + 8, 0x1275);
168 strcpy((char *)&buf[ns + 12], arch);
169 ns += 12 + strlen(arch) + 1;
170 for (i = 0; i < N_DESCR; ++i, ns += 4)
171 PUT_32BE(ns, descr[i]);
173 /* fill in the second program header entry and the RPA note area */
175 PUT_32BE(ph + PH_TYPE, PT_NOTE);
176 PUT_32BE(ph + PH_OFFSET, ns);
177 PUT_32BE(ph + PH_FILESZ, nnote2);
179 /* fill in the note area we point to */
180 PUT_32BE(ns, strlen(rpaname) + 1);
181 PUT_32BE(ns + 4, sizeof(rpanote));
182 PUT_32BE(ns + 8, 0x12759999);
183 strcpy((char *)&buf[ns + 12], rpaname);
184 ns += 12 + ROUNDUP(strlen(rpaname) + 1);
185 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
186 PUT_32BE(ns, rpanote[i]);
188 /* Update the number of program headers */
189 PUT_16BE(E_PHNUM, np + 2);
192 lseek(fd, (long) 0, SEEK_SET);
193 i = write(fd, buf, n);
199 fprintf(stderr, "%s: write truncated\n", av[1]);
206 fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
210 fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
217 * c-file-style: "k&r"