addnote.c 6.7 KB

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