piggyback_64.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. 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. if (stat (argv[3], &s) < 0) die (argv[3]);
  41. map = fopen (argv[2], "r");
  42. if (!map) die(argv[2]);
  43. while (fgets (buffer, 1024, map)) {
  44. if (!strcmp (buffer + 19, "_start\n"))
  45. start = strtoul (buffer + 8, NULL, 16);
  46. else if (!strcmp (buffer + 19, "_end\n"))
  47. end = strtoul (buffer + 8, NULL, 16);
  48. }
  49. fclose (map);
  50. if ((image = open(argv[1],O_RDWR)) < 0) die(argv[1]);
  51. if (read(image,buffer,512) != 512) die(argv[1]);
  52. if (!memcmp (buffer, "\177ELF", 4)) {
  53. unsigned int *p = (unsigned int *)(buffer + *(unsigned int *)(buffer + 28));
  54. i = p[1] + *(unsigned int *)(buffer + 24) - p[2];
  55. if (lseek(image,i,0) < 0) die("lseek");
  56. if (read(image,buffer,512) != 512) die(argv[1]);
  57. j = 0;
  58. } else if (*(unsigned int *)buffer == 0x01030107) {
  59. i = j = 32;
  60. } else {
  61. fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
  62. exit(1);
  63. }
  64. k = i;
  65. if (j == 32 && buffer[40] == 'H' && buffer[41] == 'd' && buffer[42] == 'r' && buffer[43] == 'S') {
  66. offset = 40 + 10;
  67. } else {
  68. i += ((*(unsigned short *)(buffer + j + 2))<<2) - 512;
  69. if (lseek(image,i,0) < 0) die("lseek");
  70. if (read(image,buffer,1024) != 1024) die(argv[1]);
  71. for (q = buffer, r = q + 512; q < r; q += 4) {
  72. if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
  73. break;
  74. }
  75. if (q == r) {
  76. fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
  77. exit(1);
  78. }
  79. offset = i + (q - buffer) + 10;
  80. }
  81. if (lseek(image, offset, 0) < 0) die ("lseek");
  82. *(unsigned *)buffer = 0;
  83. *(unsigned *)(buffer + 4) = 0x01000000;
  84. *(unsigned *)(buffer + 8) = ((end + 32 + 8191) & ~8191);
  85. *(unsigned *)(buffer + 12) = s.st_size;
  86. if (write(image,buffer+2,14) != 14) die (argv[1]);
  87. if (lseek(image, 4, 0) < 0) die ("lseek");
  88. *(unsigned *)buffer = ((end + 32 + 8191) & ~8191) - (start & ~0x3fffffUL) + s.st_size;
  89. *(unsigned *)(buffer + 4) = 0;
  90. *(unsigned *)(buffer + 8) = 0;
  91. if (write(image,buffer,12) != 12) die (argv[1]);
  92. if (lseek(image, k - start + ((end + 32 + 8191) & ~8191), 0) < 0) die ("lseek");
  93. if ((tail = open(argv[3],O_RDONLY)) < 0) die(argv[3]);
  94. while ((i = read (tail,buffer,1024)) > 0)
  95. if (write(image,buffer,i) != i) die (argv[1]);
  96. if (close(image) < 0) die("close");
  97. if (close(tail) < 0) die("close");
  98. return 0;
  99. }