xref: /freebsd/usr.bin/mkimg/uuid.c (revision f078c492a9b57877c723586db26d789cda1b98ea)
1 /*-
2  * Copyright (c) 2016 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <stdint.h>
31 #include <string.h>
32 
33 #include "endian.h"
34 #include "image.h"
35 #include "mkimg.h"
36 
37 static void osdep_uuidgen(mkimg_uuid_t *);
38 
39 #ifdef __APPLE__
40 #include <uuid/uuid.h>
41 
42 static void
43 osdep_uuidgen(mkimg_uuid_t *uuid)
44 {
45 
46 	uuid_generate_time((void *)uuid);
47 }
48 #endif	/* __APPLE__ */
49 
50 #ifdef __FreeBSD__
51 #include <sys/uuid.h>
52 
53 static void
54 osdep_uuidgen(mkimg_uuid_t *uuid)
55 {
56 
57 	uuidgen((void *)uuid, 1);
58 }
59 #endif	/* __FreeBSD__ */
60 
61 #ifdef __linux__
62 #include <stdlib.h>
63 #include <time.h>
64 
65 static void
66 osdep_uuidgen(mkimg_uuid_t *uuid)
67 {
68 	struct timeval tv;
69 	uint64_t time = 0x01B21DD213814000LL;
70 	u_int i;
71 	uint16_t seq;
72 
73 	if (gettimeofday(&tv, NULL) == -1)
74 		abort();
75 
76 	time += (uint64_t)tv.tv_sec * 10000000LL;
77 	time += tv.tv_usec * 10;
78 
79 	uuid->time_low = (uint32_t)time;
80 	uuid->time_mid = (uint16_t)(time >> 32);
81 	uuid->time_hi_and_version = (uint16_t)(time >> 48) & 0xfff;
82 	uuid->time_hi_and_version |= 1 << 12;
83 
84 	seq = random();
85 
86 	uuid->clock_seq_hi_and_reserved = (uint8_t)(seq >> 8) & 0x3f;
87 	uuid->clock_seq_low = (uint8_t)seq;
88 
89 	for (i = 0; i < 6; i++)
90 		uuid->node[i] = (uint8_t)random();
91 	uuid->node[0] |= 0x01;
92 }
93 #endif	/* __linux__ */
94 
95 void
96 mkimg_uuid(mkimg_uuid_t *uuid)
97 {
98 	static uint8_t gen[sizeof(mkimg_uuid_t)];
99 	u_int i;
100 
101 	if (!unit_testing) {
102 		osdep_uuidgen(uuid);
103 		return;
104 	}
105 
106 	for (i = 0; i < sizeof(gen); i++)
107 		gen[i]++;
108 	memcpy(uuid, gen, sizeof(*uuid));
109 }
110 
111 void
112 mkimg_uuid_enc(void *buf, const mkimg_uuid_t *uuid)
113 {
114 	uint8_t *p = buf;
115 	u_int i;
116 
117 	le32enc(p, uuid->time_low);
118 	le16enc(p + 4, uuid->time_mid);
119 	le16enc(p + 6, uuid->time_hi_and_version);
120 	p[8] = uuid->clock_seq_hi_and_reserved;
121 	p[9] = uuid->clock_seq_low;
122 	for (i = 0; i < 6; i++)
123 		p[10 + i] = uuid->node[i];
124 }
125