Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  linux/fs/hfs/part_tbl.c
0003  *
0004  * Copyright (C) 1996-1997  Paul H. Hargrove
0005  * (C) 2003 Ardis Technologies <roman@ardistech.com>
0006  * This file may be distributed under the terms of the GNU General Public License.
0007  *
0008  * Original code to handle the new style Mac partition table based on
0009  * a patch contributed by Holger Schemel (aeglos@valinor.owl.de).
0010  */
0011 
0012 #include "hfs_fs.h"
0013 
0014 /*
0015  * The new style Mac partition map
0016  *
0017  * For each partition on the media there is a physical block (512-byte
0018  * block) containing one of these structures.  These blocks are
0019  * contiguous starting at block 1.
0020  */
0021 struct new_pmap {
0022     __be16  pmSig;      /* signature */
0023     __be16  reSigPad;   /* padding */
0024     __be32  pmMapBlkCnt;    /* partition blocks count */
0025     __be32  pmPyPartStart;  /* physical block start of partition */
0026     __be32  pmPartBlkCnt;   /* physical block count of partition */
0027     u8  pmPartName[32]; /* (null terminated?) string
0028                    giving the name of this
0029                    partition */
0030     u8  pmPartType[32]; /* (null terminated?) string
0031                    giving the type of this
0032                    partition */
0033     /* a bunch more stuff we don't need */
0034 } __packed;
0035 
0036 /*
0037  * The old style Mac partition map
0038  *
0039  * The partition map consists for a 2-byte signature followed by an
0040  * array of these structures.  The map is terminated with an all-zero
0041  * one of these.
0042  */
0043 struct old_pmap {
0044     __be16      pdSig;  /* Signature bytes */
0045     struct  old_pmap_entry {
0046         __be32  pdStart;
0047         __be32  pdSize;
0048         __be32  pdFSID;
0049     }   pdEntry[42];
0050 } __packed;
0051 
0052 /*
0053  * hfs_part_find()
0054  *
0055  * Parse the partition map looking for the
0056  * start and length of the 'part'th HFS partition.
0057  */
0058 int hfs_part_find(struct super_block *sb,
0059           sector_t *part_start, sector_t *part_size)
0060 {
0061     struct buffer_head *bh;
0062     __be16 *data;
0063     int i, size, res;
0064 
0065     res = -ENOENT;
0066     bh = sb_bread512(sb, *part_start + HFS_PMAP_BLK, data);
0067     if (!bh)
0068         return -EIO;
0069 
0070     switch (be16_to_cpu(*data)) {
0071     case HFS_OLD_PMAP_MAGIC:
0072       {
0073         struct old_pmap *pm;
0074         struct old_pmap_entry *p;
0075 
0076         pm = (struct old_pmap *)bh->b_data;
0077         p = pm->pdEntry;
0078         size = 42;
0079         for (i = 0; i < size; p++, i++) {
0080             if (p->pdStart && p->pdSize &&
0081                 p->pdFSID == cpu_to_be32(0x54465331)/*"TFS1"*/ &&
0082                 (HFS_SB(sb)->part < 0 || HFS_SB(sb)->part == i)) {
0083                 *part_start += be32_to_cpu(p->pdStart);
0084                 *part_size = be32_to_cpu(p->pdSize);
0085                 res = 0;
0086             }
0087         }
0088         break;
0089       }
0090     case HFS_NEW_PMAP_MAGIC:
0091       {
0092         struct new_pmap *pm;
0093 
0094         pm = (struct new_pmap *)bh->b_data;
0095         size = be32_to_cpu(pm->pmMapBlkCnt);
0096         for (i = 0; i < size;) {
0097             if (!memcmp(pm->pmPartType,"Apple_HFS", 9) &&
0098                 (HFS_SB(sb)->part < 0 || HFS_SB(sb)->part == i)) {
0099                 *part_start += be32_to_cpu(pm->pmPyPartStart);
0100                 *part_size = be32_to_cpu(pm->pmPartBlkCnt);
0101                 res = 0;
0102                 break;
0103             }
0104             brelse(bh);
0105             bh = sb_bread512(sb, *part_start + HFS_PMAP_BLK + ++i, pm);
0106             if (!bh)
0107                 return -EIO;
0108             if (pm->pmSig != cpu_to_be16(HFS_NEW_PMAP_MAGIC))
0109                 break;
0110         }
0111         break;
0112       }
0113     }
0114     brelse(bh);
0115 
0116     return res;
0117 }