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