blob: 93e7c1b32eddd5aa27fc8c96f5f581f712541a53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/partitions/msdos.c
3 *
4 * Code extracted from drivers/block/genhd.c
5 * Copyright (C) 1991-1998 Linus Torvalds
6 *
7 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
8 * in the early extended-partition checks and added DM partitions
9 *
10 * Support for DiskManager v6.0x added by Mark Lord,
11 * with information provided by OnTrack. This now works for linux fdisk
12 * and LILO, as well as loadlin and bootln. Note that disks other than
13 * /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
14 *
15 * More flexible handling of extended partitions - aeb, 950831
16 *
17 * Check partition table on IDE disks for common CHS translations
18 *
19 * Re-organised Feb 1998 Russell King
20 */
Frank Seidel0607fd02008-04-28 02:16:31 -070021#include <linux/msdos_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "check.h"
24#include "msdos.h"
25#include "efi.h"
Philippe De Muyterf8f06602013-07-08 16:01:30 -070026#include "aix.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Many architectures don't like unaligned accesses, while
30 * the nr_sects and start_sect partition table entries are
31 * at a 2 (mod 4) address.
32 */
33#include <asm/unaligned.h>
34
Daniel Taylor3fbf5862010-03-23 13:35:50 -070035#define SYS_IND(p) get_unaligned(&p->sys_ind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Daniel Taylor3fbf5862010-03-23 13:35:50 -070037static inline sector_t nr_sects(struct partition *p)
38{
39 return (sector_t)get_unaligned_le32(&p->nr_sects);
40}
41
42static inline sector_t start_sect(struct partition *p)
43{
44 return (sector_t)get_unaligned_le32(&p->start_sect);
45}
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static inline int is_extended_partition(struct partition *p)
48{
49 return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
50 SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
51 SYS_IND(p) == LINUX_EXTENDED_PARTITION);
52}
53
54#define MSDOS_LABEL_MAGIC1 0x55
55#define MSDOS_LABEL_MAGIC2 0xAA
56
57static inline int
58msdos_magic_present(unsigned char *p)
59{
60 return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
61}
62
Olaf Heringe1dfa922006-09-29 01:59:39 -070063/* Value is EBCDIC 'IBMA' */
64#define AIX_LABEL_MAGIC1 0xC9
65#define AIX_LABEL_MAGIC2 0xC2
66#define AIX_LABEL_MAGIC3 0xD4
67#define AIX_LABEL_MAGIC4 0xC1
Tejun Heo1493bf22010-05-15 20:09:30 +020068static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
Olaf Heringe1dfa922006-09-29 01:59:39 -070069{
Olaf Hering4419d1a2007-02-10 01:45:47 -080070 struct partition *pt = (struct partition *) (p + 0x1be);
Olaf Heringe1dfa922006-09-29 01:59:39 -070071 Sector sect;
72 unsigned char *d;
Olaf Hering4419d1a2007-02-10 01:45:47 -080073 int slot, ret = 0;
Olaf Heringe1dfa922006-09-29 01:59:39 -070074
Olaf Heringa470e182007-02-10 01:45:48 -080075 if (!(p[0] == AIX_LABEL_MAGIC1 &&
76 p[1] == AIX_LABEL_MAGIC2 &&
77 p[2] == AIX_LABEL_MAGIC3 &&
78 p[3] == AIX_LABEL_MAGIC4))
Olaf Heringe1dfa922006-09-29 01:59:39 -070079 return 0;
Olaf Hering4419d1a2007-02-10 01:45:47 -080080 /* Assume the partition table is valid if Linux partitions exists */
81 for (slot = 1; slot <= 4; slot++, pt++) {
82 if (pt->sys_ind == LINUX_SWAP_PARTITION ||
83 pt->sys_ind == LINUX_RAID_PARTITION ||
84 pt->sys_ind == LINUX_DATA_PARTITION ||
85 pt->sys_ind == LINUX_LVM_PARTITION ||
86 is_extended_partition(pt))
87 return 0;
88 }
Tejun Heo1493bf22010-05-15 20:09:30 +020089 d = read_part_sector(state, 7, &sect);
Olaf Heringe1dfa922006-09-29 01:59:39 -070090 if (d) {
91 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
92 ret = 1;
93 put_dev_sector(sect);
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -070094 }
Olaf Heringe1dfa922006-09-29 01:59:39 -070095 return ret;
96}
97
Stephen Warrend33b98f2012-11-08 16:12:28 -080098static void set_info(struct parsed_partitions *state, int slot,
99 u32 disksig)
100{
101 struct partition_meta_info *info = &state->parts[slot].info;
102
103 snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
104 slot);
105 info->volname[0] = 0;
106 state->parts[slot].has_info = true;
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Create devices for each logical partition in an extended partition.
111 * The logical partitions form a linked list, with each entry being
112 * a partition table with two entries. The first entry
113 * is the real data partition (with a start relative to the partition
114 * table start). The second is a pointer to the next logical partition
115 * (with a start relative to the entire extended partition).
116 * We do not create a Linux partition for the partition tables, but
117 * only for the actual data partitions.
118 */
119
Tejun Heo1493bf22010-05-15 20:09:30 +0200120static void parse_extended(struct parsed_partitions *state,
Stephen Warrend33b98f2012-11-08 16:12:28 -0800121 sector_t first_sector, sector_t first_size,
122 u32 disksig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct partition *p;
125 Sector sect;
126 unsigned char *data;
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700127 sector_t this_sector, this_size;
Tejun Heo1493bf22010-05-15 20:09:30 +0200128 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int loopct = 0; /* number of links followed
130 without finding a data partition */
131 int i;
132
133 this_sector = first_sector;
134 this_size = first_size;
135
136 while (1) {
137 if (++loopct > 100)
138 return;
139 if (state->next == state->limit)
140 return;
Tejun Heo1493bf22010-05-15 20:09:30 +0200141 data = read_part_sector(state, this_sector, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!data)
143 return;
144
145 if (!msdos_magic_present(data + 510))
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700146 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 p = (struct partition *) (data + 0x1be);
149
150 /*
151 * Usually, the first entry is the real data partition,
152 * the 2nd entry is the next extended partition, or empty,
153 * and the 3rd and 4th entries are unused.
154 * However, DRDOS sometimes has the extended partition as
155 * the first entry (when the data partition is empty),
156 * and OS/2 seems to use all four entries.
157 */
158
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700159 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * First process the data partition(s)
161 */
Fabian Frederickdce14c22014-06-12 20:16:57 +0200162 for (i = 0; i < 4; i++, p++) {
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700163 sector_t offs, size, next;
Fabian Frederickdce14c22014-06-12 20:16:57 +0200164
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700165 if (!nr_sects(p) || is_extended_partition(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 continue;
167
168 /* Check the 3rd and 4th entries -
169 these sometimes contain random garbage */
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700170 offs = start_sect(p)*sector_size;
171 size = nr_sects(p)*sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 next = this_sector + offs;
173 if (i >= 2) {
174 if (offs + size > this_size)
175 continue;
176 if (next < first_sector)
177 continue;
178 if (next + size > first_sector + first_size)
179 continue;
180 }
181
182 put_partition(state, state->next, next, size);
Stephen Warrend33b98f2012-11-08 16:12:28 -0800183 set_info(state, state->next, disksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (SYS_IND(p) == LINUX_RAID_PARTITION)
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800185 state->parts[state->next].flags = ADDPART_FLAG_RAID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 loopct = 0;
187 if (++state->next == state->limit)
188 goto done;
189 }
190 /*
191 * Next, process the (first) extended partition, if present.
192 * (So far, there seems to be no reason to make
193 * parse_extended() recursive and allow a tree
194 * of extended partitions.)
195 * It should be a link to the next logical partition.
196 */
197 p -= 4;
Fabian Frederickdce14c22014-06-12 20:16:57 +0200198 for (i = 0; i < 4; i++, p++)
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700199 if (nr_sects(p) && is_extended_partition(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201 if (i == 4)
202 goto done; /* nothing left to do */
203
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700204 this_sector = first_sector + start_sect(p) * sector_size;
205 this_size = nr_sects(p) * sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 put_dev_sector(sect);
207 }
208done:
209 put_dev_sector(sect);
210}
211
212/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
213 indicates linux swap. Be careful before believing this is Solaris. */
214
Tejun Heo1493bf22010-05-15 20:09:30 +0200215static void parse_solaris_x86(struct parsed_partitions *state,
216 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218#ifdef CONFIG_SOLARIS_X86_PARTITION
219 Sector sect;
220 struct solaris_x86_vtoc *v;
221 int i;
Mark Fortescueb84d8792007-07-25 18:30:08 -0700222 short max_nparts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Tejun Heo1493bf22010-05-15 20:09:30 +0200224 v = read_part_sector(state, offset + 1, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!v)
226 return;
227 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
228 put_dev_sector(sect);
229 return;
230 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700231 {
232 char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
233
234 snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
235 strlcat(state->pp_buf, tmp, PAGE_SIZE);
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (le32_to_cpu(v->v_version) != 1) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700238 char tmp[64];
239
240 snprintf(tmp, sizeof(tmp), " cannot handle version %d vtoc>\n",
241 le32_to_cpu(v->v_version));
242 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 put_dev_sector(sect);
244 return;
245 }
Mark Fortescueb84d8792007-07-25 18:30:08 -0700246 /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
Fabian Frederickdce14c22014-06-12 20:16:57 +0200247 max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
248 for (i = 0; i < max_nparts && state->next < state->limit; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct solaris_x86_slice *s = &v->v_slice[i];
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700250 char tmp[3 + 10 + 1 + 1];
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (s->s_size == 0)
253 continue;
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700254 snprintf(tmp, sizeof(tmp), " [s%d]", i);
255 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* solaris partitions are relative to current MS-DOS
257 * one; must add the offset of the current partition */
258 put_partition(state, state->next++,
259 le32_to_cpu(s->s_start)+offset,
260 le32_to_cpu(s->s_size));
261 }
262 put_dev_sector(sect);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700263 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#endif
265}
266
Adrian Bunk486fd402005-06-25 14:58:47 -0700267#if defined(CONFIG_BSD_DISKLABEL)
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700268/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * Create devices for BSD partitions listed in a disklabel, under a
270 * dos-like partition. See parse_extended() for more information.
271 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200272static void parse_bsd(struct parsed_partitions *state,
273 sector_t offset, sector_t size, int origin, char *flavour,
274 int max_partitions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 Sector sect;
277 struct bsd_disklabel *l;
278 struct bsd_partition *p;
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700279 char tmp[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Tejun Heo1493bf22010-05-15 20:09:30 +0200281 l = read_part_sector(state, offset + 1, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!l)
283 return;
284 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
285 put_dev_sector(sect);
286 return;
287 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700288
289 snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
290 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (le16_to_cpu(l->d_npartitions) < max_partitions)
293 max_partitions = le16_to_cpu(l->d_npartitions);
294 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700295 sector_t bsd_start, bsd_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (state->next == state->limit)
298 break;
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700299 if (p->p_fstype == BSD_FS_UNUSED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 continue;
301 bsd_start = le32_to_cpu(p->p_offset);
302 bsd_size = le32_to_cpu(p->p_size);
303 if (offset == bsd_start && size == bsd_size)
304 /* full parent partition, we have it already */
305 continue;
306 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700307 strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 continue;
309 }
310 put_partition(state, state->next++, bsd_start, bsd_size);
311 }
312 put_dev_sector(sect);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700313 if (le16_to_cpu(l->d_npartitions) > max_partitions) {
314 snprintf(tmp, sizeof(tmp), " (ignored %d more)",
315 le16_to_cpu(l->d_npartitions) - max_partitions);
316 strlcat(state->pp_buf, tmp, PAGE_SIZE);
317 }
318 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320#endif
321
Tejun Heo1493bf22010-05-15 20:09:30 +0200322static void parse_freebsd(struct parsed_partitions *state,
323 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325#ifdef CONFIG_BSD_DISKLABEL
Tejun Heo1493bf22010-05-15 20:09:30 +0200326 parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#endif
328}
329
Tejun Heo1493bf22010-05-15 20:09:30 +0200330static void parse_netbsd(struct parsed_partitions *state,
331 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333#ifdef CONFIG_BSD_DISKLABEL
Tejun Heo1493bf22010-05-15 20:09:30 +0200334 parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#endif
336}
337
Tejun Heo1493bf22010-05-15 20:09:30 +0200338static void parse_openbsd(struct parsed_partitions *state,
339 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341#ifdef CONFIG_BSD_DISKLABEL
Tejun Heo1493bf22010-05-15 20:09:30 +0200342 parse_bsd(state, offset, size, origin, "openbsd",
343 OPENBSD_MAXPARTITIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#endif
345}
346
347/*
348 * Create devices for Unixware partitions listed in a disklabel, under a
349 * dos-like partition. See parse_extended() for more information.
350 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200351static void parse_unixware(struct parsed_partitions *state,
352 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354#ifdef CONFIG_UNIXWARE_DISKLABEL
355 Sector sect;
356 struct unixware_disklabel *l;
357 struct unixware_slice *p;
358
Tejun Heo1493bf22010-05-15 20:09:30 +0200359 l = read_part_sector(state, offset + 29, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!l)
361 return;
362 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
363 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
364 put_dev_sector(sect);
365 return;
366 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700367 {
368 char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
369
370 snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
371 strlcat(state->pp_buf, tmp, PAGE_SIZE);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 p = &l->vtoc.v_slice[1];
374 /* I omit the 0th slice as it is the same as whole disk. */
375 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
376 if (state->next == state->limit)
377 break;
378
379 if (p->s_label != UNIXWARE_FS_UNUSED)
380 put_partition(state, state->next++,
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700381 le32_to_cpu(p->start_sect),
382 le32_to_cpu(p->nr_sects));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 p++;
384 }
385 put_dev_sector(sect);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700386 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#endif
388}
389
390/*
391 * Minix 2.0.0/2.0.2 subpartition support.
392 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
393 * Rajeev V. Pillai <rajeevvp@yahoo.com>
394 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200395static void parse_minix(struct parsed_partitions *state,
396 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398#ifdef CONFIG_MINIX_SUBPARTITION
399 Sector sect;
400 unsigned char *data;
401 struct partition *p;
402 int i;
403
Tejun Heo1493bf22010-05-15 20:09:30 +0200404 data = read_part_sector(state, offset, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (!data)
406 return;
407
408 p = (struct partition *)(data + 0x1be);
409
410 /* The first sector of a Minix partition can have either
411 * a secondary MBR describing its subpartitions, or
412 * the normal boot sector. */
Fabian Frederickdce14c22014-06-12 20:16:57 +0200413 if (msdos_magic_present(data + 510) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700415 char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700417 snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
418 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
420 if (state->next == state->limit)
421 break;
422 /* add each partition in use */
423 if (SYS_IND(p) == MINIX_PARTITION)
424 put_partition(state, state->next++,
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700425 start_sect(p), nr_sects(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700427 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 put_dev_sector(sect);
430#endif /* CONFIG_MINIX_SUBPARTITION */
431}
432
433static struct {
434 unsigned char id;
Tejun Heo1493bf22010-05-15 20:09:30 +0200435 void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436} subtypes[] = {
437 {FREEBSD_PARTITION, parse_freebsd},
438 {NETBSD_PARTITION, parse_netbsd},
439 {OPENBSD_PARTITION, parse_openbsd},
440 {MINIX_PARTITION, parse_minix},
441 {UNIXWARE_PARTITION, parse_unixware},
442 {SOLARIS_X86_PARTITION, parse_solaris_x86},
443 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
444 {0, NULL},
445};
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700446
Tejun Heo1493bf22010-05-15 20:09:30 +0200447int msdos_partition(struct parsed_partitions *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Tejun Heo1493bf22010-05-15 20:09:30 +0200449 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 Sector sect;
451 unsigned char *data;
452 struct partition *p;
Frank Seidel0607fd02008-04-28 02:16:31 -0700453 struct fat_boot_sector *fb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int slot;
Stephen Warrend33b98f2012-11-08 16:12:28 -0800455 u32 disksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Tejun Heo1493bf22010-05-15 20:09:30 +0200457 data = read_part_sector(state, 0, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (!data)
459 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Philippe De Muyter86ee8ba2013-02-27 17:05:16 -0800461 /*
462 * Note order! (some AIX disks, e.g. unbootable kind,
463 * have no MSDOS 55aa)
464 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200465 if (aix_magic_present(state, data)) {
Olaf Heringe1dfa922006-09-29 01:59:39 -0700466 put_dev_sector(sect);
Philippe De Muyterf8f06602013-07-08 16:01:30 -0700467#ifdef CONFIG_AIX_PARTITION
468 return aix_partition(state);
469#else
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700470 strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
Olaf Heringe1dfa922006-09-29 01:59:39 -0700471 return 0;
Philippe De Muyterf8f06602013-07-08 16:01:30 -0700472#endif
Olaf Heringe1dfa922006-09-29 01:59:39 -0700473 }
474
Philippe De Muyter86ee8ba2013-02-27 17:05:16 -0800475 if (!msdos_magic_present(data + 510)) {
476 put_dev_sector(sect);
477 return 0;
478 }
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
481 * Now that the 55aa signature is present, this is probably
482 * either the boot sector of a FAT filesystem or a DOS-type
483 * partition table. Reject this in case the boot indicator
484 * is not 0 or 0x80.
485 */
486 p = (struct partition *) (data + 0x1be);
487 for (slot = 1; slot <= 4; slot++, p++) {
488 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
Frank Seidel0607fd02008-04-28 02:16:31 -0700489 /*
490 * Even without a valid boot inidicator value
491 * its still possible this is valid FAT filesystem
492 * without a partition table.
493 */
494 fb = (struct fat_boot_sector *) data;
495 if (slot == 1 && fb->reserved && fb->fats
496 && fat_valid_media(fb->media)) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700497 strlcat(state->pp_buf, "\n", PAGE_SIZE);
Frank Seidel0607fd02008-04-28 02:16:31 -0700498 put_dev_sector(sect);
499 return 1;
500 } else {
501 put_dev_sector(sect);
502 return 0;
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 }
506
507#ifdef CONFIG_EFI_PARTITION
508 p = (struct partition *) (data + 0x1be);
509 for (slot = 1 ; slot <= 4 ; slot++, p++) {
510 /* If this is an EFI GPT disk, msdos should ignore it. */
511 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
512 put_dev_sector(sect);
513 return 0;
514 }
515 }
516#endif
517 p = (struct partition *) (data + 0x1be);
518
Stephen Warrend33b98f2012-11-08 16:12:28 -0800519 disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
522 * Look for partitions in two passes:
523 * First find the primary and DOS-type extended partitions.
524 * On the second pass look inside *BSD, Unixware and Solaris partitions.
525 */
526
527 state->next = 5;
528 for (slot = 1 ; slot <= 4 ; slot++, p++) {
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700529 sector_t start = start_sect(p)*sector_size;
530 sector_t size = nr_sects(p)*sector_size;
Fabian Frederickdce14c22014-06-12 20:16:57 +0200531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!size)
533 continue;
534 if (is_extended_partition(p)) {
OGAWA Hirofumi8e0cc812010-03-23 13:35:50 -0700535 /*
536 * prevent someone doing mkfs or mkswap on an
537 * extended partition, but leave room for LILO
538 * FIXME: this uses one logical sector for > 512b
539 * sector, although it may not be enough/proper.
540 */
541 sector_t n = 2;
Fabian Frederickdce14c22014-06-12 20:16:57 +0200542
OGAWA Hirofumi8e0cc812010-03-23 13:35:50 -0700543 n = min(size, max(sector_size, n));
544 put_partition(state, slot, start, n);
545
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700546 strlcat(state->pp_buf, " <", PAGE_SIZE);
Stephen Warrend33b98f2012-11-08 16:12:28 -0800547 parse_extended(state, start, size, disksig);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700548 strlcat(state->pp_buf, " >", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 continue;
550 }
551 put_partition(state, slot, start, size);
Stephen Warrend33b98f2012-11-08 16:12:28 -0800552 set_info(state, slot, disksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (SYS_IND(p) == LINUX_RAID_PARTITION)
Cesar Eduardo Barroscc910622010-04-17 19:28:09 -0300554 state->parts[slot].flags = ADDPART_FLAG_RAID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (SYS_IND(p) == DM6_PARTITION)
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700556 strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (SYS_IND(p) == EZD_PARTITION)
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700558 strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700561 strlcat(state->pp_buf, "\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* second pass - output for each on a separate line */
564 p = (struct partition *) (0x1be + data);
565 for (slot = 1 ; slot <= 4 ; slot++, p++) {
566 unsigned char id = SYS_IND(p);
567 int n;
568
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700569 if (!nr_sects(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 continue;
571
572 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
573 ;
574
575 if (!subtypes[n].parse)
576 continue;
Tejun Heo1493bf22010-05-15 20:09:30 +0200577 subtypes[n].parse(state, start_sect(p) * sector_size,
578 nr_sects(p) * sector_size, slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 put_dev_sector(sect);
581 return 1;
582}