Home | History | Annotate | Line # | Download | only in gpt
unset.c revision 1.8
      1 /*-
      2  * Copyright (c) 2002 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/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
     34 #endif
     35 #ifdef __RCSID
     36 __RCSID("$NetBSD: unset.c,v 1.8 2015/12/01 16:32: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 unsigned int entry;
     53 static uint64_t attributes;
     54 
     55 static int cmd_unset(gpt_t, int, char *[]);
     56 
     57 static const char *unsethelp[] = {
     58     "-a attribute -i index",
     59 };
     60 
     61 struct gpt_cmd c_unset = {
     62 	"unset",
     63 	cmd_unset,
     64 	unsethelp, __arraycount(unsethelp),
     65 	0,
     66 };
     67 
     68 #define usage() gpt_usage(NULL, &c_unset)
     69 
     70 static int
     71 unset(gpt_t gpt)
     72 {
     73 	struct gpt_hdr *hdr;
     74 	struct gpt_ent *ent;
     75 	unsigned int i;
     76 
     77 
     78 	if ((hdr = gpt_hdr(gpt)) == NULL)
     79 		return -1;
     80 
     81 	if (entry > le32toh(hdr->hdr_entries)) {
     82 		gpt_warnx(gpt, "Index %u out of range (%u max)",
     83 		    entry, le32toh(hdr->hdr_entries));
     84 		return -1;
     85 	}
     86 
     87 	i = entry - 1;
     88 	ent = gpt_ent_primary(gpt, i);
     89 	if (gpt_uuid_is_nil(ent->ent_type)) {
     90 		gpt_warnx(gpt, "Entry at index %u is unused", entry);
     91 		return -1;
     92 	}
     93 
     94 	ent->ent_attr &= ~attributes;
     95 
     96 	if (gpt_write_primary(gpt) == -1)
     97 		return -1;
     98 
     99 	ent = gpt_ent_backup(gpt, i);
    100 	ent->ent_attr &= ~attributes;
    101 
    102 	if (gpt_write_backup(gpt) == -1)
    103 		return -1;
    104 	gpt_msg(gpt, "Partition %d attributes updated", entry);
    105 	return 0;
    106 }
    107 
    108 static int
    109 cmd_unset(gpt_t gpt, int argc, char *argv[])
    110 {
    111 	char *p;
    112 	int ch;
    113 
    114 	while ((ch = getopt(argc, argv, "a:i:")) != -1) {
    115 		switch(ch) {
    116 		case 'a':
    117 			if (strcmp(optarg, "biosboot") == 0)
    118 				attributes |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
    119 			else if (strcmp(optarg, "bootme") == 0)
    120 				attributes |= GPT_ENT_ATTR_BOOTME;
    121 			else if (strcmp(optarg, "bootonce") == 0)
    122 				attributes |= GPT_ENT_ATTR_BOOTONCE;
    123 			else if (strcmp(optarg, "bootfailed") == 0)
    124 				attributes |= GPT_ENT_ATTR_BOOTFAILED;
    125 			else
    126 				return usage();
    127 			break;
    128 		case 'i':
    129 			if (entry > 0)
    130 				return usage();
    131 			entry = strtoul(optarg, &p, 10);
    132 			if (*p != 0 || entry < 1)
    133 				return usage();
    134 			break;
    135 		default:
    136 			return usage();
    137 		}
    138 	}
    139 
    140 	if (argc != optind)
    141 		return usage();
    142 
    143 	if (entry == 0 || attributes == 0)
    144 		return usage();
    145 
    146 	return unset(gpt);
    147 }
    148