Home | History | Annotate | Line # | Download | only in gpt
unset.c revision 1.1
      1  1.1  jnemeth /*-
      2  1.1  jnemeth  * Copyright (c) 2002 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 #include <sys/cdefs.h>
     28  1.1  jnemeth #ifdef __FBSDID
     29  1.1  jnemeth __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
     30  1.1  jnemeth #endif
     31  1.1  jnemeth #ifdef __RCSID
     32  1.1  jnemeth __RCSID("$NetBSD: unset.c,v 1.1 2013/12/09 08:03:17 jnemeth Exp $");
     33  1.1  jnemeth #endif
     34  1.1  jnemeth 
     35  1.1  jnemeth #include <sys/types.h>
     36  1.1  jnemeth 
     37  1.1  jnemeth #include <err.h>
     38  1.1  jnemeth #include <stddef.h>
     39  1.1  jnemeth #include <stdio.h>
     40  1.1  jnemeth #include <stdlib.h>
     41  1.1  jnemeth #include <string.h>
     42  1.1  jnemeth #include <unistd.h>
     43  1.1  jnemeth #include <inttypes.h>
     44  1.1  jnemeth 
     45  1.1  jnemeth #include "map.h"
     46  1.1  jnemeth #include "gpt.h"
     47  1.1  jnemeth 
     48  1.1  jnemeth static unsigned int entry;
     49  1.1  jnemeth static uint64_t attributes;
     50  1.1  jnemeth 
     51  1.1  jnemeth const char unsetmsg[] = "unset -a attribute -i index device ...";
     52  1.1  jnemeth 
     53  1.1  jnemeth __dead static void
     54  1.1  jnemeth usage_unset(void)
     55  1.1  jnemeth {
     56  1.1  jnemeth 
     57  1.1  jnemeth 	fprintf(stderr,
     58  1.1  jnemeth 	    "usage: %s %s\n", getprogname(), unsetmsg);
     59  1.1  jnemeth 	exit(1);
     60  1.1  jnemeth }
     61  1.1  jnemeth 
     62  1.1  jnemeth static void
     63  1.1  jnemeth unset(int fd)
     64  1.1  jnemeth {
     65  1.1  jnemeth 	uuid_t uuid;
     66  1.1  jnemeth 	map_t *gpt, *tpg;
     67  1.1  jnemeth 	map_t *tbl, *lbt;
     68  1.1  jnemeth 	struct gpt_hdr *hdr;
     69  1.1  jnemeth 	struct gpt_ent *ent;
     70  1.1  jnemeth 	unsigned int i;
     71  1.1  jnemeth 
     72  1.1  jnemeth 
     73  1.1  jnemeth 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     74  1.1  jnemeth 	ent = NULL;
     75  1.1  jnemeth 	if (gpt == NULL) {
     76  1.1  jnemeth 		warnx("%s: error: no primary GPT header; run create or recover",
     77  1.1  jnemeth 		    device_name);
     78  1.1  jnemeth 		return;
     79  1.1  jnemeth 	}
     80  1.1  jnemeth 
     81  1.1  jnemeth 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     82  1.1  jnemeth 	if (tpg == NULL) {
     83  1.1  jnemeth 		warnx("%s: error: no secondary GPT header; run recover",
     84  1.1  jnemeth 		    device_name);
     85  1.1  jnemeth 		return;
     86  1.1  jnemeth 	}
     87  1.1  jnemeth 
     88  1.1  jnemeth 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
     89  1.1  jnemeth 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
     90  1.1  jnemeth 	if (tbl == NULL || lbt == NULL) {
     91  1.1  jnemeth 		warnx("%s: error: run recover -- trust me", device_name);
     92  1.1  jnemeth 		return;
     93  1.1  jnemeth 	}
     94  1.1  jnemeth 
     95  1.1  jnemeth 	hdr = gpt->map_data;
     96  1.1  jnemeth 	if (entry > le32toh(hdr->hdr_entries)) {
     97  1.1  jnemeth 		warnx("%s: error: index %u out of range (%u max)", device_name,
     98  1.1  jnemeth 		    entry, le32toh(hdr->hdr_entries));
     99  1.1  jnemeth 		return;
    100  1.1  jnemeth 	}
    101  1.1  jnemeth 
    102  1.1  jnemeth 	i = entry - 1;
    103  1.1  jnemeth 	ent = (void*)((char*)tbl->map_data + i *
    104  1.1  jnemeth 	    le32toh(hdr->hdr_entsz));
    105  1.1  jnemeth 	le_uuid_dec(ent->ent_type, &uuid);
    106  1.1  jnemeth 	if (uuid_is_nil(&uuid, NULL)) {
    107  1.1  jnemeth 		warnx("%s: error: entry at index %u is unused",
    108  1.1  jnemeth 		    device_name, entry);
    109  1.1  jnemeth 		return;
    110  1.1  jnemeth 	}
    111  1.1  jnemeth 
    112  1.1  jnemeth 	ent->ent_attr &= ~attributes;
    113  1.1  jnemeth 
    114  1.1  jnemeth 	hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
    115  1.1  jnemeth 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    116  1.1  jnemeth 	hdr->hdr_crc_self = 0;
    117  1.1  jnemeth 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    118  1.1  jnemeth 
    119  1.1  jnemeth 	gpt_write(fd, gpt);
    120  1.1  jnemeth 	gpt_write(fd, tbl);
    121  1.1  jnemeth 
    122  1.1  jnemeth 	hdr = tpg->map_data;
    123  1.1  jnemeth 	ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
    124  1.1  jnemeth 	ent->ent_attr &= ~attributes;
    125  1.1  jnemeth 
    126  1.1  jnemeth 	hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
    127  1.1  jnemeth 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    128  1.1  jnemeth 	hdr->hdr_crc_self = 0;
    129  1.1  jnemeth 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    130  1.1  jnemeth 
    131  1.1  jnemeth 	gpt_write(fd, lbt);
    132  1.1  jnemeth 	gpt_write(fd, tpg);
    133  1.1  jnemeth 
    134  1.1  jnemeth 	printf("Partition attributes updated\n");
    135  1.1  jnemeth }
    136  1.1  jnemeth 
    137  1.1  jnemeth int
    138  1.1  jnemeth cmd_unset(int argc, char *argv[])
    139  1.1  jnemeth {
    140  1.1  jnemeth 	char *p;
    141  1.1  jnemeth 	int ch, fd;
    142  1.1  jnemeth 
    143  1.1  jnemeth 	while ((ch = getopt(argc, argv, "a:i:")) != -1) {
    144  1.1  jnemeth 		switch(ch) {
    145  1.1  jnemeth 		case 'a':
    146  1.1  jnemeth 			if (strcmp(optarg, "biosboot") == 0)
    147  1.1  jnemeth 				attributes |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
    148  1.1  jnemeth 			else if (strcmp(optarg, "bootme") == 0)
    149  1.1  jnemeth 				attributes |= GPT_ENT_ATTR_BOOTME;
    150  1.1  jnemeth 			else if (strcmp(optarg, "bootonce") == 0)
    151  1.1  jnemeth 				attributes |= GPT_ENT_ATTR_BOOTONCE;
    152  1.1  jnemeth 			else if (strcmp(optarg, "bootfailed") == 0)
    153  1.1  jnemeth 				attributes |= GPT_ENT_ATTR_BOOTFAILED;
    154  1.1  jnemeth 			else
    155  1.1  jnemeth 				usage_unset();
    156  1.1  jnemeth 			break;
    157  1.1  jnemeth 		case 'i':
    158  1.1  jnemeth 			if (entry > 0)
    159  1.1  jnemeth 				usage_unset();
    160  1.1  jnemeth 			entry = strtoul(optarg, &p, 10);
    161  1.1  jnemeth 			if (*p != 0 || entry < 1)
    162  1.1  jnemeth 				usage_unset();
    163  1.1  jnemeth 			break;
    164  1.1  jnemeth 		default:
    165  1.1  jnemeth 			usage_unset();
    166  1.1  jnemeth 		}
    167  1.1  jnemeth 	}
    168  1.1  jnemeth 
    169  1.1  jnemeth 	if (argc == optind)
    170  1.1  jnemeth 		usage_unset();
    171  1.1  jnemeth 
    172  1.1  jnemeth 	if (entry == 0 || attributes == 0)
    173  1.1  jnemeth 		usage_unset();
    174  1.1  jnemeth 
    175  1.1  jnemeth 	while (optind < argc) {
    176  1.1  jnemeth 		fd = gpt_open(argv[optind++]);
    177  1.1  jnemeth 		if (fd == -1) {
    178  1.1  jnemeth 			warn("unable to open device '%s'", device_name);
    179  1.1  jnemeth 			continue;
    180  1.1  jnemeth 		}
    181  1.1  jnemeth 
    182  1.1  jnemeth 		unset(fd);
    183  1.1  jnemeth 
    184  1.1  jnemeth 		gpt_close(fd);
    185  1.1  jnemeth 	}
    186  1.1  jnemeth 
    187  1.1  jnemeth 	return 0;
    188  1.1  jnemeth }
    189