chiark / gitweb /
udevadm: add --version --help options to man page, hide them as commands
[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 #include "md5.h"
30
31 struct hfs_finder_info{
32         uint32_t        boot_folder;
33         uint32_t        start_app;
34         uint32_t        open_folder;
35         uint32_t        os9_folder;
36         uint32_t        reserved;
37         uint32_t        osx_folder;
38         uint8_t         id[8];
39 } PACKED;
40
41 static struct hfs_mdb {
42         uint8_t         signature[2];
43         uint32_t        cr_date;
44         uint32_t        ls_Mod;
45         uint16_t        atrb;
46         uint16_t        nm_fls;
47         uint16_t        vbm_st;
48         uint16_t        alloc_ptr;
49         uint16_t        nm_al_blks;
50         uint32_t        al_blk_size;
51         uint32_t        clp_size;
52         uint16_t        al_bl_st;
53         uint32_t        nxt_cnid;
54         uint16_t        free_bks;
55         uint8_t         label_len;
56         uint8_t         label[27];
57         uint32_t        vol_bkup;
58         uint16_t        vol_seq_num;
59         uint32_t        wr_cnt;
60         uint32_t        xt_clump_size;
61         uint32_t        ct_clump_size;
62         uint16_t        num_root_dirs;
63         uint32_t        file_count;
64         uint32_t        dir_count;
65         struct hfs_finder_info finder_info;
66         uint8_t         embed_sig[2];
67         uint16_t        embed_startblock;
68         uint16_t        embed_blockcount;
69 } PACKED *hfs;
70
71 struct hfsplus_bnode_descriptor {
72         uint32_t        next;
73         uint32_t        prev;
74         uint8_t         type;
75         uint8_t         height;
76         uint16_t        num_recs;
77         uint16_t        reserved;
78 } PACKED;
79
80 struct hfsplus_bheader_record {
81         uint16_t        depth;
82         uint32_t        root;
83         uint32_t        leaf_count;
84         uint32_t        leaf_head;
85         uint32_t        leaf_tail;
86         uint16_t        node_size;
87 } PACKED;
88
89 struct hfsplus_catalog_key {
90         uint16_t        key_len;
91         uint32_t        parent_id;
92         uint16_t        unicode_len;
93         uint8_t         unicode[255 * 2];
94 } PACKED;
95
96 struct hfsplus_extent {
97         uint32_t        start_block;
98         uint32_t        block_count;
99 } PACKED;
100
101 #define HFSPLUS_EXTENT_COUNT            8
102 struct hfsplus_fork {
103         uint64_t        total_size;
104         uint32_t        clump_size;
105         uint32_t        total_blocks;
106         struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
107 } PACKED;
108
109 static struct hfsplus_vol_header {
110         uint8_t         signature[2];
111         uint16_t        version;
112         uint32_t        attributes;
113         uint32_t        last_mount_vers;
114         uint32_t        reserved;
115         uint32_t        create_date;
116         uint32_t        modify_date;
117         uint32_t        backup_date;
118         uint32_t        checked_date;
119         uint32_t        file_count;
120         uint32_t        folder_count;
121         uint32_t        blocksize;
122         uint32_t        total_blocks;
123         uint32_t        free_blocks;
124         uint32_t        next_alloc;
125         uint32_t        rsrc_clump_sz;
126         uint32_t        data_clump_sz;
127         uint32_t        next_cnid;
128         uint32_t        write_count;
129         uint64_t        encodings_bmp;
130         struct hfs_finder_info finder_info;
131         struct hfsplus_fork alloc_file;
132         struct hfsplus_fork ext_file;
133         struct hfsplus_fork cat_file;
134         struct hfsplus_fork attr_file;
135         struct hfsplus_fork start_file;
136 } PACKED *hfsplus;
137
138 #define HFS_SUPERBLOCK_OFFSET           0x400
139 #define HFS_NODE_LEAF                   0xff
140 #define HFSPLUS_POR_CNID                1
141
142 static void hfsid_set_uuid(struct volume_id *id, const uint8_t *hfs_id)
143 {
144         struct 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(&md5c, uuid);
158
159         uuid[6] = 0x30 | (uuid[6] & 0x0f);
160         uuid[8] = 0x80 | (uuid[8] & 0x3f);
161         volume_id_set_uuid(id, uuid, 0, UUID_DCE);
162 }
163
164 int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off, uint64_t size)
165 {
166         unsigned int blocksize;
167         unsigned int cat_block;
168         unsigned int ext_block_start;
169         unsigned int ext_block_count;
170         int ext;
171         unsigned int leaf_node_head;
172         unsigned int leaf_node_count;
173         unsigned int leaf_node_size;
174         unsigned int leaf_block;
175         uint64_t leaf_off;
176         unsigned int alloc_block_size;
177         unsigned int alloc_first_block;
178         unsigned int embed_first_block;
179         unsigned int record_count;
180         struct hfsplus_bnode_descriptor *descr;
181         struct hfsplus_bheader_record *bnode;
182         struct hfsplus_catalog_key *key;
183         unsigned int label_len;
184         struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
185         const uint8_t *buf;
186
187         info("probing at offset 0x%llx\n", (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
193         hfs = (struct hfs_mdb *) buf;
194         if (memcmp(hfs->signature, "BD", 2) != 0)
195                 goto checkplus;
196
197         /* it may be just a hfs wrapper for hfs+ */
198         if (memcmp(hfs->embed_sig, "H+", 2) == 0) {
199                 alloc_block_size = be32_to_cpu(hfs->al_blk_size);
200                 dbg("alloc_block_size 0x%x\n", alloc_block_size);
201
202                 alloc_first_block = be16_to_cpu(hfs->al_bl_st);
203                 dbg("alloc_first_block 0x%x\n", alloc_first_block);
204
205                 embed_first_block = be16_to_cpu(hfs->embed_startblock);
206                 dbg("embed_first_block 0x%x\n", embed_first_block);
207
208                 off += (alloc_first_block * 512) +
209                        (embed_first_block * alloc_block_size);
210                 dbg("hfs wrapped hfs+ found at offset 0x%llx\n", (unsigned long long) off);
211
212                 buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
213                 if (buf == NULL)
214                         return -1;
215                 goto checkplus;
216         }
217
218         if (hfs->label_len > 0 && hfs->label_len < 28) {
219                 volume_id_set_label_raw(id, hfs->label, hfs->label_len);
220                 volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
221         }
222
223         hfsid_set_uuid(id, hfs->finder_info.id);
224
225         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
226         id->type = "hfs";
227
228         return 0;
229
230 checkplus:
231         hfsplus = (struct hfsplus_vol_header *) buf;
232         if (memcmp(hfsplus->signature, "H+", 2) == 0)
233                 goto hfsplus;
234         if (memcmp(hfsplus->signature, "HX", 2) == 0)
235                 goto hfsplus;
236         return -1;
237
238 hfsplus:
239         hfsid_set_uuid(id, hfsplus->finder_info.id);
240
241         blocksize = be32_to_cpu(hfsplus->blocksize);
242         dbg("blocksize %u\n", blocksize);
243
244         memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
245         cat_block = be32_to_cpu(extents[0].start_block);
246         dbg("catalog start block 0x%x\n", cat_block);
247
248         buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
249         if (buf == NULL)
250                 goto found;
251
252         bnode = (struct hfsplus_bheader_record *)
253                 &buf[sizeof(struct hfsplus_bnode_descriptor)];
254
255         leaf_node_head = be32_to_cpu(bnode->leaf_head);
256         dbg("catalog leaf node 0x%x\n", leaf_node_head);
257
258         leaf_node_size = be16_to_cpu(bnode->node_size);
259         dbg("leaf node size 0x%x\n", leaf_node_size);
260
261         leaf_node_count = be32_to_cpu(bnode->leaf_count);
262         dbg("leaf node count 0x%x\n", leaf_node_count);
263         if (leaf_node_count == 0)
264                 goto found;
265
266         leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
267
268         /* get physical location */
269         for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
270                 ext_block_start = be32_to_cpu(extents[ext].start_block);
271                 ext_block_count = be32_to_cpu(extents[ext].block_count);
272                 dbg("extent start block 0x%x, count 0x%x\n", ext_block_start, ext_block_count);
273
274                 if (ext_block_count == 0)
275                         goto found;
276
277                 /* this is our extent */
278                 if (leaf_block < ext_block_count)
279                         break;
280
281                 leaf_block -= ext_block_count;
282         }
283         if (ext == HFSPLUS_EXTENT_COUNT)
284                 goto found;
285         dbg("found block in extent %i\n", ext);
286
287         leaf_off = (ext_block_start + leaf_block) * blocksize;
288
289         buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
290         if (buf == NULL)
291                 goto found;
292
293         descr = (struct hfsplus_bnode_descriptor *) buf;
294         dbg("descriptor type 0x%x\n", descr->type);
295
296         record_count = be16_to_cpu(descr->num_recs);
297         dbg("number of records %u\n", record_count);
298         if (record_count == 0)
299                 goto found;
300
301         if (descr->type != HFS_NODE_LEAF)
302                 goto found;
303
304         key = (struct hfsplus_catalog_key *)
305                 &buf[sizeof(struct hfsplus_bnode_descriptor)];
306
307         dbg("parent id 0x%x\n", be32_to_cpu(key->parent_id));
308         if (be32_to_cpu(key->parent_id) != HFSPLUS_POR_CNID)
309                 goto found;
310
311         label_len = be16_to_cpu(key->unicode_len) * 2;
312         dbg("label unicode16 len %i\n", label_len);
313         volume_id_set_label_raw(id, key->unicode, label_len);
314         volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
315
316 found:
317         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
318         id->type = "hfsplus";
319
320         return 0;
321 }