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 "volume_id.h"
31 struct hfs_finder_info{
39 } __attribute__((__packed__));
60 uint32_t xt_clump_size;
61 uint32_t ct_clump_size;
62 uint16_t num_root_dirs;
65 struct hfs_finder_info finder_info;
67 uint16_t embed_startblock;
68 uint16_t embed_blockcount;
69 } __attribute__((__packed__)) *hfs;
71 struct hfsplus_bnode_descriptor {
78 } __attribute__((__packed__));
80 struct hfsplus_bheader_record {
87 } __attribute__((__packed__));
89 struct hfsplus_catalog_key {
93 uint8_t unicode[255 * 2];
94 } __attribute__((__packed__));
96 struct hfsplus_extent {
99 } __attribute__((__packed__));
101 #define HFSPLUS_EXTENT_COUNT 8
102 struct hfsplus_fork {
105 uint32_t total_blocks;
106 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
107 } __attribute__((__packed__));
109 struct hfsplus_vol_header {
110 uint8_t signature[2];
113 uint32_t last_mount_vers;
115 uint32_t create_date;
116 uint32_t modify_date;
117 uint32_t backup_date;
118 uint32_t checked_date;
120 uint32_t folder_count;
122 uint32_t total_blocks;
123 uint32_t free_blocks;
125 uint32_t rsrc_clump_sz;
126 uint32_t data_clump_sz;
128 uint32_t write_count;
129 uint64_t encodings_bmp;
130 struct hfs_finder_info finder_info;
131 struct hfsplus_fork alloc_file;
132 struct hfsplus_fork ext_file;
133 struct hfsplus_fork cat_file;
134 struct hfsplus_fork attr_file;
135 struct hfsplus_fork start_file;
136 } __attribute__((__packed__)) *hfsplus;
138 #define HFS_SUPERBLOCK_OFFSET 0x400
139 #define HFS_NODE_LEAF 0xff
140 #define HFSPLUS_POR_CNID 1
142 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
144 unsigned int blocksize;
145 unsigned int cat_block;
146 unsigned int ext_block_start;
147 unsigned int ext_block_count;
149 unsigned int leaf_node_head;
150 unsigned int leaf_node_count;
151 unsigned int leaf_node_size;
152 unsigned int leaf_block;
154 unsigned int alloc_block_size;
155 unsigned int alloc_first_block;
156 unsigned int embed_first_block;
157 unsigned int record_count;
158 struct hfsplus_bnode_descriptor *descr;
159 struct hfsplus_bheader_record *bnode;
160 struct hfsplus_catalog_key *key;
161 unsigned int label_len;
162 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
165 dbg("probing at offset 0x%llx", (unsigned long long) off);
167 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
171 hfs = (struct hfs_mdb *) buf;
172 if (memcmp(hfs->signature, "BD", 2) != 0)
175 /* it may be just a hfs wrapper for hfs+ */
176 if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
177 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
178 dbg("alloc_block_size 0x%x", alloc_block_size);
180 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
181 dbg("alloc_first_block 0x%x", alloc_first_block);
183 embed_first_block = be16_to_cpu(hfs->embed_startblock);
184 dbg("embed_first_block 0x%x", embed_first_block);
186 off += (alloc_first_block * 512) +
187 (embed_first_block * alloc_block_size);
188 dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
190 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
196 if (hfs->label_len > 0 && hfs->label_len < 28) {
197 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
198 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
201 volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS);
203 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
209 hfsplus = (struct hfsplus_vol_header *) buf;
210 if (memcmp(hfsplus->signature, "H+", 2) == 0)
212 if (memcmp(hfsplus->signature, "HX", 2) == 0)
217 volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
219 blocksize = be32_to_cpu(hfsplus->blocksize);
220 dbg("blocksize %u", blocksize);
222 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
223 cat_block = be32_to_cpu(extents[0].start_block);
224 dbg("catalog start block 0x%x", cat_block);
226 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
230 bnode = (struct hfsplus_bheader_record *)
231 &buf[sizeof(struct hfsplus_bnode_descriptor)];
233 leaf_node_head = be32_to_cpu(bnode->leaf_head);
234 dbg("catalog leaf node 0x%x", leaf_node_head);
236 leaf_node_size = be16_to_cpu(bnode->node_size);
237 dbg("leaf node size 0x%x", leaf_node_size);
239 leaf_node_count = be32_to_cpu(bnode->leaf_count);
240 dbg("leaf node count 0x%x", leaf_node_count);
241 if (leaf_node_count == 0)
244 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
246 /* get physical location */
247 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
248 ext_block_start = be32_to_cpu(extents[ext].start_block);
249 ext_block_count = be32_to_cpu(extents[ext].block_count);
250 dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
252 if (ext_block_count == 0)
255 /* this is our extent */
256 if (leaf_block < ext_block_count)
259 leaf_block -= ext_block_count;
261 if (ext == HFSPLUS_EXTENT_COUNT)
263 dbg("found block in extent %i", ext);
265 leaf_off = (ext_block_start + leaf_block) * blocksize;
267 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
271 descr = (struct hfsplus_bnode_descriptor *) buf;
272 dbg("descriptor type 0x%x", descr->type);
274 record_count = be16_to_cpu(descr->num_recs);
275 dbg("number of records %u", record_count);
276 if (record_count == 0)
279 if (descr->type != HFS_NODE_LEAF)
282 key = (struct hfsplus_catalog_key *)
283 &buf[sizeof(struct hfsplus_bnode_descriptor)];
285 dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
286 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
289 label_len = be16_to_cpu(key->unicode_len) * 2;
290 dbg("label unicode16 len %i", label_len);
291 volume_id_set_label_raw(id, key->unicode, label_len);
292 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
295 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
296 id->type = "hfsplus";