hfs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. } __attribute__((__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. } __attribute__((__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. } __attribute__((__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. } __attribute__((__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. } __attribute__((__packed__));
  81. struct hfsplus_extent {
  82. uint32_t start_block;
  83. uint32_t block_count;
  84. } __attribute__((__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. } __attribute__((__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. } __attribute__((__packed__));
  120. #define HFS_SUPERBLOCK_OFFSET 0x400
  121. #define HFS_NODE_LEAF 0xff
  122. #define HFSPLUS_POR_CNID 1
  123. int volume_id_probe_hfs_hfsplus(struct volume_id *id, uint64_t off)
  124. {
  125. unsigned blocksize;
  126. unsigned cat_block;
  127. unsigned ext_block_start;
  128. unsigned ext_block_count;
  129. int ext;
  130. unsigned leaf_node_head;
  131. unsigned leaf_node_count;
  132. unsigned leaf_node_size;
  133. unsigned leaf_block;
  134. uint64_t leaf_off;
  135. unsigned alloc_block_size;
  136. unsigned alloc_first_block;
  137. unsigned embed_first_block;
  138. unsigned record_count;
  139. struct hfsplus_vol_header *hfsplus;
  140. struct hfsplus_bnode_descriptor *descr;
  141. struct hfsplus_bheader_record *bnode;
  142. struct hfsplus_catalog_key *key;
  143. unsigned label_len;
  144. struct hfsplus_extent extents[HFSPLUS_EXTENT_COUNT];
  145. struct hfs_mdb *hfs;
  146. const uint8_t *buf;
  147. dbg("probing at offset 0x%llx", (unsigned long long) off);
  148. buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
  149. if (buf == NULL)
  150. return -1;
  151. hfs = (struct hfs_mdb *) buf;
  152. if (hfs->signature[0] != 'B' || hfs->signature[1] != 'D')
  153. goto checkplus;
  154. /* it may be just a hfs wrapper for hfs+ */
  155. if (hfs->embed_sig[0] == 'H' && hfs->embed_sig[1] == '+') {
  156. alloc_block_size = be32_to_cpu(hfs->al_blk_size);
  157. dbg("alloc_block_size 0x%x", alloc_block_size);
  158. alloc_first_block = be16_to_cpu(hfs->al_bl_st);
  159. dbg("alloc_first_block 0x%x", alloc_first_block);
  160. embed_first_block = be16_to_cpu(hfs->embed_startblock);
  161. dbg("embed_first_block 0x%x", embed_first_block);
  162. off += (alloc_first_block * 512) +
  163. (embed_first_block * alloc_block_size);
  164. dbg("hfs wrapped hfs+ found at offset 0x%llx", (unsigned long long) off);
  165. buf = volume_id_get_buffer(id, off + HFS_SUPERBLOCK_OFFSET, 0x200);
  166. if (buf == NULL)
  167. return -1;
  168. goto checkplus;
  169. }
  170. if (hfs->label_len > 0 && hfs->label_len < 28) {
  171. // volume_id_set_label_raw(id, hfs->label, hfs->label_len);
  172. volume_id_set_label_string(id, hfs->label, hfs->label_len) ;
  173. }
  174. volume_id_set_uuid(id, hfs->finder_info.id, UUID_HFS);
  175. // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
  176. // id->type = "hfs";
  177. return 0;
  178. checkplus:
  179. hfsplus = (struct hfsplus_vol_header *) buf;
  180. if (hfs->signature[0] == 'H')
  181. if (hfs->signature[1] == '+' || hfs->signature[1] == 'X')
  182. goto hfsplus;
  183. return -1;
  184. hfsplus:
  185. volume_id_set_uuid(id, hfsplus->finder_info.id, UUID_HFS);
  186. blocksize = be32_to_cpu(hfsplus->blocksize);
  187. dbg("blocksize %u", blocksize);
  188. memcpy(extents, hfsplus->cat_file.extents, sizeof(extents));
  189. cat_block = be32_to_cpu(extents[0].start_block);
  190. dbg("catalog start block 0x%x", cat_block);
  191. buf = volume_id_get_buffer(id, off + (cat_block * blocksize), 0x2000);
  192. if (buf == NULL)
  193. goto found;
  194. bnode = (struct hfsplus_bheader_record *)
  195. &buf[sizeof(struct hfsplus_bnode_descriptor)];
  196. leaf_node_head = be32_to_cpu(bnode->leaf_head);
  197. dbg("catalog leaf node 0x%x", leaf_node_head);
  198. leaf_node_size = be16_to_cpu(bnode->node_size);
  199. dbg("leaf node size 0x%x", leaf_node_size);
  200. leaf_node_count = be32_to_cpu(bnode->leaf_count);
  201. dbg("leaf node count 0x%x", leaf_node_count);
  202. if (leaf_node_count == 0)
  203. goto found;
  204. leaf_block = (leaf_node_head * leaf_node_size) / blocksize;
  205. /* get physical location */
  206. for (ext = 0; ext < HFSPLUS_EXTENT_COUNT; ext++) {
  207. ext_block_start = be32_to_cpu(extents[ext].start_block);
  208. ext_block_count = be32_to_cpu(extents[ext].block_count);
  209. dbg("extent start block 0x%x, count 0x%x", ext_block_start, ext_block_count);
  210. if (ext_block_count == 0)
  211. goto found;
  212. /* this is our extent */
  213. if (leaf_block < ext_block_count)
  214. break;
  215. leaf_block -= ext_block_count;
  216. }
  217. if (ext == HFSPLUS_EXTENT_COUNT)
  218. goto found;
  219. dbg("found block in extent %i", ext);
  220. leaf_off = (ext_block_start + leaf_block) * blocksize;
  221. buf = volume_id_get_buffer(id, off + leaf_off, leaf_node_size);
  222. if (buf == NULL)
  223. goto found;
  224. descr = (struct hfsplus_bnode_descriptor *) buf;
  225. dbg("descriptor type 0x%x", descr->type);
  226. record_count = be16_to_cpu(descr->num_recs);
  227. dbg("number of records %u", record_count);
  228. if (record_count == 0)
  229. goto found;
  230. if (descr->type != HFS_NODE_LEAF)
  231. goto found;
  232. key = (struct hfsplus_catalog_key *)
  233. &buf[sizeof(struct hfsplus_bnode_descriptor)];
  234. dbg("parent id 0x%x", be32_to_cpu(key->parent_id));
  235. if (key->parent_id != cpu_to_be32(HFSPLUS_POR_CNID))
  236. goto found;
  237. label_len = be16_to_cpu(key->unicode_len) * 2;
  238. dbg("label unicode16 len %i", label_len);
  239. // volume_id_set_label_raw(id, key->unicode, label_len);
  240. volume_id_set_label_unicode16(id, key->unicode, BE, label_len);
  241. found:
  242. // volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
  243. // id->type = "hfsplus";
  244. return 0;
  245. }