aboutsummaryrefslogtreecommitdiffstats
path: root/com32/mboot/map.c
blob: c6290793890128b0a6a39da91479f8901c00265e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

/*
 * map.c
 *
 * Functions that deal with the memory map of various objects
 */

#include "mboot.h"

static struct syslinux_movelist *ml = NULL;
static struct syslinux_memmap *mmap = NULL, *amap = NULL;
static struct multiboot_header *mbh;
static addr_t mboot_high_water_mark = 0;

/*
 * Note: although there is no such thing in the spec, at least Xen makes
 * assumptions as to where in the memory space Grub would have loaded
 * certain things.  To support that, if "high" is set, then allocate this
 * at an address strictly above any previous allocations.
 *
 * As a precaution, this also pads the data with zero up to the next
 * alignment datum.
 */
addr_t map_data(const void *data, size_t len, int align, bool high)
{
  addr_t start = high ? mboot_high_water_mark : 0;
  addr_t pad = (len+align-1) & ~(align-1);
  addr_t xlen = len+pad;

  if (syslinux_memmap_find(amap, SMT_FREE, &start, &xlen, align) ||
      syslinux_add_memmap(&amap, start, len+pad, SMT_ALLOC) ||
      syslinux_add_movelist(&ml, start, (addr_t)data, len) ||
      (pad && syslinux_add_memmap(&mmap, start+len, pad, SMT_ZERO))) {
    printf("Cannot map %zu bytes\n", len+pad);
    return 0;
  }

  if (start+len+pad > mboot_high_water_mark)
    mboot_high_water_mark = start+len+pad;

  return start;
}

addr_t map_string(const char *string)
{
  if (!string)
    return 0;
  else
    return map_data(string, strlen(string)+1, 4, true);
}

