SecureBootConfigMisc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /** @file
  2. Helper functions for SecureBoot configuration module.
  3. Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "SecureBootConfigImpl.h"
  7. /**
  8. Read file content into BufferPtr, the size of the allocate buffer
  9. is *FileSize plus AdditionAllocateSize.
  10. @param[in] FileHandle The file to be read.
  11. @param[in, out] BufferPtr Pointers to the pointer of allocated buffer.
  12. @param[out] FileSize Size of input file
  13. @param[in] AdditionAllocateSize Addition size the buffer need to be allocated.
  14. In case the buffer need to contain others besides the file content.
  15. @retval EFI_SUCCESS The file was read into the buffer.
  16. @retval EFI_INVALID_PARAMETER A parameter was invalid.
  17. @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
  18. @retval others Unexpected error.
  19. **/
  20. EFI_STATUS
  21. ReadFileContent (
  22. IN EFI_FILE_HANDLE FileHandle,
  23. IN OUT VOID **BufferPtr,
  24. OUT UINTN *FileSize,
  25. IN UINTN AdditionAllocateSize
  26. )
  27. {
  28. UINTN BufferSize;
  29. UINT64 SourceFileSize;
  30. VOID *Buffer;
  31. EFI_STATUS Status;
  32. if ((FileHandle == NULL) || (FileSize == NULL)) {
  33. return EFI_INVALID_PARAMETER;
  34. }
  35. Buffer = NULL;
  36. //
  37. // Get the file size
  38. //
  39. Status = FileHandle->SetPosition (FileHandle, (UINT64) -1);
  40. if (EFI_ERROR (Status)) {
  41. goto ON_EXIT;
  42. }
  43. Status = FileHandle->GetPosition (FileHandle, &SourceFileSize);
  44. if (EFI_ERROR (Status)) {
  45. goto ON_EXIT;
  46. }
  47. Status = FileHandle->SetPosition (FileHandle, 0);
  48. if (EFI_ERROR (Status)) {
  49. goto ON_EXIT;
  50. }
  51. BufferSize = (UINTN) SourceFileSize + AdditionAllocateSize;
  52. Buffer = AllocateZeroPool(BufferSize);
  53. if (Buffer == NULL) {
  54. return EFI_OUT_OF_RESOURCES;
  55. }
  56. BufferSize = (UINTN) SourceFileSize;
  57. *FileSize = BufferSize;
  58. Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);
  59. if (EFI_ERROR (Status) || BufferSize != *FileSize) {
  60. FreePool (Buffer);
  61. Buffer = NULL;
  62. Status = EFI_BAD_BUFFER_SIZE;
  63. goto ON_EXIT;
  64. }
  65. ON_EXIT:
  66. *BufferPtr = Buffer;
  67. return Status;
  68. }
  69. /**
  70. Close an open file handle.
  71. @param[in] FileHandle The file handle to close.
  72. **/
  73. VOID
  74. CloseFile (
  75. IN EFI_FILE_HANDLE FileHandle
  76. )
  77. {
  78. if (FileHandle != NULL) {
  79. FileHandle->Close (FileHandle);
  80. }
  81. }
  82. /**
  83. Convert a nonnegative integer to an octet string of a specified length.
  84. @param[in] Integer Pointer to the nonnegative integer to be converted
  85. @param[in] IntSizeInWords Length of integer buffer in words
  86. @param[out] OctetString Converted octet string of the specified length
  87. @param[in] OSSizeInBytes Intended length of resulting octet string in bytes
  88. Returns:
  89. @retval EFI_SUCCESS Data conversion successfully
  90. @retval EFI_BUFFER_TOOL_SMALL Buffer is too small for output string
  91. **/
  92. EFI_STATUS
  93. EFIAPI
  94. Int2OctStr (
  95. IN CONST UINTN *Integer,
  96. IN UINTN IntSizeInWords,
  97. OUT UINT8 *OctetString,
  98. IN UINTN OSSizeInBytes
  99. )
  100. {
  101. CONST UINT8 *Ptr1;
  102. UINT8 *Ptr2;
  103. for (Ptr1 = (CONST UINT8 *)Integer, Ptr2 = OctetString + OSSizeInBytes - 1;
  104. Ptr1 < (UINT8 *)(Integer + IntSizeInWords) && Ptr2 >= OctetString;
  105. Ptr1++, Ptr2--) {
  106. *Ptr2 = *Ptr1;
  107. }
  108. for (; Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords) && *Ptr1 == 0; Ptr1++);
  109. if (Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords)) {
  110. return EFI_BUFFER_TOO_SMALL;
  111. }
  112. if (Ptr2 >= OctetString) {
  113. ZeroMem (OctetString, Ptr2 - OctetString + 1);
  114. }
  115. return EFI_SUCCESS;
  116. }
  117. /**
  118. Worker function that prints an EFI_GUID into specified Buffer.
  119. @param[in] Guid Pointer to GUID to print.
  120. @param[in] Buffer Buffer to print Guid into.
  121. @param[in] BufferSize Size of Buffer.
  122. @retval Number of characters printed.
  123. **/
  124. UINTN
  125. GuidToString (
  126. IN EFI_GUID *Guid,
  127. IN CHAR16 *Buffer,
  128. IN UINTN BufferSize
  129. )
  130. {
  131. UINTN Size;
  132. Size = UnicodeSPrint (
  133. Buffer,
  134. BufferSize,
  135. L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  136. (UINTN)Guid->Data1,
  137. (UINTN)Guid->Data2,
  138. (UINTN)Guid->Data3,
  139. (UINTN)Guid->Data4[0],
  140. (UINTN)Guid->Data4[1],
  141. (UINTN)Guid->Data4[2],
  142. (UINTN)Guid->Data4[3],
  143. (UINTN)Guid->Data4[4],
  144. (UINTN)Guid->Data4[5],
  145. (UINTN)Guid->Data4[6],
  146. (UINTN)Guid->Data4[7]
  147. );
  148. //
  149. // SPrint will null terminate the string. The -1 skips the null
  150. //
  151. return Size - 1;
  152. }