~elementary-os/elementaryos/os-patch-grub2-bionic

« back to all changes in this revision

Viewing changes to grub-core/fs/fat.c

  • Committer: RabbitBot
  • Date: 2018-02-05 13:05:56 UTC
  • Revision ID: rabbitbot@elementary.io-20180205130556-qgaormf12qpm3v40
Initial import, version 2.02-2ubuntu4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* fat.c - FAT filesystem */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009  Free Software Foundation, Inc.
 
5
 *
 
6
 *  GRUB 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
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <grub/fs.h>
 
21
#include <grub/disk.h>
 
22
#include <grub/file.h>
 
23
#include <grub/types.h>
 
24
#include <grub/misc.h>
 
25
#include <grub/mm.h>
 
26
#include <grub/err.h>
 
27
#include <grub/dl.h>
 
28
#include <grub/charset.h>
 
29
#ifndef MODE_EXFAT
 
30
#include <grub/fat.h>
 
31
#else
 
32
#include <grub/exfat.h>
 
33
#endif
 
34
#include <grub/fshelp.h>
 
35
#include <grub/i18n.h>
 
36
 
 
37
GRUB_MOD_LICENSE ("GPLv3+");
 
38
 
 
39
enum
 
40
  {
 
41
    GRUB_FAT_ATTR_READ_ONLY = 0x01,
 
42
    GRUB_FAT_ATTR_HIDDEN = 0x02,
 
43
    GRUB_FAT_ATTR_SYSTEM = 0x04,
 
44
#ifndef MODE_EXFAT
 
45
    GRUB_FAT_ATTR_VOLUME_ID = 0x08,
 
46
#endif
 
47
    GRUB_FAT_ATTR_DIRECTORY = 0x10,
 
48
    GRUB_FAT_ATTR_ARCHIVE = 0x20,
 
49
 
 
50
#ifndef MODE_EXFAT
 
51
    GRUB_FAT_ATTR_LONG_NAME = (GRUB_FAT_ATTR_READ_ONLY
 
52
                               | GRUB_FAT_ATTR_HIDDEN
 
53
                               | GRUB_FAT_ATTR_SYSTEM
 
54
                               | GRUB_FAT_ATTR_VOLUME_ID),
 
55
#endif
 
56
    GRUB_FAT_ATTR_VALID = (GRUB_FAT_ATTR_READ_ONLY
 
57
                           | GRUB_FAT_ATTR_HIDDEN
 
58
                           | GRUB_FAT_ATTR_SYSTEM
 
59
                           | GRUB_FAT_ATTR_DIRECTORY
 
60
                           | GRUB_FAT_ATTR_ARCHIVE
 
61
#ifndef MODE_EXFAT
 
62
                           | GRUB_FAT_ATTR_VOLUME_ID
 
63
#endif
 
64
                           )
 
65
  };
 
66
 
 
67
#ifdef MODE_EXFAT
 
68
typedef struct grub_exfat_bpb grub_current_fat_bpb_t;
 
69
#else
 
70
typedef struct grub_fat_bpb grub_current_fat_bpb_t;
 
71
#endif
 
72
 
 
73
#ifdef MODE_EXFAT
 
74
enum
 
75
  {
 
76
    FLAG_CONTIGUOUS = 2
 
77
  };
 
78
struct grub_fat_dir_entry
 
79
{
 
80
  grub_uint8_t entry_type;
 
81
  union
 
82
  {
 
83
    grub_uint8_t placeholder[31];
 
84
    struct {
 
85
      grub_uint8_t secondary_count;
 
86
      grub_uint16_t checksum;
 
87
      grub_uint16_t attr;
 
88
      grub_uint16_t reserved1;
 
89
      grub_uint32_t c_time;
 
90
      grub_uint32_t m_time;
 
91
      grub_uint32_t a_time;
 
92
      grub_uint8_t c_time_tenth;
 
93
      grub_uint8_t m_time_tenth;
 
94
      grub_uint8_t a_time_tenth;
 
95
      grub_uint8_t reserved2[9];
 
96
    }  GRUB_PACKED file;
 
97
    struct {
 
98
      grub_uint8_t flags;
 
99
      grub_uint8_t reserved1;
 
100
      grub_uint8_t name_length;
 
101
      grub_uint16_t name_hash;
 
102
      grub_uint16_t reserved2;
 
103
      grub_uint64_t valid_size;
 
104
      grub_uint32_t reserved3;
 
105
      grub_uint32_t first_cluster;
 
106
      grub_uint64_t file_size;
 
107
    }   GRUB_PACKED stream_extension;
 
108
    struct {
 
109
      grub_uint8_t flags;
 
110
      grub_uint16_t str[15];
 
111
    }  GRUB_PACKED  file_name;
 
112
    struct {
 
113
      grub_uint8_t character_count;
 
114
      grub_uint16_t str[15];
 
115
    }  GRUB_PACKED  volume_label;
 
116
  }  GRUB_PACKED type_specific;
 
117
} GRUB_PACKED;
 
118
 
 
119
struct grub_fat_dir_node
 
120
{
 
121
  grub_uint32_t attr;
 
122
  grub_uint32_t first_cluster;
 
123
  grub_uint64_t file_size;
 
124
  grub_uint64_t valid_size;
 
125
  int have_stream;
 
126
  int is_contiguous;
 
127
};
 
128
 
 
129
typedef struct grub_fat_dir_node grub_fat_dir_node_t;
 
130
 
 
131
#else
 
132
struct grub_fat_dir_entry
 
133
{
 
134
  grub_uint8_t name[11];
 
135
  grub_uint8_t attr;
 
136
  grub_uint8_t nt_reserved;
 
137
  grub_uint8_t c_time_tenth;
 
138
  grub_uint16_t c_time;
 
139
  grub_uint16_t c_date;
 
140
  grub_uint16_t a_date;
 
141
  grub_uint16_t first_cluster_high;
 
142
  grub_uint16_t w_time;
 
143
  grub_uint16_t w_date;
 
144
  grub_uint16_t first_cluster_low;
 
145
  grub_uint32_t file_size;
 
146
} GRUB_PACKED;
 
147
 
 
148
struct grub_fat_long_name_entry
 
149
{
 
150
  grub_uint8_t id;
 
151
  grub_uint16_t name1[5];
 
152
  grub_uint8_t attr;
 
153
  grub_uint8_t reserved;
 
154
  grub_uint8_t checksum;
 
155
  grub_uint16_t name2[6];
 
156
  grub_uint16_t first_cluster;
 
157
  grub_uint16_t name3[2];
 
158
} GRUB_PACKED;
 
