blob: f32b6023164e960d00d708fbc9e64b6623a98df0 [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
Przemyslaw Marczak89c82302014-04-02 10:20:05 +02007#include <common.h>
Jason Hobbse11938e2011-08-23 11:06:56 +00008#include <linux/ctype.h>
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +02009#include <errno.h>
10#include <common.h>
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020011#include <asm/io.h>
12#include <part_efi.h>
13#include <malloc.h>
Jason Hobbse11938e2011-08-23 11:06:56 +000014
15/*
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020016 * UUID - Universally Unique IDentifier - 128 bits unique number.
17 * There are 5 versions and one variant of UUID defined by RFC4122
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +020018 * 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
Jason Hobbse11938e2011-08-23 11:06:56 +000034 *
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020035 * 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):
Jason Hobbse11938e2011-08-23 11:06:56 +000040 *
41 * 0 9 14 19 24
42 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020043 * 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
Jason Hobbse11938e2011-08-23 11:06:56 +000055 * le le le be be
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020056 *
57 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbse11938e2011-08-23 11:06:56 +000058 */
Jason Hobbse11938e2011-08-23 11:06:56 +000059int 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
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020077 if (i != UUID_STR_LEN || !valid)
Jason Hobbse11938e2011-08-23 11:06:56 +000078 return 0;
79
80 return 1;
81}
82
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020083/*
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 */
90int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
Jason Hobbse11938e2011-08-23 11:06:56 +000091{
92 uint16_t tmp16;
93 uint32_t tmp32;
94 uint64_t tmp64;
95
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020096 if (!uuid_str_valid(uuid_str))
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +020097 return -EINVAL;
98
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +020099 if (str_format == UUID_STR_FORMAT_STD) {
100 tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
101 memcpy(uuid_bin, &tmp32, 4);
Jason Hobbse11938e2011-08-23 11:06:56 +0000102
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200103 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
104 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000105
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200106 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);
Jason Hobbse11938e2011-08-23 11:06:56 +0000111
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200112 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
113 memcpy(uuid_bin + 4, &tmp16, 2);
Jason Hobbse11938e2011-08-23 11:06:56 +0000114
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200115 tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
116 memcpy(uuid_bin + 6, &tmp16, 2);
117 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000118
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200119 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);
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200124
125 return 0;
126}
127
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200128/*
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 */
135void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200136{
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200137 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;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200142 int i;
143
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200144 /*
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
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200154 for (i = 0; i < 16; i++) {
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200155 sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
156 uuid_str += 2;
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200157 switch (i) {
158 case 3:
159 case 5:
160 case 7:
161 case 9:
Przemyslaw Marczakd718ded2014-04-02 10:20:03 +0200162 *uuid_str++ = '-';
Przemyslaw Marczaka96a0e62014-04-02 10:20:02 +0200163 break;
164 }
165 }
Jason Hobbse11938e2011-08-23 11:06:56 +0000166}
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200167
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*/
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200175#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200176void 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 */
204void 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}
Przemyslaw Marczak89c82302014-04-02 10:20:05 +0200214
215#ifdef CONFIG_CMD_UUID
216int 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
239U_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
247U_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
Przemyslaw Marczak4e4815f2014-04-02 10:20:04 +0200255#endif