Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 1 | From b226e6000de0b1f55d56c1193d1fde028d64abd3 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 02/25] lib: uuid: introduce uuid_str_to_le_bin function |
| 5 | |
| 6 | convert UUID string to little endian binary data |
| 7 | |
| 8 | Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 9 | Cc: Tom Rini <trini@konsulko.com> |
| 10 | Cc: Simon Glass <sjg@chromium.org> |
| 11 | Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org> |
| 12 | Cc: Jens Wiklander <jens.wiklander@linaro.org> |
| 13 | Upstream-Status: Submitted [cover letter: https://lore.kernel.org/all/20221122131751.22747-1-abdellatif.elkhlifi@arm.com/] |
| 14 | |
| 15 | Changelog: |
| 16 | =============== |
| 17 | |
| 18 | v8: |
| 19 | |
| 20 | * use simple_strtoull() in uuid_str_to_le_bin() to support 32-bit platforms |
| 21 | |
| 22 | v7: |
| 23 | |
| 24 | * rename be_uuid_str_to_le_bin() to uuid_str_to_le_bin() |
| 25 | * make uuid_str_to_le_bin() implementation similar to uuid_str_to_bin() |
| 26 | by using same APIs |
| 27 | |
| 28 | v4: |
| 29 | |
| 30 | * rename ffa_uuid_str_to_bin to be_uuid_str_to_le_bin and put in |
| 31 | a standalone commit (the current) |
| 32 | |
| 33 | v3: |
| 34 | |
| 35 | * introduce ffa_uuid_str_to_bin (provided by |
| 36 | arm_ffa: introduce Arm FF-A low-level driver) |
| 37 | --- |
| 38 | include/uuid.h | 8 ++++++++ |
| 39 | lib/uuid.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ |
| 40 | 2 files changed, 54 insertions(+) |
| 41 | |
| 42 | diff --git a/include/uuid.h b/include/uuid.h |
| 43 | index 4a4883d3b5..293a8eb0a5 100644 |
| 44 | --- a/include/uuid.h |
| 45 | +++ b/include/uuid.h |
| 46 | @@ -2,6 +2,8 @@ |
| 47 | /* |
| 48 | * Copyright (C) 2014 Samsung Electronics |
| 49 | * Przemyslaw Marczak <p.marczak@samsung.com> |
| 50 | + * (C) Copyright 2022 ARM Limited |
| 51 | + * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 52 | */ |
| 53 | #ifndef __UUID_H__ |
| 54 | #define __UUID_H__ |
| 55 | @@ -44,4 +46,10 @@ int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin); |
| 56 | const char *uuid_guid_get_str(const unsigned char *guid_bin); |
| 57 | void gen_rand_uuid(unsigned char *uuid_bin); |
| 58 | void gen_rand_uuid_str(char *uuid_str, int str_format); |
| 59 | + |
| 60 | +/** |
| 61 | + * uuid_str_to_le_bin - Converts a UUID string to little endian binary data |
| 62 | + */ |
| 63 | +int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin); |
| 64 | + |
| 65 | #endif |
| 66 | diff --git a/lib/uuid.c b/lib/uuid.c |
| 67 | index 465e1ac38f..d29f561a70 100644 |
| 68 | --- a/lib/uuid.c |
| 69 | +++ b/lib/uuid.c |
| 70 | @@ -1,6 +1,8 @@ |
| 71 | // SPDX-License-Identifier: GPL-2.0+ |
| 72 | /* |
| 73 | * Copyright 2011 Calxeda, Inc. |
| 74 | + * (C) Copyright 2022 ARM Limited |
| 75 | + * Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com> |
| 76 | */ |
| 77 | |
| 78 | #include <common.h> |
| 79 | @@ -346,6 +348,50 @@ int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | +/** |
| 84 | + * uuid_str_to_le_bin() - Convert string UUID to little endian binary data. |
| 85 | + * @uuid_str: pointer to UUID string |
| 86 | + * @uuid_bin: pointer to allocated array for little endian output [16B] |
| 87 | + * |
| 88 | + * UUID string is 36 characters (36 bytes): |
| 89 | + * |
| 90 | + * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 91 | + * |
| 92 | + * where x is a hexadecimal character. Fields are separated by '-'s. |
| 93 | + * When converting to a little endian binary UUID, the string fields are reversed. |
| 94 | + * |
| 95 | + * Return: |
| 96 | + * |
| 97 | + * uuid_bin filled with little endian UUID data |
| 98 | + * On success 0 is returned. Otherwise, failure code. |
| 99 | + */ |
| 100 | +int uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin) |
| 101 | +{ |
| 102 | + u16 tmp16; |
| 103 | + u32 tmp32; |
| 104 | + u64 tmp64; |
| 105 | + |
| 106 | + if (!uuid_str_valid(uuid_str) || !uuid_bin) |
| 107 | + return -EINVAL; |
| 108 | + |
| 109 | + tmp32 = cpu_to_le32(hextoul(uuid_str, NULL)); |
| 110 | + memcpy(uuid_bin, &tmp32, 4); |
| 111 | + |
| 112 | + tmp16 = cpu_to_le16(hextoul(uuid_str + 9, NULL)); |
| 113 | + memcpy(uuid_bin + 4, &tmp16, 2); |
| 114 | + |
| 115 | + tmp16 = cpu_to_le16(hextoul(uuid_str + 14, NULL)); |
| 116 | + memcpy(uuid_bin + 6, &tmp16, 2); |
| 117 | + |
| 118 | + tmp16 = cpu_to_le16(hextoul(uuid_str + 19, NULL)); |
| 119 | + memcpy(uuid_bin + 8, &tmp16, 2); |
| 120 | + |
| 121 | + tmp64 = cpu_to_le64(simple_strtoull(uuid_str + 24, NULL, 16)); |
| 122 | + memcpy(uuid_bin + 10, &tmp64, 6); |
| 123 | + |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | /* |
| 128 | * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID. |
| 129 | * |
| 130 | -- |
| 131 | 2.17.1 |
| 132 | |