]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/partitions/dos.c
misc: Fix various typos
[thirdparty/util-linux.git] / libblkid / src / partitions / dos.c
1 /*
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"
17 #include "aix.h"
18
19 /* see superblocks/vfat.c */
20 extern int blkid_probe_is_vfat(blkid_probe pr);
21
22 static const struct dos_subtypes {
23 unsigned char type;
24 const struct blkid_idinfo *id;
25 } dos_nested[] = {
26 { 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 }
32 };
33
34 static inline int is_extended(struct dos_partition *p)
35 {
36 return (p->sys_ind == MBR_DOS_EXTENDED_PARTITION ||
37 p->sys_ind == MBR_W95_EXTENDED_PARTITION ||
38 p->sys_ind == MBR_LINUX_EXTENDED_PARTITION);
39 }
40
41 static 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)
55 return BLKID_PROBE_OK;
56 data = blkid_probe_get_sector(pr, cur_start);
57 if (!data) {
58 if (errno)
59 return -errno;
60 goto leave; /* malformed partition? */
61 }
62
63 if (!mbr_is_valid_magic(data))
64 goto leave;
65
66 p0 = mbr_get_partition(data, 0);
67
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 */
85 start = dos_partition_get_start(p) * ssf;
86 size = dos_partition_get_size(p) * ssf;
87 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)
104 return -ENOMEM;
105
106 blkid_partition_set_type(par, p->sys_ind);
107 blkid_partition_set_flags(par, p->boot_ind);
108 blkid_partition_gen_uuid(par);
109 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++) {
116 start = dos_partition_get_start(p) * ssf;
117 size = dos_partition_get_size(p) * ssf;
118
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 }
128 leave:
129 return BLKID_PROBE_OK;
130 }
131
132 static 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;
142 char idstr[37];
143
144
145 data = blkid_probe_get_sector(pr, 0);
146 if (!data) {
147 if (errno)
148 return -errno;
149 goto nothing;
150 }
151
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 p0 = mbr_get_partition(data, 0);
157
158 /*
159 * Reject PT where boot indicator is not 0 or 0x80.
160 */
161 for (p = p0, i = 0; i < 4; i++, p++)
162 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
163 DBG(LOWPROBE, ul_debug("missing boot indicator -- ignore"));
164 goto nothing;
165 }
166
167 /*
168 * GPT uses valid MBR
169 */
170 for (p = p0, i = 0; i < 4; i++, p++) {
171 if (p->sys_ind == MBR_GPT_PARTITION) {
172 DBG(LOWPROBE, ul_debug("probably GPT -- ignore"));
173 goto nothing;
174 }
175 }
176
177 /*
178 * Now that the 55aa signature is present, this is probably
179 * either the boot sector of a FAT filesystem or a DOS-type
180 * partition table.
181 */
182 if (blkid_probe_is_vfat(pr) == 1) {
183 DBG(LOWPROBE, ul_debug("probably FAT -- ignore"));
184 goto nothing;
185 }
186
187 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);
192
193 /*
194 * Well, all checks pass, it's MS-DOS partition table
195 */
196 if (blkid_partitions_need_typeonly(pr)) {
197 /* Non-binary interface -- caller does not ask for details
198 * about partitions, just set generic variables only. */
199 if (id)
200 blkid_partitions_strcpy_ptuuid(pr, idstr);
201 return 0;
202 }
203
204 ls = blkid_probe_get_partlist(pr);
205 if (!ls)
206 goto nothing;
207
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 */
214 tab = blkid_partlist_new_parttable(ls, "dos", MBR_PT_OFFSET);
215 if (!tab)
216 return -ENOMEM;
217
218 if (id)
219 blkid_parttable_set_id(tab, (unsigned char *) idstr);
220
221 /* Parse primary partitions */
222 for (p = p0, i = 0; i < 4; i++, p++) {
223 blkid_partition par;
224
225 start = dos_partition_get_start(p) * ssf;
226 size = dos_partition_get_size(p) * ssf;
227
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)
236 return -ENOMEM;
237
238 blkid_partition_set_type(par, p->sys_ind);
239 blkid_partition_set_flags(par, p->boot_ind);
240 blkid_partition_gen_uuid(par);
241 }
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++) {
250 start = dos_partition_get_start(p) * ssf;
251 size = dos_partition_get_size(p) * ssf;
252
253 if (!size)
254 continue;
255 if (is_extended(p) &&
256 parse_dos_extended(pr, tab, start, size, ssf) == -1)
257 goto nothing;
258 }
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;
264 int rc;
265
266 if (!dos_partition_get_size(p) || is_extended(p))
267 continue;
268
269 for (n = 0; n < ARRAY_SIZE(dos_nested); n++) {
270 if (dos_nested[n].type != p->sys_ind)
271 continue;
272
273 rc = blkid_partitions_do_subprobe(pr,
274 blkid_partlist_get_partition(ls, i),
275 dos_nested[n].id);
276 if (rc < 0)
277 return rc;
278 break;
279 }
280 }
281 }
282 return BLKID_PROBE_OK;
283
284 nothing:
285 return BLKID_PROBE_NONE;
286 }
287
288
289 const 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