blob: 0929062be83d45da8ee8d61e3b4c799146346e84 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 mkexfat.c (22.04.12)
3 FS creation engine.
4
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04005 Free exFAT implementation.
Matt Mower09ef1e42015-12-13 11:29:45 -06006 Copyright (C) 2011-2015 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05007
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04008 This program is free software; you can redistribute it and/or modify
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009 it under the terms of the GNU General Public License as published by
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040010 the Free Software Foundation, either version 2 of the License, or
bigbiff bigbiff9c754052013-01-09 09:09:08 -050011 (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 bigbiff61cdc022013-08-08 08:35:06 -040018 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 bigbiff9c754052013-01-09 09:09:08 -050021*/
22
Matt Mower09ef1e42015-12-13 11:29:45 -060023#include "mkexfat.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050024#include <sys/types.h>
25#include <unistd.h>
26#include <inttypes.h>
27#include <stdio.h>
28#include <string.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029
Spegeliusd69ac2b2014-11-23 15:15:06 +020030static int check_size(loff_t volume_size)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031{
32 const struct fs_object** pp;
Spegeliusd69ac2b2014-11-23 15:15:06 +020033 loff_t position = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050034
35 for (pp = objects; *pp; pp++)
36 {
37 position = ROUND_UP(position, (*pp)->get_alignment());
38 position += (*pp)->get_size();
39 }
40
41 if (position > volume_size)
42 {
43 struct exfat_human_bytes vhb;
44
45 exfat_humanize_bytes(volume_size, &vhb);
46 exfat_error("too small device (%"PRIu64" %s)", vhb.value, vhb.unit);
47 return 1;
48 }
49
50 return 0;
51
52}
53
54static int erase_object(struct exfat_dev* dev, const void* block,
Spegeliusd69ac2b2014-11-23 15:15:06 +020055 size_t block_size, loff_t start, loff_t size)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050056{
Spegeliusd69ac2b2014-11-23 15:15:06 +020057 const loff_t block_count = DIV_ROUND_UP(size, block_size);
58 loff_t i;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050059
Spegeliusd69ac2b2014-11-23 15:15:06 +020060 if (exfat_seek(dev, start, SEEK_SET) == (loff_t) -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050061 {
62 exfat_error("seek to 0x%"PRIx64" failed", start);
63 return 1;
64 }
65 for (i = 0; i < size; i += block_size)
66 {
67 if (exfat_write(dev, block, MIN(size - i, block_size)) < 0)
68 {
69 exfat_error("failed to erase block %"PRIu64"/%"PRIu64
70 " at 0x%"PRIx64, i + 1, block_count, start);
71 return 1;
72 }
73 }
74 return 0;
75}
76
77static int erase(struct exfat_dev* dev)
78{
79 const struct fs_object** pp;
Spegeliusd69ac2b2014-11-23 15:15:06 +020080 loff_t position = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050081 const size_t block_size = 1024 * 1024;
82 void* block = malloc(block_size);
83
84 if (block == NULL)
85 {
86 exfat_error("failed to allocate erase block of %zu bytes", block_size);
87 return 1;
88 }
89 memset(block, 0, block_size);
90
91 for (pp = objects; *pp; pp++)
92 {
93 position = ROUND_UP(position, (*pp)->get_alignment());
94 if (erase_object(dev, block, block_size, position,
95 (*pp)->get_size()) != 0)
96 {
97 free(block);
98 return 1;
99 }
100 position += (*pp)->get_size();
101 }
102
103 free(block);
104 return 0;
105}
106
107static int create(struct exfat_dev* dev)
108{
109 const struct fs_object** pp;
Spegeliusd69ac2b2014-11-23 15:15:06 +0200110 loff_t position = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500111
112 for (pp = objects; *pp; pp++)
113 {
114 position = ROUND_UP(position, (*pp)->get_alignment());
Spegeliusd69ac2b2014-11-23 15:15:06 +0200115 if (exfat_seek(dev, position, SEEK_SET) == (loff_t) -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500116 {
117 exfat_error("seek to 0x%"PRIx64" failed", position);
118 return 1;
119 }
120 if ((*pp)->write(dev) != 0)
121 return 1;
122 position += (*pp)->get_size();
123 }
124 return 0;
125}
126
Spegeliusd69ac2b2014-11-23 15:15:06 +0200127int mkfs(struct exfat_dev* dev, loff_t volume_size)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500128{
129 if (check_size(volume_size) != 0)
130 return 1;
131
132 fputs("Creating... ", stdout);
133 fflush(stdout);
134 if (erase(dev) != 0)
135 return 1;
136 if (create(dev) != 0)
137 return 1;
138 puts("done.");
139
140 fputs("Flushing... ", stdout);
141 fflush(stdout);
142 if (exfat_fsync(dev) != 0)
143 return 1;
144 puts("done.");
145
146 return 0;
147}
148
Spegeliusd69ac2b2014-11-23 15:15:06 +0200149loff_t get_position(const struct fs_object* object)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500150{
151 const struct fs_object** pp;
Spegeliusd69ac2b2014-11-23 15:15:06 +0200152 loff_t position = 0;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500153
154 for (pp = objects; *pp; pp++)
155 {
156 position = ROUND_UP(position, (*pp)->get_alignment());
157 if (*pp == object)
158 return position;
159 position += (*pp)->get_size();
160 }
161 exfat_bug("unknown object");
162}