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

« back to all changes in this revision

Viewing changes to grub-core/kern/partition.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) 2004,2007  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
#include <grub/misc.h>
 
20
#include <grub/mm.h>
 
21
#include <grub/partition.h>
 
22
#include <grub/disk.h>
 
23
 
 
24
#ifdef GRUB_UTIL
 
25
#include <grub/util/misc.h>
 
26
#endif
 
27
 
 
28
grub_partition_map_t grub_partition_map_list;
 
29
 
 
30
/*
 
31
 * Checks that disk->partition contains part.  This function assumes that the
 
32
 * start of part is relative to the start of disk->partition.  Returns 1 if
 
33
 * disk->partition is null.
 
34
 */
 
35
static int
 
36
grub_partition_check_containment (const grub_disk_t disk,
 
37
                                  const grub_partition_t part)
 
38
{
 
39
  if (disk->partition == NULL)
 
40
    return 1;
 
41
 
 
42
  if (part->start + part->len > disk->partition->len)
 
43
    {
 
44
      char *partname;
 
45
 
 
46
      partname = grub_partition_get_name (disk->partition);
 
47
      grub_dprintf ("partition", "sub-partition %s%d of (%s,%s) ends after parent.\n",
 
48
                    part->partmap->name, part->number + 1, disk->name, partname);
 
49
#ifdef GRUB_UTIL
 
50
      grub_util_warn ("Discarding improperly nested partition (%s,%s,%s%d)",
 
51
                      disk->name, partname, part->partmap->name, part->number + 1);
 
52
#endif
 
53
      grub_free (partname);
 
54
 
 
55
      return 0;
 
56
    }
 
57
 
 
58
  return 1;
 
59
}
 
60
 
 
61
static grub_partition_t
 
62
grub_partition_map_probe (const grub_partition_map_t partmap,
 
63
                          grub_disk_t disk, int partnum)
 
64
{
 
65
  grub_partition_t p = 0;
 
66
 
 
67
  auto int find_func (grub_disk_t d, const grub_partition_t partition);
 
68
 
 
69
  int find_func (grub_disk_t dsk,
 
70
                 const grub_partition_t partition)
 
71
    {
 
72
      if (partnum != partition->number)
 
73
        return 0;
 
74
 
 
75
      if (!(grub_partition_check_containment (dsk, partition)))
 
76
        return 0;
 
77
 
 
78
      p = (grub_partition_t) grub_malloc (sizeof (*p));
 
79
      if (! p)
 
80
        return 1;
 
81
 
 
82
      grub_memcpy (p, partition, sizeof (*p));
 
83
      return 1;
 
84
    }
 
85
 
 
86
  partmap->iterate (disk, find_func);
 
87
  if (grub_errno)
 
88
    goto fail;
 
89
 
 
90
  return p;
 
91
 
 
92
 fail:
 
93
  grub_free (p);
 
94
  return 0;
 
95
}
 
96
 
 
97
grub_partition_t
 
98
grub_partition_probe (struct grub_disk *disk, const char *str)
 
99
{
 
100
  grub_partition_t part = 0;
 
101
  grub_partition_t curpart = 0;
 
102
  grub_partition_t tail;
 
103
  const char *ptr;
 
104
 
 
105
  part = tail = disk->partition;
 
106
 
 
107
  for (ptr = str; *ptr;)
 
108
    {
 
109
      grub_partition_map_t partmap;
 
110
      int num;
 
111
      const char *partname, *partname_end;
 
112
 
 
113
      partname = ptr;
 
114
      while (*ptr && grub_isalpha (*ptr))
 
115
        ptr++;
 
116
      partname_end = ptr; 
 
117
      num = grub_strtoul (ptr, (char **) &ptr, 0) - 1;
 
118
 
 
119
      curpart = 0;
 
120
      /* Use the first partition map type found.  */
 
121
      FOR_PARTITION_MAPS(partmap)
 
122
      {
 
123
        if (partname_end != partname &&
 
124
            (grub_strncmp (partmap->name, partname, partname_end - partname)
 
125
             != 0 || partmap->name[partname_end - partname] != 0))
 
126
          continue;
 
127
 
 
128
        disk->partition = part;
 
129
        curpart = grub_partition_map_probe (partmap, disk, num);
 
130
        disk->partition = tail;
 
131
        if (curpart)
 
132
          break;
 
133
 
 
134
        if (grub_errno == GRUB_ERR_BAD_PART_TABLE)
 
135
          {
 
136
            /* Continue to next partition map type.  */
 
137
            grub_errno = GRUB_ERR_NONE;
 
138
            continue;
 
139
          }
 
140
 
 
141
        break;
 
142
      }
 
143
 
 
144
      if (! curpart)
 
145
        {
 
146
          while (part)
 
147
            {
 
148
              curpart = part->parent;
 
149
              grub_free (part);
 
150
              part = curpart;
 
151
            }
 
152
          return 0;
 
153
        }
 
154
      curpart->parent = part;
 
155
      part = curpart;
 
156
      if (! ptr || *ptr != ',')
 
157
        break;
 
158
      ptr++;
 
159
    }
 
160
 
 
161
  return part;
 
162
}
 
