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