1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include "volume_id_internal.h"
35
36#define EXFAT_SB_OFFSET 0
37#define EXFAT_DIR_ENTRY_SZ 32
38#define EXFAT_MAX_DIR_ENTRIES 100
39
40struct exfat_super_block {
41 uint8_t boot_jump[3];
42 uint8_t fs_name[8];
43 uint8_t must_be_zero[53];
44 uint64_t partition_offset;
45 uint64_t volume_length;
46 uint32_t fat_offset;
47 uint32_t fat_size;
48 uint32_t cluster_heap_offset;
49 uint32_t cluster_count;
50 uint32_t root_dir;
51 uint8_t vol_serial_nr[4];
52 uint16_t fs_revision;
53 uint16_t vol_flags;
54 uint8_t bytes_per_sector;
55 uint8_t sectors_per_cluster;
56 uint8_t nr_of_fats;
57
58} PACKED;
59
60struct exfat_dir_entry {
61 uint8_t entry_type;
62 union {
63 struct volume_label {
64 uint8_t char_count;
65 uint16_t vol_label[11];
66 uint8_t reserved[8];
67 } PACKED label;
68 struct volume_guid {
69 uint8_t sec_count;
70 uint16_t set_checksum;
71 uint16_t flags;
72 uint8_t vol_guid[16];
73 uint8_t reserved[10];
74 } PACKED guid;
75 } PACKED type;
76} PACKED;
77
78int FAST_FUNC volume_id_probe_exfat(struct volume_id *id )
79{
80 struct exfat_super_block *sb;
81 struct exfat_dir_entry *de;
82 unsigned sector_sz;
83 unsigned cluster_sz;
84 uint64_t root_dir_off;
85 unsigned count;
86 unsigned need_lbl_guid;
87
88
89 dbg("exFAT: probing at offset 0x%x", EXFAT_SB_OFFSET);
90 sb = volume_id_get_buffer(id, EXFAT_SB_OFFSET, sizeof(*sb));
91
92 if (!sb)
93 return -1;
94
95 if (memcmp(sb->fs_name, "EXFAT ", 8) != 0)
96 return -1;
97
98 sector_sz = 1 << sb->bytes_per_sector;
99 cluster_sz = sector_sz << sb->sectors_per_cluster;
100
101 root_dir_off = (uint64_t)EXFAT_SB_OFFSET +
102
103 (le32_to_cpu(sb->cluster_heap_offset)) * sector_sz +
104 (le32_to_cpu(sb->root_dir) - 2) * cluster_sz;
105 dbg("exFAT: sector size 0x%x bytes", sector_sz);
106 dbg("exFAT: cluster size 0x%x bytes", cluster_sz);
107 dbg("exFAT: root dir is at 0x%llx", (long long)root_dir_off);
108
109
110 volume_id_set_uuid(id, sb->vol_serial_nr, UUID_DOS);
111
112
113
114
115
116 need_lbl_guid = (1 << 0) | (1 << 1);
117 for (count = 0; count < EXFAT_MAX_DIR_ENTRIES; count++) {
118 de = volume_id_get_buffer(id, root_dir_off + (count * EXFAT_DIR_ENTRY_SZ), EXFAT_DIR_ENTRY_SZ);
119 if (de == NULL)
120 break;
121 if (de->entry_type == 0x00) {
122
123 dbg("exFAT: End of root directory reached after %u entries", count);
124 break;
125 }
126 if (de->entry_type == 0x83) {
127
128 volume_id_set_label_unicode16(id, (uint8_t *)de->type.label.vol_label,
129 LE, 2 * de->type.label.char_count);
130 need_lbl_guid &= ~(1 << 0);
131 }
132 if (de->entry_type == 0xA0) {
133
134 volume_id_set_uuid(id, de->type.guid.vol_guid, UUID_DCE);
135 need_lbl_guid &= ~(1 << 1);
136 }
137 if (!need_lbl_guid)
138 break;
139 }
140
141 IF_FEATURE_BLKID_TYPE(id->type = "exfat";)
142 return 0;
143}
144