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