diff mbox

[U-Boot,v3,2/3] lib: uuid: add functions to generate UUID version 4

Message ID bf85640549b0798b838145e9bad6dcc59454e7ae.1394807506.git.p.marczak@samsung.com
State Superseded
Delegated to: Tom Rini
Headers show

Commit Message

Przemyslaw Marczak March 14, 2014, 2:37 p.m. UTC
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

lib/uuid.c:
- add gen_rand_uuid() - this function writes 16 bytes len binary representation
  UUID v4 to the memory at given address.

- add gen_rand_uuid_str() - this function writes 37 bytes len hexadecimal
  ASCII string representation of UUID v4 to the memory at given address.

Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
Cc: Jason Hobbs <jason.hobbs@calxeda.com>
Cc: Stephen Warren <swarren@nvidia.com>
Cc: Lukasz Majewski <l.majewski@samsung.com>
Cc: trini@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

Changes v3:
- remove unused UUID_STR_BYTE_LEN
- reword comments
- remove null pointer checking from gen_rand_uuid() and gen_rand_uuid_str()
- remove unneeded memset from gen_rand_uuid()
- undo moving vsprintf.o object in lib/Makefile
- add attribute "packed" to the uuid structure
- gen_rand_uuid(): add endian functions for modify uuid data
- gen_rand_uuid(): use memcpy() to store uuid data into given buffer for avoid
  unaligned access issues
- change uuid version and variant masks to proper for use with clrsetbits_*
- add #ifdef CONFIG_RANDOM_UUID to random uuid code for avoid warnings
---
 include/common.h |  5 +++-
 lib/Makefile     |  3 +++
 lib/uuid.c       | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 84 insertions(+), 2 deletions(-)

Comments

Wolfgang Denk March 14, 2014, 4:12 p.m. UTC | #1
Dear Przemyslaw Marczak,

In message <bf85640549b0798b838145e9bad6dcc59454e7ae.1394807506.git.p.marczak@samsung.com> you wrote:
> 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
...

> +#define UUID_BIN_LEN		16
...
> +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];
> +} __packed;

Maybe we should define UUID_BIN_LEN as sizeof(struct uuid) ?


Best regards,

Wolfgang Denk
Przemyslaw Marczak March 17, 2014, 9:16 a.m. UTC | #2
On 03/14/2014 05:12 PM, Wolfgang Denk wrote:
> Dear Przemyslaw Marczak,
>
> In message <bf85640549b0798b838145e9bad6dcc59454e7ae.1394807506.git.p.marczak@samsung.com> you wrote:
>> 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
> ...
>
>> +#define UUID_BIN_LEN		16
> ...
>> +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];
>> +} __packed;
>
> Maybe we should define UUID_BIN_LEN as sizeof(struct uuid) ?
>
>
> Best regards,
>
> Wolfgang Denk
>
Ok, I add this.

Thanks
diff mbox

Patch

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..fc7a24a 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -62,7 +62,10 @@  obj-y += time.o
 obj-$(CONFIG_TRACE) += trace.o
 obj-$(CONFIG_BOOTP_PXE) += uuid.o
 obj-$(CONFIG_PARTITION_UUIDS) += uuid.o
+obj-$(CONFIG_PARTITION_UUIDS) += rand.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 925e501..0f22e14 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -7,8 +7,30 @@ 
 #include <linux/ctype.h>
 #include <errno.h>
 #include <common.h>
+#include <asm/io.h>
+#include <part_efi.h>
+#include <malloc.h>
 
 #define UUID_STR_LEN		36
+#define UUID_BIN_LEN		16
+
+#define UUID_VERSION_MASK	0xf000
+#define UUID_VERSION_SHIFT	12
+#define UUID_VERSION		0x4
+
+#define UUID_VARIANT_MASK	0xc0
+#define UUID_VARIANT_SHIFT	7
+#define UUID_VARIANT		0x1
+
+/* This is structure is in big-endian */
+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];
+} __packed;
 
 /*
  * This is what a UUID string looks like.
@@ -21,7 +43,6 @@ 
  * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  *    le     le   le   be       be
  */
-
 int uuid_str_valid(const char *uuid)
 {
 	int i, valid;
@@ -95,3 +116,58 @@  void uuid_bin_to_str(unsigned char *uuid, char *str)
 		}
 	}
 }
+
+/*
+ * gen_rand_uuid() - this function generates a random binary UUID v4 and stores
+ *                   it into the memory pointed at by the supplied pointer,
+ *                   which must be 16 bytes in size
+ *
+ * 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
+*/
+#ifdef CONFIG_RANDOM_UUID
+void gen_rand_uuid(unsigned char *uuid_bin)
+{
+	struct uuid uuid;
+	unsigned int *ptr = (unsigned int *)&uuid;
+	int i;
+
+	/* Set all fields randomly */
+	for (i = 0; i < sizeof(struct uuid) / sizeof(*ptr); i++)
+		*(ptr + i) = cpu_to_be32(rand());
+
+	clrsetbits_be16(&uuid.time_hi_and_version,
+			UUID_VERSION_MASK,
+			UUID_VERSION << UUID_VERSION_SHIFT);
+
+	clrsetbits_8(&uuid.clock_seq_hi_and_reserved,
+		     UUID_VARIANT_MASK,
+		     UUID_VARIANT << UUID_VARIANT_SHIFT);
+
+	memcpy(uuid_bin, &uuid, sizeof(struct uuid));
+}
+
+/*
+ * gen_rand_uuid_str() - this function generates a random string formatted
+ *                       UUID v4 and stores it into the memory pointed at by
+ *                       the supplied pointer, which must be 37 bytes in size
+ *
+ * @param uuid_str pointer to 37 bytes len array
+ */
+void gen_rand_uuid_str(char *uuid_str)
+{
+	unsigned char uuid_bin[UUID_BIN_LEN];
+
+	gen_rand_uuid(uuid_bin);
+
+	uuid_bin_to_str(uuid_bin, uuid_str);
+}
+#endif