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"
42 struct hfs_finder_info{
50 } __attribute__((__packed__));
76 struct hfs_finder_info finder_info;
78 __u16 embed_startblock;
79 __u16 embed_blockcount;
80 } __attribute__((__packed__)) *hfs;
82 struct hfsplus_bnode_descriptor {
89 } __attribute__((__packed__));
91 struct hfsplus_bheader_record {
98 } __attribute__((__packed__));
100 struct hfsplus_catalog_key {
104 __u8 unicode[255 * 2];
105 } __attribute__((__packed__));
107 struct hfsplus_extent {
110 } __attribute__((__packed__));
112 #define HFSPLUS_EXTENT_COUNT 8
113 struct hfsplus_fork {
117 struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
118 } __attribute__((__packed__));
120 struct hfsplus_vol_header {
124 __u32 last_mount_vers;
141 struct hfs_finder_info finder_info;
142 struct hfsplus_fork alloc_file;
143 struct hfsplus_fork ext_file;
144 struct hfsplus_fork cat_file;
145 struct hfsplus_fork attr_file;
146 struct hfsplus_fork start_file;
147 } __attribute__((__packed__)) *hfsplus;
149 #define HFS_SUPERBLOCK_OFFSET 0x400
150 #define HFS_NODE_LEAF 0xff
151 #define HFSPLUS_POR_CNID 1
153 int volume_id_probe_hfs_hfsplus(struct volume_id *id, __u64 off)
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 dbg("probing at offset 0x%llx", (unsigned long long) off);
178 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
182 hfs = (struct hfs_mdb *) buf;
183 if (memcmp(hfs->signature, "BD", 2) != 0)
186 /* it may be just a hfs wrapper for hfs+ */
187 if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
188 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
189 dbg("alloc_block_size 0x%x", alloc_block_size);
191 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
192 dbg("alloc_first_block 0x%x", alloc_first_block);
194 embed_first_block = be16_to_cpu(hfs->embed_startblock);
195 dbg("embed_first_block 0x%x", embed_first_block);
197 off += (alloc_first_block * 512) +
198 (embed_first_block * alloc_block_size);
199 dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
201 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
207 if (hfs->label_len > 0 && hfs->label_len < 28) {
208 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
209 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
212 volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS);
214 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
220 hfsplus = (struct hfsplus_vol_header *) buf;
221 if (memcmp(hfsplus->signature, "H+", 2) == 0)
223 if (memcmp(hfsplus->signature, "HX", 2) == 0)
228 volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
230 blocksize = be32_to_cpu(hfsplus->blocksize);
231 dbg("blocksize %u", blocksize);
233 memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
234 cat_block = be32_to_cpu(extents[0].start_block);
235 dbg("catalog start block 0x%x", cat_block);
237 buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
241 bnode = (struct hfsplus_bheader_record *)
242 &buf[sizeof(struct hfsplus_bnode_descriptor)];
244 leaf_node_head = be32_to_cpu(bnode->leaf_head);
245 dbg("catalog leaf node 0x%x", leaf_node_head);
247 leaf_node_size = be16_to_cpu(bnode->node_size);
248 dbg("leaf node size 0x%x", leaf_node_size);
250 leaf_node_count = be32_to_cpu(bnode->leaf_count);
251 dbg("leaf node count 0x%x", leaf_node_count);
252 if (leaf_node_count == 0)
255 leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
257 /* get physical location */
258 for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
259 ext_block_start = be32_to_cpu(extents[ext].start_block);
260 ext_block_count = be32_to_cpu(extents[ext].block_count);
261 dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
263 if (ext_block_count == 0)
266 /* this is our extent */
267 if (leaf_block < ext_block_count)
270 leaf_block -= ext_block_count;
272 if (ext == HFSPLUS_EXTENT_COUNT)
274 dbg("found block in extent %i", ext);
276 leaf_off = (ext_block_start + leaf_block) * blocksize;
278 buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
282 descr = (struct hfsplus_bnode_descriptor *) buf;
283 dbg("descriptor type 0x%x", descr->type);
285 record_count = be16_to_cpu(descr->num_recs);
286 dbg("number of records %u", record_count);
287 if (record_count == 0)
290 if (descr->type != HFS_NODE_LEAF)
293 key = (struct hfsplus_catalog_key *)
294 &buf[sizeof(struct hfsplus_bnode_descriptor)];
296 dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
297 if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
300 label_len = be16_to_cpu(key->unicode_len) * 2;
301 dbg("label unicode16 len %i", label_len);
302 volume_id_set_label_raw(id, key->unicode, label_len);
303 volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
306 volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
307 id->type = "hfsplus";