blob: 1f5b7110b419f04456c1b9fd88f6a4109ff78393 [file] [log] [blame]
relan542774e2009-09-14 18:44:13 +00001/*
relana43ba0c2010-01-10 19:53:49 +00002 utils.c (04.09.09)
3 exFAT file system implementation library.
4
relan48573ff2013-07-08 07:30:45 +00005 Free exFAT implementation.
reland2a4dd42016-06-03 08:20:53 +03006 Copyright (C) 2010-2016 Andrew Nayenko
relana43ba0c2010-01-10 19:53:49 +00007
relan48573ff2013-07-08 07:30:45 +00008 This program is free software; you can redistribute it and/or modify
relana43ba0c2010-01-10 19:53:49 +00009 it under the terms of the GNU General Public License as published by
relan48573ff2013-07-08 07:30:45 +000010 the Free Software Foundation, either version 2 of the License, or
relana43ba0c2010-01-10 19:53:49 +000011 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
relan48573ff2013-07-08 07:30:45 +000018 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
relana43ba0c2010-01-10 19:53:49 +000021*/
relan542774e2009-09-14 18:44:13 +000022
23#include "exfat.h"
Steve Kondik4a8592e2016-01-21 14:46:00 -080024#include "utf.h"
relan542774e2009-09-14 18:44:13 +000025#include <string.h>
relan541325a2010-11-09 19:02:51 +000026#include <stdio.h>
27#include <inttypes.h>
relan542774e2009-09-14 18:44:13 +000028
relan364082e2009-12-20 15:31:29 +000029void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
30 struct stat* stbuf)
relan542774e2009-09-14 18:44:13 +000031{
32 memset(stbuf, 0, sizeof(struct stat));
33 if (node->flags & EXFAT_ATTRIB_DIR)
relan360c2082009-12-20 15:44:42 +000034 stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
relan542774e2009-09-14 18:44:13 +000035 else
relan360c2082009-12-20 15:44:42 +000036 stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
relan542774e2009-09-14 18:44:13 +000037 stbuf->st_nlink = 1;
relan7d0edc52010-01-08 08:50:08 +000038 stbuf->st_uid = ef->uid;
39 stbuf->st_gid = ef->gid;
relan542774e2009-09-14 18:44:13 +000040 stbuf->st_size = node->size;
relan33668b22009-12-20 16:44:47 +000041 stbuf->st_blocks = DIV_ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) *
42 CLUSTER_SIZE(*ef->sb) / 512;
relan542774e2009-09-14 18:44:13 +000043 stbuf->st_mtime = node->mtime;
44 stbuf->st_atime = node->atime;
relancb710372012-01-02 19:36:29 +000045 /* set ctime to mtime to ensure we don't break programs that rely on ctime
46 (e.g. rsync) */
47 stbuf->st_ctime = node->mtime;
relan542774e2009-09-14 18:44:13 +000048}
49
relan542774e2009-09-14 18:44:13 +000050void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
51{
52 if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
53 exfat_bug("failed to convert name to UTF-8");
54}
relan38193f32009-10-31 08:36:23 +000055
relan94bc6dc2010-01-14 19:48:42 +000056uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
relan38193f32009-10-31 08:36:23 +000057{
58 uint16_t sum = 0;
59 int i;
60
61 for (i = 0; i < sizeof(struct exfat_entry); i++)
62 if (i != 2 && i != 3) /* skip checksum field itself */
63 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
64 return sum;
65}
66
67uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
68{
69 int i;
70
71 for (i = 0; i < sizeof(struct exfat_entry); i++)
72 sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
73 return sum;
74}
relan870974f2009-12-13 10:23:42 +000075
relan94bc6dc2010-01-14 19:48:42 +000076le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
77 const struct exfat_entry_meta2* meta2, const le16_t* name)
relan870974f2009-12-13 10:23:42 +000078{
79 uint16_t checksum;
80 const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
81 int i;
82
83 checksum = exfat_start_checksum(meta1);
84 checksum = exfat_add_checksum(meta2, checksum);
85 for (i = 0; i < name_entries; i++)
86 {
Steve Kondik49c64de2016-08-28 00:27:19 -070087 struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0, {} };
relan870974f2009-12-13 10:23:42 +000088 memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
relanbd57f2a2013-08-03 08:36:54 +000089 MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
90 sizeof(le16_t));
relan870974f2009-12-13 10:23:42 +000091 checksum = exfat_add_checksum(&name_entry, checksum);
92 }
93 return cpu_to_le16(checksum);
94}
relanf51b8212009-12-20 09:54:17 +000095
relana39bcd22011-02-23 08:38:53 +000096uint32_t exfat_vbr_start_checksum(const void* sector, size_t size)
relan65f04962010-12-29 20:51:15 +000097{
98 size_t i;
99 uint32_t sum = 0;
100
101 for (i = 0; i < size; i++)
102 /* skip volume_state and allocated_percent fields */
103 if (i != 0x6a && i != 0x6b && i != 0x70)
relana39bcd22011-02-23 08:38:53 +0000104 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
relan65f04962010-12-29 20:51:15 +0000105 return sum;
106}
107
relana39bcd22011-02-23 08:38:53 +0000108uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
relan65f04962010-12-29 20:51:15 +0000109{
110 size_t i;
111
112 for (i = 0; i < size; i++)
relana39bcd22011-02-23 08:38:53 +0000113 sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i];
relan65f04962010-12-29 20:51:15 +0000114 return sum;
115}
116
relanf51b8212009-12-20 09:54:17 +0000117le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
118{
119 size_t i;
120 size_t length = utf16_length(name);
121 uint16_t hash = 0;
122
123 for (i = 0; i < length; i++)
124 {
125 uint16_t c = le16_to_cpu(name[i]);
126
127 /* convert to upper case */
relanaab123c2016-03-25 13:06:25 +0300128 c = ef->upcase[c];
relanf51b8212009-12-20 09:54:17 +0000129
130 hash = ((hash << 15) | (hash >> 1)) + (c & 0xff);
131 hash = ((hash << 15) | (hash >> 1)) + (c >> 8);
132 }
133 return cpu_to_le16(hash);
134}
relan847a22c2010-11-09 18:13:41 +0000135
136void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
137{
138 size_t i;
relan9b149f82012-03-18 13:02:49 +0000139 /* 16 EB (minus 1 byte) is the largest size that can be represented by
140 uint64_t */
141 const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"};
relan847a22c2010-11-09 18:13:41 +0000142 uint64_t divisor = 1;
relanf42fef32012-01-08 07:58:03 +0000143 uint64_t temp = 0;
relan847a22c2010-11-09 18:13:41 +0000144
relan9b149f82012-03-18 13:02:49 +0000145 for (i = 0; ; i++, divisor *= 1024)
relan847a22c2010-11-09 18:13:41 +0000146 {
relanf42fef32012-01-08 07:58:03 +0000147 temp = (value + divisor / 2) / divisor;
148
149 if (temp == 0)
relan847a22c2010-11-09 18:13:41 +0000150 break;
relanf42fef32012-01-08 07:58:03 +0000151 if (temp / 1024 * 1024 == temp)
152 continue;
153 if (temp < 10240)
154 break;
relan847a22c2010-11-09 18:13:41 +0000155 }
relanf42fef32012-01-08 07:58:03 +0000156 hb->value = temp;
relan847a22c2010-11-09 18:13:41 +0000157 hb->unit = units[i];
158}
relan541325a2010-11-09 19:02:51 +0000159
160void exfat_print_info(const struct exfat_super_block* sb,
161 uint32_t free_clusters)
162{
163 struct exfat_human_bytes hb;
relana39bcd22011-02-23 08:38:53 +0000164 off_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb);
relan541325a2010-11-09 19:02:51 +0000165 off_t avail_space = (off_t) free_clusters * CLUSTER_SIZE(*sb);
166
167 printf("File system version %hhu.%hhu\n",
168 sb->version.major, sb->version.minor);
relana39bcd22011-02-23 08:38:53 +0000169 exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb);
relanac418ad2011-01-24 18:13:41 +0000170 printf("Sector size %10"PRIu64" %s\n", hb.value, hb.unit);
relan541325a2010-11-09 19:02:51 +0000171 exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb);
relan8d984a62011-01-22 18:55:56 +0000172 printf("Cluster size %10"PRIu64" %s\n", hb.value, hb.unit);
relan541325a2010-11-09 19:02:51 +0000173 exfat_humanize_bytes(total_space, &hb);
relan8d984a62011-01-22 18:55:56 +0000174 printf("Volume size %10"PRIu64" %s\n", hb.value, hb.unit);
relan541325a2010-11-09 19:02:51 +0000175 exfat_humanize_bytes(total_space - avail_space, &hb);
relan8d984a62011-01-22 18:55:56 +0000176 printf("Used space %10"PRIu64" %s\n", hb.value, hb.unit);
relan541325a2010-11-09 19:02:51 +0000177 exfat_humanize_bytes(avail_space, &hb);
relan8d984a62011-01-22 18:55:56 +0000178 printf("Available space %10"PRIu64" %s\n", hb.value, hb.unit);
relan541325a2010-11-09 19:02:51 +0000179}