ntfs.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. /* ntfs.c - NTFS filesystem */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2007,2008,2009 Free Software Foundation, Inc.
  5. *
  6. * This program 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. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define grub_fshelp_node grub_ntfs_file
  20. #include <grub/file.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/disk.h>
  24. #include <grub/dl.h>
  25. #include <grub/fshelp.h>
  26. #include <grub/ntfs.h>
  27. #include <grub/charset.h>
  28. #include <grub/lockdown.h>
  29. GRUB_MOD_LICENSE ("GPLv3+");
  30. static grub_dl_t my_mod;
  31. #define grub_fshelp_node grub_ntfs_file
  32. static inline grub_uint16_t
  33. u16at (void *ptr, grub_size_t ofs)
  34. {
  35. return grub_le_to_cpu16 (grub_get_unaligned16 ((char *) ptr + ofs));
  36. }
  37. static inline grub_uint32_t
  38. u32at (void *ptr, grub_size_t ofs)
  39. {
  40. return grub_le_to_cpu32 (grub_get_unaligned32 ((char *) ptr + ofs));
  41. }
  42. static inline grub_uint64_t
  43. u64at (void *ptr, grub_size_t ofs)
  44. {
  45. return grub_le_to_cpu64 (grub_get_unaligned64 ((char *) ptr + ofs));
  46. }
  47. static grub_uint16_t
  48. first_attr_off (void *mft_buf_ptr)
  49. {
  50. return u16at (mft_buf_ptr, 0x14);
  51. }
  52. static grub_uint16_t
  53. res_attr_data_off (void *res_attr_ptr)
  54. {
  55. return u16at (res_attr_ptr, 0x14);
  56. }
  57. static grub_uint32_t
  58. res_attr_data_len (void *res_attr_ptr)
  59. {
  60. return u32at (res_attr_ptr, 0x10);
  61. }
  62. /*
  63. * Check if the attribute is valid and doesn't exceed the allocated region.
  64. * This accounts for resident and non-resident data.
  65. *
  66. * This is based off the documentation from the linux-ntfs project:
  67. * https://flatcap.github.io/linux-ntfs/ntfs/concepts/attribute_header.html
  68. */
  69. static bool
  70. validate_attribute (grub_uint8_t *attr, void *end)
  71. {
  72. grub_size_t attr_size = 0;
  73. grub_size_t min_size = 0;
  74. grub_size_t spare = (grub_uint8_t *) end - attr;
  75. /*
  76. * Just used as a temporary variable to try and deal with cases where someone
  77. * tries to overlap fields.
  78. */
  79. grub_size_t curr = 0;
  80. /* Need verify we can entirely read the attributes header. */
  81. if (attr + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= (grub_uint8_t *) end)
  82. goto fail;
  83. /*
  84. * So, the rest of this code uses a 16bit int for the attribute length but
  85. * from reading the all the documentation I could find it says this field is
  86. * actually 32bit. But let's be consistent with the rest of the code.
  87. *
  88. * https://elixir.bootlin.com/linux/v6.10.7/source/fs/ntfs3/ntfs.h#L370
  89. */
  90. attr_size = u16at (attr, GRUB_NTFS_ATTRIBUTE_LENGTH);
  91. if (attr_size > spare)
  92. goto fail;
  93. /* Not an error case, just reached the end of the attributes. */
  94. if (attr_size == 0)
  95. return false;
  96. /*
  97. * Extra validation by trying to calculate a minimum possible size for this
  98. * attribute. +8 from the size of the resident data struct which is the
  99. * minimum that can be added.
  100. */
  101. min_size = GRUB_NTFS_ATTRIBUTE_HEADER_SIZE + 8;
  102. if (min_size > attr_size)
  103. goto fail;
  104. /* Is the data is resident (0) or not (1). */
  105. if (attr[GRUB_NTFS_ATTRIBUTE_RESIDENT] == 0)
  106. {
  107. /* Read the offset and size of the attribute. */
  108. curr = u16at (attr, GRUB_NTFS_ATTRIBUTE_RES_OFFSET);
  109. curr += u32at (attr, GRUB_NTFS_ATTRIBUTE_RES_LENGTH);
  110. if (curr > min_size)
  111. min_size = curr;
  112. }
  113. else
  114. {
  115. /*
  116. * If the data is non-resident, the minimum size is 64 which is where
  117. * the data runs start. We already have a minimum size of 24. So, just
  118. * adding 40 to get to the real value.
  119. */
  120. min_size += 40;
  121. if (min_size > attr_size)
  122. goto fail;
  123. /* If the compression unit size is > 0, +8 bytes*/
  124. if (u16at (attr, GRUB_NTFS_ATTRIBUTE_COMPRESSION_UNIT_SIZE) > 0)
  125. min_size += 8;
  126. /*
  127. * Need to consider the data runs now. Each member of the run has byte
  128. * that describes the size of the data length and offset. Each being
  129. * 4 bits in the byte.
  130. */
  131. curr = u16at (attr, GRUB_NTFS_ATTRIBUTE_DATA_RUNS);
  132. if (curr + 1 > min_size)
  133. min_size = curr + 1;
  134. if (min_size > attr_size)
  135. goto fail;
  136. /*
  137. * Each attribute can store multiple data runs which are stored
  138. * continuously in the attribute. They exist as one header byte
  139. * with up to 14 bytes following it depending on the lengths.
  140. * We stop when we hit a header that is just a NUL byte.
  141. *
  142. * https://flatcap.github.io/linux-ntfs/ntfs/concepts/data_runs.html
  143. */
  144. while (attr[curr] != 0)
  145. {
  146. /*
  147. * We stop when we hit a header that is just a NUL byte. The data
  148. * run header is stored as a single byte where the top 4 bits refer
  149. * to the number of bytes used to store the total length of the
  150. * data run, and the number of bytes used to store the offset.
  151. * These directly follow the header byte, so we use them to update
  152. * the minimum size.
  153. */
  154. min_size += (attr[curr] & 0x7) + ((attr[curr] >> 4) & 0x7);
  155. curr += min_size;
  156. min_size++;
  157. if (min_size > attr_size)
  158. goto fail;
  159. }
  160. }
  161. /* Name offset, doing this after data residence checks. */
  162. if (u16at (attr, GRUB_NTFS_ATTRIBUTE_NAME_OFFSET) != 0)
  163. {
  164. curr = u16at (attr, GRUB_NTFS_ATTRIBUTE_NAME_OFFSET);
  165. /*
  166. * Multiple the name length by 2 as its UTF-16. Can be zero if this in an
  167. * unamed attribute.
  168. */
  169. curr += attr[GRUB_NTFS_ATTRIBUTE_NAME_LENGTH] * 2;
  170. if (curr > min_size)
  171. min_size = curr;
  172. }
  173. /* Padded to 8 bytes. */
  174. if (min_size % 8 != 0)
  175. min_size += 8 - (min_size % 8);
  176. /*
  177. * At this point min_size should be exactly attr_size but being flexible
  178. * here to avoid any issues.
  179. */
  180. if (min_size > attr_size)
  181. goto fail;
  182. return true;
  183. fail:
  184. grub_dprintf ("ntfs", "spare=%" PRIuGRUB_SIZE " min_size=%" PRIuGRUB_SIZE " attr_size=%" PRIuGRUB_SIZE "\n",
  185. spare, min_size, attr_size);
  186. return false;
  187. }
  188. /* Return the next attribute if it exists, otherwise return NULL. */
  189. static grub_uint8_t *
  190. next_attribute (grub_uint8_t *curr_attribute, void *end)
  191. {
  192. grub_uint8_t *next = curr_attribute;
  193. /*
  194. * Need to verify we aren't exceeding the end of the buffer by reading the
  195. * header for the current attribute
  196. */
  197. if (curr_attribute + GRUB_NTFS_ATTRIBUTE_HEADER_SIZE >= (grub_uint8_t *) end)
  198. return NULL;
  199. next += u16at (curr_attribute, 4);
  200. if (validate_attribute (next, end) == false)
  201. return NULL;
  202. return next;
  203. }
  204. grub_ntfscomp_func_t grub_ntfscomp_func;
  205. static grub_err_t
  206. fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
  207. {
  208. grub_uint16_t ss;
  209. grub_uint8_t *pu;
  210. grub_uint16_t us;
  211. COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE);
  212. if (grub_memcmp (buf, magic, 4))
  213. return grub_error (GRUB_ERR_BAD_FS, "%s label not found", magic);
  214. ss = u16at (buf, 6) - 1;
  215. if (ss != len)
  216. return grub_error (GRUB_ERR_BAD_FS, "size not match");
  217. pu = buf + u16at (buf, 4);
  218. us = u16at (pu, 0);
  219. buf -= 2;
  220. while (ss > 0)
  221. {
  222. buf += GRUB_DISK_SECTOR_SIZE;
  223. pu += 2;
  224. if (u16at (buf, 0) != us)
  225. return grub_error (GRUB_ERR_BAD_FS, "fixup signature not match");
  226. buf[0] = pu[0];
  227. buf[1] = pu[1];
  228. ss--;
  229. }
  230. return 0;
  231. }
  232. static grub_err_t read_mft (struct grub_ntfs_data *data, grub_uint8_t *buf,
  233. grub_uint64_t mftno);
  234. static grub_err_t read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest,
  235. grub_disk_addr_t ofs, grub_size_t len,
  236. int cached,
  237. grub_disk_read_hook_t read_hook,
  238. void *read_hook_data);
  239. static grub_err_t read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa,
  240. grub_uint8_t *dest,
  241. grub_disk_addr_t ofs, grub_size_t len,
  242. int cached,
  243. grub_disk_read_hook_t read_hook,
  244. void *read_hook_data);
  245. static grub_err_t
  246. init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft)
  247. {
  248. at->mft = mft;
  249. at->flags = (mft == &mft->data->mmft) ? GRUB_NTFS_AF_MMFT : 0;
  250. at->attr_nxt = mft->buf + first_attr_off (mft->buf);
  251. at->end = mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  252. if (at->attr_nxt > at->end)
  253. return grub_error (GRUB_ERR_BAD_FS, "attributes start outside the MFT");
  254. at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL;
  255. return GRUB_ERR_NONE;
  256. }
  257. static void
  258. free_attr (struct grub_ntfs_attr *at)
  259. {
  260. grub_free (at->emft_buf);
  261. grub_free (at->edat_buf);
  262. grub_free (at->sbuf);
  263. }
  264. static grub_uint8_t *
  265. find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
  266. {
  267. grub_uint8_t *mft_end;
  268. if (at->flags & GRUB_NTFS_AF_ALST)
  269. {
  270. retry:
  271. while (at->attr_nxt)
  272. {
  273. at->attr_cur = at->attr_nxt;
  274. at->attr_nxt = next_attribute (at->attr_cur, at->attr_end);
  275. if ((*at->attr_cur == attr) || (attr == 0))
  276. {
  277. grub_uint8_t *new_pos, *end;
  278. if (at->flags & GRUB_NTFS_AF_MMFT)
  279. {
  280. if ((grub_disk_read
  281. (at->mft->data->disk, u32at (at->attr_cur, 0x10), 0,
  282. 512, at->emft_buf))
  283. ||
  284. (grub_disk_read
  285. (at->mft->data->disk, u32at (at->attr_cur, 0x14), 0,
  286. 512, at->emft_buf + 512)))
  287. return NULL;
  288. if (fixup (at->emft_buf, at->mft->data->mft_size,
  289. (const grub_uint8_t *) "FILE"))
  290. return NULL;
  291. }
  292. else
  293. {
  294. if (read_mft (at->mft->data, at->emft_buf,
  295. u32at (at->attr_cur, 0x10)))
  296. return NULL;
  297. }
  298. /*
  299. * Only time emft_bufs is defined is in this function, with this
  300. * size.
  301. */
  302. grub_size_t emft_buf_size =
  303. at->mft->data->mft_size << GRUB_NTFS_BLK_SHR;
  304. /*
  305. * Needs to be enough space for the successful case to even
  306. * bother.
  307. */
  308. if (first_attr_off (at->emft_buf) >= (emft_buf_size - 0x18 - 2))
  309. {
  310. grub_error (GRUB_ERR_BAD_FS,
  311. "can\'t find 0x%X in attribute list",
  312. (unsigned char) *at->attr_cur);
  313. return NULL;
  314. }
  315. new_pos = &at->emft_buf[first_attr_off (at->emft_buf)];
  316. end = &at->emft_buf[emft_buf_size];
  317. while (new_pos && *new_pos != 0xFF)
  318. {
  319. if ((*new_pos == *at->attr_cur)
  320. && (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18)))
  321. {
  322. return new_pos;
  323. }
  324. new_pos = next_attribute (new_pos, end);
  325. }
  326. grub_error (GRUB_ERR_BAD_FS,
  327. "can\'t find 0x%X in attribute list",
  328. (unsigned char) *at->attr_cur);
  329. return NULL;
  330. }
  331. }
  332. return NULL;
  333. }
  334. at->attr_cur = at->attr_nxt;
  335. mft_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  336. while (at->attr_cur < mft_end && *at->attr_cur != 0xFF)
  337. {
  338. at->attr_nxt = next_attribute (at->attr_cur, at->end);
  339. if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
  340. at->attr_end = at->attr_cur;
  341. if ((*at->attr_cur == attr) || (attr == 0))
  342. return at->attr_cur;
  343. at->attr_cur = at->attr_nxt;
  344. }
  345. if (at->attr_end)
  346. {
  347. grub_uint8_t *pa, *pa_end;
  348. at->emft_buf = grub_malloc (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  349. if (at->emft_buf == NULL)
  350. return NULL;
  351. pa = at->attr_end;
  352. if (pa[8])
  353. {
  354. grub_uint32_t n;
  355. n = ((u32at (pa, 0x30) + GRUB_DISK_SECTOR_SIZE - 1)
  356. & (~(GRUB_DISK_SECTOR_SIZE - 1)));
  357. at->attr_cur = at->attr_end;
  358. at->edat_buf = grub_malloc (n);
  359. if (!at->edat_buf)
  360. return NULL;
  361. if (read_data (at, pa, at->edat_buf, 0, n, 0, 0, 0))
  362. {
  363. grub_error (GRUB_ERR_BAD_FS,
  364. "fail to read non-resident attribute list");
  365. return NULL;
  366. }
  367. at->attr_nxt = at->edat_buf;
  368. at->attr_end = at->edat_buf + u32at (pa, 0x30);
  369. pa_end = at->edat_buf + n;
  370. }
  371. else
  372. {
  373. at->attr_nxt = at->attr_end + res_attr_data_off (pa);
  374. at->attr_end = at->attr_end + u32at (pa, 4);
  375. pa_end = at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  376. }
  377. at->flags |= GRUB_NTFS_AF_ALST;
  378. /* From this point on pa_end is the end of the buffer */
  379. at->end = pa_end;
  380. if (validate_attribute (at->attr_nxt, pa_end) == false)
  381. return NULL;
  382. while (at->attr_nxt)
  383. {
  384. if ((*at->attr_nxt == attr) || (attr == 0))
  385. break;
  386. at->attr_nxt = next_attribute (at->attr_nxt, pa_end);
  387. }
  388. if (at->attr_nxt >= at->attr_end || at->attr_nxt == NULL)
  389. return NULL;
  390. if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA))
  391. {
  392. at->flags |= GRUB_NTFS_AF_GPOS;
  393. at->attr_cur = at->attr_nxt;
  394. pa = at->attr_cur;
  395. if ((pa >= pa_end) || (pa_end - pa < 0x18))
  396. {
  397. grub_error (GRUB_ERR_BAD_FS, "can\'t parse attribute list");
  398. return NULL;
  399. }
  400. grub_set_unaligned32 ((char *) pa + 0x10,
  401. grub_cpu_to_le32 (at->mft->data->mft_start));
  402. grub_set_unaligned32 ((char *) pa + 0x14,
  403. grub_cpu_to_le32 (at->mft->data->mft_start
  404. + 1));
  405. pa = at->attr_nxt + u16at (pa, 4);
  406. if (validate_attribute (pa, pa_end) == true)
  407. pa = NULL;
  408. while (pa)
  409. {
  410. if (*pa != attr)
  411. break;
  412. if ((pa >= pa_end) || (pa_end - pa < 0x18))
  413. {
  414. grub_error (GRUB_ERR_BAD_FS, "can\'t parse attribute list");
  415. return NULL;
  416. }
  417. if (read_attr
  418. (at, pa + 0x10,
  419. u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
  420. at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
  421. return NULL;
  422. pa = next_attribute (pa, pa_end);
  423. }
  424. at->attr_nxt = at->attr_cur;
  425. at->flags &= ~GRUB_NTFS_AF_GPOS;
  426. }
  427. goto retry;
  428. }
  429. return NULL;
  430. }
  431. static grub_uint8_t *
  432. locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft,
  433. grub_uint8_t attr)
  434. {
  435. grub_uint8_t *pa;
  436. if (init_attr (at, mft) != GRUB_ERR_NONE)
  437. return NULL;
  438. pa = find_attr (at, attr);
  439. if (pa == NULL)
  440. return NULL;
  441. if ((at->flags & GRUB_NTFS_AF_ALST) == 0)
  442. {
  443. while (1)
  444. {
  445. pa = find_attr (at, attr);
  446. if (pa == NULL)
  447. break;
  448. if (at->flags & GRUB_NTFS_AF_ALST)
  449. return pa;
  450. }
  451. grub_errno = GRUB_ERR_NONE;
  452. free_attr (at);
  453. if (init_attr (at, mft) != GRUB_ERR_NONE)
  454. return NULL;
  455. pa = find_attr (at, attr);
  456. }
  457. return pa;
  458. }
  459. static grub_disk_addr_t
  460. read_run_data (const grub_uint8_t *run, int nn, int sig)
  461. {
  462. grub_uint64_t r = 0;
  463. if (sig && nn && (run[nn - 1] & 0x80))
  464. r = -1;
  465. grub_memcpy (&r, run, nn);
  466. return grub_le_to_cpu64 (r);
  467. }
  468. grub_err_t
  469. grub_ntfs_read_run_list (struct grub_ntfs_rlst * ctx)
  470. {
  471. grub_uint8_t c1, c2;
  472. grub_disk_addr_t val;
  473. grub_uint8_t *run;
  474. run = ctx->cur_run;
  475. retry:
  476. c1 = ((*run) & 0x7);
  477. c2 = ((*run) >> 4) & 0x7;
  478. run++;
  479. if (!c1)
  480. {
  481. if ((ctx->attr) && (ctx->attr->flags & GRUB_NTFS_AF_ALST))
  482. {
  483. grub_disk_read_hook_t save_hook;
  484. save_hook = ctx->comp.disk->read_hook;
  485. ctx->comp.disk->read_hook = 0;
  486. run = find_attr (ctx->attr, *ctx->attr->attr_cur);
  487. ctx->comp.disk->read_hook = save_hook;
  488. if (run)
  489. {
  490. if (run[8] == 0)
  491. return grub_error (GRUB_ERR_BAD_FS,
  492. "$DATA should be non-resident");
  493. run += u16at (run, 0x20);
  494. ctx->curr_lcn = 0;
  495. goto retry;
  496. }
  497. }
  498. return grub_error (GRUB_ERR_BAD_FS, "run list overflow");
  499. }
  500. ctx->curr_vcn = ctx->next_vcn;
  501. ctx->next_vcn += read_run_data (run, c1, 0); /* length of current VCN */
  502. run += c1;
  503. val = read_run_data (run, c2, 1); /* offset to previous LCN */
  504. run += c2;
  505. ctx->curr_lcn += val;
  506. if (val == 0)
  507. ctx->flags |= GRUB_NTFS_RF_BLNK;
  508. else
  509. ctx->flags &= ~GRUB_NTFS_RF_BLNK;
  510. ctx->cur_run = run;
  511. return 0;
  512. }
  513. static grub_disk_addr_t
  514. grub_ntfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t block)
  515. {
  516. struct grub_ntfs_rlst *ctx;
  517. ctx = (struct grub_ntfs_rlst *) node;
  518. if (block >= ctx->next_vcn)
  519. {
  520. if (grub_ntfs_read_run_list (ctx))
  521. return -1;
  522. return ctx->curr_lcn;
  523. }
  524. else
  525. return (ctx->flags & GRUB_NTFS_RF_BLNK) ? 0 : (block -
  526. ctx->curr_vcn + ctx->curr_lcn);
  527. }
  528. static grub_err_t
  529. read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
  530. grub_disk_addr_t ofs, grub_size_t len, int cached,
  531. grub_disk_read_hook_t read_hook, void *read_hook_data)
  532. {
  533. struct grub_ntfs_rlst cc, *ctx;
  534. if (len == 0)
  535. return 0;
  536. grub_memset (&cc, 0, sizeof (cc));
  537. ctx = &cc;
  538. ctx->attr = at;
  539. ctx->comp.log_spc = at->mft->data->log_spc;
  540. ctx->comp.disk = at->mft->data->disk;
  541. if (read_hook == grub_file_progress_hook)
  542. ctx->file = read_hook_data;
  543. if (pa[8] == 0)
  544. {
  545. if (ofs + len > res_attr_data_len (pa))
  546. return grub_error (GRUB_ERR_BAD_FS, "read out of range");
  547. if (res_attr_data_len (pa) > (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
  548. return grub_error (GRUB_ERR_BAD_FS, "resident attribute too large");
  549. if (pa >= at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
  550. return grub_error (GRUB_ERR_BAD_FS, "resident attribute out of range");
  551. if (res_attr_data_off (pa) + res_attr_data_len (pa) >
  552. (grub_addr_t) at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR) - (grub_addr_t) pa)
  553. return grub_error (GRUB_ERR_BAD_FS, "resident attribute out of range");
  554. grub_memcpy (dest, pa + res_attr_data_off (pa) + ofs, len);
  555. return 0;
  556. }
  557. ctx->cur_run = pa + u16at (pa, 0x20);
  558. ctx->next_vcn = u32at (pa, 0x10);
  559. ctx->curr_lcn = 0;
  560. if ((pa[0xC] & GRUB_NTFS_FLAG_COMPRESSED)
  561. && !(at->flags & GRUB_NTFS_AF_GPOS))
  562. {
  563. if (!cached)
  564. return grub_error (GRUB_ERR_BAD_FS, "attribute can\'t be compressed");
  565. return (grub_ntfscomp_func) ? grub_ntfscomp_func (dest, ofs, len, ctx)
  566. : grub_error (GRUB_ERR_BAD_FS, N_("module `%s' isn't loaded"),
  567. "ntfscomp");
  568. }
  569. ctx->target_vcn = ofs >> (GRUB_NTFS_BLK_SHR + ctx->comp.log_spc);
  570. while (ctx->next_vcn <= ctx->target_vcn)
  571. {
  572. if (grub_ntfs_read_run_list (ctx))
  573. return grub_errno;
  574. }
  575. if (at->flags & GRUB_NTFS_AF_GPOS)
  576. {
  577. grub_disk_addr_t st0, st1;
  578. grub_uint64_t m;
  579. m = (ofs >> GRUB_NTFS_BLK_SHR) & ((1 << ctx->comp.log_spc) - 1);
  580. st0 =
  581. ((ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc) + m;
  582. st1 = st0 + 1;
  583. if (st1 ==
  584. (ctx->next_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc)
  585. {
  586. if (grub_ntfs_read_run_list (ctx))
  587. return grub_errno;
  588. st1 = ctx->curr_lcn << ctx->comp.log_spc;
  589. }
  590. grub_set_unaligned32 (dest, grub_cpu_to_le32 (st0));
  591. grub_set_unaligned32 (dest + 4, grub_cpu_to_le32 (st1));
  592. return 0;
  593. }
  594. grub_fshelp_read_file (ctx->comp.disk, (grub_fshelp_node_t) ctx,
  595. read_hook, read_hook_data, ofs, len,
  596. (char *) dest,
  597. grub_ntfs_read_block, ofs + len,
  598. ctx->comp.log_spc, 0);
  599. return grub_errno;
  600. }
  601. static grub_err_t
  602. read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest, grub_disk_addr_t ofs,
  603. grub_size_t len, int cached,
  604. grub_disk_read_hook_t read_hook, void *read_hook_data)
  605. {
  606. grub_uint8_t *save_cur;
  607. grub_uint8_t attr;
  608. grub_uint8_t *pp;
  609. grub_err_t ret;
  610. save_cur = at->attr_cur;
  611. at->attr_nxt = at->attr_cur;
  612. attr = *at->attr_nxt;
  613. if (at->flags & GRUB_NTFS_AF_ALST)
  614. {
  615. grub_uint8_t *pa;
  616. grub_disk_addr_t vcn;
  617. /* If compression is possible make sure that we include possible
  618. compressed block size. */
  619. if (GRUB_NTFS_LOG_COM_SEC >= at->mft->data->log_spc)
  620. vcn = ((ofs >> GRUB_NTFS_COM_LOG_LEN)
  621. << (GRUB_NTFS_LOG_COM_SEC - at->mft->data->log_spc)) & ~0xFULL;
  622. else
  623. vcn = ofs >> (at->mft->data->log_spc + GRUB_NTFS_BLK_SHR);
  624. pa = at->attr_nxt + u16at (at->attr_nxt, 4);
  625. if (validate_attribute (pa, at->attr_end) == false)
  626. pa = NULL;
  627. while (pa)
  628. {
  629. if (*pa != attr)
  630. break;
  631. if (u32at (pa, 8) > vcn)
  632. break;
  633. at->attr_nxt = pa;
  634. pa = next_attribute (pa, at->attr_end);
  635. }
  636. }
  637. pp = find_attr (at, attr);
  638. if (pp)
  639. ret = read_data (at, pp, dest, ofs, len, cached,
  640. read_hook, read_hook_data);
  641. else
  642. ret =
  643. (grub_errno) ? grub_errno : grub_error (GRUB_ERR_BAD_FS,
  644. "attribute not found");
  645. at->attr_cur = save_cur;
  646. return ret;
  647. }
  648. static grub_err_t
  649. read_mft (struct grub_ntfs_data *data, grub_uint8_t *buf, grub_uint64_t mftno)
  650. {
  651. if (read_attr
  652. (&data->mmft.attr, buf, mftno * ((grub_disk_addr_t) data->mft_size << GRUB_NTFS_BLK_SHR),
  653. data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
  654. return grub_error (GRUB_ERR_BAD_FS, "read MFT 0x%llx fails", (unsigned long long) mftno);
  655. return fixup (buf, data->mft_size, (const grub_uint8_t *) "FILE");
  656. }
  657. static grub_err_t
  658. init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
  659. {
  660. unsigned short flag;
  661. mft->inode_read = 1;
  662. mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  663. if (mft->buf == NULL)
  664. return grub_errno;
  665. if (read_mft (mft->data, mft->buf, mftno))
  666. return grub_errno;
  667. flag = u16at (mft->buf, 0x16);
  668. if ((flag & 1) == 0)
  669. return grub_error (GRUB_ERR_BAD_FS, "MFT 0x%llx is not in use",
  670. (unsigned long long) mftno);
  671. if ((flag & 2) == 0)
  672. {
  673. grub_uint8_t *pa;
  674. pa = locate_attr (&mft->attr, mft, GRUB_NTFS_AT_DATA);
  675. if (pa == NULL)
  676. return grub_error (GRUB_ERR_BAD_FS, "no $DATA in MFT 0x%llx",
  677. (unsigned long long) mftno);
  678. if (!pa[8])
  679. mft->size = res_attr_data_len (pa);
  680. else
  681. mft->size = u64at (pa, 0x30);
  682. if ((mft->attr.flags & GRUB_NTFS_AF_ALST) == 0)
  683. mft->attr.attr_end = 0; /* Don't jump to attribute list */
  684. }
  685. else
  686. return init_attr (&mft->attr, mft);
  687. return 0;
  688. }
  689. static void
  690. free_file (struct grub_ntfs_file *mft)
  691. {
  692. free_attr (&mft->attr);
  693. grub_free (mft->buf);
  694. }
  695. static char *
  696. get_utf8 (grub_uint8_t *in, grub_size_t len)
  697. {
  698. grub_uint8_t *buf;
  699. grub_uint16_t *tmp;
  700. grub_size_t i;
  701. buf = grub_calloc (len, GRUB_MAX_UTF8_PER_UTF16 + 1);
  702. tmp = grub_calloc (len, sizeof (tmp[0]));
  703. if (!buf || !tmp)
  704. {
  705. grub_free (buf);
  706. grub_free (tmp);
  707. return NULL;
  708. }
  709. for (i = 0; i < len; i++)
  710. tmp[i] = grub_le_to_cpu16 (grub_get_unaligned16 (in + 2 * i));
  711. *grub_utf16_to_utf8 (buf, tmp, len) = '\0';
  712. grub_free (tmp);
  713. return (char *) buf;
  714. }
  715. static int
  716. list_file (struct grub_ntfs_file *diro, grub_uint8_t *pos, grub_uint8_t *end_pos,
  717. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  718. {
  719. grub_uint8_t *np;
  720. int ns;
  721. while (1)
  722. {
  723. grub_uint8_t namespace;
  724. char *ustr;
  725. if ((pos >= end_pos) || (end_pos - pos < 0x52))
  726. break;
  727. if (pos[0xC] & 2) /* end signature */
  728. break;
  729. np = pos + 0x50;
  730. ns = *(np++);
  731. namespace = *(np++);
  732. if (2 * ns > end_pos - pos - 0x52)
  733. break;
  734. /*
  735. * Ignore files in DOS namespace, as they will reappear as Win32
  736. * names.
  737. */
  738. if ((ns) && (namespace != 2))
  739. {
  740. enum grub_fshelp_filetype type;
  741. struct grub_ntfs_file *fdiro;
  742. grub_uint32_t attr;
  743. attr = u32at (pos, 0x48);
  744. if (attr & GRUB_NTFS_ATTR_REPARSE)
  745. type = GRUB_FSHELP_SYMLINK;
  746. else if (attr & GRUB_NTFS_ATTR_DIRECTORY)
  747. type = GRUB_FSHELP_DIR;
  748. else
  749. type = GRUB_FSHELP_REG;
  750. fdiro = grub_zalloc (sizeof (struct grub_ntfs_file));
  751. if (!fdiro)
  752. return 0;
  753. fdiro->data = diro->data;
  754. fdiro->ino = u64at (pos, 0) & 0xffffffffffffULL;
  755. fdiro->mtime = u64at (pos, 0x20);
  756. ustr = get_utf8 (np, ns);
  757. if (ustr == NULL)
  758. {
  759. grub_free (fdiro);
  760. return 0;
  761. }
  762. if (namespace)
  763. type |= GRUB_FSHELP_CASE_INSENSITIVE;
  764. if (hook (ustr, type, fdiro, hook_data))
  765. {
  766. grub_free (ustr);
  767. return 1;
  768. }
  769. grub_free (ustr);
  770. }
  771. pos += u16at (pos, 8);
  772. }
  773. return 0;
  774. }
  775. struct symlink_descriptor
  776. {
  777. grub_uint32_t type;
  778. grub_uint32_t total_len;
  779. grub_uint16_t off1;
  780. grub_uint16_t len1;
  781. grub_uint16_t off2;
  782. grub_uint16_t len2;
  783. } GRUB_PACKED;
  784. static char *
  785. grub_ntfs_read_symlink (grub_fshelp_node_t node)
  786. {
  787. struct grub_ntfs_file *mft;
  788. struct symlink_descriptor symdesc;
  789. grub_err_t err;
  790. grub_uint8_t *buf16 = NULL;
  791. char *buf, *end;
  792. grub_size_t len;
  793. grub_uint8_t *pa;
  794. grub_size_t off;
  795. mft = (struct grub_ntfs_file *) node;
  796. mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  797. if (mft->buf == NULL)
  798. return NULL;
  799. if (read_mft (mft->data, mft->buf, mft->ino))
  800. goto fail;
  801. pa = locate_attr (&mft->attr, mft, GRUB_NTFS_AT_SYMLINK);
  802. if (pa == NULL)
  803. {
  804. grub_error (GRUB_ERR_BAD_FS, "no $SYMLINK in MFT 0x%llx",
  805. (unsigned long long) mft->ino);
  806. goto fail;
  807. }
  808. err = read_attr (&mft->attr, (grub_uint8_t *) &symdesc, 0,
  809. sizeof (struct symlink_descriptor), 1, 0, 0);
  810. if (err)
  811. goto fail;
  812. switch (grub_cpu_to_le32 (symdesc.type))
  813. {
  814. case 0xa000000c:
  815. off = (sizeof (struct symlink_descriptor) + 4
  816. + grub_cpu_to_le32 (symdesc.off1));
  817. len = grub_cpu_to_le32 (symdesc.len1);
  818. break;
  819. case 0xa0000003:
  820. off = (sizeof (struct symlink_descriptor)
  821. + grub_cpu_to_le32 (symdesc.off1));
  822. len = grub_cpu_to_le32 (symdesc.len1);
  823. break;
  824. default:
  825. grub_error (GRUB_ERR_BAD_FS, "symlink type invalid (%x)",
  826. grub_cpu_to_le32 (symdesc.type));
  827. goto fail;
  828. }
  829. buf16 = grub_malloc (len);
  830. if (!buf16)
  831. goto fail;
  832. err = read_attr (&mft->attr, buf16, off, len, 1, 0, 0);
  833. if (err)
  834. goto fail;
  835. buf = get_utf8 (buf16, len / 2);
  836. if (!buf)
  837. goto fail;
  838. grub_free (mft->buf);
  839. grub_free (buf16);
  840. for (end = buf; *end; end++)
  841. if (*end == '\\')
  842. *end = '/';
  843. /* Split the sequence to avoid GCC thinking that this is a trigraph. */
  844. if (grub_memcmp (buf, "/?" "?/", 4) == 0 && buf[5] == ':' && buf[6] == '/'
  845. && grub_isalpha (buf[4]))
  846. {
  847. grub_memmove (buf, buf + 6, end - buf + 1 - 6);
  848. end -= 6;
  849. }
  850. return buf;
  851. fail:
  852. grub_free (mft->buf);
  853. grub_free (buf16);
  854. return NULL;
  855. }
  856. static int
  857. grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
  858. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  859. {
  860. grub_uint8_t *bitmap;
  861. struct grub_ntfs_attr attr, *at;
  862. grub_uint8_t *cur_pos, *indx, *bmp;
  863. int ret = 0;
  864. grub_size_t bitmap_len;
  865. struct grub_ntfs_file *mft;
  866. mft = (struct grub_ntfs_file *) dir;
  867. if (!mft->inode_read)
  868. {
  869. if (init_file (mft, mft->ino))
  870. return 0;
  871. }
  872. indx = NULL;
  873. bmp = NULL;
  874. at = &attr;
  875. if (init_attr (at, mft) != GRUB_ERR_NONE)
  876. return 0;
  877. while (1)
  878. {
  879. cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ROOT);
  880. if (cur_pos == NULL)
  881. {
  882. grub_error (GRUB_ERR_BAD_FS, "no $INDEX_ROOT");
  883. goto done;
  884. }
  885. /* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */
  886. if ((u32at (cur_pos, 8) != 0x180400) ||
  887. (u32at (cur_pos, 0x18) != 0x490024) ||
  888. (u32at (cur_pos, 0x1C) != 0x300033))
  889. continue;
  890. cur_pos += res_attr_data_off (cur_pos);
  891. if (*cur_pos != 0x30) /* Not filename index */
  892. continue;
  893. break;
  894. }
  895. cur_pos += 0x10; /* Skip index root */
  896. ret = list_file (mft, cur_pos + u16at (cur_pos, 0),
  897. at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
  898. hook, hook_data);
  899. if (ret)
  900. goto done;
  901. bitmap = NULL;
  902. bitmap_len = 0;
  903. free_attr (at);
  904. /* No need to check errors here, as it will already be fine */
  905. init_attr (at, mft);
  906. while ((cur_pos = find_attr (at, GRUB_NTFS_AT_BITMAP)) != NULL)
  907. {
  908. int ofs;
  909. ofs = cur_pos[0xA];
  910. /* Namelen=4, Name="$I30" */
  911. if ((cur_pos[9] == 4) &&
  912. (u32at (cur_pos, ofs) == 0x490024) &&
  913. (u32at (cur_pos, ofs + 4) == 0x300033))
  914. {
  915. int is_resident = (cur_pos[8] == 0);
  916. bitmap_len = ((is_resident) ? res_attr_data_len (cur_pos) :
  917. u32at (cur_pos, 0x28));
  918. bmp = grub_malloc (bitmap_len);
  919. if (bmp == NULL)
  920. goto done;
  921. if (is_resident)
  922. {
  923. if (bitmap_len > (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
  924. {
  925. grub_error (GRUB_ERR_BAD_FS, "resident bitmap too large");
  926. goto done;
  927. }
  928. if (cur_pos >= at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR))
  929. {
  930. grub_error (GRUB_ERR_BAD_FS, "resident bitmap out of range");
  931. goto done;
  932. }
  933. if (res_attr_data_off (cur_pos) + res_attr_data_len (cur_pos) >
  934. (grub_addr_t) at->mft->buf + (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR) - (grub_addr_t) cur_pos)
  935. {
  936. grub_error (GRUB_ERR_BAD_FS, "resident bitmap out of range");
  937. goto done;
  938. }
  939. grub_memcpy (bmp, cur_pos + res_attr_data_off (cur_pos),
  940. bitmap_len);
  941. }
  942. else
  943. {
  944. if (read_data (at, cur_pos, bmp, 0, bitmap_len, 0, 0, 0))
  945. {
  946. grub_error (GRUB_ERR_BAD_FS,
  947. "fails to read non-resident $BITMAP");
  948. goto done;
  949. }
  950. bitmap_len = u32at (cur_pos, 0x30);
  951. }
  952. bitmap = bmp;
  953. break;
  954. }
  955. }
  956. free_attr (at);
  957. cur_pos = locate_attr (at, mft, GRUB_NTFS_AT_INDEX_ALLOCATION);
  958. while (cur_pos != NULL)
  959. {
  960. /* Non-resident, Namelen=4, Offset=0x40, Flags=0, Name="$I30" */
  961. if ((u32at (cur_pos, 8) == 0x400401) &&
  962. (u32at (cur_pos, 0x40) == 0x490024) &&
  963. (u32at (cur_pos, 0x44) == 0x300033))
  964. break;
  965. cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ALLOCATION);
  966. }
  967. if ((!cur_pos) && (bitmap))
  968. {
  969. grub_error (GRUB_ERR_BAD_FS, "$BITMAP without $INDEX_ALLOCATION");
  970. goto done;
  971. }
  972. if (bitmap)
  973. {
  974. grub_disk_addr_t i;
  975. grub_uint8_t v;
  976. indx = grub_malloc (mft->data->idx_size << GRUB_NTFS_BLK_SHR);
  977. if (indx == NULL)
  978. goto done;
  979. v = 1;
  980. for (i = 0; i < (grub_disk_addr_t)bitmap_len * 8; i++)
  981. {
  982. if (*bitmap & v)
  983. {
  984. if ((read_attr
  985. (at, indx, i * (mft->data->idx_size << GRUB_NTFS_BLK_SHR),
  986. (mft->data->idx_size << GRUB_NTFS_BLK_SHR), 0, 0, 0))
  987. || (fixup (indx, mft->data->idx_size,
  988. (const grub_uint8_t *) "INDX")))
  989. goto done;
  990. ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)],
  991. indx + (mft->data->idx_size << GRUB_NTFS_BLK_SHR),
  992. hook, hook_data);
  993. if (ret)
  994. goto done;
  995. }
  996. v <<= 1;
  997. if (!v)
  998. {
  999. v = 1;
  1000. bitmap++;
  1001. }
  1002. }
  1003. }
  1004. done:
  1005. free_attr (at);
  1006. grub_free (indx);
  1007. grub_free (bmp);
  1008. return ret;
  1009. }
  1010. static struct grub_ntfs_data *
  1011. grub_ntfs_mount (grub_disk_t disk)
  1012. {
  1013. struct grub_ntfs_bpb bpb;
  1014. struct grub_ntfs_data *data = 0;
  1015. grub_uint32_t spc;
  1016. if (!disk)
  1017. goto fail;
  1018. data = (struct grub_ntfs_data *) grub_zalloc (sizeof (*data));
  1019. if (!data)
  1020. goto fail;
  1021. data->disk = disk;
  1022. /* Read the BPB. */
  1023. if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
  1024. goto fail;
  1025. if (grub_memcmp ((char *) &bpb.oem_name, "NTFS", 4) != 0
  1026. || bpb.sectors_per_cluster == 0
  1027. || (bpb.sectors_per_cluster & (bpb.sectors_per_cluster - 1)) != 0
  1028. || bpb.bytes_per_sector == 0
  1029. || (bpb.bytes_per_sector & (bpb.bytes_per_sector - 1)) != 0)
  1030. goto fail;
  1031. spc = (((grub_uint32_t) bpb.sectors_per_cluster
  1032. * (grub_uint32_t) grub_le_to_cpu16 (bpb.bytes_per_sector))
  1033. >> GRUB_NTFS_BLK_SHR);
  1034. if (spc == 0)
  1035. goto fail;
  1036. for (data->log_spc = 0; (1U << data->log_spc) < spc; data->log_spc++);
  1037. if (bpb.clusters_per_mft > 0)
  1038. data->mft_size = ((grub_disk_addr_t) bpb.clusters_per_mft) << data->log_spc;
  1039. else if (-bpb.clusters_per_mft < GRUB_NTFS_BLK_SHR || -bpb.clusters_per_mft >= 31)
  1040. goto fail;
  1041. else
  1042. data->mft_size = 1ULL << (-bpb.clusters_per_mft - GRUB_NTFS_BLK_SHR);
  1043. if (bpb.clusters_per_index > 0)
  1044. data->idx_size = (((grub_disk_addr_t) bpb.clusters_per_index)
  1045. << data->log_spc);
  1046. else if (-bpb.clusters_per_index < GRUB_NTFS_BLK_SHR || -bpb.clusters_per_index >= 31)
  1047. goto fail;
  1048. else
  1049. data->idx_size = 1ULL << (-bpb.clusters_per_index - GRUB_NTFS_BLK_SHR);
  1050. data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) << data->log_spc;
  1051. if ((data->mft_size > GRUB_NTFS_MAX_MFT) || (data->idx_size > GRUB_NTFS_MAX_IDX))
  1052. goto fail;
  1053. data->mmft.data = data;
  1054. data->cmft.data = data;
  1055. data->mmft.buf = grub_malloc (data->mft_size << GRUB_NTFS_BLK_SHR);
  1056. if (!data->mmft.buf)
  1057. goto fail;
  1058. if (grub_disk_read
  1059. (disk, data->mft_start, 0, data->mft_size << GRUB_NTFS_BLK_SHR, data->mmft.buf))
  1060. goto fail;
  1061. data->uuid = grub_le_to_cpu64 (bpb.num_serial);
  1062. if (fixup (data->mmft.buf, data->mft_size, (const grub_uint8_t *) "FILE"))
  1063. goto fail;
  1064. if (!locate_attr (&data->mmft.attr, &data->mmft, GRUB_NTFS_AT_DATA))
  1065. goto fail;
  1066. if (init_file (&data->cmft, GRUB_NTFS_FILE_ROOT))
  1067. goto fail;
  1068. return data;
  1069. fail:
  1070. grub_error (GRUB_ERR_BAD_FS, "not an ntfs filesystem");
  1071. if (data)
  1072. {
  1073. free_file (&data->mmft);
  1074. free_file (&data->cmft);
  1075. grub_free (data);
  1076. }
  1077. return 0;
  1078. }
  1079. /* Context for grub_ntfs_dir. */
  1080. struct grub_ntfs_dir_ctx
  1081. {
  1082. grub_fs_dir_hook_t hook;
  1083. void *hook_data;
  1084. };
  1085. /* Helper for grub_ntfs_dir. */
  1086. static int
  1087. grub_ntfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  1088. grub_fshelp_node_t node, void *data)
  1089. {
  1090. struct grub_ntfs_dir_ctx *ctx = data;
  1091. struct grub_dirhook_info info;
  1092. grub_memset (&info, 0, sizeof (info));
  1093. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  1094. info.mtimeset = 1;
  1095. info.mtime = grub_divmod64 (node->mtime, 10000000, 0)
  1096. - 86400ULL * 365 * (1970 - 1601)
  1097. - 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
  1098. grub_free (node);
  1099. return ctx->hook (filename, &info, ctx->hook_data);
  1100. }
  1101. static grub_err_t
  1102. grub_ntfs_dir (grub_device_t device, const char *path,
  1103. grub_fs_dir_hook_t hook, void *hook_data)
  1104. {
  1105. struct grub_ntfs_dir_ctx ctx = { hook, hook_data };
  1106. struct grub_ntfs_data *data = 0;
  1107. struct grub_fshelp_node *fdiro = 0;
  1108. grub_dl_ref (my_mod);
  1109. data = grub_ntfs_mount (device->disk);
  1110. if (!data)
  1111. goto fail;
  1112. grub_fshelp_find_file (path, &data->cmft, &fdiro, grub_ntfs_iterate_dir,
  1113. grub_ntfs_read_symlink, GRUB_FSHELP_DIR);
  1114. if (grub_errno)
  1115. goto fail;
  1116. grub_ntfs_iterate_dir (fdiro, grub_ntfs_dir_iter, &ctx);
  1117. fail:
  1118. if ((fdiro) && (fdiro != &data->cmft))
  1119. {
  1120. free_file (fdiro);
  1121. grub_free (fdiro);
  1122. }
  1123. if (data)
  1124. {
  1125. free_file (&data->mmft);
  1126. free_file (&data->cmft);
  1127. grub_free (data);
  1128. }
  1129. grub_dl_unref (my_mod);
  1130. return grub_errno;
  1131. }
  1132. static grub_err_t
  1133. grub_ntfs_open (grub_file_t file, const char *name)
  1134. {
  1135. struct grub_ntfs_data *data = 0;
  1136. struct grub_fshelp_node *mft = 0;
  1137. grub_dl_ref (my_mod);
  1138. data = grub_ntfs_mount (file->device->disk);
  1139. if (!data)
  1140. goto fail;
  1141. grub_fshelp_find_file (name, &data->cmft, &mft, grub_ntfs_iterate_dir,
  1142. grub_ntfs_read_symlink, GRUB_FSHELP_REG);
  1143. if (grub_errno)
  1144. goto fail;
  1145. if (mft != &data->cmft)
  1146. {
  1147. free_file (&data->cmft);
  1148. grub_memcpy (&data->cmft, mft, sizeof (*mft));
  1149. grub_free (mft);
  1150. if (!data->cmft.inode_read)
  1151. {
  1152. if (init_file (&data->cmft, data->cmft.ino))
  1153. goto fail;
  1154. }
  1155. }
  1156. file->size = data->cmft.size;
  1157. file->data = data;
  1158. file->offset = 0;
  1159. return 0;
  1160. fail:
  1161. if (data)
  1162. {
  1163. free_file (&data->mmft);
  1164. free_file (&data->cmft);
  1165. grub_free (data);
  1166. }
  1167. grub_dl_unref (my_mod);
  1168. return grub_errno;
  1169. }
  1170. static grub_ssize_t
  1171. grub_ntfs_read (grub_file_t file, char *buf, grub_size_t len)
  1172. {
  1173. struct grub_ntfs_file *mft;
  1174. mft = &((struct grub_ntfs_data *) file->data)->cmft;
  1175. if (file->read_hook)
  1176. mft->attr.save_pos = 1;
  1177. read_attr (&mft->attr, (grub_uint8_t *) buf, file->offset, len, 1,
  1178. file->read_hook, file->read_hook_data);
  1179. return (grub_errno) ? -1 : (grub_ssize_t) len;
  1180. }
  1181. static grub_err_t
  1182. grub_ntfs_close (grub_file_t file)
  1183. {
  1184. struct grub_ntfs_data *data;
  1185. data = file->data;
  1186. if (data)
  1187. {
  1188. free_file (&data->mmft);
  1189. free_file (&data->cmft);
  1190. grub_free (data);
  1191. }
  1192. grub_dl_unref (my_mod);
  1193. return grub_errno;
  1194. }
  1195. static grub_err_t
  1196. grub_ntfs_label (grub_device_t device, char **label)
  1197. {
  1198. struct grub_ntfs_data *data = 0;
  1199. struct grub_fshelp_node *mft = 0;
  1200. grub_uint8_t *pa;
  1201. grub_err_t err;
  1202. grub_dl_ref (my_mod);
  1203. *label = 0;
  1204. data = grub_ntfs_mount (device->disk);
  1205. if (!data)
  1206. goto fail;
  1207. grub_fshelp_find_file ("/$Volume", &data->cmft, &mft, grub_ntfs_iterate_dir,
  1208. 0, GRUB_FSHELP_REG);
  1209. if (grub_errno)
  1210. goto fail;
  1211. if (!mft->inode_read)
  1212. {
  1213. mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  1214. if (mft->buf == NULL)
  1215. goto fail;
  1216. if (read_mft (mft->data, mft->buf, mft->ino))
  1217. goto fail;
  1218. }
  1219. err = init_attr (&mft->attr, mft);
  1220. if (err != GRUB_ERR_NONE)
  1221. return err;
  1222. pa = find_attr (&mft->attr, GRUB_NTFS_AT_VOLUME_NAME);
  1223. if (pa >= mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR))
  1224. {
  1225. grub_error (GRUB_ERR_BAD_FS, "can\'t parse volume label");
  1226. goto fail;
  1227. }
  1228. if (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR) - pa < 0x16)
  1229. {
  1230. grub_error (GRUB_ERR_BAD_FS, "can\'t parse volume label");
  1231. goto fail;
  1232. }
  1233. if ((pa) && (pa[8] == 0) && (res_attr_data_len (pa)))
  1234. {
  1235. int len;
  1236. len = res_attr_data_len (pa) / 2;
  1237. pa += res_attr_data_off (pa);
  1238. if (mft->buf + (mft->data->mft_size << GRUB_NTFS_BLK_SHR) - pa >= 2 * len)
  1239. *label = get_utf8 (pa, len);
  1240. else
  1241. grub_error (GRUB_ERR_BAD_FS, "can\'t parse volume label");
  1242. }
  1243. fail:
  1244. if ((mft) && (mft != &data->cmft))
  1245. {
  1246. free_file (mft);
  1247. grub_free (mft);
  1248. }
  1249. if (data)
  1250. {
  1251. free_file (&data->mmft);
  1252. free_file (&data->cmft);
  1253. grub_free (data);
  1254. }
  1255. grub_dl_unref (my_mod);
  1256. return grub_errno;
  1257. }
  1258. static grub_err_t
  1259. grub_ntfs_uuid (grub_device_t device, char **uuid)
  1260. {
  1261. struct grub_ntfs_data *data;
  1262. grub_disk_t disk = device->disk;
  1263. grub_dl_ref (my_mod);
  1264. data = grub_ntfs_mount (disk);
  1265. if (data)
  1266. {
  1267. char *ptr;
  1268. *uuid = grub_xasprintf ("%016llx", (unsigned long long) data->uuid);
  1269. if (*uuid)
  1270. for (ptr = *uuid; *ptr; ptr++)
  1271. *ptr = grub_toupper (*ptr);
  1272. free_file (&data->mmft);
  1273. free_file (&data->cmft);
  1274. grub_free (data);
  1275. }
  1276. else
  1277. *uuid = NULL;
  1278. grub_dl_unref (my_mod);
  1279. return grub_errno;
  1280. }
  1281. static struct grub_fs grub_ntfs_fs =
  1282. {
  1283. .name = "ntfs",
  1284. .fs_dir = grub_ntfs_dir,
  1285. .fs_open = grub_ntfs_open,
  1286. .fs_read = grub_ntfs_read,
  1287. .fs_close = grub_ntfs_close,
  1288. .fs_label = grub_ntfs_label,
  1289. .fs_uuid = grub_ntfs_uuid,
  1290. #ifdef GRUB_UTIL
  1291. .reserved_first_sector = 1,
  1292. .blocklist_install = 1,
  1293. #endif
  1294. .next = 0
  1295. };
  1296. GRUB_MOD_INIT (ntfs)
  1297. {
  1298. if (!grub_is_lockdown ())
  1299. {
  1300. grub_ntfs_fs.mod = mod;
  1301. grub_fs_register (&grub_ntfs_fs);
  1302. }
  1303. my_mod = mod;
  1304. }
  1305. GRUB_MOD_FINI (ntfs)
  1306. {
  1307. if (!grub_is_lockdown ())
  1308. grub_fs_unregister (&grub_ntfs_fs);
  1309. }