Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v3.5.6
 
  1/*
  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 */
 21#include <linux/msdos_fs.h>
 22
 23#include "check.h"
 24#include "msdos.h"
 25#include "efi.h"
 
 26
 27/*
 28 * Many architectures don't like unaligned accesses, while
 29 * the nr_sects and start_sect partition table entries are
 30 * at a 2 (mod 4) address.
 31 */
 32#include <asm/unaligned.h>
 33
 34#define SYS_IND(p)	get_unaligned(&p->sys_ind)
 35
 36static inline sector_t nr_sects(struct partition *p)
 37{
 38	return (sector_t)get_unaligned_le32(&p->nr_sects);
 39}
 40
 41static inline sector_t start_sect(struct partition *p)
 42{
 43	return (sector_t)get_unaligned_le32(&p->start_sect);
 44}
 45
 46static inline int is_extended_partition(struct partition *p)
 47{
 48	return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
 49		SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
 50		SYS_IND(p) == LINUX_EXTENDED_PARTITION);
 51}
 52
 53#define MSDOS_LABEL_MAGIC1	0x55
 54#define MSDOS_LABEL_MAGIC2	0xAA
 55
 56static inline int
 57msdos_magic_present(unsigned char *p)
 58{
 59	return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
 60}
 61
 62/* Value is EBCDIC 'IBMA' */
 63#define AIX_LABEL_MAGIC1	0xC9
 64#define AIX_LABEL_MAGIC2	0xC2
 65#define AIX_LABEL_MAGIC3	0xD4
 66#define AIX_LABEL_MAGIC4	0xC1
 67static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
 68{
 69	struct partition *pt = (struct partition *) (p + 0x1be);
 70	Sector sect;
 71	unsigned char *d;
 72	int slot, ret = 0;
 73
 74	if (!(p[0] == AIX_LABEL_MAGIC1 &&
 75		p[1] == AIX_LABEL_MAGIC2 &&
 76		p[2] == AIX_LABEL_MAGIC3 &&
 77		p[3] == AIX_LABEL_MAGIC4))
 78		return 0;
 79	/* Assume the partition table is valid if Linux partitions exists */
 80	for (slot = 1; slot <= 4; slot++, pt++) {
 81		if (pt->sys_ind == LINUX_SWAP_PARTITION ||
 82			pt->sys_ind == LINUX_RAID_PARTITION ||
 83			pt->sys_ind == LINUX_DATA_PARTITION ||
 84			pt->sys_ind == LINUX_LVM_PARTITION ||
 85			is_extended_partition(pt))
 86			return 0;
 87	}
 88	d = read_part_sector(state, 7, &sect);
 89	if (d) {
 90		if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
 91			ret = 1;
 92		put_dev_sector(sect);
 93	};
 94	return ret;
 95}
 96
 
 
 
 
 
 
 
 
 
 
 
 97/*
 98 * Create devices for each logical partition in an extended partition.
 99 * The logical partitions form a linked list, with each entry being
100 * a partition table with two entries.  The first entry
101 * is the real data partition (with a start relative to the partition
102 * table start).  The second is a pointer to the next logical partition
103 * (with a start relative to the entire extended partition).
104 * We do not create a Linux partition for the partition tables, but
105 * only for the actual data partitions.
106 */
107
108static void parse_extended(struct parsed_partitions *state,
109			   sector_t first_sector, sector_t first_size)
 
