blob: b1e5611b2ab1d639edd67346ca50f88d625b9629 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
5 * ELF section.
6 *
7 * Copyright 2000 Paul Mackerras.
8 *
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.
13 *
Paul Mackerras5663a122008-10-31 22:27:17 +110014 * Usage: addnote zImage
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16#include <stdio.h>
17#include <stdlib.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <string.h>
21
Paul Mackerras66faf982005-05-01 08:58:45 -070022/* CHRP note section */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023char arch[] = "PowerPC";
24
25#define N_DESCR 6
26unsigned int descr[N_DESCR] = {
27 0xffffffff, /* real-mode = true */
Tony Breeds9b09c6d2008-06-24 14:20:29 +100028 0x02000000, /* real-base, i.e. where we expect OF to be */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 0xffffffff, /* real-size */
30 0xffffffff, /* virt-base */
31 0xffffffff, /* virt-size */
32 0x4000, /* load-base */
33};
34
Paul Mackerras66faf982005-05-01 08:58:45 -070035/* RPA note section */
36char rpaname[] = "IBM,RPA-Client-Config";
37
38/*
39 * Note: setting ignore_my_client_config *should* mean that OF ignores
40 * all the other fields, but there is a firmware bug which means that
41 * it looks at the splpar field at least. So these values need to be
42 * reasonable.
43 */
44#define N_RPA_DESCR 8
45unsigned int rpanote[N_RPA_DESCR] = {
Paul Mackerras5663a122008-10-31 22:27:17 +110046 0, /* lparaffinity */
47 64, /* min_rmo_size */
Paul Mackerras66faf982005-05-01 08:58:45 -070048 0, /* min_rmo_percent */
Paul Mackerras5663a122008-10-31 22:27:17 +110049 40, /* max_pft_size */
Paul Mackerras66faf982005-05-01 08:58:45 -070050 1, /* splpar */
51 -1, /* min_load */
Paul Mackerras5663a122008-10-31 22:27:17 +110052 0, /* new_mem_def */
53 1, /* ignore_my_client_config */
Paul Mackerras66faf982005-05-01 08:58:45 -070054};
55
56#define ROUNDUP(len) (((len) + 3) & ~3)
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058unsigned char buf[512];
59
Paul Mackerras5663a122008-10-31 22:27:17 +110060#define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
61#define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Paul Mackerras5663a122008-10-31 22:27:17 +110063#define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \
64 buf[(off) + 1] = (v) & 0xff)
65#define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \
66 PUT_16BE((off) + 2, (v)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Structure of an ELF file */
69#define E_IDENT 0 /* ELF header */
70#define E_PHOFF 28
71#define E_PHENTSIZE 42
72#define E_PHNUM 44
73#define E_HSIZE 52 /* size of ELF header */
74
75#define EI_MAGIC 0 /* offsets in E_IDENT area */
76#define EI_CLASS 4
77#define EI_DATA 5
78
79#define PH_TYPE 0 /* ELF program header */
80#define PH_OFFSET 4
81#define PH_FILESZ 16
82#define PH_HSIZE 32 /* size of program header */
83
84#define PT_NOTE 4 /* Program header type = note */
85
86#define ELFCLASS32 1
87#define ELFDATA2MSB 2
88
89unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
90
91int
92main(int ac, char **av)
93{
Paul Mackerras5663a122008-10-31 22:27:17 +110094 int fd, n, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int ph, ps, np;
Paul Mackerras66faf982005-05-01 08:58:45 -070096 int nnote, nnote2, ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Paul Mackerras5663a122008-10-31 22:27:17 +110098 if (ac != 2) {
99 fprintf(stderr, "Usage: %s elf-file\n", av[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 exit(1);
101 }
Paul Mackerras5663a122008-10-31 22:27:17 +1100102 fd = open(av[1], O_RDWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (fd < 0) {
Paul Mackerras5663a122008-10-31 22:27:17 +1100104 perror(av[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 exit(1);
106 }
107
Paul Mackerras66faf982005-05-01 08:58:45 -0700108 nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
109 nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 n = read(fd, buf, sizeof(buf));
112 if (n < 0) {
113 perror("read");
114 exit(1);
115 }
116
117 if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
118 goto notelf;
119
120 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
121 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
122 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
Paul Mackerras5663a122008-10-31 22:27:17 +1100123 av[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 exit(1);
125 }
126
Paul Mackerras5663a122008-10-31 22:27:17 +1100127 ph = GET_32BE(E_PHOFF);
128 ps = GET_16BE(E_PHENTSIZE);
129 np = GET_16BE(E_PHNUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
131 goto notelf;
Paul Mackerras66faf982005-05-01 08:58:45 -0700132 if (ph + (np + 2) * ps + nnote + nnote2 > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto nospace;
134
135 for (i = 0; i < np; ++i) {
Paul Mackerras5663a122008-10-31 22:27:17 +1100136 if (GET_32BE(ph + PH_TYPE) == PT_NOTE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 fprintf(stderr, "%s already has a note entry\n",
Paul Mackerras5663a122008-10-31 22:27:17 +1100138 av[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 exit(0);
140 }
141 ph += ps;
142 }
143
144 /* XXX check that the area we want to use is all zeroes */
Paul Mackerras66faf982005-05-01 08:58:45 -0700145 for (i = 0; i < 2 * ps + nnote + nnote2; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (buf[ph + i] != 0)
147 goto nospace;
148
149 /* fill in the program header entry */
Paul Mackerras66faf982005-05-01 08:58:45 -0700150 ns = ph + 2 * ps;
Paul Mackerras5663a122008-10-31 22:27:17 +1100151 PUT_32BE(ph + PH_TYPE, PT_NOTE);
152 PUT_32BE(ph + PH_OFFSET, ns);
153 PUT_32BE(ph + PH_FILESZ, nnote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* fill in the note area we point to */
156 /* XXX we should probably make this a proper section */
Paul Mackerras5663a122008-10-31 22:27:17 +1100157 PUT_32BE(ns, strlen(arch) + 1);
158 PUT_32BE(ns + 4, N_DESCR * 4);
159 PUT_32BE(ns + 8, 0x1275);
Olaf Heringdecd3002005-08-08 13:24:38 +1000160 strcpy((char *) &buf[ns + 12], arch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 ns += 12 + strlen(arch) + 1;
Paul Mackerras66faf982005-05-01 08:58:45 -0700162 for (i = 0; i < N_DESCR; ++i, ns += 4)
Paul Mackerras5663a122008-10-31 22:27:17 +1100163 PUT_32BE(ns, descr[i]);
Paul Mackerras66faf982005-05-01 08:58:45 -0700164
165 /* fill in the second program header entry and the RPA note area */
166 ph += ps;
Paul Mackerras5663a122008-10-31 22:27:17 +1100167 PUT_32BE(ph + PH_TYPE, PT_NOTE);
168 PUT_32BE(ph + PH_OFFSET, ns);
169 PUT_32BE(ph + PH_FILESZ, nnote2);
Paul Mackerras66faf982005-05-01 08:58:45 -0700170
171 /* fill in the note area we point to */
Paul Mackerras5663a122008-10-31 22:27:17 +1100172 PUT_32BE(ns, strlen(rpaname) + 1);
173 PUT_32BE(ns + 4, sizeof(rpanote));
174 PUT_32BE(ns + 8, 0x12759999);
175 strcpy((char *) &buf[ns + 12], rpaname);
176 ns += 12 + ROUNDUP(strlen(rpaname) + 1);
177 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
178 PUT_32BE(ns, rpanote[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* Update the number of program headers */
Paul Mackerras5663a122008-10-31 22:27:17 +1100181 PUT_16BE(E_PHNUM, np + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* write back */
184 lseek(fd, (long) 0, SEEK_SET);
185 i = write(fd, buf, n);
186 if (i < 0) {
187 perror("write");
188 exit(1);
189 }
190 if (i < n) {
Paul Mackerras5663a122008-10-31 22:27:17 +1100191 fprintf(stderr, "%s: write truncated\n", av[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 exit(1);
193 }
194
195 exit(0);
196
197 notelf:
Paul Mackerras5663a122008-10-31 22:27:17 +1100198 fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 exit(1);
200
201 nospace:
202 fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
Paul Mackerras5663a122008-10-31 22:27:17 +1100203 av[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 exit(1);
205}