1 /* $NetBSD: msdos.c,v 1.20 2017/04/14 15:40:35 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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #if HAVE_NBTOOL_CONFIG_H
32 #include "nbtool_config.h"
35 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(__lint)
37 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #if !HAVE_NBTOOL_CONFIG_H
43 #include <sys/mount.h>
58 #include <mkfs_msdos.h>
59 #include <fs/msdosfs/bpb.h>
60 #include "msdos/direntry.h"
61 #include "msdos/denode.h"
62 #include <fs/msdosfs/msdosfsmount.h>
69 static int msdos_populate_dir(const char *, struct denode *, fsnode *,
70 fsnode *, fsinfo_t *);
72 struct msdos_options_ex {
73 struct msdos_options options;
77 msdos_prep_opts(fsinfo_t *fsopts)
79 struct msdos_options_ex *msdos_opt = ecalloc(1, sizeof(*msdos_opt));
80 const option_t msdos_options[] = {
81 #define AOPT(_opt, _type, _name, _min, _desc) { \
84 .type = _min == -1 ? OPT_STRPTR : \
85 (_min == -2 ? OPT_BOOL : \
86 (sizeof(_type) == 1 ? OPT_INT8 : \
87 (sizeof(_type) == 2 ? OPT_INT16 : \
88 (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))), \
89 .value = &msdos_opt->options._name, \
91 .maximum = sizeof(_type) == 1 ? UINT8_MAX : \
92 (sizeof(_type) == 2 ? UINT16_MAX : \
93 (sizeof(_type) == 4 ? UINT32_MAX : INT64_MAX)), \
101 fsopts->fs_specific = msdos_opt;
102 fsopts->fs_options = copy_opts(msdos_options);
106 msdos_cleanup_opts(fsinfo_t *fsopts)
108 free(fsopts->fs_specific);
109 free(fsopts->fs_options);
113 msdos_parse_opts(const char *option, fsinfo_t *fsopts)
115 struct msdos_options *msdos_opt = fsopts->fs_specific;
116 option_t *msdos_options = fsopts->fs_options;
119 assert(option != NULL);
120 assert(fsopts != NULL);
121 assert(msdos_opt != NULL);
123 if (debug & DEBUG_FS_PARSE_OPTS)
124 printf("msdos_parse_opts: got `%s'\n", option);
126 rv = set_option(msdos_options, option, NULL, 0);
130 if (strcmp(msdos_options[rv].name, "volume_id") == 0)
131 msdos_opt->volume_id_set = 1;
132 else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0)
133 msdos_opt->media_descriptor_set = 1;
134 else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0)
135 msdos_opt->hidden_sectors_set = 1;
137 if (stampst.st_ino) {
138 msdos_opt->timestamp_set = 1;
139 msdos_opt->timestamp = stampst.st_mtime;
147 msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
149 struct msdos_options_ex *msdos_opt = fsopts->fs_specific;
150 struct m_vnode vp, rootvp;
151 struct timeval start;
152 struct msdosfsmount *pmp;
154 assert(image != NULL);
156 assert(root != NULL);
157 assert(fsopts != NULL);
159 fsopts->size = fsopts->maxsize;
160 msdos_opt->options.create_size = MAX(msdos_opt->options.create_size,
161 fsopts->offset + fsopts->size);
162 if (fsopts->offset > 0)
163 msdos_opt->options.offset = fsopts->offset;
164 if (msdos_opt->options.bytes_per_sector == 0) {
165 if (fsopts->sectorsize == -1)
166 fsopts->sectorsize = 512;
167 msdos_opt->options.bytes_per_sector = fsopts->sectorsize;
168 } else if (fsopts->sectorsize == -1) {
169 fsopts->sectorsize = msdos_opt->options.bytes_per_sector;
170 } else if (fsopts->sectorsize != msdos_opt->options.bytes_per_sector) {
171 err(1, "inconsistent sectorsize -S %u"
172 "!= -o bytes_per_sector %u",
173 fsopts->sectorsize, msdos_opt->options.bytes_per_sector);
177 printf("Creating `%s'\n", image);
179 if (mkfs_msdos(image, NULL, &msdos_opt->options) == -1)
181 TIMER_RESULTS(start, "mkfs_msdos");
183 fsopts->fd = open(image, O_RDWR);
186 if ((pmp = m_msdosfs_mount(&vp)) == NULL)
187 err(1, "msdosfs_mount");
189 if (msdosfs_root(pmp, &rootvp) != 0)
190 err(1, "msdosfs_root");
192 if (debug & DEBUG_FS_MAKEFS)
193 printf("msdos_makefs: image %s directory %s root %p\n",
197 printf("Populating `%s'\n", image);
199 if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
200 errx(1, "Image file `%s' not created.", image);
201 TIMER_RESULTS(start, "msdos_populate_dir");
203 if (msdosfs_fsiflush(pmp) != 0)
204 errx(1, "Unable to update FSInfo block.");
205 if (debug & DEBUG_FS_MAKEFS)
208 /* ensure no outstanding buffers remain */
209 if (debug & DEBUG_FS_MAKEFS)
212 printf("Image `%s' complete\n", image);
216 msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
217 fsnode *parent, fsinfo_t *fsopts)
220 char pbuf[MAXPATHLEN];
223 assert(root != NULL);
224 assert(fsopts != NULL);
226 for (cur = root->next; cur != NULL; cur = cur->next) {
227 if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
228 cur->name) >= sizeof(pbuf)) {
229 warnx("path %s too long", pbuf);
233 if ((cur->inode->flags & FI_ALLOCATED) == 0) {
234 cur->inode->flags |= FI_ALLOCATED;
237 cur->inode->ino = fsopts->curinode;
238 cur->parent = parent;
242 if (cur->inode->flags & FI_WRITTEN) {
243 continue; // hard link
245 cur->inode->flags |= FI_WRITTEN;
249 if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
250 warn("msdosfs_mkdire %s", pbuf);
253 if (msdos_populate_dir(pbuf, de, cur->child, cur,
255 warn("msdos_populate_dir %s", pbuf);
259 } else if (!S_ISREG(cur->type)) {
260 warnx("skipping non-regular file %s/%s", cur->path,
264 if (msdosfs_mkfile(cur->contents ? cur->contents : pbuf, dir,
266 warn("msdosfs_mkfile %s", pbuf);