31
33
static grub_dl_t my_mod;
33
ntfscomp_func_t grub_ntfscomp_func;
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;
36
fixup (struct grub_ntfs_data *data, char *buf, int len, char *magic)
58
fixup (char *buf, int len, const char *magic)
64
COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE);
42
66
if (grub_memcmp (buf, magic, 4))
43
67
return grub_error (GRUB_ERR_BAD_FS, "%s label not found", magic);
45
69
ss = u16at (buf, 6) - 1;
46
if (ss * (int) data->blocksize != len * GRUB_DISK_SECTOR_SIZE)
47
return grub_error (GRUB_ERR_BAD_FS, "size not match",
48
ss * (int) data->blocksize,
49
len * GRUB_DISK_SECTOR_SIZE);
71
return grub_error (GRUB_ERR_BAD_FS, "size not match");
50
72
pu = buf + u16at (buf, 4);
51
73
us = u16at (pu, 0);
55
buf += data->blocksize;
77
buf += GRUB_DISK_SECTOR_SIZE;
57
79
if (u16at (buf, 0) != us)
58
80
return grub_error (GRUB_ERR_BAD_FS, "fixup signature not match");
59
v16at (buf, 0) = v16at (pu, 0);
87
110
init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft)
90
at->flags = (mft == &mft->data->mmft) ? AF_MMFT : 0;
113
at->flags = (mft == &mft->data->mmft) ? GRUB_NTFS_AF_MMFT : 0;
91
114
at->attr_nxt = mft->buf + u16at (mft->buf, 0x14);
92
115
at->attr_end = at->emft_buf = at->edat_buf = at->sbuf = NULL;
117
if (at->flags & AF_MMFT)
140
if (at->flags & GRUB_NTFS_AF_MMFT)
119
142
if ((grub_disk_read
120
(at->mft->data->disk, v32at (at->attr_cur, 0x10), 0,
143
(at->mft->data->disk, u32at (at->attr_cur, 0x10), 0,
121
144
512, at->emft_buf))
124
(at->mft->data->disk, v32at (at->attr_cur, 0x14), 0,
147
(at->mft->data->disk, u32at (at->attr_cur, 0x14), 0,
125
148
512, at->emft_buf + 512)))
129
(at->mft->data, at->emft_buf, at->mft->data->mft_size,
151
if (fixup (at->emft_buf, at->mft->data->mft_size, "FILE"))
160
181
while ((unsigned char) *at->attr_cur != 0xFF)
162
183
at->attr_nxt += u16at (at->attr_cur, 4);
163
if ((unsigned char) *at->attr_cur == AT_ATTRIBUTE_LIST)
184
if ((unsigned char) *at->attr_cur == GRUB_NTFS_AT_ATTRIBUTE_LIST)
164
185
at->attr_end = at->attr_cur;
165
186
if (((unsigned char) *at->attr_cur == attr) || (attr == 0))
166
187
return at->attr_cur;
209
230
if (at->attr_nxt >= at->attr_end)
212
if ((at->flags & AF_MMFT) && (attr == AT_DATA))
233
if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA))
214
at->flags |= AF_GPOS;
235
at->flags |= GRUB_NTFS_AF_GPOS;
215
236
at->attr_cur = at->attr_nxt;
216
237
pa = at->attr_cur;
217
v32at (pa, 0x10) = at->mft->data->mft_start;
218
v32at (pa, 0x14) = at->mft->data->mft_start + 1;
238
grub_set_unaligned32 ((char *) pa + 0x10,
239
grub_cpu_to_le32 (at->mft->data->mft_start));
240
grub_set_unaligned32 ((char *) pa + 0x14,
241
grub_cpu_to_le32 (at->mft->data->mft_start
219
243
pa = at->attr_nxt + u16at (pa, 4);
220
244
while (pa < at->attr_end)
226
u32at (pa, 0x10) * (at->mft->data->mft_size << BLK_SHR),
227
at->mft->data->mft_size << BLK_SHR, 0, 0))
250
u32at (pa, 0x10) * (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR),
251
at->mft->data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0))
229
253
pa += u16at (pa, 4);
231
255
at->attr_nxt = at->attr_cur;
232
at->flags &= ~AF_GPOS;
256
at->flags &= ~GRUB_NTFS_AF_GPOS;
245
269
init_attr (at, mft);
246
if ((pa = find_attr (at, attr)) == NULL)
270
pa = find_attr (at, attr);
248
if ((at->flags & AF_ALST) == 0)
273
if ((at->flags & GRUB_NTFS_AF_ALST) == 0)
252
if ((pa = find_attr (at, attr)) == NULL)
277
pa = find_attr (at, attr);
254
if (at->flags & AF_ALST)
280
if (at->flags & GRUB_NTFS_AF_ALST)
257
283
grub_errno = GRUB_ERR_NONE;
379
if (u16at (pa, 0xC) & FLAG_COMPRESSED)
380
ctx->flags |= RF_COMP;
405
if (u16at (pa, 0xC) & GRUB_NTFS_FLAG_COMPRESSED)
406
ctx->flags |= GRUB_NTFS_RF_COMP;
382
ctx->flags &= ~RF_COMP;
408
ctx->flags &= ~GRUB_NTFS_RF_COMP;
383
409
ctx->cur_run = pa + u16at (pa, 0x20);
385
if (ctx->flags & RF_COMP)
411
if (ctx->flags & GRUB_NTFS_RF_COMP)
388
414
return grub_error (GRUB_ERR_BAD_FS, "attribute can\'t be compressed");
392
if ((ofs & (~(COM_LEN - 1))) == at->save_pos)
418
if ((ofs & (~(GRUB_NTFS_COM_LEN - 1))) == at->save_pos)
394
420
grub_disk_addr_t n;
396
n = COM_LEN - (ofs - at->save_pos);
422
n = GRUB_NTFS_COM_LEN - (ofs - at->save_pos);
411
at->sbuf = grub_malloc (COM_LEN);
437
at->sbuf = grub_malloc (GRUB_NTFS_COM_LEN);
412
438
if (at->sbuf == NULL)
413
439
return grub_errno;
414
440
at->save_pos = 1;
417
vcn = ctx->target_vcn = (ofs >> COM_LOG_LEN) * (COM_SEC / ctx->comp.spc);
418
ctx->target_vcn &= ~0xF;
443
vcn = ctx->target_vcn = (ofs >> GRUB_NTFS_COM_LOG_LEN) * (GRUB_NTFS_COM_SEC / ctx->comp.spc);
444
ctx->target_vcn &= ~0xFULL;
421
vcn = ctx->target_vcn = grub_divmod64 (ofs >> BLK_SHR, ctx->comp.spc, 0);
447
vcn = ctx->target_vcn = grub_divmod64 (ofs >> GRUB_NTFS_BLK_SHR, ctx->comp.spc, 0);
423
449
ctx->next_vcn = u32at (pa, 0x10);
424
450
ctx->curr_lcn = 0;
428
454
return grub_errno;
431
if (at->flags & AF_GPOS)
457
if (at->flags & GRUB_NTFS_AF_GPOS)
433
459
grub_disk_addr_t st0, st1;
436
grub_divmod64 (ofs >> BLK_SHR, ctx->comp.spc, &m);
462
grub_divmod64 (ofs >> GRUB_NTFS_BLK_SHR, ctx->comp.spc, &m);
439
465
(ctx->target_vcn - ctx->curr_vcn + ctx->curr_lcn) * ctx->comp.spc + m;
445
471
return grub_errno;
446
472
st1 = ctx->curr_lcn * ctx->comp.spc;
448
v32at (dest, 0) = st0;
449
v32at (dest, 4) = st1;
474
grub_set_unaligned32 (dest, grub_cpu_to_le32 (st0));
475
grub_set_unaligned32 (dest + 4, grub_cpu_to_le32 (st1));
453
if (!(ctx->flags & RF_COMP))
479
if (!(ctx->flags & GRUB_NTFS_RF_COMP))
455
481
unsigned int pow;
457
483
if (!grub_fshelp_log2blksize (ctx->comp.spc, &pow))
458
484
grub_fshelp_read_file (ctx->comp.disk, (grub_fshelp_node_t) ctx,
459
485
read_hook, ofs, len, dest,
460
grub_ntfs_read_block, ofs + len, pow);
486
grub_ntfs_read_block, ofs + len, pow, 0);
461
487
return grub_errno;
464
490
return (grub_ntfscomp_func) ? grub_ntfscomp_func (at, dest, ofs, len, ctx,
466
grub_error (GRUB_ERR_BAD_FS, "ntfscomp module not loaded");
492
grub_error (GRUB_ERR_BAD_FS, N_("module `%s' isn't loaded"),
469
496
static grub_err_t
481
508
save_cur = at->attr_cur;
482
509
at->attr_nxt = at->attr_cur;
483
510
attr = (unsigned char) *at->attr_nxt;
484
if (at->flags & AF_ALST)
511
if (at->flags & GRUB_NTFS_AF_ALST)
487
514
grub_disk_addr_t vcn;
489
vcn = grub_divmod64 (ofs, at->mft->data->spc << BLK_SHR, 0);
516
/* If compression is possible make sure that we include possible
517
compressed block size. */
518
if (GRUB_NTFS_COM_SEC >= at->mft->data->spc)
519
vcn = ((ofs >> GRUB_NTFS_COM_LOG_LEN)
520
* (GRUB_NTFS_COM_SEC / at->mft->data->spc)) & ~0xFULL;
522
vcn = grub_divmod64 (ofs, at->mft->data->spc << GRUB_NTFS_BLK_SHR, 0);
490
523
pa = at->attr_nxt + u16at (at->attr_nxt, 4);
491
524
while (pa < at->attr_end)
513
546
read_mft (struct grub_ntfs_data *data, char *buf, grub_uint32_t mftno)
516
(&data->mmft.attr, buf, mftno * ((grub_disk_addr_t) data->mft_size << BLK_SHR),
517
data->mft_size << BLK_SHR, 0, 0))
549
(&data->mmft.attr, buf, mftno * ((grub_disk_addr_t) data->mft_size << GRUB_NTFS_BLK_SHR),
550
data->mft_size << GRUB_NTFS_BLK_SHR, 0, 0))
518
551
return grub_error (GRUB_ERR_BAD_FS, "read MFT 0x%X fails", mftno);
519
return fixup (data, buf, data->mft_size, "FILE");
552
return fixup (buf, data->mft_size, "FILE");
522
555
static grub_err_t
606
(u32at (pos, 0x48) & ATTR_DIRECTORY) ? GRUB_FSHELP_DIR :
639
attr = u32at (pos, 0x48);
640
if (attr & GRUB_NTFS_ATTR_REPARSE)
641
type = GRUB_FSHELP_SYMLINK;
642
else if (attr & GRUB_NTFS_ATTR_DIRECTORY)
643
type = GRUB_FSHELP_DIR;
645
type = GRUB_FSHELP_REG;
609
647
fdiro = grub_zalloc (sizeof (struct grub_ntfs_file));
613
651
fdiro->data = diro->data;
614
652
fdiro->ino = u32at (pos, 0);
653
fdiro->mtime = u64at (pos, 0x20);
616
ustr = grub_malloc (ns * 4 + 1);
655
ustr = grub_malloc (ns * GRUB_MAX_UTF8_PER_UTF16 + 1);
617
656
if (ustr == NULL)
619
*grub_utf16_to_utf8 ((grub_uint8_t *) ustr, (grub_uint16_t *) np,
659
grub_uint16_t tmp[ns];
661
for (i = 0; i < ns; i++)
662
tmp[i] = grub_le_to_cpu16 (grub_get_unaligned16 ((char *) np
665
*grub_utf16_to_utf8 ((grub_uint8_t *) ustr, tmp, ns) = '\0';
623
668
type |= GRUB_FSHELP_CASE_INSENSITIVE;
683
struct symlink_descriptor
686
grub_uint32_t total_len;
691
} __attribute__ ((packed));
694
grub_ntfs_read_symlink (grub_fshelp_node_t node)
696
struct grub_ntfs_file *mft;
697
struct symlink_descriptor symdesc;
699
grub_uint16_t *buf16;
706
mft = (struct grub_ntfs_file *) node;
708
mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
709
if (mft->buf == NULL)
712
if (read_mft (mft->data, mft->buf, mft->ino))
715
pa = locate_attr (&mft->attr, mft, GRUB_NTFS_AT_SYMLINK);
718
grub_error (GRUB_ERR_BAD_FS, "no $SYMLINK in MFT 0x%X", mft->ino);
722
err = read_attr (&mft->attr, (char *) &symdesc, 0,
723
sizeof (struct symlink_descriptor), 1, 0);
727
switch (grub_cpu_to_le32 (symdesc.type))
730
off = (sizeof (struct symlink_descriptor) + 4
731
+ grub_cpu_to_le32 (symdesc.off1));
732
len = grub_cpu_to_le32 (symdesc.len1);
735
off = (sizeof (struct symlink_descriptor)
736
+ grub_cpu_to_le32 (symdesc.off1));
737
len = grub_cpu_to_le32 (symdesc.len1);
740
grub_error (GRUB_ERR_BAD_FS, "symlink type invalid (%x)",
741
grub_cpu_to_le32 (symdesc.type));
745
buf16 = grub_malloc (len);
749
err = read_attr (&mft->attr, (char *) buf16, off, len, 1, 0);
753
buf = grub_malloc (len * 2 + 1);
760
for (i = 0; i < len / 2; i++)
762
buf16[i] = grub_le_to_cpu16 (buf16[i]);
763
if (buf16[i] == '\\')
767
end = (char *) grub_utf16_to_utf8 ((grub_uint8_t *) buf, buf16, len / 2);
769
/* Split the sequence to avoid GCC thinking that this is a trigraph. */
770
if (grub_memcmp (buf, "/?" "?/", 4) == 0 && buf[5] == ':' && buf[6] == '/'
771
&& grub_isalpha (buf[4]))
773
grub_memmove (buf, buf + 6, end - buf + 1 - 6);
639
780
grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
640
781
int NESTED_FUNC_ATTR
734
cur_pos = locate_attr (at, mft, AT_INDEX_ALLOCATION);
876
cur_pos = locate_attr (at, mft, GRUB_NTFS_AT_INDEX_ALLOCATION);
735
877
while (cur_pos != NULL)
737
879
/* Non-resident, Namelen=4, Offset=0x40, Flags=0, Name="$I30" */
739
881
(u32at (cur_pos, 0x40) == 0x490024) &&
740
882
(u32at (cur_pos, 0x44) == 0x300033))
742
cur_pos = find_attr (at, AT_INDEX_ALLOCATION);
884
cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ALLOCATION);
745
887
if ((!cur_pos) && (bitmap))
765
(at, indx, i * (mft->data->idx_size << BLK_SHR),
766
(mft->data->idx_size << BLK_SHR), 0, 0))
767
|| (fixup (mft->data, indx, mft->data->idx_size, "INDX")))
907
(at, indx, i * (mft->data->idx_size << GRUB_NTFS_BLK_SHR),
908
(mft->data->idx_size << GRUB_NTFS_BLK_SHR), 0, 0))
909
|| (fixup (indx, mft->data->idx_size, "INDX")))
769
911
ret = list_file (mft, &indx[0x18 + u16at (indx, 0x18)], hook);
806
948
if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
809
if (grub_memcmp ((char *) &bpb.oem_name, "NTFS", 4))
951
if (grub_memcmp ((char *) &bpb.oem_name, "NTFS", 4) != 0
952
|| bpb.sectors_per_cluster == 0
953
|| (bpb.sectors_per_cluster & (bpb.sectors_per_cluster - 1)) != 0
954
|| bpb.bytes_per_sector == 0
955
|| (bpb.bytes_per_sector & (bpb.bytes_per_sector - 1)) != 0)
812
data->blocksize = grub_le_to_cpu16 (bpb.bytes_per_sector);
813
data->spc = bpb.sectors_per_cluster * (data->blocksize >> BLK_SHR);
958
data->spc = (((grub_uint32_t) bpb.sectors_per_cluster
959
* (grub_uint32_t) grub_le_to_cpu16 (bpb.bytes_per_sector))
960
>> GRUB_NTFS_BLK_SHR);
815
964
if (bpb.clusters_per_mft > 0)
816
965
data->mft_size = data->spc * bpb.clusters_per_mft;
818
data->mft_size = 1 << (-bpb.clusters_per_mft - BLK_SHR);
967
data->mft_size = 1 << (-bpb.clusters_per_mft - GRUB_NTFS_BLK_SHR);
820
969
if (bpb.clusters_per_index > 0)
821
970
data->idx_size = data->spc * bpb.clusters_per_index;
823
data->idx_size = 1 << (-bpb.clusters_per_index - BLK_SHR);
972
data->idx_size = 1 << (-bpb.clusters_per_index - GRUB_NTFS_BLK_SHR);
825
974
data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) * data->spc;
827
if ((data->mft_size > MAX_MFT) || (data->idx_size > MAX_IDX))
976
if ((data->mft_size > GRUB_NTFS_MAX_MFT) || (data->idx_size > GRUB_NTFS_MAX_IDX))
830
979
data->mmft.data = data;
831
980
data->cmft.data = data;
833
data->mmft.buf = grub_malloc (data->mft_size << BLK_SHR);
982
data->mmft.buf = grub_malloc (data->mft_size << GRUB_NTFS_BLK_SHR);
834
983
if (!data->mmft.buf)
837
986
if (grub_disk_read
838
(disk, data->mft_start, 0, data->mft_size << BLK_SHR, data->mmft.buf))
987
(disk, data->mft_start, 0, data->mft_size << GRUB_NTFS_BLK_SHR, data->mmft.buf))
841
990
data->uuid = grub_le_to_cpu64 (bpb.num_serial);
843
if (fixup (data, data->mmft.buf, data->mft_size, "FILE"))
846
if (!locate_attr (&data->mmft.attr, &data->mmft, AT_DATA))
849
if (init_file (&data->cmft, FILE_ROOT))
992
if (fixup (data->mmft.buf, data->mft_size, "FILE"))
995
if (!locate_attr (&data->mmft.attr, &data->mmft, GRUB_NTFS_AT_DATA))
998
if (init_file (&data->cmft, GRUB_NTFS_FILE_ROOT))
882
1031
struct grub_dirhook_info info;
883
1032
grub_memset (&info, 0, sizeof (info));
884
1033
info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
1035
info.mtime = grub_divmod64 (node->mtime, 10000000, 0)
1036
- 86400ULL * 365 * (1970 - 1601)
1037
- 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
885
1038
grub_free (node);
886
1039
return hook (filename, &info);
1040
1193
len = u32at (pa, 0x10) / 2;
1041
1194
buf = grub_malloc (len * 4 + 1);
1042
1195
pa += u16at (pa, 0x14);
1043
*grub_utf16_to_utf8 ((grub_uint8_t *) buf, (grub_uint16_t *) pa, len) =
1197
grub_uint16_t tmp[len];
1199
for (i = 0; i < len; i++)
1200
tmp[i] = grub_le_to_cpu16 (grub_get_unaligned16 (pa + 2 * i));
1201
*grub_utf16_to_utf8 ((grub_uint8_t *) buf, tmp, len) =