blob: 40ad88c12c64923e28600a75f05b958a3777726d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/fs/hfsplus/part_tbl.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1996-1997 Paul H. Hargrove
Anton Salikhmetov2753cc22010-12-16 18:08:38 +02005 * This file may be distributed under the terms of
6 * the GNU General Public License.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Original code to handle the new style Mac partition table based on
9 * a patch contributed by Holger Schemel (aeglos@valinor.owl.de).
10 *
11 * In function preconditions the term "valid" applied to a pointer to
12 * a structure means that the pointer is non-NULL and the structure it
13 * points to has all fields initialized to consistent values.
14 *
15 */
16
Christoph Hellwig358f26d2010-11-23 14:37:51 +010017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "hfsplus_fs.h"
19
20/* offsets to various blocks */
21#define HFS_DD_BLK 0 /* Driver Descriptor block */
22#define HFS_PMAP_BLK 1 /* First block of partition map */
23#define HFS_MDB_BLK 2 /* Block (w/i partition) of MDB */
24
25/* magic numbers for various disk blocks */
26#define HFS_DRVR_DESC_MAGIC 0x4552 /* "ER": driver descriptor map */
27#define HFS_OLD_PMAP_MAGIC 0x5453 /* "TS": old-type partition map */
28#define HFS_NEW_PMAP_MAGIC 0x504D /* "PM": new-type partition map */
29#define HFS_SUPER_MAGIC 0x4244 /* "BD": HFS MDB (super block) */
30#define HFS_MFS_SUPER_MAGIC 0xD2D7 /* MFS MDB (super block) */
31
32/*
33 * The new style Mac partition map
34 *
35 * For each partition on the media there is a physical block (512-byte
36 * block) containing one of these structures. These blocks are
37 * contiguous starting at block 1.
38 */
39struct new_pmap {
40 __be16 pmSig; /* signature */
41 __be16 reSigPad; /* padding */
42 __be32 pmMapBlkCnt; /* partition blocks count */
43 __be32 pmPyPartStart; /* physical block start of partition */
44 __be32 pmPartBlkCnt; /* physical block count of partition */
45 u8 pmPartName[32]; /* (null terminated?) string
46 giving the name of this
47 partition */
48 u8 pmPartType[32]; /* (null terminated?) string
49 giving the type of this
50 partition */
51 /* a bunch more stuff we don't need */
52} __packed;
53
54/*
55 * The old style Mac partition map
56 *
57 * The partition map consists for a 2-byte signature followed by an
58 * array of these structures. The map is terminated with an all-zero
59 * one of these.
60 */
61struct old_pmap {
62 __be16 pdSig; /* Signature bytes */
Anton Salikhmetov20b76432010-12-16 18:08:40 +020063 struct old_pmap_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 __be32 pdStart;
65 __be32 pdSize;
66 __be32 pdFSID;
67 } pdEntry[42];
68} __packed;
69
Christoph Hellwig358f26d2010-11-23 14:37:51 +010070static int hfs_parse_old_pmap(struct super_block *sb, struct old_pmap *pm,
71 sector_t *part_start, sector_t *part_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Christoph Hellwigdd73a012010-10-01 05:42:59 +020073 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Christoph Hellwig358f26d2010-11-23 14:37:51 +010074 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Christoph Hellwig358f26d2010-11-23 14:37:51 +010076 for (i = 0; i < 42; i++) {
77 struct old_pmap_entry *p = &pm->pdEntry[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Christoph Hellwig358f26d2010-11-23 14:37:51 +010079 if (p->pdStart && p->pdSize &&
80 p->pdFSID == cpu_to_be32(0x54465331)/*"TFS1"*/ &&
81 (sbi->part < 0 || sbi->part == i)) {
82 *part_start += be32_to_cpu(p->pdStart);
83 *part_size = be32_to_cpu(p->pdSize);
84 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Christoph Hellwig358f26d2010-11-23 14:37:51 +010088 return -ENOENT;
89}
90
91static int hfs_parse_new_pmap(struct super_block *sb, struct new_pmap *pm,
92 sector_t *part_start, sector_t *part_size)
93{
94 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
95 int size = be32_to_cpu(pm->pmMapBlkCnt);
96 int res;
97 int i = 0;
98
99 do {
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200100 if (!memcmp(pm->pmPartType, "Apple_HFS", 9) &&
Christoph Hellwig358f26d2010-11-23 14:37:51 +0100101 (sbi->part < 0 || sbi->part == i)) {
102 *part_start += be32_to_cpu(pm->pmPyPartStart);
103 *part_size = be32_to_cpu(pm->pmPartBlkCnt);
104 return 0;
105 }
106
107 if (++i >= size)
108 return -ENOENT;
109
110 res = hfsplus_submit_bio(sb->s_bdev,
111 *part_start + HFS_PMAP_BLK + i,
112 pm, READ);
113 if (res)
114 return res;
115 } while (pm->pmSig == cpu_to_be16(HFS_NEW_PMAP_MAGIC));
116
117 return -ENOENT;
118}
119
120/*
121 * Parse the partition map looking for the start and length of a
122 * HFS/HFS+ partition.
123 */
124int hfs_part_find(struct super_block *sb,
125 sector_t *part_start, sector_t *part_size)
126{
127 void *data;
128 int res;
129
130 data = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
131 if (!data)
132 return -ENOMEM;
133
134 res = hfsplus_submit_bio(sb->s_bdev, *part_start + HFS_PMAP_BLK,
135 data, READ);
136 if (res)
Chuck Ebbert14dd01f2011-02-01 16:41:55 -0500137 goto out;
Christoph Hellwig358f26d2010-11-23 14:37:51 +0100138
139 switch (be16_to_cpu(*((__be16 *)data))) {
140 case HFS_OLD_PMAP_MAGIC:
141 res = hfs_parse_old_pmap(sb, data, part_start, part_size);
142 break;
143 case HFS_NEW_PMAP_MAGIC:
144 res = hfs_parse_new_pmap(sb, data, part_start, part_size);
145 break;
146 default:
147 res = -ENOENT;
148 break;
149 }
Chuck Ebbert14dd01f2011-02-01 16:41:55 -0500150out:
Christoph Hellwig358f26d2010-11-23 14:37:51 +0100151 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return res;
153}