Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 1 | From af17d357674393565c8be15f21c86cba972963e7 Mon Sep 17 00:00:00 2001 |
| 2 | From: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 3 | Date: Thu, 4 Aug 2022 16:46:47 +0100 |
| 4 | Subject: [PATCH 08/26] lib: uuid: introduce be_uuid_str_to_le_bin function |
| 5 | |
| 6 | convert big endian UUID string to little endian buffer |
| 7 | |
| 8 | Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 9 | Upstream-Status: Submitted [cover letter: https://lore.kernel.org/all/20220926101723.9965-1-abdellatif.elkhlifi@arm.com/] |
| 10 | --- |
| 11 | |
| 12 | Changelog: |
| 13 | =============== |
| 14 | |
| 15 | v4: |
| 16 | |
| 17 | * rename ffa_uuid_str_to_bin to be_uuid_str_to_le_bin and put in |
| 18 | a standalone commit (the current) |
| 19 | |
| 20 | v3: |
| 21 | |
| 22 | * introduce ffa_uuid_str_to_bin (provided by |
| 23 | arm_ffa: introduce Arm FF-A low-level driver) |
| 24 | |
| 25 | include/uuid.h | 6 +++++ |
| 26 | lib/uuid.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 27 | 2 files changed, 69 insertions(+) |
| 28 | |
| 29 | diff --git a/include/uuid.h b/include/uuid.h |
| 30 | index 4a4883d3b5..5355230b5e 100644 |
| 31 | --- a/include/uuid.h |
| 32 | +++ b/include/uuid.h |
| 33 | @@ -44,4 +44,10 @@ int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin); |
| 34 | const char *uuid_guid_get_str(const unsigned char *guid_bin); |
| 35 | void gen_rand_uuid(unsigned char *uuid_bin); |
| 36 | void gen_rand_uuid_str(char *uuid_str, int str_format); |
| 37 | + |
| 38 | +/** |
| 39 | + * be_uuid_str_to_le_bin - Converts a big endian UUID string to a little endian buffer |
| 40 | + */ |
| 41 | +int be_uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin); |
| 42 | + |
| 43 | #endif |
| 44 | diff --git a/lib/uuid.c b/lib/uuid.c |
| 45 | index 284f8113ff..d0fa51d0bf 100644 |
| 46 | --- a/lib/uuid.c |
| 47 | +++ b/lib/uuid.c |
| 48 | @@ -1,6 +1,7 @@ |
| 49 | // SPDX-License-Identifier: GPL-2.0+ |
| 50 | /* |
| 51 | * Copyright 2011 Calxeda, Inc. |
| 52 | + * Copyright 2022 ARM Limited |
| 53 | */ |
| 54 | |
| 55 | #include <common.h> |
| 56 | @@ -342,6 +343,68 @@ int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | +/** |
| 61 | + * be_uuid_str_to_le_bin - Converts a big endian UUID string to a little endian buffer |
| 62 | + * @uuid_str: UUID string in big endian format (36 bytes wide + '/0') |
| 63 | + * @uuid_bin: preallocated 16 bytes UUID buffer in little endian format |
| 64 | + * |
| 65 | + * UUID string is 36 characters (36 bytes): |
| 66 | + * |
| 67 | + * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 68 | + * be be be be be |
| 69 | + * |
| 70 | + * where x is a hexadecimal character. Fields are separated by '-'s. |
| 71 | + * When converting to a binary UUID, these endianness rules apply: |
| 72 | + * be: means the field in the string is considered a big endian hex number |
| 73 | + * and should be converted to little endian binary format |
| 74 | + * |
| 75 | + * Return: |
| 76 | + * |
| 77 | + * uuid_bin filled with little endian UUID data |
| 78 | + * On success 0 is returned. Otherwise, failure code. |
| 79 | + */ |
| 80 | +int be_uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin) |
| 81 | +{ |
| 82 | + u16 tmp16 = 0; |
| 83 | + u32 tmp32 = 0; |
| 84 | + u64 tmp64 = 0; |
| 85 | + |
| 86 | + if (!uuid_str_valid(uuid_str) || !uuid_bin) |
| 87 | + return -EINVAL; |
| 88 | + |
| 89 | + /* |
| 90 | + * reverse bytes from big to little endian |
| 91 | + */ |
| 92 | + tmp32 = simple_strtoul(uuid_str, NULL, 16); |
| 93 | + memcpy(uuid_bin, &tmp32, 4); |
| 94 | + |
| 95 | + /* |
| 96 | + * reverse bytes from big to little endian |
| 97 | + */ |
| 98 | + tmp16 = simple_strtoul(uuid_str + 9, NULL, 16); |
| 99 | + memcpy(uuid_bin + 4, &tmp16, 2); |
| 100 | + |
| 101 | + /* |
| 102 | + * reverse bytes from big to little endian |
| 103 | + */ |
| 104 | + tmp16 = simple_strtoul(uuid_str + 14, NULL, 16); |
| 105 | + memcpy(uuid_bin + 6, &tmp16, 2); |
| 106 | + |
| 107 | + /* |
| 108 | + * reverse bytes from big to little endian |
| 109 | + */ |
| 110 | + tmp16 = simple_strtoul(uuid_str + 19, NULL, 16); |
| 111 | + memcpy(uuid_bin + 8, &tmp16, 2); |
| 112 | + |
| 113 | + /* |
| 114 | + * reverse bytes from big to little endian |
| 115 | + */ |
| 116 | + tmp64 = simple_strtoull(uuid_str + 24, NULL, 16); |
| 117 | + memcpy(uuid_bin + 10, (char *)&tmp64, 6); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | /* |
| 123 | * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID. |
| 124 | * |
| 125 | -- |
| 126 | 2.17.1 |
| 127 | |