~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to loader/multiboot_elfxx.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  GRUB  --  GRand Unified Bootloader
3
 
 *  Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009  Free Software Foundation, Inc.
4
 
 *
5
 
 *  GRUB is free software: you can redistribute it and/or modify
6
 
 *  it under the terms of the GNU General Public License as published by
7
 
 *  the Free Software Foundation, either version 3 of the License, or
8
 
 *  (at your option) any later version.
9
 
 *
10
 
 *  GRUB is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
 
19
 
#if defined(MULTIBOOT_LOAD_ELF32)
20
 
# define XX             32
21
 
# define E_MACHINE      MULTIBOOT_ELF32_MACHINE
22
 
# define ELFCLASSXX     ELFCLASS32
23
 
# define Elf_Ehdr       Elf32_Ehdr
24
 
# define Elf_Phdr       Elf32_Phdr
25
 
#elif defined(MULTIBOOT_LOAD_ELF64)
26
 
# define XX             64
27
 
# define E_MACHINE      MULTIBOOT_ELF64_MACHINE
28
 
# define ELFCLASSXX     ELFCLASS64
29
 
# define Elf_Ehdr       Elf64_Ehdr
30
 
# define Elf_Phdr       Elf64_Phdr
31
 
#else
32
 
#error "I'm confused"
33
 
#endif
34
 
 
35
 
#include <grub/i386/relocator.h>
36
 
 
37
 
#define CONCAT(a,b)     CONCAT_(a, b)
38
 
#define CONCAT_(a,b)    a ## b
39
 
 
40
 
/* Check if BUFFER contains ELF32 (or ELF64).  */
41
 
static int
42
 
CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
43
 
{
44
 
  Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
45
 
 
46
 
  return ehdr->e_ident[EI_CLASS] == ELFCLASSXX;
47
 
}
48
 
 
49
 
static grub_err_t
50
 
CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, void *buffer)
51
 
{
52
 
  Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
53
 
  char *phdr_base;
54
 
  int lowest_segment = -1, highest_segment = -1;
55
 
  int i;
56
 
  grub_size_t code_size;
57
 
 
58
 
  if (ehdr->e_ident[EI_CLASS] != ELFCLASSXX)
59
 
    return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF class");
60
 
 
61
 
  if (ehdr->e_ident[EI_MAG0] != ELFMAG0
62
 
      || ehdr->e_ident[EI_MAG1] != ELFMAG1
63
 
      || ehdr->e_ident[EI_MAG2] != ELFMAG2
64
 
      || ehdr->e_ident[EI_MAG3] != ELFMAG3
65
 
      || ehdr->e_version != EV_CURRENT
66
 
      || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
67
 
      || ehdr->e_machine != E_MACHINE)
68
 
    return grub_error(GRUB_ERR_UNKNOWN_OS, "no valid ELF header found");
69
 
 
70
 
  if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN)
71
 
    return grub_error (GRUB_ERR_UNKNOWN_OS, "invalid ELF file type");
72
 
 
73
 
  /* FIXME: Should we support program headers at strange locations?  */
74
 
  if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
75
 
    return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");
76
 
 
77
 
#if defined (MULTIBOOT_LOAD_ELF64) && defined (__mips)
78
 
  /* We still in 32-bit mode.  */
79
 
  if (ehdr->e_entry < 0xffffffff80000000ULL)
80
 
    return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
81
 
#else
82
 
  /* We still in 32-bit mode.  */
83
 
  if (ehdr->e_entry > 0xffffffff)
84
 
    return grub_error (GRUB_ERR_BAD_OS, "invalid entry point for ELF64");
85
 
#endif
86
 
 
87
 
  phdr_base = (char *) buffer + ehdr->e_phoff;
88
 
#define phdr(i)                 ((Elf_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
89
 
 
90
 
  for (i = 0; i < ehdr->e_phnum; i++)
91
 
    if (phdr(i)->p_type == PT_LOAD && phdr(i)->p_filesz != 0)
92
 
      {
93
 
        /* Beware that segment 0 isn't necessarily loadable */
94
 
        if (lowest_segment == -1
95
 
            || phdr(i)->p_paddr < phdr(lowest_segment)->p_paddr)
96
 
          lowest_segment = i;
97
 
        if (highest_segment == -1
98
 
            || phdr(i)->p_paddr > phdr(highest_segment)->p_paddr)
99
 
          highest_segment = i;
100
 
      }
101
 
 
102
 
  if (lowest_segment == -1)
103
 
    return grub_error (GRUB_ERR_BAD_OS, "ELF contains no loadable segments");
104
 
 
105
 
  code_size = (phdr(highest_segment)->p_paddr + phdr(highest_segment)->p_memsz) - phdr(lowest_segment)->p_paddr;
106
 
  grub_multiboot_payload_dest = phdr(lowest_segment)->p_paddr;
107
 
 
108
 
  grub_multiboot_pure_size += code_size;
109
 
 
110
 
  grub_multiboot_alloc_mbi = grub_multiboot_get_mbi_size () + 65536;
111
 
  grub_multiboot_payload_orig
112
 
    = grub_relocator32_alloc (grub_multiboot_pure_size + grub_multiboot_alloc_mbi);
113
 
 
114
 
  if (!grub_multiboot_payload_orig)
115
 
    return grub_errno;
116
 
 
117
 
  /* Load every loadable segment in memory.  */
118
 
  for (i = 0; i < ehdr->e_phnum; i++)
119
 
    {
120
 
      if (phdr(i)->p_type == PT_LOAD && phdr(i)->p_filesz != 0)
121
 
        {
122
 
          char *load_this_module_at = (char *) (grub_multiboot_payload_orig + (long) (phdr(i)->p_paddr - phdr(lowest_segment)->p_paddr));
123
 
 
124
 
          grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
125
 
                        i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
126
 
 
127
 
          if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
128
 
              == (grub_off_t) -1)
129
 
            return grub_error (GRUB_ERR_BAD_OS,
130
 
                               "invalid offset in program header");
131
 
 
132
 
          if (grub_file_read (file, load_this_module_at, phdr(i)->p_filesz)
133
 
              != (grub_ssize_t) phdr(i)->p_filesz)
134
 
            return grub_error (GRUB_ERR_BAD_OS,
135
 
                               "couldn't read segment from file");
136
 
 
137
 
          if (phdr(i)->p_filesz < phdr(i)->p_memsz)
138
 
            grub_memset (load_this_module_at + phdr(i)->p_filesz, 0,
139
 
                         phdr(i)->p_memsz - phdr(i)->p_filesz);
140
 
        }
141
 
    }
142
 
 
143
 
  for (i = 0; i < ehdr->e_phnum; i++)
144
 
    if (phdr(i)->p_vaddr <= ehdr->e_entry
145
 
        && phdr(i)->p_vaddr + phdr(i)->p_memsz > ehdr->e_entry)
146
 
      {
147
 
        grub_multiboot_payload_eip = grub_multiboot_payload_dest
148
 
          + (ehdr->e_entry - phdr(i)->p_vaddr) + (phdr(i)->p_paddr  - phdr(lowest_segment)->p_paddr);
149
 
        break;
150
 
      }
151
 
 
152
 
  if (i == ehdr->e_phnum)
153
 
    return grub_error (GRUB_ERR_BAD_OS, "entry point isn't in a segment");
154
 
 
155
 
#undef phdr
156
 
 
157
 
  return grub_errno;
158
 
}
159
 
 
160
 
#undef XX
161
 
#undef E_MACHINE
162
 
#undef ELFCLASSXX
163
 
#undef Elf_Ehdr
164
 
#undef Elf_Phdr