addnote.c 5.0 KB

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