163
 
 
164
int
 
165
grub_partition_iterate (struct grub_disk *disk,
 
166
                        int (*hook) (grub_disk_t disk,
 
167
                                     const grub_partition_t partition))
 
168
{
 
169
  int ret = 0;
 
170
 
 
171
  auto int part_iterate (grub_disk_t dsk, const grub_partition_t p);
 
172
 
 
173
  int part_iterate (grub_disk_t dsk,
 
174
                    const grub_partition_t partition)
 
175
    {
 
176
      struct grub_partition p = *partition;
 
177
 
 
178
      if (!(grub_partition_check_containment (dsk, partition)))
 
179
        return 0;
 
180
 
 
181
      p.parent = dsk->partition;
 
182
      dsk->partition = 0;
 
183
      if (hook (dsk, &p))
 
184
        {
 
185
          ret = 1;
 
186
          return 1;
 
187
        }
 
188
      if (p.start != 0)
 
189
        {
 
190
          const struct grub_partition_map *partmap;
 
191
          dsk->partition = &p;
 
192
          FOR_PARTITION_MAPS(partmap)
 
193
          {
 
194
            grub_err_t err;
 
195
            err = partmap->iterate (dsk, part_iterate);
 
196
            if (err)
 
197
              grub_errno = GRUB_ERR_NONE;
 
198
            if (ret)
 
199
              break;
 
200
          }
 
201
        }
 
202
      dsk->partition = p.parent;
 
203
      return ret;
 
204
    }
 
205
 
 
206
  {
 
207
    const struct grub_partition_map *partmap;
 
208
    FOR_PARTITION_MAPS(partmap)
 
209
    {
 
210
      grub_err_t err;
 
211
      err = partmap->iterate (disk, part_iterate);
 
212
      if (err)
 
213
        grub_errno = GRUB_ERR_NONE;
 
214
      if (ret)
 
215
        break;
 
216
    }
 
217
  }
 
218
 
 
219
  return ret;
 
220
}
 
221
 
 
222
char *
 
223
grub_partition_get_name (const grub_partition_t partition)
 
224
{
 
225
  char *out = 0;
 
226
  int curlen = 0;
 
227
  grub_partition_t part;
 
228
  for (part = partition; part; part = part->parent)
 
229
    {
 
230
      /* Even on 64-bit machines this buffer is enough to hold
 
231
         longest number.  */
 
232
      char buf[grub_strlen (part->partmap->name) + 25];
 
233
      int strl;
 
234
      grub_snprintf (buf, sizeof (buf), "%s%d", part->partmap->name,
 
235
                     part->number + 1);
 
236
      strl = grub_strlen (buf);
 
237
      if (curlen)
 
238
        {
 
239
          out = grub_realloc (out, curlen + strl + 2);
 
240
          grub_memcpy (out + strl + 1, out, curlen);
 
241
          out[curlen + 1 + strl] = 0;
 
242
          grub_memcpy (out, buf, strl);
 
243
          out[strl] = ',';
 
244
          curlen = curlen + 1 + strl;
 
245
        }
 
246
      else
 
247
        {
 
248
          curlen = strl;
 
249
          out = grub_strdup (buf);
 
250
        }
 
251
    }
 
252
  return out;
 
253
}