chiark / gitweb /
volume_id: provide library
[elogind.git] / extras / volume_id / libvolume_id / 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 "logging.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 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 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 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
142 {
143         unsigned int blocksize;
144         unsigned int cat_block;
145         unsigned int ext_block_start;
146         unsigned int ext_block_count;
147         int ext;
148         unsigned int leaf_node_head;
149         unsigned int leaf_node_count;
150         unsigned int leaf_node_size;
151         unsigned int leaf_block;
152         uint64_t leaf_off;
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];
162         const uint8_t *buf;
163
164         dbg("probing at offset 0x%llx", (unsigned long long) off);
165
166         buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
167         if (buf == NULL)
168                 return -1;
169
170         hfs = (struct hfs_mdb *) buf;
171         if (memcmp(hfs->signature, "BD", 2) != 0)
172                 goto checkplus;
173
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);
178
179                 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
180                 dbg("alloc_first_block 0x%x", alloc_first_block);
181
182                 embed_first_block = be16_to_cpu(hfs->embed_startblock);
183                 dbg("embed_first_block 0x%x", embed_first_block);
184
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);
188
189                 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
190                 if (buf == NULL)
191                         return -1;
192                 goto checkplus;
193         }
194
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) ;
198         }
199
200         volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS);
201
202         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
203         id->type = "hfs";
204
205         return 0;
206
207 checkplus:
208         hfsplus = (struct hfsplus_vol_header *) buf;
209         if (memcmp(hfsplus->signature, "H+", 2) == 0)
210                 goto hfsplus;
211         if (memcmp(hfsplus->signature, "HX", 2) == 0)
212                 goto hfsplus;
213         return -1;
214
215 hfsplus:
216         volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
217
218         blocksize = be32_to_cpu(hfsplus->blocksize);
219         dbg("blocksize %u", blocksize);
220
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);
224
225         buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
226         if (buf == NULL)
227                 goto found;
228
229         bnode = (struct hfsplus_bheader_record *)
230                 &buf[sizeof(struct hfsplus_bnode_descriptor)];
231
232         leaf_node_head = be32_to_cpu(bnode->leaf_head);
233         dbg("catalog leaf node 0x%x", leaf_node_head);
234
235         leaf_node_size = be16_to_cpu(bnode->node_size);
236         dbg("leaf node size 0x%x", leaf_node_size);
237
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)
241                 goto found;
242
243         leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
244
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);
250
251                 if (ext_block_count == 0)
252                         goto found;
253
254                 /* this is our extent */
255                 if (leaf_block < ext_block_count)
256                         break;
257
258                 leaf_block -= ext_block_count;
259         }
260         if (ext == HFSPLUS_EXTENT_COUNT)
261                 goto found;
262         dbg("found block in extent %i", ext);
263
264         leaf_off = (ext_block_start + leaf_block) * blocksize;
265
266         buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
267         if (buf == NULL)
268                 goto found;
269
270         descr = (struct hfsplus_bnode_descriptor *) buf;
271         dbg("descriptor type 0x%x", descr->type);
272
273         record_count = be16_to_cpu(descr->num_recs);
274         dbg("number of records %u", record_count);
275         if (record_count == 0)
276                 goto found;
277
278         if (descr->type != HFS_NODE_LEAF)
279                 goto found;
280
281         key = (struct hfsplus_catalog_key *)
282                 &buf[sizeof(struct hfsplus_bnode_descriptor)];
283
284         dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
285         if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
286                 goto found;
287
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);
292
293 found:
294         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
295         id->type = "hfsplus";
296
297         return 0;
298 }