159
 
 
160
typedef struct grub_fat_dir_entry grub_fat_dir_node_t;
 
161
 
 
162
#endif
 
163
 
 
164
struct grub_fat_data
 
165
{
 
166
  int logical_sector_bits;
 
167
  grub_uint32_t num_sectors;
 
168
 
 
169
  grub_uint32_t fat_sector;
 
170
  grub_uint32_t sectors_per_fat;
 
171
  int fat_size;
 
172
 
 
173
  grub_uint32_t root_cluster;
 
174
#ifndef MODE_EXFAT
 
175
  grub_uint32_t root_sector;
 
176
  grub_uint32_t num_root_sectors;
 
177
#endif
 
178
 
 
179
  int cluster_bits;
 
180
  grub_uint32_t cluster_eof_mark;
 
181
  grub_uint32_t cluster_sector;
 
182
  grub_uint32_t num_clusters;
 
183
 
 
184
  grub_uint32_t uuid;
 
185
};
 
186
 
 
187
struct grub_fshelp_node {
 
188
  grub_disk_t disk;
 
189
  struct grub_fat_data *data;
 
190
 
 
191
  grub_uint8_t attr;
 
192
#ifndef MODE_EXFAT
 
193
  grub_uint32_t file_size;
 
194
#else
 
195
  grub_uint64_t file_size;
 
196
#endif
 
197
  grub_uint32_t file_cluster;
 
198
  grub_uint32_t cur_cluster_num;
 
199
  grub_uint32_t cur_cluster;
 
200
 
 
201
#ifdef MODE_EXFAT
 
202
  int is_contiguous;
 
203
#endif
 
204
};
 
205
 
 
206
static grub_dl_t my_mod;
 
207
 
 
208
#ifndef MODE_EXFAT
 
209
static int
 
210
fat_log2 (unsigned x)
 
211
{
 
212
  int i;
 
213
 
 
214
  if (x == 0)
 
215
    return -1;
 
216
 
 
217
  for (i = 0; (x & 1) == 0; i++)
 
218
    x >>= 1;
 
219
 
 
220
  if (x != 1)
 
221
    return -1;
 
222
 
 
223
  return i;
 
224
}
 
225
#endif
 
226
 
 
227
static struct grub_fat_data *
 
228
grub_fat_mount (grub_disk_t disk)
 
229
{
 
230
  grub_current_fat_bpb_t bpb;
 
231
  struct grub_fat_data *data = 0;
 
232
  grub_uint32_t first_fat, magic;
 
233
 
 
234
  if (! disk)
 
235
    goto fail;
 
236
 
 
237
  data = (struct grub_fat_data *) grub_malloc (sizeof (*data));
 
238
  if (! data)
 
239
    goto fail;
 
240
 
 
241
  /* Read the BPB.  */
 
242
  if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
 
243
    goto fail;
 
244
 
 
245
#ifdef MODE_EXFAT
 
246
  if (grub_memcmp ((const char *) bpb.oem_name, "EXFAT   ",
 
247
                   sizeof (bpb.oem_name)) != 0)
 
248
    goto fail;    
 
249
#endif
 
250
 
 
251
  /* Get the sizes of logical sectors and clusters.  */
 
252
#ifdef MODE_EXFAT
 
253
  data->logical_sector_bits = bpb.bytes_per_sector_shift;
 
254
#else
 
255
  data->logical_sector_bits =
 
256
    fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector));
 
257
#endif
 
258
  if (data->logical_sector_bits < GRUB_DISK_SECTOR_BITS
 
259
      || data->logical_sector_bits >= 16)
 
260
    goto fail;
 
261
  data->logical_sector_bits -= GRUB_DISK_SECTOR_BITS;
 
262
 
 
263
#ifdef MODE_EXFAT
 
264
  data->cluster_bits = bpb.sectors_per_cluster_shift;
 
265
#else
 
266
  data->cluster_bits = fat_log2 (bpb.sectors_per_cluster);
 
267
#endif
 
268
  if (data->cluster_bits < 0 || data->cluster_bits > 25)
 
269
    goto fail;
 
270
  data->cluster_bits += data->logical_sector_bits;
 
271
 
 
272
  /* Get information about FATs.  */
 
273
#ifdef MODE_EXFAT
 
274
  data->fat_sector = (grub_le_to_cpu32 (bpb.num_reserved_sectors)
 
275
                      << data->logical_sector_bits);
 
276
#else
 
277
  data->fat_sector = (grub_le_to_cpu16 (bpb.num_reserved_sectors)
 
278
                      << data->logical_sector_bits);
 
279
#endif
 
280
  if (data->fat_sector == 0)
 
281
    goto fail;
 
282
 
 
283
#ifdef MODE_EXFAT
 
284
  data->sectors_per_fat = (grub_le_to_cpu32 (bpb.sectors_per_fat)
 
285
                           << data->logical_sector_bits);
 
286
#else
 
287
  data->sectors_per_fat = ((bpb.sectors_per_fat_16
 
288
                            ? grub_le_to_cpu16 (bpb.sectors_per_fat_16)
 
289
                            : grub_le_to_cpu32 (bpb.version_specific.fat32.sectors_per_fat_32))
 
290
                           << data->logical_sector_bits);
 
291
#endif
 
292
  if (data->sectors_per_fat == 0)
 
293
    goto fail;
 
294
 
 
295
  /* Get the number of sectors in this volume.  */
 
296
#ifdef MODE_EXFAT
 
297
  data->num_sectors = ((grub_le_to_cpu64 (bpb.num_total_sectors))
 
298
                       << data->logical_sector_bits);
 
299
#else
 
300
  data->num_sectors = ((bpb.num_total_sectors_16
 
301
                        ? grub_le_to_cpu16 (bpb.num_total_sectors_16)
 
302
                        : grub_le_to_cpu32 (bpb.num_total_sectors_32))
 
303
                       << data->logical_sector_bits);
 
304
#endif
 
305
  if (data->num_sectors == 0)
 
306
    goto fail;
 
307
 
 
308
  /* Get information about the root directory.  */
 
309
  if (bpb.num_fats == 0)
 
310
    goto fail;
 
311
 
 
312
#ifndef MODE_EXFAT
 
313
  data->root_sector = data->fat_sector + bpb.num_fats * data->sectors_per_fat;
 
314
  data->num_root_sectors
 
