xref: /freebsd/usr.sbin/makefs/msdos.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 /*	$NetBSD: msdos.c,v 1.20 2017/04/14 15:40:35 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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  *
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.
30  */
31 #if HAVE_NBTOOL_CONFIG_H
32 #include "nbtool_config.h"
33 #endif
34 
35 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(__lint)
37 __FBSDID("$FreeBSD$");
38 #endif	/* !__lint */
39 
40 #include <sys/param.h>
41 
42 #if !HAVE_NBTOOL_CONFIG_H
43 #include <sys/mount.h>
44 #endif
45 
46 #include <assert.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <stdint.h>
54 #include <unistd.h>
55 #include <dirent.h>
56 #include <util.h>
57 
58 #include "ffs/buf.h"
59 #include "makefs.h"
60 #include "msdos.h"
61 
62 #include <mkfs_msdos.h>
63 #include <fs/msdosfs/bpb.h>
64 #include "msdos/direntry.h"
65 #include <fs/msdosfs/denode.h>
66 #include <fs/msdosfs/msdosfsmount.h>
67 
68 static int msdos_populate_dir(const char *, struct denode *, fsnode *,
69     fsnode *, fsinfo_t *);
70 
71 struct msdos_options_ex {
72 	struct msdos_options options;
73 };
74 
75 void
76 msdos_prep_opts(fsinfo_t *fsopts)
77 {
78 	struct msdos_options_ex *msdos_opt = ecalloc(1, sizeof(*msdos_opt));
79 	const option_t msdos_options[] = {
80 #define AOPT(_opt, _type, _name, _min, _desc) {				\
81 	.letter = _opt,							\
82 	.name = # _name,						\
83 	.type = _min == -1 ? OPT_STRPTR :				\
84 	    (_min == -2 ? OPT_BOOL :					\
85 	    (sizeof(_type) == 1 ? OPT_INT8 :				\
86 	    (sizeof(_type) == 2 ? OPT_INT16 :				\
87 	    (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))),		\
88 	.value = &msdos_opt->options._name,				\
89 	.minimum = _min,						\
90 	.maximum = sizeof(_type) == 1 ? UINT8_MAX :			\
91 	    (sizeof(_type) == 2 ? UINT16_MAX :				\
92 	    (sizeof(_type) == 4 ? UINT32_MAX : INT64_MAX)),		\
93 	.desc = _desc,							\
94 },
95 ALLOPTS
96 #undef AOPT
97 		{ .name = NULL }
98 	};
99 
100 	fsopts->fs_specific = msdos_opt;
101 	fsopts->fs_options = copy_opts(msdos_options);
102 }
103 
104 void
105 msdos_cleanup_opts(fsinfo_t *fsopts)
106 {
107 	free(fsopts->fs_specific);
108 	free(fsopts->fs_options);
109 }
110 
111 int
112 msdos_parse_opts(const char *option, fsinfo_t *fsopts)
113 {
114 	struct msdos_options *msdos_opt = fsopts->fs_specific;
115 	option_t *msdos_options = fsopts->fs_options;
116 	int rv;
117 
118 	assert(option != NULL);
119 	assert(fsopts != NULL);
120 	assert(msdos_opt != NULL);
121 
122 	if (debug & DEBUG_FS_PARSE_OPTS)
123 		printf("msdos_parse_opts: got `%s'\n", option);
124 
125 	rv = set_option(msdos_options, option, NULL, 0);
126 	if (rv == -1)
127 		return rv;
128 
129 	if (strcmp(msdos_options[rv].name, "volume_id") == 0)
130 		msdos_opt->volume_id_set = 1;
131 	else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0)
132 		msdos_opt->media_descriptor_set = 1;
133 	else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0)
134 		msdos_opt->hidden_sectors_set = 1;
135 
136 	if (stampst.st_ino) {
137 		msdos_opt->timestamp_set = 1;
138 		msdos_opt->timestamp = stampst.st_mtime;
139 	}
140 
141 	return 1;
142 }
143 
144 
145 void
146 msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
147 {
148 	struct msdos_options_ex *msdos_opt = fsopts->fs_specific;
149 	struct vnode vp, rootvp;
150 	struct timeval start;
151 	struct msdosfsmount *pmp;
152 	uint32_t flags;
153 
154 	assert(image != NULL);
155 	assert(dir != NULL);
156 	assert(root != NULL);
157 	assert(fsopts != NULL);
158 
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);
174 	}
175 
176 	/* create image */
177 	printf("Creating `%s'\n", image);
178 	TIMER_START(start);
179 	if (mkfs_msdos(image, NULL, &msdos_opt->options) == -1)
180 		return;
181 	TIMER_RESULTS(start, "mkfs_msdos");
182 
183 	fsopts->fd = open(image, O_RDWR);
184 	vp.fs = fsopts;
185 
186 	flags = 0;
187 	if ((pmp = msdosfs_mount(&vp)) == NULL)
188 		err(1, "msdosfs_mount");
189 
190 	if (msdosfs_root(pmp, &rootvp) != 0)
191 		err(1, "msdosfs_root");
192 
193 	if (debug & DEBUG_FS_MAKEFS)
194 		printf("msdos_makefs: image %s directory %s root %p\n",
195 		    image, dir, root);
196 
197 	/* populate image */
198 	printf("Populating `%s'\n", image);
199 	TIMER_START(start);
200 	if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
201 		errx(1, "Image file `%s' not created.", image);
202 	TIMER_RESULTS(start, "msdos_populate_dir");
203 
204 	if (msdosfs_fsiflush(pmp) != 0)
205 		errx(1, "Unable to update FSInfo block.");
206 	if (debug & DEBUG_FS_MAKEFS)
207 		putchar('\n');
208 
209 	/* ensure no outstanding buffers remain */
210 	if (debug & DEBUG_FS_MAKEFS)
211 		bcleanup();
212 
213 	printf("Image `%s' complete\n", image);
214 }
215 
216 static int
217 msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
218     fsnode *parent, fsinfo_t *fsopts)
219 {
220 	fsnode *cur;
221 	char pbuf[MAXPATHLEN];
222 
223 	assert(dir != NULL);
224 	assert(root != NULL);
225 	assert(fsopts != NULL);
226 
227 	for (cur = root->next; cur != NULL; cur = cur->next) {
228 		if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
229 		    cur->name) >= sizeof(pbuf)) {
230 			warnx("path %s too long", pbuf);
231 			return -1;
232 		}
233 
234 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
235 			cur->inode->flags |= FI_ALLOCATED;
236 			if (cur != root) {
237 				fsopts->curinode++;
238 				cur->inode->ino = fsopts->curinode;
239 				cur->parent = parent;
240 			}
241 		}
242 
243 		if (cur->inode->flags & FI_WRITTEN) {
244 			continue;	// hard link
245 		}
246 		cur->inode->flags |= FI_WRITTEN;
247 
248 		if (cur->child) {
249 			struct denode *de;
250 			if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
251 				warn("msdosfs_mkdire %s", pbuf);
252 				return -1;
253 			}
254 			if (msdos_populate_dir(pbuf, de, cur->child, cur,
255 			    fsopts) == -1) {
256 				warn("msdos_populate_dir %s", pbuf);
257 				return -1;
258 			}
259 			continue;
260 		} else if (!S_ISREG(cur->type)) {
261 			warnx("skipping non-regular file %s/%s", cur->path,
262 			    cur->name);
263 			continue;
264 		}
265 		if (msdosfs_mkfile(cur->contents ? cur->contents : pbuf, dir,
266 		    cur) == NULL) {
267 			warn("msdosfs_mkfile %s", pbuf);
268 			return -1;
269 		}
270 	}
271 	return 0;
272 }
273