8 void xlate( char * inb, char * trb, unsigned len )
11 for ( i=0; i<len; ++i )
30 #define ElfHeaderSize (64 * 1024)
31 #define ElfPages (ElfHeaderSize / 4096)
33 void get4k( /*istream *inf*/FILE *file, char *buf )
36 unsigned num = fread(buf, 1, 4096, file);
37 for ( j=num; j<4096; ++j )
41 void put4k( /*ostream *outf*/FILE *file, char *buf )
43 fwrite(buf, 1, 4096, file);
46 int main(int argc, char **argv)
51 FILE *inputVmlinux = NULL;
52 FILE *outputVmlinux = NULL;
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;
67 fprintf(stderr, "Name of System Map file missing.\n");
73 fprintf(stderr, "Name of vmlinux file missing.\n");
79 fprintf(stderr, "Name of vmlinux output file missing.\n");
83 sysmap = fopen(argv[1], "r");
86 fprintf(stderr, "System Map file \"%s\" failed to open.\n", argv[1]);
89 inputVmlinux = fopen(argv[2], "r");
92 fprintf(stderr, "vmlinux file \"%s\" failed to open.\n", argv[2]);
95 outputVmlinux = fopen(argv[3], "w");
96 if ( ! outputVmlinux )
98 fprintf(stderr, "output vmlinux file \"%s\" failed to open.\n", argv[3]);
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 )
110 fprintf(stderr, "You must have a linux kernel specified as argv[2]\n");
115 actualKernelLen = kernelLen - ElfHeaderSize;
117 printf("actual kernel length (minus ELF header) = %ld/%lxx \n", actualKernelLen, actualKernelLen);
119 round = actualKernelLen % 4096;
120 roundedKernelLen = actualKernelLen;
122 roundedKernelLen += (4096 - round);
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);
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);
136 sysmapLen = sysmapFileLen;
138 roundR = 4096 - (sysmapLen % 4096);
141 printf("Rounding System Map file up to a multiple of 4096, adding %ld\n", roundR);
144 printf("Rounded System Map size is %ld\n", sysmapLen);
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");
155 fprintf(stderr, "Unable to find _end in the sysmap file \n");
156 fprintf(stderr, "inbuf: \n");
157 fprintf(stderr, "%s \n", inbuf);
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;
165 /* Check and see if the vmlinux is larger than _end in System.map */
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);
171 printf("will insert %lx pages between the vmlinux and the start of the sysmap \n", padPages);
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);
182 /* Copy 64K ELF header */
183 for (i=0; i<(ElfPages); ++i)
185 get4k( inputVmlinux, inbuf );
186 put4k( outputVmlinux, inbuf );
190 /* Copy the vmlinux (as full pages). */
191 fseek(inputVmlinux, ElfHeaderSize, SEEK_SET);
192 for ( i=0; i<roundedKernelPages; ++i )
194 get4k( inputVmlinux, inbuf );
196 /* Set the offsets (of the start and end) of the embedded sysmap so it is set in the vmlinux.sm */
200 printf("Storing embedded_sysmap_start at 0x3c\n");
201 p = (unsigned long *)(inbuf + 0x3c);
203 #if (BYTE_ORDER == __BIG_ENDIAN)
204 *p = sysmapStartOffs;
206 *p = bswap_32(sysmapStartOffs);
209 printf("Storing embedded_sysmap_end at 0x44\n");
210 p = (unsigned long *)(inbuf + 0x44);
212 #if (BYTE_ORDER == __BIG_ENDIAN)
213 *p = sysmapStartOffs + sysmapFileLen;
215 *p = bswap_32(sysmapStartOffs + sysmapFileLen);
219 put4k( outputVmlinux, inbuf );
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)
226 memset(inbuf, 0, 4096);
227 put4k(outputVmlinux, inbuf);
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 )
235 get4k( sysmap, inbuf );
236 put4k( outputVmlinux, inbuf );
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);