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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #include <sys/types.h>
29 #include <sys/inttypes.h>
30 #include <sys/systm.h>
32 #include <sys/elf_notes.h>
34 #include <util/memcpy.h>
36 #include "dboot_xboot.h"
37 #include "dboot_elfload.h"
38 #include "dboot_printf.h"
40 static caddr_t elf_file
= 0;
42 #define PGETBYTES(offset) ((void *)(elf_file + (offset)))
52 dboot_panic("Cannot read kernel ELF header");
54 if (ident
[EI_MAG0
] != ELFMAG0
|| ident
[EI_MAG1
] != ELFMAG1
||
55 ident
[EI_MAG2
] != ELFMAG2
|| ident
[EI_MAG3
] != ELFMAG3
)
56 dboot_panic("not an ELF file!");
58 if (ident
[EI_CLASS
] == ELFCLASS32
)
60 else if (ident
[EI_CLASS
] == ELFCLASS64
)
63 dboot_panic("Unknown ELF class");
70 * parse the elf file for program information
73 dboot_elfload64(uintptr_t file_image
)
78 caddr_t allphdrs
, sechdrs
;
84 elf_file
= (caddr_t
)file_image
;
90 dboot_panic("getehdr() failed");
92 if (eh
->e_type
!= ET_EXEC
)
93 dboot_panic("not ET_EXEC, e_type = 0x%x", eh
->e_type
);
95 if (eh
->e_phnum
== 0 || eh
->e_phoff
== 0)
96 dboot_panic("no program headers");
99 * Get the program headers.
101 allphdrs
= PGETBYTES(eh
->e_phoff
);
102 if (allphdrs
== NULL
)
103 dboot_panic("Failed to get program headers e_phnum = %d",
107 * Get the section headers.
109 sechdrs
= PGETBYTES(eh
->e_shoff
);
111 dboot_panic("Failed to get section headers e_shnum = %d",
115 * Next look for interesting program headers.
117 for (i
= 0; i
< eh
->e_phnum
; i
++) {
118 /*LINTED [ELF program header alignment]*/
119 phdr
= (Elf64_Phdr
*)(allphdrs
+ eh
->e_phentsize
* i
);
122 * Dynamically-linked executable.
125 if (phdr
->p_type
== PT_INTERP
) {
126 dboot_printf("warning: PT_INTERP section\n");
131 * at this point we only care about PT_LOAD segments
133 if (phdr
->p_type
!= PT_LOAD
)
136 if (phdr
->p_flags
== (PF_R
| PF_W
) && phdr
->p_vaddr
== 0) {
137 dboot_printf("warning: krtld reloc info?\n");
142 * If memory size is zero just ignore this header.
144 if (phdr
->p_memsz
== 0)
148 * If load address 1:1 then ignore this header.
150 if (phdr
->p_paddr
== phdr
->p_vaddr
) {
152 dboot_printf("Skipping PT_LOAD segment for "
153 "paddr = 0x%lx\n", (ulong_t
)phdr
->p_paddr
);
158 * copy the data to kernel area
160 if (phdr
->p_paddr
!= FOUR_MEG
&& phdr
->p_paddr
!= 2 * FOUR_MEG
)
161 dboot_panic("Bad paddr for kernel nucleus segment");
162 src
= (uintptr_t)PGETBYTES(phdr
->p_offset
);
163 dst
= ktext_phys
+ phdr
->p_paddr
- FOUR_MEG
;
165 dboot_printf("copying %ld bytes from ELF offset 0x%lx "
166 "to physaddr 0x%lx (va=0x%lx)\n",
167 (ulong_t
)phdr
->p_filesz
, (ulong_t
)phdr
->p_offset
,
168 (ulong_t
)dst
, (ulong_t
)phdr
->p_vaddr
);
169 (void) memcpy((void *)(uintptr_t)dst
,
170 (void *)(uintptr_t)src
, (size_t)phdr
->p_filesz
);
172 next_addr
= dst
+ phdr
->p_filesz
;
179 for (i
= 0; i
< eh
->e_shnum
; i
++) {
180 shdr
= (Elf64_Shdr
*)(sechdrs
+ eh
->e_shentsize
* i
);
183 if (shdr
->sh_type
== SHT_NOBITS
) {
185 dboot_printf("zeroing BSS %ld bytes from "
186 "physaddr 0x%llx (end=0x%llx)\n",
187 (ulong_t
)shdr
->sh_size
,
188 (long long unsigned)next_addr
,
189 next_addr
+ shdr
->sh_size
);
190 (void) memset((void *)(uintptr_t)next_addr
, 0,
197 * Ignore the intepreter (or should we die if there is one??)