315
    = ((((grub_uint32_t) grub_le_to_cpu16 (bpb.num_root_entries)
 
316
         * sizeof (struct grub_fat_dir_entry)
 
317
         + grub_le_to_cpu16 (bpb.bytes_per_sector) - 1)
 
318
        >> (data->logical_sector_bits + GRUB_DISK_SECTOR_BITS))
 
319
       << (data->logical_sector_bits));
 
320
#endif
 
321
 
 
322
#ifdef MODE_EXFAT
 
323
  data->cluster_sector = (grub_le_to_cpu32 (bpb.cluster_offset) 
 
324
                          << data->logical_sector_bits);
 
325
  data->num_clusters = (grub_le_to_cpu32 (bpb.cluster_count)
 
326
                          << data->logical_sector_bits);
 
327
#else
 
328
  data->cluster_sector = data->root_sector + data->num_root_sectors;
 
329
  data->num_clusters = (((data->num_sectors - data->cluster_sector)
 
330
                         >> data->cluster_bits)
 
331
                        + 2);
 
332
#endif
 
333
 
 
334
  if (data->num_clusters <= 2)
 
335
    goto fail;
 
336
 
 
337
#ifdef MODE_EXFAT
 
338
  {
 
339
    /* exFAT.  */
 
340
    data->root_cluster = grub_le_to_cpu32 (bpb.root_cluster);
 
341
    data->fat_size = 32;
 
342
    data->cluster_eof_mark = 0xffffffff;
 
343
 
 
344
    if ((bpb.volume_flags & grub_cpu_to_le16_compile_time (0x1))
 
345
        && bpb.num_fats > 1)
 
346
      data->fat_sector += data->sectors_per_fat;
 
347
  }
 
348
#else
 
349
  if (! bpb.sectors_per_fat_16)
 
350
    {
 
351
      /* FAT32.  */
 
352
      grub_uint16_t flags = grub_le_to_cpu16 (bpb.version_specific.fat32.extended_flags);
 
353
 
 
354
      data->root_cluster = grub_le_to_cpu32 (bpb.version_specific.fat32.root_cluster);
 
355
      data->fat_size = 32;
 
356
      data->cluster_eof_mark = 0x0ffffff8;
 
357
 
 
358
      if (flags & 0x80)
 
359
        {
 
360
          /* Get an active FAT.  */
 
361
          unsigned active_fat = flags & 0xf;
 
362
 
 
363
          if (active_fat > bpb.num_fats)
 
364
            goto fail;
 
365
 
 
366
          data->fat_sector += active_fat * data->sectors_per_fat;
 
367
        }
 
368
 
 
369
      if (bpb.num_root_entries != 0 || bpb.version_specific.fat32.fs_version != 0)
 
370
        goto fail;
 
371
    }
 
372
  else
 
373
    {
 
374
      /* FAT12 or FAT16.  */
 
375
      data->root_cluster = ~0U;
 
376
 
 
377
      if (data->num_clusters <= 4085 + 2)
 
378
        {
 
379
          /* FAT12.  */
 
380
          data->fat_size = 12;
 
381
          data->cluster_eof_mark = 0x0ff8;
 
382
        }
 
383
      else
 
384
        {
 
385
          /* FAT16.  */
 
386
          data->fat_size = 16;
 
387
          data->cluster_eof_mark = 0xfff8;
 
388
        }
 
389
    }
 
390
#endif
 
391
 
 
392
  /* More sanity checks.  */
 
393
  if (data->num_sectors <= data->fat_sector)
 
394
    goto fail;
 
395
 
 
396
  if (grub_disk_read (disk,
 
397
                      data->fat_sector,
 
398
                      0,
 
399
                      sizeof (first_fat),
 
400
                      &first_fat))
 
401
    goto fail;
 
402
 
 
403
  first_fat = grub_le_to_cpu32 (first_fat);
 
404
 
 
405
  if (data->fat_size == 32)
 
406
    {
 
407
      first_fat &= 0x0fffffff;
 
408
      magic = 0x0fffff00;
 
409
    }
 
410
  else if (data->fat_size == 16)
 
411
    {
 
412
      first_fat &= 0x0000ffff;
 
413
      magic = 0xff00;
 
414
    }
 
415
  else
 
416
    {
 
417
      first_fat &= 0x00000fff;
 
418
      magic = 0x0f00;
 
419
    }
 
420
 
 
421
  /* Serial number.  */
 
422
#ifdef MODE_EXFAT
 
423
    data->uuid = grub_le_to_cpu32 (bpb.num_serial);
 
424
#else
 
425
  if (bpb.sectors_per_fat_16)
 
426
    data->uuid = grub_le_to_cpu32 (bpb.version_specific.fat12_or_fat16.num_serial);
 
427
  else
 
428
    data->uuid = grub_le_to_cpu32 (bpb.version_specific.fat32.num_serial);
 
429
#endif
 
430
 
 
431
#ifndef MODE_EXFAT
 
432
  /* Ignore the 3rd bit, because some BIOSes assigns 0xF0 to the media
 
433
     descriptor, even if it is a so-called superfloppy (e.g. an USB key).
 
434
     The check may be too strict for this kind of stupid BIOSes, as
 
435
     they overwrite the media descriptor.  */
 
436
  if ((first_fat | 0x8) != (magic | bpb.media | 0x8))
 
437
    goto fail;
 
438
#else
 
439
  (void) magic;
 
440
#endif
 
441
 
 
442
  return data;
 
443
 
 
444
 fail:
 
445
 
 
446
  grub_free (data);
 
447
  grub_error (GRUB_ERR_BAD_FS, "not a FAT filesystem");
 
448
  return 0;
 
449
}
 
450
 
 
451
static grub_ssize_t
 
452
grub_fat_read_data (grub_disk_t disk, grub_fshelp_node_t node,
 
453
                    grub_disk_read_hook_t read_hook, void *read_hook_data,
 
454
                    grub_off_t offset, grub_size_t len, char *buf)
 
