gpt_uuid.c revision 1.9
1/*	$NetBSD: gpt_uuid.c,v 1.9 2014/10/04 11:23:35 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#if HAVE_NBTOOL_CONFIG_H
30#include "nbtool_config.h"
31#endif
32
33#include <sys/cdefs.h>
34#ifdef __RCSID
35__RCSID("$NetBSD: gpt_uuid.c,v 1.9 2014/10/04 11:23:35 riastradh Exp $");
36#endif
37
38#include <err.h>
39#include <fcntl.h>
40#include <stdio.h>
41#include <unistd.h>
42
43#include "map.h"
44#include "gpt.h"
45
46#if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H)
47#include <sys/endian.h>
48#endif
49
50
51const gpt_uuid_t gpt_uuid_nil;
52
53struct dce_uuid {
54	uint32_t	time_low;
55	uint16_t	time_mid;
56	uint16_t	time_hi_and_version;
57	uint8_t		clock_seq_hi_and_reserved;
58	uint8_t		clock_seq_low;
59	uint8_t		node[6];
60};
61
62static const struct {
63	struct dce_uuid u;
64	const char *n;
65	const char *d;
66} gpt_nv[] = {
67	{ GPT_ENT_TYPE_APPLE_HFS, "apple", "Apple HFS" },
68	{ GPT_ENT_TYPE_BIOS, "bios", "BIOS Boot" },
69	{ GPT_ENT_TYPE_EFI, "efi", "EFI System" },
70	{ GPT_ENT_TYPE_FREEBSD, "fbsd-legacy", "FreeBSD legacy" },
71	{ GPT_ENT_TYPE_FREEBSD_SWAP, "fbsd-swap", "FreeBSD swap" },
72	{ GPT_ENT_TYPE_FREEBSD_UFS, "fbsd-ufs", "FreeBSD UFS/UFS2" },
73	{ GPT_ENT_TYPE_FREEBSD_VINUM, "fbsd-vinum", "FreeBSD vinum" },
74	{ GPT_ENT_TYPE_FREEBSD_ZFS, "fbsd-zfs", "FreeBSD ZFS" },
75	{ GPT_ENT_TYPE_LINUX_DATA, "linux-data", "Linux data" },
76	{ GPT_ENT_TYPE_LINUX_SWAP, "linux-swap", "Linux swap" },
77	{ GPT_ENT_TYPE_MS_BASIC_DATA, "windows", "Windows basic data" },
78	{ GPT_ENT_TYPE_MS_RESERVED, "windows-reserved", "Windows reserved" },
79	{ GPT_ENT_TYPE_NETBSD_CCD, "ccd", "NetBSD ccd component" },
80	{ GPT_ENT_TYPE_NETBSD_CGD, "cgd", "NetBSD Cryptographic Disk" },
81	{ GPT_ENT_TYPE_NETBSD_FFS, "ffs", "NetBSD FFSv1/FFSv2" },
82	{ GPT_ENT_TYPE_NETBSD_LFS, "lfs", "NetBSD LFS" },
83	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME, "raid",
84	    "NetBSD RAIDFrame component" },
85	{ GPT_ENT_TYPE_NETBSD_SWAP, "swap", "NetBSD swap" },
86};
87
88static void
89gpt_uuid_to_dce(const gpt_uuid_t buf, struct dce_uuid *uuid)
90{
91	const uint8_t *p = buf;
92	size_t i;
93
94	uuid->time_low = le32dec(p);
95	uuid->time_mid = le16dec(p + 4);
96	uuid->time_hi_and_version = le16dec(p + 6);
97	uuid->clock_seq_hi_and_reserved = p[8];
98	uuid->clock_seq_low = p[9];
99	for (i = 0; i < sizeof(uuid->node); i++)
100		uuid->node[i] = p[10 + i];
101}
102
103static void
104gpt_dce_to_uuid(const struct dce_uuid *uuid, uint8_t *buf)
105{
106	uint8_t *p = buf;
107	size_t i;
108
109	le32enc(p, uuid->time_low);
110	le16enc(p + 4, uuid->time_mid);
111	le16enc(p + 6, uuid->time_hi_and_version);
112	p[8] = uuid->clock_seq_hi_and_reserved;
113	p[9] = uuid->clock_seq_low;
114	for (i = 0; i < sizeof(uuid->node); i++)
115		p[10 + i] = uuid->node[i];
116}
117
118static int
119gpt_uuid_numeric(char *buf, size_t bufsiz, const struct dce_uuid *u)
120{
121	return snprintf(buf, bufsiz,
122	    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
123	    u->time_low, u->time_mid, u->time_hi_and_version,
124	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
125	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
126}
127
128
129static int
130gpt_uuid_symbolic(char *buf, size_t bufsiz, const struct dce_uuid *u)
131{
132	size_t i;
133
134	for (i = 0; i < __arraycount(gpt_nv); i++)
135		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
136			return strlcpy(buf, gpt_nv[i].n, bufsiz);
137	return -1;
138}
139
140static int
141gpt_uuid_descriptive(char *buf, size_t bufsiz, const struct dce_uuid *u)
142{
143	size_t i;
144
145	for (i = 0; i < __arraycount(gpt_nv); i++)
146		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
147			return strlcpy(buf, gpt_nv[i].d, bufsiz);
148	return -1;
149}
150
151int
152gpt_uuid_snprintf(char *buf, size_t bufsiz, const char *fmt,
153    const gpt_uuid_t uu)
154{
155	struct dce_uuid u;
156	gpt_uuid_to_dce(uu, &u);
157
158	if (fmt[1] == 's') {
159		int r;
160		if ((r = gpt_uuid_symbolic(buf, bufsiz, &u)) != -1)
161			return r;
162	}
163	if (fmt[1] == 'l') {
164		int r;
165		if ((r = gpt_uuid_descriptive(buf, bufsiz, &u)) != -1)
166			return r;
167	}
168	return gpt_uuid_numeric(buf, bufsiz, &u);
169}
170
171static int
172gpt_uuid_parse_numeric(const char *s, struct dce_uuid *u)
173{
174	int n;
175
176	if (s == NULL || *s == '\0') {
177		memset(u, 0, sizeof(*u));
178		return 0;
179	}
180
181	n = sscanf(s,
182	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
183	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
184	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
185	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
186
187	/* Make sure we have all conversions. */
188	if (n != 11)
189		return -1;
190
191	/* We have a successful scan. Check semantics... */
192	n = u->clock_seq_hi_and_reserved;
193	if ((n & 0x80) != 0x00 &&			/* variant 0? */
194	    (n & 0xc0) != 0x80 &&			/* variant 1? */
195	    (n & 0xe0) != 0xc0) 			/* variant 2? */
196		return -1;
197	return 0;
198}
199
200static int
201gpt_uuid_parse_symbolic(const char *s, struct dce_uuid *u)
202{
203	size_t i;
204
205	for (i = 0; i < __arraycount(gpt_nv); i++)
206		if (strcmp(gpt_nv[i].n, s) == 0) {
207			*u = gpt_nv[i].u;
208			return 0;
209		}
210	return -1;
211}
212
213int
214gpt_uuid_parse(const char *s, gpt_uuid_t uuid)
215{
216	struct dce_uuid u;
217
218	if (gpt_uuid_parse_numeric(s, &u) != -1) {
219		gpt_dce_to_uuid(&u, uuid);
220		return 0;
221	}
222
223	if (gpt_uuid_parse_symbolic(s, &u) == -1)
224		return -1;
225
226	gpt_dce_to_uuid(&u, uuid);
227	return 0;
228}
229
230void
231gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s)
232{
233	gpt_dce_to_uuid(&gpt_nv[t].u, u);
234	if (b)
235		utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b));
236}
237
238void
239gpt_uuid_generate(gpt_uuid_t t)
240{
241	struct dce_uuid u;
242	int fd;
243	uint8_t *p;
244	size_t n;
245	ssize_t nread;
246
247	/* Randomly generate the content.  */
248	fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
249	if (fd == -1)
250		err(1, "open(/dev/urandom)");
251	for (p = (void *)&u, n = sizeof u; 0 < n; p += nread, n -= nread) {
252		nread = read(fd, p, n);
253		if (nread < 0)
254			err(1, "read(/dev/urandom)");
255		if (nread == 0)
256			errx(1, "EOF from /dev/urandom");
257		if ((size_t)nread > n)
258			errx(1, "read too much: %zd > %zu", nread, n);
259	}
260	(void)close(fd);
261
262	/* Set the version number to 4.  */
263	u.time_hi_and_version &= ~(uint32_t)0xf000;
264	u.time_hi_and_version |= 0x4000;
265
266	/* Fix the reserved bits.  */
267	u.clock_seq_hi_and_reserved &= ~(uint8_t)0x40;
268	u.clock_seq_hi_and_reserved |= 0x80;
269
270	gpt_dce_to_uuid(&u, t);
271}
272