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