addSystemMap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <byteswap.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. void xlate( char * inb, char * trb, unsigned len )
  7. {
  8. unsigned i;
  9. for ( i=0; i<len; ++i ) {
  10. char c = *inb++;
  11. char c1 = c >> 4;
  12. char c2 = c & 0xf;
  13. if ( c1 > 9 )
  14. c1 = c1 + 'A' - 10;
  15. else
  16. c1 = c1 + '0';
  17. if ( c2 > 9 )
  18. c2 = c2 + 'A' - 10;
  19. else
  20. c2 = c2 + '0';
  21. *trb++ = c1;
  22. *trb++ = c2;
  23. }
  24. *trb = 0;
  25. }
  26. #define ElfHeaderSize (64 * 1024)
  27. #define ElfPages (ElfHeaderSize / 4096)
  28. #define KERNELBASE (0xc0000000)
  29. void get4k( /*istream *inf*/FILE *file, char *buf )
  30. {
  31. unsigned j;
  32. unsigned num = fread(buf, 1, 4096, file);
  33. for ( j=num; j<4096; ++j )
  34. buf[j] = 0;
  35. }
  36. void put4k( /*ostream *outf*/FILE *file, char *buf )
  37. {
  38. fwrite(buf, 1, 4096, file);
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. char inbuf[4096];
  43. FILE *ramDisk = NULL;
  44. FILE *inputVmlinux = NULL;
  45. FILE *outputVmlinux = NULL;
  46. unsigned i = 0;
  47. unsigned long ramFileLen = 0;
  48. unsigned long ramLen = 0;
  49. unsigned long roundR = 0;
  50. unsigned long kernelLen = 0;
  51. unsigned long actualKernelLen = 0;
  52. unsigned long round = 0;
  53. unsigned long roundedKernelLen = 0;
  54. unsigned long ramStartOffs = 0;
  55. unsigned long ramPages = 0;
  56. unsigned long roundedKernelPages = 0;
  57. if ( argc < 2 ) {
  58. printf("Name of System Map file missing.\n");
  59. exit(1);
  60. }
  61. if ( argc < 3 ) {
  62. printf("Name of vmlinux file missing.\n");
  63. exit(1);
  64. }
  65. if ( argc < 4 ) {
  66. printf("Name of vmlinux output file missing.\n");
  67. exit(1);
  68. }
  69. ramDisk = fopen(argv[1], "r");
  70. if ( ! ramDisk ) {
  71. printf("System Map file \"%s\" failed to open.\n", argv[1]);
  72. exit(1);
  73. }
  74. inputVmlinux = fopen(argv[2], "r");
  75. if ( ! inputVmlinux ) {
  76. printf("vmlinux file \"%s\" failed to open.\n", argv[2]);
  77. exit(1);
  78. }
  79. outputVmlinux = fopen(argv[3], "w");
  80. if ( ! outputVmlinux ) {
  81. printf("output vmlinux file \"%s\" failed to open.\n", argv[3]);
  82. exit(1);
  83. }
  84. fseek(ramDisk, 0, SEEK_END);
  85. ramFileLen = ftell(ramDisk);
  86. fseek(ramDisk, 0, SEEK_SET);
  87. printf("%s file size = %ld\n", argv[1], ramFileLen);
  88. ramLen = ramFileLen;
  89. roundR = 4096 - (ramLen % 4096);
  90. if ( roundR ) {
  91. printf("Rounding System Map file up to a multiple of 4096, adding %ld\n", roundR);
  92. ramLen += roundR;
  93. }
  94. printf("Rounded System Map size is %ld\n", ramLen);
  95. fseek(inputVmlinux, 0, SEEK_END);
  96. kernelLen = ftell(inputVmlinux);
  97. fseek(inputVmlinux, 0, SEEK_SET);
  98. printf("kernel file size = %ld\n", kernelLen);
  99. if ( kernelLen == 0 ) {
  100. printf("You must have a linux kernel specified as argv[2]\n");
  101. exit(1);
  102. }
  103. actualKernelLen = kernelLen - ElfHeaderSize;
  104. printf("actual kernel length (minus ELF header) = %ld\n", actualKernelLen);
  105. round = actualKernelLen % 4096;
  106. roundedKernelLen = actualKernelLen;
  107. if ( round )
  108. roundedKernelLen += (4096 - round);
  109. printf("actual kernel length rounded up to a 4k multiple = %ld\n", roundedKernelLen);
  110. ramStartOffs = roundedKernelLen;
  111. ramPages = ramLen / 4096;
  112. printf("System map pages to copy = %ld\n", ramPages);
  113. // Copy 64K ELF header
  114. for (i=0; i<(ElfPages); ++i) {
  115. get4k( inputVmlinux, inbuf );
  116. put4k( outputVmlinux, inbuf );
  117. }
  118. roundedKernelPages = roundedKernelLen / 4096;
  119. fseek(inputVmlinux, ElfHeaderSize, SEEK_SET);
  120. {
  121. for ( i=0; i<roundedKernelPages; ++i ) {
  122. get4k( inputVmlinux, inbuf );
  123. if ( i == 0 ) {
  124. unsigned long * p;
  125. printf("Storing embedded_sysmap_start at 0x3c\n");
  126. p = (unsigned long *)(inbuf + 0x3c);
  127. #if (BYTE_ORDER == __BIG_ENDIAN)
  128. *p = ramStartOffs;
  129. #else
  130. *p = bswap_32(ramStartOffs);
  131. #endif
  132. printf("Storing embedded_sysmap_end at 0x44\n");
  133. p = (unsigned long *)(inbuf + 0x44);
  134. #if (BYTE_ORDER == __BIG_ENDIAN)
  135. *p = ramStartOffs + ramFileLen;
  136. #else
  137. *p = bswap_32(ramStartOffs + ramFileLen);
  138. #endif
  139. }
  140. put4k( outputVmlinux, inbuf );
  141. }
  142. }
  143. {
  144. for ( i=0; i<ramPages; ++i ) {
  145. get4k( ramDisk, inbuf );
  146. put4k( outputVmlinux, inbuf );
  147. }
  148. }
  149. fclose(ramDisk);
  150. fclose(inputVmlinux);
  151. fclose(outputVmlinux);
  152. /* Set permission to executable */
  153. chmod(argv[3], S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
  154. return 0;
  155. }