]> git.ozlabs.org Git - yaboot.git/blob - util/addnote.c
Update first stage to be compatible with new Macs
[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 char arch[] = "PowerPC";
32
33 #define N_DESCR 6
34 unsigned int descr[N_DESCR] = {
35      0xffffffff,                /* real-mode = true */
36      0x00c00000,                /* real-base, i.e. where we expect OF to be */
37      0xffffffff,                /* real-size */
38      0xffffffff,                /* virt-base */
39      0xffffffff,                /* virt-size */
40      0x4000,                    /* load-base */
41 };
42
43 unsigned char buf[512];
44
45 #define GET_16BE(off)   ((buf[off] << 8) + (buf[(off)+1]))
46 #define GET_32BE(off)   ((GET_16BE(off) << 16) + GET_16BE((off)+2))
47 #define PUT_16BE(off, v)        (buf[off] = ((v) >> 8) & 0xff, \
48                          buf[(off) + 1] = (v) & 0xff)
49 #define PUT_32BE(off, v)        (PUT_16BE((off), (v) >> 16), \
50                                  PUT_16BE((off) + 2, (v)))
51
52 /* Structure of an ELF file */
53 #define E_IDENT         0       /* ELF header */
54 #define E_PHOFF         28
55 #define E_PHENTSIZE     42
56 #define E_PHNUM         44
57 #define E_HSIZE         52      /* size of ELF header */
58
59 #define EI_MAGIC        0       /* offsets in E_IDENT area */
60 #define EI_CLASS        4
61 #define EI_DATA         5
62
63 #define PH_TYPE         0       /* ELF program header */
64 #define PH_OFFSET       4
65 #define PH_FILESZ       16
66 #define PH_HSIZE        32      /* size of program header */
67
68 #define PT_NOTE         4       /* Program header type = note */
69
70 #define ELFCLASS32      1
71 #define ELFDATA2MSB     2
72
73 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
74
75 int
76 main(int ac, char **av)
77 {
78      int fd, n, i;
79      int ph, ps, np;
80      int nnote, ns;
81
82      if (ac != 2) {
83           fprintf(stderr, "Usage: %s elf-file\n", av[0]);
84           exit(1);
85      }
86      fd = open(av[1], O_RDWR);
87      if (fd < 0) {
88           perror(av[1]);
89           exit(1);
90      }
91
92      nnote = strlen(arch) + 1 + (N_DESCR + 3) * 4;
93
94      n = read(fd, buf, sizeof(buf));
95      if (n < 0) {
96           perror("read");
97           exit(1);
98      }
99
100      if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
101           goto notelf;
102
103      if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
104          || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
105           fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
106                   av[1]);
107           exit(1);
108      }
109
110      ph = GET_32BE(E_PHOFF);
111      ps = GET_16BE(E_PHENTSIZE);
112      np = GET_16BE(E_PHNUM);
113      if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
114           goto notelf;
115      if (ph + (np + 1) * ps + nnote > n)
116           goto nospace;
117
118      for (i = 0; i < np; ++i) {
119           if (GET_32BE(ph + PH_TYPE) == PT_NOTE) {
120                fprintf(stderr, "%s already has a note entry\n",
121                        av[1]);
122                exit(0);
123           }
124           ph += ps;
125      }
126
127      /* XXX check that the area we want to use is all zeroes */
128      for (i = 0; i < ps + nnote; ++i)
129           if (buf[ph + i] != 0)
130                goto nospace;
131
132      /* fill in the program header entry */
133      ns = ph + ps;
134      PUT_32BE(ph + PH_TYPE, PT_NOTE);
135      PUT_32BE(ph + PH_OFFSET, ns);
136      PUT_32BE(ph + PH_FILESZ, nnote);
137
138      /* fill in the note area we point to */
139      /* XXX we should probably make this a proper section */
140      PUT_32BE(ns, strlen(arch) + 1);
141      PUT_32BE(ns + 4, N_DESCR * 4);
142      PUT_32BE(ns + 8, 0x1275);
143      strcpy(&buf[ns + 12], arch);
144      ns += 12 + strlen(arch) + 1;
145      for (i = 0; i < N_DESCR; ++i)
146           PUT_32BE(ns + i * 4, descr[i]);
147
148      /* Update the number of program headers */
149      PUT_16BE(E_PHNUM, np + 1);
150
151      /* write back */
152      lseek(fd, (long) 0, SEEK_SET);
153      i = write(fd, buf, n);
154      if (i < 0) {
155           perror("write");
156           exit(1);
157      }
158      if (i < n) {
159           fprintf(stderr, "%s: write truncated\n", av[1]);
160           exit(1);
161      }
162
163      exit(0);
164
165 notelf:
166      fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
167      exit(1);
168
169 nospace:
170      fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
171              av[1]);
172      exit(1);
173 }
174
175 /* 
176  * Local variables:
177  * c-file-style: "k&r"
178  * c-basic-offset: 5
179  * End:
180  */