int map_image(void *ptr, size_t len)
{
  int mbh_len;
  char *cptr = ptr;
  Elf32_Ehdr *eh = ptr;
  Elf32_Phdr *ph;
  Elf32_Shdr *sh;
  unsigned int i;
  char *stack_frame = NULL;
  uint32_t bad_flags;

  regs.eax = MULTIBOOT_VALID;

  /*
   * Search for the multiboot header...
   */
  mbh_len = 0;
  for (i = 0 ; i < MULTIBOOT_SEARCH ; i += 4) {
    mbh = (struct multiboot_header *)((char *)ptr + i);
    if (mbh->magic != MULTIBOOT_MAGIC)
      continue;
    if (mbh->magic + mbh->flags + mbh->checksum)
      continue;
    if (mbh->flags & MULTIBOOT_VIDEO_MODE)
      mbh_len = 48;
    else if (mbh->flags & MULTIBOOT_AOUT_KLUDGE)
      mbh_len = 32;
    else
      mbh_len = 12;

    if (i + mbh_len < len)
      mbh_len = 0;		/* Invalid... */
    else
      break;			/* Found something... */
  }

  if (mbh_len) {
    bad_flags = mbh->flags & (MULTIBOOT_UNSUPPORTED|MULTIBOOT_VIDEO_MODE);
    if (bad_flags) {
      printf("Unsupported Multiboot flags set: %#x\n", bad_flags);
      return -1;
    }
  }

  /*
   * Note: mmap is the memory map (containing free and zeroed regions)
   * needed by syslinux_shuffle_boot_pm(); amap is a map where we keep
   * track ourselves which target memory ranges have already been
   * allocated.
   */
  if ( len < sizeof(Elf32_Ehdr) ||
       memcmp(eh->e_ident, "\x7f""ELF\1\1\1", 6) ||
       (eh->e_machine != EM_386 && eh->e_machine != EM_486 &&
	eh->e_machine != EM_X86_64) ||
       eh->e_version != EV_CURRENT ||
       eh->e_ehsize < sizeof(Elf32_Ehdr) || eh->e_ehsize >= len ||
       eh->e_phentsize < sizeof(Elf32_Phdr) ||
       !eh->e_phnum ||
       eh->e_phoff+eh->e_phentsize*eh->e_phnum > len )
    eh = NULL;			/* No valid ELF header found */

  mmap = syslinux_memory_map();
  amap = syslinux_dup_memmap(mmap);
  if (!mmap || !amap)
    goto bail;

#if DEBUG
  dprintf("Initial memory map:\n");
  syslinux_dump_memmap(stdout, mmap);
#endif

  /*
   * Note: the Multiboot Specification implies that AOUT_KLUDGE should
   * have precedence over the ELF header.  However, Grub disagrees, and
   * Grub is "the reference bootloader" for the Multiboot Specification.
   * This is insane, since it makes the AOUT_KLUDGE bit functionally
   * useless, but at least Solaris apparently depends on this behavior.
   */
  if (eh) {
    regs.eip = eh->e_entry;

    ph = (Elf32_Phdr *)(cptr+eh->e_phoff);

    for (i = 0; i < eh->e_phnum; i++) {
      if (ph->p_type == PT_LOAD || ph->p_type == PT_PHDR) {
	/* This loads at p_paddr, which is arguably the correct semantics.
	   The SysV spec says that SysV loads at p_vaddr (and thus Linux does,
	   too); that is, however, a major brainfuckage in the spec. */
	addr_t addr  = ph->p_paddr;
	addr_t msize = ph->p_memsz;
	addr_t dsize = min(msize, ph->p_filesz);

	dprintf("Segment at 0x%08x data 0x%08x len 0x%08x\n",
		addr, dsize, msize);

	if (syslinux_memmap_type(amap, addr, msize) != SMT_FREE) {
	  printf("Memory segment at 0x%08x (len 0x%08x) is unavailable\n",
		 addr, msize);
	  goto bail;		/* Memory region unavailable */
	}

	/* Mark this region as allocated in the available map */
	if (syslinux_add_memmap(&amap, addr, msize, SMT_ALLOC))
	  goto bail;

	if (ph->p_filesz) {
	  /* Data present region.  Create a move entry for it. */
	  if (syslinux_add_movelist(&ml, addr, (addr_t)cptr+ph->p_offset,
				    dsize))
	    goto bail;
	}
	if (msize > dsize) {
	  /* Zero-filled region.  Mark as a zero region in the memory map. */
	  if (syslinux_add_memmap(&mmap, addr+dsize, msize-dsize, SMT_ZERO))
	    goto bail;
	}
	if (addr+msize > mboot_high_water_mark)
	  mboot_high_water_mark = addr+msize;
      } else {
	/* Ignore this program header */
      }

      ph = (Elf32_Phdr *)((char *)ph + eh->e_phentsize);
    }

    /* Load the ELF symbol table */
    if (eh->e_shoff) {
      addr_t addr, len;

      sh = (Elf32_Shdr *)((char *)eh + eh->e_shoff);

      len = eh->e_shentsize * eh->e_shnum;
      addr = map_data(sh, len, 4096, true);
      if (!addr)
	goto bail;

      mbinfo.flags |= MB_INFO_ELF_SHDR;
      mbinfo.syms.e.addr  = addr;
      mbinfo.syms.e.num   = eh->e_shnum;
      mbinfo.syms.e.size  = eh->e_shentsize;
      mbinfo.syms.e.shndx = eh->e_shstrndx;

      for (i = 0; i < eh->e_shnum; i++) {
	addr_t align;

	if (!sh[i].sh_size)
	  continue;		/* Empty section */
	if (sh[i].sh_flags & SHF_ALLOC)
	  continue;		/* SHF_ALLOC sections should have PHDRs */

	align = sh[i].sh_addralign ? sh[i].sh_addralign : 0;
	addr = map_data((char *)ptr + sh[i].sh_offset, sh[i].sh_size,
			align, true);
	if (!addr)
	  goto bail;
	sh[i].sh_addr = addr;
      }
    }
  } else if (mbh_len && (mbh->flags & MULTIBOOT_AOUT_KLUDGE)) {
    /*
     * a.out kludge thing...
     */
    char *data_ptr;
    addr_t data_len, bss_len;

    regs.eip = mbh->entry_addr;

    data_ptr = (char *)mbh - (mbh->header_addr - mbh->load_addr);
    data_len = mbh->load_end_addr - mbh->load_addr;
    bss_len  = mbh->bss_end_addr - mbh->load_end_addr;

    if (syslinux_memmap_type(amap, mbh->load_addr, data_len+bss_len)
	!= SMT_FREE) {
      printf("Memory segment at 0x%08x (len 0x%08x) is unavailable\n",
	     mbh->load_addr, data_len+bss_len);
      goto bail;		/* Memory region unavailable */
    }
    if (syslinux_add_memmap(&amap, mbh->load_addr,
			    data_len+bss_len, SMT_ALLOC))
      goto bail;
    if (data_len)
      if (syslinux_add_movelist(&ml, mbh->load_addr, (addr_t)data_ptr,
				data_len))
	goto bail;
    if (bss_len)
      if (syslinux_add_memmap(&mmap, mbh->load_end_addr, bss_len, SMT_ZERO))
	goto bail;
    if (mbh->bss_end_addr > mboot_high_water_mark)
      mboot_high_water_mark = mbh->bss_end_addr;
  } else {
    printf("Invalid Multiboot image: neither ELF header nor a.out kludge found\n");
    goto bail;
  }

 bail:
  if (stack_frame)
    free(stack_frame);
  syslinux_free_memmap(amap);
  syslinux_free_memmap(mmap);
  syslinux_free_movelist(ml);

  return -1;
}

void mboot_run(int bootflags)
{
  syslinux_shuffle_boot_pm(ml, mmap, bootflags, &regs);
}