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
21 #include "volume_id_internal.h"
23 struct hfs_finder_info
{
31 } __attribute__((__packed__
));
52 uint32_t xt_clump_size
;
53 uint32_t ct_clump_size
;
54 uint16_t num_root_dirs
;
57 struct hfs_finder_info finder_info
;
59 uint16_t embed_startblock
;
60 uint16_t embed_blockcount
;
61 } __attribute__((__packed__
));
63 struct hfsplus_bnode_descriptor
{
70 } __attribute__((__packed__
));
72 struct hfsplus_bheader_record
{
79 } __attribute__((__packed__
));
81 struct hfsplus_catalog_key
{
85 uint8_t unicode
[255 * 2];
86 } __attribute__((__packed__
));
88 struct hfsplus_extent
{
91 } __attribute__((__packed__
));
93 #define HFSPLUS_EXTENT_COUNT 8
97 uint32_t total_blocks
;
98 struct hfsplus_extent extents
[HFSPLUS_EXTENT_COUNT
];
99 } __attribute__((__packed__
));
101 struct hfsplus_vol_header
{
102 uint8_t signature
[2];
105 uint32_t last_mount_vers
;
107 uint32_t create_date
;
108 uint32_t modify_date
;
109 uint32_t backup_date
;
110 uint32_t checked_date
;
112 uint32_t folder_count
;
114 uint32_t total_blocks
;
115 uint32_t free_blocks
;
117 uint32_t rsrc_clump_sz
;
118 uint32_t data_clump_sz
;
120 uint32_t write_count
;
121 uint64_t encodings_bmp
;
122 struct hfs_finder_info finder_info
;
123 struct hfsplus_fork alloc_file
;
124 struct hfsplus_fork ext_file
;
125 struct hfsplus_fork cat_file
;
126 struct hfsplus_fork attr_file
;
127 struct hfsplus_fork start_file
;
128 } __attribute__((__packed__
));
130 #define HFS_SUPERBLOCK_OFFSET 0x400
131 #define HFS_NODE_LEAF 0xff
132 #define HFSPLUS_POR_CNID 1
134 int volume_id_probe_hfs_hfsplus(struct volume_id
*id
/*,uint64_t off*/)
139 unsigned ext_block_start
;
140 unsigned ext_block_count
;
142 unsigned leaf_node_head
;
143 unsigned leaf_node_count
;
144 unsigned leaf_node_size
;
147 unsigned alloc_block_size
;
148 unsigned alloc_first_block
;
149 unsigned embed_first_block
;
150 unsigned record_count
;
151 struct hfsplus_vol_header
*hfsplus
;
152 struct hfsplus_bnode_descriptor
*descr
;
153 struct hfsplus_bheader_record
*bnode
;
154 struct hfsplus_catalog_key
*key
;
156 struct hfsplus_extent extents
[HFSPLUS_EXTENT_COUNT
];
160 dbg("probing at offset 0x%llx", (unsigned long long) off
);
162 buf
= volume_id_get_buffer(id
, off
+ HFS_SUPERBLOCK_OFFSET
, 0x200);
166 hfs
= (struct hfs_mdb
*) buf
;
167 if (hfs
->signature
[0] != 'B' || hfs
->signature
[1] != 'D')
170 /* it may be just a hfs wrapper for hfs+ */
171 if (hfs
->embed_sig
[0] == 'H' && hfs
->embed_sig
[1] == '+') {
172 alloc_block_size
= be32_to_cpu(hfs
->al_blk_size
);
173 dbg("alloc_block_size 0x%x", alloc_block_size
);
175 alloc_first_block
= be16_to_cpu(hfs
->al_bl_st
);
176 dbg("alloc_first_block 0x%x", alloc_first_block
);
178 embed_first_block
= be16_to_cpu(hfs
->embed_startblock
);
179 dbg("embed_first_block 0x%x", embed_first_block
);
181 off
+= (alloc_first_block
* 512) +
182 (embed_first_block
* alloc_block_size
);
183 dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off
);
185 buf
= volume_id_get_buffer(id
, off
+ HFS_SUPERBLOCK_OFFSET
, 0x200);
191 if (hfs
->label_len
> 0 && hfs
->label_len
< 28) {
192 // volume_id_set_label_raw(id, hfs->label, hfs->label_len);
193 volume_id_set_label_string(id
, hfs
->label
, hfs
->label_len
) ;
196 volume_id_set_uuid(id
, hfs
->finder_info
.id
, UUID_HFS
);
197 // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
203 hfsplus
= (struct hfsplus_vol_header
*) buf
;
204 if (hfs
->signature
[0] == 'H')
205 if (hfs
->signature
[1] == '+' || hfs
->signature
[1] == 'X')
210 volume_id_set_uuid(id
, hfsplus
->finder_info
.id
, UUID_HFS
);
212 blocksize
= be32_to_cpu(hfsplus
->blocksize
);
213 dbg("blocksize %u", blocksize
);
215 memcpy(extents
, hfsplus
->cat_file
.extents
, sizeof(extents
));
216 cat_block
= be32_to_cpu(extents
[0].start_block
);
217 dbg("catalog start block 0x%x", cat_block
);
219 buf
= volume_id_get_buffer(id
, off
+ (cat_block
* blocksize
), 0x2000);
223 bnode
= (struct hfsplus_bheader_record
*)
224 &buf
[sizeof(struct hfsplus_bnode_descriptor
)];
226 leaf_node_head
= be32_to_cpu(bnode
->leaf_head
);
227 dbg("catalog leaf node 0x%x", leaf_node_head
);
229 leaf_node_size
= be16_to_cpu(bnode
->node_size
);
230 dbg("leaf node size 0x%x", leaf_node_size
);
232 leaf_node_count
= be32_to_cpu(bnode
->leaf_count
);
233 dbg("leaf node count 0x%x", leaf_node_count
);
234 if (leaf_node_count
== 0)
237 leaf_block
= (leaf_node_head
* leaf_node_size
) / blocksize
;
239 /* get physical location */
240 for (ext
= 0; ext
< HFSPLUS_EXTENT_COUNT
; ext
++) {
241 ext_block_start
= be32_to_cpu(extents
[ext
].start_block
);
242 ext_block_count
= be32_to_cpu(extents
[ext
].block_count
);
243 dbg("extent start block 0x%x, count 0x%x", ext_block_start
, ext_block_count
);
245 if (ext_block_count
== 0)
248 /* this is our extent */
249 if (leaf_block
< ext_block_count
)
252 leaf_block
-= ext_block_count
;
254 if (ext
== HFSPLUS_EXTENT_COUNT
)
256 dbg("found block in extent %i", ext
);
258 leaf_off
= (ext_block_start
+ leaf_block
) * blocksize
;
260 buf
= volume_id_get_buffer(id
, off
+ leaf_off
, leaf_node_size
);
264 descr
= (struct hfsplus_bnode_descriptor
*) buf
;
265 dbg("descriptor type 0x%x", descr
->type
);
267 record_count
= be16_to_cpu(descr
->num_recs
);
268 dbg("number of records %u", record_count
);
269 if (record_count
== 0)
272 if (descr
->type
!= HFS_NODE_LEAF
)
275 key
= (struct hfsplus_catalog_key
*)
276 &buf
[sizeof(struct hfsplus_bnode_descriptor
)];
278 dbg("parent id 0x%x", be32_to_cpu(key
->parent_id
));
279 if (key
->parent_id
!= cpu_to_be32(HFSPLUS_POR_CNID
))
282 label_len
= be16_to_cpu(key
->unicode_len
) * 2;
283 dbg("label unicode16 len %i", label_len
);
284 // volume_id_set_label_raw(id, key->unicode, label_len);
285 volume_id_set_label_unicode16(id
, key
->unicode
, BE
, label_len
);
288 // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
289 // id->type = "hfsplus";