unset.c revision 1.2.4.2 1 1.2.4.2 yamt /*-
2 1.2.4.2 yamt * Copyright (c) 2002 Marcel Moolenaar
3 1.2.4.2 yamt * All rights reserved.
4 1.2.4.2 yamt *
5 1.2.4.2 yamt * Redistribution and use in source and binary forms, with or without
6 1.2.4.2 yamt * modification, are permitted provided that the following conditions
7 1.2.4.2 yamt * are met:
8 1.2.4.2 yamt *
9 1.2.4.2 yamt * 1. Redistributions of source code must retain the above copyright
10 1.2.4.2 yamt * notice, this list of conditions and the following disclaimer.
11 1.2.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
12 1.2.4.2 yamt * notice, this list of conditions and the following disclaimer in the
13 1.2.4.2 yamt * documentation and/or other materials provided with the distribution.
14 1.2.4.2 yamt *
15 1.2.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.2.4.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.2.4.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.2.4.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.2.4.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.2.4.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.2.4.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.2.4.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.2.4.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.2.4.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.2.4.2 yamt */
26 1.2.4.2 yamt
27 1.2.4.2 yamt #include <sys/cdefs.h>
28 1.2.4.2 yamt #ifdef __FBSDID
29 1.2.4.2 yamt __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
30 1.2.4.2 yamt #endif
31 1.2.4.2 yamt #ifdef __RCSID
32 1.2.4.2 yamt __RCSID("$NetBSD: unset.c,v 1.2.4.2 2014/05/22 11:37:28 yamt Exp $");
33 1.2.4.2 yamt #endif
34 1.2.4.2 yamt
35 1.2.4.2 yamt #include <sys/types.h>
36 1.2.4.2 yamt
37 1.2.4.2 yamt #include <err.h>
38 1.2.4.2 yamt #include <stddef.h>
39 1.2.4.2 yamt #include <stdio.h>
40 1.2.4.2 yamt #include <stdlib.h>
41 1.2.4.2 yamt #include <string.h>
42 1.2.4.2 yamt #include <unistd.h>
43 1.2.4.2 yamt #include <inttypes.h>
44 1.2.4.2 yamt
45 1.2.4.2 yamt #include "map.h"
46 1.2.4.2 yamt #include "gpt.h"
47 1.2.4.2 yamt
48 1.2.4.2 yamt static unsigned int entry;
49 1.2.4.2 yamt static uint64_t attributes;
50 1.2.4.2 yamt
51 1.2.4.2 yamt const char unsetmsg[] = "unset -a attribute -i index device ...";
52 1.2.4.2 yamt
53 1.2.4.2 yamt __dead static void
54 1.2.4.2 yamt usage_unset(void)
55 1.2.4.2 yamt {
56 1.2.4.2 yamt
57 1.2.4.2 yamt fprintf(stderr,
58 1.2.4.2 yamt "usage: %s %s\n", getprogname(), unsetmsg);
59 1.2.4.2 yamt exit(1);
60 1.2.4.2 yamt }
61 1.2.4.2 yamt
62 1.2.4.2 yamt static void
63 1.2.4.2 yamt unset(int fd)
64 1.2.4.2 yamt {
65 1.2.4.2 yamt uuid_t uuid;
66 1.2.4.2 yamt map_t *gpt, *tpg;
67 1.2.4.2 yamt map_t *tbl, *lbt;
68 1.2.4.2 yamt struct gpt_hdr *hdr;
69 1.2.4.2 yamt struct gpt_ent *ent;
70 1.2.4.2 yamt unsigned int i;
71 1.2.4.2 yamt
72 1.2.4.2 yamt
73 1.2.4.2 yamt gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
74 1.2.4.2 yamt ent = NULL;
75 1.2.4.2 yamt if (gpt == NULL) {
76 1.2.4.2 yamt warnx("%s: error: no primary GPT header; run create or recover",
77 1.2.4.2 yamt device_name);
78 1.2.4.2 yamt return;
79 1.2.4.2 yamt }
80 1.2.4.2 yamt
81 1.2.4.2 yamt tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
82 1.2.4.2 yamt if (tpg == NULL) {
83 1.2.4.2 yamt warnx("%s: error: no secondary GPT header; run recover",
84 1.2.4.2 yamt device_name);
85 1.2.4.2 yamt return;
86 1.2.4.2 yamt }
87 1.2.4.2 yamt
88 1.2.4.2 yamt tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
89 1.2.4.2 yamt lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
90 1.2.4.2 yamt if (tbl == NULL || lbt == NULL) {
91 1.2.4.2 yamt warnx("%s: error: run recover -- trust me", device_name);
92 1.2.4.2 yamt return;
93 1.2.4.2 yamt }
94 1.2.4.2 yamt
95 1.2.4.2 yamt hdr = gpt->map_data;
96 1.2.4.2 yamt if (entry > le32toh(hdr->hdr_entries)) {
97 1.2.4.2 yamt warnx("%s: error: index %u out of range (%u max)", device_name,
98 1.2.4.2 yamt entry, le32toh(hdr->hdr_entries));
99 1.2.4.2 yamt return;
100 1.2.4.2 yamt }
101 1.2.4.2 yamt
102 1.2.4.2 yamt i = entry - 1;
103 1.2.4.2 yamt ent = (void*)((char*)tbl->map_data + i *
104 1.2.4.2 yamt le32toh(hdr->hdr_entsz));
105 1.2.4.2 yamt le_uuid_dec(ent->ent_type, &uuid);
106 1.2.4.2 yamt if (uuid_is_nil(&uuid, NULL)) {
107 1.2.4.2 yamt warnx("%s: error: entry at index %u is unused",
108 1.2.4.2 yamt device_name, entry);
109 1.2.4.2 yamt return;
110 1.2.4.2 yamt }
111 1.2.4.2 yamt
112 1.2.4.2 yamt ent->ent_attr &= ~attributes;
113 1.2.4.2 yamt
114 1.2.4.2 yamt hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
115 1.2.4.2 yamt le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
116 1.2.4.2 yamt hdr->hdr_crc_self = 0;
117 1.2.4.2 yamt hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
118 1.2.4.2 yamt
119 1.2.4.2 yamt gpt_write(fd, gpt);
120 1.2.4.2 yamt gpt_write(fd, tbl);
121 1.2.4.2 yamt
122 1.2.4.2 yamt hdr = tpg->map_data;
123 1.2.4.2 yamt ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
124 1.2.4.2 yamt ent->ent_attr &= ~attributes;
125 1.2.4.2 yamt
126 1.2.4.2 yamt hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
127 1.2.4.2 yamt le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
128 1.2.4.2 yamt hdr->hdr_crc_self = 0;
129 1.2.4.2 yamt hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
130 1.2.4.2 yamt
131 1.2.4.2 yamt gpt_write(fd, lbt);
132 1.2.4.2 yamt gpt_write(fd, tpg);
133 1.2.4.2 yamt
134 1.2.4.2 yamt printf("Partition %d attributes updated\n", entry);
135 1.2.4.2 yamt }
136 1.2.4.2 yamt
137 1.2.4.2 yamt int
138 1.2.4.2 yamt cmd_unset(int argc, char *argv[])
139 1.2.4.2 yamt {
140 1.2.4.2 yamt char *p;
141 1.2.4.2 yamt int ch, fd;
142 1.2.4.2 yamt
143 1.2.4.2 yamt while ((ch = getopt(argc, argv, "a:i:")) != -1) {
144 1.2.4.2 yamt switch(ch) {
145 1.2.4.2 yamt case 'a':
146 1.2.4.2 yamt if (strcmp(optarg, "biosboot") == 0)
147 1.2.4.2 yamt attributes |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE;
148 1.2.4.2 yamt else if (strcmp(optarg, "bootme") == 0)
149 1.2.4.2 yamt attributes |= GPT_ENT_ATTR_BOOTME;
150 1.2.4.2 yamt else if (strcmp(optarg, "bootonce") == 0)
151 1.2.4.2 yamt attributes |= GPT_ENT_ATTR_BOOTONCE;
152 1.2.4.2 yamt else if (strcmp(optarg, "bootfailed") == 0)
153 1.2.4.2 yamt attributes |= GPT_ENT_ATTR_BOOTFAILED;
154 1.2.4.2 yamt else
155 1.2.4.2 yamt usage_unset();
156 1.2.4.2 yamt break;
157 1.2.4.2 yamt case 'i':
158 1.2.4.2 yamt if (entry > 0)
159 1.2.4.2 yamt usage_unset();
160 1.2.4.2 yamt entry = strtoul(optarg, &p, 10);
161 1.2.4.2 yamt if (*p != 0 || entry < 1)
162 1.2.4.2 yamt usage_unset();
163 1.2.4.2 yamt break;
164 1.2.4.2 yamt default:
165 1.2.4.2 yamt usage_unset();
166 1.2.4.2 yamt }
167 1.2.4.2 yamt }
168 1.2.4.2 yamt
169 1.2.4.2 yamt if (argc == optind)
170 1.2.4.2 yamt usage_unset();
171 1.2.4.2 yamt
172 1.2.4.2 yamt if (entry == 0 || attributes == 0)
173 1.2.4.2 yamt usage_unset();
174 1.2.4.2 yamt
175 1.2.4.2 yamt while (optind < argc) {
176 1.2.4.2 yamt fd = gpt_open(argv[optind++]);
177 1.2.4.2 yamt if (fd == -1) {
178 1.2.4.2 yamt warn("unable to open device '%s'", device_name);
179 1.2.4.2 yamt continue;
180 1.2.4.2 yamt }
181 1.2.4.2 yamt
182 1.2.4.2 yamt unset(fd);
183 1.2.4.2 yamt
184 1.2.4.2 yamt gpt_close(fd);
185 1.2.4.2 yamt }
186 1.2.4.2 yamt
187 1.2.4.2 yamt return 0;
188 1.2.4.2 yamt }
189