blob: 44d0c932ac507c12276616b9e9c0afa19d6728f7 [file] [log] [blame]
Jason Hobbse11938e2011-08-23 11:06:56 +00001/*
2 * Copyright 2011 Calxeda, Inc.
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbse11938e2011-08-23 11:06:56 +00005 */
6
7#include <linux/ctype.h>
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +02008#include <errno.h>
9#include <common.h>
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020010#include <asm/io.h>
11#include <part_efi.h>
12#include <malloc.h>
Jason Hobbse11938e2011-08-23 11:06:56 +000013
14/*
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020015 * UUID - Universally Unique IDentifier - 128 bits unique number.
16 * There are 5 versions and one variant of UUID defined by RFC4122
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +020017 * specification. A UUID contains a set of fields. The set varies
18 * depending on the version of the UUID, as shown below:
19 * - time, MAC address(v1),
20 * - user ID(v2),
21 * - MD5 of name or URL(v3),
22 * - random data(v4),
23 * - SHA-1 of name or URL(v5),
24 *
25 * Layout of UUID:
26 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
27 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
28 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
29 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
30 * node - 48 bit
31 *
32 * source: https://www.ietf.org/rfc/rfc4122.txt
Jason Hobbse11938e2011-08-23 11:06:56 +000033 *
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020034 * UUID binary format (16 bytes):
35 *
36 * 4B-2B-2B-2B-6B (big endian - network byte order)
37 *
38 * UUID string is 36 length of characters (36 bytes):
Jason Hobbse11938e2011-08-23 11:06:56 +000039 *
40 * 0 9 14 19 24
41 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020042 * be be be be be
43 *
44 * where x is a hexadecimal character. Fields are separated by '-'s.
45 * When converting to a binary UUID, le means the field should be converted
46 * to little endian and be means it should be converted to big endian.
47 *
48 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
49 * format but it differs in string format like below.
50 *
51 * GUID:
52 * 0 9 14 19 24
53 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Jason Hobbse11938e2011-08-23 11:06:56 +000054 * le le le be be
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020055 *
56 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbse11938e2011-08-23 11:06:56 +000057 */
Jason Hobbse11938e2011-08-23 11:06:56 +000058int uuid_str_valid(const char *uuid)
59{
60 int i, valid;
61
62 if (uuid == NULL)
63 return 0;
64
65 for (i = 0, valid = 1; uuid[i] && valid; i++) {
66 switch (i) {
67 case 8: case 13: case 18: case 23:
68 valid = (uuid[i] == '-');
69 break;
70 default:
71 valid = isxdigit(uuid[i]);
72 break;
73 }
74 }
75
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020076 if (i != UUID_STR_LEN || !valid)
Jason Hobbse11938e2011-08-23 11:06:56 +000077 return 0;
78
79 return 1;
80}
81
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020082/*
83 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
84 *
85 * @param uuid_str - pointer to UUID or GUID string [37B]
86 * @param uuid_bin - pointer to allocated array for big endian output [16B]
87 * @str_format - UUID string format: 0 - UUID; 1 - GUID
88 */
89int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
Jason Hobbse11938e2011-08-23 11:06:56 +000090{
91 uint16_t tmp16;
92 uint32_t tmp32;
93 uint64_t tmp64;
94
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020095 if (!uuid_str_valid(uuid_str))
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020096 return -EINVAL;
97
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020098 if (str_format == UUID_STR_FORMAT_STD) {
99 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
100 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000101
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200102 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
103 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000104
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200105 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
106 memcpy(uuid_bin + 6, &tmp16, 2);
107 } else {
108 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
109 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000110
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200111 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
112 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000113
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200114 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
115 memcpy(uuid_bin + 6, &tmp16, 2);
116 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000117
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200118 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
119 memcpy(uuid_bin + 8, &tmp16, 2);
120
121 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
122 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200123
124 return 0;
125}
126
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200127/*
128 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
129 *
130 * @param uuid_bin - pointer to binary data of UUID (big endian) [16B]
131 * @param uuid_str - pointer to allocated array for output string [37B]
132 * @str_format - UUID string format: 0 - UUID; 1 - GUID
133 */
134void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200135{
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200136 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
137 9, 10, 11, 12, 13, 14, 15};
138 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
139 9, 10, 11, 12, 13, 14, 15};
140 const u8 *char_order;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200141 int i;
142
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200143 /*
144 * UUID and GUID bin data - always in big endian:
145 * 4B-2B-2B-2B-6B
146 * be be be be be
147 */
148 if (str_format == UUID_STR_FORMAT_STD)
149 char_order = uuid_char_order;
150 else
151 char_order = guid_char_order;
152
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200153 for (i = 0; i < 16; i++) {
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200154 sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
155 uuid_str += 2;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200156 switch (i) {
157 case 3:
158 case 5:
159 case 7:
160 case 9:
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200161 *uuid_str++ = '-';
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200162 break;
163 }
164 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000165}
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200166
167/*
168 * gen_rand_uuid() - this function generates a random binary UUID version 4.
169 * In this version all fields beside 4 bits of version and
170 * 2 bits of variant are randomly generated.
171 *
172 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
173*/
174#ifdef CONFIG_RANDOM_UUID
175void gen_rand_uuid(unsigned char *uuid_bin)
176{
177 struct uuid uuid;
178 unsigned int *ptr = (unsigned int *)&uuid;
179 int i;
180
181 /* Set all fields randomly */
182 for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
183 *(ptr + i) = cpu_to_be32(rand());
184
185 clrsetbits_be16(&uuid.time_hi_and_version,
186 UUID_VERSION_MASK,
187 UUID_VERSION << UUID_VERSION_SHIFT);
188
189 clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
190 UUID_VARIANT_MASK,
191 UUID_VARIANT << UUID_VARIANT_SHIFT);
192
193 memcpy(uuid_bin, &uuid, sizeof(struct uuid));
194}
195
196/*
197 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
198 * formats UUID or GUID.
199 *
200 * @param uuid_str - pointer to allocated array [37B].
201 * @param - uuid output type: UUID - 0, GUID - 1
202 */
203void gen_rand_uuid_str(char *uuid_str, int str_format)
204{
205 unsigned char uuid_bin[UUID_BIN_LEN];
206
207 /* Generate UUID (big endian) */
208 gen_rand_uuid(uuid_bin);
209
210 /* Convert UUID bin to UUID or GUID formated STRING */
211 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
212}
213#endif