5 * Copyright (C) 1991-1998 Linus Torvalds
8 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
9 * in the early extended-partition checks and added DM partitions
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).
16 * More flexible handling of extended partitions - aeb, 950831
18 * Check partition table on IDE disks for common CHS translations
20 * Added needed MAJORS for new pairs, {hdi,hdj}, {hdk,hdl}
23 #include <linux/config.h>
25 #include <linux/genhd.h>
26 #include <linux/kernel.h>
27 #include <linux/major.h>
28 #include <linux/string.h>
29 #include <linux/blk.h>
30 #include <linux/init.h>
32 #include <asm/system.h>
35 * Many architectures don't like unaligned accesses, which is
36 * frequently the case with the nr_sects and start_sect partition
39 #include <asm/unaligned.h>
41 #define SYS_IND(p) (get_unaligned(&p->sys_ind))
42 #define NR_SECTS(p) ({ __typeof__(p->nr_sects) __a = \
43 get_unaligned(&p->nr_sects); \
47 #define START_SECT(p) ({ __typeof__(p->start_sect) __a = \
48 get_unaligned(&p->start_sect); \
52 struct gendisk
*gendisk_head
= NULL
;
54 static int current_minor
= 0;
55 extern int *blk_size
[];
56 extern void rd_load(void);
57 extern void initrd_load(void);
59 extern int chr_dev_init(void);
60 extern int blk_dev_init(void);
61 extern int scsi_dev_init(void);
62 extern int net_dev_init(void);
65 extern void note_bootable_part(kdev_t dev
, int part
);
69 * disk_name() is used by genhd.c and blkpg.c.
70 * It formats the devicename of the indicated disk into
71 * the supplied buffer (of size at least 32), and returns
72 * a pointer to that same buffer (for convenience).
74 char *disk_name (struct gendisk
*hd
, int minor
, char *buf
)
77 const char *maj
= hd
->major_name
;
78 int unit
= (minor
>> hd
->minor_shift
) + 'a';
81 * IDE devices use multiple major numbers, but the drives
82 * are named as: {hda,hdb}, {hdc,hdd}, {hde,hdf}, {hdg,hdh}..
83 * This requires special handling here.
104 part
= minor
& ((1 << hd
->minor_shift
) - 1);
105 if (hd
->major
>= SCSI_DISK1_MAJOR
&& hd
->major
<= SCSI_DISK7_MAJOR
) {
106 unit
= unit
+ (hd
->major
- SCSI_DISK1_MAJOR
+ 1) * 16;
109 sprintf(buf
, "sd%c%c", 'a' + unit
/ 26, 'a' + unit
% 26);
111 sprintf(buf
+ 4, "%d", part
);
116 sprintf(buf
, "%s%c%d", maj
, unit
, part
);
118 sprintf(buf
, "%s%c", maj
, unit
);
122 static void add_partition (struct gendisk
*hd
, int minor
, int start
, int size
)
125 hd
->part
[minor
].start_sect
= start
;
126 hd
->part
[minor
].nr_sects
= size
;
127 printk(" %s", disk_name(hd
, minor
, buf
));
130 static inline int is_extended_partition(struct partition
*p
)
132 return (SYS_IND(p
) == DOS_EXTENDED_PARTITION
||
133 SYS_IND(p
) == WIN98_EXTENDED_PARTITION
||
134 SYS_IND(p
) == LINUX_EXTENDED_PARTITION
);
137 int get_hardsect_size(kdev_t dev
)
139 if (hardsect_size
[MAJOR(dev
)] != NULL
)
140 return hardsect_size
[MAJOR(dev
)][MINOR(dev
)];
145 static unsigned int get_ptable_blocksize(kdev_t dev
)
150 * See whether the low-level driver has given us a minumum blocksize.
151 * If so, check to see whether it is larger than the default of 1024.
153 if (!blksize_size
[MAJOR(dev
)])
159 * Check for certain special power of two sizes that we allow.
160 * With anything larger than 1024, we must force the blocksize up to
161 * the natural blocksize for the device so that we don't have to try
162 * and read partial sectors. Anything smaller should be just fine.
165 switch( blksize_size
[MAJOR(dev
)][MINOR(dev
)] )
185 panic("Strange blocksize for partition table\n");
192 #ifdef CONFIG_MSDOS_PARTITION
194 * Create devices for each logical partition in an extended partition.
195 * The logical partitions form a linked list, with each entry being
196 * a partition table with two entries. The first entry
197 * is the real data partition (with a start relative to the partition
198 * table start). The second is a pointer to the next logical partition
199 * (with a start relative to the entire extended partition).
200 * We do not create a Linux partition for the partition tables, but
201 * only for the actual data partitions.
204 #define MSDOS_LABEL_MAGIC 0xAA55
206 static void extended_partition(struct gendisk
*hd
, kdev_t dev
)
208 struct buffer_head
*bh
;
210 unsigned long first_sector
, first_size
, this_sector
, this_size
;
211 int mask
= (1 << hd
->minor_shift
) - 1;
212 int sector_size
= get_hardsect_size(dev
) / 512;
215 first_sector
= hd
->part
[MINOR(dev
)].start_sect
;
216 first_size
= hd
->part
[MINOR(dev
)].nr_sects
;
217 this_sector
= first_sector
;
220 if ((current_minor
& mask
) == 0)
222 if (!(bh
= bread(dev
,0,get_ptable_blocksize(dev
))))
225 * This block is from a device that we're about to stomp on.
226 * So make sure nobody thinks this block is usable.
230 if ((*(unsigned short *) (bh
->b_data
+510)) != cpu_to_le16(MSDOS_LABEL_MAGIC
))
233 p
= (struct partition
*) (0x1BE + bh
->b_data
);
235 this_size
= hd
->part
[MINOR(dev
)].nr_sects
;
238 * Usually, the first entry is the real data partition,
239 * the 2nd entry is the next extended partition, or empty,
240 * and the 3rd and 4th entries are unused.
241 * However, DRDOS sometimes has the extended partition as
242 * the first entry (when the data partition is empty),
243 * and OS/2 seems to use all four entries.
247 * First process the data partition(s)
249 for (i
=0; i
<4; i
++, p
++) {
250 if (!NR_SECTS(p
) || is_extended_partition(p
))
253 /* Check the 3rd and 4th entries -
254 these sometimes contain random garbage */
256 && START_SECT(p
) + NR_SECTS(p
) > this_size
257 && (this_sector
+ START_SECT(p
) < first_sector
||
258 this_sector
+ START_SECT(p
) + NR_SECTS(p
) >
259 first_sector
+ first_size
))
262 add_partition(hd
, current_minor
, this_sector
+START_SECT(p
)*sector_size
, NR_SECTS(p
)*sector_size
);
264 if ((current_minor
& mask
) == 0)
268 * Next, process the (first) extended partition, if present.
269 * (So far, there seems to be no reason to make
270 * extended_partition() recursive and allow a tree
271 * of extended partitions.)
272 * It should be a link to the next logical partition.
273 * Create a minor for this just long enough to get the next
274 * partition table. The minor will be reused for the next
278 for (i
=0; i
<4; i
++, p
++)
279 if(NR_SECTS(p
) && is_extended_partition(p
))
282 goto done
; /* nothing left to do */
284 hd
->part
[current_minor
].nr_sects
= NR_SECTS(p
) * sector_size
; /* JSt */
285 hd
->part
[current_minor
].start_sect
= first_sector
+ START_SECT(p
) * sector_size
;
286 this_sector
= first_sector
+ START_SECT(p
) * sector_size
;
287 dev
= MKDEV(hd
->major
, current_minor
);
294 #ifdef CONFIG_SOLARIS_X86_PARTITION
296 solaris_x86_partition(struct gendisk
*hd
, kdev_t dev
, long offset
) {
298 struct buffer_head
*bh
;
299 struct solaris_x86_vtoc
*v
;
300 struct solaris_x86_slice
*s
;
303 if(!(bh
= bread(dev
, 0, get_ptable_blocksize(dev
))))
305 v
= (struct solaris_x86_vtoc
*)(bh
->b_data
+ 512);
306 if(v
->v_sanity
!= SOLARIS_X86_VTOC_SANE
) {
310 printk(" <solaris:");
311 if(v
->v_version
!= 1) {
312 printk(" cannot handle version %ld vtoc>", v
->v_version
);
316 for(i
=0; i
<SOLARIS_X86_NUMSLICE
; i
++) {
322 /* solaris partitions are relative to current MS-DOS
323 * one but add_partition starts relative to sector
324 * zero of the disk. Therefore, must add the offset
325 * of the current partition */
326 add_partition(hd
, current_minor
, s
->s_start
+offset
, s
->s_size
);
334 #ifdef CONFIG_BSD_DISKLABEL
335 static void check_and_add_bsd_partition(struct gendisk
*hd
,
336 struct bsd_partition
*bsd_p
, kdev_t dev
)
338 struct hd_struct
*lin_p
;
339 /* check relative position of partitions. */
340 for (lin_p
= hd
->part
+ 1 + MINOR(dev
);
341 lin_p
- hd
->part
- MINOR(dev
) < current_minor
; lin_p
++) {
342 /* no relationship -> try again */
343 if (lin_p
->start_sect
+ lin_p
->nr_sects
<= bsd_p
->p_offset
344 || lin_p
->start_sect
>= bsd_p
->p_offset
+ bsd_p
->p_size
)
346 /* equal -> no need to add */
347 if (lin_p
->start_sect
== bsd_p
->p_offset
&&
348 lin_p
->nr_sects
== bsd_p
->p_size
)
350 /* bsd living within dos partition */
351 if (lin_p
->start_sect
<= bsd_p
->p_offset
&& lin_p
->start_sect
352 + lin_p
->nr_sects
>= bsd_p
->p_offset
+ bsd_p
->p_size
) {
353 #ifdef DEBUG_BSD_DISKLABEL
354 printk("w: %d %ld+%ld,%d+%d",
356 lin_p
->start_sect
, lin_p
->nr_sects
,
357 bsd_p
->p_offset
, bsd_p
->p_size
);
361 /* ouch: bsd and linux overlap. Don't even try for that partition */
362 #ifdef DEBUG_BSD_DISKLABEL
363 printk("???: %d %ld+%ld,%d+%d",
364 lin_p
- hd
->part
, lin_p
->start_sect
, lin_p
->nr_sects
,
365 bsd_p
->p_offset
, bsd_p
->p_size
);
369 } /* if the bsd partition is not currently known to linux, we end
372 add_partition(hd
, current_minor
, bsd_p
->p_offset
, bsd_p
->p_size
);
376 * Create devices for BSD partitions listed in a disklabel, under a
377 * dos-like partition. See extended_partition() for more information.
379 static void bsd_disklabel_partition(struct gendisk
*hd
, kdev_t dev
,
382 struct buffer_head
*bh
;
383 struct bsd_disklabel
*l
;
384 struct bsd_partition
*p
;
385 int mask
= (1 << hd
->minor_shift
) - 1;
387 if (!(bh
= bread(dev
,0,get_ptable_blocksize(dev
))))
390 l
= (struct bsd_disklabel
*) (bh
->b_data
+512);
391 if (l
->d_magic
!= BSD_DISKMAGIC
) {
396 if (l
->d_npartitions
< max_partitions
)
397 max_partitions
= l
->d_npartitions
;
398 for (p
= l
->d_partitions
; p
- l
->d_partitions
< max_partitions
; p
++) {
399 if ((current_minor
& mask
) >= (4 + hd
->max_p
))
402 if (p
->p_fstype
!= BSD_FS_UNUSED
)
403 check_and_add_bsd_partition(hd
, p
, dev
);
410 #ifdef CONFIG_UNIXWARE_DISKLABEL
412 * Create devices for Unixware partitions listed in a disklabel, under a
413 * dos-like partition. See extended_partition() for more information.
415 static void unixware_partition(struct gendisk
*hd
, kdev_t dev
)
417 struct buffer_head
*bh
;
418 struct unixware_disklabel
*l
;
419 struct unixware_slice
*p
;
420 int mask
= (1 << hd
->minor_shift
) - 1;
422 if (!(bh
= bread(dev
, 14, get_ptable_blocksize(dev
))))
425 l
= (struct unixware_disklabel
*) (bh
->b_data
+512);
426 if (le32_to_cpu(l
->d_magic
) != UNIXWARE_DISKMAGIC
||
427 le32_to_cpu(l
->vtoc
.v_magic
) != UNIXWARE_DISKMAGIC2
) {
431 printk(" <unixware:");
432 p
= &l
->vtoc
.v_slice
[1];
433 /* I omit the 0th slice as it is the same as whole disk. */
434 while (p
- &l
->vtoc
.v_slice
[0] < UNIXWARE_NUMSLICE
) {
435 if ((current_minor
& mask
) == 0)
438 if (p
->s_label
!= UNIXWARE_FS_UNUSED
) {
439 add_partition(hd
, current_minor
, START_SECT(p
), NR_SECTS(p
));
449 static int msdos_partition(struct gendisk
*hd
, kdev_t dev
, unsigned long first_sector
)
451 int i
, minor
= current_minor
;
452 struct buffer_head
*bh
;
455 int mask
= (1 << hd
->minor_shift
) - 1;
456 int sector_size
= get_hardsect_size(dev
) / 512;
457 #ifdef CONFIG_BSD_DISKLABEL
458 /* no bsd disklabel as a default */
460 int bsd_maxpart
= BSD_MAXPARTITIONS
;
462 #ifdef CONFIG_BLK_DEV_IDE
463 int tested_for_xlate
= 0;
467 if (!(bh
= bread(dev
,0,get_ptable_blocksize(dev
)))) {
468 printk(" unable to read partition table\n");
472 /* In some cases we modify the geometry */
473 /* of the drive (below), so ensure that */
474 /* nobody else tries to re-use this data. */
476 #ifdef CONFIG_BLK_DEV_IDE
479 if (*(unsigned short *) (0x1fe + data
) != cpu_to_le16(MSDOS_LABEL_MAGIC
)) {
483 p
= (struct partition
*) (0x1be + data
);
485 #ifdef CONFIG_BLK_DEV_IDE
486 if (!tested_for_xlate
++) { /* Do this only once per disk */
488 * Look for various forms of IDE disk geometry translation
490 extern int ide_xlate_1024(kdev_t
, int, const char *);
491 unsigned int sig
= le16_to_cpu(*(unsigned short *)(data
+ 2));
492 if (SYS_IND(p
) == EZD_PARTITION
) {
494 * The remainder of the disk must be accessed using
495 * a translated geometry that reduces the number of
496 * apparent cylinders to less than 1024 if possible.
498 * ide_xlate_1024() will take care of the necessary
499 * adjustments to fool fdisk/LILO and partition check.
501 if (ide_xlate_1024(dev
, -1, " [EZD]")) {
505 } else if (SYS_IND(p
) == DM6_PARTITION
) {
508 * Everything on the disk is offset by 63 sectors,
509 * including a "new" MBR with its own partition table,
510 * and the remainder of the disk must be accessed using
511 * a translated geometry that reduces the number of
512 * apparent cylinders to less than 1024 if possible.
514 * ide_xlate_1024() will take care of the necessary
515 * adjustments to fool fdisk/LILO and partition check.
517 if (ide_xlate_1024(dev
, 1, " [DM6:DDO]")) {
519 goto read_mbr
; /* start over with new MBR */
521 } else if (sig
<= 0x1ae &&
522 *(unsigned short *)(data
+ sig
) == cpu_to_le16(0x55AA) &&
523 (1 & *(unsigned char *)(data
+ sig
+ 2))) {
524 /* DM6 signature in MBR, courtesy of OnTrack */
525 (void) ide_xlate_1024 (dev
, 0, " [DM6:MBR]");
526 } else if (SYS_IND(p
) == DM6_AUX1PARTITION
|| SYS_IND(p
) == DM6_AUX3PARTITION
) {
528 * DM6 on other than the first (boot) drive
530 (void) ide_xlate_1024(dev
, 0, " [DM6:AUX]");
533 * Examine the partition table for common translations.
534 * This is useful for drives in situations where the
535 * translated geometry is unavailable from the BIOS.
537 for (i
= 0; i
< 4; i
++) {
538 struct partition
*q
= &p
[i
];
540 && (q
->sector
& 63) == 1
541 && (q
->end_sector
& 63) == 63) {
542 unsigned int heads
= q
->end_head
+ 1;
543 if (heads
== 32 || heads
== 64 ||
544 heads
== 128 || heads
== 240 ||
546 (void) ide_xlate_1024(dev
, heads
, " [PTBL]");
553 #endif /* CONFIG_BLK_DEV_IDE */
555 current_minor
+= 4; /* first "extra" minor (for extended partitions) */
556 for (i
=1 ; i
<=4 ; minor
++,i
++,p
++) {
559 add_partition(hd
, minor
, first_sector
+START_SECT(p
)*sector_size
, NR_SECTS(p
)*sector_size
);
560 if (is_extended_partition(p
)) {
563 * If we are rereading the partition table, we need
564 * to set the size of the partition so that we will
565 * be able to bread the block containing the extended
568 hd
->sizes
[minor
] = hd
->part
[minor
].nr_sects
569 >> (BLOCK_SIZE_BITS
- 9);
570 extended_partition(hd
, MKDEV(hd
->major
, minor
));
572 /* prevent someone doing mkfs or mkswap on an
573 extended partition, but leave room for LILO */
574 if (hd
->part
[minor
].nr_sects
> 2)
575 hd
->part
[minor
].nr_sects
= 2;
577 #ifdef CONFIG_BSD_DISKLABEL
578 /* tag first disklabel for late recognition */
579 if (SYS_IND(p
) == BSD_PARTITION
|| SYS_IND(p
) == NETBSD_PARTITION
) {
582 bsd_kdev
= MKDEV(hd
->major
, minor
);
583 } else if (SYS_IND(p
) == OPENBSD_PARTITION
) {
586 bsd_kdev
= MKDEV(hd
->major
, minor
);
587 bsd_maxpart
= OPENBSD_MAXPARTITIONS
;
591 #ifdef CONFIG_UNIXWARE_DISKLABEL
592 if (SYS_IND(p
) == UNIXWARE_PARTITION
)
593 unixware_partition(hd
, MKDEV(hd
->major
, minor
));
595 #ifdef CONFIG_SOLARIS_X86_PARTITION
597 /* james@bpgc.com: Solaris has a nasty indicator: 0x82
598 * which also means linux swap. For that reason, all
599 * of the prints are done inside the
600 * solaris_x86_partition routine */
602 if(SYS_IND(p
) == SOLARIS_X86_PARTITION
) {
603 solaris_x86_partition(hd
, MKDEV(hd
->major
, minor
),
604 first_sector
+START_SECT(p
));
608 #ifdef CONFIG_BSD_DISKLABEL
611 bsd_disklabel_partition(hd
, bsd_kdev
, bsd_maxpart
);
616 * Check for old-style Disk Manager partition table
618 if (*(unsigned short *) (data
+0xfc) == cpu_to_le16(MSDOS_LABEL_MAGIC
)) {
619 p
= (struct partition
*) (0x1be + data
);
620 for (i
= 4 ; i
< 16 ; i
++, current_minor
++) {
622 if ((current_minor
& mask
) == 0)
624 if (!(START_SECT(p
) && NR_SECTS(p
)))
626 add_partition(hd
, current_minor
, START_SECT(p
), NR_SECTS(p
));
634 #endif /* CONFIG_MSDOS_PARTITION */
636 #ifdef CONFIG_OSF_PARTITION
638 static int osf_partition(struct gendisk
*hd
, unsigned int dev
, unsigned long first_sector
)
641 int mask
= (1 << hd
->minor_shift
) - 1;
642 struct buffer_head
*bh
;
645 u16 d_type
,d_subtype
;
654 u16 d_sparespertrack
;
657 u16 d_rpm
, d_interleave
, d_trackskew
, d_cylskew
;
658 u32 d_headswitch
, d_trkseek
, d_flags
;
664 u32 d_bbsize
, d_sbsize
;
674 struct d_partition
* partition
;
675 #define DISKLABELMAGIC (0x82564557UL)
677 if (!(bh
= bread(dev
,0,get_ptable_blocksize(dev
)))) {
678 printk("unable to read partition table\n");
681 label
= (struct disklabel
*) (bh
->b_data
+64);
682 partition
= label
->d_partitions
;
683 if (label
->d_magic
!= DISKLABELMAGIC
) {
684 printk("magic: %08x\n", label
->d_magic
);
688 if (label
->d_magic2
!= DISKLABELMAGIC
) {
689 printk("magic2: %08x\n", label
->d_magic2
);
693 for (i
= 0 ; i
< label
->d_npartitions
; i
++, partition
++) {
694 if ((current_minor
& mask
) == 0)
696 if (partition
->p_size
)
697 add_partition(hd
, current_minor
,
698 first_sector
+partition
->p_offset
,
707 #endif /* CONFIG_OSF_PARTITION */
709 #ifdef CONFIG_SUN_PARTITION
711 static int sun_partition(struct gendisk
*hd
, kdev_t dev
, unsigned long first_sector
)
715 struct buffer_head
*bh
;
716 struct sun_disklabel
{
717 unsigned char info
[128]; /* Informative text string */
718 unsigned char spare
[292]; /* Boot information etc. */
719 unsigned short rspeed
; /* Disk rotational speed */
720 unsigned short pcylcount
; /* Physical cylinder count */
721 unsigned short sparecyl
; /* extra sects per cylinder */
722 unsigned char spare2
[4]; /* More magic... */
723 unsigned short ilfact
; /* Interleave factor */
724 unsigned short ncyl
; /* Data cylinder count */
725 unsigned short nacyl
; /* Alt. cylinder count */
726 unsigned short ntrks
; /* Tracks per cylinder */
727 unsigned short nsect
; /* Sectors per track */
728 unsigned char spare3
[4]; /* Even more magic... */
729 struct sun_partition
{
730 __u32 start_cylinder
;
733 unsigned short magic
; /* Magic number */
734 unsigned short csum
; /* Label xor'd checksum */
736 struct sun_partition
*p
;
738 #define SUN_LABEL_MAGIC 0xDABE
740 if(!(bh
= bread(dev
, 0, get_ptable_blocksize(dev
)))) {
741 printk("Dev %s: unable to read partition table\n",
745 label
= (struct sun_disklabel
*) bh
->b_data
;
746 p
= label
->partitions
;
747 if (be16_to_cpu(label
->magic
) != SUN_LABEL_MAGIC
) {
748 printk("Dev %s Sun disklabel: bad magic %04x\n",
749 kdevname(dev
), be16_to_cpu(label
->magic
));
753 /* Look at the checksum */
754 ush
= ((unsigned short *) (label
+1)) - 1;
755 for(csum
= 0; ush
>= ((unsigned short *) label
);)
758 printk("Dev %s Sun disklabel: Csum bad, label corrupted\n",
763 /* All Sun disks have 8 partition entries */
764 spc
= be16_to_cpu(label
->ntrks
) * be16_to_cpu(label
->nsect
);
765 for(i
=0; i
< 8; i
++, p
++) {
766 unsigned long st_sector
;
769 st_sector
= first_sector
+ be32_to_cpu(p
->start_cylinder
) * spc
;
770 num_sectors
= be32_to_cpu(p
->num_sectors
);
772 add_partition(hd
, current_minor
, st_sector
, num_sectors
);
780 #endif /* CONFIG_SUN_PARTITION */
782 #ifdef CONFIG_SGI_PARTITION
783 #include <asm/byteorder.h>
785 static int sgi_partition(struct gendisk
*hd
, kdev_t dev
, unsigned long first_sector
)
789 unsigned int start
, blocks
, cs
;
791 struct buffer_head
*bh
;
792 struct sgi_disklabel
{
793 int magic_mushroom
; /* Big fat spliff... */
794 short root_part_num
; /* Root partition number */
795 short swap_part_num
; /* Swap partition number */
796 char boot_file
[16]; /* Name of boot file for ARCS */
797 unsigned char _unused0
[48]; /* Device parameter useless crapola.. */
799 char name
[8]; /* Name of volume */
800 int block_num
; /* Logical block number */
801 int num_bytes
; /* How big, in bytes */
803 struct sgi_partition
{
804 int num_blocks
; /* Size in logical blocks */
805 int first_block
; /* First logical block */
806 int type
; /* Type of this partition */
808 int csum
; /* Disk label checksum */
809 int _unused1
; /* Padding */
811 struct sgi_partition
*p
;
812 #define SGI_LABEL_MAGIC 0x0be5a941
814 if(!(bh
= bread(dev
, 0, get_ptable_blocksize(dev
)))) {
815 printk("Dev %s: unable to read partition table\n", kdevname(dev
));
818 label
= (struct sgi_disklabel
*) bh
->b_data
;
819 p
= &label
->partitions
[0];
820 magic
= label
->magic_mushroom
;
821 if(be32_to_cpu(magic
) != SGI_LABEL_MAGIC
) {
822 printk("Dev %s SGI disklabel: bad magic %08x\n",
823 kdevname(dev
), magic
);
827 ui
= ((unsigned int *) (label
+ 1)) - 1;
828 for(csum
= 0; ui
>= ((unsigned int *) label
);) {
830 csum
+= be32_to_cpu(cs
);
833 printk("Dev %s SGI disklabel: csum bad, label corrupted\n",
838 /* All SGI disk labels have 16 partitions, disks under Linux only
839 * have 15 minor's. Luckily there are always a few zero length
840 * partitions which we don't care about so we never overflow the
843 for(i
= 0; i
< 16; i
++, p
++) {
844 blocks
= be32_to_cpu(p
->num_blocks
);
845 start
= be32_to_cpu(p
->first_block
);
848 add_partition(hd
, current_minor
, start
, blocks
);
858 #ifdef CONFIG_AMIGA_PARTITION
859 #include <asm/byteorder.h>
860 #include <linux/affs_hardblocks.h>
862 static __inline__ u32
863 checksum_block(u32
*m
, int size
)
873 amiga_partition(struct gendisk
*hd
, kdev_t dev
, unsigned long first_sector
)
875 struct buffer_head
*bh
;
876 struct RigidDiskBlock
*rdb
;
877 struct PartitionBlock
*pb
;
885 old_blocksize
= get_ptable_blocksize(dev
);
886 blocksize
= get_hardsect_size(dev
);
888 set_blocksize(dev
,blocksize
);
891 for (blk
= 0; blk
< RDB_ALLOCATION_LIMIT
; blk
++) {
892 if(!(bh
= bread(dev
,blk
,blocksize
))) {
893 printk("Dev %s: unable to read RDB block %d\n",
897 if (*(u32
*)bh
->b_data
== htonl(IDNAME_RIGIDDISK
)) {
898 rdb
= (struct RigidDiskBlock
*)bh
->b_data
;
899 if (checksum_block((u32
*)bh
->b_data
,htonl(rdb
->rdb_SummedLongs
) & 0x7F)) {
900 /* Try again with 0xdc..0xdf zeroed, Windows might have
903 *(u32
*)(&bh
->b_data
[0xdc]) = 0;
904 if (checksum_block((u32
*)bh
->b_data
,
905 htonl(rdb
->rdb_SummedLongs
) & 0x7F)) {
907 printk("Dev %s: RDB in block %d has bad checksum\n",
911 printk("Warning: Trashed word at 0xd0 in block %d "
912 "ignored in checksum calculation\n",blk
);
915 blk
= htonl(rdb
->rdb_PartitionList
);
917 for (part
= 1; blk
> 0 && part
<= 16; part
++) {
918 if (!(bh
= bread(dev
,blk
,blocksize
))) {
919 printk("Dev %s: unable to read partition block %d\n",
923 pb
= (struct PartitionBlock
*)bh
->b_data
;
924 blk
= htonl(pb
->pb_Next
);
925 if (pb
->pb_ID
== htonl(IDNAME_PARTITION
) && checksum_block(
926 (u32
*)pb
,htonl(pb
->pb_SummedLongs
) & 0x7F) == 0 ) {
928 /* Tell Kernel about it */
930 if (!(nr_sects
= (htonl(pb
->pb_Environment
[10]) + 1 -
931 htonl(pb
->pb_Environment
[9])) *
932 htonl(pb
->pb_Environment
[3]) *
933 htonl(pb
->pb_Environment
[5]))) {
936 start_sect
= htonl(pb
->pb_Environment
[9]) *
937 htonl(pb
->pb_Environment
[3]) *
938 htonl(pb
->pb_Environment
[5]);
939 add_partition(hd
,current_minor
,start_sect
,nr_sects
);
951 set_blocksize(dev
,old_blocksize
);
954 #endif /* CONFIG_AMIGA_PARTITION */
956 #ifdef CONFIG_MAC_PARTITION
957 #include <linux/ctype.h>
960 * Code to understand MacOS partition tables.
963 #define MAC_PARTITION_MAGIC 0x504d
965 /* type field value for A/UX or other Unix partitions */
966 #define APPLE_AUX_TYPE "Apple_UNIX_SVR2"
968 struct mac_partition
{
969 __u16 signature
; /* expected to be MAC_PARTITION_MAGIC */
971 __u32 map_count
; /* # blocks in partition map */
972 __u32 start_block
; /* absolute starting block # of partition */
973 __u32 block_count
; /* number of blocks in partition */
974 char name
[32]; /* partition name */
975 char type
[32]; /* string type description */
976 __u32 data_start
; /* rel block # of first data block */
977 __u32 data_count
; /* number of data blocks */
978 __u32 status
; /* partition status bits */
986 char processor
[16]; /* identifies ISA of boot */
987 /* there is more stuff after this that we don't need */
990 #define MAC_STATUS_BOOTABLE 8 /* partition is bootable */
992 #define MAC_DRIVER_MAGIC 0x4552
994 /* Driver descriptor structure, in block 0 */
995 struct mac_driver_desc
{
996 __u16 signature
; /* expected to be MAC_DRIVER_MAGIC */
1002 static int mac_partition(struct gendisk
*hd
, kdev_t dev
, unsigned long fsec
)
1004 struct buffer_head
*bh
;
1005 int blk
, blocks_in_map
;
1006 int dev_bsize
, dev_pos
, pos
;
1009 int first_bootable
= 1;
1011 struct mac_partition
*part
;
1012 struct mac_driver_desc
*md
;
1014 dev_bsize
= get_ptable_blocksize(dev
);
1016 /* Get 0th block and look at the first partition map entry. */
1017 if ((bh
= bread(dev
, 0, dev_bsize
)) == 0) {
1018 printk("%s: error reading partition table\n",
1022 md
= (struct mac_driver_desc
*) bh
->b_data
;
1023 if (be16_to_cpu(md
->signature
) != MAC_DRIVER_MAGIC
) {
1027 secsize
= be16_to_cpu(md
->block_size
);
1028 if (secsize
>= dev_bsize
) {
1031 if ((bh
= bread(dev
, secsize
/dev_bsize
, dev_bsize
)) == 0) {
1032 printk("%s: error reading partition table\n",
1037 part
= (struct mac_partition
*) (bh
->b_data
+ secsize
- dev_pos
);
1038 if (be16_to_cpu(part
->signature
) != MAC_PARTITION_MAGIC
) {
1040 return 0; /* not a MacOS disk */
1042 blocks_in_map
= be32_to_cpu(part
->map_count
);
1043 for (blk
= 1; blk
<= blocks_in_map
; ++blk
) {
1044 pos
= blk
* secsize
;
1045 if (pos
>= dev_pos
+ dev_bsize
) {
1048 if ((bh
= bread(dev
, pos
/dev_bsize
, dev_bsize
)) == 0) {
1049 printk("%s: error reading partition table\n",
1054 part
= (struct mac_partition
*) (bh
->b_data
+ pos
- dev_pos
);
1055 if (be16_to_cpu(part
->signature
) != MAC_PARTITION_MAGIC
)
1057 blocks_in_map
= be32_to_cpu(part
->map_count
);
1058 add_partition(hd
, current_minor
,
1059 fsec
+ be32_to_cpu(part
->start_block
) * (secsize
/512),
1060 be32_to_cpu(part
->block_count
) * (secsize
/512));
1064 * If this is the first bootable partition, tell the
1065 * setup code, in case it wants to make this the root.
1067 if ( (_machine
== _MACH_Pmac
) && first_bootable
1068 && (be32_to_cpu(part
->status
) & MAC_STATUS_BOOTABLE
)
1069 && strcasecmp(part
->processor
, "powerpc") == 0) {
1070 note_bootable_part(dev
, blk
);
1073 #endif /* CONFIG_PPC */
1082 #endif /* CONFIG_MAC_PARTITION */
1084 #ifdef CONFIG_ATARI_PARTITION
1085 #include <asm/atari_rootsec.h>
1087 /* ++guenther: this should be settable by the user ("make config")?.
1091 static int atari_partition (struct gendisk
*hd
, kdev_t dev
,
1092 unsigned long first_sector
)
1094 int minor
= current_minor
, m_lim
= current_minor
+ hd
->max_p
;
1095 struct buffer_head
*bh
;
1096 struct rootsector
*rs
;
1097 struct partition_info
*pi
;
1100 int part_fmt
= 0; /* 0:unknown, 1:AHDI, 2:ICD/Supra */
1103 bh
= bread (dev
, 0, get_ptable_blocksize(dev
));
1106 printk (" unable to read block 0\n");
1110 rs
= (struct rootsector
*) bh
->b_data
;
1113 for (; pi
< &rs
->part
[4] && minor
< m_lim
; minor
++, pi
++)
1116 /* active partition */
1118 if (memcmp (pi
->id
, "XGM", 3) == 0)
1119 /* extension partition */
1121 struct rootsector
*xrs
;
1122 struct buffer_head
*xbh
;
1129 partsect
= extensect
= pi
->st
;
1132 xbh
= bread (dev
, partsect
/ 2, get_ptable_blocksize(dev
));
1135 printk (" block %ld read failed\n", partsect
);
1140 xrs
= (struct rootsector
*) &xbh
->b_data
[512];
1142 xrs
= (struct rootsector
*) &xbh
->b_data
[0];
1144 /* ++roman: sanity check: bit 0 of flg field must be set */
1145 if (!(xrs
->part
[0].flg
& 1)) {
1146 printk( "\nFirst sub-partition in extended partition is not valid!\n" );
1150 add_partition(hd
, minor
, partsect
+ xrs
->part
[0].st
,
1153 if (!(xrs
->part
[1].flg
& 1)) {
1154 /* end of linked partition list */
1158 if (memcmp( xrs
->part
[1].id
, "XGM", 3 ) != 0) {
1159 printk( "\nID of extended partition is not XGM!\n" );
1164 partsect
= xrs
->part
[1].st
+ extensect
;
1167 if (minor
>= m_lim
) {
1168 printk( "\nMaximum number of partitions reached!\n" );
1176 /* we don't care about other id's */
1177 add_partition (hd
, minor
, pi
->st
, pi
->siz
);
1182 if ( part_fmt
!=1 ) /* no extended partitions -> test ICD-format */
1184 pi
= &rs
->icdpart
[0];
1185 /* sanity check: no ICD format if first partition invalid */
1186 if (memcmp (pi
->id
, "GEM", 3) == 0 ||
1187 memcmp (pi
->id
, "BGM", 3) == 0 ||
1188 memcmp (pi
->id
, "LNX", 3) == 0 ||
1189 memcmp (pi
->id
, "SWP", 3) == 0 ||
1190 memcmp (pi
->id
, "RAW", 3) == 0 )
1193 for (; pi
< &rs
->icdpart
[8] && minor
< m_lim
; minor
++, pi
++)
1195 /* accept only GEM,BGM,RAW,LNX,SWP partitions */
1197 (memcmp (pi
->id
, "GEM", 3) == 0 ||
1198 memcmp (pi
->id
, "BGM", 3) == 0 ||
1199 memcmp (pi
->id
, "LNX", 3) == 0 ||
1200 memcmp (pi
->id
, "SWP", 3) == 0 ||
1201 memcmp (pi
->id
, "RAW", 3) == 0) )
1204 add_partition (hd
, minor
, pi
->st
, pi
->siz
);
1217 #endif /* CONFIG_ATARI_PARTITION */
1219 static void check_partition(struct gendisk
*hd
, kdev_t dev
)
1221 static int first_time
= 1;
1222 unsigned long first_sector
;
1226 printk("Partition check:\n");
1228 first_sector
= hd
->part
[MINOR(dev
)].start_sect
;
1231 * This is a kludge to allow the partition check to be
1232 * skipped for specific drives (e.g. IDE CD-ROM drives)
1234 if ((int)first_sector
== -1) {
1235 hd
->part
[MINOR(dev
)].start_sect
= 0;
1239 printk(" %s:", disk_name(hd
, MINOR(dev
), buf
));
1240 #ifdef CONFIG_MSDOS_PARTITION
1241 if (msdos_partition(hd
, dev
, first_sector
))
1244 #ifdef CONFIG_OSF_PARTITION
1245 if (osf_partition(hd
, dev
, first_sector
))
1248 #ifdef CONFIG_SUN_PARTITION
1249 if(sun_partition(hd
, dev
, first_sector
))
1252 #ifdef CONFIG_AMIGA_PARTITION
1253 if(amiga_partition(hd
, dev
, first_sector
))
1256 #ifdef CONFIG_ATARI_PARTITION
1257 if(atari_partition(hd
, dev
, first_sector
))
1260 #ifdef CONFIG_MAC_PARTITION
1261 if (mac_partition(hd
, dev
, first_sector
))
1264 #ifdef CONFIG_SGI_PARTITION
1265 if(sgi_partition(hd
, dev
, first_sector
))
1268 printk(" unknown partition table\n");
1271 /* This function is used to re-read partition tables for removable disks.
1272 Much of the cleanup from the old partition tables should have already been
1275 /* This function will re-read the partition tables for a given device,
1276 and set things back up again. There are some important caveats,
1277 however. You must ensure that no one is using the device, and no one
1278 can start using the device while this function is being executed. */
1280 void resetup_one_dev(struct gendisk
*dev
, int drive
)
1283 int first_minor
= drive
<< dev
->minor_shift
;
1284 int end_minor
= first_minor
+ dev
->max_p
;
1286 blk_size
[dev
->major
] = NULL
;
1287 current_minor
= 1 + first_minor
;
1288 check_partition(dev
, MKDEV(dev
->major
, first_minor
));
1291 * We need to set the sizes array before we will be able to access
1292 * any of the partitions on this device.
1294 if (dev
->sizes
!= NULL
) { /* optional safeguard in ll_rw_blk.c */
1295 for (i
= first_minor
; i
< end_minor
; i
++)
1296 dev
->sizes
[i
] = dev
->part
[i
].nr_sects
>> (BLOCK_SIZE_BITS
- 9);
1297 blk_size
[dev
->major
] = dev
->sizes
;
1301 static inline void setup_dev(struct gendisk
*dev
)
1304 int end_minor
= dev
->max_nr
* dev
->max_p
;
1306 blk_size
[dev
->major
] = NULL
;
1307 for (i
= 0; i
< end_minor
; i
++) {
1308 dev
->part
[i
].start_sect
= 0;
1309 dev
->part
[i
].nr_sects
= 0;
1313 for (drive
= 0; drive
< dev
->nr_real
; drive
++)
1314 resetup_one_dev(dev
, drive
);
1317 __initfunc(void device_setup(void))
1319 extern void console_map_init(void);
1320 #ifdef CONFIG_PARPORT
1321 extern int parport_init(void) __init
;
1323 #ifdef CONFIG_MD_BOOT
1324 extern void md_setup_drive(void) __init
;
1326 #ifdef CONFIG_FC4_SOC
1327 extern int soc_probe(void);
1331 #ifdef CONFIG_PARPORT
1337 #ifdef CONFIG_FC4_SOC
1338 /* This has to be done before scsi_dev_init */
1351 for (p
= gendisk_head
; p
; p
=p
->next
)
1354 #ifdef CONFIG_BLK_DEV_RAM
1355 #ifdef CONFIG_BLK_DEV_INITRD
1356 if (initrd_start
&& mount_initrd
) initrd_load();
1361 #ifdef CONFIG_MD_BOOT
1366 #ifdef CONFIG_PROC_FS
1367 int get_partition_list(char * page
)
1373 len
= sprintf(page
, "major minor #blocks name\n\n");
1374 for (p
= gendisk_head
; p
; p
= p
->next
) {
1375 for (n
=0; n
< (p
->nr_real
<< p
->minor_shift
); n
++) {
1376 if (p
->part
[n
].nr_sects
&& len
< PAGE_SIZE
- 80) {
1377 len
+= sprintf(page
+len
,
1378 "%4d %4d %10d %s\n",
1379 p
->major
, n
, p
->sizes
[n
],
1380 disk_name(p
, n
, buf
));