xref: /titanic_41/usr/src/uts/i86pc/dboot/dboot_elfload.c (revision 5ce5f3670f7934e376808da0d1309924ecf8f9e5)
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 <sys/types.h>
30 #include <sys/inttypes.h>
31 #include <sys/systm.h>
32 #include <sys/elf.h>
33 #include <sys/elf_notes.h>
34 
35 #include <util/memcpy.h>
36 
37 #include "dboot_xboot.h"
38 #include "dboot_elfload.h"
39 #include "dboot_printf.h"
40 
41 static caddr_t elf_file = 0;
42 
43 #define	PGETBYTES(offset)	((void *)(elf_file + (offset)))
44 
45 static void *
46 getehdr(void)
47 {
48 	uchar_t *ident;
49 	void *hdr = NULL;
50 
51 	ident = PGETBYTES(0);
52 	if (ident == NULL)
53 		dboot_panic("Cannot read kernel ELF header\n");
54 
55 	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
56 	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
57 		dboot_panic("not an ELF file!\n");
58 
59 	if (ident[EI_CLASS] == ELFCLASS32)
60 		hdr = PGETBYTES(0);
61 	else if (ident[EI_CLASS] == ELFCLASS64)
62 		hdr = PGETBYTES(0);
63 	else
64 		dboot_panic("Unknown ELF class\n");
65 
66 	return (hdr);
67 }
68 
69 
70 /*
71  * parse the elf file for program information
72  */
73 int
74 dboot_elfload64(uintptr_t file_image)
75 {
76 	Elf64_Ehdr *eh;
77 	Elf64_Phdr *phdr;
78 	caddr_t allphdrs;
79 	int i;
80 	paddr_t src;
81 	paddr_t dst;
82 
83 	elf_file = (caddr_t)file_image;
84 
85 	allphdrs = NULL;
86 
87 	eh = getehdr();
88 	if (eh == NULL)
89 		dboot_panic("getehdr() failed\n");
90 
91 	if (eh->e_type != ET_EXEC)
92 		dboot_panic("not ET_EXEC, e_type = 0x%x\n", eh->e_type);
93 
94 	if (eh->e_phnum == 0 || eh->e_phoff == 0)
95 		dboot_panic("no program headers\n");
96 
97 	/*
98 	 * Get the program headers.
99 	 */
100 	allphdrs = PGETBYTES(eh->e_phoff);
101 	if (allphdrs == NULL)
102 		dboot_panic("Failed to get program headers e_phnum = %d\n",
103 		    eh->e_phnum);
104 
105 	/*
106 	 * Next look for interesting program headers.
107 	 */
108 	for (i = 0; i < eh->e_phnum; i++) {
109 		/*LINTED [ELF program header alignment]*/
110 		phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i);
111 
112 		/*
113 		 * Dynamically-linked executable.
114 		 * Complain.
115 		 */
116 		if (phdr->p_type == PT_INTERP) {
117 			dboot_printf("warning: PT_INTERP section\n");
118 			continue;
119 		}
120 
121 		/*
122 		 * at this point we only care about PT_LOAD segments
123 		 */
124 		if (phdr->p_type != PT_LOAD)
125 			continue;
126 
127 		if (phdr->p_flags == (PF_R | PF_W) && phdr->p_vaddr == 0) {
128 			dboot_printf("warning: krtld reloc info?\n");
129 			continue;
130 		}
131 
132 		/*
133 		 * If memory size is zero just ignore this header.
134 		 */
135 		if (phdr->p_memsz == 0)
136 			continue;
137 
138 		/*
139 		 * If load address 1:1 then ignore this header.
140 		 */
141 		if (phdr->p_paddr == phdr->p_vaddr) {
142 			if (prom_debug)
143 				dboot_printf("Skipping PT_LOAD segment for "
144 				    "paddr = 0x%lx\n", (ulong_t)phdr->p_paddr);
145 			continue;
146 		}
147 
148 		/*
149 		 * copy the data to kernel area
150 		 */
151 		if (phdr->p_paddr != FOUR_MEG && phdr->p_paddr != 2 * FOUR_MEG)
152 			dboot_panic("Bad paddr for kernel nucleus segment\n");
153 		src = (uintptr_t)PGETBYTES(phdr->p_offset);
154 		dst = ktext_phys + phdr->p_paddr - FOUR_MEG;
155 		if (prom_debug)
156 			dboot_printf("copying %lu bytes from ELF offset 0x%lx "
157 			    "to physaddr 0x%lx (va=0x%lx)\n",
158 			    (ulong_t)phdr->p_filesz, (ulong_t)phdr->p_offset,
159 			    (ulong_t)dst, (ulong_t)phdr->p_vaddr);
160 		(void) memcpy((void *)(uintptr_t)dst,
161 		    (void *)(uintptr_t)src, (size_t)phdr->p_filesz);
162 	}
163 
164 	/*
165 	 * Ignore the intepreter (or should we die if there is one??)
166 	 */
167 	return (0);
168 }
169