[U-Boot] [PATCH 1/2] lib: uuid: add function to generate UUID version 4
Przemyslaw Marczak
p.marczak at samsung.com
Fri Feb 28 16:18:31 CET 2014
lib/uuid.c:
Add get_uuid_str() - this function returns 36 character hexadecimal ASCII
string representation of a 128-bit (16 octets) UUID (Universally Unique
Identifier) version 4 based on RFC4122, which is randomly generated.
Source: https://www.ietf.org/rfc/rfc4122.txt
Changes:
Move functions:
- disk/part_efi.c uuid_string() to lib/uuid.c -> uuid_bin_to_str()
- disk/part_efi.c string_uuid() to lib/uuid.c -> uuid_str_to_bin()
Update files:
- include/common.h
- disk/part_efi.c
- lib/Makefile
Signed-off-by: Przemyslaw Marczak <p.marczak at samsung.com>
cc: Jason Hobbs <jason.hobbs at calxeda.com>
cc: Stephen Warren <swarren at nvidia.com>
cc: Lukasz Majewski <l.majewski at samsung.com>
cc: trini at ti.com
---
disk/part_efi.c | 90 ++++++------------------------------------
include/common.h | 7 +++-
lib/Makefile | 4 ++
lib/uuid.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
4 files changed, 131 insertions(+), 84 deletions(-)
diff --git a/disk/part_efi.c b/disk/part_efi.c
index 733d5bd..a280ab5 100644
--- a/disk/part_efi.c
+++ b/disk/part_efi.c
@@ -63,26 +63,6 @@ static char *print_efiname(gpt_entry *pte)
return name;
}
-static void uuid_string(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;
- }
- }
-}
-
static efi_guid_t system_guid = PARTITION_SYSTEM_GUID;
static inline int is_bootable(gpt_entry *p)
@@ -103,6 +83,7 @@ void print_part_efi(block_dev_desc_t * dev_desc)
gpt_entry *gpt_pte = NULL;
int i = 0;
char uuid[37];
+ unsigned char *uuid_bin;
if (!dev_desc) {
printf("%s: Invalid Argument(s)\n", __func__);
@@ -132,9 +113,11 @@ void print_part_efi(block_dev_desc_t * dev_desc)
le64_to_cpu(gpt_pte[i].ending_lba),
print_efiname(&gpt_pte[i]));
printf("\tattrs:\t0x%016llx\n", gpt_pte[i].attributes.raw);
- uuid_string(gpt_pte[i].partition_type_guid.b, uuid);
+ uuid_bin = (unsigned char *)gpt_pte[i].partition_type_guid.b;
+ uuid_bin_to_str(uuid_bin, uuid);
printf("\ttype:\t%s\n", uuid);
- uuid_string(gpt_pte[i].unique_partition_guid.b, uuid);
+ uuid_bin = (unsigned char *)gpt_pte[i].unique_partition_guid.b;
+ uuid_bin_to_str(uuid_bin, uuid);
printf("\tuuid:\t%s\n", uuid);
}
@@ -182,7 +165,7 @@ int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
sprintf((char *)info->type, "U-Boot");
info->bootable = is_bootable(&gpt_pte[part - 1]);
#ifdef CONFIG_PARTITION_UUIDS
- uuid_string(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
+ uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b, info->uuid);
#endif
debug("%s: start 0x" LBAF ", size 0x" LBAF ", name %s", __func__,
@@ -237,60 +220,6 @@ static int set_protective_mbr(block_dev_desc_t *dev_desc)
return 0;
}
-/**
- * string_uuid(); Convert UUID stored as string to bytes
- *
- * @param uuid - UUID represented as string
- * @param dst - GUID buffer
- *
- * @return return 0 on successful conversion
- */
-static int string_uuid(char *uuid, u8 *dst)
-{
- efi_guid_t guid;
- u16 b, c, d;
- u64 e;
- u32 a;
- u8 *p;
- u8 i;
-
- const u8 uuid_str_len = 36;
-
- /* The UUID is written in text: */
- /* 1 9 14 19 24 */
- /* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */
-
- debug("%s: uuid: %s\n", __func__, uuid);
-
- if (strlen(uuid) != uuid_str_len)
- return -1;
-
- for (i = 0; i < uuid_str_len; i++) {
- if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
- if (uuid[i] != '-')
- return -1;
- } else {
- if (!isxdigit(uuid[i]))
- return -1;
- }
- }
-
- a = (u32)simple_strtoul(uuid, NULL, 16);
- b = (u16)simple_strtoul(uuid + 9, NULL, 16);
- c = (u16)simple_strtoul(uuid + 14, NULL, 16);
- d = (u16)simple_strtoul(uuid + 19, NULL, 16);
- e = (u64)simple_strtoull(uuid + 24, NULL, 16);
-
- p = (u8 *) &e;
- guid = EFI_GUID(a, b, c, d >> 8, d & 0xFF,
- *(p + 5), *(p + 4), *(p + 3),
- *(p + 2), *(p + 1) , *p);
-
- memcpy(dst, guid.b, sizeof(efi_guid_t));
-
- return 0;
-}
-
int write_gpt_table(block_dev_desc_t *dev_desc,
gpt_header *gpt_h, gpt_entry *gpt_e)
{
@@ -358,6 +287,7 @@ int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
size_t efiname_len, dosname_len;
#ifdef CONFIG_PARTITION_UUIDS
char *str_uuid;
+ unsigned char *bin_uuid;
#endif
for (i = 0; i < parts; i++) {
@@ -391,7 +321,9 @@ int gpt_fill_pte(gpt_header *gpt_h, gpt_entry *gpt_e,
#ifdef CONFIG_PARTITION_UUIDS
str_uuid = partitions[i].uuid;
- if (string_uuid(str_uuid, gpt_e[i].unique_partition_guid.b)) {
+ bin_uuid = gpt_e[i].unique_partition_guid.b;
+
+ if (uuid_str_to_bin(str_uuid, bin_uuid)) {
printf("Partition no. %d: invalid guid: %s\n",
i, str_uuid);
return -1;
@@ -438,7 +370,7 @@ int gpt_fill_header(block_dev_desc_t *dev_desc, gpt_header *gpt_h,
gpt_h->header_crc32 = 0;
gpt_h->partition_entry_array_crc32 = 0;
- if (string_uuid(str_guid, gpt_h->disk_guid.b))
+ if (uuid_str_to_bin(str_guid, gpt_h->disk_guid.b))
return -1;
return 0;
diff --git a/include/common.h b/include/common.h
index 96a45a6..18f0224 100644
--- a/include/common.h
+++ b/include/common.h
@@ -815,7 +815,9 @@ void udelay (unsigned long);
void mdelay(unsigned long);
/* lib/uuid.c */
-void uuid_str_to_bin(const char *uuid, unsigned char *out);
+char *get_uuid_str(void);
+int uuid_bin_to_str(unsigned char *uuid, char *str);
+int uuid_str_to_bin(char *uuid, unsigned char *out);
int uuid_str_valid(const char *uuid);
/* lib/vsprintf.c */
@@ -830,7 +832,8 @@ char * strmhz(char *buf, unsigned long hz);
/* lib/rand.c */
#if defined(CONFIG_RANDOM_MACADDR) || \
defined(CONFIG_BOOTP_RANDOM_DELAY) || \
- defined(CONFIG_CMD_LINK_LOCAL)
+ defined(CONFIG_CMD_LINK_LOCAL) || \
+ defined(CONFIG_PARTITION_UUIDS)
#define RAND_MAX -1U
void srand(unsigned int seed);
unsigned int rand(void);
diff --git a/lib/Makefile b/lib/Makefile
index dedb97b..ca7f878 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -65,3 +65,7 @@ obj-y += vsprintf.o
obj-$(CONFIG_RANDOM_MACADDR) += rand.o
obj-$(CONFIG_BOOTP_RANDOM_DELAY) += rand.o
obj-$(CONFIG_CMD_LINK_LOCAL) += rand.o
+ifdef CONFIG_PARTITION_UUIDS
+obj-y += rand.o
+obj-y += uuid.o
+endif
diff --git a/lib/uuid.c b/lib/uuid.c
index c48bf38..d75ae16 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -5,7 +5,29 @@
*/
#include <linux/ctype.h>
-#include "common.h"
+#include <errno.h>
+#include <common.h>
+#include <part_efi.h>
+#include <malloc.h>
+
+#define UUID_STR_BYTE_LEN 37
+
+#define UUID_VERSION_CLEAR_BITS 0x0fff
+#define UUID_VERSION_SHIFT 12
+#define UUID_VERSION 0x4
+
+#define UUID_VARIANT_CLEAR_BITS 0x3f
+#define UUID_VARIANT_SHIFT 7
+#define UUID_VARIANT 0x1
+
+struct uuid {
+ unsigned int time_low;
+ unsigned short time_mid;
+ unsigned short time_hi_and_version;
+ unsigned char clock_seq_hi_and_reserved;
+ unsigned char clock_seq_low;
+ unsigned char node[6];
+};
/*
* This is what a UUID string looks like.
@@ -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 (!uuid_str_valid(uuid))
+ return -EINVAL;
tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16));
memcpy(out, &tmp32, 4);
@@ -66,4 +91,87 @@ 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;
+}
+
+int 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};
+ char *str_ptr = str;
+ 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;
+ }
+ }
+
+ if (!uuid_str_valid(str_ptr))
+ return -EINVAL;
+
+ return 0;
+}
+
+/*
+ * get_uuid_str() - this function returns pointer to 36 character hexadecimal
+ * ASCII string representation of a 128-bit (16 octets) UUID (Universally
+ * Unique Identifier) version 4 based on RFC4122.
+ * source: https://www.ietf.org/rfc/rfc4122.txt
+ *
+ * Layout of UUID Version 4:
+ * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
+ * version - 4 bit (bit 4 through 7 of the time_hi_and_version)
+ * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
+ * variant: - bit 6 and 7 of clock_seq_hi_and_reserved
+ * node - 48 bit
+ * In this version all fields beside 4 bit version are randomly generated.
+ *
+ * @ret: pointer to 36 bytes len characters array
+ */
+char *get_uuid_str(void)
+{
+ struct uuid uuid;
+ char *uuid_str = NULL;
+ int *ptr = (int *)&uuid;
+ int i;
+
+ uuid_str = malloc(UUID_STR_BYTE_LEN);
+ if (!uuid_str) {
+ error("uuid_str pointer is null");
+ return NULL;
+ }
+
+ memset(&uuid, 0x0, sizeof(uuid));
+
+ /* Set all fields randomly */
+ for (i = 0; i < sizeof(uuid) / 4; i++)
+ *(ptr + i) = rand();
+
+ /* Set V4 format */
+ uuid.time_hi_and_version &= UUID_VERSION_CLEAR_BITS;
+ uuid.time_hi_and_version |= UUID_VERSION << UUID_VERSION_SHIFT;
+
+ uuid.clock_seq_hi_and_reserved &= UUID_VARIANT_CLEAR_BITS;
+ uuid.clock_seq_hi_and_reserved |= UUID_VARIANT << UUID_VARIANT_SHIFT;
+
+ uuid_bin_to_str((unsigned char *)&uuid, uuid_str);
+
+ if (!uuid_str_valid(uuid_str)) {
+ error("Invalid UUID string");
+ return NULL;
+ }
+
+ /* Put end of string */
+ uuid_str[UUID_STR_BYTE_LEN - 1] = '\0';
+
+ return uuid_str;
}
--
1.7.9.5
More information about the U-Boot
mailing list