[PATCH v6 02/10] lib: uuid: introduce be_uuid_str_to_le_bin function

Ilias Apalodimas ilias.apalodimas at linaro.org
Mon Oct 24 14:07:35 CEST 2022


Hi Abdellatif, 

On Thu, Oct 13, 2022 at 11:38:49AM +0100, Abdellatif El Khlifi wrote:
> convert big endian UUID string to little endian buffer

You don't really need the be_ suffix on the declaration, endianness doesn't
apply to strings.  Can't we do something similar to uuid_str_to_bin() and
just convert to LE?

Thanks
/Ilias
> 
> Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhlifi at arm.com>
> Cc: Tom Rini <trini at konsulko.com>
> Cc: Simon Glass <sjg at chromium.org>
> Cc: Ilias Apalodimas <ilias.apalodimas at linaro.org>
> Cc: Jens Wiklander <jens.wiklander at linaro.org>
> 
> ---
> 
> Changelog:
> ===============
> 
> v4:
> 
> * rename ffa_uuid_str_to_bin to be_uuid_str_to_le_bin and put in
>   a standalone commit (the current)
> 
> v3:
> 
> * introduce ffa_uuid_str_to_bin (provided by
>   arm_ffa: introduce Arm FF-A low-level driver)
> 
> include/uuid.h |  8 +++++++
>  lib/uuid.c     | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 72 insertions(+)
> 
> diff --git a/include/uuid.h b/include/uuid.h
> index 4a4883d3b5..ad3af350f9 100644
> --- a/include/uuid.h
> +++ b/include/uuid.h
> @@ -2,6 +2,8 @@
>  /*
>   * Copyright (C) 2014 Samsung Electronics
>   * Przemyslaw Marczak <p.marczak at samsung.com>
> + * (C) Copyright 2022 ARM Limited
> + * Abdellatif El Khlifi <abdellatif.elkhlifi at arm.com>
>   */
>  #ifndef __UUID_H__
>  #define __UUID_H__
> @@ -44,4 +46,10 @@ int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin);
>  const char *uuid_guid_get_str(const unsigned char *guid_bin);
>  void gen_rand_uuid(unsigned char *uuid_bin);
>  void gen_rand_uuid_str(char *uuid_str, int str_format);
> +
> +/**
> + * be_uuid_str_to_le_bin - Converts a big endian UUID string to a little endian buffer
> + */
> +int be_uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin);
> +
>  #endif
> diff --git a/lib/uuid.c b/lib/uuid.c
> index 465e1ac38f..15a9ab49d5 100644
> --- a/lib/uuid.c
> +++ b/lib/uuid.c
> @@ -1,6 +1,8 @@
>  // SPDX-License-Identifier: GPL-2.0+
>  /*
>   * Copyright 2011 Calxeda, Inc.
> + * (C) Copyright 2022 ARM Limited
> + * Abdellatif El Khlifi <abdellatif.elkhlifi at arm.com>
>   */
>  
>  #include <common.h>
> @@ -346,6 +348,68 @@ int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin,
>  	return 0;
>  }
>  
> +/**
> + * be_uuid_str_to_le_bin - Converts a big endian UUID string to a little endian buffer
> + * @uuid_str:	UUID string in big endian format (36 bytes wide + '/0')
> + * @uuid_bin:	preallocated 16 bytes UUID buffer in little endian format
> + *
> + * UUID string is 36 characters (36 bytes):
> + *
> + * 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, these endianness rules apply:
> + *     be: means the field in the string is considered a big endian hex number
> + *	   and should be converted to little endian binary format
> + *
> + * Return:
> + *
> + *    uuid_bin filled with little endian UUID data
> + *    On success 0 is returned. Otherwise, failure code.
> + */
> +int be_uuid_str_to_le_bin(const char *uuid_str, unsigned char *uuid_bin)
> +{
> +	u16 tmp16 = 0;
> +	u32 tmp32 = 0;
> +	u64 tmp64 = 0;
> +
> +	if (!uuid_str_valid(uuid_str) || !uuid_bin)
> +		return -EINVAL;
> +
> +	/*
> +	 * reverse bytes from big to little endian
> +	 */
> +	tmp32 = simple_strtoul(uuid_str, NULL, 16);
> +	memcpy(uuid_bin, &tmp32, 4);
> +
> +	/*
> +	 * reverse bytes from big to little endian
> +	 */
> +	tmp16 = simple_strtoul(uuid_str + 9, NULL, 16);
> +	memcpy(uuid_bin + 4, &tmp16, 2);
> +
> +	/*
> +	 * reverse bytes from big to little endian
> +	 */
> +	tmp16 = simple_strtoul(uuid_str + 14, NULL, 16);
> +	memcpy(uuid_bin + 6, &tmp16, 2);
> +
> +	/*
> +	 * reverse bytes from big to little endian
> +	 */
> +	tmp16 = simple_strtoul(uuid_str + 19, NULL, 16);
> +	memcpy(uuid_bin + 8, &tmp16, 2);
> +
> +	/*
> +	 * reverse bytes from big to little endian
> +	 */
> +	tmp64 = simple_strtoull(uuid_str + 24, NULL, 16);
> +	memcpy(uuid_bin + 10, (char *)&tmp64, 6);
> +
> +	return 0;
> +}
> +
>  /*
>   * uuid_bin_to_str() - convert big endian binary data to string UUID or GUID.
>   *
> -- 
> 2.17.1
> 


More information about the U-Boot mailing list