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