2 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
3 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
5 * This file may be redistributed under the terms of the
6 * GNU Lesser General Public License.
14 #include "superblocks.h"
18 struct hfs_finder_info {
26 } __attribute__((packed));
47 uint32_t xt_clump_size;
48 uint32_t ct_clump_size;
49 uint16_t num_root_dirs;
52 struct hfs_finder_info finder_info;
54 uint16_t embed_startblock;
55 uint16_t embed_blockcount;
56 } __attribute__((packed));
59 #define HFS_NODE_LEAF 0xff
60 #define HFSPLUS_POR_CNID 1
62 struct hfsplus_bnode_descriptor {
69 } __attribute__((packed));
71 struct hfsplus_bheader_record {
78 } __attribute__((packed));
80 struct hfsplus_catalog_key {
84 uint8_t unicode[255 * 2];
85 } __attribute__((packed));
87 struct hfsplus_extent {
90 } __attribute__((packed));
92 #define HFSPLUS_EXTENT_COUNT 8
96 uint32_t total_blocks;
97 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
98 } __attribute__((packed));
100 struct hfsplus_vol_header {
101 uint8_t signature[2];
104 uint32_t last_mount_vers;
106 uint32_t create_date;
107 uint32_t modify_date;
108 uint32_t backup_date;
109 uint32_t checked_date;
111 uint32_t folder_count;
113 uint32_t total_blocks;
114 uint32_t free_blocks;
116 uint32_t rsrc_clump_sz;
117 uint32_t data_clump_sz;
119 uint32_t write_count;
120 uint64_t encodings_bmp;
121 struct hfs_finder_info finder_info;
122 struct hfsplus_fork alloc_file;
123 struct hfsplus_fork ext_file;
124 struct hfsplus_fork cat_file;
125 struct hfsplus_fork attr_file;
126 struct hfsplus_fork start_file;
127 } __attribute__((packed));
129 #define HFSPLUS_SECTOR_SIZE 512
131 static int hfs_set_uuid(blkid_probe pr, unsigned char const *hfs_info, size_t len)
133 static unsigned char const hash_init[MD5LENGTH] = {
134 0xb3, 0xe2, 0x0f, 0x39, 0xf2, 0x92, 0x11, 0xd6,
135 0x97, 0xa4, 0x00, 0x30, 0x65, 0x43, 0xec, 0xac
137 unsigned char uuid[MD5LENGTH];
138 struct MD5Context md5c;
140 if (memcmp(hfs_info, "\0\0\0\0\0\0\0\0", len) == 0)
143 MD5Update(&md5c, hash_init, MD5LENGTH);
144 MD5Update(&md5c, hfs_info, len);
145 MD5Final(uuid, &md5c);
146 uuid[6] = 0x30 | (uuid[6] & 0x0f);
147 uuid[8] = 0x80 | (uuid[8] & 0x3f);
148 return blkid_probe_set_uuid(pr, uuid);
151 static int probe_hfs(blkid_probe pr, const struct blkid_idmag *mag)
155 hfs = blkid_probe_get_sb(pr, mag, struct hfs_mdb);
159 if ((memcmp(hfs->embed_sig, "H+", 2) == 0) ||
160 (memcmp(hfs->embed_sig, "HX", 2) == 0))
161 return 1; /* Not hfs, but an embedded HFS+ */
163 hfs_set_uuid(pr, hfs->finder_info.id, sizeof(hfs->finder_info.id));
165 blkid_probe_set_label(pr, hfs->label, hfs->label_len);
169 static int probe_hfsplus(blkid_probe pr, const struct blkid_idmag *mag)
171 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
172 struct hfsplus_bnode_descriptor *descr;
173 struct hfsplus_bheader_record *bnode;
174 struct hfsplus_catalog_key *key;
175 struct hfsplus_vol_header *hfsplus;
177 unsigned int alloc_block_size;
178 unsigned int alloc_first_block;
179 unsigned int embed_first_block;
180 unsigned int off = 0;
181 unsigned int blocksize;
182 unsigned int cat_block;
183 unsigned int ext_block_start;
184 unsigned int ext_block_count;
185 unsigned int record_count;
186 unsigned int leaf_node_head;
187 unsigned int leaf_node_count;
188 unsigned int leaf_node_size;
189 unsigned int leaf_block;
194 sbd = blkid_probe_get_sb(pr, mag, struct hfs_mdb);
198 /* Check for a HFS+ volume embedded in a HFS volume */
199 if (memcmp(sbd->signature, "BD", 2) == 0) {
200 if ((memcmp(sbd->embed_sig, "H+", 2) != 0) &&
201 (memcmp(sbd->embed_sig, "HX", 2) != 0))
202 /* This must be an HFS volume, so fail */
205 alloc_block_size = be32_to_cpu(sbd->al_blk_size);
206 alloc_first_block = be16_to_cpu(sbd->al_bl_st);
207 embed_first_block = be16_to_cpu(sbd->embed_startblock);
208 off = (alloc_first_block * 512) +
209 (embed_first_block * alloc_block_size);
211 buf = blkid_probe_get_buffer(pr,
212 off + (mag->kboff * 1024),
213 sizeof(struct hfsplus_vol_header));
214 hfsplus = (struct hfsplus_vol_header *) buf;
217 hfsplus = blkid_probe_get_sb(pr, mag,
218 struct hfsplus_vol_header);
223 if ((memcmp(hfsplus->signature, "H+", 2) != 0) &&
224 (memcmp(hfsplus->signature, "HX", 2) != 0))
227 hfs_set_uuid(pr, hfsplus->finder_info.id, sizeof(hfsplus->finder_info.id));
229 blocksize = be32_to_cpu(hfsplus->blocksize);
230 if (blocksize < HFSPLUS_SECTOR_SIZE)
233 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
234 cat_block = be32_to_cpu(extents[0].start_block);
236 buf = blkid_probe_get_buffer(pr,
237 off + ((blkid_loff_t) cat_block * blocksize), 0x2000);
241 bnode = (struct hfsplus_bheader_record *)
242 &buf[sizeof(struct hfsplus_bnode_descriptor)];
244 leaf_node_head = be32_to_cpu(bnode->leaf_head);
245 leaf_node_size = be16_to_cpu(bnode->node_size);
246 leaf_node_count = be32_to_cpu(bnode->leaf_count);
247 if (leaf_node_count == 0)
250 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
252 /* get physical location */
253 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
254 ext_block_start = be32_to_cpu(extents[ext].start_block);
255 ext_block_count = be32_to_cpu(extents[ext].block_count);
256 if (ext_block_count == 0)
259 /* this is our extent */
260 if (leaf_block < ext_block_count)
263 leaf_block -= ext_block_count;
265 if (ext == HFSPLUS_EXTENT_COUNT)
268 leaf_off = (ext_block_start + leaf_block) * blocksize;
270 buf = blkid_probe_get_buffer(pr,
271 (blkid_loff_t) off + leaf_off,
276 descr = (struct hfsplus_bnode_descriptor *) buf;
277 record_count = be16_to_cpu(descr->num_recs);
278 if (record_count == 0)
281 if (descr->type != HFS_NODE_LEAF)
284 key = (struct hfsplus_catalog_key *)
285 &buf[sizeof(struct hfsplus_bnode_descriptor)];
287 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
290 blkid_probe_set_utf8label(pr, key->unicode,
291 be16_to_cpu(key->unicode_len) * 2,
296 const struct blkid_idinfo hfs_idinfo =
299 .usage = BLKID_USAGE_FILESYSTEM,
300 .probefunc = probe_hfs,
301 .flags = BLKID_IDINFO_TOLERANT,
304 { .magic = "BD", .len = 2, .kboff = 1 },
309 const struct blkid_idinfo hfsplus_idinfo =
312 .usage = BLKID_USAGE_FILESYSTEM,
313 .probefunc = probe_hfsplus,
316 { .magic = "BD", .len = 2, .kboff = 1 },
317 { .magic = "H+", .len = 2, .kboff = 1 },
318 { .magic = "HX", .len = 2, .kboff = 1 },