addRamDisk.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #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. #define ElfHeaderSize (64 * 1024)
  9. #define ElfPages (ElfHeaderSize / 4096)
  10. #define KERNELBASE (0xc000000000000000)
  11. void get4k(FILE *file, char *buf )
  12. {
  13. unsigned j;
  14. unsigned num = fread(buf, 1, 4096, file);
  15. for ( j=num; j<4096; ++j )
  16. buf[j] = 0;
  17. }
  18. void put4k(FILE *file, char *buf )
  19. {
  20. fwrite(buf, 1, 4096, file);
  21. }
  22. void death(const char *msg, FILE *fdesc, const char *fname)
  23. {
  24. fprintf(stderr, msg);
  25. fclose(fdesc);
  26. unlink(fname);
  27. exit(1);
  28. }
  29. int main(int argc, char **argv)
  30. {
  31. char inbuf[4096];
  32. FILE *ramDisk;
  33. FILE *sysmap;
  34. FILE *inputVmlinux;
  35. FILE *outputVmlinux;
  36. char *rd_name, *lx_name, *out_name;
  37. unsigned i;
  38. unsigned long ramFileLen;
  39. unsigned long ramLen;
  40. unsigned long roundR;
  41. unsigned long sysmapFileLen;
  42. unsigned long sysmapLen;
  43. unsigned long sysmapPages;
  44. char *ptr_end;
  45. unsigned long offset_end;
  46. unsigned long kernelLen;
  47. unsigned long actualKernelLen;
  48. unsigned long round;
  49. unsigned long roundedKernelLen;
  50. unsigned long ramStartOffs;
  51. unsigned long ramPages;
  52. unsigned long roundedKernelPages;
  53. unsigned long hvReleaseData;
  54. u_int32_t eyeCatcher = 0xc8a5d9c4;
  55. unsigned long naca;
  56. unsigned long xRamDisk;
  57. unsigned long xRamDiskSize;
  58. long padPages;
  59. if (argc < 2) {
  60. fprintf(stderr, "Name of RAM disk file missing.\n");
  61. exit(1);
  62. }
  63. rd_name = argv[1]
  64. if (argc < 3) {
  65. fprintf(stderr, "Name of System Map input file is missing.\n");
  66. exit(1);
  67. }
  68. if (argc < 4) {
  69. fprintf(stderr, "Name of vmlinux file missing.\n");
  70. exit(1);
  71. }
  72. lx_name = argv[3];
  73. if (argc < 5) {
  74. fprintf(stderr, "Name of vmlinux output file missing.\n");
  75. exit(1);
  76. }
  77. out_name = argv[4];
  78. ramDisk = fopen(rd_name, "r");
  79. if ( ! ramDisk ) {
  80. fprintf(stderr, "RAM disk file \"%s\" failed to open.\n", rd_name);
  81. exit(1);
  82. }
  83. sysmap = fopen(argv[2], "r");
  84. if ( ! sysmap ) {
  85. fprintf(stderr, "System Map file \"%s\" failed to open.\n", argv[2]);
  86. exit(1);
  87. }
  88. inputVmlinux = fopen(lx_name, "r");
  89. if ( ! inputVmlinux ) {
  90. fprintf(stderr, "vmlinux file \"%s\" failed to open.\n", lx_name);
  91. exit(1);
  92. }
  93. outputVmlinux = fopen(out_name, "w+");
  94. if ( ! outputVmlinux ) {
  95. fprintf(stderr, "output vmlinux file \"%s\" failed to open.\n", out_name);
  96. exit(1);
  97. }
  98. /* Input Vmlinux file */
  99. fseek(inputVmlinux, 0, SEEK_END);
  100. kernelLen = ftell(inputVmlinux);
  101. fseek(inputVmlinux, 0, SEEK_SET);
  102. printf("kernel file size = %d\n", kernelLen);
  103. if ( kernelLen == 0 ) {
  104. fprintf(stderr, "You must have a linux kernel specified as argv[3]\n");
  105. exit(1);
  106. }
  107. actualKernelLen = kernelLen - ElfHeaderSize;
  108. printf("actual kernel length (minus ELF header) = %d\n", actualKernelLen);
  109. round = actualKernelLen % 4096;
  110. roundedKernelLen = actualKernelLen;
  111. if ( round )
  112. roundedKernelLen += (4096 - round);
  113. printf("Vmlinux length rounded up to a 4k multiple = %ld/0x%lx \n", roundedKernelLen, roundedKernelLen);
  114. roundedKernelPages = roundedKernelLen / 4096;
  115. printf("Vmlinux pages to copy = %ld/0x%lx \n", roundedKernelPages, roundedKernelPages);
  116. /* Input System Map file */
  117. /* (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) */
  118. fseek(sysmap, 0, SEEK_END);
  119. sysmapFileLen = ftell(sysmap);
  120. fseek(sysmap, 0, SEEK_SET);
  121. printf("%s file size = %ld/0x%lx \n", argv[2], sysmapFileLen, sysmapFileLen);
  122. sysmapLen = sysmapFileLen;
  123. roundR = 4096 - (sysmapLen % 4096);
  124. if (roundR) {
  125. printf("Rounding System Map file up to a multiple of 4096, adding %ld/0x%lx \n", roundR, roundR);
  126. sysmapLen += roundR;
  127. }
  128. printf("Rounded System Map size is %ld/0x%lx \n", sysmapLen, sysmapLen);
  129. /* Process the Sysmap file to determine where _end is */
  130. sysmapPages = sysmapLen / 4096;
  131. /* read the whole file line by line, expect that it doesn't fail */
  132. while ( fgets(inbuf, 4096, sysmap) ) ;
  133. /* search for _end in the last page of the system map */
  134. ptr_end = strstr(inbuf, " _end");
  135. if (!ptr_end) {
  136. fprintf(stderr, "Unable to find _end in the sysmap file \n");
  137. fprintf(stderr, "inbuf: \n");
  138. fprintf(stderr, "%s \n", inbuf);
  139. exit(1);
  140. }
  141. printf("Found _end in the last page of the sysmap - backing up 10 characters it looks like %s", ptr_end-10);
  142. /* convert address of _end in system map to hex offset. */
  143. offset_end = (unsigned int)strtol(ptr_end-10, NULL, 16);
  144. /* calc how many pages we need to insert between the vmlinux and the start of the ram disk */
  145. padPages = offset_end/4096 - roundedKernelPages;
  146. /* Check and see if the vmlinux is already larger than _end in System.map */
  147. if (padPages < 0) {
  148. /* vmlinux is larger than _end - adjust the offset to the start of the embedded ram disk */
  149. offset_end = roundedKernelLen;
  150. printf("vmlinux is larger than _end indicates it needs to be - offset_end = %lx \n", offset_end);
  151. padPages = 0;
  152. printf("will insert %lx pages between the vmlinux and the start of the ram disk \n", padPages);
  153. }
  154. else {
  155. /* _end is larger than vmlinux - use the offset to _end that we calculated from the system map */
  156. printf("vmlinux is smaller than _end indicates is needed - offset_end = %lx \n", offset_end);
  157. printf("will insert %lx pages between the vmlinux and the start of the ram disk \n", padPages);
  158. }
  159. /* Input Ram Disk file */
  160. // Set the offset that the ram disk will be started at.
  161. ramStartOffs = offset_end; /* determined from the input vmlinux file and the system map */
  162. printf("Ram Disk will start at offset = 0x%lx \n", ramStartOffs);
  163. fseek(ramDisk, 0, SEEK_END);
  164. ramFileLen = ftell(ramDisk);
  165. fseek(ramDisk, 0, SEEK_SET);
  166. printf("%s file size = %ld/0x%lx \n", rd_name, ramFileLen, ramFileLen);
  167. ramLen = ramFileLen;
  168. roundR = 4096 - (ramLen % 4096);
  169. if ( roundR ) {
  170. printf("Rounding RAM disk file up to a multiple of 4096, adding %ld/0x%lx \n", roundR, roundR);
  171. ramLen += roundR;
  172. }
  173. printf("Rounded RAM disk size is %ld/0x%lx \n", ramLen, ramLen);
  174. ramPages = ramLen / 4096;
  175. printf("RAM disk pages to copy = %ld/0x%lx\n", ramPages, ramPages);
  176. // Copy 64K ELF header
  177. for (i=0; i<(ElfPages); ++i) {
  178. get4k( inputVmlinux, inbuf );
  179. put4k( outputVmlinux, inbuf );
  180. }
  181. /* Copy the vmlinux (as full pages). */
  182. fseek(inputVmlinux, ElfHeaderSize, SEEK_SET);
  183. for ( i=0; i<roundedKernelPages; ++i ) {
  184. get4k( inputVmlinux, inbuf );
  185. put4k( outputVmlinux, inbuf );
  186. }
  187. /* Insert pad pages (if appropriate) that are needed between */
  188. /* | the end of the vmlinux and the ram disk. */
  189. for (i=0; i<padPages; ++i) {
  190. memset(inbuf, 0, 4096);
  191. put4k(outputVmlinux, inbuf);
  192. }
  193. /* Copy the ram disk (as full pages). */
  194. for ( i=0; i<ramPages; ++i ) {
  195. get4k( ramDisk, inbuf );
  196. put4k( outputVmlinux, inbuf );
  197. }
  198. /* Close the input files */
  199. fclose(ramDisk);
  200. fclose(inputVmlinux);
  201. /* And flush the written output file */
  202. fflush(outputVmlinux);
  203. /* Fixup the new vmlinux to contain the ram disk starting offset (xRamDisk) and the ram disk size (xRamDiskSize) */
  204. /* fseek to the hvReleaseData pointer */
  205. fseek(outputVmlinux, ElfHeaderSize + 0x24, SEEK_SET);
  206. if (fread(&hvReleaseData, 4, 1, outputVmlinux) != 1) {
  207. death("Could not read hvReleaseData pointer\n", outputVmlinux, out_name);
  208. }
  209. hvReleaseData = ntohl(hvReleaseData); /* Convert to native int */
  210. printf("hvReleaseData is at %08x\n", hvReleaseData);
  211. /* fseek to the hvReleaseData */
  212. fseek(outputVmlinux, ElfHeaderSize + hvReleaseData, SEEK_SET);
  213. if (fread(inbuf, 0x40, 1, outputVmlinux) != 1) {
  214. death("Could not read hvReleaseData\n", outputVmlinux, out_name);
  215. }
  216. /* Check hvReleaseData sanity */
  217. if (memcmp(inbuf, &eyeCatcher, 4) != 0) {
  218. death("hvReleaseData is invalid\n", outputVmlinux, out_name);
  219. }
  220. /* Get the naca pointer */
  221. naca = ntohl(*((u_int32_t*) &inbuf[0x0C])) - KERNELBASE;
  222. printf("Naca is at offset 0x%lx \n", naca);
  223. /* fseek to the naca */
  224. fseek(outputVmlinux, ElfHeaderSize + naca, SEEK_SET);
  225. if (fread(inbuf, 0x18, 1, outputVmlinux) != 1) {
  226. death("Could not read naca\n", outputVmlinux, out_name);
  227. }
  228. xRamDisk = ntohl(*((u_int32_t *) &inbuf[0x0c]));
  229. xRamDiskSize = ntohl(*((u_int32_t *) &inbuf[0x14]));
  230. /* Make sure a RAM disk isn't already present */
  231. if ((xRamDisk != 0) || (xRamDiskSize != 0)) {
  232. death("RAM disk is already attached to this kernel\n", outputVmlinux, out_name);
  233. }
  234. /* Fill in the values */
  235. *((u_int32_t *) &inbuf[0x0c]) = htonl(ramStartOffs);
  236. *((u_int32_t *) &inbuf[0x14]) = htonl(ramPages);
  237. /* Write out the new naca */
  238. fflush(outputVmlinux);
  239. fseek(outputVmlinux, ElfHeaderSize + naca, SEEK_SET);
  240. if (fwrite(inbuf, 0x18, 1, outputVmlinux) != 1) {
  241. death("Could not write naca\n", outputVmlinux, out_name);
  242. }
  243. printf("Ram Disk of 0x%lx pages is attached to the kernel at offset 0x%08x\n",
  244. ramPages, ramStartOffs);
  245. /* Done */
  246. fclose(outputVmlinux);
  247. /* Set permission to executable */
  248. chmod(out_name, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
  249. return 0;
  250. }