/*
- * utils.c
- * exFAT file system implementation library.
- *
- * Created by Andrew Nayenko on 04.09.09.
- * This software is distributed under the GNU General Public License
- * version 3 or any later.
- */
+ utils.c (04.09.09)
+ exFAT file system implementation library.
+
+ Free exFAT implementation.
+ Copyright (C) 2010-2016 Andrew Nayenko
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
#include "exfat.h"
-#include <stdlib.h>
#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#define _XOPEN_SOURCE /* for timezone in Linux */
-#include <time.h>
-#include <sys/time.h>
-
-static uint64_t rootdir_size(const struct exfat* ef)
-{
- uint64_t clusters = 0;
- cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
+#include <stdio.h>
+#include <inttypes.h>
- while (!CLUSTER_INVALID(rootdir_cluster))
- {
- clusters++;
- /* root directory cannot be contiguous because there is no flag
- to indicate this */
- rootdir_cluster = exfat_next_cluster(ef, rootdir_cluster, 0);
- }
- return clusters * CLUSTER_SIZE(*ef->sb);
+void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
+ struct stat* stbuf)
+{
+ memset(stbuf, 0, sizeof(struct stat));
+ if (node->attrib & EXFAT_ATTRIB_DIR)
+ stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
+ else
+ stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
+ stbuf->st_nlink = 1;
+ stbuf->st_uid = ef->uid;
+ stbuf->st_gid = ef->gid;
+ stbuf->st_size = node->size;
+ stbuf->st_blocks = ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) / 512;
+ stbuf->st_mtime = node->mtime;
+ stbuf->st_atime = node->atime;
+ /* set ctime to mtime to ensure we don't break programs that rely on ctime
+ (e.g. rsync) */
+ stbuf->st_ctime = node->mtime;
}
-int exfat_mount(struct exfat* ef, const char* spec)
+void exfat_get_name(const struct exfat_node* node,
+ char buffer[EXFAT_UTF8_NAME_BUFFER_MAX])
{
- tzset();
-
- ef->sb = malloc(sizeof(struct exfat_super_block));
- if (ef->sb == NULL)
- {
- exfat_error("memory allocation failed");
- return -ENOMEM;
- }
-
- ef->fd = open(spec, O_RDONLY); /* currently read only */
- if (ef->fd < 0)
- {
- free(ef->sb);
- exfat_error("failed to open `%s'", spec);
- return -EIO;
- }
+ if (utf16_to_utf8(buffer, node->name, EXFAT_UTF8_NAME_BUFFER_MAX,
+ EXFAT_NAME_MAX) != 0)
+ exfat_bug("failed to convert name to UTF-8");
+}
- exfat_read_raw(ef->sb, sizeof(struct exfat_super_block), 0, ef->fd);
- if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0)
- {
- close(ef->fd);
- free(ef->sb);
- exfat_error("exFAT file system is not found");
- return -EIO;
- }
+static uint16_t add_checksum_byte(uint16_t sum, uint8_t byte)
+{
+ return ((sum << 15) | (sum >> 1)) + byte;
+}
- ef->upcase = NULL;
- ef->upcase_chars = 0;
- ef->rootdir_size = rootdir_size(ef);
+static uint16_t add_checksum_bytes(uint16_t sum, const void* buffer, size_t n)
+{
+ int i;
- return 0;
+ for (i = 0; i < n; i++)
+ sum = add_checksum_byte(sum, ((const uint8_t*) buffer)[i]);
+ return sum;
}
-void exfat_unmount(struct exfat* ef)
+uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
{
- close(ef->fd);
- ef->fd = 0;
- free(ef->sb);
- ef->sb = NULL;
- free(ef->upcase);
- ef->upcase = NULL;
- ef->upcase_chars = 0;
+ uint16_t sum = 0;
+ int i;
+
+ for (i = 0; i < sizeof(struct exfat_entry); i++)
+ if (i != 2 && i != 3) /* skip checksum field itself */
+ sum = add_checksum_byte(sum, ((const uint8_t*) entry)[i]);
+ return sum;
}
-void exfat_stat(const struct exfat_node* node, struct stat *stbuf)
+uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
{
- memset(stbuf, 0, sizeof(struct stat));
- if (node->flags & EXFAT_ATTRIB_DIR)
- stbuf->st_mode = S_IFDIR | 0755;
- else
- stbuf->st_mode = S_IFREG | 0444;
- stbuf->st_nlink = 1;
- stbuf->st_size = node->size;
- stbuf->st_mtime = node->mtime;
- stbuf->st_atime = node->atime;
- stbuf->st_ctime = 0; /* unapplicable */
+ return add_checksum_bytes(sum, entry, sizeof(struct exfat_entry));
}
-#define SEC_IN_MIN 60ll
-#define SEC_IN_HOUR (60 * SEC_IN_MIN)
-#define SEC_IN_DAY (24 * SEC_IN_HOUR)
-#define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */
-/* Unix epoch started at 0:00:00 UTC 1 January 1970 */
-#define UNIX_EPOCH_YEAR 1970
-/* exFAT epoch started at 0:00:00 UTC 1 January 1980 */
-#define EXFAT_EPOCH_YEAR 1980
-/* number of years from Unix epoch to exFAT epoch */
-#define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR)
-/* number of seconds from Unix epoch to exFAT epoch (considering leap years) */
-#define EPOCH_DIFF_SEC ((EPOCH_DIFF_YEAR*365 + EPOCH_DIFF_YEAR/4) * SEC_IN_DAY)
-
-static const time_t days_in_year[] =
+le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
+ const struct exfat_entry_meta2* meta2, const le16_t* name)
{
- /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
- 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
-};
+ uint16_t checksum;
+ const int name_entries = DIV_ROUND_UP(meta2->name_length, EXFAT_ENAME_MAX);
+ int i;
-union exfat_date
-{
- uint16_t raw;
- struct
+ checksum = exfat_start_checksum(meta1);
+ checksum = exfat_add_checksum(meta2, checksum);
+ for (i = 0; i < name_entries; i++)
{
- uint16_t day : 5; /* 1-31 */
- uint16_t month : 4; /* 1-12 */
- uint16_t year : 7; /* 1-127 (+1980) */
- };
-};
+ checksum = add_checksum_byte(checksum, EXFAT_ENTRY_FILE_NAME);
+ checksum = add_checksum_byte(checksum, 0);
+ checksum = add_checksum_bytes(checksum, name + i * EXFAT_ENAME_MAX,
+ EXFAT_ENAME_MAX * sizeof(le16_t));
+ }
+ return cpu_to_le16(checksum);
+}
-union exfat_time
+uint32_t exfat_vbr_start_checksum(const void* sector, size_t size)
{
- uint16_t raw;
- struct
- {
- uint16_t twosec : 5; /* 0-29 (2 sec granularity) */
- uint16_t min : 6; /* 0-59 */
- uint16_t hour : 5; /* 0-23 */
- };
-};
+ size_t i;
+ uint32_t sum = 0;
+
+ for (i = 0; i < size; i++)
+ /* skip volume_state and allocated_percent fields */
+ if (i != 0x6a && i != 0x6b && i != 0x70)
+ sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
+ return sum;
+}
-time_t exfat_exfat2unix(le16_t date, le16_t time)
+uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
{
- union exfat_date edate;
- union exfat_time etime;
- time_t unix_time = EPOCH_DIFF_SEC;
+ size_t i;
- edate.raw = le16_to_cpu(date);
- etime.raw = le16_to_cpu(time);
+ for (i = 0; i < size; i++)
+ sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
+ return sum;
+}
- /*
- exfat_debug("%hu-%02hu-%02hu %hu:%02hu:%02hu",
- edate.year + 1980, edate.month, edate.day,
- etime.hour, etime.min, etime.twosec * 2);
- */
+le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name,
+ size_t length)
+{
+ size_t i;
+ uint16_t hash = 0;
- if (edate.day == 0 || edate.month == 0 || edate.month > 12)
+ for (i = 0; i < length; i++)
{
- exfat_error("bad date %hu-%02hu-%02hu",
- edate.year + EXFAT_EPOCH_YEAR, edate.month, edate.day);
- return 0;
+ uint16_t c = le16_to_cpu(name[i]);
+
+ /* convert to upper case */
+ c = ef->upcase[c];
+
+ hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
+ hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
}
- if (etime.hour > 23 || etime.min > 59 || etime.twosec > 29)
+ return cpu_to_le16(hash);
+}
+
+void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
+{
+ size_t i;
+ /* 16 EB (minus 1 byte) is the largest size that can be represented by
+ uint64_t */
+ const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"};
+ uint64_t divisor = 1;
+ uint64_t temp = 0;
+
+ for (i = 0; ; i++, divisor *= 1024)
{
- exfat_error("bad time %hu:%02hu:%02hu",
- etime.hour, etime.min, etime.twosec * 2);
- return 0;
+ temp = (value + divisor / 2) / divisor;
+
+ if (temp == 0)
+ break;
+ if (temp / 1024 * 1024 == temp)
+ continue;
+ if (temp < 10240)
+ break;
}
-
- /* every 4th year between 1904 and 2096 is leap */
- unix_time += edate.year * SEC_IN_YEAR
- + ((EXFAT_EPOCH_YEAR + edate.year - 1) / 4 - (EXFAT_EPOCH_YEAR - 1) / 4)
- * SEC_IN_DAY;
- unix_time += days_in_year[edate.month] * SEC_IN_DAY;
- /* if it's leap year and February has passed we should add 1 day */
- if ((EXFAT_EPOCH_YEAR + edate.year) % 4 == 0 && edate.month > 2)
- unix_time += SEC_IN_DAY;
- unix_time += (edate.day - 1) * SEC_IN_DAY;
-
- unix_time += etime.hour * SEC_IN_HOUR;
- unix_time += etime.min * SEC_IN_MIN;
- /* exFAT represents time with 2 sec granularity */
- unix_time += etime.twosec * 2;
-
- /* exFAT stores timestamps in local time, so we correct it to UTC */
- unix_time += timezone;
-
- return unix_time;
+ hb->value = temp;
+ hb->unit = units[i];
}
-void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
+void exfat_print_info(const struct exfat_super_block* sb,
+ uint32_t free_clusters)
{
- if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
- exfat_bug("failed to convert name to UTF-8");
+ struct exfat_human_bytes hb;
+ off_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb);
+ off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
+
+ printf("File system version %hhu.%hhu\n",
+ sb->version.major, sb->version.minor);
+ exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb);
+ printf("Sector size %10"PRIu64" %s\n", hb.value, hb.unit);
+ exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
+ printf("Cluster size %10"PRIu64" %s\n", hb.value, hb.unit);
+ exfat_humanize_bytes(total_space, &hb);
+ printf("Volume size %10"PRIu64" %s\n", hb.value, hb.unit);
+ exfat_humanize_bytes(total_space - avail_space, &hb);
+ printf("Used space %10"PRIu64" %s\n", hb.value, hb.unit);
+ exfat_humanize_bytes(avail_space, &hb);
+ printf("Available space %10"PRIu64" %s\n", hb.value, hb.unit);
}