3
0

hfs.c 8.3 KB

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