455
{
 
456
  grub_size_t size;
 
457
  grub_uint32_t logical_cluster;
 
458
  unsigned logical_cluster_bits;
 
459
  grub_ssize_t ret = 0;
 
460
  unsigned long sector;
 
461
 
 
462
#ifndef MODE_EXFAT
 
463
  /* This is a special case. FAT12 and FAT16 doesn't have the root directory
 
464
     in clusters.  */
 
465
  if (node->file_cluster == ~0U)
 
466
    {
 
467
      size = (node->data->num_root_sectors << GRUB_DISK_SECTOR_BITS) - offset;
 
468
      if (size > len)
 
469
        size = len;
 
470
 
 
471
      if (grub_disk_read (disk, node->data->root_sector, offset, size, buf))
 
472
        return -1;
 
473
 
 
474
      return size;
 
475
    }
 
476
#endif
 
477
 
 
478
#ifdef MODE_EXFAT
 
479
  if (node->is_contiguous)
 
480
    {
 
481
      /* Read the data here.  */
 
482
      sector = (node->data->cluster_sector
 
483
                + ((node->file_cluster - 2)
 
484
                   << node->data->cluster_bits));
 
485
 
 
486
      disk->read_hook = read_hook;
 
487
      disk->read_hook_data = read_hook_data;
 
488
      grub_disk_read (disk, sector + (offset >> GRUB_DISK_SECTOR_BITS),
 
489
                      offset & (GRUB_DISK_SECTOR_SIZE - 1), len, buf);
 
490
      disk->read_hook = 0;
 
491
      if (grub_errno)
 
492
        return -1;
 
493
 
 
494
      return len;
 
495
    }
 
496
#endif
 
497
 
 
498
  /* Calculate the logical cluster number and offset.  */
 
499
  logical_cluster_bits = (node->data->cluster_bits
 
500
                          + GRUB_DISK_SECTOR_BITS);
 
501
  logical_cluster = offset >> logical_cluster_bits;
 
502
  offset &= (1ULL << logical_cluster_bits) - 1;
 
503
 
 
504
  if (logical_cluster < node->cur_cluster_num)
 
505
    {
 
506
      node->cur_cluster_num = 0;
 
507
      node->cur_cluster = node->file_cluster;
 
508
    }
 
509
 
 
510
  while (len)
 
511
    {
 
512
      while (logical_cluster > node->cur_cluster_num)
 
513
        {
 
514
          /* Find next cluster.  */
 
515
          grub_uint32_t next_cluster;
 
516
          grub_uint32_t fat_offset;
 
517
 
 
518
          switch (node->data->fat_size)
 
519
            {
 
520
            case 32:
 
521
              fat_offset = node->cur_cluster << 2;
 
522
              break;
 
523
            case 16:
 
524
              fat_offset = node->cur_cluster << 1;
 
525
              break;
 
526
            default:
 
527
              /* case 12: */
 
528
              fat_offset = node->cur_cluster + (node->cur_cluster >> 1);
 
529
              break;
 
530
            }
 
531
 
 
532
          /* Read the FAT.  */
 
533
          if (grub_disk_read (disk, node->data->fat_sector, fat_offset,
 
534
                              (node->data->fat_size + 7) >> 3,
 
535
                              (char *) &next_cluster))
 
536
            return -1;
 
537
 
 
538
          next_cluster = grub_le_to_cpu32 (next_cluster);
 
539
          switch (node->data->fat_size)
 
540
            {
 
541
            case 16:
 
542
              next_cluster &= 0xFFFF;
 
543
              break;
 
544
            case 12:
 
545
              if (node->cur_cluster & 1)
 
546
                next_cluster >>= 4;
 
547
 
 
548
              next_cluster &= 0x0FFF;
 
549
              break;
 
550
            }
 
551
 
 
552
          grub_dprintf ("fat", "fat_size=%d, next_cluster=%u\n",
 
553
                        node->data->fat_size, next_cluster);
 
554
 
 
555
          /* Check the end.  */
 
556
          if (next_cluster >= node->data->cluster_eof_mark)
 
557
            return ret;
 
558
 
 
559
          if (next_cluster < 2 || next_cluster >= node->data->num_clusters)
 
560
            {
 
561
              grub_error (GRUB_ERR_BAD_FS, "invalid cluster %u",
 
562
                          next_cluster);
 
563
              return -1;
 
564
            }
 
565
 
 
566
          node->cur_cluster = next_cluster;
 
567
          node->cur_cluster_num++;
 
568
        }
 
569
 
 
570
      /* Read the data here.  */
 
571
      sector = (node->data->cluster_sector
 
572
                + ((node->cur_cluster - 2)
 
573
                   << node->data->cluster_bits));
 
574
      size = (1 << logical_cluster_bits) - offset;
 
575
      if (size > len)
 
576
        size = len;
 
577
 
 
578
      disk->read_hook = read_hook;
 
579
      disk->read_hook_data = read_hook_data;
 
580
      grub_disk_read (disk, sector, offset, size, buf);
 
581
      disk->read_hook = 0;
 
582
      if (grub_errno)
 
583
        return -1;
 
584
 
 
585
      len -= size;
 
586
      buf += size;
 
587
      ret += size;
 
588
      logical_cluster++;
 
589
      offset = 0;
 
590
    }
 
591
 
 
592
  return ret;
 
593
}
 
594
 
 
595
struct grub_fat_iterate_context
 
596
{
 
597
#ifdef MODE_EXFAT
 
598
  struct grub_fat_dir_node dir;
 
599
#else
 
600
  struct grub_fat_dir_entry dir;
 
601
#endif
 
602
  char *filename;
 
603
  grub_uint16_t *unibuf;
 
604
  grub_ssize_t offset;
 
605
};
 
606
 
 
607
static grub_err_t
 
608
grub_fat_iterate_init (struct grub_fat_iterate_context *ctxt)
 
609
{
 
610
  ctxt->offset = -sizeof (struct grub_fat_dir_entry);
 
611
 
 
612
#ifndef MODE_EXFAT
 
613
  /* Allocate space enough to hold a long name.  */
 
614
  ctxt->filename = grub_malloc (0x40 * 13 * GRUB_MAX_UTF8_PER_UTF16 + 1);
 
615
  ctxt->unibuf = (grub_uint16_t *) grub_malloc (0x40 * 13 * 2);
 
616
#else
 
617
  ctxt->unibuf = grub_malloc (15 * 256 * 2);
 
618
  ctxt->filename = grub_malloc (15 * 256 * GRUB_MAX_UTF8_PER_UTF16 + 1);
 
619
#endif
 
620
 
 
621
  if (! ctxt->filename || ! ctxt->unibuf)
 
622
    {
 
623
      grub_free (ctxt->filename);
 
624
      grub_free (ctxt->unibuf);
 
625
      return grub_errno;
 
626
    }
 
627
  return GRUB_ERR_NONE;
 
628
}
 
629
 
 
630
static void
 
631
grub_fat_iterate_fini (struct grub_fat_iterate_context *ctxt)
 
