0a71d4c96843b163320ba7407d579229799cc190
[profile/ivi/syslinux.git] / com32 / mboot / map.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4  *   Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
5  *
6  *   Permission is hereby granted, free of charge, to any person
7  *   obtaining a copy of this software and associated documentation
8  *   files (the "Software"), to deal in the Software without
9  *   restriction, including without limitation the rights to use,
10  *   copy, modify, merge, publish, distribute, sublicense, and/or
11  *   sell copies of the Software, and to permit persons to whom
12  *   the Software is furnished to do so, subject to the following
13  *   conditions:
14  *
15  *   The above copyright notice and this permission notice shall
16  *   be included in all copies or substantial portions of the Software.
17  *
18  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * ----------------------------------------------------------------------- */
28
29 /*
30  * map.c
31  *
32  * Functions that deal with the memory map of various objects
33  */
34
35 #include "mboot.h"
36
37 static struct syslinux_movelist *ml = NULL;
38 static struct syslinux_memmap *mmap = NULL, *amap = NULL;
39 static addr_t mboot_high_water_mark = 0x100000;
40
41 /*
42  * Note: although there is no such thing in the spec, at least Xen makes
43  * assumptions as to where in the memory space Grub would have loaded
44  * certain things.  To support that, if "high" is set, then allocate this
45  * at an address strictly above any previous allocations.
46  *
47  * As a precaution, this also pads the data with zero up to the next
48  * alignment datum.
49  */
50 addr_t map_data(const void *data, size_t len, size_t align, int flags)
51 {
52     addr_t start = (flags & MAP_HIGH) ? mboot_high_water_mark : 0x2000;
53     addr_t pad = (flags & MAP_NOPAD) ? 0 : -len & (align - 1);
54     addr_t xlen = len + pad;
55
56     if (syslinux_memmap_find(amap, SMT_FREE, &start, &xlen, align) ||
57         syslinux_add_memmap(&amap, start, len + pad, SMT_ALLOC) ||
58         syslinux_add_movelist(&ml, start, (addr_t) data, len) ||
59         (pad && syslinux_add_memmap(&mmap, start + len, pad, SMT_ZERO))) {
60         printf("Cannot map %zu bytes\n", len + pad);
61         return 0;
62     }
63
64     dprintf("Mapping 0x%08x bytes (%#x pad) at 0x%08x\n", len, pad, start);
65
66     if (start + len + pad > mboot_high_water_mark)
67         mboot_high_water_mark = start + len + pad;
68
69     return start;
70 }
71
72 addr_t map_string(const char *string)
73 {
74     if (!string)
75         return 0;
76     else
77         return map_data(string, strlen(string) + 1, 1, 0);
78 }
79
80 int init_map(void)
81 {
82     /*
83      * Note: mmap is the memory map (containing free and zeroed regions)
84      * needed by syslinux_shuffle_boot_pm(); amap is a map where we keep
85      * track ourselves which target memory ranges have already been
86      * allocated.
87      */
88     mmap = syslinux_memory_map();
89     amap = syslinux_dup_memmap(mmap);
90     if (!mmap || !amap) {
91         error("Failed to allocate initial memory map!\n");
92         return -1;
93     }
94 #if DEBUG
95     dprintf("Initial memory map:\n");
96     syslinux_dump_memmap(stdout, mmap);
97 #endif
98
99     return 0;
100 }
101
102 struct multiboot_header *map_image(void *ptr, size_t len)
103 {
104     struct multiboot_header *mbh;
105     int mbh_len;
106     char *cptr = ptr;
107     Elf32_Ehdr *eh = ptr;
108     Elf32_Phdr *ph;
109     Elf32_Shdr *sh;
110     unsigned int i, mbh_offset;
111     uint32_t bad_flags;
112
113     /*
114      * Search for the multiboot header...
115      */
116     mbh_len = 0;
117     for (mbh_offset = 0; mbh_offset < MULTIBOOT_SEARCH; mbh_offset += 4) {
118         mbh = (struct multiboot_header *)((char *)ptr + mbh_offset);
119         if (mbh->magic != MULTIBOOT_MAGIC)
120             continue;
121         if (mbh->magic + mbh->flags + mbh->checksum)
122             continue;
123         if (mbh->flags & MULTIBOOT_VIDEO_MODE)
124             mbh_len = 48;
125         else if (mbh->flags & MULTIBOOT_AOUT_KLUDGE)
126             mbh_len = 32;
127         else
128             mbh_len = 12;
129
130         if (mbh_offset + mbh_len > len)
131             mbh_len = 0;        /* Invalid... */
132         else
133             break;              /* Found something... */
134     }
135
136     if (mbh_len) {
137         bad_flags = mbh->flags & MULTIBOOT_UNSUPPORTED;
138         if (bad_flags) {
139             printf("Unsupported Multiboot flags set: %#x\n", bad_flags);
140             return NULL;
141         }
142     }
143
144     if (len < sizeof(Elf32_Ehdr) ||
145         memcmp(eh->e_ident, "\x7f" "ELF\1\1\1", 6) ||
146         (eh->e_machine != EM_386 && eh->e_machine != EM_486 &&
147          eh->e_machine != EM_X86_64) ||
148         eh->e_version != EV_CURRENT ||
149         eh->e_ehsize < sizeof(Elf32_Ehdr) || eh->e_ehsize >= len ||
150         eh->e_phentsize < sizeof(Elf32_Phdr) ||
151         !eh->e_phnum || eh->e_phoff + eh->e_phentsize * eh->e_phnum > len)
152         eh = NULL;              /* No valid ELF header found */
153
154     /* Is this a Solaris kernel? */
155     if (!set.solaris && eh && kernel_is_solaris(eh))
156         opt.solaris = true;
157
158     /*
159      * Note: the Multiboot Specification implies that AOUT_KLUDGE should
160      * have precedence over the ELF header.  However, Grub disagrees, and
161      * Grub is "the reference bootloader" for the Multiboot Specification.
162      * This is insane, since it makes the AOUT_KLUDGE bit functionally
163      * useless, but at least Solaris apparently depends on this behavior.
164      */
165     if (eh && !(opt.aout && mbh_len && (mbh->flags & MULTIBOOT_AOUT_KLUDGE))) {
166         regs.eip = eh->e_entry; /* Can be overridden further down... */
167
168         ph = (Elf32_Phdr *) (cptr + eh->e_phoff);
169
170         for (i = 0; i < eh->e_phnum; i++) {
171             if (ph->p_type == PT_LOAD || ph->p_type == PT_PHDR) {
172                 /*
173                  * This loads at p_paddr, which matches Grub.  However, if
174                  * e_entry falls within the p_vaddr range of this PHDR, then
175                  * adjust it to match the p_paddr range... this is how Grub
176                  * behaves, so it's by definition correct (it doesn't have to
177                  * make sense...)
178                  */
179                 addr_t addr = ph->p_paddr;
180                 addr_t msize = ph->p_memsz;
181                 addr_t dsize = min(msize, ph->p_filesz);
182
183                 if (eh->e_entry >= ph->p_vaddr
184                     && eh->e_entry < ph->p_vaddr + msize)
185                     regs.eip = eh->e_entry + (ph->p_paddr - ph->p_vaddr);
186
187                 dprintf("Segment at 0x%08x data 0x%08x len 0x%08x\n",
188                         addr, dsize, msize);
189
190                 if (syslinux_memmap_type(amap, addr, msize) != SMT_FREE) {
191                     printf
192                         ("Memory segment at 0x%08x (len 0x%08x) is unavailable\n",
193                          addr, msize);
194                     return NULL;        /* Memory region unavailable */
195                 }
196
197                 /* Mark this region as allocated in the available map */
198                 if (syslinux_add_memmap(&amap, addr, msize, SMT_ALLOC)) {
199                     error("Overlapping segments found in ELF header\n");
200                     return NULL;
201                 }
202
203                 if (ph->p_filesz) {
204                     /* Data present region.  Create a move entry for it. */
205                     if (syslinux_add_movelist
206                         (&ml, addr, (addr_t) cptr + ph->p_offset, dsize)) {
207                         error("Failed to map PHDR data\n");
208                         return NULL;
209                     }
210                 }
211                 if (msize > dsize) {
212                     /* Zero-filled region.  Mark as a zero region in the memory map. */
213                     if (syslinux_add_memmap
214                         (&mmap, addr + dsize, msize - dsize, SMT_ZERO)) {
215                         error("Failed to map PHDR zero region\n");
216                         return NULL;
217                     }
218                 }
219                 if (addr + msize > mboot_high_water_mark)
220                     mboot_high_water_mark = addr + msize;
221             } else {
222                 /* Ignore this program header */
223             }
224
225             ph = (Elf32_Phdr *) ((char *)ph + eh->e_phentsize);
226         }
227
228         /* Load the ELF symbol table */
229         if (eh->e_shoff) {
230             addr_t addr, len;
231
232             sh = (Elf32_Shdr *) ((char *)eh + eh->e_shoff);
233
234             len = eh->e_shentsize * eh->e_shnum;
235             /*
236              * Align this, but don't pad -- in general this means a bunch of
237              * smaller sections gets packed into a single page.
238              */
239             addr = map_data(sh, len, 4096, MAP_HIGH | MAP_NOPAD);
240             if (!addr) {
241                 error("Failed to map symbol table\n");
242                 return NULL;
243             }
244
245             mbinfo.flags |= MB_INFO_ELF_SHDR;
246             mbinfo.syms.e.addr = addr;
247             mbinfo.syms.e.num = eh->e_shnum;
248             mbinfo.syms.e.size = eh->e_shentsize;
249             mbinfo.syms.e.shndx = eh->e_shstrndx;
250
251             for (i = 0; i < eh->e_shnum; i++) {
252                 addr_t align;
253
254                 if (!sh[i].sh_size)
255                     continue;   /* Empty section */
256                 if (sh[i].sh_flags & SHF_ALLOC)
257                     continue;   /* SHF_ALLOC sections should have PHDRs */
258
259                 align = sh[i].sh_addralign ? sh[i].sh_addralign : 0;
260                 addr = map_data((char *)ptr + sh[i].sh_offset, sh[i].sh_size,
261                                 align, MAP_HIGH);
262                 if (!addr) {
263                     error("Failed to map symbol section\n");
264                     return NULL;
265                 }
266                 sh[i].sh_addr = addr;
267             }
268         }
269     } else if (mbh_len && (mbh->flags & MULTIBOOT_AOUT_KLUDGE)) {
270         /*
271          * a.out kludge thing...
272          */
273         char *data_ptr;
274         addr_t data_len, bss_len;
275         addr_t bss_addr;
276
277         regs.eip = mbh->entry_addr;
278
279         data_ptr = (char *)mbh - (mbh->header_addr - mbh->load_addr);
280
281         if (mbh->load_end_addr)
282             data_len = mbh->load_end_addr - mbh->load_addr;
283         else
284             data_len = len - mbh_offset + (mbh->header_addr - mbh->load_addr);
285
286         bss_addr = mbh->load_addr + data_len;
287
288         if (mbh->bss_end_addr)
289             bss_len = mbh->bss_end_addr - mbh->load_end_addr;
290         else
291             bss_len = 0;
292
293         if (syslinux_memmap_type(amap, mbh->load_addr, data_len + bss_len)
294             != SMT_FREE) {
295             printf("Memory segment at 0x%08x (len 0x%08x) is unavailable\n",
296                    mbh->load_addr, data_len + bss_len);
297             return NULL;                /* Memory region unavailable */
298         }
299         if (syslinux_add_memmap(&amap, mbh->load_addr,
300                                 data_len + bss_len, SMT_ALLOC)) {
301             error("Failed to claim a.out address space!\n");
302             return NULL;
303         }
304         if (data_len)
305             if (syslinux_add_movelist(&ml, mbh->load_addr, (addr_t) data_ptr,
306                                       data_len)) {
307                 error("Failed to map a.out data\n");
308                 return NULL;
309             }
310         if (bss_len)
311             if (syslinux_add_memmap
312                 (&mmap, bss_addr, bss_len, SMT_ZERO)) {
313                 error("Failed to map a.out bss\n");
314                 return NULL;
315             }
316         if (bss_addr + bss_len > mboot_high_water_mark)
317             mboot_high_water_mark = bss_addr + bss_len;
318     } else {
319         error
320             ("Invalid Multiboot image: neither ELF header nor a.out kludge found\n");
321         return NULL;
322     }
323
324     return mbh;
325 }
326
327 /*
328  * Set up a stack.  This isn't actually required by the spec, but it seems
329  * like a prudent thing to do.  Also, put enough zeros at the top of the
330  * stack that something that looks for an ELF invocation record will know
331  * there isn't one.
332  */
333 static void mboot_map_stack(void)
334 {
335     addr_t start, len;
336
337     if (syslinux_memmap_largest(amap, SMT_FREE, &start, &len) || len < 64)
338         return;                 /* Not much we can do, here... */
339
340     regs.esp = (start + len - 32) & ~15;
341     dprintf("Mapping stack at 0x%08x\n", regs.esp);
342     syslinux_add_memmap(&mmap, regs.esp, 32, SMT_ZERO);
343 }
344
345 void mboot_run(int bootflags)
346 {
347     mboot_map_stack();
348
349     dprintf("Running, eip = 0x%08x, ebx = 0x%08x\n", regs.eip, regs.ebx);
350
351     regs.eax = MULTIBOOT_VALID;
352     syslinux_shuffle_boot_pm(ml, mmap, bootflags, &regs);
353 }