Remove svn:eol-style property for compatibility with Autoconf 2.62
[grub2/bean.git] / fs / ntfs.c
blob3363eb02bca78ff30676c27899d57ef8a324cdc0
1 /* ntfs.c - NTFS filesystem */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007,2008 Free Software Foundation, Inc.
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.
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.
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/>.
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>
28 #ifndef GRUB_UTIL
29 static grub_dl_t my_mod;
30 #endif
32 ntfscomp_func_t grub_ntfscomp_func;
34 static grub_err_t
35 fixup (struct grub_ntfs_data *data, char *buf, int len, char *magic)
37 int ss;
38 char *pu;
39 grub_uint16_t us;
41 if (grub_memcmp (buf, magic, 4))
42 return grub_error (GRUB_ERR_BAD_FS, "%s label not found", magic);
44 ss = u16at (buf, 6) - 1;
45 if (ss * (int) data->blocksize != len * GRUB_DISK_SECTOR_SIZE)
46 return grub_error (GRUB_ERR_BAD_FS, "Size not match",
47 ss * (int) data->blocksize,
48 len * GRUB_DISK_SECTOR_SIZE);
49 pu = buf + u16at (buf, 4);
50 us = u16at (pu, 0);
51 buf -= 2;
52 while (ss > 0)
54 buf += data->blocksize;
55 pu += 2;
56 if (u16at (buf, 0) != us)
57 return grub_error (GRUB_ERR_BAD_FS, "Fixup signature not match");
58 v16at (buf, 0) = v16at (pu, 0);
59 ss--;
62 return 0;
65 static grub_err_t read_mft (struct grub_ntfs_data *data, char *buf,
66 grub_uint32_t mftno);
67 static grub_err_t read_attr (struct grub_ntfs_attr *at, char *dest,
68 grub_uint32_t ofs, grub_uint32_t len,
69 int cached,
70 void
71 NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t
72 sector,
73 unsigned offset,
74 unsigned length));
76 static grub_err_t read_data (struct grub_ntfs_attr *at, char *pa, char *dest,
77 grub_uint32_t ofs, grub_uint32_t len,
78 int cached,
79 void
80 NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t
81 sector,
82 unsigned offset,
83 unsigned length));
85 static void
86 init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft)
88 at->mft = mft;
89 at->flags = (mft == &mft->data->mmft) ? AF_MMFT : 0;
90 at->attr_nxt = mft->buf + u16at (mft->buf, 0x14);
91 at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL;
94 static void
95 free_attr (struct grub_ntfs_attr *at)
97 grub_free (at->emft_buf);
98 grub_free (at->edat_buf);
99 grub_free (at->sbuf);
102 static char *
103 find_attr (struct grub_ntfs_attr *at, unsigned char attr)
105 if (at->flags & AF_ALST)
107 retry:
108 while (at->attr_nxt < at->attr_end)
110 at->attr_cur = at->attr_nxt;
111 at->attr_nxt += u16at (at->attr_cur, 4);
112 if (((unsigned char) *at->attr_cur == attr) || (attr == 0))
114 char *new_pos;
116 if (at->flags & AF_MMFT)
118 if ((grub_disk_read
119 (at->mft->data->disk, v32at (at->attr_cur, 0x10), 0,
120 512, at->emft_buf))
122 (grub_disk_read
123 (at->mft->data->disk, v32at (at->attr_cur, 0x14), 0,
124 512, at->emft_buf + 512)))
125 return NULL;
127 if (fixup
128 (at->mft->data, at->emft_buf, at->mft->data->mft_size,
129 "FILE"))
130 return NULL;
132 else
134 if (read_mft (at->mft->data, at->emft_buf,
135 u32at (at->attr_cur, 0x10)))
136 return NULL;
139 new_pos = &at->emft_buf[u16at (at->emft_buf, 0x14)];
140 while ((unsigned char) *new_pos != 0xFF)
142 if (((unsigned char) *new_pos ==
143 (unsigned char) *at->attr_cur)
144 && (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18)))
146 return new_pos;
148 new_pos += u16at (new_pos, 4);
150 grub_error (GRUB_ERR_BAD_FS,
151 "Can\'t find 0x%X in attribute list",
152 (unsigned char) *at->attr_cur);
153 return NULL;
156 return NULL;
158 at->attr_cur = at->attr_nxt;
159 while ((unsigned char) *at->attr_cur != 0xFF)
161 at->attr_nxt += u16at (at->attr_cur, 4);
162 if ((unsigned char) *at->attr_cur == AT_ATTRIBUTE_LIST)
163 at->attr_end = at->attr_cur;
164 if (((unsigned char) *at->attr_cur == attr) || (attr == 0))
165 return at->attr_cur;
166 at->attr_cur = at->attr_nxt;
168 if (at->attr_end)
170 char *pa;
172 at->emft_buf = grub_malloc (at->mft->data->mft_size << BLK_SHR);
173 if (at->emft_buf == NULL)
174 return NULL;
176 pa = at->attr_end;
177 if (pa[8])
179 int n;
181 n = ((u32at (pa, 0x30) + GRUB_DISK_SECTOR_SIZE - 1)
182 & (~(GRUB_DISK_SECTOR_SIZE - 1)));
183 at->attr_cur = at->attr_end;
184 at->edat_buf = grub_malloc (n);
185 if (!at->edat_buf)
186 return NULL;
187 if (read_data (at, pa, at->edat_buf, 0, n, 0, 0))
189 grub_error (GRUB_ERR_BAD_FS,
190 "Fail to read non-resident attribute list");
191 return NULL;
193 at->attr_nxt = at->edat_buf;
194 at->attr_end = at->edat_buf + u32at (pa, 0x30);
196 else
198 at->attr_nxt = at->attr_end + u16at (pa, 0x14);
199 at->attr_end = at->attr_end + u32at (pa, 4);
201 at->flags |= AF_ALST;
202 while (at->attr_nxt < at->attr_end)
204 if (((unsigned char) *at->attr_nxt == attr) || (attr == 0))
205 break;
206 at->attr_nxt += u16at (at->attr_nxt, 4);
208 if (at->attr_nxt >= at->attr_end)
209 return NULL;
211 if ((at->flags & AF_MMFT) && (attr == AT_DATA))
213 at->flags |= AF_GPOS;
214 at->attr_cur = at->attr_nxt;
215 pa = at->attr_cur;
216 v32at (pa, 0x10) = at->mft->data->mft_start;
217 v32at (pa, 0x14) = at->mft->data->mft_start + 1;
218 pa = at->attr_nxt + u16at (pa, 4);
219 while (pa < at->attr_end)
221 if ((unsigned char) *pa != attr)
222 break;
223 if (read_attr
224 (at, pa + 0x10,
225 u32at (pa, 0x10) * (at->mft->data->mft_size << BLK_SHR),
226 at->mft->data->mft_size << BLK_SHR, 0, 0))
227 return NULL;
228 pa += u16at (pa, 4);
230 at->attr_nxt = at->attr_cur;
231 at->flags &= ~AF_GPOS;
233 goto retry;
235 return NULL;
238 static char *
239 locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft,
240 unsigned char attr)
242 char *pa;
244 init_attr (at, mft);
245 if ((pa = find_attr (at, attr)) == NULL)
246 return NULL;
247 if ((at->flags & AF_ALST) == 0)
249 while (1)
251 if ((pa = find_attr (at, attr)) == NULL)
252 break;
253 if (at->flags & AF_ALST)
254 return pa;
256 grub_errno = GRUB_ERR_NONE;
257 free_attr (at);
258 init_attr (at, mft);
259 pa = find_attr (at, attr);
261 return pa;
264 static char *
265 read_run_data (char *run, int nn, grub_uint32_t * val, int sig)
267 grub_uint32_t r, v;
269 r = 0;
270 v = 1;
272 while (nn--)
274 r += v * (*(unsigned char *) (run++));
275 v <<= 8;
278 if ((sig) && (r & (v >> 1)))
279 r -= v;
281 *val = r;
282 return run;
285 grub_err_t
286 grub_ntfs_read_run_list (struct grub_ntfs_rlst * ctx)
288 int c1, c2;
289 grub_uint32_t val;
290 char *run;
292 run = ctx->cur_run;
293 retry:
294 c1 = ((unsigned char) (*run) & 0xF);
295 c2 = ((unsigned char) (*run) >> 4);
296 if (!c1)
298 if ((ctx->attr) && (ctx->attr->flags & AF_ALST))
300 void NESTED_FUNC_ATTR (*save_hook) (grub_disk_addr_t sector,
301 unsigned offset,
302 unsigned length);
304 save_hook = ctx->comp.disk->read_hook;
305 ctx->comp.disk->read_hook = 0;
306 run = find_attr (ctx->attr, (unsigned char) *ctx->attr->attr_cur);
307 ctx->comp.disk->read_hook = save_hook;
308 if (run)
310 if (run[8] == 0)
311 return grub_error (GRUB_ERR_BAD_FS,
312 "$DATA should be non-resident");
314 run += u16at (run, 0x20);
315 ctx->curr_lcn = 0;
316 goto retry;
319 return grub_error (GRUB_ERR_BAD_FS, "Run list overflown");
321 run = read_run_data (run + 1, c1, &val, 0); /* length of current VCN */
322 ctx->curr_vcn = ctx->next_vcn;
323 ctx->next_vcn += val;
324 run = read_run_data (run, c2, &val, 1); /* offset to previous LCN */
325 ctx->curr_lcn += val;
326 if (val == 0)
327 ctx->flags |= RF_BLNK;
328 else
329 ctx->flags &= ~RF_BLNK;
330 ctx->cur_run = run;
331 return 0;
334 static grub_disk_addr_t
335 grub_ntfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t block)
337 struct grub_ntfs_rlst *ctx;
339 ctx = (struct grub_ntfs_rlst *) node;
340 if ((grub_uint32_t) block >= ctx->next_vcn)
342 if (grub_ntfs_read_run_list (ctx))
343 return -1;
344 return ctx->curr_lcn;
346 else
347 return (ctx->flags & RF_BLNK) ? 0 : ((grub_uint32_t) block -
348 ctx->curr_vcn + ctx->curr_lcn);
351 static grub_err_t
352 read_data (struct grub_ntfs_attr *at, char *pa, char *dest, grub_uint32_t ofs,
353 grub_uint32_t len, int cached,
354 void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
355 unsigned offset,
356 unsigned length))
358 grub_uint32_t vcn;
359 struct grub_ntfs_rlst cc, *ctx;
361 if (len == 0)
362 return 0;
364 grub_memset (&cc, 0, sizeof (cc));
365 ctx = &cc;
366 ctx->attr = at;
367 ctx->comp.spc = at->mft->data->spc;
368 ctx->comp.disk = at->mft->data->disk;
370 if (pa[8] == 0)
372 if (ofs + len > u32at (pa, 0x10))
373 return grub_error (GRUB_ERR_BAD_FS, "Read out of range");
374 grub_memcpy (dest, pa + u32at (pa, 0x14) + ofs, len);
375 return 0;
378 if (u16at (pa, 0xC) & FLAG_COMPRESSED)
379 ctx->flags |= RF_COMP;
380 else
381 ctx->flags &= ~RF_COMP;
382 ctx->cur_run = pa + u16at (pa, 0x20);
384 if (ctx->flags & RF_COMP)
386 if (!cached)
387 return grub_error (GRUB_ERR_BAD_FS, "Attribute can\'t be compressed");
389 if (at->sbuf)
391 if ((ofs & (~(COM_LEN - 1))) == at->save_pos)
393 grub_uint32_t n;
395 n = COM_LEN - (ofs - at->save_pos);
396 if (n > len)
397 n = len;
399 grub_memcpy (dest, at->sbuf + ofs - at->save_pos, n);
400 if (n == len)
401 return 0;
403 dest += n;
404 len -= n;
405 ofs += n;
408 else
410 at->sbuf = grub_malloc (COM_LEN);
411 if (at->sbuf == NULL)
412 return grub_errno;
413 at->save_pos = 1;
416 vcn = ctx->target_vcn = (ofs / COM_LEN) * (COM_SEC / ctx->comp.spc);
417 ctx->target_vcn &= ~0xF;
419 else
420 vcn = ctx->target_vcn = (ofs >> BLK_SHR) / ctx->comp.spc;
422 ctx->next_vcn = u32at (pa, 0x10);
423 ctx->curr_lcn = 0;
424 while (ctx->next_vcn <= ctx->target_vcn)
426 if (grub_ntfs_read_run_list (ctx))
427 return grub_errno;
430 if (at->flags & AF_GPOS)
432 grub_uint32_t st0, st1;
434 st0 =
435 (ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) * ctx->comp.spc +
436 ((ofs >> BLK_SHR) % ctx->comp.spc);
437 st1 = st0 + 1;
438 if (st1 ==
439 (ctx->next_vcn - ctx->curr_vcn + ctx->curr_lcn) * ctx->comp.spc)
441 if (grub_ntfs_read_run_list (ctx))
442 return grub_errno;
443 st1 = ctx->curr_lcn * ctx->comp.spc;
445 v32at (dest, 0) = st0;
446 v32at (dest, 4) = st1;
447 return 0;
450 if (!(ctx->flags & RF_COMP))
452 unsigned int pow;
454 if (!grub_fshelp_log2blksize (ctx->comp.spc, &pow))
455 grub_fshelp_read_file (ctx->comp.disk, (grub_fshelp_node_t) ctx,
456 read_hook, ofs, len, dest,
457 grub_ntfs_read_block, ofs + len, pow);
458 return grub_errno;
461 return (grub_ntfscomp_func) ? grub_ntfscomp_func (at, dest, ofs, len, ctx,
462 vcn) :
463 grub_error (GRUB_ERR_BAD_FS, "ntfscomp module not loaded");
466 static grub_err_t
467 read_attr (struct grub_ntfs_attr *at, char *dest, grub_uint32_t ofs,
468 grub_uint32_t len, int cached,
469 void NESTED_FUNC_ATTR (*read_hook) (grub_disk_addr_t sector,
470 unsigned offset,
471 unsigned length))
473 char *save_cur;
474 unsigned char attr;
475 char *pp;
476 grub_err_t ret;
478 save_cur = at->attr_cur;
479 at->attr_nxt = at->attr_cur;
480 attr = (unsigned char) *at->attr_nxt;
481 if (at->flags & AF_ALST)
483 char *pa;
484 grub_uint32_t vcn;
486 vcn = ofs / (at->mft->data->spc << BLK_SHR);
487 pa = at->attr_nxt + u16at (at->attr_nxt, 4);
488 while (pa < at->attr_end)
490 if ((unsigned char) *pa != attr)
491 break;
492 if (u32at (pa, 8) > vcn)
493 break;
494 at->attr_nxt = pa;
495 pa += u16at (pa, 4);
498 pp = find_attr (at, attr);
499 if (pp)
500 ret = read_data (at, pp, dest, ofs, len, cached, read_hook);
501 else
502 ret =
503 (grub_errno) ? grub_errno : grub_error (GRUB_ERR_BAD_FS,
504 "Attribute not found");
505 at->attr_cur = save_cur;
506 return ret;
509 static grub_err_t
510 read_mft (struct grub_ntfs_data *data, char *buf, grub_uint32_t mftno)
512 if (read_attr
513 (&data->mmft.attr, buf, mftno * (data->mft_size << BLK_SHR),
514 data->mft_size << BLK_SHR, 0, 0))
515 return grub_error (GRUB_ERR_BAD_FS, "Read MFT 0x%X fails", mftno);
516 return fixup (data, buf, data->mft_size, "FILE");
519 static grub_err_t
520 init_file (struct grub_ntfs_file *mft, grub_uint32_t mftno)
522 unsigned short flag;
524 mft->inode_read = 1;
526 mft->buf = grub_malloc (mft->data->mft_size << BLK_SHR);
527 if (mft->buf == NULL)
528 return grub_errno;
530 if (read_mft (mft->data, mft->buf, mftno))
531 return grub_errno;
533 flag = u16at (mft->buf, 0x16);
534 if ((flag & 1) == 0)
535 return grub_error (GRUB_ERR_BAD_FS, "MFT 0x%X is not in use", mftno);
537 if ((flag & 2) == 0)
539 char *pa;
541 pa = locate_attr (&mft->attr, mft, AT_DATA);
542 if (pa == NULL)
543 return grub_error (GRUB_ERR_BAD_FS, "No $DATA in MFT 0x%X", mftno);
545 if (!pa[8])
546 mft->size = u32at (pa, 0x10);
547 else
548 mft->size = u32at (pa, 0x30);
550 if ((mft->attr.flags & AF_ALST) == 0)
551 mft->attr.attr_end = 0; /* Don't jump to attribute list */
553 else
554 init_attr (&mft->attr, mft);
556 return 0;
559 static void
560 free_file (struct grub_ntfs_file *mft)
562 free_attr (&mft->attr);
563 grub_free (mft->buf);
566 static int
567 list_file (struct grub_ntfs_file *diro, char *pos,
568 int NESTED_FUNC_ATTR
569 (*hook) (const char *filename,
570 enum grub_fshelp_filetype filetype,
571 grub_fshelp_node_t node))
573 char *np;
574 int ns;
576 while (1)
578 char *ustr;
579 if (pos[0xC] & 2) /* end signature */
580 break;
582 np = pos + 0x52;
583 ns = (unsigned char) *(np - 2);
584 if (ns)
586 enum grub_fshelp_filetype type;
587 struct grub_ntfs_file *fdiro;
589 if (u16at (pos, 4))
591 grub_error (GRUB_ERR_BAD_FS, "64-bit MFT number");
592 return 0;
595 type =
596 (u32at (pos, 0x48) & ATTR_DIRECTORY) ? GRUB_FSHELP_DIR :
597 GRUB_FSHELP_REG;
599 fdiro = grub_malloc (sizeof (struct grub_ntfs_file));
600 if (!fdiro)
601 return 0;
603 grub_memset (fdiro, 0, sizeof (*fdiro));
604 fdiro->data = diro->data;
605 fdiro->ino = u32at (pos, 0);
607 ustr = grub_malloc (ns * 4 + 1);
608 if (ustr == NULL)
609 return 0;
610 *grub_utf16_to_utf8 ((grub_uint8_t *) ustr, (grub_uint16_t *) np,
611 ns) = '\0';
613 if (hook (ustr, type, fdiro))
615 grub_free (ustr);
616 return 1;
619 grub_free (ustr);
621 pos += u16at (pos, 8);
623 return 0;
626 static int
627 grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
628 int NESTED_FUNC_ATTR
629 (*hook) (const char *filename,
630 enum grub_fshelp_filetype filetype,
631 grub_fshelp_node_t node))
633 unsigned char *bitmap;
634 struct grub_ntfs_attr attr, *at;
635 char *cur_pos, *indx, *bmp;
636 int bitmap_len, ret = 0;
637 struct grub_ntfs_file *mft;
639 mft = (struct grub_ntfs_file *) dir;
641 if (!mft->inode_read)
643 if (init_file (mft, mft->ino))
644 return 0;
647 indx = NULL;
648 bmp = NULL;
650 at = &attr;
651 init_attr (at, mft);
652 while (1)
654 if ((cur_pos = find_attr (at, AT_INDEX_ROOT)) == NULL)
656 grub_error (GRUB_ERR_BAD_FS, "No $INDEX_ROOT");
657 goto done;
660 /* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */
661 if ((u32at (cur_pos, 8) != 0x180400) ||
662 (u32at (cur_pos, 0x18) != 0x490024) ||
663 (u32at (cur_pos, 0x1C) != 0x300033))
664 continue;
665 cur_pos += u16at (cur_pos, 0x14);
666 if (*cur_pos != 0x30) /* Not filename index */
667 continue;
668 break;
671 cur_pos += 0x10; /* Skip index root */
672 ret = list_file (mft, cur_pos + u16at (cur_pos, 0), hook);
673 if (ret)
674 goto done;
676 bitmap = NULL;
677 bitmap_len = 0;
678 free_attr (at);
679 init_attr (at, mft);
680 while ((cur_pos = find_attr (at, AT_BITMAP)) != NULL)
682 int ofs;
684 ofs = (unsigned char) cur_pos[0xA];
685 /* Namelen=4, Name="$I30" */
686 if ((cur_pos[9] == 4) &&
687 (u32at (cur_pos, ofs) == 0x490024) &&
688 (u32at (cur_pos, ofs + 4) == 0x300033))
690 if ((at->flags & AF_ALST) && (cur_pos[8] == 0))
692 grub_error (GRUB_ERR_BAD_FS,
693 "$BITMAP should be non-resident when in attribute list");
694 goto done;
696 if (cur_pos[8] == 0)
698 bitmap = (unsigned char *) (cur_pos + u16at (cur_pos, 0x14));
699 bitmap_len = u32at (cur_pos, 0x10);
700 break;
702 if (u32at (cur_pos, 0x28) > BMP_LEN)
704 grub_error (GRUB_ERR_BAD_FS, "Non-resident $BITMAP too large");
705 goto done;
707 bmp = grub_malloc (u32at (cur_pos, 0x28));
708 if (bmp == NULL)
709 goto done;
711 bitmap = (unsigned char *) bmp;
712 bitmap_len = u32at (cur_pos, 0x30);
713 if (read_data (at, cur_pos, bmp, 0, u32at (cur_pos, 0x28), 0, 0))
715 grub_error (GRUB_ERR_BAD_FS,
716 "Fails to read non-resident $BITMAP");
717 goto done;
719 break;
723 free_attr (at);
724 cur_pos = locate_attr (at, mft, AT_INDEX_ALLOCATION);
725 while (cur_pos != NULL)
727 /* Non-resident, Namelen=4, Offset=0x40, Flags=0, Name="$I30" */
728 if ((u32at (cur_pos, 8) == 0x400401) &&
729 (u32at (cur_pos, 0x40) == 0x490024) &&
730 (u32at (cur_pos, 0x44) == 0x300033))
731 break;
732 cur_pos = find_attr (at, AT_INDEX_ALLOCATION);
735 if ((!cur_pos) && (bitmap))
737 grub_error (GRUB_ERR_BAD_FS, "$BITMAP without $INDEX_ALLOCATION");
738 goto done;
741 if (bitmap)
743 grub_uint32_t v, i;
745 indx = grub_malloc (mft->data->idx_size << BLK_SHR);
746 if (indx == NULL)
747 goto done;
749 v = 1;
750 for (i = 0; i < (grub_uint32_t) bitmap_len * 8; i++)
752 if (*bitmap & v)
754 if ((read_attr
755 (at, indx, i * (mft->data->idx_size << BLK_SHR),
756 (mft->data->idx_size << BLK_SHR), 0, 0))
757 || (fixup (mft->data, indx, mft->data->idx_size, "INDX")))
758 goto done;
759 ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)], hook);
760 if (ret)
761 goto done;
763 v <<= 1;
764 if (v >= 0x100)
766 v = 1;
767 bitmap++;
772 done:
773 free_attr (at);
774 grub_free (indx);
775 grub_free (bmp);
777 return ret;
780 static struct grub_ntfs_data *
781 grub_ntfs_mount (grub_disk_t disk)
783 struct grub_ntfs_bpb bpb;
784 struct grub_ntfs_data *data = 0;
786 if (!disk)
787 goto fail;
789 data = (struct grub_ntfs_data *) grub_malloc (sizeof (*data));
790 if (!data)
791 goto fail;
793 grub_memset (data, 0, sizeof (*data));
795 data->disk = disk;
797 /* Read the BPB. */
798 if (grub_disk_read (disk, 0, 0, sizeof (bpb), (char *) &bpb))
799 goto fail;
801 if (grub_memcmp ((char *) &bpb.oem_name, "NTFS", 4))
802 goto fail;
804 data->blocksize = grub_le_to_cpu16 (bpb.bytes_per_sector);
805 data->spc = bpb.sectors_per_cluster * (data->blocksize >> BLK_SHR);
807 if (bpb.clusters_per_mft > 0)
808 data->mft_size = data->spc * bpb.clusters_per_mft;
809 else
810 data->mft_size = 1 << (-bpb.clusters_per_mft - BLK_SHR);
812 if (bpb.clusters_per_index > 0)
813 data->idx_size = data->spc * bpb.clusters_per_index;
814 else
815 data->idx_size = 1 << (-bpb.clusters_per_index - BLK_SHR);
817 data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) * data->spc;
819 if ((data->mft_size > MAX_MFT) || (data->idx_size > MAX_IDX))
820 goto fail;
822 data->mmft.data = data;
823 data->cmft.data = data;
825 data->mmft.buf = grub_malloc (data->mft_size << BLK_SHR);
826 if (!data->mmft.buf)
827 goto fail;
829 if (grub_disk_read
830 (disk, data->mft_start, 0, data->mft_size << BLK_SHR, data->mmft.buf))
831 goto fail;
833 data->uuid = grub_le_to_cpu64 (bpb.num_serial);
835 if (fixup (data, data->mmft.buf, data->mft_size, "FILE"))
836 goto fail;
838 if (!locate_attr (&data->mmft.attr, &data->mmft, AT_DATA))
839 goto fail;
841 if (init_file (&data->cmft, FILE_ROOT))
842 goto fail;
844 return data;
846 fail:
847 grub_error (GRUB_ERR_BAD_FS, "not an ntfs filesystem");
849 if (data)
851 free_file (&data->mmft);
852 free_file (&data->cmft);
854 return 0;
857 static grub_err_t
858 grub_ntfs_dir (grub_device_t device, const char *path,
859 int (*hook) (const char *filename, int dir))
861 struct grub_ntfs_data *data = 0;
862 struct grub_fshelp_node *fdiro = 0;
864 auto int NESTED_FUNC_ATTR iterate (const char *filename,
865 enum grub_fshelp_filetype filetype,
866 grub_fshelp_node_t node);
868 int NESTED_FUNC_ATTR iterate (const char *filename,
869 enum grub_fshelp_filetype filetype,
870 grub_fshelp_node_t node)
872 grub_free (node);
874 if (filetype == GRUB_FSHELP_DIR)
875 return hook (filename, 1);
876 else
877 return hook (filename, 0);
879 return 0;
882 #ifndef GRUB_UTIL
883 grub_dl_ref (my_mod);
884 #endif
887 data = grub_ntfs_mount (device->disk);
888 if (!data)
889 goto fail;
891 grub_fshelp_find_file (path, &data->cmft, &fdiro, grub_ntfs_iterate_dir,
892 0, GRUB_FSHELP_DIR);
894 if (grub_errno)
895 goto fail;
897 grub_ntfs_iterate_dir (fdiro, iterate);
899 fail:
900 if ((fdiro) && (fdiro != &data->cmft))
902 free_file (fdiro);
903 grub_free (fdiro);
905 if (data)
907 free_file (&data->mmft);
908 free_file (&data->cmft);
909 grub_free (data);
912 #ifndef GRUB_UTIL
913 grub_dl_unref (my_mod);
914 #endif
916 return grub_errno;
919 static grub_err_t
920 grub_ntfs_open (grub_file_t file, const char *name)
922 struct grub_ntfs_data *data = 0;
923 struct grub_fshelp_node *mft = 0;
925 #ifndef GRUB_UTIL
926 grub_dl_ref (my_mod);
927 #endif
929 data = grub_ntfs_mount (file->device->disk);
930 if (!data)
931 goto fail;
933 grub_fshelp_find_file (name, &data->cmft, &mft, grub_ntfs_iterate_dir,
934 0, GRUB_FSHELP_REG);
936 if (grub_errno)
937 goto fail;
939 if (mft != &data->cmft)
941 free_file (&data->cmft);
942 grub_memcpy (&data->cmft, mft, sizeof (*mft));
943 grub_free (mft);
944 if (!data->cmft.inode_read)
946 if (init_file (&data->cmft, data->cmft.ino))
947 goto fail;
951 file->size = data->cmft.size;
952 file->data = data;
953 file->offset = 0;
955 return 0;
957 fail:
958 if (data)
960 free_file (&data->mmft);
961 free_file (&data->cmft);
962 grub_free (data);
965 #ifndef GRUB_UTIL
966 grub_dl_unref (my_mod);
967 #endif
969 return grub_errno;
972 static grub_ssize_t
973 grub_ntfs_read (grub_file_t file, char *buf, grub_size_t len)
975 struct grub_ntfs_file *mft;
977 mft = &((struct grub_ntfs_data *) file->data)->cmft;
978 if (file->read_hook)
979 mft->attr.save_pos = 1;
981 if (file->offset > file->size)
983 grub_error (GRUB_ERR_BAD_FS, "Bad offset");
984 return -1;
987 if (file->offset + len > file->size)
988 len = file->size - file->offset;
990 read_attr (&mft->attr, buf, file->offset, len, 1, file->read_hook);
991 return (grub_errno) ? 0 : len;
994 static grub_err_t
995 grub_ntfs_close (grub_file_t file)
997 struct grub_ntfs_data *data;
999 data = file->data;
1001 if (data)
1003 free_file (&data->mmft);
1004 free_file (&data->cmft);
1005 grub_free (data);
1008 #ifndef GRUB_UTIL
1009 grub_dl_unref (my_mod);
1010 #endif
1012 return grub_errno;
1015 static grub_err_t
1016 grub_ntfs_label (grub_device_t device, char **label)
1018 struct grub_ntfs_data *data = 0;
1019 struct grub_fshelp_node *mft = 0;
1020 char *pa;
1022 #ifndef GRUB_UTIL
1023 grub_dl_ref (my_mod);
1024 #endif
1026 *label = 0;
1028 data = grub_ntfs_mount (device->disk);
1029 if (!data)
1030 goto fail;
1032 grub_fshelp_find_file ("/$Volume", &data->cmft, &mft, grub_ntfs_iterate_dir,
1033 0, GRUB_FSHELP_REG);
1035 if (grub_errno)
1036 goto fail;
1038 if (!mft->inode_read)
1040 mft->buf = grub_malloc (mft->data->mft_size << BLK_SHR);
1041 if (mft->buf == NULL)
1042 goto fail;
1044 if (read_mft (mft->data, mft->buf, mft->ino))
1045 goto fail;
1048 init_attr (&mft->attr, mft);
1049 pa = find_attr (&mft->attr, AT_VOLUME_NAME);
1050 if ((pa) && (pa[8] == 0) && (u32at (pa, 0x10)))
1052 char *buf;
1053 int len;
1055 len = u32at (pa, 0x10) / 2;
1056 buf = grub_malloc (len * 4 + 1);
1057 pa += u16at (pa, 0x14);
1058 *grub_utf16_to_utf8 ((grub_uint8_t *) buf, (grub_uint16_t *) pa, len) =
1059 '\0';
1060 *label = buf;
1063 fail:
1064 if ((mft) && (mft != &data->cmft))
1066 free_file (mft);
1067 grub_free (mft);
1069 if (data)
1071 free_file (&data->mmft);
1072 free_file (&data->cmft);
1073 grub_free (data);
1076 #ifndef GRUB_UTIL
1077 grub_dl_unref (my_mod);
1078 #endif
1080 return grub_errno;
1083 static grub_err_t
1084 grub_ntfs_uuid (grub_device_t device, char **uuid)
1086 struct grub_ntfs_data *data;
1087 grub_disk_t disk = device->disk;
1089 #ifndef GRUB_UTIL
1090 grub_dl_ref (my_mod);
1091 #endif
1093 data = grub_ntfs_mount (disk);
1094 if (data)
1096 *uuid = grub_malloc (16 + sizeof ('\0'));
1097 grub_sprintf (*uuid, "%016llx", (unsigned long long) data->uuid);
1099 else
1100 *uuid = NULL;
1102 #ifndef GRUB_UTIL
1103 grub_dl_unref (my_mod);
1104 #endif
1106 grub_free (data);
1108 return grub_errno;
1111 static struct grub_fs grub_ntfs_fs = {
1112 .name = "ntfs",
1113 .dir = grub_ntfs_dir,
1114 .open = grub_ntfs_open,
1115 .read = grub_ntfs_read,
1116 .close = grub_ntfs_close,
1117 .label = grub_ntfs_label,
1118 .uuid = grub_ntfs_uuid,
1119 .next = 0
1122 GRUB_MOD_INIT (ntfs)
1124 grub_fs_register (&grub_ntfs_fs);
1125 #ifndef GRUB_UTIL
1126 my_mod = mod;
1127 #endif
1130 GRUB_MOD_FINI (ntfs)
1132 grub_fs_unregister (&grub_ntfs_fs);