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