piggyback_64.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Simple utility to make a single-image install kernel with initial ramdisk
  3. for Sparc64 tftpbooting without need to set up nfs.
  4. Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <dirent.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. /* Note: run this on an a.out kernel (use elftoaout for it), as PROM looks for a.out image onlly
  27. usage: piggyback vmlinux System.map tail, where tail is gzipped fs of the initial ramdisk */
  28. static void die(char *str)
  29. {
  30. perror (str);
  31. exit(1);
  32. }
  33. int main(int argc,char **argv)
  34. {
  35. char buffer [1024], *q, *r;
  36. unsigned int i, j, k, start, end, offset;
  37. FILE *map;
  38. struct stat s;
  39. int image, tail;
  40. start = end = 0;
  41. if (stat (argv[3], &s) < 0) die (argv[3]);
  42. map = fopen (argv[2], "r");
  43. if (!map) die(argv[2]);
  44. while (fgets (buffer, 1024, map)) {
  45. if (!strcmp (buffer + 19, "_start\n"))
  46. start = strtoul (buffer + 8, NULL, 16);
  47. else if (!strcmp (buffer + 19, "_end\n"))
  48. end = strtoul (buffer + 8, NULL, 16);
  49. }
  50. fclose (map);
  51. if ((image = open(argv[1],O_RDWR)) < 0) die(argv[1]);
  52. if (read(image,buffer,512) != 512) die(argv[1]);
  53. if (!memcmp (buffer, "\177ELF", 4)) {
  54. unsigned int *p = (unsigned int *)(buffer + *(unsigned int *)(buffer + 28));
  55. i = p[1] + *(unsigned int *)(buffer + 24) - p[2];
  56. if (lseek(image,i,0) < 0) die("lseek");
  57. if (read(image,buffer,512) != 512) die(argv[1]);
  58. j = 0;
  59. } else if (*(unsigned int *)buffer == 0x01030107) {
  60. i = j = 32;
  61. } else {
  62. fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
  63. exit(1);
  64. }
  65. k = i;
  66. if (j == 32 && buffer[40] == 'H' && buffer[41] == 'd' && buffer[42] == 'r' && buffer[43] == 'S') {
  67. offset = 40 + 10;
  68. } else {
  69. i += ((*(unsigned short *)(buffer + j + 2))<<2) - 512;
  70. if (lseek(image,i,0) < 0) die("lseek");
  71. if (read(image,buffer,1024) != 1024) die(argv[1]);
  72. for (q = buffer, r = q + 512; q < r; q += 4) {
  73. if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
  74. break;
  75. }
  76. if (q == r) {
  77. fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
  78. exit(1);
  79. }
  80. offset = i + (q - buffer) + 10;
  81. }
  82. if (lseek(image, offset, 0) < 0) die ("lseek");
  83. *(unsigned *)buffer = 0;
  84. *(unsigned *)(buffer + 4) = 0x01000000;
  85. *(unsigned *)(buffer + 8) = ((end + 32 + 8191) & ~8191);
  86. *(unsigned *)(buffer + 12) = s.st_size;
  87. if (write(image,buffer+2,14) != 14) die (argv[1]);
  88. if (lseek(image, 4, 0) < 0) die ("lseek");
  89. *(unsigned *)buffer = ((end + 32 + 8191) & ~8191) - (start & ~0x3fffffUL) + s.st_size;
  90. *(unsigned *)(buffer + 4) = 0;
  91. *(unsigned *)(buffer + 8) = 0;
  92. if (write(image,buffer,12) != 12) die (argv[1]);
  93. if (lseek(image, k - start + ((end + 32 + 8191) & ~8191), 0) < 0) die ("lseek");
  94. if ((tail = open(argv[3],O_RDONLY)) < 0) die(argv[3]);
  95. while ((i = read (tail,buffer,1024)) > 0)
  96. if (write(image,buffer,i) != i) die (argv[1]);
  97. if (close(image) < 0) die("close");
  98. if (close(tail) < 0) die("close");
  99. return 0;
  100. }