Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / fs / ntfs.c
blob6f8468862628383736acf25db5bf715d9a095791
1 /* ntfs.c - NTFS filesystem */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007,2008,2009 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 #define grub_fshelp_node grub_ntfs_file
22 #include <grub/file.h>
23 #include <grub/mm.h>
24 #include <grub/misc.h>
25 #include <grub/disk.h>
26 #include <grub/dl.h>
27 #include <grub/fshelp.h>
28 #include <grub/ntfs.h>
29 #include <grub/charset.h>
31 GRUB_MOD_LICENSE ("GPLv3+");
33 static grub_dl_t my_mod;
35 #define grub_fshelp_node grub_ntfs_file
37 static inline grub_uint16_t
38 u16at (void *ptr, grub_size_t ofs)
40 return grub_le_to_cpu16 (grub_get_unaligned16 ((char *) ptr + ofs));
43 static inline grub_uint32_t
44 u32at (void *ptr, grub_size_t ofs)
46 return grub_le_to_cpu32 (grub_get_unaligned32 ((char *) ptr + ofs));
49 static inline grub_uint64_t
50 u64at (void *ptr, grub_size_t ofs)
52 return grub_le_to_cpu64 (grub_get_unaligned64 ((char *) ptr + ofs));
55 grub_ntfscomp_func_t grub_ntfscomp_func;
57 static grub_err_t
58 fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
60 grub_uint16_t ss;
61 grub_uint8_t *pu;
62 grub_uint16_t us;
64 COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE);
66 if (grub_memcmp (buf, magic, 4))
67 return grub_error (GRUB_ERR_BAD_FS, "%s label not found", magic);
69 ss = u16at (buf, 6) - 1;
70 if (ss != len)
71 return grub_error (GRUB_ERR_BAD_FS, "size not match");
72 pu = buf + u16at (buf, 4);
73 us = u16at (pu, 0);
74 buf -= 2;
75 while (ss > 0)
77 buf += GRUB_DISK_SECTOR_SIZE;
78 pu += 2;
79 if (u16at (buf, 0) != us)
80 return grub_error (GRUB_ERR_BAD_FS, "fixup signature not match");
81 buf[0] = pu[0];
82 buf[1] = pu[1];
83 ss--;
86 return 0;
89 static grub_err_t read_mft (struct grub_ntfs_data *data, grub_uint8_t *buf,
90 grub_uint64_t mftno);
91 static grub_err_t read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest,
92 grub_disk_addr_t ofs, grub_size_t len,
93 int cached,
94 grub_disk_read_hook_t read_hook,
95 void *read_hook_data);
97 static grub_err_t read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa,
98 grub_uint8_t *dest,
99 grub_disk_addr_t ofs, grub_size_t len,
100 int cached,
101 grub_disk_read_hook_t read_hook,
102 void *read_hook_data);
104 static void
105 init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft)
107 at->mft = mft;
108 at->flags = (mft == &mft->data->mmft) ? GRUB_NTFS_AF_MMFT : 0;
109 at->attr_nxt = mft->buf + u16at (mft->buf, 0x14);
110 at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL;
113 static void
114 free_attr (struct grub_ntfs_attr *at)
116 grub_free (at->emft_buf);
117 grub_free (at->edat_buf);
118 grub_free (at->sbuf);
121 static grub_uint8_t *
122 find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
124 if (at->flags & GRUB_NTFS_AF_ALST)
126 retry:
127 while (at->attr_nxt < at->attr_end)
129 at->attr_cur = at->attr_nxt;
130 at->attr_nxt += u16at (at->attr_cur, 4);
131 if ((*at->attr_cur == attr) || (attr == 0))
133 grub_uint8_t *new_pos;
135 if (at->flags & GRUB_NTFS_AF_MMFT)
137 if ((grub_disk_read
138 (at->mft->data->disk, u32at (at->attr_cur, 0x10), 0,
139 512, at->emft_buf))
141 (grub_disk_read
142 (at->mft->data->disk, u32at (at->attr_cur, 0x14), 0,
143 512, at->emft_buf + 512)))
144 return NULL;
146 if (fixup (at->emft_buf, at->mft->data->mft_size,
147 (const grub_uint8_t *) "FILE"))
148 return NULL;
150 else
152 if (read_mft (at->mft->data, at->emft_buf,
153 u32at (at->attr_cur, 0x10)))
154 return NULL;
157 new_pos = &at->emft_buf[u16at (at->emft_buf, 0x14)];
158 while (*new_pos != 0xFF)
160 if ((*new_pos == *at->attr_cur)
161 && (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18)))
163 return new_pos;
165 new_pos += u16at (new_pos, 4);
167 grub_error (GRUB_ERR_BAD_FS,
168 "can\'t find 0x%X in attribute list",
169 (unsigned char) *at->attr_cur);
170 return NULL;
173 return NULL;
175 at->attr_cur = at->attr_nxt;
176 while (*at->attr_cur != 0xFF)
178 at->attr_nxt += u16at (at->attr_cur, 4);
179 if (*at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
180 at->attr_end = at->attr_cur;
181 if ((*at->attr_cur == attr) || (attr == 0))
182 return at->attr_cur;
183 at->attr_cur = at->attr_nxt;
185 if (at->attr_end)
187 grub_uint8_t *pa;
189 at->emft_buf = grub_malloc (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
190 if (at->emft_buf == NULL)
191 return NULL;
193 pa = at->attr_end;
194 if (pa[8])
196 grub_uint32_t n;
198 n = ((u32at (pa, 0x30) + GRUB_DISK_SECTOR_SIZE - 1)
199 & (~(GRUB_DISK_SECTOR_SIZE - 1)));
200 at->attr_cur = at->attr_end;
201 at->edat_buf = grub_malloc (n);
202 if (!at->edat_buf)
203 return NULL;
204 if (read_data (at, pa, at->edat_buf, 0, n, 0, 0, 0))
206 grub_error (GRUB_ERR_BAD_FS,
207 "fail to read non-resident attribute list");
208 return NULL;
210 at->attr_nxt = at->edat_buf;
211 at->attr_end = at->edat_buf + u32at (pa, 0x30);
213 else
215 at->attr_nxt = at->attr_end + u16at (pa, 0x14);
216 at->attr_end = at->attr_end + u32at (pa, 4);
218 at->flags |= GRUB_NTFS_AF_ALST;
219 while (at->attr_nxt < at->attr_end)
221 if ((*at->attr_nxt == attr) || (attr == 0))
222 break;
223 at->attr_nxt += u16at (at->attr_nxt, 4);
225 if (at->attr_nxt >= at->attr_end)
226 return NULL;
228 if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA))
230 at->flags |= GRUB_NTFS_AF_GPOS;
231 at->attr_cur = at->attr_nxt;
232 pa = at->attr_cur;
233 grub_set_unaligned32 ((char *) pa + 0x10,
234 grub_cpu_to_le32 (at->mft->data->mft_start));
235 grub_set_unaligned32 ((char *) pa + 0x14,
236 grub_cpu_to_le32 (at->mft->data->mft_start
237 + 1));
238 pa = at->attr_nxt + u16at (pa, 4);
239 while (pa < at->attr_end)
241 if (*pa != attr)
242 break;
243 if (read_attr
244 (at, pa + 0x10,
245 u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
246 at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
247 return NULL;
248 pa += u16at (pa, 4);
250 at->attr_nxt = at->attr_cur;
251 at->flags &= ~GRUB_NTFS_AF_GPOS;
253 goto retry;
255 return NULL;
258 static grub_uint8_t *
259 locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft,
260 grub_uint8_t attr)
262 grub_uint8_t *pa;
264 init_attr (at, mft);
265 pa = find_attr (at, attr);
266 if (pa == NULL)
267 return NULL;
268 if ((at->flags & GRUB_NTFS_AF_ALST) == 0)
270 while (1)
272 pa = find_attr (at, attr);
273 if (pa == NULL)
274 break;
275 if (at->flags & GRUB_NTFS_AF_ALST)
276 return pa;
278 grub_errno = GRUB_ERR_NONE;
279 free_attr (at);
280 init_attr (at, mft);
281 pa = find_attr (at, attr);
283 return pa;
286 static grub_disk_addr_t
287 read_run_data (const grub_uint8_t *run, int nn, int sig)
289 grub_uint64_t r = 0;
291 if (sig && nn && (run[nn - 1] & 0x80))
292 r = -1;
294 grub_memcpy (&r, run, nn);
296 return grub_le_to_cpu64 (r);
299 grub_err_t
300 grub_ntfs_read_run_list (struct grub_ntfs_rlst * ctx)
302 grub_uint8_t c1, c2;
303 grub_disk_addr_t val;
304 grub_uint8_t *run;
306 run = ctx->cur_run;
307 retry:
308 c1 = ((*run) & 0x7);
309 c2 = ((*run) >> 4) & 0x7;
310 run++;
311 if (!c1)
313 if ((ctx->attr) && (ctx->attr->flags & GRUB_NTFS_AF_ALST))
315 grub_disk_read_hook_t save_hook;
317 save_hook = ctx->comp.disk->read_hook;
318 ctx->comp.disk->read_hook = 0;
319 run = find_attr (ctx->attr, *ctx->attr->attr_cur);
320 ctx->comp.disk->read_hook = save_hook;
321 if (run)
323 if (run[8] == 0)
324 return grub_error (GRUB_ERR_BAD_FS,
325 "$DATA should be non-resident");
327 run += u16at (run, 0x20);
328 ctx->curr_lcn = 0;
329 goto retry;
332 return grub_error (GRUB_ERR_BAD_FS, "run list overflown");
334 ctx->curr_vcn = ctx->next_vcn;
335 ctx->next_vcn += read_run_data (run, c1, 0); /* length of current VCN */
336 run += c1;
337 val = read_run_data (run, c2, 1); /* offset to previous LCN */
338 run += c2;
339 ctx->curr_lcn += val;
340 if (val == 0)
341 ctx->flags |= GRUB_NTFS_RF_BLNK;
342 else
343 ctx->flags &= ~GRUB_NTFS_RF_BLNK;
344 ctx->cur_run = run;
345 return 0;
348 static grub_disk_addr_t
349 grub_ntfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t block)
351 struct grub_ntfs_rlst *ctx;
353 ctx = (struct grub_ntfs_rlst *) node;
354 if (block >= ctx->next_vcn)
356 if (grub_ntfs_read_run_list (ctx))
357 return -1;
358 return ctx->curr_lcn;
360 else
361 return (ctx->flags & GRUB_NTFS_RF_BLNK) ? 0 : (block -
362 ctx->curr_vcn + ctx->curr_lcn);
365 static grub_err_t
366 read_data (struct grub_ntfs_attr *at, grub_uint8_t *pa, grub_uint8_t *dest,
367 grub_disk_addr_t ofs, grub_size_t len, int cached,
368 grub_disk_read_hook_t read_hook, void *read_hook_data)
370 struct grub_ntfs_rlst cc, *ctx;
372 if (len == 0)
373 return 0;
375 grub_memset (&cc, 0, sizeof (cc));
376 ctx = &cc;
377 ctx->attr = at;
378 ctx->comp.log_spc = at->mft->data->log_spc;
379 ctx->comp.disk = at->mft->data->disk;
381 if (read_hook == grub_file_progress_hook)
382 ctx->file = read_hook_data;
384 if (pa[8] == 0)
386 if (ofs + len > u32at (pa, 0x10))
387 return grub_error (GRUB_ERR_BAD_FS, "read out of range");
388 grub_memcpy (dest, pa + u32at (pa, 0x14) + ofs, len);
389 return 0;
392 ctx->cur_run = pa + u16at (pa, 0x20);
394 ctx->next_vcn = u32at (pa, 0x10);
395 ctx->curr_lcn = 0;
397 if ((pa[0xC] & GRUB_NTFS_FLAG_COMPRESSED)
398 && !(at->flags & GRUB_NTFS_AF_GPOS))
400 if (!cached)
401 return grub_error (GRUB_ERR_BAD_FS, "attribute can\'t be compressed");
403 return (grub_ntfscomp_func) ? grub_ntfscomp_func (dest, ofs, len, ctx)
404 : grub_error (GRUB_ERR_BAD_FS, N_("module `%s' isn't loaded"),
405 "ntfscomp");
408 ctx->target_vcn = ofs >> (GRUB_NTFS_BLK_SHR + ctx->comp.log_spc);
409 while (ctx->next_vcn <= ctx->target_vcn)
411 if (grub_ntfs_read_run_list (ctx))
412 return grub_errno;
415 if (at->flags & GRUB_NTFS_AF_GPOS)
417 grub_disk_addr_t st0, st1;
418 grub_uint64_t m;
420 m = (ofs >> GRUB_NTFS_BLK_SHR) & ((1 << ctx->comp.log_spc) - 1);
422 st0 =
423 ((ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc) + m;
424 st1 = st0 + 1;
425 if (st1 ==
426 (ctx->next_vcn - ctx->curr_vcn + ctx->curr_lcn) << ctx->comp.log_spc)
428 if (grub_ntfs_read_run_list (ctx))
429 return grub_errno;
430 st1 = ctx->curr_lcn << ctx->comp.log_spc;
432 grub_set_unaligned32 (dest, grub_cpu_to_le32 (st0));
433 grub_set_unaligned32 (dest + 4, grub_cpu_to_le32 (st1));
434 return 0;
437 grub_fshelp_read_file (ctx->comp.disk, (grub_fshelp_node_t) ctx,
438 read_hook, read_hook_data, ofs, len,
439 (char *) dest,
440 grub_ntfs_read_block, ofs + len,
441 ctx->comp.log_spc, 0);
442 return grub_errno;
445 static grub_err_t
446 read_attr (struct grub_ntfs_attr *at, grub_uint8_t *dest, grub_disk_addr_t ofs,
447 grub_size_t len, int cached,
448 grub_disk_read_hook_t read_hook, void *read_hook_data)
450 grub_uint8_t *save_cur;
451 grub_uint8_t attr;
452 grub_uint8_t *pp;
453 grub_err_t ret;
455 save_cur = at->attr_cur;
456 at->attr_nxt = at->attr_cur;
457 attr = *at->attr_nxt;
458 if (at->flags & GRUB_NTFS_AF_ALST)
460 grub_uint8_t *pa;
461 grub_disk_addr_t vcn;
463 /* If compression is possible make sure that we include possible
464 compressed block size. */
465 if (GRUB_NTFS_LOG_COM_SEC >= at->mft->data->log_spc)
466 vcn = ((ofs >> GRUB_NTFS_COM_LOG_LEN)
467 << (GRUB_NTFS_LOG_COM_SEC - at->mft->data->log_spc)) & ~0xFULL;
468 else
469 vcn = ofs >> (at->mft->data->log_spc + GRUB_NTFS_BLK_SHR);
470 pa = at->attr_nxt + u16at (at->attr_nxt, 4);
471 while (pa < at->attr_end)
473 if (*pa != attr)
474 break;
475 if (u32at (pa, 8) > vcn)
476 break;
477 at->attr_nxt = pa;
478 pa += u16at (pa, 4);
481 pp = find_attr (at, attr);
482 if (pp)
483 ret = read_data (at, pp, dest, ofs, len, cached,
484 read_hook, read_hook_data);
485 else
486 ret =
487 (grub_errno) ? grub_errno : grub_error (GRUB_ERR_BAD_FS,
488 "attribute not found");
489 at->attr_cur = save_cur;
490 return ret;
493 static grub_err_t
494 read_mft (struct grub_ntfs_data *data, grub_uint8_t *buf, grub_uint64_t mftno)
496 if (read_attr
497 (&data->mmft.attr, buf, mftno * ((grub_disk_addr_t) data->mft_size << GRUB_NTFS_BLK_SHR),
498 data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0, 0))
499 return grub_error (GRUB_ERR_BAD_FS, "read MFT 0x%llx fails", (unsigned long long) mftno);
500 return fixup (buf, data->mft_size, (const grub_uint8_t *) "FILE");
503 static grub_err_t
504 init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
506 unsigned short flag;
508 mft->inode_read = 1;
510 mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
511 if (mft->buf == NULL)
512 return grub_errno;
514 if (read_mft (mft->data, mft->buf, mftno))
515 return grub_errno;
517 flag = u16at (mft->buf, 0x16);
518 if ((flag & 1) == 0)
519 return grub_error (GRUB_ERR_BAD_FS, "MFT 0x%llx is not in use",
520 (unsigned long long) mftno);
522 if ((flag & 2) == 0)
524 grub_uint8_t *pa;
526 pa = locate_attr (&mft->attr, mft, GRUB_NTFS_AT_DATA);
527 if (pa == NULL)
528 return grub_error (GRUB_ERR_BAD_FS, "no $DATA in MFT 0x%llx",
529 (unsigned long long) mftno);
531 if (!pa[8])
532 mft->size = u32at (pa, 0x10);
533 else
534 mft->size = u64at (pa, 0x30);
536 if ((mft->attr.flags & GRUB_NTFS_AF_ALST) == 0)
537 mft->attr.attr_end = 0; /* Don't jump to attribute list */
539 else
540 init_attr (&mft->attr, mft);
542 return 0;
545 static void
546 free_file (struct grub_ntfs_file *mft)
548 free_attr (&mft->attr);
549 grub_free (mft->buf);
552 static char *
553 get_utf8 (grub_uint8_t *in, grub_size_t len)
555 grub_uint8_t *buf;
556 grub_uint16_t *tmp;
557 grub_size_t i;
559 buf = grub_malloc (len * GRUB_MAX_UTF8_PER_UTF16 + 1);
560 tmp = grub_malloc (len * sizeof (tmp[0]));
561 if (!buf || !tmp)
563 grub_free (buf);
564 grub_free (tmp);
565 return NULL;
567 for (i = 0; i < len; i++)
568 tmp[i] = grub_le_to_cpu16 (grub_get_unaligned16 (in + 2 * i));
569 *grub_utf16_to_utf8 (buf, tmp, len) = '\0';
570 grub_free (tmp);
571 return (char *) buf;
574 static int
575 list_file (struct grub_ntfs_file *diro, grub_uint8_t *pos,
576 grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
578 grub_uint8_t *np;
579 int ns;
581 while (1)
583 grub_uint8_t namespace;
584 char *ustr;
586 if (pos[0xC] & 2) /* end signature */
587 break;
589 np = pos + 0x50;
590 ns = *(np++);
591 namespace = *(np++);
594 * Ignore files in DOS namespace, as they will reappear as Win32
595 * names.
597 if ((ns) && (namespace != 2))
599 enum grub_fshelp_filetype type;
600 struct grub_ntfs_file *fdiro;
601 grub_uint32_t attr;
603 attr = u32at (pos, 0x48);
604 if (attr & GRUB_NTFS_ATTR_REPARSE)
605 type = GRUB_FSHELP_SYMLINK;
606 else if (attr & GRUB_NTFS_ATTR_DIRECTORY)
607 type = GRUB_FSHELP_DIR;
608 else
609 type = GRUB_FSHELP_REG;
611 fdiro = grub_zalloc (sizeof (struct grub_ntfs_file));
612 if (!fdiro)
613 return 0;
615 fdiro->data = diro->data;
616 fdiro->ino = u64at (pos, 0) & 0xffffffffffffULL;
617 fdiro->mtime = u64at (pos, 0x20);
619 ustr = get_utf8 (np, ns);
620 if (ustr == NULL)
622 grub_free (fdiro);
623 return 0;
625 if (namespace)
626 type |= GRUB_FSHELP_CASE_INSENSITIVE;
628 if (hook (ustr, type, fdiro, hook_data))
630 grub_free (ustr);
631 return 1;
634 grub_free (ustr);
636 pos += u16at (pos, 8);
638 return 0;
641 struct symlink_descriptor
643 grub_uint32_t type;
644 grub_uint32_t total_len;
645 grub_uint16_t off1;
646 grub_uint16_t len1;
647 grub_uint16_t off2;
648 grub_uint16_t len2;
649 } GRUB_PACKED;
651 static char *
652 grub_ntfs_read_symlink (grub_fshelp_node_t node)
654 struct grub_ntfs_file *mft;
655 struct symlink_descriptor symdesc;
656 grub_err_t err;
657 grub_uint8_t *buf16;
658 char *buf, *end;
659 grub_size_t len;
660 grub_uint8_t *pa;
661 grub_size_t off;
663 mft = (struct grub_ntfs_file *) node;
665 mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
666 if (mft->buf == NULL)
667 return NULL;
669 if (read_mft (mft->data, mft->buf, mft->ino))
670 return NULL;
672 pa = locate_attr (&mft->attr, mft, GRUB_NTFS_AT_SYMLINK);
673 if (pa == NULL)
675 grub_error (GRUB_ERR_BAD_FS, "no $SYMLINK in MFT 0x%llx",
676 (unsigned long long) mft->ino);
677 return NULL;
680 err = read_attr (&mft->attr, (grub_uint8_t *) &symdesc, 0,
681 sizeof (struct symlink_descriptor), 1, 0, 0);
682 if (err)
683 return NULL;
685 switch (grub_cpu_to_le32 (symdesc.type))
687 case 0xa000000c:
688 off = (sizeof (struct symlink_descriptor) + 4
689 + grub_cpu_to_le32 (symdesc.off1));
690 len = grub_cpu_to_le32 (symdesc.len1);
691 break;
692 case 0xa0000003:
693 off = (sizeof (struct symlink_descriptor)
694 + grub_cpu_to_le32 (symdesc.off1));
695 len = grub_cpu_to_le32 (symdesc.len1);
696 break;
697 default:
698 grub_error (GRUB_ERR_BAD_FS, "symlink type invalid (%x)",
699 grub_cpu_to_le32 (symdesc.type));
700 return NULL;
703 buf16 = grub_malloc (len);
704 if (!buf16)
705 return NULL;
707 err = read_attr (&mft->attr, buf16, off, len, 1, 0, 0);
708 if (err)
709 return NULL;
711 buf = get_utf8 (buf16, len / 2);
712 if (!buf)
714 grub_free (buf16);
715 return NULL;
717 grub_free (buf16);
719 for (end = buf; *end; end++)
720 if (*end == '\\')
721 *end = '/';
723 /* Split the sequence to avoid GCC thinking that this is a trigraph. */
724 if (grub_memcmp (buf, "/?" "?/", 4) == 0 && buf[5] == ':' && buf[6] == '/'
725 && grub_isalpha (buf[4]))
727 grub_memmove (buf, buf + 6, end - buf + 1 - 6);
728 end -= 6;
730 return buf;
733 static int
734 grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
735 grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
737 grub_uint8_t *bitmap;
738 struct grub_ntfs_attr attr, *at;
739 grub_uint8_t *cur_pos, *indx, *bmp;
740 int ret = 0;
741 grub_size_t bitmap_len;
742 struct grub_ntfs_file *mft;
744 mft = (struct grub_ntfs_file *) dir;
746 if (!mft->inode_read)
748 if (init_file (mft, mft->ino))
749 return 0;
752 indx = NULL;
753 bmp = NULL;
755 at = &attr;
756 init_attr (at, mft);
757 while (1)
759 cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ROOT);
760 if (cur_pos == NULL)
762 grub_error (GRUB_ERR_BAD_FS, "no $INDEX_ROOT");
763 goto done;
766 /* Resident, Namelen=4, Offset=0x18, Flags=0x00, Name="$I30" */
767 if ((u32at (cur_pos, 8) != 0x180400) ||
768 (u32at (cur_pos, 0x18) != 0x490024) ||
769 (u32at (cur_pos, 0x1C) != 0x300033))
770 continue;
771 cur_pos += u16at (cur_pos, 0x14);
772 if (*cur_pos != 0x30) /* Not filename index */
773 continue;
774 break;
777 cur_pos += 0x10; /* Skip index root */
778 ret = list_file (mft, cur_pos + u16at (cur_pos, 0), hook, hook_data);
779 if (ret)
780 goto done;
782 bitmap = NULL;
783 bitmap_len = 0;
784 free_attr (at);
785 init_attr (at, mft);
786 while ((cur_pos = find_attr (at, GRUB_NTFS_AT_BITMAP)) != NULL)
788 int ofs;
790 ofs = cur_pos[0xA];
791 /* Namelen=4, Name="$I30" */
792 if ((cur_pos[9] == 4) &&
793 (u32at (cur_pos, ofs) == 0x490024) &&
794 (u32at (cur_pos, ofs + 4) == 0x300033))
796 int is_resident = (cur_pos[8] == 0);
798 bitmap_len = ((is_resident) ? u32at (cur_pos, 0x10) :
799 u32at (cur_pos, 0x28));
801 bmp = grub_malloc (bitmap_len);
802 if (bmp == NULL)
803 goto done;
805 if (is_resident)
807 grub_memcpy (bmp, cur_pos + u16at (cur_pos, 0x14),
808 bitmap_len);
810 else
812 if (read_data (at, cur_pos, bmp, 0, bitmap_len, 0, 0, 0))
814 grub_error (GRUB_ERR_BAD_FS,
815 "fails to read non-resident $BITMAP");
816 goto done;
818 bitmap_len = u32at (cur_pos, 0x30);
821 bitmap = bmp;
822 break;
826 free_attr (at);
827 cur_pos = locate_attr (at, mft, GRUB_NTFS_AT_INDEX_ALLOCATION);
828 while (cur_pos != NULL)
830 /* Non-resident, Namelen=4, Offset=0x40, Flags=0, Name="$I30" */
831 if ((u32at (cur_pos, 8) == 0x400401) &&
832 (u32at (cur_pos, 0x40) == 0x490024) &&
833 (u32at (cur_pos, 0x44) == 0x300033))
834 break;
835 cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ALLOCATION);
838 if ((!cur_pos) && (bitmap))
840 grub_error (GRUB_ERR_BAD_FS, "$BITMAP without $INDEX_ALLOCATION");
841 goto done;
844 if (bitmap)
846 grub_disk_addr_t i;
847 grub_uint8_t v;
849 indx = grub_malloc (mft->data->idx_size << GRUB_NTFS_BLK_SHR);
850 if (indx == NULL)
851 goto done;
853 v = 1;
854 for (i = 0; i < (grub_disk_addr_t)bitmap_len * 8; i++)
856 if (*bitmap & v)
858 if ((read_attr
859 (at, indx, i * (mft->data->idx_size << GRUB_NTFS_BLK_SHR),
860 (mft->data->idx_size << GRUB_NTFS_BLK_SHR), 0, 0, 0))
861 || (fixup (indx, mft->data->idx_size,
862 (const grub_uint8_t *) "INDX")))
863 goto done;
864 ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)],
865 hook, hook_data);
866 if (ret)
867 goto done;
869 v <<= 1;
870 if (!v)
872 v = 1;
873 bitmap++;
878 done:
879 free_attr (at);
880 grub_free (indx);
881 grub_free (bmp);
883 return ret;
886 static struct grub_ntfs_data *
887 grub_ntfs_mount (grub_disk_t disk)
889 struct grub_ntfs_bpb bpb;
890 struct grub_ntfs_data *data = 0;
891 grub_uint32_t spc;
893 if (!disk)
894 goto fail;
896 data = (struct grub_ntfs_data *) grub_zalloc (sizeof (*data));
897 if (!data)
898 goto fail;
900 data->disk = disk;
902 /* Read the BPB. */
903 if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
904 goto fail;
906 if (grub_memcmp ((char *) &bpb.oem_name, "NTFS", 4) != 0
907 || bpb.sectors_per_cluster == 0
908 || (bpb.sectors_per_cluster & (bpb.sectors_per_cluster - 1)) != 0
909 || bpb.bytes_per_sector == 0
910 || (bpb.bytes_per_sector & (bpb.bytes_per_sector - 1)) != 0)
911 goto fail;
913 spc = (((grub_uint32_t) bpb.sectors_per_cluster
914 * (grub_uint32_t) grub_le_to_cpu16 (bpb.bytes_per_sector))
915 >> GRUB_NTFS_BLK_SHR);
916 if (spc == 0)
917 goto fail;
919 for (data->log_spc = 0; (1U << data->log_spc) < spc; data->log_spc++);
921 if (bpb.clusters_per_mft > 0)
922 data->mft_size = ((grub_disk_addr_t) bpb.clusters_per_mft) << data->log_spc;
923 else if (-bpb.clusters_per_mft < GRUB_NTFS_BLK_SHR || -bpb.clusters_per_mft >= 31)
924 goto fail;
925 else
926 data->mft_size = 1ULL << (-bpb.clusters_per_mft - GRUB_NTFS_BLK_SHR);
928 if (bpb.clusters_per_index > 0)
929 data->idx_size = (((grub_disk_addr_t) bpb.clusters_per_index)
930 << data->log_spc);
931 else if (-bpb.clusters_per_index < GRUB_NTFS_BLK_SHR || -bpb.clusters_per_index >= 31)
932 goto fail;
933 else
934 data->idx_size = 1ULL << (-bpb.clusters_per_index - GRUB_NTFS_BLK_SHR);
936 data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) << data->log_spc;
938 if ((data->mft_size > GRUB_NTFS_MAX_MFT) || (data->idx_size > GRUB_NTFS_MAX_IDX))
939 goto fail;
941 data->mmft.data = data;
942 data->cmft.data = data;
944 data->mmft.buf = grub_malloc (data->mft_size << GRUB_NTFS_BLK_SHR);
945 if (!data->mmft.buf)
946 goto fail;
948 if (grub_disk_read
949 (disk, data->mft_start, 0, data->mft_size << GRUB_NTFS_BLK_SHR, data->mmft.buf))
950 goto fail;
952 data->uuid = grub_le_to_cpu64 (bpb.num_serial);
954 if (fixup (data->mmft.buf, data->mft_size, (const grub_uint8_t *) "FILE"))
955 goto fail;
957 if (!locate_attr (&data->mmft.attr, &data->mmft, GRUB_NTFS_AT_DATA))
958 goto fail;
960 if (init_file (&data->cmft, GRUB_NTFS_FILE_ROOT))
961 goto fail;
963 return data;
965 fail:
966 grub_error (GRUB_ERR_BAD_FS, "not an ntfs filesystem");
968 if (data)
970 free_file (&data->mmft);
971 free_file (&data->cmft);
972 grub_free (data);
974 return 0;
977 /* Context for grub_ntfs_dir. */
978 struct grub_ntfs_dir_ctx
980 grub_fs_dir_hook_t hook;
981 void *hook_data;
984 /* Helper for grub_ntfs_dir. */
985 static int
986 grub_ntfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
987 grub_fshelp_node_t node, void *data)
989 struct grub_ntfs_dir_ctx *ctx = data;
990 struct grub_dirhook_info info;
992 grub_memset (&info, 0, sizeof (info));
993 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
994 info.mtimeset = 1;
995 info.mtime = grub_divmod64 (node->mtime, 10000000, 0)
996 - 86400ULL * 365 * (1970 - 1601)
997 - 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
998 grub_free (node);
999 return ctx->hook (filename, &info, ctx->hook_data);
1002 static grub_err_t
1003 grub_ntfs_dir (grub_device_t device, const char *path,
1004 grub_fs_dir_hook_t hook, void *hook_data)
1006 struct grub_ntfs_dir_ctx ctx = { hook, hook_data };
1007 struct grub_ntfs_data *data = 0;
1008 struct grub_fshelp_node *fdiro = 0;
1010 grub_dl_ref (my_mod);
1012 data = grub_ntfs_mount (device->disk);
1013 if (!data)
1014 goto fail;
1016 grub_fshelp_find_file (path, &data->cmft, &fdiro, grub_ntfs_iterate_dir,
1017 grub_ntfs_read_symlink, GRUB_FSHELP_DIR);
1019 if (grub_errno)
1020 goto fail;
1022 grub_ntfs_iterate_dir (fdiro, grub_ntfs_dir_iter, &ctx);
1024 fail:
1025 if ((fdiro) && (fdiro != &data->cmft))
1027 free_file (fdiro);
1028 grub_free (fdiro);
1030 if (data)
1032 free_file (&data->mmft);
1033 free_file (&data->cmft);
1034 grub_free (data);
1037 grub_dl_unref (my_mod);
1039 return grub_errno;
1042 static grub_err_t
1043 grub_ntfs_open (grub_file_t file, const char *name)
1045 struct grub_ntfs_data *data = 0;
1046 struct grub_fshelp_node *mft = 0;
1048 grub_dl_ref (my_mod);
1050 data = grub_ntfs_mount (file->device->disk);
1051 if (!data)
1052 goto fail;
1054 grub_fshelp_find_file (name, &data->cmft, &mft, grub_ntfs_iterate_dir,
1055 grub_ntfs_read_symlink, GRUB_FSHELP_REG);
1057 if (grub_errno)
1058 goto fail;
1060 if (mft != &data->cmft)
1062 free_file (&data->cmft);
1063 grub_memcpy (&data->cmft, mft, sizeof (*mft));
1064 grub_free (mft);
1065 if (!data->cmft.inode_read)
1067 if (init_file (&data->cmft, data->cmft.ino))
1068 goto fail;
1072 file->size = data->cmft.size;
1073 file->data = data;
1074 file->offset = 0;
1076 return 0;
1078 fail:
1079 if (data)
1081 free_file (&data->mmft);
1082 free_file (&data->cmft);
1083 grub_free (data);
1086 grub_dl_unref (my_mod);
1088 return grub_errno;
1091 static grub_ssize_t
1092 grub_ntfs_read (grub_file_t file, char *buf, grub_size_t len)
1094 struct grub_ntfs_file *mft;
1096 mft = &((struct grub_ntfs_data *) file->data)->cmft;
1097 if (file->read_hook)
1098 mft->attr.save_pos = 1;
1100 read_attr (&mft->attr, (grub_uint8_t *) buf, file->offset, len, 1,
1101 file->read_hook, file->read_hook_data);
1102 return (grub_errno) ? -1 : (grub_ssize_t) len;
1105 static grub_err_t
1106 grub_ntfs_close (grub_file_t file)
1108 struct grub_ntfs_data *data;
1110 data = file->data;
1112 if (data)
1114 free_file (&data->mmft);
1115 free_file (&data->cmft);
1116 grub_free (data);
1119 grub_dl_unref (my_mod);
1121 return grub_errno;
1124 static grub_err_t
1125 grub_ntfs_label (grub_device_t device, char **label)
1127 struct grub_ntfs_data *data = 0;
1128 struct grub_fshelp_node *mft = 0;
1129 grub_uint8_t *pa;
1131 grub_dl_ref (my_mod);
1133 *label = 0;
1135 data = grub_ntfs_mount (device->disk);
1136 if (!data)
1137 goto fail;
1139 grub_fshelp_find_file ("/$Volume", &data->cmft, &mft, grub_ntfs_iterate_dir,
1140 0, GRUB_FSHELP_REG);
1142 if (grub_errno)
1143 goto fail;
1145 if (!mft->inode_read)
1147 mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
1148 if (mft->buf == NULL)
1149 goto fail;
1151 if (read_mft (mft->data, mft->buf, mft->ino))
1152 goto fail;
1155 init_attr (&mft->attr, mft);
1156 pa = find_attr (&mft->attr, GRUB_NTFS_AT_VOLUME_NAME);
1157 if ((pa) && (pa[8] == 0) && (u32at (pa, 0x10)))
1159 int len;
1161 len = u32at (pa, 0x10) / 2;
1162 pa += u16at (pa, 0x14);
1163 *label = get_utf8 (pa, len);
1166 fail:
1167 if ((mft) && (mft != &data->cmft))
1169 free_file (mft);
1170 grub_free (mft);
1172 if (data)
1174 free_file (&data->mmft);
1175 free_file (&data->cmft);
1176 grub_free (data);
1179 grub_dl_unref (my_mod);
1181 return grub_errno;
1184 static grub_err_t
1185 grub_ntfs_uuid (grub_device_t device, char **uuid)
1187 struct grub_ntfs_data *data;
1188 grub_disk_t disk = device->disk;
1190 grub_dl_ref (my_mod);
1192 data = grub_ntfs_mount (disk);
1193 if (data)
1195 char *ptr;
1196 *uuid = grub_xasprintf ("%016llx", (unsigned long long) data->uuid);
1197 if (*uuid)
1198 for (ptr = *uuid; *ptr; ptr++)
1199 *ptr = grub_toupper (*ptr);
1200 free_file (&data->mmft);
1201 free_file (&data->cmft);
1202 grub_free (data);
1204 else
1205 *uuid = NULL;
1207 grub_dl_unref (my_mod);
1209 return grub_errno;
1212 static struct grub_fs grub_ntfs_fs =
1214 .name = "ntfs",
1215 .dir = grub_ntfs_dir,
1216 .open = grub_ntfs_open,
1217 .read = grub_ntfs_read,
1218 .close = grub_ntfs_close,
1219 .label = grub_ntfs_label,
1220 .uuid = grub_ntfs_uuid,
1221 #ifdef GRUB_UTIL
1222 .reserved_first_sector = 1,
1223 .blocklist_install = 1,
1224 #endif
1225 .next = 0
1228 GRUB_MOD_INIT (ntfs)
1230 grub_fs_register (&grub_ntfs_fs);
1231 my_mod = mod;
1234 GRUB_MOD_FINI (ntfs)
1236 grub_fs_unregister (&grub_ntfs_fs);