1 1.15 christos /* $NetBSD: main.c,v 1.15 2025/02/23 20:47:19 christos Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2002 Marcel Moolenaar 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 11 1.1 christos * 1. Redistributions of source code must retain the above copyright 12 1.1 christos * notice, this list of conditions and the following disclaimer. 13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer in the 15 1.1 christos * documentation and/or other materials provided with the distribution. 16 1.1 christos * 17 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 1.1 christos * 28 1.1 christos * CRC32 code derived from work by Gary S. Brown. 29 1.1 christos */ 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 #ifdef __RCSID 37 1.15 christos __RCSID("$NetBSD: main.c,v 1.15 2025/02/23 20:47:19 christos Exp $"); 38 1.1 christos #endif 39 1.1 christos 40 1.1 christos #include <stdio.h> 41 1.1 christos #include <stdlib.h> 42 1.1 christos #include <unistd.h> 43 1.1 christos #include <err.h> 44 1.9 christos #include <errno.h> 45 1.9 christos #include <sys/stat.h> 46 1.9 christos #ifndef NBTOOL_CONFIG_H 47 1.9 christos #include <util.h> 48 1.9 christos #endif 49 1.1 christos 50 1.1 christos #include "map.h" 51 1.1 christos #include "gpt.h" 52 1.1 christos 53 1.15 christos static const struct gpt_cmd c_null = { 0 }; 54 1.3 christos 55 1.3 christos static const struct gpt_cmd *cmdsw[] = { 56 1.3 christos &c_add, 57 1.3 christos #ifndef HAVE_NBTOOL_CONFIG_H 58 1.3 christos &c_backup, 59 1.3 christos #endif 60 1.3 christos &c_biosboot, 61 1.3 christos &c_create, 62 1.3 christos &c_destroy, 63 1.3 christos &c_header, 64 1.3 christos &c_label, 65 1.3 christos &c_migrate, 66 1.3 christos &c_recover, 67 1.3 christos &c_remove, 68 1.3 christos &c_resize, 69 1.3 christos &c_resizedisk, 70 1.3 christos #ifndef HAVE_NBTOOL_CONFIG_H 71 1.3 christos &c_restore, 72 1.3 christos #endif 73 1.3 christos &c_set, 74 1.3 christos &c_show, 75 1.3 christos &c_type, 76 1.3 christos &c_unset, 77 1.13 jnemeth &c_uuid, 78 1.3 christos &c_null, 79 1.1 christos }; 80 1.1 christos 81 1.1 christos __dead static void 82 1.1 christos usage(void) 83 1.1 christos { 84 1.1 christos const char *p = getprogname(); 85 1.1 christos const char *f = 86 1.12 sevan "[-nrqv] [-m mediasize] [-s sectorsize] [-T timestamp]"; 87 1.3 christos size_t i; 88 1.1 christos 89 1.2 christos if (strcmp(p, "gpt") == 0) 90 1.2 christos fprintf(stderr, 91 1.5 christos "Usage: %s %s command device\n", p, f); 92 1.2 christos else 93 1.2 christos fprintf(stderr, 94 1.5 christos "Usage: %s %s device command\n", p, f); 95 1.3 christos fprintf(stderr, "Commands:\n"); 96 1.3 christos for (i = 0; i < __arraycount(cmdsw); i++) 97 1.3 christos gpt_usage("\t", cmdsw[i]); 98 1.3 christos exit(EXIT_FAILURE); 99 1.1 christos } 100 1.1 christos 101 1.1 christos static void 102 1.1 christos prefix(const char *cmd) 103 1.1 christos { 104 1.1 christos char *pfx; 105 1.1 christos 106 1.1 christos if (asprintf(&pfx, "%s %s", getprogname(), cmd) < 0) 107 1.1 christos pfx = NULL; 108 1.1 christos else 109 1.1 christos setprogname(pfx); 110 1.1 christos } 111 1.1 christos 112 1.9 christos static time_t 113 1.9 christos get_tstamp(const char *b) 114 1.9 christos { 115 1.9 christos struct stat st; 116 1.9 christos char *eb; 117 1.9 christos long long l; 118 1.9 christos #ifndef HAVE_NBTOOL_CONFIG_H 119 1.9 christos time_t when; 120 1.9 christos #endif 121 1.9 christos 122 1.9 christos if (stat(b, &st) != -1) 123 1.9 christos return (time_t)st.st_mtime; 124 1.9 christos 125 1.9 christos #ifndef HAVE_NBTOOL_CONFIG_H 126 1.9 christos errno = 0; 127 1.9 christos if ((when = parsedate(b, NULL, NULL)) != -1 || errno == 0) 128 1.9 christos return when; 129 1.9 christos #endif 130 1.9 christos errno = 0; 131 1.9 christos l = strtoll(b, &eb, 0); 132 1.9 christos if (b == eb || *eb || errno) 133 1.9 christos errx(EXIT_FAILURE, "Can't parse timestamp `%s'", b); 134 1.9 christos return (time_t)l; 135 1.9 christos } 136 1.9 christos 137 1.1 christos int 138 1.1 christos main(int argc, char *argv[]) 139 1.1 christos { 140 1.2 christos char *cmd, *p, *dev = NULL; 141 1.1 christos int ch, i; 142 1.2 christos u_int secsz = 0; 143 1.2 christos off_t mediasz = 0; 144 1.2 christos int flags = 0; 145 1.2 christos int verbose = 0; 146 1.9 christos time_t timestamp = 0; 147 1.2 christos gpt_t gpt; 148 1.2 christos 149 1.2 christos setprogname(argv[0]); 150 1.2 christos 151 1.2 christos if (strcmp(getprogname(), "gpt") == 0) { 152 1.2 christos if (argc < 3) 153 1.2 christos usage(); 154 1.2 christos dev = argv[--argc]; 155 1.2 christos } 156 1.1 christos 157 1.8 aymeric #ifdef __GLIBC__ 158 1.8 aymeric #define GETOPT_BE_POSIX "+" 159 1.8 aymeric #else 160 1.8 aymeric #define GETOPT_BE_POSIX "" 161 1.8 aymeric #endif 162 1.8 aymeric 163 1.1 christos /* Get the generic options */ 164 1.14 jmcneill while ((ch = getopt(argc, argv, GETOPT_BE_POSIX "Hm:nqrs:T:v")) != -1) { 165 1.1 christos switch(ch) { 166 1.14 jmcneill case 'H': 167 1.14 jmcneill flags |= GPT_HYBRID; 168 1.14 jmcneill break; 169 1.1 christos case 'm': 170 1.1 christos if (mediasz > 0) 171 1.1 christos usage(); 172 1.6 christos mediasz = strtol(optarg, &p, 10); 173 1.1 christos if (*p != 0 || mediasz < 1) 174 1.1 christos usage(); 175 1.1 christos break; 176 1.1 christos case 'n': 177 1.2 christos flags |= GPT_NOSYNC; 178 1.1 christos break; 179 1.1 christos case 'r': 180 1.2 christos flags |= GPT_READONLY; 181 1.1 christos break; 182 1.1 christos case 'q': 183 1.2 christos flags |= GPT_QUIET; 184 1.1 christos break; 185 1.1 christos case 's': 186 1.7 christos if (gpt_uint_get(NULL, &secsz) == -1) 187 1.1 christos usage(); 188 1.1 christos break; 189 1.9 christos case 'T': 190 1.10 christos flags |= GPT_TIMESTAMP; 191 1.9 christos timestamp = get_tstamp(optarg); 192 1.9 christos break; 193 1.1 christos case 'v': 194 1.1 christos verbose++; 195 1.1 christos break; 196 1.1 christos default: 197 1.1 christos usage(); 198 1.1 christos } 199 1.1 christos } 200 1.2 christos 201 1.2 christos if (argc == optind) 202 1.2 christos usage(); 203 1.2 christos 204 1.2 christos if (dev == NULL) 205 1.2 christos dev = argv[optind++]; 206 1.1 christos 207 1.1 christos if (argc == optind) 208 1.1 christos usage(); 209 1.1 christos 210 1.1 christos cmd = argv[optind++]; 211 1.3 christos for (i = 0; cmdsw[i]->name != NULL && strcmp(cmd, cmdsw[i]->name); i++) 212 1.2 christos continue; 213 1.1 christos 214 1.3 christos if (cmdsw[i]->fptr == NULL) 215 1.2 christos errx(EXIT_FAILURE, "Unknown command: %s", cmd); 216 1.1 christos 217 1.1 christos prefix(cmd); 218 1.2 christos 219 1.7 christos if (*dev != '-') { 220 1.7 christos gpt = gpt_open(dev, flags | cmdsw[i]->flags, 221 1.9 christos verbose, mediasz, secsz, timestamp); 222 1.7 christos if (gpt == NULL) 223 1.7 christos return EXIT_FAILURE; 224 1.7 christos } else { 225 1.11 mlelstv if ((cmdsw[i]->flags & GPT_OPTDEV) == 0) 226 1.11 mlelstv errx(EXIT_FAILURE, 227 1.11 mlelstv "Command %s needs a device parameter", cmd); 228 1.7 christos argc++; 229 1.7 christos gpt = NULL; 230 1.7 christos } 231 1.2 christos 232 1.3 christos if ((*cmdsw[i]->fptr)(gpt, argc, argv) == -1) 233 1.2 christos return EXIT_FAILURE; 234 1.2 christos 235 1.7 christos if (gpt) 236 1.7 christos gpt_close(gpt); 237 1.2 christos return EXIT_SUCCESS; 238 1.1 christos } 239