SignedCapsulePkg: Replace [Ascii|Unicode]ValueToString
[mirror_edk2.git] / SignedCapsulePkg / Universal / SystemFirmwareUpdate / ParseConfigProfile.c
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
5 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions
9 of the BSD License which accompanies this distribution. The
10 full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include "SystemFirmwareDxe.h"
19 #include <Library/IniParsingLib.h>
20 #include <Library/PrintLib.h>
21
22 #define MAX_LINE_LENGTH 512
23
24 /**
25 Parse Config data file to get the updated data array.
26
27 @param[in] DataBuffer Config raw file buffer.
28 @param[in] BufferSize Size of raw buffer.
29 @param[in, out] ConfigHeader Pointer to the config header.
30 @param[in, out] UpdateArray Pointer to the config of update data.
31
32 @retval EFI_NOT_FOUND No config data is found.
33 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
34 @retval EFI_SUCCESS Parse the config file successfully.
35
36 **/
37 EFI_STATUS
38 ParseUpdateDataFile (
39 IN UINT8 *DataBuffer,
40 IN UINTN BufferSize,
41 IN OUT CONFIG_HEADER *ConfigHeader,
42 IN OUT UPDATE_CONFIG_DATA **UpdateArray
43 )
44 {
45 EFI_STATUS Status;
46 CHAR8 *SectionName;
47 CHAR8 Entry[MAX_LINE_LENGTH];
48 UINTN Num;
49 UINT64 Num64;
50 UINTN Index;
51 EFI_GUID FileGuid;
52 VOID *Context;
53
54 //
55 // First process the data buffer and get all sections and entries
56 //
57 Context = OpenIniFile(DataBuffer, BufferSize);
58 if (Context == NULL) {
59 return EFI_INVALID_PARAMETER;
60 }
61
62 //
63 // Now get NumOfUpdate
64 //
65 Status = GetDecimalUintnFromDataFile(
66 Context,
67 "Head",
68 "NumOfUpdate",
69 &Num
70 );
71 if (EFI_ERROR(Status) || (Num == 0)) {
72 DEBUG((DEBUG_ERROR, "NumOfUpdate not found\n"));
73 CloseIniFile(Context);
74 return EFI_NOT_FOUND;
75 }
76
77 ConfigHeader->NumOfUpdates = Num;
78 *UpdateArray = AllocateZeroPool ((sizeof (UPDATE_CONFIG_DATA) * Num));
79 if (*UpdateArray == NULL) {
80 CloseIniFile(Context);
81 return EFI_OUT_OF_RESOURCES;
82 }
83
84 for (Index = 0 ; Index < ConfigHeader->NumOfUpdates ; Index++) {
85 //
86 // Get the section name of each update
87 //
88 AsciiStrCpyS (Entry, MAX_LINE_LENGTH, "Update");
89 AsciiValueToStringS (
90 Entry + AsciiStrnLenS (Entry, MAX_LINE_LENGTH),
91 MAX_LINE_LENGTH - AsciiStrnLenS (Entry, MAX_LINE_LENGTH),
92 0,
93 Index,
94 0
95 );
96 Status = GetStringFromDataFile(
97 Context,
98 "Head",
99 Entry,
100 &SectionName
101 );
102 if (EFI_ERROR(Status) || (SectionName == NULL)) {
103 DEBUG((DEBUG_ERROR, "[%d] %a not found\n", Index, Entry));
104 CloseIniFile(Context);
105 return EFI_NOT_FOUND;
106 }
107
108 //
109 // The section name of this update has been found.
110 // Now looks for all the config data of this update
111 //
112 (*UpdateArray)[Index].Index = Index;
113
114 //
115 // FirmwareType
116 //
117 Status = GetDecimalUintnFromDataFile(
118 Context,
119 SectionName,
120 "FirmwareType",
121 &Num
122 );
123 if (EFI_ERROR(Status)) {
124 CloseIniFile(Context);
125 DEBUG((DEBUG_ERROR, "[%d] FirmwareType not found\n", Index));
126 return EFI_NOT_FOUND;
127 }
128 (*UpdateArray)[Index].FirmwareType = (PLATFORM_FIRMWARE_TYPE) Num;
129
130 //
131 // AddressType
132 //
133 Status = GetDecimalUintnFromDataFile(
134 Context,
135 SectionName,
136 "AddressType",
137 &Num
138 );
139 if (EFI_ERROR(Status)) {
140 CloseIniFile(Context);
141 DEBUG((DEBUG_ERROR, "[%d] AddressType not found\n", Index));
142 return EFI_NOT_FOUND;
143 }
144 (*UpdateArray)[Index].AddressType = (FLASH_ADDRESS_TYPE) Num;
145
146 //
147 // BaseAddress
148 //
149 Status = GetHexUint64FromDataFile(
150 Context,
151 SectionName,
152 "BaseAddress",
153 &Num64
154 );
155 if (EFI_ERROR(Status)) {
156 CloseIniFile(Context);
157 DEBUG((DEBUG_ERROR, "[%d] BaseAddress not found\n", Index));
158 return EFI_NOT_FOUND;
159 }
160 (*UpdateArray)[Index].BaseAddress = (EFI_PHYSICAL_ADDRESS) Num64;
161
162 //
163 // FileBuid
164 //
165 Status = GetGuidFromDataFile(
166 Context,
167 SectionName,
168 "FileGuid",
169 &FileGuid
170 );
171 if (EFI_ERROR(Status)) {
172 CloseIniFile(Context);
173 DEBUG((DEBUG_ERROR, "[%d] FileGuid not found\n", Index));
174 return EFI_NOT_FOUND;
175 }
176
177 CopyGuid(&((*UpdateArray)[Index].FileGuid), &FileGuid);
178
179 //
180 // Length
181 //
182 Status = GetHexUintnFromDataFile(
183 Context,
184 SectionName,
185 "Length",
186 &Num
187 );
188 if (EFI_ERROR(Status)) {
189 CloseIniFile(Context);
190 DEBUG((DEBUG_ERROR, "[%d] Length not found\n", Index));
191 return EFI_NOT_FOUND;
192 }
193 (*UpdateArray)[Index].Length = (UINTN) Num;
194
195 //
196 // ImageOffset
197 //
198 Status = GetHexUintnFromDataFile(
199 Context,
200 SectionName,
201 "ImageOffset",
202 &Num
203 );
204 if (EFI_ERROR(Status)) {
205 CloseIniFile(Context);
206 DEBUG((DEBUG_ERROR, "[%d] ImageOffset not found\n", Index));
207 return EFI_NOT_FOUND;
208 }
209 (*UpdateArray)[Index].ImageOffset = (UINTN) Num;
210 }
211
212 //
213 // Now all configuration data got. Free those temporary buffers
214 //
215 CloseIniFile(Context);
216
217 return EFI_SUCCESS;
218 }
219