^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 1) /*
f30c2269544bf (Uwe Zeisberger 2006-10-03 23:01:26 +0200 2) * linux/fs/hfsplus/part_tbl.c
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) * Copyright (C) 1996-1997 Paul H. Hargrove
2753cc281c9a0 (Anton Salikhmetov 2010-12-16 18:08:38 +0200 5) * This file may be distributed under the terms of
2753cc281c9a0 (Anton Salikhmetov 2010-12-16 18:08:38 +0200 6) * the GNU General Public License.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) * Original code to handle the new style Mac partition table based on
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9) * a patch contributed by Holger Schemel (aeglos@valinor.owl.de).
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 10) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) * In function preconditions the term "valid" applied to a pointer to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) * a structure means that the pointer is non-NULL and the structure it
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 13) * points to has all fields initialized to consistent values.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 14) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 15) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 17) #include <linux/slab.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 18) #include "hfsplus_fs.h"
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 20) /* offsets to various blocks */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 21) #define HFS_DD_BLK 0 /* Driver Descriptor block */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 22) #define HFS_PMAP_BLK 1 /* First block of partition map */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 23) #define HFS_MDB_BLK 2 /* Block (w/i partition) of MDB */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 24)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 25) /* magic numbers for various disk blocks */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 26) #define HFS_DRVR_DESC_MAGIC 0x4552 /* "ER": driver descriptor map */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 27) #define HFS_OLD_PMAP_MAGIC 0x5453 /* "TS": old-type partition map */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 28) #define HFS_NEW_PMAP_MAGIC 0x504D /* "PM": new-type partition map */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 29) #define HFS_SUPER_MAGIC 0x4244 /* "BD": HFS MDB (super block) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 30) #define HFS_MFS_SUPER_MAGIC 0xD2D7 /* MFS MDB (super block) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 31)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 32) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 33) * The new style Mac partition map
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 34) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 35) * For each partition on the media there is a physical block (512-byte
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 36) * block) containing one of these structures. These blocks are
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 37) * contiguous starting at block 1.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 38) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 39) struct new_pmap {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 40) __be16 pmSig; /* signature */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 41) __be16 reSigPad; /* padding */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 42) __be32 pmMapBlkCnt; /* partition blocks count */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 43) __be32 pmPyPartStart; /* physical block start of partition */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 44) __be32 pmPartBlkCnt; /* physical block count of partition */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 45) u8 pmPartName[32]; /* (null terminated?) string
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 46) giving the name of this
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 47) partition */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 48) u8 pmPartType[32]; /* (null terminated?) string
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 49) giving the type of this
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 50) partition */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 51) /* a bunch more stuff we don't need */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 52) } __packed;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 53)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 54) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 55) * The old style Mac partition map
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 56) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 57) * The partition map consists for a 2-byte signature followed by an
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 58) * array of these structures. The map is terminated with an all-zero
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 59) * one of these.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 60) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 61) struct old_pmap {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 62) __be16 pdSig; /* Signature bytes */
20b7643d8ee44 (Anton Salikhmetov 2010-12-16 18:08:40 +0200 63) struct old_pmap_entry {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 64) __be32 pdStart;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 65) __be32 pdSize;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 66) __be32 pdFSID;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 67) } pdEntry[42];
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 68) } __packed;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 69)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 70) static int hfs_parse_old_pmap(struct super_block *sb, struct old_pmap *pm,
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 71) sector_t *part_start, sector_t *part_size)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 72) {
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 73) struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 74) int i;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 75)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 76) for (i = 0; i < 42; i++) {
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 77) struct old_pmap_entry *p = &pm->pdEntry[i];
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 78)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 79) if (p->pdStart && p->pdSize &&
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 80) p->pdFSID == cpu_to_be32(0x54465331)/*"TFS1"*/ &&
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 81) (sbi->part < 0 || sbi->part == i)) {
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 82) *part_start += be32_to_cpu(p->pdStart);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 83) *part_size = be32_to_cpu(p->pdSize);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 84) return 0;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 85) }
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 86) }
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 87)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 88) return -ENOENT;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 89) }
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 90)
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 91) static int hfs_parse_new_pmap(struct super_block *sb, void *buf,
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 92) struct new_pmap *pm, sector_t *part_start, sector_t *part_size)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 93) {
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 94) struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 95) int size = be32_to_cpu(pm->pmMapBlkCnt);
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 96) int buf_size = hfsplus_min_io_size(sb);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 97) int res;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 98) int i = 0;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 99)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 100) do {
20b7643d8ee44 (Anton Salikhmetov 2010-12-16 18:08:40 +0200 101) if (!memcmp(pm->pmPartType, "Apple_HFS", 9) &&
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 102) (sbi->part < 0 || sbi->part == i)) {
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 103) *part_start += be32_to_cpu(pm->pmPyPartStart);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 104) *part_size = be32_to_cpu(pm->pmPartBlkCnt);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 105) return 0;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 106) }
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 107)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 108) if (++i >= size)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 109) return -ENOENT;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 110)
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 111) pm = (struct new_pmap *)((u8 *)pm + HFSPLUS_SECTOR_SIZE);
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 112) if ((u8 *)pm - (u8 *)buf >= buf_size) {
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 113) res = hfsplus_submit_bio(sb,
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 114) *part_start + HFS_PMAP_BLK + i,
67ed25961c428 (Mike Christie 2016-06-05 14:31:58 -0500 115) buf, (void **)&pm, REQ_OP_READ,
67ed25961c428 (Mike Christie 2016-06-05 14:31:58 -0500 116) 0);
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 117) if (res)
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 118) return res;
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 119) }
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 120) } while (pm->pmSig == cpu_to_be16(HFS_NEW_PMAP_MAGIC));
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 121)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 122) return -ENOENT;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 123) }
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 124)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 125) /*
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 126) * Parse the partition map looking for the start and length of a
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 127) * HFS/HFS+ partition.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 128) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 129) int hfs_part_find(struct super_block *sb,
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 130) sector_t *part_start, sector_t *part_size)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 131) {
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 132) void *buf, *data;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 133) int res;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 134)
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 135) buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 136) if (!buf)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 137) return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 138)
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 139) res = hfsplus_submit_bio(sb, *part_start + HFS_PMAP_BLK,
67ed25961c428 (Mike Christie 2016-06-05 14:31:58 -0500 140) buf, &data, REQ_OP_READ, 0);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 141) if (res)
14dd01f88319a (Chuck Ebbert 2011-02-01 16:41:55 -0500 142) goto out;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 143)
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 144) switch (be16_to_cpu(*((__be16 *)data))) {
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 145) case HFS_OLD_PMAP_MAGIC:
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 146) res = hfs_parse_old_pmap(sb, data, part_start, part_size);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 147) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 148) case HFS_NEW_PMAP_MAGIC:
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 149) res = hfs_parse_new_pmap(sb, buf, data, part_start, part_size);
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 150) break;
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 151) default:
358f26d52680c (Christoph Hellwig 2010-11-23 14:37:51 +0100 152) res = -ENOENT;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 153) break;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 154) }
14dd01f88319a (Chuck Ebbert 2011-02-01 16:41:55 -0500 155) out:
6596528e391ad (Seth Forshee 2011-07-18 08:06:23 -0700 156) kfree(buf);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 157) return res;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 158) }