1 /* $NetBSD: newfs_v7fs.c,v 1.7 2026/01/17 16:42:15 rillig Exp $ */ 2 3 /*- 4 * Copyright (c) 2004, 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 #include <sys/cdefs.h> 33 #ifndef lint 34 __RCSID("$NetBSD: newfs_v7fs.c,v 1.7 2026/01/17 16:42:15 rillig Exp $"); 35 #endif /* not lint */ 36 37 #include <sys/types.h> 38 #include <sys/param.h> 39 #include <sys/disklabel.h> 40 #include <sys/ioctl.h> 41 #include <sys/stat.h> 42 43 #include <err.h> 44 #include <endian.h> 45 #include <stdio.h> 46 #include <string.h> 47 #include <stdlib.h> 48 #include <unistd.h> 49 #include <fcntl.h> 50 51 #include <fs/v7fs/v7fs.h> 52 #include "v7fs_impl.h" 53 #include "progress.h" 54 #include "newfs_v7fs.h" 55 56 static void usage(void) __dead; 57 static bool progress_bar_enable = false; 58 int v7fs_newfs_verbose = 3; /* newfs compatible */ 59 60 int 61 main(int argc, char **argv) 62 { 63 const char *device; 64 struct disklabel d; 65 struct partition *p; 66 struct stat st; 67 uint32_t partsize; 68 int Fflag, Zflag; 69 int part; 70 int fd, ch; 71 int endian = _BYTE_ORDER; 72 int32_t maxfile = 0; 73 74 if (argc < 2) 75 usage(); 76 77 Fflag = Zflag = partsize = 0; 78 while ((ch = getopt(argc, argv, "FZs:n:B:V:")) != -1) { 79 switch (ch) { 80 case 'V': 81 v7fs_newfs_verbose = atoi(optarg); 82 break; 83 case 'F': 84 Fflag = 1; 85 break; 86 case 's': 87 partsize = atoi(optarg); 88 break; 89 case 'n': 90 maxfile = atoi(optarg); 91 break; 92 case 'Z': 93 Zflag = 1; 94 break; 95 case 'B': 96 switch (optarg[0]) { 97 case 'l': 98 endian = _LITTLE_ENDIAN; 99 break; 100 case 'b': 101 endian = _BIG_ENDIAN; 102 break; 103 case 'p': 104 endian = _PDP_ENDIAN; 105 break; 106 } 107 break; 108 default: 109 usage(); 110 /*NOTREACHED*/ 111 } 112 } 113 argc -= optind; 114 argv += optind; 115 116 if (argc != 1) 117 usage(); 118 device = argv[0]; 119 120 progress_bar_enable = v7fs_newfs_verbose > 1; 121 122 123 if (progress_bar_enable) { 124 progress_switch(progress_bar_enable); 125 progress_init(); 126 progress(&(struct progress_arg){ .cdev = device }); 127 } 128 129 if (!Fflag) { 130 if ((fd = open(device, O_RDWR)) == -1) { 131 err(EXIT_FAILURE, "%s", device); 132 } 133 if (fstat(fd, &st) != 0) { 134 goto err_exit; 135 } 136 if (!S_ISCHR(st.st_mode)) { 137 warnx("not a raw device"); 138 } 139 140 part = DISKPART(st.st_rdev); 141 142 if (ioctl(fd, DIOCGDINFO, &d) == -1) { 143 goto err_exit; 144 } 145 p = &d.d_partitions[part]; 146 if (v7fs_newfs_verbose) { 147 printf("partition=%d size=%d offset=%d fstype=%d" 148 " secsize=%d\n", part, p->p_size, p->p_offset, 149 p->p_fstype, d.d_secsize); 150 } 151 if (p->p_fstype != FS_V7) { 152 warnx("not a Version 7 partition"); 153 goto err_exit; 154 } 155 partsize = p->p_size; 156 } else { 157 off_t filesize; 158 uint8_t zbuf[8192] = {0, }; 159 160 if (partsize == 0) { 161 errx(EXIT_FAILURE, "-F requires -s"); 162 } 163 164 filesize = partsize << V7FS_BSHIFT; 165 166 fd = open(device, O_RDWR|O_CREAT|O_TRUNC, 0666); 167 if (fd == -1) { 168 err(EXIT_FAILURE, "%s", device); 169 } 170 171 if (Zflag) { 172 while (filesize > 0) { 173 size_t writenow = MIN(filesize, 174 (off_t)sizeof(zbuf)); 175 176 if ((size_t)write(fd, zbuf, writenow) != 177 writenow) { 178 err(EXIT_FAILURE, NULL); 179 } 180 filesize -= writenow; 181 } 182 } else { 183 if (lseek(fd, filesize - 1, SEEK_SET) == -1) { 184 goto err_exit; 185 } 186 if (write(fd, zbuf, 1) != 1) { 187 goto err_exit; 188 } 189 if (lseek(fd, 0, SEEK_SET) == -1) { 190 goto err_exit; 191 } 192 } 193 } 194 195 if (v7fs_newfs(&(struct v7fs_mount_device) 196 { .device.fd = fd, .endian = endian, .sectors = partsize }, 197 maxfile) != 0) 198 goto err_exit; 199 200 close(fd); 201 202 return EXIT_SUCCESS; 203 err_exit: 204 close(fd); 205 err(EXIT_FAILURE, NULL); 206 } 207 208 void 209 progress(const struct progress_arg *p) 210 { 211 static struct progress_arg Progress; 212 static char cdev[32]; 213 static char label[32]; 214 215 if (!progress_bar_enable) 216 return; 217 218 if (p) { 219 Progress = *p; 220 if (p->cdev) 221 strcpy(cdev, p->cdev); 222 if (p->label) 223 strcpy(label, p->label); 224 } 225 226 if (!Progress.tick) 227 return; 228 if (++Progress.cnt > Progress.tick) { 229 Progress.cnt = 0; 230 Progress.total++; 231 progress_bar(cdev, label, Progress.total, PROGRESS_BAR_GRANULE); 232 } 233 } 234 235 static void 236 usage(void) 237 { 238 239 (void)fprintf(stderr, "usage: \n%s [-FZ] [-B byte-order]" 240 " [-n inodes] [-s sectors] [-V verbose] special\n", getprogname()); 241 242 exit(EXIT_FAILURE); 243 } 244