1 1.25 christos /* $NetBSD: gpt_uuid.c,v 1.25 2025/02/23 20:47:19 christos Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2014 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * Redistribution and use in source and binary forms, with or without 8 1.1 christos * modification, are permitted provided that the following conditions 9 1.1 christos * are met: 10 1.1 christos * 1. Redistributions of source code must retain the above copyright 11 1.1 christos * notice, this list of conditions and the following disclaimer. 12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 christos * notice, this list of conditions and the following disclaimer in the 14 1.1 christos * documentation and/or other materials provided with the distribution. 15 1.1 christos * 16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 27 1.1 christos */ 28 1.1 christos 29 1.1 christos #if HAVE_NBTOOL_CONFIG_H 30 1.1 christos #include "nbtool_config.h" 31 1.1 christos #endif 32 1.1 christos 33 1.1 christos #include <sys/cdefs.h> 34 1.1 christos #ifdef __RCSID 35 1.25 christos __RCSID("$NetBSD: gpt_uuid.c,v 1.25 2025/02/23 20:47:19 christos Exp $"); 36 1.1 christos #endif 37 1.1 christos 38 1.8 riastrad #include <err.h> 39 1.8 riastrad #include <fcntl.h> 40 1.1 christos #include <stdio.h> 41 1.8 riastrad #include <unistd.h> 42 1.1 christos 43 1.1 christos #include "map.h" 44 1.1 christos #include "gpt.h" 45 1.14 christos #include "gpt_private.h" 46 1.1 christos 47 1.5 apb #if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H) 48 1.1 christos #include <sys/endian.h> 49 1.1 christos #endif 50 1.1 christos 51 1.6 jnemeth 52 1.1 christos const gpt_uuid_t gpt_uuid_nil; 53 1.1 christos 54 1.1 christos struct dce_uuid { 55 1.1 christos uint32_t time_low; 56 1.1 christos uint16_t time_mid; 57 1.1 christos uint16_t time_hi_and_version; 58 1.1 christos uint8_t clock_seq_hi_and_reserved; 59 1.1 christos uint8_t clock_seq_low; 60 1.1 christos uint8_t node[6]; 61 1.1 christos }; 62 1.1 christos 63 1.1 christos static const struct { 64 1.1 christos struct dce_uuid u; 65 1.1 christos const char *n; 66 1.1 christos const char *d; 67 1.1 christos } gpt_nv[] = { 68 1.21 riastrad /* Must match the gpt_type_t enum in gpt_uuid.h */ 69 1.1 christos { GPT_ENT_TYPE_APPLE_HFS, "apple", "Apple HFS" }, 70 1.10 mlelstv { GPT_ENT_TYPE_APPLE_UFS, "apple-ufs", "Apple UFS" }, 71 1.1 christos { GPT_ENT_TYPE_BIOS, "bios", "BIOS Boot" }, 72 1.1 christos { GPT_ENT_TYPE_EFI, "efi", "EFI System" }, 73 1.1 christos { GPT_ENT_TYPE_FREEBSD, "fbsd-legacy", "FreeBSD legacy" }, 74 1.1 christos { GPT_ENT_TYPE_FREEBSD_SWAP, "fbsd-swap", "FreeBSD swap" }, 75 1.1 christos { GPT_ENT_TYPE_FREEBSD_UFS, "fbsd-ufs", "FreeBSD UFS/UFS2" }, 76 1.1 christos { GPT_ENT_TYPE_FREEBSD_VINUM, "fbsd-vinum", "FreeBSD vinum" }, 77 1.19 martin { GPT_ENT_TYPE_FREEBSD_ZFS, "zfs", "ZFS" }, 78 1.1 christos { GPT_ENT_TYPE_LINUX_DATA, "linux-data", "Linux data" }, 79 1.10 mlelstv { GPT_ENT_TYPE_LINUX_RAID, "linux-raid", "Linux RAID" }, 80 1.1 christos { GPT_ENT_TYPE_LINUX_SWAP, "linux-swap", "Linux swap" }, 81 1.10 mlelstv { GPT_ENT_TYPE_LINUX_LVM, "linux-lvm", "Linux LVM" }, 82 1.1 christos { GPT_ENT_TYPE_MS_BASIC_DATA, "windows", "Windows basic data" }, 83 1.1 christos { GPT_ENT_TYPE_MS_RESERVED, "windows-reserved", "Windows reserved" }, 84 1.20 kre { GPT_ENT_TYPE_MS_RECOVERY, "windows-recovery", "Windows recovery" }, 85 1.1 christos { GPT_ENT_TYPE_NETBSD_CCD, "ccd", "NetBSD ccd component" }, 86 1.1 christos { GPT_ENT_TYPE_NETBSD_CGD, "cgd", "NetBSD Cryptographic Disk" }, 87 1.1 christos { GPT_ENT_TYPE_NETBSD_FFS, "ffs", "NetBSD FFSv1/FFSv2" }, 88 1.1 christos { GPT_ENT_TYPE_NETBSD_LFS, "lfs", "NetBSD LFS" }, 89 1.1 christos { GPT_ENT_TYPE_NETBSD_RAIDFRAME, "raid", 90 1.1 christos "NetBSD RAIDFrame component" }, 91 1.1 christos { GPT_ENT_TYPE_NETBSD_SWAP, "swap", "NetBSD swap" }, 92 1.22 christos { GPT_ENT_TYPE_OPENBSD_DATA, "obsd", "OpenBSD data" }, 93 1.17 martin { GPT_ENT_TYPE_VMWARE_VMKCORE, "vmcore", "VMware VMkernel core dump" }, 94 1.17 martin { GPT_ENT_TYPE_VMWARE_VMFS, "vmfs", "VMware VMFS" }, 95 1.17 martin { GPT_ENT_TYPE_VMWARE_RESERVED, "vmresered", "VMware reserved" }, 96 1.1 christos }; 97 1.1 christos 98 1.1 christos static void 99 1.1 christos gpt_uuid_to_dce(const gpt_uuid_t buf, struct dce_uuid *uuid) 100 1.1 christos { 101 1.1 christos const uint8_t *p = buf; 102 1.1 christos size_t i; 103 1.1 christos 104 1.1 christos uuid->time_low = le32dec(p); 105 1.1 christos uuid->time_mid = le16dec(p + 4); 106 1.1 christos uuid->time_hi_and_version = le16dec(p + 6); 107 1.1 christos uuid->clock_seq_hi_and_reserved = p[8]; 108 1.1 christos uuid->clock_seq_low = p[9]; 109 1.1 christos for (i = 0; i < sizeof(uuid->node); i++) 110 1.1 christos uuid->node[i] = p[10 + i]; 111 1.1 christos } 112 1.1 christos 113 1.1 christos static void 114 1.1 christos gpt_dce_to_uuid(const struct dce_uuid *uuid, uint8_t *buf) 115 1.1 christos { 116 1.1 christos uint8_t *p = buf; 117 1.1 christos size_t i; 118 1.1 christos 119 1.1 christos le32enc(p, uuid->time_low); 120 1.1 christos le16enc(p + 4, uuid->time_mid); 121 1.1 christos le16enc(p + 6, uuid->time_hi_and_version); 122 1.1 christos p[8] = uuid->clock_seq_hi_and_reserved; 123 1.1 christos p[9] = uuid->clock_seq_low; 124 1.1 christos for (i = 0; i < sizeof(uuid->node); i++) 125 1.1 christos p[10 + i] = uuid->node[i]; 126 1.1 christos } 127 1.1 christos 128 1.1 christos static int 129 1.1 christos gpt_uuid_numeric(char *buf, size_t bufsiz, const struct dce_uuid *u) 130 1.1 christos { 131 1.1 christos return snprintf(buf, bufsiz, 132 1.1 christos "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 133 1.1 christos u->time_low, u->time_mid, u->time_hi_and_version, 134 1.1 christos u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0], 135 1.1 christos u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]); 136 1.1 christos } 137 1.1 christos 138 1.1 christos 139 1.1 christos static int 140 1.1 christos gpt_uuid_symbolic(char *buf, size_t bufsiz, const struct dce_uuid *u) 141 1.1 christos { 142 1.1 christos size_t i; 143 1.1 christos 144 1.1 christos for (i = 0; i < __arraycount(gpt_nv); i++) 145 1.1 christos if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0) 146 1.12 christos return (int)strlcpy(buf, gpt_nv[i].n, bufsiz); 147 1.1 christos return -1; 148 1.1 christos } 149 1.1 christos 150 1.2 jnemeth static int 151 1.2 jnemeth gpt_uuid_descriptive(char *buf, size_t bufsiz, const struct dce_uuid *u) 152 1.2 jnemeth { 153 1.2 jnemeth size_t i; 154 1.2 jnemeth 155 1.2 jnemeth for (i = 0; i < __arraycount(gpt_nv); i++) 156 1.2 jnemeth if (memcmp(&gpt_nv[i].u, u, sizeof(*u)) == 0) 157 1.12 christos return (int)strlcpy(buf, gpt_nv[i].d, bufsiz); 158 1.2 jnemeth return -1; 159 1.2 jnemeth } 160 1.2 jnemeth 161 1.1 christos int 162 1.1 christos gpt_uuid_snprintf(char *buf, size_t bufsiz, const char *fmt, 163 1.1 christos const gpt_uuid_t uu) 164 1.1 christos { 165 1.1 christos struct dce_uuid u; 166 1.1 christos gpt_uuid_to_dce(uu, &u); 167 1.1 christos 168 1.1 christos if (fmt[1] == 's') { 169 1.1 christos int r; 170 1.1 christos if ((r = gpt_uuid_symbolic(buf, bufsiz, &u)) != -1) 171 1.1 christos return r; 172 1.1 christos } 173 1.2 jnemeth if (fmt[1] == 'l') { 174 1.2 jnemeth int r; 175 1.2 jnemeth if ((r = gpt_uuid_descriptive(buf, bufsiz, &u)) != -1) 176 1.2 jnemeth return r; 177 1.2 jnemeth } 178 1.1 christos return gpt_uuid_numeric(buf, bufsiz, &u); 179 1.1 christos } 180 1.1 christos 181 1.1 christos static int 182 1.1 christos gpt_uuid_parse_numeric(const char *s, struct dce_uuid *u) 183 1.1 christos { 184 1.1 christos int n; 185 1.1 christos 186 1.1 christos if (s == NULL || *s == '\0') { 187 1.1 christos memset(u, 0, sizeof(*u)); 188 1.1 christos return 0; 189 1.1 christos } 190 1.1 christos 191 1.1 christos n = sscanf(s, 192 1.1 christos "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", 193 1.1 christos &u->time_low, &u->time_mid, &u->time_hi_and_version, 194 1.1 christos &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0], 195 1.1 christos &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]); 196 1.1 christos 197 1.1 christos /* Make sure we have all conversions. */ 198 1.1 christos if (n != 11) 199 1.1 christos return -1; 200 1.1 christos 201 1.1 christos /* We have a successful scan. Check semantics... */ 202 1.1 christos n = u->clock_seq_hi_and_reserved; 203 1.1 christos if ((n & 0x80) != 0x00 && /* variant 0? */ 204 1.1 christos (n & 0xc0) != 0x80 && /* variant 1? */ 205 1.1 christos (n & 0xe0) != 0xc0) /* variant 2? */ 206 1.1 christos return -1; 207 1.1 christos return 0; 208 1.1 christos } 209 1.1 christos 210 1.1 christos static int 211 1.1 christos gpt_uuid_parse_symbolic(const char *s, struct dce_uuid *u) 212 1.1 christos { 213 1.1 christos size_t i; 214 1.1 christos 215 1.1 christos for (i = 0; i < __arraycount(gpt_nv); i++) 216 1.1 christos if (strcmp(gpt_nv[i].n, s) == 0) { 217 1.1 christos *u = gpt_nv[i].u; 218 1.1 christos return 0; 219 1.1 christos } 220 1.1 christos return -1; 221 1.1 christos } 222 1.1 christos 223 1.1 christos int 224 1.1 christos gpt_uuid_parse(const char *s, gpt_uuid_t uuid) 225 1.1 christos { 226 1.1 christos struct dce_uuid u; 227 1.1 christos 228 1.3 jnemeth if (gpt_uuid_parse_numeric(s, &u) != -1) { 229 1.3 jnemeth gpt_dce_to_uuid(&u, uuid); 230 1.1 christos return 0; 231 1.3 jnemeth } 232 1.1 christos 233 1.1 christos if (gpt_uuid_parse_symbolic(s, &u) == -1) 234 1.1 christos return -1; 235 1.1 christos 236 1.1 christos gpt_dce_to_uuid(&u, uuid); 237 1.1 christos return 0; 238 1.1 christos } 239 1.1 christos 240 1.17 martin size_t 241 1.17 martin gpt_uuid_query( 242 1.17 martin void (*func)(const char *uuid, const char *short_name, const char *desc)) 243 1.17 martin { 244 1.17 martin size_t i; 245 1.17 martin char buf[64]; 246 1.17 martin 247 1.17 martin if (func != NULL) { 248 1.17 martin for (i = 0; i < __arraycount(gpt_nv); i++) { 249 1.17 martin gpt_uuid_numeric(buf, sizeof(buf), &gpt_nv[i].u); 250 1.17 martin (*func)(buf, gpt_nv[i].n, gpt_nv[i].d); 251 1.17 martin } 252 1.17 martin } 253 1.17 martin return __arraycount(gpt_nv); 254 1.17 martin } 255 1.17 martin 256 1.17 martin #ifndef GPT_UUID_QUERY_ONLY 257 1.1 christos void 258 1.13 christos gpt_uuid_help(const char *prefix) 259 1.13 christos { 260 1.13 christos size_t i; 261 1.13 christos 262 1.13 christos for (i = 0; i < __arraycount(gpt_nv); i++) 263 1.13 christos printf("%s%18.18s\t%s\n", prefix, gpt_nv[i].n, gpt_nv[i].d); 264 1.13 christos } 265 1.13 christos 266 1.13 christos void 267 1.1 christos gpt_uuid_create(gpt_type_t t, gpt_uuid_t u, uint16_t *b, size_t s) 268 1.1 christos { 269 1.1 christos gpt_dce_to_uuid(&gpt_nv[t].u, u); 270 1.1 christos if (b) 271 1.1 christos utf8_to_utf16((const uint8_t *)gpt_nv[t].d, b, s / sizeof(*b)); 272 1.1 christos } 273 1.6 jnemeth 274 1.14 christos static int 275 1.23 mlelstv gpt_uuid_random(gpt_t gpt, struct dce_uuid *u, size_t n) 276 1.6 jnemeth { 277 1.8 riastrad int fd; 278 1.8 riastrad uint8_t *p; 279 1.8 riastrad ssize_t nread; 280 1.8 riastrad 281 1.8 riastrad /* Randomly generate the content. */ 282 1.8 riastrad fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); 283 1.11 christos if (fd == -1) { 284 1.11 christos gpt_warn(gpt, "Can't open `/dev/urandom'"); 285 1.11 christos return -1; 286 1.11 christos } 287 1.23 mlelstv for (p = (uint8_t *)u; n > 0; p += nread, n -= (size_t)nread) { 288 1.8 riastrad nread = read(fd, p, n); 289 1.11 christos if (nread < 0) { 290 1.11 christos gpt_warn(gpt, "Can't read `/dev/urandom'"); 291 1.11 christos goto out; 292 1.11 christos } 293 1.11 christos if (nread == 0) { 294 1.11 christos gpt_warn(gpt, "EOF from /dev/urandom"); 295 1.11 christos goto out; 296 1.11 christos } 297 1.11 christos if ((size_t)nread > n) { 298 1.11 christos gpt_warnx(gpt, "read too much: %zd > %zu", nread, n); 299 1.11 christos goto out; 300 1.11 christos } 301 1.8 riastrad } 302 1.8 riastrad (void)close(fd); 303 1.23 mlelstv 304 1.23 mlelstv /* Set the version number to 4. */ 305 1.23 mlelstv u->time_hi_and_version &= (uint16_t)~0xf000; 306 1.23 mlelstv u->time_hi_and_version |= 0x4000; 307 1.23 mlelstv 308 1.14 christos return 0; 309 1.14 christos out: 310 1.14 christos (void)close(fd); 311 1.14 christos return -1; 312 1.14 christos } 313 1.14 christos 314 1.23 mlelstv /* 315 1.24 andvar * For reproducible builds, we can base UUIDs on one external timestamp. 316 1.23 mlelstv * 317 1.23 mlelstv * Bump timestamp by one 100ns unit to make them unique within a GPT. 318 1.23 mlelstv * Use zero clock sequence and node id, ideally these should also be 319 1.23 mlelstv * passed as input. 320 1.23 mlelstv */ 321 1.14 christos static int 322 1.25 christos gpt_uuid_tstamp(gpt_t gpt, struct dce_uuid *u, size_t l __unused) 323 1.14 christos { 324 1.23 mlelstv uint64_t x; 325 1.23 mlelstv 326 1.23 mlelstv /* check for underflow/overflow of 60bit UUID time */ 327 1.23 mlelstv if (gpt->timestamp < -12219292800 || 328 1.23 mlelstv gpt->timestamp > 103072857660) 329 1.23 mlelstv return -1; 330 1.14 christos 331 1.23 mlelstv /* 332 1.23 mlelstv * Convert to UUID epoch (Gregorian) 333 1.23 mlelstv * and 100ns units 334 1.23 mlelstv */ 335 1.23 mlelstv x = (uint64_t)(gpt->timestamp + 12219292800) * 10000000; 336 1.23 mlelstv 337 1.23 mlelstv /* Make UUID unique */ 338 1.23 mlelstv x += gpt->uuidgen++; 339 1.23 mlelstv 340 1.23 mlelstv /* Set UUID fields for version 1 */ 341 1.25 christos u->time_low = x & UINT64_C(0xffffffff); 342 1.23 mlelstv u->time_mid = (x >> 32) & 0xffff; 343 1.23 mlelstv u->time_hi_and_version = 0x1000 | ((x >> 48) & 0xfff); 344 1.23 mlelstv 345 1.23 mlelstv /* 346 1.23 mlelstv * The clock sequence should make UUIDs unique in case 347 1.23 mlelstv * the clock went backwards. 348 1.23 mlelstv */ 349 1.23 mlelstv u->clock_seq_hi_and_reserved = 0; 350 1.23 mlelstv u->clock_seq_low = 0; 351 1.23 mlelstv 352 1.23 mlelstv /* 353 1.23 mlelstv * A unique system identifier (usually MAC address) 354 1.23 mlelstv */ 355 1.23 mlelstv memset(u->node, 0, sizeof(u->node)); 356 1.14 christos 357 1.14 christos return 0; 358 1.14 christos } 359 1.14 christos 360 1.14 christos int 361 1.14 christos gpt_uuid_generate(gpt_t gpt, gpt_uuid_t t) 362 1.14 christos { 363 1.14 christos int rv; 364 1.14 christos struct dce_uuid u; 365 1.23 mlelstv 366 1.18 jnemeth if (gpt && (gpt->flags & GPT_TIMESTAMP)) 367 1.15 christos rv = gpt_uuid_tstamp(gpt, &u, sizeof(u)); 368 1.15 christos else 369 1.14 christos rv = gpt_uuid_random(gpt, &u, sizeof(u)); 370 1.14 christos 371 1.14 christos if (rv == -1) 372 1.14 christos return -1; 373 1.8 riastrad 374 1.8 riastrad /* Fix the reserved bits. */ 375 1.25 christos u.clock_seq_hi_and_reserved &= (uint8_t)~0x40U; 376 1.8 riastrad u.clock_seq_hi_and_reserved |= 0x80; 377 1.6 jnemeth 378 1.7 christos gpt_dce_to_uuid(&u, t); 379 1.11 christos return 0; 380 1.6 jnemeth } 381 1.17 martin #endif 382