1ae115bc7Smrj /*
2ae115bc7Smrj  * CDDL HEADER START
3ae115bc7Smrj  *
4ae115bc7Smrj  * The contents of this file are subject to the terms of the
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * You may not use this file except in compliance with the License.
7ae115bc7Smrj  *
8ae115bc7Smrj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ae115bc7Smrj  * or http://www.opensolaris.org/os/licensing.
10ae115bc7Smrj  * See the License for the specific language governing permissions
11ae115bc7Smrj  * and limitations under the License.
12ae115bc7Smrj  *
13ae115bc7Smrj  * When distributing Covered Code, include this CDDL HEADER in each
14ae115bc7Smrj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ae115bc7Smrj  * If applicable, add the following below this CDDL HEADER, with the
16ae115bc7Smrj  * fields enclosed by brackets "[]" replaced with your own identifying
17ae115bc7Smrj  * information: Portions Copyright [yyyy] [name of copyright owner]
18ae115bc7Smrj  *
19ae115bc7Smrj  * CDDL HEADER END
20ae115bc7Smrj  */
21ae115bc7Smrj 
22ae115bc7Smrj /*
23ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24ae115bc7Smrj  * Use is subject to license terms.
25ae115bc7Smrj  */
26ae115bc7Smrj 
27ae115bc7Smrj #include <stdlib.h>
28d1827f25Srie #include <errno.h>
29ae115bc7Smrj #include <fcntl.h>
30ae115bc7Smrj #include <strings.h>
31ae115bc7Smrj #include <stdio.h>
32ae115bc7Smrj #include <sys/types.h>
33ae115bc7Smrj #include <sys/inttypes.h>
34ae115bc7Smrj #include <sys/elf.h>
35ae115bc7Smrj #include <sys/elf_notes.h>
36ae115bc7Smrj #include <sys/mman.h>
37ae115bc7Smrj #include <sys/stat.h>
381738dd6eSToomas Soome #include <sys/sysmacros.h>
39ae115bc7Smrj #include "sys/multiboot.h"
401738dd6eSToomas Soome #include "sys/multiboot2.h"
41ae115bc7Smrj 
42ae115bc7Smrj static char *pname;
43ae115bc7Smrj static char *fname;
44ae115bc7Smrj static char *image;	/* pointer to the ELF file in memory */
45ae115bc7Smrj 
46ae115bc7Smrj #define	ELFSEEK(offset) ((void *)(image + offset))
47ae115bc7Smrj 
48ae115bc7Smrj /*
491738dd6eSToomas Soome  * Find MB2 header tags for entry and patch it.
501738dd6eSToomas Soome  * The first tag is right after header.
511738dd6eSToomas Soome  */
521738dd6eSToomas Soome static int
patch64_mb2(multiboot2_header_t * mbh2,int file_offset,Elf64_Addr ptload_start,Elf32_Off ptload_offset)531738dd6eSToomas Soome patch64_mb2(multiboot2_header_t *mbh2, int file_offset,
541738dd6eSToomas Soome     Elf64_Addr ptload_start, Elf32_Off ptload_offset)
551738dd6eSToomas Soome {
561738dd6eSToomas Soome 	multiboot_header_tag_t *tagp = mbh2->mb2_tags;
571738dd6eSToomas Soome 	multiboot_header_tag_address_t *mbaddr = NULL;
581738dd6eSToomas Soome 	multiboot_header_tag_entry_address_t *mbentry = NULL;
591738dd6eSToomas Soome 
601738dd6eSToomas Soome 	/*
611738dd6eSToomas Soome 	 * Loop until we get end TAG or we have both tags.
621738dd6eSToomas Soome 	 */
631738dd6eSToomas Soome 	while (tagp->mbh_type != MULTIBOOT_HEADER_TAG_END &&
641738dd6eSToomas Soome 	    (mbaddr == NULL || mbentry == NULL)) {
651738dd6eSToomas Soome 		switch (tagp->mbh_type) {
661738dd6eSToomas Soome 		case MULTIBOOT_HEADER_TAG_ADDRESS:
671738dd6eSToomas Soome 			mbaddr = (multiboot_header_tag_address_t *)tagp;
681738dd6eSToomas Soome 			break;
691738dd6eSToomas Soome 		case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS:
701738dd6eSToomas Soome 			mbentry = (multiboot_header_tag_entry_address_t *)tagp;
711738dd6eSToomas Soome 			break;
721738dd6eSToomas Soome 		}
731738dd6eSToomas Soome 		tagp = (multiboot_header_tag_t *)
741738dd6eSToomas Soome 		    ((uintptr_t)tagp +
751738dd6eSToomas Soome 		    P2ROUNDUP(tagp->mbh_size, MULTIBOOT_TAG_ALIGN));
761738dd6eSToomas Soome 	}
771738dd6eSToomas Soome 
781738dd6eSToomas Soome 	if (mbaddr == NULL || mbentry == NULL) {
791738dd6eSToomas Soome 		(void) fprintf(stderr, "Missing multiboot2 %s tag\n",
801738dd6eSToomas Soome 		    (mbaddr == NULL)? "address" : "entry");
811738dd6eSToomas Soome 		return (1);
821738dd6eSToomas Soome 	}
831738dd6eSToomas Soome 
841738dd6eSToomas Soome 	/* Patch it. */
851738dd6eSToomas Soome 	mbaddr->mbh_load_addr = ptload_start - ptload_offset;
861738dd6eSToomas Soome 	mbaddr->mbh_header_addr = mbaddr->mbh_load_addr + file_offset;
871738dd6eSToomas Soome 	mbentry->mbh_entry_addr = ptload_start;
881738dd6eSToomas Soome 
891738dd6eSToomas Soome #ifdef VERBOSE
901738dd6eSToomas Soome 	(void) printf("  ELF64 MB2 header patched\n");
911738dd6eSToomas Soome 	(void) printf("\tload_addr now:   0x%x\n", mbaddr->mbh_load_addr);
921738dd6eSToomas Soome 	(void) printf("\theader_addr now: 0x%x\n", mbaddr->mbh_header_addr);
931738dd6eSToomas Soome 	(void) printf("\tentry_addr now:  0x%x\n", mbentry->mbh_entry_addr);
941738dd6eSToomas Soome #endif
951738dd6eSToomas Soome 	return (0);
961738dd6eSToomas Soome }
971738dd6eSToomas Soome 
981738dd6eSToomas Soome /*
991738dd6eSToomas Soome  * Patch the load address / entry address for MB1 and MB2 if present.
100ae115bc7Smrj  * Find the physical load address of the 1st PT_LOAD segment.
101ae115bc7Smrj  * Find the amount that e_entry exceeds that amount.
102ae115bc7Smrj  * Now go back and subtract the excess from the p_paddr of the LOAD segment.
103ae115bc7Smrj  */
104d1827f25Srie static int
patch64(Elf64_Ehdr * eh)105ae115bc7Smrj patch64(Elf64_Ehdr *eh)
106ae115bc7Smrj {
107d1827f25Srie 	Elf64_Phdr		*phdr;
108d1827f25Srie 	caddr_t			phdrs = NULL;
109*af0ef7c4SToomas Soome 	unsigned		ndx, mem, mem2;
110d1827f25Srie 	multiboot_header_t	*mbh;
1111738dd6eSToomas Soome 	multiboot2_header_t	*mbh2;
112ae115bc7Smrj 
113d1827f25Srie 	/*
114d1827f25Srie 	 * Verify some ELF basics - this must be an executable with program
115d1827f25Srie 	 * headers.
116d1827f25Srie 	 */
117ae115bc7Smrj 	if (eh->e_type != ET_EXEC) {
118d1827f25Srie 		(void) fprintf(stderr, "%s: %s: not ET_EXEC, e_type = 0x%x\n",
119d1827f25Srie 		    pname, fname, eh->e_type);
120d1827f25Srie 		return (1);
121ae115bc7Smrj 	}
122d1827f25Srie 	if ((eh->e_phnum == 0) || (eh->e_phoff == 0)) {
123d1827f25Srie 		(void) fprintf(stderr, "%s: %s: no program headers\n", pname,
124d1827f25Srie 		    fname);
125d1827f25Srie 		return (1);
126ae115bc7Smrj 	}
127ae115bc7Smrj 
128ae115bc7Smrj 	/*
129ae115bc7Smrj 	 * Get the program headers.
130ae115bc7Smrj 	 */
131d1827f25Srie 	if ((phdrs = ELFSEEK(eh->e_phoff)) == NULL) {
132d1827f25Srie 		(void) fprintf(stderr, "%s: %s: failed to get %d program "
133d1827f25Srie 		    "hdrs\n", pname, fname, eh->e_phnum);
134d1827f25Srie 		return (1);
135ae115bc7Smrj 	}
136ae115bc7Smrj 
137ae115bc7Smrj 	/*
1381738dd6eSToomas Soome 	 * Look for multiboot1 header.  It must be 32-bit aligned and
139ae115bc7Smrj 	 * completely contained in the 1st 8K of the file.
140ae115bc7Smrj 	 */
141d1827f25Srie 	for (mem = 0; mem < 8192 - sizeof (multiboot_header_t); mem += 4) {
142d1827f25Srie 		mbh = ELFSEEK(mem);
143ae115bc7Smrj 		if (mbh->magic == MB_HEADER_MAGIC)
144ae115bc7Smrj 			break;
145ae115bc7Smrj 	}
146ae115bc7Smrj 
147d1827f25Srie 	if (mem >= 8192 - sizeof (multiboot_header_t)) {
148d1827f25Srie 		(void) fprintf(stderr, "%s: %s: Didn't find multiboot header\n",
149d1827f25Srie 		    pname, fname);
150d1827f25Srie 		return (1);
151ae115bc7Smrj 	}
152ae115bc7Smrj 
1531738dd6eSToomas Soome 	/*
1541738dd6eSToomas Soome 	 * Look for multiboot2 header.  It must be 64-bit aligned and
1551738dd6eSToomas Soome 	 * completely contained in the 1st 32K of the file.
1561738dd6eSToomas Soome 	 * We do not require it to be present.
1571738dd6eSToomas Soome 	 */
1581738dd6eSToomas Soome 	ndx = 0;
1591738dd6eSToomas Soome 	for (mem2 = 0;
1601738dd6eSToomas Soome 	    mem2 <= MULTIBOOT_SEARCH - sizeof (multiboot2_header_t);
1611738dd6eSToomas Soome 	    mem2 += MULTIBOOT_HEADER_ALIGN) {
1621738dd6eSToomas Soome 		mbh2 = ELFSEEK(mem2);
1631738dd6eSToomas Soome 		ndx = mbh2->mb2_header_length;
1641738dd6eSToomas Soome 		if (mbh2->mb2_magic == MULTIBOOT2_HEADER_MAGIC)
1651738dd6eSToomas Soome 			break;
1661738dd6eSToomas Soome 		ndx = 0;
1671738dd6eSToomas Soome 	}
1681738dd6eSToomas Soome 
1691738dd6eSToomas Soome 	if (ndx == 0 || mem2 + ndx > MULTIBOOT_SEARCH) {
1701738dd6eSToomas Soome #ifdef VERBOSE
1711738dd6eSToomas Soome 		(void) fprintf(stderr, "%s: %s: Didn't find multiboot2 "
1721738dd6eSToomas Soome 		    "header\n", pname, fname);
1731738dd6eSToomas Soome #endif
1741738dd6eSToomas Soome 		mbh2 = NULL;
1751738dd6eSToomas Soome 	}
1761738dd6eSToomas Soome 
177ae115bc7Smrj 	/*
178ae115bc7Smrj 	 * Find the 1:1 mapped PT_LOAD section
179ae115bc7Smrj 	 */
180d1827f25Srie 	for (ndx = 0; ndx < eh->e_phnum; ndx++) {
181ae115bc7Smrj 		/*LINTED [ELF program header alignment]*/
182d1827f25Srie 		phdr = (Elf64_Phdr *)(phdrs + eh->e_phentsize * ndx);
183ae115bc7Smrj 
184ae115bc7Smrj 		/*
185ae115bc7Smrj 		 * Find the low memory 1:1 PT_LOAD section!
186ae115bc7Smrj 		 */
187ae115bc7Smrj 		if (phdr->p_type != PT_LOAD)
188ae115bc7Smrj 			continue;
189ae115bc7Smrj 
190ae115bc7Smrj 		if (phdr->p_memsz == 0)
191ae115bc7Smrj 			continue;
192ae115bc7Smrj 
193ae115bc7Smrj 		if (phdr->p_paddr != phdr->p_vaddr)
194ae115bc7Smrj 			continue;
195ae115bc7Smrj 
196ae115bc7Smrj 		/*
197d1827f25Srie 		 * Make sure the multiboot header is part of the first PT_LOAD
198d1827f25Srie 		 * segment, and that the executables entry point starts at the
199d1827f25Srie 		 * same segment.
200ae115bc7Smrj 		 */
201d1827f25Srie 		if ((mem < phdr->p_offset) ||
202d1827f25Srie 		    (mem >= (phdr->p_offset + phdr->p_filesz))) {
203d1827f25Srie 			(void) fprintf(stderr, "%s: %s: identity mapped "
204d1827f25Srie 			    "PT_LOAD wasn't 1st PT_LOAD\n", pname, fname);
205d1827f25Srie 			return (1);
206d1827f25Srie 		}
207ae115bc7Smrj 		if (eh->e_entry != phdr->p_paddr) {
208d1827f25Srie 			(void) fprintf(stderr, "%s: %s: entry != paddr\n",
209d1827f25Srie 			    pname, fname);
210d1827f25Srie 			return (1);
211ae115bc7Smrj 		}
212ae115bc7Smrj 
2131738dd6eSToomas Soome 		if (mbh2 != NULL && ((mem2 < phdr->p_offset) ||
2141738dd6eSToomas Soome 		    (mem2 >= (phdr->p_offset + phdr->p_filesz)))) {
2151738dd6eSToomas Soome #ifdef VERBOSE
2161738dd6eSToomas Soome 			(void) fprintf(stderr, "%s: %s: multiboot2 header not"
2171738dd6eSToomas Soome 			    " in 1st PT_LOAD\n", pname, fname);
2181738dd6eSToomas Soome #endif
2191738dd6eSToomas Soome 			mem2 = 0;
2201738dd6eSToomas Soome 			mbh2 = NULL;
2211738dd6eSToomas Soome 		}
2221738dd6eSToomas Soome 
223ae115bc7Smrj 		/*
224d1827f25Srie 		 * Patch the multiboot header fields to get entire file loaded.
225ae115bc7Smrj 		 * Grub uses the MB header for 64 bit loading.
226ae115bc7Smrj 		 */
227ae115bc7Smrj 		mbh->load_addr = phdr->p_paddr - phdr->p_offset;
228ae115bc7Smrj 		mbh->entry_addr = phdr->p_paddr;
229d1827f25Srie 		mbh->header_addr = mbh->load_addr + mem;
230ae115bc7Smrj #ifdef VERBOSE
231d1827f25Srie 		(void) printf("  %s: ELF64 MB header patched\n", fname);
232d1827f25Srie 		(void) printf("\tload_addr now:   0x%x\n", mbh->load_addr);
233d1827f25Srie 		(void) printf("\tentry_addr now:  0x%x\n", mbh->entry_addr);
234d1827f25Srie 		(void) printf("\theader_addr now: 0x%x\n", mbh->header_addr);
235ae115bc7Smrj #endif
2361738dd6eSToomas Soome 		if (mbh2 != NULL)
2371738dd6eSToomas Soome 			return (patch64_mb2(mbh2, mem2, phdr->p_paddr,
2381738dd6eSToomas Soome 			    phdr->p_offset));
239d1827f25Srie 		return (0);
240ae115bc7Smrj 	}
241ae115bc7Smrj 
242d1827f25Srie 	(void) fprintf(stderr, "%s: %s: Didn't find 1:1 mapped PT_LOAD "
243d1827f25Srie 	    "section\n", pname, fname);
244d1827f25Srie 	return (1);
245ae115bc7Smrj }
246ae115bc7Smrj 
247ae115bc7Smrj int
main(int argc,char ** argv)248ae115bc7Smrj main(int argc, char **argv)
249ae115bc7Smrj {
250d1827f25Srie 	int	fd;
251ae115bc7Smrj 	uchar_t *ident;
252d1827f25Srie 	void	*hdr = NULL;
2531738dd6eSToomas Soome 	struct	stat sb;
254ae115bc7Smrj 
255ae115bc7Smrj 	/*
2561738dd6eSToomas Soome 	 * We expect one argument -- the elf file.
257ae115bc7Smrj 	 */
258ae115bc7Smrj 	if (argc != 2) {
259ae115bc7Smrj 		(void) fprintf(stderr, "usage: %s <unix-elf-file>\n", argv[0]);
260d1827f25Srie 		return (1);
261ae115bc7Smrj 	}
262ae115bc7Smrj 
263ae115bc7Smrj 	pname = strrchr(argv[0], '/');
264ae115bc7Smrj 	if (pname == NULL)
265ae115bc7Smrj 		pname = argv[0];
266ae115bc7Smrj 	else
267ae115bc7Smrj 		++pname;
268ae115bc7Smrj 
269ae115bc7Smrj 	fname = argv[1];
270d1827f25Srie 	if ((fd = open(fname, O_RDWR)) < 0) {
271d1827f25Srie 		(void) fprintf(stderr, "%s: open(%s, O_RDWR) failed: %s\n",
272d1827f25Srie 		    pname, fname, strerror(errno));
273d1827f25Srie 		return (1);
274ae115bc7Smrj 	}
275ae115bc7Smrj 
2761738dd6eSToomas Soome 	if (fstat(fd, &sb) != 0) {
2771738dd6eSToomas Soome 		(void) fprintf(stderr, "%s: fstat failed: %s\n",
2781738dd6eSToomas Soome 		    pname, strerror(errno));
2791738dd6eSToomas Soome 		return (1);
2801738dd6eSToomas Soome 	}
2811738dd6eSToomas Soome 
2821738dd6eSToomas Soome 	/* Make sure we have at least MULTIBOOT_SEARCH bytes. */
2831738dd6eSToomas Soome 	if (sb.st_size < MULTIBOOT_SEARCH) {
2841738dd6eSToomas Soome 		(void) fprintf(stderr, "%s: %s is too small for a kernel\n",
2851738dd6eSToomas Soome 		    pname, fname);
2861738dd6eSToomas Soome 		return (1);
2871738dd6eSToomas Soome 	}
2881738dd6eSToomas Soome 
289ae115bc7Smrj 	/*
2901738dd6eSToomas Soome 	 * mmap the 1st 32K -- MB1 header is within first 8k and MB2 header
2911738dd6eSToomas Soome 	 * is within 32k.
292ae115bc7Smrj 	 */
2931738dd6eSToomas Soome 	image = mmap(NULL, MULTIBOOT_SEARCH, PROT_READ | PROT_WRITE,
2941738dd6eSToomas Soome 	    MAP_SHARED, fd, 0);
295ae115bc7Smrj 	if (image == MAP_FAILED) {
296d1827f25Srie 		(void) fprintf(stderr, "%s: mmap() of %s failed: %s\n",
297d1827f25Srie 		    pname, fname, strerror(errno));
298d1827f25Srie 		return (1);
299ae115bc7Smrj 	}
300ae115bc7Smrj 
301ae115bc7Smrj 	ident = ELFSEEK(0);
302ae115bc7Smrj 	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
303ae115bc7Smrj 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
304d1827f25Srie 		(void) fprintf(stderr, "%s: %s: not an ELF file!\n", pname,
305d1827f25Srie 		    fname);
306d1827f25Srie 		return (1);
307ae115bc7Smrj 	}
308ae115bc7Smrj 
309ae115bc7Smrj 	if (ident[EI_CLASS] == ELFCLASS64) {
310ae115bc7Smrj 		hdr = ELFSEEK(0);
311d1827f25Srie 		return (patch64(hdr));
312d1827f25Srie 	}
313d1827f25Srie 	if (ident[EI_CLASS] != ELFCLASS32) {
314ae115bc7Smrj 		(void) fprintf(stderr, "%s: Unknown ELF class 0x%x\n", pname,
315ae115bc7Smrj 		    ident[EI_CLASS]);
316d1827f25Srie 		return (1);
317ae115bc7Smrj 	}
318ae115bc7Smrj 	return (0);
319ae115bc7Smrj }
320