summaryrefslogtreecommitdiff
path: root/nuttx/fs/fat/fs_mkfatfs.c
blob: f5df0b65368af2107243ce9cb062a8d07539e5f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/****************************************************************************
 * fs/fat/fs_writefat.c
 *
 *   Copyright (C) 2008-2009, 2013 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <debug.h>
#include <errno.h>

#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/fat.h>
#include <nuttx/fs/mkfatfs.h>

#include "fs_internal.h"
#include "fs_fat32.h"
#include "fs_mkfatfs.h"

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: mkfatfs_getgeometry
 *
 * Description:
 *   Get the sector size and number of sectors of the underlying block
 *   device.
 *
 * Input:
 *   fmt - Caller specified format parameters
 *   var - Other format parameters that are not caller specifiable. (Most
 *     set by mkfatfs_configfatfs()).
 *
 * Return:
 *   Zero on success; negated errno on failure
 *
 ****************************************************************************/

static inline int mkfatfs_getgeometry(FAR struct fat_format_s *fmt,
                                      FAR struct fat_var_s *var)
{
  struct geometry geometry;
  int ret;
 
  /* Get the device geometry */

  ret = DEV_GEOMETRY(geometry);
  if (ret < 0)
    {
      fdbg("geometry() returned %d\n", ret);
      return ret;
    }

  if (!geometry.geo_available || !geometry.geo_writeenabled)
    {
      fdbg("Media is not available\n", ret);
      return -ENODEV;
    }

  /* Check if the user provided maxblocks was provided and, if so, that is it less than
   * the actual number of blocks on the device.
   */

  if (fmt->ff_nsectors != 0)
    {
      if (fmt->ff_nsectors > geometry.geo_nsectors)
        {
          fdbg("User maxblocks (%d) exceeds blocks on device (%d)\n",
               fmt->ff_nsectors, geometry.geo_nsectors);
          return -EINVAL;
        }
    }
  else
    {
      /* Use the actual number of blocks on the device */

      fmt->ff_nsectors = geometry.geo_nsectors;
    }

  /* Verify that we can handle this sector size */

  var->fv_sectorsize = geometry.geo_sectorsize;
  switch (var->fv_sectorsize)
    {
      case 512:
        var->fv_sectshift = 9;
        break;

      case 1024:
        var->fv_sectshift = 10;
        break;

      case 2048:
        var->fv_sectshift = 11;
        break;

      case 4096:
        var->fv_sectshift = 12;
        break;

      default:
        fdbg("Unsupported sector size: %d\n", var->fv_sectorsize);
        return -EPERM;
    }
  return 0;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: mkfatfs
 *
 * Description:
 *   Make a FAT file system image on the specified block device
 *
 * Inputs:
 *   pathname - the full path to a registered block driver
 *   fmt - Describes characteristics of the desired filesystem
 *
 * Return:
 *   Zero (OK) on success; -1 (ERROR) on failure with errno set appropriately:
 *
 *   EINVAL  - NULL block driver string, bad number of FATS in 'fmt', bad FAT
 *     size in 'fmt', bad cluster size in 'fmt'
 *   ENOENT  - 'pathname' does not refer to anything in the filesystem.
 *   ENOTBLK - 'pathname' does not refer to a block driver
 *   EACCES  - block driver does not support wrie or geometry methods
 *
 * Assumptions:
 *   - The caller must assure that the block driver is not mounted and not in
 *     use when this function is called.  The result of formatting a mounted
 *     device is indeterminate (but likely not good).
 *
 ****************************************************************************/
int mkfatfs(FAR const char *pathname, FAR struct fat_format_s *fmt)
{
  struct fat_var_s var;
  int ret;

  /* Initialize */

  memset(&var, 0, sizeof(struct fat_var_s));

  /* Get the filesystem creation time */

  var.fv_createtime = fat_systime2fattime();

  /* Verify format options (only when DEBUG enabled) */

#ifdef CONFIG_DEBUG
  if (!pathname)
    {
      fdbg("No block driver path\n");
      ret = -EINVAL;
      goto errout;
    }

  if (fmt->ff_nfats < 1 || fmt->ff_nfats > 4)
    {
      fdbg("Invalid number of fats: %d\n", fmt->ff_nfats);
      ret = -EINVAL;
      goto errout;
    }

  if (fmt->ff_fattype != 0  && fmt->ff_fattype != 12 &&
      fmt->ff_fattype != 16 && fmt->ff_fattype != 32)
    {
      fdbg("Invalid FAT size: %d\n", fmt->ff_fattype);
      ret = -EINVAL;
      goto errout;
    }
#endif
  var.fv_fattype = fmt->ff_fattype;

  /* The valid range off ff_clustshift is {0,1,..7} corresponding to
   * cluster sizes of {1,2,..128} sectors.  The special value of 0xff
   * means that we should autoselect the cluster sizel.
   */
#ifdef CONFIG_DEBUG
  if (fmt->ff_clustshift > 7 && fmt->ff_clustshift != 0xff)
    {
      fdbg("Invalid cluster shift value: %d\n", fmt->ff_clustshift);
      ret = -EINVAL;
      goto errout;
    }

   if (fmt->ff_rootdirentries != 0 && (fmt->ff_rootdirentries < 16 || fmt->ff_rootdirentries > 32767))
    {
      fdbg("Invalid number of root dir entries: %d\n", fmt->ff_rootdirentries);
      ret = -EINVAL;
      goto errout;
    }

   if (fmt->ff_rsvdseccount != 0 && (fmt->ff_rsvdseccount < 1 || fmt->ff_rsvdseccount > 32767))
    {
      fdbg("Invalid number of reserved sectors: %d\n", fmt->ff_rsvdseccount);
      ret = -EINVAL;
      goto errout;
    }
#endif

  /* Find the inode of the block driver indentified by 'source' */

  ret = open_blockdriver(pathname, 0, &var.fv_inode);
  if (ret < 0)
    {
      fdbg("Failed to open %s\n", pathname);
      goto errout;
    }

  /* Make sure that the inode supports the write and geometry methods at a minimum */

  if (!var.fv_inode->u.i_bops->write || !var.fv_inode->u.i_bops->geometry)
    {
      fdbg("%s does not support write or geometry methods\n", pathname);
      ret = -EACCES;
      goto errout_with_driver;
    }

  /* Determine the volume configuration based upon the input values and upon the
   * reported device geometry.
   */

  ret = mkfatfs_getgeometry(fmt, &var);
  if (ret < 0)
    {
      goto errout_with_driver;
    }

  /* Configure the file system */

  ret = mkfatfs_configfatfs(fmt, &var);
  if (ret < 0)
    {
      goto errout_with_driver;
    }

  /* Allocate a buffer that will be working sector memory */
#ifdef CONFIG_FAT_DMAMEMORY
  var.fv_sect = (uint8_t*)fat_dma_alloc(var.fv_sectorsize);
#else
  var.fv_sect = (uint8_t*)kmalloc(var.fv_sectorsize);
#endif
  if (!var.fv_sect)
    {
      fdbg("Failed to allocate working buffers\n");
      goto errout_with_driver;
    }

  /* Write the filesystem to media */

  ret = mkfatfs_writefatfs(fmt, &var);

errout_with_driver:
  /* Close the driver */

  (void)close_blockdriver(var.fv_inode);

errout:
  /* Release all allocated memory */

  if (var.fv_sect)
    {
#ifdef CONFIG_FAT_DMAMEMORY
      fat_dma_free(var.fv_sect, var.fv_sectorsize);
#else
      kfree(var.fv_sect);
#endif
    }

  /* Return any reported errors */

  if (ret < 0)
    {
      errno = -ret;
      return ERROR; 
    }

  return OK;
}