summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPrzemyslaw Marczak <p.marczak@samsung.com>2014-04-02 10:20:02 +0200
committerTom Rini <trini@ti.com>2014-04-02 15:44:40 -0400
commita96a0e6153e3d9071c1a4516bf3e94c4cd96c77c (patch)
tree31b51b7f6fc24fda1236a482527922ef0e975019 /lib
parent3f62971162370213271dc1e7a396b6b3a6d5b6c6 (diff)
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
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile1
-rw-r--r--lib/uuid.c61
2 files changed, 55 insertions, 7 deletions
diff --git a/lib/Makefile b/lib/Makefile
index ae8086529e..d7ff7caa35 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -61,6 +61,7 @@ obj-y += string.o
obj-y += time.o
obj-$(CONFIG_TRACE) += trace.o
obj-$(CONFIG_BOOTP_PXE) += uuid.o
+obj-$(CONFIG_PARTITION_UUIDS) += uuid.o
obj-y += vsprintf.o
obj-$(CONFIG_LIB_RAND) += rand.o
diff --git a/lib/uuid.c b/lib/uuid.c
index c48bf38362..3af3a7dfae 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -5,18 +5,40 @@
*/
#include <linux/ctype.h>
-#include "common.h"
+#include <errno.h>
+#include <common.h>
+
+#define UUID_STR_LEN 36
/*
- * This is what a UUID string looks like.
+ * UUID - Universally Unique IDentifier - 128 bits unique number.
+ * There are 5 versions and one variant of UUID defined by RFC4122
+ * specification. Depends on version uuid number base on a time,
+ * host name, MAC address or random data.
+ *
+ * UUID binary format (16 bytes):
+ *
+ * 4B-2B-2B-2B-6B (big endian - network byte order)
+ *
+ * UUID string is 36 length of characters (36 bytes):
+ *
+ * 0 9 14 19 24
+ * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
+ * be be be be be
+ *
+ * where x is a hexadecimal character. Fields are separated by '-'s.
+ * When converting to a binary UUID, le means the field should be converted
+ * to little endian and be means it should be converted to big endian.
*
- * x is a hexadecimal character. fields are separated by '-'s. When converting
- * to a binary UUID, le means the field should be converted to little endian,
- * and be means it should be converted to big endian.
+ * UUID is also used as GUID (Globally Unique Identifier) with the same binary
+ * format but it differs in string format like below.
*
+ * GUID:
* 0 9 14 19 24
* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
* le le le be be
+ *
+ * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id.
*/
int uuid_str_valid(const char *uuid)
@@ -43,14 +65,17 @@ int uuid_str_valid(const char *uuid)
return 1;
}
-void uuid_str_to_bin(const char *uuid, unsigned char *out)
+int uuid_str_to_bin(char *uuid, unsigned char *out)
{
uint16_t tmp16;
uint32_t tmp32;
uint64_t tmp64;
if (!uuid || !out)
- return;
+ return -EINVAL;
+
+ if (strlen(uuid) != UUID_STR_LEN)
+ return -EINVAL;
tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16));
memcpy(out, &tmp32, 4);
@@ -66,4 +91,26 @@ void uuid_str_to_bin(const char *uuid, unsigned char *out)
tmp64 = cpu_to_be64(simple_strtoull(uuid + 24, NULL, 16));
memcpy(out + 10, (char *)&tmp64 + 2, 6);
+
+ return 0;
+}
+
+void uuid_bin_to_str(unsigned char *uuid, char *str)
+{
+ static const u8 le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11,
+ 12, 13, 14, 15};
+ int i;
+
+ for (i = 0; i < 16; i++) {
+ sprintf(str, "%02x", uuid[le[i]]);
+ str += 2;
+ switch (i) {
+ case 3:
+ case 5:
+ case 7:
+ case 9:
+ *str++ = '-';
+ break;
+ }
+ }
}