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