xref: /onnv-gate/usr/src/tools/mbh_patch/mbh_patch.c (revision 4284:f3c7d94a1537)
13446Smrj /*
23446Smrj  * CDDL HEADER START
33446Smrj  *
43446Smrj  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
73446Smrj  *
83446Smrj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93446Smrj  * or http://www.opensolaris.org/os/licensing.
103446Smrj  * See the License for the specific language governing permissions
113446Smrj  * and limitations under the License.
123446Smrj  *
133446Smrj  * When distributing Covered Code, include this CDDL HEADER in each
143446Smrj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153446Smrj  * If applicable, add the following below this CDDL HEADER, with the
163446Smrj  * fields enclosed by brackets "[]" replaced with your own identifying
173446Smrj  * information: Portions Copyright [yyyy] [name of copyright owner]
183446Smrj  *
193446Smrj  * CDDL HEADER END
203446Smrj  */
213446Smrj 
223446Smrj /*
233446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243446Smrj  * Use is subject to license terms.
253446Smrj  */
263446Smrj 
273446Smrj #pragma ident	"%Z%%M%	%I%	%E% SMI"
283446Smrj 
293446Smrj #include <stdlib.h>
30*4284Srie #include <errno.h>
313446Smrj #include <fcntl.h>
323446Smrj #include <strings.h>
333446Smrj #include <stdio.h>
343446Smrj #include <sys/types.h>
353446Smrj #include <sys/inttypes.h>
363446Smrj #include <sys/elf.h>
373446Smrj #include <sys/elf_notes.h>
383446Smrj #include <sys/mman.h>
393446Smrj #include <sys/stat.h>
403446Smrj #include "sys/multiboot.h"
413446Smrj 
423446Smrj static char *pname;
433446Smrj static char *fname;
443446Smrj static char *image;	/* pointer to the ELF file in memory */
453446Smrj 
463446Smrj #define	ELFSEEK(offset) ((void *)(image + offset))
473446Smrj 
483446Smrj /*
493446Smrj  * patch the load address / entry address
503446Smrj  * Find the physical load address of the 1st PT_LOAD segment.
513446Smrj  * Find the amount that e_entry exceeds that amount.
523446Smrj  * Now go back and subtract the excess from the p_paddr of the LOAD segment.
533446Smrj  */
54*4284Srie static int
patch64(Elf64_Ehdr * eh)553446Smrj patch64(Elf64_Ehdr *eh)
563446Smrj {
57*4284Srie 	Elf64_Phdr		*phdr;
58*4284Srie 	caddr_t			phdrs = NULL;
59*4284Srie 	int			ndx, mem;
60*4284Srie 	multiboot_header_t	*mbh;
613446Smrj 
62*4284Srie 	/*
63*4284Srie 	 * Verify some ELF basics - this must be an executable with program
64*4284Srie 	 * headers.
65*4284Srie 	 */
663446Smrj 	if (eh->e_type != ET_EXEC) {
67*4284Srie 		(void) fprintf(stderr, "%s: %s: not ET_EXEC, e_type = 0x%x\n",
68*4284Srie 		    pname, fname, eh->e_type);
69*4284Srie 		return (1);
703446Smrj 	}
71*4284Srie 	if ((eh->e_phnum == 0) || (eh->e_phoff == 0)) {
72*4284Srie 		(void) fprintf(stderr, "%s: %s: no program headers\n", pname,
73*4284Srie 		    fname);
74*4284Srie 		return (1);
753446Smrj 	}
763446Smrj 
773446Smrj 	/*
783446Smrj 	 * Get the program headers.
793446Smrj 	 */
80*4284Srie 	if ((phdrs = ELFSEEK(eh->e_phoff)) == NULL) {
81*4284Srie 		(void) fprintf(stderr, "%s: %s: failed to get %d program "
82*4284Srie 		    "hdrs\n", pname, fname, eh->e_phnum);
83*4284Srie 		return (1);
843446Smrj 	}
853446Smrj 
863446Smrj 	/*
873446Smrj 	 * Look for multiboot header.  It must be 32-bit aligned and
883446Smrj 	 * completely contained in the 1st 8K of the file.
893446Smrj 	 */
90*4284Srie 	for (mem = 0; mem < 8192 - sizeof (multiboot_header_t); mem += 4) {
91*4284Srie 		mbh = ELFSEEK(mem);
923446Smrj 		if (mbh->magic == MB_HEADER_MAGIC)
933446Smrj 			break;
943446Smrj 	}
953446Smrj 
96*4284Srie 	if (mem >= 8192 - sizeof (multiboot_header_t)) {
97*4284Srie 		(void) fprintf(stderr, "%s: %s: Didn't find multiboot header\n",
98*4284Srie 		    pname, fname);
99*4284Srie 		return (1);
1003446Smrj 	}
1013446Smrj 
1023446Smrj 	/*
1033446Smrj 	 * Find the 1:1 mapped PT_LOAD section
1043446Smrj 	 */
105*4284Srie 	for (ndx = 0; ndx < eh->e_phnum; ndx++) {
1063446Smrj 		/*LINTED [ELF program header alignment]*/
107*4284Srie 		phdr = (Elf64_Phdr *)(phdrs + eh->e_phentsize * ndx);
1083446Smrj 
1093446Smrj 		/*
1103446Smrj 		 * Find the low memory 1:1 PT_LOAD section!
1113446Smrj 		 */
1123446Smrj 		if (phdr->p_type != PT_LOAD)
1133446Smrj 			continue;
1143446Smrj 
1153446Smrj 		if (phdr->p_memsz == 0)
1163446Smrj 			continue;
1173446Smrj 
1183446Smrj 		if (phdr->p_paddr != phdr->p_vaddr)
1193446Smrj 			continue;
1203446Smrj 
121*4284Srie 		/*
122*4284Srie 		 * Make sure the multiboot header is part of the first PT_LOAD
123*4284Srie 		 * segment, and that the executables entry point starts at the
124*4284Srie 		 * same segment.
125*4284Srie 		 */
126*4284Srie 		if ((mem < phdr->p_offset) ||
127*4284Srie 		    (mem >= (phdr->p_offset + phdr->p_filesz))) {
128*4284Srie 			(void) fprintf(stderr, "%s: %s: identity mapped "
129*4284Srie 			    "PT_LOAD wasn't 1st PT_LOAD\n", pname, fname);
130*4284Srie 			return (1);
131*4284Srie 		}
132*4284Srie 		if (eh->e_entry != phdr->p_paddr) {
133*4284Srie 			(void) fprintf(stderr, "%s: %s: entry != paddr\n",
134*4284Srie 			    pname, fname);
135*4284Srie 			return (1);
1363446Smrj 		}
1373446Smrj 
1383446Smrj 		/*
139*4284Srie 		 * Patch the multiboot header fields to get entire file loaded.
1403446Smrj 		 * Grub uses the MB header for 64 bit loading.
1413446Smrj 		 */
1423446Smrj 		mbh->load_addr = phdr->p_paddr - phdr->p_offset;
1433446Smrj 		mbh->entry_addr = phdr->p_paddr;
144*4284Srie 		mbh->header_addr = mbh->load_addr + mem;
1453446Smrj #ifdef VERBOSE
146*4284Srie 		(void) printf("  %s: ELF64 MB header patched\n", fname);
147*4284Srie 		(void) printf("\tload_addr now:   0x%x\n", mbh->load_addr);
148*4284Srie 		(void) printf("\tentry_addr now:  0x%x\n", mbh->entry_addr);
149*4284Srie 		(void) printf("\theader_addr now: 0x%x\n", mbh->header_addr);
1503446Smrj #endif
151*4284Srie 		return (0);
1523446Smrj 	}
1533446Smrj 
154*4284Srie 	(void) fprintf(stderr, "%s: %s: Didn't find 1:1 mapped PT_LOAD "
155*4284Srie 	    "section\n", pname, fname);
156*4284Srie 	return (1);
1573446Smrj }
1583446Smrj 
1593446Smrj int
main(int argc,char ** argv)1603446Smrj main(int argc, char **argv)
1613446Smrj {
162*4284Srie 	int	fd;
1633446Smrj 	uchar_t *ident;
164*4284Srie 	void	*hdr = NULL;
1653446Smrj 
1663446Smrj 	/*
1673446Smrj 	 * we expect one argument -- the elf file
1683446Smrj 	 */
1693446Smrj 	if (argc != 2) {
1703446Smrj 		(void) fprintf(stderr, "usage: %s <unix-elf-file>\n", argv[0]);
171*4284Srie 		return (1);
1723446Smrj 	}
1733446Smrj 
1743446Smrj 	pname = strrchr(argv[0], '/');
1753446Smrj 	if (pname == NULL)
1763446Smrj 		pname = argv[0];
1773446Smrj 	else
1783446Smrj 		++pname;
1793446Smrj 
1803446Smrj 	fname = argv[1];
181*4284Srie 	if ((fd = open(fname, O_RDWR)) < 0) {
182*4284Srie 		(void) fprintf(stderr, "%s: open(%s, O_RDWR) failed: %s\n",
183*4284Srie 		    pname, fname, strerror(errno));
184*4284Srie 		return (1);
1853446Smrj 	}
1863446Smrj 
1873446Smrj 	/*
1883446Smrj 	 * mmap just the 1st 8K -- since that's where the GRUB
1893446Smrj 	 * multiboot header must be located.
1903446Smrj 	 */
1913446Smrj 	image = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1923446Smrj 	if (image == MAP_FAILED) {
193*4284Srie 		(void) fprintf(stderr, "%s: mmap() of %s failed: %s\n",
194*4284Srie 		    pname, fname, strerror(errno));
195*4284Srie 		return (1);
1963446Smrj 	}
1973446Smrj 
1983446Smrj 	ident = ELFSEEK(0);
1993446Smrj 	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
2003446Smrj 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
201*4284Srie 		(void) fprintf(stderr, "%s: %s: not an ELF file!\n", pname,
202*4284Srie 		    fname);
203*4284Srie 		return (1);
2043446Smrj 	}
2053446Smrj 
2063446Smrj 	if (ident[EI_CLASS] == ELFCLASS64) {
2073446Smrj 		hdr = ELFSEEK(0);
208*4284Srie 		return (patch64(hdr));
209*4284Srie 	}
210*4284Srie 	if (ident[EI_CLASS] != ELFCLASS32) {
2113446Smrj 		(void) fprintf(stderr, "%s: Unknown ELF class 0x%x\n", pname,
2123446Smrj 		    ident[EI_CLASS]);
213*4284Srie 		return (1);
2143446Smrj 	}
2153446Smrj 	return (0);
2163446Smrj }
217