632
{
 
633
  grub_free (ctxt->filename);
 
634
  grub_free (ctxt->unibuf);
 
635
}
 
636
 
 
637
#ifdef MODE_EXFAT
 
638
static grub_err_t
 
639
grub_fat_iterate_dir_next (grub_fshelp_node_t node,
 
640
                           struct grub_fat_iterate_context *ctxt)
 
641
{
 
642
  grub_memset (&ctxt->dir, 0, sizeof (ctxt->dir));
 
643
  while (1)
 
644
    {
 
645
      struct grub_fat_dir_entry dir;
 
646
 
 
647
      ctxt->offset += sizeof (dir);
 
648
 
 
649
      if (grub_fat_read_data (node->disk, node, 0, 0, ctxt->offset, sizeof (dir),
 
650
                              (char *) &dir)
 
651
           != sizeof (dir))
 
652
        break;
 
653
 
 
654
      if (dir.entry_type == 0)
 
655
        break;
 
656
      if (!(dir.entry_type & 0x80))
 
657
        continue;
 
658
 
 
659
      if (dir.entry_type == 0x85)
 
660
        {
 
661
          unsigned i, nsec, slots = 0;
 
662
 
 
663
          nsec = dir.type_specific.file.secondary_count;
 
664
 
 
665
          ctxt->dir.attr = grub_cpu_to_le16 (dir.type_specific.file.attr);
 
666
          ctxt->dir.have_stream = 0;
 
667
          for (i = 0; i < nsec; i++)
 
668
            {
 
669
              struct grub_fat_dir_entry sec;
 
670
              ctxt->offset += sizeof (sec);
 
671
              if (grub_fat_read_data (node->disk, node, 0, 0,
 
672
                                      ctxt->offset, sizeof (sec), (char *) &sec)
 
673
                  != sizeof (sec))
 
674
                break;
 
675
              if (!(sec.entry_type & 0x80))
 
676
                continue;
 
677
              if (!(sec.entry_type & 0x40))
 
678
                break;
 
679
              switch (sec.entry_type)
 
680
                {
 
681
                case 0xc0:
 
682
                  ctxt->dir.first_cluster = grub_cpu_to_le32 (sec.type_specific.stream_extension.first_cluster);
 
683
                  ctxt->dir.valid_size
 
684
                    = grub_cpu_to_le64 (sec.type_specific.stream_extension.valid_size);
 
685
                  ctxt->dir.file_size
 
686
                    = grub_cpu_to_le64 (sec.type_specific.stream_extension.file_size);
 
687
                  ctxt->dir.have_stream = 1;
 
688
                  ctxt->dir.is_contiguous = !!(sec.type_specific.stream_extension.flags
 
689
                                               & grub_cpu_to_le16_compile_time (FLAG_CONTIGUOUS));
 
690
                  break;
 
691
                case 0xc1:
 
692
                  {
 
693
                    int j;
 
694
                    for (j = 0; j < 15; j++)
 
695
                      ctxt->unibuf[slots * 15 + j] 
 
696
                        = grub_le_to_cpu16 (sec.type_specific.file_name.str[j]);
 
697
                    slots++;
 
698
                  }
 
699
                  break;
 
700
                default:
 
701
                  grub_dprintf ("exfat", "unknown secondary type 0x%02x\n",
 
702
                                sec.entry_type);
 
703
                }
 
704
            }
 
705
 
 
706
          if (i != nsec)
 
707
            {
 
708
              ctxt->offset -= sizeof (dir);
 
709
              continue;
 
710
            }
 
711
 
 
712
          *grub_utf16_to_utf8 ((grub_uint8_t *) ctxt->filename, ctxt->unibuf,
 
713
                               slots * 15) = '\0';
 
714
 
 
715
          return 0;
 
716
        }
 
717
      /* Allocation bitmap. */
 
718
      if (dir.entry_type == 0x81)
 
719
        continue;
 
720
      /* Upcase table. */
 
721
      if (dir.entry_type == 0x82)
 
722
        continue;
 
723
      /* Volume label. */
 
724
      if (dir.entry_type == 0x83)
 
725
        continue;
 
726
      grub_dprintf ("exfat", "unknown primary type 0x%02x\n",
 
727
                    dir.entry_type);
 
728
    }
 
729
  return grub_errno ? : GRUB_ERR_EOF;
 
730
}
 
731
 
 
732
#else
 
733
 
 
734
static grub_err_t
 
735
grub_fat_iterate_dir_next (grub_fshelp_node_t node,
 
736
                           struct grub_fat_iterate_context *ctxt)
 
