addnote.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Program to hack in a PT_NOTE program header entry in an ELF file.
  3. * This is needed for OF on RS/6000s to load an image correctly.
  4. * Note that OF needs a program header entry for the note, not an
  5. * ELF section.
  6. *
  7. * Copyright 2000 Paul Mackerras.
  8. *
  9. * Adapted for 64 bit little endian images by Andrew Tauferner.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * Usage: addnote zImage
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. /* CHRP note section */
  24. static const char arch[] = "PowerPC";
  25. #define N_DESCR 6
  26. unsigned int descr[N_DESCR] = {
  27. 0xffffffff, /* real-mode = true */
  28. 0x02000000, /* real-base, i.e. where we expect OF to be */
  29. 0xffffffff, /* real-size */
  30. 0xffffffff, /* virt-base */
  31. 0xffffffff, /* virt-size */
  32. 0x4000, /* load-base */
  33. };
  34. /* RPA note section */
  35. static const char rpaname[] = "IBM,RPA-Client-Config";
  36. /*
  37. * Note: setting ignore_my_client_config *should* mean that OF ignores
  38. * all the other fields, but there is a firmware bug which means that
  39. * it looks at the splpar field at least. So these values need to be
  40. * reasonable.
  41. */
  42. #define N_RPA_DESCR 8
  43. unsigned int rpanote[N_RPA_DESCR] = {
  44. 0, /* lparaffinity */
  45. 64, /* min_rmo_size */
  46. 0, /* min_rmo_percent */
  47. 40, /* max_pft_size */
  48. 1, /* splpar */
  49. -1, /* min_load */
  50. 0, /* new_mem_def */
  51. 1, /* ignore_my_client_config */
  52. };
  53. #define ROUNDUP(len) (((len) + 3) & ~3)
  54. unsigned char buf[1024];
  55. #define ELFDATA2LSB 1
  56. #define ELFDATA2MSB 2
  57. static int e_data = ELFDATA2MSB;
  58. #define ELFCLASS32 1
  59. #define ELFCLASS64 2
  60. static int e_class = ELFCLASS32;
  61. #define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
  62. #define GET_32BE(off) ((GET_16BE(off) << 16U) + GET_16BE((off)+2U))
  63. #define GET_64BE(off) ((((unsigned long long)GET_32BE(off)) << 32ULL) + \
  64. ((unsigned long long)GET_32BE((off)+4ULL)))
  65. #define PUT_16BE(off, v)(buf[off] = ((v) >> 8) & 0xff, \
  66. buf[(off) + 1] = (v) & 0xff)
  67. #define PUT_32BE(off, v)(PUT_16BE((off), (v) >> 16L), PUT_16BE((off) + 2, (v)))
  68. #define PUT_64BE(off, v)((PUT_32BE((off), (v) >> 32L), \
  69. PUT_32BE((off) + 4, (v))))
  70. #define GET_16LE(off) ((buf[off]) + (buf[(off)+1] << 8))
  71. #define GET_32LE(off) (GET_16LE(off) + (GET_16LE((off)+2U) << 16U))
  72. #define GET_64LE(off) ((unsigned long long)GET_32LE(off) + \
  73. (((unsigned long long)GET_32LE((off)+4ULL)) << 32ULL))
  74. #define PUT_16LE(off, v) (buf[off] = (v) & 0xff, \
  75. buf[(off) + 1] = ((v) >> 8) & 0xff)
  76. #define PUT_32LE(off, v) (PUT_16LE((off), (v)), PUT_16LE((off) + 2, (v) >> 16L))
  77. #define PUT_64LE(off, v) (PUT_32LE((off), (v)), PUT_32LE((off) + 4, (v) >> 32L))
  78. #define GET_16(off) (e_data == ELFDATA2MSB ? GET_16BE(off) : GET_16LE(off))
  79. #define GET_32(off) (e_data == ELFDATA2MSB ? GET_32BE(off) : GET_32LE(off))
  80. #define GET_64(off) (e_data == ELFDATA2MSB ? GET_64BE(off) : GET_64LE(off))
  81. #define PUT_16(off, v) (e_data == ELFDATA2MSB ? PUT_16BE(off, v) : \
  82. PUT_16LE(off, v))
  83. #define PUT_32(off, v) (e_data == ELFDATA2MSB ? PUT_32BE(off, v) : \
  84. PUT_32LE(off, v))
  85. #define PUT_64(off, v) (e_data == ELFDATA2MSB ? PUT_64BE(off, v) : \
  86. PUT_64LE(off, v))
  87. /* Structure of an ELF file */
  88. #define E_IDENT 0 /* ELF header */
  89. #define E_PHOFF (e_class == ELFCLASS32 ? 28 : 32)
  90. #define E_PHENTSIZE (e_class == ELFCLASS32 ? 42 : 54)
  91. #define E_PHNUM (e_class == ELFCLASS32 ? 44 : 56)
  92. #define E_HSIZE (e_class == ELFCLASS32 ? 52 : 64)
  93. #define EI_MAGIC 0 /* offsets in E_IDENT area */
  94. #define EI_CLASS 4
  95. #define EI_DATA 5
  96. #define PH_TYPE 0 /* ELF program header */
  97. #define PH_OFFSET (e_class == ELFCLASS32 ? 4 : 8)
  98. #define PH_FILESZ (e_class == ELFCLASS32 ? 16 : 32)
  99. #define PH_HSIZE (e_class == ELFCLASS32 ? 32 : 56)
  100. #define PT_NOTE 4 /* Program header type = note */
  101. unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
  102. int
  103. main(int ac, char **av)
  104. {
  105. int fd, n, i;
  106. unsigned long ph, ps, np;
  107. long nnote, nnote2, ns;
  108. if (ac != 2) {
  109. fprintf(stderr, "Usage: %s elf-file\n", av[0]);
  110. exit(1);
  111. }
  112. fd = open(av[1], O_RDWR);
  113. if (fd < 0) {
  114. perror(av[1]);
  115. exit(1);
  116. }
  117. nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
  118. nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
  119. n = read(fd, buf, sizeof(buf));
  120. if (n < 0) {
  121. perror("read");
  122. exit(1);
  123. }
  124. if (memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
  125. goto notelf;
  126. e_class = buf[E_IDENT+EI_CLASS];
  127. if (e_class != ELFCLASS32 && e_class != ELFCLASS64)
  128. goto notelf;
  129. e_data = buf[E_IDENT+EI_DATA];
  130. if (e_data != ELFDATA2MSB && e_data != ELFDATA2LSB)
  131. goto notelf;
  132. if (n < E_HSIZE)
  133. goto notelf;
  134. ph = (e_class == ELFCLASS32 ? GET_32(E_PHOFF) : GET_64(E_PHOFF));
  135. ps = GET_16(E_PHENTSIZE);
  136. np = GET_16(E_PHNUM);
  137. if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
  138. goto notelf;
  139. if (ph + (np + 2) * ps + nnote + nnote2 > n)
  140. goto nospace;
  141. for (i = 0; i < np; ++i) {
  142. if (GET_32(ph + PH_TYPE) == PT_NOTE) {
  143. fprintf(stderr, "%s already has a note entry\n",
  144. av[1]);
  145. exit(0);
  146. }
  147. ph += ps;
  148. }
  149. /* XXX check that the area we want to use is all zeroes */
  150. for (i = 0; i < 2 * ps + nnote + nnote2; ++i)
  151. if (buf[ph + i] != 0)
  152. goto nospace;
  153. /* fill in the program header entry */
  154. ns = ph + 2 * ps;
  155. PUT_32(ph + PH_TYPE, PT_NOTE);
  156. if (e_class == ELFCLASS32)
  157. PUT_32(ph + PH_OFFSET, ns);
  158. else
  159. PUT_64(ph + PH_OFFSET, ns);
  160. if (e_class == ELFCLASS32)
  161. PUT_32(ph + PH_FILESZ, nnote);
  162. else
  163. PUT_64(ph + PH_FILESZ, nnote);
  164. /* fill in the note area we point to */
  165. /* XXX we should probably make this a proper section */
  166. PUT_32(ns, strlen(arch) + 1);
  167. PUT_32(ns + 4, N_DESCR * 4);
  168. PUT_32(ns + 8, 0x1275);
  169. strcpy((char *) &buf[ns + 12], arch);
  170. ns += 12 + strlen(arch) + 1;
  171. for (i = 0; i < N_DESCR; ++i, ns += 4)
  172. PUT_32BE(ns, descr[i]);
  173. /* fill in the second program header entry and the RPA note area */
  174. ph += ps;
  175. PUT_32(ph + PH_TYPE, PT_NOTE);
  176. if (e_class == ELFCLASS32)
  177. PUT_32(ph + PH_OFFSET, ns);
  178. else
  179. PUT_64(ph + PH_OFFSET, ns);
  180. if (e_class == ELFCLASS32)
  181. PUT_32(ph + PH_FILESZ, nnote);
  182. else
  183. PUT_64(ph + PH_FILESZ, nnote2);
  184. /* fill in the note area we point to */
  185. PUT_32(ns, strlen(rpaname) + 1);
  186. PUT_32(ns + 4, sizeof(rpanote));
  187. PUT_32(ns + 8, 0x12759999);
  188. strcpy((char *) &buf[ns + 12], rpaname);
  189. ns += 12 + ROUNDUP(strlen(rpaname) + 1);
  190. for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
  191. PUT_32BE(ns, rpanote[i]);
  192. /* Update the number of program headers */
  193. PUT_16(E_PHNUM, np + 2);
  194. /* write back */
  195. lseek(fd, (long) 0, SEEK_SET);
  196. i = write(fd, buf, n);
  197. if (i < 0) {
  198. perror("write");
  199. exit(1);
  200. }
  201. if (i < n) {
  202. fprintf(stderr, "%s: write truncated\n", av[1]);
  203. exit(1);
  204. }
  205. exit(0);
  206. notelf:
  207. fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
  208. exit(1);
  209. nospace:
  210. fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
  211. av[1]);
  212. exit(1);
  213. }