ParseConfigProfile.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /** @file
  2. Parse the INI configuration file and pass the information to the recovery driver
  3. so that the driver can perform recovery accordingly.
  4. Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "RecoveryModuleLoadPei.h"
  8. #include <Library/IniParsingLib.h>
  9. #include <Library/PrintLib.h>
  10. #define MAX_LINE_LENGTH 512
  11. /**
  12. Parse Config data file to get the updated data array.
  13. @param[in] DataBuffer Config raw file buffer.
  14. @param[in] BufferSize Size of raw buffer.
  15. @param[in, out] ConfigHeader Pointer to the config header.
  16. @param[in, out] RecoveryArray Pointer to the config of recovery data.
  17. @retval EFI_NOT_FOUND No config data is found.
  18. @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
  19. @retval EFI_SUCCESS Parse the config file successfully.
  20. **/
  21. EFI_STATUS
  22. ParseRecoveryDataFile (
  23. IN UINT8 *DataBuffer,
  24. IN UINTN BufferSize,
  25. IN OUT CONFIG_HEADER *ConfigHeader,
  26. IN OUT RECOVERY_CONFIG_DATA **RecoveryArray
  27. )
  28. {
  29. EFI_STATUS Status;
  30. CHAR8 *SectionName;
  31. CHAR8 Entry[MAX_LINE_LENGTH];
  32. UINTN Num;
  33. UINTN Index;
  34. EFI_GUID FileGuid;
  35. VOID *Context;
  36. //
  37. // First process the data buffer and get all sections and entries
  38. //
  39. Context = OpenIniFile (DataBuffer, BufferSize);
  40. if (Context == NULL) {
  41. return EFI_INVALID_PARAMETER;
  42. }
  43. //
  44. // Now get NumOfUpdate
  45. //
  46. Status = GetDecimalUintnFromDataFile (
  47. Context,
  48. "Head",
  49. "NumOfRecovery",
  50. &Num
  51. );
  52. if (EFI_ERROR (Status) || (Num == 0)) {
  53. DEBUG ((DEBUG_ERROR, "NumOfRecovery not found\n"));
  54. CloseIniFile (Context);
  55. return EFI_NOT_FOUND;
  56. }
  57. ConfigHeader->NumOfRecovery = Num;
  58. *RecoveryArray = AllocateZeroPool ((sizeof (RECOVERY_CONFIG_DATA) * Num));
  59. if (*RecoveryArray == NULL) {
  60. CloseIniFile (Context);
  61. return EFI_OUT_OF_RESOURCES;
  62. }
  63. for (Index = 0; Index < ConfigHeader->NumOfRecovery; Index++) {
  64. //
  65. // Get the section name of each update
  66. //
  67. AsciiStrCpyS (Entry, MAX_LINE_LENGTH, "Recovery");
  68. AsciiValueToStringS (
  69. Entry + AsciiStrnLenS (Entry, MAX_LINE_LENGTH),
  70. MAX_LINE_LENGTH - AsciiStrnLenS (Entry, MAX_LINE_LENGTH),
  71. 0,
  72. Index,
  73. 0
  74. );
  75. Status = GetStringFromDataFile (
  76. Context,
  77. "Head",
  78. Entry,
  79. &SectionName
  80. );
  81. if (EFI_ERROR (Status) || (SectionName == NULL)) {
  82. DEBUG ((DEBUG_ERROR, "[%d] %a not found\n", Index, Entry));
  83. CloseIniFile (Context);
  84. return EFI_NOT_FOUND;
  85. }
  86. //
  87. // The section name of this update has been found.
  88. // Now looks for all the config data of this update
  89. //
  90. //
  91. // FileGuid
  92. //
  93. Status = GetGuidFromDataFile (
  94. Context,
  95. SectionName,
  96. "FileGuid",
  97. &FileGuid
  98. );
  99. if (EFI_ERROR (Status)) {
  100. CloseIniFile (Context);
  101. DEBUG ((DEBUG_ERROR, "[%d] FileGuid not found\n", Index));
  102. return EFI_NOT_FOUND;
  103. }
  104. CopyGuid (&((*RecoveryArray)[Index].FileGuid), &FileGuid);
  105. //
  106. // Length
  107. //
  108. Status = GetHexUintnFromDataFile (
  109. Context,
  110. SectionName,
  111. "Length",
  112. &Num
  113. );
  114. if (EFI_ERROR (Status)) {
  115. CloseIniFile (Context);
  116. DEBUG ((DEBUG_ERROR, "[%d] Length not found\n", Index));
  117. return EFI_NOT_FOUND;
  118. }
  119. (*RecoveryArray)[Index].Length = Num;
  120. //
  121. // ImageOffset
  122. //
  123. Status = GetHexUintnFromDataFile (
  124. Context,
  125. SectionName,
  126. "ImageOffset",
  127. &Num
  128. );
  129. if (EFI_ERROR (Status)) {
  130. CloseIniFile (Context);
  131. DEBUG ((DEBUG_ERROR, "[%d] ImageOffset not found\n", Index));
  132. return EFI_NOT_FOUND;
  133. }
  134. (*RecoveryArray)[Index].ImageOffset = Num;
  135. }
  136. //
  137. // Now all configuration data got. Free those temporary buffers
  138. //
  139. CloseIniFile (Context);
  140. return EFI_SUCCESS;
  141. }