1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
31 * Module to load a protected-mode ELF kernel
47 #include <syslinux/loadfile.h>
48 #include <syslinux/movebits.h>
49 #include <syslinux/bootpm.h>
51 /* If we don't have this much memory for the stack, signal failure */
54 static inline void error(const char *msg
)
59 int boot_elf(void *ptr
, size_t len
, char **argv
)
65 struct syslinux_movelist
*ml
= NULL
;
66 struct syslinux_memmap
*mmap
= NULL
, *amap
= NULL
;
67 struct syslinux_pm_regs regs
;
72 char *stack_frame
= NULL
;
73 addr_t stack_frame_size
;
79 memset(®s
, 0, sizeof regs
);
82 * Note: mmap is the memory map (containing free and zeroed regions)
83 * needed by syslinux_shuffle_boot_pm(); amap is a map where we keep
84 * track ourselves which target memory ranges have already been
88 if (len
< sizeof(Elf32_Ehdr
))
91 /* Must be ELF, 32-bit, littleendian, version 1 */
92 if (memcmp(eh
->e_ident
, "\x7f" "ELF\1\1\1", 6))
95 /* Is this a worthwhile test? In particular x86-64 normally
96 would imply ELF64 support, which we could do as long as
97 the addresses are 32-bit addresses, and entry is 32 bits.
98 64-bit addresses would take a lot more work. */
99 if (eh
->e_machine
!= EM_386
&& eh
->e_machine
!= EM_486
&&
100 eh
->e_machine
!= EM_X86_64
)
103 if (eh
->e_version
!= EV_CURRENT
)
106 if (eh
->e_ehsize
< sizeof(Elf32_Ehdr
) || eh
->e_ehsize
>= len
)
109 if (eh
->e_phentsize
< sizeof(Elf32_Phdr
))
115 if (eh
->e_phoff
+ eh
->e_phentsize
* eh
->e_phnum
> len
)
118 mmap
= syslinux_memory_map();
119 amap
= syslinux_dup_memmap(mmap
);
123 dprintf("Initial memory map:\n");
124 syslinux_dump_memmap(mmap
);
126 ph
= (Elf32_Phdr
*) (cptr
+ eh
->e_phoff
);
128 for (i
= 0; i
< eh
->e_phnum
; i
++) {
129 if (ph
->p_type
== PT_LOAD
|| ph
->p_type
== PT_PHDR
) {
130 /* This loads at p_paddr, which is arguably the correct semantics.
131 The SysV spec says that SysV loads at p_vaddr (and thus Linux does,
132 too); that is, however, a major brainfuckage in the spec. */
133 addr_t addr
= ph
->p_paddr
;
134 addr_t msize
= ph
->p_memsz
;
135 addr_t dsize
= min(msize
, ph
->p_filesz
);
137 dprintf("Segment at 0x%08x data 0x%08x len 0x%08x\n",
140 if (syslinux_memmap_type(amap
, addr
, msize
) != SMT_FREE
) {
141 printf("Memory segment at 0x%08x (len 0x%08x) is unavailable\n",
143 goto bail
; /* Memory region unavailable */
146 /* Mark this region as allocated in the available map */
147 if (syslinux_add_memmap(&amap
, addr
, dsize
, SMT_ALLOC
))
151 /* Data present region. Create a move entry for it. */
152 if (syslinux_add_movelist
153 (&ml
, addr
, (addr_t
) cptr
+ ph
->p_offset
, dsize
))
157 /* Zero-filled region. Mark as a zero region in the memory map. */
158 if (syslinux_add_memmap
159 (&mmap
, addr
+ dsize
, msize
- dsize
, SMT_ZERO
))
163 /* Ignore this program header */
166 ph
= (Elf32_Phdr
*) ((char *)ph
+ eh
->e_phentsize
);
169 /* Create the invocation record (initial stack frame) */
172 for (argp
= argv
; *argp
; argp
++) {
173 dprintf("argv[%2d] = \"%s\"\n", argc
, *argp
);
175 argsize
+= strlen(*argp
) + 1;
178 /* We need the argument strings, argument pointers,
179 argc, plus four zero-word terminators. */
180 stack_frame_size
= argsize
+ argc
* sizeof(char *) + 5 * sizeof(long);
181 stack_frame_size
= (stack_frame_size
+ 15) & ~15;
182 stack_frame
= calloc(stack_frame_size
, 1);
186 dprintf("Right before syslinux_memmap_largest()...\n");
187 syslinux_dump_memmap(amap
);
189 if (syslinux_memmap_largest(amap
, SMT_FREE
, &lstart
, &llen
))
190 goto bail
; /* NO free memory?! */
192 if (llen
< stack_frame_size
+ MIN_STACK
+ 16)
193 goto bail
; /* Insufficient memory */
195 /* Initial stack pointer address */
196 stack_pointer
= (lstart
+ llen
- stack_frame_size
) & ~15;
198 dprintf("Stack frame at 0x%08x len 0x%08x\n",
199 stack_pointer
, stack_frame_size
);
201 /* Create the stack frame. sfp is the pointer in current memory for
202 the next argument string, sfa is the address in its final resting place.
203 spp is the pointer into the argument array in current memory. */
204 spp
= (uint32_t *) stack_frame
;
205 sfp
= stack_frame
+ argc
* sizeof(char *) + 5 * sizeof(long);
206 sfa
= stack_pointer
+ argc
* sizeof(char *) + 5 * sizeof(long);
209 for (argp
= argv
; *argp
; argp
++) {
210 int bytes
= strlen(*argp
) + 1; /* Including final null */
212 memcpy(sfp
, *argp
, bytes
);
216 /* Zero fields are aready taken care of by calloc() */
218 /* ... and we'll want to move it into the right place... */
220 if (syslinux_memmap_type(amap
, stack_pointer
, stack_frame_size
)
222 dprintf("Stack frame area not free (how did that happen?)!\n");
223 goto bail
; /* Memory region unavailable */
227 if (syslinux_add_memmap(&amap
, stack_pointer
, stack_frame_size
, SMT_ALLOC
))
230 if (syslinux_add_movelist(&ml
, stack_pointer
, (addr_t
) stack_frame
,
234 memset(®s
, 0, sizeof regs
);
235 regs
.eip
= eh
->e_entry
;
236 regs
.esp
= stack_pointer
;
238 dprintf("Final memory map:\n");
239 syslinux_dump_memmap(mmap
);
241 dprintf("Final available map:\n");
242 syslinux_dump_memmap(amap
);
244 dprintf("Movelist:\n");
245 syslinux_dump_movelist(ml
);
247 /* This should not return... */
248 fputs("Booting...\n", stdout
);
249 syslinux_shuffle_boot_pm(ml
, mmap
, 0, ®s
);
254 syslinux_free_memmap(amap
);
255 syslinux_free_memmap(mmap
);
256 syslinux_free_movelist(ml
);
261 int main(int argc
, char *argv
[])
267 error("Usage: elf.c32 elf_file arguments...\n");
271 if (zloadfile(argv
[1], &data
, &data_len
)) {
272 error("Unable to load file\n");
276 boot_elf(data
, data_len
, &argv
[1]);
277 error("Invalid ELF file or insufficient memory\n");