]> git.ozlabs.org Git - yaboot.git/blob - util/addnote.c
Determine last ext3 LBA to fix wild LBA reads
[yaboot.git] / util / addnote.c
1 /*
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
5  *             ELF section.
6  *
7  *  Copyright 2000 Paul Mackerras.
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 /* Usage: addnote zImage */
25
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 /* CHRP note section */
32 char arch[] = "PowerPC";
33
34 #define N_DESCR 6
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 */
42 };
43
44 /* RPA note section */
45 char rpaname[] = "IBM,RPA-Client-Config";
46
47 /*
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
51  * reasonable.
52  */
53 #define N_RPA_DESCR     8
54 unsigned int rpanote[N_RPA_DESCR] = {
55      0,                         /* lparaffinity */
56      64,                        /* min_rmo_size */
57      0,                         /* min_rmo_percent */
58      40,                        /* max_pft_size */
59      1,                         /* splpar */
60      -1,                        /* min_load */
61      0,                         /* new_mem_def */
62      1,                         /* ignore_my_client_config */
63 };
64
65 #define ROUNDUP(len)    (((len) + 3) & ~3)
66
67 unsigned char buf[512];
68
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)))
75
76 /* Structure of an ELF file */
77 #define E_IDENT         0       /* ELF header */
78 #define E_PHOFF         28
79 #define E_PHENTSIZE     42
80 #define E_PHNUM         44
81 #define E_HSIZE         52      /* size of ELF header */
82
83 #define EI_MAGIC        0       /* offsets in E_IDENT area */
84 #define EI_CLASS        4
85 #define EI_DATA         5
86
87 #define PH_TYPE         0       /* ELF program header */
88 #define PH_OFFSET       4
89 #define PH_FILESZ       16
90 #define PH_HSIZE        32      /* size of program header */
91
92 #define PT_NOTE         4       /* Program header type = note */
93
94 #define ELFCLASS32      1
95 #define ELFDATA2MSB     2
96
97 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
98
99 int
100 main(int ac, char **av)
101 {
102      int fd, n, i;
103      int ph, ps, np;
104      int nnote, nnote2, ns;
105
106      if (ac != 2) {
107           fprintf(stderr, "Usage: %s elf-file\n", av[0]);
108           exit(1);
109      }
110      fd = open(av[1], O_RDWR);
111      if (fd < 0) {
112           perror(av[1]);
113           exit(1);
114      }
115
116      nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
117      nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
118
119      n = read(fd, buf, sizeof(buf));
120      if (n < 0) {
121           perror("read");
122           exit(1);
123      }
124
125      if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
126           goto notelf;
127
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",
131                   av[1]);
132           exit(1);
133      }
134
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)
139           goto notelf;
140      if (ph + (np + 2) * ps + nnote + nnote2 > n)
141           goto nospace;
142
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",
146                        av[1]);
147                exit(0);
148           }
149           ph += ps;
150      }
151
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)
155                goto nospace;
156
157      /* fill in the program header entry */
158      ns = ph + 2 * ps;
159      PUT_32BE(ph + PH_TYPE, PT_NOTE);
160      PUT_32BE(ph + PH_OFFSET, ns);
161      PUT_32BE(ph + PH_FILESZ, nnote);
162
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(&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]);
172
173      /* fill in the second program header entry and the RPA note area */
174      ph += ps;
175      PUT_32BE(ph + PH_TYPE, PT_NOTE);
176      PUT_32BE(ph + PH_OFFSET, ns);
177      PUT_32BE(ph + PH_FILESZ, nnote2);
178
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(&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]);
187
188      /* Update the number of program headers */
189      PUT_16BE(E_PHNUM, np + 2);
190
191      /* write back */
192      lseek(fd, (long) 0, SEEK_SET);
193      i = write(fd, buf, n);
194      if (i < 0) {
195           perror("write");
196           exit(1);
197      }
198      if (i < n) {
199           fprintf(stderr, "%s: write truncated\n", av[1]);
200           exit(1);
201      }
202
203      exit(0);
204
205 notelf:
206      fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
207      exit(1);
208
209 nospace:
210      fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
211              av[1]);
212      exit(1);
213 }
214
215 /*
216  * Local variables:
217  * c-file-style: "k&r"
218  * c-basic-offset: 5
219  * End:
220  */