1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <errno.h> 32 #include <paths.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <stdint.h> 36 #include <string.h> 37 #include <strings.h> 38 #include <assert.h> 39 #include <libgeom.h> 40 #include <geom/shsec/g_shsec.h> 41 42 #include "core/geom.h" 43 #include "misc/subr.h" 44 45 46 uint32_t lib_version = G_LIB_VERSION; 47 uint32_t version = G_SHSEC_VERSION; 48 49 static void shsec_main(struct gctl_req *req, unsigned flags); 50 static void shsec_clear(struct gctl_req *req); 51 static void shsec_dump(struct gctl_req *req); 52 static void shsec_label(struct gctl_req *req); 53 54 struct g_command class_commands[] = { 55 { "clear", G_FLAG_VERBOSE, shsec_main, G_NULL_OPTS, 56 "[-v] prov ..." 57 }, 58 { "dump", 0, shsec_main, G_NULL_OPTS, 59 "prov ..." 60 }, 61 { "label", G_FLAG_VERBOSE | G_FLAG_LOADKLD, shsec_main, 62 { 63 { 'h', "hardcode", NULL, G_TYPE_BOOL }, 64 G_OPT_SENTINEL 65 }, 66 "[-hv] name prov prov ..." 67 }, 68 { "stop", G_FLAG_VERBOSE, NULL, 69 { 70 { 'f', "force", NULL, G_TYPE_BOOL }, 71 G_OPT_SENTINEL 72 }, 73 "[-fv] name ..." 74 }, 75 G_CMD_SENTINEL 76 }; 77 78 static int verbose = 0; 79 80 static void 81 shsec_main(struct gctl_req *req, unsigned flags) 82 { 83 const char *name; 84 85 if ((flags & G_FLAG_VERBOSE) != 0) 86 verbose = 1; 87 88 name = gctl_get_ascii(req, "verb"); 89 if (name == NULL) { 90 gctl_error(req, "No '%s' argument.", "verb"); 91 return; 92 } 93 if (strcmp(name, "label") == 0) 94 shsec_label(req); 95 else if (strcmp(name, "clear") == 0) 96 shsec_clear(req); 97 else if (strcmp(name, "dump") == 0) 98 shsec_dump(req); 99 else 100 gctl_error(req, "Unknown command: %s.", name); 101 } 102 103 static void 104 shsec_label(struct gctl_req *req) 105 { 106 struct g_shsec_metadata md; 107 off_t compsize, msize; 108 u_char sector[512]; 109 unsigned ssize, secsize; 110 const char *name; 111 int error, i, nargs, hardcode; 112 113 bzero(sector, sizeof(sector)); 114 nargs = gctl_get_int(req, "nargs"); 115 if (nargs <= 2) { 116 gctl_error(req, "Too few arguments."); 117 return; 118 } 119 hardcode = gctl_get_int(req, "hardcode"); 120 121 /* 122 * Clear last sector first to spoil all components if device exists. 123 */ 124 compsize = 0; 125 secsize = 0; 126 for (i = 1; i < nargs; i++) { 127 name = gctl_get_ascii(req, "arg%d", i); 128 msize = g_get_mediasize(name); 129 ssize = g_get_sectorsize(name); 130 if (msize == 0 || ssize == 0) { 131 gctl_error(req, "Can't get informations about %s: %s.", 132 name, strerror(errno)); 133 return; 134 } 135 msize -= ssize; 136 if (compsize == 0 || (compsize > 0 && msize < compsize)) 137 compsize = msize; 138 if (secsize == 0) 139 secsize = ssize; 140 else 141 secsize = g_lcm(secsize, ssize); 142 143 error = g_metadata_clear(name, NULL); 144 if (error != 0) { 145 gctl_error(req, "Can't store metadata on %s: %s.", name, 146 strerror(error)); 147 return; 148 } 149 } 150 151 strlcpy(md.md_magic, G_SHSEC_MAGIC, sizeof(md.md_magic)); 152 md.md_version = G_SHSEC_VERSION; 153 name = gctl_get_ascii(req, "arg0"); 154 strlcpy(md.md_name, name, sizeof(md.md_name)); 155 md.md_id = arc4random(); 156 md.md_all = nargs - 1; 157 158 /* 159 * Ok, store metadata. 160 */ 161 for (i = 1; i < nargs; i++) { 162 name = gctl_get_ascii(req, "arg%d", i); 163 msize = g_get_mediasize(name); 164 ssize = g_get_sectorsize(name); 165 if (compsize < msize - ssize) { 166 fprintf(stderr, 167 "warning: %s: only %jd bytes from %jd bytes used.\n", 168 name, (intmax_t)compsize, (intmax_t)(msize - ssize)); 169 } 170 171 md.md_no = i - 1; 172 md.md_provsize = msize; 173 if (!hardcode) 174 bzero(md.md_provider, sizeof(md.md_provider)); 175 else { 176 if (strncmp(name, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) 177 name += sizeof(_PATH_DEV) - 1; 178 strlcpy(md.md_provider, name, sizeof(md.md_provider)); 179 } 180 shsec_metadata_encode(&md, sector); 181 error = g_metadata_store(name, sector, sizeof(sector)); 182 if (error != 0) { 183 fprintf(stderr, "Can't store metadata on %s: %s.\n", 184 name, strerror(error)); 185 gctl_error(req, "Not fully done."); 186 continue; 187 } 188 if (verbose) 189 printf("Metadata value stored on %s.\n", name); 190 } 191 } 192 193 static void 194 shsec_clear(struct gctl_req *req) 195 { 196 const char *name; 197 int error, i, nargs; 198 199 nargs = gctl_get_int(req, "nargs"); 200 if (nargs < 1) { 201 gctl_error(req, "Too few arguments."); 202 return; 203 } 204 205 for (i = 0; i < nargs; i++) { 206 name = gctl_get_ascii(req, "arg%d", i); 207 error = g_metadata_clear(name, G_SHSEC_MAGIC); 208 if (error != 0) { 209 fprintf(stderr, "Can't clear metadata on %s: %s.\n", 210 name, strerror(error)); 211 gctl_error(req, "Not fully done."); 212 continue; 213 } 214 if (verbose) 215 printf("Metadata cleared on %s.\n", name); 216 } 217 } 218 219 static void 220 shsec_metadata_dump(const struct g_shsec_metadata *md) 221 { 222 223 printf(" Magic string: %s\n", md->md_magic); 224 printf(" Metadata version: %u\n", (u_int)md->md_version); 225 printf(" Device name: %s\n", md->md_name); 226 printf(" Device ID: %u\n", (u_int)md->md_id); 227 printf(" Disk number: %u\n", (u_int)md->md_no); 228 printf("Total number of disks: %u\n", (u_int)md->md_all); 229 printf(" Hardcoded provider: %s\n", md->md_provider); 230 } 231 232 static void 233 shsec_dump(struct gctl_req *req) 234 { 235 struct g_shsec_metadata md, tmpmd; 236 const char *name; 237 int error, i, nargs; 238 239 nargs = gctl_get_int(req, "nargs"); 240 if (nargs < 1) { 241 gctl_error(req, "Too few arguments."); 242 return; 243 } 244 245 for (i = 0; i < nargs; i++) { 246 name = gctl_get_ascii(req, "arg%d", i); 247 error = g_metadata_read(name, (u_char *)&tmpmd, sizeof(tmpmd), 248 G_SHSEC_MAGIC); 249 if (error != 0) { 250 fprintf(stderr, "Can't read metadata from %s: %s.\n", 251 name, strerror(error)); 252 gctl_error(req, "Not fully done."); 253 continue; 254 } 255 shsec_metadata_decode((u_char *)&tmpmd, &md); 256 printf("Metadata on %s:\n", name); 257 shsec_metadata_dump(&md); 258 printf("\n"); 259 } 260 } 261