ntfs.c 42 KB

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