unset.c revision 1.5 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.5 christos __RCSID("$NetBSD: unset.c,v 1.5 2014/09/30 18:00:00 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.1 jnemeth
51 1.1 jnemeth static unsigned int entry;
52 1.1 jnemeth static uint64_t attributes;
53 1.1 jnemeth
54 1.1 jnemeth const char unsetmsg[] = "unset -a attribute -i index device ...";
55 1.1 jnemeth
56 1.1 jnemeth __dead static void
57 1.1 jnemeth usage_unset(void)
58 1.1 jnemeth {
59 1.1 jnemeth
60 1.1 jnemeth fprintf(stderr,
61 1.1 jnemeth "usage: %s %s\n", getprogname(), unsetmsg);
62 1.1 jnemeth exit(1);
63 1.1 jnemeth }
64 1.1 jnemeth
65 1.1 jnemeth static void
66 1.1 jnemeth unset(int fd)
67 1.1 jnemeth {
68 1.1 jnemeth map_t *gpt, *tpg;
69 1.1 jnemeth map_t *tbl, *lbt;
70 1.1 jnemeth struct gpt_hdr *hdr;
71 1.1 jnemeth struct gpt_ent *ent;
72 1.1 jnemeth unsigned int i;
73 1.1 jnemeth
74 1.1 jnemeth
75 1.1 jnemeth gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
76 1.1 jnemeth ent = NULL;
77 1.1 jnemeth if (gpt == NULL) {
78 1.1 jnemeth warnx("%s: error: no primary GPT header; run create or recover",
79 1.1 jnemeth device_name);
80 1.1 jnemeth return;
81 1.1 jnemeth }
82 1.1 jnemeth
83 1.1 jnemeth tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
84 1.1 jnemeth if (tpg == NULL) {
85 1.1 jnemeth warnx("%s: error: no secondary GPT header; run recover",
86 1.1 jnemeth device_name);
87 1.1 jnemeth return;
88 1.1 jnemeth }
89 1.1 jnemeth
90 1.1 jnemeth tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
91 1.1 jnemeth lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
92 1.1 jnemeth if (tbl == NULL || lbt == NULL) {
93 1.1 jnemeth warnx("%s: error: run recover -- trust me", device_name);
94 1.1 jnemeth return;
95 1.1 jnemeth }
96 1.1 jnemeth
97 1.1 jnemeth hdr = gpt->map_data;
98 1.1 jnemeth if (entry > le32toh(hdr->hdr_entries)) {
99 1.1 jnemeth warnx("%s: error: index %u out of range (%u max)", device_name,
100 1.1 jnemeth entry, le32toh(hdr->hdr_entries));
101 1.1 jnemeth return;
102 1.1 jnemeth }
103 1.1 jnemeth
104 1.1 jnemeth i = entry - 1;
105 1.1 jnemeth ent = (void*)((char*)tbl->map_data + i *
106 1.1 jnemeth le32toh(hdr->hdr_entsz));
107 1.5 christos if (gpt_uuid_is_nil(ent->ent_type)) {
108 1.1 jnemeth warnx("%s: error: entry at index %u is unused",
109 1.1 jnemeth device_name, entry);
110 1.1 jnemeth return;
111 1.1 jnemeth }
112 1.1 jnemeth
113 1.1 jnemeth ent->ent_attr &= ~attributes;
114 1.1 jnemeth
115 1.1 jnemeth hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
116 1.1 jnemeth le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
117 1.1 jnemeth hdr->hdr_crc_self = 0;
118 1.1 jnemeth hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
119 1.1 jnemeth
120 1.1 jnemeth gpt_write(fd, gpt);
121 1.1 jnemeth gpt_write(fd, tbl);
122 1.1 jnemeth
123 1.1 jnemeth hdr = tpg->map_data;
124 1.1 jnemeth ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
125 1.1 jnemeth ent->ent_attr &= ~attributes;
126 1.1 jnemeth
127 1.1 jnemeth hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
128 1.1 jnemeth le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
129 1.1 jnemeth hdr->hdr_crc_self = 0;
130 1.1 jnemeth hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
131 1.1 jnemeth
132 1.1 jnemeth gpt_write(fd, lbt);
133 1.1 jnemeth gpt_write(fd, tpg);
134 1.1 jnemeth
135 1.2 jnemeth printf("Partition %d attributes updated\n", entry);
136 1.1 jnemeth }
137 1.1 jnemeth
138 1.1 jnemeth int
139 1.1 jnemeth cmd_unset(int argc, char *argv[])
140 1.1 jnemeth {
141 1.1 jnemeth char *p;
142 1.1 jnemeth int ch, fd;
143 1.1 jnemeth
144 1.1 jnemeth while ((ch = getopt(argc, argv, "a:i:")) != -1) {
145 1.1 jnemeth switch(ch) {
146 1.1 jnemeth case 'a':
147 1.1 jnemeth if (strcmp(optarg, "biosboot") == 0)
148 1.1 jnemeth attributes |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
149 1.1 jnemeth else if (strcmp(optarg, "bootme") == 0)
150 1.1 jnemeth attributes |= GPT_ENT_ATTR_BOOTME;
151 1.1 jnemeth else if (strcmp(optarg, "bootonce") == 0)
152 1.1 jnemeth attributes |= GPT_ENT_ATTR_BOOTONCE;
153 1.1 jnemeth else if (strcmp(optarg, "bootfailed") == 0)
154 1.1 jnemeth attributes |= GPT_ENT_ATTR_BOOTFAILED;
155 1.1 jnemeth else
156 1.1 jnemeth usage_unset();
157 1.1 jnemeth break;
158 1.1 jnemeth case 'i':
159 1.1 jnemeth if (entry > 0)
160 1.1 jnemeth usage_unset();
161 1.1 jnemeth entry = strtoul(optarg, &p, 10);
162 1.1 jnemeth if (*p != 0 || entry < 1)
163 1.1 jnemeth usage_unset();
164 1.1 jnemeth break;
165 1.1 jnemeth default:
166 1.1 jnemeth usage_unset();
167 1.1 jnemeth }
168 1.1 jnemeth }
169 1.1 jnemeth
170 1.1 jnemeth if (argc == optind)
171 1.1 jnemeth usage_unset();
172 1.1 jnemeth
173 1.1 jnemeth if (entry == 0 || attributes == 0)
174 1.1 jnemeth usage_unset();
175 1.1 jnemeth
176 1.1 jnemeth while (optind < argc) {
177 1.1 jnemeth fd = gpt_open(argv[optind++]);
178 1.1 jnemeth if (fd == -1) {
179 1.1 jnemeth warn("unable to open device '%s'", device_name);
180 1.1 jnemeth continue;
181 1.1 jnemeth }
182 1.1 jnemeth
183 1.1 jnemeth unset(fd);
184 1.1 jnemeth
185 1.1 jnemeth gpt_close(fd);
186 1.1 jnemeth }
187 1.1 jnemeth
188 1.1 jnemeth return 0;
189 1.1 jnemeth }
190