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