hfs.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. //config:config FEATURE_VOLUMEID_HFS
  21. //config: bool "hfs filesystem"
  22. //config: default y
  23. //config: depends on VOLUMEID
  24. //kbuild:lib-$(CONFIG_FEATURE_VOLUMEID_HFS) += hfs.o
  25. #include "volume_id_internal.h"
  26. struct hfs_finder_info{
  27. uint32_t boot_folder;
  28. uint32_t start_app;
  29. uint32_t open_folder;
  30. uint32_t os9_folder;
  31. uint32_t reserved;
  32. uint32_t osx_folder;
  33. uint8_t id[8];
  34. } PACKED;
  35. struct hfs_mdb {
  36. uint8_t signature[2];
  37. uint32_t cr_date;
  38. uint32_t ls_Mod;
  39. uint16_t atrb;
  40. uint16_t nm_fls;
  41. uint16_t vbm_st;
  42. uint16_t alloc_ptr;
  43. uint16_t nm_al_blks;
  44. uint32_t al_blk_size;
  45. uint32_t clp_size;
  46. uint16_t al_bl_st;
  47. uint32_t nxt_cnid;
  48. uint16_t free_bks;
  49. uint8_t label_len;
  50. uint8_t label[27];
  51. uint32_t vol_bkup;
  52. uint16_t vol_seq_num;
  53. uint32_t wr_cnt;
  54. uint32_t xt_clump_size;
  55. uint32_t ct_clump_size;
  56. uint16_t num_root_dirs;
  57. uint32_t file_count;
  58. uint32_t dir_count;
  59. struct hfs_finder_info finder_info;
  60. uint8_t embed_sig[2];
  61. uint16_t embed_startblock;
  62. uint16_t embed_blockcount;
  63. } PACKED;
  64. struct hfsplus_bnode_descriptor {
  65. uint32_t next;
  66. uint32_t prev;
  67. uint8_t type;
  68. uint8_t height;
  69. uint16_t num_recs;
  70. uint16_t reserved;
  71. } PACKED;
  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. } PACKED;
  80. struct hfsplus_catalog_key {
  81. uint16_t key_len;
  82. uint32_t parent_id;
  83. uint16_t unicode_len;
  84. uint8_t unicode[255 * 2];
  85. } PACKED;
  86. struct hfsplus_extent {
  87. uint32_t start_block;
  88. uint32_t block_count;
  89. } PACKED;
  90. #define HFSPLUS_EXTENT_COUNT 8
  91. struct hfsplus_fork {
  92. uint64_t total_size;
  93. uint32_t clump_size;
  94. uint32_t total_blocks;
  95. struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
  96. } PACKED;
  97. struct hfsplus_vol_header {
  98. uint8_t signature[2];
  99. uint16_t version;
  100. uint32_t attributes;
  101. uint32_t last_mount_vers;
  102. uint32_t reserved;
  103. uint32_t create_date;
  104. uint32_t modify_date;
  105. uint32_t backup_date;
  106. uint32_t checked_date;
  107. uint32_t file_count;
  108. uint32_t folder_count;
  109. uint32_t blocksize;
  110. uint32_t total_blocks;
  111. uint32_t free_blocks;
  112. uint32_t next_alloc;
  113. uint32_t rsrc_clump_sz;
  114. uint32_t data_clump_sz;
  115. uint32_t next_cnid;
  116. uint32_t write_count;
  117. uint64_t encodings_bmp;
  118. struct hfs_finder_info finder_info;
  119. struct hfsplus_fork alloc_file;
  120. struct hfsplus_fork ext_file;
  121. struct hfsplus_fork cat_file;
  122. struct hfsplus_fork attr_file;
  123. struct hfsplus_fork start_file;
  124. } PACKED;
  125. #define HFS_SUPERBLOCK_OFFSET 0x400
  126. #define HFS_NODE_LEAF 0xff
  127. #define HFSPLUS_POR_CNID 1
  128. static void FAST_FUNC hfs_set_uuid(struct volume_id *id, const uint8_t *hfs_id)
  129. {
  130. #define hfs_id_len 8
  131. md5_ctx_t md5c;
  132. uint8_t uuid[16];
  133. unsigned i;
  134. for (i = 0; i < hfs_id_len; i++)
  135. if (hfs_id[i] != 0)
  136. goto do_md5;
  137. return;
  138. do_md5:
  139. md5_begin(&md5c);
  140. md5_hash(&md5c, "\263\342\17\71\362\222\21\326\227\244\0\60\145\103\354\254", 16);
  141. md5_hash(&md5c, hfs_id, hfs_id_len);
  142. md5_end(&md5c, uuid);
  143. uuid[6] = 0x30 | (uuid[6] & 0x0f);
  144. uuid[8] = 0x80 | (uuid[8] & 0x3f);
  145. volume_id_set_uuid(id, uuid, UUID_DCE);
  146. }
  147. int FAST_FUNC volume_id_probe_hfs_hfsplus(struct volume_id *id /*,uint64_t off*/)
  148. {
  149. uint64_t off = 0;
  150. unsigned blocksize;
  151. unsigned cat_block;
  152. unsigned ext_block_start;
  153. unsigned ext_block_count;
  154. int ext;
  155. unsigned leaf_node_head;
  156. unsigned leaf_node_count;
  157. unsigned leaf_node_size;
  158. unsigned leaf_block;
  159. uint64_t leaf_off;
  160. unsigned alloc_block_size;
  161. unsigned alloc_first_block;
  162. unsigned embed_first_block;
  163. unsigned record_count;
  164. struct hfsplus_vol_header *hfsplus;
  165. struct hfsplus_bnode_descriptor *descr;
  166. struct hfsplus_bheader_record *bnode;
  167. struct hfsplus_catalog_key *key;
  168. unsigned label_len;
  169. struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
  170. struct hfs_mdb *hfs;
  171. const uint8_t *buf;
  172. dbg("probing at offset 0x%llx", (unsigned long long) off);
  173. buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
  174. if (buf == NULL)
  175. return -1;
  176. hfs = (struct hfs_mdb *) buf;
  177. if (hfs->signature[0] != 'B' || hfs->signature[1] != 'D')
  178. goto checkplus;
  179. /* it may be just a hfs wrapper for hfs+ */
  180. if (hfs->embed_sig[0] == 'H' && hfs->embed_sig[1] == '+') {
  181. alloc_block_size = be32_to_cpu(hfs->al_blk_size);
  182. dbg("alloc_block_size 0x%x", alloc_block_size);
  183. alloc_first_block = be16_to_cpu(hfs->al_bl_st);
  184. dbg("alloc_first_block 0x%x", alloc_first_block);
  185. embed_first_block = be16_to_cpu(hfs->embed_startblock);
  186. dbg("embed_first_block 0x%x", embed_first_block);
  187. off += (alloc_first_block * 512) +
  188. (embed_first_block * alloc_block_size);
  189. dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
  190. buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
  191. if (buf == NULL)
  192. return -1;
  193. goto checkplus;
  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. hfs_set_uuid(id, hfs->finder_info.id);
  200. // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
  201. IF_FEATURE_BLKID_TYPE(id->type = "hfs";)
  202. return 0;
  203. checkplus:
  204. hfsplus = (struct hfsplus_vol_header *) buf;
  205. if (hfs->signature[0] == 'H')
  206. if (hfs->signature[1] == '+' || hfs->signature[1] == 'X')
  207. goto hfsplus;
  208. return -1;
  209. hfsplus:
  210. hfs_set_uuid(id, hfsplus->finder_info.id);
  211. blocksize = be32_to_cpu(hfsplus->blocksize);
  212. dbg("blocksize %u", blocksize);
  213. memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
  214. cat_block = be32_to_cpu(extents[0].start_block);
  215. dbg("catalog start block 0x%x", cat_block);
  216. buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
  217. if (buf == NULL)
  218. goto found;
  219. bnode = (struct hfsplus_bheader_record *)
  220. &buf[sizeof(struct hfsplus_bnode_descriptor)];
  221. leaf_node_head = be32_to_cpu(bnode->leaf_head);
  222. dbg("catalog leaf node 0x%x", leaf_node_head);
  223. leaf_node_size = be16_to_cpu(bnode->node_size);
  224. dbg("leaf node size 0x%x", leaf_node_size);
  225. leaf_node_count = be32_to_cpu(bnode->leaf_count);
  226. dbg("leaf node count 0x%x", leaf_node_count);
  227. if (leaf_node_count == 0)
  228. goto found;
  229. leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
  230. /* get physical location */
  231. for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
  232. ext_block_start = be32_to_cpu(extents[ext].start_block);
  233. ext_block_count = be32_to_cpu(extents[ext].block_count);
  234. dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
  235. if (ext_block_count == 0)
  236. goto found;
  237. /* this is our extent */
  238. if (leaf_block < ext_block_count)
  239. break;
  240. leaf_block -= ext_block_count;
  241. }
  242. if (ext == HFSPLUS_EXTENT_COUNT)
  243. goto found;
  244. dbg("found block in extent %i", ext);
  245. leaf_off = (ext_block_start + leaf_block) * blocksize;
  246. buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
  247. if (buf == NULL)
  248. goto found;
  249. descr = (struct hfsplus_bnode_descriptor *) buf;
  250. dbg("descriptor type 0x%x", descr->type);
  251. record_count = be16_to_cpu(descr->num_recs);
  252. dbg("number of records %u", record_count);
  253. if (record_count == 0)
  254. goto found;
  255. if (descr->type != HFS_NODE_LEAF)
  256. goto found;
  257. key = (struct hfsplus_catalog_key *)
  258. &buf[sizeof(struct hfsplus_bnode_descriptor)];
  259. dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
  260. if (key->parent_id != cpu_to_be32(HFSPLUS_POR_CNID))
  261. goto found;
  262. label_len = be16_to_cpu(key->unicode_len) * 2;
  263. dbg("label unicode16 len %i", label_len);
  264. // volume_id_set_label_raw(id, key->unicode, label_len);
  265. volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
  266. found:
  267. // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
  268. IF_FEATURE_BLKID_TYPE(id->type = "hfsplus";)
  269. return 0;
  270. }