737
{
 
738
  char *filep = 0;
 
739
  int checksum = -1;
 
740
  int slot = -1, slots = -1;
 
741
 
 
742
  while (1)
 
743
    {
 
744
      unsigned i;
 
745
 
 
746
      /* Adjust the offset.  */
 
747
      ctxt->offset += sizeof (ctxt->dir);
 
748
 
 
749
      /* Read a directory entry.  */
 
750
      if (grub_fat_read_data (node->disk, node, 0, 0,
 
751
                              ctxt->offset, sizeof (ctxt->dir),
 
752
                              (char *) &ctxt->dir)
 
753
           != sizeof (ctxt->dir) || ctxt->dir.name[0] == 0)
 
754
        break;
 
755
 
 
756
      /* Handle long name entries.  */
 
757
      if (ctxt->dir.attr == GRUB_FAT_ATTR_LONG_NAME)
 
758
        {
 
759
          struct grub_fat_long_name_entry *long_name
 
760
            = (struct grub_fat_long_name_entry *) &ctxt->dir;
 
761
          grub_uint8_t id = long_name->id;
 
762
 
 
763
          if (id & 0x40)
 
764
            {
 
765
              id &= 0x3f;
 
766
              slots = slot = id;
 
767
              checksum = long_name->checksum;
 
768
            }
 
769
 
 
770
          if (id != slot || slot == 0 || checksum != long_name->checksum)
 
771
            {
 
772
              checksum = -1;
 
773
              continue;
 
774
            }
 
775
 
 
776
          slot--;
 
777
          grub_memcpy (ctxt->unibuf + slot * 13, long_name->name1, 5 * 2);
 
778
          grub_memcpy (ctxt->unibuf + slot * 13 + 5, long_name->name2, 6 * 2);
 
779
          grub_memcpy (ctxt->unibuf + slot * 13 + 11, long_name->name3, 2 * 2);
 
780
          continue;
 
781
        }
 
782
 
 
783
      /* Check if this entry is valid.  */
 
784
      if (ctxt->dir.name[0] == 0xe5 || (ctxt->dir.attr & ~GRUB_FAT_ATTR_VALID))
 
785
        continue;
 
786
 
 
787
      /* This is a workaround for Japanese.  */
 
788
      if (ctxt->dir.name[0] == 0x05)
 
789
        ctxt->dir.name[0] = 0xe5;
 
790
 
 
791
      if (checksum != -1 && slot == 0)
 
792
        {
 
793
          grub_uint8_t sum;
 
794
 
 
795
          for (sum = 0, i = 0; i < sizeof (ctxt->dir.name); i++)
 
796
            sum = ((sum >> 1) | (sum << 7)) + ctxt->dir.name[i];
 
797
 
 
798
          if (sum == checksum)
 
799
            {
 
800
              int u;
 
801
 
 
802
              for (u = 0; u < slots * 13; u++)
 
803
                ctxt->unibuf[u] = grub_le_to_cpu16 (ctxt->unibuf[u]);
 
804
 
 
805
              *grub_utf16_to_utf8 ((grub_uint8_t *) ctxt->filename,
 
806
                                   ctxt->unibuf,
 
807
                                   slots * 13) = '\0';
 
808
 
 
809
              return GRUB_ERR_NONE;
 
810
            }
 
811
 
 
812
          checksum = -1;
 
813
        }
 
814
 
 
815
      /* Convert the 8.3 file name.  */
 
816
      filep = ctxt->filename;
 
817
      if (ctxt->dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
 
818
        {
 
819
          for (i = 0; i < sizeof (ctxt->dir.name) && ctxt->dir.name[i]; i++)
 
820
            *filep++ = ctxt->dir.name[i];
 
821
          while (i > 0 && ctxt->dir.name[i - 1] == ' ')
 
822
            {
 
823
              filep--;
 
824
              i--;
 
825
            }
 
826
        }
 
827
      else
 
828
        {
 
829
          for (i = 0; i < 8 && ctxt->dir.name[i]; i++)
 
830
            *filep++ = grub_tolower (ctxt->dir.name[i]);
 
831
          while (i > 0 && ctxt->dir.name[i - 1] == ' ')
 
832
            {
 
833
              filep--;
 
834
              i--;
 
835
            }
 
836
 
 
837
          /* XXX should we check that dir position is 0 or 1? */
 
838
          if (i > 2 || filep[0] != '.' || (i == 2 && filep[1] != '.'))
 
839
            *filep++ = '.';
 
840
 
 
841
          for (i = 8; i < 11 && ctxt->dir.name[i]; i++)
 
842
            *filep++ = grub_tolower (ctxt->dir.name[i]);
 
843
          while (i > 8 && ctxt->dir.name[i - 1] == ' ')
 
844
            {
 
845
              filep--;
 
846
              i--;
 
847
            }
 
848
 
 
849
          if (i == 8)
 
850
            filep--;
 
851
        }
 
852
      *filep = '\0';
 
853
      return GRUB_ERR_NONE;
 
854
    }
 
855
 
 
856
  return grub_errno ? : GRUB_ERR_EOF;
 
857
}
 
858
 
 
859
#endif
 
860
 
 
861
static grub_err_t lookup_file (grub_fshelp_node_t node,
 
862
                               const char *name,
 
863
                               grub_fshelp_node_t *foundnode,
 
864
                               enum grub_fshelp_filetype *foundtype)
 
865
{
 
866
  grub_err_t err;
 
867
  struct grub_fat_iterate_context ctxt;
 
868
 
 
869
  err = grub_fat_iterate_init (&ctxt);
 
870
  if (err)
 
871
    return err;
 
872
 
 
873
  while (!(err = grub_fat_iterate_dir_next (node, &ctxt)))
 
874
    {
 
875
 
 
876
#ifdef MODE_EXFAT
 
877
      if (!ctxt.dir.have_stream)
 
878
        continue;
 
879
#else
 
880
      if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
 
881
        continue;
 
882
#endif
 
883
 
 
884
      if (grub_strcasecmp (name, ctxt.filename) == 0)
 
885
        {
 
886
          *foundnode = grub_malloc (sizeof (struct grub_fshelp_node));
 
887
          if (!*foundnode)
 
888
            return grub_errno;
 
889
          (*foundnode)->attr = ctxt.dir.attr;
 
890
#ifdef MODE_EXFAT
 
891
          (*foundnode)->file_size = ctxt.dir.file_size;
 
892
          (*foundnode)->file_cluster = ctxt.dir.first_cluster;
 
893
          (*foundnode)->is_contiguous = ctxt.dir.is_contiguous;
 
894
#else
 
895
          (*foundnode)->file_size = grub_le_to_cpu32 (ctxt.dir.file_size);
 
896
          (*foundnode)->file_cluster = ((grub_le_to_cpu16 (ctxt.dir.first_cluster_high) << 16)
 
897
                                | grub_le_to_cpu16 (ctxt.dir.first_cluster_low));
 
898
          /* If directory points to root, starting cluster is 0 */
 
899
          if (!(*foundnode)->file_cluster)
 
900
            (*foundnode)->file_cluster = node->data->root_cluster;
 
901
#endif
 
902
          (*foundnode)->cur_cluster_num = ~0U;
 
903
          (*foundnode)->data = node->data;
 
904
          (*foundnode)->disk = node->disk;
 
905
 
 
906
          *foundtype = ((*foundnode)->attr & GRUB_FAT_ATTR_DIRECTORY) ? GRUB_FSHELP_DIR : GRUB_FSHELP_REG;
 
907
 
 
908
          grub_fat_iterate_fini (&ctxt);
 
909
          return GRUB_ERR_NONE;
 
910
        }
 
911
    }
 
912
 
 
913
  grub_fat_iterate_fini (&ctxt);
 
914
  if (err == GRUB_ERR_EOF)
 
915
    err = 0;
 
916
 
 
917
  return err;
 
918
 
 
919
}
 
920
 
 
921
static grub_err_t
 
922
grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
 
923
              void *hook_data)
 
