2
* MS-DOS partition parsing code
4
* Copyright (C) 2009 Karel Zak <kzak@redhat.com>
6
* This file may be redistributed under the terms of the
7
* GNU Lesser General Public License.
9
* Inspired by fdisk, partx, Linux kernel and libparted.
16
#include "partitions.h"
20
/* see superblocks/vfat.c */
21
extern int blkid_probe_is_vfat(blkid_probe pr);
23
static const struct dos_subtypes {
25
const struct blkid_idinfo *id;
27
{ BLKID_FREEBSD_PARTITION, &bsd_pt_idinfo },
28
{ BLKID_NETBSD_PARTITION, &bsd_pt_idinfo },
29
{ BLKID_OPENBSD_PARTITION, &bsd_pt_idinfo },
30
{ BLKID_UNIXWARE_PARTITION, &unixware_pt_idinfo },
31
{ BLKID_SOLARIS_X86_PARTITION, &solaris_x86_pt_idinfo },
32
{ BLKID_MINIX_PARTITION, &minix_pt_idinfo }
35
static inline int is_extended(struct dos_partition *p)
37
return (p->sys_type == BLKID_DOS_EXTENDED_PARTITION ||
38
p->sys_type == BLKID_W95_EXTENDED_PARTITION ||
39
p->sys_type == BLKID_LINUX_EXTENDED_PARTITION);
42
static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
43
uint32_t ex_start, uint32_t ex_size, int ssf)
45
blkid_partlist ls = blkid_probe_get_partlist(pr);
46
uint32_t cur_start = ex_start, cur_size = ex_size;
48
int ct_nodata = 0; /* count ext.partitions without data partitions */
52
struct dos_partition *p, *p0;
55
if (++ct_nodata > 100)
57
data = blkid_probe_get_sector(pr, cur_start);
59
goto leave; /* malformed partition? */
61
if (!is_valid_mbr_signature(data))
64
p0 = (struct dos_partition *) (data + BLKID_MSDOS_PT_OFFSET);
66
/* Usually, the first entry is the real data partition,
67
* the 2nd entry is the next extended partition, or empty,
68
* and the 3rd and 4th entries are unused.
69
* However, DRDOS sometimes has the extended partition as
70
* the first entry (when the data partition is empty),
71
* and OS/2 seems to use all four entries.
72
* -- Linux kernel fs/partitions/dos.c
74
* See also http://en.wikipedia.org/wiki/Extended_boot_record
77
/* Parse data partition */
78
for (p = p0, i = 0; i < 4; i++, p++) {
82
/* the start is relative to the parental ext.partition */
83
start = dos_partition_start(p) * ssf;
84
size = dos_partition_size(p) * ssf;
85
abs_start = cur_start + start; /* absolute start */
87
if (!size || is_extended(p))
90
/* extra checks to detect real data on
91
* 3rd and 4th entries */
92
if (start + size > cur_size)
94
if (abs_start < ex_start)
96
if (abs_start + size > ex_start + ex_size)
100
par = blkid_partlist_add_partition(ls, tab, abs_start, size);
104
blkid_partition_set_type(par, p->sys_type);
105
blkid_partition_set_flags(par, p->boot_ind);
108
/* The first nested ext.partition should be a link to the next
109
* logical partition. Everything other (recursive ext.partitions)
112
for (p = p0, i = 0; i < 4; i++, p++) {
113
start = dos_partition_start(p) * ssf;
114
size = dos_partition_size(p) * ssf;
116
if (size && is_extended(p))
122
cur_start = ex_start + start;
131
static int probe_dos_pt(blkid_probe pr, const struct blkid_idmag *mag)
135
blkid_parttable tab = NULL;
137
struct dos_partition *p0, *p;
139
uint32_t start, size;
141
data = blkid_probe_get_sector(pr, 0);
145
/* ignore disks with AIX magic number -- for more details see aix.c */
146
if (memcmp(data, BLKID_AIX_MAGIC_STRING, BLKID_AIX_MAGIC_STRLEN) == 0)
150
* Now that the 55aa signature is present, this is probably
151
* either the boot sector of a FAT filesystem or a DOS-type
154
if (blkid_probe_is_vfat(pr)) {
155
DBG(DEBUG_LOWPROBE, printf("probably FAT -- ignore\n"));
159
p0 = (struct dos_partition *) (data + BLKID_MSDOS_PT_OFFSET);
162
* Reject PT where boot indicator is not 0 or 0x80.
164
for (p = p0, i = 0; i < 4; i++, p++)
165
if (p->boot_ind != 0 && p->boot_ind != 0x80) {
166
DBG(DEBUG_LOWPROBE, printf("missing boot indicator -- ignore\n"));
173
for (p = p0, i = 0; i < 4; i++, p++) {
174
if (p->sys_type == BLKID_GPT_PARTITION) {
175
DBG(DEBUG_LOWPROBE, printf("probably GPT -- ignore\n"));
180
blkid_probe_use_wiper(pr, BLKID_MSDOS_PT_OFFSET,
181
512 - BLKID_MSDOS_PT_OFFSET);
184
* Well, all checks pass, it's MS-DOS partiton table
186
if (blkid_partitions_need_typeonly(pr))
187
/* caller does not ask for details about partitions */
190
ls = blkid_probe_get_partlist(pr);
192
/* sector size factor (the start and size are in the real sectors, but
193
* we need to convert all sizes to 512 logical sectors
195
ssf = blkid_probe_get_sectorsize(pr) / 512;
197
/* allocate a new partition table */
198
tab = blkid_partlist_new_parttable(ls, "dos", BLKID_MSDOS_PT_OFFSET);
202
/* Parse primary partitions */
203
for (p = p0, i = 0; i < 4; i++, p++) {
206
start = dos_partition_start(p) * ssf;
207
size = dos_partition_size(p) * ssf;
210
/* Linux kernel ignores empty partitions, but partno for
211
* the empty primary partitions is not reused */
212
blkid_partlist_increment_partno(ls);
215
par = blkid_partlist_add_partition(ls, tab, start, size);
219
blkid_partition_set_type(par, p->sys_type);
220
blkid_partition_set_flags(par, p->boot_ind);
223
/* Linux uses partition numbers greater than 4
224
* for all logical partition and all nested partition tables (bsd, ..)
226
blkid_partlist_set_partno(ls, 5);
228
/* Parse logical partitions */
229
for (p = p0, i = 0; i < 4; i++, p++) {
230
start = dos_partition_start(p) * ssf;
231
size = dos_partition_size(p) * ssf;
235
if (is_extended(p) &&
236
parse_dos_extended(pr, tab, start, size, ssf) == -1)
240
/* Parse subtypes (nested partitions) on large disks */
241
if (!blkid_probe_is_tiny(pr)) {
242
for (p = p0, i = 0; i < 4; i++, p++) {
245
if (!dos_partition_size(p) || is_extended(p))
248
for (n = 0; n < ARRAY_SIZE(dos_nested); n++) {
249
if (dos_nested[n].type != p->sys_type)
252
if (blkid_partitions_do_subprobe(pr,
253
blkid_partlist_get_partition(ls, i),
254
dos_nested[n].id) == -1)
269
const struct blkid_idinfo dos_pt_idinfo =
272
.probefunc = probe_dos_pt,
275
/* DOS master boot sector:
278
* 440 | Optional Disk signature
279
* 446 | Partition table
283
{ .magic = "\x55\xAA", .len = 2, .sboff = 510 },