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