xref: /kernel/linux/linux-5.10/fs/hfs/part_tbl.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  linux/fs/hfs/part_tbl.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 1996-1997  Paul H. Hargrove
58c2ecf20Sopenharmony_ci * (C) 2003 Ardis Technologies <roman@ardistech.com>
68c2ecf20Sopenharmony_ci * This file may be distributed under the terms of the GNU General Public License.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Original code to handle the new style Mac partition table based on
98c2ecf20Sopenharmony_ci * a patch contributed by Holger Schemel (aeglos@valinor.owl.de).
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "hfs_fs.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * The new style Mac partition map
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * For each partition on the media there is a physical block (512-byte
188c2ecf20Sopenharmony_ci * block) containing one of these structures.  These blocks are
198c2ecf20Sopenharmony_ci * contiguous starting at block 1.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_cistruct new_pmap {
228c2ecf20Sopenharmony_ci	__be16	pmSig;		/* signature */
238c2ecf20Sopenharmony_ci	__be16	reSigPad;	/* padding */
248c2ecf20Sopenharmony_ci	__be32	pmMapBlkCnt;	/* partition blocks count */
258c2ecf20Sopenharmony_ci	__be32	pmPyPartStart;	/* physical block start of partition */
268c2ecf20Sopenharmony_ci	__be32	pmPartBlkCnt;	/* physical block count of partition */
278c2ecf20Sopenharmony_ci	u8	pmPartName[32];	/* (null terminated?) string
288c2ecf20Sopenharmony_ci				   giving the name of this
298c2ecf20Sopenharmony_ci				   partition */
308c2ecf20Sopenharmony_ci	u8	pmPartType[32];	/* (null terminated?) string
318c2ecf20Sopenharmony_ci				   giving the type of this
328c2ecf20Sopenharmony_ci				   partition */
338c2ecf20Sopenharmony_ci	/* a bunch more stuff we don't need */
348c2ecf20Sopenharmony_ci} __packed;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * The old style Mac partition map
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * The partition map consists for a 2-byte signature followed by an
408c2ecf20Sopenharmony_ci * array of these structures.  The map is terminated with an all-zero
418c2ecf20Sopenharmony_ci * one of these.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistruct old_pmap {
448c2ecf20Sopenharmony_ci	__be16		pdSig;	/* Signature bytes */
458c2ecf20Sopenharmony_ci	struct 	old_pmap_entry {
468c2ecf20Sopenharmony_ci		__be32	pdStart;
478c2ecf20Sopenharmony_ci		__be32	pdSize;
488c2ecf20Sopenharmony_ci		__be32	pdFSID;
498c2ecf20Sopenharmony_ci	}	pdEntry[42];
508c2ecf20Sopenharmony_ci} __packed;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/*
538c2ecf20Sopenharmony_ci * hfs_part_find()
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * Parse the partition map looking for the
568c2ecf20Sopenharmony_ci * start and length of the 'part'th HFS partition.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ciint hfs_part_find(struct super_block *sb,
598c2ecf20Sopenharmony_ci		  sector_t *part_start, sector_t *part_size)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct buffer_head *bh;
628c2ecf20Sopenharmony_ci	__be16 *data;
638c2ecf20Sopenharmony_ci	int i, size, res;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	res = -ENOENT;
668c2ecf20Sopenharmony_ci	bh = sb_bread512(sb, *part_start + HFS_PMAP_BLK, data);
678c2ecf20Sopenharmony_ci	if (!bh)
688c2ecf20Sopenharmony_ci		return -EIO;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	switch (be16_to_cpu(*data)) {
718c2ecf20Sopenharmony_ci	case HFS_OLD_PMAP_MAGIC:
728c2ecf20Sopenharmony_ci	  {
738c2ecf20Sopenharmony_ci		struct old_pmap *pm;
748c2ecf20Sopenharmony_ci		struct old_pmap_entry *p;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci		pm = (struct old_pmap *)bh->b_data;
778c2ecf20Sopenharmony_ci		p = pm->pdEntry;
788c2ecf20Sopenharmony_ci		size = 42;
798c2ecf20Sopenharmony_ci		for (i = 0; i < size; p++, i++) {
808c2ecf20Sopenharmony_ci			if (p->pdStart && p->pdSize &&
818c2ecf20Sopenharmony_ci			    p->pdFSID == cpu_to_be32(0x54465331)/*"TFS1"*/ &&
828c2ecf20Sopenharmony_ci			    (HFS_SB(sb)->part < 0 || HFS_SB(sb)->part == i)) {
838c2ecf20Sopenharmony_ci				*part_start += be32_to_cpu(p->pdStart);
848c2ecf20Sopenharmony_ci				*part_size = be32_to_cpu(p->pdSize);
858c2ecf20Sopenharmony_ci				res = 0;
868c2ecf20Sopenharmony_ci			}
878c2ecf20Sopenharmony_ci		}
888c2ecf20Sopenharmony_ci		break;
898c2ecf20Sopenharmony_ci	  }
908c2ecf20Sopenharmony_ci	case HFS_NEW_PMAP_MAGIC:
918c2ecf20Sopenharmony_ci	  {
928c2ecf20Sopenharmony_ci		struct new_pmap *pm;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci		pm = (struct new_pmap *)bh->b_data;
958c2ecf20Sopenharmony_ci		size = be32_to_cpu(pm->pmMapBlkCnt);
968c2ecf20Sopenharmony_ci		for (i = 0; i < size;) {
978c2ecf20Sopenharmony_ci			if (!memcmp(pm->pmPartType,"Apple_HFS", 9) &&
988c2ecf20Sopenharmony_ci			    (HFS_SB(sb)->part < 0 || HFS_SB(sb)->part == i)) {
998c2ecf20Sopenharmony_ci				*part_start += be32_to_cpu(pm->pmPyPartStart);
1008c2ecf20Sopenharmony_ci				*part_size = be32_to_cpu(pm->pmPartBlkCnt);
1018c2ecf20Sopenharmony_ci				res = 0;
1028c2ecf20Sopenharmony_ci				break;
1038c2ecf20Sopenharmony_ci			}
1048c2ecf20Sopenharmony_ci			brelse(bh);
1058c2ecf20Sopenharmony_ci			bh = sb_bread512(sb, *part_start + HFS_PMAP_BLK + ++i, pm);
1068c2ecf20Sopenharmony_ci			if (!bh)
1078c2ecf20Sopenharmony_ci				return -EIO;
1088c2ecf20Sopenharmony_ci			if (pm->pmSig != cpu_to_be16(HFS_NEW_PMAP_MAGIC))
1098c2ecf20Sopenharmony_ci				break;
1108c2ecf20Sopenharmony_ci		}
1118c2ecf20Sopenharmony_ci		break;
1128c2ecf20Sopenharmony_ci	  }
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci	brelse(bh);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	return res;
1178c2ecf20Sopenharmony_ci}
118