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