3
0

hfs.c 7.6 KB

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