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