blob: c10746a187b636c6df725bc571b2cb916f70ff22 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <netinet/in.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <string.h>
8
9#define ElfHeaderSize (64 * 1024)
10#define ElfPages (ElfHeaderSize / 4096)
11#define KERNELBASE (0xc000000000000000)
12
13void get4k(FILE *file, char *buf )
14{
15 unsigned j;
16 unsigned num = fread(buf, 1, 4096, file);
17 for ( j=num; j<4096; ++j )
18 buf[j] = 0;
19}
20
21void put4k(FILE *file, char *buf )
22{
23 fwrite(buf, 1, 4096, file);
24}
25
26void death(const char *msg, FILE *fdesc, const char *fname)
27{
28 fprintf(stderr, msg);
29 fclose(fdesc);
30 unlink(fname);
31 exit(1);
32}
33
34int main(int argc, char **argv)
35{
36 char inbuf[4096];
Olaf Heringeba2fb22005-11-09 20:51:03 +010037 FILE *ramDisk;
38 FILE *sysmap;
39 FILE *inputVmlinux;
40 FILE *outputVmlinux;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Olaf Hering58638bf2005-11-09 20:52:20 +010042 char *rd_name, *lx_name, *out_name;
Olaf Heringeba2fb22005-11-09 20:51:03 +010043 unsigned i;
44 unsigned long ramFileLen;
45 unsigned long ramLen;
46 unsigned long roundR;
47
48 unsigned long sysmapFileLen;
49 unsigned long sysmapLen;
50 unsigned long sysmapPages;
51 char *ptr_end;
52 unsigned long offset_end;
53
54 unsigned long kernelLen;
55 unsigned long actualKernelLen;
56 unsigned long round;
57 unsigned long roundedKernelLen;
58 unsigned long ramStartOffs;
59 unsigned long ramPages;
60 unsigned long roundedKernelPages;
61 unsigned long hvReleaseData;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 u_int32_t eyeCatcher = 0xc8a5d9c4;
Olaf Heringeba2fb22005-11-09 20:51:03 +010063 unsigned long naca;
64 unsigned long xRamDisk;
65 unsigned long xRamDiskSize;
66 long padPages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68
69 if (argc < 2) {
70 fprintf(stderr, "Name of RAM disk file missing.\n");
71 exit(1);
72 }
Olaf Hering58638bf2005-11-09 20:52:20 +010073 rd_name = argv[1]
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (argc < 3) {
76 fprintf(stderr, "Name of System Map input file is missing.\n");
77 exit(1);
78 }
79
80 if (argc < 4) {
81 fprintf(stderr, "Name of vmlinux file missing.\n");
82 exit(1);
83 }
Olaf Hering58638bf2005-11-09 20:52:20 +010084 lx_name = argv[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 if (argc < 5) {
87 fprintf(stderr, "Name of vmlinux output file missing.\n");
88 exit(1);
89 }
Olaf Hering58638bf2005-11-09 20:52:20 +010090 out_name = argv[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92
Olaf Hering58638bf2005-11-09 20:52:20 +010093 ramDisk = fopen(rd_name, "r");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if ( ! ramDisk ) {
Olaf Hering58638bf2005-11-09 20:52:20 +010095 fprintf(stderr, "RAM disk file \"%s\" failed to open.\n", rd_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 exit(1);
97 }
98
99 sysmap = fopen(argv[2], "r");
100 if ( ! sysmap ) {
101 fprintf(stderr, "System Map file \"%s\" failed to open.\n", argv[2]);
102 exit(1);
103 }
104
Olaf Hering58638bf2005-11-09 20:52:20 +0100105 inputVmlinux = fopen(lx_name, "r");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if ( ! inputVmlinux ) {
Olaf Hering58638bf2005-11-09 20:52:20 +0100107 fprintf(stderr, "vmlinux file \"%s\" failed to open.\n", lx_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 exit(1);
109 }
110
Olaf Hering58638bf2005-11-09 20:52:20 +0100111 outputVmlinux = fopen(out_name, "w+");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if ( ! outputVmlinux ) {
Olaf Hering58638bf2005-11-09 20:52:20 +0100113 fprintf(stderr, "output vmlinux file \"%s\" failed to open.\n", out_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 exit(1);
115 }
116
117
118
119 /* Input Vmlinux file */
120 fseek(inputVmlinux, 0, SEEK_END);
121 kernelLen = ftell(inputVmlinux);
122 fseek(inputVmlinux, 0, SEEK_SET);
123 printf("kernel file size = %d\n", kernelLen);
124 if ( kernelLen == 0 ) {
125 fprintf(stderr, "You must have a linux kernel specified as argv[3]\n");
126 exit(1);
127 }
128
129 actualKernelLen = kernelLen - ElfHeaderSize;
130
131 printf("actual kernel length (minus ELF header) = %d\n", actualKernelLen);
132
133 round = actualKernelLen % 4096;
134 roundedKernelLen = actualKernelLen;
135 if ( round )
136 roundedKernelLen += (4096 - round);
137 printf("Vmlinux length rounded up to a 4k multiple = %ld/0x%lx \n", roundedKernelLen, roundedKernelLen);
138 roundedKernelPages = roundedKernelLen / 4096;
139 printf("Vmlinux pages to copy = %ld/0x%lx \n", roundedKernelPages, roundedKernelPages);
140
141
142
143 /* Input System Map file */
144 /* (needs to be processed simply to determine if we need to add pad pages due to the static variables not being included in the vmlinux) */
145 fseek(sysmap, 0, SEEK_END);
146 sysmapFileLen = ftell(sysmap);
147 fseek(sysmap, 0, SEEK_SET);
148 printf("%s file size = %ld/0x%lx \n", argv[2], sysmapFileLen, sysmapFileLen);
149
150 sysmapLen = sysmapFileLen;
151
152 roundR = 4096 - (sysmapLen % 4096);
153 if (roundR) {
154 printf("Rounding System Map file up to a multiple of 4096, adding %ld/0x%lx \n", roundR, roundR);
155 sysmapLen += roundR;
156 }
157 printf("Rounded System Map size is %ld/0x%lx \n", sysmapLen, sysmapLen);
158
159 /* Process the Sysmap file to determine where _end is */
160 sysmapPages = sysmapLen / 4096;
161 /* read the whole file line by line, expect that it doesn't fail */
162 while ( fgets(inbuf, 4096, sysmap) ) ;
163 /* search for _end in the last page of the system map */
164 ptr_end = strstr(inbuf, " _end");
165 if (!ptr_end) {
166 fprintf(stderr, "Unable to find _end in the sysmap file \n");
167 fprintf(stderr, "inbuf: \n");
168 fprintf(stderr, "%s \n", inbuf);
169 exit(1);
170 }
171 printf("Found _end in the last page of the sysmap - backing up 10 characters it looks like %s", ptr_end-10);
172 /* convert address of _end in system map to hex offset. */
173 offset_end = (unsigned int)strtol(ptr_end-10, NULL, 16);
174 /* calc how many pages we need to insert between the vmlinux and the start of the ram disk */
175 padPages = offset_end/4096 - roundedKernelPages;
176
177 /* Check and see if the vmlinux is already larger than _end in System.map */
178 if (padPages < 0) {
179 /* vmlinux is larger than _end - adjust the offset to the start of the embedded ram disk */
180 offset_end = roundedKernelLen;
181 printf("vmlinux is larger than _end indicates it needs to be - offset_end = %lx \n", offset_end);
182 padPages = 0;
183 printf("will insert %lx pages between the vmlinux and the start of the ram disk \n", padPages);
184 }
185 else {
186 /* _end is larger than vmlinux - use the offset to _end that we calculated from the system map */
187 printf("vmlinux is smaller than _end indicates is needed - offset_end = %lx \n", offset_end);
188 printf("will insert %lx pages between the vmlinux and the start of the ram disk \n", padPages);
189 }
190
191
192
193 /* Input Ram Disk file */
194 // Set the offset that the ram disk will be started at.
195 ramStartOffs = offset_end; /* determined from the input vmlinux file and the system map */
196 printf("Ram Disk will start at offset = 0x%lx \n", ramStartOffs);
197
198 fseek(ramDisk, 0, SEEK_END);
199 ramFileLen = ftell(ramDisk);
200 fseek(ramDisk, 0, SEEK_SET);
Olaf Hering58638bf2005-11-09 20:52:20 +0100201 printf("%s file size = %ld/0x%lx \n", rd_name, ramFileLen, ramFileLen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 ramLen = ramFileLen;
204
205 roundR = 4096 - (ramLen % 4096);
206 if ( roundR ) {
207 printf("Rounding RAM disk file up to a multiple of 4096, adding %ld/0x%lx \n", roundR, roundR);
208 ramLen += roundR;
209 }
210
211 printf("Rounded RAM disk size is %ld/0x%lx \n", ramLen, ramLen);
212 ramPages = ramLen / 4096;
213 printf("RAM disk pages to copy = %ld/0x%lx\n", ramPages, ramPages);
214
215
216
217 // Copy 64K ELF header
218 for (i=0; i<(ElfPages); ++i) {
219 get4k( inputVmlinux, inbuf );
220 put4k( outputVmlinux, inbuf );
221 }
222
223 /* Copy the vmlinux (as full pages). */
224 fseek(inputVmlinux, ElfHeaderSize, SEEK_SET);
225 for ( i=0; i<roundedKernelPages; ++i ) {
226 get4k( inputVmlinux, inbuf );
227 put4k( outputVmlinux, inbuf );
228 }
229
230 /* Insert pad pages (if appropriate) that are needed between */
231 /* | the end of the vmlinux and the ram disk. */
232 for (i=0; i<padPages; ++i) {
233 memset(inbuf, 0, 4096);
234 put4k(outputVmlinux, inbuf);
235 }
236
237 /* Copy the ram disk (as full pages). */
238 for ( i=0; i<ramPages; ++i ) {
239 get4k( ramDisk, inbuf );
240 put4k( outputVmlinux, inbuf );
241 }
242
243 /* Close the input files */
244 fclose(ramDisk);
245 fclose(inputVmlinux);
246 /* And flush the written output file */
247 fflush(outputVmlinux);
248
249
250
251 /* Fixup the new vmlinux to contain the ram disk starting offset (xRamDisk) and the ram disk size (xRamDiskSize) */
252 /* fseek to the hvReleaseData pointer */
253 fseek(outputVmlinux, ElfHeaderSize + 0x24, SEEK_SET);
254 if (fread(&hvReleaseData, 4, 1, outputVmlinux) != 1) {
Olaf Hering58638bf2005-11-09 20:52:20 +0100255 death("Could not read hvReleaseData pointer\n", outputVmlinux, out_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 hvReleaseData = ntohl(hvReleaseData); /* Convert to native int */
258 printf("hvReleaseData is at %08x\n", hvReleaseData);
259
260 /* fseek to the hvReleaseData */
261 fseek(outputVmlinux, ElfHeaderSize + hvReleaseData, SEEK_SET);
262 if (fread(inbuf, 0x40, 1, outputVmlinux) != 1) {
Olaf Hering58638bf2005-11-09 20:52:20 +0100263 death("Could not read hvReleaseData\n", outputVmlinux, out_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 /* Check hvReleaseData sanity */
266 if (memcmp(inbuf, &eyeCatcher, 4) != 0) {
Olaf Hering58638bf2005-11-09 20:52:20 +0100267 death("hvReleaseData is invalid\n", outputVmlinux, out_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 /* Get the naca pointer */
270 naca = ntohl(*((u_int32_t*) &inbuf[0x0C])) - KERNELBASE;
271 printf("Naca is at offset 0x%lx \n", naca);
272
273 /* fseek to the naca */
274 fseek(outputVmlinux, ElfHeaderSize + naca, SEEK_SET);
275 if (fread(inbuf, 0x18, 1, outputVmlinux) != 1) {
Olaf Hering58638bf2005-11-09 20:52:20 +0100276 death("Could not read naca\n", outputVmlinux, out_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 xRamDisk = ntohl(*((u_int32_t *) &inbuf[0x0c]));
279 xRamDiskSize = ntohl(*((u_int32_t *) &inbuf[0x14]));
280 /* Make sure a RAM disk isn't already present */
281 if ((xRamDisk != 0) || (xRamDiskSize != 0)) {
Olaf Hering58638bf2005-11-09 20:52:20 +0100282 death("RAM disk is already attached to this kernel\n", outputVmlinux, out_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 /* Fill in the values */
285 *((u_int32_t *) &inbuf[0x0c]) = htonl(ramStartOffs);
286 *((u_int32_t *) &inbuf[0x14]) = htonl(ramPages);
287
288 /* Write out the new naca */
289 fflush(outputVmlinux);
290 fseek(outputVmlinux, ElfHeaderSize + naca, SEEK_SET);
291 if (fwrite(inbuf, 0x18, 1, outputVmlinux) != 1) {
Olaf Hering58638bf2005-11-09 20:52:20 +0100292 death("Could not write naca\n", outputVmlinux, out_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 printf("Ram Disk of 0x%lx pages is attached to the kernel at offset 0x%08x\n",
295 ramPages, ramStartOffs);
296
297 /* Done */
298 fclose(outputVmlinux);
299 /* Set permission to executable */
Olaf Hering58638bf2005-11-09 20:52:20 +0100300 chmod(out_name, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 return 0;
303}
304