924
{
 
925
  struct grub_fat_data *data = 0;
 
926
  grub_disk_t disk = device->disk;
 
927
  grub_fshelp_node_t found = NULL;
 
928
  grub_err_t err;
 
929
  struct grub_fat_iterate_context ctxt;
 
930
 
 
931
  grub_dl_ref (my_mod);
 
932
 
 
933
  data = grub_fat_mount (disk);
 
934
  if (! data)
 
935
    goto fail;
 
936
 
 
937
  struct grub_fshelp_node root = {
 
938
    .data = data,
 
939
    .disk = disk,
 
940
    .attr = GRUB_FAT_ATTR_DIRECTORY,
 
941
    .file_size = 0,
 
942
    .file_cluster = data->root_cluster,
 
943
    .cur_cluster_num = ~0U,
 
944
    .cur_cluster = 0,
 
945
#ifdef MODE_EXFAT
 
946
    .is_contiguous = 0,
 
947
#endif
 
948
  };
 
949
 
 
950
  err = grub_fshelp_find_file_lookup (path, &root, &found, lookup_file, NULL, GRUB_FSHELP_DIR);
 
951
  if (err)
 
952
    goto fail;
 
953
 
 
954
  err = grub_fat_iterate_init (&ctxt);
 
955
  if (err)
 
956
    goto fail;
 
957
 
 
958
  while (!(err = grub_fat_iterate_dir_next (found, &ctxt)))
 
959
    {
 
960
      struct grub_dirhook_info info;
 
961
      grub_memset (&info, 0, sizeof (info));
 
962
 
 
963
      info.dir = !! (ctxt.dir.attr & GRUB_FAT_ATTR_DIRECTORY);
 
964
      info.case_insensitive = 1;
 
965
#ifdef MODE_EXFAT
 
966
      if (!ctxt.dir.have_stream)
 
967
        continue;
 
968
#else
 
969
      if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
 
970
        continue;
 
971
#endif
 
972
 
 
973
      if (hook (ctxt.filename, &info, hook_data))
 
974
        break;
 
975
    }
 
976
  grub_fat_iterate_fini (&ctxt);
 
977
  if (err == GRUB_ERR_EOF)
 
978
    err = 0;
 
979
 
 
980
 fail:
 
981
  if (found != &root)
 
982
    grub_free (found);
 
983
 
 
984
  grub_free (data);
 
985
 
 
986
  grub_dl_unref (my_mod);
 
987
 
 
988
  return grub_errno;
 
989
}
 
990
 
 
991
static grub_err_t
 
992
grub_fat_open (grub_file_t file, const char *name)
 
993
{
 
994
  struct grub_fat_data *data = 0;
 
995
  grub_fshelp_node_t found = NULL;
 
996
  grub_err_t err;
 
997
  grub_disk_t disk = file->device->disk;
 
998
 
 
999
  grub_dl_ref (my_mod);
 
1000
 
 
1001
  data = grub_fat_mount (disk);
 
1002
  if (! data)
 
1003
    goto fail;
 
1004
 
 
1005
  struct grub_fshelp_node root = {
 
1006
    .data = data,
 
1007
    .disk = disk,
 
1008
    .attr = GRUB_FAT_ATTR_DIRECTORY,
 
1009
    .file_size = 0,
 
1010
    .file_cluster = data->root_cluster,
 
1011
    .cur_cluster_num = ~0U,
 
1012
    .cur_cluster = 0,
 
1013
#ifdef MODE_EXFAT
 
1014
    .is_contiguous = 0,
 
1015
#endif
 
1016
  };
 
1017
 
 
1018
  err = grub_fshelp_find_file_lookup (name, &root, &found, lookup_file, NULL, GRUB_FSHELP_REG);
 
1019
  if (err)
 
1020
    goto fail;
 
1021
 
 
1022
  file->data = found;
 
1023
  file->size = found->file_size;
 
1024
 
 
1025
  return GRUB_ERR_NONE;
 
1026
 
 
1027
 fail:
 
1028
 
 
1029
  if (found != &root)
 
1030
    grub_free (found);
 
1031
 
 
1032
  grub_free (data);
 
1033
 
 
1034
  grub_dl_unref (my_mod);
 
1035
 
 
1036
  return grub_errno;
 
1037
}
 
1038
 
 
1039
static grub_ssize_t
 
1040
grub_fat_read (grub_file_t file, char *buf, grub_size_t len)
 
1041
{
 
1042
  return grub_fat_read_data (file->device->disk, file->data,
 
1043
                             file->read_hook, file->read_hook_data,
 
1044
                             file->offset, len, buf);
 
1045
}
 
1046
 
 
1047
static grub_err_t
 
1048
grub_fat_close (grub_file_t file)
 
1049
{
 
1050
  grub_fshelp_node_t node = file->data;
 
1051
 
 
1052
  grub_free (node->data);
 
1053
  grub_free (node);
 
1054
 
 
1055
  grub_dl_unref (my_mod);
 
1056
 
 
1057
  return grub_errno;
 
1058
}
 
1059
 
 
1060
#ifdef MODE_EXFAT
 
1061
static grub_err_t
 
1062
grub_fat_label (grub_device_t device, char **label)
 
1063
{
 
1064
  struct grub_fat_dir_entry dir;
 
1065
  grub_ssize_t offset = -sizeof(dir);
 
1066
  grub_disk_t disk = device->disk;
 
1067
  struct grub_fshelp_node root = {
 
1068
    .disk = disk,
 
1069
    .attr = GRUB_FAT_ATTR_DIRECTORY,
 
1070
    .file_size = 0,
 
1071
    .cur_cluster_num = ~0U,
 
1072
    .cur_cluster = 0,
 
1073
    .is_contiguous = 0,
 
1074
  };
 
1075
 
 
1076
  root.data = grub_fat_mount (disk);
 
1077
  if (! root.data)
 
1078
    return grub_errno;
 
1079
 
 
1080
  root.file_cluster = root.data->root_cluster;
 
1081
 
 
1082
  *label = NULL;
 
1083
 
 
1084
  while (1)
 
1085
    {
 
1086
      offset += sizeof (dir);
 
1087
 
 
1088
      if (grub_fat_read_data (disk, &root, 0, 0,
 
1089
                               offset, sizeof (dir), (char *) &dir)
 
1090
           != sizeof (dir))
 
1091
        break;
 
1092
 
 
1093
      if (dir.entry_type == 0)
 
1094
        break;
 
1095
      if (!(dir.entry_type & 0x80))
 
1096
        continue;
 
1097
 
 
1098
      /* Volume label. */
 
1099
      if (dir.entry_type == 0x83)
 
1100
        {
 
1101
          grub_size_t chc;
 
1102
          grub_uint16_t t[ARRAY_SIZE (dir.type_specific.volume_label.str)];
 
1103
          grub_size_t i;
 
1104
          *label = grub_malloc (ARRAY_SIZE (dir.type_specific.volume_label.str)
 
1105
                                * GRUB_MAX_UTF8_PER_UTF16 + 1);
 
1106
          if (!*label)
 
1107
            {
 
1108
              grub_free (root.data);
 
1109
              return grub_errno;
 
1110
            }
 
1111
          chc = dir.type_specific.volume_label.character_count;
 
1112
          if (chc > ARRAY_SIZE (dir.type_specific.volume_label.str))
 
1113
            chc = ARRAY_SIZE (dir.type_specific.volume_label.str);
 
1114
          for (i = 0; i < chc; i++)
 
1115
            t[i] = grub_le_to_cpu16 (dir.type_specific.volume_label.str[i]);
 
1116
          *grub_utf16_to_utf8 ((grub_uint8_t *) *label, t, chc) = '\0';
 
1117
        }
 
1118
    }
 
1119
 
 
1120
  grub_free (root.data);
 
1121
  return grub_errno;
 
1122
}
 
