addRamDisk.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:9k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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