From patchwork Wed Apr 2 08:20:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Przemyslaw Marczak X-Patchwork-Id: 336273 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 E1BC11400E0 for ; Wed, 2 Apr 2014 19:21:10 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id E6BD94BB54; Wed, 2 Apr 2014 10:21:05 +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 317JTRfZGbpX; Wed, 2 Apr 2014 10:21:05 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id C7A4C4BB49; Wed, 2 Apr 2014 10:20:52 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 63E3A4BB3D for ; Wed, 2 Apr 2014 10:20:47 +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 Ntz6-Xt5TK8f for ; Wed, 2 Apr 2014 10:20:42 +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 mailout3.w1.samsung.com (mailout3.w1.samsung.com [210.118.77.13]) by theia.denx.de (Postfix) with ESMTPS id 5722A4BB3E for ; Wed, 2 Apr 2014 10:20:38 +0200 (CEST) Received: from eucpsbgm1.samsung.com (unknown [203.254.199.244]) by mailout3.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0N3E00EWG9UAJ070@mailout3.w1.samsung.com> for u-boot@lists.denx.de; Wed, 02 Apr 2014 09:20:34 +0100 (BST) X-AuditID: cbfec7f4-b7f796d000005a13-eb-533bc8522897 Received: from eusync4.samsung.com ( [203.254.199.214]) by eucpsbgm1.samsung.com (EUCPMTA) with SMTP id DD.78.23059.258CB335; Wed, 02 Apr 2014 09:20:34 +0100 (BST) Received: from AMDC1186.digital.local ([106.116.147.185]) by eusync4.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0N3E00C5D9U22Z80@eusync4.samsung.com>; Wed, 02 Apr 2014 09:20:34 +0100 (BST) From: Przemyslaw Marczak To: u-boot@lists.denx.de Date: Wed, 02 Apr 2014 10:20:02 +0200 Message-id: <1396426807-30510-1-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: <1396362643-4280-1-git-send-email-p.marczak@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrBJMWRmVeSWpSXmKPExsVy+t/xa7pBJ6yDDQ43M1u8ebiZ0WLH5Rss FuuerGW1uDG9hdVi8uL5zBZv93ayW0x++IrRgd1j3qwTLB5n7+xg9Ohtfsfm0bdlFaPH8Rvb mQJYo7hsUlJzMstSi/TtErgyNr7cylzQ517xeNcuxgbG0+ZdjJwcEgImEufPr2SHsMUkLtxb z9bFyMUhJLCUUWLixBtQTh+TxOfzZxhBqtgEDCT2XDrDDGKLCEhI/Oq/yghSxCxwllHi5NVt YKOEBaIlpmy4ywJiswioSuy61w3WzCvgKvFs1i2odXISU34vALM5geIXb79lA7GFBFwk1j15 xjKBkXcBI8MqRtHU0uSC4qT0XEO94sTc4tK8dL3k/NxNjJDQ+rKDcfExq0OMAhyMSjy8Epes goVYE8uKK3MPMUpwMCuJ8HIdtA4W4k1JrKxKLcqPLyrNSS0+xMjEwSnVwJjcb8m3l+2M84Oe FB9PwZzO7ykJ3Re94iwiHt213FO8WerMmYP/69pFzp3gDd+/82TD+u32NZ9nrz0pECvRlHT6 uIdK/aWsw3fuB0zenvPz7AFGzl3dWlllefdqGRcV++7si+8T8i7P2XSBITNtp929qXO0XQx+ lr0QcS/1XWou0FTm9sT1thJLcUaioRZzUXEiACapZ0gLAgAA Cc: Stephen Warren , Piotr Wilczek , Przemyslaw Marczak , Tom Rini Subject: [U-Boot] [PATCH v6 1/6] part_efi: move uuid<->string conversion functions into lib/uuid.c 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 commit introduces cleanup for uuid library. Changes: - move uuid<->string conversion functions into lib/uuid.c so they can be used by code outside part_efi.c. - rename uuid_string() to uuid_bin_to_str() for consistency with existing uuid_str_to_bin() - add an error return code to uuid_str_to_bin() - update existing code to the new library functions. Signed-off-by: Przemyslaw Marczak Cc: Stephen Warren Cc: Lukasz Majewski Cc: trini@ti.com --- Changes v2: - This commit is new after separate: [PATCH 1/2] lib: uuid: add function to generate UUID version 4 - it introduces small refactor of common lib uuid functions Changes v3: - reword commit message - add UUID_STR_LEN definition in lib/uuid.c - remove unused string pointer from uuid_bin_to_str() Changes v4: - add uuid/guid description Changes v5: - none Changes v6: - none --- disk/part_efi.c | 90 +++++++------------------------------------------------- include/common.h | 3 +- lib/Makefile | 1 + lib/uuid.c | 61 +++++++++++++++++++++++++++++++++----- 4 files changed, 68 insertions(+), 87 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 072a1e1..c48c696 100644 --- a/include/common.h +++ b/include/common.h @@ -822,7 +822,8 @@ void udelay (unsigned long); void mdelay(unsigned long); /* lib/uuid.c */ -void uuid_str_to_bin(const char *uuid, unsigned char *out); +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); /* lib/vsprintf.c */ diff --git a/lib/Makefile b/lib/Makefile index ae80865..d7ff7ca 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -61,6 +61,7 @@ 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-y += vsprintf.o obj-$(CONFIG_LIB_RAND) += rand.o diff --git a/lib/uuid.c b/lib/uuid.c index c48bf38..3af3a7d 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -5,18 +5,40 @@ */ #include -#include "common.h" +#include +#include + +#define UUID_STR_LEN 36 /* - * This is what a UUID string looks like. + * 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. + * + * UUID binary format (16 bytes): + * + * 4B-2B-2B-2B-6B (big endian - network byte order) + * + * UUID string is 36 length of characters (36 bytes): + * + * 0 9 14 19 24 + * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + * be be be be be + * + * where x is a hexadecimal character. Fields are separated by '-'s. + * When converting to a binary UUID, le means the field should be converted + * to little endian and be means it should be converted to big endian. * - * x is a hexadecimal character. fields are separated by '-'s. When converting - * to a binary UUID, le means the field should be converted to little endian, - * and be means it should be converted to big endian. + * UUID is also used as GUID (Globally Unique Identifier) with the same binary + * format but it differs in string format like below. * + * GUID: * 0 9 14 19 24 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx * le le le be be + * + * GUID is used e.g. in GPT (GUID Partition Table) as a partiions unique id. */ int uuid_str_valid(const char *uuid) @@ -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 (strlen(uuid) != UUID_STR_LEN) + return -EINVAL; tmp32 = cpu_to_le32(simple_strtoul(uuid, NULL, 16)); memcpy(out, &tmp32, 4); @@ -66,4 +91,26 @@ 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; +} + +void 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}; + 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; + } + } }