unset.c revision 1.7 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.3 christos #if HAVE_NBTOOL_CONFIG_H
28 1.3 christos #include "nbtool_config.h"
29 1.3 christos #endif
30 1.3 christos
31 1.1 jnemeth #include <sys/cdefs.h>
32 1.1 jnemeth #ifdef __FBSDID
33 1.1 jnemeth __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
34 1.1 jnemeth #endif
35 1.1 jnemeth #ifdef __RCSID
36 1.7 christos __RCSID("$NetBSD: unset.c,v 1.7 2015/12/01 09:05:33 christos Exp $");
37 1.1 jnemeth #endif
38 1.1 jnemeth
39 1.1 jnemeth #include <sys/types.h>
40 1.1 jnemeth
41 1.1 jnemeth #include <err.h>
42 1.1 jnemeth #include <stddef.h>
43 1.1 jnemeth #include <stdio.h>
44 1.1 jnemeth #include <stdlib.h>
45 1.1 jnemeth #include <string.h>
46 1.1 jnemeth #include <unistd.h>
47 1.1 jnemeth
48 1.1 jnemeth #include "map.h"
49 1.1 jnemeth #include "gpt.h"
50 1.7 christos #include "gpt_private.h"
51 1.1 jnemeth
52 1.1 jnemeth static unsigned int entry;
53 1.1 jnemeth static uint64_t attributes;
54 1.1 jnemeth
55 1.7 christos const char unsetmsg[] = "unset -a attribute -i index";
56 1.1 jnemeth
57 1.7 christos static int
58 1.1 jnemeth usage_unset(void)
59 1.1 jnemeth {
60 1.1 jnemeth
61 1.1 jnemeth fprintf(stderr,
62 1.1 jnemeth "usage: %s %s\n", getprogname(), unsetmsg);
63 1.7 christos return -1;
64 1.1 jnemeth }
65 1.1 jnemeth
66 1.7 christos static int
67 1.7 christos unset(gpt_t gpt)
68 1.1 jnemeth {
69 1.1 jnemeth struct gpt_hdr *hdr;
70 1.1 jnemeth struct gpt_ent *ent;
71 1.1 jnemeth unsigned int i;
72 1.1 jnemeth
73 1.1 jnemeth
74 1.7 christos if ((hdr = gpt_hdr(gpt)) == NULL)
75 1.7 christos return -1;
76 1.1 jnemeth
77 1.1 jnemeth if (entry > le32toh(hdr->hdr_entries)) {
78 1.7 christos gpt_warnx(gpt, "Index %u out of range (%u max)",
79 1.1 jnemeth entry, le32toh(hdr->hdr_entries));
80 1.7 christos return -1;
81 1.1 jnemeth }
82 1.1 jnemeth
83 1.1 jnemeth i = entry - 1;
84 1.7 christos ent = gpt_ent_primary(gpt, i);
85 1.5 christos if (gpt_uuid_is_nil(ent->ent_type)) {
86 1.7 christos gpt_warnx(gpt, "Entry at index %u is unused", entry);
87 1.7 christos return -1;
88 1.1 jnemeth }
89 1.1 jnemeth
90 1.1 jnemeth ent->ent_attr &= ~attributes;
91 1.1 jnemeth
92 1.7 christos if (gpt_write_primary(gpt) == -1)
93 1.7 christos return -1;
94 1.1 jnemeth
95 1.7 christos ent = gpt_ent_backup(gpt, i);
96 1.1 jnemeth ent->ent_attr &= ~attributes;
97 1.1 jnemeth
98 1.7 christos if (gpt_write_backup(gpt) == -1)
99 1.7 christos return -1;
100 1.7 christos gpt_msg(gpt, "Partition %d attributes updated", entry);
101 1.7 christos return 0;
102 1.1 jnemeth }
103 1.1 jnemeth
104 1.1 jnemeth int
105 1.7 christos cmd_unset(gpt_t gpt, int argc, char *argv[])
106 1.1 jnemeth {
107 1.1 jnemeth char *p;
108 1.7 christos int ch;
109 1.1 jnemeth
110 1.1 jnemeth while ((ch = getopt(argc, argv, "a:i:")) != -1) {
111 1.1 jnemeth switch(ch) {
112 1.1 jnemeth case 'a':
113 1.1 jnemeth if (strcmp(optarg, "biosboot") == 0)
114 1.1 jnemeth attributes |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
115 1.1 jnemeth else if (strcmp(optarg, "bootme") == 0)
116 1.1 jnemeth attributes |= GPT_ENT_ATTR_BOOTME;
117 1.1 jnemeth else if (strcmp(optarg, "bootonce") == 0)
118 1.1 jnemeth attributes |= GPT_ENT_ATTR_BOOTONCE;
119 1.1 jnemeth else if (strcmp(optarg, "bootfailed") == 0)
120 1.1 jnemeth attributes |= GPT_ENT_ATTR_BOOTFAILED;
121 1.1 jnemeth else
122 1.7 christos return usage_unset();
123 1.1 jnemeth break;
124 1.1 jnemeth case 'i':
125 1.1 jnemeth if (entry > 0)
126 1.7 christos return usage_unset();
127 1.1 jnemeth entry = strtoul(optarg, &p, 10);
128 1.1 jnemeth if (*p != 0 || entry < 1)
129 1.7 christos return usage_unset();
130 1.1 jnemeth break;
131 1.1 jnemeth default:
132 1.7 christos return usage_unset();
133 1.1 jnemeth }
134 1.1 jnemeth }
135 1.1 jnemeth
136 1.7 christos if (argc != optind)
137 1.7 christos return usage_unset();
138 1.1 jnemeth
139 1.1 jnemeth if (entry == 0 || attributes == 0)
140 1.7 christos return usage_unset();
141 1.1 jnemeth
142 1.7 christos return unset(gpt);
143 1.1 jnemeth }
144