addnote.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Usage: addnote zImage [note.elf]
  15. *
  16. * If note.elf is supplied, it is the name of an ELF file that contains
  17. * an RPA note to use instead of the built-in one. Alternatively, the
  18. * note.elf file may be empty, in which case the built-in RPA note is
  19. * used (this is to simplify how this is invoked from the wrapper script).
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. /* CHRP note section */
  27. char arch[] = "PowerPC";
  28. #define N_DESCR 6
  29. unsigned int descr[N_DESCR] = {
  30. 0xffffffff, /* real-mode = true */
  31. 0x02000000, /* real-base, i.e. where we expect OF to be */
  32. 0xffffffff, /* real-size */
  33. 0xffffffff, /* virt-base */
  34. 0xffffffff, /* virt-size */
  35. 0x4000, /* load-base */
  36. };
  37. /* RPA note section */
  38. char rpaname[] = "IBM,RPA-Client-Config";
  39. /*
  40. * Note: setting ignore_my_client_config *should* mean that OF ignores
  41. * all the other fields, but there is a firmware bug which means that
  42. * it looks at the splpar field at least. So these values need to be
  43. * reasonable.
  44. */
  45. #define N_RPA_DESCR 8
  46. unsigned int rpanote[N_RPA_DESCR] = {
  47. 1, /* lparaffinity */
  48. 128, /* min_rmo_size */
  49. 0, /* min_rmo_percent */
  50. 46, /* max_pft_size */
  51. 1, /* splpar */
  52. -1, /* min_load */
  53. 1, /* new_mem_def */
  54. 0, /* ignore_my_client_config */
  55. };
  56. #define ROUNDUP(len) (((len) + 3) & ~3)
  57. unsigned char buf[512];
  58. unsigned char notebuf[512];
  59. #define GET_16BE(b, off) (((b)[off] << 8) + ((b)[(off)+1]))
  60. #define GET_32BE(b, off) ((GET_16BE((b), (off)) << 16) + \
  61. GET_16BE((b), (off)+2))
  62. #define PUT_16BE(b, off, v) ((b)[off] = ((v) >> 8) & 0xff, \
  63. (b)[(off) + 1] = (v) & 0xff)
  64. #define PUT_32BE(b, off, v) (PUT_16BE((b), (off), (v) >> 16), \
  65. PUT_16BE((b), (off) + 2, (v)))
  66. /* Structure of an ELF file */
  67. #define E_IDENT 0 /* ELF header */
  68. #define E_PHOFF 28
  69. #define E_PHENTSIZE 42
  70. #define E_PHNUM 44
  71. #define E_HSIZE 52 /* size of ELF header */
  72. #define EI_MAGIC 0 /* offsets in E_IDENT area */
  73. #define EI_CLASS 4
  74. #define EI_DATA 5
  75. #define PH_TYPE 0 /* ELF program header */
  76. #define PH_OFFSET 4
  77. #define PH_FILESZ 16
  78. #define PH_HSIZE 32 /* size of program header */
  79. #define PT_NOTE 4 /* Program header type = note */
  80. #define ELFCLASS32 1
  81. #define ELFDATA2MSB 2
  82. unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
  83. unsigned char *read_rpanote(const char *fname, int *nnp)
  84. {
  85. int notefd, nr, i;
  86. int ph, ps, np;
  87. int note, notesize;
  88. notefd = open(fname, O_RDONLY);
  89. if (notefd < 0) {
  90. perror(fname);
  91. exit(1);
  92. }
  93. nr = read(notefd, notebuf, sizeof(notebuf));
  94. if (nr < 0) {
  95. perror("read note");
  96. exit(1);
  97. }
  98. if (nr == 0) /* empty file */
  99. return NULL;
  100. if (nr < E_HSIZE ||
  101. memcmp(&notebuf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0 ||
  102. notebuf[E_IDENT+EI_CLASS] != ELFCLASS32 ||
  103. notebuf[E_IDENT+EI_DATA] != ELFDATA2MSB)
  104. goto notelf;
  105. close(notefd);
  106. /* now look for the RPA-note */
  107. ph = GET_32BE(notebuf, E_PHOFF);
  108. ps = GET_16BE(notebuf, E_PHENTSIZE);
  109. np = GET_16BE(notebuf, E_PHNUM);
  110. if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
  111. goto notelf;
  112. for (i = 0; i < np; ++i, ph += ps) {
  113. if (GET_32BE(notebuf, ph + PH_TYPE) != PT_NOTE)
  114. continue;
  115. note = GET_32BE(notebuf, ph + PH_OFFSET);
  116. notesize = GET_32BE(notebuf, ph + PH_FILESZ);
  117. if (notesize < 34 || note + notesize > nr)
  118. continue;
  119. if (GET_32BE(notebuf, note) != strlen(rpaname) + 1 ||
  120. GET_32BE(notebuf, note + 8) != 0x12759999 ||
  121. strcmp((char *)&notebuf[note + 12], rpaname) != 0)
  122. continue;
  123. /* looks like an RPA note, return it */
  124. *nnp = notesize;
  125. return &notebuf[note];
  126. }
  127. /* no RPA note found */
  128. return NULL;
  129. notelf:
  130. fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n", fname);
  131. exit(1);
  132. }
  133. int
  134. main(int ac, char **av)
  135. {
  136. int fd, n, i;
  137. int ph, ps, np;
  138. int nnote, nnote2, ns;
  139. unsigned char *rpap;
  140. if (ac != 2 && ac != 3) {
  141. fprintf(stderr, "Usage: %s elf-file [rpanote.elf]\n", av[0]);
  142. exit(1);
  143. }
  144. fd = open(av[1], O_RDWR);
  145. if (fd < 0) {
  146. perror(av[1]);
  147. exit(1);
  148. }
  149. nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
  150. nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
  151. rpap = NULL;
  152. n = read(fd, buf, sizeof(buf));
  153. if (n < 0) {
  154. perror("read");
  155. exit(1);
  156. }
  157. if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
  158. goto notelf;
  159. if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
  160. || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
  161. fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
  162. av[1]);
  163. exit(1);
  164. }
  165. if (ac == 3)
  166. rpap = read_rpanote(av[2], &nnote2);
  167. ph = GET_32BE(buf, E_PHOFF);
  168. ps = GET_16BE(buf, E_PHENTSIZE);
  169. np = GET_16BE(buf, E_PHNUM);
  170. if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
  171. goto notelf;
  172. if (ph + (np + 2) * ps + nnote + nnote2 > n)
  173. goto nospace;
  174. for (i = 0; i < np; ++i) {
  175. if (GET_32BE(buf, ph + PH_TYPE) == PT_NOTE) {
  176. fprintf(stderr, "%s already has a note entry\n",
  177. av[1]);
  178. exit(0);
  179. }
  180. ph += ps;
  181. }
  182. /* XXX check that the area we want to use is all zeroes */
  183. for (i = 0; i < 2 * ps + nnote + nnote2; ++i)
  184. if (buf[ph + i] != 0)
  185. goto nospace;
  186. /* fill in the program header entry */
  187. ns = ph + 2 * ps;
  188. PUT_32BE(buf, ph + PH_TYPE, PT_NOTE);
  189. PUT_32BE(buf, ph + PH_OFFSET, ns);
  190. PUT_32BE(buf, ph + PH_FILESZ, nnote);
  191. /* fill in the note area we point to */
  192. /* XXX we should probably make this a proper section */
  193. PUT_32BE(buf, ns, strlen(arch) + 1);
  194. PUT_32BE(buf, ns + 4, N_DESCR * 4);
  195. PUT_32BE(buf, ns + 8, 0x1275);
  196. strcpy((char *) &buf[ns + 12], arch);
  197. ns += 12 + strlen(arch) + 1;
  198. for (i = 0; i < N_DESCR; ++i, ns += 4)
  199. PUT_32BE(buf, ns, descr[i]);
  200. /* fill in the second program header entry and the RPA note area */
  201. ph += ps;
  202. PUT_32BE(buf, ph + PH_TYPE, PT_NOTE);
  203. PUT_32BE(buf, ph + PH_OFFSET, ns);
  204. PUT_32BE(buf, ph + PH_FILESZ, nnote2);
  205. /* fill in the note area we point to */
  206. if (rpap) {
  207. /* RPA note supplied in file, just copy the whole thing over */
  208. memcpy(buf + ns, rpap, nnote2);
  209. } else {
  210. PUT_32BE(buf, ns, strlen(rpaname) + 1);
  211. PUT_32BE(buf, ns + 4, sizeof(rpanote));
  212. PUT_32BE(buf, ns + 8, 0x12759999);
  213. strcpy((char *) &buf[ns + 12], rpaname);
  214. ns += 12 + ROUNDUP(strlen(rpaname) + 1);
  215. for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
  216. PUT_32BE(buf, ns, rpanote[i]);
  217. }
  218. /* Update the number of program headers */
  219. PUT_16BE(buf, E_PHNUM, np + 2);
  220. /* write back */
  221. lseek(fd, (long) 0, SEEK_SET);
  222. i = write(fd, buf, n);
  223. if (i < 0) {
  224. perror("write");
  225. exit(1);
  226. }
  227. if (i < n) {
  228. fprintf(stderr, "%s: write truncated\n", av[1]);
  229. exit(1);
  230. }
  231. exit(0);
  232. notelf:
  233. fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
  234. exit(1);
  235. nospace:
  236. fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
  237. av[1]);
  238. exit(1);
  239. }