blob: 25399080020a4d47fd969bd3edecaf1e7f7b9d4e [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
2 * MS-DOS partition parsing code
3 *
4 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 *
9 * Inspired by fdisk, partx, Linux kernel and libparted.
10 */
11#include <stdio.h>
12#include <string.h>
13#include <stdlib.h>
14#include <stdint.h>
15
16#include "partitions.h"
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050017#include "aix.h"
18
19/* see superblocks/vfat.c */
20extern int blkid_probe_is_vfat(blkid_probe pr);
21
22static const struct dos_subtypes {
23 unsigned char type;
24 const struct blkid_idinfo *id;
25} dos_nested[] = {
bigbiff7b4c7a62015-01-01 19:44:14 -050026 { MBR_FREEBSD_PARTITION, &bsd_pt_idinfo },
27 { MBR_NETBSD_PARTITION, &bsd_pt_idinfo },
28 { MBR_OPENBSD_PARTITION, &bsd_pt_idinfo },
29 { MBR_UNIXWARE_PARTITION, &unixware_pt_idinfo },
30 { MBR_SOLARIS_X86_PARTITION, &solaris_x86_pt_idinfo },
31 { MBR_MINIX_PARTITION, &minix_pt_idinfo }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050032};
33
34static inline int is_extended(struct dos_partition *p)
35{
bigbiff7b4c7a62015-01-01 19:44:14 -050036 return (p->sys_ind == MBR_DOS_EXTENDED_PARTITION ||
37 p->sys_ind == MBR_W95_EXTENDED_PARTITION ||
38 p->sys_ind == MBR_LINUX_EXTENDED_PARTITION);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050039}
40
41static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
42 uint32_t ex_start, uint32_t ex_size, int ssf)
43{
44 blkid_partlist ls = blkid_probe_get_partlist(pr);
45 uint32_t cur_start = ex_start, cur_size = ex_size;
46 unsigned char *data;
47 int ct_nodata = 0; /* count ext.partitions without data partitions */
48 int i;
49
50 while (1) {
51 struct dos_partition *p, *p0;
52 uint32_t start, size;
53
54 if (++ct_nodata > 100)
bigbiff7b4c7a62015-01-01 19:44:14 -050055 return BLKID_PROBE_OK;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050056 data = blkid_probe_get_sector(pr, cur_start);
bigbiff7b4c7a62015-01-01 19:44:14 -050057 if (!data) {
58 if (errno)
59 return -errno;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050060 goto leave; /* malformed partition? */
bigbiff7b4c7a62015-01-01 19:44:14 -050061 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050062
bigbiff7b4c7a62015-01-01 19:44:14 -050063 if (!mbr_is_valid_magic(data))
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050064 goto leave;
65
bigbiff7b4c7a62015-01-01 19:44:14 -050066 p0 = mbr_get_partition(data, 0);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050067
68 /* Usually, the first entry is the real data partition,
69 * the 2nd entry is the next extended partition, or empty,
70 * and the 3rd and 4th entries are unused.
71 * However, DRDOS sometimes has the extended partition as
72 * the first entry (when the data partition is empty),
73 * and OS/2 seems to use all four entries.
74 * -- Linux kernel fs/partitions/dos.c
75 *
76 * See also http://en.wikipedia.org/wiki/Extended_boot_record
77 */
78
79 /* Parse data partition */
80 for (p = p0, i = 0; i < 4; i++, p++) {
81 uint32_t abs_start;
82 blkid_partition par;
83
84 /* the start is relative to the parental ext.partition */
bigbiff7b4c7a62015-01-01 19:44:14 -050085 start = dos_partition_get_start(p) * ssf;
86 size = dos_partition_get_size(p) * ssf;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050087 abs_start = cur_start + start; /* absolute start */
88
89 if (!size || is_extended(p))
90 continue;
91 if (i >= 2) {
92 /* extra checks to detect real data on
93 * 3rd and 4th entries */
94 if (start + size > cur_size)
95 continue;
96 if (abs_start < ex_start)
97 continue;
98 if (abs_start + size > ex_start + ex_size)
99 continue;
100 }
101
102 par = blkid_partlist_add_partition(ls, tab, abs_start, size);
103 if (!par)
bigbiff7b4c7a62015-01-01 19:44:14 -0500104 return -ENOMEM;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500105
bigbiff7b4c7a62015-01-01 19:44:14 -0500106 blkid_partition_set_type(par, p->sys_ind);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500107 blkid_partition_set_flags(par, p->boot_ind);
bigbiff7b4c7a62015-01-01 19:44:14 -0500108 blkid_partition_gen_uuid(par);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500109 ct_nodata = 0;
110 }
111 /* The first nested ext.partition should be a link to the next
112 * logical partition. Everything other (recursive ext.partitions)
113 * is junk.
114 */
115 for (p = p0, i = 0; i < 4; i++, p++) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500116 start = dos_partition_get_start(p) * ssf;
117 size = dos_partition_get_size(p) * ssf;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500118
119 if (size && is_extended(p))
120 break;
121 }
122 if (i == 4)
123 goto leave;
124
125 cur_start = ex_start + start;
126 cur_size = size;
127 }
128leave:
bigbiff7b4c7a62015-01-01 19:44:14 -0500129 return BLKID_PROBE_OK;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500130}
131
132static int probe_dos_pt(blkid_probe pr,
133 const struct blkid_idmag *mag __attribute__((__unused__)))
134{
135 int i;
136 int ssf;
137 blkid_parttable tab = NULL;
138 blkid_partlist ls;
139 struct dos_partition *p0, *p;
140 unsigned char *data;
141 uint32_t start, size, id;
bigbiff7b4c7a62015-01-01 19:44:14 -0500142 char idstr[37];
143
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500144
145 data = blkid_probe_get_sector(pr, 0);
bigbiff7b4c7a62015-01-01 19:44:14 -0500146 if (!data) {
147 if (errno)
148 return -errno;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500149 goto nothing;
bigbiff7b4c7a62015-01-01 19:44:14 -0500150 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500151
152 /* ignore disks with AIX magic number -- for more details see aix.c */
153 if (memcmp(data, BLKID_AIX_MAGIC_STRING, BLKID_AIX_MAGIC_STRLEN) == 0)
154 goto nothing;
155
156 /*
157 * Now that the 55aa signature is present, this is probably
158 * either the boot sector of a FAT filesystem or a DOS-type
159 * partition table.
160 */
bigbiff7b4c7a62015-01-01 19:44:14 -0500161 if (blkid_probe_is_vfat(pr) == 1) {
162 DBG(LOWPROBE, ul_debug("probably FAT -- ignore"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500163 goto nothing;
164 }
165
bigbiff7b4c7a62015-01-01 19:44:14 -0500166 p0 = mbr_get_partition(data, 0);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500167
168 /*
169 * Reject PT where boot indicator is not 0 or 0x80.
170 */
171 for (p = p0, i = 0; i < 4; i++, p++)
172 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500173 DBG(LOWPROBE, ul_debug("missing boot indicator -- ignore"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500174 goto nothing;
175 }
176
177 /*
178 * GPT uses valid MBR
179 */
180 for (p = p0, i = 0; i < 4; i++, p++) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500181 if (p->sys_ind == MBR_GPT_PARTITION) {
182 DBG(LOWPROBE, ul_debug("probably GPT -- ignore"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500183 goto nothing;
184 }
185 }
186
bigbiff7b4c7a62015-01-01 19:44:14 -0500187 blkid_probe_use_wiper(pr, MBR_PT_OFFSET, 512 - MBR_PT_OFFSET);
188
189 id = mbr_get_id(data);
190 if (id)
191 snprintf(idstr, sizeof(idstr), "%08x", id);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500192
193 /*
194 * Well, all checks pass, it's MS-DOS partiton table
195 */
bigbiff7b4c7a62015-01-01 19:44:14 -0500196 if (blkid_partitions_need_typeonly(pr)) {
197 /* Non-binary interface -- caller does not ask for details
198 * about partitions, just set generic varibles only. */
199 if (id)
200 blkid_partitions_strcpy_ptuuid(pr, idstr);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500201 return 0;
bigbiff7b4c7a62015-01-01 19:44:14 -0500202 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500203
204 ls = blkid_probe_get_partlist(pr);
bigbiff7b4c7a62015-01-01 19:44:14 -0500205 if (!ls)
206 goto nothing;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500207
208 /* sector size factor (the start and size are in the real sectors, but
209 * we need to convert all sizes to 512 logical sectors
210 */
211 ssf = blkid_probe_get_sectorsize(pr) / 512;
212
213 /* allocate a new partition table */
bigbiff7b4c7a62015-01-01 19:44:14 -0500214 tab = blkid_partlist_new_parttable(ls, "dos", MBR_PT_OFFSET);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500215 if (!tab)
bigbiff7b4c7a62015-01-01 19:44:14 -0500216 return -ENOMEM;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500217
bigbiff7b4c7a62015-01-01 19:44:14 -0500218 if (id)
219 blkid_parttable_set_id(tab, (unsigned char *) idstr);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500220
221 /* Parse primary partitions */
222 for (p = p0, i = 0; i < 4; i++, p++) {
223 blkid_partition par;
224
bigbiff7b4c7a62015-01-01 19:44:14 -0500225 start = dos_partition_get_start(p) * ssf;
226 size = dos_partition_get_size(p) * ssf;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500227
228 if (!size) {
229 /* Linux kernel ignores empty partitions, but partno for
230 * the empty primary partitions is not reused */
231 blkid_partlist_increment_partno(ls);
232 continue;
233 }
234 par = blkid_partlist_add_partition(ls, tab, start, size);
235 if (!par)
bigbiff7b4c7a62015-01-01 19:44:14 -0500236 return -ENOMEM;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500237
bigbiff7b4c7a62015-01-01 19:44:14 -0500238 blkid_partition_set_type(par, p->sys_ind);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500239 blkid_partition_set_flags(par, p->boot_ind);
bigbiff7b4c7a62015-01-01 19:44:14 -0500240 blkid_partition_gen_uuid(par);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500241 }
242
243 /* Linux uses partition numbers greater than 4
244 * for all logical partition and all nested partition tables (bsd, ..)
245 */
246 blkid_partlist_set_partno(ls, 5);
247
248 /* Parse logical partitions */
249 for (p = p0, i = 0; i < 4; i++, p++) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500250 start = dos_partition_get_start(p) * ssf;
251 size = dos_partition_get_size(p) * ssf;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500252
253 if (!size)
254 continue;
255 if (is_extended(p) &&
256 parse_dos_extended(pr, tab, start, size, ssf) == -1)
bigbiff7b4c7a62015-01-01 19:44:14 -0500257 goto nothing;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500258 }
259
260 /* Parse subtypes (nested partitions) on large disks */
261 if (!blkid_probe_is_tiny(pr)) {
262 for (p = p0, i = 0; i < 4; i++, p++) {
263 size_t n;
bigbiff7b4c7a62015-01-01 19:44:14 -0500264 int rc;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500265
bigbiff7b4c7a62015-01-01 19:44:14 -0500266 if (!dos_partition_get_size(p) || is_extended(p))
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500267 continue;
268
269 for (n = 0; n < ARRAY_SIZE(dos_nested); n++) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500270 if (dos_nested[n].type != p->sys_ind)
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500271 continue;
272
bigbiff7b4c7a62015-01-01 19:44:14 -0500273 rc = blkid_partitions_do_subprobe(pr,
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500274 blkid_partlist_get_partition(ls, i),
bigbiff7b4c7a62015-01-01 19:44:14 -0500275 dos_nested[n].id);
276 if (rc < 0)
277 return rc;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500278 break;
279 }
280 }
281 }
bigbiff7b4c7a62015-01-01 19:44:14 -0500282 return BLKID_PROBE_OK;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500283
284nothing:
bigbiff7b4c7a62015-01-01 19:44:14 -0500285 return BLKID_PROBE_NONE;
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500286}
287
288
289const struct blkid_idinfo dos_pt_idinfo =
290{
291 .name = "dos",
292 .probefunc = probe_dos_pt,
293 .magics =
294 {
295 /* DOS master boot sector:
296 *
297 * 0 | Code Area
298 * 440 | Optional Disk signature
299 * 446 | Partition table
300 * 510 | 0x55
301 * 511 | 0xAA
302 */
303 { .magic = "\x55\xAA", .len = 2, .sboff = 510 },
304 { NULL }
305 }
306};
307