Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | /* |
3 | | * Unified UUID/GUID definition |
4 | | * |
5 | | * Copyright (C) 2009, 2016 Intel Corp. |
6 | | * Huang Ying <ying.huang@intel.com> |
7 | | */ |
8 | | |
9 | | #include <linux/uuid.h> |
10 | | #include <module.h> |
11 | | #include <stdlib.h> |
12 | | #include <linux/ctype.h> |
13 | | #include <linux/export.h> |
14 | | |
15 | | const guid_t guid_null; |
16 | | EXPORT_SYMBOL(guid_null); |
17 | | const uuid_t uuid_null; |
18 | | EXPORT_SYMBOL(uuid_null); |
19 | | |
20 | | const u8 guid_index[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; |
21 | | const u8 uuid_index[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
22 | | |
23 | | /** |
24 | | * generate_random_uuid - generate a random UUID |
25 | | * @uuid: where to put the generated UUID |
26 | | * |
27 | | * Random UUID interface |
28 | | * |
29 | | * Used to create a Boot ID or a filesystem UUID/GUID, but can be |
30 | | * useful for other kernel drivers. |
31 | | */ |
32 | | void generate_random_uuid(unsigned char uuid[16]) |
33 | 0 | { |
34 | 0 | get_noncrypto_bytes(uuid, 16); |
35 | | /* Set UUID version to 4 --- truly random generation */ |
36 | 0 | uuid[6] = (uuid[6] & 0x0F) | 0x40; |
37 | | /* Set the UUID variant to DCE */ |
38 | 0 | uuid[8] = (uuid[8] & 0x3F) | 0x80; |
39 | 0 | } |
40 | | EXPORT_SYMBOL(generate_random_uuid); |
41 | | |
42 | | void generate_random_guid(unsigned char guid[16]) |
43 | 0 | { |
44 | 0 | get_noncrypto_bytes(guid, 16); |
45 | | /* Set GUID version to 4 --- truly random generation */ |
46 | 0 | guid[7] = (guid[7] & 0x0F) | 0x40; |
47 | | /* Set the GUID variant to DCE */ |
48 | 0 | guid[8] = (guid[8] & 0x3F) | 0x80; |
49 | 0 | } |
50 | | EXPORT_SYMBOL(generate_random_guid); |
51 | | |
52 | | /** |
53 | | * uuid_is_valid - checks if a UUID string is valid |
54 | | * @uuid: UUID string to check |
55 | | * |
56 | | * Description: |
57 | | * It checks if the UUID string is following the format: |
58 | | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
59 | | * |
60 | | * where x is a hex digit. |
61 | | * |
62 | | * Return: true if input is valid UUID string. |
63 | | */ |
64 | | bool uuid_is_valid(const char *uuid) |
65 | 0 | { |
66 | 0 | unsigned int i; |
67 | |
|
68 | 0 | for (i = 0; i < UUID_STRING_LEN; i++) { |
69 | 0 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
70 | 0 | if (uuid[i] != '-') |
71 | 0 | return false; |
72 | 0 | } else if (!isxdigit(uuid[i])) { |
73 | 0 | return false; |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | 0 | return true; |
78 | 0 | } |
79 | | EXPORT_SYMBOL(uuid_is_valid); |
80 | | |
81 | | static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16]) |
82 | 0 | { |
83 | 0 | static const u8 si[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34}; |
84 | 0 | unsigned int i; |
85 | |
|
86 | 0 | if (!uuid_is_valid(uuid)) |
87 | 0 | return -EINVAL; |
88 | | |
89 | 0 | for (i = 0; i < 16; i++) { |
90 | 0 | int hi = hex_to_bin(uuid[si[i] + 0]); |
91 | 0 | int lo = hex_to_bin(uuid[si[i] + 1]); |
92 | |
|
93 | 0 | b[ei[i]] = (hi << 4) | lo; |
94 | 0 | } |
95 | |
|
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | | int guid_parse(const char *uuid, guid_t *u) |
100 | 0 | { |
101 | 0 | return __uuid_parse(uuid, u->b, guid_index); |
102 | 0 | } |
103 | | EXPORT_SYMBOL(guid_parse); |
104 | | |
105 | | int uuid_parse(const char *uuid, uuid_t *u) |
106 | 0 | { |
107 | 0 | return __uuid_parse(uuid, u->b, uuid_index); |
108 | 0 | } |
109 | | EXPORT_SYMBOL(uuid_parse); |