1ae115bc7Smrj /*
2ae115bc7Smrj * CDDL HEADER START
3ae115bc7Smrj *
4ae115bc7Smrj * The contents of this file are subject to the terms of the
5ae115bc7Smrj * Common Development and Distribution License (the "License").
6ae115bc7Smrj * You may not use this file except in compliance with the License.
7ae115bc7Smrj *
8ae115bc7Smrj * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ae115bc7Smrj * or http://www.opensolaris.org/os/licensing.
10ae115bc7Smrj * See the License for the specific language governing permissions
11ae115bc7Smrj * and limitations under the License.
12ae115bc7Smrj *
13ae115bc7Smrj * When distributing Covered Code, include this CDDL HEADER in each
14ae115bc7Smrj * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ae115bc7Smrj * If applicable, add the following below this CDDL HEADER, with the
16ae115bc7Smrj * fields enclosed by brackets "[]" replaced with your own identifying
17ae115bc7Smrj * information: Portions Copyright [yyyy] [name of copyright owner]
18ae115bc7Smrj *
19ae115bc7Smrj * CDDL HEADER END
20ae115bc7Smrj */
21ae115bc7Smrj
22ae115bc7Smrj /*
23*19397407SSherry Moore * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24ae115bc7Smrj * Use is subject to license terms.
25ae115bc7Smrj */
26ae115bc7Smrj
27ae115bc7Smrj
28ae115bc7Smrj #include <sys/types.h>
29ae115bc7Smrj #include <sys/inttypes.h>
30ae115bc7Smrj #include <sys/systm.h>
31ae115bc7Smrj #include <sys/elf.h>
32ae115bc7Smrj #include <sys/elf_notes.h>
33ae115bc7Smrj
34ae115bc7Smrj #include <util/memcpy.h>
35ae115bc7Smrj
36ae115bc7Smrj #include "dboot_xboot.h"
37ae115bc7Smrj #include "dboot_elfload.h"
38ae115bc7Smrj #include "dboot_printf.h"
39ae115bc7Smrj
40ae115bc7Smrj static caddr_t elf_file = 0;
41ae115bc7Smrj
42ae115bc7Smrj #define PGETBYTES(offset) ((void *)(elf_file + (offset)))
43ae115bc7Smrj
44ae115bc7Smrj static void *
getehdr(void)45ae115bc7Smrj getehdr(void)
46ae115bc7Smrj {
47ae115bc7Smrj uchar_t *ident;
48ae115bc7Smrj void *hdr = NULL;
49ae115bc7Smrj
50ae115bc7Smrj ident = PGETBYTES(0);
51ae115bc7Smrj if (ident == NULL)
52843e1988Sjohnlev dboot_panic("Cannot read kernel ELF header");
53ae115bc7Smrj
54ae115bc7Smrj if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
55ae115bc7Smrj ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
56843e1988Sjohnlev dboot_panic("not an ELF file!");
57ae115bc7Smrj
58ae115bc7Smrj if (ident[EI_CLASS] == ELFCLASS32)
59ae115bc7Smrj hdr = PGETBYTES(0);
60ae115bc7Smrj else if (ident[EI_CLASS] == ELFCLASS64)
61ae115bc7Smrj hdr = PGETBYTES(0);
62ae115bc7Smrj else
63843e1988Sjohnlev dboot_panic("Unknown ELF class");
64ae115bc7Smrj
65ae115bc7Smrj return (hdr);
66ae115bc7Smrj }
67ae115bc7Smrj
68ae115bc7Smrj
69ae115bc7Smrj /*
70ae115bc7Smrj * parse the elf file for program information
71ae115bc7Smrj */
72ae115bc7Smrj int
dboot_elfload64(uintptr_t file_image)73ae115bc7Smrj dboot_elfload64(uintptr_t file_image)
74ae115bc7Smrj {
75ae115bc7Smrj Elf64_Ehdr *eh;
76ae115bc7Smrj Elf64_Phdr *phdr;
77*19397407SSherry Moore Elf64_Shdr *shdr;
78*19397407SSherry Moore caddr_t allphdrs, sechdrs;
79ae115bc7Smrj int i;
80ae115bc7Smrj paddr_t src;
81ae115bc7Smrj paddr_t dst;
82*19397407SSherry Moore paddr_t next_addr;
83ae115bc7Smrj
84ae115bc7Smrj elf_file = (caddr_t)file_image;
85ae115bc7Smrj
86ae115bc7Smrj allphdrs = NULL;
87ae115bc7Smrj
88ae115bc7Smrj eh = getehdr();
89ae115bc7Smrj if (eh == NULL)
90843e1988Sjohnlev dboot_panic("getehdr() failed");
91ae115bc7Smrj
92ae115bc7Smrj if (eh->e_type != ET_EXEC)
93843e1988Sjohnlev dboot_panic("not ET_EXEC, e_type = 0x%x", eh->e_type);
94ae115bc7Smrj
95ae115bc7Smrj if (eh->e_phnum == 0 || eh->e_phoff == 0)
96843e1988Sjohnlev dboot_panic("no program headers");
97ae115bc7Smrj
98ae115bc7Smrj /*
99ae115bc7Smrj * Get the program headers.
100ae115bc7Smrj */
101ae115bc7Smrj allphdrs = PGETBYTES(eh->e_phoff);
102ae115bc7Smrj if (allphdrs == NULL)
103843e1988Sjohnlev dboot_panic("Failed to get program headers e_phnum = %d",
104ae115bc7Smrj eh->e_phnum);
105ae115bc7Smrj
106ae115bc7Smrj /*
107*19397407SSherry Moore * Get the section headers.
108*19397407SSherry Moore */
109*19397407SSherry Moore sechdrs = PGETBYTES(eh->e_shoff);
110*19397407SSherry Moore if (sechdrs == NULL)
111*19397407SSherry Moore dboot_panic("Failed to get section headers e_shnum = %d",
112*19397407SSherry Moore eh->e_shnum);
113*19397407SSherry Moore
114*19397407SSherry Moore /*
115ae115bc7Smrj * Next look for interesting program headers.
116ae115bc7Smrj */
117ae115bc7Smrj for (i = 0; i < eh->e_phnum; i++) {
118ae115bc7Smrj /*LINTED [ELF program header alignment]*/
119ae115bc7Smrj phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i);
120ae115bc7Smrj
121ae115bc7Smrj /*
122ae115bc7Smrj * Dynamically-linked executable.
123ae115bc7Smrj * Complain.
124ae115bc7Smrj */
125ae115bc7Smrj if (phdr->p_type == PT_INTERP) {
126ae115bc7Smrj dboot_printf("warning: PT_INTERP section\n");
127ae115bc7Smrj continue;
128ae115bc7Smrj }
129ae115bc7Smrj
130ae115bc7Smrj /*
131ae115bc7Smrj * at this point we only care about PT_LOAD segments
132ae115bc7Smrj */
133ae115bc7Smrj if (phdr->p_type != PT_LOAD)
134ae115bc7Smrj continue;
135ae115bc7Smrj
136ae115bc7Smrj if (phdr->p_flags == (PF_R | PF_W) && phdr->p_vaddr == 0) {
137ae115bc7Smrj dboot_printf("warning: krtld reloc info?\n");
138ae115bc7Smrj continue;
139ae115bc7Smrj }
140ae115bc7Smrj
141ae115bc7Smrj /*
142ae115bc7Smrj * If memory size is zero just ignore this header.
143ae115bc7Smrj */
144ae115bc7Smrj if (phdr->p_memsz == 0)
145ae115bc7Smrj continue;
146ae115bc7Smrj
147ae115bc7Smrj /*
148ae115bc7Smrj * If load address 1:1 then ignore this header.
149ae115bc7Smrj */
150ae115bc7Smrj if (phdr->p_paddr == phdr->p_vaddr) {
151ae115bc7Smrj if (prom_debug)
152ae115bc7Smrj dboot_printf("Skipping PT_LOAD segment for "
153ae115bc7Smrj "paddr = 0x%lx\n", (ulong_t)phdr->p_paddr);
154ae115bc7Smrj continue;
155ae115bc7Smrj }
156ae115bc7Smrj
157ae115bc7Smrj /*
158ae115bc7Smrj * copy the data to kernel area
159ae115bc7Smrj */
160ae115bc7Smrj if (phdr->p_paddr != FOUR_MEG && phdr->p_paddr != 2 * FOUR_MEG)
161843e1988Sjohnlev dboot_panic("Bad paddr for kernel nucleus segment");
162ae115bc7Smrj src = (uintptr_t)PGETBYTES(phdr->p_offset);
163ae115bc7Smrj dst = ktext_phys + phdr->p_paddr - FOUR_MEG;
164ae115bc7Smrj if (prom_debug)
165adb91f47Srscott dboot_printf("copying %ld bytes from ELF offset 0x%lx "
166ae115bc7Smrj "to physaddr 0x%lx (va=0x%lx)\n",
167ae115bc7Smrj (ulong_t)phdr->p_filesz, (ulong_t)phdr->p_offset,
168ae115bc7Smrj (ulong_t)dst, (ulong_t)phdr->p_vaddr);
169ae115bc7Smrj (void) memcpy((void *)(uintptr_t)dst,
170ae115bc7Smrj (void *)(uintptr_t)src, (size_t)phdr->p_filesz);
171*19397407SSherry Moore
172*19397407SSherry Moore next_addr = dst + phdr->p_filesz;
173*19397407SSherry Moore }
174*19397407SSherry Moore
175*19397407SSherry Moore
176*19397407SSherry Moore /*
177*19397407SSherry Moore * Next look for bss
178*19397407SSherry Moore */
179*19397407SSherry Moore for (i = 0; i < eh->e_shnum; i++) {
180*19397407SSherry Moore shdr = (Elf64_Shdr *)(sechdrs + eh->e_shentsize * i);
181*19397407SSherry Moore
182*19397407SSherry Moore /* zero out bss */
183*19397407SSherry Moore if (shdr->sh_type == SHT_NOBITS) {
184*19397407SSherry Moore (void) memset((void *)(uintptr_t)next_addr, 0,
185*19397407SSherry Moore shdr->sh_size);
186*19397407SSherry Moore break;
187*19397407SSherry Moore }
188ae115bc7Smrj }
189ae115bc7Smrj
190ae115bc7Smrj /*
191ae115bc7Smrj * Ignore the intepreter (or should we die if there is one??)
192ae115bc7Smrj */
193ae115bc7Smrj return (0);
194ae115bc7Smrj }
195