Annotation of src/sbin/gpt/unset.c, Revision 1.7
1.1 jnemeth 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:
1.3 christos 27: #if HAVE_NBTOOL_CONFIG_H
28: #include "nbtool_config.h"
29: #endif
30:
1.1 jnemeth 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
1.7 ! christos 36: __RCSID("$NetBSD: unset.c,v 1.6 2015/11/29 00:14:46 christos Exp $");
1.1 jnemeth 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"
1.7 ! christos 50: #include "gpt_private.h"
1.1 jnemeth 51:
52: static unsigned int entry;
53: static uint64_t attributes;
54:
1.7 ! christos 55: const char unsetmsg[] = "unset -a attribute -i index";
1.1 jnemeth 56:
1.7 ! christos 57: static int
1.1 jnemeth 58: usage_unset(void)
59: {
60:
61: fprintf(stderr,
62: "usage: %s %s\n", getprogname(), unsetmsg);
1.7 ! christos 63: return -1;
1.1 jnemeth 64: }
65:
1.7 ! christos 66: static int
! 67: unset(gpt_t gpt)
1.1 jnemeth 68: {
69: struct gpt_hdr *hdr;
70: struct gpt_ent *ent;
71: unsigned int i;
72:
73:
1.7 ! christos 74: if ((hdr = gpt_hdr(gpt)) == NULL)
! 75: return -1;
1.1 jnemeth 76:
77: if (entry > le32toh(hdr->hdr_entries)) {
1.7 ! christos 78: gpt_warnx(gpt, "Index %u out of range (%u max)",
1.1 jnemeth 79: entry, le32toh(hdr->hdr_entries));
1.7 ! christos 80: return -1;
1.1 jnemeth 81: }
82:
83: i = entry - 1;
1.7 ! christos 84: ent = gpt_ent_primary(gpt, i);
1.5 christos 85: if (gpt_uuid_is_nil(ent->ent_type)) {
1.7 ! christos 86: gpt_warnx(gpt, "Entry at index %u is unused", entry);
! 87: return -1;
1.1 jnemeth 88: }
89:
90: ent->ent_attr &= ~attributes;
91:
1.7 ! christos 92: if (gpt_write_primary(gpt) == -1)
! 93: return -1;
1.1 jnemeth 94:
1.7 ! christos 95: ent = gpt_ent_backup(gpt, i);
1.1 jnemeth 96: ent->ent_attr &= ~attributes;
97:
1.7 ! christos 98: if (gpt_write_backup(gpt) == -1)
! 99: return -1;
! 100: gpt_msg(gpt, "Partition %d attributes updated", entry);
! 101: return 0;
1.1 jnemeth 102: }
103:
104: int
1.7 ! christos 105: cmd_unset(gpt_t gpt, int argc, char *argv[])
1.1 jnemeth 106: {
107: char *p;
1.7 ! christos 108: int ch;
1.1 jnemeth 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
1.7 ! christos 122: return usage_unset();
1.1 jnemeth 123: break;
124: case 'i':
125: if (entry > 0)
1.7 ! christos 126: return usage_unset();
1.1 jnemeth 127: entry = strtoul(optarg, &p, 10);
128: if (*p != 0 || entry < 1)
1.7 ! christos 129: return usage_unset();
1.1 jnemeth 130: break;
131: default:
1.7 ! christos 132: return usage_unset();
1.1 jnemeth 133: }
134: }
135:
1.7 ! christos 136: if (argc != optind)
! 137: return usage_unset();
1.1 jnemeth 138:
139: if (entry == 0 || attributes == 0)
1.7 ! christos 140: return usage_unset();
1.1 jnemeth 141:
1.7 ! christos 142: return unset(gpt);
1.1 jnemeth 143: }
CVSweb <webmaster@jp.NetBSD.org>