xref: /freebsd-src/usr.sbin/makefs/msdos.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
10661ef2aSEd Maste /*	$NetBSD: msdos.c,v 1.20 2017/04/14 15:40:35 christos Exp $	*/
20661ef2aSEd Maste 
30661ef2aSEd Maste /*-
40661ef2aSEd Maste  * Copyright (c) 2013 The NetBSD Foundation, Inc.
50661ef2aSEd Maste  * All rights reserved.
60661ef2aSEd Maste  *
70661ef2aSEd Maste  * This code is derived from software contributed to The NetBSD Foundation
80661ef2aSEd Maste  * by Christos Zoulas.
90661ef2aSEd Maste  *
100661ef2aSEd Maste  * Redistribution and use in source and binary forms, with or without
110661ef2aSEd Maste  * modification, are permitted provided that the following conditions
120661ef2aSEd Maste  * are met:
130661ef2aSEd Maste  * 1. Redistributions of source code must retain the above copyright
140661ef2aSEd Maste  *    notice, this list of conditions and the following disclaimer.
150661ef2aSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
160661ef2aSEd Maste  *    notice, this list of conditions and the following disclaimer in the
170661ef2aSEd Maste  *    documentation and/or other materials provided with the distribution.
180661ef2aSEd Maste  *
190661ef2aSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
200661ef2aSEd Maste  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
210661ef2aSEd Maste  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220661ef2aSEd Maste  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
230661ef2aSEd Maste  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
240661ef2aSEd Maste  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
250661ef2aSEd Maste  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260661ef2aSEd Maste  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
270661ef2aSEd Maste  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
280661ef2aSEd Maste  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
290661ef2aSEd Maste  * POSSIBILITY OF SUCH DAMAGE.
300661ef2aSEd Maste  */
310661ef2aSEd Maste #if HAVE_NBTOOL_CONFIG_H
320661ef2aSEd Maste #include "nbtool_config.h"
330661ef2aSEd Maste #endif
340661ef2aSEd Maste 
350661ef2aSEd Maste #include <sys/param.h>
360661ef2aSEd Maste 
370661ef2aSEd Maste #if !HAVE_NBTOOL_CONFIG_H
380661ef2aSEd Maste #include <sys/mount.h>
390661ef2aSEd Maste #endif
400661ef2aSEd Maste 
410661ef2aSEd Maste #include <assert.h>
420661ef2aSEd Maste #include <errno.h>
430661ef2aSEd Maste #include <fcntl.h>
440661ef2aSEd Maste #include <stdarg.h>
450661ef2aSEd Maste #include <stdio.h>
460661ef2aSEd Maste #include <stdlib.h>
470661ef2aSEd Maste #include <string.h>
4898dc8da5SEd Maste #include <stdint.h>
490661ef2aSEd Maste #include <unistd.h>
500661ef2aSEd Maste #include <dirent.h>
510661ef2aSEd Maste #include <util.h>
520661ef2aSEd Maste 
5398dc8da5SEd Maste #include <mkfs_msdos.h>
5498dc8da5SEd Maste #include <fs/msdosfs/bpb.h>
55476b0ab7SEd Maste #include "msdos/direntry.h"
56*ba2cfa80SAlex Richardson #include "msdos/denode.h"
57840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h>
580661ef2aSEd Maste 
59d485c77fSKonstantin Belousov #undef clrbuf
60d485c77fSKonstantin Belousov #include "ffs/buf.h"
61d485c77fSKonstantin Belousov #include "makefs.h"
62d485c77fSKonstantin Belousov #include "msdos.h"
63d485c77fSKonstantin Belousov 
640661ef2aSEd Maste static int msdos_populate_dir(const char *, struct denode *, fsnode *,
650661ef2aSEd Maste     fsnode *, fsinfo_t *);
660661ef2aSEd Maste 
670661ef2aSEd Maste struct msdos_options_ex {
680661ef2aSEd Maste 	struct msdos_options options;
690661ef2aSEd Maste };
700661ef2aSEd Maste 
710661ef2aSEd Maste void
msdos_prep_opts(fsinfo_t * fsopts)720661ef2aSEd Maste msdos_prep_opts(fsinfo_t *fsopts)
730661ef2aSEd Maste {
740661ef2aSEd Maste 	struct msdos_options_ex *msdos_opt = ecalloc(1, sizeof(*msdos_opt));
750661ef2aSEd Maste 	const option_t msdos_options[] = {
760661ef2aSEd Maste #define AOPT(_opt, _type, _name, _min, _desc) {				\
770661ef2aSEd Maste 	.letter = _opt,							\
780661ef2aSEd Maste 	.name = # _name,						\
790661ef2aSEd Maste 	.type = _min == -1 ? OPT_STRPTR :				\
800661ef2aSEd Maste 	    (_min == -2 ? OPT_BOOL :					\
810661ef2aSEd Maste 	    (sizeof(_type) == 1 ? OPT_INT8 :				\
820661ef2aSEd Maste 	    (sizeof(_type) == 2 ? OPT_INT16 :				\
830661ef2aSEd Maste 	    (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))),		\
840661ef2aSEd Maste 	.value = &msdos_opt->options._name,				\
850661ef2aSEd Maste 	.minimum = _min,						\
8698dc8da5SEd Maste 	.maximum = sizeof(_type) == 1 ? UINT8_MAX :			\
8798dc8da5SEd Maste 	    (sizeof(_type) == 2 ? UINT16_MAX :				\
8898dc8da5SEd Maste 	    (sizeof(_type) == 4 ? UINT32_MAX : INT64_MAX)),		\
890661ef2aSEd Maste 	.desc = _desc,							\
900661ef2aSEd Maste },
910661ef2aSEd Maste ALLOPTS
920661ef2aSEd Maste #undef AOPT
930661ef2aSEd Maste 		{ .name = NULL }
940661ef2aSEd Maste 	};
950661ef2aSEd Maste 
960661ef2aSEd Maste 	fsopts->fs_specific = msdos_opt;
970661ef2aSEd Maste 	fsopts->fs_options = copy_opts(msdos_options);
980661ef2aSEd Maste }
990661ef2aSEd Maste 
1000661ef2aSEd Maste void
msdos_cleanup_opts(fsinfo_t * fsopts)1010661ef2aSEd Maste msdos_cleanup_opts(fsinfo_t *fsopts)
1020661ef2aSEd Maste {
1030661ef2aSEd Maste 	free(fsopts->fs_specific);
1040661ef2aSEd Maste 	free(fsopts->fs_options);
1050661ef2aSEd Maste }
1060661ef2aSEd Maste 
1070661ef2aSEd Maste int
msdos_parse_opts(const char * option,fsinfo_t * fsopts)1080661ef2aSEd Maste msdos_parse_opts(const char *option, fsinfo_t *fsopts)
1090661ef2aSEd Maste {
1100661ef2aSEd Maste 	struct msdos_options *msdos_opt = fsopts->fs_specific;
1110661ef2aSEd Maste 	option_t *msdos_options = fsopts->fs_options;
1120661ef2aSEd Maste 	int rv;
1130661ef2aSEd Maste 
1140661ef2aSEd Maste 	assert(option != NULL);
1150661ef2aSEd Maste 	assert(fsopts != NULL);
1160661ef2aSEd Maste 	assert(msdos_opt != NULL);
1170661ef2aSEd Maste 
1180661ef2aSEd Maste 	if (debug & DEBUG_FS_PARSE_OPTS)
1190661ef2aSEd Maste 		printf("msdos_parse_opts: got `%s'\n", option);
1200661ef2aSEd Maste 
1210661ef2aSEd Maste 	rv = set_option(msdos_options, option, NULL, 0);
1220661ef2aSEd Maste 	if (rv == -1)
1230661ef2aSEd Maste 		return rv;
1240661ef2aSEd Maste 
1250661ef2aSEd Maste 	if (strcmp(msdos_options[rv].name, "volume_id") == 0)
1260661ef2aSEd Maste 		msdos_opt->volume_id_set = 1;
1270661ef2aSEd Maste 	else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0)
1280661ef2aSEd Maste 		msdos_opt->media_descriptor_set = 1;
1290661ef2aSEd Maste 	else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0)
1300661ef2aSEd Maste 		msdos_opt->hidden_sectors_set = 1;
1310661ef2aSEd Maste 
1320661ef2aSEd Maste 	if (stampst.st_ino) {
1330661ef2aSEd Maste 		msdos_opt->timestamp_set = 1;
1340661ef2aSEd Maste 		msdos_opt->timestamp = stampst.st_mtime;
1350661ef2aSEd Maste 	}
1360661ef2aSEd Maste 
1370661ef2aSEd Maste 	return 1;
1380661ef2aSEd Maste }
1390661ef2aSEd Maste 
1400661ef2aSEd Maste 
1410661ef2aSEd Maste void
msdos_makefs(const char * image,const char * dir,fsnode * root,fsinfo_t * fsopts)1420661ef2aSEd Maste msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
1430661ef2aSEd Maste {
1440661ef2aSEd Maste 	struct msdos_options_ex *msdos_opt = fsopts->fs_specific;
145d485c77fSKonstantin Belousov 	struct m_vnode vp, rootvp;
1460661ef2aSEd Maste 	struct timeval start;
1470661ef2aSEd Maste 	struct msdosfsmount *pmp;
1480661ef2aSEd Maste 
1490661ef2aSEd Maste 	assert(image != NULL);
1500661ef2aSEd Maste 	assert(dir != NULL);
1510661ef2aSEd Maste 	assert(root != NULL);
1520661ef2aSEd Maste 	assert(fsopts != NULL);
1530661ef2aSEd Maste 
1540661ef2aSEd Maste 	fsopts->size = fsopts->maxsize;
1550661ef2aSEd Maste 	msdos_opt->options.create_size = MAX(msdos_opt->options.create_size,
1560661ef2aSEd Maste 	    fsopts->offset + fsopts->size);
15798dc8da5SEd Maste 	if (fsopts->offset > 0)
1580661ef2aSEd Maste 		msdos_opt->options.offset = fsopts->offset;
1590661ef2aSEd Maste 	if (msdos_opt->options.bytes_per_sector == 0) {
1600661ef2aSEd Maste 		if (fsopts->sectorsize == -1)
1610661ef2aSEd Maste 			fsopts->sectorsize = 512;
1620661ef2aSEd Maste 		msdos_opt->options.bytes_per_sector = fsopts->sectorsize;
1630661ef2aSEd Maste 	} else if (fsopts->sectorsize == -1) {
1640661ef2aSEd Maste 		fsopts->sectorsize = msdos_opt->options.bytes_per_sector;
1650661ef2aSEd Maste 	} else if (fsopts->sectorsize != msdos_opt->options.bytes_per_sector) {
1660661ef2aSEd Maste 		err(1, "inconsistent sectorsize -S %u"
1670661ef2aSEd Maste 		    "!= -o bytes_per_sector %u",
1680661ef2aSEd Maste 		    fsopts->sectorsize, msdos_opt->options.bytes_per_sector);
1690661ef2aSEd Maste 	}
1700661ef2aSEd Maste 
1710661ef2aSEd Maste 	/* create image */
1720661ef2aSEd Maste 	printf("Creating `%s'\n", image);
1730661ef2aSEd Maste 	TIMER_START(start);
1740661ef2aSEd Maste 	if (mkfs_msdos(image, NULL, &msdos_opt->options) == -1)
1750661ef2aSEd Maste 		return;
1760661ef2aSEd Maste 	TIMER_RESULTS(start, "mkfs_msdos");
1770661ef2aSEd Maste 
1780661ef2aSEd Maste 	fsopts->fd = open(image, O_RDWR);
1790661ef2aSEd Maste 	vp.fs = fsopts;
1800661ef2aSEd Maste 
181d485c77fSKonstantin Belousov 	if ((pmp = m_msdosfs_mount(&vp)) == NULL)
1820661ef2aSEd Maste 		err(1, "msdosfs_mount");
1830661ef2aSEd Maste 
1840661ef2aSEd Maste 	if (msdosfs_root(pmp, &rootvp) != 0)
1850661ef2aSEd Maste 		err(1, "msdosfs_root");
1860661ef2aSEd Maste 
1870661ef2aSEd Maste 	if (debug & DEBUG_FS_MAKEFS)
1880661ef2aSEd Maste 		printf("msdos_makefs: image %s directory %s root %p\n",
1890661ef2aSEd Maste 		    image, dir, root);
1900661ef2aSEd Maste 
1910661ef2aSEd Maste 	/* populate image */
1920661ef2aSEd Maste 	printf("Populating `%s'\n", image);
1930661ef2aSEd Maste 	TIMER_START(start);
1940661ef2aSEd Maste 	if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
1950661ef2aSEd Maste 		errx(1, "Image file `%s' not created.", image);
1960661ef2aSEd Maste 	TIMER_RESULTS(start, "msdos_populate_dir");
1970661ef2aSEd Maste 
1988651679aSXin LI 	if (msdosfs_fsiflush(pmp) != 0)
1998651679aSXin LI 		errx(1, "Unable to update FSInfo block.");
2000661ef2aSEd Maste 	if (debug & DEBUG_FS_MAKEFS)
2010661ef2aSEd Maste 		putchar('\n');
2020661ef2aSEd Maste 
2030661ef2aSEd Maste 	/* ensure no outstanding buffers remain */
2040661ef2aSEd Maste 	if (debug & DEBUG_FS_MAKEFS)
2050661ef2aSEd Maste 		bcleanup();
2060661ef2aSEd Maste 
2070661ef2aSEd Maste 	printf("Image `%s' complete\n", image);
2080661ef2aSEd Maste }
2090661ef2aSEd Maste 
2100661ef2aSEd Maste static int
msdos_populate_dir(const char * path,struct denode * dir,fsnode * root,fsnode * parent,fsinfo_t * fsopts)2110661ef2aSEd Maste msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
2120661ef2aSEd Maste     fsnode *parent, fsinfo_t *fsopts)
2130661ef2aSEd Maste {
2140661ef2aSEd Maste 	fsnode *cur;
2150661ef2aSEd Maste 	char pbuf[MAXPATHLEN];
2160661ef2aSEd Maste 
2170661ef2aSEd Maste 	assert(dir != NULL);
2180661ef2aSEd Maste 	assert(root != NULL);
2190661ef2aSEd Maste 	assert(fsopts != NULL);
2200661ef2aSEd Maste 
2210661ef2aSEd Maste 	for (cur = root->next; cur != NULL; cur = cur->next) {
2220661ef2aSEd Maste 		if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
2230661ef2aSEd Maste 		    cur->name) >= sizeof(pbuf)) {
2240661ef2aSEd Maste 			warnx("path %s too long", pbuf);
2250661ef2aSEd Maste 			return -1;
2260661ef2aSEd Maste 		}
2270661ef2aSEd Maste 
2280661ef2aSEd Maste 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
2290661ef2aSEd Maste 			cur->inode->flags |= FI_ALLOCATED;
2300661ef2aSEd Maste 			if (cur != root) {
2310661ef2aSEd Maste 				fsopts->curinode++;
2320661ef2aSEd Maste 				cur->inode->ino = fsopts->curinode;
2330661ef2aSEd Maste 				cur->parent = parent;
2340661ef2aSEd Maste 			}
2350661ef2aSEd Maste 		}
2360661ef2aSEd Maste 
2370661ef2aSEd Maste 		if (cur->inode->flags & FI_WRITTEN) {
2380661ef2aSEd Maste 			continue;	// hard link
2390661ef2aSEd Maste 		}
2400661ef2aSEd Maste 		cur->inode->flags |= FI_WRITTEN;
2410661ef2aSEd Maste 
2420661ef2aSEd Maste 		if (cur->child) {
2430661ef2aSEd Maste 			struct denode *de;
2440661ef2aSEd Maste 			if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
2450661ef2aSEd Maste 				warn("msdosfs_mkdire %s", pbuf);
2460661ef2aSEd Maste 				return -1;
2470661ef2aSEd Maste 			}
2480661ef2aSEd Maste 			if (msdos_populate_dir(pbuf, de, cur->child, cur,
2490661ef2aSEd Maste 			    fsopts) == -1) {
2500661ef2aSEd Maste 				warn("msdos_populate_dir %s", pbuf);
2510661ef2aSEd Maste 				return -1;
2520661ef2aSEd Maste 			}
2530661ef2aSEd Maste 			continue;
2540661ef2aSEd Maste 		} else if (!S_ISREG(cur->type)) {
2550661ef2aSEd Maste 			warnx("skipping non-regular file %s/%s", cur->path,
2560661ef2aSEd Maste 			    cur->name);
2570661ef2aSEd Maste 			continue;
2580661ef2aSEd Maste 		}
25962a3510fSAlex Richardson 		if (msdosfs_mkfile(cur->contents ? cur->contents : pbuf, dir,
26062a3510fSAlex Richardson 		    cur) == NULL) {
2610661ef2aSEd Maste 			warn("msdosfs_mkfile %s", pbuf);
2620661ef2aSEd Maste 			return -1;
2630661ef2aSEd Maste 		}
2640661ef2aSEd Maste 	}
2650661ef2aSEd Maste 	return 0;
2660661ef2aSEd Maste }
267