gpt_uuid.c revision 1.15
1/*	$NetBSD: gpt_uuid.c,v 1.15 2017/02/16 22:40:19 christos 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.15 2017/02/16 22:40:19 christos 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#include "gpt_private.h"
46
47#if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H)
48#include <sys/endian.h>
49#endif
50
51
52const gpt_uuid_t gpt_uuid_nil;
53
54struct dce_uuid {
55	uint32_t	time_low;
56	uint16_t	time_mid;
57	uint16_t	time_hi_and_version;
58	uint8_t		clock_seq_hi_and_reserved;
59	uint8_t		clock_seq_low;
60	uint8_t		node[6];
61};
62
63static const struct {
64	struct dce_uuid u;
65	const char *n;
66	const char *d;
67} gpt_nv[] = {
68	{ GPT_ENT_TYPE_APPLE_HFS, "apple", "Apple HFS" },
69	{ GPT_ENT_TYPE_APPLE_UFS, "apple-ufs", "Apple UFS" },
70	{ GPT_ENT_TYPE_BIOS, "bios", "BIOS Boot" },
71	{ GPT_ENT_TYPE_EFI, "efi", "EFI System" },
72	{ GPT_ENT_TYPE_FREEBSD, "fbsd-legacy", "FreeBSD legacy" },
73	{ GPT_ENT_TYPE_FREEBSD_SWAP, "fbsd-swap", "FreeBSD swap" },
74	{ GPT_ENT_TYPE_FREEBSD_UFS, "fbsd-ufs", "FreeBSD UFS/UFS2" },
75	{ GPT_ENT_TYPE_FREEBSD_VINUM, "fbsd-vinum", "FreeBSD vinum" },
76	{ GPT_ENT_TYPE_FREEBSD_ZFS, "fbsd-zfs", "FreeBSD ZFS" },
77	{ GPT_ENT_TYPE_LINUX_DATA, "linux-data", "Linux data" },
78	{ GPT_ENT_TYPE_LINUX_RAID, "linux-raid", "Linux RAID" },
79	{ GPT_ENT_TYPE_LINUX_SWAP, "linux-swap", "Linux swap" },
80	{ GPT_ENT_TYPE_LINUX_LVM, "linux-lvm", "Linux LVM" },
81	{ GPT_ENT_TYPE_MS_BASIC_DATA, "windows", "Windows basic data" },
82	{ GPT_ENT_TYPE_MS_RESERVED, "windows-reserved", "Windows reserved" },
83	{ GPT_ENT_TYPE_NETBSD_CCD, "ccd", "NetBSD ccd component" },
84	{ GPT_ENT_TYPE_NETBSD_CGD, "cgd", "NetBSD Cryptographic Disk" },
85	{ GPT_ENT_TYPE_NETBSD_FFS, "ffs", "NetBSD FFSv1/FFSv2" },
86	{ GPT_ENT_TYPE_NETBSD_LFS, "lfs", "NetBSD LFS" },
87	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME, "raid",
88	    "NetBSD RAIDFrame component" },
89	{ GPT_ENT_TYPE_NETBSD_SWAP, "swap", "NetBSD swap" },
90};
91
92static void
93gpt_uuid_to_dce(const gpt_uuid_t buf, struct dce_uuid *uuid)
94{
95	const uint8_t *p = buf;
96	size_t i;
97
98	uuid->time_low = le32dec(p);
99	uuid->time_mid = le16dec(p + 4);
100	uuid->time_hi_and_version = le16dec(p + 6);
101	uuid->clock_seq_hi_and_reserved = p[8];
102	uuid->clock_seq_low = p[9];
103	for (i = 0; i < sizeof(uuid->node); i++)
104		uuid->node[i] = p[10 + i];
105}
106
107static void
108gpt_dce_to_uuid(const struct dce_uuid *uuid, uint8_t *buf)
109{
110	uint8_t *p = buf;
111	size_t i;
112
113	le32enc(p, uuid->time_low);
114	le16enc(p + 4, uuid->time_mid);
115	le16enc(p + 6, uuid->time_hi_and_version);
116	p[8] = uuid->clock_seq_hi_and_reserved;
117	p[9] = uuid->clock_seq_low;
118	for (i = 0; i < sizeof(uuid->node); i++)
119		p[10 + i] = uuid->node[i];
120}
121
122static int
123gpt_uuid_numeric(char *buf, size_t bufsiz, const struct dce_uuid *u)
124{
125	return snprintf(buf, bufsiz,
126	    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
127	    u->time_low, u->time_mid, u->time_hi_and_version,
128	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
129	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
130}
131
132
133static int
134gpt_uuid_symbolic(char *buf, size_t bufsiz, const struct dce_uuid *u)
135{
136	size_t i;
137
138	for (i = 0; i < __arraycount(gpt_nv); i++)
139		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
140			return (int)strlcpy(buf, gpt_nv[i].n, bufsiz);
141	return -1;
142}
143
144static int
145gpt_uuid_descriptive(char *buf, size_t bufsiz, const struct dce_uuid *u)
146{
147	size_t i;
148
149	for (i = 0; i < __arraycount(gpt_nv); i++)
150		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
151			return (int)strlcpy(buf, gpt_nv[i].d, bufsiz);
152	return -1;
153}
154
155int
156gpt_uuid_snprintf(char *buf, size_t bufsiz, const char *fmt,
157    const gpt_uuid_t uu)
158{
159	struct dce_uuid u;
160	gpt_uuid_to_dce(uu, &u);
161
162	if (fmt[1] == 's') {
163		int r;
164		if ((r = gpt_uuid_symbolic(buf, bufsiz, &u)) != -1)
165			return r;
166	}
167	if (fmt[1] == 'l') {
168		int r;
169		if ((r = gpt_uuid_descriptive(buf, bufsiz, &u)) != -1)
170			return r;
171	}
172	return gpt_uuid_numeric(buf, bufsiz, &u);
173}
174
175static int
176gpt_uuid_parse_numeric(const char *s, struct dce_uuid *u)
177{
178	int n;
179
180	if (s == NULL || *s == '\0') {
181		memset(u, 0, sizeof(*u));
182		return 0;
183	}
184
185	n = sscanf(s,
186	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
187	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
188	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
189	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
190
191	/* Make sure we have all conversions. */
192	if (n != 11)
193		return -1;
194
195	/* We have a successful scan. Check semantics... */
196	n = u->clock_seq_hi_and_reserved;
197	if ((n & 0x80) != 0x00 &&			/* variant 0? */
198	    (n & 0xc0) != 0x80 &&			/* variant 1? */
199	    (n & 0xe0) != 0xc0) 			/* variant 2? */
200		return -1;
201	return 0;
202}
203
204static int
205gpt_uuid_parse_symbolic(const char *s, struct dce_uuid *u)
206{
207	size_t i;
208
209	for (i = 0; i < __arraycount(gpt_nv); i++)
210		if (strcmp(gpt_nv[i].n, s) == 0) {
211			*u = gpt_nv[i].u;
212			return 0;
213		}
214	return -1;
215}
216
217int
218gpt_uuid_parse(const char *s, gpt_uuid_t uuid)
219{
220	struct dce_uuid u;
221
222	if (gpt_uuid_parse_numeric(s, &u) != -1) {
223		gpt_dce_to_uuid(&u, uuid);
224		return 0;
225	}
226
227	if (gpt_uuid_parse_symbolic(s, &u) == -1)
228		return -1;
229
230	gpt_dce_to_uuid(&u, uuid);
231	return 0;
232}
233
234void
235gpt_uuid_help(const char *prefix)
236{
237	size_t i;
238
239	for (i = 0; i < __arraycount(gpt_nv); i++)
240		printf("%s%18.18s\t%s\n", prefix, gpt_nv[i].n, gpt_nv[i].d);
241}
242
243void
244gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s)
245{
246	gpt_dce_to_uuid(&gpt_nv[t].u, u);
247	if (b)
248		utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b));
249}
250
251static int
252gpt_uuid_random(gpt_t gpt, void *v, size_t n)
253{
254	int fd;
255	uint8_t *p;
256	ssize_t nread;
257
258	/* Randomly generate the content.  */
259	fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
260	if (fd == -1) {
261		gpt_warn(gpt, "Can't open `/dev/urandom'");
262		return -1;
263	}
264	for (p = v;  n > 0; p += nread, n -= (size_t)nread) {
265		nread = read(fd, p, n);
266		if (nread < 0) {
267			gpt_warn(gpt, "Can't read `/dev/urandom'");
268			goto out;
269		}
270		if (nread == 0) {
271			gpt_warn(gpt, "EOF from /dev/urandom");
272			goto out;
273		}
274		if ((size_t)nread > n) {
275			gpt_warnx(gpt, "read too much: %zd > %zu", nread, n);
276			goto out;
277		}
278	}
279	(void)close(fd);
280	return 0;
281out:
282	(void)close(fd);
283	return -1;
284}
285
286static int
287gpt_uuid_tstamp(gpt_t gpt, void *v, size_t l)
288{
289	uint8_t *p;
290	// Perhaps use SHA?
291	uint32_t x = (uint32_t)gpt->timestamp;
292
293	for (p = v; l > 0; p += sizeof(x), l -= sizeof(x))
294		memcpy(p, &x, sizeof(x));
295
296	return 0;
297}
298
299int
300gpt_uuid_generate(gpt_t gpt, gpt_uuid_t t)
301{
302	int rv;
303	struct dce_uuid u;
304	if (gpt->flags & GPT_TIMESTAMP)
305		rv = gpt_uuid_tstamp(gpt, &u, sizeof(u));
306	else
307		rv = gpt_uuid_random(gpt, &u, sizeof(u));
308
309	if (rv == -1)
310		return -1;
311
312	/* Set the version number to 4.  */
313	u.time_hi_and_version &= (uint16_t)~0xf000;
314	u.time_hi_and_version |= 0x4000;
315
316	/* Fix the reserved bits.  */
317	u.clock_seq_hi_and_reserved &= (uint8_t)~0x40;
318	u.clock_seq_hi_and_reserved |= 0x80;
319
320	gpt_dce_to_uuid(&u, t);
321	return 0;
322}
323