ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / boot / addSystemMap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <byteswap.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <string.h>
7
8 void xlate( char * inb, char * trb, unsigned len )
9 {
10         unsigned i;
11         for (  i=0; i<len; ++i )
12         {
13                 char c = *inb++;
14                 char c1 = c >> 4;
15                 char c2 = c & 0xf;
16                 if ( c1 > 9 )
17                         c1 = c1 + 'A' - 10;
18                 else
19                         c1 = c1 + '0';
20                 if ( c2 > 9 )
21                         c2 = c2 + 'A' - 10;
22                 else
23                         c2 = c2 + '0';
24                 *trb++ = c1;
25                 *trb++ = c2;
26         }
27         *trb = 0;
28 }
29
30 #define ElfHeaderSize  (64 * 1024)
31 #define ElfPages  (ElfHeaderSize / 4096)
32
33 void get4k( /*istream *inf*/FILE *file, char *buf )
34 {
35         unsigned j;
36         unsigned num = fread(buf, 1, 4096, file);
37         for ( j=num; j<4096; ++j )
38                 buf[j] = 0;
39 }
40
41 void put4k( /*ostream *outf*/FILE *file, char *buf )
42 {
43         fwrite(buf, 1, 4096, file);
44 }
45
46 int main(int argc, char **argv)
47 {
48         char inbuf[4096];
49         FILE *sysmap = NULL;
50         char* ptr_end = NULL; 
51         FILE *inputVmlinux = NULL;
52         FILE *outputVmlinux = NULL;
53         long i = 0;
54         unsigned long sysmapFileLen = 0;
55         unsigned long sysmapLen = 0;
56         unsigned long roundR = 0;
57         unsigned long kernelLen = 0;
58         unsigned long actualKernelLen = 0;
59         unsigned long round = 0;
60         unsigned long roundedKernelLen = 0;
61         unsigned long sysmapStartOffs = 0;
62         unsigned long sysmapPages = 0;
63         unsigned long roundedKernelPages = 0;
64         long padPages = 0;
65         if ( argc < 2 )
66         {
67                 fprintf(stderr, "Name of System Map file missing.\n");
68                 exit(1);
69         }
70
71         if ( argc < 3 )
72         {
73                 fprintf(stderr, "Name of vmlinux file missing.\n");
74                 exit(1);
75         }
76
77         if ( argc < 4 )
78         {
79                 fprintf(stderr, "Name of vmlinux output file missing.\n");
80                 exit(1);
81         }
82
83         sysmap = fopen(argv[1], "r");
84         if ( ! sysmap )
85         {
86                 fprintf(stderr, "System Map file \"%s\" failed to open.\n", argv[1]);
87                 exit(1);
88         }
89         inputVmlinux = fopen(argv[2], "r");
90         if ( ! inputVmlinux )
91         {
92                 fprintf(stderr, "vmlinux file \"%s\" failed to open.\n", argv[2]);
93                 exit(1);
94         }
95         outputVmlinux = fopen(argv[3], "w");
96         if ( ! outputVmlinux )
97         {
98                 fprintf(stderr, "output vmlinux file \"%s\" failed to open.\n", argv[3]);
99                 exit(1);
100         }
101
102
103   
104         fseek(inputVmlinux, 0, SEEK_END);
105         kernelLen = ftell(inputVmlinux);
106         fseek(inputVmlinux, 0, SEEK_SET);
107         printf("kernel file size = %ld\n", kernelLen);
108         if ( kernelLen == 0 )
109         {
110                 fprintf(stderr, "You must have a linux kernel specified as argv[2]\n");
111                 exit(1);
112         }
113
114
115         actualKernelLen = kernelLen - ElfHeaderSize;
116
117         printf("actual kernel length (minus ELF header) = %ld/%lxx \n", actualKernelLen, actualKernelLen);
118
119         round = actualKernelLen % 4096;
120         roundedKernelLen = actualKernelLen;
121         if ( round )
122                 roundedKernelLen += (4096 - round);
123
124         printf("Kernel length rounded up to a 4k multiple = %ld/%lxx \n", roundedKernelLen, roundedKernelLen);
125         roundedKernelPages = roundedKernelLen / 4096;
126         printf("Kernel pages to copy = %ld/%lxx\n", roundedKernelPages, roundedKernelPages);
127
128
129
130         /* Sysmap file */
131         fseek(sysmap, 0, SEEK_END);
132         sysmapFileLen = ftell(sysmap);
133         fseek(sysmap, 0, SEEK_SET);
134         printf("%s file size = %ld\n", argv[1], sysmapFileLen);
135
136         sysmapLen = sysmapFileLen;
137
138         roundR = 4096 - (sysmapLen % 4096);
139         if (roundR)
140         {
141                 printf("Rounding System Map file up to a multiple of 4096, adding %ld\n", roundR);
142                 sysmapLen += roundR;
143         }
144         printf("Rounded System Map size is %ld\n", sysmapLen);
145   
146   /* Process the Sysmap file to determine the true end of the kernel */
147         sysmapPages = sysmapLen / 4096;
148         printf("System map pages to copy = %ld\n", sysmapPages);
149         /* read the whole file line by line, expect that it doesn't fail */
150         while ( fgets(inbuf, 4096, sysmap) )  ;
151         /* search for _end in the last page of the system map */
152         ptr_end = strstr(inbuf, " _end");
153         if (!ptr_end)
154         {
155                 fprintf(stderr, "Unable to find _end in the sysmap file \n");
156                 fprintf(stderr, "inbuf: \n");
157                 fprintf(stderr, "%s \n", inbuf);
158                 exit(1);
159         }
160         printf("Found _end in the last page of the sysmap - backing up 10 characters it looks like %s", ptr_end-10);
161         sysmapStartOffs = (unsigned int)strtol(ptr_end-10, NULL, 16);
162         /* calc how many pages we need to insert between the vmlinux and the start of the sysmap */
163         padPages = sysmapStartOffs/4096 - roundedKernelPages;
164
165         /* Check and see if the vmlinux is larger than _end in System.map */
166         if (padPages < 0)
167         { /* vmlinux is larger than _end - adjust the offset to start the embedded system map */ 
168                 sysmapStartOffs = roundedKernelLen;
169                 printf("vmlinux is larger than _end indicates it needs to be - sysmapStartOffs = %lx \n", sysmapStartOffs);
170                 padPages = 0;
171                 printf("will insert %lx pages between the vmlinux and the start of the sysmap \n", padPages);
172         }
173         else
174         { /* _end is larger than vmlinux - use the sysmapStartOffs we calculated from the system map */
175                 printf("vmlinux is smaller than _end indicates is needed - sysmapStartOffs = %lx \n", sysmapStartOffs);
176                 printf("will insert %lx pages between the vmlinux and the start of the sysmap \n", padPages);
177         }
178
179
180
181
182         /* Copy 64K ELF header */
183         for (i=0; i<(ElfPages); ++i)
184         {
185                 get4k( inputVmlinux, inbuf );
186                 put4k( outputVmlinux, inbuf );
187         }
188
189   
190         /* Copy the vmlinux (as full pages). */
191         fseek(inputVmlinux, ElfHeaderSize, SEEK_SET);
192         for ( i=0; i<roundedKernelPages; ++i )
193         {
194                 get4k( inputVmlinux, inbuf );
195     
196                 /* Set the offsets (of the start and end) of the embedded sysmap so it is set in the vmlinux.sm */
197                 if ( i == 0 )
198                 {
199                         unsigned long * p;
200                         printf("Storing embedded_sysmap_start at 0x3c\n");
201                         p = (unsigned long *)(inbuf + 0x3c);
202
203 #if (BYTE_ORDER == __BIG_ENDIAN)
204                         *p = sysmapStartOffs;
205 #else
206                         *p = bswap_32(sysmapStartOffs);
207 #endif
208
209                         printf("Storing embedded_sysmap_end at 0x44\n");
210                         p = (unsigned long *)(inbuf + 0x44);
211
212 #if (BYTE_ORDER == __BIG_ENDIAN)
213                         *p = sysmapStartOffs + sysmapFileLen;
214 #else
215                         *p = bswap_32(sysmapStartOffs + sysmapFileLen);
216 #endif
217                 }
218     
219                 put4k( outputVmlinux, inbuf );
220         }
221   
222   
223         /* Insert any pad pages between the end of the vmlinux and where the system map needs to be. */
224         for (i=0; i<padPages; ++i)
225         {
226                 memset(inbuf, 0, 4096);
227                 put4k(outputVmlinux, inbuf);
228         }
229
230
231         /* Copy the system map (as full pages). */
232         fseek(sysmap, 0, SEEK_SET);  /* start reading from begining of the system map */
233         for ( i=0; i<sysmapPages; ++i )
234         {
235                 get4k( sysmap, inbuf );
236                 put4k( outputVmlinux, inbuf );
237         }
238
239
240         fclose(sysmap);
241         fclose(inputVmlinux);
242         fclose(outputVmlinux);
243         /* Set permission to executable */
244         chmod(argv[3], S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
245
246         return 0;
247 }
248