blob: f32b6023164e960d00d708fbc9e64b6623a98df0 [file] [log] [blame]
Jason Hobbs6e39cce2011-08-23 11:06:56 +00001/*
2 * Copyright 2011 Calxeda, Inc.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Jason Hobbs6e39cce2011-08-23 11:06:56 +00005 */
6
Przemyslaw Marczak2eb40ee2014-04-02 10:20:05 +02007#include <common.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +00008#include <linux/ctype.h>
Przemyslaw Marczakfa907342014-04-02 10:20:02 +02009#include <errno.h>
10#include <common.h>
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020011#include <asm/io.h>
12#include <part_efi.h>
13#include <malloc.h>
Jason Hobbs6e39cce2011-08-23 11:06:56 +000014
15/*
Przemyslaw Marczakfa907342014-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 Marczakb4285142014-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
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020034 *
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.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000048 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020049 * UUID is also used as GUID (Globally Unique Identifier) with the same binary
50 * format but it differs in string format like below.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000051 *
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020052 * GUID:
Jason Hobbs6e39cce2011-08-23 11:06:56 +000053 * 0 9 14 19 24
54 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
55 * le le le be be
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020056 *
57 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
Jason Hobbs6e39cce2011-08-23 11:06:56 +000058 */
Jason Hobbs6e39cce2011-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 Marczak0c813362014-04-02 10:20:03 +020077 if (i != UUID_STR_LEN || !valid)
Jason Hobbs6e39cce2011-08-23 11:06:56 +000078 return 0;
79
80 return 1;
81}
82
Przemyslaw Marczak0c813362014-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 Hobbs6e39cce2011-08-23 11:06:56 +000091{
92 uint16_t tmp16;
93 uint32_t tmp32;
94 uint64_t tmp64;
95
Przemyslaw Marczak0c813362014-04-02 10:20:03 +020096 if (!uuid_str_valid(uuid_str))
Przemyslaw Marczakfa907342014-04-02 10:20:02 +020097 return -EINVAL;
98
Przemyslaw Marczak0c813362014-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 Hobbs6e39cce2011-08-23 11:06:56 +0000102
Przemyslaw Marczak0c813362014-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 Hobbs6e39cce2011-08-23 11:06:56 +0000105
Przemyslaw Marczak0c813362014-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 Hobbs6e39cce2011-08-23 11:06:56 +0000111
Przemyslaw Marczak0c813362014-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 Hobbs6e39cce2011-08-23 11:06:56 +0000114
Przemyslaw Marczak0c813362014-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 }
118
119 tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
120 memcpy(uuid_bin + 8, &tmp16, 2);
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000121
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200122 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
123 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200124
125 return 0;
126}
127
Przemyslaw Marczak0c813362014-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 Marczakfa907342014-04-02 10:20:02 +0200136{
Przemyslaw Marczak0c813362014-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 Marczakfa907342014-04-02 10:20:02 +0200142 int i;
143
Przemyslaw Marczak0c813362014-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 Marczakfa907342014-04-02 10:20:02 +0200154 for (i = 0; i < 16; i++) {
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200155 sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
156 uuid_str += 2;
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200157 switch (i) {
158 case 3:
159 case 5:
160 case 7:
161 case 9:
Przemyslaw Marczak0c813362014-04-02 10:20:03 +0200162 *uuid_str++ = '-';
Przemyslaw Marczakfa907342014-04-02 10:20:02 +0200163 break;
164 }
165 }
Jason Hobbs6e39cce2011-08-23 11:06:56 +0000166}
Przemyslaw Marczakb4285142014-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 Marczak2eb40ee2014-04-02 10:20:05 +0200175#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
Przemyslaw Marczakb4285142014-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 Marczak2eb40ee2014-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 Marczakb4285142014-04-02 10:20:04 +0200255#endif