]> git.ipfire.org Git - people/ms/u-boot.git/blob - lib/uuid.c
board: axs103 - add maintainer information
[people/ms/u-boot.git] / lib / uuid.c
1 /*
2 * Copyright 2011 Calxeda, Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include <common.h>
8 #include <linux/ctype.h>
9 #include <errno.h>
10 #include <common.h>
11 #include <asm/io.h>
12 #include <part_efi.h>
13 #include <malloc.h>
14
15 /*
16 * UUID - Universally Unique IDentifier - 128 bits unique number.
17 * There are 5 versions and one variant of UUID defined by RFC4122
18 * specification. A UUID contains a set of fields. The set varies
19 * depending on the version of the UUID, as shown below:
20 * - time, MAC address(v1),
21 * - user ID(v2),
22 * - MD5 of name or URL(v3),
23 * - random data(v4),
24 * - SHA-1 of name or URL(v5),
25 *
26 * Layout of UUID:
27 * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
28 * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
29 * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
30 * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
31 * node - 48 bit
32 *
33 * source: https://www.ietf.org/rfc/rfc4122.txt
34 *
35 * UUID binary format (16 bytes):
36 *
37 * 4B-2B-2B-2B-6B (big endian - network byte order)
38 *
39 * UUID string is 36 length of characters (36 bytes):
40 *
41 * 0 9 14 19 24
42 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
43 * be be be be be
44 *
45 * where x is a hexadecimal character. Fields are separated by '-'s.
46 * When converting to a binary UUID, le means the field should be converted
47 * to little endian and be means it should be converted to big endian.
48 *
49 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
50 * format but it differs in string format like below.
51 *
52 * GUID:
53 * 0 9 14 19 24
54 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
55 * le le le be be
56 *
57 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
58 */
59 int uuid_str_valid(const char *uuid)
60 {
61 int i, valid;
62
63 if (uuid == NULL)
64 return 0;
65
66 for (i = 0, valid = 1; uuid[i] && valid; i++) {
67 switch (i) {
68 case 8: case 13: case 18: case 23:
69 valid = (uuid[i] == '-');
70 break;
71 default:
72 valid = isxdigit(uuid[i]);
73 break;
74 }
75 }
76
77 if (i != UUID_STR_LEN || !valid)
78 return 0;
79
80 return 1;
81 }
82
83 /*
84 * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
85 *
86 * @param uuid_str - pointer to UUID or GUID string [37B]
87 * @param uuid_bin - pointer to allocated array for big endian output [16B]
88 * @str_format - UUID string format: 0 - UUID; 1 - GUID
89 */
90 int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
91 {
92 uint16_t tmp16;
93 uint32_t tmp32;
94 uint64_t tmp64;
95
96 if (!uuid_str_valid(uuid_str))
97 return -EINVAL;
98
99 if (str_format == UUID_STR_FORMAT_STD) {
100 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
101 memcpy(uuid_bin, &tmp32, 4);
102
103 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
104 memcpy(uuid_bin + 4, &tmp16, 2);
105
106 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
107 memcpy(uuid_bin + 6, &tmp16, 2);
108 } else {
109 tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
110 memcpy(uuid_bin, &tmp32, 4);
111
112 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
113 memcpy(uuid_bin + 4, &tmp16, 2);
114
115 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
116 memcpy(uuid_bin + 6, &tmp16, 2);
117 }
118
119 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
120 memcpy(uuid_bin + 8, &tmp16, 2);
121
122 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
123 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
124
125 return 0;
126 }
127
128 /*
129 * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
130 *
131 * @param uuid_bin - pointer to binary data of UUID (big endian) [16B]
132 * @param uuid_str - pointer to allocated array for output string [37B]
133 * @str_format - UUID string format: 0 - UUID; 1 - GUID
134 */
135 void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
136 {
137 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
138 9, 10, 11, 12, 13, 14, 15};
139 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
140 9, 10, 11, 12, 13, 14, 15};
141 const u8 *char_order;
142 int i;
143
144 /*
145 * UUID and GUID bin data - always in big endian:
146 * 4B-2B-2B-2B-6B
147 * be be be be be
148 */
149 if (str_format == UUID_STR_FORMAT_STD)
150 char_order = uuid_char_order;
151 else
152 char_order = guid_char_order;
153
154 for (i = 0; i < 16; i++) {
155 sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
156 uuid_str += 2;
157 switch (i) {
158 case 3:
159 case 5:
160 case 7:
161 case 9:
162 *uuid_str++ = '-';
163 break;
164 }
165 }
166 }
167
168 /*
169 * gen_rand_uuid() - this function generates a random binary UUID version 4.
170 * In this version all fields beside 4 bits of version and
171 * 2 bits of variant are randomly generated.
172 *
173 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
174 */
175 #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
176 void gen_rand_uuid(unsigned char *uuid_bin)
177 {
178 struct uuid uuid;
179 unsigned int *ptr = (unsigned int *)&uuid;
180 int i;
181
182 /* Set all fields randomly */
183 for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
184 *(ptr + i) = cpu_to_be32(rand());
185
186 clrsetbits_be16(&uuid.time_hi_and_version,
187 UUID_VERSION_MASK,
188 UUID_VERSION << UUID_VERSION_SHIFT);
189
190 clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
191 UUID_VARIANT_MASK,
192 UUID_VARIANT << UUID_VARIANT_SHIFT);
193
194 memcpy(uuid_bin, &uuid, sizeof(struct uuid));
195 }
196
197 /*
198 * gen_rand_uuid_str() - this function generates UUID v4 (random) in two string
199 * formats UUID or GUID.
200 *
201 * @param uuid_str - pointer to allocated array [37B].
202 * @param - uuid output type: UUID - 0, GUID - 1
203 */
204 void gen_rand_uuid_str(char *uuid_str, int str_format)
205 {
206 unsigned char uuid_bin[UUID_BIN_LEN];
207
208 /* Generate UUID (big endian) */
209 gen_rand_uuid(uuid_bin);
210
211 /* Convert UUID bin to UUID or GUID formated STRING */
212 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
213 }
214
215 #ifdef CONFIG_CMD_UUID
216 int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
217 {
218 char uuid[UUID_STR_LEN + 1];
219 int str_format;
220
221 if (!strcmp(argv[0], "uuid"))
222 str_format = UUID_STR_FORMAT_STD;
223 else
224 str_format = UUID_STR_FORMAT_GUID;
225
226 if (argc > 2)
227 return CMD_RET_USAGE;
228
229 gen_rand_uuid_str(uuid, str_format);
230
231 if (argc == 1)
232 printf("%s\n", uuid);
233 else
234 setenv(argv[1], uuid);
235
236 return CMD_RET_SUCCESS;
237 }
238
239 U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
240 "UUID - generate random Universally Unique Identifier",
241 "[<varname>]\n"
242 "Argument:\n"
243 "varname: for set result in a environment variable\n"
244 "e.g. uuid uuid_env"
245 );
246
247 U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
248 "GUID - generate Globally Unique Identifier based on random UUID",
249 "[<varname>]\n"
250 "Argument:\n"
251 "varname: for set result in a environment variable\n"
252 "e.g. guid guid_env"
253 );
254 #endif /* CONFIG_CMD_UUID */
255 #endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */