1
2
3
4
5
6#include <common.h>
7#include <command.h>
8#include <env.h>
9#include <rand.h>
10#include <time.h>
11#include <uuid.h>
12#include <linux/ctype.h>
13#include <errno.h>
14#include <common.h>
15#include <asm/io.h>
16#include <part_efi.h>
17#include <malloc.h>
18#include <dm/uclass.h>
19#include <rng.h>
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65int uuid_str_valid(const char *uuid)
66{
67 int i, valid;
68
69 if (uuid == NULL)
70 return 0;
71
72 for (i = 0, valid = 1; uuid[i] && valid; i++) {
73 switch (i) {
74 case 8: case 13: case 18: case 23:
75 valid = (uuid[i] == '-');
76 break;
77 default:
78 valid = isxdigit(uuid[i]);
79 break;
80 }
81 }
82
83 if (i != UUID_STR_LEN || !valid)
84 return 0;
85
86 return 1;
87}
88
89#ifdef CONFIG_PARTITION_TYPE_GUID
90static const struct {
91 const char *string;
92 efi_guid_t guid;
93} list_guid[] = {
94 {"system", PARTITION_SYSTEM_GUID},
95 {"mbr", LEGACY_MBR_PARTITION_GUID},
96 {"msft", PARTITION_MSFT_RESERVED_GUID},
97 {"data", PARTITION_BASIC_DATA_GUID},
98 {"linux", PARTITION_LINUX_FILE_SYSTEM_DATA_GUID},
99 {"raid", PARTITION_LINUX_RAID_GUID},
100 {"swap", PARTITION_LINUX_SWAP_GUID},
101 {"lvm", PARTITION_LINUX_LVM_GUID},
102 {"u-boot-env", PARTITION_U_BOOT_ENVIRONMENT},
103};
104
105
106
107
108
109
110
111int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin)
112{
113 int i;
114
115 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
116 if (!strcmp(list_guid[i].string, guid_str)) {
117 memcpy(guid_bin, &list_guid[i].guid, 16);
118 return 0;
119 }
120 }
121 return -ENODEV;
122}
123
124
125
126
127
128
129
130
131const char *uuid_guid_get_str(const unsigned char *guid_bin)
132{
133 int i;
134
135 for (i = 0; i < ARRAY_SIZE(list_guid); i++) {
136 if (!memcmp(list_guid[i].guid.b, guid_bin, 16)) {
137 return list_guid[i].string;
138 }
139 }
140 return NULL;
141}
142#endif
143
144
145
146
147
148
149
150
151int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
152 int str_format)
153{
154 uint16_t tmp16;
155 uint32_t tmp32;
156 uint64_t tmp64;
157
158 if (!uuid_str_valid(uuid_str)) {
159#ifdef CONFIG_PARTITION_TYPE_GUID
160 if (!uuid_guid_get_bin(uuid_str, uuid_bin))
161 return 0;
162#endif
163 return -EINVAL;
164 }
165
166 if (str_format == UUID_STR_FORMAT_STD) {
167 tmp32 = cpu_to_be32(hextoul(uuid_str, NULL));
168 memcpy(uuid_bin, &tmp32, 4);
169
170 tmp16 = cpu_to_be16(hextoul(uuid_str + 9, NULL));
171 memcpy(uuid_bin + 4, &tmp16, 2);
172
173 tmp16 = cpu_to_be16(hextoul(uuid_str + 14, NULL));
174 memcpy(uuid_bin + 6, &tmp16, 2);
175 } else {
176 tmp32 = cpu_to_le32(hextoul(uuid_str, NULL));
177 memcpy(uuid_bin, &tmp32, 4);
178
179 tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL));
180 memcpy(uuid_bin + 4, &tmp16, 2);
181
182 tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL));
183 memcpy(uuid_bin + 6, &tmp16, 2);
184 }
185
186 tmp16 = cpu_to_be16(hextoul(uuid_str + 19, NULL));
187 memcpy(uuid_bin + 8, &tmp16, 2);
188
189 tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
190 memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
191
192 return 0;
193}
194
195
196
197
198
199
200
201
202
203void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
204 int str_format)
205{
206 const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
207 9, 10, 11, 12, 13, 14, 15};
208 const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
209 9, 10, 11, 12, 13, 14, 15};
210 const u8 *char_order;
211 const char *format;
212 int i;
213
214
215
216
217
218
219 if (str_format & UUID_STR_FORMAT_GUID)
220 char_order = guid_char_order;
221 else
222 char_order = uuid_char_order;
223 if (str_format & UUID_STR_UPPER_CASE)
224 format = "%02X";
225 else
226 format = "%02x";
227
228 for (i = 0; i < 16; i++) {
229 sprintf(uuid_str, format, uuid_bin[char_order[i]]);
230 uuid_str += 2;
231 switch (i) {
232 case 3:
233 case 5:
234 case 7:
235 case 9:
236 *uuid_str++ = '-';
237 break;
238 }
239 }
240}
241
242
243
244
245
246
247
248
249#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
250void gen_rand_uuid(unsigned char *uuid_bin)
251{
252 u32 ptr[4];
253 struct uuid *uuid = (struct uuid *)ptr;
254 int i, ret;
255 struct udevice *devp;
256 u32 randv = 0;
257
258 if (IS_ENABLED(CONFIG_DM_RNG)) {
259 ret = uclass_get_device(UCLASS_RNG, 0, &devp);
260 if (!ret) {
261 ret = dm_rng_read(devp, &randv, sizeof(randv));
262 if (ret < 0)
263 randv = 0;
264 }
265 }
266 if (randv)
267 srand(randv);
268 else
269 srand(get_ticks() + rand());
270
271
272 for (i = 0; i < 4; i++)
273 ptr[i] = rand();
274
275 clrsetbits_be16(&uuid->time_hi_and_version,
276 UUID_VERSION_MASK,
277 UUID_VERSION << UUID_VERSION_SHIFT);
278
279 clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
280 UUID_VARIANT_MASK,
281 UUID_VARIANT << UUID_VARIANT_SHIFT);
282
283 memcpy(uuid_bin, uuid, 16);
284}
285
286
287
288
289
290
291
292
293void gen_rand_uuid_str(char *uuid_str, int str_format)
294{
295 unsigned char uuid_bin[UUID_BIN_LEN];
296
297
298 gen_rand_uuid(uuid_bin);
299
300
301 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
302}
303
304#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CMD_UUID)
305int do_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
306{
307 char uuid[UUID_STR_LEN + 1];
308 int str_format;
309
310 if (!strcmp(argv[0], "uuid"))
311 str_format = UUID_STR_FORMAT_STD;
312 else
313 str_format = UUID_STR_FORMAT_GUID;
314
315 if (argc > 2)
316 return CMD_RET_USAGE;
317
318 gen_rand_uuid_str(uuid, str_format);
319
320 if (argc == 1)
321 printf("%s\n", uuid);
322 else
323 env_set(argv[1], uuid);
324
325 return CMD_RET_SUCCESS;
326}
327
328U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
329 "UUID - generate random Universally Unique Identifier",
330 "[<varname>]\n"
331 "Argument:\n"
332 "varname: for set result in a environment variable\n"
333 "e.g. uuid uuid_env"
334);
335
336U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
337 "GUID - generate Globally Unique Identifier based on random UUID",
338 "[<varname>]\n"
339 "Argument:\n"
340 "varname: for set result in a environment variable\n"
341 "e.g. guid guid_env"
342);
343#endif
344#endif
345