aboutsummaryrefslogtreecommitdiff
path: root/nuttx/fs/fat/fs_mkfatfs.h
blob: 05801c92d7c71168b926792bb0c2a8c95264d12e (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
/****************************************************************************
 * fs/fat/fs_mkfat.h
 *
 *   Copyright (C) 2008-2009 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.
 *
 ****************************************************************************/

#ifndef __FS_FAT_FS_MKATFS_H
#define __FS_FAT_FS_MKATFS_H

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

#include <nuttx/config.h>
#include <stdint.h>

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* Only the "hard drive" media type is used */

#define FAT_DEFAULT_MEDIA_TYPE         0xf8

/*  Default hard driver geometry */

#define FAT_DEFAULT_SECPERTRK          63
#define FAT_DEFAULT_NUMHEADS           255

/* FSINFO is always at this sector */

#define FAT_DEFAULT_FSINFO_SECTOR      1

/* FAT32 foot cluster number */

#define FAT32_DEFAULT_ROOT_CLUSTER     2

/* Macros to simplify direct block driver access */

#define DEV_OPEN() \
    var->fb_inode->u.i_bops->open ? \
    var->fv_inode->u.i_bops->open(var->fv_inode) : \
    0
#define DEV_CLOSE() \
    var->fb_inode->u.i_bops->close ? \
    var->fv_inode->u.i_bops->close(var->fv_inode) : \
    0
#define DEV_READ(buf, sect, nsect) \
    var->fv_inode->u.i_bops->read(var->fv_inode, buf, sect, nsect)
#define DEV_WRITE(buf, sect, nsect) \
    var->fv_inode->u.i_bops->write(var->fv_inode, buf, sect, nsect)
#define DEV_GEOMETRY(geo) \
    var->fv_inode->u.i_bops->geometry(var->fv_inode, &geo)
#define DEV_IOCTL(cmd, arg) \
    var->fv_inode->u.i_bops->ioctl(var->fv_inode, cmd, arg)

/****************************************************************************
 * Public Types
 ****************************************************************************/

/* This structure (plus the user-provided struct fat_format_s) describes
 * the format FAT file system.  All "global" variables used in the format
 * logic are contained in this structure so that is possible to format two
 * block devices concurrently.
 */

struct fat_var_s
{
   struct inode  *fv_inode;          /* The block driver "handle" */
   uint8_t        fv_jump[3];        /* 3-byte boot jump instruction */
   uint8_t        fv_sectshift;      /* Log2 of fv_sectorsize */
   uint8_t        fv_nrootdirsects;  /* Number of root directory sectors */
   uint8_t        fv_fattype;        /* FAT size: 0 (not determined), 12, 16, or 32 */
   uint16_t       fv_bootcodesize;   /* Size of array at fv_bootcode */
   uint32_t       fv_createtime;     /* Creation time */
   uint32_t       fv_sectorsize;     /* Size of one hardware sector */
   uint32_t       fv_nfatsects;      /* Number of sectors in each FAT */
   uint32_t       fv_nclusters;      /* Number of clusters */
   uint8_t       *fv_sect;           /* Allocated working sector buffer */
   const uint8_t *fv_bootcode;       /* Points to boot code to put into MBR */
};

/****************************************************************************
 * Global Variables
 ****************************************************************************/

/****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif

/****************************************************************************
 * Name: mkfatfs_configfatfs
 *
 * Description:
 *   Based on the geometry of the block device and upon the caller-selected
 *   values, configure the FAT filesystem for the device.
 *
 * Input:
 *    fmt  - Caller specified format parameters
 *    var  - Holds disk geomtry data.  Also, the location to return FAT
 *           configuration data
 *
 * Return:
 *    Zero on success; negated errno on failure
 *
 ****************************************************************************/
EXTERN int mkfatfs_configfatfs(FAR struct fat_format_s *fmt,
                               FAR struct fat_var_s *var);

/****************************************************************************
 * Name: mkfatfs_writefat
 *
 * Description:
 *   Write the configured fat filesystem to the 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
 *
 ****************************************************************************/
EXTERN int mkfatfs_writefatfs(FAR struct fat_format_s *fmt,
                              FAR struct fat_var_s *var);

#undef EXTERN
#if defined(__cplusplus)
}
#endif

#endif /* __FS_FAT_FS_MKATFS_H */