xref: /illumos-gate/usr/src/tools/mbh_patch/mbh_patch.c (revision 2a6e99a0f1f7d22c0396e8b2ce9b9babbd1056cf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <strings.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/inttypes.h>
34 #include <sys/elf.h>
35 #include <sys/elf_notes.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
38 #include <sys/sysmacros.h>
39 #include "sys/multiboot.h"
40 #include "sys/multiboot2.h"
41 
42 static char *pname;
43 static char *fname;
44 static char *image;	/* pointer to the ELF file in memory */
45 
46 #define	ELFSEEK(offset) ((void *)(image + offset))
47 
48 /*
49  * Find MB2 header tags for entry and patch it.
50  * The first tag is right after header.
51  */
52 static int
53 patch64_mb2(multiboot2_header_t *mbh2, int file_offset,
54     Elf64_Addr ptload_start, Elf32_Off ptload_offset)
55 {
56 	multiboot_header_tag_t *tagp = mbh2->mb2_tags;
57 	multiboot_header_tag_address_t *mbaddr = NULL;
58 	multiboot_header_tag_entry_address_t *mbentry = NULL;
59 
60 	/*
61 	 * Loop until we get end TAG or we have both tags.
62 	 */
63 	while (tagp->mbh_type != MULTIBOOT_HEADER_TAG_END &&
64 	    (mbaddr == NULL || mbentry == NULL)) {
65 		switch (tagp->mbh_type) {
66 		case MULTIBOOT_HEADER_TAG_ADDRESS:
67 			mbaddr = (multiboot_header_tag_address_t *)tagp;
68 			break;
69 		case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS:
70 			mbentry = (multiboot_header_tag_entry_address_t *)tagp;
71 			break;
72 		}
73 		tagp = (multiboot_header_tag_t *)
74 		    ((uintptr_t)tagp +
75 		    P2ROUNDUP(tagp->mbh_size, MULTIBOOT_TAG_ALIGN));
76 	}
77 
78 	if (mbaddr == NULL || mbentry == NULL) {
79 		(void) fprintf(stderr, "Missing multiboot2 %s tag\n",
80 		    (mbaddr == NULL)? "address" : "entry");
81 		return (1);
82 	}
83 
84 	/* Patch it. */
85 	mbaddr->mbh_load_addr = ptload_start - ptload_offset;
86 	mbaddr->mbh_header_addr = mbaddr->mbh_load_addr + file_offset;
87 	mbentry->mbh_entry_addr = ptload_start;
88 
89 #ifdef VERBOSE
90 	(void) printf("  ELF64 MB2 header patched\n");
91 	(void) printf("\tload_addr now:   0x%x\n", mbaddr->mbh_load_addr);
92 	(void) printf("\theader_addr now: 0x%x\n", mbaddr->mbh_header_addr);
93 	(void) printf("\tentry_addr now:  0x%x\n", mbentry->mbh_entry_addr);
94 #endif
95 	return (0);
96 }
97 
98 /*
99  * Patch the load address / entry address for MB1 and MB2 if present.
100  * Find the physical load address of the 1st PT_LOAD segment.
101  * Find the amount that e_entry exceeds that amount.
102  * Now go back and subtract the excess from the p_paddr of the LOAD segment.
103  */
104 static int
105 patch64(Elf64_Ehdr *eh)
106 {
107 	Elf64_Phdr		*phdr;
108 	caddr_t			phdrs = NULL;
109 	int			ndx, mem, mem2;
110 	multiboot_header_t	*mbh;
111 	multiboot2_header_t	*mbh2;
112 
113 	/*
114 	 * Verify some ELF basics - this must be an executable with program
115 	 * headers.
116 	 */
117 	if (eh->e_type != ET_EXEC) {
118 		(void) fprintf(stderr, "%s: %s: not ET_EXEC, e_type = 0x%x\n",
119 		    pname, fname, eh->e_type);
120 		return (1);
121 	}
122 	if ((eh->e_phnum == 0) || (eh->e_phoff == 0)) {
123 		(void) fprintf(stderr, "%s: %s: no program headers\n", pname,
124 		    fname);
125 		return (1);
126 	}
127 
128 	/*
129 	 * Get the program headers.
130 	 */
131 	if ((phdrs = ELFSEEK(eh->e_phoff)) == NULL) {
132 		(void) fprintf(stderr, "%s: %s: failed to get %d program "
133 		    "hdrs\n", pname, fname, eh->e_phnum);
134 		return (1);
135 	}
136 
137 	/*
138 	 * Look for multiboot1 header.  It must be 32-bit aligned and
139 	 * completely contained in the 1st 8K of the file.
140 	 */
141 	for (mem = 0; mem < 8192 - sizeof (multiboot_header_t); mem += 4) {
142 		mbh = ELFSEEK(mem);
143 		if (mbh->magic == MB_HEADER_MAGIC)
144 			break;
145 	}
146 
147 	if (mem >= 8192 - sizeof (multiboot_header_t)) {
148 		(void) fprintf(stderr, "%s: %s: Didn't find multiboot header\n",
149 		    pname, fname);
150 		return (1);
151 	}
152 
153 	/*
154 	 * Look for multiboot2 header.  It must be 64-bit aligned and
155 	 * completely contained in the 1st 32K of the file.
156 	 * We do not require it to be present.
157 	 */
158 	ndx = 0;
159 	for (mem2 = 0;
160 	    mem2 <= MULTIBOOT_SEARCH - sizeof (multiboot2_header_t);
161 	    mem2 += MULTIBOOT_HEADER_ALIGN) {
162 		mbh2 = ELFSEEK(mem2);
163 		ndx = mbh2->mb2_header_length;
164 		if (mbh2->mb2_magic == MULTIBOOT2_HEADER_MAGIC)
165 			break;
166 		ndx = 0;
167 	}
168 
169 	if (ndx == 0 || mem2 + ndx > MULTIBOOT_SEARCH) {
170 #ifdef VERBOSE
171 		(void) fprintf(stderr, "%s: %s: Didn't find multiboot2 "
172 		    "header\n", pname, fname);
173 #endif
174 		mbh2 = NULL;
175 	}
176 
177 	/*
178 	 * Find the 1:1 mapped PT_LOAD section
179 	 */
180 	for (ndx = 0; ndx < eh->e_phnum; ndx++) {
181 		/*LINTED [ELF program header alignment]*/
182 		phdr = (Elf64_Phdr *)(phdrs + eh->e_phentsize * ndx);
183 
184 		/*
185 		 * Find the low memory 1:1 PT_LOAD section!
186 		 */
187 		if (phdr->p_type != PT_LOAD)
188 			continue;
189 
190 		if (phdr->p_memsz == 0)
191 			continue;
192 
193 		if (phdr->p_paddr != phdr->p_vaddr)
194 			continue;
195 
196 		/*
197 		 * Make sure the multiboot header is part of the first PT_LOAD
198 		 * segment, and that the executables entry point starts at the
199 		 * same segment.
200 		 */
201 		if ((mem < phdr->p_offset) ||
202 		    (mem >= (phdr->p_offset + phdr->p_filesz))) {
203 			(void) fprintf(stderr, "%s: %s: identity mapped "
204 			    "PT_LOAD wasn't 1st PT_LOAD\n", pname, fname);
205 			return (1);
206 		}
207 		if (eh->e_entry != phdr->p_paddr) {
208 			(void) fprintf(stderr, "%s: %s: entry != paddr\n",
209 			    pname, fname);
210 			return (1);
211 		}
212 
213 		if (mbh2 != NULL && ((mem2 < phdr->p_offset) ||
214 		    (mem2 >= (phdr->p_offset + phdr->p_filesz)))) {
215 #ifdef VERBOSE
216 			(void) fprintf(stderr, "%s: %s: multiboot2 header not"
217 			    " in 1st PT_LOAD\n", pname, fname);
218 #endif
219 			mem2 = 0;
220 			mbh2 = NULL;
221 		}
222 
223 		/*
224 		 * Patch the multiboot header fields to get entire file loaded.
225 		 * Grub uses the MB header for 64 bit loading.
226 		 */
227 		mbh->load_addr = phdr->p_paddr - phdr->p_offset;
228 		mbh->entry_addr = phdr->p_paddr;
229 		mbh->header_addr = mbh->load_addr + mem;
230 #ifdef VERBOSE
231 		(void) printf("  %s: ELF64 MB header patched\n", fname);
232 		(void) printf("\tload_addr now:   0x%x\n", mbh->load_addr);
233 		(void) printf("\tentry_addr now:  0x%x\n", mbh->entry_addr);
234 		(void) printf("\theader_addr now: 0x%x\n", mbh->header_addr);
235 #endif
236 		if (mbh2 != NULL)
237 			return (patch64_mb2(mbh2, mem2, phdr->p_paddr,
238 			    phdr->p_offset));
239 		return (0);
240 	}
241 
242 	(void) fprintf(stderr, "%s: %s: Didn't find 1:1 mapped PT_LOAD "
243 	    "section\n", pname, fname);
244 	return (1);
245 }
246 
247 int
248 main(int argc, char **argv)
249 {
250 	int	fd;
251 	uchar_t *ident;
252 	void	*hdr = NULL;
253 	struct	stat sb;
254 
255 	/*
256 	 * We expect one argument -- the elf file.
257 	 */
258 	if (argc != 2) {
259 		(void) fprintf(stderr, "usage: %s <unix-elf-file>\n", argv[0]);
260 		return (1);
261 	}
262 
263 	pname = strrchr(argv[0], '/');
264 	if (pname == NULL)
265 		pname = argv[0];
266 	else
267 		++pname;
268 
269 	fname = argv[1];
270 	if ((fd = open(fname, O_RDWR)) < 0) {
271 		(void) fprintf(stderr, "%s: open(%s, O_RDWR) failed: %s\n",
272 		    pname, fname, strerror(errno));
273 		return (1);
274 	}
275 
276 	if (fstat(fd, &sb) != 0) {
277 		(void) fprintf(stderr, "%s: fstat failed: %s\n",
278 		    pname, strerror(errno));
279 		return (1);
280 	}
281 
282 	/* Make sure we have at least MULTIBOOT_SEARCH bytes. */
283 	if (sb.st_size < MULTIBOOT_SEARCH) {
284 		(void) fprintf(stderr, "%s: %s is too small for a kernel\n",
285 		    pname, fname);
286 		return (1);
287 	}
288 
289 	/*
290 	 * mmap the 1st 32K -- MB1 header is within first 8k and MB2 header
291 	 * is within 32k.
292 	 */
293 	image = mmap(NULL, MULTIBOOT_SEARCH, PROT_READ | PROT_WRITE,
294 	    MAP_SHARED, fd, 0);
295 	if (image == MAP_FAILED) {
296 		(void) fprintf(stderr, "%s: mmap() of %s failed: %s\n",
297 		    pname, fname, strerror(errno));
298 		return (1);
299 	}
300 
301 	ident = ELFSEEK(0);
302 	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
303 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
304 		(void) fprintf(stderr, "%s: %s: not an ELF file!\n", pname,
305 		    fname);
306 		return (1);
307 	}
308 
309 	if (ident[EI_CLASS] == ELFCLASS64) {
310 		hdr = ELFSEEK(0);
311 		return (patch64(hdr));
312 	}
313 	if (ident[EI_CLASS] != ELFCLASS32) {
314 		(void) fprintf(stderr, "%s: Unknown ELF class 0x%x\n", pname,
315 		    ident[EI_CLASS]);
316 		return (1);
317 	}
318 	return (0);
319 }
320