1 /* $NetBSD: msdos.c,v 1.15 2015/10/16 16:40:02 christos 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.15 2015/10/16 16:40:02 christos 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/denode.h> 59 #include "makefs.h" 60 #include "msdos.h" 61 #include "mkfs_msdos.h" 62 63 static int msdos_populate_dir(const char *, struct denode *, fsnode *, 64 fsnode *, fsinfo_t *); 65 66 void 67 msdos_prep_opts(fsinfo_t *fsopts) 68 { 69 struct msdos_options *msdos_opt = ecalloc(1, sizeof(*msdos_opt)); 70 const option_t msdos_options[] = { 71 #define AOPT(_opt, _type, _name, _min, _desc) { \ 72 .letter = _opt, \ 73 .name = # _name, \ 74 .type = _min == -1 ? OPT_STRPTR : \ 75 (_min == -2 ? OPT_BOOL : \ 76 (sizeof(_type) == 1 ? OPT_INT8 : \ 77 (sizeof(_type) == 2 ? OPT_INT16 : \ 78 (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))), \ 79 .value = &msdos_opt->_name, \ 80 .minimum = _min, \ 81 .maximum = sizeof(_type) == 1 ? 0xff : \ 82 (sizeof(_type) == 2 ? 0xffff : \ 83 (sizeof(_type) == 4 ? 0xffffffff : 0xffffffffffffffffLL)), \ 84 .desc = _desc, \ 85 }, 86 ALLOPTS 87 #undef AOPT 88 { .name = NULL } 89 }; 90 91 fsopts->fs_specific = msdos_opt; 92 fsopts->fs_options = copy_opts(msdos_options); 93 } 94 95 void 96 msdos_cleanup_opts(fsinfo_t *fsopts) 97 { 98 free(fsopts->fs_specific); 99 free(fsopts->fs_options); 100 } 101 102 int 103 msdos_parse_opts(const char *option, fsinfo_t *fsopts) 104 { 105 struct msdos_options *msdos_opt = fsopts->fs_specific; 106 option_t *msdos_options = fsopts->fs_options; 107 108 int rv; 109 110 assert(option != NULL); 111 assert(fsopts != NULL); 112 assert(msdos_opt != NULL); 113 114 if (debug & DEBUG_FS_PARSE_OPTS) 115 printf("msdos_parse_opts: got `%s'\n", option); 116 117 rv = set_option(msdos_options, option, NULL, 0); 118 if (rv == -1) 119 return rv; 120 121 if (strcmp(msdos_options[rv].name, "volume_id") == 0) 122 msdos_opt->volume_id_set = 1; 123 else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0) 124 msdos_opt->media_descriptor_set = 1; 125 else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0) 126 msdos_opt->hidden_sectors_set = 1; 127 return 1; 128 } 129 130 131 void 132 msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts) 133 { 134 struct msdos_options *msdos_opt = fsopts->fs_specific; 135 struct vnode vp, rootvp; 136 struct timeval start; 137 struct msdosfsmount *pmp; 138 139 assert(image != NULL); 140 assert(dir != NULL); 141 assert(root != NULL); 142 assert(fsopts != NULL); 143 144 /* 145 * XXX: pick up other options from the msdos specific ones? 146 * Is minsize right here? 147 */ 148 msdos_opt->create_size = MAX(msdos_opt->create_size, fsopts->minsize); 149 msdos_opt->offset = fsopts->offset; 150 if (msdos_opt->bytes_per_sector == 0) { 151 if (fsopts->sectorsize == -1) 152 fsopts->sectorsize = 512; 153 msdos_opt->bytes_per_sector = fsopts->sectorsize; 154 } else if (fsopts->sectorsize == -1) { 155 fsopts->sectorsize = msdos_opt->bytes_per_sector; 156 } else if (fsopts->sectorsize != msdos_opt->bytes_per_sector) { 157 err(1, "inconsistent sectorsize -S %u" 158 "!= -o bytes_per_sector %u", 159 fsopts->sectorsize, msdos_opt->bytes_per_sector); 160 } 161 162 /* create image */ 163 printf("Creating `%s'\n", image); 164 TIMER_START(start); 165 if (mkfs_msdos(image, NULL, msdos_opt) == -1) 166 return; 167 TIMER_RESULTS(start, "mkfs_msdos"); 168 169 fsopts->fd = open(image, O_RDWR); 170 vp.fs = fsopts; 171 172 if ((pmp = msdosfs_mount(&vp, 0)) == NULL) 173 err(1, "msdosfs_mount"); 174 175 if (msdosfs_root(pmp, &rootvp) != 0) 176 err(1, "msdosfs_root"); 177 178 if (debug & DEBUG_FS_MAKEFS) 179 printf("msdos_makefs: image %s directory %s root %p\n", 180 image, dir, root); 181 182 /* populate image */ 183 printf("Populating `%s'\n", image); 184 TIMER_START(start); 185 if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1) 186 errx(1, "Image file `%s' not created.", image); 187 TIMER_RESULTS(start, "msdos_populate_dir"); 188 189 if (debug & DEBUG_FS_MAKEFS) 190 putchar('\n'); 191 192 /* ensure no outstanding buffers remain */ 193 if (debug & DEBUG_FS_MAKEFS) 194 bcleanup(); 195 196 printf("Image `%s' complete\n", image); 197 } 198 199 static int 200 msdos_populate_dir(const char *path, struct denode *dir, fsnode *root, 201 fsnode *parent, fsinfo_t *fsopts) 202 { 203 fsnode *cur; 204 char pbuf[MAXPATHLEN]; 205 206 assert(dir != NULL); 207 assert(root != NULL); 208 assert(fsopts != NULL); 209 210 for (cur = root->next; cur != NULL; cur = cur->next) { 211 if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path, 212 cur->name) >= sizeof(pbuf)) { 213 warnx("path %s too long", pbuf); 214 return -1; 215 } 216 217 if ((cur->inode->flags & FI_ALLOCATED) == 0) { 218 cur->inode->flags |= FI_ALLOCATED; 219 if (cur != root) { 220 fsopts->curinode++; 221 cur->inode->ino = fsopts->curinode; 222 cur->parent = parent; 223 } 224 } 225 226 if (cur->inode->flags & FI_WRITTEN) { 227 continue; // hard link 228 } 229 cur->inode->flags |= FI_WRITTEN; 230 231 if (cur->child) { 232 struct denode *de; 233 if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) { 234 warn("msdosfs_mkdire %s", pbuf); 235 return -1; 236 } 237 if (msdos_populate_dir(pbuf, de, cur->child, cur, 238 fsopts) == -1) { 239 warn("msdos_populate_dir %s", pbuf); 240 return -1; 241 } 242 continue; 243 } else if (!S_ISREG(cur->type)) { 244 warnx("skipping non-regular file %s/%s", cur->path, 245 cur->name); 246 continue; 247 } 248 if (msdosfs_mkfile(pbuf, dir, cur) == NULL) { 249 warn("msdosfs_mkfile %s", pbuf); 250 return -1; 251 } 252 } 253 return 0; 254 } 255