at master 248 lines 7.0 kB view raw
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 20/* CHRP note section */ 21static const char arch[] = "PowerPC"; 22 23#define N_DESCR 6 24unsigned 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 33/* RPA note section */ 34static const char rpaname[] = "IBM,RPA-Client-Config"; 35 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 43unsigned 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 54#define ROUNDUP(len) (((len) + 3) & ~3) 55 56unsigned char buf[1024]; 57#define ELFDATA2LSB 1 58#define ELFDATA2MSB 2 59static int e_data = ELFDATA2MSB; 60#define ELFCLASS32 1 61#define ELFCLASS64 2 62static int e_class = ELFCLASS32; 63 64#define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1])) 65#define GET_32BE(off) ((GET_16BE(off) << 16U) + GET_16BE((off)+2U)) 66#define GET_64BE(off) ((((unsigned long long)GET_32BE(off)) << 32ULL) + \ 67 ((unsigned long long)GET_32BE((off)+4ULL))) 68#define PUT_16BE(off, v)(buf[off] = ((v) >> 8) & 0xff, \ 69 buf[(off) + 1] = (v) & 0xff) 70#define PUT_32BE(off, v)(PUT_16BE((off), (v) >> 16L), PUT_16BE((off) + 2, (v))) 71#define PUT_64BE(off, v)((PUT_32BE((off), (unsigned long long)(v) >> 32L), \ 72 PUT_32BE((off) + 4, (unsigned long long)(v)))) 73 74#define GET_16LE(off) ((buf[off]) + (buf[(off)+1] << 8)) 75#define GET_32LE(off) (GET_16LE(off) + (GET_16LE((off)+2U) << 16U)) 76#define GET_64LE(off) ((unsigned long long)GET_32LE(off) + \ 77 (((unsigned long long)GET_32LE((off)+4ULL)) << 32ULL)) 78#define PUT_16LE(off, v) (buf[off] = (v) & 0xff, \ 79 buf[(off) + 1] = ((v) >> 8) & 0xff) 80#define PUT_32LE(off, v) (PUT_16LE((off), (v)), PUT_16LE((off) + 2, (v) >> 16L)) 81#define PUT_64LE(off, v) (PUT_32LE((off), (unsigned long long)(v)), \ 82 PUT_32LE((off) + 4, (unsigned long long)(v) >> 32L)) 83 84#define GET_16(off) (e_data == ELFDATA2MSB ? GET_16BE(off) : GET_16LE(off)) 85#define GET_32(off) (e_data == ELFDATA2MSB ? GET_32BE(off) : GET_32LE(off)) 86#define GET_64(off) (e_data == ELFDATA2MSB ? GET_64BE(off) : GET_64LE(off)) 87#define PUT_16(off, v) (e_data == ELFDATA2MSB ? PUT_16BE(off, v) : \ 88 PUT_16LE(off, v)) 89#define PUT_32(off, v) (e_data == ELFDATA2MSB ? PUT_32BE(off, v) : \ 90 PUT_32LE(off, v)) 91#define PUT_64(off, v) (e_data == ELFDATA2MSB ? PUT_64BE(off, v) : \ 92 PUT_64LE(off, v)) 93 94/* Structure of an ELF file */ 95#define E_IDENT 0 /* ELF header */ 96#define E_PHOFF (e_class == ELFCLASS32 ? 28 : 32) 97#define E_PHENTSIZE (e_class == ELFCLASS32 ? 42 : 54) 98#define E_PHNUM (e_class == ELFCLASS32 ? 44 : 56) 99#define E_HSIZE (e_class == ELFCLASS32 ? 52 : 64) 100 101#define EI_MAGIC 0 /* offsets in E_IDENT area */ 102#define EI_CLASS 4 103#define EI_DATA 5 104 105#define PH_TYPE 0 /* ELF program header */ 106#define PH_OFFSET (e_class == ELFCLASS32 ? 4 : 8) 107#define PH_FILESZ (e_class == ELFCLASS32 ? 16 : 32) 108#define PH_HSIZE (e_class == ELFCLASS32 ? 32 : 56) 109 110#define PT_NOTE 4 /* Program header type = note */ 111 112 113unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' }; 114 115int 116main(int ac, char **av) 117{ 118 int fd, n, i; 119 unsigned long ph, ps, np; 120 long nnote, nnote2, ns; 121 122 if (ac != 2) { 123 fprintf(stderr, "Usage: %s elf-file\n", av[0]); 124 exit(1); 125 } 126 fd = open(av[1], O_RDWR); 127 if (fd < 0) { 128 perror(av[1]); 129 exit(1); 130 } 131 132 nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr); 133 nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote); 134 135 n = read(fd, buf, sizeof(buf)); 136 if (n < 0) { 137 perror("read"); 138 exit(1); 139 } 140 141 if (memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0) 142 goto notelf; 143 e_class = buf[E_IDENT+EI_CLASS]; 144 if (e_class != ELFCLASS32 && e_class != ELFCLASS64) 145 goto notelf; 146 e_data = buf[E_IDENT+EI_DATA]; 147 if (e_data != ELFDATA2MSB && e_data != ELFDATA2LSB) 148 goto notelf; 149 if (n < E_HSIZE) 150 goto notelf; 151 152 ph = (e_class == ELFCLASS32 ? GET_32(E_PHOFF) : GET_64(E_PHOFF)); 153 ps = GET_16(E_PHENTSIZE); 154 np = GET_16(E_PHNUM); 155 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1) 156 goto notelf; 157 if (ph + (np + 2) * ps + nnote + nnote2 > n) 158 goto nospace; 159 160 for (i = 0; i < np; ++i) { 161 if (GET_32(ph + PH_TYPE) == PT_NOTE) { 162 fprintf(stderr, "%s already has a note entry\n", 163 av[1]); 164 exit(0); 165 } 166 ph += ps; 167 } 168 169 /* XXX check that the area we want to use is all zeroes */ 170 for (i = 0; i < 2 * ps + nnote + nnote2; ++i) 171 if (buf[ph + i] != 0) 172 goto nospace; 173 174 /* fill in the program header entry */ 175 ns = ph + 2 * ps; 176 PUT_32(ph + PH_TYPE, PT_NOTE); 177 if (e_class == ELFCLASS32) 178 PUT_32(ph + PH_OFFSET, ns); 179 else 180 PUT_64(ph + PH_OFFSET, ns); 181 182 if (e_class == ELFCLASS32) 183 PUT_32(ph + PH_FILESZ, nnote); 184 else 185 PUT_64(ph + PH_FILESZ, nnote); 186 187 /* fill in the note area we point to */ 188 /* XXX we should probably make this a proper section */ 189 PUT_32(ns, strlen(arch) + 1); 190 PUT_32(ns + 4, N_DESCR * 4); 191 PUT_32(ns + 8, 0x1275); 192 strcpy((char *) &buf[ns + 12], arch); 193 ns += 12 + strlen(arch) + 1; 194 for (i = 0; i < N_DESCR; ++i, ns += 4) 195 PUT_32BE(ns, descr[i]); 196 197 /* fill in the second program header entry and the RPA note area */ 198 ph += ps; 199 PUT_32(ph + PH_TYPE, PT_NOTE); 200 if (e_class == ELFCLASS32) 201 PUT_32(ph + PH_OFFSET, ns); 202 else 203 PUT_64(ph + PH_OFFSET, ns); 204 205 if (e_class == ELFCLASS32) 206 PUT_32(ph + PH_FILESZ, nnote); 207 else 208 PUT_64(ph + PH_FILESZ, nnote2); 209 210 /* fill in the note area we point to */ 211 PUT_32(ns, strlen(rpaname) + 1); 212 PUT_32(ns + 4, sizeof(rpanote)); 213 PUT_32(ns + 8, 0x12759999); 214 strcpy((char *) &buf[ns + 12], rpaname); 215 ns += 12 + ROUNDUP(strlen(rpaname) + 1); 216 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4) 217 PUT_32BE(ns, rpanote[i]); 218 219 /* Update the number of program headers */ 220 PUT_16(E_PHNUM, np + 2); 221 222 /* write back */ 223 i = lseek(fd, (long) 0, SEEK_SET); 224 if (i < 0) { 225 perror("lseek"); 226 exit(1); 227 } 228 i = write(fd, buf, n); 229 if (i < 0) { 230 perror("write"); 231 exit(1); 232 } 233 if (i < n) { 234 fprintf(stderr, "%s: write truncated\n", av[1]); 235 exit(1); 236 } 237 238 exit(0); 239 240 notelf: 241 fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]); 242 exit(1); 243 244 nospace: 245 fprintf(stderr, "sorry, I can't find space in %s to put the note\n", 246 av[1]); 247 exit(1); 248}