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. |
bigbiff bigbiff | 2e33c5e | 2014-09-04 20:58:41 -0400 | [diff] [blame] | 6 | Copyright (C) 2011-2014 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 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <unistd.h> |
| 26 | #include <inttypes.h> |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <limits.h> |
| 30 | #include <exfat.h> |
| 31 | #include "mkexfat.h" |
| 32 | #include "vbr.h" |
| 33 | #include "fat.h" |
| 34 | #include "cbm.h" |
| 35 | #include "uct.h" |
| 36 | #include "rootdir.h" |
| 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; |
| 54 | off64_t volume_size; |
| 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 | |
| 71 | off64_t get_volume_size(void) |
| 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 | |
| 101 | static int setup_spc_bits(int sector_bits, int user_defined, off64_t volume_size) |
| 102 | { |
| 103 | int i; |
| 104 | |
| 105 | if (user_defined != -1) |
| 106 | { |
| 107 | off64_t cluster_size = 1 << sector_bits << user_defined; |
| 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 | |
| 207 | printf("mkexfatfs %u.%u.%u\n", |
| 208 | EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); |
| 209 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 210 | while ((opt = getopt(argc, argv, "i:n:p:s:V")) != -1) |
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 | switch (opt) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 213 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 214 | case 'i': |
| 215 | volume_serial = strtol(optarg, NULL, 16); |
| 216 | break; |
| 217 | case 'n': |
| 218 | volume_label = optarg; |
| 219 | break; |
| 220 | case 'p': |
| 221 | first_sector = strtoll(optarg, NULL, 10); |
| 222 | break; |
| 223 | case 's': |
| 224 | spc_bits = logarithm2(atoi(optarg)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 225 | if (spc_bits < 0) |
| 226 | { |
bigbiff bigbiff | 2e33c5e | 2014-09-04 20:58:41 -0400 | [diff] [blame] | 227 | exfat_error("invalid option value: '%s'", optarg); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 228 | return 1; |
| 229 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 230 | break; |
| 231 | case 'V': |
bigbiff bigbiff | 2e33c5e | 2014-09-04 20:58:41 -0400 | [diff] [blame] | 232 | puts("Copyright (C) 2011-2014 Andrew Nayenko"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 233 | return 0; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 234 | default: |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 235 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 236 | break; |
| 237 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 238 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 239 | if (argc - optind != 1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 240 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 241 | spec = argv[optind]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 242 | |
| 243 | dev = exfat_open(spec, EXFAT_MODE_RW); |
| 244 | if (dev == NULL) |
| 245 | return 1; |
| 246 | if (setup(dev, 9, spc_bits, volume_label, volume_serial, |
| 247 | first_sector) != 0) |
| 248 | { |
| 249 | exfat_close(dev); |
| 250 | return 1; |
| 251 | } |
| 252 | if (exfat_close(dev) != 0) |
| 253 | return 1; |
| 254 | printf("File system created successfully.\n"); |
| 255 | return 0; |
| 256 | } |