ntfs.c 26 KB

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