2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
35 struct hfs_finder_info{
45 static struct hfs_mdb {
64 uint32_t xt_clump_size;
65 uint32_t ct_clump_size;
66 uint16_t num_root_dirs;
69 struct hfs_finder_info finder_info;
71 uint16_t embed_startblock;
72 uint16_t embed_blockcount;
75 struct hfsplus_bnode_descriptor {
84 struct hfsplus_bheader_record {
93 struct hfsplus_catalog_key {
97 uint8_t unicode[255 * 2];
100 struct hfsplus_extent {
101 uint32_t start_block;
102 uint32_t block_count;
105 #define HFSPLUS_EXTENT_COUNT 8
106 struct hfsplus_fork {
109 uint32_t total_blocks;
110 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
113 static struct hfsplus_vol_header {
114 uint8_t signature[2];
117 uint32_t last_mount_vers;
119 uint32_t create_date;
120 uint32_t modify_date;
121 uint32_t backup_date;
122 uint32_t checked_date;
124 uint32_t folder_count;
126 uint32_t total_blocks;
127 uint32_t free_blocks;
129 uint32_t rsrc_clump_sz;
130 uint32_t data_clump_sz;
132 uint32_t write_count;
133 uint64_t encodings_bmp;
134 struct hfs_finder_info finder_info;
135 struct hfsplus_fork alloc_file;
136 struct hfsplus_fork ext_file;
137 struct hfsplus_fork cat_file;
138 struct hfsplus_fork attr_file;
139 struct hfsplus_fork start_file;
142 #define HFS_SUPERBLOCK_OFFSET 0x400
143 #define HFS_NODE_LEAF 0xff
144 #define HFSPLUS_POR_CNID 1
146 static void hfsid_set_uuid(struct volume_id *id, const uint8_t *hfs_id)
149 static const uint8_t hash_init[16] = {
150 0xb3, 0xe2, 0x0f, 0x39, 0xf2, 0x92, 0x11, 0xd6,
151 0x97, 0xa4, 0x00, 0x30, 0x65, 0x43, 0xec, 0xac
155 if (*((uint64_t *)hfs_id) == 0)
159 md5_update(&md5c, hash_init, 16);
160 md5_update(&md5c, hfs_id, 8);
161 md5_final(&md5c, uuid);
163 uuid[6] = 0x30 | (uuid[6] & 0x0f);
164 uuid[8] = 0x80 | (uuid[8] & 0x3f);
165 volume_id_set_uuid(id, uuid, 0, UUID_DCE);
168 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off, uint64_t size)
170 unsigned int blocksize;
171 unsigned int cat_block;
172 unsigned int ext_block_start;
173 unsigned int ext_block_count;
175 unsigned int leaf_node_head;
176 unsigned int leaf_node_count;
177 unsigned int leaf_node_size;
178 unsigned int leaf_block;
180 unsigned int alloc_block_size;
181 unsigned int alloc_first_block;
182 unsigned int embed_first_block;
183 unsigned int record_count;
184 struct hfsplus_bnode_descriptor *descr;
185 struct hfsplus_bheader_record *bnode;
186 struct hfsplus_catalog_key *key;
187 unsigned int label_len;
188 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
191 info("probing at offset 0x%llx\n", (unsigned long long) off);
193 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
197 hfs = (struct hfs_mdb *) buf;
198 if (memcmp(hfs->signature, "BD", 2) != 0)
201 /* it may be just a hfs wrapper for hfs+ */
202 if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
203 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
204 dbg("alloc_block_size 0x%x\n", alloc_block_size);
206 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
207 dbg("alloc_first_block 0x%x\n", alloc_first_block);
209 embed_first_block = be16_to_cpu(hfs->embed_startblock);
210 dbg("embed_first_block 0x%x\n", embed_first_block);
212 off += (alloc_first_block * 512) +
213 (embed_first_block * alloc_block_size);
214 dbg("hfs wrapped hfs+ found at offset 0x%llx\n", (unsigned long long) off);
216 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
222 if (hfs->label_len > 0 && hfs->label_len < 28) {
223 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
224 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
227 hfsid_set_uuid(id, hfs->finder_info.id);
229 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
235 hfsplus = (struct hfsplus_vol_header *) buf;
236 if (memcmp(hfsplus->signature, "H+", 2) == 0)
238 if (memcmp(hfsplus->signature, "HX", 2) == 0)
243 hfsid_set_uuid(id, hfsplus->finder_info.id);
245 blocksize = be32_to_cpu(hfsplus->blocksize);
246 dbg("blocksize %u\n", blocksize);
248 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
249 cat_block = be32_to_cpu(extents[0].start_block);
250 dbg("catalog start block 0x%x\n", cat_block);
252 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
256 bnode = (struct hfsplus_bheader_record *)
257 &buf[sizeof(struct hfsplus_bnode_descriptor)];
259 leaf_node_head = be32_to_cpu(bnode->leaf_head);
260 dbg("catalog leaf node 0x%x\n", leaf_node_head);
262 leaf_node_size = be16_to_cpu(bnode->node_size);
263 dbg("leaf node size 0x%x\n", leaf_node_size);
265 leaf_node_count = be32_to_cpu(bnode->leaf_count);
266 dbg("leaf node count 0x%x\n", leaf_node_count);
267 if (leaf_node_count == 0)
270 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
272 /* get physical location */
273 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
274 ext_block_start = be32_to_cpu(extents[ext].start_block);
275 ext_block_count = be32_to_cpu(extents[ext].block_count);
276 dbg("extent start block 0x%x, count 0x%x\n", ext_block_start, ext_block_count);
278 if (ext_block_count == 0)
281 /* this is our extent */
282 if (leaf_block < ext_block_count)
285 leaf_block -= ext_block_count;
287 if (ext == HFSPLUS_EXTENT_COUNT)
289 dbg("found block in extent %i\n", ext);
291 leaf_off = (ext_block_start + leaf_block) * blocksize;
293 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
297 descr = (struct hfsplus_bnode_descriptor *) buf;
298 dbg("descriptor type 0x%x\n", descr->type);
300 record_count = be16_to_cpu(descr->num_recs);
301 dbg("number of records %u\n", record_count);
302 if (record_count == 0)
305 if (descr->type != HFS_NODE_LEAF)
308 key = (struct hfsplus_catalog_key *)
309 &buf[sizeof(struct hfsplus_bnode_descriptor)];
311 dbg("parent id 0x%x\n", be32_to_cpu(key->parent_id));
312 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
315 label_len = be16_to_cpu(key->unicode_len) * 2;
316 dbg("label unicode16 len %i\n", label_len);
317 volume_id_set_label_raw(id, key->unicode, label_len);
318 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
321 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
322 id->type = "hfsplus";