addRamDisk.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 (0xc0000000)
  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. printf(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 *inputVmlinux = NULL;
  34. FILE *outputVmlinux = NULL;
  35. unsigned i = 0;
  36. u_int32_t ramFileLen = 0;
  37. u_int32_t ramLen = 0;
  38. u_int32_t roundR = 0;
  39. u_int32_t kernelLen = 0;
  40. u_int32_t actualKernelLen = 0;
  41. u_int32_t round = 0;
  42. u_int32_t roundedKernelLen = 0;
  43. u_int32_t ramStartOffs = 0;
  44. u_int32_t ramPages = 0;
  45. u_int32_t roundedKernelPages = 0;
  46. u_int32_t hvReleaseData = 0;
  47. u_int32_t eyeCatcher = 0xc8a5d9c4;
  48. u_int32_t naca = 0;
  49. u_int32_t xRamDisk = 0;
  50. u_int32_t xRamDiskSize = 0;
  51. if ( argc < 2 ) {
  52. printf("Name of RAM disk file missing.\n");
  53. exit(1);
  54. }
  55. if ( argc < 3 ) {
  56. printf("Name of vmlinux file missing.\n");
  57. exit(1);
  58. }
  59. if ( argc < 4 ) {
  60. printf("Name of vmlinux output file missing.\n");
  61. exit(1);
  62. }
  63. ramDisk = fopen(argv[1], "r");
  64. if ( ! ramDisk ) {
  65. printf("RAM disk file \"%s\" failed to open.\n", argv[1]);
  66. exit(1);
  67. }
  68. inputVmlinux = fopen(argv[2], "r");
  69. if ( ! inputVmlinux ) {
  70. printf("vmlinux file \"%s\" failed to open.\n", argv[2]);
  71. exit(1);
  72. }
  73. outputVmlinux = fopen(argv[3], "w+");
  74. if ( ! outputVmlinux ) {
  75. printf("output vmlinux file \"%s\" failed to open.\n", argv[3]);
  76. exit(1);
  77. }
  78. fseek(ramDisk, 0, SEEK_END);
  79. ramFileLen = ftell(ramDisk);
  80. fseek(ramDisk, 0, SEEK_SET);
  81. printf("%s file size = %d\n", argv[1], ramFileLen);
  82. ramLen = ramFileLen;
  83. roundR = 4096 - (ramLen % 4096);
  84. if ( roundR ) {
  85. printf("Rounding RAM disk file up to a multiple of 4096, adding %d\n", roundR);
  86. ramLen += roundR;
  87. }
  88. printf("Rounded RAM disk size is %d\n", ramLen);
  89. fseek(inputVmlinux, 0, SEEK_END);
  90. kernelLen = ftell(inputVmlinux);
  91. fseek(inputVmlinux, 0, SEEK_SET);
  92. printf("kernel file size = %d\n", kernelLen);
  93. if ( kernelLen == 0 ) {
  94. printf("You must have a linux kernel specified as argv[2]\n");
  95. exit(1);
  96. }
  97. actualKernelLen = kernelLen - ElfHeaderSize;
  98. printf("actual kernel length (minus ELF header) = %d\n", actualKernelLen);
  99. round = actualKernelLen % 4096;
  100. roundedKernelLen = actualKernelLen;
  101. if ( round )
  102. roundedKernelLen += (4096 - round);
  103. printf("actual kernel length rounded up to a 4k multiple = %d\n", roundedKernelLen);
  104. ramStartOffs = roundedKernelLen;
  105. ramPages = ramLen / 4096;
  106. printf("RAM disk pages to copy = %d\n", ramPages);
  107. // Copy 64K ELF header
  108. for (i=0; i<(ElfPages); ++i) {
  109. get4k( inputVmlinux, inbuf );
  110. put4k( outputVmlinux, inbuf );
  111. }
  112. roundedKernelPages = roundedKernelLen / 4096;
  113. fseek(inputVmlinux, ElfHeaderSize, SEEK_SET);
  114. for ( i=0; i<roundedKernelPages; ++i ) {
  115. get4k( inputVmlinux, inbuf );
  116. put4k( outputVmlinux, inbuf );
  117. }
  118. for ( i=0; i<ramPages; ++i ) {
  119. get4k( ramDisk, inbuf );
  120. put4k( outputVmlinux, inbuf );
  121. }
  122. /* Close the input files */
  123. fclose(ramDisk);
  124. fclose(inputVmlinux);
  125. /* And flush the written output file */
  126. fflush(outputVmlinux);
  127. /* fseek to the hvReleaseData pointer */
  128. fseek(outputVmlinux, ElfHeaderSize + 0x24, SEEK_SET);
  129. if (fread(&hvReleaseData, 4, 1, outputVmlinux) != 1) {
  130. death("Could not read hvReleaseData pointer\n", outputVmlinux, argv[3]);
  131. }
  132. hvReleaseData = ntohl(hvReleaseData); /* Convert to native int */
  133. printf("hvReleaseData is at %08x\n", hvReleaseData);
  134. /* fseek to the hvReleaseData */
  135. fseek(outputVmlinux, ElfHeaderSize + hvReleaseData, SEEK_SET);
  136. if (fread(inbuf, 0x40, 1, outputVmlinux) != 1) {
  137. death("Could not read hvReleaseData\n", outputVmlinux, argv[3]);
  138. }
  139. /* Check hvReleaseData sanity */
  140. if (memcmp(inbuf, &eyeCatcher, 4) != 0) {
  141. death("hvReleaseData is invalid\n", outputVmlinux, argv[3]);
  142. }
  143. /* Get the naca pointer */
  144. naca = ntohl(*((u_int32_t *) &inbuf[0x0c])) - KERNELBASE;
  145. printf("naca is at %08x\n", naca);
  146. /* fseek to the naca */
  147. fseek(outputVmlinux, ElfHeaderSize + naca, SEEK_SET);
  148. if (fread(inbuf, 0x18, 1, outputVmlinux) != 1) {
  149. death("Could not read naca\n", outputVmlinux, argv[3]);
  150. }
  151. xRamDisk = ntohl(*((u_int32_t *) &inbuf[0x0c]));
  152. xRamDiskSize = ntohl(*((u_int32_t *) &inbuf[0x14]));
  153. /* Make sure a RAM disk isn't already present */
  154. if ((xRamDisk != 0) || (xRamDiskSize != 0)) {
  155. death("RAM disk is already attached to this kernel\n", outputVmlinux, argv[3]);
  156. }
  157. /* Fill in the values */
  158. *((u_int32_t *) &inbuf[0x0c]) = htonl(ramStartOffs);
  159. *((u_int32_t *) &inbuf[0x14]) = htonl(ramPages);
  160. /* Write out the new naca */
  161. fflush(outputVmlinux);
  162. fseek(outputVmlinux, ElfHeaderSize + naca, SEEK_SET);
  163. if (fwrite(inbuf, 0x18, 1, outputVmlinux) != 1) {
  164. death("Could not write naca\n", outputVmlinux, argv[3]);
  165. }
  166. printf("RAM Disk of 0x%x pages size is attached to the kernel at offset 0x%08x\n",
  167. ramPages, ramStartOffs);
  168. /* Done */
  169. fclose(outputVmlinux);
  170. /* Set permission to executable */
  171. chmod(argv[3], S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
  172. return 0;
  173. }