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