ParseConfigProfile.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /** @file
  2. Parse the INI configuration file and pass the information to the update driver
  3. so that the driver can perform update accordingly.
  4. Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "SystemFirmwareDxe.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] UpdateArray Pointer to the config of update 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. ParseUpdateDataFile (
  23. IN UINT8 *DataBuffer,
  24. IN UINTN BufferSize,
  25. IN OUT CONFIG_HEADER *ConfigHeader,
  26. IN OUT UPDATE_CONFIG_DATA **UpdateArray
  27. )
  28. {
  29. EFI_STATUS Status;
  30. CHAR8 *SectionName;
  31. CHAR8 Entry[MAX_LINE_LENGTH];
  32. UINTN Num;
  33. UINT64 Num64;
  34. UINTN Index;
  35. EFI_GUID FileGuid;
  36. VOID *Context;
  37. //
  38. // First process the data buffer and get all sections and entries
  39. //
  40. Context = OpenIniFile (DataBuffer, BufferSize);
  41. if (Context == NULL) {
  42. return EFI_INVALID_PARAMETER;
  43. }
  44. //
  45. // Now get NumOfUpdate
  46. //
  47. Status = GetDecimalUintnFromDataFile (
  48. Context,
  49. "Head",
  50. "NumOfUpdate",
  51. &Num
  52. );
  53. if (EFI_ERROR (Status) || (Num == 0)) {
  54. DEBUG ((DEBUG_ERROR, "NumOfUpdate not found\n"));
  55. CloseIniFile (Context);
  56. return EFI_NOT_FOUND;
  57. }
  58. ConfigHeader->NumOfUpdates = Num;
  59. *UpdateArray = AllocateZeroPool ((sizeof (UPDATE_CONFIG_DATA) * Num));
  60. if (*UpdateArray == NULL) {
  61. CloseIniFile (Context);
  62. return EFI_OUT_OF_RESOURCES;
  63. }
  64. for (Index = 0; Index < ConfigHeader->NumOfUpdates; Index++) {
  65. //
  66. // Get the section name of each update
  67. //
  68. AsciiStrCpyS (Entry, MAX_LINE_LENGTH, "Update");
  69. AsciiValueToStringS (
  70. Entry + AsciiStrnLenS (Entry, MAX_LINE_LENGTH),
  71. MAX_LINE_LENGTH - AsciiStrnLenS (Entry, MAX_LINE_LENGTH),
  72. 0,
  73. Index,
  74. 0
  75. );
  76. Status = GetStringFromDataFile (
  77. Context,
  78. "Head",
  79. Entry,
  80. &SectionName
  81. );
  82. if (EFI_ERROR (Status) || (SectionName == NULL)) {
  83. DEBUG ((DEBUG_ERROR, "[%d] %a not found\n", Index, Entry));
  84. CloseIniFile (Context);
  85. return EFI_NOT_FOUND;
  86. }
  87. //
  88. // The section name of this update has been found.
  89. // Now looks for all the config data of this update
  90. //
  91. (*UpdateArray)[Index].Index = Index;
  92. //
  93. // FirmwareType
  94. //
  95. Status = GetDecimalUintnFromDataFile (
  96. Context,
  97. SectionName,
  98. "FirmwareType",
  99. &Num
  100. );
  101. if (EFI_ERROR (Status)) {
  102. CloseIniFile (Context);
  103. DEBUG ((DEBUG_ERROR, "[%d] FirmwareType not found\n", Index));
  104. return EFI_NOT_FOUND;
  105. }
  106. (*UpdateArray)[Index].FirmwareType = (PLATFORM_FIRMWARE_TYPE)Num;
  107. //
  108. // AddressType
  109. //
  110. Status = GetDecimalUintnFromDataFile (
  111. Context,
  112. SectionName,
  113. "AddressType",
  114. &Num
  115. );
  116. if (EFI_ERROR (Status)) {
  117. CloseIniFile (Context);
  118. DEBUG ((DEBUG_ERROR, "[%d] AddressType not found\n", Index));
  119. return EFI_NOT_FOUND;
  120. }
  121. (*UpdateArray)[Index].AddressType = (FLASH_ADDRESS_TYPE)Num;
  122. //
  123. // BaseAddress
  124. //
  125. Status = GetHexUint64FromDataFile (
  126. Context,
  127. SectionName,
  128. "BaseAddress",
  129. &Num64
  130. );
  131. if (EFI_ERROR (Status)) {
  132. CloseIniFile (Context);
  133. DEBUG ((DEBUG_ERROR, "[%d] BaseAddress not found\n", Index));
  134. return EFI_NOT_FOUND;
  135. }
  136. (*UpdateArray)[Index].BaseAddress = (EFI_PHYSICAL_ADDRESS)Num64;
  137. //
  138. // FileGuid
  139. //
  140. Status = GetGuidFromDataFile (
  141. Context,
  142. SectionName,
  143. "FileGuid",
  144. &FileGuid
  145. );
  146. if (EFI_ERROR (Status)) {
  147. CloseIniFile (Context);
  148. DEBUG ((DEBUG_ERROR, "[%d] FileGuid not found\n", Index));
  149. return EFI_NOT_FOUND;
  150. }
  151. CopyGuid (&((*UpdateArray)[Index].FileGuid), &FileGuid);
  152. //
  153. // Length
  154. //
  155. Status = GetHexUintnFromDataFile (
  156. Context,
  157. SectionName,
  158. "Length",
  159. &Num
  160. );
  161. if (EFI_ERROR (Status)) {
  162. CloseIniFile (Context);
  163. DEBUG ((DEBUG_ERROR, "[%d] Length not found\n", Index));
  164. return EFI_NOT_FOUND;
  165. }
  166. (*UpdateArray)[Index].Length = (UINTN)Num;
  167. //
  168. // ImageOffset
  169. //
  170. Status = GetHexUintnFromDataFile (
  171. Context,
  172. SectionName,
  173. "ImageOffset",
  174. &Num
  175. );
  176. if (EFI_ERROR (Status)) {
  177. CloseIniFile (Context);
  178. DEBUG ((DEBUG_ERROR, "[%d] ImageOffset not found\n", Index));
  179. return EFI_NOT_FOUND;
  180. }
  181. (*UpdateArray)[Index].ImageOffset = (UINTN)Num;
  182. }
  183. //
  184. // Now all configuration data got. Free those temporary buffers
  185. //
  186. CloseIniFile (Context);
  187. return EFI_SUCCESS;
  188. }