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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <strings.h>
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <sys/inttypes.h>
36 #include <sys/elf.h>
37 #include <sys/elf_notes.h>
38 #include <sys/mman.h>
39 #include <sys/stat.h>
40 #include "sys/multiboot.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  * patch the load address / entry address
50  * Find the physical load address of the 1st PT_LOAD segment.
51  * Find the amount that e_entry exceeds that amount.
52  * Now go back and subtract the excess from the p_paddr of the LOAD segment.
53  */
54 static int
55 patch64(Elf64_Ehdr *eh)
56 {
57 	Elf64_Phdr		*phdr;
58 	caddr_t			phdrs = NULL;
59 	int			ndx, mem;
60 	multiboot_header_t	*mbh;
61 
62 	/*
63 	 * Verify some ELF basics - this must be an executable with program
64 	 * headers.
65 	 */
66 	if (eh->e_type != ET_EXEC) {
67 		(void) fprintf(stderr, "%s: %s: not ET_EXEC, e_type = 0x%x\n",
68 		    pname, fname, eh->e_type);
69 		return (1);
70 	}
71 	if ((eh->e_phnum == 0) || (eh->e_phoff == 0)) {
72 		(void) fprintf(stderr, "%s: %s: no program headers\n", pname,
73 		    fname);
74 		return (1);
75 	}
76 
77 	/*
78 	 * Get the program headers.
79 	 */
80 	if ((phdrs = ELFSEEK(eh->e_phoff)) == NULL) {
81 		(void) fprintf(stderr, "%s: %s: failed to get %d program "
82 		    "hdrs\n", pname, fname, eh->e_phnum);
83 		return (1);
84 	}
85 
86 	/*
87 	 * Look for multiboot header.  It must be 32-bit aligned and
88 	 * completely contained in the 1st 8K of the file.
89 	 */
90 	for (mem = 0; mem < 8192 - sizeof (multiboot_header_t); mem += 4) {
91 		mbh = ELFSEEK(mem);
92 		if (mbh->magic == MB_HEADER_MAGIC)
93 			break;
94 	}
95 
96 	if (mem >= 8192 - sizeof (multiboot_header_t)) {
97 		(void) fprintf(stderr, "%s: %s: Didn't find multiboot header\n",
98 		    pname, fname);
99 		return (1);
100 	}
101 
102 	/*
103 	 * Find the 1:1 mapped PT_LOAD section
104 	 */
105 	for (ndx = 0; ndx < eh->e_phnum; ndx++) {
106 		/*LINTED [ELF program header alignment]*/
107 		phdr = (Elf64_Phdr *)(phdrs + eh->e_phentsize * ndx);
108 
109 		/*
110 		 * Find the low memory 1:1 PT_LOAD section!
111 		 */
112 		if (phdr->p_type != PT_LOAD)
113 			continue;
114 
115 		if (phdr->p_memsz == 0)
116 			continue;
117 
118 		if (phdr->p_paddr != phdr->p_vaddr)
119 			continue;
120 
121 		/*
122 		 * Make sure the multiboot header is part of the first PT_LOAD
123 		 * segment, and that the executables entry point starts at the
124 		 * same segment.
125 		 */
126 		if ((mem < phdr->p_offset) ||
127 		    (mem >= (phdr->p_offset + phdr->p_filesz))) {
128 			(void) fprintf(stderr, "%s: %s: identity mapped "
129 			    "PT_LOAD wasn't 1st PT_LOAD\n", pname, fname);
130 			return (1);
131 		}
132 		if (eh->e_entry != phdr->p_paddr) {
133 			(void) fprintf(stderr, "%s: %s: entry != paddr\n",
134 			    pname, fname);
135 			return (1);
136 		}
137 
138 		/*
139 		 * Patch the multiboot header fields to get entire file loaded.
140 		 * Grub uses the MB header for 64 bit loading.
141 		 */
142 		mbh->load_addr = phdr->p_paddr - phdr->p_offset;
143 		mbh->entry_addr = phdr->p_paddr;
144 		mbh->header_addr = mbh->load_addr + mem;
145 #ifdef VERBOSE
146 		(void) printf("  %s: ELF64 MB header patched\n", fname);
147 		(void) printf("\tload_addr now:   0x%x\n", mbh->load_addr);
148 		(void) printf("\tentry_addr now:  0x%x\n", mbh->entry_addr);
149 		(void) printf("\theader_addr now: 0x%x\n", mbh->header_addr);
150 #endif
151 		return (0);
152 	}
153 
154 	(void) fprintf(stderr, "%s: %s: Didn't find 1:1 mapped PT_LOAD "
155 	    "section\n", pname, fname);
156 	return (1);
157 }
158 
159 int
160 main(int argc, char **argv)
161 {
162 	int	fd;
163 	uchar_t *ident;
164 	void	*hdr = NULL;
165 
166 	/*
167 	 * we expect one argument -- the elf file
168 	 */
169 	if (argc != 2) {
170 		(void) fprintf(stderr, "usage: %s <unix-elf-file>\n", argv[0]);
171 		return (1);
172 	}
173 
174 	pname = strrchr(argv[0], '/');
175 	if (pname == NULL)
176 		pname = argv[0];
177 	else
178 		++pname;
179 
180 	fname = argv[1];
181 	if ((fd = open(fname, O_RDWR)) < 0) {
182 		(void) fprintf(stderr, "%s: open(%s, O_RDWR) failed: %s\n",
183 		    pname, fname, strerror(errno));
184 		return (1);
185 	}
186 
187 	/*
188 	 * mmap just the 1st 8K -- since that's where the GRUB
189 	 * multiboot header must be located.
190 	 */
191 	image = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
192 	if (image == MAP_FAILED) {
193 		(void) fprintf(stderr, "%s: mmap() of %s failed: %s\n",
194 		    pname, fname, strerror(errno));
195 		return (1);
196 	}
197 
198 	ident = ELFSEEK(0);
199 	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
200 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
201 		(void) fprintf(stderr, "%s: %s: not an ELF file!\n", pname,
202 		    fname);
203 		return (1);
204 	}
205 
206 	if (ident[EI_CLASS] == ELFCLASS64) {
207 		hdr = ELFSEEK(0);
208 		return (patch64(hdr));
209 	}
210 	if (ident[EI_CLASS] != ELFCLASS32) {
211 		(void) fprintf(stderr, "%s: Unknown ELF class 0x%x\n", pname,
212 		    ident[EI_CLASS]);
213 		return (1);
214 	}
215 	return (0);
216 }
217