piggyback_32.c 5.1 KB

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