Commit d718ded056eefb6239bd2e0a57b7f6d99c6e9e4b

Authored by Przemyslaw Marczak
Committed by Tom Rini
1 parent a96a0e6153

lib: uuid: code refactor for proper maintain between uuid bin and string

Changes in lib/uuid.c to:
- uuid_str_to_bin()
- uuid_bin_to_str()

New parameter is added to specify input/output string format in listed functions
This change allows easy recognize which UUID type is or should be stored in given
string array. Binary data of UUID and GUID is always stored in big endian, only
string representations are different as follows.

String byte: 0                                  36
String char: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
string UUID:    be     be   be   be       be
string GUID:    le     le   le   be       be

This patch also updates functions calls and declarations in a whole code.

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 5 changed files with 91 additions and 36 deletions Side-by-side Diff

... ... @@ -100,8 +100,8 @@
100 100  
101 101 printf("Part\tStart LBA\tEnd LBA\t\tName\n");
102 102 printf("\tAttributes\n");
103   - printf("\tType UUID\n");
104   - printf("\tPartition UUID\n");
  103 + printf("\tType GUID\n");
  104 + printf("\tPartition GUID\n");
105 105  
106 106 for (i = 0; i < le32_to_cpu(gpt_head->num_partition_entries); i++) {
107 107 /* Stop at the first non valid PTE */
108 108  
... ... @@ -114,11 +114,11 @@
114 114 print_efiname(&gpt_pte[i]));
115 115 printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
116 116 uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
117   - uuid_bin_to_str(uuid_bin, uuid);
  117 + uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
118 118 printf("\ttype:\t%s\n", uuid);
119 119 uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
120   - uuid_bin_to_str(uuid_bin, uuid);
121   - printf("\tuuid:\t%s\n", uuid);
  120 + uuid_bin_to_str(uuid_bin, uuid, UUID_STR_FORMAT_GUID);
  121 + printf("\tguid:\t%s\n", uuid);
122 122 }
123 123  
124 124 /* Remember to free pte */
... ... @@ -165,7 +165,8 @@
165 165 sprintf((char *)info->type, "U-Boot");
166 166 info->bootable = is_bootable(&gpt_pte[part - 1]);
167 167 #ifdef CONFIG_PARTITION_UUIDS
168   - uuid_bin_to_str(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,
  169 + UUID_STR_FORMAT_GUID);
169 170 #endif
170 171  
171 172 debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s", __func__,
... ... @@ -323,7 +324,7 @@
323 324 str_uuid = partitions[i].uuid;
324 325 bin_uuid = gpt_e[i].unique_partition_guid.b;
325 326  
326   - if (uuid_str_to_bin(str_uuid, bin_uuid)) {
  327 + if (uuid_str_to_bin(str_uuid, bin_uuid, UUID_STR_FORMAT_STD)) {
327 328 printf("Partition no. %d: invalid guid: %s\n",
328 329 i, str_uuid);
329 330 return -1;
... ... @@ -370,7 +371,7 @@
370 371 gpt_h->header_crc32 = 0;
371 372 gpt_h->partition_entry_array_crc32 = 0;
372 373  
373   - if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b))
  374 + if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b, UUID_STR_FORMAT_GUID))
374 375 return -1;
375 376  
376 377 return 0;
... ... @@ -822,9 +822,7 @@
822 822 void mdelay(unsigned long);
823 823  
824 824 /* lib/uuid.c */
825   -void uuid_bin_to_str(unsigned char *uuid, char *str);
826   -int uuid_str_to_bin(char *uuid, unsigned char *out);
827   -int uuid_str_valid(const char *uuid);
  825 +#include <uuid.h>
828 826  
829 827 /* lib/vsprintf.c */
830 828 #include <vsprintf.h>
  1 +/*
  2 + * Copyright (C) 2014 Samsung Electronics
  3 + * Przemyslaw Marczak <p.marczak@samsung.com>
  4 + *
  5 + * SPDX-License-Identifier: GPL-2.0+
  6 + */
  7 +#ifndef __UUID_H__
  8 +#define __UUID_H__
  9 +
  10 +enum {
  11 + UUID_STR_FORMAT_STD,
  12 + UUID_STR_FORMAT_GUID
  13 +};
  14 +
  15 +#define UUID_STR_LEN 36
  16 +#define UUID_BIN_LEN 16
  17 +
  18 +int uuid_str_valid(const char *uuid);
  19 +int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format);
  20 +void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format);
  21 +#endif
... ... @@ -7,9 +7,10 @@
7 7 #include <linux/ctype.h>
8 8 #include <errno.h>
9 9 #include <common.h>
  10 +#include <asm/io.h>
  11 +#include <part_efi.h>
  12 +#include <malloc.h>
10 13  
11   -#define UUID_STR_LEN 36
12   -
13 14 /*
14 15 * UUID - Universally Unique IDentifier - 128 bits unique number.
15 16 * There are 5 versions and one variant of UUID defined by RFC4122
... ... @@ -40,7 +41,6 @@
40 41 *
41 42 * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
42 43 */
43   -
44 44 int uuid_str_valid(const char *uuid)
45 45 {
46 46 int i, valid;
47 47  
48 48  
49 49  
50 50  
51 51  
52 52  
53 53  
54 54  
55 55  
56 56  
57 57  
58 58  
59 59  
60 60  
... ... @@ -59,57 +59,92 @@
59 59 }
60 60 }
61 61  
62   - if (i != 36 || !valid)
  62 + if (i != UUID_STR_LEN || !valid)
