Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * volume_id - reads filesystem label and uuid |
| 3 | * |
| 4 | * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | |
| 21 | #include "volume_id_internal.h" |
| 22 | |
| 23 | struct hfs_finder_info{ |
| 24 | uint32_t boot_folder; |
| 25 | uint32_t start_app; |
| 26 | uint32_t open_folder; |
| 27 | uint32_t os9_folder; |
| 28 | uint32_t reserved; |
| 29 | uint32_t osx_folder; |
| 30 | uint8_t id[8]; |
| 31 | } __attribute__((__packed__)); |
| 32 | |
| 33 | struct hfs_mdb { |
| 34 | uint8_t signature[2]; |
| 35 | uint32_t cr_date; |
| 36 | uint32_t ls_Mod; |
| 37 | uint16_t atrb; |
| 38 | uint16_t nm_fls; |
| 39 | uint16_t vbm_st; |
| 40 | uint16_t alloc_ptr; |
| 41 | uint16_t nm_al_blks; |
| 42 | uint32_t al_blk_size; |
| 43 | uint32_t clp_size; |
| 44 | uint16_t al_bl_st; |
| 45 | uint32_t nxt_cnid; |
| 46 | uint16_t free_bks; |
| 47 | uint8_t label_len; |
| 48 | uint8_t label[27]; |
| 49 | uint32_t vol_bkup; |
| 50 | uint16_t vol_seq_num; |
| 51 | uint32_t wr_cnt; |
| 52 | uint32_t xt_clump_size; |
| 53 | uint32_t ct_clump_size; |
| 54 | uint16_t num_root_dirs; |
| 55 | uint32_t file_count; |
| 56 | uint32_t dir_count; |
| 57 | struct hfs_finder_info finder_info; |
| 58 | uint8_t embed_sig[2]; |
| 59 | uint16_t embed_startblock; |
| 60 | uint16_t embed_blockcount; |
| 61 | } __attribute__((__packed__)); |
| 62 | |
| 63 | struct hfsplus_bnode_descriptor { |
| 64 | uint32_t next; |
| 65 | uint32_t prev; |
| 66 | uint8_t type; |
| 67 | uint8_t height; |
| 68 | uint16_t num_recs; |
| 69 | uint16_t reserved; |
| 70 | } __attribute__((__packed__)); |
| 71 | |
| 72 | struct hfsplus_bheader_record { |
| 73 | uint16_t depth; |
| 74 | uint32_t root; |
| 75 | uint32_t leaf_count; |
| 76 | uint32_t leaf_head; |
| 77 | uint32_t leaf_tail; |
| 78 | uint16_t node_size; |
| 79 | } __attribute__((__packed__)); |
| 80 | |
| 81 | struct hfsplus_catalog_key { |
| 82 | uint16_t key_len; |
| 83 | uint32_t parent_id; |
| 84 | uint16_t unicode_len; |
| 85 | uint8_t unicode[255 * 2]; |
| 86 | } __attribute__((__packed__)); |
| 87 | |
| 88 | struct hfsplus_extent { |
| 89 | uint32_t start_block; |
| 90 | uint32_t block_count; |
| 91 | } __attribute__((__packed__)); |
| 92 | |
| 93 | #define HFSPLUS_EXTENT_COUNT 8 |
| 94 | struct hfsplus_fork { |
| 95 | uint64_t total_size; |
| 96 | uint32_t clump_size; |
| 97 | uint32_t total_blocks; |
| 98 | struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT]; |
| 99 | } __attribute__((__packed__)); |
| 100 | |
| 101 | struct hfsplus_vol_header { |
| 102 | uint8_t signature[2]; |
| 103 | uint16_t version; |
| 104 | uint32_t attributes; |
| 105 | uint32_t last_mount_vers; |
| 106 | uint32_t reserved; |
| 107 | uint32_t create_date; |
| 108 | uint32_t modify_date; |
| 109 | uint32_t backup_date; |
| 110 | uint32_t checked_date; |
| 111 | uint32_t file_count; |
| 112 | uint32_t folder_count; |
| 113 | uint32_t blocksize; |
| 114 | uint32_t total_blocks; |
| 115 | uint32_t free_blocks; |
| 116 | uint32_t next_alloc; |
| 117 | uint32_t rsrc_clump_sz; |
| 118 | uint32_t data_clump_sz; |
| 119 | uint32_t next_cnid; |
| 120 | uint32_t write_count; |
| 121 | uint64_t encodings_bmp; |
| 122 | struct hfs_finder_info finder_info; |
| 123 | struct hfsplus_fork alloc_file; |
| 124 | struct hfsplus_fork ext_file; |
| 125 | struct hfsplus_fork cat_file; |
| 126 | struct hfsplus_fork attr_file; |
| 127 | struct hfsplus_fork start_file; |
| 128 | } __attribute__((__packed__)); |
| 129 | |
| 130 | #define HFS_SUPERBLOCK_OFFSET 0x400 |
| 131 | #define HFS_NODE_LEAF 0xff |
| 132 | #define HFSPLUS_POR_CNID 1 |
| 133 | |
| 134 | int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off) |
| 135 | { |
| 136 | unsigned blocksize; |
| 137 | unsigned cat_block; |
| 138 | unsigned ext_block_start; |
| 139 | unsigned ext_block_count; |
| 140 | int ext; |
| 141 | unsigned leaf_node_head; |
| 142 | unsigned leaf_node_count; |
| 143 | unsigned leaf_node_size; |
| 144 | unsigned leaf_block; |
| 145 | uint64_t leaf_off; |
| 146 | unsigned alloc_block_size; |
| 147 | unsigned alloc_first_block; |
| 148 | unsigned embed_first_block; |
| 149 | unsigned record_count; |
| 150 | struct hfsplus_vol_header *hfsplus; |
| 151 | struct hfsplus_bnode_descriptor *descr; |
| 152 | struct hfsplus_bheader_record *bnode; |
| 153 | struct hfsplus_catalog_key *key; |
| 154 | unsigned label_len; |
| 155 | struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT]; |
| 156 | struct hfs_mdb *hfs; |
| 157 | const uint8_t *buf; |
| 158 | |
| 159 | dbg("probing at offset 0x%llx", (unsigned long long) off); |
| 160 | |
| 161 | buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200); |
| 162 | if (buf == NULL) |
| 163 | return -1; |
| 164 | |
| 165 | hfs = (struct hfs_mdb *) buf; |
| 166 | if (hfs->signature[0] != 'B' || hfs->signature[1] != 'D') |
| 167 | goto checkplus; |
| 168 | |
| 169 | /* it may be just a hfs wrapper for hfs+ */ |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame^] | 170 | if (hfs->embed_sig[0] == 'H' && hfs->embed_sig[1] == '+') { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 171 | alloc_block_size = be32_to_cpu(hfs->al_blk_size); |
| 172 | dbg("alloc_block_size 0x%x", alloc_block_size); |
| 173 | |
| 174 | alloc_first_block = be16_to_cpu(hfs->al_bl_st); |
| 175 | dbg("alloc_first_block 0x%x", alloc_first_block); |
| 176 | |
| 177 | embed_first_block = be16_to_cpu(hfs->embed_startblock); |
| 178 | dbg("embed_first_block 0x%x", embed_first_block); |
| 179 | |
| 180 | off += (alloc_first_block * 512) + |
| 181 | (embed_first_block * alloc_block_size); |
| 182 | dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off); |
| 183 | |
| 184 | buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200); |
| 185 | if (buf == NULL) |
| 186 | return -1; |
| 187 | goto checkplus; |
| 188 | } |
| 189 | |
| 190 | if (hfs->label_len > 0 && hfs->label_len < 28) { |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame^] | 191 | // volume_id_set_label_raw(id, hfs->label, hfs->label_len); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 192 | volume_id_set_label_string(id, hfs->label, hfs->label_len) ; |
| 193 | } |
| 194 | |
| 195 | volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS); |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame^] | 196 | // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM); |
| 197 | // id->type = "hfs"; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 198 | |
| 199 | return 0; |
| 200 | |
| 201 | checkplus: |
| 202 | hfsplus = (struct hfsplus_vol_header *) buf; |
| 203 | if (hfs->signature[0] == 'H') |
| 204 | if (hfs->signature[1] == '+' || hfs->signature[1] == 'X') |
| 205 | goto hfsplus; |
| 206 | return -1; |
| 207 | |
| 208 | hfsplus: |
| 209 | volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS); |
| 210 | |
| 211 | blocksize = be32_to_cpu(hfsplus->blocksize); |
| 212 | dbg("blocksize %u", blocksize); |
| 213 | |
| 214 | memcpy(extents, hfsplus->cat_file.extents, sizeof(extents)); |
| 215 | cat_block = be32_to_cpu(extents[0].start_block); |
| 216 | dbg("catalog start block 0x%x", cat_block); |
| 217 | |
| 218 | buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000); |
| 219 | if (buf == NULL) |
| 220 | goto found; |
| 221 | |
| 222 | bnode = (struct hfsplus_bheader_record *) |
| 223 | &buf[sizeof(struct hfsplus_bnode_descriptor)]; |
| 224 | |
| 225 | leaf_node_head = be32_to_cpu(bnode->leaf_head); |
| 226 | dbg("catalog leaf node 0x%x", leaf_node_head); |
| 227 | |
| 228 | leaf_node_size = be16_to_cpu(bnode->node_size); |
| 229 | dbg("leaf node size 0x%x", leaf_node_size); |
| 230 | |
| 231 | leaf_node_count = be32_to_cpu(bnode->leaf_count); |
| 232 | dbg("leaf node count 0x%x", leaf_node_count); |
| 233 | if (leaf_node_count == 0) |
| 234 | goto found; |
| 235 | |
| 236 | leaf_block = (leaf_node_head * leaf_node_size) / blocksize; |
| 237 | |
| 238 | /* get physical location */ |
| 239 | for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) { |
| 240 | ext_block_start = be32_to_cpu(extents[ext].start_block); |
| 241 | ext_block_count = be32_to_cpu(extents[ext].block_count); |
| 242 | dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count); |
| 243 | |
| 244 | if (ext_block_count == 0) |
| 245 | goto found; |
| 246 | |
| 247 | /* this is our extent */ |
| 248 | if (leaf_block < ext_block_count) |
| 249 | break; |
| 250 | |
| 251 | leaf_block -= ext_block_count; |
| 252 | } |
| 253 | if (ext == HFSPLUS_EXTENT_COUNT) |
| 254 | goto found; |
| 255 | dbg("found block in extent %i", ext); |
| 256 | |
| 257 | leaf_off = (ext_block_start + leaf_block) * blocksize; |
| 258 | |
| 259 | buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size); |
| 260 | if (buf == NULL) |
| 261 | goto found; |
| 262 | |
| 263 | descr = (struct hfsplus_bnode_descriptor *) buf; |
| 264 | dbg("descriptor type 0x%x", descr->type); |
| 265 | |
| 266 | record_count = be16_to_cpu(descr->num_recs); |
| 267 | dbg("number of records %u", record_count); |
| 268 | if (record_count == 0) |
| 269 | goto found; |
| 270 | |
| 271 | if (descr->type != HFS_NODE_LEAF) |
| 272 | goto found; |
| 273 | |
| 274 | key = (struct hfsplus_catalog_key *) |
| 275 | &buf[sizeof(struct hfsplus_bnode_descriptor)]; |
| 276 | |
| 277 | dbg("parent id 0x%x", be32_to_cpu(key->parent_id)); |
| 278 | if (key->parent_id != cpu_to_be32(HFSPLUS_POR_CNID)) |
| 279 | goto found; |
| 280 | |
| 281 | label_len = be16_to_cpu(key->unicode_len) * 2; |
| 282 | dbg("label unicode16 len %i", label_len); |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame^] | 283 | // volume_id_set_label_raw(id, key->unicode, label_len); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 284 | volume_id_set_label_unicode16(id, key->unicode, BE, label_len); |
| 285 | |
| 286 | found: |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame^] | 287 | // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM); |
| 288 | // id->type = "hfsplus"; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |