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 /* set ctime to mtime to ensure we don't break programs that rely on ctime
46 stbuf->st_ctime = node->mtime;
49 #define SEC_IN_MIN 60ll
50 #define SEC_IN_HOUR (60 * SEC_IN_MIN)
51 #define SEC_IN_DAY (24 * SEC_IN_HOUR)
52 #define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
53 /* Unix epoch started at 0:00:00 UTC 1 January 1970 */
54 #define UNIX_EPOCH_YEAR 1970
55 /* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
56 #define EXFAT_EPOCH_YEAR 1980
57 /* number of years from Unix epoch to exFAT epoch */
58 #define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
59 /* number of days from Unix epoch to exFAT epoch (considering leap days) */
60 #define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4)
61 /* number of seconds from Unix epoch to exFAT epoch (considering leap days) */
62 #define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY)
63 /* number of leap years passed from exFAT epoch to the specified year
64 (excluding the specified year itself) */
65 #define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \
66 - (EXFAT_EPOCH_YEAR - 1) / 4)
67 /* checks whether the specified year is leap */
68 #define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0)
70 static const time_t days_in_year[] =
72 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
73 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
76 time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec)
78 time_t unix_time = EPOCH_DIFF_SEC;
79 uint16_t ndate = le16_to_cpu(date);
80 uint16_t ntime = le16_to_cpu(time);
82 uint16_t day = ndate & 0x1f; /* 5 bits, 1-31 */
83 uint16_t month = ndate >> 5 & 0xf; /* 4 bits, 1-12 */
84 uint16_t year = ndate >> 9; /* 7 bits, 1-127 (+1980) */
86 uint16_t twosec = ntime & 0x1f; /* 5 bits, 0-29 (2 sec granularity) */
87 uint16_t min = ntime >> 5 & 0xf; /* 6 bits, 0-59 */
88 uint16_t hour = ntime >> 11; /* 5 bits, 0-23 */
90 if (day == 0 || month == 0 || month > 12)
92 exfat_error("bad date %hu-%02hu-%02hu",
93 year + EXFAT_EPOCH_YEAR, month, day);
96 if (hour > 23 || min > 59 || twosec > 29)
98 exfat_error("bad time %hu:%02hu:%02hu",
99 hour, min, twosec * 2);
104 exfat_error("bad centiseconds count %hhu", centisec);
108 /* every 4th year between 1904 and 2096 is leap */
109 unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY;
110 unix_time += days_in_year[month] * SEC_IN_DAY;
111 /* if it's leap year and February has passed we should add 1 day */
112 if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2)
113 unix_time += SEC_IN_DAY;
114 unix_time += (day - 1) * SEC_IN_DAY;
116 unix_time += hour * SEC_IN_HOUR;
117 unix_time += min * SEC_IN_MIN;
118 /* exFAT represents time with 2 sec granularity */
119 unix_time += twosec * 2;
120 unix_time += centisec / 100;
122 /* exFAT stores timestamps in local time, so we correct it to UTC */
123 unix_time += timezone;
128 void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time,
131 time_t shift = EPOCH_DIFF_SEC + timezone;
132 uint16_t day, month, year;
133 uint16_t twosec, min, hour;
137 /* time before exFAT epoch cannot be represented */
138 if (unix_time < shift)
143 days = unix_time / SEC_IN_DAY;
144 year = (4 * days) / (4 * 365 + 1);
145 days -= year * 365 + LEAP_YEARS(year);
147 for (i = 1; i <= 12; i++)
149 int leap_day = (IS_LEAP_YEAR(year) && i == 2);
150 int leap_sub = (IS_LEAP_YEAR(year) && i >= 3);
152 if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day)
155 days -= days_in_year[i] + leap_sub;
161 hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR;
162 min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN;
163 twosec = (unix_time % SEC_IN_MIN) / 2;
165 *date = cpu_to_le16(day | (month << 5) | (year << 9));
166 *time = cpu_to_le16(twosec | (min << 5) | (hour << 11));
168 *centisec = (unix_time % 2) * 100;
171 void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
173 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
174 exfat_bug("failed to convert name to UTF-8");
177 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
182 for (i = 0; i < sizeof(struct exfat_entry); i++)
183 if (i != 2 && i != 3) /* skip checksum field itself */
184 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
188 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
192 for (i = 0; i < sizeof(struct exfat_entry); i++)
193 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
197 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
198 const struct exfat_entry_meta2* meta2, const le16_t* name)
201 const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
204 checksum = exfat_start_checksum(meta1);
205 checksum = exfat_add_checksum(meta2, checksum);
206 for (i = 0; i < name_entries; i++)
208 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
209 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
210 EXFAT_ENAME_MAX * sizeof(le16_t));
211 checksum = exfat_add_checksum(&name_entry, checksum);
213 return cpu_to_le16(checksum);
216 uint32_t exfat_vbr_start_checksum(const void* sector, size_t size)
221 for (i = 0; i < size; i++)
222 /* skip volume_state and allocated_percent fields */
223 if (i != 0x6a && i != 0x6b && i != 0x70)
224 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
228 uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
232 for (i = 0; i < size; i++)
233 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
238 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
241 size_t length = utf16_length(name);
244 for (i = 0; i < length; i++)
246 uint16_t c = le16_to_cpu(name[i]);
248 /* convert to upper case */
249 if (c < ef->upcase_chars)
250 c = le16_to_cpu(ef->upcase[c]);
252 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
253 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
255 return cpu_to_le16(hash);
258 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
261 const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB"};
262 uint64_t divisor = 1;
265 for (i = 0; i < sizeof(units) / sizeof(units[0]) - 1; i++, divisor *= 1024)
267 temp = (value + divisor / 2) / divisor;
271 if (temp / 1024 * 1024 == temp)
280 void exfat_print_info(const struct exfat_super_block* sb,
281 uint32_t free_clusters)
283 struct exfat_human_bytes hb;
284 off_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb);
285 off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
287 printf("File system version %hhu.%hhu\n",
288 sb->version.major, sb->version.minor);
289 exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb);
290 printf("Sector size %10"PRIu64" %s\n", hb.value, hb.unit);
291 exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
292 printf("Cluster size %10"PRIu64" %s\n", hb.value, hb.unit);
293 exfat_humanize_bytes(total_space, &hb);
294 printf("Volume size %10"PRIu64" %s\n", hb.value, hb.unit);
295 exfat_humanize_bytes(total_space - avail_space, &hb);
296 printf("Used space %10"PRIu64" %s\n", hb.value, hb.unit);
297 exfat_humanize_bytes(avail_space, &hb);
298 printf("Available space %10"PRIu64" %s\n", hb.value, hb.unit);