63 63 return 0;
64 64  
65 65 return 1;
66 66 }
67 67  
68   -int uuid_str_to_bin(char *uuid, unsigned char *out)
  68 +/*
  69 + * uuid_str_to_bin() - convert string UUID or GUID to big endian binary data.
  70 + *
  71 + * @param uuid_str - pointer to UUID or GUID string [37B]
  72 + * @param uuid_bin - pointer to allocated array for big endian output [16B]
  73 + * @str_format - UUID string format: 0 - UUID; 1 - GUID
  74 + */
  75 +int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format)
69 76 {
70 77 uint16_t tmp16;
71 78 uint32_t tmp32;
72 79 uint64_t tmp64;
73 80  
74   - if (!uuid || !out)
  81 + if (!uuid_str_valid(uuid_str))
75 82 return -EINVAL;
76 83  
77   - if (strlen(uuid) != UUID_STR_LEN)
78   - return -EINVAL;
  84 + if (str_format == UUID_STR_FORMAT_STD) {
  85 + tmp32 = cpu_to_be32(simple_strtoul(uuid_str, NULL, 16));
  86 + memcpy(uuid_bin, &tmp32, 4);
79 87  
80   - tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16));
81   - memcpy(out, &tmp32, 4);
  88 + tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 9, NULL, 16));
  89 + memcpy(uuid_bin + 4, &tmp16, 2);
82 90  
83   - tmp16 = cpu_to_le16(simple_strtoul(uuid + 9, NULL, 16));
84   - memcpy(out + 4, &tmp16, 2);
  91 + tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 14, NULL, 16));
  92 + memcpy(uuid_bin + 6, &tmp16, 2);
  93 + } else {
  94 + tmp32 = cpu_to_le32(simple_strtoul(uuid_str, NULL, 16));
  95 + memcpy(uuid_bin, &tmp32, 4);
85 96  
86   - tmp16 = cpu_to_le16(simple_strtoul(uuid + 14, NULL, 16));
87   - memcpy(out + 6, &tmp16, 2);
  97 + tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 9, NULL, 16));
  98 + memcpy(uuid_bin + 4, &tmp16, 2);
88 99  
89   - tmp16 = cpu_to_be16(simple_strtoul(uuid + 19, NULL, 16));
90   - memcpy(out + 8, &tmp16, 2);
  100 + tmp16 = cpu_to_le16(simple_strtoul(uuid_str + 14, NULL, 16));
  101 + memcpy(uuid_bin + 6, &tmp16, 2);
  102 + }
91 103  
92   - tmp64 = cpu_to_be64(simple_strtoull(uuid + 24, NULL, 16));
93   - memcpy(out + 10, (char *)&tmp64 + 2, 6);
  104 + tmp16 = cpu_to_be16(simple_strtoul(uuid_str + 19, NULL, 16));
  105 + memcpy(uuid_bin + 8, &tmp16, 2);
94 106  
  107 + tmp64 = cpu_to_be64(simple_strtoull(uuid_str + 24, NULL, 16));
  108 + memcpy(uuid_bin + 10, (char *)&tmp64 + 2, 6);
  109 +
95 110 return 0;
96 111 }
97 112  
98   -void uuid_bin_to_str(unsigned char *uuid, char *str)
  113 +/*
  114 + * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
  115 + *
  116 + * @param uuid_bin - pointer to binary data of UUID (big endian) [16B]
  117 + * @param uuid_str - pointer to allocated array for output string [37B]
  118 + * @str_format - UUID string format: 0 - UUID; 1 - GUID
  119 + */
  120 +void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format)
99 121 {
100   - static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
101   - 12, 13, 14, 15};
  122 + const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
  123 + 9, 10, 11, 12, 13, 14, 15};
  124 + const u8 guid_char_order[UUID_BIN_LEN] = {3, 2, 1, 0, 5, 4, 7, 6, 8,
  125 + 9, 10, 11, 12, 13, 14, 15};
  126 + const u8 *char_order;
102 127 int i;
103 128  
  129 + /*
  130 + * UUID and GUID bin data - always in big endian:
  131 + * 4B-2B-2B-2B-6B
  132 + * be be be be be
  133 + */
  134 + if (str_format == UUID_STR_FORMAT_STD)
  135 + char_order = uuid_char_order;
  136 + else
  137 + char_order = guid_char_order;
  138 +
104 139 for (i = 0; i < 16; i++) {
105   - sprintf(str, "%02x", uuid[le[i]]);
106   - str += 2;
  140 + sprintf(uuid_str, "%02x", uuid_bin[char_order[i]]);
  141 + uuid_str += 2;
107 142 switch (i) {
108 143 case 3:
109 144 case 5:
110 145 case 7:
111 146 case 9:
112   - *str++ = '-';
  147 + *uuid_str++ = '-';
113 148 break;
114 149 }
115 150 }
... ... @@ -439,7 +439,7 @@
439 439 *e++ = 17;
440 440 *e++ = 0; /* type 0 - UUID */
441 441  
442   - uuid_str_to_bin(uuid, e);
  442 + uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
443 443 e += 16;
444 444 } else {
445 445 printf("Invalid pxeuuid: %s\n", uuid);