xref: /openbsd-src/usr.sbin/makefs/msdos.c (revision ffcef06798eb7b98532e76a80212f0772bebc4f6)
1 /*	$OpenBSD: msdos.c,v 1.12 2021/09/01 15:19:00 deraadt Exp $	*/
2 /*	$NetBSD: msdos.c,v 1.16 2016/01/30 09:59:27 mlelstv Exp $	*/
3 
4 /*-
5  * Copyright (c) 2013 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Christos Zoulas.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <assert.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "ffs/buf.h"
40 #include "msdos/denode.h"
41 #include "makefs.h"
42 #include "msdos.h"
43 #include "msdos/mkfs_msdos.h"
44 
45 static int msdos_populate_dir(const char *, struct denode *, fsnode *,
46     fsnode *, fsinfo_t *);
47 
48 void
49 msdos_prep_opts(fsinfo_t *fsopts)
50 {
51 	struct msdos_options *msdos_opt = ecalloc(1, sizeof(*msdos_opt));
52 	const option_t msdos_options[] = {
53 #define AOPT(_type, _name, _min) {					\
54 	.name = # _name,						\
55 	.type = _min == -1 ? OPT_STRPTR :				\
56 	    (_min == -2 ? OPT_BOOL :					\
57 	    (sizeof(_type) == 1 ? OPT_INT8 :				\
58 	    (sizeof(_type) == 2 ? OPT_INT16 :				\
59 	    (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))),		\
60 	.value = &msdos_opt->_name,					\
61 	.minimum = _min,						\
62 	.maximum = sizeof(_type) == 1 ? 0xff :				\
63 	    (sizeof(_type) == 2 ? 0xffff :				\
64 	    (sizeof(_type) == 4 ? 0xffffffff : 0x7fffffffffffffffLL)),	\
65 },
66 ALLOPTS
67 #undef AOPT
68 		{ .name = NULL }
69 	};
70 
71 	fsopts->fs_specific = msdos_opt;
72 	fsopts->fs_options = copy_opts(msdos_options);
73 }
74 
75 void
76 msdos_cleanup_opts(fsinfo_t *fsopts)
77 {
78 	free(fsopts->fs_specific);
79 	free(fsopts->fs_options);
80 }
81 
82 int
83 msdos_parse_opts(const char *option, fsinfo_t *fsopts)
84 {
85 	struct msdos_options *msdos_opt = fsopts->fs_specific;
86 	option_t *msdos_options = fsopts->fs_options;
87 
88 	int rv;
89 
90 	assert(option != NULL);
91 	assert(fsopts != NULL);
92 	assert(msdos_opt != NULL);
93 
94 	rv = set_option(msdos_options, option, NULL, 0);
95 	if (rv == -1)
96 		return rv;
97 
98 	if (strcmp(msdos_options[rv].name, "volume_id") == 0)
99 		msdos_opt->volume_id_set = 1;
100 	else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0)
101 		msdos_opt->media_descriptor_set = 1;
102 	else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0)
103 		msdos_opt->hidden_sectors_set = 1;
104 	return 1;
105 }
106 
107 
108 void
109 msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
110 {
111 	struct msdos_options *msdos_opt = fsopts->fs_specific;
112 	struct mkfsvnode vp, rootvp;
113 	struct msdosfsmount *pmp;
114 
115 	assert(image != NULL);
116 	assert(dir != NULL);
117 	assert(root != NULL);
118 	assert(fsopts != NULL);
119 
120 	/*
121 	 * XXX: pick up other options from the msdos specific ones?
122 	 * Is minsize right here?
123 	 */
124 	msdos_opt->create_size = MAX(msdos_opt->create_size,
125 		fsopts->minsize);
126 	msdos_opt->offset = fsopts->offset;
127 	if (msdos_opt->bytes_per_sector == 0) {
128 		if (fsopts->sectorsize == -1)
129 			fsopts->sectorsize = 512;
130 		msdos_opt->bytes_per_sector = fsopts->sectorsize;
131 	} else if (fsopts->sectorsize == -1) {
132 		fsopts->sectorsize = msdos_opt->bytes_per_sector;
133 	} else if (fsopts->sectorsize != msdos_opt->bytes_per_sector) {
134 		err(1, "inconsistent sectorsize -S %u"
135 		    "!= -o bytes_per_sector %u",
136 		    fsopts->sectorsize, msdos_opt->bytes_per_sector);
137 	}
138 
139 		/* create image */
140 	printf("Creating `%s'\n", image);
141 	if (mkfs_msdos(image, NULL, msdos_opt) == -1)
142 		return;
143 
144 	fsopts->fd = open(image, O_RDWR);
145 	vp.fs = fsopts;
146 
147 	if ((pmp = msdosfs_mount(&vp, 0)) == NULL)
148 		err(1, "msdosfs_mount");
149 
150 	if (msdosfs_root(pmp, &rootvp) != 0)
151 		err(1, "msdosfs_root");
152 
153 		/* populate image */
154 	printf("Populating `%s'\n", image);
155 	if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
156 		errx(1, "Image file `%s' not created.", image);
157 
158 	bcleanup();
159 
160 	printf("Image `%s' complete\n", image);
161 }
162 
163 static int
164 msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
165     fsnode *parent, fsinfo_t *fsopts)
166 {
167 	fsnode *cur;
168 	char pbuf[MAXPATHLEN];
169 
170 	assert(dir != NULL);
171 	assert(root != NULL);
172 	assert(fsopts != NULL);
173 
174 	for (cur = root->next; cur != NULL; cur = cur->next) {
175 		if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
176 		    cur->name) >= sizeof(pbuf)) {
177 			warnx("path %s too long", pbuf);
178 			return -1;
179 		}
180 
181 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
182 			cur->inode->flags |= FI_ALLOCATED;
183 			if (cur != root) {
184 				fsopts->curinode++;
185 				cur->inode->ino = fsopts->curinode;
186 				cur->parent = parent;
187 			}
188 		}
189 
190 		if (cur->inode->flags & FI_WRITTEN) {
191 			continue;	// hard link
192 		}
193 		cur->inode->flags |= FI_WRITTEN;
194 
195 		if (cur->child) {
196 			struct denode *de;
197 			if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
198 				warn("msdosfs_mkdire %s", pbuf);
199 				return -1;
200 			}
201 			if (msdos_populate_dir(pbuf, de, cur->child, cur,
202 			    fsopts) == -1) {
203 				warn("msdos_populate_dir %s", pbuf);
204 				return -1;
205 			}
206 			continue;
207 		} else if (!S_ISREG(cur->type)) {
208 			warnx("skipping non-regular file %s/%s", cur->path,
209 			    cur->name);
210 			continue;
211 		}
212 		if (msdosfs_mkfile(pbuf, dir, cur) == NULL) {
213 			warn("msdosfs_mkfile %s", pbuf);
214 			return -1;
215 		}
216 	}
217 	return 0;
218 }
219