Import 2.3.9pre5
[davej-history.git] / drivers / block / genhd.c
blobc801ab8144f533b0bcc61571c15ec86f8cf9d3e1
1 /*
2 * Code extracted from
3 * linux/kernel/hd.c
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>
24 #include <linux/fs.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
37 * table entries.
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); \
44 le32_to_cpu(__a); \
47 #define START_SECT(p) ({ __typeof__(p->start_sect) __a = \
48 get_unaligned(&p->start_sect); \
49 le32_to_cpu(__a); \
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);
64 #ifdef CONFIG_PPC
65 extern void note_bootable_part(kdev_t dev, int part);
66 #endif
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)
76 unsigned int part;
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.
85 switch (hd->major) {
86 case IDE7_MAJOR:
87 unit += 2;
88 case IDE6_MAJOR:
89 unit += 2;
90 case IDE5_MAJOR:
91 unit += 2;
92 case IDE4_MAJOR:
93 unit += 2;
94 case IDE3_MAJOR:
95 unit += 2;
96 case IDE2_MAJOR:
97 unit += 2;
98 case IDE1_MAJOR:
99 unit += 2;
100 case IDE0_MAJOR:
101 maj = "hd";
102 break;
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;
107 if (unit > 'z') {
108 unit -= 'z' + 1;
109 sprintf(buf, "sd%c%c", 'a' + unit / 26, 'a' + unit % 26);
110 if (part)
111 sprintf(buf + 4, "%d", part);
112 return buf;
115 if (part)
116 sprintf(buf, "%s%c%d", maj, unit, part);
117 else
118 sprintf(buf, "%s%c", maj, unit);
119 return buf;
122 static void add_partition (struct gendisk *hd, int minor, int start, int size)
124 char buf[32];
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)];
141 else
142 return 512;
145 static unsigned int get_ptable_blocksize(kdev_t dev)
147 int ret = 1024;
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)])
155 return ret;
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)] )
167 case 2048:
168 ret = 2048;
169 break;
170 case 4096:
171 ret = 4096;
172 break;
173 case 8192:
174 ret = 8192;
175 break;
176 case 1024:
177 case 512:
178 case 256:
179 case 0:
181 * These are all OK.
183 break;
184 default:
185 panic("Strange blocksize for partition table\n");
188 return ret;
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;
209 struct partition *p;
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;
213 int i;
215 first_sector = hd->part[MINOR(dev)].start_sect;
216 first_size = hd->part[MINOR(dev)].nr_sects;
217 this_sector = first_sector;
219 while (1) {
220 if ((current_minor & mask) == 0)
221 return;
222 if (!(bh = bread(dev,0,get_ptable_blocksize(dev))))
223 return;
225 * This block is from a device that we're about to stomp on.
226 * So make sure nobody thinks this block is usable.
228 bh->b_state = 0;
230 if ((*(unsigned short *) (bh->b_data+510)) != cpu_to_le16(MSDOS_LABEL_MAGIC))
231 goto done;
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))
251 continue;
253 /* Check the 3rd and 4th entries -
254 these sometimes contain random garbage */
255 if (i >= 2
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))
260 continue;
262 add_partition(hd, current_minor, this_sector+START_SECT(p)*sector_size, NR_SECTS(p)*sector_size);
263 current_minor++;
264 if ((current_minor & mask) == 0)
265 goto done;
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
275 * data partition.
277 p -= 4;
278 for (i=0; i<4; i++, p++)
279 if(NR_SECTS(p) && is_extended_partition(p))
280 break;
281 if (i == 4)
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);
288 brelse(bh);
290 done:
291 brelse(bh);
294 #ifdef CONFIG_SOLARIS_X86_PARTITION
295 static void
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;
301 int i;
303 if(!(bh = bread(dev, 0, get_ptable_blocksize(dev))))
304 return;
305 v = (struct solaris_x86_vtoc *)(bh->b_data + 512);
306 if(v->v_sanity != SOLARIS_X86_VTOC_SANE) {
307 brelse(bh);
308 return;
310 printk(" <solaris:");
311 if(v->v_version != 1) {
312 printk(" cannot handle version %ld vtoc>", v->v_version);
313 brelse(bh);
314 return;
316 for(i=0; i<SOLARIS_X86_NUMSLICE; i++) {
317 s = &v->v_slice[i];
319 if (s->s_size == 0)
320 continue;
321 printk(" [s%d]", 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);
327 current_minor++;
329 brelse(bh);
330 printk(" >");
332 #endif
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)
345 continue;
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)
349 return;
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",
355 lin_p - hd->part,
356 lin_p->start_sect, lin_p->nr_sects,
357 bsd_p->p_offset, bsd_p->p_size);
358 #endif
359 break;
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);
366 #endif
367 printk("???");
368 return;
369 } /* if the bsd partition is not currently known to linux, we end
370 * up here
372 add_partition(hd, current_minor, bsd_p->p_offset, bsd_p->p_size);
373 current_minor++;
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,
380 int max_partitions)
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))))
388 return;
389 bh->b_state = 0;
390 l = (struct bsd_disklabel *) (bh->b_data+512);
391 if (l->d_magic != BSD_DISKMAGIC) {
392 brelse(bh);
393 return;
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))
400 break;
402 if (p->p_fstype != BSD_FS_UNUSED)
403 check_and_add_bsd_partition(hd, p, dev);
405 brelse(bh);
408 #endif
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))))
423 return;
424 bh->b_state = 0;
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) {
428 brelse(bh);
429 return;
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)
436 break;
438 if (p->s_label != UNIXWARE_FS_UNUSED) {
439 add_partition(hd, current_minor, START_SECT(p), NR_SECTS(p));
440 current_minor++;
442 p++;
444 brelse(bh);
445 printk(" >");
447 #endif
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;
453 struct partition *p;
454 unsigned char *data;
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 */
459 kdev_t bsd_kdev = 0;
460 int bsd_maxpart = BSD_MAXPARTITIONS;
461 #endif
462 #ifdef CONFIG_BLK_DEV_IDE
463 int tested_for_xlate = 0;
465 read_mbr:
466 #endif
467 if (!(bh = bread(dev,0,get_ptable_blocksize(dev)))) {
468 printk(" unable to read partition table\n");
469 return -1;
471 data = bh->b_data;
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. */
475 bh->b_state = 0;
476 #ifdef CONFIG_BLK_DEV_IDE
477 check_table:
478 #endif
479 if (*(unsigned short *) (0x1fe + data) != cpu_to_le16(MSDOS_LABEL_MAGIC)) {
480 brelse(bh);
481 return 0;
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]")) {
502 data += 512;
503 goto check_table;
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]")) {
518 brelse(bh);
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]");
531 } else {
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];
539 if (NR_SECTS(q)
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 ||
545 heads == 255) {
546 (void) ide_xlate_1024(dev, heads, " [PTBL]");
547 break;
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++) {
557 if (!NR_SECTS(p))
558 continue;
559 add_partition(hd, minor, first_sector+START_SECT(p)*sector_size, NR_SECTS(p)*sector_size);
560 if (is_extended_partition(p)) {
561 printk(" <");
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
566 * partition info.
568 hd->sizes[minor] = hd->part[minor].nr_sects
569 >> (BLOCK_SIZE_BITS - 9);
570 extended_partition(hd, MKDEV(hd->major, minor));
571 printk(" >");
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) {
580 printk("!");
581 if (!bsd_kdev)
582 bsd_kdev = MKDEV(hd->major, minor);
583 } else if (SYS_IND(p) == OPENBSD_PARTITION) {
584 printk("!");
585 if (!bsd_kdev) {
586 bsd_kdev = MKDEV(hd->major, minor);
587 bsd_maxpart = OPENBSD_MAXPARTITIONS;
590 #endif
591 #ifdef CONFIG_UNIXWARE_DISKLABEL
592 if (SYS_IND(p) == UNIXWARE_PARTITION)
593 unixware_partition(hd, MKDEV(hd->major, minor));
594 #endif
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));
606 #endif
608 #ifdef CONFIG_BSD_DISKLABEL
609 if (bsd_kdev) {
610 printk(" <");
611 bsd_disklabel_partition(hd, bsd_kdev, bsd_maxpart);
612 printk(" >");
614 #endif
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++) {
621 p--;
622 if ((current_minor & mask) == 0)
623 break;
624 if (!(START_SECT(p) && NR_SECTS(p)))
625 continue;
626 add_partition(hd, current_minor, START_SECT(p), NR_SECTS(p));
629 printk("\n");
630 brelse(bh);
631 return 1;
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)
640 int i;
641 int mask = (1 << hd->minor_shift) - 1;
642 struct buffer_head *bh;
643 struct disklabel {
644 u32 d_magic;
645 u16 d_type,d_subtype;
646 u8 d_typename[16];
647 u8 d_packname[16];
648 u32 d_secsize;
649 u32 d_nsectors;
650 u32 d_ntracks;
651 u32 d_ncylinders;
652 u32 d_secpercyl;
653 u32 d_secprtunit;
654 u16 d_sparespertrack;
655 u16 d_sparespercyl;
656 u32 d_acylinders;
657 u16 d_rpm, d_interleave, d_trackskew, d_cylskew;
658 u32 d_headswitch, d_trkseek, d_flags;
659 u32 d_drivedata[5];
660 u32 d_spare[5];
661 u32 d_magic2;
662 u16 d_checksum;
663 u16 d_npartitions;
664 u32 d_bbsize, d_sbsize;
665 struct d_partition {
666 u32 p_size;
667 u32 p_offset;
668 u32 p_fsize;
669 u8 p_fstype;
670 u8 p_frag;
671 u16 p_cpg;
672 } d_partitions[8];
673 } * label;
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");
679 return -1;
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);
685 brelse(bh);
686 return 0;
688 if (label->d_magic2 != DISKLABELMAGIC) {
689 printk("magic2: %08x\n", label->d_magic2);
690 brelse(bh);
691 return 0;
693 for (i = 0 ; i < label->d_npartitions; i++, partition++) {
694 if ((current_minor & mask) == 0)
695 break;
696 if (partition->p_size)
697 add_partition(hd, current_minor,
698 first_sector+partition->p_offset,
699 partition->p_size);
700 current_minor++;
702 printk("\n");
703 brelse(bh);
704 return 1;
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)
713 int i, csum;
714 unsigned short *ush;
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;
731 __u32 num_sectors;
732 } partitions[8];
733 unsigned short magic; /* Magic number */
734 unsigned short csum; /* Label xor'd checksum */
735 } * label;
736 struct sun_partition *p;
737 unsigned long spc;
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",
742 kdevname(dev));
743 return -1;
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));
750 brelse(bh);
751 return 0;
753 /* Look at the checksum */
754 ush = ((unsigned short *) (label+1)) - 1;
755 for(csum = 0; ush >= ((unsigned short *) label);)
756 csum ^= *ush--;
757 if(csum) {
758 printk("Dev %s Sun disklabel: Csum bad, label corrupted\n",
759 kdevname(dev));
760 brelse(bh);
761 return 0;
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;
767 int num_sectors;
769 st_sector = first_sector + be32_to_cpu(p->start_cylinder) * spc;
770 num_sectors = be32_to_cpu(p->num_sectors);
771 if (num_sectors)
772 add_partition(hd, current_minor, st_sector, num_sectors);
773 current_minor++;
775 printk("\n");
776 brelse(bh);
777 return 1;
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)
787 int i, csum;
788 unsigned int *ui;
789 unsigned int start, blocks, cs;
790 int magic;
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.. */
798 struct sgi_volume {
799 char name[8]; /* Name of volume */
800 int block_num; /* Logical block number */
801 int num_bytes; /* How big, in bytes */
802 } volume[15];
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 */
807 } partitions[16];
808 int csum; /* Disk label checksum */
809 int _unused1; /* Padding */
810 } *label;
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));
816 return -1;
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);
824 brelse(bh);
825 return 0;
827 ui = ((unsigned int *) (label + 1)) - 1;
828 for(csum = 0; ui >= ((unsigned int *) label);) {
829 cs = *ui--;
830 csum += be32_to_cpu(cs);
832 if(csum) {
833 printk("Dev %s SGI disklabel: csum bad, label corrupted\n",
834 kdevname(dev));
835 brelse(bh);
836 return 0;
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
841 * current_minor.
843 for(i = 0; i < 16; i++, p++) {
844 blocks = be32_to_cpu(p->num_blocks);
845 start = be32_to_cpu(p->first_block);
846 if(!blocks)
847 continue;
848 add_partition(hd, current_minor, start, blocks);
849 current_minor++;
851 printk("\n");
852 brelse(bh);
853 return 1;
856 #endif
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)
865 u32 sum = 0;
867 while (size--)
868 sum += htonl(*m++);
869 return sum;
872 static int
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;
878 int start_sect;
879 int nr_sects;
880 int blk;
881 int part, res;
882 int old_blocksize;
883 int blocksize;
885 old_blocksize = get_ptable_blocksize(dev);
886 blocksize = get_hardsect_size(dev);
888 set_blocksize(dev,blocksize);
889 res = 0;
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",
894 kdevname(dev),blk);
895 goto rdb_done;
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
901 * trashed it.
903 *(u32 *)(&bh->b_data[0xdc]) = 0;
904 if (checksum_block((u32 *)bh->b_data,
905 htonl(rdb->rdb_SummedLongs) & 0x7F)) {
906 brelse(bh);
907 printk("Dev %s: RDB in block %d has bad checksum\n",
908 kdevname(dev),blk);
909 continue;
911 printk("Warning: Trashed word at 0xd0 in block %d "
912 "ignored in checksum calculation\n",blk);
914 printk(" RDSK");
915 blk = htonl(rdb->rdb_PartitionList);
916 brelse(bh);
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",
920 kdevname(dev),blk);
921 goto rdb_done;
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]))) {
934 continue;
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);
940 current_minor++;
941 res = 1;
943 brelse(bh);
945 printk("\n");
946 break;
950 rdb_done:
951 set_blocksize(dev,old_blocksize);
952 return res;
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 */
970 __u16 res1;
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 */
979 __u32 boot_start;
980 __u32 boot_size;
981 __u32 boot_load;
982 __u32 boot_load2;
983 __u32 boot_entry;
984 __u32 boot_entry2;
985 __u32 boot_cksum;
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 */
997 __u16 block_size;
998 __u32 block_count;
999 /* ... more stuff */
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;
1007 unsigned secsize;
1008 #ifdef CONFIG_PPC
1009 int first_bootable = 1;
1010 #endif
1011 struct mac_partition *part;
1012 struct mac_driver_desc *md;
1014 dev_bsize = get_ptable_blocksize(dev);
1015 dev_pos = 0;
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",
1019 kdevname(dev));
1020 return -1;
1022 md = (struct mac_driver_desc *) bh->b_data;
1023 if (be16_to_cpu(md->signature) != MAC_DRIVER_MAGIC) {
1024 brelse(bh);
1025 return 0;
1027 secsize = be16_to_cpu(md->block_size);
1028 if (secsize >= dev_bsize) {
1029 brelse(bh);
1030 dev_pos = secsize;
1031 if ((bh = bread(dev, secsize/dev_bsize, dev_bsize)) == 0) {
1032 printk("%s: error reading partition table\n",
1033 kdevname(dev));
1034 return -1;
1037 part = (struct mac_partition *) (bh->b_data + secsize - dev_pos);
1038 if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC) {
1039 brelse(bh);
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) {
1046 brelse(bh);
1047 dev_pos = pos;
1048 if ((bh = bread(dev, pos/dev_bsize, dev_bsize)) == 0) {
1049 printk("%s: error reading partition table\n",
1050 kdevname(dev));
1051 return -1;
1054 part = (struct mac_partition *) (bh->b_data + pos - dev_pos);
1055 if (be16_to_cpu(part->signature) != MAC_PARTITION_MAGIC)
1056 break;
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));
1062 #ifdef CONFIG_PPC
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);
1071 first_bootable = 0;
1073 #endif /* CONFIG_PPC */
1075 ++current_minor;
1077 brelse(bh);
1078 printk("\n");
1079 return 1;
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")?.
1089 #define ICD_PARTS
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;
1098 ulong extensect;
1099 #ifdef ICD_PARTS
1100 int part_fmt = 0; /* 0:unknown, 1:AHDI, 2:ICD/Supra */
1101 #endif
1103 bh = bread (dev, 0, get_ptable_blocksize(dev));
1104 if (!bh)
1106 printk (" unable to read block 0\n");
1107 return -1;
1110 rs = (struct rootsector *) bh->b_data;
1111 pi = &rs->part[0];
1112 printk (" AHDI");
1113 for (; pi < &rs->part[4] && minor < m_lim; minor++, pi++)
1115 if (pi->flg & 1)
1116 /* active partition */
1118 if (memcmp (pi->id, "XGM", 3) == 0)
1119 /* extension partition */
1121 struct rootsector *xrs;
1122 struct buffer_head *xbh;
1123 ulong partsect;
1125 #ifdef ICD_PARTS
1126 part_fmt = 1;
1127 #endif
1128 printk(" XGM<");
1129 partsect = extensect = pi->st;
1130 while (1)
1132 xbh = bread (dev, partsect / 2, get_ptable_blocksize(dev));
1133 if (!xbh)
1135 printk (" block %ld read failed\n", partsect);
1136 brelse(bh);
1137 return 0;
1139 if (partsect & 1)
1140 xrs = (struct rootsector *) &xbh->b_data[512];
1141 else
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" );
1147 break;
1150 add_partition(hd, minor, partsect + xrs->part[0].st,
1151 xrs->part[0].siz);
1153 if (!(xrs->part[1].flg & 1)) {
1154 /* end of linked partition list */
1155 brelse( xbh );
1156 break;
1158 if (memcmp( xrs->part[1].id, "XGM", 3 ) != 0) {
1159 printk( "\nID of extended partition is not XGM!\n" );
1160 brelse( xbh );
1161 break;
1164 partsect = xrs->part[1].st + extensect;
1165 brelse (xbh);
1166 minor++;
1167 if (minor >= m_lim) {
1168 printk( "\nMaximum number of partitions reached!\n" );
1169 break;
1172 printk(" >");
1174 else
1176 /* we don't care about other id's */
1177 add_partition (hd, minor, pi->st, pi->siz);
1181 #ifdef ICD_PARTS
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 )
1192 printk(" ICD<");
1193 for (; pi < &rs->icdpart[8] && minor < m_lim; minor++, pi++)
1195 /* accept only GEM,BGM,RAW,LNX,SWP partitions */
1196 if (pi->flg & 1 &&
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) )
1203 part_fmt = 2;
1204 add_partition (hd, minor, pi->st, pi->siz);
1207 printk(" >");
1210 #endif
1211 brelse (bh);
1213 printk ("\n");
1215 return 1;
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;
1223 char buf[32];
1225 if (first_time)
1226 printk("Partition check:\n");
1227 first_time = 0;
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;
1236 return;
1239 printk(" %s:", disk_name(hd, MINOR(dev), buf));
1240 #ifdef CONFIG_MSDOS_PARTITION
1241 if (msdos_partition(hd, dev, first_sector))
1242 return;
1243 #endif
1244 #ifdef CONFIG_OSF_PARTITION
1245 if (osf_partition(hd, dev, first_sector))
1246 return;
1247 #endif
1248 #ifdef CONFIG_SUN_PARTITION
1249 if(sun_partition(hd, dev, first_sector))
1250 return;
1251 #endif
1252 #ifdef CONFIG_AMIGA_PARTITION
1253 if(amiga_partition(hd, dev, first_sector))
1254 return;
1255 #endif
1256 #ifdef CONFIG_ATARI_PARTITION
1257 if(atari_partition(hd, dev, first_sector))
1258 return;
1259 #endif
1260 #ifdef CONFIG_MAC_PARTITION
1261 if (mac_partition(hd, dev, first_sector))
1262 return;
1263 #endif
1264 #ifdef CONFIG_SGI_PARTITION
1265 if(sgi_partition(hd, dev, first_sector))
1266 return;
1267 #endif
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
1273 done */
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)
1282 int i;
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)
1303 int i, drive;
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;
1310 dev->sizes[i] = 0;
1312 dev->init(dev);
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;
1322 #endif
1323 #ifdef CONFIG_MD_BOOT
1324 extern void md_setup_drive(void) __init;
1325 #endif
1326 #ifdef CONFIG_FC4_SOC
1327 extern int soc_probe(void);
1328 #endif
1329 struct gendisk *p;
1331 #ifdef CONFIG_PARPORT
1332 parport_init();
1333 #endif
1334 chr_dev_init();
1335 blk_dev_init();
1336 sti();
1337 #ifdef CONFIG_FC4_SOC
1338 /* This has to be done before scsi_dev_init */
1339 soc_probe();
1340 #endif
1341 #ifdef CONFIG_SCSI
1342 scsi_dev_init();
1343 #endif
1344 #ifdef CONFIG_INET
1345 net_dev_init();
1346 #endif
1347 #ifdef CONFIG_VT
1348 console_map_init();
1349 #endif
1351 for (p = gendisk_head ; p ; p=p->next)
1352 setup_dev(p);
1354 #ifdef CONFIG_BLK_DEV_RAM
1355 #ifdef CONFIG_BLK_DEV_INITRD
1356 if (initrd_start && mount_initrd) initrd_load();
1357 else
1358 #endif
1359 rd_load();
1360 #endif
1361 #ifdef CONFIG_MD_BOOT
1362 md_setup_drive();
1363 #endif
1366 #ifdef CONFIG_PROC_FS
1367 int get_partition_list(char * page)
1369 struct gendisk *p;
1370 char buf[32];
1371 int n, len;
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));
1384 return len;
1386 #endif