gpt_uuid.c revision 1.6
1/*	$NetBSD: gpt_uuid.c,v 1.6 2014/10/03 00:51:31 jnemeth 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.6 2014/10/03 00:51:31 jnemeth Exp $");
36#endif
37
38#include <stdio.h>
39
40#include "map.h"
41#include "gpt.h"
42
43#if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H)
44#include <sys/endian.h>
45#endif
46
47#if !defined(HAVE_NBTOOL_CONFIG_H)
48#include <sys/types.h>
49#include <sys/uuid.h>
50#endif
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_BIOS, "bios", "BIOS Boot" },
70	{ GPT_ENT_TYPE_EFI, "efi", "EFI System" },
71	{ GPT_ENT_TYPE_FREEBSD, "fbsd-legacy", "FreeBSD legacy" },
72	{ GPT_ENT_TYPE_FREEBSD_SWAP, "fbsd-swap", "FreeBSD swap" },
73	{ GPT_ENT_TYPE_FREEBSD_UFS, "fbsd-ufs", "FreeBSD UFS/UFS2" },
74	{ GPT_ENT_TYPE_FREEBSD_VINUM, "fbsd-vinum", "FreeBSD vinum" },
75	{ GPT_ENT_TYPE_FREEBSD_ZFS, "fbsd-zfs", "FreeBSD ZFS" },
76	{ GPT_ENT_TYPE_LINUX_DATA, "linux-data", "Linux data" },
77	{ GPT_ENT_TYPE_LINUX_SWAP, "linux-swap", "Linux swap" },
78	{ GPT_ENT_TYPE_MS_BASIC_DATA, "windows", "Windows basic data" },
79	{ GPT_ENT_TYPE_MS_RESERVED, "windows-reserved", "Windows reserved" },
80	{ GPT_ENT_TYPE_NETBSD_CCD, "ccd", "NetBSD ccd component" },
81	{ GPT_ENT_TYPE_NETBSD_CGD, "cgd", "NetBSD Cryptographic Disk" },
82	{ GPT_ENT_TYPE_NETBSD_FFS, "ffs", "NetBSD FFSv1/FFSv2" },
83	{ GPT_ENT_TYPE_NETBSD_LFS, "lfs", "NetBSD LFS" },
84	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME, "raid",
85	    "NetBSD RAIDFrame component" },
86	{ GPT_ENT_TYPE_NETBSD_SWAP, "swap", "NetBSD swap" },
87};
88
89static void
90gpt_uuid_to_dce(const gpt_uuid_t buf, struct dce_uuid *uuid)
91{
92	const uint8_t *p = buf;
93	size_t i;
94
95	uuid->time_low = le32dec(p);
96	uuid->time_mid = le16dec(p + 4);
97	uuid->time_hi_and_version = le16dec(p + 6);
98	uuid->clock_seq_hi_and_reserved = p[8];
99	uuid->clock_seq_low = p[9];
100	for (i = 0; i < sizeof(uuid->node); i++)
101		uuid->node[i] = p[10 + i];
102}
103
104static void
105gpt_dce_to_uuid(const struct dce_uuid *uuid, uint8_t *buf)
106{
107	uint8_t *p = buf;
108	size_t i;
109
110	le32enc(p, uuid->time_low);
111	le16enc(p + 4, uuid->time_mid);
112	le16enc(p + 6, uuid->time_hi_and_version);
113	p[8] = uuid->clock_seq_hi_and_reserved;
114	p[9] = uuid->clock_seq_low;
115	for (i = 0; i < sizeof(uuid->node); i++)
116		p[10 + i] = uuid->node[i];
117}
118
119static int
120gpt_uuid_numeric(char *buf, size_t bufsiz, const struct dce_uuid *u)
121{
122	return snprintf(buf, bufsiz,
123	    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
124	    u->time_low, u->time_mid, u->time_hi_and_version,
125	    u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
126	    u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
127}
128
129
130static int
131gpt_uuid_symbolic(char *buf, size_t bufsiz, const struct dce_uuid *u)
132{
133	size_t i;
134
135	for (i = 0; i < __arraycount(gpt_nv); i++)
136		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
137			return strlcpy(buf, gpt_nv[i].n, bufsiz);
138	return -1;
139}
140
141static int
142gpt_uuid_descriptive(char *buf, size_t bufsiz, const struct dce_uuid *u)
143{
144	size_t i;
145
146	for (i = 0; i < __arraycount(gpt_nv); i++)
147		if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0)
148			return strlcpy(buf, gpt_nv[i].d, bufsiz);
149	return -1;
150}
151
152int
153gpt_uuid_snprintf(char *buf, size_t bufsiz, const char *fmt,
154    const gpt_uuid_t uu)
155{
156	struct dce_uuid u;
157	gpt_uuid_to_dce(uu, &u);
158
159	if (fmt[1] == 's') {
160		int r;
161		if ((r = gpt_uuid_symbolic(buf, bufsiz, &u)) != -1)
162			return r;
163	}
164	if (fmt[1] == 'l') {
165		int r;
166		if ((r = gpt_uuid_descriptive(buf, bufsiz, &u)) != -1)
167			return r;
168	}
169	return gpt_uuid_numeric(buf, bufsiz, &u);
170}
171
172static int
173gpt_uuid_parse_numeric(const char *s, struct dce_uuid *u)
174{
175	int n;
176
177	if (s == NULL || *s == '\0') {
178		memset(u, 0, sizeof(*u));
179		return 0;
180	}
181
182	n = sscanf(s,
183	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
184	    &u->time_low, &u->time_mid, &u->time_hi_and_version,
185	    &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
186	    &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
187
188	/* Make sure we have all conversions. */
189	if (n != 11)
190		return -1;
191
192	/* We have a successful scan. Check semantics... */
193	n = u->clock_seq_hi_and_reserved;
194	if ((n & 0x80) != 0x00 &&			/* variant 0? */
195	    (n & 0xc0) != 0x80 &&			/* variant 1? */
196	    (n & 0xe0) != 0xc0) 			/* variant 2? */
197		return -1;
198	return 0;
199}
200
201static int
202gpt_uuid_parse_symbolic(const char *s, struct dce_uuid *u)
203{
204	size_t i;
205
206	for (i = 0; i < __arraycount(gpt_nv); i++)
207		if (strcmp(gpt_nv[i].n, s) == 0) {
208			*u = gpt_nv[i].u;
209			return 0;
210		}
211	return -1;
212}
213
214int
215gpt_uuid_parse(const char *s, gpt_uuid_t uuid)
216{
217	struct dce_uuid u;
218
219	if (gpt_uuid_parse_numeric(s, &u) != -1) {
220		gpt_dce_to_uuid(&u, uuid);
221		return 0;
222	}
223
224	if (gpt_uuid_parse_symbolic(s, &u) == -1)
225		return -1;
226
227	gpt_dce_to_uuid(&u, uuid);
228	return 0;
229}
230
231void
232gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s)
233{
234	gpt_dce_to_uuid(&gpt_nv[t].u, u);
235	if (b)
236		utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b));
237}
238
239#if !defined(HAVE_NBTOOL_CONFIG_H)
240void
241gpt_uuid_create_new(gpt_uuid_t t)
242{
243	struct uuid u;
244
245	uuidgen(&u, 1);
246	gpt_dce_to_uuid((struct dce_uuid *)&u, t);
247}
248#endif
249