1 /* $NetBSD: msdos.c,v 1.14 2013/02/03 03:21:21 christos Exp $ */
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(__lint)
40 __RCSID("$NetBSD: msdos.c,v 1.14 2013/02/03 03:21:21 christos Exp $");
43 #include <sys/param.h>
45 #if !HAVE_NBTOOL_CONFIG_H
46 #include <sys/mount.h>
61 #include <fs/msdosfs/denode.h>
64 #include "mkfs_msdos.h"
66 static int msdos_populate_dir(const char *, struct denode
*, fsnode
*,
67 fsnode
*, fsinfo_t
*);
70 msdos_prep_opts(fsinfo_t
*fsopts
)
72 struct msdos_options
*msdos_opt
= ecalloc(1, sizeof(*msdos_opt
));
73 const option_t msdos_options
[] = {
74 #define AOPT(_opt, _type, _name, _min, _desc) { \
77 .type = _min == -1 ? OPT_STRPTR : \
78 (_min == -2 ? OPT_BOOL : \
79 (sizeof(_type) == 1 ? OPT_INT8 : \
80 (sizeof(_type) == 2 ? OPT_INT16 : \
81 (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))), \
82 .value = &msdos_opt->_name, \
84 .maximum = sizeof(_type) == 1 ? 0xff : \
85 (sizeof(_type) == 2 ? 0xffff : \
86 (sizeof(_type) == 4 ? 0xffffffff : 0xffffffffffffffffLL)), \
94 fsopts
->fs_specific
= msdos_opt
;
95 fsopts
->fs_options
= copy_opts(msdos_options
);
99 msdos_cleanup_opts(fsinfo_t
*fsopts
)
101 free(fsopts
->fs_specific
);
102 free(fsopts
->fs_options
);
106 msdos_parse_opts(const char *option
, fsinfo_t
*fsopts
)
108 struct msdos_options
*msdos_opt
= fsopts
->fs_specific
;
109 option_t
*msdos_options
= fsopts
->fs_options
;
113 assert(option
!= NULL
);
114 assert(fsopts
!= NULL
);
115 assert(msdos_opt
!= NULL
);
117 if (debug
& DEBUG_FS_PARSE_OPTS
)
118 printf("msdos_parse_opts: got `%s'\n", option
);
120 rv
= set_option(msdos_options
, option
, NULL
, 0);
124 if (strcmp(msdos_options
[rv
].name
, "volume_id") == 0)
125 msdos_opt
->volume_id_set
= 1;
126 else if (strcmp(msdos_options
[rv
].name
, "media_descriptor") == 0)
127 msdos_opt
->media_descriptor_set
= 1;
128 else if (strcmp(msdos_options
[rv
].name
, "hidden_sectors") == 0)
129 msdos_opt
->hidden_sectors_set
= 1;
135 msdos_makefs(const char *image
, const char *dir
, fsnode
*root
, fsinfo_t
*fsopts
)
137 struct msdos_options
*msdos_opt
= fsopts
->fs_specific
;
138 struct vnode vp
, rootvp
;
139 struct timeval start
;
140 struct msdosfsmount
*pmp
;
142 assert(image
!= NULL
);
144 assert(root
!= NULL
);
145 assert(fsopts
!= NULL
);
148 * XXX: pick up other options from the msdos specific ones?
149 * Is minsize right here?
151 msdos_opt
->create_size
= MAX(msdos_opt
->create_size
, fsopts
->minsize
);
152 msdos_opt
->offset
= fsopts
->offset
;
153 if (msdos_opt
->bytes_per_sector
== 0) {
154 if (fsopts
->sectorsize
== -1)
155 fsopts
->sectorsize
= 512;
156 msdos_opt
->bytes_per_sector
= fsopts
->sectorsize
;
157 } else if (fsopts
->sectorsize
== -1) {
158 fsopts
->sectorsize
= msdos_opt
->bytes_per_sector
;
159 } else if (fsopts
->sectorsize
!= msdos_opt
->bytes_per_sector
) {
160 err(1, "inconsistent sectorsize -S %u"
161 "!= -o bytes_per_sector %u",
162 fsopts
->sectorsize
, msdos_opt
->bytes_per_sector
);
166 printf("Creating `%s'\n", image
);
168 if (mkfs_msdos(image
, NULL
, msdos_opt
) == -1)
170 TIMER_RESULTS(start
, "mkfs_msdos");
172 fsopts
->fd
= open(image
, O_RDWR
);
175 if ((pmp
= msdosfs_mount(&vp
, 0)) == NULL
)
176 err(1, "msdosfs_mount");
178 if (msdosfs_root(pmp
, &rootvp
) != 0)
179 err(1, "msdosfs_root");
181 if (debug
& DEBUG_FS_MAKEFS
)
182 printf("msdos_makefs: image %s directory %s root %p\n",
186 printf("Populating `%s'\n", image
);
188 if (msdos_populate_dir(dir
, VTODE(&rootvp
), root
, root
, fsopts
) == -1)
189 errx(1, "Image file `%s' not created.", image
);
190 TIMER_RESULTS(start
, "msdos_populate_dir");
192 if (debug
& DEBUG_FS_MAKEFS
)
195 /* ensure no outstanding buffers remain */
196 if (debug
& DEBUG_FS_MAKEFS
)
199 printf("Image `%s' complete\n", image
);
203 msdos_populate_dir(const char *path
, struct denode
*dir
, fsnode
*root
,
204 fsnode
*parent
, fsinfo_t
*fsopts
)
207 char pbuf
[MAXPATHLEN
];
210 assert(root
!= NULL
);
211 assert(fsopts
!= NULL
);
213 for (cur
= root
->next
; cur
!= NULL
; cur
= cur
->next
) {
214 if ((size_t)snprintf(pbuf
, sizeof(pbuf
), "%s/%s", path
,
215 cur
->name
) >= sizeof(pbuf
)) {
216 warnx("path %s too long", pbuf
);
220 if ((cur
->inode
->flags
& FI_ALLOCATED
) == 0) {
221 cur
->inode
->flags
|= FI_ALLOCATED
;
224 cur
->inode
->ino
= fsopts
->curinode
;
225 cur
->parent
= parent
;
229 if (cur
->inode
->flags
& FI_WRITTEN
) {
230 continue; // hard link
232 cur
->inode
->flags
|= FI_WRITTEN
;
236 if ((de
= msdosfs_mkdire(pbuf
, dir
, cur
)) == NULL
) {
237 warn("msdosfs_mkdire %s", pbuf
);
240 if (msdos_populate_dir(pbuf
, de
, cur
->child
, cur
,
242 warn("msdos_populate_dir %s", pbuf
);
246 } else if (!S_ISREG(cur
->type
)) {
247 warnx("skipping non-regular file %s/%s", cur
->path
,
251 if (msdosfs_mkfile(pbuf
, dir
, cur
) == NULL
) {
252 warn("msdosfs_mkfile %s", pbuf
);