xref: /freebsd/sys/compat/linuxkpi/common/include/linux/uuid.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1cae16831SBjoern A. Zeeb /*-
2cae16831SBjoern A. Zeeb  * SPDX-License-Identifier: BSD-2-Clause
3cae16831SBjoern A. Zeeb  *
4*c1b6e912SBjoern A. Zeeb  * Copyright (c) 2021,2023 The FreeBSD Foundation
5cae16831SBjoern A. Zeeb  *
6cae16831SBjoern A. Zeeb  * This software was developed by Björn Zeeb under sponsorship from
7cae16831SBjoern A. Zeeb  * the FreeBSD Foundation.
8cae16831SBjoern A. Zeeb  *
9cae16831SBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
10cae16831SBjoern A. Zeeb  * modification, are permitted provided that the following conditions
11cae16831SBjoern A. Zeeb  * are met:
12cae16831SBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
13cae16831SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
14cae16831SBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
15cae16831SBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
16cae16831SBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
17cae16831SBjoern A. Zeeb  *
18cae16831SBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19cae16831SBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20cae16831SBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21cae16831SBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22cae16831SBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23cae16831SBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24cae16831SBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25cae16831SBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26cae16831SBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27cae16831SBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28cae16831SBjoern A. Zeeb  * SUCH DAMAGE.
29cae16831SBjoern A. Zeeb  */
30cae16831SBjoern A. Zeeb 
31307f78f3SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_UUID_H
32307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_UUID_H
33cae16831SBjoern A. Zeeb 
34*c1b6e912SBjoern A. Zeeb #include <linux/random.h>
35*c1b6e912SBjoern A. Zeeb 
3697009980SBjoern A. Zeeb #define	UUID_STRING_LEN	36
3797009980SBjoern A. Zeeb 
3897009980SBjoern A. Zeeb #define	GUID_INIT(x0_3, x4_5, x6_7, x8, x9, x10, x11, x12, x13, x14, x15) \
3997009980SBjoern A. Zeeb 	((guid_t) { .x = { 						\
4097009980SBjoern A. Zeeb 		[0]  =  (x0_3)        & 0xff,				\
4197009980SBjoern A. Zeeb 		[1]  = ((x0_3) >> 8)  & 0xff,				\
4297009980SBjoern A. Zeeb 		[2]  = ((x0_3) >> 16) & 0xff,				\
4397009980SBjoern A. Zeeb 		[3]  = ((x0_3) >> 24) & 0xff,				\
4497009980SBjoern A. Zeeb 		[4]  =  (x4_5)        & 0xff,				\
4597009980SBjoern A. Zeeb 		[5]  = ((x4_5) >> 8)  & 0xff,				\
4697009980SBjoern A. Zeeb 		[6]  =  (x6_7)        & 0xff,				\
4797009980SBjoern A. Zeeb 		[7]  = ((x6_7) >> 8)  & 0xff,				\
4897009980SBjoern A. Zeeb 		[8]  =  (x8),						\
4997009980SBjoern A. Zeeb 		[9]  =  (x9),						\
5097009980SBjoern A. Zeeb 		[10] =  (x10),						\
5197009980SBjoern A. Zeeb 		[11] =  (x11),						\
5297009980SBjoern A. Zeeb 		[12] =  (x12),						\
5397009980SBjoern A. Zeeb 		[13] =  (x13),						\
5497009980SBjoern A. Zeeb 		[14] =  (x14),						\
5597009980SBjoern A. Zeeb 		[15] =  (x15)						\
5697009980SBjoern A. Zeeb }})
5797009980SBjoern A. Zeeb 
58cae16831SBjoern A. Zeeb typedef struct {
59cae16831SBjoern A. Zeeb 	char	x[16];
60cae16831SBjoern A. Zeeb } guid_t;
61cae16831SBjoern A. Zeeb 
62*c1b6e912SBjoern A. Zeeb static inline void
guid_gen(guid_t * g)63*c1b6e912SBjoern A. Zeeb guid_gen(guid_t *g)
64*c1b6e912SBjoern A. Zeeb {
65*c1b6e912SBjoern A. Zeeb 
66*c1b6e912SBjoern A. Zeeb 	get_random_bytes(g, 16);
67*c1b6e912SBjoern A. Zeeb 	g->x[7] = (g->x[7] & 0x0f) | 0x40;
68*c1b6e912SBjoern A. Zeeb 	g->x[8] = (g->x[8] & 0x3f) | 0x80;
69*c1b6e912SBjoern A. Zeeb }
70*c1b6e912SBjoern A. Zeeb 
71*c1b6e912SBjoern A. Zeeb static inline void
guid_copy(guid_t * dst,const guid_t * src)72*c1b6e912SBjoern A. Zeeb guid_copy(guid_t *dst, const guid_t *src)
73*c1b6e912SBjoern A. Zeeb {
74*c1b6e912SBjoern A. Zeeb 	memcpy(dst, src, sizeof(*dst));
75*c1b6e912SBjoern A. Zeeb }
76*c1b6e912SBjoern A. Zeeb 
77307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_UUID_H */
78