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