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
36 #include "volume_id.h"
41 struct hfs_finder_info{
49 } __attribute__((__packed__));
70 uint32_t xt_clump_size;
71 uint32_t ct_clump_size;
72 uint16_t num_root_dirs;
75 struct hfs_finder_info finder_info;
77 uint16_t embed_startblock;
78 uint16_t embed_blockcount;
79 } __attribute__((__packed__)) *hfs;
81 struct hfsplus_bnode_descriptor {
88 } __attribute__((__packed__));
90 struct hfsplus_bheader_record {
97 } __attribute__((__packed__));
99 struct hfsplus_catalog_key {
102 uint16_t unicode_len;
103 uint8_t unicode[255 * 2];
104 } __attribute__((__packed__));
106 struct hfsplus_extent {
107 uint32_t start_block;
108 uint32_t block_count;
109 } __attribute__((__packed__));
111 #define HFSPLUS_EXTENT_COUNT 8
112 struct hfsplus_fork {
115 uint32_t total_blocks;
116 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
117 } __attribute__((__packed__));
119 struct hfsplus_vol_header {
120 uint8_t signature[2];
123 uint32_t last_mount_vers;
125 uint32_t create_date;
126 uint32_t modify_date;
127 uint32_t backup_date;
128 uint32_t checked_date;
130 uint32_t folder_count;
132 uint32_t total_blocks;
133 uint32_t free_blocks;
135 uint32_t rsrc_clump_sz;
136 uint32_t data_clump_sz;
138 uint32_t write_count;
139 uint64_t encodings_bmp;
140 struct hfs_finder_info finder_info;
141 struct hfsplus_fork alloc_file;
142 struct hfsplus_fork ext_file;
143 struct hfsplus_fork cat_file;
144 struct hfsplus_fork attr_file;
145 struct hfsplus_fork start_file;
146 } __attribute__((__packed__)) *hfsplus;
148 #define HFS_SUPERBLOCK_OFFSET 0x400
149 #define HFS_NODE_LEAF 0xff
150 #define HFSPLUS_POR_CNID 1
152 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
154 unsigned int blocksize;
155 unsigned int cat_block;
156 unsigned int ext_block_start;
157 unsigned int ext_block_count;
159 unsigned int leaf_node_head;
160 unsigned int leaf_node_count;
161 unsigned int leaf_node_size;
162 unsigned int leaf_block;
164 unsigned int alloc_block_size;
165 unsigned int alloc_first_block;
166 unsigned int embed_first_block;
167 unsigned int record_count;
168 struct hfsplus_bnode_descriptor *descr;
169 struct hfsplus_bheader_record *bnode;
170 struct hfsplus_catalog_key *key;
171 unsigned int label_len;
172 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
175 dbg("probing at offset 0x%llx", (unsigned long long) off);
177 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
181 hfs = (struct hfs_mdb *) buf;
182 if (memcmp(hfs->signature, "BD", 2) != 0)
185 /* it may be just a hfs wrapper for hfs+ */
186 if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
187 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
188 dbg("alloc_block_size 0x%x", alloc_block_size);
190 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
191 dbg("alloc_first_block 0x%x", alloc_first_block);
193 embed_first_block = be16_to_cpu(hfs->embed_startblock);
194 dbg("embed_first_block 0x%x", embed_first_block);
196 off += (alloc_first_block * 512) +
197 (embed_first_block * alloc_block_size);
198 dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
200 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
206 if (hfs->label_len > 0 && hfs->label_len < 28) {
207 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
208 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
211 volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS);
213 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
219 hfsplus = (struct hfsplus_vol_header *) buf;
220 if (memcmp(hfsplus->signature, "H+", 2) == 0)
222 if (memcmp(hfsplus->signature, "HX", 2) == 0)
227 volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
229 blocksize = be32_to_cpu(hfsplus->blocksize);
230 dbg("blocksize %u", blocksize);
232 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
233 cat_block = be32_to_cpu(extents[0].start_block);
234 dbg("catalog start block 0x%x", cat_block);
236 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
240 bnode = (struct hfsplus_bheader_record *)
241 &buf[sizeof(struct hfsplus_bnode_descriptor)];
243 leaf_node_head = be32_to_cpu(bnode->leaf_head);
244 dbg("catalog leaf node 0x%x", leaf_node_head);
246 leaf_node_size = be16_to_cpu(bnode->node_size);
247 dbg("leaf node size 0x%x", leaf_node_size);
249 leaf_node_count = be32_to_cpu(bnode->leaf_count);
250 dbg("leaf node count 0x%x", leaf_node_count);
251 if (leaf_node_count == 0)
254 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
256 /* get physical location */
257 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
258 ext_block_start = be32_to_cpu(extents[ext].start_block);
259 ext_block_count = be32_to_cpu(extents[ext].block_count);
260 dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
262 if (ext_block_count == 0)
265 /* this is our extent */
266 if (leaf_block < ext_block_count)
269 leaf_block -= ext_block_count;
271 if (ext == HFSPLUS_EXTENT_COUNT)
273 dbg("found block in extent %i", ext);
275 leaf_off = (ext_block_start + leaf_block) * blocksize;
277 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
281 descr = (struct hfsplus_bnode_descriptor *) buf;
282 dbg("descriptor type 0x%x", descr->type);
284 record_count = be16_to_cpu(descr->num_recs);
285 dbg("number of records %u", record_count);
286 if (record_count == 0)
289 if (descr->type != HFS_NODE_LEAF)
292 key = (struct hfsplus_catalog_key *)
293 &buf[sizeof(struct hfsplus_bnode_descriptor)];
295 dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
296 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
299 label_len = be16_to_cpu(key->unicode_len) * 2;
300 dbg("label unicode16 len %i", label_len);
301 volume_id_set_label_raw(id, key->unicode, label_len);
302 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
305 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
306 id->type = "hfsplus";