File: /usr/src/linux/arch/ppc/boot/utils/addnote.c
1 /*
2 * BK Id: SCCS/s.addnote.c 1.7 05/18/01 15:17:23 cort
3 */
4 /*
5 * Program to hack in a PT_NOTE program header entry in an ELF file.
6 * This is needed for OF on RS/6000s to load an image correctly.
7 * Note that OF needs a program header entry for the note, not an
8 * ELF section.
9 *
10 * Copyright 2000 Paul Mackerras.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * Usage: addnote zImage
18 */
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <string.h>
23
24 char arch[] = "PowerPC";
25
26 #define N_DESCR 6
27 unsigned int descr[N_DESCR] = {
28 #if 1
29 /* values for IBM RS/6000 machines */
30 0xffffffff, /* real-mode = true */
31 0x00c00000, /* real-base, i.e. where we expect OF to be */
32 0xffffffff, /* real-size */
33 0xffffffff, /* virt-base */
34 0xffffffff, /* virt-size */
35 0x4000, /* load-base */
36 #else
37 /* values for longtrail CHRP */
38 0, /* real-mode = false */
39 0xffffffff, /* real-base */
40 0xffffffff, /* real-size */
41 0xffffffff, /* virt-base */
42 0xffffffff, /* virt-size */
43 0x00600000, /* load-base */
44 #endif
45 };
46
47 unsigned char buf[512];
48
49 #define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
50 #define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2))
51
52 #define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \
53 buf[(off) + 1] = (v) & 0xff)
54 #define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \
55 PUT_16BE((off) + 2, (v)))
56
57 /* Structure of an ELF file */
58 #define E_IDENT 0 /* ELF header */
59 #define E_PHOFF 28
60 #define E_PHENTSIZE 42
61 #define E_PHNUM 44
62 #define E_HSIZE 52 /* size of ELF header */
63
64 #define EI_MAGIC 0 /* offsets in E_IDENT area */
65 #define EI_CLASS 4
66 #define EI_DATA 5
67
68 #define PH_TYPE 0 /* ELF program header */
69 #define PH_OFFSET 4
70 #define PH_FILESZ 16
71 #define PH_HSIZE 32 /* size of program header */
72
73 #define PT_NOTE 4 /* Program header type = note */
74
75 #define ELFCLASS32 1
76 #define ELFDATA2MSB 2
77
78 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
79
80 int main(int ac, char **av)
81 {
82 int fd, n, i;
83 int ph, ps, np;
84 int nnote, ns;
85
86 if (ac != 2) {
87 fprintf(stderr, "Usage: %s elf-file\n", av[0]);
88 exit(1);
89 }
90 fd = open(av[1], O_RDWR);
91 if (fd < 0) {
92 perror(av[1]);
93 exit(1);
94 }
95
96 nnote = strlen(arch) + 1 + (N_DESCR + 3) * 4;
97
98 n = read(fd, buf, sizeof(buf));
99 if (n < 0) {
100 perror("read");
101 exit(1);
102 }
103
104 if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
105 goto notelf;
106
107 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
108 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
109 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
110 av[1]);
111 exit(1);
112 }
113
114 ph = GET_32BE(E_PHOFF);
115 ps = GET_16BE(E_PHENTSIZE);
116 np = GET_16BE(E_PHNUM);
117 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
118 goto notelf;
119 if (ph + (np + 1) * ps + nnote > n)
120 goto nospace;
121
122 for (i = 0; i < np; ++i) {
123 if (GET_32BE(ph + PH_TYPE) == PT_NOTE) {
124 fprintf(stderr, "%s already has a note entry\n",
125 av[1]);
126 exit(0);
127 }
128 ph += ps;
129 }
130
131 /* XXX check that the area we want to use is all zeroes */
132 for (i = 0; i < ps + nnote; ++i)
133 if (buf[ph + i] != 0)
134 goto nospace;
135
136 /* fill in the program header entry */
137 ns = ph + ps;
138 PUT_32BE(ph + PH_TYPE, PT_NOTE);
139 PUT_32BE(ph + PH_OFFSET, ns);
140 PUT_32BE(ph + PH_FILESZ, nnote);
141
142 /* fill in the note area we point to */
143 /* XXX we should probably make this a proper section */
144 PUT_32BE(ns, strlen(arch) + 1);
145 PUT_32BE(ns + 4, N_DESCR * 4);
146 PUT_32BE(ns + 8, 0x1275);
147 strcpy(&buf[ns + 12], arch);
148 ns += 12 + strlen(arch) + 1;
149 for (i = 0; i < N_DESCR; ++i)
150 PUT_32BE(ns + i * 4, descr[i]);
151
152 /* Update the number of program headers */
153 PUT_16BE(E_PHNUM, np + 1);
154
155 /* write back */
156 lseek(fd, (long) 0, SEEK_SET);
157 i = write(fd, buf, n);
158 if (i < 0) {
159 perror("write");
160 exit(1);
161 }
162 if (i < n) {
163 fprintf(stderr, "%s: write truncated\n", av[1]);
164 exit(1);
165 }
166
167 exit(0);
168
169 notelf:
170 fprintf(stderr, "%s does not appear to be an ELF file\n", av[0]);
171 exit(1);
172
173 nospace:
174 fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
175 av[0]);
176 exit(1);
177 }
178