2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
26 #include "libvolume_id.h"
29 struct hfs_finder_info{
39 static struct hfs_mdb {
58 uint32_t xt_clump_size;
59 uint32_t ct_clump_size;
60 uint16_t num_root_dirs;
63 struct hfs_finder_info finder_info;
65 uint16_t embed_startblock;
66 uint16_t embed_blockcount;
69 struct hfsplus_bnode_descriptor {
78 struct hfsplus_bheader_record {
87 struct hfsplus_catalog_key {
91 uint8_t unicode[255 * 2];
94 struct hfsplus_extent {
99 #define HFSPLUS_EXTENT_COUNT 8
100 struct hfsplus_fork {
103 uint32_t total_blocks;
104 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
107 static struct hfsplus_vol_header {
108 uint8_t signature[2];
111 uint32_t last_mount_vers;
113 uint32_t create_date;
114 uint32_t modify_date;
115 uint32_t backup_date;
116 uint32_t checked_date;
118 uint32_t folder_count;
120 uint32_t total_blocks;
121 uint32_t free_blocks;
123 uint32_t rsrc_clump_sz;
124 uint32_t data_clump_sz;
126 uint32_t write_count;
127 uint64_t encodings_bmp;
128 struct hfs_finder_info finder_info;
129 struct hfsplus_fork alloc_file;
130 struct hfsplus_fork ext_file;
131 struct hfsplus_fork cat_file;
132 struct hfsplus_fork attr_file;
133 struct hfsplus_fork start_file;
136 #define HFS_SUPERBLOCK_OFFSET 0x400
137 #define HFS_NODE_LEAF 0xff
138 #define HFSPLUS_POR_CNID 1
140 static void hfsid_set_uuid(struct volume_id *id, const uint8_t *hfs_id)
144 static const uint8_t hash_init[16] = {
145 0xb3, 0xe2, 0x0f, 0x39, 0xf2, 0x92, 0x11, 0xd6,
146 0x97, 0xa4, 0x00, 0x30, 0x65, 0x43, 0xec, 0xac
150 if (*((uint64_t *)hfs_id) == 0)
154 MD5_Update(&md5c, &hash_init, 16);
155 MD5_Update(&md5c, hfs_id, 8);
156 MD5_Final(uuid, &md5c);
158 uuid[6] = 0x30 | (uuid[6] & 0x0f);
159 uuid[8] = 0x80 | (uuid[8] & 0x3f);
160 volume_id_set_uuid(id, uuid, UUID_DCE);
163 volume_id_set_uuid(id, hfs_id, 0, UUID_64BIT_BE);
166 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off, uint64_t size)
168 unsigned int blocksize;
169 unsigned int cat_block;
170 unsigned int ext_block_start;
171 unsigned int ext_block_count;
173 unsigned int leaf_node_head;
174 unsigned int leaf_node_count;
175 unsigned int leaf_node_size;
176 unsigned int leaf_block;
178 unsigned int alloc_block_size;
179 unsigned int alloc_first_block;
180 unsigned int embed_first_block;
181 unsigned int record_count;
182 struct hfsplus_bnode_descriptor *descr;
183 struct hfsplus_bheader_record *bnode;
184 struct hfsplus_catalog_key *key;
185 unsigned int label_len;
186 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
189 info("probing at offset 0x%llx", (unsigned long long) off);
191 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
195 hfs = (struct hfs_mdb *) buf;
196 if (memcmp(hfs->signature, "BD", 2) != 0)
199 /* it may be just a hfs wrapper for hfs+ */
200 if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
201 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
202 dbg("alloc_block_size 0x%x", alloc_block_size);
204 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
205 dbg("alloc_first_block 0x%x", alloc_first_block);
207 embed_first_block = be16_to_cpu(hfs->embed_startblock);
208 dbg("embed_first_block 0x%x", embed_first_block);
210 off += (alloc_first_block * 512) +
211 (embed_first_block * alloc_block_size);
212 dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
214 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
220 if (hfs->label_len > 0 && hfs->label_len < 28) {
221 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
222 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
225 hfsid_set_uuid(id, hfs->finder_info.id);
227 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
233 hfsplus = (struct hfsplus_vol_header *) buf;
234 if (memcmp(hfsplus->signature, "H+", 2) == 0)
236 if (memcmp(hfsplus->signature, "HX", 2) == 0)
241 hfsid_set_uuid(id, hfsplus->finder_info.id);
243 blocksize = be32_to_cpu(hfsplus->blocksize);
244 dbg("blocksize %u", blocksize);
246 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
247 cat_block = be32_to_cpu(extents[0].start_block);
248 dbg("catalog start block 0x%x", cat_block);
250 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
254 bnode = (struct hfsplus_bheader_record *)
255 &buf[sizeof(struct hfsplus_bnode_descriptor)];
257 leaf_node_head = be32_to_cpu(bnode->leaf_head);
258 dbg("catalog leaf node 0x%x", leaf_node_head);
260 leaf_node_size = be16_to_cpu(bnode->node_size);
261 dbg("leaf node size 0x%x", leaf_node_size);
263 leaf_node_count = be32_to_cpu(bnode->leaf_count);
264 dbg("leaf node count 0x%x", leaf_node_count);
265 if (leaf_node_count == 0)
268 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
270 /* get physical location */
271 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
272 ext_block_start = be32_to_cpu(extents[ext].start_block);
273 ext_block_count = be32_to_cpu(extents[ext].block_count);
274 dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
276 if (ext_block_count == 0)
279 /* this is our extent */
280 if (leaf_block < ext_block_count)
283 leaf_block -= ext_block_count;
285 if (ext == HFSPLUS_EXTENT_COUNT)
287 dbg("found block in extent %i", ext);
289 leaf_off = (ext_block_start + leaf_block) * blocksize;
291 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
295 descr = (struct hfsplus_bnode_descriptor *) buf;
296 dbg("descriptor type 0x%x", descr->type);
298 record_count = be16_to_cpu(descr->num_recs);
299 dbg("number of records %u", record_count);
300 if (record_count == 0)
303 if (descr->type != HFS_NODE_LEAF)
306 key = (struct hfsplus_catalog_key *)
307 &buf[sizeof(struct hfsplus_bnode_descriptor)];
309 dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
310 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
313 label_len = be16_to_cpu(key->unicode_len) * 2;
314 dbg("label unicode16 len %i", label_len);
315 volume_id_set_label_raw(id, key->unicode, label_len);
316 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
319 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
320 id->type = "hfsplus";