188085c2fSjnemeth /*-
288085c2fSjnemeth * Copyright (c) 2004 Marcel Moolenaar
388085c2fSjnemeth * All rights reserved.
488085c2fSjnemeth *
588085c2fSjnemeth * Redistribution and use in source and binary forms, with or without
688085c2fSjnemeth * modification, are permitted provided that the following conditions
788085c2fSjnemeth * are met:
888085c2fSjnemeth *
988085c2fSjnemeth * 1. Redistributions of source code must retain the above copyright
1088085c2fSjnemeth * notice, this list of conditions and the following disclaimer.
1188085c2fSjnemeth * 2. Redistributions in binary form must reproduce the above copyright
1288085c2fSjnemeth * notice, this list of conditions and the following disclaimer in the
1388085c2fSjnemeth * documentation and/or other materials provided with the distribution.
1488085c2fSjnemeth *
1588085c2fSjnemeth * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1688085c2fSjnemeth * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1788085c2fSjnemeth * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1888085c2fSjnemeth * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1988085c2fSjnemeth * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2088085c2fSjnemeth * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2188085c2fSjnemeth * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2288085c2fSjnemeth * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2388085c2fSjnemeth * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2488085c2fSjnemeth * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2588085c2fSjnemeth */
2688085c2fSjnemeth
27a50708a1Schristos #if HAVE_NBTOOL_CONFIG_H
28a50708a1Schristos #include "nbtool_config.h"
29a50708a1Schristos #endif
30a50708a1Schristos
3188085c2fSjnemeth #include <sys/cdefs.h>
3288085c2fSjnemeth #ifdef __FBSDID
3388085c2fSjnemeth __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
3488085c2fSjnemeth #endif
3588085c2fSjnemeth #ifdef __RCSID
36*e003a26fSjnemeth __RCSID("$NetBSD: type.c,v 1.16 2019/06/21 02:14:59 jnemeth Exp $");
3788085c2fSjnemeth #endif
3888085c2fSjnemeth
3988085c2fSjnemeth #include <sys/types.h>
4088085c2fSjnemeth
4188085c2fSjnemeth #include <err.h>
4288085c2fSjnemeth #include <stddef.h>
4388085c2fSjnemeth #include <stdio.h>
4488085c2fSjnemeth #include <stdlib.h>
4588085c2fSjnemeth #include <string.h>
4688085c2fSjnemeth #include <unistd.h>
4788085c2fSjnemeth
4888085c2fSjnemeth #include "map.h"
4988085c2fSjnemeth #include "gpt.h"
500b43d398Schristos #include "gpt_private.h"
5188085c2fSjnemeth
528ca93e46Schristos static int cmd_type(gpt_t, int, char *[]);
5388085c2fSjnemeth
548ca93e46Schristos static const char *typehelp[] = {
558ca93e46Schristos "-a -T newtype",
568ca93e46Schristos "[-b blocknr] [-i index] [-L label] [-s sectors] [-t type] -T newtype",
574d523900Schristos "-l",
588ca93e46Schristos };
5988085c2fSjnemeth
608ca93e46Schristos struct gpt_cmd c_type = {
618ca93e46Schristos "type",
628ca93e46Schristos cmd_type,
638ca93e46Schristos typehelp, __arraycount(typehelp),
64aa3b5bb2Sjnemeth GPT_SYNC | GPT_OPTDEV,
658ca93e46Schristos };
668ca93e46Schristos
678ca93e46Schristos #define usage() gpt_usage(NULL, &c_type)
6888085c2fSjnemeth
69bbb4a8abSchristos static void
change(struct gpt_ent * ent,void * v,int backup)70*e003a26fSjnemeth change(struct gpt_ent *ent, void *v, int backup)
7188085c2fSjnemeth {
72bbb4a8abSchristos gpt_uuid_t *newtype = v;
73bbb4a8abSchristos gpt_uuid_copy(ent->ent_type, *newtype);
7488085c2fSjnemeth }
7588085c2fSjnemeth
768ca93e46Schristos static int
cmd_type(gpt_t gpt,int argc,char * argv[])770b43d398Schristos cmd_type(gpt_t gpt, int argc, char *argv[])
7888085c2fSjnemeth {
790b43d398Schristos int ch;
80bbb4a8abSchristos gpt_uuid_t newtype;
81bbb4a8abSchristos struct gpt_find find;
82bbb4a8abSchristos
83bbb4a8abSchristos memset(&find, 0, sizeof(find));
84da0a3b4cSchristos gpt_uuid_copy(newtype, gpt_uuid_nil);
85bbb4a8abSchristos find.msg = "type changed";
8688085c2fSjnemeth
8788085c2fSjnemeth /* Get the type options */
884d523900Schristos while ((ch = getopt(argc, argv, GPT_FIND "T:l")) != -1) {
8988085c2fSjnemeth switch(ch) {
904d523900Schristos case 'l':
914d523900Schristos gpt_uuid_help("\t");
924d523900Schristos return 0;
9388085c2fSjnemeth case 'T':
94a23ee504Smlelstv if (gpt == NULL || gpt_uuid_get(gpt, &newtype) == -1)
95c94670c3Schristos return -1;
9688085c2fSjnemeth break;
9788085c2fSjnemeth default:
98a23ee504Smlelstv if (gpt == NULL || gpt_add_find(gpt, &find, ch) == -1)
998ca93e46Schristos return usage();
100bbb4a8abSchristos break;
10188085c2fSjnemeth }
10288085c2fSjnemeth }
10388085c2fSjnemeth
104a23ee504Smlelstv if (gpt == NULL || gpt_uuid_is_nil(newtype) || argc != optind)
1058ca93e46Schristos return usage();
10688085c2fSjnemeth
107bbb4a8abSchristos return gpt_change_ent(gpt, &find, change, &newtype);
10888085c2fSjnemeth }
109