Commit a96a0e6153e3d9071c1a4516bf3e94c4cd96c77c

Authored by Przemyslaw Marczak
Committed by Tom Rini
1 parent 3f62971162

part_efi: move uuid<->string conversion functions into lib/uuid.c

This commit introduces cleanup for uuid library.
Changes:
- move uuid<->string conversion functions into lib/uuid.c so they can be
  used by code outside part_efi.c.
- rename uuid_string() to uuid_bin_to_str() for consistency with existing
  uuid_str_to_bin()
- add an error return code to uuid_str_to_bin()
- update existing code to the new library functions.

Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
Cc: Stephen Warren <swarren@nvidia.com>
Cc: Lukasz Majewski <l.majewski@samsung.com>
Cc: trini@ti.com

Showing 4 changed files with 68 additions and 87 deletions Side-by-side Diff

... ... @@ -63,26 +63,6 @@
63 63 return name;
64 64 }
65 65  
66   -static void uuid_string(unsigned char *uuid, char *str)
67   -{
68   - static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
69   - 12, 13, 14, 15};
70   - int i;
71   -
72   - for (i = 0; i < 16; i++) {
73   - sprintf(str, "%02x", uuid[le[i]]);
74   - str += 2;
75   - switch (i) {
76   - case 3:
77   - case 5:
78   - case 7:
79   - case 9:
80   - *str++ = '-';
81   - break;
82   - }
83   - }
84   -}
85   -
86 66 static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
87 67  
88 68 static inline int is_bootable(gpt_entry *p)
... ... @@ -103,6 +83,7 @@
103 83 gpt_entry *gpt_pte = NULL;
104 84 int i = 0;
105 85 char uuid[37];
  86 + unsigned char *uuid_bin;
106 87  
107 88 if (!dev_desc) {
108 89 printf("%s: Invalid Argument(s)\n", __func__);
109 90  
... ... @@ -132,9 +113,11 @@
132 113 le64_to_cpu(gpt_pte[i].ending_lba),
133 114 print_efiname(&gpt_pte[i]));
134 115 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
135   - uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
  116 + uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
  117 + uuid_bin_to_str(uuid_bin, uuid);
136 118 printf("\ttype:\t%s\n", uuid);
137   - uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
  119 + uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
  120 + uuid_bin_to_str(uuid_bin, uuid);
138 121 printf("\tuuid:\t%s\n", uuid);
139 122 }
140 123  
... ... @@ -182,7 +165,7 @@
182 165 sprintf((char *)info->type, "U-Boot");
183 166 info->bootable = is_bootable(&gpt_pte[part - 1]);
184 167 #ifdef CONFIG_PARTITION_UUIDS
185   - uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
  168 + uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
