ntfs.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* ntfs.h - header for the NTFS filesystem */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2007,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef GRUB_NTFS_H
  20. #define GRUB_NTFS_H 1
  21. enum
  22. {
  23. GRUB_NTFS_FILE_MFT = 0,
  24. GRUB_NTFS_FILE_MFTMIRR = 1,
  25. GRUB_NTFS_FILE_LOGFILE = 2,
  26. GRUB_NTFS_FILE_VOLUME = 3,
  27. GRUB_NTFS_FILE_ATTRDEF = 4,
  28. GRUB_NTFS_FILE_ROOT = 5,
  29. GRUB_NTFS_FILE_BITMAP = 6,
  30. GRUB_NTFS_FILE_BOOT = 7,
  31. GRUB_NTFS_FILE_BADCLUS = 8,
  32. GRUB_NTFS_FILE_QUOTA = 9,
  33. GRUB_NTFS_FILE_UPCASE = 10,
  34. };
  35. enum
  36. {
  37. GRUB_NTFS_AT_STANDARD_INFORMATION = 0x10,
  38. GRUB_NTFS_AT_ATTRIBUTE_LIST = 0x20,
  39. GRUB_NTFS_AT_FILENAME = 0x30,
  40. GRUB_NTFS_AT_OBJECT_ID = 0x40,
  41. GRUB_NTFS_AT_SECURITY_DESCRIPTOR = 0x50,
  42. GRUB_NTFS_AT_VOLUME_NAME = 0x60,
  43. GRUB_NTFS_AT_VOLUME_INFORMATION = 0x70,
  44. GRUB_NTFS_AT_DATA = 0x80,
  45. GRUB_NTFS_AT_INDEX_ROOT = 0x90,
  46. GRUB_NTFS_AT_INDEX_ALLOCATION = 0xA0,
  47. GRUB_NTFS_AT_BITMAP = 0xB0,
  48. GRUB_NTFS_AT_SYMLINK = 0xC0,
  49. GRUB_NTFS_AT_EA_INFORMATION = 0xD0,
  50. GRUB_NTFS_AT_EA = 0xE0,
  51. };
  52. enum
  53. {
  54. GRUB_NTFS_ATTR_READ_ONLY = 0x1,
  55. GRUB_NTFS_ATTR_HIDDEN = 0x2,
  56. GRUB_NTFS_ATTR_SYSTEM = 0x4,
  57. GRUB_NTFS_ATTR_ARCHIVE = 0x20,
  58. GRUB_NTFS_ATTR_DEVICE = 0x40,
  59. GRUB_NTFS_ATTR_NORMAL = 0x80,
  60. GRUB_NTFS_ATTR_TEMPORARY = 0x100,
  61. GRUB_NTFS_ATTR_SPARSE = 0x200,
  62. GRUB_NTFS_ATTR_REPARSE = 0x400,
  63. GRUB_NTFS_ATTR_COMPRESSED = 0x800,
  64. GRUB_NTFS_ATTR_OFFLINE = 0x1000,
  65. GRUB_NTFS_ATTR_NOT_INDEXED = 0x2000,
  66. GRUB_NTFS_ATTR_ENCRYPTED = 0x4000,
  67. GRUB_NTFS_ATTR_DIRECTORY = 0x10000000,
  68. GRUB_NTFS_ATTR_INDEX_VIEW = 0x20000000
  69. };
  70. enum
  71. {
  72. GRUB_NTFS_FLAG_COMPRESSED = 1,
  73. GRUB_NTFS_FLAG_ENCRYPTED = 0x4000,
  74. GRUB_NTFS_FLAG_SPARSE = 0x8000
  75. };
  76. #define GRUB_NTFS_BLK_SHR GRUB_DISK_SECTOR_BITS
  77. #define GRUB_NTFS_MAX_MFT (4096 >> GRUB_NTFS_BLK_SHR)
  78. #define GRUB_NTFS_MAX_IDX (16384 >> GRUB_NTFS_BLK_SHR)
  79. #define GRUB_NTFS_COM_LEN 4096
  80. #define GRUB_NTFS_COM_LOG_LEN 12
  81. #define GRUB_NTFS_COM_SEC (GRUB_NTFS_COM_LEN >> GRUB_NTFS_BLK_SHR)
  82. #define GRUB_NTFS_LOG_COM_SEC (GRUB_NTFS_COM_LOG_LEN - GRUB_NTFS_BLK_SHR)
  83. #define GRUB_NTFS_ATTRIBUTE_HEADER_SIZE 16
  84. /*
  85. * To make attribute validation clearer the offsets for each value in the
  86. * attribute headers are defined as macros.
  87. *
  88. * These offsets are all from:
  89. * https://flatcap.github.io/linux-ntfs/ntfs/concepts/attribute_header.html
  90. */
  91. /* These offsets are part of the attribute header. */
  92. #define GRUB_NTFS_ATTRIBUTE_LENGTH 4
  93. #define GRUB_NTFS_ATTRIBUTE_RESIDENT 8
  94. #define GRUB_NTFS_ATTRIBUTE_NAME_LENGTH 9
  95. #define GRUB_NTFS_ATTRIBUTE_NAME_OFFSET 10
  96. /* Offsets for values needed for resident data. */
  97. #define GRUB_NTFS_ATTRIBUTE_RES_LENGTH 16
  98. #define GRUB_NTFS_ATTRIBUTE_RES_OFFSET 20
  99. /* Offsets for values needed for non-resident data. */
  100. #define GRUB_NTFS_ATTRIBUTE_DATA_RUNS 32
  101. #define GRUB_NTFS_ATTRIBUTE_COMPRESSION_UNIT_SIZE 34
  102. enum
  103. {
  104. GRUB_NTFS_AF_ALST = 1,
  105. GRUB_NTFS_AF_MMFT = 2,
  106. GRUB_NTFS_AF_GPOS = 4,
  107. };
  108. enum
  109. {
  110. GRUB_NTFS_RF_BLNK = 1
  111. };
  112. struct grub_ntfs_bpb
  113. {
  114. grub_uint8_t jmp_boot[3];
  115. grub_uint8_t oem_name[8];
  116. grub_uint16_t bytes_per_sector;
  117. grub_uint8_t sectors_per_cluster;
  118. grub_uint8_t reserved_1[7];
  119. grub_uint8_t media;
  120. grub_uint16_t reserved_2;
  121. grub_uint16_t sectors_per_track;
  122. grub_uint16_t num_heads;
  123. grub_uint32_t num_hidden_sectors;
  124. grub_uint32_t reserved_3;
  125. grub_uint8_t bios_drive;
  126. grub_uint8_t reserved_4[3];
  127. grub_uint64_t num_total_sectors;
  128. grub_uint64_t mft_lcn;
  129. grub_uint64_t mft_mirr_lcn;
  130. grub_int8_t clusters_per_mft;
  131. grub_int8_t reserved_5[3];
  132. grub_int8_t clusters_per_index;
  133. grub_int8_t reserved_6[3];
  134. grub_uint64_t num_serial;
  135. grub_uint32_t checksum;
  136. } GRUB_PACKED;
  137. struct grub_ntfs_attr
  138. {
  139. int flags;
  140. grub_uint8_t *emft_buf, *edat_buf;
  141. grub_uint8_t *attr_cur, *attr_nxt, *attr_end;
  142. grub_uint32_t save_pos;
  143. grub_uint8_t *sbuf;
  144. grub_uint8_t *end;
  145. struct grub_ntfs_file *mft;
  146. };
  147. struct grub_ntfs_file
  148. {
  149. struct grub_ntfs_data *data;
  150. grub_uint8_t *buf;
  151. grub_uint64_t size;
  152. grub_uint64_t mtime;
  153. grub_uint64_t ino;
  154. int inode_read;
  155. struct grub_ntfs_attr attr;
  156. };
  157. struct grub_ntfs_data
  158. {
  159. struct grub_ntfs_file cmft;
  160. struct grub_ntfs_file mmft;
  161. grub_disk_t disk;
  162. grub_uint64_t mft_size;
  163. grub_uint64_t idx_size;
  164. int log_spc;
  165. grub_uint64_t mft_start;
  166. grub_uint64_t uuid;
  167. };
  168. struct grub_ntfs_comp_table_element
  169. {
  170. grub_uint32_t next_vcn;
  171. grub_uint32_t next_lcn;
  172. };
  173. struct grub_ntfs_comp
  174. {
  175. grub_disk_t disk;
  176. int comp_head, comp_tail;
  177. struct grub_ntfs_comp_table_element comp_table[16];
  178. grub_uint32_t cbuf_ofs, cbuf_vcn;
  179. int log_spc;
  180. grub_uint8_t *cbuf;
  181. };
  182. struct grub_ntfs_rlst
  183. {
  184. int flags;
  185. grub_disk_addr_t target_vcn, curr_vcn, next_vcn, curr_lcn;
  186. grub_uint8_t *cur_run;
  187. struct grub_ntfs_attr *attr;
  188. struct grub_ntfs_comp comp;
  189. void *file;
  190. };
  191. typedef grub_err_t (*grub_ntfscomp_func_t) (grub_uint8_t *dest,
  192. grub_disk_addr_t ofs,
  193. grub_size_t len,
  194. struct grub_ntfs_rlst * ctx);
  195. extern grub_ntfscomp_func_t grub_ntfscomp_func;
  196. grub_err_t grub_ntfs_read_run_list (struct grub_ntfs_rlst *ctx);
  197. #endif /* ! GRUB_NTFS_H */