bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | main.c (15.08.10) |
| 3 | Creates exFAT file system. |
| 4 | |
bigbiff bigbiff | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 5 | Copyright (C) 2011-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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/>. |
| 19 | */ |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/time.h> |
| 23 | #include <unistd.h> |
| 24 | #include <inttypes.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <limits.h> |
| 28 | #include <exfat.h> |
| 29 | #include "mkexfat.h" |
| 30 | #include "vbr.h" |
| 31 | #include "fat.h" |
| 32 | #include "cbm.h" |
| 33 | #include "uct.h" |
| 34 | #include "rootdir.h" |
| 35 | |
| 36 | const struct fs_object* objects[] = |
| 37 | { |
| 38 | &vbr, |
| 39 | &vbr, |
| 40 | &fat, |
| 41 | /* clusters heap */ |
| 42 | &cbm, |
| 43 | &uct, |
| 44 | &rootdir, |
| 45 | NULL, |
| 46 | }; |
| 47 | |
| 48 | static struct |
| 49 | { |
| 50 | int sector_bits; |
| 51 | int spc_bits; |
| 52 | off64_t volume_size; |
| 53 | le16_t volume_label[EXFAT_ENAME_MAX + 1]; |
| 54 | uint32_t volume_serial; |
| 55 | uint64_t first_sector; |
| 56 | } |
| 57 | param; |
| 58 | |
| 59 | int get_sector_bits(void) |
| 60 | { |
| 61 | return param.sector_bits; |
| 62 | } |
| 63 | |
| 64 | int get_spc_bits(void) |
| 65 | { |
| 66 | return param.spc_bits; |
| 67 | } |
| 68 | |
| 69 | off64_t get_volume_size(void) |
| 70 | { |
| 71 | return param.volume_size; |
| 72 | } |
| 73 | |
| 74 | const le16_t* get_volume_label(void) |
| 75 | { |
| 76 | return param.volume_label; |
| 77 | } |
| 78 | |
| 79 | uint32_t get_volume_serial(void) |
| 80 | { |
| 81 | return param.volume_serial; |
| 82 | } |
| 83 | |
| 84 | uint64_t get_first_sector(void) |
| 85 | { |
| 86 | return param.first_sector; |
| 87 | } |
| 88 | |
| 89 | int get_sector_size(void) |
| 90 | { |
| 91 | return 1 << get_sector_bits(); |
| 92 | } |
| 93 | |
| 94 | int get_cluster_size(void) |
| 95 | { |
| 96 | return get_sector_size() << get_spc_bits(); |
| 97 | } |
| 98 | |
| 99 | static int setup_spc_bits(int sector_bits, int user_defined, off64_t volume_size) |
| 100 | { |
| 101 | int i; |
| 102 | |
| 103 | if (user_defined != -1) |
| 104 | { |
| 105 | off64_t cluster_size = 1 << sector_bits << user_defined; |
| 106 | if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER) |
| 107 | { |
| 108 | struct exfat_human_bytes chb, vhb; |
| 109 | |
| 110 | exfat_humanize_bytes(cluster_size, &chb); |
| 111 | exfat_humanize_bytes(volume_size, &vhb); |
| 112 | exfat_error("cluster size %"PRIu64" %s is too small for " |
| 113 | "%"PRIu64" %s volume, try -s %d", |
| 114 | chb.value, chb.unit, |
| 115 | vhb.value, vhb.unit, |
| 116 | 1 << setup_spc_bits(sector_bits, -1, volume_size)); |
| 117 | return -1; |
| 118 | } |
| 119 | return user_defined; |
| 120 | } |
| 121 | |
| 122 | if (volume_size < 256ull * 1024 * 1024) |
| 123 | return MAX(0, 12 - sector_bits); /* 4 KB */ |
| 124 | if (volume_size < 32ull * 1024 * 1024 * 1024) |
| 125 | return MAX(0, 15 - sector_bits); /* 32 KB */ |
| 126 | |
| 127 | for (i = 17; ; i++) /* 128 KB or more */ |
| 128 | if (DIV_ROUND_UP(volume_size, 1 << i) <= EXFAT_LAST_DATA_CLUSTER) |
| 129 | return MAX(0, i - sector_bits); |
| 130 | } |
| 131 | |
| 132 | static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s) |
| 133 | { |
| 134 | memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t)); |
| 135 | if (s == NULL) |
| 136 | return 0; |
| 137 | return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s)); |
| 138 | } |
| 139 | |
| 140 | static uint32_t setup_volume_serial(uint32_t user_defined) |
| 141 | { |
| 142 | struct timeval now; |
| 143 | |
| 144 | if (user_defined != 0) |
| 145 | return user_defined; |
| 146 | |
| 147 | if (gettimeofday(&now, NULL) != 0) |
| 148 | { |
| 149 | exfat_error("failed to form volume id"); |
| 150 | return 0; |
| 151 | } |
| 152 | return (now.tv_sec << 20) | now.tv_usec; |
| 153 | } |
| 154 | |
| 155 | static int setup(struct exfat_dev* dev, int sector_bits, int spc_bits, |
| 156 | const char* volume_label, uint32_t volume_serial, |
| 157 | uint64_t first_sector) |
| 158 | { |
| 159 | param.sector_bits = sector_bits; |
| 160 | param.first_sector = first_sector; |
| 161 | param.volume_size = exfat_get_size(dev); |
| 162 | |
| 163 | param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size); |
| 164 | if (param.spc_bits == -1) |
| 165 | return 1; |
| 166 | |
| 167 | if (setup_volume_label(param.volume_label, volume_label) != 0) |
| 168 | return 1; |
| 169 | |
| 170 | param.volume_serial = setup_volume_serial(volume_serial); |
| 171 | if (param.volume_serial == 0) |
| 172 | return 1; |
| 173 | |
| 174 | return mkfs(dev, param.volume_size); |
| 175 | } |
| 176 | |
| 177 | static int logarithm2(int n) |
| 178 | { |
| 179 | int i; |
| 180 | |
| 181 | for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++) |
| 182 | if ((1 << i) == n) |
| 183 | return i; |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | static void usage(const char* prog) |
| 188 | { |
| 189 | fprintf(stderr, "Usage: %s [-i volume-id] [-n label] " |
| 190 | "[-p partition-first-sector] " |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 191 | "[-s sectors-per-cluster] [-V] <device>\n", prog); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 192 | exit(1); |
| 193 | } |
| 194 | |
| 195 | int main(int argc, char* argv[]) |
| 196 | { |
| 197 | const char* spec = NULL; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 198 | int opt; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 199 | int spc_bits = -1; |
| 200 | const char* volume_label = NULL; |
| 201 | uint32_t volume_serial = 0; |
| 202 | uint64_t first_sector = 0; |
| 203 | struct exfat_dev* dev; |
| 204 | |
| 205 | printf("mkexfatfs %u.%u.%u\n", |
| 206 | EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); |
| 207 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 208 | while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 209 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 210 | switch (opt) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 211 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 212 | case 'i': |
| 213 | volume_serial = strtol(optarg, NULL, 16); |
| 214 | break; |
| 215 | case 'n': |
| 216 | volume_label = optarg; |
| 217 | break; |
| 218 | case 'p': |
| 219 | first_sector = strtoll(optarg, NULL, 10); |
| 220 | break; |
| 221 | case 's': |
| 222 | spc_bits = logarithm2(atoi(optarg)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 223 | if (spc_bits < 0) |
| 224 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 225 | exfat_error("invalid option value: `%s'", optarg); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 226 | return 1; |
| 227 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 228 | break; |
| 229 | case 'V': |
bigbiff bigbiff | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 230 | puts("Copyright (C) 2011-2013 Andrew Nayenko"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 231 | return 0; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 232 | default: |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 233 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 234 | break; |
| 235 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 236 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 237 | if (argc - optind != 1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 238 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 239 | spec = argv[optind]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 240 | |
| 241 | dev = exfat_open(spec, EXFAT_MODE_RW); |
| 242 | if (dev == NULL) |
| 243 | return 1; |
| 244 | if (setup(dev, 9, spc_bits, volume_label, volume_serial, |
| 245 | first_sector) != 0) |
| 246 | { |
| 247 | exfat_close(dev); |
| 248 | return 1; |
| 249 | } |
| 250 | if (exfat_close(dev) != 0) |
| 251 | return 1; |
| 252 | printf("File system created successfully.\n"); |
| 253 | return 0; |
| 254 | } |