1 /* $NetBSD: v7fs.c,v 1.9 2025/12/21 23:55:14 nia Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 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 32 #if HAVE_NBTOOL_CONFIG_H 33 #include "nbtool_config.h" 34 #endif 35 36 #include <sys/cdefs.h> 37 #if defined(__RCSID) && !defined(__lint) 38 __RCSID("$NetBSD: v7fs.c,v 1.9 2025/12/21 23:55:14 nia Exp $"); 39 #endif /* !__lint */ 40 41 #if defined(HAVE_SYS_ENDIAN_H) || !defined(HAVE_NBTOOL_CONFIG_H) 42 #include <sys/endian.h> 43 #endif 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <unistd.h> 47 #include <string.h> 48 #include <fcntl.h> 49 #include <util.h> 50 51 #include "makefs.h" 52 #include "v7fs.h" 53 #include "v7fs_impl.h" 54 #include "v7fs_makefs.h" 55 #include "newfs_v7fs.h" 56 57 58 #ifndef HAVE_NBTOOL_CONFIG_H 59 #include "progress.h" 60 static bool progress_bar_enable; 61 #endif 62 int v7fs_newfs_verbose; 63 64 void 65 v7fs_prep_opts(fsinfo_t *fsopts) 66 { 67 v7fs_opt_t *v7fs_opts = ecalloc(1, sizeof(*v7fs_opts)); 68 const option_t v7fs_options[] = { 69 { 'p', "pdp", &v7fs_opts->pdp_endian, OPT_INT32, false, true, 70 "PDP endian" }, 71 { 'P', "progress", &v7fs_opts->progress, OPT_INT32, false, true, 72 "Progress bar" }, 73 { .name = NULL } 74 }; 75 76 fsopts->fs_specific = v7fs_opts; 77 fsopts->fs_options = copy_opts(v7fs_options); 78 } 79 80 void 81 v7fs_cleanup_opts(fsinfo_t *fsopts) 82 { 83 free(fsopts->fs_specific); 84 free(fsopts->fs_options); 85 } 86 87 int 88 v7fs_parse_opts(const char *option, fsinfo_t *fsopts) 89 { 90 91 return set_option_var(fsopts->fs_options, option, "1", NULL, 0) != -1; 92 } 93 94 void 95 v7fs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts) 96 { 97 struct v7fs_mount_device v7fs_mount; 98 int fd, endian, error = 1; 99 v7fs_opt_t *v7fs_opts = fsopts->fs_specific; 100 101 v7fs_newfs_verbose = debug; 102 #ifndef HAVE_NBTOOL_CONFIG_H 103 if ((progress_bar_enable = v7fs_opts->progress)) { 104 progress_switch(progress_bar_enable); 105 progress_init(); 106 progress(&(struct progress_arg){ .cdev = image }); 107 } 108 #endif 109 110 /* Determine filesystem image size */ 111 v7fs_estimate(dir, root, fsopts); 112 printf("Calculated size of `%s': %lld bytes, %ld inodes\n", 113 image, (long long)fsopts->size, (long)fsopts->inodes); 114 115 if ((fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) { 116 err(EXIT_FAILURE, "%s", image); 117 } 118 if (lseek(fd, fsopts->size - 1, SEEK_SET) == -1) { 119 goto err_exit; 120 } 121 if (write(fd, &fd, 1) != 1) { 122 goto err_exit; 123 } 124 if (lseek(fd, 0, SEEK_SET) == -1) { 125 goto err_exit; 126 } 127 fsopts->fd = fd; 128 v7fs_mount.device.fd = fd; 129 130 #if !defined BYTE_ORDER 131 #error 132 #endif 133 #if BYTE_ORDER == LITTLE_ENDIAN 134 if (fsopts->needswap) 135 endian = BIG_ENDIAN; 136 else 137 endian = LITTLE_ENDIAN; 138 #else 139 if (fsopts->needswap) 140 endian = LITTLE_ENDIAN; 141 else 142 endian = BIG_ENDIAN; 143 #endif 144 if (v7fs_opts->pdp_endian) { 145 endian = PDP_ENDIAN; 146 } 147 148 v7fs_mount.endian = endian; 149 v7fs_mount.sectors = fsopts->size >> V7FS_BSHIFT; 150 if (v7fs_newfs(&v7fs_mount, fsopts->inodes) != 0) { 151 goto err_exit; 152 } 153 154 if (v7fs_populate(dir, root, fsopts, &v7fs_mount) != 0) { 155 error = 2; /* some files couldn't add */ 156 goto err_exit; 157 } 158 159 close(fd); 160 return; 161 162 err_exit: 163 close(fd); 164 err(error, "%s", image); 165 } 166 167 void 168 progress(const struct progress_arg *p) 169 { 170 #ifndef HAVE_NBTOOL_CONFIG_H 171 static struct progress_arg Progress; 172 static char cdev[32]; 173 static char label[32]; 174 175 if (!progress_bar_enable) 176 return; 177 178 if (p) { 179 Progress = *p; 180 if (p->cdev) 181 strcpy(cdev, p->cdev); 182 if (p->label) 183 strcpy(label, p->label); 184 } 185 186 if (!Progress.tick) 187 return; 188 if (++Progress.cnt > Progress.tick) { 189 Progress.cnt = 0; 190 Progress.total++; 191 progress_bar(cdev, label, Progress.total, PROGRESS_BAR_GRANULE); 192 } 193 #endif 194 } 195