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"
30 struct hfs_finder_info{
38 } __attribute__((__packed__));
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;
68 } __attribute__((__packed__)) *hfs;
70 struct hfsplus_bnode_descriptor {
77 } __attribute__((__packed__));
79 struct hfsplus_bheader_record {
86 } __attribute__((__packed__));
88 struct hfsplus_catalog_key {
92 uint8_t unicode[255 * 2];
93 } __attribute__((__packed__));
95 struct hfsplus_extent {
98 } __attribute__((__packed__));
100 #define HFSPLUS_EXTENT_COUNT 8
101 struct hfsplus_fork {
104 uint32_t total_blocks;
105 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
106 } __attribute__((__packed__));
108 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;
135 } __attribute__((__packed__)) *hfsplus;
137 #define HFS_SUPERBLOCK_OFFSET 0x400
138 #define HFS_NODE_LEAF 0xff
139 #define HFSPLUS_POR_CNID 1
141 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
143 unsigned int blocksize;
144 unsigned int cat_block;
145 unsigned int ext_block_start;
146 unsigned int ext_block_count;
148 unsigned int leaf_node_head;
149 unsigned int leaf_node_count;
150 unsigned int leaf_node_size;
151 unsigned int leaf_block;
153 unsigned int alloc_block_size;
154 unsigned int alloc_first_block;
155 unsigned int embed_first_block;
156 unsigned int record_count;
157 struct hfsplus_bnode_descriptor *descr;
158 struct hfsplus_bheader_record *bnode;
159 struct hfsplus_catalog_key *key;
160 unsigned int label_len;
161 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
164 dbg("probing at offset 0x%llx", (unsigned long long) off);
166 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
170 hfs = (struct hfs_mdb *) buf;
171 if (memcmp(hfs->signature, "BD", 2) != 0)
174 /* it may be just a hfs wrapper for hfs+ */
175 if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
176 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
177 dbg("alloc_block_size 0x%x", alloc_block_size);
179 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
180 dbg("alloc_first_block 0x%x", alloc_first_block);
182 embed_first_block = be16_to_cpu(hfs->embed_startblock);
183 dbg("embed_first_block 0x%x", embed_first_block);
185 off += (alloc_first_block * 512) +
186 (embed_first_block * alloc_block_size);
187 dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
189 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
195 if (hfs->label_len > 0 && hfs->label_len < 28) {
196 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
197 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
200 volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS);
202 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
208 hfsplus = (struct hfsplus_vol_header *) buf;
209 if (memcmp(hfsplus->signature, "H+", 2) == 0)
211 if (memcmp(hfsplus->signature, "HX", 2) == 0)
216 volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
218 blocksize = be32_to_cpu(hfsplus->blocksize);
219 dbg("blocksize %u", blocksize);
221 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
222 cat_block = be32_to_cpu(extents[0].start_block);
223 dbg("catalog start block 0x%x", cat_block);
225 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
229 bnode = (struct hfsplus_bheader_record *)
230 &buf[sizeof(struct hfsplus_bnode_descriptor)];
232 leaf_node_head = be32_to_cpu(bnode->leaf_head);
233 dbg("catalog leaf node 0x%x", leaf_node_head);
235 leaf_node_size = be16_to_cpu(bnode->node_size);
236 dbg("leaf node size 0x%x", leaf_node_size);
238 leaf_node_count = be32_to_cpu(bnode->leaf_count);
239 dbg("leaf node count 0x%x", leaf_node_count);
240 if (leaf_node_count == 0)
243 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
245 /* get physical location */
246 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
247 ext_block_start = be32_to_cpu(extents[ext].start_block);
248 ext_block_count = be32_to_cpu(extents[ext].block_count);
249 dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
251 if (ext_block_count == 0)
254 /* this is our extent */
255 if (leaf_block < ext_block_count)
258 leaf_block -= ext_block_count;
260 if (ext == HFSPLUS_EXTENT_COUNT)
262 dbg("found block in extent %i", ext);
264 leaf_off = (ext_block_start + leaf_block) * blocksize;
266 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
270 descr = (struct hfsplus_bnode_descriptor *) buf;
271 dbg("descriptor type 0x%x", descr->type);
273 record_count = be16_to_cpu(descr->num_recs);
274 dbg("number of records %u", record_count);
275 if (record_count == 0)
278 if (descr->type != HFS_NODE_LEAF)
281 key = (struct hfsplus_catalog_key *)
282 &buf[sizeof(struct hfsplus_bnode_descriptor)];
284 dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
285 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
288 label_len = be16_to_cpu(key->unicode_len) * 2;
289 dbg("label unicode16 len %i", label_len);
290 volume_id_set_label_raw(id, key->unicode, label_len);
291 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
294 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
295 id->type = "hfsplus";