1123
 
 
1124
#else
 
1125
 
 
1126
static grub_err_t
 
1127
grub_fat_label (grub_device_t device, char **label)
 
1128
{
 
1129
  grub_disk_t disk = device->disk;
 
1130
  grub_err_t err;
 
1131
  struct grub_fat_iterate_context ctxt;
 
1132
  struct grub_fshelp_node root = {
 
1133
    .disk = disk,
 
1134
    .attr = GRUB_FAT_ATTR_DIRECTORY,
 
1135
    .file_size = 0,
 
1136
    .cur_cluster_num = ~0U,
 
1137
    .cur_cluster = 0,
 
1138
  };
 
1139
 
 
1140
  *label = 0;
 
1141
 
 
1142
  grub_dl_ref (my_mod);
 
1143
 
 
1144
  root.data = grub_fat_mount (disk);
 
1145
  if (! root.data)
 
1146
    goto fail;
 
1147
 
 
1148
  root.file_cluster = root.data->root_cluster;
 
1149
 
 
1150
  err = grub_fat_iterate_init (&ctxt);
 
1151
  if (err)
 
1152
    goto fail;
 
1153
 
 
1154
  while (!(err = grub_fat_iterate_dir_next (&root, &ctxt)))
 
1155
    if ((ctxt.dir.attr & ~GRUB_FAT_ATTR_ARCHIVE) == GRUB_FAT_ATTR_VOLUME_ID)
 
1156
      {
 
1157
        *label = grub_strdup (ctxt.filename);
 
1158
        break;
 
1159
      }
 
1160
 
 
1161
  grub_fat_iterate_fini (&ctxt);
 
1162
 
 
1163
 fail:
 
1164
 
 
1165
  grub_dl_unref (my_mod);
 
1166
 
 
1167
  grub_free (root.data);
 
1168
 
 
1169
  return grub_errno;
 
1170
}
 
1171
 
 
1172
#endif
 
1173
 
 
1174
static grub_err_t
 
1175
grub_fat_uuid (grub_device_t device, char **uuid)
 
1176
{
 
1177
  struct grub_fat_data *data;
 
1178
  grub_disk_t disk = device->disk;
 
1179
 
 
1180
  grub_dl_ref (my_mod);
 
1181
 
 
1182
  data = grub_fat_mount (disk);
 
1183
  if (data)
 
1184
    {
 
1185
      char *ptr;
 
1186
      *uuid = grub_xasprintf ("%04x-%04x",
 
1187
                             (grub_uint16_t) (data->uuid >> 16),
 
1188
                             (grub_uint16_t) data->uuid);
 
1189
      for (ptr = *uuid; ptr && *ptr; ptr++)
 
1190
        *ptr = grub_toupper (*ptr);
 
1191
    }
 
1192
  else
 
1193
    *uuid = NULL;
 
1194
 
 
1195
  grub_dl_unref (my_mod);
 
1196
 
 
1197
  grub_free (data);
 
1198
 
 
1199
  return grub_errno;
 
1200
}
 
1201
 
 
1202
#ifdef GRUB_UTIL
 
1203
#ifndef MODE_EXFAT
 
1204
grub_disk_addr_t
 
1205
grub_fat_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
 
1206
#else
 
1207
grub_disk_addr_t
 
1208
  grub_exfat_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
 
1209
#endif
 
1210
{
 
1211
  grub_disk_addr_t ret;
 
1212
  struct grub_fat_data *data;
 
1213
  data = grub_fat_mount (disk);
 
1214
  if (!data)
 
1215
    return 0;
 
1216
  ret = data->cluster_sector;
 
1217
 
 
1218
  *sec_per_lcn = 1ULL << data->cluster_bits;
 
1219
 
 
1220
  grub_free (data);
 
1221
  return ret;
 
1222
}
 
1223
#endif
 
1224
 
 
1225
static struct grub_fs grub_fat_fs =
 
1226
  {
 
1227
#ifdef MODE_EXFAT
 
1228
    .name = "exfat",
 
1229
#else
 
1230
    .name = "fat",
 
1231
#endif
 
1232
    .dir = grub_fat_dir,
 
1233
    .open = grub_fat_open,
 
1234
    .read = grub_fat_read,
 
1235
    .close = grub_fat_close,
 
1236
    .label = grub_fat_label,
 
1237
    .uuid = grub_fat_uuid,
 
1238
#ifdef GRUB_UTIL
 
1239
#ifdef MODE_EXFAT
 
1240
    /* ExFAT BPB is 30 larger than FAT32 one.  */
 
1241
    .reserved_first_sector = 0,
 
1242
#else
 
1243
    .reserved_first_sector = 1,
 
1244
#endif
 
1245
    .blocklist_install = 1,
 
1246
#endif
 
1247
    .next = 0
 
1248
  };
 
1249
 
 
1250
#ifdef MODE_EXFAT
 
1251
GRUB_MOD_INIT(exfat)
 
1252
#else
 
1253
GRUB_MOD_INIT(fat)
 
1254
#endif
 
1255
{
 
1256
  COMPILE_TIME_ASSERT (sizeof (struct grub_fat_dir_entry) == 32);
 
1257
  grub_fs_register (&grub_fat_fs);
 
1258
  my_mod = mod;
 
1259
}
 
1260
#ifdef MODE_EXFAT
 
1261
GRUB_MOD_FINI(exfat)
 
1262
#else
 
1263
GRUB_MOD_FINI(fat)
 
1264
#endif
 
1265
{
 
1266
  grub_fs_unregister (&grub_fat_fs);
 
1267
}
 
1268