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