3 exFAT file system implementation library.
5 Copyright (C) 2009, 2010 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #define _XOPEN_SOURCE /* for timezone in Linux */
28 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
31 memset(stbuf, 0, sizeof(struct stat));
32 if (node->flags & EXFAT_ATTRIB_DIR)
33 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
35 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
37 stbuf->st_uid = ef->uid;
38 stbuf->st_gid = ef->gid;
39 stbuf->st_size = node->size;
40 stbuf->st_blocks = DIV_ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) *
41 CLUSTER_SIZE(*ef->sb) / 512;
42 stbuf->st_mtime = node->mtime;
43 stbuf->st_atime = node->atime;
44 stbuf->st_ctime = 0; /* unapplicable */
47 #define SEC_IN_MIN 60ll
48 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
49 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
50 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
51 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
52 #define UNIX_EPOCH_YEAR 1970
53 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
54 #define EXFAT_EPOCH_YEAR 1980
55 /* number of years from Unix epoch to exFAT epoch */
56 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
57 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
58 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
59 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
60 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
61 /* number of leap years passed from exFAT epoch to the specified year
62 (excluding the specified year itself) */
63 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
64 - (EXFAT_EPOCH_YEAR - 1) / 4)
65 /* checks whether the specified year is leap */
66 #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
68 static const time_t days_in_year[] =
70 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
71 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
74 time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec)
76 time_t unix_time = EPOCH_DIFF_SEC;
77 uint16_t ndate = le16_to_cpu(date);
78 uint16_t ntime = le16_to_cpu(time);
80 uint16_t day = ndate & 0x1f; /* 5 bits, 1-31 */
81 uint16_t month = ndate >> 5 & 0xf; /* 4 bits, 1-12 */
82 uint16_t year = ndate >> 9; /* 7 bits, 1-127 (+1980) */
84 uint16_t twosec = ntime & 0x1f; /* 5 bits, 0-29 (2 sec granularity) */
85 uint16_t min = ntime >> 5 & 0xf; /* 6 bits, 0-59 */
86 uint16_t hour = ntime >> 11; /* 5 bits, 0-23 */
88 if (day == 0 || month == 0 || month > 12)
90 exfat_error("bad date %hu-%02hu-%02hu",
91 year + EXFAT_EPOCH_YEAR, month, day);
94 if (hour > 23 || min > 59 || twosec > 29)
96 exfat_error("bad time %hu:%02hu:%02hu",
97 hour, min, twosec * 2);
102 exfat_error("bad centiseconds count %hhu", centisec);
106 /* every 4th year between 1904 and 2096 is leap */
107 unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
108 unix_time += days_in_year[month] * SEC_IN_DAY;
109 /* if it's leap year and February has passed we should add 1 day */
110 if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
111 unix_time += SEC_IN_DAY;
112 unix_time += (day - 1) * SEC_IN_DAY;
114 unix_time += hour * SEC_IN_HOUR;
115 unix_time += min * SEC_IN_MIN;
116 /* exFAT represents time with 2 sec granularity */
117 unix_time += twosec * 2;
118 unix_time += centisec / 100;
120 /* exFAT stores timestamps in local time, so we correct it to UTC */
121 unix_time += timezone;
126 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time,
129 time_t shift = EPOCH_DIFF_SEC + timezone;
130 uint16_t day, month, year;
131 uint16_t twosec, min, hour;
135 /* time before exFAT epoch cannot be represented */
136 if (unix_time < shift)
141 days = unix_time / SEC_IN_DAY;
142 year = (4 * days) / (4 * 365 + 1);
143 days -= year * 365 + LEAP_YEARS(year);
145 for (i = 1; i <= 12; i++)
147 int leap_day = (IS_LEAP_YEAR(year) && i == 2);
148 int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
150 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
153 days -= days_in_year[i] + leap_sub;
159 hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
160 min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
161 twosec = (unix_time % SEC_IN_MIN) / 2;
163 *date = cpu_to_le16(day | (month << 5) | (year << 9));
164 *time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
166 *centisec = (unix_time % 2) * 100;
169 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
171 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
172 exfat_bug("failed to convert name to UTF-8");
175 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
180 for (i = 0; i < sizeof(struct exfat_entry); i++)
181 if (i != 2 && i != 3) /* skip checksum field itself */
182 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
186 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
190 for (i = 0; i < sizeof(struct exfat_entry); i++)
191 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
195 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
196 const struct exfat_entry_meta2* meta2, const le16_t* name)
199 const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
202 checksum = exfat_start_checksum(meta1);
203 checksum = exfat_add_checksum(meta2, checksum);
204 for (i = 0; i < name_entries; i++)
206 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
207 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
208 EXFAT_ENAME_MAX * sizeof(le16_t));
209 checksum = exfat_add_checksum(&name_entry, checksum);
211 return cpu_to_le16(checksum);
214 uint32_t exfat_vbr_start_checksum(const void* sector, size_t size)
219 for (i = 0; i < size; i++)
220 /* skip volume_state and allocated_percent fields */
221 if (i != 0x6a && i != 0x6b && i != 0x70)
222 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
226 uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
230 for (i = 0; i < size; i++)
231 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
236 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
239 size_t length = utf16_length(name);
242 for (i = 0; i < length; i++)
244 uint16_t c = le16_to_cpu(name[i]);
246 /* convert to upper case */
247 if (c < ef->upcase_chars)
248 c = le16_to_cpu(ef->upcase[c]);
250 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
251 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
253 return cpu_to_le16(hash);
256 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
259 const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB"};
260 uint64_t divisor = 1;
262 for (i = 0; i < sizeof(units) / sizeof(units[0]) - 1; i++)
264 if ((value + divisor / 2) / divisor < 1024)
268 hb->value = (value + divisor / 2) / divisor;
272 void exfat_print_info(const struct exfat_super_block* sb,
273 uint32_t free_clusters)
275 struct exfat_human_bytes hb;
276 off_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb);
277 off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
279 printf("File system version %hhu.%hhu\n",
280 sb->version.major, sb->version.minor);
281 exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb);
282 printf("Sector size %10"PRIu64" %s\n", hb.value, hb.unit);
283 exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
284 printf("Cluster size %10"PRIu64" %s\n", hb.value, hb.unit);
285 exfat_humanize_bytes(total_space, &hb);
286 printf("Volume size %10"PRIu64" %s\n", hb.value, hb.unit);
287 exfat_humanize_bytes(total_space - avail_space, &hb);
288 printf("Used space %10"PRIu64" %s\n", hb.value, hb.unit);
289 exfat_humanize_bytes(avail_space, &hb);
290 printf("Available space %10"PRIu64" %s\n", hb.value, hb.unit);