blob: 8f2fcc0802642bec8edaaaea963d9a7be954d9bb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * fs/partitions/msdos.c
4 *
5 * Code extracted from drivers/block/genhd.c
6 * Copyright (C) 1991-1998 Linus Torvalds
7 *
8 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
9 * in the early extended-partition checks and added DM partitions
10 *
11 * Support for DiskManager v6.0x added by Mark Lord,
12 * with information provided by OnTrack. This now works for linux fdisk
13 * and LILO, as well as loadlin and bootln. Note that disks other than
14 * /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
15 *
16 * More flexible handling of extended partitions - aeb, 950831
17 *
18 * Check partition table on IDE disks for common CHS translations
19 *
20 * Re-organised Feb 1998 Russell King
Christoph Hellwig3f4fc592020-03-24 08:25:29 +010021 *
22 * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
23 * updated by Marc Espie <Marc.Espie@openbsd.org>
24 *
25 * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
26 * and Krzysztof G. Baranowski <kgb@knm.org.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
Frank Seidel0607fd02008-04-28 02:16:31 -070028#include <linux/msdos_fs.h>
Christoph Hellwig1442f762020-03-24 08:25:26 +010029#include <linux/msdos_partition.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "check.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "efi.h"
33
34/*
35 * Many architectures don't like unaligned accesses, while
36 * the nr_sects and start_sect partition table entries are
37 * at a 2 (mod 4) address.
38 */
39#include <asm/unaligned.h>
40
Daniel Taylor3fbf5862010-03-23 13:35:50 -070041#define SYS_IND(p) get_unaligned(&p->sys_ind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Christoph Hellwig1442f762020-03-24 08:25:26 +010043static inline sector_t nr_sects(struct msdos_partition *p)
Daniel Taylor3fbf5862010-03-23 13:35:50 -070044{
45 return (sector_t)get_unaligned_le32(&p->nr_sects);
46}
47
Christoph Hellwig1442f762020-03-24 08:25:26 +010048static inline sector_t start_sect(struct msdos_partition *p)
Daniel Taylor3fbf5862010-03-23 13:35:50 -070049{
50 return (sector_t)get_unaligned_le32(&p->start_sect);
51}
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Christoph Hellwig1442f762020-03-24 08:25:26 +010053static inline int is_extended_partition(struct msdos_partition *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
56 SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
57 SYS_IND(p) == LINUX_EXTENDED_PARTITION);
58}
59
60#define MSDOS_LABEL_MAGIC1 0x55
61#define MSDOS_LABEL_MAGIC2 0xAA
62
63static inline int
64msdos_magic_present(unsigned char *p)
65{
66 return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
67}
68
Olaf Heringe1dfa922006-09-29 01:59:39 -070069/* Value is EBCDIC 'IBMA' */
70#define AIX_LABEL_MAGIC1 0xC9
71#define AIX_LABEL_MAGIC2 0xC2
72#define AIX_LABEL_MAGIC3 0xD4
73#define AIX_LABEL_MAGIC4 0xC1
Tejun Heo1493bf22010-05-15 20:09:30 +020074static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
Olaf Heringe1dfa922006-09-29 01:59:39 -070075{
Christoph Hellwig1442f762020-03-24 08:25:26 +010076 struct msdos_partition *pt = (struct msdos_partition *) (p + 0x1be);
Olaf Heringe1dfa922006-09-29 01:59:39 -070077 Sector sect;
78 unsigned char *d;
Olaf Hering4419d1a2007-02-10 01:45:47 -080079 int slot, ret = 0;
Olaf Heringe1dfa922006-09-29 01:59:39 -070080
Olaf Heringa470e182007-02-10 01:45:48 -080081 if (!(p[0] == AIX_LABEL_MAGIC1 &&
82 p[1] == AIX_LABEL_MAGIC2 &&
83 p[2] == AIX_LABEL_MAGIC3 &&
84 p[3] == AIX_LABEL_MAGIC4))
Olaf Heringe1dfa922006-09-29 01:59:39 -070085 return 0;
Christoph Hellwigcb0ab522020-03-24 08:25:28 +010086
87 /*
88 * Assume the partition table is valid if Linux partitions exists.
89 * Note that old Solaris/x86 partitions use the same indicator as
90 * Linux swap partitions, so we consider that a Linux partition as
91 * well.
92 */
Olaf Hering4419d1a2007-02-10 01:45:47 -080093 for (slot = 1; slot <= 4; slot++, pt++) {
Christoph Hellwigcb0ab522020-03-24 08:25:28 +010094 if (pt->sys_ind == SOLARIS_X86_PARTITION ||
95 pt->sys_ind == LINUX_RAID_PARTITION ||
96 pt->sys_ind == LINUX_DATA_PARTITION ||
97 pt->sys_ind == LINUX_LVM_PARTITION ||
98 is_extended_partition(pt))
Olaf Hering4419d1a2007-02-10 01:45:47 -080099 return 0;
100 }
Tejun Heo1493bf22010-05-15 20:09:30 +0200101 d = read_part_sector(state, 7, &sect);
Olaf Heringe1dfa922006-09-29 01:59:39 -0700102 if (d) {
103 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
104 ret = 1;
105 put_dev_sector(sect);
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700106 }
Olaf Heringe1dfa922006-09-29 01:59:39 -0700107 return ret;
108}
109
Stephen Warrend33b98f2012-11-08 16:12:28 -0800110static void set_info(struct parsed_partitions *state, int slot,
111 u32 disksig)
112{
113 struct partition_meta_info *info = &state->parts[slot].info;
114
115 snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
116 slot);
117 info->volname[0] = 0;
118 state->parts[slot].has_info = true;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
122 * Create devices for each logical partition in an extended partition.
123 * The logical partitions form a linked list, with each entry being
124 * a partition table with two entries. The first entry
125 * is the real data partition (with a start relative to the partition
126 * table start). The second is a pointer to the next logical partition
127 * (with a start relative to the entire extended partition).
128 * We do not create a Linux partition for the partition tables, but
129 * only for the actual data partitions.
130 */
131
Tejun Heo1493bf22010-05-15 20:09:30 +0200132static void parse_extended(struct parsed_partitions *state,
Stephen Warrend33b98f2012-11-08 16:12:28 -0800133 sector_t first_sector, sector_t first_size,
134 u32 disksig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Christoph Hellwig1442f762020-03-24 08:25:26 +0100136 struct msdos_partition *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 Sector sect;
138 unsigned char *data;
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700139 sector_t this_sector, this_size;
Tejun Heo1493bf22010-05-15 20:09:30 +0200140 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int loopct = 0; /* number of links followed
142 without finding a data partition */
143 int i;
144
145 this_sector = first_sector;
146 this_size = first_size;
147
148 while (1) {
149 if (++loopct > 100)
150 return;
151 if (state->next == state->limit)
152 return;
Tejun Heo1493bf22010-05-15 20:09:30 +0200153 data = read_part_sector(state, this_sector, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!data)
155 return;
156
157 if (!msdos_magic_present(data + 510))
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700158 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Christoph Hellwig1442f762020-03-24 08:25:26 +0100160 p = (struct msdos_partition *) (data + 0x1be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /*
163 * Usually, the first entry is the real data partition,
164 * the 2nd entry is the next extended partition, or empty,
165 * and the 3rd and 4th entries are unused.
166 * However, DRDOS sometimes has the extended partition as
167 * the first entry (when the data partition is empty),
168 * and OS/2 seems to use all four entries.
169 */
170
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700171 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * First process the data partition(s)
173 */
Fabian Frederickdce14c22014-06-12 20:16:57 +0200174 for (i = 0; i < 4; i++, p++) {
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700175 sector_t offs, size, next;
Fabian Frederickdce14c22014-06-12 20:16:57 +0200176
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700177 if (!nr_sects(p) || is_extended_partition(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 continue;
179
180 /* Check the 3rd and 4th entries -
181 these sometimes contain random garbage */
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700182 offs = start_sect(p)*sector_size;
183 size = nr_sects(p)*sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 next = this_sector + offs;
185 if (i >= 2) {
186 if (offs + size > this_size)
187 continue;
188 if (next < first_sector)
189 continue;
190 if (next + size > first_sector + first_size)
191 continue;
192 }
193
194 put_partition(state, state->next, next, size);
Stephen Warrend33b98f2012-11-08 16:12:28 -0800195 set_info(state, state->next, disksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (SYS_IND(p) == LINUX_RAID_PARTITION)
Fabio Massimo Di Nittod18d7682007-02-10 23:50:00 -0800197 state->parts[state->next].flags = ADDPART_FLAG_RAID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 loopct = 0;
199 if (++state->next == state->limit)
200 goto done;
201 }
202 /*
203 * Next, process the (first) extended partition, if present.
204 * (So far, there seems to be no reason to make
205 * parse_extended() recursive and allow a tree
206 * of extended partitions.)
207 * It should be a link to the next logical partition.
208 */
209 p -= 4;
Fabian Frederickdce14c22014-06-12 20:16:57 +0200210 for (i = 0; i < 4; i++, p++)
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700211 if (nr_sects(p) && is_extended_partition(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 break;
213 if (i == 4)
214 goto done; /* nothing left to do */
215
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700216 this_sector = first_sector + start_sect(p) * sector_size;
217 this_size = nr_sects(p) * sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 put_dev_sector(sect);
219 }
220done:
221 put_dev_sector(sect);
222}
223
Christoph Hellwig3f4fc592020-03-24 08:25:29 +0100224#define SOLARIS_X86_NUMSLICE 16
225#define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL)
226
227struct solaris_x86_slice {
228 __le16 s_tag; /* ID tag of partition */
229 __le16 s_flag; /* permission flags */
230 __le32 s_start; /* start sector no of partition */
231 __le32 s_size; /* # of blocks in partition */
232};
233
234struct solaris_x86_vtoc {
235 unsigned int v_bootinfo[3]; /* info needed by mboot */
236 __le32 v_sanity; /* to verify vtoc sanity */
237 __le32 v_version; /* layout version */
238 char v_volume[8]; /* volume name */
239 __le16 v_sectorsz; /* sector size in bytes */
240 __le16 v_nparts; /* number of partitions */
241 unsigned int v_reserved[10]; /* free space */
242 struct solaris_x86_slice
243 v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
244 unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp */
245 char v_asciilabel[128]; /* for compatibility */
246};
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
249 indicates linux swap. Be careful before believing this is Solaris. */
250
Tejun Heo1493bf22010-05-15 20:09:30 +0200251static void parse_solaris_x86(struct parsed_partitions *state,
252 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254#ifdef CONFIG_SOLARIS_X86_PARTITION
255 Sector sect;
256 struct solaris_x86_vtoc *v;
257 int i;
Mark Fortescueb84d8792007-07-25 18:30:08 -0700258 short max_nparts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Tejun Heo1493bf22010-05-15 20:09:30 +0200260 v = read_part_sector(state, offset + 1, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!v)
262 return;
263 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
264 put_dev_sector(sect);
265 return;
266 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700267 {
268 char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
269
270 snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
271 strlcat(state->pp_buf, tmp, PAGE_SIZE);
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (le32_to_cpu(v->v_version) != 1) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700274 char tmp[64];
275
276 snprintf(tmp, sizeof(tmp), " cannot handle version %d vtoc>\n",
277 le32_to_cpu(v->v_version));
278 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 put_dev_sector(sect);
280 return;
281 }
Mark Fortescueb84d8792007-07-25 18:30:08 -0700282 /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
Fabian Frederickdce14c22014-06-12 20:16:57 +0200283 max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
284 for (i = 0; i < max_nparts && state->next < state->limit; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct solaris_x86_slice *s = &v->v_slice[i];
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700286 char tmp[3 + 10 + 1 + 1];
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (s->s_size == 0)
289 continue;
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700290 snprintf(tmp, sizeof(tmp), " [s%d]", i);
291 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* solaris partitions are relative to current MS-DOS
293 * one; must add the offset of the current partition */
294 put_partition(state, state->next++,
295 le32_to_cpu(s->s_start)+offset,
296 le32_to_cpu(s->s_size));
297 }
298 put_dev_sector(sect);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700299 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#endif
301}
302
Christoph Hellwig3f4fc592020-03-24 08:25:29 +0100303/* check against BSD src/sys/sys/disklabel.h for consistency */
304#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
305#define BSD_MAXPARTITIONS 16
306#define OPENBSD_MAXPARTITIONS 16
307#define BSD_FS_UNUSED 0 /* disklabel unused partition entry ID */
308struct bsd_disklabel {
309 __le32 d_magic; /* the magic number */
310 __s16 d_type; /* drive type */
311 __s16 d_subtype; /* controller/d_type specific */
312 char d_typename[16]; /* type name, e.g. "eagle" */
313 char d_packname[16]; /* pack identifier */
314 __u32 d_secsize; /* # of bytes per sector */
315 __u32 d_nsectors; /* # of data sectors per track */
316 __u32 d_ntracks; /* # of tracks per cylinder */
317 __u32 d_ncylinders; /* # of data cylinders per unit */
318 __u32 d_secpercyl; /* # of data sectors per cylinder */
319 __u32 d_secperunit; /* # of data sectors per unit */
320 __u16 d_sparespertrack; /* # of spare sectors per track */
321 __u16 d_sparespercyl; /* # of spare sectors per cylinder */
322 __u32 d_acylinders; /* # of alt. cylinders per unit */
323 __u16 d_rpm; /* rotational speed */
324 __u16 d_interleave; /* hardware sector interleave */
325 __u16 d_trackskew; /* sector 0 skew, per track */
326 __u16 d_cylskew; /* sector 0 skew, per cylinder */
327 __u32 d_headswitch; /* head switch time, usec */
328 __u32 d_trkseek; /* track-to-track seek, usec */
329 __u32 d_flags; /* generic flags */
330#define NDDATA 5
331 __u32 d_drivedata[NDDATA]; /* drive-type specific information */
332#define NSPARE 5
333 __u32 d_spare[NSPARE]; /* reserved for future use */
334 __le32 d_magic2; /* the magic number (again) */
335 __le16 d_checksum; /* xor of data incl. partitions */
336
337 /* filesystem and partition information: */
338 __le16 d_npartitions; /* number of partitions in following */
339 __le32 d_bbsize; /* size of boot area at sn0, bytes */
340 __le32 d_sbsize; /* max size of fs superblock, bytes */
341 struct bsd_partition { /* the partition table */
342 __le32 p_size; /* number of sectors in partition */
343 __le32 p_offset; /* starting sector */
344 __le32 p_fsize; /* filesystem basic fragment size */
345 __u8 p_fstype; /* filesystem type, see below */
346 __u8 p_frag; /* filesystem fragments per block */
347 __le16 p_cpg; /* filesystem cylinders per group */
348 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
349};
350
Adrian Bunk486fd402005-06-25 14:58:47 -0700351#if defined(CONFIG_BSD_DISKLABEL)
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700352/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * Create devices for BSD partitions listed in a disklabel, under a
354 * dos-like partition. See parse_extended() for more information.
355 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200356static void parse_bsd(struct parsed_partitions *state,
357 sector_t offset, sector_t size, int origin, char *flavour,
358 int max_partitions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 Sector sect;
361 struct bsd_disklabel *l;
362 struct bsd_partition *p;
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700363 char tmp[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Tejun Heo1493bf22010-05-15 20:09:30 +0200365 l = read_part_sector(state, offset + 1, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (!l)
367 return;
368 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
369 put_dev_sector(sect);
370 return;
371 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700372
373 snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
374 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (le16_to_cpu(l->d_npartitions) < max_partitions)
377 max_partitions = le16_to_cpu(l->d_npartitions);
378 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700379 sector_t bsd_start, bsd_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (state->next == state->limit)
382 break;
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700383 if (p->p_fstype == BSD_FS_UNUSED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 continue;
385 bsd_start = le32_to_cpu(p->p_offset);
386 bsd_size = le32_to_cpu(p->p_size);
Richard Narron5f156842018-01-10 09:12:16 -0700387 /* FreeBSD has relative offset if C partition offset is zero */
388 if (memcmp(flavour, "bsd\0", 4) == 0 &&
389 le32_to_cpu(l->d_partitions[2].p_offset) == 0)
Richard22322032017-05-21 12:27:00 -0700390 bsd_start += offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (offset == bsd_start && size == bsd_size)
392 /* full parent partition, we have it already */
393 continue;
394 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700395 strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 continue;
397 }
398 put_partition(state, state->next++, bsd_start, bsd_size);
399 }
400 put_dev_sector(sect);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700401 if (le16_to_cpu(l->d_npartitions) > max_partitions) {
402 snprintf(tmp, sizeof(tmp), " (ignored %d more)",
403 le16_to_cpu(l->d_npartitions) - max_partitions);
404 strlcat(state->pp_buf, tmp, PAGE_SIZE);
405 }
406 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408#endif
409
Tejun Heo1493bf22010-05-15 20:09:30 +0200410static void parse_freebsd(struct parsed_partitions *state,
411 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413#ifdef CONFIG_BSD_DISKLABEL
Tejun Heo1493bf22010-05-15 20:09:30 +0200414 parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#endif
416}
417
Tejun Heo1493bf22010-05-15 20:09:30 +0200418static void parse_netbsd(struct parsed_partitions *state,
419 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421#ifdef CONFIG_BSD_DISKLABEL
Tejun Heo1493bf22010-05-15 20:09:30 +0200422 parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#endif
424}
425
Tejun Heo1493bf22010-05-15 20:09:30 +0200426static void parse_openbsd(struct parsed_partitions *state,
427 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429#ifdef CONFIG_BSD_DISKLABEL
Tejun Heo1493bf22010-05-15 20:09:30 +0200430 parse_bsd(state, offset, size, origin, "openbsd",
431 OPENBSD_MAXPARTITIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432#endif
433}
434
Christoph Hellwig3f4fc592020-03-24 08:25:29 +0100435#define UNIXWARE_DISKMAGIC (0xCA5E600DUL) /* The disk magic number */
436#define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL) /* The slice table magic nr */
437#define UNIXWARE_NUMSLICE 16
438#define UNIXWARE_FS_UNUSED 0 /* Unused slice entry ID */
439
440struct unixware_slice {
441 __le16 s_label; /* label */
442 __le16 s_flags; /* permission flags */
443 __le32 start_sect; /* starting sector */
444 __le32 nr_sects; /* number of sectors in slice */
445};
446
447struct unixware_disklabel {
448 __le32 d_type; /* drive type */
449 __le32 d_magic; /* the magic number */
450 __le32 d_version; /* version number */
451 char d_serial[12]; /* serial number of the device */
452 __le32 d_ncylinders; /* # of data cylinders per device */
453 __le32 d_ntracks; /* # of tracks per cylinder */
454 __le32 d_nsectors; /* # of data sectors per track */
455 __le32 d_secsize; /* # of bytes per sector */
456 __le32 d_part_start; /* # of first sector of this partition*/
457 __le32 d_unknown1[12]; /* ? */
458 __le32 d_alt_tbl; /* byte offset of alternate table */
459 __le32 d_alt_len; /* byte length of alternate table */
460 __le32 d_phys_cyl; /* # of physical cylinders per device */
461 __le32 d_phys_trk; /* # of physical tracks per cylinder */
462 __le32 d_phys_sec; /* # of physical sectors per track */
463 __le32 d_phys_bytes; /* # of physical bytes per sector */
464 __le32 d_unknown2; /* ? */
465 __le32 d_unknown3; /* ? */
466 __le32 d_pad[8]; /* pad */
467
468 struct unixware_vtoc {
469 __le32 v_magic; /* the magic number */
470 __le32 v_version; /* version number */
471 char v_name[8]; /* volume name */
472 __le16 v_nslices; /* # of slices */
473 __le16 v_unknown1; /* ? */
474 __le32 v_reserved[10]; /* reserved */
475 struct unixware_slice
476 v_slice[UNIXWARE_NUMSLICE]; /* slice headers */
477 } vtoc;
478}; /* 408 */
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
481 * Create devices for Unixware partitions listed in a disklabel, under a
482 * dos-like partition. See parse_extended() for more information.
483 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200484static void parse_unixware(struct parsed_partitions *state,
485 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487#ifdef CONFIG_UNIXWARE_DISKLABEL
488 Sector sect;
489 struct unixware_disklabel *l;
490 struct unixware_slice *p;
491
Tejun Heo1493bf22010-05-15 20:09:30 +0200492 l = read_part_sector(state, offset + 29, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (!l)
494 return;
495 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
496 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
497 put_dev_sector(sect);
498 return;
499 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700500 {
501 char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
502
503 snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
504 strlcat(state->pp_buf, tmp, PAGE_SIZE);
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 p = &l->vtoc.v_slice[1];
507 /* I omit the 0th slice as it is the same as whole disk. */
508 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
509 if (state->next == state->limit)
510 break;
511
512 if (p->s_label != UNIXWARE_FS_UNUSED)
513 put_partition(state, state->next++,
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700514 le32_to_cpu(p->start_sect),
515 le32_to_cpu(p->nr_sects));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 p++;
517 }
518 put_dev_sector(sect);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700519 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#endif
521}
522
Christoph Hellwig3f4fc592020-03-24 08:25:29 +0100523#define MINIX_NR_SUBPARTITIONS 4
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/*
526 * Minix 2.0.0/2.0.2 subpartition support.
527 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
528 * Rajeev V. Pillai <rajeevvp@yahoo.com>
529 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200530static void parse_minix(struct parsed_partitions *state,
531 sector_t offset, sector_t size, int origin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533#ifdef CONFIG_MINIX_SUBPARTITION
534 Sector sect;
535 unsigned char *data;
Christoph Hellwig1442f762020-03-24 08:25:26 +0100536 struct msdos_partition *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int i;
538
Tejun Heo1493bf22010-05-15 20:09:30 +0200539 data = read_part_sector(state, offset, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (!data)
541 return;
542
Christoph Hellwig1442f762020-03-24 08:25:26 +0100543 p = (struct msdos_partition *)(data + 0x1be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* The first sector of a Minix partition can have either
546 * a secondary MBR describing its subpartitions, or
547 * the normal boot sector. */
Fabian Frederickdce14c22014-06-12 20:16:57 +0200548 if (msdos_magic_present(data + 510) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700550 char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700552 snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
553 strlcat(state->pp_buf, tmp, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
555 if (state->next == state->limit)
556 break;
557 /* add each partition in use */
558 if (SYS_IND(p) == MINIX_PARTITION)
559 put_partition(state, state->next++,
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700560 start_sect(p), nr_sects(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700562 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 put_dev_sector(sect);
565#endif /* CONFIG_MINIX_SUBPARTITION */
566}
567
568static struct {
569 unsigned char id;
Tejun Heo1493bf22010-05-15 20:09:30 +0200570 void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571} subtypes[] = {
572 {FREEBSD_PARTITION, parse_freebsd},
573 {NETBSD_PARTITION, parse_netbsd},
574 {OPENBSD_PARTITION, parse_openbsd},
575 {MINIX_PARTITION, parse_minix},
576 {UNIXWARE_PARTITION, parse_unixware},
577 {SOLARIS_X86_PARTITION, parse_solaris_x86},
578 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
579 {0, NULL},
580};
Philippe De Muyter1d04f3c2013-07-08 16:01:28 -0700581
Tejun Heo1493bf22010-05-15 20:09:30 +0200582int msdos_partition(struct parsed_partitions *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Tejun Heo1493bf22010-05-15 20:09:30 +0200584 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 Sector sect;
586 unsigned char *data;
Christoph Hellwig1442f762020-03-24 08:25:26 +0100587 struct msdos_partition *p;
Frank Seidel0607fd02008-04-28 02:16:31 -0700588 struct fat_boot_sector *fb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int slot;
Stephen Warrend33b98f2012-11-08 16:12:28 -0800590 u32 disksig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Tejun Heo1493bf22010-05-15 20:09:30 +0200592 data = read_part_sector(state, 0, &sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!data)
594 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Philippe De Muyter86ee8ba2013-02-27 17:05:16 -0800596 /*
597 * Note order! (some AIX disks, e.g. unbootable kind,
598 * have no MSDOS 55aa)
599 */
Tejun Heo1493bf22010-05-15 20:09:30 +0200600 if (aix_magic_present(state, data)) {
Olaf Heringe1dfa922006-09-29 01:59:39 -0700601 put_dev_sector(sect);
Philippe De Muyterf8f06602013-07-08 16:01:30 -0700602#ifdef CONFIG_AIX_PARTITION
603 return aix_partition(state);
604#else
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700605 strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
Olaf Heringe1dfa922006-09-29 01:59:39 -0700606 return 0;
Philippe De Muyterf8f06602013-07-08 16:01:30 -0700607#endif
Olaf Heringe1dfa922006-09-29 01:59:39 -0700608 }
609
Philippe De Muyter86ee8ba2013-02-27 17:05:16 -0800610 if (!msdos_magic_present(data + 510)) {
611 put_dev_sector(sect);
612 return 0;
613 }
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * Now that the 55aa signature is present, this is probably
617 * either the boot sector of a FAT filesystem or a DOS-type
618 * partition table. Reject this in case the boot indicator
619 * is not 0 or 0x80.
620 */
Christoph Hellwig1442f762020-03-24 08:25:26 +0100621 p = (struct msdos_partition *) (data + 0x1be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 for (slot = 1; slot <= 4; slot++, p++) {
623 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
Frank Seidel0607fd02008-04-28 02:16:31 -0700624 /*
625 * Even without a valid boot inidicator value
626 * its still possible this is valid FAT filesystem
627 * without a partition table.
628 */
629 fb = (struct fat_boot_sector *) data;
630 if (slot == 1 && fb->reserved && fb->fats
631 && fat_valid_media(fb->media)) {
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700632 strlcat(state->pp_buf, "\n", PAGE_SIZE);
Frank Seidel0607fd02008-04-28 02:16:31 -0700633 put_dev_sector(sect);
634 return 1;
635 } else {
636 put_dev_sector(sect);
637 return 0;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 }
641
642#ifdef CONFIG_EFI_PARTITION
Christoph Hellwig1442f762020-03-24 08:25:26 +0100643 p = (struct msdos_partition *) (data + 0x1be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 for (slot = 1 ; slot <= 4 ; slot++, p++) {
645 /* If this is an EFI GPT disk, msdos should ignore it. */
646 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
647 put_dev_sector(sect);
648 return 0;
649 }
650 }
651#endif
Christoph Hellwig1442f762020-03-24 08:25:26 +0100652 p = (struct msdos_partition *) (data + 0x1be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Stephen Warrend33b98f2012-11-08 16:12:28 -0800654 disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
657 * Look for partitions in two passes:
658 * First find the primary and DOS-type extended partitions.
659 * On the second pass look inside *BSD, Unixware and Solaris partitions.
660 */
661
662 state->next = 5;
663 for (slot = 1 ; slot <= 4 ; slot++, p++) {
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700664 sector_t start = start_sect(p)*sector_size;
665 sector_t size = nr_sects(p)*sector_size;
Fabian Frederickdce14c22014-06-12 20:16:57 +0200666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!size)
668 continue;
669 if (is_extended_partition(p)) {
OGAWA Hirofumi8e0cc812010-03-23 13:35:50 -0700670 /*
671 * prevent someone doing mkfs or mkswap on an
672 * extended partition, but leave room for LILO
673 * FIXME: this uses one logical sector for > 512b
674 * sector, although it may not be enough/proper.
675 */
676 sector_t n = 2;
Fabian Frederickdce14c22014-06-12 20:16:57 +0200677
OGAWA Hirofumi8e0cc812010-03-23 13:35:50 -0700678 n = min(size, max(sector_size, n));
679 put_partition(state, slot, start, n);
680
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700681 strlcat(state->pp_buf, " <", PAGE_SIZE);
Stephen Warrend33b98f2012-11-08 16:12:28 -0800682 parse_extended(state, start, size, disksig);
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700683 strlcat(state->pp_buf, " >", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 continue;
685 }
686 put_partition(state, slot, start, size);
Stephen Warrend33b98f2012-11-08 16:12:28 -0800687 set_info(state, slot, disksig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (SYS_IND(p) == LINUX_RAID_PARTITION)
Cesar Eduardo Barroscc910622010-04-17 19:28:09 -0300689 state->parts[slot].flags = ADDPART_FLAG_RAID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (SYS_IND(p) == DM6_PARTITION)
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700691 strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (SYS_IND(p) == EZD_PARTITION)
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700693 strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695
Alexey Dobriyan9c867fb2010-08-10 18:03:14 -0700696 strlcat(state->pp_buf, "\n", PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* second pass - output for each on a separate line */
Christoph Hellwig1442f762020-03-24 08:25:26 +0100699 p = (struct msdos_partition *) (0x1be + data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 for (slot = 1 ; slot <= 4 ; slot++, p++) {
701 unsigned char id = SYS_IND(p);
702 int n;
703
Daniel Taylor3fbf5862010-03-23 13:35:50 -0700704 if (!nr_sects(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 continue;
706
707 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
708 ;
709
710 if (!subtypes[n].parse)
711 continue;
Tejun Heo1493bf22010-05-15 20:09:30 +0200712 subtypes[n].parse(state, start_sect(p) * sector_size,
713 nr_sects(p) * sector_size, slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 put_dev_sector(sect);
716 return 1;
717}