[U-Boot] [PATCH V2 2/3] lib: uuid: add functions to generate UUID version 4
Przemyslaw Marczak
p.marczak at samsung.com
Wed Mar 5 17:45:21 CET 2014
This patch adds support to generate UUID (Universally Unique Identifier)
in version 4 based on RFC4122, which is randomly.
Source: https://www.ietf.org/rfc/rfc4122.txt
Changes:
- add new config: CONFIG_RANDOM_UUID: compile uuid.c and rand.c
Update files:
- include/common.h
- lib/Makefile
- lib/uuid.c
lib/uuid.c:
- add gen_rand_uuid() - this function writes 16 bytes len binary representation
UUID v4 to address given by user.
- add gen_rand_uuid_str() - this function writes 37 bytes len hexadecimal
ASCII string representation of 16 bytes binary UUID to address given by user.
Signed-off-by: Przemyslaw Marczak <p.marczak at samsung.com>
cc: Stephen Warren <swarren at nvidia.com>
cc: trini at ti.com
---
Changes v2:
- put uuid generation changes in a separate commit
- get_uuid_str() - change name to gen_rand_uuid_str()
- add new function: gen_rand_uuid()
- remove unnecessary '\0' at the end of uuid string
- drop unnecessary error checking
- functions now takes pointers to allocated memory instead of alloc it itself
- add new config option: CONFIG_RANDOM_UUID
---
include/common.h | 5 +++-
lib/Makefile | 4 ++-
lib/uuid.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 88 insertions(+), 2 deletions(-)
diff --git a/include/common.h b/include/common.h
index 32377ad..20e9ae6 100644
--- a/include/common.h
+++ b/include/common.h
@@ -815,6 +815,8 @@ void udelay (unsigned long);
void mdelay(unsigned long);
/* lib/uuid.c */
+void gen_rand_uuid(unsigned char *uuid_bin);
+void gen_rand_uuid_str(char *uuid_str);
void 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);
@@ -831,7 +833,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_RANDOM_UUID)
#define RAND_MAX -1U
void srand(unsigned int seed);
unsigned int rand(void);
diff --git a/lib/Makefile b/lib/Makefile
index 70962b2..64a430f 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -59,10 +59,12 @@ obj-y += linux_string.o
obj-$(CONFIG_REGEX) += slre.o
obj-y += string.o
obj-y += time.o
+obj-y += vsprintf.o
obj-$(CONFIG_TRACE) += trace.o
obj-$(CONFIG_BOOTP_PXE) += uuid.o
obj-$(CONFIG_PARTITION_UUIDS) += uuid.o
-obj-y += vsprintf.o
+obj-$(CONFIG_RANDOM_UUID) += uuid.o
+obj-$(CONFIG_RANDOM_UUID) += rand.o
obj-$(CONFIG_RANDOM_MACADDR) += rand.o
obj-$(CONFIG_BOOTP_RANDOM_DELAY) += rand.o
obj-$(CONFIG_CMD_LINK_LOCAL) += rand.o
diff --git a/lib/uuid.c b/lib/uuid.c
index 803bdcd..c0218ba 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -7,6 +7,29 @@
#include <linux/ctype.h>
#include <errno.h>
#include <common.h>
+#include <part_efi.h>
+#include <malloc.h>
+
+#define UUID_STR_LEN 36
+#define UUID_STR_BYTE_LEN 37
+#define UUID_BIN_BYTE_LEN 16
+
+#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.
@@ -94,3 +117,61 @@ void uuid_bin_to_str(unsigned char *uuid, char *str)
}
}
}
+
+/*
+ * gen_rand_uuid() - this function generates 16 bytes len UUID V4 (randomly)
+ * and stores it at a given pointer.
+ *
+ * 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.
+ * source: https://www.ietf.org/rfc/rfc4122.txt
+ *
+ * @param uuid_bin pointer to 16 bytes len array
+*/
+void gen_rand_uuid(unsigned char *uuid_bin)
+{
+ struct uuid *uuid = (struct uuid *)uuid_bin;
+ unsigned int *ptr = (unsigned int *)uuid_bin;
+ int i;
+
+ if (!uuid_bin)
+ return;
+
+ memset(uuid_bin, 0x0, sizeof(struct uuid));
+
+ /* Set all fields randomly */
+ for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); 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;
+}
+
+/*
+ * gen_rand_uuid_str() - this function generates UUID v4 (randomly)
+ * into 36 character hexadecimal ASCII string representation of a 128-bit
+ * (16 octets) UUID (Universally Unique Identifier) version 4 based on RFC4122
+ * and stores it at a given pointer.
+ *
+ * @param uuid_str pointer to 37 bytes len array
+ */
+void gen_rand_uuid_str(char *uuid_str)
+{
+ unsigned char uuid_bin[UUID_BIN_BYTE_LEN];
+
+ if (!uuid_str)
+ return;
+
+ gen_rand_uuid(uuid_bin);
+
+ uuid_bin_to_str(uuid_bin, uuid_str);
+}
--
1.7.9.5
More information about the U-Boot
mailing list