From patchwork Tue Apr 1 14:30:40 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Przemyslaw Marczak X-Patchwork-Id: 335954 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 9195E14009D for ; Wed, 2 Apr 2014 01:31:55 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id BCB844B869; Tue, 1 Apr 2014 16:31:46 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JXEQMDK3nVau; Tue, 1 Apr 2014 16:31:46 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 182F54B847; Tue, 1 Apr 2014 16:31:29 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 19D1A4B829 for ; Tue, 1 Apr 2014 16:31:22 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xQANT7Yq70fd for ; Tue, 1 Apr 2014 16:31:19 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mailout4.w1.samsung.com (mailout4.w1.samsung.com [210.118.77.14]) by theia.denx.de (Postfix) with ESMTPS id A7E9B4B819 for ; Tue, 1 Apr 2014 16:31:14 +0200 (CEST) Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout4.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0N3C00ENUWBWXV90@mailout4.w1.samsung.com> for u-boot@lists.denx.de; Tue, 01 Apr 2014 15:31:08 +0100 (BST) X-AuditID: cbfec7f4-b7f796d000005a13-7e-533acdadf671 Received: from eusync3.samsung.com ( [203.254.199.213]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id E5.A4.23059.DADCA335; Tue, 01 Apr 2014 15:31:09 +0100 (BST) Received: from AMDC1186.digital.local ([106.116.147.185]) by eusync3.samsung.com (Oracle Communications Messaging Server 7u4-23.01(7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0N3C00M7IWBQN710@eusync3.samsung.com>; Tue, 01 Apr 2014 15:31:09 +0100 (BST) From: Przemyslaw Marczak To: u-boot@lists.denx.de Date: Tue, 01 Apr 2014 16:30:40 +0200 Message-id: <1396362643-4280-3-git-send-email-p.marczak@samsung.com> X-Mailer: git-send-email 1.9.0 In-reply-to: <1396362643-4280-1-git-send-email-p.marczak@samsung.com> References: <1395251911-26540-1-git-send-email-p.marczak@samsung.com> <1396362643-4280-1-git-send-email-p.marczak@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrOJMWRmVeSWpSXmKPExsVy+t/xq7prz1oFG0zbam3x5uFmRosdl2+w WKx7spbV4sb0FlaLyYvnM1u83dvJbjH54StGB3aPebNOsHicvbOD0aO3+R2bR9+WVYwex29s ZwpgjeKySUnNySxLLdK3S+DKmHQ/o+CFZUXDvg7mBsbJel2MnBwSAiYS987PZIewxSQu3FvP 1sXIxSEksJRRYvX6DewQTh+TxMuTZ8Gq2AQMJPZcOsMMYosISEj86r/KCFLELHCWUeLk1W1g RcIC3hI/Zvxk6WLk4GARUJX4v9MfJMwr4CJx/8pEJohtchJTfi8AK+cUcJW4ePst1OYGRolv vXvYJzDyLmBkWMUomlqaXFCclJ5rqFecmFtcmpeul5yfu4kRElhfdjAuPmZ1iFGAg1GJh9ei 3DJYiDWxrLgy9xCjBAezkgiv9QqrYCHelMTKqtSi/Pii0pzU4kOMTBycUg2M0bsseld+3GyQ feK5cnO6wqpb1scO6DfuinaYvObrrB45ho41V+vEBB9/evI/3uifgElLjXFXiuCmkxzc2ql/ va4Jv1NSjq6dqm/6mPvfvVOW3le2BwadT0jfnf7/sUXR9ZdhwuLfK14YLbaOkHg+44dXw6GF r3pNxC/Ee7xRVbwk4ByuJFmkxFKckWioxVxUnAgArOBkjAoCAAA= Cc: Stephen Warren , Piotr Wilczek , Przemyslaw Marczak , Tom Rini Subject: [U-Boot] [PATCH v5 3/6] lib: uuid: add functions to generate UUID version 4 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de 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: - new configs: - CONFIG_LIB_UUID for compile lib/uuid.c - CONFIG_RANDOM_UUID for functions gen_rand_uuid() and gen_rand_uuid_str() - add configs dependency to include/config_fallbacks.h for lib uuid. lib/uuid.c: - add gen_rand_uuid() - this function writes 16 bytes len binary representation of 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 Cc: Stephen Warren Cc: Lukasz Majewski 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 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 Changes v5: - reword some comments - introduce config CMD_LIB_UUID, cleanup config dependency - remove use of typedef uuid_str_t --- include/config_fallbacks.h | 13 +++++++++ include/uuid.h | 22 +++++++++++++++- lib/Makefile | 3 +-- lib/uuid.c | 66 ++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 99 insertions(+), 5 deletions(-) diff --git a/include/config_fallbacks.h b/include/config_fallbacks.h index d8339b2..f31a2f5 100644 --- a/include/config_fallbacks.h +++ b/include/config_fallbacks.h @@ -55,6 +55,19 @@ #define HAVE_BLOCK_DEVICE #endif +#if (defined(CONFIG_PARTITION_UUIDS) || \ + defined(CONFIG_RANDOM_UUID) || \ + defined(CONFIG_BOOTP_PXE)) && \ + !defined(CONFIG_LIB_UUID) +#define CONFIG_LIB_UUID +#endif + +#if defined(CONFIG_RANDOM_UUID) && \ + !defined(CONFIG_LIB_RAND) && \ + !defined(CONFIG_LIB_HW_RAND) +#define CONFIG_LIB_RAND +#endif + #ifndef CONFIG_SYS_PROMPT #define CONFIG_SYS_PROMPT "=> " #endif diff --git a/include/uuid.h b/include/uuid.h index e8feeed..93027c1 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; + enum { UUID_STR_FORMAT_STD, UUID_STR_FORMAT_GUID }; #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, int str_format); void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format); +void gen_rand_uuid(unsigned char *uuid_bin); +void gen_rand_uuid_str(char *uuid_str, int str_format); #endif diff --git a/lib/Makefile b/lib/Makefile index d7ff7ca..27e4f78 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -60,8 +60,7 @@ obj-$(CONFIG_REGEX) += slre.o 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-$(CONFIG_LIB_UUID) += uuid.o obj-y += vsprintf.o obj-$(CONFIG_LIB_RAND) += rand.o diff --git a/lib/uuid.c b/lib/uuid.c index 8db865f..44d0c93 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -14,8 +14,22 @@ /* * 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. A UUID contains a set of fields. The set varies + * depending on the version of the UUID, as shown below: + * - time, MAC address(v1), + * - user ID(v2), + * - MD5 of name or URL(v3), + * - random data(v4), + * - SHA-1 of name or URL(v5), + * + * Layout of UUID: + * 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 + * + * source: https://www.ietf.org/rfc/rfc4122.txt * * UUID binary format (16 bytes): * @@ -149,3 +163,51 @@ void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format) } } } + +/* + * gen_rand_uuid() - this function generates a random binary UUID version 4. + * In this version all fields beside 4 bits of version and + * 2 bits of variant are randomly generated. + * + * @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, int 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