piggyback_32.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <dirent.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <stdio.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. #define AOUT_TEXT_OFFSET 32
  32. /* read two bytes as big endian */
  33. static unsigned short ld2(char *p)
  34. {
  35. return (p[0] << 8) | p[1];
  36. }
  37. /* read 4 bytes as big endian */
  38. static unsigned int ld4(char *p)
  39. {
  40. return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  41. }
  42. /* save 4 bytes as big endian */
  43. static void st4(char *p, unsigned int x)
  44. {
  45. p[0] = x >> 24;
  46. p[1] = x >> 16;
  47. p[2] = x >> 8;
  48. p[3] = x;
  49. }
  50. static void die(const char *str)
  51. {
  52. perror(str);
  53. exit(1);
  54. }
  55. static void usage(void)
  56. {
  57. /* fs_img.gz is an image of initial ramdisk. */
  58. fprintf(stderr, "Usage: piggyback vmlinux.aout System.map fs_img.gz\n");
  59. fprintf(stderr, "\tKernel image will be modified in place.\n");
  60. exit(1);
  61. }
  62. static int start_line(const char *line)
  63. {
  64. if (strcmp(line + 8, " T _start\n") == 0)
  65. return 1;
  66. else if (strcmp(line + 16, " T _start\n") == 0)
  67. return 1;
  68. return 0;
  69. }
  70. static int end_line(const char *line)
  71. {
  72. if (strcmp(line + 8, " A _end\n") == 0)
  73. return 1;
  74. else if (strcmp (line + 16, " A _end\n") == 0)
  75. return 1;
  76. return 0;
  77. }
  78. /*
  79. * Find address for start and end in System.map.
  80. * The file looks like this:
  81. * f0004000 T _start
  82. * f0379f79 A _end
  83. * 1234567890123456
  84. * ^coloumn 1
  85. * There is support for 64 bit addresses too.
  86. *
  87. * Return 0 if either start or end is not found
  88. */
  89. static int get_start_end(const char *filename, unsigned int *start, unsigned int *end)
  90. {
  91. FILE *map;
  92. char buffer[1024];
  93. *start = 0;
  94. *end = 0;
  95. map = fopen(filename, "r");
  96. if (!map)
  97. die(filename);
  98. while (fgets(buffer, 1024, map)) {
  99. if (start_line(buffer))
  100. *start = strtoul(buffer, NULL, 16);
  101. else if (end_line(buffer))
  102. *end = strtoul(buffer, NULL, 16);
  103. }
  104. fclose (map);
  105. if (*start == 0 || *end == 0)
  106. return 0;
  107. return 1;
  108. }
  109. int main(int argc,char **argv)
  110. {
  111. static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
  112. char buffer[1024], *q, *r;
  113. unsigned int i, j, k, start, end, offset;
  114. struct stat s;
  115. int image, tail;
  116. if (argc != 4)
  117. usage();
  118. if (stat (argv[3], &s) < 0)
  119. die(argv[3]);
  120. if (!get_start_end(argv[2], &start, &end)) {
  121. fprintf (stderr, "Could not determine start and end from %s\n", argv[2]);
  122. exit(1);
  123. }
  124. if ((image = open(argv[1], O_RDWR)) < 0)
  125. die(argv[1]);
  126. if (read(image, buffer, 512) != 512)
  127. die(argv[1]);
  128. if (memcmp (buffer, "\177ELF", 4) == 0) {
  129. q = buffer + ld4(buffer + 28);
  130. i = ld4(q + 4) + ld4(buffer + 24) - ld4(q + 8);
  131. if (lseek(image, i, 0) < 0)
  132. die("lseek");
  133. if (read(image, buffer, 512) != 512)
  134. die(argv[1]);
  135. j = 0;
  136. } else if (memcmp(buffer, aout_magic, 4) == 0) {
  137. i = j = AOUT_TEXT_OFFSET;
  138. } else {
  139. fprintf (stderr, "Not ELF nor a.out. Don't blame me.\n");
  140. exit(1);
  141. }
  142. k = i;
  143. /*
  144. * We need to fill in values for sparc_ramdisk_image + sparc_ramdisk_size
  145. * To locate these symbols search for the "HdrS" text which appear
  146. * in the image a little before the gokernel symbol.
  147. * See definition of these in init_32.S
  148. */
  149. /* Find the gokernel label */
  150. i += (ld2(buffer + j + 2) << 2) - 512;
  151. if (lseek(image, i, 0) < 0)
  152. die("lseek");
  153. if (read(image, buffer, 1024) != 1024)
  154. die(argv[1]);
  155. for (q = buffer, r = q + 512; q < r; q += 4) {
  156. if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
  157. break;
  158. }
  159. if (q == r) {
  160. fprintf (stderr, "Couldn't find headers signature in the kernel.\n");
  161. exit(1);
  162. }
  163. offset = i + (q - buffer) + 10;
  164. if (lseek(image, offset, 0) < 0)
  165. die("lseek");
  166. /*
  167. * root_flags = 0
  168. * root_dev = 1 (RAMDISK_MAJOR)
  169. * ram_flags = 0
  170. * sparc_ramdisk_image = "PAGE aligned address after _end")
  171. * sparc_ramdisk_size = size of image
  172. */
  173. st4(buffer, 0);
  174. st4(buffer + 4, 0x01000000);
  175. st4(buffer + 8, (end + 32 + 4095) & ~4095);
  176. st4(buffer + 12, s.st_size);
  177. if (write(image, buffer + 2, 14) != 14)
  178. die(argv[1]);
  179. /* seek page aligned boundary in the image file and add boot image */
  180. if (lseek(image, k - start + ((end + 32 + 4095) & ~4095), 0) < 0)
  181. die("lseek");
  182. if ((tail = open(argv[3],O_RDONLY)) < 0)
  183. die(argv[3]);
  184. while ((i = read(tail, buffer, 1024)) > 0)
  185. if (write(image, buffer, i) != i)
  186. die(argv[1]);
  187. if (close(image) < 0)
  188. die("close");
  189. if (close(tail) < 0)
  190. die("close");
  191. return 0;
  192. }