2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //config:config FEATURE_VOLUMEID_HFS
21 //config: bool "hfs filesystem"
23 //config: depends on VOLUMEID
25 //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_HFS) += hfs.o
27 #include "volume_id_internal.h"
29 struct hfs_finder_info{
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 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 FAST_FUNC hfs_set_uuid(struct volume_id *id, const uint8_t *hfs_id)
147 for (i = 0; i < hfs_id_len; i++)
153 md5_hash(&md5c, "\263\342\17\71\362\222\21\326\227\244\0\60\145\103\354\254", 16);
154 md5_hash(&md5c, hfs_id, hfs_id_len);
155 md5_end(&md5c, uuid);
156 uuid[6] = 0x30 | (uuid[6] & 0x0f);
157 uuid[8] = 0x80 | (uuid[8] & 0x3f);
158 volume_id_set_uuid(id, uuid, UUID_DCE);
161 int FAST_FUNC volume_id_probe_hfs_hfsplus(struct volume_id *id /*,uint64_t off*/)
166 unsigned ext_block_start;
167 unsigned ext_block_count;
169 unsigned leaf_node_head;
170 unsigned leaf_node_count;
171 unsigned leaf_node_size;
174 unsigned alloc_block_size;
175 unsigned alloc_first_block;
176 unsigned embed_first_block;
177 unsigned record_count;
178 struct hfsplus_vol_header *hfsplus;
179 struct hfsplus_bnode_descriptor *descr;
180 struct hfsplus_bheader_record *bnode;
181 struct hfsplus_catalog_key *key;
183 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
187 dbg("probing at offset 0x%llx", (unsigned long long) off);
189 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
193 hfs = (struct hfs_mdb *) buf;
194 if (hfs->signature[0] != 'B' || hfs->signature[1] != 'D')
197 /* it may be just a hfs wrapper for hfs+ */
198 if (hfs->embed_sig[0] == 'H' && hfs->embed_sig[1] == '+') {
199 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
200 dbg("alloc_block_size 0x%x", alloc_block_size);
202 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
203 dbg("alloc_first_block 0x%x", alloc_first_block);
205 embed_first_block = be16_to_cpu(hfs->embed_startblock);
206 dbg("embed_first_block 0x%x", embed_first_block);
208 off += (alloc_first_block * 512) +
209 (embed_first_block * alloc_block_size);
210 dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
212 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
218 if (hfs->label_len > 0 && hfs->label_len < 28) {
219 // volume_id_set_label_raw(id, hfs->label, hfs->label_len);
220 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
223 hfs_set_uuid(id, hfs->finder_info.id);
224 // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
225 IF_FEATURE_BLKID_TYPE(id->type = "hfs";)
230 hfsplus = (struct hfsplus_vol_header *) buf;
231 if (hfs->signature[0] == 'H')
232 if (hfs->signature[1] == '+' || hfs->signature[1] == 'X')
237 hfs_set_uuid(id, hfsplus->finder_info.id);
239 blocksize = be32_to_cpu(hfsplus->blocksize);
240 dbg("blocksize %u", blocksize);
242 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
243 cat_block = be32_to_cpu(extents[0].start_block);
244 dbg("catalog start block 0x%x", cat_block);
246 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
250 bnode = (struct hfsplus_bheader_record *)
251 &buf[sizeof(struct hfsplus_bnode_descriptor)];
253 leaf_node_head = be32_to_cpu(bnode->leaf_head);
254 dbg("catalog leaf node 0x%x", leaf_node_head);
256 leaf_node_size = be16_to_cpu(bnode->node_size);
257 dbg("leaf node size 0x%x", leaf_node_size);
259 leaf_node_count = be32_to_cpu(bnode->leaf_count);
260 dbg("leaf node count 0x%x", leaf_node_count);
261 if (leaf_node_count == 0)
264 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
266 /* get physical location */
267 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
268 ext_block_start = be32_to_cpu(extents[ext].start_block);
269 ext_block_count = be32_to_cpu(extents[ext].block_count);
270 dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
272 if (ext_block_count == 0)
275 /* this is our extent */
276 if (leaf_block < ext_block_count)
279 leaf_block -= ext_block_count;
281 if (ext == HFSPLUS_EXTENT_COUNT)
283 dbg("found block in extent %i", ext);
285 leaf_off = (ext_block_start + leaf_block) * blocksize;
287 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
291 descr = (struct hfsplus_bnode_descriptor *) buf;
292 dbg("descriptor type 0x%x", descr->type);
294 record_count = be16_to_cpu(descr->num_recs);
295 dbg("number of records %u", record_count);
296 if (record_count == 0)
299 if (descr->type != HFS_NODE_LEAF)
302 key = (struct hfsplus_catalog_key *)
303 &buf[sizeof(struct hfsplus_bnode_descriptor)];
305 dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
306 if (key->parent_id != cpu_to_be32(HFSPLUS_POR_CNID))
309 label_len = be16_to_cpu(key->unicode_len) * 2;
310 dbg("label unicode16 len %i", label_len);
311 // volume_id_set_label_raw(id, key->unicode, label_len);
312 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
315 // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
316 IF_FEATURE_BLKID_TYPE(id->type = "hfsplus";)