main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. main.c (15.08.10)
  3. Creates exFAT file system.
  4. Copyright (C) 2011-2013 Andrew Nayenko
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <sys/types.h>
  17. #include <sys/time.h>
  18. #include <unistd.h>
  19. #include <inttypes.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <limits.h>
  23. #include <exfat.h>
  24. #include "mkexfat.h"
  25. #include "vbr.h"
  26. #include "fat.h"
  27. #include "cbm.h"
  28. #include "uct.h"
  29. #include "rootdir.h"
  30. const struct fs_object* objects[] =
  31. {
  32. &vbr,
  33. &vbr,
  34. &fat,
  35. /* clusters heap */
  36. &cbm,
  37. &uct,
  38. &rootdir,
  39. NULL,
  40. };
  41. static struct
  42. {
  43. int sector_bits;
  44. int spc_bits;
  45. off_t volume_size;
  46. le16_t volume_label[EXFAT_ENAME_MAX + 1];
  47. uint32_t volume_serial;
  48. uint64_t first_sector;
  49. }
  50. param;
  51. int get_sector_bits(void)
  52. {
  53. return param.sector_bits;
  54. }
  55. int get_spc_bits(void)
  56. {
  57. return param.spc_bits;
  58. }
  59. off_t get_volume_size(void)
  60. {
  61. return param.volume_size;
  62. }
  63. const le16_t* get_volume_label(void)
  64. {
  65. return param.volume_label;
  66. }
  67. uint32_t get_volume_serial(void)
  68. {
  69. return param.volume_serial;
  70. }
  71. uint64_t get_first_sector(void)
  72. {
  73. return param.first_sector;
  74. }
  75. int get_sector_size(void)
  76. {
  77. return 1 << get_sector_bits();
  78. }
  79. int get_cluster_size(void)
  80. {
  81. return get_sector_size() << get_spc_bits();
  82. }
  83. static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
  84. {
  85. int i;
  86. if (user_defined != -1)
  87. {
  88. off_t cluster_size = 1 << sector_bits << user_defined;
  89. if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER)
  90. {
  91. struct exfat_human_bytes chb, vhb;
  92. exfat_humanize_bytes(cluster_size, &chb);
  93. exfat_humanize_bytes(volume_size, &vhb);
  94. exfat_error("cluster size %"PRIu64" %s is too small for "
  95. "%"PRIu64" %s volume, try -s %d",
  96. chb.value, chb.unit,
  97. vhb.value, vhb.unit,
  98. 1 << setup_spc_bits(sector_bits, -1, volume_size));
  99. return -1;
  100. }
  101. return user_defined;
  102. }
  103. if (volume_size < 256ull * 1024 * 1024)
  104. return MAX(0, 12 - sector_bits); /* 4 KB */
  105. if (volume_size < 32ull * 1024 * 1024 * 1024)
  106. return MAX(0, 15 - sector_bits); /* 32 KB */
  107. for (i = 17; ; i++) /* 128 KB or more */
  108. if (DIV_ROUND_UP(volume_size, 1 << i) <= EXFAT_LAST_DATA_CLUSTER)
  109. return MAX(0, i - sector_bits);
  110. }
  111. static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
  112. {
  113. memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
  114. if (s == NULL)
  115. return 0;
  116. return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
  117. }
  118. static uint32_t setup_volume_serial(uint32_t user_defined)
  119. {
  120. struct timeval now;
  121. if (user_defined != 0)
  122. return user_defined;
  123. if (gettimeofday(&now, NULL) != 0)
  124. {
  125. exfat_error("failed to form volume id");
  126. return 0;
  127. }
  128. return (now.tv_sec << 20) | now.tv_usec;
  129. }
  130. static int setup(struct exfat_dev* dev, int sector_bits, int spc_bits,
  131. const char* volume_label, uint32_t volume_serial,
  132. uint64_t first_sector)
  133. {
  134. param.sector_bits = sector_bits;
  135. param.first_sector = first_sector;
  136. param.volume_size = exfat_get_size(dev);
  137. param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
  138. if (param.spc_bits == -1)
  139. return 1;
  140. if (setup_volume_label(param.volume_label, volume_label) != 0)
  141. return 1;
  142. param.volume_serial = setup_volume_serial(volume_serial);
  143. if (param.volume_serial == 0)
  144. return 1;
  145. return mkfs(dev, param.volume_size);
  146. }
  147. static int logarithm2(int n)
  148. {
  149. int i;
  150. for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++)
  151. if ((1 << i) == n)
  152. return i;
  153. return -1;
  154. }
  155. static void usage(const char* prog)
  156. {
  157. fprintf(stderr, "Usage: %s [-i volume-id] [-n label] "
  158. "[-p partition-first-sector] "
  159. "[-s sectors-per-cluster] [-v] <device>\n", prog);
  160. exit(1);
  161. }
  162. int main(int argc, char* argv[])
  163. {
  164. const char* spec = NULL;
  165. char** pp;
  166. int spc_bits = -1;
  167. const char* volume_label = NULL;
  168. uint32_t volume_serial = 0;
  169. uint64_t first_sector = 0;
  170. struct exfat_dev* dev;
  171. printf("mkexfatfs %u.%u.%u\n",
  172. EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
  173. for (pp = argv + 1; *pp; pp++)
  174. {
  175. if (strcmp(*pp, "-s") == 0)
  176. {
  177. pp++;
  178. if (*pp == NULL)
  179. usage(argv[0]);
  180. spc_bits = logarithm2(atoi(*pp));
  181. if (spc_bits < 0)
  182. {
  183. exfat_error("invalid option value: `%s'", *pp);
  184. return 1;
  185. }
  186. }
  187. else if (strcmp(*pp, "-n") == 0)
  188. {
  189. pp++;
  190. if (*pp == NULL)
  191. usage(argv[0]);
  192. volume_label = *pp;
  193. }
  194. else if (strcmp(*pp, "-i") == 0)
  195. {
  196. pp++;
  197. if (*pp == NULL)
  198. usage(argv[0]);
  199. volume_serial = strtol(*pp, NULL, 16);
  200. }
  201. else if (strcmp(*pp, "-p") == 0)
  202. {
  203. pp++;
  204. if (*pp == NULL)
  205. usage(argv[0]);
  206. first_sector = strtoll(*pp, NULL, 10);
  207. }
  208. else if (strcmp(*pp, "-v") == 0)
  209. {
  210. puts("Copyright (C) 2011-2013 Andrew Nayenko");
  211. return 0;
  212. }
  213. else if (spec == NULL)
  214. spec = *pp;
  215. else
  216. usage(argv[0]);
  217. }
  218. if (spec == NULL)
  219. usage(argv[0]);
  220. dev = exfat_open(spec, EXFAT_MODE_RW);
  221. if (dev == NULL)
  222. return 1;
  223. if (setup(dev, 9, spc_bits, volume_label, volume_serial,
  224. first_sector) != 0)
  225. {
  226. exfat_close(dev);
  227. return 1;
  228. }
  229. if (exfat_close(dev) != 0)
  230. return 1;
  231. printf("File system created successfully.\n");
  232. return 0;
  233. }