2 * linux/fs/hfs/part_tbl.c
4 * Copyright (C) 1996-1997 Paul H. Hargrove
5 * This file may be distributed under the terms of the GNU General Public License.
7 * Original code to handle the new style Mac partition table based on
8 * a patch contributed by Holger Schemel (aeglos@valinor.owl.de).
10 * "XXX" in a comment is a note to myself to consider changing something.
12 * In function preconditions the term "valid" applied to a pointer to
13 * a structure means that the pointer is non-NULL and the structure it
14 * points to has all fields initialized to consistent values.
16 * The code in this file initializes some structures which contain
17 * pointers by calling memset(&foo, 0, sizeof(foo)).
18 * This produces the desired behavior only due to the non-ANSI
19 * assumption that the machine representation of NULL is all zeros.
24 /*================ File-local data types ================*/
27 * The Macintosh Driver Descriptor Block
29 * On partitioned Macintosh media this is block 0.
30 * We really only need the "magic number" to check for partitioned media.
32 struct hfs_drvr_desc {
33 hfs_word_t ddSig; /* The signature word */
34 /* a bunch more stuff we don't need */
38 * The new style Mac partition map
40 * For each partition on the media there is a physical block (512-byte
41 * block) containing one of these structures. These blocks are
42 * contiguous starting at block 1.
45 hfs_word_t pmSig; /* Signature bytes to verify
46 that this is a partition
48 hfs_word_t reSigPad; /* padding */
49 hfs_lword_t pmMapBlkCnt; /* (At least in block 1) this
50 is the number of partition
52 hfs_lword_t pmPyPartStart; /* The physical block number
53 of the first block in this
55 hfs_lword_t pmPartBlkCnt; /* The number of physical
56 blocks in this partition */
57 hfs_byte_t pmPartName[32]; /* (null terminated?) string
58 giving the name of this
60 hfs_byte_t pmPartType[32]; /* (null terminated?) string
61 giving the type of this
63 /* a bunch more stuff we don't need */
67 * The old style Mac partition map
69 * The partition map consists for a 2-byte signature followed by an
70 * array of these structures. The map is terminated with an all-zero
74 hfs_word_t pdSig; /* Signature bytes */
75 struct old_pmap_entry {
80 } __attribute__((packed));
82 /*================ File-local functions ================*/
85 * parse_new_part_table()
87 * Parse a new style partition map looking for the
88 * start and length of the 'part'th HFS partition.
90 static int parse_new_part_table(hfs_sysmdb sys_mdb, hfs_buffer buf,
91 int part, hfs_s32 *size, hfs_s32 *start)
93 struct new_pmap *pm = (struct new_pmap *)hfs_buffer_data(buf);
94 hfs_u32 pmap_entries = hfs_get_hl(pm->pmMapBlkCnt);
98 for (entry = 0; (entry < pmap_entries) && !(*start); ++entry) {
100 /* read the next partition map entry */
101 buf = hfs_buffer_get(sys_mdb, HFS_PMAP_BLK + entry, 1);
102 if (!hfs_buffer_ok(buf)) {
103 hfs_warn("hfs_fs: unable to "
104 "read partition map.\n");
107 pm = (struct new_pmap *)hfs_buffer_data(buf);
108 if (hfs_get_ns(pm->pmSig) !=
109 htons(HFS_NEW_PMAP_MAGIC)) {
110 hfs_warn("hfs_fs: invalid "
111 "entry in partition map\n");
117 /* look for an HFS partition */
118 if (!memcmp(pm->pmPartType,"Apple_HFS",9) &&
119 ((hfs_part++) == part)) {
121 *start = hfs_get_hl(pm->pmPyPartStart);
122 *size = hfs_get_hl(pm->pmPartBlkCnt);
135 * parse_old_part_table()
137 * Parse a old style partition map looking for the
138 * start and length of the 'part'th HFS partition.
140 static int parse_old_part_table(hfs_sysmdb sys_mdb, hfs_buffer buf,
141 int part, hfs_s32 *size, hfs_s32 *start)
143 struct old_pmap *pm = (struct old_pmap *)hfs_buffer_data(buf);
144 struct old_pmap_entry *p = &pm->pdEntry[0];
147 while ((p->pdStart || p->pdSize || p->pdFSID) && !(*start)) {
148 /* look for an HFS partition */
149 if ((hfs_get_nl(p->pdFSID) == htonl(0x54465331)/*"TFS1"*/) &&
150 ((hfs_part++) == part)) {
152 *start = hfs_get_hl(p->pdStart);
153 *size = hfs_get_hl(p->pdSize);
162 /*================ Global functions ================*/
167 * Parse the partition map looking for the
168 * start and length of the 'part'th HFS partition.
170 int hfs_part_find(hfs_sysmdb sys_mdb, int part, int silent,
171 hfs_s32 *size, hfs_s32 *start)
178 /* Read block 0 to see if this media is partitioned */
179 buf = hfs_buffer_get(sys_mdb, HFS_DD_BLK, 1);
180 if (!hfs_buffer_ok(buf)) {
181 hfs_warn("hfs_fs: Unable to read block 0.\n");
184 sig = hfs_get_ns(((struct hfs_drvr_desc *)hfs_buffer_data(buf))->ddSig);
187 if (sig == htons(HFS_DRVR_DESC_MAGIC)) {
188 /* We are definitely on partitioned media. */
192 buf = hfs_buffer_get(sys_mdb, HFS_PMAP_BLK, 1);
193 if (!hfs_buffer_ok(buf)) {
194 hfs_warn("hfs_fs: Unable to read block 1.\n");
200 switch (hfs_get_ns(hfs_buffer_data(buf))) {
201 case __constant_htons(HFS_OLD_PMAP_MAGIC):
202 retval = parse_old_part_table(sys_mdb, buf, part, size, start);
205 case __constant_htons(HFS_NEW_PMAP_MAGIC):
206 retval = parse_new_part_table(sys_mdb, buf, part, size, start);
211 /* The media claimed to have a partition map */
213 hfs_warn("hfs_fs: This disk has an "
214 "unrecognized partition map type.\n");
217 /* Conclude that the media is not partitioned */
226 hfs_warn("hfs_fs: unable to locate "
227 "HFS partition number %d.\n", part);
229 hfs_warn("hfs_fs: unable to locate any "
230 "HFS partitions.\n");
233 } else if (*size < 0) {
234 hfs_warn("hfs_fs: Partition size > 1 Terabyte.\n");
236 } else if (*start < 0) {
237 hfs_warn("hfs_fs: Partition begins beyond 1 "