addRamDisk.c 8.9 KB

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