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