aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/makefs/msdos.c
blob: a0e0f7174f251b4cf40f638306319d2ae1f409be (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*	$NetBSD: msdos.c,v 1.20 2017/04/14 15:40:35 christos Exp $	*/

/*-
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Christos Zoulas.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif

#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(__lint)
__FBSDID("$FreeBSD$");
#endif	/* !__lint */

#include <sys/param.h>

#if !HAVE_NBTOOL_CONFIG_H
#include <sys/mount.h>
#endif

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <dirent.h>
#include <util.h>

#include "ffs/buf.h"
#include "makefs.h"
#include "msdos.h"

#include <mkfs_msdos.h>
#include <fs/msdosfs/bpb.h>
#include "msdos/direntry.h"
#include <fs/msdosfs/denode.h>
#include <fs/msdosfs/msdosfsmount.h>

static int msdos_populate_dir(const char *, struct denode *, fsnode *,
    fsnode *, fsinfo_t *);

struct msdos_options_ex {
	struct msdos_options options;
};

void
msdos_prep_opts(fsinfo_t *fsopts)
{
	struct msdos_options_ex *msdos_opt = ecalloc(1, sizeof(*msdos_opt));
	const option_t msdos_options[] = {
#define AOPT(_opt, _type, _name, _min, _desc) {				\
	.letter = _opt,							\
	.name = # _name,						\
	.type = _min == -1 ? OPT_STRPTR :				\
	    (_min == -2 ? OPT_BOOL :					\
	    (sizeof(_type) == 1 ? OPT_INT8 :				\
	    (sizeof(_type) == 2 ? OPT_INT16 :				\
	    (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))),		\
	.value = &msdos_opt->options._name,				\
	.minimum = _min,						\
	.maximum = sizeof(_type) == 1 ? UINT8_MAX :			\
	    (sizeof(_type) == 2 ? UINT16_MAX :				\
	    (sizeof(_type) == 4 ? UINT32_MAX : INT64_MAX)),		\
	.desc = _desc,							\
},
ALLOPTS
#undef AOPT
		{ .name = NULL }
	};

	fsopts->fs_specific = msdos_opt;
	fsopts->fs_options = copy_opts(msdos_options);
}

void
msdos_cleanup_opts(fsinfo_t *fsopts)
{
	free(fsopts->fs_specific);
	free(fsopts->fs_options);
}

int
msdos_parse_opts(const char *option, fsinfo_t *fsopts)
{
	struct msdos_options *msdos_opt = fsopts->fs_specific;
	option_t *msdos_options = fsopts->fs_options;
	int rv;

	assert(option != NULL);
	assert(fsopts != NULL);
	assert(msdos_opt != NULL);

	if (debug & DEBUG_FS_PARSE_OPTS)
		printf("msdos_parse_opts: got `%s'\n", option);

	rv = set_option(msdos_options, option, NULL, 0);
	if (rv == -1)
		return rv;

	if (strcmp(msdos_options[rv].name, "volume_id") == 0)
		msdos_opt->volume_id_set = 1;
	else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0)
		msdos_opt->media_descriptor_set = 1;
	else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0)
		msdos_opt->hidden_sectors_set = 1;

	if (stampst.st_ino) {
		msdos_opt->timestamp_set = 1;
		msdos_opt->timestamp = stampst.st_mtime;
	}

	return 1;
}


void
msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
{
	struct msdos_options_ex *msdos_opt = fsopts->fs_specific;
	struct vnode vp, rootvp;
	struct timeval start;
	struct msdosfsmount *pmp;
	uint32_t flags;

	assert(image != NULL);
	assert(dir != NULL);
	assert(root != NULL);
	assert(fsopts != NULL);

	fsopts->size = fsopts->maxsize;
	msdos_opt->options.create_size = MAX(msdos_opt->options.create_size,
	    fsopts->offset + fsopts->size);
	if (fsopts->offset > 0)
		msdos_opt->options.offset = fsopts->offset;
	if (msdos_opt->options.bytes_per_sector == 0) {
		if (fsopts->sectorsize == -1)
			fsopts->sectorsize = 512;
		msdos_opt->options.bytes_per_sector = fsopts->sectorsize;
	} else if (fsopts->sectorsize == -1) {
		fsopts->sectorsize = msdos_opt->options.bytes_per_sector;
	} else if (fsopts->sectorsize != msdos_opt->options.bytes_per_sector) {
		err(1, "inconsistent sectorsize -S %u"
		    "!= -o bytes_per_sector %u",
		    fsopts->sectorsize, msdos_opt->options.bytes_per_sector);
	}

	/* create image */
	printf("Creating `%s'\n", image);
	TIMER_START(start);
	if (mkfs_msdos(image, NULL, &msdos_opt->options) == -1)
		return;
	TIMER_RESULTS(start, "mkfs_msdos");

	fsopts->fd = open(image, O_RDWR);
	vp.fs = fsopts;

	flags = 0;
	if ((pmp = msdosfs_mount(&vp)) == NULL)
		err(1, "msdosfs_mount");

	if (msdosfs_root(pmp, &rootvp) != 0)
		err(1, "msdosfs_root");

	if (debug & DEBUG_FS_MAKEFS)
		printf("msdos_makefs: image %s directory %s root %p\n",
		    image, dir, root);

	/* populate image */
	printf("Populating `%s'\n", image);
	TIMER_START(start);
	if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
		errx(1, "Image file `%s' not created.", image);
	TIMER_RESULTS(start, "msdos_populate_dir");

	if (msdosfs_fsiflush(pmp) != 0)
		errx(1, "Unable to update FSInfo block.");
	if (debug & DEBUG_FS_MAKEFS)
		putchar('\n');

	/* ensure no outstanding buffers remain */
	if (debug & DEBUG_FS_MAKEFS)
		bcleanup();

	printf("Image `%s' complete\n", image);
}

static int
msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
    fsnode *parent, fsinfo_t *fsopts)
{
	fsnode *cur;
	char pbuf[MAXPATHLEN];

	assert(dir != NULL);
	assert(root != NULL);
	assert(fsopts != NULL);

	for (cur = root->next; cur != NULL; cur = cur->next) {
		if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
		    cur->name) >= sizeof(pbuf)) {
			warnx("path %s too long", pbuf);
			return -1;
		}

		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
			cur->inode->flags |= FI_ALLOCATED;
			if (cur != root) {
				fsopts->curinode++;
				cur->inode->ino = fsopts->curinode;
				cur->parent = parent;
			}
		}

		if (cur->inode->flags & FI_WRITTEN) {
			continue;	// hard link
		}
		cur->inode->flags |= FI_WRITTEN;

		if (cur->child) {
			struct denode *de;
			if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
				warn("msdosfs_mkdire %s", pbuf);
				return -1;
			}
			if (msdos_populate_dir(pbuf, de, cur->child, cur,
			    fsopts) == -1) {
				warn("msdos_populate_dir %s", pbuf);
				return -1;
			}
			continue;
		} else if (!S_ISREG(cur->type)) {
			warnx("skipping non-regular file %s/%s", cur->path,
			    cur->name);
			continue;
		}
		if (msdosfs_mkfile(cur->contents ? cur->contents : pbuf, dir,
		    cur) == NULL) {
			warn("msdosfs_mkfile %s", pbuf);
			return -1;
		}
	}
	return 0;
}