ntfs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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. GRUB_MOD_LICENSE ("GPLv3+");
  29. static grub_dl_t my_mod;
  30. #define grub_fshelp_node grub_ntfs_file
  31. static inline grub_uint16_t
  32. u16at (void *ptr, grub_size_t ofs)
  33. {
  34. return grub_le_to_cpu16 (grub_get_unaligned16 ((char *) ptr + ofs));
  35. }
  36. static inline grub_uint32_t
  37. u32at (void *ptr, grub_size_t ofs)
  38. {
  39. return grub_le_to_cpu32 (grub_get_unaligned32 ((char *) ptr + ofs));
  40. }
  41. static inline grub_uint64_t
  42. u64at (void *ptr, grub_size_t ofs)
  43. {
  44. return grub_le_to_cpu64 (grub_get_unaligned64 ((char *) ptr + ofs));
  45. }
  46. grub_ntfscomp_func_t grub_ntfscomp_func;
  47. static grub_err_t
  48. fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
  49. {
  50. grub_uint16_t ss;
  51. grub_uint8_t *pu;
  52. grub_uint16_t us;
  53. COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE);
  54. if (grub_memcmp (buf, magic, 4))
  55. return grub_error (GRUB_ERR_BAD_FS, "%s label not found", magic);
  56. ss = u16at (buf, 6) - 1;
  57. if (ss != len)
  58. return grub_error (GRUB_ERR_BAD_FS, "size not match");
  59. pu = buf + u16at (buf, 4);
  60. us = u16at (pu, 0);
  61. buf -= 2;
  62. while (ss > 0)
  63. {
  64. buf += GRUB_DISK_SECTOR_SIZE;
  65. pu += 2;
  66. if (u16at (buf, 0) != us)
  67. return grub_error (GRUB_ERR_BAD_FS, "fixup signature not match");
  68. buf[0] = pu[0];
  69. buf[1] = pu[1];
  70. ss--;
  71. }
  72. return 0;
  73. }
  74. static grub_err_t read_mft (struct grub_ntfs_data *data, grub_uint8_t *buf,
  75. grub_uint64_t mftno);
  76. static grub_err_t read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest,
  77. grub_disk_addr_t ofs, grub_size_t len,
  78. int cached,
  79. grub_disk_read_hook_t read_hook,
  80. void *read_hook_data);
  81. static grub_err_t read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa,
  82. grub_uint8_t *dest,
  83. grub_disk_addr_t ofs, grub_size_t len,
  84. int cached,
  85. grub_disk_read_hook_t read_hook,
  86. void *read_hook_data);
  87. static void
  88. init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft)
  89. {
  90. at->mft = mft;
  91. at->flags = (mft == &mft->data->mmft) ? GRUB_NTFS_AF_MMFT : 0;
  92. at->attr_nxt = mft->buf + u16at (mft->buf, 0x14);
  93. at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL;
  94. }
  95. static void
  96. free_attr (struct grub_ntfs_attr *at)
  97. {
  98. grub_free (at->emft_buf);
  99. grub_free (at->edat_buf);
  100. grub_free (at->sbuf);
  101. }
  102. static grub_uint8_t *
  103. find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
  104. {
  105. if (at->flags & GRUB_NTFS_AF_ALST)
  106. {
  107. retry:
  108. while (at->attr_nxt < at->attr_end)
  109. {
  110. at->attr_cur = at->attr_nxt;
  111. at->attr_nxt += u16at (at->attr_cur, 4);
  112. if ((*at->attr_cur == attr) || (attr == 0))
  113. {
  114. grub_uint8_t *new_pos;
  115. if (at->flags & GRUB_NTFS_AF_MMFT)
  116. {
  117. if ((grub_disk_read
  118. (at->mft->data->disk, u32at (at->attr_cur, 0x10), 0,
  119. 512, at->emft_buf))
  120. ||
  121. (grub_disk_read
  122. (at->mft->data->disk, u32at (at->attr_cur, 0x14), 0,
  123. 512, at->emft_buf + 512)))
  124. return NULL;
  125. if (fixup (at->emft_buf, at->mft->data->mft_size,
  126. (const grub_uint8_t *) "FILE"))
  127. return NULL;
  128. }
  129. else
  130. {
  131. if (read_mft (at->mft->data, at->emft_buf,
  132. u32at (at->attr_cur, 0x10)))
  133. return NULL;
  134. }
  135. new_pos = &at->emft_buf[u16at (at->emft_buf, 0x14)];
  136. while (*new_pos != 0xFF)
  137. {
  138. if ((*new_pos == *at->attr_cur)
  139. && (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18)))
  140. {
  141. return new_pos;
  142. }
  143. new_pos += u16at (new_pos, 4);
  144. }
  145. grub_error (GRUB_ERR_BAD_FS,
  146. "can\'t find 0x%X in attribute list",
  147. (unsigned char) *at->attr_cur);
  148. return NULL;
  149. }
  150. }
  151. return NULL;
  152. }
  153. at->attr_cur = at->attr_nxt;
  154. while (*at->attr_cur != 0xFF)
  155. {
  156. at->attr_nxt += u16at (at->attr_cur, 4);
  157. if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
  158. at->attr_end = at->attr_cur;
  159. if ((*at->attr_cur == attr) || (attr == 0))
  160. return at->attr_cur;
  161. at->attr_cur = at->attr_nxt;
  162. }
  163. if (at->attr_end)
  164. {
  165. grub_uint8_t *pa;
  166. at->emft_buf = grub_malloc (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  167. if (at->emft_buf == NULL)
  168. return NULL;
  169. pa = at->attr_end;
  170. if (pa[8])
  171. {
  172. grub_uint32_t n;
  173. n = ((u32at (pa, 0x30) + GRUB_DISK_SECTOR_SIZE - 1)
  174. & (~(GRUB_DISK_SECTOR_SIZE - 1)));
  175. at->attr_cur = at->attr_end;
  176. at->edat_buf = grub_malloc (n);
  177. if (!at->edat_buf)
  178. return NULL;
  179. if (read_data (at, pa, at->edat_buf, 0, n, 0, 0, 0))
  180. {
  181. grub_error (GRUB_ERR_BAD_FS,
  182. "fail to read non-resident attribute list");
  183. return NULL;
  184. }
  185. at->attr_nxt = at->edat_buf;
  186. at->attr_end = at->edat_buf + u32at (pa, 0x30);
  187. }
  188. else
  189. {
  190. at->attr_nxt = at->attr_end + u16at (pa, 0x14);
  191. at->attr_end = at->attr_end + u32at (pa, 4);
  192. }
  193. at->flags |= GRUB_NTFS_AF_ALST;
  194. while (at->attr_nxt < at->attr_end)
  195. {
  196. if ((*at->attr_nxt == attr) || (attr == 0))
  197. break;
  198. at->attr_nxt += u16at (at->attr_nxt, 4);
  199. }
  200. if (at->attr_nxt >= at->attr_end)
  201. return NULL;
  202. if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA))
  203. {
  204. at->flags |= GRUB_NTFS_AF_GPOS;
  205. at->attr_cur = at->attr_nxt;
  206. pa = at->attr_cur;
  207. grub_set_unaligned32 ((char *) pa + 0x10,
  208. grub_cpu_to_le32 (at->mft->data->mft_start));
  209. grub_set_unaligned32 ((char *) pa + 0x14,
  210. grub_cpu_to_le32 (at->mft->data->mft_start
  211. + 1));
  212. pa = at->attr_nxt + u16at (pa, 4);
  213. while (pa < at->attr_end)
  214. {
  215. if (*pa != attr)
  216. break;
  217. if (read_attr
  218. (at, pa + 0x10,
  219. u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
  220. at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
  221. return NULL;
  222. pa += u16at (pa, 4);
  223. }
  224. at->attr_nxt = at->attr_cur;
  225. at->flags &= ~GRUB_NTFS_AF_GPOS;
  226. }
  227. goto retry;
  228. }
  229. return NULL;
  230. }
  231. static grub_uint8_t *
  232. locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft,
  233. grub_uint8_t attr)
  234. {
  235. grub_uint8_t *pa;
  236. init_attr (at, mft);
  237. pa = find_attr (at, attr);
  238. if (pa == NULL)
  239. return NULL;
  240. if ((at->flags & GRUB_NTFS_AF_ALST) == 0)
  241. {
  242. while (1)
  243. {
  244. pa = find_attr (at, attr);
  245. if (pa == NULL)
  246. break;
  247. if (at->flags & GRUB_NTFS_AF_ALST)
  248. return pa;
  249. }
  250. grub_errno = GRUB_ERR_NONE;
  251. free_attr (at);
  252. init_attr (at, mft);
  253. pa = find_attr (at, attr);
  254. }
  255. return pa;
  256. }
  257. static grub_disk_addr_t
  258. read_run_data (const grub_uint8_t *run, int nn, int sig)
  259. {
  260. grub_uint64_t r = 0;
  261. if (sig && nn && (run[nn - 1] & 0x80))
  262. r = -1;
  263. grub_memcpy (&r, run, nn);
  264. return grub_le_to_cpu64 (r);
  265. }
  266. grub_err_t
  267. grub_ntfs_read_run_list (struct grub_ntfs_rlst * ctx)
  268. {
  269. grub_uint8_t c1, c2;
  270. grub_disk_addr_t val;
  271. grub_uint8_t *run;
  272. run = ctx->cur_run;
  273. retry:
  274. c1 = ((*run) & 0x7);
  275. c2 = ((*run) >> 4) & 0x7;
  276. run++;
  277. if (!c1)
  278. {
  279. if ((ctx->attr) && (ctx->attr->flags & GRUB_NTFS_AF_ALST))
  280. {
  281. grub_disk_read_hook_t save_hook;
  282. save_hook = ctx->comp.disk->read_hook;
  283. ctx->comp.disk->read_hook = 0;
  284. run = find_attr (ctx->attr, *ctx->attr->attr_cur);
  285. ctx->comp.disk->read_hook = save_hook;
  286. if (run)
  287. {
  288. if (run[8] == 0)
  289. return grub_error (GRUB_ERR_BAD_FS,
  290. "$DATA should be non-resident");
  291. run += u16at (run, 0x20);
  292. ctx->curr_lcn = 0;
  293. goto retry;
  294. }
  295. }
  296. return grub_error (GRUB_ERR_BAD_FS, "run list overflown");
  297. }
  298. ctx->curr_vcn = ctx->next_vcn;
  299. ctx->next_vcn += read_run_data (run, c1, 0); /* length of current VCN */
  300. run += c1;
  301. val = read_run_data (run, c2, 1); /* offset to previous LCN */
  302. run += c2;
  303. ctx->curr_lcn += val;
  304. if (val == 0)
  305. ctx->flags |= GRUB_NTFS_RF_BLNK;
  306. else
  307. ctx->flags &= ~GRUB_NTFS_RF_BLNK;
  308. ctx->cur_run = run;
  309. return 0;
  310. }
  311. static grub_disk_addr_t
  312. grub_ntfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t block)
  313. {
  314. struct grub_ntfs_rlst *ctx;
  315. ctx = (struct grub_ntfs_rlst *) node;
  316. if (block >= ctx->next_vcn)
  317. {
  318. if (grub_ntfs_read_run_list (ctx))
  319. return -1;
  320. return ctx->curr_lcn;
  321. }
  322. else
  323. return (ctx->flags & GRUB_NTFS_RF_BLNK) ? 0 : (block -
  324. ctx->curr_vcn + ctx->curr_lcn);
  325. }
  326. static grub_err_t
  327. read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
  328. grub_disk_addr_t ofs, grub_size_t len, int cached,
  329. grub_disk_read_hook_t read_hook, void *read_hook_data)
  330. {
  331. struct grub_ntfs_rlst cc, *ctx;
  332. if (len == 0)
  333. return 0;
  334. grub_memset (&cc, 0, sizeof (cc));
  335. ctx = &cc;
  336. ctx->attr = at;
  337. ctx->comp.log_spc = at->mft->data->log_spc;
  338. ctx->comp.disk = at->mft->data->disk;
  339. if (read_hook == grub_file_progress_hook)
  340. ctx->file = read_hook_data;
  341. if (pa[8] == 0)
  342. {
  343. if (ofs + len > u32at (pa, 0x10))
  344. return grub_error (GRUB_ERR_BAD_FS, "read out of range");
  345. grub_memcpy (dest, pa + u32at (pa, 0x14) + ofs, len);
  346. return 0;
  347. }
  348. ctx->cur_run = pa + u16at (pa, 0x20);
  349. ctx->next_vcn = u32at (pa, 0x10);
  350. ctx->curr_lcn = 0;
  351. if ((pa[0xC] & GRUB_NTFS_FLAG_COMPRESSED)
  352. && !(at->flags & GRUB_NTFS_AF_GPOS))
  353. {
  354. if (!cached)
  355. return grub_error (GRUB_ERR_BAD_FS, "attribute can\'t be compressed");
  356. return (grub_ntfscomp_func) ? grub_ntfscomp_func (dest, ofs, len, ctx)
  357. : grub_error (GRUB_ERR_BAD_FS, N_("module `%s' isn't loaded"),
  358. "ntfscomp");
  359. }
  360. ctx->target_vcn = ofs >> (GRUB_NTFS_BLK_SHR + ctx->comp.log_spc);
  361. while (ctx->next_vcn <= ctx->target_vcn)
  362. {
  363. if (grub_ntfs_read_run_list (ctx))
  364. return grub_errno;
  365. }
  366. if (at->flags & GRUB_NTFS_AF_GPOS)
  367. {
  368. grub_disk_addr_t st0, st1;
  369. grub_uint64_t m;
  370. m = (ofs >> GRUB_NTFS_BLK_SHR) & ((1 << ctx->comp.log_spc) - 1);
  371. st0 =
  372. ((ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc) + m;
  373. st1 = st0 + 1;
  374. if (st1 ==
  375. (ctx->next_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc)
  376. {
  377. if (grub_ntfs_read_run_list (ctx))
  378. return grub_errno;
  379. st1 = ctx->curr_lcn << ctx->comp.log_spc;
  380. }
  381. grub_set_unaligned32 (dest, grub_cpu_to_le32 (st0));
  382. grub_set_unaligned32 (dest + 4, grub_cpu_to_le32 (st1));
  383. return 0;
  384. }
  385. grub_fshelp_read_file (ctx->comp.disk, (grub_fshelp_node_t) ctx,
  386. read_hook, read_hook_data, ofs, len,
  387. (char *) dest,
  388. grub_ntfs_read_block, ofs + len,
  389. ctx->comp.log_spc, 0);
  390. return grub_errno;
  391. }
  392. static grub_err_t
  393. read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest, grub_disk_addr_t ofs,
  394. grub_size_t len, int cached,
  395. grub_disk_read_hook_t read_hook, void *read_hook_data)
  396. {
  397. grub_uint8_t *save_cur;
  398. grub_uint8_t attr;
  399. grub_uint8_t *pp;
  400. grub_err_t ret;
  401. save_cur = at->attr_cur;
  402. at->attr_nxt = at->attr_cur;
  403. attr = *at->attr_nxt;
  404. if (at->flags & GRUB_NTFS_AF_ALST)
  405. {
  406. grub_uint8_t *pa;
  407. grub_disk_addr_t vcn;
  408. /* If compression is possible make sure that we include possible
  409. compressed block size. */
  410. if (GRUB_NTFS_LOG_COM_SEC >= at->mft->data->log_spc)
  411. vcn = ((ofs >> GRUB_NTFS_COM_LOG_LEN)
  412. << (GRUB_NTFS_LOG_COM_SEC - at->mft->data->log_spc)) & ~0xFULL;
  413. else
  414. vcn = ofs >> (at->mft->data->log_spc + GRUB_NTFS_BLK_SHR);
  415. pa = at->attr_nxt + u16at (at->attr_nxt, 4);
  416. while (pa < at->attr_end)
  417. {
  418. if (*pa != attr)
  419. break;
  420. if (u32at (pa, 8) > vcn)
  421. break;
  422. at->attr_nxt = pa;
  423. pa += u16at (pa, 4);
  424. }
  425. }
  426. pp = find_attr (at, attr);
  427. if (pp)
  428. ret = read_data (at, pp, dest, ofs, len, cached,
  429. read_hook, read_hook_data);
  430. else
  431. ret =
  432. (grub_errno) ? grub_errno : grub_error (GRUB_ERR_BAD_FS,
  433. "attribute not found");
  434. at->attr_cur = save_cur;
  435. return ret;
  436. }
  437. static grub_err_t
  438. read_mft (struct grub_ntfs_data *data, grub_uint8_t *buf, grub_uint64_t mftno)
  439. {
  440. if (read_attr
  441. (&data->mmft.attr, buf, mftno * ((grub_disk_addr_t) data->mft_size << GRUB_NTFS_BLK_SHR),
  442. data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
  443. return grub_error (GRUB_ERR_BAD_FS, "read MFT 0x%llx fails", (unsigned long long) mftno);
  444. return fixup (buf, data->mft_size, (const grub_uint8_t *) "FILE");
  445. }
  446. static grub_err_t
  447. init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
  448. {
  449. unsigned short flag;
  450. mft->inode_read = 1;
  451. mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  452. if (mft->buf == NULL)
  453. return grub_errno;
  454. if (read_mft (mft->data, mft->buf, mftno))
  455. return grub_errno;
  456. flag = u16at (mft->buf, 0x16);
  457. if ((flag & 1) == 0)
  458. return grub_error (GRUB_ERR_BAD_FS, "MFT 0x%llx is not in use",
  459. (unsigned long long) mftno);
  460. if ((flag & 2) == 0)
  461. {
  462. grub_uint8_t *pa;
  463. pa = locate_attr (&mft->attr, mft, GRUB_NTFS_AT_DATA);
  464. if (pa == NULL)
  465. return grub_error (GRUB_ERR_BAD_FS, "no $DATA in MFT 0x%llx",
  466. (unsigned long long) mftno);
  467. if (!pa[8])
  468. mft->size = u32at (pa, 0x10);
  469. else
  470. mft->size = u64at (pa, 0x30);
  471. if ((mft->attr.flags & GRUB_NTFS_AF_ALST) == 0)
  472. mft->attr.attr_end = 0; /* Don't jump to attribute list */
  473. }
  474. else
  475. init_attr (&mft->attr, mft);
  476. return 0;
  477. }
  478. static void
  479. free_file (struct grub_ntfs_file *mft)
  480. {
  481. free_attr (&mft->attr);
  482. grub_free (mft->buf);
  483. }
  484. static char *
  485. get_utf8 (grub_uint8_t *in, grub_size_t len)
  486. {
  487. grub_uint8_t *buf;
  488. grub_uint16_t *tmp;
  489. grub_size_t i;
  490. buf = grub_malloc (len * GRUB_MAX_UTF8_PER_UTF16 + 1);
  491. tmp = grub_malloc (len * sizeof (tmp[0]));
  492. if (!buf || !tmp)
  493. {
  494. grub_free (buf);
  495. grub_free (tmp);
  496. return NULL;
  497. }
  498. for (i = 0; i < len; i++)
  499. tmp[i] = grub_le_to_cpu16 (grub_get_unaligned16 (in + 2 * i));
  500. *grub_utf16_to_utf8 (buf, tmp, len) = '\0';
  501. grub_free (tmp);
  502. return (char *) buf;
  503. }
  504. static int
  505. list_file (struct grub_ntfs_file *diro, grub_uint8_t *pos,
  506. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  507. {
  508. grub_uint8_t *np;
  509. int ns;
  510. while (1)
  511. {
  512. grub_uint8_t namespace;
  513. char *ustr;
  514. if (pos[0xC] & 2) /* end signature */
  515. break;
  516. np = pos + 0x50;
  517. ns = *(np++);
  518. namespace = *(np++);
  519. /*
  520. * Ignore files in DOS namespace, as they will reappear as Win32
  521. * names.
  522. */
  523. if ((ns) && (namespace != 2))
  524. {
  525. enum grub_fshelp_filetype type;
  526. struct grub_ntfs_file *fdiro;
  527. grub_uint32_t attr;
  528. attr = u32at (pos, 0x48);
  529. if (attr & GRUB_NTFS_ATTR_REPARSE)
  530. type = GRUB_FSHELP_SYMLINK;
  531. else if (attr & GRUB_NTFS_ATTR_DIRECTORY)
  532. type = GRUB_FSHELP_DIR;
  533. else
  534. type = GRUB_FSHELP_REG;
  535. fdiro = grub_zalloc (sizeof (struct grub_ntfs_file));
  536. if (!fdiro)
  537. return 0;
  538. fdiro->data = diro->data;
  539. fdiro->ino = u64at (pos, 0) & 0xffffffffffffULL;
  540. fdiro->mtime = u64at (pos, 0x20);
  541. ustr = get_utf8 (np, ns);
  542. if (ustr == NULL)
  543. {
  544. grub_free (fdiro);
  545. return 0;
  546. }
  547. if (namespace)
  548. type |= GRUB_FSHELP_CASE_INSENSITIVE;
  549. if (hook (ustr, type, fdiro, hook_data))
  550. {
  551. grub_free (ustr);
  552. return 1;
  553. }
  554. grub_free (ustr);
  555. }
  556. pos += u16at (pos, 8);
  557. }
  558. return 0;
  559. }
  560. struct symlink_descriptor
  561. {
  562. grub_uint32_t type;
  563. grub_uint32_t total_len;
  564. grub_uint16_t off1;
  565. grub_uint16_t len1;
  566. grub_uint16_t off2;
  567. grub_uint16_t len2;
  568. } GRUB_PACKED;
  569. static char *
  570. grub_ntfs_read_symlink (grub_fshelp_node_t node)
  571. {
  572. struct grub_ntfs_file *mft;
  573. struct symlink_descriptor symdesc;
  574. grub_err_t err;
  575. grub_uint8_t *buf16;
  576. char *buf, *end;
  577. grub_size_t len;
  578. grub_uint8_t *pa;
  579. grub_size_t off;
  580. mft = (struct grub_ntfs_file *) node;
  581. mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  582. if (mft->buf == NULL)
  583. return NULL;
  584. if (read_mft (mft->data, mft->buf, mft->ino))
  585. return NULL;
  586. pa = locate_attr (&mft->attr, mft, GRUB_NTFS_AT_SYMLINK);
  587. if (pa == NULL)
  588. {
  589. grub_error (GRUB_ERR_BAD_FS, "no $SYMLINK in MFT 0x%llx",
  590. (unsigned long long) mft->ino);
  591. return NULL;
  592. }
  593. err = read_attr (&mft->attr, (grub_uint8_t *) &symdesc, 0,
  594. sizeof (struct symlink_descriptor), 1, 0, 0);
  595. if (err)
  596. return NULL;
  597. switch (grub_cpu_to_le32 (symdesc.type))
  598. {
  599. case 0xa000000c:
  600. off = (sizeof (struct symlink_descriptor) + 4
  601. + grub_cpu_to_le32 (symdesc.off1));
  602. len = grub_cpu_to_le32 (symdesc.len1);
  603. break;
  604. case 0xa0000003:
  605. off = (sizeof (struct symlink_descriptor)
  606. + grub_cpu_to_le32 (symdesc.off1));
  607. len = grub_cpu_to_le32 (symdesc.len1);
  608. break;
  609. default:
  610. grub_error (GRUB_ERR_BAD_FS, "symlink type invalid (%x)",
  611. grub_cpu_to_le32 (symdesc.type));
  612. return NULL;
  613. }
  614. buf16 = grub_malloc (len);
  615. if (!buf16)
  616. return NULL;
  617. err = read_attr (&mft->attr, buf16, off, len, 1, 0, 0);
  618. if (err)
  619. return NULL;
  620. buf = get_utf8 (buf16, len / 2);
  621. if (!buf)
  622. {
  623. grub_free (buf16);
  624. return NULL;
  625. }
  626. grub_free (buf16);
  627. for (end = buf; *end; end++)
  628. if (*end == '\\')
  629. *end = '/';
  630. /* Split the sequence to avoid GCC thinking that this is a trigraph. */
  631. if (grub_memcmp (buf, "/?" "?/", 4) == 0 && buf[5] == ':' && buf[6] == '/'
  632. && grub_isalpha (buf[4]))
  633. {
  634. grub_memmove (buf, buf + 6, end - buf + 1 - 6);
  635. end -= 6;
  636. }
  637. return buf;
  638. }
  639. static int
  640. grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
  641. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  642. {
  643. grub_uint8_t *bitmap;
  644. struct grub_ntfs_attr attr, *at;
  645. grub_uint8_t *cur_pos, *indx, *bmp;
  646. int ret = 0;
  647. grub_size_t bitmap_len;
  648. struct grub_ntfs_file *mft;
  649. mft = (struct grub_ntfs_file *) dir;
  650. if (!mft->inode_read)
  651. {
  652. if (init_file (mft, mft->ino))
  653. return 0;
  654. }
  655. indx = NULL;
  656. bmp = NULL;
  657. at = &attr;
  658. init_attr (at, mft);
  659. while (1)
  660. {
  661. cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ROOT);
  662. if (cur_pos == NULL)
  663. {
  664. grub_error (GRUB_ERR_BAD_FS, "no $INDEX_ROOT");
  665. goto done;
  666. }
  667. /* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */
  668. if ((u32at (cur_pos, 8) != 0x180400) ||
  669. (u32at (cur_pos, 0x18) != 0x490024) ||
  670. (u32at (cur_pos, 0x1C) != 0x300033))
  671. continue;
  672. cur_pos += u16at (cur_pos, 0x14);
  673. if (*cur_pos != 0x30) /* Not filename index */
  674. continue;
  675. break;
  676. }
  677. cur_pos += 0x10; /* Skip index root */
  678. ret = list_file (mft, cur_pos + u16at (cur_pos, 0), hook, hook_data);
  679. if (ret)
  680. goto done;
  681. bitmap = NULL;
  682. bitmap_len = 0;
  683. free_attr (at);
  684. init_attr (at, mft);
  685. while ((cur_pos = find_attr (at, GRUB_NTFS_AT_BITMAP)) != NULL)
  686. {
  687. int ofs;
  688. ofs = cur_pos[0xA];
  689. /* Namelen=4, Name="$I30" */
  690. if ((cur_pos[9] == 4) &&
  691. (u32at (cur_pos, ofs) == 0x490024) &&
  692. (u32at (cur_pos, ofs + 4) == 0x300033))
  693. {
  694. int is_resident = (cur_pos[8] == 0);
  695. bitmap_len = ((is_resident) ? u32at (cur_pos, 0x10) :
  696. u32at (cur_pos, 0x28));
  697. bmp = grub_malloc (bitmap_len);
  698. if (bmp == NULL)
  699. goto done;
  700. if (is_resident)
  701. {
  702. grub_memcpy (bmp, cur_pos + u16at (cur_pos, 0x14),
  703. bitmap_len);
  704. }
  705. else
  706. {
  707. if (read_data (at, cur_pos, bmp, 0, bitmap_len, 0, 0, 0))
  708. {
  709. grub_error (GRUB_ERR_BAD_FS,
  710. "fails to read non-resident $BITMAP");
  711. goto done;
  712. }
  713. bitmap_len = u32at (cur_pos, 0x30);
  714. }
  715. bitmap = bmp;
  716. break;
  717. }
  718. }
  719. free_attr (at);
  720. cur_pos = locate_attr (at, mft, GRUB_NTFS_AT_INDEX_ALLOCATION);
  721. while (cur_pos != NULL)
  722. {
  723. /* Non-resident, Namelen=4, Offset=0x40, Flags=0, Name="$I30" */
  724. if ((u32at (cur_pos, 8) == 0x400401) &&
  725. (u32at (cur_pos, 0x40) == 0x490024) &&
  726. (u32at (cur_pos, 0x44) == 0x300033))
  727. break;
  728. cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ALLOCATION);
  729. }
  730. if ((!cur_pos) && (bitmap))
  731. {
  732. grub_error (GRUB_ERR_BAD_FS, "$BITMAP without $INDEX_ALLOCATION");
  733. goto done;
  734. }
  735. if (bitmap)
  736. {
  737. grub_disk_addr_t i;
  738. grub_uint8_t v;
  739. indx = grub_malloc (mft->data->idx_size << GRUB_NTFS_BLK_SHR);
  740. if (indx == NULL)
  741. goto done;
  742. v = 1;
  743. for (i = 0; i < (grub_disk_addr_t)bitmap_len * 8; i++)
  744. {
  745. if (*bitmap & v)
  746. {
  747. if ((read_attr
  748. (at, indx, i * (mft->data->idx_size << GRUB_NTFS_BLK_SHR),
  749. (mft->data->idx_size << GRUB_NTFS_BLK_SHR), 0, 0, 0))
  750. || (fixup (indx, mft->data->idx_size,
  751. (const grub_uint8_t *) "INDX")))
  752. goto done;
  753. ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)],
  754. hook, hook_data);
  755. if (ret)
  756. goto done;
  757. }
  758. v <<= 1;
  759. if (!v)
  760. {
  761. v = 1;
  762. bitmap++;
  763. }
  764. }
  765. }
  766. done:
  767. free_attr (at);
  768. grub_free (indx);
  769. grub_free (bmp);
  770. return ret;
  771. }
  772. static struct grub_ntfs_data *
  773. grub_ntfs_mount (grub_disk_t disk)
  774. {
  775. struct grub_ntfs_bpb bpb;
  776. struct grub_ntfs_data *data = 0;
  777. grub_uint32_t spc;
  778. if (!disk)
  779. goto fail;
  780. data = (struct grub_ntfs_data *) grub_zalloc (sizeof (*data));
  781. if (!data)
  782. goto fail;
  783. data->disk = disk;
  784. /* Read the BPB. */
  785. if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
  786. goto fail;
  787. if (grub_memcmp ((char *) &bpb.oem_name, "NTFS", 4) != 0
  788. || bpb.sectors_per_cluster == 0
  789. || (bpb.sectors_per_cluster & (bpb.sectors_per_cluster - 1)) != 0
  790. || bpb.bytes_per_sector == 0
  791. || (bpb.bytes_per_sector & (bpb.bytes_per_sector - 1)) != 0)
  792. goto fail;
  793. spc = (((grub_uint32_t) bpb.sectors_per_cluster
  794. * (grub_uint32_t) grub_le_to_cpu16 (bpb.bytes_per_sector))
  795. >> GRUB_NTFS_BLK_SHR);
  796. if (spc == 0)
  797. goto fail;
  798. for (data->log_spc = 0; (1U << data->log_spc) < spc; data->log_spc++);
  799. if (bpb.clusters_per_mft > 0)
  800. data->mft_size = ((grub_disk_addr_t) bpb.clusters_per_mft) << data->log_spc;
  801. else if (-bpb.clusters_per_mft < GRUB_NTFS_BLK_SHR || -bpb.clusters_per_mft >= 31)
  802. goto fail;
  803. else
  804. data->mft_size = 1ULL << (-bpb.clusters_per_mft - GRUB_NTFS_BLK_SHR);
  805. if (bpb.clusters_per_index > 0)
  806. data->idx_size = (((grub_disk_addr_t) bpb.clusters_per_index)
  807. << data->log_spc);
  808. else if (-bpb.clusters_per_index < GRUB_NTFS_BLK_SHR || -bpb.clusters_per_index >= 31)
  809. goto fail;
  810. else
  811. data->idx_size = 1ULL << (-bpb.clusters_per_index - GRUB_NTFS_BLK_SHR);
  812. data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) << data->log_spc;
  813. if ((data->mft_size > GRUB_NTFS_MAX_MFT) || (data->idx_size > GRUB_NTFS_MAX_IDX))
  814. goto fail;
  815. data->mmft.data = data;
  816. data->cmft.data = data;
  817. data->mmft.buf = grub_malloc (data->mft_size << GRUB_NTFS_BLK_SHR);
  818. if (!data->mmft.buf)
  819. goto fail;
  820. if (grub_disk_read
  821. (disk, data->mft_start, 0, data->mft_size << GRUB_NTFS_BLK_SHR, data->mmft.buf))
  822. goto fail;
  823. data->uuid = grub_le_to_cpu64 (bpb.num_serial);
  824. if (fixup (data->mmft.buf, data->mft_size, (const grub_uint8_t *) "FILE"))
  825. goto fail;
  826. if (!locate_attr (&data->mmft.attr, &data->mmft, GRUB_NTFS_AT_DATA))
  827. goto fail;
  828. if (init_file (&data->cmft, GRUB_NTFS_FILE_ROOT))
  829. goto fail;
  830. return data;
  831. fail:
  832. grub_error (GRUB_ERR_BAD_FS, "not an ntfs filesystem");
  833. if (data)
  834. {
  835. free_file (&data->mmft);
  836. free_file (&data->cmft);
  837. grub_free (data);
  838. }
  839. return 0;
  840. }
  841. /* Context for grub_ntfs_dir. */
  842. struct grub_ntfs_dir_ctx
  843. {
  844. grub_fs_dir_hook_t hook;
  845. void *hook_data;
  846. };
  847. /* Helper for grub_ntfs_dir. */
  848. static int
  849. grub_ntfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  850. grub_fshelp_node_t node, void *data)
  851. {
  852. struct grub_ntfs_dir_ctx *ctx = data;
  853. struct grub_dirhook_info info;
  854. grub_memset (&info, 0, sizeof (info));
  855. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  856. info.mtimeset = 1;
  857. info.mtime = grub_divmod64 (node->mtime, 10000000, 0)
  858. - 86400ULL * 365 * (1970 - 1601)
  859. - 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
  860. grub_free (node);
  861. return ctx->hook (filename, &info, ctx->hook_data);
  862. }
  863. static grub_err_t
  864. grub_ntfs_dir (grub_device_t device, const char *path,
  865. grub_fs_dir_hook_t hook, void *hook_data)
  866. {
  867. struct grub_ntfs_dir_ctx ctx = { hook, hook_data };
  868. struct grub_ntfs_data *data = 0;
  869. struct grub_fshelp_node *fdiro = 0;
  870. grub_dl_ref (my_mod);
  871. data = grub_ntfs_mount (device->disk);
  872. if (!data)
  873. goto fail;
  874. grub_fshelp_find_file (path, &data->cmft, &fdiro, grub_ntfs_iterate_dir,
  875. grub_ntfs_read_symlink, GRUB_FSHELP_DIR);
  876. if (grub_errno)
  877. goto fail;
  878. grub_ntfs_iterate_dir (fdiro, grub_ntfs_dir_iter, &ctx);
  879. fail:
  880. if ((fdiro) && (fdiro != &data->cmft))
  881. {
  882. free_file (fdiro);
  883. grub_free (fdiro);
  884. }
  885. if (data)
  886. {
  887. free_file (&data->mmft);
  888. free_file (&data->cmft);
  889. grub_free (data);
  890. }
  891. grub_dl_unref (my_mod);
  892. return grub_errno;
  893. }
  894. static grub_err_t
  895. grub_ntfs_open (grub_file_t file, const char *name)
  896. {
  897. struct grub_ntfs_data *data = 0;
  898. struct grub_fshelp_node *mft = 0;
  899. grub_dl_ref (my_mod);
  900. data = grub_ntfs_mount (file->device->disk);
  901. if (!data)
  902. goto fail;
  903. grub_fshelp_find_file (name, &data->cmft, &mft, grub_ntfs_iterate_dir,
  904. grub_ntfs_read_symlink, GRUB_FSHELP_REG);
  905. if (grub_errno)
  906. goto fail;
  907. if (mft != &data->cmft)
  908. {
  909. free_file (&data->cmft);
  910. grub_memcpy (&data->cmft, mft, sizeof (*mft));
  911. grub_free (mft);
  912. if (!data->cmft.inode_read)
  913. {
  914. if (init_file (&data->cmft, data->cmft.ino))
  915. goto fail;
  916. }
  917. }
  918. file->size = data->cmft.size;
  919. file->data = data;
  920. file->offset = 0;
  921. return 0;
  922. fail:
  923. if (data)
  924. {
  925. free_file (&data->mmft);
  926. free_file (&data->cmft);
  927. grub_free (data);
  928. }
  929. grub_dl_unref (my_mod);
  930. return grub_errno;
  931. }
  932. static grub_ssize_t
  933. grub_ntfs_read (grub_file_t file, char *buf, grub_size_t len)
  934. {
  935. struct grub_ntfs_file *mft;
  936. mft = &((struct grub_ntfs_data *) file->data)->cmft;
  937. if (file->read_hook)
  938. mft->attr.save_pos = 1;
  939. read_attr (&mft->attr, (grub_uint8_t *) buf, file->offset, len, 1,
  940. file->read_hook, file->read_hook_data);
  941. return (grub_errno) ? -1 : (grub_ssize_t) len;
  942. }
  943. static grub_err_t
  944. grub_ntfs_close (grub_file_t file)
  945. {
  946. struct grub_ntfs_data *data;
  947. data = file->data;
  948. if (data)
  949. {
  950. free_file (&data->mmft);
  951. free_file (&data->cmft);
  952. grub_free (data);
  953. }
  954. grub_dl_unref (my_mod);
  955. return grub_errno;
  956. }
  957. static grub_err_t
  958. grub_ntfs_label (grub_device_t device, char **label)
  959. {
  960. struct grub_ntfs_data *data = 0;
  961. struct grub_fshelp_node *mft = 0;
  962. grub_uint8_t *pa;
  963. grub_dl_ref (my_mod);
  964. *label = 0;
  965. data = grub_ntfs_mount (device->disk);
  966. if (!data)
  967. goto fail;
  968. grub_fshelp_find_file ("/$Volume", &data->cmft, &mft, grub_ntfs_iterate_dir,
  969. 0, GRUB_FSHELP_REG);
  970. if (grub_errno)
  971. goto fail;
  972. if (!mft->inode_read)
  973. {
  974. mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
  975. if (mft->buf == NULL)
  976. goto fail;
  977. if (read_mft (mft->data, mft->buf, mft->ino))
  978. goto fail;
  979. }
  980. init_attr (&mft->attr, mft);
  981. pa = find_attr (&mft->attr, GRUB_NTFS_AT_VOLUME_NAME);
  982. if ((pa) && (pa[8] == 0) && (u32at (pa, 0x10)))
  983. {
  984. int len;
  985. len = u32at (pa, 0x10) / 2;
  986. pa += u16at (pa, 0x14);
  987. *label = get_utf8 (pa, len);
  988. }
  989. fail:
  990. if ((mft) && (mft != &data->cmft))
  991. {
  992. free_file (mft);
  993. grub_free (mft);
  994. }
  995. if (data)
  996. {
  997. free_file (&data->mmft);
  998. free_file (&data->cmft);
  999. grub_free (data);
  1000. }
  1001. grub_dl_unref (my_mod);
  1002. return grub_errno;
  1003. }
  1004. static grub_err_t
  1005. grub_ntfs_uuid (grub_device_t device, char **uuid)
  1006. {
  1007. struct grub_ntfs_data *data;
  1008. grub_disk_t disk = device->disk;
  1009. grub_dl_ref (my_mod);
  1010. data = grub_ntfs_mount (disk);
  1011. if (data)
  1012. {
  1013. char *ptr;
  1014. *uuid = grub_xasprintf ("%016llx", (unsigned long long) data->uuid);
  1015. if (*uuid)
  1016. for (ptr = *uuid; *ptr; ptr++)
  1017. *ptr = grub_toupper (*ptr);
  1018. free_file (&data->mmft);
  1019. free_file (&data->cmft);
  1020. grub_free (data);
  1021. }
  1022. else
  1023. *uuid = NULL;
  1024. grub_dl_unref (my_mod);
  1025. return grub_errno;
  1026. }
  1027. static struct grub_fs grub_ntfs_fs =
  1028. {
  1029. .name = "ntfs",
  1030. .dir = grub_ntfs_dir,
  1031. .open = grub_ntfs_open,
  1032. .read = grub_ntfs_read,
  1033. .close = grub_ntfs_close,
  1034. .label = grub_ntfs_label,
  1035. .uuid = grub_ntfs_uuid,
  1036. #ifdef GRUB_UTIL
  1037. .reserved_first_sector = 1,
  1038. .blocklist_install = 1,
  1039. #endif
  1040. .next = 0
  1041. };
  1042. GRUB_MOD_INIT (ntfs)
  1043. {
  1044. grub_fs_register (&grub_ntfs_fs);
  1045. my_mod = mod;
  1046. }
  1047. GRUB_MOD_FINI (ntfs)
  1048. {
  1049. grub_fs_unregister (&grub_ntfs_fs);
  1050. }