110{
111	struct partition *p;
112	Sector sect;
113	unsigned char *data;
114	sector_t this_sector, this_size;
115	sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
116	int loopct = 0;		/* number of links followed
117				   without finding a data partition */
118	int i;
119
120	this_sector = first_sector;
121	this_size = first_size;
122
123	while (1) {
124		if (++loopct > 100)
125			return;
126		if (state->next == state->limit)
127			return;
128		data = read_part_sector(state, this_sector, &sect);
129		if (!data)
130			return;
131
132		if (!msdos_magic_present(data + 510))
133			goto done; 
134
135		p = (struct partition *) (data + 0x1be);
136
137		/*
138		 * Usually, the first entry is the real data partition,
139		 * the 2nd entry is the next extended partition, or empty,
140		 * and the 3rd and 4th entries are unused.
141		 * However, DRDOS sometimes has the extended partition as
142		 * the first entry (when the data partition is empty),
143		 * and OS/2 seems to use all four entries.
144		 */
145
146		/* 
147		 * First process the data partition(s)
148		 */
149		for (i=0; i<4; i++, p++) {
150			sector_t offs, size, next;
 
151			if (!nr_sects(p) || is_extended_partition(p))
152				continue;
153
154			/* Check the 3rd and 4th entries -
155			   these sometimes contain random garbage */
156			offs = start_sect(p)*sector_size;
157			size = nr_sects(p)*sector_size;
158			next = this_sector + offs;
159			if (i >= 2) {
160				if (offs + size > this_size)
161					continue;
162				if (next < first_sector)
163					continue;
164				if (next + size > first_sector + first_size)
165					continue;
166			}
167
168			put_partition(state, state->next, next, size);
 
169			if (SYS_IND(p) == LINUX_RAID_PARTITION)
170				state->parts[state->next].flags = ADDPART_FLAG_RAID;
171			loopct = 0;
172			if (++state->next == state->limit)
173				goto done;
174		}
175		/*
176		 * Next, process the (first) extended partition, if present.
177		 * (So far, there seems to be no reason to make
178		 *  parse_extended()  recursive and allow a tree
179		 *  of extended partitions.)
180		 * It should be a link to the next logical partition.
181		 */
182		p -= 4;
183		for (i=0; i<4; i++, p++)
184			if (nr_sects(p) && is_extended_partition(p))
185				break;
186		if (i == 4)
187			goto done;	 /* nothing left to do */
188
189		this_sector = first_sector + start_sect(p) * sector_size;
190		this_size = nr_sects(p) * sector_size;
191		put_dev_sector(sect);
192	}
193done:
194	put_dev_sector(sect);
195}
196
197/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
198   indicates linux swap.  Be careful before believing this is Solaris. */
199
200static void parse_solaris_x86(struct parsed_partitions *state,
201			      sector_t offset, sector_t size, int origin)
202{
203#ifdef CONFIG_SOLARIS_X86_PARTITION
204	Sector sect;
205	struct solaris_x86_vtoc *v;
206	int i;
207	short max_nparts;
208
209	v = read_part_sector(state, offset + 1, &sect);
210	if (!v)
211		return;
212	if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
213		put_dev_sector(sect);
214		return;
215	}
216	{
217		char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
218
219		snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
220		strlcat(state->pp_buf, tmp, PAGE_SIZE);
221	}
222	if (le32_to_cpu(v->v_version) != 1) {
223		char tmp[64];
224
225		snprintf(tmp, sizeof(tmp), "  cannot handle version %d vtoc>\n",
226			 le32_to_cpu(v->v_version));
227		strlcat(state->pp_buf, tmp, PAGE_SIZE);
228		put_dev_sector(sect);
229		return;
230	}
231	/* Ensure we can handle previous case of VTOC with 8 entries gracefully */
232	max_nparts = le16_to_cpu (v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
233	for (i=0; i<max_nparts && state->next<state->limit; i++) {
234		struct solaris_x86_slice *s = &v->v_slice[i];
235		char tmp[3 + 10 + 1 + 1];
236
237		if (s->s_size == 0)
238			continue;
239		snprintf(tmp, sizeof(tmp), " [s%d]", i);
240		strlcat(state->pp_buf, tmp, PAGE_SIZE);
241		/* solaris partitions are relative to current MS-DOS
242		 * one; must add the offset of the current partition */
243		put_partition(state, state->next++,
244				 le32_to_cpu(s->s_start)+offset,
245				 le32_to_cpu(s->s_size));
246	}
247	put_dev_sector(sect);
248	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
249#endif
250}
251
252#if defined(CONFIG_BSD_DISKLABEL)
253/* 
254 * Create devices for BSD partitions listed in a disklabel, under a
255 * dos-like partition. See parse_extended() for more information.
256 */
257static void parse_bsd(struct parsed_partitions *state,
258		      sector_t offset, sector_t size, int origin, char *flavour,
259		      int max_partitions)
260{
261	Sector sect;
262	struct bsd_disklabel *l;
263	struct bsd_partition *p;
264	char tmp[64];
265
266	l = read_part_sector(state, offset + 1, &sect);
267	if (!l)
268		return;
269	if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
270		put_dev_sector(sect);
271		return;
272	}
273
274	snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
275	strlcat(state->pp_buf, tmp, PAGE_SIZE);
276
277	if (le16_to_cpu(l->d_npartitions) < max_partitions)
278		max_partitions = le16_to_cpu(l->d_npartitions);
279	for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
280		sector_t bsd_start, bsd_size;
281
282		if (state->next == state->limit)
283			break;
284		if (p->p_fstype == BSD_FS_UNUSED) 
285			continue;
286		bsd_start = le32_to_cpu(p->p_offset);
287		bsd_size = le32_to_cpu(p->p_size);
 
 
 
 
288		if (offset == bsd_start && size == bsd_size)
289			/* full parent partition, we have it already */
290			continue;
291		if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
292			strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
293			continue;
294		}
295		put_partition(state, state->next++, bsd_start, bsd_size);
296	}
297	put_dev_sector(sect);
298	if (le16_to_cpu(l->d_npartitions) > max_partitions) {
299		snprintf(tmp, sizeof(tmp), " (ignored %d more)",
300			 le16_to_cpu(l->d_npartitions) - max_partitions);
301		strlcat(state->pp_buf, tmp, PAGE_SIZE);
302	}
303	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
304}
305#endif
306
307static void parse_freebsd(struct parsed_partitions *state,
308			  sector_t offset, sector_t size, int origin)
309{
310#ifdef CONFIG_BSD_DISKLABEL
311	parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
312#endif
313}
314
315static void parse_netbsd(struct parsed_partitions *state,
316			 sector_t offset, sector_t size, int origin)
317{
318#ifdef CONFIG_BSD_DISKLABEL
319	parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
320#endif
321}
322
323static void parse_openbsd(struct parsed_partitions *state,
324			  sector_t offset, sector_t size, int origin)
325{
326#ifdef CONFIG_BSD_DISKLABEL
327	parse_bsd(state, offset, size, origin, "openbsd",
328		  OPENBSD_MAXPARTITIONS);
329#endif
330}
331
332/*
333 * Create devices for Unixware partitions listed in a disklabel, under a
334 * dos-like partition. See parse_extended() for more information.
335 */
336static void parse_unixware(struct parsed_partitions *state,
337			   sector_t offset, sector_t size, int origin)
338{
339#ifdef CONFIG_UNIXWARE_DISKLABEL
340	Sector sect;
341	struct unixware_disklabel *l;
342	struct unixware_slice *p;
343
344	l = read_part_sector(state, offset + 29, &sect);
345	if (!l)
346		return;
347	if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
348	    le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
349		put_dev_sector(sect);
350		return;
351	}
352	{
353		char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
354
355		snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
356		strlcat(state->pp_buf, tmp, PAGE_SIZE);
357	}
358	p = &l->vtoc.v_slice[1];
359	/* I omit the 0th slice as it is the same as whole disk. */
360	while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
361		if (state->next == state->limit)
362			break;
363
364		if (p->s_label != UNIXWARE_FS_UNUSED)
365			put_partition(state, state->next++,
366				      le32_to_cpu(p->start_sect),
367				      le32_to_cpu(p->nr_sects));
368		p++;
369	}
370	put_dev_sector(sect);
371	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
372#endif
373}
374
375/*
376 * Minix 2.0.0/2.0.2 subpartition support.
377 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
378 * Rajeev V. Pillai    <rajeevvp@yahoo.com>
379 */
380static void parse_minix(struct parsed_partitions *state,
381			sector_t offset, sector_t size, int origin)
382{
383#ifdef CONFIG_MINIX_SUBPARTITION
384	Sector sect;
385	unsigned char *data;
386	struct partition *p;
387	int i;
388
389	data = read_part_sector(state, offset, &sect);
390	if (!data)
391		return;
392
393	p = (struct partition *)(data + 0x1be);
394
395	/* The first sector of a Minix partition can have either
396	 * a secondary MBR describing its subpartitions, or
397	 * the normal boot sector. */
398	if (msdos_magic_present (data + 510) &&
399	    SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
400		char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
401
402		snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
403		strlcat(state->pp_buf, tmp, PAGE_SIZE);
404		for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
405			if (state->next == state->limit)
406				break;
407			/* add each partition in use */
408			if (SYS_IND(p) == MINIX_PARTITION)
409				put_partition(state, state->next++,
410					      start_sect(p), nr_sects(p));
411		}
412		strlcat(state->pp_buf, " >\n", PAGE_SIZE);
413	}
414	put_dev_sector(sect);
415#endif /* CONFIG_MINIX_SUBPARTITION */
416}
417
418static struct {
419	unsigned char id;
420	void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
421} subtypes[] = {
422	{FREEBSD_PARTITION, parse_freebsd},
423	{NETBSD_PARTITION, parse_netbsd},
424	{OPENBSD_PARTITION, parse_openbsd},
425	{MINIX_PARTITION, parse_minix},
426	{UNIXWARE_PARTITION, parse_unixware},
427	{SOLARIS_X86_PARTITION, parse_solaris_x86},
428	{NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
429	{0, NULL},
430};
431 
432int msdos_partition(struct parsed_partitions *state)
433{
434	sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
435	Sector sect;
436	unsigned char *data;
437	struct partition *p;
438	struct fat_boot_sector *fb;
439	int slot;
 
440
441	data = read_part_sector(state, 0, &sect);
442	if (!data)
443		return -1;
444	if (!msdos_magic_present(data + 510)) {
 
 
 
 
 
445		put_dev_sector(sect);
 
 
 
 
446		return 0;
 
447	}
448
449	if (aix_magic_present(state, data)) {
450		put_dev_sector(sect);
451		strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
452		return 0;
453	}
454
455	/*
456	 * Now that the 55aa signature is present, this is probably
457	 * either the boot sector of a FAT filesystem or a DOS-type
458	 * partition table. Reject this in case the boot indicator
459	 * is not 0 or 0x80.
460	 */
461	p = (struct partition *) (data + 0x1be);
462	for (slot = 1; slot <= 4; slot++, p++) {
463		if (p->boot_ind != 0 && p->boot_ind != 0x80) {
464			/*
465			 * Even without a valid boot inidicator value
466			 * its still possible this is valid FAT filesystem
467			 * without a partition table.
468			 */
469			fb = (struct fat_boot_sector *) data;
470			if (slot == 1 && fb->reserved && fb->fats
471				&& fat_valid_media(fb->media)) {
472				strlcat(state->pp_buf, "\n", PAGE_SIZE);
473				put_dev_sector(sect);
474				return 1;
475			} else {
476				put_dev_sector(sect);
477				return 0;
478			}
479		}
480	}
481
482#ifdef CONFIG_EFI_PARTITION
483	p = (struct partition *) (data + 0x1be);
484	for (slot = 1 ; slot <= 4 ; slot++, p++) {
485		/* If this is an EFI GPT disk, msdos should ignore it. */
486		if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
487			put_dev_sector(sect);
488			return 0;
489		}
490	}
491#endif
492	p = (struct partition *) (data + 0x1be);
493
 
 
494	/*
495	 * Look for partitions in two passes:
496	 * First find the primary and DOS-type extended partitions.
497	 * On the second pass look inside *BSD, Unixware and Solaris partitions.
498	 */
499
500	state->next = 5;
501	for (slot = 1 ; slot <= 4 ; slot++, p++) {
502		sector_t start = start_sect(p)*sector_size;
503		sector_t size = nr_sects(p)*sector_size;
 
504		if (!size)
505			continue;
506		if (is_extended_partition(p)) {
507			/*
508			 * prevent someone doing mkfs or mkswap on an
509			 * extended partition, but leave room for LILO
510			 * FIXME: this uses one logical sector for > 512b
511			 * sector, although it may not be enough/proper.
512			 */
513			sector_t n = 2;
 
514			n = min(size, max(sector_size, n));
515			put_partition(state, slot, start, n);
516
517			strlcat(state->pp_buf, " <", PAGE_SIZE);
518			parse_extended(state, start, size);
519			strlcat(state->pp_buf, " >", PAGE_SIZE);
520			continue;
521		}
522		put_partition(state, slot, start, size);
 
523		if (SYS_IND(p) == LINUX_RAID_PARTITION)
524			state->parts[slot].flags = ADDPART_FLAG_RAID;
525		if (SYS_IND(p) == DM6_PARTITION)
526			strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
527		if (SYS_IND(p) == EZD_PARTITION)
528			strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
529	}
530
531	strlcat(state->pp_buf, "\n", PAGE_SIZE);
532
533	/* second pass - output for each on a separate line */
534	p = (struct partition *) (0x1be + data);
535	for (slot = 1 ; slot <= 4 ; slot++, p++) {
536		unsigned char id = SYS_IND(p);
537		int n;
538
539		if (!nr_sects(p))
540			continue;
541
542		for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
543			;
544
545		if (!subtypes[n].parse)
546			continue;
547		subtypes[n].parse(state, start_sect(p) * sector_size,
548				  nr_sects(p) * sector_size, slot);
549	}
550	put_dev_sector(sect);
551	return 1;
552}
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  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
 21 */
 22#include <linux/msdos_fs.h>
 23
 24#include "check.h"
 25#include "msdos.h"
 26#include "efi.h"
 27#include "aix.h"
 28
 29/*
 30 * Many architectures don't like unaligned accesses, while
 31 * the nr_sects and start_sect partition table entries are
 32 * at a 2 (mod 4) address.
 33 */
 34#include <asm/unaligned.h>
 35
 36#define SYS_IND(p)	get_unaligned(&p->sys_ind)
 37
 38static inline sector_t nr_sects(struct partition *p)
 39{
 40	return (sector_t)get_unaligned_le32(&p->nr_sects);
 41}
 42
 43static inline sector_t start_sect(struct partition *p)
 44{
 45	return (sector_t)get_unaligned_le32(&p->start_sect);
 46}
 47
 48static inline int is_extended_partition(struct partition *p)
 49{
 50	return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
 51		SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
 52		SYS_IND(p) == LINUX_EXTENDED_PARTITION);
 53}
 54
 55#define MSDOS_LABEL_MAGIC1	0x55
 56#define MSDOS_LABEL_MAGIC2	0xAA
 57
 58static inline int
 59msdos_magic_present(unsigned char *p)
 60{
 61	return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
 62}
 63
 64/* Value is EBCDIC 'IBMA' */
 65#define AIX_LABEL_MAGIC1	0xC9
 66#define AIX_LABEL_MAGIC2	0xC2
 67#define AIX_LABEL_MAGIC3	0xD4
 68#define AIX_LABEL_MAGIC4	0xC1
 69static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
 70{
 71	struct partition *pt = (struct partition *) (p + 0x1be);
 72	Sector sect;
 73	unsigned char *d;
 74	int slot, ret = 0;
 75
 76	if (!(p[0] == AIX_LABEL_MAGIC1 &&
 77		p[1] == AIX_LABEL_MAGIC2 &&
 78		p[2] == AIX_LABEL_MAGIC3 &&
 79		p[3] == AIX_LABEL_MAGIC4))
 80		return 0;
 81	/* Assume the partition table is valid if Linux partitions exists */
 82	for (slot = 1; slot <= 4; slot++, pt++) {
 83		if (pt->sys_ind == LINUX_SWAP_PARTITION ||
 84			pt->sys_ind == LINUX_RAID_PARTITION ||
 85			pt->sys_ind == LINUX_DATA_PARTITION ||
 86			pt->sys_ind == LINUX_LVM_PARTITION ||
 87			is_extended_partition(pt))
 88			return 0;
 89	}
 90	d = read_part_sector(state, 7, &sect);
 91	if (d) {
 92		if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
 93			ret = 1;
 94		put_dev_sector(sect);
 95	}
 96	return ret;
 97}
 98
 99static void set_info(struct parsed_partitions *state, int slot,
100		     u32 disksig)
101{
102	struct partition_meta_info *info = &state->parts[slot].info;
103
104	snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
105		 slot);
106	info->volname[0] = 0;
107	state->parts[slot].has_info = true;
108}
109
110/*
111 * Create devices for each logical partition in an extended partition.
112 * The logical partitions form a linked list, with each entry being
113 * a partition table with two entries.  The first entry
114 * is the real data partition (with a start relative to the partition
115 * table start).  The second is a pointer to the next logical partition
116 * (with a start relative to the entire extended partition).
117 * We do not create a Linux partition for the partition tables, but
118 * only for the actual data partitions.
119 */
120
121static void parse_extended(struct parsed_partitions *state,
122			   sector_t first_sector, sector_t first_size,
123			   u32 disksig)
124{
125	struct partition *p;
126	Sector sect;
127	unsigned char *data;
128	sector_t this_sector, this_size;
129	sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
130	int loopct = 0;		/* number of links followed
131				   without finding a data partition */
132	int i;
133
134	this_sector = first_sector;
135	this_size = first_size;
136
137	while (1) {
138		if (++loopct > 100)
139			return;
140		if (state->next == state->limit)
141			return;
142		data = read_part_sector(state, this_sector, &sect);
143		if (!data)
144			return;
145
146		if (!msdos_magic_present(data + 510))
147			goto done;
148
149		p = (struct partition *) (data + 0x1be);
150
151		/*
152		 * Usually, the first entry is the real data partition,
153		 * the 2nd entry is the next extended partition, or empty,
154		 * and the 3rd and 4th entries are unused.
155		 * However, DRDOS sometimes has the extended partition as
156		 * the first entry (when the data partition is empty),
157		 * and OS/2 seems to use all four entries.
158		 */
159
160		/*
161		 * First process the data partition(s)
162		 */
163		for (i = 0; i < 4; i++, p++) {
164			sector_t offs, size, next;
165
166			if (!nr_sects(p) || is_extended_partition(p))
167				continue;
168
169			/* Check the 3rd and 4th entries -
170			   these sometimes contain random garbage */
171			offs = start_sect(p)*sector_size;
172			size = nr_sects(p)*sector_size;
173			next = this_sector + offs;
174			if (i >= 2) {
175				if (offs + size > this_size)
176					continue;
177				if (next < first_sector)
178					continue;
179				if (next + size > first_sector + first_size)
180					continue;
181			}
182
183			put_partition(state, state->next, next, size);
184			set_info(state, state->next, disksig);
185			if (SYS_IND(p) == LINUX_RAID_PARTITION)
186				state->parts[state->next].flags = ADDPART_FLAG_RAID;
187			loopct = 0;
188			if (++state->next == state->limit)
189				goto done;
190		}
191		/*
192		 * Next, process the (first) extended partition, if present.
193		 * (So far, there seems to be no reason to make
194		 *  parse_extended()  recursive and allow a tree
195		 *  of extended partitions.)
196		 * It should be a link to the next logical partition.
197		 */
198		p -= 4;
199		for (i = 0; i < 4; i++, p++)
200			if (nr_sects(p) && is_extended_partition(p))
201				break;
202		if (i == 4)
203			goto done;	 /* nothing left to do */
204
205		this_sector = first_sector + start_sect(p) * sector_size;
206		this_size = nr_sects(p) * sector_size;
207		put_dev_sector(sect);
208	}
209done:
210	put_dev_sector(sect);
211}
212
213/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
214   indicates linux swap.  Be careful before believing this is Solaris. */
215
216static void parse_solaris_x86(struct parsed_partitions *state,
217			      sector_t offset, sector_t size, int origin)
218{
219#ifdef CONFIG_SOLARIS_X86_PARTITION
220	Sector sect;
221	struct solaris_x86_vtoc *v;
222	int i;
223	short max_nparts;
224
225	v = read_part_sector(state, offset + 1, &sect);
226	if (!v)
227		return;
228	if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
229		put_dev_sector(sect);
230		return;
231	}
232	{
233		char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
234
235		snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
236		strlcat(state->pp_buf, tmp, PAGE_SIZE);
237	}
238	if (le32_to_cpu(v->v_version) != 1) {
239		char tmp[64];
240
241		snprintf(tmp, sizeof(tmp), "  cannot handle version %d vtoc>\n",
242			 le32_to_cpu(v->v_version));
243		strlcat(state->pp_buf, tmp, PAGE_SIZE);
244		put_dev_sector(sect);
245		return;
246	}
247	/* Ensure we can handle previous case of VTOC with 8 entries gracefully */
248	max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
249	for (i = 0; i < max_nparts && state->next < state->limit; i++) {
250		struct solaris_x86_slice *s = &v->v_slice[i];
251		char tmp[3 + 10 + 1 + 1];
252
253		if (s->s_size == 0)
254			continue;
255		snprintf(tmp, sizeof(tmp), " [s%d]", i);
256		strlcat(state->pp_buf, tmp, PAGE_SIZE);
257		/* solaris partitions are relative to current MS-DOS
258		 * one; must add the offset of the current partition */
259		put_partition(state, state->next++,
260				 le32_to_cpu(s->s_start)+offset,
261				 le32_to_cpu(s->s_size));
262	}
263	put_dev_sector(sect);
264	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
265#endif
266}
267
268#if defined(CONFIG_BSD_DISKLABEL)
269/*
270 * Create devices for BSD partitions listed in a disklabel, under a
271 * dos-like partition. See parse_extended() for more information.
272 */
273static void parse_bsd(struct parsed_partitions *state,
274		      sector_t offset, sector_t size, int origin, char *flavour,
275		      int max_partitions)
276{
277	Sector sect;
278	struct bsd_disklabel *l;
279	struct bsd_partition *p;
280	char tmp[64];
281
282	l = read_part_sector(state, offset + 1, &sect);
283	if (!l)
284		return;
285	if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
286		put_dev_sector(sect);
287		return;
288	}
289
290	snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
291	strlcat(state->pp_buf, tmp, PAGE_SIZE);
292
293	if (le16_to_cpu(l->d_npartitions) < max_partitions)
294		max_partitions = le16_to_cpu(l->d_npartitions);
295	for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
296		sector_t bsd_start, bsd_size;
297
298		if (state->next == state->limit)
299			break;
300		if (p->p_fstype == BSD_FS_UNUSED)
301			continue;
302		bsd_start = le32_to_cpu(p->p_offset);
303		bsd_size = le32_to_cpu(p->p_size);
304		/* FreeBSD has relative offset if C partition offset is zero */
305		if (memcmp(flavour, "bsd\0", 4) == 0 &&
306		    le32_to_cpu(l->d_partitions[2].p_offset) == 0)
307			bsd_start += offset;
308		if (offset == bsd_start && size == bsd_size)
309			/* full parent partition, we have it already */
310			continue;
311		if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
312			strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
313			continue;
314		}
315		put_partition(state, state->next++, bsd_start, bsd_size);
316	}
317	put_dev_sector(sect);
318	if (le16_to_cpu(l->d_npartitions) > max_partitions) {
319		snprintf(tmp, sizeof(tmp), " (ignored %d more)",
320			 le16_to_cpu(l->d_npartitions) - max_partitions);
321		strlcat(state->pp_buf, tmp, PAGE_SIZE);
322	}
323	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
324}
325#endif
326
327static void parse_freebsd(struct parsed_partitions *state,
328			  sector_t offset, sector_t size, int origin)
329{
330#ifdef CONFIG_BSD_DISKLABEL
331	parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
332#endif
333}
334
335static void parse_netbsd(struct parsed_partitions *state,
336			 sector_t offset, sector_t size, int origin)
337{
338#ifdef CONFIG_BSD_DISKLABEL
339	parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
340#endif
341}
342
343static void parse_openbsd(struct parsed_partitions *state,
344			  sector_t offset, sector_t size, int origin)
345{
346#ifdef CONFIG_BSD_DISKLABEL
347	parse_bsd(state, offset, size, origin, "openbsd",
348		  OPENBSD_MAXPARTITIONS);
349#endif
350}
351
352/*
353 * Create devices for Unixware partitions listed in a disklabel, under a
354 * dos-like partition. See parse_extended() for more information.
355 */
356static void parse_unixware(struct parsed_partitions *state,
357			   sector_t offset, sector_t size, int origin)
358{
359#ifdef CONFIG_UNIXWARE_DISKLABEL
360	Sector sect;
361	struct unixware_disklabel *l;
362	struct unixware_slice *p;
363
364	l = read_part_sector(state, offset + 29, &sect);
365	if (!l)
366		return;
367	if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
368	    le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
369		put_dev_sector(sect);
370		return;
371	}
372	{
373		char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
374
375		snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
376		strlcat(state->pp_buf, tmp, PAGE_SIZE);
377	}
378	p = &l->vtoc.v_slice[1];
379	/* I omit the 0th slice as it is the same as whole disk. */
380	while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
381		if (state->next == state->limit)
382			break;
383
384		if (p->s_label != UNIXWARE_FS_UNUSED)
385			put_partition(state, state->next++,
386				      le32_to_cpu(p->start_sect),
387				      le32_to_cpu(p->nr_sects));
388		p++;
389	}
390	put_dev_sector(sect);
391	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
392#endif
393}
394
395/*
396 * Minix 2.0.0/2.0.2 subpartition support.
397 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
398 * Rajeev V. Pillai    <rajeevvp@yahoo.com>
399 */
400static void parse_minix(struct parsed_partitions *state,
401			sector_t offset, sector_t size, int origin)
402{
403#ifdef CONFIG_MINIX_SUBPARTITION
404	Sector sect;
405	unsigned char *data;
406	struct partition *p;
407	int i;
408
409	data = read_part_sector(state, offset, &sect);
410	if (!data)
411		return;
412
413	p = (struct partition *)(data + 0x1be);
414
415	/* The first sector of a Minix partition can have either
416	 * a secondary MBR describing its subpartitions, or
417	 * the normal boot sector. */
418	if (msdos_magic_present(data + 510) &&
419	    SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
420		char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
421
422		snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
423		strlcat(state->pp_buf, tmp, PAGE_SIZE);
424		for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
425			if (state->next == state->limit)
426				break;
427			/* add each partition in use */
428			if (SYS_IND(p) == MINIX_PARTITION)
429				put_partition(state, state->next++,
430					      start_sect(p), nr_sects(p));
431		}
432		strlcat(state->pp_buf, " >\n", PAGE_SIZE);
433	}
434	put_dev_sector(sect);
435#endif /* CONFIG_MINIX_SUBPARTITION */
436}
437
438static struct {
439	unsigned char id;
440	void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
441} subtypes[] = {
442	{FREEBSD_PARTITION, parse_freebsd},
443	{NETBSD_PARTITION, parse_netbsd},
444	{OPENBSD_PARTITION, parse_openbsd},
445	{MINIX_PARTITION, parse_minix},
446	{UNIXWARE_PARTITION, parse_unixware},
447	{SOLARIS_X86_PARTITION, parse_solaris_x86},
448	{NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
449	{0, NULL},
450};
451
452int msdos_partition(struct parsed_partitions *state)
453{
454	sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
455	Sector sect;
456	unsigned char *data;
457	struct partition *p;
458	struct fat_boot_sector *fb;
459	int slot;
460	u32 disksig;
461
462	data = read_part_sector(state, 0, &sect);
463	if (!data)
464		return -1;
465
466	/*
467	 * Note order! (some AIX disks, e.g. unbootable kind,
468	 * have no MSDOS 55aa)
469	 */
470	if (aix_magic_present(state, data)) {
471		put_dev_sector(sect);
472#ifdef CONFIG_AIX_PARTITION
473		return aix_partition(state);
474#else
475		strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
476		return 0;
477#endif
478	}
479
480	if (!msdos_magic_present(data + 510)) {
481		put_dev_sector(sect);
 
482		return 0;
483	}
484
485	/*
486	 * Now that the 55aa signature is present, this is probably
487	 * either the boot sector of a FAT filesystem or a DOS-type
488	 * partition table. Reject this in case the boot indicator
489	 * is not 0 or 0x80.
490	 */
491	p = (struct partition *) (data + 0x1be);
492	for (slot = 1; slot <= 4; slot++, p++) {
493		if (p->boot_ind != 0 && p->boot_ind != 0x80) {
494			/*
495			 * Even without a valid boot inidicator value
496			 * its still possible this is valid FAT filesystem
497			 * without a partition table.
498			 */
499			fb = (struct fat_boot_sector *) data;
500			if (slot == 1 && fb->reserved && fb->fats
501				&& fat_valid_media(fb->media)) {
502				strlcat(state->pp_buf, "\n", PAGE_SIZE);
503				put_dev_sector(sect);
504				return 1;
505			} else {
506				put_dev_sector(sect);
507				return 0;
508			}
509		}
510	}
511
512#ifdef CONFIG_EFI_PARTITION
513	p = (struct partition *) (data + 0x1be);
514	for (slot = 1 ; slot <= 4 ; slot++, p++) {
515		/* If this is an EFI GPT disk, msdos should ignore it. */
516		if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
517			put_dev_sector(sect);
518			return 0;
519		}
520	}
521#endif
522	p = (struct partition *) (data + 0x1be);
523
524	disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
525
526	/*
527	 * Look for partitions in two passes:
528	 * First find the primary and DOS-type extended partitions.
529	 * On the second pass look inside *BSD, Unixware and Solaris partitions.
530	 */
531
532	state->next = 5;
533	for (slot = 1 ; slot <= 4 ; slot++, p++) {
534		sector_t start = start_sect(p)*sector_size;
535		sector_t size = nr_sects(p)*sector_size;
536
537		if (!size)
538			continue;
539		if (is_extended_partition(p)) {
540			/*
541			 * prevent someone doing mkfs or mkswap on an
542			 * extended partition, but leave room for LILO
543			 * FIXME: this uses one logical sector for > 512b
544			 * sector, although it may not be enough/proper.
545			 */
546			sector_t n = 2;
547
548			n = min(size, max(sector_size, n));
549			put_partition(state, slot, start, n);
550
551			strlcat(state->pp_buf, " <", PAGE_SIZE);
552			parse_extended(state, start, size, disksig);
553			strlcat(state->pp_buf, " >", PAGE_SIZE);
554			continue;
555		}
556		put_partition(state, slot, start, size);
557		set_info(state, slot, disksig);
558		if (SYS_IND(p) == LINUX_RAID_PARTITION)
559			state->parts[slot].flags = ADDPART_FLAG_RAID;
560		if (SYS_IND(p) == DM6_PARTITION)
561			strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
562		if (SYS_IND(p) == EZD_PARTITION)
563			strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
564	}
565
566	strlcat(state->pp_buf, "\n", PAGE_SIZE);
567
568	/* second pass - output for each on a separate line */
569	p = (struct partition *) (0x1be + data);
570	for (slot = 1 ; slot <= 4 ; slot++, p++) {
571		unsigned char id = SYS_IND(p);
572		int n;
573
574		if (!nr_sects(p))
575			continue;
576
577		for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
578			;
579
580		if (!subtypes[n].parse)
581			continue;
582		subtypes[n].parse(state, start_sect(p) * sector_size,
583				  nr_sects(p) * sector_size, slot);
584	}
585	put_dev_sector(sect);
586	return 1;
587}