mboot: fix cmdline; a few more layout tweaks
[profile/ivi/syslinux.git] / com32 / mboot / map.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4  *   Copyright 2009 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 struct multiboot_header *mbh;
40 static addr_t mboot_high_water_mark = 0x100000;
41
42 /*
43  * Note: although there is no such thing in the spec, at least Xen makes
44  * assumptions as to where in the memory space Grub would have loaded
45  * certain things.  To support that, if "high" is set, then allocate this
46  * at an address strictly above any previous allocations.
47  *
48  * As a precaution, this also pads the data with zero up to the next
49  * alignment datum.
50  */
51 addr_t map_data(const void *data, size_t len, size_t align, int flags)
52 {
53   addr_t start = (flags & MAP_HIGH) ? mboot_high_water_mark : 0x2000;
54   addr_t pad   = (flags & MAP_NOPAD) ? 0 : -len & (align-1);
55   addr_t xlen  = len+pad;
56
57   if (syslinux_memmap_find(amap, SMT_FREE, &start, &xlen, align) ||
58       syslinux_add_memmap(&amap, start, len+pad, SMT_ALLOC) ||
59       syslinux_add_movelist(&ml, start, (addr_t)data, len) ||
60       (pad && syslinux_add_memmap(&mmap, start+len, pad, SMT_ZERO))) {
61     printf("Cannot map %zu bytes\n", len+pad);
62     return 0;
63   }
64
65   dprintf("Mapping 0x%08x bytes (%#x pad) at 0x%08x\n", len, pad, start);
66
67   if (start+len+pad > mboot_high_water_mark)
68     mboot_high_water_mark = start+len+pad;
69
70   return start;
71 }
72
73 addr_t map_string(const char *string)
74 {
75   if (!string)
76     return 0;
77   else
78     return map_data(string, strlen(string)+1, 1, 0);
79 }
80
81 int map_image(void *ptr, size_t len)
82 {
83   int mbh_len;
84   char *cptr = ptr;
85   Elf32_Ehdr *eh = ptr;
86   Elf32_Phdr *ph;
87   Elf32_Shdr *sh;
88   unsigned int i;
89   uint32_t bad_flags;
90
91   regs.eax = MULTIBOOT_VALID;
92
93   /*
94    * Search for the multiboot header...
95    */
96   mbh_len = 0;
97   for (i = 0 ; i < MULTIBOOT_SEARCH ; i += 4) {
98     mbh = (struct multiboot_header *)((char *)ptr + i);
99     if (mbh->magic != MULTIBOOT_MAGIC)
100       continue;
101     if (mbh->magic + mbh->flags + mbh->checksum)
102       continue;
103     if (mbh->flags & MULTIBOOT_VIDEO_MODE)
104       mbh_len = 48;
105     else if (mbh->flags & MULTIBOOT_AOUT_KLUDGE)
106       mbh_len = 32;
107     else
108       mbh_len = 12;
109
110     if (i + mbh_len < len)
111       mbh_len = 0;              /* Invalid... */
112     else
113       break;                    /* Found something... */
114   }
115
116   if (mbh_len) {
117     bad_flags = mbh->flags & (MULTIBOOT_UNSUPPORTED|MULTIBOOT_VIDEO_MODE);
118     if (bad_flags) {
119       printf("Unsupported Multiboot flags set: %#x\n", bad_flags);
120       return -1;
121     }
122   }
123
124   /*
125    * Note: mmap is the memory map (containing free and zeroed regions)
126    * needed by syslinux_shuffle_boot_pm(); amap is a map where we keep
127    * track ourselves which target memory ranges have already been
128    * allocated.
129    */
130   if ( len < sizeof(Elf32_Ehdr) ||
131        memcmp(eh->e_ident, "\x7f""ELF\1\1\1", 6) ||
132        (eh->e_machine != EM_386 && eh->e_machine != EM_486 &&
133         eh->e_machine != EM_X86_64) ||
134        eh->e_version != EV_CURRENT ||
135        eh->e_ehsize < sizeof(Elf32_Ehdr) || eh->e_ehsize >= len ||
136        eh->e_phentsize < sizeof(Elf32_Phdr) ||
137        !eh->e_phnum ||
138        eh->e_phoff+eh->e_phentsize*eh->e_phnum > len )
139     eh = NULL;                  /* No valid ELF header found */
140
141   mmap = syslinux_memory_map();
142   amap = syslinux_dup_memmap(mmap);
143   if (!mmap || !amap) {
144     error("Failed to allocate initial memory map!\n");
145     goto bail;
146   }
147
148 #if DEBUG
149   dprintf("Initial memory map:\n");
150   syslinux_dump_memmap(stdout, mmap);
151 #endif
152
153   /*
154    * Note: the Multiboot Specification implies that AOUT_KLUDGE should
155    * have precedence over the ELF header.  However, Grub disagrees, and
156    * Grub is "the reference bootloader" for the Multiboot Specification.
157    * This is insane, since it makes the AOUT_KLUDGE bit functionally
158    * useless, but at least Solaris apparently depends on this behavior.
159    */
160   if (eh) {
161     regs.eip = eh->e_entry;
162
163     ph = (Elf32_Phdr *)(cptr+eh->e_phoff);
164
165     for (i = 0; i < eh->e_phnum; i++) {
166       if (ph->p_type == PT_LOAD || ph->p_type == PT_PHDR) {
167         /* This loads at p_paddr, which is arguably the correct semantics.
168            The SysV spec says that SysV loads at p_vaddr (and thus Linux does,
169            too); that is, however, a major brainfuckage in the spec. */
170         addr_t addr  = ph->p_paddr;
171         addr_t msize = ph->p_memsz;
172         addr_t dsize = min(msize, ph->p_filesz);
173
174         dprintf("Segment at 0x%08x data 0x%08x len 0x%08x\n",
175                 addr, dsize, msize);
176
177         if (syslinux_memmap_type(amap, addr, msize) != SMT_FREE) {
178           printf("Memory segment at 0x%08x (len 0x%08x) is unavailable\n",
179                  addr, msize);
180           goto bail;            /* Memory region unavailable */
181         }
182
183         /* Mark this region as allocated in the available map */
184         if (syslinux_add_memmap(&amap, addr, msize, SMT_ALLOC)) {
185           error("Overlapping segments found in ELF header\n");
186           goto bail;
187         }
188
189         if (ph->p_filesz) {
190           /* Data present region.  Create a move entry for it. */
191           if (syslinux_add_movelist(&ml, addr, (addr_t)cptr+ph->p_offset,
192                                     dsize)) {
193             error("Failed to map PHDR data\n");
194             goto bail;
195           }
196         }
197         if (msize > dsize) {
198           /* Zero-filled region.  Mark as a zero region in the memory map. */
199           if (syslinux_add_memmap(&mmap, addr+dsize, msize-dsize, SMT_ZERO)) {
200             error("Failed to map PHDR zero region\n");
201             goto bail;
202           }
203         }
204         if (addr+msize > mboot_high_water_mark)
205           mboot_high_water_mark = addr+msize;
206       } else {
207         /* Ignore this program header */
208       }
209
210       ph = (Elf32_Phdr *)((char *)ph + eh->e_phentsize);
211     }
212
213     /* Load the ELF symbol table */
214     if (eh->e_shoff) {
215       addr_t addr, len;
216
217       sh = (Elf32_Shdr *)((char *)eh + eh->e_shoff);
218
219       len = eh->e_shentsize * eh->e_shnum;
220       /*
221        * Align this, but don't pad -- in general this means a bunch of
222        * smaller sections gets packed into a single page.
223        */
224       addr = map_data(sh, len, 4096, MAP_HIGH|MAP_NOPAD);
225       if (!addr) {
226         error("Failed to map symbol table\n");
227         goto bail;
228       }
229
230       mbinfo.flags |= MB_INFO_ELF_SHDR;
231       mbinfo.syms.e.addr  = addr;
232       mbinfo.syms.e.num   = eh->e_shnum;
233       mbinfo.syms.e.size  = eh->e_shentsize;
234       mbinfo.syms.e.shndx = eh->e_shstrndx;
235
236       for (i = 0; i < eh->e_shnum; i++) {
237         addr_t align;
238
239         if (!sh[i].sh_size)
240           continue;             /* Empty section */
241         if (sh[i].sh_flags & SHF_ALLOC)
242           continue;             /* SHF_ALLOC sections should have PHDRs */
243
244         align = sh[i].sh_addralign ? sh[i].sh_addralign : 0;
245         addr = map_data((char *)ptr + sh[i].sh_offset, sh[i].sh_size,
246                         align, MAP_HIGH);
247         if (!addr) {
248           error("Failed to map symbol section\n");
249           goto bail;
250         }
251         sh[i].sh_addr = addr;
252       }
253     }
254   } else if (mbh_len && (mbh->flags & MULTIBOOT_AOUT_KLUDGE)) {
255     /*
256      * a.out kludge thing...
257      */
258     char *data_ptr;
259     addr_t data_len, bss_len;
260
261     regs.eip = mbh->entry_addr;
262
263     data_ptr = (char *)mbh - (mbh->header_addr - mbh->load_addr);
264     data_len = mbh->load_end_addr - mbh->load_addr;
265     bss_len  = mbh->bss_end_addr - mbh->load_end_addr;
266
267     if (syslinux_memmap_type(amap, mbh->load_addr, data_len+bss_len)
268         != SMT_FREE) {
269       printf("Memory segment at 0x%08x (len 0x%08x) is unavailable\n",
270              mbh->load_addr, data_len+bss_len);
271       goto bail;                /* Memory region unavailable */
272     }
273     if (syslinux_add_memmap(&amap, mbh->load_addr,
274                             data_len+bss_len, SMT_ALLOC)) {
275       error("Failed to claim a.out address space!\n");
276       goto bail;
277     }
278     if (data_len)
279       if (syslinux_add_movelist(&ml, mbh->load_addr, (addr_t)data_ptr,
280                                 data_len)) {
281         error("Failed to map a.out data\n");
282         goto bail;
283       }
284     if (bss_len)
285       if (syslinux_add_memmap(&mmap, mbh->load_end_addr, bss_len, SMT_ZERO)) {
286         error("Failed to map a.out bss\n");
287         goto bail;
288       }
289     if (mbh->bss_end_addr > mboot_high_water_mark)
290       mboot_high_water_mark = mbh->bss_end_addr;
291   } else {
292     error("Invalid Multiboot image: neither ELF header nor a.out kludge found\n");
293     goto bail;
294   }
295
296   return 0;
297
298  bail:
299   syslinux_free_memmap(amap);
300   syslinux_free_memmap(mmap);
301   syslinux_free_movelist(ml);
302   return -1;
303 }
304
305 /*
306  * Set up a stack.  This isn't actually required by the spec, but it seems
307  * like a prudent thing to do.  Also, put enough zeros at the top of the
308  * stack that something that looks for an ELF invocation record will know
309  * there isn't one.
310  */
311 static void mboot_map_stack(void)
312 {
313   addr_t start, len;
314
315   if (syslinux_memmap_largest(amap, SMT_FREE, &start, &len) || len < 64)
316     return;                     /* Not much we can do, here... */
317
318   regs.esp = (start+len-32) & ~7;
319   dprintf("Mapping stack at 0x%08x\n", regs.esp);
320   syslinux_add_memmap(&mmap, regs.esp, 32, SMT_ZERO);
321 }
322
323 void mboot_run(int bootflags)
324 {
325   mboot_map_stack();
326   
327   dprintf("Running, eip = 0x%08x, ebx = 0x%08x\n", regs.eip, regs.ebx);
328
329   syslinux_shuffle_boot_pm(ml, mmap, bootflags, &regs);
330 }