~hamo/ubuntu/precise/grub2/grub2.hi_res

« 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, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

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
}