📄 adfspart.c
字号:
/* * linux/arch/arm/drivers/block/adfspart.c * * Copyright (c) 1996 Russell King. * * Scan ADFS partitions on hard disk drives. */#include <linux/config.h>#include <linux/kernel.h>#include <linux/kdev_t.h>#include <linux/major.h>#include <linux/string.h>#include <linux/genhd.h>#include <linux/fs.h>#include "adfs.h"#define CONFIG_ICS#define CONFIG_ADFS#define CONFIG_POWERTEC#undef CONFIG_CUMANA/* * All partition types below here have been disabled. */#define PARTITION_LINUX 9extern void add_partition (struct gendisk *hd, unsigned int minor, unsigned int start, unsigned int size);static void adfspart_setgeometry (kdev_t dev, unsigned int secspertrack, unsigned int heads, unsigned long totalblocks){ extern void hd_set_geometry (kdev_t dev, unsigned char, unsigned char, unsigned long, unsigned int); extern void xd_set_geometry (kdev_t dev, unsigned char, unsigned char, unsigned long, unsigned int); switch (MAJOR(dev)) {#ifdef CONFIG_BLK_DEV_HD /* * It would actually look like the HD driver doesn't actually need this, * since it asks the drive about its the geometry... However, the 'BIOS' * information is not updated... */ case HD_MAJOR: hd_set_geometry (dev, secspertrack, heads, totalblocks, 1); break;#endif#ifdef CONFIG_BLK_DEV_IDE#ifndef CONFIG_BLK_DEV_HD case IDE0_MAJOR:#endif case IDE1_MAJOR: case IDE2_MAJOR: case IDE3_MAJOR: break;#endif#ifdef CONFIG_BLK_DEV_XD case XT_DISK_MAJOR: xd_set_geometry (dev, secspertrack, heads, totalblocks, 1); break;#endif case SCSI_DISK_MAJOR: /* we don't need to set geometry for SCSI disks */ break; default: printk ("%s: don't know how to set geometry\n", kdevname (dev)); break; }}#define LINUX_NATIVE_MAGIC 0xdeafa1de#define LINUX_SWAP_MAGIC 0xdeafab1estruct linux_part { unsigned long magic; unsigned long start_sect; unsigned long nr_sects;};static struct disc_record *adfs_partition (struct gendisk *hd, char *name, char *data, unsigned long first_sector, unsigned int minor){ struct disc_record *dr; unsigned int nr_sects; if (adfs_checkbblk (data)) return NULL; dr = (struct disc_record *)(data + 0x1c0); if (dr->disc_size == 0 && dr->disc_size_high == 0) return NULL; nr_sects = (dr->disc_size_high << 23) | (dr->disc_size >> 9); if (name) printk (" [%s]", name); add_partition (hd, minor, first_sector, nr_sects); return dr;}static int riscix_partition (struct gendisk *hd, kdev_t dev, unsigned long first_sect, unsigned int minor, unsigned long nr_sects){ struct buffer_head *bh; struct riscix_record *rr; unsigned int riscix_minor; printk (" [RISCiX]"); add_partition (hd, riscix_minor = minor++, first_sect, nr_sects); hd->sizes[riscix_minor] = hd->part[riscix_minor].nr_sects >> (BLOCK_SIZE_BITS - 9); dev = MKDEV(hd->major, riscix_minor); if (!(bh = bread (dev, 0, 1024))) return -1; rr = (struct riscix_record *)bh->b_data; if (rr->magic == RISCIX_MAGIC) { int part; printk (" <"); for (part = 0; part < 8; part++) { if (rr->part[part].one && memcmp (rr->part[part].name, "All\0", 4)) { add_partition (hd, minor++, rr->part[part].start, rr->part[part].length); printk ("(%s)", rr->part[part].name); } } printk (" >"); if (hd->part[riscix_minor].nr_sects > 2) hd->part[riscix_minor].nr_sects = 2; } brelse (bh); return minor;}static int linux_partition (struct gendisk *hd, kdev_t dev, unsigned long first_sect, unsigned int minor, unsigned long nr_sects){ struct buffer_head *bh; struct linux_part *linuxp; unsigned int linux_minor, mask = (1 << hd->minor_shift) - 1; printk (" [Linux]"); add_partition (hd, linux_minor = minor++, first_sect, nr_sects); hd->sizes[linux_minor] = hd->part[linux_minor].nr_sects >> (BLOCK_SIZE_BITS - 9); dev = MKDEV(hd->major, linux_minor); if (!(bh = bread (dev, 0, 1024))) return -1; linuxp = (struct linux_part *)bh->b_data; printk (" <"); while (linuxp->magic == LINUX_NATIVE_MAGIC || linuxp->magic == LINUX_SWAP_MAGIC) { if (!(minor & mask)) break; add_partition (hd, minor++, first_sect + linuxp->start_sect, linuxp->nr_sects); linuxp ++; } printk (" >"); /* * Prevent someone doing a mkswap or mkfs on this partition */ if(hd->part[linux_minor].nr_sects > 2) hd->part[linux_minor].nr_sects = 2; brelse(bh); return minor;}#ifdef CONFIG_CUMANAstatic int adfspart_check_CUMANA (struct gendisk *hd, kdev_t dev, unsigned long first_sector, unsigned int minor){ unsigned int start_blk = 0, mask = (1 << hd->minor_shift) - 1; struct buffer_head *bh; char *name = "CUMANA/ADFS"; /* * Try Cumana style partitions - sector 3 contains ADFS boot block with pointer * to next 'drive'. * * There are unknowns in this code - is the 'cylinder number' of the next * partition relative to the start of this one - I'm assuming it is. * * Also, which ID did Cumana use? * * This is totally unfinished, and will require more work to get it going. * Hence it is totally untested. */ do { struct disc_record *dr; unsigned int nr_sects; if (!(minor & mask)) break; if (!(bh = bread (dev, start_blk + 3, 1024))) return -1; dr = adfs_partition (hd, name, bh->b_data, first_sector, minor++); if (!dr) break; name = NULL; nr_sects = (bh->b_data[0x1fd] + (bh->b_data[0x1fe] << 8)) * (dr->heads + (dr->lowsector & 0x40 ? 1 : 0)) * dr->secspertrack; if (!nr_sects) break; first_sector += nr_sects; start_blk += nr_sects >> (BLOCK_SIZE_BITS - 9); nr_sects = 0; /* hmm - should be partition size */ switch (bh->b_data[0x1fc] & 15) { case 0: /* No partition / ADFS? */ break; case PARTITION_RISCIX_SCSI: /* RiscIX - we don't know how to find the next one. */ minor = riscix_partition (hd, dev, first_sector, minor, nr_sects); break; case PARTITION_LINUX: minor = linux_partition (hd, dev, first_sector, minor, nr_sects); break; } brelse (bh); if (minor == -1) return minor; } while (1); brelse (bh); return first ? 0 : 1;}#endif#ifdef CONFIG_ADFS/* * Purpose: allocate ADFS partitions. * * Params : hd - pointer to gendisk structure to store partition info. * dev - device number to access. * first_sector- first readable sector on the device. * minor - first available minor on device. * * Returns: -1 on error, 0 for no ADFS boot sector, 1 for ok. * * Alloc : hda = whole drive * hda1 = ADFS partition on first drive. * hda2 = non-ADFS partition. */static int adfspart_check_ADFS (struct gendisk *hd, kdev_t dev, unsigned long first_sector, unsigned int minor){ unsigned long start_sect, nr_sects, sectscyl, heads; struct buffer_head *bh; struct disc_record *dr; if (!(bh = bread (dev, 3, 1024))) return -1; dr = adfs_partition (hd, "ADFS", bh->b_data, first_sector, minor++); if (!dr) { brelse (bh); return 0; } heads = dr->heads + ((dr->lowsector >> 6) & 1); adfspart_setgeometry (dev, dr->secspertrack, heads, hd->part[MINOR(dev)].nr_sects); sectscyl = dr->secspertrack * heads; /* * Work out start of non-adfs partition. */ start_sect = ((bh->b_data[0x1fe] << 8) + bh->b_data[0x1fd]) * sectscyl; nr_sects = hd->part[MINOR(dev)].nr_sects - start_sect; if (start_sect) { first_sector += start_sect; /* * we now have a problem - how to set the origional disk size if the * disk doesn't report it, since there is no standard way of getting * that info. */ switch (bh->b_data[0x1fc] & 15) { case PARTITION_RISCIX_SCSI: case PARTITION_RISCIX_MFM: minor = riscix_partition (hd, dev, first_sector, minor, nr_sects); break; case PARTITION_LINUX: minor = linux_partition (hd, dev, first_sector, minor, nr_sects); break; } } brelse(bh); return 1;}#endif#ifdef CONFIG_POWERTECstatic int adfspart_check_POWERTEC(struct gendisk *hd, kdev_t dev, unsigned long first_sector, unsigned int minor){ struct buffer_head *bh; unsigned long sum = 0x2a; unsigned int i, next_minor; struct pe { unsigned long unused1; unsigned long unused2; unsigned long start; unsigned long size; unsigned long unused5; char type[8]; } *pe; if (!(bh = bread(dev, 0, 1024))) return -1; for (i = 0; i < 511; i++) sum += bh->b_data[i]; if ((sum & 255) != bh->b_data[511]) { brelse(bh); return 0; /* not Powertec partition table */ } printk(" [PTEC]"); next_minor = minor + 8; for (pe = (struct pe *)bh->b_data, i = 0; i < 8; i += 1, pe += 1) { if (pe->size) { add_partition(hd, minor, first_sector + pe->start, pe->size); if (memcmp(pe->type, "DOSDisc:", 8) == 0) { printk(" <"); printk(" ..."); printk(" >"); } } minor += 1; } brelse(bh); return 1;}#endif#ifdef CONFIG_ICSstatic int adfspart_check_ICSLinux (kdev_t dev, unsigned long block){ struct buffer_head *bh; int result = 0; if ((bh = bread (dev, block >> 1, 1024)) != NULL) { if (memcmp (bh->b_data + ((block & 1) ? 512 : 0), "LinuxPart", 9) == 0) result = 1; brelse (bh); } return result;}/* * Purpose: allocate ICS partitions. * Params : hd - pointer to gendisk structure to store partition info. * dev - device number to access. * first_sector- first readable sector on the device. * minor - first available minor on device. * Returns: -1 on error, 0 for no ICS table, 1 for partitions ok. * Alloc : hda = whole drive * hda1 = ADFS partition 0 on first drive. * hda2 = ADFS partition 1 on first drive. * ..etc.. */static int adfspart_check_ICS (struct gendisk *hd, kdev_t dev, unsigned long first_sector, unsigned int minor){ struct buffer_head *bh; unsigned long sum; unsigned int i, mask = (1 << hd->minor_shift) - 1; struct ics_part { unsigned long start; unsigned long size; } *p; /* * Try ICS style partitions - sector 0 contains partition info. */ if (!(bh = bread (dev, 0, 1024))) return -1; /* * check for a valid checksum */ for (i = 0, sum = 0x50617274; i < 508; i++) sum += bh->b_data[i]; if (sum != *(unsigned long *)(&bh->b_data[508])) { brelse (bh); return 0; /* not ICS partition table */ } printk (" [ICS]"); for (p = (struct ics_part *)bh->b_data; p->size; p++) { if ((minor & mask) == 0) break; if (p->size < 0 && adfspart_check_ICSLinux (dev, p->start)) { /* * We use the first sector to identify what type * this partition is... */ if (p->size > 1) add_partition (hd, minor, first_sector + p->start + 1, p->size - 1); } else add_partition (hd, minor, first_sector + p->start, p->size); minor++; } brelse (bh); return 1;}#endif/* * Purpose: initialise all the partitions on an ADFS drive. * These may be other ADFS partitions or a Linux/RiscBSD/RiscIX * partition. * * Params : hd - pointer to gendisk structure to store devices partitions. * dev - device number to access * first_sector - first available sector on the disk. * minor - first available minor on this device. * * Returns: -1 on error, 0 if not ADFS format, 1 if ok. */int adfspart_initdev (struct gendisk *hd, kdev_t dev, unsigned long first_sector, int minor){ int r = 0;#ifdef CONFIG_ICS if (r == 0) r = adfspart_check_ICS (hd, dev, first_sector, minor);#endif#ifdef CONFIG_CUMANA if (r == 0) r = adfspart_check_CUMANA (hd, dev, first_sector, minor);#endif#ifdef CONFIG_POWERTEC if (r == 0) r = adfspart_check_POWERTEC(hd, dev, first_sector, minor);#endif#ifdef CONFIG_ADFS if (r == 0) r = adfspart_check_ADFS (hd, dev, first_sector, minor);#endif if (r < 0) printk (" unable to read boot sectors / partition sectors\n"); else if (r) printk ("\n"); return r;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -