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: type.c,v 1.16 2019/06/21 02:14:59 jnemeth 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_type(gpt_t, int, char *[]);
53
54 static const char *typehelp[] = {
55 "-a -T newtype",
56 "[-b blocknr] [-i index] [-L label] [-s sectors] [-t type] -T newtype",
57 "-l",
58 };
59
60 struct gpt_cmd c_type = {
61 "type",
62 cmd_type,
63 typehelp, __arraycount(typehelp),
64 GPT_SYNC | GPT_OPTDEV,
65 };
66
67 #define usage() gpt_usage(NULL, &c_type)
68
69 static void
change(struct gpt_ent * ent,void * v,int backup)70 change(struct gpt_ent *ent, void *v, int backup)
71 {
72 gpt_uuid_t *newtype = v;
73 gpt_uuid_copy(ent->ent_type, *newtype);
74 }
75
76 static int
cmd_type(gpt_t gpt,int argc,char * argv[])77 cmd_type(gpt_t gpt, int argc, char *argv[])
78 {
79 int ch;
80 gpt_uuid_t newtype;
81 struct gpt_find find;
82
83 memset(&find, 0, sizeof(find));
84 gpt_uuid_copy(newtype, gpt_uuid_nil);
85 find.msg = "type changed";
86
87 /* Get the type options */
88 while ((ch = getopt(argc, argv, GPT_FIND "T:l")) != -1) {
89 switch(ch) {
90 case 'l':
91 gpt_uuid_help("\t");
92 return 0;
93 case 'T':
94 if (gpt == NULL || gpt_uuid_get(gpt, &newtype) == -1)
95 return -1;
96 break;
97 default:
98 if (gpt == NULL || gpt_add_find(gpt, &find, ch) == -1)
99 return usage();
100 break;
101 }
102 }
103
104 if (gpt == NULL || gpt_uuid_is_nil(newtype) || argc != optind)
105 return usage();
106
107 return gpt_change_ent(gpt, &find, change, &newtype);
108 }
109