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"
27 #include "libvolume_id-private.h"
30 struct hfs_finder_info{
40 static struct hfs_mdb {
59 uint32_t xt_clump_size;
60 uint32_t ct_clump_size;
61 uint16_t num_root_dirs;
64 struct hfs_finder_info finder_info;
66 uint16_t embed_startblock;
67 uint16_t embed_blockcount;
70 struct hfsplus_bnode_descriptor {
79 struct hfsplus_bheader_record {
88 struct hfsplus_catalog_key {
92 uint8_t unicode[255 * 2];
95 struct hfsplus_extent {
100 #define HFSPLUS_EXTENT_COUNT 8
101 struct hfsplus_fork {
104 uint32_t total_blocks;
105 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
108 static struct hfsplus_vol_header {
109 uint8_t signature[2];
112 uint32_t last_mount_vers;
114 uint32_t create_date;
115 uint32_t modify_date;
116 uint32_t backup_date;
117 uint32_t checked_date;
119 uint32_t folder_count;
121 uint32_t total_blocks;
122 uint32_t free_blocks;
124 uint32_t rsrc_clump_sz;
125 uint32_t data_clump_sz;
127 uint32_t write_count;
128 uint64_t encodings_bmp;
129 struct hfs_finder_info finder_info;
130 struct hfsplus_fork alloc_file;
131 struct hfsplus_fork ext_file;
132 struct hfsplus_fork cat_file;
133 struct hfsplus_fork attr_file;
134 struct hfsplus_fork start_file;
137 #define HFS_SUPERBLOCK_OFFSET 0x400
138 #define HFS_NODE_LEAF 0xff
139 #define HFSPLUS_POR_CNID 1
141 static void hfsid_set_uuid(struct volume_id *id, const uint8_t *hfs_id)
145 static const uint8_t hash_init[16] = {
146 0xb3, 0xe2, 0x0f, 0x39, 0xf2, 0x92, 0x11, 0xd6,
147 0x97, 0xa4, 0x00, 0x30, 0x65, 0x43, 0xec, 0xac
151 if (*((uint64_t *)hfs_id) == 0)
155 MD5_Update(&md5c, &hash_init, 16);
156 MD5_Update(&md5c, hfs_id, 8);
157 MD5_Final(uuid, &md5c);
159 uuid[6] = 0x30 | (uuid[6] & 0x0f);
160 uuid[8] = 0x80 | (uuid[8] & 0x3f);
161 volume_id_set_uuid(id, uuid, UUID_DCE);
164 volume_id_set_uuid(id, hfs_id, 0, UUID_64BIT_BE);
167 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off, uint64_t size)
169 unsigned int blocksize;
170 unsigned int cat_block;
171 unsigned int ext_block_start;
172 unsigned int ext_block_count;
174 unsigned int leaf_node_head;
175 unsigned int leaf_node_count;
176 unsigned int leaf_node_size;
177 unsigned int leaf_block;
179 unsigned int alloc_block_size;
180 unsigned int alloc_first_block;
181 unsigned int embed_first_block;
182 unsigned int record_count;
183 struct hfsplus_bnode_descriptor *descr;
184 struct hfsplus_bheader_record *bnode;
185 struct hfsplus_catalog_key *key;
186 unsigned int label_len;
187 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
190 info("probing at offset 0x%llx\n", (unsigned long long) off);
192 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
196 hfs = (struct hfs_mdb *) buf;
197 if (memcmp(hfs->signature, "BD", 2) != 0)
200 /* it may be just a hfs wrapper for hfs+ */
201 if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
202 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
203 dbg("alloc_block_size 0x%x\n", alloc_block_size);
205 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
206 dbg("alloc_first_block 0x%x\n", alloc_first_block);
208 embed_first_block = be16_to_cpu(hfs->embed_startblock);
209 dbg("embed_first_block 0x%x\n", embed_first_block);
211 off += (alloc_first_block * 512) +
212 (embed_first_block * alloc_block_size);
213 dbg("hfs wrapped hfs+ found at offset 0x%llx\n", (unsigned long long) off);
215 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
221 if (hfs->label_len > 0 && hfs->label_len < 28) {
222 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
223 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
226 hfsid_set_uuid(id, hfs->finder_info.id);
228 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
234 hfsplus = (struct hfsplus_vol_header *) buf;
235 if (memcmp(hfsplus->signature, "H+", 2) == 0)
237 if (memcmp(hfsplus->signature, "HX", 2) == 0)
242 hfsid_set_uuid(id, hfsplus->finder_info.id);
244 blocksize = be32_to_cpu(hfsplus->blocksize);
245 dbg("blocksize %u\n", blocksize);
247 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
248 cat_block = be32_to_cpu(extents[0].start_block);
249 dbg("catalog start block 0x%x\n", cat_block);
251 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
255 bnode = (struct hfsplus_bheader_record *)
256 &buf[sizeof(struct hfsplus_bnode_descriptor)];
258 leaf_node_head = be32_to_cpu(bnode->leaf_head);
259 dbg("catalog leaf node 0x%x\n", leaf_node_head);
261 leaf_node_size = be16_to_cpu(bnode->node_size);
262 dbg("leaf node size 0x%x\n", leaf_node_size);
264 leaf_node_count = be32_to_cpu(bnode->leaf_count);
265 dbg("leaf node count 0x%x\n", leaf_node_count);
266 if (leaf_node_count == 0)
269 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
271 /* get physical location */
272 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
273 ext_block_start = be32_to_cpu(extents[ext].start_block);
274 ext_block_count = be32_to_cpu(extents[ext].block_count);
275 dbg("extent start block 0x%x, count 0x%x\n", ext_block_start, ext_block_count);
277 if (ext_block_count == 0)
280 /* this is our extent */
281 if (leaf_block < ext_block_count)
284 leaf_block -= ext_block_count;
286 if (ext == HFSPLUS_EXTENT_COUNT)
288 dbg("found block in extent %i\n", ext);
290 leaf_off = (ext_block_start + leaf_block) * blocksize;
292 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
296 descr = (struct hfsplus_bnode_descriptor *) buf;
297 dbg("descriptor type 0x%x\n", descr->type);
299 record_count = be16_to_cpu(descr->num_recs);
300 dbg("number of records %u\n", record_count);
301 if (record_count == 0)
304 if (descr->type != HFS_NODE_LEAF)
307 key = (struct hfsplus_catalog_key *)
308 &buf[sizeof(struct hfsplus_bnode_descriptor)];
310 dbg("parent id 0x%x\n", be32_to_cpu(key->parent_id));
311 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
314 label_len = be16_to_cpu(key->unicode_len) * 2;
315 dbg("label unicode16 len %i\n", label_len);
316 volume_id_set_label_raw(id, key->unicode, label_len);
317 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
320 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
321 id->type = "hfsplus";