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 representati 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.marc...@samsung.com> Cc: Stephen Warren <swar...@nvidia.com> Cc: Lukasz Majewski <l.majew...@samsung.com> Cc: tr...@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 avoi 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 Changes v4: - add new parameter to define UUID string format for UUID or GUID which differs in endianness of first three string blocks. - add uuid structure and version 4 data to uuid header file - lib/Makefile: add CONFIG_RAND_UUID dependency to rand.o and uuid.o --- include/common.h | 3 ++- include/uuid.h | 22 ++++++++++++++++++- lib/Makefile | 2 ++ lib/uuid.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 88 insertions(+), 4 deletions(-) diff --git a/include/common.h b/include/common.h index 658c326..deb08f2 100644 --- a/include/common.h +++ b/include/common.h @@ -830,7 +830,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/include/uuid.h b/include/uuid.h index 0d16038..32e592c 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -7,15 +7,35 @@ #ifndef __UUID_H__ #define __UUID_H__ +/* 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; + typedef enum { UUID_STR_FORMAT_STD, UUID_STR_FORMAT_GUID } uuid_str_t; #define UUID_STR_LEN 36 -#define UUID_BIN_LEN 16 +#define UUID_BIN_LEN sizeof(struct uuid) + +#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 int uuid_str_valid(const char *uuid); int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, uuid_str_t format); void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, uuid_str_t format); +void gen_rand_uuid(unsigned char *uuid_bin); +void gen_rand_uuid_str(char *uuid_str, uuid_str_t format); #endif diff --git a/lib/Makefile b/lib/Makefile index 2e8bd20..fd75e80 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -63,6 +63,8 @@ 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 75a5608..d3ba60e 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -14,8 +14,23 @@ /* * 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. + * specification. Depends on version uuid number base on: + * - time, MAC address(v1), + * - user ID(v2), + * - MD5 of name or URL(v3), + * - random data(v4), + * - SHA-1 of name or URL(v5), + * + * This library implements UUID v4. + * + * 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 * * UUID binary format (16 bytes): * @@ -151,3 +166,49 @@ void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, } } } + +/* + * gen_rand_uuid() - this function generates a random binary UUID v4. + * + * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian. +*/ +#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 UUID v4 (random) in two string + * formats UUID or GUID. + * + * @param uuid_str - pointer to allocated array [37B]. + * @param - uuid output type: UUID - 0, GUID - 1 + */ +void gen_rand_uuid_str(char *uuid_str, uuid_str_t str_format) +{ + unsigned char uuid_bin[UUID_BIN_LEN]; + + /* Generate UUID (big endian) */ + gen_rand_uuid(uuid_bin); + + /* Convert UUID bin to UUID or GUID formated STRING */ + uuid_bin_to_str(uuid_bin, uuid_str, str_format); +} +#endif -- 1.9.0 _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot