1
/* fat.c - FAT filesystem */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
21
#include <grub/disk.h>
22
#include <grub/file.h>
23
#include <grub/types.h>
24
#include <grub/misc.h>
28
#include <grub/charset.h>
32
#include <grub/exfat.h>
34
#include <grub/fshelp.h>
35
#include <grub/i18n.h>
37
GRUB_MOD_LICENSE ("GPLv3+");
41
GRUB_FAT_ATTR_READ_ONLY = 0x01,
42
GRUB_FAT_ATTR_HIDDEN = 0x02,
43
GRUB_FAT_ATTR_SYSTEM = 0x04,
45
GRUB_FAT_ATTR_VOLUME_ID = 0x08,
47
GRUB_FAT_ATTR_DIRECTORY = 0x10,
48
GRUB_FAT_ATTR_ARCHIVE = 0x20,
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),
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
62
| GRUB_FAT_ATTR_VOLUME_ID
68
typedef struct grub_exfat_bpb grub_current_fat_bpb_t;
70
typedef struct grub_fat_bpb grub_current_fat_bpb_t;
78
struct grub_fat_dir_entry
80
grub_uint8_t entry_type;
83
grub_uint8_t placeholder[31];
85
grub_uint8_t secondary_count;
86
grub_uint16_t checksum;
88
grub_uint16_t reserved1;
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];
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;
110
grub_uint16_t str[15];
111
} GRUB_PACKED file_name;
113
grub_uint8_t character_count;
114
grub_uint16_t str[15];
115
} GRUB_PACKED volume_label;
116
} GRUB_PACKED type_specific;
119
struct grub_fat_dir_node
122
grub_uint32_t first_cluster;
123
grub_uint64_t file_size;
124
grub_uint64_t valid_size;
129
typedef struct grub_fat_dir_node grub_fat_dir_node_t;
132
struct grub_fat_dir_entry
134
grub_uint8_t name[11];
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;
148
struct grub_fat_long_name_entry
151
grub_uint16_t name1[5];
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];
160
typedef struct grub_fat_dir_entry grub_fat_dir_node_t;
166
int logical_sector_bits;
167
grub_uint32_t num_sectors;
169
grub_uint32_t fat_sector;
170
grub_uint32_t sectors_per_fat;
173
grub_uint32_t root_cluster;
175
grub_uint32_t root_sector;
176
grub_uint32_t num_root_sectors;
180
grub_uint32_t cluster_eof_mark;
181
grub_uint32_t cluster_sector;
182
grub_uint32_t num_clusters;
187
struct grub_fshelp_node {
189
struct grub_fat_data *data;
193
grub_uint32_t file_size;
195
grub_uint64_t file_size;
197
grub_uint32_t file_cluster;
198
grub_uint32_t cur_cluster_num;
199
grub_uint32_t cur_cluster;
206
static grub_dl_t my_mod;
210
fat_log2 (unsigned x)
217
for (i = 0; (x & 1) == 0; i++)
227
static struct grub_fat_data *
228
grub_fat_mount (grub_disk_t disk)
230
grub_current_fat_bpb_t bpb;
231
struct grub_fat_data *data = 0;
232
grub_uint32_t first_fat, magic;
237
data = (struct grub_fat_data *) grub_malloc (sizeof (*data));
242
if (grub_disk_read (disk, 0, 0, sizeof (bpb), &bpb))
246
if (grub_memcmp ((const char *) bpb.oem_name, "EXFAT ",
247
sizeof (bpb.oem_name)) != 0)
251
/* Get the sizes of logical sectors and clusters. */
253
data->logical_sector_bits = bpb.bytes_per_sector_shift;
255
data->logical_sector_bits =
256
fat_log2 (grub_le_to_cpu16 (bpb.bytes_per_sector));
258
if (data->logical_sector_bits < GRUB_DISK_SECTOR_BITS
259
|| data->logical_sector_bits >= 16)
261
data->logical_sector_bits -= GRUB_DISK_SECTOR_BITS;
264
data->cluster_bits = bpb.sectors_per_cluster_shift;
266
data->cluster_bits = fat_log2 (bpb.sectors_per_cluster);
268
if (data->cluster_bits < 0 || data->cluster_bits > 25)
270
data->cluster_bits += data->logical_sector_bits;
272
/* Get information about FATs. */
274
data->fat_sector = (grub_le_to_cpu32 (bpb.num_reserved_sectors)
275
<< data->logical_sector_bits);
277
data->fat_sector = (grub_le_to_cpu16 (bpb.num_reserved_sectors)
278
<< data->logical_sector_bits);
280
if (data->fat_sector == 0)
284
data->sectors_per_fat = (grub_le_to_cpu32 (bpb.sectors_per_fat)
285
<< data->logical_sector_bits);
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);
292
if (data->sectors_per_fat == 0)
295
/* Get the number of sectors in this volume. */
297
data->num_sectors = ((grub_le_to_cpu64 (bpb.num_total_sectors))
298
<< data->logical_sector_bits);
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);
305
if (data->num_sectors == 0)
308
/* Get information about the root directory. */
309
if (bpb.num_fats == 0)
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));
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);
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)
334
if (data->num_clusters <= 2)
340
data->root_cluster = grub_le_to_cpu32 (bpb.root_cluster);
342
data->cluster_eof_mark = 0xffffffff;
344
if ((bpb.volume_flags & grub_cpu_to_le16_compile_time (0x1))
346
data->fat_sector += data->sectors_per_fat;
349
if (! bpb.sectors_per_fat_16)
352
grub_uint16_t flags = grub_le_to_cpu16 (bpb.version_specific.fat32.extended_flags);
354
data->root_cluster = grub_le_to_cpu32 (bpb.version_specific.fat32.root_cluster);
356
data->cluster_eof_mark = 0x0ffffff8;
360
/* Get an active FAT. */
361
unsigned active_fat = flags & 0xf;
363
if (active_fat > bpb.num_fats)
366
data->fat_sector += active_fat * data->sectors_per_fat;
369
if (bpb.num_root_entries != 0 || bpb.version_specific.fat32.fs_version != 0)
374
/* FAT12 or FAT16. */
375
data->root_cluster = ~0U;
377
if (data->num_clusters <= 4085 + 2)
381
data->cluster_eof_mark = 0x0ff8;
387
data->cluster_eof_mark = 0xfff8;
392
/* More sanity checks. */
393
if (data->num_sectors <= data->fat_sector)
396
if (grub_disk_read (disk,
403
first_fat = grub_le_to_cpu32 (first_fat);
405
if (data->fat_size == 32)
407
first_fat &= 0x0fffffff;
410
else if (data->fat_size == 16)
412
first_fat &= 0x0000ffff;
417
first_fat &= 0x00000fff;
423
data->uuid = grub_le_to_cpu32 (bpb.num_serial);
425
if (bpb.sectors_per_fat_16)
426
data->uuid = grub_le_to_cpu32 (bpb.version_specific.fat12_or_fat16.num_serial);
428
data->uuid = grub_le_to_cpu32 (bpb.version_specific.fat32.num_serial);
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))
447
grub_error (GRUB_ERR_BAD_FS, "not a FAT filesystem");
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)
457
grub_uint32_t logical_cluster;
458
unsigned logical_cluster_bits;
459
grub_ssize_t ret = 0;
460
unsigned long sector;
463
/* This is a special case. FAT12 and FAT16 doesn't have the root directory
465
if (node->file_cluster == ~0U)
467
size = (node->data->num_root_sectors << GRUB_DISK_SECTOR_BITS) - offset;
471
if (grub_disk_read (disk, node->data->root_sector, offset, size, buf))
479
if (node->is_contiguous)
481
/* Read the data here. */
482
sector = (node->data->cluster_sector
483
+ ((node->file_cluster - 2)
484
<< node->data->cluster_bits));
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);
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;
504
if (logical_cluster < node->cur_cluster_num)
506
node->cur_cluster_num = 0;
507
node->cur_cluster = node->file_cluster;
512
while (logical_cluster > node->cur_cluster_num)
514
/* Find next cluster. */
515
grub_uint32_t next_cluster;
516
grub_uint32_t fat_offset;
518
switch (node->data->fat_size)
521
fat_offset = node->cur_cluster << 2;
524
fat_offset = node->cur_cluster << 1;
528
fat_offset = node->cur_cluster + (node->cur_cluster >> 1);
533
if (grub_disk_read (disk, node->data->fat_sector, fat_offset,
534
(node->data->fat_size + 7) >> 3,
535
(char *) &next_cluster))
538
next_cluster = grub_le_to_cpu32 (next_cluster);
539
switch (node->data->fat_size)
542
next_cluster &= 0xFFFF;
545
if (node->cur_cluster & 1)
548
next_cluster &= 0x0FFF;
552
grub_dprintf ("fat", "fat_size=%d, next_cluster=%u\n",
553
node->data->fat_size, next_cluster);
556
if (next_cluster >= node->data->cluster_eof_mark)
559
if (next_cluster < 2 || next_cluster >= node->data->num_clusters)
561
grub_error (GRUB_ERR_BAD_FS, "invalid cluster %u",
566
node->cur_cluster = next_cluster;
567
node->cur_cluster_num++;
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;
578
disk->read_hook = read_hook;
579
disk->read_hook_data = read_hook_data;
580
grub_disk_read (disk, sector, offset, size, buf);
595
struct grub_fat_iterate_context
598
struct grub_fat_dir_node dir;
600
struct grub_fat_dir_entry dir;
603
grub_uint16_t *unibuf;
608
grub_fat_iterate_init (struct grub_fat_iterate_context *ctxt)
610
ctxt->offset = -sizeof (struct grub_fat_dir_entry);
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);
617
ctxt->unibuf = grub_malloc (15 * 256 * 2);
618
ctxt->filename = grub_malloc (15 * 256 * GRUB_MAX_UTF8_PER_UTF16 + 1);
621
if (! ctxt->filename || ! ctxt->unibuf)
623
grub_free (ctxt->filename);
624
grub_free (ctxt->unibuf);
627
return GRUB_ERR_NONE;
631
grub_fat_iterate_fini (struct grub_fat_iterate_context *ctxt)
633
grub_free (ctxt->filename);
634
grub_free (ctxt->unibuf);
639
grub_fat_iterate_dir_next (grub_fshelp_node_t node,
640
struct grub_fat_iterate_context *ctxt)
642
grub_memset (&ctxt->dir, 0, sizeof (ctxt->dir));
645
struct grub_fat_dir_entry dir;
647
ctxt->offset += sizeof (dir);
649
if (grub_fat_read_data (node->disk, node, 0, 0, ctxt->offset, sizeof (dir),
654
if (dir.entry_type == 0)
656
if (!(dir.entry_type & 0x80))
659
if (dir.entry_type == 0x85)
661
unsigned i, nsec, slots = 0;
663
nsec = dir.type_specific.file.secondary_count;
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++)
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)
675
if (!(sec.entry_type & 0x80))
677
if (!(sec.entry_type & 0x40))
679
switch (sec.entry_type)
682
ctxt->dir.first_cluster = grub_cpu_to_le32 (sec.type_specific.stream_extension.first_cluster);
684
= grub_cpu_to_le64 (sec.type_specific.stream_extension.valid_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));
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]);
701
grub_dprintf ("exfat", "unknown secondary type 0x%02x\n",
708
ctxt->offset -= sizeof (dir);
712
*grub_utf16_to_utf8 ((grub_uint8_t *) ctxt->filename, ctxt->unibuf,
717
/* Allocation bitmap. */
718
if (dir.entry_type == 0x81)
721
if (dir.entry_type == 0x82)
724
if (dir.entry_type == 0x83)
726
grub_dprintf ("exfat", "unknown primary type 0x%02x\n",
729
return grub_errno ? : GRUB_ERR_EOF;
735
grub_fat_iterate_dir_next (grub_fshelp_node_t node,
736
struct grub_fat_iterate_context *ctxt)
740
int slot = -1, slots = -1;
746
/* Adjust the offset. */
747
ctxt->offset += sizeof (ctxt->dir);
749
/* Read a directory entry. */
750
if (grub_fat_read_data (node->disk, node, 0, 0,
751
ctxt->offset, sizeof (ctxt->dir),
753
!= sizeof (ctxt->dir) || ctxt->dir.name[0] == 0)
756
/* Handle long name entries. */
757
if (ctxt->dir.attr == GRUB_FAT_ATTR_LONG_NAME)
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;
767
checksum = long_name->checksum;
770
if (id != slot || slot == 0 || checksum != long_name->checksum)
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);
783
/* Check if this entry is valid. */
784
if (ctxt->dir.name[0] == 0xe5 || (ctxt->dir.attr & ~GRUB_FAT_ATTR_VALID))
787
/* This is a workaround for Japanese. */
788
if (ctxt->dir.name[0] == 0x05)
789
ctxt->dir.name[0] = 0xe5;
791
if (checksum != -1 && slot == 0)
795
for (sum = 0, i = 0; i < sizeof (ctxt->dir.name); i++)
796
sum = ((sum >> 1) | (sum << 7)) + ctxt->dir.name[i];
802
for (u = 0; u < slots * 13; u++)
803
ctxt->unibuf[u] = grub_le_to_cpu16 (ctxt->unibuf[u]);
805
*grub_utf16_to_utf8 ((grub_uint8_t *) ctxt->filename,
809
return GRUB_ERR_NONE;
815
/* Convert the 8.3 file name. */
816
filep = ctxt->filename;
817
if (ctxt->dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
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] == ' ')
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] == ' ')
837
/* XXX should we check that dir position is 0 or 1? */
838
if (i > 2 || filep[0] != '.' || (i == 2 && filep[1] != '.'))
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] == ' ')
853
return GRUB_ERR_NONE;
856
return grub_errno ? : GRUB_ERR_EOF;
861
static grub_err_t lookup_file (grub_fshelp_node_t node,
863
grub_fshelp_node_t *foundnode,
864
enum grub_fshelp_filetype *foundtype)
867
struct grub_fat_iterate_context ctxt;
869
err = grub_fat_iterate_init (&ctxt);
873
while (!(err = grub_fat_iterate_dir_next (node, &ctxt)))
877
if (!ctxt.dir.have_stream)
880
if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
884
if (grub_strcasecmp (name, ctxt.filename) == 0)
886
*foundnode = grub_malloc (sizeof (struct grub_fshelp_node));
889
(*foundnode)->attr = ctxt.dir.attr;
891
(*foundnode)->file_size = ctxt.dir.file_size;
892
(*foundnode)->file_cluster = ctxt.dir.first_cluster;
893
(*foundnode)->is_contiguous = ctxt.dir.is_contiguous;
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;
902
(*foundnode)->cur_cluster_num = ~0U;
903
(*foundnode)->data = node->data;
904
(*foundnode)->disk = node->disk;
906
*foundtype = ((*foundnode)->attr & GRUB_FAT_ATTR_DIRECTORY) ? GRUB_FSHELP_DIR : GRUB_FSHELP_REG;
908
grub_fat_iterate_fini (&ctxt);
909
return GRUB_ERR_NONE;
913
grub_fat_iterate_fini (&ctxt);
914
if (err == GRUB_ERR_EOF)
922
grub_fat_dir (grub_device_t device, const char *path, grub_fs_dir_hook_t hook,
925
struct grub_fat_data *data = 0;
926
grub_disk_t disk = device->disk;
927
grub_fshelp_node_t found = NULL;
929
struct grub_fat_iterate_context ctxt;
931
grub_dl_ref (my_mod);
933
data = grub_fat_mount (disk);
937
struct grub_fshelp_node root = {
940
.attr = GRUB_FAT_ATTR_DIRECTORY,
942
.file_cluster = data->root_cluster,
943
.cur_cluster_num = ~0U,
950
err = grub_fshelp_find_file_lookup (path, &root, &found, lookup_file, NULL, GRUB_FSHELP_DIR);
954
err = grub_fat_iterate_init (&ctxt);
958
while (!(err = grub_fat_iterate_dir_next (found, &ctxt)))
960
struct grub_dirhook_info info;
961
grub_memset (&info, 0, sizeof (info));
963
info.dir = !! (ctxt.dir.attr & GRUB_FAT_ATTR_DIRECTORY);
964
info.case_insensitive = 1;
966
if (!ctxt.dir.have_stream)
969
if (ctxt.dir.attr & GRUB_FAT_ATTR_VOLUME_ID)
973
if (hook (ctxt.filename, &info, hook_data))
976
grub_fat_iterate_fini (&ctxt);
977
if (err == GRUB_ERR_EOF)
986
grub_dl_unref (my_mod);
992
grub_fat_open (grub_file_t file, const char *name)
994
struct grub_fat_data *data = 0;
995
grub_fshelp_node_t found = NULL;
997
grub_disk_t disk = file->device->disk;
999
grub_dl_ref (my_mod);
1001
data = grub_fat_mount (disk);
1005
struct grub_fshelp_node root = {
1008
.attr = GRUB_FAT_ATTR_DIRECTORY,
1010
.file_cluster = data->root_cluster,
1011
.cur_cluster_num = ~0U,
1018
err = grub_fshelp_find_file_lookup (name, &root, &found, lookup_file, NULL, GRUB_FSHELP_REG);
1023
file->size = found->file_size;
1025
return GRUB_ERR_NONE;
1034
grub_dl_unref (my_mod);
1040
grub_fat_read (grub_file_t file, char *buf, grub_size_t len)
1042
return grub_fat_read_data (file->device->disk, file->data,
1043
file->read_hook, file->read_hook_data,
1044
file->offset, len, buf);
1048
grub_fat_close (grub_file_t file)
1050
grub_fshelp_node_t node = file->data;
1052
grub_free (node->data);
1055
grub_dl_unref (my_mod);
1062
grub_fat_label (grub_device_t device, char **label)
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 = {
1069
.attr = GRUB_FAT_ATTR_DIRECTORY,
1071
.cur_cluster_num = ~0U,
1076
root.data = grub_fat_mount (disk);
1080
root.file_cluster = root.data->root_cluster;
1086
offset += sizeof (dir);
1088
if (grub_fat_read_data (disk, &root, 0, 0,
1089
offset, sizeof (dir), (char *) &dir)
1093
if (dir.entry_type == 0)
1095
if (!(dir.entry_type & 0x80))
1099
if (dir.entry_type == 0x83)
1102
grub_uint16_t t[ARRAY_SIZE (dir.type_specific.volume_label.str)];
1104
*label = grub_malloc (ARRAY_SIZE (dir.type_specific.volume_label.str)
1105
* GRUB_MAX_UTF8_PER_UTF16 + 1);
1108
grub_free (root.data);
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';
1120
grub_free (root.data);
1127
grub_fat_label (grub_device_t device, char **label)
1129
grub_disk_t disk = device->disk;
1131
struct grub_fat_iterate_context ctxt;
1132
struct grub_fshelp_node root = {
1134
.attr = GRUB_FAT_ATTR_DIRECTORY,
1136
.cur_cluster_num = ~0U,
1142
grub_dl_ref (my_mod);
1144
root.data = grub_fat_mount (disk);
1148
root.file_cluster = root.data->root_cluster;
1150
err = grub_fat_iterate_init (&ctxt);
1154
while (!(err = grub_fat_iterate_dir_next (&root, &ctxt)))
1155
if ((ctxt.dir.attr & ~GRUB_FAT_ATTR_ARCHIVE) == GRUB_FAT_ATTR_VOLUME_ID)
1157
*label = grub_strdup (ctxt.filename);
1161
grub_fat_iterate_fini (&ctxt);
1165
grub_dl_unref (my_mod);
1167
grub_free (root.data);
1175
grub_fat_uuid (grub_device_t device, char **uuid)
1177
struct grub_fat_data *data;
1178
grub_disk_t disk = device->disk;
1180
grub_dl_ref (my_mod);
1182
data = grub_fat_mount (disk);
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);
1195
grub_dl_unref (my_mod);
1205
grub_fat_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
1208
grub_exfat_get_cluster_sector (grub_disk_t disk, grub_uint64_t *sec_per_lcn)
1211
grub_disk_addr_t ret;
1212
struct grub_fat_data *data;
1213
data = grub_fat_mount (disk);
1216
ret = data->cluster_sector;
1218
*sec_per_lcn = 1ULL << data->cluster_bits;
1225
static struct grub_fs grub_fat_fs =
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,
1240
/* ExFAT BPB is 30 larger than FAT32 one. */
1241
.reserved_first_sector = 0,
1243
.reserved_first_sector = 1,
1245
.blocklist_install = 1,
1251
GRUB_MOD_INIT(exfat)
1256
COMPILE_TIME_ASSERT (sizeof (struct grub_fat_dir_entry) == 32);
1257
grub_fs_register (&grub_fat_fs);
1261
GRUB_MOD_FINI(exfat)
1266
grub_fs_unregister (&grub_fat_fs);