1 /* $NetBSD: msdos.c,v 1.20 2017/04/14 15:40:35 christos Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2013 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Christos Zoulas.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33 #if HAVE_NBTOOL_CONFIG_H
34 #include "nbtool_config.h"
35 #endif
36
37 #include <sys/param.h>
38
39 #if !HAVE_NBTOOL_CONFIG_H
40 #include <sys/mount.h>
41 #endif
42
43 #include <assert.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stdint.h>
51 #include <unistd.h>
52 #include <dirent.h>
53 #include <util.h>
54
55 #include <mkfs_msdos.h>
56 #include <fs/msdosfs/bpb.h>
57 #include "msdos/direntry.h"
58 #include "msdos/denode.h"
59 #include <fs/msdosfs/msdosfsmount.h>
60
61 #undef clrbuf
62 #include "ffs/buf.h"
63 #include "makefs.h"
64 #include "msdos.h"
65
66 static int msdos_populate_dir(const char *, struct denode *, fsnode *,
67 fsnode *, fsinfo_t *);
68
69 struct msdos_options_ex {
70 struct msdos_options options;
71 };
72
73 void
msdos_prep_opts(fsinfo_t * fsopts)74 msdos_prep_opts(fsinfo_t *fsopts)
75 {
76 struct msdos_options_ex *msdos_opt = ecalloc(1, sizeof(*msdos_opt));
77 const option_t msdos_options[] = {
78 #define AOPT(_opt, _type, _name, _min, _desc) { \
79 .letter = _opt, \
80 .name = # _name, \
81 .type = _min == -1 ? OPT_STRPTR : \
82 (_min == -2 ? OPT_BOOL : \
83 (sizeof(_type) == 1 ? OPT_INT8 : \
84 (sizeof(_type) == 2 ? OPT_INT16 : \
85 (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))), \
86 .value = &msdos_opt->options._name, \
87 .minimum = _min, \
88 .maximum = sizeof(_type) == 1 ? UINT8_MAX : \
89 (sizeof(_type) == 2 ? UINT16_MAX : \
90 (sizeof(_type) == 4 ? UINT32_MAX : INT64_MAX)), \
91 .desc = _desc, \
92 },
93 ALLOPTS
94 #undef AOPT
95 { .name = NULL }
96 };
97
98 fsopts->fs_specific = msdos_opt;
99 fsopts->fs_options = copy_opts(msdos_options);
100 }
101
102 void
msdos_cleanup_opts(fsinfo_t * fsopts)103 msdos_cleanup_opts(fsinfo_t *fsopts)
104 {
105 free(fsopts->fs_specific);
106 free(fsopts->fs_options);
107 }
108
109 int
msdos_parse_opts(const char * option,fsinfo_t * fsopts)110 msdos_parse_opts(const char *option, fsinfo_t *fsopts)
111 {
112 struct msdos_options *msdos_opt = fsopts->fs_specific;
113 option_t *msdos_options = fsopts->fs_options;
114 int rv;
115
116 assert(option != NULL);
117 assert(fsopts != NULL);
118 assert(msdos_opt != NULL);
119
120 if (debug & DEBUG_FS_PARSE_OPTS)
121 printf("msdos_parse_opts: got `%s'\n", option);
122
123 rv = set_option(msdos_options, option, NULL, 0);
124 if (rv == -1)
125 return rv;
126
127 if (strcmp(msdos_options[rv].name, "volume_id") == 0)
128 msdos_opt->volume_id_set = 1;
129 else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0)
130 msdos_opt->media_descriptor_set = 1;
131 else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0)
132 msdos_opt->hidden_sectors_set = 1;
133
134 if (stampst.st_ino) {
135 msdos_opt->timestamp_set = 1;
136 msdos_opt->timestamp = stampst.st_mtime;
137 }
138
139 return 1;
140 }
141
142
143 void
msdos_makefs(const char * image,const char * dir,fsnode * root,fsinfo_t * fsopts)144 msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
145 {
146 struct msdos_options_ex *msdos_opt = fsopts->fs_specific;
147 struct m_vnode vp, rootvp;
148 struct timeval start;
149 struct msdosfsmount *pmp;
150
151 assert(image != NULL);
152 assert(dir != NULL);
153 assert(root != NULL);
154 assert(fsopts != NULL);
155
156 fsopts->size = fsopts->maxsize;
157 msdos_opt->options.create_size = MAX(msdos_opt->options.create_size,
158 fsopts->offset + fsopts->size);
159 if (fsopts->offset > 0)
160 msdos_opt->options.offset = fsopts->offset;
161 if (msdos_opt->options.bytes_per_sector == 0) {
162 if (fsopts->sectorsize == -1)
163 fsopts->sectorsize = 512;
164 msdos_opt->options.bytes_per_sector = fsopts->sectorsize;
165 } else if (fsopts->sectorsize == -1) {
166 fsopts->sectorsize = msdos_opt->options.bytes_per_sector;
167 } else if (fsopts->sectorsize != msdos_opt->options.bytes_per_sector) {
168 err(1, "inconsistent sectorsize -S %u"
169 "!= -o bytes_per_sector %u",
170 fsopts->sectorsize, msdos_opt->options.bytes_per_sector);
171 }
172
173 /* create image */
174 printf("Creating `%s'\n", image);
175 TIMER_START(start);
176 if (mkfs_msdos(image, NULL, &msdos_opt->options) == -1)
177 return;
178 TIMER_RESULTS(start, "mkfs_msdos");
179
180 fsopts->fd = open(image, O_RDWR);
181 vp.fs = fsopts;
182
183 if ((pmp = m_msdosfs_mount(&vp)) == NULL)
184 err(1, "msdosfs_mount");
185
186 if (msdosfs_root(pmp, &rootvp) != 0)
187 err(1, "msdosfs_root");
188
189 if (debug & DEBUG_FS_MAKEFS)
190 printf("msdos_makefs: image %s directory %s root %p\n",
191 image, dir, root);
192
193 /* populate image */
194 printf("Populating `%s'\n", image);
195 TIMER_START(start);
196 if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
197 errx(1, "Image file `%s' not created.", image);
198 TIMER_RESULTS(start, "msdos_populate_dir");
199
200 if (msdosfs_fsiflush(pmp) != 0)
201 errx(1, "Unable to update FSInfo block.");
202 if (debug & DEBUG_FS_MAKEFS)
203 putchar('\n');
204
205 /* ensure no outstanding buffers remain */
206 if (debug & DEBUG_FS_MAKEFS)
207 bcleanup();
208
209 printf("Image `%s' complete\n", image);
210 }
211
212 static int
msdos_populate_dir(const char * path,struct denode * dir,fsnode * root,fsnode * parent,fsinfo_t * fsopts)213 msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
214 fsnode *parent, fsinfo_t *fsopts)
215 {
216 fsnode *cur;
217 char pbuf[MAXPATHLEN];
218
219 assert(dir != NULL);
220 assert(root != NULL);
221 assert(fsopts != NULL);
222
223 for (cur = root->next; cur != NULL; cur = cur->next) {
224 if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
225 cur->name) >= sizeof(pbuf)) {
226 warnx("path %s too long", pbuf);
227 return -1;
228 }
229
230 if ((cur->inode->flags & FI_ALLOCATED) == 0) {
231 cur->inode->flags |= FI_ALLOCATED;
232 if (cur != root) {
233 fsopts->curinode++;
234 cur->inode->ino = fsopts->curinode;
235 cur->parent = parent;
236 }
237 }
238
239 if (cur->inode->flags & FI_WRITTEN) {
240 continue; // hard link
241 }
242 cur->inode->flags |= FI_WRITTEN;
243
244 if (cur->child) {
245 struct denode *de;
246 if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
247 warn("msdosfs_mkdire %s", pbuf);
248 return -1;
249 }
250 if (msdos_populate_dir(pbuf, de, cur->child, cur,
251 fsopts) == -1) {
252 warn("msdos_populate_dir %s", pbuf);
253 return -1;
254 }
255 continue;
256 } else if (!S_ISREG(cur->type)) {
257 warnx("skipping non-regular file %s/%s", cur->path,
258 cur->name);
259 continue;
260 }
261 if (msdosfs_mkfile(cur->contents ? cur->contents : pbuf, dir,
262 cur) == NULL) {
263 warn("msdosfs_mkfile %s", pbuf);
264 return -1;
265 }
266 }
267 return 0;
268 }
269