1 /*- 2 * Copyright (c) 2004 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #if HAVE_NBTOOL_CONFIG_H 28 #include "nbtool_config.h" 29 #endif 30 31 #include <sys/cdefs.h> 32 #ifdef __FBSDID 33 __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $"); 34 #endif 35 #ifdef __RCSID 36 __RCSID("$NetBSD: uuid.c,v 1.4 2025/02/23 20:47:19 christos Exp $"); 37 #endif 38 39 #include <sys/types.h> 40 41 #include <err.h> 42 #include <stddef.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #include "map.h" 49 #include "gpt.h" 50 #include "gpt_private.h" 51 52 static int cmd_uuid(gpt_t, int, char *[]); 53 54 static const char *uuidhelp[] = { 55 "-a", 56 "[-b blocknr] [-i index] [-L label] [-s sectors] [-t type] [-U newuuid]", 57 }; 58 59 const struct gpt_cmd c_uuid = { 60 "uuid", 61 cmd_uuid, 62 uuidhelp, __arraycount(uuidhelp), 63 GPT_SYNC, 64 }; 65 66 #define usage() gpt_usage(NULL, &c_uuid) 67 68 struct uuidctx { 69 gpt_t gpt; 70 gpt_uuid_t *uuid; 71 }; 72 73 static void 74 change_ent(struct gpt_ent *ent, void *v, int backup) 75 { 76 struct uuidctx *ctx = v; 77 static gpt_uuid_t uuidstore; 78 79 if (!backup) { 80 if (ctx->uuid != NULL) 81 memcpy(uuidstore, ctx->uuid, sizeof(uuidstore)); 82 else 83 gpt_uuid_generate(ctx->gpt, uuidstore); 84 } 85 memmove(ent->ent_guid, uuidstore, sizeof(ent->ent_guid)); 86 } 87 88 static void 89 change_hdr(struct gpt_hdr *hdr, void *v, int backup) 90 { 91 struct uuidctx *ctx = v; 92 static gpt_uuid_t uuidstore; 93 94 if (!backup) 95 gpt_uuid_generate(ctx->gpt, uuidstore); 96 memmove(hdr->hdr_guid, uuidstore, sizeof(hdr->hdr_guid)); 97 } 98 99 static int 100 cmd_uuid(gpt_t gpt, int argc, char *argv[]) 101 { 102 int ch, rc; 103 struct gpt_find find; 104 gpt_uuid_t new_uuid; 105 struct uuidctx ctx; 106 107 if (gpt == NULL) 108 return usage(); 109 110 memset(&find, 0, sizeof(find)); 111 find.msg = "UUID changed"; 112 113 /* Get the uuid options */ 114 ctx.gpt = gpt; 115 ctx.uuid = NULL; 116 while ((ch = getopt(argc, argv, GPT_FIND "U:")) != -1) { 117 switch (ch) { 118 case 'U': 119 if (gpt_uuid_parse(optarg, new_uuid) == -1) 120 return usage(); 121 ctx.uuid = &new_uuid; 122 break; 123 case 'L': 124 case 'a': 125 case 'b': 126 case 'i': 127 case 's': 128 case 't': 129 if (gpt_add_find(gpt, &find, ch) == -1) 130 return usage(); 131 break; 132 default: 133 return usage(); 134 } 135 } 136 137 if (argc != optind) 138 return usage(); 139 140 rc = gpt_change_ent(gpt, &find, change_ent, &ctx); 141 if (rc != 0) 142 return rc; 143 144 if (find.all) 145 return gpt_change_hdr(gpt, &find, change_hdr, &ctx); 146 147 return 0; 148 } 149