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
35 #include <asm/types.h>
37 #include "../volume_id.h"
38 #include "../logging.h"
42 #define HFS_SUPERBLOCK_OFFSET 0x400
43 #define HFS_NODE_LEAF 0xff
44 #define HFSPLUS_POR_CNID 1
45 #define HFSPLUS_EXTENT_COUNT 8
47 int volume_id_probe_hfs_hfsplus(struct volume_id *id, __u64 off)
49 struct hfs_finder_info{
57 } __attribute__((__packed__));
83 struct hfs_finder_info finder_info;
85 __u16 embed_startblock;
86 __u16 embed_blockcount;
87 } __attribute__((__packed__)) *hfs;
89 struct hfsplus_bnode_descriptor {
96 } __attribute__((__packed__));
98 struct hfsplus_bheader_record {
105 } __attribute__((__packed__));
107 struct hfsplus_catalog_key {
111 __u8 unicode[255 * 2];
112 } __attribute__((__packed__));
114 struct hfsplus_extent {
117 } __attribute__((__packed__));
119 struct hfsplus_fork {
123 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
124 } __attribute__((__packed__));
126 struct hfsplus_vol_header {
130 __u32 last_mount_vers;
147 struct hfs_finder_info finder_info;
148 struct hfsplus_fork alloc_file;
149 struct hfsplus_fork ext_file;
150 struct hfsplus_fork cat_file;
151 struct hfsplus_fork attr_file;
152 struct hfsplus_fork start_file;
153 } __attribute__((__packed__)) *hfsplus;
155 unsigned int blocksize;
156 unsigned int cat_block;
157 unsigned int ext_block_start;
158 unsigned int ext_block_count;
160 unsigned int leaf_node_head;
161 unsigned int leaf_node_count;
162 unsigned int leaf_node_size;
163 unsigned int leaf_block;
165 unsigned int alloc_block_size;
166 unsigned int alloc_first_block;
167 unsigned int embed_first_block;
168 unsigned int record_count;
169 struct hfsplus_bnode_descriptor *descr;
170 struct hfsplus_bheader_record *bnode;
171 struct hfsplus_catalog_key *key;
172 unsigned int label_len;
173 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
176 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
180 hfs = (struct hfs_mdb *) buf;
181 if (memcmp(hfs->signature, "BD", 2) != 0)
184 /* it may be just a hfs wrapper for hfs+ */
185 if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
186 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
187 dbg("alloc_block_size 0x%x", alloc_block_size);
189 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
190 dbg("alloc_first_block 0x%x", alloc_first_block);
192 embed_first_block = be16_to_cpu(hfs->embed_startblock);
193 dbg("embed_first_block 0x%x", embed_first_block);
195 off += (alloc_first_block * 512) +
196 (embed_first_block * alloc_block_size);
197 dbg("hfs wrapped hfs+ found at offset 0x%llx", off);
199 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
205 if (hfs->label_len > 0 && hfs->label_len < 28) {
206 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
207 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
210 volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS);
212 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
218 hfsplus = (struct hfsplus_vol_header *) buf;
219 if (memcmp(hfsplus->signature, "H+", 2) == 0)
221 if (memcmp(hfsplus->signature, "HX", 2) == 0)
226 volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
228 blocksize = be32_to_cpu(hfsplus->blocksize);
229 dbg("blocksize %u", blocksize);
231 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
232 cat_block = be32_to_cpu(extents[0].start_block);
233 dbg("catalog start block 0x%x", cat_block);
235 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
239 bnode = (struct hfsplus_bheader_record *)
240 &buf[sizeof(struct hfsplus_bnode_descriptor)];
242 leaf_node_head = be32_to_cpu(bnode->leaf_head);
243 dbg("catalog leaf node 0x%x", leaf_node_head);
245 leaf_node_size = be16_to_cpu(bnode->node_size);
246 dbg("leaf node size 0x%x", leaf_node_size);
248 leaf_node_count = be32_to_cpu(bnode->leaf_count);
249 dbg("leaf node count 0x%x", leaf_node_count);
250 if (leaf_node_count == 0)
253 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
255 /* get physical location */
256 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
257 ext_block_start = be32_to_cpu(extents[ext].start_block);
258 ext_block_count = be32_to_cpu(extents[ext].block_count);
259 dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
261 if (ext_block_count == 0)
264 /* this is our extent */
265 if (leaf_block < ext_block_count)
268 leaf_block -= ext_block_count;
270 if (ext == HFSPLUS_EXTENT_COUNT)
272 dbg("found block in extent %i", ext);
274 leaf_off = (ext_block_start + leaf_block) * blocksize;
276 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
280 descr = (struct hfsplus_bnode_descriptor *) buf;
281 dbg("descriptor type 0x%x", descr->type);
283 record_count = be16_to_cpu(descr->num_recs);
284 dbg("number of records %u", record_count);
285 if (record_count == 0)
288 if (descr->type != HFS_NODE_LEAF)
291 key = (struct hfsplus_catalog_key *)
292 &buf[sizeof(struct hfsplus_bnode_descriptor)];
294 dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
295 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
298 label_len = be16_to_cpu(key->unicode_len) * 2;
299 dbg("label unicode16 len %i", label_len);
300 volume_id_set_label_raw(id, key->unicode, label_len);
301 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
304 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
305 id->type = "hfsplus";