186 169 #endif
187 170  
188 171 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s", __func__,
... ... @@ -237,60 +220,6 @@
237 220 return 0;
238 221 }
239 222  
240   -/**
241   - * string_uuid(); Convert UUID stored as string to bytes
242   - *
243   - * @param uuid - UUID represented as string
244   - * @param dst - GUID buffer
245   - *
246   - * @return return 0 on successful conversion
247   - */
248   -static int string_uuid(char *uuid, u8 *dst)
249   -{
250   - efi_guid_t guid;
251   - u16 b, c, d;
252   - u64 e;
253   - u32 a;
254   - u8 *p;
255   - u8 i;
256   -
257   - const u8 uuid_str_len = 36;
258   -
259   - /* The UUID is written in text: */
260   - /* 1 9 14 19 24 */
261   - /* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
262   -
263   - debug("%s: uuid: %s\n", __func__, uuid);
264   -
265   - if (strlen(uuid) != uuid_str_len)
266   - return -1;
267   -
268   - for (i = 0; i < uuid_str_len; i++) {
269   - if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
270   - if (uuid[i] != '-')
271   - return -1;
272   - } else {
273   - if (!isxdigit(uuid[i]))
274   - return -1;
275   - }
276   - }
277   -
278   - a = (u32)simple_strtoul(uuid, NULL, 16);
279   - b = (u16)simple_strtoul(uuid + 9, NULL, 16);
280   - c = (u16)simple_strtoul(uuid + 14, NULL, 16);
281   - d = (u16)simple_strtoul(uuid + 19, NULL, 16);
282   - e = (u64)simple_strtoull(uuid + 24, NULL, 16);
283   -
284   - p = (u8 *) &e;
285   - guid = EFI_GUID(a, b, c, d >> 8, d & 0xFF,
286   - *(p + 5), *(p + 4), *(p + 3),
287   - *(p + 2), *(p + 1) , *p);
288   -
289   - memcpy(dst, guid.b, sizeof(efi_guid_t));
290   -
291   - return 0;
292   -}
293   -
294 223 int write_gpt_table(block_dev_desc_t *dev_desc,
295 224 gpt_header *gpt_h, gpt_entry *gpt_e)
296 225 {
... ... @@ -358,6 +287,7 @@
358 287 size_t efiname_len, dosname_len;
359 288 #ifdef CONFIG_PARTITION_UUIDS
360 289 char *str_uuid;
  290 + unsigned char *bin_uuid;
361 291 #endif
362 292  
363 293 for (i = 0; i < parts; i++) {
... ... @@ -391,7 +321,9 @@
391 321  
392 322 #ifdef CONFIG_PARTITION_UUIDS
393 323 str_uuid = partitions[i].uuid;
394   - if (string_uuid(str_uuid, gpt_e[i].unique_partition_guid.b)) {
  324 + bin_uuid = gpt_e[i].unique_partition_guid.b;
  325 +
  326 + if (uuid_str_to_bin(str_uuid, bin_uuid)) {
395 327 printf("Partition no. %d: invalid guid: %s\n",
396 328 i, str_uuid);
397 329 return -1;
... ... @@ -438,7 +370,7 @@
438 370 gpt_h->header_crc32 = 0;
439 371 gpt_h->partition_entry_array_crc32 = 0;
440 372  
441   - if (string_uuid(str_guid, gpt_h->disk_guid.b))
  373 + if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b))
442 374 return -1;
443 375  
444 376 return 0;
... ... @@ -822,7 +822,8 @@
822 822 void mdelay(unsigned long);
823 823  
824 824 /* lib/uuid.c */
825   -void uuid_str_to_bin(const char *uuid, unsigned char *out);
  825 +void uuid_bin_to_str(unsigned char *uuid, char *str);
  826 +int uuid_str_to_bin(char *uuid, unsigned char *out);
826 827 int uuid_str_valid(const char *uuid);
827 828  
828 829 /* lib/vsprintf.c */
... ... @@ -61,6 +61,7 @@
61 61 obj-y += time.o
62 62 obj-$(CONFIG_TRACE) += trace.o
63 63 obj-$(CONFIG_BOOTP_PXE) += uuid.o
  64 +obj-$(CONFIG_PARTITION_UUIDS) += uuid.o
64 65 obj-y += vsprintf.o
65 66 obj-$(CONFIG_LIB_RAND) += rand.o
66 67  
... ... @@ -5,18 +5,40 @@
5 5 */
6 6  
7 7 #include <linux/ctype.h>
8   -#include "common.h"
  8 +#include <errno.h>
  9 +#include <common.h>
9 10  
  11 +#define UUID_STR_LEN 36
  12 +
10 13 /*
11   - * This is what a UUID string looks like.
  14 + * UUID - Universally Unique IDentifier - 128 bits unique number.
  15 + * There are 5 versions and one variant of UUID defined by RFC4122
  16 + * specification. Depends on version uuid number base on a time,
  17 + * host name, MAC address or random data.
12 18 *
13   - * x is a hexadecimal character. fields are separated by '-'s. When converting
14   - * to a binary UUID, le means the field should be converted to little endian,
15   - * and be means it should be converted to big endian.
  19 + * UUID binary format (16 bytes):
16 20 *
  21 + * 4B-2B-2B-2B-6B (big endian - network byte order)
  22 + *
  23 + * UUID string is 36 length of characters (36 bytes):
  24 + *
17 25 * 0 9 14 19 24
18 26 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  27 + * be be be be be
  28 + *
  29 + * where x is a hexadecimal character. Fields are separated by '-'s.
  30 + * When converting to a binary UUID, le means the field should be converted
  31 + * to little endian and be means it should be converted to big endian.
  32 + *
  33 + * UUID is also used as GUID (Globally Unique Identifier) with the same binary
  34 + * format but it differs in string format like below.
  35 + *
  36 + * GUID:
  37 + * 0 9 14 19 24
  38 + * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
19 39 * le le le be be
  40 + *
  41 + * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
20 42 */
21 43  
22 44 int uuid_str_valid(const char *uuid)
23 45  
24 46  
... ... @@ -43,15 +65,18 @@
43 65 return 1;
44 66 }
45 67  
46   -void uuid_str_to_bin(const char *uuid, unsigned char *out)
  68 +int uuid_str_to_bin(char *uuid, unsigned char *out)
47 69 {
48 70 uint16_t tmp16;
49 71 uint32_t tmp32;
50 72 uint64_t tmp64;
51 73  
52 74 if (!uuid || !out)
53   - return;
  75 + return -EINVAL;
54 76  
  77 + if (strlen(uuid) != UUID_STR_LEN)
  78 + return -EINVAL;
  79 +
55 80 tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16));
56 81 memcpy(out, &tmp32, 4);
57 82  
... ... @@ -66,5 +91,27 @@
66 91  
67 92 tmp64 = cpu_to_be64(simple_strtoull(uuid + 24, NULL, 16));
68 93 memcpy(out + 10, (char *)&tmp64 + 2, 6);
  94 +
  95 + return 0;
  96 +}
  97 +
  98 +void uuid_bin_to_str(unsigned char *uuid, char *str)
  99 +{
  100 + static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
  101 + 12, 13, 14, 15};
  102 + int i;
  103 +
  104 + for (i = 0; i < 16; i++) {
  105 + sprintf(str, "%02x", uuid[le[i]]);
  106 + str += 2;
  107 + switch (i) {
  108 + case 3:
  109 + case 5:
  110 + case 7:
  111 + case 9:
  112 + *str++ = '-';
  113 + break;
  114 + }
  115 + }
69 116 }