piggyback.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  5. Pete Zaitcev <zaitcev@yahoo.com> endian fixes for cross-compiles, 2000.
  6. Copyright (C) 2011 Sam Ravnborg <sam@ravnborg.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  18. #include <dirent.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. /*
  29. * Note: run this on an a.out kernel (use elftoaout for it),
  30. * as PROM looks for a.out image only.
  31. */
  32. #define AOUT_TEXT_OFFSET 32
  33. static int is64bit = 0;
  34. /* align to power-of-two size */
  35. static int align(int n)
  36. {
  37. if (is64bit)
  38. return (n + 0x1fff) & ~0x1fff;
  39. else
  40. return (n + 0xfff) & ~0xfff;
  41. }
  42. /* read two bytes as big endian */
  43. static unsigned short ld2(char *p)
  44. {
  45. return (p[0] << 8) | p[1];
  46. }
  47. /* save 4 bytes as big endian */
  48. static void st4(char *p, unsigned int x)
  49. {
  50. p[0] = x >> 24;
  51. p[1] = x >> 16;
  52. p[2] = x >> 8;
  53. p[3] = x;
  54. }
  55. static void die(const char *str)
  56. {
  57. perror(str);
  58. exit(1);
  59. }
  60. static void usage(void)
  61. {
  62. /* fs_img.gz is an image of initial ramdisk. */
  63. fprintf(stderr, "Usage: piggyback bits vmlinux.aout System.map fs_img.gz\n");
  64. fprintf(stderr, "\tKernel image will be modified in place.\n");
  65. exit(1);
  66. }
  67. static int start_line(const char *line)
  68. {
  69. if (strcmp(line + 10, " _start\n") == 0)
  70. return 1;
  71. else if (strcmp(line + 18, " _start\n") == 0)
  72. return 1;
  73. return 0;
  74. }
  75. static int end_line(const char *line)
  76. {
  77. if (strcmp(line + 10, " _end\n") == 0)
  78. return 1;
  79. else if (strcmp (line + 18, " _end\n") == 0)
  80. return 1;
  81. return 0;
  82. }
  83. /*
  84. * Find address for start and end in System.map.
  85. * The file looks like this:
  86. * f0004000 ... _start
  87. * f0379f79 ... _end
  88. * 1234567890123456
  89. * ^coloumn 1
  90. * There is support for 64 bit addresses too.
  91. *
  92. * Return 0 if either start or end is not found
  93. */
  94. static int get_start_end(const char *filename, unsigned int *start,
  95. unsigned int *end)
  96. {
  97. FILE *map;
  98. char buffer[1024];
  99. *start = 0;
  100. *end = 0;
  101. map = fopen(filename, "r");
  102. if (!map)
  103. die(filename);
  104. while (fgets(buffer, 1024, map)) {
  105. if (start_line(buffer))
  106. *start = strtoul(buffer, NULL, 16);
  107. else if (end_line(buffer))
  108. *end = strtoul(buffer, NULL, 16);
  109. }
  110. fclose (map);
  111. if (*start == 0 || *end == 0)
  112. return 0;
  113. return 1;
  114. }
  115. #define LOOKBACK (128 * 4)
  116. #define BUFSIZE 1024
  117. /*
  118. * Find the HdrS entry from head_32/head_64.
  119. * We check if it is at the beginning of the file (sparc64 case)
  120. * and if not we search for it.
  121. * When we search do so in steps of 4 as HdrS is on a 4-byte aligned
  122. * address (it is on same alignment as sparc instructions)
  123. * Return the offset to the HdrS entry (as off_t)
  124. */
  125. static off_t get_hdrs_offset(int kernelfd, const char *filename)
  126. {
  127. char buffer[BUFSIZE];
  128. off_t offset;
  129. int i;
  130. if (lseek(kernelfd, 0, SEEK_SET) < 0)
  131. die("lseek");
  132. if (read(kernelfd, buffer, BUFSIZE) != BUFSIZE)
  133. die(filename);
  134. if (buffer[40] == 'H' && buffer[41] == 'd' &&
  135. buffer[42] == 'r' && buffer[43] == 'S') {
  136. return 40;
  137. } else {
  138. /* Find the gokernel label */
  139. /* Decode offset from branch instruction */
  140. offset = ld2(buffer + AOUT_TEXT_OFFSET + 2) << 2;
  141. /* Go back 512 bytes so we do not miss HdrS */
  142. offset -= LOOKBACK;
  143. /* skip a.out header */
  144. offset += AOUT_TEXT_OFFSET;
  145. if (lseek(kernelfd, offset, SEEK_SET) < 0)
  146. die("lseek");
  147. if (read(kernelfd, buffer, BUFSIZE) != BUFSIZE)
  148. die(filename);
  149. for (i = 0; i < LOOKBACK; i += 4) {
  150. if (buffer[i + 0] == 'H' && buffer[i + 1] == 'd' &&
  151. buffer[i + 2] == 'r' && buffer[i + 3] == 'S') {
  152. return offset + i;
  153. }
  154. }
  155. }
  156. fprintf (stderr, "Couldn't find headers signature in %s\n", filename);
  157. exit(1);
  158. }
  159. int main(int argc,char **argv)
  160. {
  161. static char aout_magic[] = { 0x01, 0x03, 0x01, 0x07 };
  162. char buffer[1024];
  163. unsigned int i, start, end;
  164. off_t offset;
  165. struct stat s;
  166. int image, tail;
  167. if (argc != 5)
  168. usage();
  169. if (strcmp(argv[1], "64") == 0)
  170. is64bit = 1;
  171. if (stat (argv[4], &s) < 0)
  172. die(argv[4]);
  173. if (!get_start_end(argv[3], &start, &end)) {
  174. fprintf(stderr, "Could not determine start and end from %s\n",
  175. argv[3]);
  176. exit(1);
  177. }
  178. if ((image = open(argv[2], O_RDWR)) < 0)
  179. die(argv[2]);
  180. if (read(image, buffer, 512) != 512)
  181. die(argv[2]);
  182. if (memcmp(buffer, aout_magic, 4) != 0) {
  183. fprintf (stderr, "Not a.out. Don't blame me.\n");
  184. exit(1);
  185. }
  186. /*
  187. * We need to fill in values for
  188. * sparc_ramdisk_image + sparc_ramdisk_size
  189. * To locate these symbols search for the "HdrS" text which appear
  190. * in the image a little before the gokernel symbol.
  191. * See definition of these in init_32.S
  192. */
  193. offset = get_hdrs_offset(image, argv[2]);
  194. /* skip HdrS + LINUX_VERSION_CODE + HdrS version */
  195. offset += 10;
  196. if (lseek(image, offset, 0) < 0)
  197. die("lseek");
  198. /*
  199. * root_flags = 0
  200. * root_dev = 1 (RAMDISK_MAJOR)
  201. * ram_flags = 0
  202. * sparc_ramdisk_image = "PAGE aligned address after _end")
  203. * sparc_ramdisk_size = size of image
  204. */
  205. st4(buffer, 0);
  206. st4(buffer + 4, 0x01000000);
  207. st4(buffer + 8, align(end + 32));
  208. st4(buffer + 12, s.st_size);
  209. if (write(image, buffer + 2, 14) != 14)
  210. die(argv[2]);
  211. /* For sparc64 update a_text and clear a_data + a_bss */
  212. if (is64bit)
  213. {
  214. if (lseek(image, 4, 0) < 0)
  215. die("lseek");
  216. /* a_text */
  217. st4(buffer, align(end + 32 + 8191) - (start & ~0x3fffffUL) +
  218. s.st_size);
  219. /* a_data */
  220. st4(buffer + 4, 0);
  221. /* a_bss */
  222. st4(buffer + 8, 0);
  223. if (write(image, buffer, 12) != 12)
  224. die(argv[2]);
  225. }
  226. /* seek page aligned boundary in the image file and add boot image */
  227. if (lseek(image, AOUT_TEXT_OFFSET - start + align(end + 32), 0) < 0)
  228. die("lseek");
  229. if ((tail = open(argv[4], O_RDONLY)) < 0)
  230. die(argv[4]);
  231. while ((i = read(tail, buffer, 1024)) > 0)
  232. if (write(image, buffer, i) != i)
  233. die(argv[2]);
  234. if (close(image) < 0)
  235. die("close");
  236. if (close(tail) < 0)
  237. die("close");
  238. return 0;
  239. }