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