addRamDisk.c 8.8 KB

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