[edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib

Ruiyu Ni posted 1 patch 5 years, 11 months ago
Failed in applying to current master (apply log)
MdePkg/Include/Library/BaseLib.h          | 18 ++++++++-
MdePkg/Library/BaseLib/BaseLibInternals.h |  3 +-
MdePkg/Library/BaseLib/GenerateGuid.c     | 64 +++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+), 2 deletions(-)
create mode 100644 MdePkg/Library/BaseLib/GenerateGuid.c
[edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib
Posted by Ruiyu Ni 5 years, 11 months ago
Per RFC4122, there are five versions of UUID (GUID).
The version 4 only depends on truly random or pseudo-random number
generation.
So GenerateGuid () can be added to BaseLib. It uses the
GetRandomNumber128() services exposed from MdePkg/RngLib.
This API can be used by some EFI utilities which needs the guidgen
services, e.g.: utility that partitions the disk in GPT format needs
to fill the generated GUID in partition table.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
---
 MdePkg/Include/Library/BaseLib.h          | 18 ++++++++-
 MdePkg/Library/BaseLib/BaseLibInternals.h |  3 +-
 MdePkg/Library/BaseLib/GenerateGuid.c     | 64 +++++++++++++++++++++++++++++++
 3 files changed, 83 insertions(+), 2 deletions(-)
 create mode 100644 MdePkg/Library/BaseLib/GenerateGuid.c

diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/BaseLib.h
index eb2899f852..272596d64c 100644
--- a/MdePkg/Include/Library/BaseLib.h
+++ b/MdePkg/Include/Library/BaseLib.h
@@ -2,7 +2,7 @@
   Provides string functions, linked list functions, math functions, synchronization
   functions, file path functions, and CPU architecture-specific functions.
 
-Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
@@ -5104,6 +5104,22 @@ EFIAPI
 CpuDeadLoop (
   VOID
   );
+
+/**
+  Generate a raw 128-bit (16-byte) GUID.
+
+  If Guid is NULL, then ASSERT().
+
+  @param Guid   Receive the 128-bit (16-byte) GUID.
+
+  @retval TRUE  The GUID is generated successfully.
+  @retval FALSE The GUID is not generated.
+**/
+BOOLEAN
+EFIAPI
+GenerateGuid (
+  OUT     GUID                      *Guid
+  );
  
 #if defined (MDE_CPU_IPF)
 
diff --git a/MdePkg/Library/BaseLib/BaseLibInternals.h b/MdePkg/Library/BaseLib/BaseLibInternals.h
index 9dca97a0dc..427eb44eba 100644
--- a/MdePkg/Library/BaseLib/BaseLibInternals.h
+++ b/MdePkg/Library/BaseLib/BaseLibInternals.h
@@ -1,7 +1,7 @@
 /** @file
   Declaration of internal functions in BaseLib.
 
-  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -20,6 +20,7 @@
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
 #include <Library/PcdLib.h>
+#include <Library/RngLib.h>
 
 //
 // Math functions
diff --git a/MdePkg/Library/BaseLib/GenerateGuid.c b/MdePkg/Library/BaseLib/GenerateGuid.c
new file mode 100644
index 0000000000..b90f7c5a83
--- /dev/null
+++ b/MdePkg/Library/BaseLib/GenerateGuid.c
@@ -0,0 +1,64 @@
+/** @file
+  Generate GUID implementation.
+
+  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php.
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include "BaseLibInternals.h"
+
+/**
+  Generate a raw 128-bit (16-byte) GUID.
+
+  If Guid is NULL, then ASSERT().
+
+  @param Guid   Receive the generated 128-bit (16-byte) GUID.
+
+  @retval TRUE  The GUID is generated successfully.
+  @retval FALSE The GUID is not generated.
+**/
+BOOLEAN
+EFIAPI
+GenerateGuid (
+  OUT     GUID                      *Guid
+  )
+{
+  ASSERT (Guid != NULL);
+
+  //
+  // A GUID is encoded as a 128-bit object as follows:
+  //   0                   1                   2                   3
+  //   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+  //   |                          time_low                             |
+  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+  //   |       time_mid                |         time_hi_and_version   |
+  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+  //   |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
+  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+  //   |                         node (2-5)                            |
+  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+  // The below algorithm generates version 4 GUID from truly-random or pseudo-random numbers.
+  // The algorithm is as follows (per RFC 4122):
+  // > Set all the bits to randomly (or pseudo-randomly) values.
+  // > Set the two most significant bits (bits 6 and 7) of clk_seq_hi_res field to zero and one, respectively.
+  // > Set the four most significant bits (bits 12 through 15) of time_hi_and_version field to 4 (4-bit version number).
+  //
+  if (!GetRandomNumber128 ((UINT64 *)Guid)) {
+    return FALSE;
+  }
+
+  //
+  // Version 4 (Random GUID)
+  //
+  Guid->Data4[0] = BitFieldWrite8  (Guid->Data4[0], 6,  7,  0b10);
+  Guid->Data3    = BitFieldWrite16 (Guid->Data3,    12, 15, 4);
+}
+
-- 
2.16.1.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib
Posted by Ni, Ruiyu 5 years, 11 months ago
Sorry, I forgot to include Long Qin.

Thanks/Ray

> -----Original Message-----
> From: edk2-devel <edk2-devel-bounces@lists.01.org> On Behalf Of Ruiyu Ni
> Sent: Wednesday, May 23, 2018 1:34 PM
> To: edk2-devel@lists.01.org
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> <liming.gao@intel.com>
> Subject: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib
> 
> Per RFC4122, there are five versions of UUID (GUID).
> The version 4 only depends on truly random or pseudo-random number
> generation.
> So GenerateGuid () can be added to BaseLib. It uses the
> GetRandomNumber128() services exposed from MdePkg/RngLib.
> This API can be used by some EFI utilities which needs the guidgen services,
> e.g.: utility that partitions the disk in GPT format needs to fill the generated
> GUID in partition table.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Qin Long <qin.long@intel.com>
> ---
>  MdePkg/Include/Library/BaseLib.h          | 18 ++++++++-
>  MdePkg/Library/BaseLib/BaseLibInternals.h |  3 +-
>  MdePkg/Library/BaseLib/GenerateGuid.c     | 64
> +++++++++++++++++++++++++++++++
>  3 files changed, 83 insertions(+), 2 deletions(-)  create mode 100644
> MdePkg/Library/BaseLib/GenerateGuid.c
> 
> diff --git a/MdePkg/Include/Library/BaseLib.h
> b/MdePkg/Include/Library/BaseLib.h
> index eb2899f852..272596d64c 100644
> --- a/MdePkg/Include/Library/BaseLib.h
> +++ b/MdePkg/Include/Library/BaseLib.h
> @@ -2,7 +2,7 @@
>    Provides string functions, linked list functions, math functions,
> synchronization
>    functions, file path functions, and CPU architecture-specific functions.
> 
> -Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
>  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>  This
> program and the accompanying materials  are licensed and made available
> under the terms and conditions of the BSD License @@ -5104,6 +5104,22 @@
> EFIAPI  CpuDeadLoop (
>    VOID
>    );
> +
> +/**
> +  Generate a raw 128-bit (16-byte) GUID.
> +
> +  If Guid is NULL, then ASSERT().
> +
> +  @param Guid   Receive the 128-bit (16-byte) GUID.
> +
> +  @retval TRUE  The GUID is generated successfully.
> +  @retval FALSE The GUID is not generated.
> +**/
> +BOOLEAN
> +EFIAPI
> +GenerateGuid (
> +  OUT     GUID                      *Guid
> +  );
> 
>  #if defined (MDE_CPU_IPF)
> 
> diff --git a/MdePkg/Library/BaseLib/BaseLibInternals.h
> b/MdePkg/Library/BaseLib/BaseLibInternals.h
> index 9dca97a0dc..427eb44eba 100644
> --- a/MdePkg/Library/BaseLib/BaseLibInternals.h
> +++ b/MdePkg/Library/BaseLib/BaseLibInternals.h
> @@ -1,7 +1,7 @@
>  /** @file
>    Declaration of internal functions in BaseLib.
> 
> -  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2006 - 2018, Intel Corporation. All rights
> + reserved.<BR>
>    This program and the accompanying materials
>    are licensed and made available under the terms and conditions of the BSD
> License
>    which accompanies this distribution.  The full text of the license may be
> found at @@ -20,6 +20,7 @@  #include <Library/BaseMemoryLib.h>
> #include <Library/DebugLib.h>  #include <Library/PcdLib.h>
> +#include <Library/RngLib.h>
> 
>  //
>  // Math functions
> diff --git a/MdePkg/Library/BaseLib/GenerateGuid.c
> b/MdePkg/Library/BaseLib/GenerateGuid.c
> new file mode 100644
> index 0000000000..b90f7c5a83
> --- /dev/null
> +++ b/MdePkg/Library/BaseLib/GenerateGuid.c
> @@ -0,0 +1,64 @@
> +/** @file
> +  Generate GUID implementation.
> +
> +  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>  This
> + program and the accompanying materials  are licensed and made
> + available under the terms and conditions of the BSD License  which
> + accompanies this distribution.  The full text of the license may be
> + found at  http://opensource.org/licenses/bsd-license.php.
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "BaseLibInternals.h"
> +
> +/**
> +  Generate a raw 128-bit (16-byte) GUID.
> +
> +  If Guid is NULL, then ASSERT().
> +
> +  @param Guid   Receive the generated 128-bit (16-byte) GUID.
> +
> +  @retval TRUE  The GUID is generated successfully.
> +  @retval FALSE The GUID is not generated.
> +**/
> +BOOLEAN
> +EFIAPI
> +GenerateGuid (
> +  OUT     GUID                      *Guid
> +  )
> +{
> +  ASSERT (Guid != NULL);
> +
> +  //
> +  // A GUID is encoded as a 128-bit object as follows:
> +  //   0                   1                   2                   3
> +  //   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  //   |                          time_low                             |
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  //   |       time_mid                |         time_hi_and_version   |
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  //   |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  //   |                         node (2-5)                            |
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  // The below algorithm generates version 4 GUID from truly-random or
> pseudo-random numbers.
> +  // The algorithm is as follows (per RFC 4122):
> +  // > Set all the bits to randomly (or pseudo-randomly) values.
> +  // > Set the two most significant bits (bits 6 and 7) of clk_seq_hi_res field
> to zero and one, respectively.
> +  // > Set the four most significant bits (bits 12 through 15) of
> time_hi_and_version field to 4 (4-bit version number).
> +  //
> +  if (!GetRandomNumber128 ((UINT64 *)Guid)) {
> +    return FALSE;
> +  }
> +
> +  //
> +  // Version 4 (Random GUID)
> +  //
> +  Guid->Data4[0] = BitFieldWrite8  (Guid->Data4[0], 6,  7,  0b10);
> +  Guid->Data3    = BitFieldWrite16 (Guid->Data3,    12, 15, 4);
> +}
> +
> --
> 2.16.1.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib
Posted by Zeng, Star 5 years, 11 months ago
Ray,

Two points need be noticed.
1. BaseLib.inf forgets to include RngLib.
2. This patch will include RngLib dependency to BaseLib, that may break many platforms that do not declare RngLib in their platform dsc.


Thanks,
Star
-----Original Message-----
From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ni, Ruiyu
Sent: Wednesday, May 23, 2018 1:58 PM
To: Ni, Ruiyu <ruiyu.ni@intel.com>; edk2-devel@lists.01.org
Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming <liming.gao@intel.com>; Long, Qin <qin.long@intel.com>
Subject: Re: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib

Sorry, I forgot to include Long Qin.

Thanks/Ray

> -----Original Message-----
> From: edk2-devel <edk2-devel-bounces@lists.01.org> On Behalf Of Ruiyu 
> Ni
> Sent: Wednesday, May 23, 2018 1:34 PM
> To: edk2-devel@lists.01.org
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> <liming.gao@intel.com>
> Subject: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib
> 
> Per RFC4122, there are five versions of UUID (GUID).
> The version 4 only depends on truly random or pseudo-random number 
> generation.
> So GenerateGuid () can be added to BaseLib. It uses the
> GetRandomNumber128() services exposed from MdePkg/RngLib.
> This API can be used by some EFI utilities which needs the guidgen 
> services,
> e.g.: utility that partitions the disk in GPT format needs to fill the 
> generated GUID in partition table.
> 
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Qin Long <qin.long@intel.com>
> ---
>  MdePkg/Include/Library/BaseLib.h          | 18 ++++++++-
>  MdePkg/Library/BaseLib/BaseLibInternals.h |  3 +-
>  MdePkg/Library/BaseLib/GenerateGuid.c     | 64
> +++++++++++++++++++++++++++++++
>  3 files changed, 83 insertions(+), 2 deletions(-)  create mode 100644 
> MdePkg/Library/BaseLib/GenerateGuid.c
> 
> diff --git a/MdePkg/Include/Library/BaseLib.h
> b/MdePkg/Include/Library/BaseLib.h
> index eb2899f852..272596d64c 100644
> --- a/MdePkg/Include/Library/BaseLib.h
> +++ b/MdePkg/Include/Library/BaseLib.h
> @@ -2,7 +2,7 @@
>    Provides string functions, linked list functions, math functions, 
> synchronization
>    functions, file path functions, and CPU architecture-specific functions.
> 
> -Copyright (c) 2006 - 2017, Intel Corporation. All rights 
> reserved.<BR>
> +Copyright (c) 2006 - 2018, Intel Corporation. All rights 
> +reserved.<BR>
>  Portions copyright (c) 2008 - 2009, Apple Inc. All rights 
> reserved.<BR>  This program and the accompanying materials  are 
> licensed and made available under the terms and conditions of the BSD 
> License @@ -5104,6 +5104,22 @@ EFIAPI  CpuDeadLoop (
>    VOID
>    );
> +
> +/**
> +  Generate a raw 128-bit (16-byte) GUID.
> +
> +  If Guid is NULL, then ASSERT().
> +
> +  @param Guid   Receive the 128-bit (16-byte) GUID.
> +
> +  @retval TRUE  The GUID is generated successfully.
> +  @retval FALSE The GUID is not generated.
> +**/
> +BOOLEAN
> +EFIAPI
> +GenerateGuid (
> +  OUT     GUID                      *Guid
> +  );
> 
>  #if defined (MDE_CPU_IPF)
> 
> diff --git a/MdePkg/Library/BaseLib/BaseLibInternals.h
> b/MdePkg/Library/BaseLib/BaseLibInternals.h
> index 9dca97a0dc..427eb44eba 100644
> --- a/MdePkg/Library/BaseLib/BaseLibInternals.h
> +++ b/MdePkg/Library/BaseLib/BaseLibInternals.h
> @@ -1,7 +1,7 @@
>  /** @file
>    Declaration of internal functions in BaseLib.
> 
> -  Copyright (c) 2006 - 2017, Intel Corporation. All rights 
> reserved.<BR>
> +  Copyright (c) 2006 - 2018, Intel Corporation. All rights 
> + reserved.<BR>
>    This program and the accompanying materials
>    are licensed and made available under the terms and conditions of 
> the BSD License
>    which accompanies this distribution.  The full text of the license 
> may be found at @@ -20,6 +20,7 @@  #include <Library/BaseMemoryLib.h> 
> #include <Library/DebugLib.h>  #include <Library/PcdLib.h>
> +#include <Library/RngLib.h>
> 
>  //
>  // Math functions
> diff --git a/MdePkg/Library/BaseLib/GenerateGuid.c
> b/MdePkg/Library/BaseLib/GenerateGuid.c
> new file mode 100644
> index 0000000000..b90f7c5a83
> --- /dev/null
> +++ b/MdePkg/Library/BaseLib/GenerateGuid.c
> @@ -0,0 +1,64 @@
> +/** @file
> +  Generate GUID implementation.
> +
> +  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>  
> + This program and the accompanying materials  are licensed and made 
> + available under the terms and conditions of the BSD License  which 
> + accompanies this distribution.  The full text of the license may be 
> + found at  http://opensource.org/licenses/bsd-license.php.
> +
> +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> BASIS,
> + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> EXPRESS OR IMPLIED.
> +
> +**/
> +
> +#include "BaseLibInternals.h"
> +
> +/**
> +  Generate a raw 128-bit (16-byte) GUID.
> +
> +  If Guid is NULL, then ASSERT().
> +
> +  @param Guid   Receive the generated 128-bit (16-byte) GUID.
> +
> +  @retval TRUE  The GUID is generated successfully.
> +  @retval FALSE The GUID is not generated.
> +**/
> +BOOLEAN
> +EFIAPI
> +GenerateGuid (
> +  OUT     GUID                      *Guid
> +  )
> +{
> +  ASSERT (Guid != NULL);
> +
> +  //
> +  // A GUID is encoded as a 128-bit object as follows:
> +  //   0                   1                   2                   3
> +  //   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  //   |                          time_low                             |
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  //   |       time_mid                |         time_hi_and_version   |
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  //   |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  //   |                         node (2-5)                            |
> +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> +  // The below algorithm generates version 4 GUID from truly-random 
> + or
> pseudo-random numbers.
> +  // The algorithm is as follows (per RFC 4122):
> +  // > Set all the bits to randomly (or pseudo-randomly) values.
> +  // > Set the two most significant bits (bits 6 and 7) of 
> + clk_seq_hi_res field
> to zero and one, respectively.
> +  // > Set the four most significant bits (bits 12 through 15) of
> time_hi_and_version field to 4 (4-bit version number).
> +  //
> +  if (!GetRandomNumber128 ((UINT64 *)Guid)) {
> +    return FALSE;
> +  }
> +
> +  //
> +  // Version 4 (Random GUID)
> +  //
> +  Guid->Data4[0] = BitFieldWrite8  (Guid->Data4[0], 6,  7,  0b10);
> +  Guid->Data3    = BitFieldWrite16 (Guid->Data3,    12, 15, 4);
> +}
> +
> --
> 2.16.1.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib
Posted by Ni, Ruiyu 5 years, 11 months ago
Good points!
I will hold the patch.

Thanks/Ray

> -----Original Message-----
> From: Zeng, Star
> Sent: Wednesday, May 23, 2018 2:11 PM
> To: Ni, Ruiyu <ruiyu.ni@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; edk2-
> devel@lists.01.org
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Long, Qin <qin.long@intel.com>; Zeng, Star
> <star.zeng@intel.com>
> Subject: RE: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to
> BaseLib
> 
> Ray,
> 
> Two points need be noticed.
> 1. BaseLib.inf forgets to include RngLib.
> 2. This patch will include RngLib dependency to BaseLib, that may break many
> platforms that do not declare RngLib in their platform dsc.
> 
> 
> Thanks,
> Star
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ni,
> Ruiyu
> Sent: Wednesday, May 23, 2018 1:58 PM
> To: Ni, Ruiyu <ruiyu.ni@intel.com>; edk2-devel@lists.01.org
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> <liming.gao@intel.com>; Long, Qin <qin.long@intel.com>
> Subject: Re: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to
> BaseLib
> 
> Sorry, I forgot to include Long Qin.
> 
> Thanks/Ray
> 
> > -----Original Message-----
> > From: edk2-devel <edk2-devel-bounces@lists.01.org> On Behalf Of Ruiyu
> > Ni
> > Sent: Wednesday, May 23, 2018 1:34 PM
> > To: edk2-devel@lists.01.org
> > Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> > <liming.gao@intel.com>
> > Subject: [edk2] [PATCH] MdePkg/BaseLib: Add GenerateGuid() to BaseLib
> >
> > Per RFC4122, there are five versions of UUID (GUID).
> > The version 4 only depends on truly random or pseudo-random number
> > generation.
> > So GenerateGuid () can be added to BaseLib. It uses the
> > GetRandomNumber128() services exposed from MdePkg/RngLib.
> > This API can be used by some EFI utilities which needs the guidgen
> > services,
> > e.g.: utility that partitions the disk in GPT format needs to fill the
> > generated GUID in partition table.
> >
> > Contributed-under: TianoCore Contribution Agreement 1.1
> > Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
> > Cc: Liming Gao <liming.gao@intel.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Qin Long <qin.long@intel.com>
> > ---
> >  MdePkg/Include/Library/BaseLib.h          | 18 ++++++++-
> >  MdePkg/Library/BaseLib/BaseLibInternals.h |  3 +-
> >  MdePkg/Library/BaseLib/GenerateGuid.c     | 64
> > +++++++++++++++++++++++++++++++
> >  3 files changed, 83 insertions(+), 2 deletions(-)  create mode 100644
> > MdePkg/Library/BaseLib/GenerateGuid.c
> >
> > diff --git a/MdePkg/Include/Library/BaseLib.h
> > b/MdePkg/Include/Library/BaseLib.h
> > index eb2899f852..272596d64c 100644
> > --- a/MdePkg/Include/Library/BaseLib.h
> > +++ b/MdePkg/Include/Library/BaseLib.h
> > @@ -2,7 +2,7 @@
> >    Provides string functions, linked list functions, math functions,
> > synchronization
> >    functions, file path functions, and CPU architecture-specific functions.
> >
> > -Copyright (c) 2006 - 2017, Intel Corporation. All rights
> > reserved.<BR>
> > +Copyright (c) 2006 - 2018, Intel Corporation. All rights
> > +reserved.<BR>
> >  Portions copyright (c) 2008 - 2009, Apple Inc. All rights
> > reserved.<BR>  This program and the accompanying materials  are
> > licensed and made available under the terms and conditions of the BSD
> > License @@ -5104,6 +5104,22 @@ EFIAPI  CpuDeadLoop (
> >    VOID
> >    );
> > +
> > +/**
> > +  Generate a raw 128-bit (16-byte) GUID.
> > +
> > +  If Guid is NULL, then ASSERT().
> > +
> > +  @param Guid   Receive the 128-bit (16-byte) GUID.
> > +
> > +  @retval TRUE  The GUID is generated successfully.
> > +  @retval FALSE The GUID is not generated.
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +GenerateGuid (
> > +  OUT     GUID                      *Guid
> > +  );
> >
> >  #if defined (MDE_CPU_IPF)
> >
> > diff --git a/MdePkg/Library/BaseLib/BaseLibInternals.h
> > b/MdePkg/Library/BaseLib/BaseLibInternals.h
> > index 9dca97a0dc..427eb44eba 100644
> > --- a/MdePkg/Library/BaseLib/BaseLibInternals.h
> > +++ b/MdePkg/Library/BaseLib/BaseLibInternals.h
> > @@ -1,7 +1,7 @@
> >  /** @file
> >    Declaration of internal functions in BaseLib.
> >
> > -  Copyright (c) 2006 - 2017, Intel Corporation. All rights
> > reserved.<BR>
> > +  Copyright (c) 2006 - 2018, Intel Corporation. All rights
> > + reserved.<BR>
> >    This program and the accompanying materials
> >    are licensed and made available under the terms and conditions of
> > the BSD License
> >    which accompanies this distribution.  The full text of the license
> > may be found at @@ -20,6 +20,7 @@  #include <Library/BaseMemoryLib.h>
> > #include <Library/DebugLib.h>  #include <Library/PcdLib.h>
> > +#include <Library/RngLib.h>
> >
> >  //
> >  // Math functions
> > diff --git a/MdePkg/Library/BaseLib/GenerateGuid.c
> > b/MdePkg/Library/BaseLib/GenerateGuid.c
> > new file mode 100644
> > index 0000000000..b90f7c5a83
> > --- /dev/null
> > +++ b/MdePkg/Library/BaseLib/GenerateGuid.c
> > @@ -0,0 +1,64 @@
> > +/** @file
> > +  Generate GUID implementation.
> > +
> > +  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
> > + This program and the accompanying materials  are licensed and made
> > + available under the terms and conditions of the BSD License  which
> > + accompanies this distribution.  The full text of the license may be
> > + found at  http://opensource.org/licenses/bsd-license.php.
> > +
> > +  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
> > BASIS,
> > + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
> > EXPRESS OR IMPLIED.
> > +
> > +**/
> > +
> > +#include "BaseLibInternals.h"
> > +
> > +/**
> > +  Generate a raw 128-bit (16-byte) GUID.
> > +
> > +  If Guid is NULL, then ASSERT().
> > +
> > +  @param Guid   Receive the generated 128-bit (16-byte) GUID.
> > +
> > +  @retval TRUE  The GUID is generated successfully.
> > +  @retval FALSE The GUID is not generated.
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +GenerateGuid (
> > +  OUT     GUID                      *Guid
> > +  )
> > +{
> > +  ASSERT (Guid != NULL);
> > +
> > +  //
> > +  // A GUID is encoded as a 128-bit object as follows:
> > +  //   0                   1                   2                   3
> > +  //   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
> > +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> > +  //   |                          time_low                             |
> > +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> > +  //   |       time_mid                |         time_hi_and_version   |
> > +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> > +  //   |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
> > +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> > +  //   |                         node (2-5)                            |
> > +  //   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> > +  // The below algorithm generates version 4 GUID from truly-random
> > + or
> > pseudo-random numbers.
> > +  // The algorithm is as follows (per RFC 4122):
> > +  // > Set all the bits to randomly (or pseudo-randomly) values.
> > +  // > Set the two most significant bits (bits 6 and 7) of
> > + clk_seq_hi_res field
> > to zero and one, respectively.
> > +  // > Set the four most significant bits (bits 12 through 15) of
> > time_hi_and_version field to 4 (4-bit version number).
> > +  //
> > +  if (!GetRandomNumber128 ((UINT64 *)Guid)) {
> > +    return FALSE;
> > +  }
> > +
> > +  //
> > +  // Version 4 (Random GUID)
> > +  //
> > +  Guid->Data4[0] = BitFieldWrite8  (Guid->Data4[0], 6,  7,  0b10);
> > +  Guid->Data3    = BitFieldWrite16 (Guid->Data3,    12, 15, 4);
> > +}
> > +
> > --
> > 2.16.1.windows.1
> >
> > _______________________________________________
> > edk2-devel mailing list
> > edk2-devel@lists.01.org
> > https://lists.01.org/mailman/listinfo/edk2-devel
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel