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