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