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