chiark / gitweb /
volume_id: better DDF raid detection
[elogind.git] / extras / volume_id / lib / hfs.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
33 #include "md5.h"
34
35 struct hfs_finder_info{
36         uint32_t        boot_folder;
37         uint32_t        start_app;
38         uint32_t        open_folder;
39         uint32_t        os9_folder;
40         uint32_t        reserved;
41         uint32_t        osx_folder;
42         uint8_t         id[8];
43 } PACKED;
44
45 static struct hfs_mdb {
46         uint8_t         signature[2];
47         uint32_t        cr_date;
48         uint32_t        ls_Mod;
49         uint16_t        atrb;
50         uint16_t        nm_fls;
51         uint16_t        vbm_st;
52         uint16_t        alloc_ptr;
53         uint16_t        nm_al_blks;
54         uint32_t        al_blk_size;
55         uint32_t        clp_size;
56         uint16_t        al_bl_st;
57         uint32_t        nxt_cnid;
58         uint16_t        free_bks;
59         uint8_t         label_len;
60         uint8_t         label[27];
61         uint32_t        vol_bkup;
62         uint16_t        vol_seq_num;
63         uint32_t        wr_cnt;
64         uint32_t        xt_clump_size;
65         uint32_t        ct_clump_size;
66         uint16_t        num_root_dirs;
67         uint32_t        file_count;
68         uint32_t        dir_count;
69         struct hfs_finder_info finder_info;
70         uint8_t         embed_sig[2];
71         uint16_t        embed_startblock;
72         uint16_t        embed_blockcount;
73 } PACKED *hfs;
74
75 struct hfsplus_bnode_descriptor {
76         uint32_t        next;
77         uint32_t        prev;
78         uint8_t         type;
79         uint8_t         height;
80         uint16_t        num_recs;
81         uint16_t        reserved;
82 } PACKED;
83
84 struct hfsplus_bheader_record {
85         uint16_t        depth;
86         uint32_t        root;
87         uint32_t        leaf_count;
88         uint32_t        leaf_head;
89         uint32_t        leaf_tail;
90         uint16_t        node_size;
91 } PACKED;
92
93 struct hfsplus_catalog_key {
94         uint16_t        key_len;
95         uint32_t        parent_id;
96         uint16_t        unicode_len;
97         uint8_t         unicode[255 * 2];
98 } PACKED;
99
100 struct hfsplus_extent {
101         uint32_t        start_block;
102         uint32_t        block_count;
103 } PACKED;
104
105 #define HFSPLUS_EXTENT_COUNT            8
106 struct hfsplus_fork {
107         uint64_t        total_size;
108         uint32_t        clump_size;
109         uint32_t        total_blocks;
110         struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
111 } PACKED;
112
113 static struct hfsplus_vol_header {
114         uint8_t         signature[2];
115         uint16_t        version;
116         uint32_t        attributes;
117         uint32_t        last_mount_vers;
118         uint32_t        reserved;
119         uint32_t        create_date;
120         uint32_t        modify_date;
121         uint32_t        backup_date;
122         uint32_t        checked_date;
123         uint32_t        file_count;
124         uint32_t        folder_count;
125         uint32_t        blocksize;
126         uint32_t        total_blocks;
127         uint32_t        free_blocks;
128         uint32_t        next_alloc;
129         uint32_t        rsrc_clump_sz;
130         uint32_t        data_clump_sz;
131         uint32_t        next_cnid;
132         uint32_t        write_count;
133         uint64_t        encodings_bmp;
134         struct hfs_finder_info finder_info;
135         struct hfsplus_fork alloc_file;
136         struct hfsplus_fork ext_file;
137         struct hfsplus_fork cat_file;
138         struct hfsplus_fork attr_file;
139         struct hfsplus_fork start_file;
140 } PACKED *hfsplus;
141
142 #define HFS_SUPERBLOCK_OFFSET           0x400
143 #define HFS_NODE_LEAF                   0xff
144 #define HFSPLUS_POR_CNID                1
145
146 static void hfsid_set_uuid(struct volume_id *id, const uint8_t *hfs_id)
147 {
148         struct md5_ctx md5c;
149         static const uint8_t hash_init[16] = {
150                 0xb3, 0xe2, 0x0f, 0x39, 0xf2, 0x92, 0x11, 0xd6,
151                 0x97, 0xa4, 0x00, 0x30, 0x65, 0x43, 0xec, 0xac
152         };
153         uint8_t uuid[16];
154
155         if (*((uint64_t *)hfs_id) == 0)
156                 return;
157
158         md5_init(&md5c);
159         md5_update(&md5c, hash_init, 16);
160         md5_update(&md5c, hfs_id, 8);
161         md5_final(&md5c, uuid);
162
163         uuid[6] = 0x30 | (uuid[6] & 0x0f);
164         uuid[8] = 0x80 | (uuid[8] & 0x3f);
165         volume_id_set_uuid(id, uuid, 0, UUID_DCE);
166 }
167
168 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off, uint64_t size)
169 {
170         unsigned int blocksize;
171         unsigned int cat_block;
172         unsigned int ext_block_start;
173         unsigned int ext_block_count;
174         int ext;
175         unsigned int leaf_node_head;
176         unsigned int leaf_node_count;
177         unsigned int leaf_node_size;
178         unsigned int leaf_block;
179         uint64_t leaf_off;
180         unsigned int alloc_block_size;
181         unsigned int alloc_first_block;
182         unsigned int embed_first_block;
183         unsigned int record_count;
184         struct hfsplus_bnode_descriptor *descr;
185         struct hfsplus_bheader_record *bnode;
186         struct hfsplus_catalog_key *key;
187         unsigned int label_len;
188         struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
189         const uint8_t *buf;
190
191         info("probing at offset 0x%llx\n", (unsigned long long) off);
192
193         buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
194         if (buf == NULL)
195                 return -1;
196
197         hfs = (struct hfs_mdb *) buf;
198         if (memcmp(hfs->signature, "BD", 2) != 0)
199                 goto checkplus;
200
201         /* it may be just a hfs wrapper for hfs+ */
202         if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
203                 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
204                 dbg("alloc_block_size 0x%x\n", alloc_block_size);
205
206                 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
207                 dbg("alloc_first_block 0x%x\n", alloc_first_block);
208
209                 embed_first_block = be16_to_cpu(hfs->embed_startblock);
210                 dbg("embed_first_block 0x%x\n", embed_first_block);
211
212                 off += (alloc_first_block * 512) +
213                        (embed_first_block * alloc_block_size);
214                 dbg("hfs wrapped hfs+ found at offset 0x%llx\n", (unsigned long long) off);
215
216                 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
217                 if (buf == NULL)
218                         return -1;
219                 goto checkplus;
220         }
221
222         if (hfs->label_len > 0 && hfs->label_len < 28) {
223                 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
224                 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
225         }
226
227         hfsid_set_uuid(id, hfs->finder_info.id);
228
229         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
230         id->type = "hfs";
231
232         return 0;
233
234 checkplus:
235         hfsplus = (struct hfsplus_vol_header *) buf;
236         if (memcmp(hfsplus->signature, "H+", 2) == 0)
237                 goto hfsplus;
238         if (memcmp(hfsplus->signature, "HX", 2) == 0)
239                 goto hfsplus;
240         return -1;
241
242 hfsplus:
243         hfsid_set_uuid(id, hfsplus->finder_info.id);
244
245         blocksize = be32_to_cpu(hfsplus->blocksize);
246         dbg("blocksize %u\n", blocksize);
247
248         memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
249         cat_block = be32_to_cpu(extents[0].start_block);
250         dbg("catalog start block 0x%x\n", cat_block);
251
252         buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
253         if (buf == NULL)
254                 goto found;
255
256         bnode = (struct hfsplus_bheader_record *)
257                 &buf[sizeof(struct hfsplus_bnode_descriptor)];
258
259         leaf_node_head = be32_to_cpu(bnode->leaf_head);
260         dbg("catalog leaf node 0x%x\n", leaf_node_head);
261
262         leaf_node_size = be16_to_cpu(bnode->node_size);
263         dbg("leaf node size 0x%x\n", leaf_node_size);
264
265         leaf_node_count = be32_to_cpu(bnode->leaf_count);
266         dbg("leaf node count 0x%x\n", leaf_node_count);
267         if (leaf_node_count == 0)
268                 goto found;
269
270         leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
271
272         /* get physical location */
273         for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
274                 ext_block_start = be32_to_cpu(extents[ext].start_block);
275                 ext_block_count = be32_to_cpu(extents[ext].block_count);
276                 dbg("extent start block 0x%x, count 0x%x\n", ext_block_start, ext_block_count);
277
278                 if (ext_block_count == 0)
279                         goto found;
280
281                 /* this is our extent */
282                 if (leaf_block < ext_block_count)
283                         break;
284
285                 leaf_block -= ext_block_count;
286         }
287         if (ext == HFSPLUS_EXTENT_COUNT)
288                 goto found;
289         dbg("found block in extent %i\n", ext);
290
291         leaf_off = (ext_block_start + leaf_block) * blocksize;
292
293         buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
294         if (buf == NULL)
295                 goto found;
296
297         descr = (struct hfsplus_bnode_descriptor *) buf;
298         dbg("descriptor type 0x%x\n", descr->type);
299
300         record_count = be16_to_cpu(descr->num_recs);
301         dbg("number of records %u\n", record_count);
302         if (record_count == 0)
303                 goto found;
304
305         if (descr->type != HFS_NODE_LEAF)
306                 goto found;
307
308         key = (struct hfsplus_catalog_key *)
309                 &buf[sizeof(struct hfsplus_bnode_descriptor)];
310
311         dbg("parent id 0x%x\n", be32_to_cpu(key->parent_id));
312         if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
313                 goto found;
314
315         label_len = be16_to_cpu(key->unicode_len) * 2;
316         dbg("label unicode16 len %i\n", label_len);
317         volume_id_set_label_raw(id, key->unicode, label_len);
318         volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
319
320 found:
321         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
322         id->type = "hfsplus";
323
324         return 0;
325 }