piggyback.c 6.0 KB

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