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