]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/fs/ntfs.c
Support NTFS with large cluster size (greater than 64KB).
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / fs / ntfs.c
1 /* ntfs.c - NTFS filesystem */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007,2008,2009 Free Software Foundation, Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #define grub_fshelp_node grub_ntfs_file
21
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>
30
31 GRUB_MOD_LICENSE ("GPLv3+");
32
33 static grub_dl_t my_mod;
34
35 #define grub_fshelp_node grub_ntfs_file
36
37 static inline grub_uint16_t
38 u16at (void *ptr, grub_size_t ofs)
39 {
40 return grub_le_to_cpu16 (grub_get_unaligned16 ((char *) ptr + ofs));
41 }
42
43 static inline grub_uint32_t
44 u32at (void *ptr, grub_size_t ofs)
45 {
46 return grub_le_to_cpu32 (grub_get_unaligned32 ((char *) ptr + ofs));
47 }
48
49 static inline grub_uint64_t
50 u64at (void *ptr, grub_size_t ofs)
51 {
52 return grub_le_to_cpu64 (grub_get_unaligned64 ((char *) ptr + ofs));
53 }
54
55 grub_ntfscomp_func_t grub_ntfscomp_func;
56
57 static grub_err_t
58 fixup (grub_uint8_t *buf, grub_size_t len, const grub_uint8_t *magic)
59 {
60 grub_uint16_t ss;
61 grub_uint8_t *pu;
62 grub_uint16_t us;
63
64 COMPILE_TIME_ASSERT ((1 << GRUB_NTFS_BLK_SHR) == GRUB_DISK_SECTOR_SIZE);
65
66 if (grub_memcmp (buf, magic, 4))
67 return grub_error (GRUB_ERR_BAD_FS, "%s label not found", magic);
68
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)
76 {
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--;
84 }
85
86 return 0;
87 }
88
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);
96
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);
103
104 static void
105 init_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft)
106 {
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;
111 }
112
113 static void
114 free_attr (struct grub_ntfs_attr *at)
115 {
116 grub_free (at->emft_buf);
117 grub_free (at->edat_buf);
118 grub_free (at->sbuf);
119 }
120
121 static grub_uint8_t *
122 find_attr (struct grub_ntfs_attr *at, grub_uint8_t attr)
123 {
124 if (at->flags & GRUB_NTFS_AF_ALST)
125 {
126 retry:
127 while (at->attr_nxt < at->attr_end)
128 {
129 at->attr_cur = at->attr_nxt;
130 at->attr_nxt += u16at (at->attr_cur, 4);
131 if ((*at->attr_cur == attr) || (attr == 0))
132 {
133 grub_uint8_t *new_pos;
134
135 if (at->flags & GRUB_NTFS_AF_MMFT)
136 {
137 if ((grub_disk_read
138 (at->mft->data->disk, u32at (at->attr_cur, 0x10), 0,
139 512, at->emft_buf))
140 ||
141 (grub_disk_read
142 (at->mft->data->disk, u32at (at->attr_cur, 0x14), 0,
143 512, at->emft_buf + 512)))
144 return NULL;
145
146 if (fixup (at->emft_buf, at->mft->data->mft_size,
147 (const grub_uint8_t *) "FILE"))
148 return NULL;
149 }
150 else
151 {
152 if (read_mft (at->mft->data, at->emft_buf,
153 u32at (at->attr_cur, 0x10)))
154 return NULL;
155 }
156
157 new_pos = &at->emft_buf[u16at (at->emft_buf, 0x14)];
158 while (*new_pos != 0xFF)
159 {
160 if ((*new_pos == *at->attr_cur)
161 && (u16at (new_pos, 0xE) == u16at (at->attr_cur, 0x18)))
162 {
163 return new_pos;
164 }
165 new_pos += u16at (new_pos, 4);
166 }
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;
171 }
172 }
173 return NULL;
174 }
175 at->attr_cur = at->attr_nxt;
176 while (*at->attr_cur != 0xFF)
177 {
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;
184 }
185 if (at->attr_end)
186 {
187 grub_uint8_t *pa;
188
189 at->emft_buf = grub_malloc (at->mft->data->mft_size << GRUB_NTFS_BLK_SHR);
190 if (at->emft_buf == NULL)
191 return NULL;
192
193 pa = at->attr_end;
194 if (pa[8])
195 {
196 grub_uint32_t n;
197
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))
205 {
206 grub_error (GRUB_ERR_BAD_FS,
207 "fail to read non-resident attribute list");
208 return NULL;
209 }
210 at->attr_nxt = at->edat_buf;
211 at->attr_end = at->edat_buf + u32at (pa, 0x30);
212 }
213 else
214 {
215 at->attr_nxt = at->attr_end + u16at (pa, 0x14);
216 at->attr_end = at->attr_end + u32at (pa, 4);
217 }
218 at->flags |= GRUB_NTFS_AF_ALST;
219 while (at->attr_nxt < at->attr_end)
220 {
221 if ((*at->attr_nxt == attr) || (attr == 0))
222 break;
223 at->attr_nxt += u16at (at->attr_nxt, 4);
224 }
225 if (at->attr_nxt >= at->attr_end)
226 return NULL;
227
228 if ((at->flags & GRUB_NTFS_AF_MMFT) && (attr == GRUB_NTFS_AT_DATA))
229 {
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)
240 {
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);
249 }
250 at->attr_nxt = at->attr_cur;
251 at->flags &= ~GRUB_NTFS_AF_GPOS;
252 }
253 goto retry;
254 }
255 return NULL;
256 }
257
258 static grub_uint8_t *
259 locate_attr (struct grub_ntfs_attr *at, struct grub_ntfs_file *mft,
260 grub_uint8_t attr)
261 {
262 grub_uint8_t *pa;
263
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)
269 {
270 while (1)
271 {
272 pa = find_attr (at, attr);
273 if (pa == NULL)
274 break;
275 if (at->flags & GRUB_NTFS_AF_ALST)
276 return pa;
277 }
278 grub_errno = GRUB_ERR_NONE;
279 free_attr (at);
280 init_attr (at, mft);
281 pa = find_attr (at, attr);
282 }
283 return pa;
284 }
285
286 static grub_disk_addr_t
287 read_run_data (const grub_uint8_t *run, int nn, int sig)
288 {
289 grub_uint64_t r = 0;
290
291 if (sig && nn && (run[nn - 1] & 0x80))
292 r = -1;
293
294 grub_memcpy (&r, run, nn);
295
296 return grub_le_to_cpu64 (r);
297 }
298
299 grub_err_t
300 grub_ntfs_read_run_list (struct grub_ntfs_rlst * ctx)
301 {
302 grub_uint8_t c1, c2;
303 grub_disk_addr_t val;
304 grub_uint8_t *run;
305
306 run = ctx->cur_run;
307 retry:
308 c1 = ((*run) & 0x7);
309 c2 = ((*run) >> 4) & 0x7;
310 run++;
311 if (!c1)
312 {
313 if ((ctx->attr) && (ctx->attr->flags & GRUB_NTFS_AF_ALST))
314 {
315 grub_disk_read_hook_t save_hook;
316
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)
322 {
323 if (run[8] == 0)
324 return grub_error (GRUB_ERR_BAD_FS,
325 "$DATA should be non-resident");
326
327 run += u16at (run, 0x20);
328 ctx->curr_lcn = 0;
329 goto retry;
330 }
331 }
332 return grub_error (GRUB_ERR_BAD_FS, "run list overflown");
333 }
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;
346 }
347
348 static grub_disk_addr_t
349 grub_ntfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t block)
350 {
351 struct grub_ntfs_rlst *ctx;
352
353 ctx = (struct grub_ntfs_rlst *) node;
354 if (block >= ctx->next_vcn)
355 {
356 if (grub_ntfs_read_run_list (ctx))
357 return -1;
358 return ctx->curr_lcn;
359 }
360 else
361 return (ctx->flags & GRUB_NTFS_RF_BLNK) ? 0 : (block -
362 ctx->curr_vcn + ctx->curr_lcn);
363 }
364
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)
369 {
370 struct grub_ntfs_rlst cc, *ctx;
371
372 if (len == 0)
373 return 0;
374
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;
380
381 if (read_hook == grub_file_progress_hook)
382 ctx->file = read_hook_data;
383
384 if (pa[8] == 0)
385 {
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;
390 }
391
392 ctx->cur_run = pa + u16at (pa, 0x20);
393
394 ctx->next_vcn = u32at (pa, 0x10);
395 ctx->curr_lcn = 0;
396
397 if ((pa[0xC] & GRUB_NTFS_FLAG_COMPRESSED)
398 && !(at->flags & GRUB_NTFS_AF_GPOS))
399 {
400 if (!cached)
401 return grub_error (GRUB_ERR_BAD_FS, "attribute can\'t be compressed");
402
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");
406 }
407
408 ctx->target_vcn = ofs >> (GRUB_NTFS_BLK_SHR + ctx->comp.log_spc);
409 while (ctx->next_vcn <= ctx->target_vcn)
410 {
411 if (grub_ntfs_read_run_list (ctx))
412 return grub_errno;
413 }
414
415 if (at->flags & GRUB_NTFS_AF_GPOS)
416 {
417 grub_disk_addr_t st0, st1;
418 grub_uint64_t m;
419
420 m = (ofs >> GRUB_NTFS_BLK_SHR) & ((1 << ctx->comp.log_spc) - 1);
421
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)
427 {
428 if (grub_ntfs_read_run_list (ctx))
429 return grub_errno;
430 st1 = ctx->curr_lcn << ctx->comp.log_spc;
431 }
432 grub_set_unaligned32 (dest, grub_cpu_to_le32 (st0));
433 grub_set_unaligned32 (dest + 4, grub_cpu_to_le32 (st1));
434 return 0;
435 }
436
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;
443 }
444
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)
449 {
450 grub_uint8_t *save_cur;
451 grub_uint8_t attr;
452 grub_uint8_t *pp;
453 grub_err_t ret;
454
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)
459 {
460 grub_uint8_t *pa;
461 grub_disk_addr_t vcn;
462
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)
472 {
473 if (*pa != attr)
474 break;
475 if (u32at (pa, 8) > vcn)
476 break;
477 at->attr_nxt = pa;
478 pa += u16at (pa, 4);
479 }
480 }
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;
491 }
492
493 static grub_err_t
494 read_mft (struct grub_ntfs_data *data, grub_uint8_t *buf, grub_uint64_t mftno)
495 {
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");
501 }
502
503 static grub_err_t
504 init_file (struct grub_ntfs_file *mft, grub_uint64_t mftno)
505 {
506 unsigned short flag;
507
508 mft->inode_read = 1;
509
510 mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
511 if (mft->buf == NULL)
512 return grub_errno;
513
514 if (read_mft (mft->data, mft->buf, mftno))
515 return grub_errno;
516
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);
521
522 if ((flag & 2) == 0)
523 {
524 grub_uint8_t *pa;
525
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);
530
531 if (!pa[8])
532 mft->size = u32at (pa, 0x10);
533 else
534 mft->size = u64at (pa, 0x30);
535
536 if ((mft->attr.flags & GRUB_NTFS_AF_ALST) == 0)
537 mft->attr.attr_end = 0; /* Don't jump to attribute list */
538 }
539 else
540 init_attr (&mft->attr, mft);
541
542 return 0;
543 }
544
545 static void
546 free_file (struct grub_ntfs_file *mft)
547 {
548 free_attr (&mft->attr);
549 grub_free (mft->buf);
550 }
551
552 static char *
553 get_utf8 (grub_uint8_t *in, grub_size_t len)
554 {
555 grub_uint8_t *buf;
556 grub_uint16_t *tmp;
557 grub_size_t i;
558
559 buf = grub_malloc (len * GRUB_MAX_UTF8_PER_UTF16 + 1);
560 tmp = grub_malloc (len * sizeof (tmp[0]));
561 if (!buf || !tmp)
562 {
563 grub_free (buf);
564 grub_free (tmp);
565 return NULL;
566 }
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;
572 }
573
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)
577 {
578 grub_uint8_t *np;
579 int ns;
580
581 while (1)
582 {
583 grub_uint8_t namespace;
584 char *ustr;
585
586 if (pos[0xC] & 2) /* end signature */
587 break;
588
589 np = pos + 0x50;
590 ns = *(np++);
591 namespace = *(np++);
592
593 /*
594 * Ignore files in DOS namespace, as they will reappear as Win32
595 * names.
596 */
597 if ((ns) && (namespace != 2))
598 {
599 enum grub_fshelp_filetype type;
600 struct grub_ntfs_file *fdiro;
601 grub_uint32_t attr;
602
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;
610
611 fdiro = grub_zalloc (sizeof (struct grub_ntfs_file));
612 if (!fdiro)
613 return 0;
614
615 fdiro->data = diro->data;
616 fdiro->ino = u64at (pos, 0) & 0xffffffffffffULL;
617 fdiro->mtime = u64at (pos, 0x20);
618
619 ustr = get_utf8 (np, ns);
620 if (ustr == NULL)
621 {
622 grub_free (fdiro);
623 return 0;
624 }
625 if (namespace)
626 type |= GRUB_FSHELP_CASE_INSENSITIVE;
627
628 if (hook (ustr, type, fdiro, hook_data))
629 {
630 grub_free (ustr);
631 return 1;
632 }
633
634 grub_free (ustr);
635 }
636 pos += u16at (pos, 8);
637 }
638 return 0;
639 }
640
641 struct symlink_descriptor
642 {
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;
650
651 static char *
652 grub_ntfs_read_symlink (grub_fshelp_node_t node)
653 {
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;
662
663 mft = (struct grub_ntfs_file *) node;
664
665 mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
666 if (mft->buf == NULL)
667 return NULL;
668
669 if (read_mft (mft->data, mft->buf, mft->ino))
670 return NULL;
671
672 pa = locate_attr (&mft->attr, mft, GRUB_NTFS_AT_SYMLINK);
673 if (pa == NULL)
674 {
675 grub_error (GRUB_ERR_BAD_FS, "no $SYMLINK in MFT 0x%llx",
676 (unsigned long long) mft->ino);
677 return NULL;
678 }
679
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;
684
685 switch (grub_cpu_to_le32 (symdesc.type))
686 {
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;
701 }
702
703 buf16 = grub_malloc (len);
704 if (!buf16)
705 return NULL;
706
707 err = read_attr (&mft->attr, buf16, off, len, 1, 0, 0);
708 if (err)
709 return NULL;
710
711 buf = get_utf8 (buf16, len / 2);
712 if (!buf)
713 {
714 grub_free (buf16);
715 return NULL;
716 }
717 grub_free (buf16);
718
719 for (end = buf; *end; end++)
720 if (*end == '\\')
721 *end = '/';
722
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]))
726 {
727 grub_memmove (buf, buf + 6, end - buf + 1 - 6);
728 end -= 6;
729 }
730 return buf;
731 }
732
733 static int
734 grub_ntfs_iterate_dir (grub_fshelp_node_t dir,
735 grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
736 {
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;
743
744 mft = (struct grub_ntfs_file *) dir;
745
746 if (!mft->inode_read)
747 {
748 if (init_file (mft, mft->ino))
749 return 0;
750 }
751
752 indx = NULL;
753 bmp = NULL;
754
755 at = &attr;
756 init_attr (at, mft);
757 while (1)
758 {
759 cur_pos = find_attr (at, GRUB_NTFS_AT_INDEX_ROOT);
760 if (cur_pos == NULL)
761 {
762 grub_error (GRUB_ERR_BAD_FS, "no $INDEX_ROOT");
763 goto done;
764 }
765
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;
775 }
776
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;
781
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)
787 {
788 int ofs;
789
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))
795 {
796 int is_resident = (cur_pos[8] == 0);
797
798 bitmap_len = ((is_resident) ? u32at (cur_pos, 0x10) :
799 u32at (cur_pos, 0x28));
800
801 bmp = grub_malloc (bitmap_len);
802 if (bmp == NULL)
803 goto done;
804
805 if (is_resident)
806 {
807 grub_memcpy (bmp, cur_pos + u16at (cur_pos, 0x14),
808 bitmap_len);
809 }
810 else
811 {
812 if (read_data (at, cur_pos, bmp, 0, bitmap_len, 0, 0, 0))
813 {
814 grub_error (GRUB_ERR_BAD_FS,
815 "fails to read non-resident $BITMAP");
816 goto done;
817 }
818 bitmap_len = u32at (cur_pos, 0x30);
819 }
820
821 bitmap = bmp;
822 break;
823 }
824 }
825
826 free_attr (at);
827 cur_pos = locate_attr (at, mft, GRUB_NTFS_AT_INDEX_ALLOCATION);
828 while (cur_pos != NULL)
829 {
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);
836 }
837
838 if ((!cur_pos) && (bitmap))
839 {
840 grub_error (GRUB_ERR_BAD_FS, "$BITMAP without $INDEX_ALLOCATION");
841 goto done;
842 }
843
844 if (bitmap)
845 {
846 grub_disk_addr_t i;
847 grub_uint8_t v;
848
849 indx = grub_malloc (mft->data->idx_size << GRUB_NTFS_BLK_SHR);
850 if (indx == NULL)
851 goto done;
852
853 v = 1;
854 for (i = 0; i < (grub_disk_addr_t)bitmap_len * 8; i++)
855 {
856 if (*bitmap & v)
857 {
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;
868 }
869 v <<= 1;
870 if (!v)
871 {
872 v = 1;
873 bitmap++;
874 }
875 }
876 }
877
878 done:
879 free_attr (at);
880 grub_free (indx);
881 grub_free (bmp);
882
883 return ret;
884 }
885
886 static struct grub_ntfs_data *
887 grub_ntfs_mount (grub_disk_t disk)
888 {
889 struct grub_ntfs_bpb bpb;
890 struct grub_ntfs_data *data = 0;
891 grub_uint32_t spc;
892 grub_uint32_t sectors_per_cluster;
893
894 if (!disk)
895 goto fail;
896
897 data = (struct grub_ntfs_data *) grub_zalloc (sizeof (*data));
898 if (!data)
899 goto fail;
900
901 data->disk = disk;
902
903 /* Read the BPB. */
904 if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
905 goto fail;
906
907 sectors_per_cluster = bpb.sectors_per_cluster;
908 if (sectors_per_cluster > 0x80)
909 sectors_per_cluster = 1U << (256U - bpb.sectors_per_cluster);
910
911 if (grub_memcmp ((char *) &bpb.oem_name, "NTFS", 4) != 0
912 || sectors_per_cluster == 0
913 || (sectors_per_cluster & (sectors_per_cluster - 1)) != 0
914 || bpb.bytes_per_sector == 0
915 || (bpb.bytes_per_sector & (bpb.bytes_per_sector - 1)) != 0)
916 goto fail;
917
918 spc = (((grub_uint32_t) sectors_per_cluster
919 * (grub_uint32_t) grub_le_to_cpu16 (bpb.bytes_per_sector))
920 >> GRUB_NTFS_BLK_SHR);
921 if (spc == 0)
922 goto fail;
923
924 for (data->log_spc = 0; (1U << data->log_spc) < spc; data->log_spc++);
925
926 if (bpb.clusters_per_mft > 0)
927 data->mft_size = ((grub_disk_addr_t) bpb.clusters_per_mft) << data->log_spc;
928 else if (-bpb.clusters_per_mft < GRUB_NTFS_BLK_SHR || -bpb.clusters_per_mft >= 31)
929 goto fail;
930 else
931 data->mft_size = 1ULL << (-bpb.clusters_per_mft - GRUB_NTFS_BLK_SHR);
932
933 if (bpb.clusters_per_index > 0)
934 data->idx_size = (((grub_disk_addr_t) bpb.clusters_per_index)
935 << data->log_spc);
936 else if (-bpb.clusters_per_index < GRUB_NTFS_BLK_SHR || -bpb.clusters_per_index >= 31)
937 goto fail;
938 else
939 data->idx_size = 1ULL << (-bpb.clusters_per_index - GRUB_NTFS_BLK_SHR);
940
941 data->mft_start = grub_le_to_cpu64 (bpb.mft_lcn) << data->log_spc;
942
943 if ((data->mft_size > GRUB_NTFS_MAX_MFT) || (data->idx_size > GRUB_NTFS_MAX_IDX))
944 goto fail;
945
946 data->mmft.data = data;
947 data->cmft.data = data;
948
949 data->mmft.buf = grub_malloc (data->mft_size << GRUB_NTFS_BLK_SHR);
950 if (!data->mmft.buf)
951 goto fail;
952
953 if (grub_disk_read
954 (disk, data->mft_start, 0, data->mft_size << GRUB_NTFS_BLK_SHR, data->mmft.buf))
955 goto fail;
956
957 data->uuid = grub_le_to_cpu64 (bpb.num_serial);
958
959 if (fixup (data->mmft.buf, data->mft_size, (const grub_uint8_t *) "FILE"))
960 goto fail;
961
962 if (!locate_attr (&data->mmft.attr, &data->mmft, GRUB_NTFS_AT_DATA))
963 goto fail;
964
965 if (init_file (&data->cmft, GRUB_NTFS_FILE_ROOT))
966 goto fail;
967
968 return data;
969
970 fail:
971 grub_error (GRUB_ERR_BAD_FS, "not an ntfs filesystem");
972
973 if (data)
974 {
975 free_file (&data->mmft);
976 free_file (&data->cmft);
977 grub_free (data);
978 }
979 return 0;
980 }
981
982 /* Context for grub_ntfs_dir. */
983 struct grub_ntfs_dir_ctx
984 {
985 grub_fs_dir_hook_t hook;
986 void *hook_data;
987 };
988
989 /* Helper for grub_ntfs_dir. */
990 static int
991 grub_ntfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
992 grub_fshelp_node_t node, void *data)
993 {
994 struct grub_ntfs_dir_ctx *ctx = data;
995 struct grub_dirhook_info info;
996
997 grub_memset (&info, 0, sizeof (info));
998 info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
999 info.mtimeset = 1;
1000 info.mtime = grub_divmod64 (node->mtime, 10000000, 0)
1001 - 86400ULL * 365 * (1970 - 1601)
1002 - 86400ULL * ((1970 - 1601) / 4) + 86400ULL * ((1970 - 1601) / 100);
1003 if (!info.dir)
1004 info.size = node->size;
1005 grub_free (node);
1006 return ctx->hook (filename, &info, ctx->hook_data);
1007 }
1008
1009 static grub_err_t
1010 grub_ntfs_dir (grub_device_t device, const char *path,
1011 grub_fs_dir_hook_t hook, void *hook_data)
1012 {
1013 struct grub_ntfs_dir_ctx ctx = { hook, hook_data };
1014 struct grub_ntfs_data *data = 0;
1015 struct grub_fshelp_node *fdiro = 0;
1016
1017 grub_dl_ref (my_mod);
1018
1019 data = grub_ntfs_mount (device->disk);
1020 if (!data)
1021 goto fail;
1022
1023 grub_fshelp_find_file (path, &data->cmft, &fdiro, grub_ntfs_iterate_dir,
1024 grub_ntfs_read_symlink, GRUB_FSHELP_DIR);
1025
1026 if (grub_errno)
1027 goto fail;
1028
1029 grub_ntfs_iterate_dir (fdiro, grub_ntfs_dir_iter, &ctx);
1030
1031 fail:
1032 if ((fdiro) && (fdiro != &data->cmft))
1033 {
1034 free_file (fdiro);
1035 grub_free (fdiro);
1036 }
1037 if (data)
1038 {
1039 free_file (&data->mmft);
1040 free_file (&data->cmft);
1041 grub_free (data);
1042 }
1043
1044 grub_dl_unref (my_mod);
1045
1046 return grub_errno;
1047 }
1048
1049 static grub_err_t
1050 grub_ntfs_open (grub_file_t file, const char *name)
1051 {
1052 struct grub_ntfs_data *data = 0;
1053 struct grub_fshelp_node *mft = 0;
1054
1055 grub_dl_ref (my_mod);
1056
1057 data = grub_ntfs_mount (file->device->disk);
1058 if (!data)
1059 goto fail;
1060
1061 grub_fshelp_find_file (name, &data->cmft, &mft, grub_ntfs_iterate_dir,
1062 grub_ntfs_read_symlink, GRUB_FSHELP_REG);
1063
1064 if (grub_errno)
1065 goto fail;
1066
1067 if (mft != &data->cmft)
1068 {
1069 free_file (&data->cmft);
1070 grub_memcpy (&data->cmft, mft, sizeof (*mft));
1071 grub_free (mft);
1072 if (!data->cmft.inode_read)
1073 {
1074 if (init_file (&data->cmft, data->cmft.ino))
1075 goto fail;
1076 }
1077 }
1078
1079 file->size = data->cmft.size;
1080 file->data = data;
1081 file->offset = 0;
1082
1083 return 0;
1084
1085 fail:
1086 if (data)
1087 {
1088 free_file (&data->mmft);
1089 free_file (&data->cmft);
1090 grub_free (data);
1091 }
1092
1093 grub_dl_unref (my_mod);
1094
1095 return grub_errno;
1096 }
1097
1098 static grub_ssize_t
1099 grub_ntfs_read (grub_file_t file, char *buf, grub_size_t len)
1100 {
1101 struct grub_ntfs_file *mft;
1102
1103 mft = &((struct grub_ntfs_data *) file->data)->cmft;
1104 if (file->read_hook)
1105 mft->attr.save_pos = 1;
1106
1107 read_attr (&mft->attr, (grub_uint8_t *) buf, file->offset, len, 1,
1108 file->read_hook, file->read_hook_data);
1109 return (grub_errno) ? -1 : (grub_ssize_t) len;
1110 }
1111
1112 static grub_err_t
1113 grub_ntfs_close (grub_file_t file)
1114 {
1115 struct grub_ntfs_data *data;
1116
1117 data = file->data;
1118
1119 if (data)
1120 {
1121 free_file (&data->mmft);
1122 free_file (&data->cmft);
1123 grub_free (data);
1124 }
1125
1126 grub_dl_unref (my_mod);
1127
1128 return grub_errno;
1129 }
1130
1131 static grub_err_t
1132 grub_ntfs_label (grub_device_t device, char **label)
1133 {
1134 struct grub_ntfs_data *data = 0;
1135 struct grub_fshelp_node *mft = 0;
1136 grub_uint8_t *pa;
1137
1138 grub_dl_ref (my_mod);
1139
1140 *label = 0;
1141
1142 data = grub_ntfs_mount (device->disk);
1143 if (!data)
1144 goto fail;
1145
1146 grub_fshelp_find_file ("/$Volume", &data->cmft, &mft, grub_ntfs_iterate_dir,
1147 0, GRUB_FSHELP_REG);
1148
1149 if (grub_errno)
1150 goto fail;
1151
1152 if (!mft->inode_read)
1153 {
1154 mft->buf = grub_malloc (mft->data->mft_size << GRUB_NTFS_BLK_SHR);
1155 if (mft->buf == NULL)
1156 goto fail;
1157
1158 if (read_mft (mft->data, mft->buf, mft->ino))
1159 goto fail;
1160 }
1161
1162 init_attr (&mft->attr, mft);
1163 pa = find_attr (&mft->attr, GRUB_NTFS_AT_VOLUME_NAME);
1164 if ((pa) && (pa[8] == 0) && (u32at (pa, 0x10)))
1165 {
1166 int len;
1167
1168 len = u32at (pa, 0x10) / 2;
1169 pa += u16at (pa, 0x14);
1170 *label = get_utf8 (pa, len);
1171 }
1172
1173 fail:
1174 if ((mft) && (mft != &data->cmft))
1175 {
1176 free_file (mft);
1177 grub_free (mft);
1178 }
1179 if (data)
1180 {
1181 free_file (&data->mmft);
1182 free_file (&data->cmft);
1183 grub_free (data);
1184 }
1185
1186 grub_dl_unref (my_mod);
1187
1188 return grub_errno;
1189 }
1190
1191 static grub_err_t
1192 grub_ntfs_uuid (grub_device_t device, char **uuid)
1193 {
1194 struct grub_ntfs_data *data;
1195 grub_disk_t disk = device->disk;
1196
1197 grub_dl_ref (my_mod);
1198
1199 data = grub_ntfs_mount (disk);
1200 if (data)
1201 {
1202 char *ptr;
1203 *uuid = grub_xasprintf ("%016llx", (unsigned long long) data->uuid);
1204 if (*uuid)
1205 for (ptr = *uuid; *ptr; ptr++)
1206 *ptr = grub_toupper (*ptr);
1207 free_file (&data->mmft);
1208 free_file (&data->cmft);
1209 grub_free (data);
1210 }
1211 else
1212 *uuid = NULL;
1213
1214 grub_dl_unref (my_mod);
1215
1216 return grub_errno;
1217 }
1218
1219 static struct grub_fs grub_ntfs_fs =
1220 {
1221 .name = "ntfs",
1222 .fs_dir = grub_ntfs_dir,
1223 .fs_open = grub_ntfs_open,
1224 .fs_read = grub_ntfs_read,
1225 .fs_close = grub_ntfs_close,
1226 .fs_label = grub_ntfs_label,
1227 .fs_uuid = grub_ntfs_uuid,
1228 #ifdef GRUB_UTIL
1229 .reserved_first_sector = 1,
1230 .blocklist_install = 1,
1231 #endif
1232 .next = 0
1233 };
1234
1235 GRUB_MOD_INIT (ntfs)
1236 {
1237 grub_fs_register (&grub_ntfs_fs);
1238 my_mod = mod;
1239 }
1240
1241 GRUB_MOD_FINI (ntfs)
1242 {
1243 grub_fs_unregister (&grub_ntfs_fs);
1244 }