piggyback_32.c 3.9 KB

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