2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2004,2007 Free Software Foundation, Inc.
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.
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.
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/>.
19
#include <grub/misc.h>
21
#include <grub/partition.h>
22
#include <grub/disk.h>
25
#include <grub/util/misc.h>
28
grub_partition_map_t grub_partition_map_list;
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.
36
grub_partition_check_containment (const grub_disk_t disk,
37
const grub_partition_t part)
39
if (disk->partition == NULL)
42
if (part->start + part->len > disk->partition->len)
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);
50
grub_util_warn ("Discarding improperly nested partition (%s,%s,%s%d)",
51
disk->name, partname, part->partmap->name, part->number + 1);
61
static grub_partition_t
62
grub_partition_map_probe (const grub_partition_map_t partmap,
63
grub_disk_t disk, int partnum)
65
grub_partition_t p = 0;
67
auto int find_func (grub_disk_t d, const grub_partition_t partition);
69
int find_func (grub_disk_t dsk,
70
const grub_partition_t partition)
72
if (partnum != partition->number)
75
if (!(grub_partition_check_containment (dsk, partition)))
78
p = (grub_partition_t) grub_malloc (sizeof (*p));
82
grub_memcpy (p, partition, sizeof (*p));
86
partmap->iterate (disk, find_func);
98
grub_partition_probe (struct grub_disk *disk, const char *str)
100
grub_partition_t part = 0;
101
grub_partition_t curpart = 0;
102
grub_partition_t tail;
105
part = tail = disk->partition;
107
for (ptr = str; *ptr;)
109
grub_partition_map_t partmap;
111
const char *partname, *partname_end;
114
while (*ptr && grub_isalpha (*ptr))
117
num = grub_strtoul (ptr, (char **) &ptr, 0) - 1;
120
/* Use the first partition map type found. */
121
FOR_PARTITION_MAPS(partmap)
123
if (partname_end != partname &&
124
(grub_strncmp (partmap->name, partname, partname_end - partname)
125
!= 0 || partmap->name[partname_end - partname] != 0))
128
disk->partition = part;
129
curpart = grub_partition_map_probe (partmap, disk, num);
130
disk->partition = tail;
134
if (grub_errno == GRUB_ERR_BAD_PART_TABLE)
136
/* Continue to next partition map type. */
137
grub_errno = GRUB_ERR_NONE;
148
curpart = part->parent;
154
curpart->parent = part;
156
if (! ptr || *ptr != ',')
165
grub_partition_iterate (struct grub_disk *disk,
166
int (*hook) (grub_disk_t disk,
167
const grub_partition_t partition))
171
auto int part_iterate (grub_disk_t dsk, const grub_partition_t p);
173
int part_iterate (grub_disk_t dsk,
174
const grub_partition_t partition)
176
struct grub_partition p = *partition;
178
if (!(grub_partition_check_containment (dsk, partition)))
181
p.parent = dsk->partition;
190
const struct grub_partition_map *partmap;
192
FOR_PARTITION_MAPS(partmap)
195
err = partmap->iterate (dsk, part_iterate);
197
grub_errno = GRUB_ERR_NONE;
202
dsk->partition = p.parent;
207
const struct grub_partition_map *partmap;
208
FOR_PARTITION_MAPS(partmap)
211
err = partmap->iterate (disk, part_iterate);
213
grub_errno = GRUB_ERR_NONE;
223
grub_partition_get_name (const grub_partition_t partition)
227
grub_partition_t part;
228
for (part = partition; part; part = part->parent)
230
/* Even on 64-bit machines this buffer is enough to hold
232
char buf[grub_strlen (part->partmap->name) + 25];
234
grub_snprintf (buf, sizeof (buf), "%s%d", part->partmap->name,
236
strl = grub_strlen (buf);
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);
244
curlen = curlen + 1 + strl;
249
out = grub_strdup (buf);