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 	elf_file = (caddr_t)file_image;
88 
89 	allphdrs = NULL;
90 
91 	eh = getehdr();
92 	if (eh == NULL)
93 		dboot_panic("getehdr() failed");
94 
95 	if (eh->e_type != ET_EXEC)
96 		dboot_panic("not ET_EXEC, e_type = 0x%x", eh->e_type);
97 
98 	if (eh->e_phnum == 0 || eh->e_phoff == 0)
99 		dboot_panic("no program headers");
100 
101 	/*
102 	 * Get the program headers.
103 	 */
104 	allphdrs = PGETBYTES(eh->e_phoff);
105 	if (allphdrs == NULL)
106 		dboot_panic("Failed to get program headers e_phnum = %d",
107 		    eh->e_phnum);
108 
109 	/*
110 	 * Get the section headers.
111 	 */
112 	sechdrs = PGETBYTES(eh->e_shoff);
113 	if (sechdrs == NULL)
114 		dboot_panic("Failed to get section headers e_shnum = %d",
115 		    eh->e_shnum);
116 
117 	/*
118 	 * Next look for interesting program headers.
119 	 */
120 	for (i = 0; i < eh->e_phnum; i++) {
121 		/*LINTED [ELF program header alignment]*/
122 		phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i);
123 
124 		/*
125 		 * Dynamically-linked executable.
126 		 * Complain.
127 		 */
128 		if (phdr->p_type == PT_INTERP) {
129 			dboot_printf("warning: PT_INTERP section\n");
130 			continue;
131 		}
132 
133 		/*
134 		 * at this point we only care about PT_LOAD segments
135 		 */
136 		if (phdr->p_type != PT_LOAD)
137 			continue;
138 
139 		if (phdr->p_flags == (PF_R | PF_W) && phdr->p_vaddr == 0) {
140 			dboot_printf("warning: krtld reloc info?\n");
141 			continue;
142 		}
143 
144 		/*
145 		 * If memory size is zero just ignore this header.
146 		 */
147 		if (phdr->p_memsz == 0)
148 			continue;
149 
150 		/*
151 		 * If load address 1:1 then ignore this header.
152 		 */
153 		if (phdr->p_paddr == phdr->p_vaddr) {
154 			if (prom_debug)
155 				dboot_printf("Skipping PT_LOAD segment for "
156 				    "paddr = 0x%lx\n", (ulong_t)phdr->p_paddr);
157 			continue;
158 		}
159 
160 		/*
161 		 * copy the data to kernel area
162 		 */
163 		if (phdr->p_paddr != FOUR_MEG && phdr->p_paddr != 2 * FOUR_MEG)
164 			dboot_panic("Bad paddr for kernel nucleus segment");
165 		src = (uintptr_t)PGETBYTES(phdr->p_offset);
166 		dst = ktext_phys + phdr->p_paddr - FOUR_MEG;
167 		if (prom_debug)
168 			dboot_printf("copying %ld bytes from ELF offset 0x%lx "
169 			    "to physaddr 0x%lx (va=0x%lx)\n",
170 			    (ulong_t)phdr->p_filesz, (ulong_t)phdr->p_offset,
171 			    (ulong_t)dst, (ulong_t)phdr->p_vaddr);
172 		(void) memcpy((void *)(uintptr_t)dst,
173 		    (void *)(uintptr_t)src, (size_t)phdr->p_filesz);
174 
175 		next_addr = dst + phdr->p_filesz;
176 	}
177 
178 
179 	/*
180 	 * Next look for bss
181 	 */
182 	for (i = 0; i < eh->e_shnum; i++) {
183 		shdr = (Elf64_Shdr *)(sechdrs + eh->e_shentsize * i);
184 
185 		/* zero out bss */
186 		if (shdr->sh_type == SHT_NOBITS) {
187 			if (prom_debug)
188 				dboot_printf("zeroing BSS %ld bytes from "
189 				    "physaddr 0x%" PRIx64
190 				    " (end=0x%" PRIx64 ")\n",
191 				    (ulong_t)shdr->sh_size,
192 				    next_addr,
193 				    next_addr + shdr->sh_size);
194 			(void) memset((void *)(uintptr_t)next_addr, 0,
195 			    shdr->sh_size);
196 			break;
197 		}
198 	}
199 
200 	/*
201 	 * Ignore the intepreter (or should we die if there is one??)
202 	 */
203 	return (0);
204 }
205