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 /*
2319397407SSherry Moore * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24ae115bc7Smrj * Use is subject to license terms.
25ae115bc7Smrj */
26ae115bc7Smrj
276554ec17SJohn Levon /*
286554ec17SJohn Levon * Copyright 2020 Joyent, Inc.
296554ec17SJohn Levon */
30ae115bc7Smrj
31ae115bc7Smrj #include <sys/types.h>
32ae115bc7Smrj #include <sys/inttypes.h>
33ae115bc7Smrj #include <sys/systm.h>
34ae115bc7Smrj #include <sys/elf.h>
35ae115bc7Smrj #include <sys/elf_notes.h>
36ae115bc7Smrj
37ae115bc7Smrj #include <util/memcpy.h>
38ae115bc7Smrj
39ae115bc7Smrj #include "dboot_xboot.h"
40ae115bc7Smrj #include "dboot_elfload.h"
41ae115bc7Smrj #include "dboot_printf.h"
42ae115bc7Smrj
43ae115bc7Smrj static caddr_t elf_file = 0;
44ae115bc7Smrj
45ae115bc7Smrj #define PGETBYTES(offset) ((void *)(elf_file + (offset)))
46ae115bc7Smrj
47ae115bc7Smrj static void *
getehdr(void)48ae115bc7Smrj getehdr(void)
49ae115bc7Smrj {
50ae115bc7Smrj uchar_t *ident;
51ae115bc7Smrj void *hdr = NULL;
52ae115bc7Smrj
53ae115bc7Smrj ident = PGETBYTES(0);
54ae115bc7Smrj if (ident == NULL)
55843e1988Sjohnlev dboot_panic("Cannot read kernel ELF header");
56ae115bc7Smrj
57ae115bc7Smrj if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
58ae115bc7Smrj ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3)
59843e1988Sjohnlev dboot_panic("not an ELF file!");
60ae115bc7Smrj
61ae115bc7Smrj if (ident[EI_CLASS] == ELFCLASS32)
62ae115bc7Smrj hdr = PGETBYTES(0);
63ae115bc7Smrj else if (ident[EI_CLASS] == ELFCLASS64)
64ae115bc7Smrj hdr = PGETBYTES(0);
65ae115bc7Smrj else
66843e1988Sjohnlev dboot_panic("Unknown ELF class");
67ae115bc7Smrj
68ae115bc7Smrj return (hdr);
69ae115bc7Smrj }
70ae115bc7Smrj
71ae115bc7Smrj
72ae115bc7Smrj /*
73ae115bc7Smrj * parse the elf file for program information
74ae115bc7Smrj */
75ae115bc7Smrj int
dboot_elfload64(uintptr_t file_image)76ae115bc7Smrj dboot_elfload64(uintptr_t file_image)
77ae115bc7Smrj {
78ae115bc7Smrj Elf64_Ehdr *eh;
79ae115bc7Smrj Elf64_Phdr *phdr;
8019397407SSherry Moore Elf64_Shdr *shdr;
8119397407SSherry Moore caddr_t allphdrs, sechdrs;
82ae115bc7Smrj int i;
83ae115bc7Smrj paddr_t src;
84ae115bc7Smrj paddr_t dst;
8519397407SSherry Moore paddr_t next_addr;
86ae115bc7Smrj
87*584b574aSToomas Soome next_addr = 0;
88ae115bc7Smrj elf_file = (caddr_t)file_image;
89ae115bc7Smrj
90ae115bc7Smrj allphdrs = NULL;
91ae115bc7Smrj
92ae115bc7Smrj eh = getehdr();
93ae115bc7Smrj if (eh == NULL)
94843e1988Sjohnlev dboot_panic("getehdr() failed");
95ae115bc7Smrj
96ae115bc7Smrj if (eh->e_type != ET_EXEC)
97843e1988Sjohnlev dboot_panic("not ET_EXEC, e_type = 0x%x", eh->e_type);
98ae115bc7Smrj
99ae115bc7Smrj if (eh->e_phnum == 0 || eh->e_phoff == 0)
100843e1988Sjohnlev dboot_panic("no program headers");
101ae115bc7Smrj
102ae115bc7Smrj /*
103ae115bc7Smrj * Get the program headers.
104ae115bc7Smrj */
105ae115bc7Smrj allphdrs = PGETBYTES(eh->e_phoff);
106ae115bc7Smrj if (allphdrs == NULL)
107843e1988Sjohnlev dboot_panic("Failed to get program headers e_phnum = %d",
108ae115bc7Smrj eh->e_phnum);
109ae115bc7Smrj
11019397407SSherry Moore /*
11119397407SSherry Moore * Get the section headers.
11219397407SSherry Moore */
11319397407SSherry Moore sechdrs = PGETBYTES(eh->e_shoff);
11419397407SSherry Moore if (sechdrs == NULL)
11519397407SSherry Moore dboot_panic("Failed to get section headers e_shnum = %d",
11619397407SSherry Moore eh->e_shnum);
11719397407SSherry Moore
118ae115bc7Smrj /*
119ae115bc7Smrj * Next look for interesting program headers.
120ae115bc7Smrj */
121ae115bc7Smrj for (i = 0; i < eh->e_phnum; i++) {
122ae115bc7Smrj /*LINTED [ELF program header alignment]*/
123ae115bc7Smrj phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i);
124ae115bc7Smrj
125ae115bc7Smrj /*
126ae115bc7Smrj * Dynamically-linked executable.
127ae115bc7Smrj * Complain.
128ae115bc7Smrj */
129ae115bc7Smrj if (phdr->p_type == PT_INTERP) {
130ae115bc7Smrj dboot_printf("warning: PT_INTERP section\n");
131ae115bc7Smrj continue;
132ae115bc7Smrj }
133ae115bc7Smrj
134ae115bc7Smrj /*
135ae115bc7Smrj * at this point we only care about PT_LOAD segments
136ae115bc7Smrj */
137ae115bc7Smrj if (phdr->p_type != PT_LOAD)
138ae115bc7Smrj continue;
139ae115bc7Smrj
140ae115bc7Smrj if (phdr->p_flags == (PF_R | PF_W) && phdr->p_vaddr == 0) {
141ae115bc7Smrj dboot_printf("warning: krtld reloc info?\n");
142ae115bc7Smrj continue;
143ae115bc7Smrj }
144ae115bc7Smrj
145ae115bc7Smrj /*
146ae115bc7Smrj * If memory size is zero just ignore this header.
147ae115bc7Smrj */
148ae115bc7Smrj if (phdr->p_memsz == 0)
149ae115bc7Smrj continue;
150ae115bc7Smrj
151ae115bc7Smrj /*
152ae115bc7Smrj * If load address 1:1 then ignore this header.
153ae115bc7Smrj */
154ae115bc7Smrj if (phdr->p_paddr == phdr->p_vaddr) {
155ae115bc7Smrj if (prom_debug)
156ae115bc7Smrj dboot_printf("Skipping PT_LOAD segment for "
157ae115bc7Smrj "paddr = 0x%lx\n", (ulong_t)phdr->p_paddr);
158ae115bc7Smrj continue;
159ae115bc7Smrj }
160ae115bc7Smrj
161ae115bc7Smrj /*
162ae115bc7Smrj * copy the data to kernel area
163ae115bc7Smrj */
164ae115bc7Smrj if (phdr->p_paddr != FOUR_MEG && phdr->p_paddr != 2 * FOUR_MEG)
165843e1988Sjohnlev dboot_panic("Bad paddr for kernel nucleus segment");
166ae115bc7Smrj src = (uintptr_t)PGETBYTES(phdr->p_offset);
167ae115bc7Smrj dst = ktext_phys + phdr->p_paddr - FOUR_MEG;
168ae115bc7Smrj if (prom_debug)
169adb91f47Srscott dboot_printf("copying %ld bytes from ELF offset 0x%lx "
170ae115bc7Smrj "to physaddr 0x%lx (va=0x%lx)\n",
171ae115bc7Smrj (ulong_t)phdr->p_filesz, (ulong_t)phdr->p_offset,
172ae115bc7Smrj (ulong_t)dst, (ulong_t)phdr->p_vaddr);
173ae115bc7Smrj (void) memcpy((void *)(uintptr_t)dst,
174ae115bc7Smrj (void *)(uintptr_t)src, (size_t)phdr->p_filesz);
17519397407SSherry Moore
17619397407SSherry Moore next_addr = dst + phdr->p_filesz;
17719397407SSherry Moore }
17819397407SSherry Moore
17919397407SSherry Moore
18019397407SSherry Moore /*
18119397407SSherry Moore * Next look for bss
18219397407SSherry Moore */
18319397407SSherry Moore for (i = 0; i < eh->e_shnum; i++) {
18419397407SSherry Moore shdr = (Elf64_Shdr *)(sechdrs + eh->e_shentsize * i);
18519397407SSherry Moore
18619397407SSherry Moore /* zero out bss */
18719397407SSherry Moore if (shdr->sh_type == SHT_NOBITS) {
188d43bd0beSToomas Soome if (prom_debug)
189101bc1d0SJohn Levon dboot_printf("zeroing BSS %lu bytes from "
1907e6ac639SToomas Soome "physaddr 0x%" PRIx64
1917e6ac639SToomas Soome " (end=0x%" PRIx64 ")\n",
192d43bd0beSToomas Soome (ulong_t)shdr->sh_size,
1937e6ac639SToomas Soome next_addr,
194d43bd0beSToomas Soome next_addr + shdr->sh_size);
19519397407SSherry Moore (void) memset((void *)(uintptr_t)next_addr, 0,
19619397407SSherry Moore shdr->sh_size);
19719397407SSherry Moore break;
19819397407SSherry Moore }
199ae115bc7Smrj }
200ae115bc7Smrj
201ae115bc7Smrj /*
202ae115bc7Smrj * Ignore the intepreter (or should we die if there is one??)
203ae115bc7Smrj */
204ae115bc7Smrj return (0);
205ae115bc7Smrj }
206