Home | History | Annotate | Line # | Download | only in makefs
msdos.c revision 1.22
      1  1.22   tsutsui /*	$NetBSD: msdos.c,v 1.22 2023/12/20 15:07:16 tsutsui Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*-
      4   1.1  christos  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5   1.1  christos  * All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  christos  * by Christos Zoulas.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  christos  *    documentation and/or other materials provided with the distribution.
     18   1.1  christos  *
     19   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  christos  */
     31   1.1  christos #if HAVE_NBTOOL_CONFIG_H
     32   1.1  christos #include "nbtool_config.h"
     33   1.1  christos #endif
     34   1.1  christos 
     35   1.1  christos #include <sys/cdefs.h>
     36   1.1  christos #if defined(__RCSID) && !defined(__lint)
     37  1.22   tsutsui __RCSID("$NetBSD: msdos.c,v 1.22 2023/12/20 15:07:16 tsutsui Exp $");
     38   1.1  christos #endif	/* !__lint */
     39   1.1  christos 
     40   1.1  christos #include <sys/param.h>
     41   1.1  christos 
     42   1.1  christos #if !HAVE_NBTOOL_CONFIG_H
     43   1.1  christos #include <sys/mount.h>
     44   1.1  christos #endif
     45   1.1  christos 
     46   1.1  christos #include <assert.h>
     47   1.1  christos #include <errno.h>
     48   1.1  christos #include <fcntl.h>
     49   1.1  christos #include <stdarg.h>
     50   1.1  christos #include <stdio.h>
     51   1.1  christos #include <stdlib.h>
     52   1.1  christos #include <string.h>
     53   1.1  christos #include <unistd.h>
     54   1.6  christos #include <dirent.h>
     55  1.10  christos #include <util.h>
     56   1.1  christos 
     57   1.6  christos #include <ffs/buf.h>
     58  1.16   mlelstv #include <fs/msdosfs/bpb.h>
     59   1.6  christos #include <fs/msdosfs/denode.h>
     60  1.16   mlelstv #include <fs/msdosfs/msdosfsmount.h>
     61   1.1  christos #include "makefs.h"
     62   1.6  christos #include "msdos.h"
     63   1.1  christos #include "mkfs_msdos.h"
     64   1.1  christos 
     65   1.6  christos static int msdos_populate_dir(const char *, struct denode *, fsnode *,
     66   1.6  christos     fsnode *, fsinfo_t *);
     67   1.6  christos 
     68  1.16   mlelstv struct msdos_options_ex {
     69  1.16   mlelstv 	struct msdos_options options;
     70  1.16   mlelstv 	bool utf8;
     71  1.16   mlelstv };
     72  1.16   mlelstv 
     73   1.1  christos void
     74   1.1  christos msdos_prep_opts(fsinfo_t *fsopts)
     75   1.1  christos {
     76  1.16   mlelstv 	struct msdos_options_ex *msdos_opt = ecalloc(1, sizeof(*msdos_opt));
     77  1.10  christos 	const option_t msdos_options[] = {
     78   1.1  christos #define AOPT(_opt, _type, _name, _min, _desc) { 			\
     79   1.1  christos 	.letter = _opt,							\
     80   1.1  christos 	.name = # _name,						\
     81   1.1  christos 	.type = _min == -1 ? OPT_STRPTR :				\
     82   1.1  christos 	    (_min == -2 ? OPT_BOOL :					\
     83   1.1  christos 	    (sizeof(_type) == 1 ? OPT_INT8 :				\
     84   1.1  christos 	    (sizeof(_type) == 2 ? OPT_INT16 :				\
     85   1.1  christos 	    (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))),		\
     86  1.16   mlelstv 	.value = &msdos_opt->options._name,				\
     87   1.1  christos 	.minimum = _min,						\
     88   1.1  christos 	.maximum = sizeof(_type) == 1 ? 0xff :				\
     89   1.1  christos 	    (sizeof(_type) == 2 ? 0xffff :				\
     90   1.1  christos 	    (sizeof(_type) == 4 ? 0xffffffff : 0xffffffffffffffffLL)),	\
     91   1.1  christos 	.desc = _desc,						\
     92   1.1  christos },
     93   1.1  christos ALLOPTS
     94  1.21  riastrad #undef AOPT
     95  1.16   mlelstv 		{ 'U', "utf8", &msdos_opt->utf8, OPT_BOOL,
     96  1.16   mlelstv 		  0, 1, "Use UTF8 names" },
     97   1.1  christos 		{ .name = NULL }
     98   1.1  christos 	};
     99  1.10  christos 
    100  1.10  christos 	fsopts->fs_specific = msdos_opt;
    101  1.10  christos 	fsopts->fs_options = copy_opts(msdos_options);
    102  1.10  christos }
    103  1.10  christos 
    104  1.10  christos void
    105  1.10  christos msdos_cleanup_opts(fsinfo_t *fsopts)
    106  1.10  christos {
    107  1.10  christos 	free(fsopts->fs_specific);
    108  1.10  christos 	free(fsopts->fs_options);
    109  1.10  christos }
    110  1.10  christos 
    111  1.10  christos int
    112  1.10  christos msdos_parse_opts(const char *option, fsinfo_t *fsopts)
    113  1.10  christos {
    114  1.10  christos 	struct msdos_options *msdos_opt = fsopts->fs_specific;
    115  1.10  christos 	option_t *msdos_options = fsopts->fs_options;
    116  1.10  christos 
    117   1.5  christos 	int rv;
    118   1.1  christos 
    119   1.1  christos 	assert(option != NULL);
    120   1.1  christos 	assert(fsopts != NULL);
    121   1.1  christos 	assert(msdos_opt != NULL);
    122   1.1  christos 
    123   1.1  christos 	if (debug & DEBUG_FS_PARSE_OPTS)
    124   1.1  christos 		printf("msdos_parse_opts: got `%s'\n", option);
    125   1.1  christos 
    126  1.11  christos 	rv = set_option(msdos_options, option, NULL, 0);
    127   1.5  christos 	if (rv == -1)
    128   1.3  christos 		return rv;
    129   1.3  christos 
    130   1.5  christos 	if (strcmp(msdos_options[rv].name, "volume_id") == 0)
    131   1.3  christos 		msdos_opt->volume_id_set = 1;
    132   1.5  christos 	else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0)
    133   1.3  christos 		msdos_opt->media_descriptor_set = 1;
    134   1.5  christos 	else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0)
    135   1.3  christos 		msdos_opt->hidden_sectors_set = 1;
    136  1.17  christos 
    137  1.18  christos 	if (stampst.st_ino) {
    138  1.18  christos 		msdos_opt->timestamp_set = 1;
    139  1.18  christos 		msdos_opt->timestamp = stampst.st_mtime;
    140  1.18  christos 	}
    141  1.17  christos 
    142   1.5  christos 	return 1;
    143   1.1  christos }
    144   1.1  christos 
    145   1.1  christos 
    146   1.1  christos void
    147   1.1  christos msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
    148   1.1  christos {
    149  1.16   mlelstv 	struct msdos_options_ex *msdos_opt = fsopts->fs_specific;
    150   1.6  christos 	struct vnode vp, rootvp;
    151   1.6  christos 	struct timeval	start;
    152   1.6  christos 	struct msdosfsmount *pmp;
    153  1.16   mlelstv 	uint32_t flags;
    154   1.6  christos 
    155   1.6  christos 	assert(image != NULL);
    156   1.6  christos 	assert(dir != NULL);
    157   1.6  christos 	assert(root != NULL);
    158   1.6  christos 	assert(fsopts != NULL);
    159   1.4  christos 
    160  1.20  christos 	fsopts->size = fsopts->maxsize;
    161  1.16   mlelstv 	msdos_opt->options.create_size = MAX(msdos_opt->options.create_size,
    162  1.20  christos 	    fsopts->offset + fsopts->size);
    163  1.16   mlelstv 	msdos_opt->options.offset = fsopts->offset;
    164  1.16   mlelstv 	if (msdos_opt->options.bytes_per_sector == 0) {
    165  1.13  christos 		if (fsopts->sectorsize == -1)
    166  1.12  christos 			fsopts->sectorsize = 512;
    167  1.16   mlelstv 		msdos_opt->options.bytes_per_sector = fsopts->sectorsize;
    168  1.13  christos 	} else if (fsopts->sectorsize == -1) {
    169  1.16   mlelstv 		fsopts->sectorsize = msdos_opt->options.bytes_per_sector;
    170  1.16   mlelstv 	} else if (fsopts->sectorsize != msdos_opt->options.bytes_per_sector) {
    171  1.12  christos 		err(1, "inconsistent sectorsize -S %u"
    172  1.21  riastrad 		    "!= -o bytes_per_sector %u",
    173  1.16   mlelstv 		    fsopts->sectorsize, msdos_opt->options.bytes_per_sector);
    174  1.12  christos 	}
    175   1.4  christos 
    176   1.6  christos 		/* create image */
    177   1.6  christos 	printf("Creating `%s'\n", image);
    178   1.6  christos 	TIMER_START(start);
    179  1.16   mlelstv 	if (mkfs_msdos(image, NULL, &msdos_opt->options) == -1)
    180  1.22   tsutsui 		errx(1, "Image file `%s' not created.", image);
    181   1.6  christos 	TIMER_RESULTS(start, "mkfs_msdos");
    182   1.6  christos 
    183  1.13  christos 	fsopts->fd = open(image, O_RDWR);
    184  1.13  christos 	vp.fs = fsopts;
    185   1.6  christos 
    186  1.16   mlelstv 	flags = 0;
    187  1.16   mlelstv 	if (msdos_opt->utf8)
    188  1.16   mlelstv 		flags |= MSDOSFSMNT_UTF8;
    189  1.16   mlelstv 
    190  1.16   mlelstv 	if ((pmp = msdosfs_mount(&vp, flags)) == NULL)
    191   1.6  christos 		err(1, "msdosfs_mount");
    192   1.1  christos 
    193   1.6  christos 	if (msdosfs_root(pmp, &rootvp) != 0)
    194   1.6  christos 		err(1, "msdosfs_root");
    195   1.1  christos 
    196   1.1  christos 	if (debug & DEBUG_FS_MAKEFS)
    197   1.1  christos 		printf("msdos_makefs: image %s directory %s root %p\n",
    198   1.1  christos 		    image, dir, root);
    199   1.1  christos 
    200   1.6  christos 		/* populate image */
    201   1.6  christos 	printf("Populating `%s'\n", image);
    202   1.1  christos 	TIMER_START(start);
    203   1.6  christos 	if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
    204  1.22   tsutsui 		errx(1, "Image file `%s' not populated.", image);
    205   1.6  christos 	TIMER_RESULTS(start, "msdos_populate_dir");
    206   1.1  christos 
    207   1.1  christos 	if (debug & DEBUG_FS_MAKEFS)
    208   1.1  christos 		putchar('\n');
    209   1.1  christos 
    210   1.1  christos 		/* ensure no outstanding buffers remain */
    211   1.1  christos 	if (debug & DEBUG_FS_MAKEFS)
    212   1.1  christos 		bcleanup();
    213   1.1  christos 
    214   1.1  christos 	printf("Image `%s' complete\n", image);
    215   1.6  christos }
    216   1.6  christos 
    217   1.6  christos static int
    218   1.6  christos msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
    219   1.6  christos     fsnode *parent, fsinfo_t *fsopts)
    220   1.6  christos {
    221   1.6  christos 	fsnode *cur;
    222   1.6  christos 	char pbuf[MAXPATHLEN];
    223   1.6  christos 
    224   1.6  christos 	assert(dir != NULL);
    225   1.6  christos 	assert(root != NULL);
    226  1.21  riastrad 	assert(fsopts != NULL);
    227  1.21  riastrad 
    228   1.6  christos 	for (cur = root->next; cur != NULL; cur = cur->next) {
    229   1.6  christos 		if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
    230   1.6  christos 		    cur->name) >= sizeof(pbuf)) {
    231   1.6  christos 			warnx("path %s too long", pbuf);
    232   1.6  christos 			return -1;
    233   1.6  christos 		}
    234   1.6  christos 
    235   1.6  christos 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
    236   1.6  christos 			cur->inode->flags |= FI_ALLOCATED;
    237   1.6  christos 			if (cur != root) {
    238   1.6  christos 				fsopts->curinode++;
    239   1.6  christos 				cur->inode->ino = fsopts->curinode;
    240   1.6  christos 				cur->parent = parent;
    241   1.6  christos 			}
    242   1.6  christos 		}
    243   1.6  christos 
    244   1.6  christos 		if (cur->inode->flags & FI_WRITTEN) {
    245   1.6  christos 			continue;	// hard link
    246   1.6  christos 		}
    247   1.6  christos 		cur->inode->flags |= FI_WRITTEN;
    248   1.6  christos 
    249   1.6  christos 		if (cur->child) {
    250   1.6  christos 			struct denode *de;
    251   1.9  christos 			if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
    252   1.9  christos 				warn("msdosfs_mkdire %s", pbuf);
    253   1.9  christos 				return -1;
    254   1.9  christos 			}
    255   1.9  christos 			if (msdos_populate_dir(pbuf, de, cur->child, cur,
    256   1.9  christos 			    fsopts) == -1) {
    257   1.9  christos 				warn("msdos_populate_dir %s", pbuf);
    258   1.9  christos 				return -1;
    259   1.9  christos 			}
    260   1.6  christos 			continue;
    261   1.6  christos 		} else if (!S_ISREG(cur->type)) {
    262   1.6  christos 			warnx("skipping non-regular file %s/%s", cur->path,
    263   1.6  christos 			    cur->name);
    264   1.6  christos 			continue;
    265   1.6  christos 		}
    266   1.9  christos 		if (msdosfs_mkfile(pbuf, dir, cur) == NULL) {
    267   1.9  christos 			warn("msdosfs_mkfile %s", pbuf);
    268   1.9  christos 			return -1;
    269   1.9  christos 		}
    270   1.6  christos 	}
    271   1.6  christos 	return 0;
    272   1.1  christos }
    273