]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/UefiIfrSupportLib/UefiIfrForm.c
K8:
[mirror_edk2.git] / MdeModulePkg / Library / UefiIfrSupportLib / UefiIfrForm.c
CommitLineData
08e4b3cf 1/** @file\r
2Utility functions which helps in opcode creation, HII configuration string manipulations, \r
3pop up window creations, setup browser persistence data set and get.\r
4\r
5Copyright (c) 2007- 2008, Intel Corporation\r
6All rights reserved. This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "UefiIfrLibraryInternal.h"\r
17\r
18CONST EFI_FORM_BROWSER2_PROTOCOL *mFormBrowser2 = NULL;\r
19CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *mIfrSupportLibHiiConfigRouting = NULL;\r
ac7e320c 20GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 mIfrSupportLibHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r
08e4b3cf 21\r
aa79b0b3 22//\r
23// Fake <ConfigHdr>\r
24//\r
25GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT16 mFakeConfigHdr[] = L"GUID=00000000000000000000000000000000&NAME=0000&PATH=0";\r
26\r
08e4b3cf 27/**\r
28 This function locate FormBrowser2 protocols for later usage.\r
29\r
30 @return Status the status to locate protocol.\r
31**/\r
32EFI_STATUS\r
33LocateFormBrowser2Protocols (\r
34 VOID\r
35 )\r
36{\r
37 EFI_STATUS Status;\r
38 //\r
39 // Locate protocols for later usage\r
40 //\r
41 if (mFormBrowser2 == NULL) {\r
42 Status = gBS->LocateProtocol (&gEfiFormBrowser2ProtocolGuid, NULL, (VOID **) &mFormBrowser2);\r
43 if (EFI_ERROR (Status)) {\r
44 return Status;\r
45 }\r
46 }\r
47 \r
48 if (mIfrSupportLibHiiConfigRouting == NULL) {\r
49 Status = gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &mIfrSupportLibHiiConfigRouting);\r
50 if (EFI_ERROR (Status)) {\r
51 return Status;\r
52 }\r
53 }\r
54\r
55 return EFI_SUCCESS;\r
56}\r
57\r
08e4b3cf 58/**\r
59 Draw a dialog and return the selected key.\r
60\r
61 @param NumberOfLines The number of lines for the dialog box\r
62 @param KeyValue The EFI_KEY value returned if HotKey is TRUE..\r
35d59af3 63 @param String The first String to be displayed in the Pop-Up.\r
08e4b3cf 64 @param Marker A series of (quantity == NumberOfLines - 1) text\r
65 strings which will be used to construct the dialog\r
66 box\r
67\r
68 @retval EFI_SUCCESS Displayed dialog and received user interaction\r
69 @retval EFI_INVALID_PARAMETER One of the parameters was invalid.\r
70 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.\r
71\r
72**/\r
73EFI_STATUS\r
74EFIAPI\r
75IfrLibCreatePopUp2 (\r
76 IN UINTN NumberOfLines,\r
77 OUT EFI_INPUT_KEY *KeyValue,\r
35d59af3 78 IN CHAR16 *String,\r
08e4b3cf 79 IN VA_LIST Marker\r
80 )\r
81{\r
82 UINTN Index;\r
83 UINTN Count;\r
84 UINTN Start;\r
85 UINTN Top;\r
86 CHAR16 *StringPtr;\r
87 UINTN LeftColumn;\r
88 UINTN RightColumn;\r
89 UINTN TopRow;\r
90 UINTN BottomRow;\r
91 UINTN DimensionsWidth;\r
92 UINTN DimensionsHeight;\r
93 EFI_INPUT_KEY Key;\r
94 UINTN LargestString;\r
95 CHAR16 *StackString;\r
96 EFI_STATUS Status;\r
97 UINTN StringLen;\r
98 CHAR16 *LineBuffer;\r
99 CHAR16 **StringArray;\r
100 EFI_EVENT TimerEvent;\r
101 EFI_EVENT WaitList[2];\r
102 UINTN CurrentAttribute;\r
103 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut;\r
08e4b3cf 104\r
08e4b3cf 105 if ((KeyValue == NULL) || (String == NULL)) {\r
106 return EFI_INVALID_PARAMETER;\r
107 }\r
108\r
109 TopRow = 0;\r
110 BottomRow = 0;\r
111 LeftColumn = 0;\r
112 RightColumn = 0;\r
113\r
114 ConOut = gST->ConOut;\r
115 ConOut->QueryMode (ConOut, ConOut->Mode->Mode, &RightColumn, &BottomRow);\r
116\r
117 DimensionsWidth = RightColumn - LeftColumn;\r
118 DimensionsHeight = BottomRow - TopRow;\r
119\r
120 CurrentAttribute = ConOut->Mode->Attribute;\r
121\r
122 LineBuffer = AllocateZeroPool (DimensionsWidth * sizeof (CHAR16));\r
123 if (LineBuffer == NULL) {\r
124 return EFI_OUT_OF_RESOURCES;\r
125 }\r
126\r
127 //\r
128 // Determine the largest string in the dialog box\r
129 // Notice we are starting with 1 since String is the first string\r
130 //\r
131 StringArray = AllocateZeroPool (NumberOfLines * sizeof (CHAR16 *));\r
132 if (StringArray == NULL) {\r
133 FreePool (LineBuffer);\r
134 return EFI_OUT_OF_RESOURCES;\r
135 }\r
136 LargestString = StrLen (String);\r
137 StringArray[0] = String;\r
138\r
139 for (Index = 1; Index < NumberOfLines; Index++) {\r
140 StackString = VA_ARG (Marker, CHAR16 *);\r
141\r
142 if (StackString == NULL) {\r
143 FreePool (LineBuffer);\r
144 FreePool (StringArray);\r
145 return EFI_INVALID_PARAMETER;\r
146 }\r
147\r
148 StringArray[Index] = StackString;\r
149 StringLen = StrLen (StackString);\r
150 if (StringLen > LargestString) {\r
151 LargestString = StringLen;\r
152 }\r
153 }\r
154\r
155 if ((LargestString + 2) > DimensionsWidth) {\r
156 LargestString = DimensionsWidth - 2;\r
157 }\r
158\r
159 //\r
160 // Subtract the PopUp width from total Columns, allow for one space extra on\r
161 // each end plus a border.\r
162 //\r
163 Start = (DimensionsWidth - LargestString - 2) / 2 + LeftColumn + 1;\r
164\r
165 Top = ((DimensionsHeight - NumberOfLines - 2) / 2) + TopRow - 1;\r
166\r
167 //\r
168 // Disable cursor\r
169 //\r
170 ConOut->EnableCursor (ConOut, FALSE);\r
171 ConOut->SetAttribute (ConOut, EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE);\r
172\r
173 StringPtr = &LineBuffer[0];\r
174 *StringPtr++ = BOXDRAW_DOWN_RIGHT;\r
175 for (Index = 0; Index < LargestString; Index++) {\r
176 *StringPtr++ = BOXDRAW_HORIZONTAL;\r
177 }\r
178 *StringPtr++ = BOXDRAW_DOWN_LEFT;\r
179 *StringPtr = L'\0';\r
180\r
181 ConOut->SetCursorPosition (ConOut, Start, Top);\r
182 ConOut->OutputString (ConOut, LineBuffer);\r
183\r
184 for (Index = 0; Index < NumberOfLines; Index++) {\r
185 StringPtr = &LineBuffer[0];\r
186 *StringPtr++ = BOXDRAW_VERTICAL;\r
187\r
188 for (Count = 0; Count < LargestString; Count++) {\r
189 StringPtr[Count] = L' ';\r
190 }\r
191\r
192 StringLen = StrLen (StringArray[Index]);\r
193 if (StringLen > LargestString) {\r
194 StringLen = LargestString;\r
195 }\r
196 CopyMem (\r
197 StringPtr + ((LargestString - StringLen) / 2),\r
198 StringArray[Index],\r
199 StringLen * sizeof (CHAR16)\r
200 );\r
201 StringPtr += LargestString;\r
202\r
203 *StringPtr++ = BOXDRAW_VERTICAL;\r
204 *StringPtr = L'\0';\r
205\r
206 ConOut->SetCursorPosition (ConOut, Start, Top + 1 + Index);\r
207 ConOut->OutputString (ConOut, LineBuffer);\r
208 }\r
209\r
210 StringPtr = &LineBuffer[0];\r
211 *StringPtr++ = BOXDRAW_UP_RIGHT;\r
212 for (Index = 0; Index < LargestString; Index++) {\r
213 *StringPtr++ = BOXDRAW_HORIZONTAL;\r
214 }\r
215 *StringPtr++ = BOXDRAW_UP_LEFT;\r
216 *StringPtr = L'\0';\r
217\r
218 ConOut->SetCursorPosition (ConOut, Start, Top + NumberOfLines + 1);\r
219 ConOut->OutputString (ConOut, LineBuffer);\r
220\r
221 do {\r
222 Status = gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &TimerEvent);\r
223\r
224 //\r
225 // Set a timer event of 1 second expiration\r
226 //\r
227 gBS->SetTimer (\r
228 TimerEvent,\r
229 TimerRelative,\r
230 10000000\r
231 );\r
232\r
233 //\r
234 // Wait for the keystroke event or the timer\r
235 //\r
236 WaitList[0] = gST->ConIn->WaitForKey;\r
237 WaitList[1] = TimerEvent;\r
238 Status = gBS->WaitForEvent (2, WaitList, &Index);\r
239\r
240 //\r
241 // Check for the timer expiration\r
242 //\r
243 if (!EFI_ERROR (Status) && Index == 1) {\r
244 Status = EFI_TIMEOUT;\r
245 }\r
246\r
247 gBS->CloseEvent (TimerEvent);\r
248 } while (Status == EFI_TIMEOUT);\r
249\r
250 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
251 CopyMem (KeyValue, &Key, sizeof (EFI_INPUT_KEY));\r
252\r
253 ConOut->SetAttribute (ConOut, CurrentAttribute);\r
254 ConOut->EnableCursor (ConOut, TRUE);\r
255\r
256 FreePool (LineBuffer);\r
257 FreePool (StringArray);\r
258\r
259 return Status;\r
260}\r
261\r
262\r
263/**\r
264 Draw a dialog and return the selected key.\r
265\r
266 @param NumberOfLines The number of lines for the dialog box\r
267 @param KeyValue The EFI_KEY value returned if HotKey is TRUE..\r
268 @param String Pointer to the first string in the list\r
269 @param ... A series of (quantity == NumberOfLines - 1) text\r
270 strings which will be used to construct the dialog\r
271 box\r
272\r
273 @retval EFI_SUCCESS Displayed dialog and received user interaction\r
274 @retval EFI_INVALID_PARAMETER One of the parameters was invalid.\r
275\r
276**/\r
277EFI_STATUS\r
278EFIAPI\r
279IfrLibCreatePopUp (\r
280 IN UINTN NumberOfLines,\r
281 OUT EFI_INPUT_KEY *KeyValue,\r
282 IN CHAR16 *String,\r
283 ...\r
284 )\r
285{\r
286 EFI_STATUS Status;\r
287 VA_LIST Marker;\r
288\r
35d59af3 289 VA_START (Marker, String);\r
08e4b3cf 290\r
35d59af3 291 Status = IfrLibCreatePopUp2 (NumberOfLines, KeyValue, String, Marker);\r
08e4b3cf 292\r
293 VA_END (Marker);\r
294\r
295 return Status;\r
296}\r
297\r
298/**\r
299 Extract block name from the array generated by VFR compiler. The name of\r
300 this array is "Vfr + <StorageName> + BlockName", e.g. "VfrMyIfrNVDataBlockName".\r
301 Format of this array is:\r
302 Array length | 4-bytes\r
303 Offset | 2-bytes\r
304 Width | 2-bytes\r
305 Offset | 2-bytes\r
306 Width | 2-bytes\r
307 ... ...\r
308\r
309 @param Buffer Array generated by VFR compiler.\r
310 @param BlockName The returned <BlockName>\r
311\r
312 @retval EFI_OUT_OF_RESOURCES Run out of memory resource.\r
313 @retval EFI_INVALID_PARAMETER Buffer is NULL or BlockName is NULL.\r
314 @retval EFI_SUCCESS Operation successful.\r
315\r
316**/\r
317EFI_STATUS\r
318ExtractBlockName (\r
319 IN UINT8 *Buffer,\r
320 OUT CHAR16 **BlockName\r
321 )\r
322\r
323{\r
324 UINTN Index;\r
325 UINT32 Length;\r
326 UINT32 BlockNameNumber;\r
327 UINTN HexStringBufferLen;\r
328 CHAR16 *StringPtr;\r
329\r
330 if ((Buffer == NULL) || (BlockName == NULL)) {\r
331 return EFI_INVALID_PARAMETER;\r
332 }\r
333\r
334 //\r
335 // Calculate number of Offset/Width pair\r
336 //\r
337 CopyMem (&Length, Buffer, sizeof (UINT32));\r
338 BlockNameNumber = (Length - sizeof (UINT32)) / (sizeof (UINT16) * 2);\r
339\r
340 //\r
341 // <BlockName> ::= &OFFSET=1234&WIDTH=1234\r
342 // | 8 | 4 | 7 | 4 |\r
343 //\r
344 StringPtr = AllocateZeroPool ((BlockNameNumber * (8 + 4 + 7 + 4) + 1) * sizeof (CHAR16));\r
08e4b3cf 345 if (StringPtr == NULL) {\r
346 return EFI_OUT_OF_RESOURCES;\r
347 }\r
fa7b3168 348 *BlockName = StringPtr;\r
08e4b3cf 349\r
350 Buffer += sizeof (UINT32);\r
351 for (Index = 0; Index < BlockNameNumber; Index++) {\r
352 StrCpy (StringPtr, L"&OFFSET=");\r
353 StringPtr += 8;\r
354\r
355 HexStringBufferLen = 5;\r
356 BufToHexString (StringPtr, &HexStringBufferLen, Buffer, sizeof (UINT16));\r
357 Buffer += sizeof (UINT16);\r
358 StringPtr += 4;\r
359\r
360 StrCpy (StringPtr, L"&WIDTH=");\r
361 StringPtr += 7;\r
362\r
363 HexStringBufferLen = 5;\r
364 BufToHexString (StringPtr, &HexStringBufferLen, Buffer, sizeof (UINT16));\r
365 Buffer += sizeof (UINT16);\r
366 StringPtr += 4;\r
367 }\r
368\r
369 return EFI_SUCCESS;\r
370}\r
371\r
372/**\r
373\r
374 Extract block config from the array generated by VFR compiler. The name of\r
375 this array is "Vfr + <StorageName> + Default<HexCh>4", e.g. "VfrMyIfrNVDataDefault0000".\r
376\r
377 @param Buffer - Array generated by VFR compiler.\r
378 @param BlockConfig - The returned <BlockConfig>\r
379\r
380 @retval EFI_OUT_OF_RESOURCES - Run out of memory resource.\r
381 @retval EFI_INVALID_PARAMETER - Buffer is NULL or BlockConfig is NULL.\r
382 @retval EFI_SUCCESS - Operation successful.\r
383\r
384**/\r
385EFI_STATUS\r
386ExtractBlockConfig (\r
387 IN UINT8 *Buffer,\r
388 OUT CHAR16 **BlockConfig\r
389 )\r
390{\r
391 UINT32 Length;\r
392 UINT16 Width;\r
393 UINTN HexStringBufferLen;\r
394 CHAR16 *StringPtr;\r
395 UINT8 *BufferEnd;\r
396 CHAR16 *StringEnd;\r
397 EFI_STATUS Status;\r
398\r
399 if ((Buffer == NULL) || (BlockConfig == NULL)) {\r
400 return EFI_INVALID_PARAMETER;\r
401 }\r
402\r
403 //\r
404 // Calculate length of AltResp string\r
405 // Format of Default value array is:\r
406 // Array length | 4-bytes\r
407 // Offset | 2-bytes\r
408 // Width | 2-bytes\r
409 // Value | Variable length\r
410 // Offset | 2-bytes\r
411 // Width | 2-bytes\r
412 // Value | Variable length\r
413 // ... ...\r
414 // When value is 1 byte in length, overhead of AltResp string will be maximum,\r
415 // BlockConfig ::= <&OFFSET=1234&WIDTH=1234&VALUE=12>+\r
416 // | 8 | 4 | 7 | 4 | 7 |2|\r
417 // so the maximum length of BlockConfig could be calculated as:\r
418 // (ArrayLength / 5) * (8 + 4 + 7 + 4 + 7 + 2) = ArrayLength * 6.4 < ArrayLength * 7\r
419 //\r
420 CopyMem (&Length, Buffer, sizeof (UINT32));\r
421 BufferEnd = Buffer + Length;\r
422 StringPtr = AllocatePool (Length * 7 * sizeof (CHAR16));\r
423 *BlockConfig = StringPtr;\r
424 if (StringPtr == NULL) {\r
425 return EFI_OUT_OF_RESOURCES;\r
426 }\r
427 StringEnd = StringPtr + (Length * 7);\r
428\r
429 Buffer += sizeof (UINT32);\r
430 while (Buffer < BufferEnd) {\r
431 StrCpy (StringPtr, L"&OFFSET=");\r
432 StringPtr += 8;\r
433\r
434 HexStringBufferLen = 5;\r
435 BufToHexString (StringPtr, &HexStringBufferLen, Buffer, sizeof (UINT16));\r
436 Buffer += sizeof (UINT16);\r
437 StringPtr += 4;\r
438\r
439 StrCpy (StringPtr, L"&WIDTH=");\r
440 StringPtr += 7;\r
441\r
442 HexStringBufferLen = 5;\r
443 BufToHexString (StringPtr, &HexStringBufferLen, Buffer, sizeof (UINT16));\r
444 CopyMem (&Width, Buffer, sizeof (UINT16));\r
445 Buffer += sizeof (UINT16);\r
446 StringPtr += 4;\r
447\r
448 StrCpy (StringPtr, L"&VALUE=");\r
449 StringPtr += 7;\r
450\r
451 HexStringBufferLen = StringEnd - StringPtr;\r
452 Status = BufToHexString (StringPtr, &HexStringBufferLen, Buffer, Width);\r
453 if (EFI_ERROR (Status)) {\r
454 return Status;\r
455 }\r
456 Buffer += Width;\r
457 StringPtr += (Width * 2);\r
458 }\r
459\r
460 return EFI_SUCCESS;\r
461}\r
462\r
463/**\r
464 Construct <ConfigAltResp> for a buffer storage.\r
465\r
466 @param ConfigRequest The Config request string. If set to NULL, all the\r
467 configurable elements will be extracted from BlockNameArray.\r
468 @param ConfigAltResp The returned <ConfigAltResp>.\r
469 @param Progress On return, points to a character in the Request.\r
470 @param Guid GUID of the buffer storage.\r
471 @param Name Name of the buffer storage.\r
472 @param DriverHandle The DriverHandle which is used to invoke HiiDatabase\r
473 protocol interface NewPackageList().\r
474 @param BufferStorage Content of the buffer storage.\r
475 @param BufferStorageSize Length in bytes of the buffer storage.\r
476 @param BlockNameArray Array generated by VFR compiler.\r
477 @param NumberAltCfg Number of Default value array generated by VFR compiler.\r
478 The sequential input parameters will be number of\r
479 AltCfgId and DefaultValueArray pairs. When set to 0,\r
480 there will be no <AltResp>.\r
481\r
482 retval EFI_OUT_OF_RESOURCES Run out of memory resource.\r
483 retval EFI_INVALID_PARAMETER ConfigAltResp is NULL.\r
484 retval EFI_SUCCESS Operation successful.\r
485\r
486**/\r
487EFI_STATUS\r
488ConstructConfigAltResp (\r
489 IN EFI_STRING ConfigRequest, OPTIONAL\r
490 OUT EFI_STRING *Progress,\r
491 OUT EFI_STRING *ConfigAltResp,\r
492 IN EFI_GUID *Guid,\r
493 IN CHAR16 *Name,\r
494 IN EFI_HANDLE *DriverHandle,\r
495 IN VOID *BufferStorage,\r
496 IN UINTN BufferStorageSize,\r
497 IN VOID *BlockNameArray, OPTIONAL\r
498 IN UINTN NumberAltCfg,\r
499 ...\r
500//IN UINT16 AltCfgId,\r
501//IN VOID *DefaultValueArray,\r
502 )\r
503{\r
504 EFI_STATUS Status;\r
505 CHAR16 *ConfigHdr;\r
506 CHAR16 *BlockName;\r
507 CHAR16 *DescHdr;\r
508 CHAR16 *StringPtr;\r
509 CHAR16 **AltCfg;\r
510 UINT16 AltCfgId;\r
511 VOID *DefaultValueArray;\r
512 UINTN StrBufferLen;\r
513 EFI_STRING ConfigResp;\r
514 EFI_STRING TempStr;\r
515 VA_LIST Args;\r
516 UINTN AltRespLen;\r
517 UINTN Index;\r
518 BOOLEAN NeedFreeConfigRequest;\r
519 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;\r
520 UINTN Len;\r
521\r
522 if (ConfigAltResp == NULL) {\r
523 return EFI_INVALID_PARAMETER;\r
524 }\r
fa7b3168 525 \r
526 DescHdr = NULL;\r
527 StringPtr = NULL;\r
528 AltCfg = NULL;\r
529 ConfigResp = NULL;\r
530 BlockName = NULL;\r
531 NeedFreeConfigRequest = FALSE;\r
08e4b3cf 532\r
533 //\r
534 // Construct <ConfigHdr> : "GUID=...&NAME=...&PATH=..."\r
535 //\r
536 ConfigHdr = NULL;\r
537 StrBufferLen = 0;\r
538 Status = ConstructConfigHdr (\r
539 ConfigHdr,\r
540 &StrBufferLen,\r
541 Guid,\r
542 Name,\r
543 DriverHandle\r
544 );\r
fa7b3168 545 ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
546 ConfigHdr = AllocateZeroPool (StrBufferLen);\r
547 if (ConfigHdr == NULL) {\r
548 Status = EFI_OUT_OF_RESOURCES;\r
549 goto Exit;\r
08e4b3cf 550 }\r
fa7b3168 551 Status = ConstructConfigHdr (\r
552 ConfigHdr,\r
553 &StrBufferLen,\r
554 Guid,\r
555 Name,\r
556 DriverHandle\r
557 );\r
08e4b3cf 558\r
559 if (EFI_ERROR (Status)) {\r
fa7b3168 560 goto Exit;\r
08e4b3cf 561 }\r
562\r
563 //\r
564 // Construct <ConfigResp>\r
565 //\r
08e4b3cf 566 if (ConfigRequest == NULL) {\r
567 //\r
568 // If ConfigRequest is set to NULL, export all configurable elements in BlockNameArray\r
569 //\r
570 Status = ExtractBlockName (BlockNameArray, &BlockName);\r
571 if (EFI_ERROR (Status)) {\r
fa7b3168 572 goto Exit;\r
08e4b3cf 573 }\r
574 \r
575 Len = StrSize (ConfigHdr);\r
576 ConfigRequest = AllocateZeroPool (Len + StrSize (BlockName) - sizeof (CHAR16));\r
fa7b3168 577 if (ConfigRequest == NULL) {\r
578 Status = EFI_OUT_OF_RESOURCES;\r
579 goto Exit;\r
580 }\r
581 \r
08e4b3cf 582 StrCpy (ConfigRequest, ConfigHdr);\r
583 StrCat (ConfigRequest, BlockName);\r
584 NeedFreeConfigRequest = TRUE;\r
fa7b3168 585\r
08e4b3cf 586 }\r
587\r
588 Status = gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &HiiConfigRouting);\r
589 if (EFI_ERROR (Status)) {\r
590 return Status;\r
591 }\r
592\r
593 Status = HiiConfigRouting->BlockToConfig (\r
594 HiiConfigRouting,\r
595 ConfigRequest,\r
596 BufferStorage,\r
597 BufferStorageSize,\r
598 &ConfigResp,\r
599 (Progress == NULL) ? &TempStr : Progress\r
600 );\r
601 if (EFI_ERROR (Status)) {\r
602 return Status;\r
603 }\r
604\r
fa7b3168 605 AltRespLen = 0;\r
08e4b3cf 606 //\r
607 // Construct <AltResp>\r
608 //\r
fa7b3168 609 if (NumberAltCfg > 0) {\r
610 DescHdr = AllocateZeroPool (NumberAltCfg * 16 * sizeof (CHAR16));\r
611 if (DescHdr == NULL) {\r
612 Status = EFI_OUT_OF_RESOURCES;\r
613 goto Exit;\r
614 }\r
615 \r
616 StringPtr = DescHdr;\r
617 AltCfg = AllocateZeroPool (NumberAltCfg * sizeof (CHAR16 *));\r
618 if (AltCfg == NULL) {\r
619 Status = EFI_OUT_OF_RESOURCES;\r
620 goto Exit;\r
621 }\r
08e4b3cf 622\r
fa7b3168 623 VA_START (Args, NumberAltCfg);\r
624 for (Index = 0; Index < NumberAltCfg; Index++) {\r
625 AltCfgId = (UINT16) VA_ARG (Args, UINT16);\r
626 DefaultValueArray = (UINT8 *) VA_ARG (Args, VOID *);\r
627 \r
628 //\r
629 // '&' <ConfigHdr>\r
630 //\r
631 AltRespLen += (StrLen (ConfigHdr) + 1);\r
632 \r
633 StringPtr = DescHdr + Index * 16;\r
634 StrCpy (StringPtr, L"&ALTCFG=");\r
635 AltRespLen += (8 + sizeof (UINT16) * 2);\r
636 \r
637 StrBufferLen = 5;\r
638 BufToHexString (StringPtr + 8, &StrBufferLen, (UINT8 *) &AltCfgId, sizeof (UINT16));\r
639 Status = ExtractBlockConfig (DefaultValueArray, &AltCfg[Index]);\r
640 if (EFI_ERROR (Status)) {\r
641 goto Exit;\r
642 }\r
643 AltRespLen += StrLen (AltCfg[Index]);\r
08e4b3cf 644 }\r
fa7b3168 645 VA_END (Args);\r
08e4b3cf 646 }\r
08e4b3cf 647\r
648 //\r
649 // Generate the final <ConfigAltResp>\r
650 //\r
651 StrBufferLen = (StrLen ((CHAR16 *) ConfigResp) + AltRespLen + 1) * sizeof (CHAR16);\r
652 TempStr = AllocateZeroPool (StrBufferLen);\r
653 *ConfigAltResp = TempStr;\r
654 if (TempStr == NULL) {\r
fa7b3168 655 goto Exit;\r
08e4b3cf 656 }\r
657\r
658 //\r
659 // <ConfigAltResp> ::= <ConfigResp> ['&' <AltResp>]*\r
660 //\r
661 StrCpy (TempStr, ConfigResp);\r
662 for (Index = 0; Index < NumberAltCfg; Index++) {\r
663 StrCat (TempStr, L"&");\r
664 StrCat (TempStr, ConfigHdr);\r
665 StrCat (TempStr, DescHdr + Index * 16);\r
666 StrCat (TempStr, AltCfg[Index]);\r
667\r
fca1cc71 668 FreePool (AltCfg[Index]);\r
08e4b3cf 669 }\r
670\r
fa7b3168 671Exit:\r
08e4b3cf 672 if (NeedFreeConfigRequest) {\r
fca1cc71 673 FreePool (ConfigRequest);\r
08e4b3cf 674 }\r
fca1cc71 675 FreePool (ConfigHdr);\r
fa7b3168 676 if (ConfigResp != NULL) {\r
677 FreePool (ConfigResp);\r
678 }\r
679\r
680 if (BlockName != NULL) {\r
681 FreePool (BlockName);\r
682 }\r
683\r
684 if (NumberAltCfg > 0) {\r
685 FreePool (DescHdr);\r
686 FreePool (AltCfg);\r
687 }\r
08e4b3cf 688\r
689 return EFI_SUCCESS;\r
690}\r
691\r
692/**\r
693 Swap bytes in the buffer. This is a internal function.\r
694\r
695 @param Buffer Binary buffer.\r
696 @param BufferSize Size of the buffer in bytes.\r
697\r
698 @return None.\r
699\r
700**/\r
701VOID\r
702SwapBuffer (\r
703 IN OUT UINT8 *Buffer,\r
704 IN UINTN BufferSize\r
705 )\r
706{\r
707 UINTN Index;\r
708 UINT8 Temp;\r
709 UINTN SwapCount;\r
710\r
711 SwapCount = BufferSize / 2;\r
712 for (Index = 0; Index < SwapCount; Index++) {\r
713 Temp = Buffer[Index];\r
714 Buffer[Index] = Buffer[BufferSize - 1 - Index];\r
715 Buffer[BufferSize - 1 - Index] = Temp;\r
716 }\r
717}\r
718\r
719/**\r
720 Converts the unicode character of the string from uppercase to lowercase.\r
721 This is a internal function.\r
722\r
723 @param Str String to be converted\r
724\r
725**/\r
726VOID\r
727EFIAPI\r
728ToLower (\r
729 IN OUT CHAR16 *Str\r
730 )\r
731{\r
732 CHAR16 *Ptr;\r
733 \r
734 for (Ptr = Str; *Ptr != L'\0'; Ptr++) {\r
735 if (*Ptr >= L'A' && *Ptr <= L'Z') {\r
736 *Ptr = (CHAR16) (*Ptr - L'A' + L'a');\r
737 }\r
738 }\r
739}\r
740\r
741\r
742/**\r
743 Converts binary buffer to Unicode string in reversed byte order from BufToHexString().\r
744\r
745 @param Str String for output\r
746 @param Buffer Binary buffer.\r
747 @param BufferSize Size of the buffer in bytes.\r
748\r
749 @retval EFI_SUCCESS The function completed successfully.\r
750 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.\r
751\r
752**/\r
753EFI_STATUS\r
754EFIAPI\r
755BufInReverseOrderToHexString (\r
756 IN OUT CHAR16 *Str,\r
757 IN UINT8 *Buffer,\r
758 IN UINTN BufferSize\r
759 )\r
760{\r
761 EFI_STATUS Status;\r
762 UINT8 *NewBuffer;\r
763 UINTN StrBufferLen;\r
764\r
765 NewBuffer = AllocateCopyPool (BufferSize, Buffer);\r
766 if (NewBuffer == NULL) {\r
767 return EFI_OUT_OF_RESOURCES;\r
768 }\r
769 SwapBuffer (NewBuffer, BufferSize);\r
770\r
771 StrBufferLen = BufferSize * sizeof (CHAR16) + 1;\r
772 Status = BufToHexString (Str, &StrBufferLen, NewBuffer, BufferSize);\r
773\r
774 FreePool (NewBuffer);\r
775 //\r
776 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.\r
777 //\r
778 ToLower (Str);\r
779\r
780 return Status;\r
781}\r
782\r
783\r
784/**\r
785 Converts Hex String to binary buffer in reversed byte order from HexStringToBuf().\r
786\r
787 @param Buffer Pointer to buffer that receives the data.\r
788 @param BufferSize Length in bytes of the buffer to hold converted\r
789 data. If routine return with EFI_SUCCESS,\r
790 containing length of converted data. If routine\r
791 return with EFI_BUFFER_TOO_SMALL, containg length\r
792 of buffer desired.\r
793 @param Str String to be converted from.\r
794\r
795 @retval EFI_SUCCESS The function completed successfully.\r
796 @retval RETURN_BUFFER_TOO_SMALL The input BufferSize is too small to hold the output. BufferSize\r
797 will be updated to the size required for the converstion.\r
798\r
799**/\r
800EFI_STATUS\r
801EFIAPI\r
802HexStringToBufInReverseOrder (\r
803 IN OUT UINT8 *Buffer,\r
804 IN OUT UINTN *BufferSize,\r
805 IN CHAR16 *Str\r
806 )\r
807{\r
808 EFI_STATUS Status;\r
809 UINTN ConvertedStrLen;\r
810\r
811 ConvertedStrLen = 0;\r
812 Status = HexStringToBuf (Buffer, BufferSize, Str, &ConvertedStrLen);\r
813 if (!EFI_ERROR (Status)) {\r
814 SwapBuffer (Buffer, (ConvertedStrLen + 1) / 2);\r
815 }\r
816\r
817 return Status;\r
818}\r
819\r
820/**\r
821 Convert binary representation Config string (e.g. "0041004200430044") to the\r
822 original string (e.g. "ABCD"). Config string appears in <ConfigHdr> (i.e.\r
823 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").\r
824\r
825 @param UnicodeString Original Unicode string.\r
826 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.\r
827 Includes tailing '\0' character.\r
828 On output:\r
829 If return EFI_SUCCESS, containing length of Unicode string buffer.\r
830 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.\r
831 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+\r
832\r
833 @retval EFI_SUCCESS Operation completes successfully.\r
834 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.\r
835\r
836**/\r
837EFI_STATUS\r
838EFIAPI\r
839ConfigStringToUnicode (\r
840 IN OUT CHAR16 *UnicodeString,\r
841 IN OUT UINTN *StrBufferLen,\r
842 IN CHAR16 *ConfigString\r
843 )\r
844{\r
845 UINTN Index;\r
846 UINTN Len;\r
847 UINTN BufferSize;\r
848 CHAR16 BackupChar;\r
849\r
850 Len = StrLen (ConfigString) / 4;\r
851 BufferSize = (Len + 1) * sizeof (CHAR16);\r
852\r
853 if (*StrBufferLen < BufferSize) {\r
854 *StrBufferLen = BufferSize;\r
855 return EFI_BUFFER_TOO_SMALL;\r
856 }\r
857\r
858 *StrBufferLen = BufferSize;\r
859\r
860 for (Index = 0; Index < Len; Index++) {\r
861 BackupChar = ConfigString[4];\r
862 ConfigString[4] = L'\0';\r
863\r
864 HexStringToBuf ((UINT8 *) UnicodeString, &BufferSize, ConfigString, NULL);\r
865\r
866 ConfigString[4] = BackupChar;\r
867\r
868 ConfigString += 4;\r
869 UnicodeString += 1;\r
870 }\r
871\r
872 //\r
873 // Add tailing '\0' character\r
874 //\r
875 *UnicodeString = L'\0';\r
876\r
877 return EFI_SUCCESS;\r
878}\r
879\r
880/**\r
881 Convert Unicode string to binary representation Config string, e.g.\r
882 "ABCD" => "0041004200430044". Config string appears in <ConfigHdr> (i.e.\r
883 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").\r
884\r
885 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+\r
886 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.\r
887 Includes tailing '\0' character.\r
888 On output:\r
889 If return EFI_SUCCESS, containing length of Unicode string buffer.\r
890 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.\r
891 @param UnicodeString Original Unicode string.\r
892\r
893 @retval EFI_SUCCESS Operation completes successfully.\r
894 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.\r
895\r
896**/\r
897EFI_STATUS\r
898EFIAPI\r
899UnicodeToConfigString (\r
900 IN OUT CHAR16 *ConfigString,\r
901 IN OUT UINTN *StrBufferLen,\r
902 IN CHAR16 *UnicodeString\r
903 )\r
904{\r
905 UINTN Index;\r
906 UINTN Len;\r
907 UINTN BufferSize;\r
908 CHAR16 *String;\r
909\r
910 Len = StrLen (UnicodeString);\r
911 BufferSize = (Len * 4 + 1) * sizeof (CHAR16);\r
912\r
913 if (*StrBufferLen < BufferSize) {\r
914 *StrBufferLen = BufferSize;\r
915 return EFI_BUFFER_TOO_SMALL;\r
916 }\r
917\r
918 *StrBufferLen = BufferSize;\r
919 String = ConfigString;\r
920\r
921 for (Index = 0; Index < Len; Index++) {\r
922 BufToHexString (ConfigString, &BufferSize, (UINT8 *) UnicodeString, 2);\r
923\r
924 ConfigString += 4;\r
925 UnicodeString += 1;\r
926 }\r
927\r
928 //\r
929 // Add tailing '\0' character\r
930 //\r
931 *ConfigString = L'\0';\r
932\r
933 //\r
934 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.\r
935 //\r
936 ToLower (String); \r
937 return EFI_SUCCESS;\r
938}\r
939\r
940/**\r
941 Construct <ConfigHdr> using routing information GUID/NAME/PATH.\r
942\r
943 @param ConfigHdr Pointer to the ConfigHdr string.\r
944 @param StrBufferLen On input: Length in bytes of buffer to hold the\r
945 ConfigHdr string. Includes tailing '\0' character.\r
946 On output: If return EFI_SUCCESS, containing\r
947 length of ConfigHdr string buffer. If return\r
948 EFI_BUFFER_TOO_SMALL, containg length of string\r
949 buffer desired.\r
950 @param Guid Routing information: GUID.\r
951 @param Name Routing information: NAME.\r
952 @param DriverHandle Driver handle which contains the routing\r
953 information: PATH.\r
954\r
955 @retval EFI_SUCCESS Operation completes successfully.\r
956 @retval EFI_BUFFER_TOO_SMALL The ConfigHdr string buffer is too small.\r
957\r
958**/\r
959EFI_STATUS\r
960EFIAPI\r
961ConstructConfigHdr (\r
962 IN OUT CHAR16 *ConfigHdr,\r
963 IN OUT UINTN *StrBufferLen,\r
964 IN CONST EFI_GUID *Guid,\r
965 IN CHAR16 *Name, OPTIONAL\r
966 IN EFI_HANDLE *DriverHandle\r
967 )\r
968{\r
969 EFI_STATUS Status;\r
970 UINTN NameStrLen;\r
971 UINTN DevicePathSize;\r
972 UINTN BufferSize;\r
973 CHAR16 *StrPtr;\r
974 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
975\r
976 if (Name == NULL) {\r
977 //\r
978 // There will be no "NAME" in <ConfigHdr> for Name/Value storage\r
979 //\r
980 NameStrLen = 0;\r
981 } else {\r
982 //\r
983 // For buffer storage\r
984 //\r
985 NameStrLen = StrLen (Name);\r
986 }\r
987\r
988 //\r
989 // Retrieve DevicePath Protocol associated with this HiiPackageList\r
990 //\r
991 Status = gBS->HandleProtocol (\r
992 DriverHandle,\r
993 &gEfiDevicePathProtocolGuid,\r
994 (VOID **) &DevicePath\r
995 );\r
996 if (EFI_ERROR (Status)) {\r
997 return Status;\r
998 }\r
999\r
1000 DevicePathSize = GetDevicePathSize (DevicePath);\r
1001\r
1002 //\r
1003 // GUID=<HexCh>32&NAME=<Char>NameStrLen&PATH=<HexChar>DevicePathStrLen <NULL>\r
1004 // | 5 | 32 | 6 | NameStrLen*4 | 6 | DevicePathStrLen | 1 |\r
1005 //\r
1006 BufferSize = (5 + 32 + 6 + NameStrLen * 4 + 6 + DevicePathSize * 2 + 1) * sizeof (CHAR16);\r
1007 if (*StrBufferLen < BufferSize) {\r
1008 *StrBufferLen = BufferSize;\r
1009 return EFI_BUFFER_TOO_SMALL;\r
1010 }\r
1011\r
1012 *StrBufferLen = BufferSize;\r
1013\r
1014 StrPtr = ConfigHdr;\r
1015\r
1016 StrCpy (StrPtr, L"GUID=");\r
1017 StrPtr += 5;\r
1018 BufInReverseOrderToHexString (StrPtr, (UINT8 *) Guid, sizeof (EFI_GUID));\r
1019 StrPtr += 32;\r
1020\r
1021 //\r
1022 // Convert name string, e.g. name "ABCD" => "&NAME=0041004200430044"\r
1023 //\r
1024 StrCpy (StrPtr, L"&NAME=");\r
1025 StrPtr += 6;\r
1026 if (Name != NULL) {\r
1027 BufferSize = (NameStrLen * 4 + 1) * sizeof (CHAR16);\r
1028 UnicodeToConfigString (StrPtr, &BufferSize, Name);\r
1029 StrPtr += (NameStrLen * 4);\r
1030 }\r
1031\r
1032 StrCpy (StrPtr, L"&PATH=");\r
1033 StrPtr += 6;\r
1034 BufInReverseOrderToHexString (StrPtr, (UINT8 *) DevicePath, DevicePathSize);\r
1035\r
1036 return EFI_SUCCESS;\r
1037}\r
1038\r
1039/**\r
1040 Determines if the Routing data (Guid and Name) is correct in <ConfigHdr>.\r
1041\r
1042 @param ConfigString Either <ConfigRequest> or <ConfigResp>.\r
1043 @param StorageGuid GUID of the storage.\r
1044 @param StorageName Name of the stoarge.\r
1045\r
1046 @retval TRUE Routing information is correct in ConfigString.\r
1047 @retval FALSE Routing information is incorrect in ConfigString.\r
1048\r
1049**/\r
1050BOOLEAN\r
1051IsConfigHdrMatch (\r
1052 IN EFI_STRING ConfigString,\r
1053 IN EFI_GUID *StorageGuid, OPTIONAL\r
1054 IN CHAR16 *StorageName OPTIONAL\r
1055 )\r
1056{\r
1057 EFI_STATUS Status;\r
1058 BOOLEAN Match;\r
1059 EFI_GUID Guid;\r
1060 CHAR16 *Name;\r
1061 CHAR16 *StrPtr;\r
1062 UINTN BufferSize;\r
1063\r
1064 //\r
1065 // <ConfigHdr> ::=\r
1066 // GUID=<HexCh>32&NAME=<Char>NameStrLen&PATH=<HexChar>DevicePathStrLen <NULL>\r
1067 // | 5 | 32 | 6 | NameStrLen*4 | 6 | DevicePathStrLen | 1 |\r
1068 //\r
1069 if (StrLen (ConfigString) <= (5 + 32 + 6)) {\r
1070 return FALSE;\r
1071 }\r
1072\r
1073 //\r
1074 // Compare GUID\r
1075 //\r
1076 if (StorageGuid != NULL) {\r
1077\r
1078 StrPtr = ConfigString + 5 + 32;\r
1079 if (*StrPtr != L'&') {\r
1080 return FALSE;\r
1081 }\r
1082 *StrPtr = L'\0';\r
1083\r
1084 BufferSize = sizeof (EFI_GUID);\r
1085 Status = HexStringToBufInReverseOrder (\r
1086 (UINT8 *) &Guid,\r
1087 &BufferSize,\r
1088 ConfigString + 5\r
1089 );\r
1090 *StrPtr = L'&';\r
1091\r
1092 if (EFI_ERROR (Status)) {\r
1093 return FALSE;\r
1094 }\r
1095\r
1096 if (!CompareGuid (&Guid, StorageGuid)) {\r
1097 return FALSE;\r
1098 }\r
1099 }\r
1100\r
1101 //\r
1102 // Compare Name\r
1103 //\r
1104 Match = TRUE;\r
1105 if (StorageName != NULL) {\r
1106 StrPtr = ConfigString + 5 + 32 + 6;\r
1107 while (*StrPtr != L'\0' && *StrPtr != L'&') {\r
1108 StrPtr++;\r
1109 }\r
1110 if (*StrPtr != L'&') {\r
1111 return FALSE;\r
1112 }\r
1113\r
1114 *StrPtr = L'\0';\r
1115 BufferSize = (((UINTN) StrPtr) - ((UINTN) &ConfigString[5 + 32 + 6])) / 4 + sizeof (CHAR16);\r
1116 Name = AllocatePool (BufferSize);\r
1117 ASSERT (Name != NULL);\r
1118 Status = ConfigStringToUnicode (\r
1119 Name,\r
1120 &BufferSize,\r
1121 ConfigString + 5 + 32 + 6\r
1122 );\r
1123 *StrPtr = L'&';\r
1124\r
1125 if (EFI_ERROR (Status) || (StrCmp (Name, StorageName) != 0)) {\r
1126 Match = FALSE;\r
1127 }\r
fca1cc71 1128 FreePool (Name);\r
08e4b3cf 1129 }\r
1130\r
1131 return Match;\r
1132}\r
1133\r
1134/**\r
1135 Search BlockName "&OFFSET=Offset&WIDTH=Width" in a string.\r
1136\r
1137 @param String The string to be searched in.\r
1138 @param Offset Offset in BlockName.\r
1139 @param Width Width in BlockName.\r
1140\r
1141 @retval TRUE Block name found.\r
1142 @retval FALSE Block name not found.\r
1143\r
1144**/\r
1145BOOLEAN\r
1146EFIAPI\r
1147FindBlockName (\r
1148 IN OUT CHAR16 *String,\r
1149 IN UINTN Offset,\r
1150 IN UINTN Width\r
1151 )\r
1152{\r
1153 EFI_STATUS Status;\r
1154 UINTN Data;\r
1155 UINTN BufferSize;\r
1156 UINTN ConvertedStrLen;\r
1157\r
1158 while ((String = StrStr (String, L"&OFFSET=")) != NULL) {\r
1159 //\r
1160 // Skip '&OFFSET='\r
1161 //\r
1162 String = String + 8;\r
1163\r
1164 Data = 0;\r
1165 BufferSize = sizeof (UINTN);\r
1166 Status = HexStringToBuf ((UINT8 *) &Data, &BufferSize, String, &ConvertedStrLen);\r
1167 if (EFI_ERROR (Status)) {\r
1168 return FALSE;\r
1169 }\r
1170 String = String + ConvertedStrLen;\r
1171\r
1172 if (Data != Offset) {\r
1173 continue;\r
1174 }\r
1175\r
1176 if (StrnCmp (String, L"&WIDTH=", 7) != 0) {\r
1177 return FALSE;\r
1178 }\r
1179 String = String + 7;\r
1180\r
1181 Data = 0;\r
1182 BufferSize = sizeof (UINTN);\r
1183 Status = HexStringToBuf ((UINT8 *) &Data, &BufferSize, String, &ConvertedStrLen);\r
1184 if (EFI_ERROR (Status)) {\r
1185 return FALSE;\r
1186 }\r
1187 if (Data == Width) {\r
1188 return TRUE;\r
1189 }\r
1190\r
1191 String = String + ConvertedStrLen;\r
1192 }\r
1193\r
1194 return FALSE;\r
1195}\r
1196\r
1197\r
1198/**\r
1199 This routine is invoked by ConfigAccess.Callback() to retrived uncommitted data from Form Browser.\r
1200\r
1201 @param VariableGuid An optional field to indicate the target variable\r
1202 GUID name to use.\r
1203 @param VariableName An optional field to indicate the target\r
1204 human-readable variable name.\r
1205 @param BufferSize On input: Length in bytes of buffer to hold\r
1206 retrived data. On output: If return\r
1207 EFI_BUFFER_TOO_SMALL, containg length of buffer\r
1208 desired.\r
1209 @param Buffer Buffer to hold retrived data.\r
1210\r
1211 @retval EFI_SUCCESS Operation completes successfully.\r
1212 @retval EFI_BUFFER_TOO_SMALL The intput buffer is too small.\r
1213 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.\r
1214\r
1215**/\r
1216EFI_STATUS\r
1217EFIAPI\r
1218GetBrowserData (\r
1219 IN CONST EFI_GUID *VariableGuid, OPTIONAL\r
1220 IN CONST CHAR16 *VariableName, OPTIONAL\r
1221 IN OUT UINTN *BufferSize,\r
1222 IN OUT UINT8 *Buffer\r
1223 )\r
1224{\r
1225 EFI_STATUS Status;\r
1226 CONST CHAR16 *ConfigHdr;\r
1227 CHAR16 *ConfigResp;\r
1228 CHAR16 *StringPtr;\r
1229 UINTN HeaderLen;\r
1230 UINTN BufferLen;\r
1231 CHAR16 *Progress;\r
1232\r
1233 //\r
1234 // Locate protocols for use\r
1235 //\r
1236 Status = LocateFormBrowser2Protocols ();\r
1237 if (EFI_ERROR (Status)) {\r
1238 return Status;\r
1239 }\r
1240\r
1241 //\r
1242 // Retrive formset storage data from Form Browser\r
1243 //\r
1244 ConfigHdr = mFakeConfigHdr;\r
1245 HeaderLen = StrLen (ConfigHdr);\r
1246 \r
1247 //\r
1248 // First try allocate 0x4000 buffer for the formet storage data.\r
1249 //\r
1250 BufferLen = 0x4000;\r
1251 ConfigResp = AllocateZeroPool (BufferLen + HeaderLen);\r
1252 if (ConfigResp == NULL) {\r
fa7b3168 1253 return EFI_OUT_OF_RESOURCES;\r
08e4b3cf 1254 }\r
1255\r
1256 StringPtr = ConfigResp + HeaderLen;\r
1257 *StringPtr = L'&';\r
1258 StringPtr++;\r
1259\r
1260 Status = mFormBrowser2->BrowserCallback (\r
1261 mFormBrowser2,\r
1262 &BufferLen,\r
1263 StringPtr,\r
1264 TRUE,\r
1265 VariableGuid,\r
1266 VariableName\r
1267 );\r
1268 if (Status == EFI_BUFFER_TOO_SMALL) {\r
fa7b3168 1269 FreePool (ConfigResp);\r
08e4b3cf 1270\r
1271 ConfigResp = AllocateZeroPool (BufferLen + HeaderLen);\r
1272 if (ConfigResp == NULL) {\r
1273 return EFI_OUT_OF_RESOURCES;\r
1274 }\r
1275\r
1276 StringPtr = ConfigResp + HeaderLen;\r
1277 *StringPtr = L'&';\r
1278 StringPtr++;\r
1279\r
1280 Status = mFormBrowser2->BrowserCallback (\r
1281 mFormBrowser2,\r
1282 &BufferLen,\r
1283 StringPtr,\r
1284 TRUE,\r
1285 VariableGuid,\r
1286 VariableName\r
1287 );\r
1288 }\r
1289 if (EFI_ERROR (Status)) {\r
1290 FreePool (ConfigResp);\r
1291 return Status;\r
1292 }\r
1293 CopyMem (ConfigResp, ConfigHdr, HeaderLen * sizeof (UINT16));\r
1294\r
1295 //\r
1296 // Convert <ConfigResp> to buffer data\r
1297 //\r
1298 Status = mIfrSupportLibHiiConfigRouting->ConfigToBlock (\r
1299 mIfrSupportLibHiiConfigRouting,\r
1300 ConfigResp,\r
1301 Buffer,\r
1302 BufferSize,\r
1303 &Progress\r
1304 );\r
1305 FreePool (ConfigResp);\r
1306\r
1307 return Status;\r
1308}\r
1309\r
1310\r
1311/**\r
1312 This routine is invoked by ConfigAccess.Callback() to update uncommitted data of Form Browser.\r
1313\r
1314 @param VariableGuid An optional field to indicate the target variable\r
1315 GUID name to use.\r
1316 @param VariableName An optional field to indicate the target\r
1317 human-readable variable name.\r
1318 @param BufferSize Length in bytes of buffer to hold retrived data.\r
1319 @param Buffer Buffer to hold retrived data.\r
1320 @param RequestElement An optional field to specify which part of the\r
1321 buffer data will be send back to Browser. If NULL,\r
1322 the whole buffer of data will be committed to\r
1323 Browser. <RequestElement> ::=\r
1324 &OFFSET=<Number>&WIDTH=<Number>*\r
1325\r
1326 @retval EFI_SUCCESS Operation completes successfully.\r
1327 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.\r
1328 @retval Other Updating Browser uncommitted data failed.\r
1329\r
1330**/\r
1331EFI_STATUS\r
1332EFIAPI\r
1333SetBrowserData (\r
1334 IN CONST EFI_GUID *VariableGuid, OPTIONAL\r
1335 IN CONST CHAR16 *VariableName, OPTIONAL\r
1336 IN UINTN BufferSize,\r
1337 IN CONST UINT8 *Buffer,\r
1338 IN CONST CHAR16 *RequestElement OPTIONAL\r
1339 )\r
1340{\r
1341 EFI_STATUS Status;\r
1342 CONST CHAR16 *ConfigHdr;\r
1343 CHAR16 *ConfigResp;\r
1344 CHAR16 *StringPtr;\r
1345 UINTN HeaderLen;\r
1346 UINTN BufferLen;\r
1347 CHAR16 *Progress;\r
1348 CHAR16 BlockName[33];\r
1349 CHAR16 *ConfigRequest;\r
1350 CONST CHAR16 *Request;\r
1351\r
1352 //\r
1353 // Locate protocols for use\r
1354 //\r
1355 Status = LocateFormBrowser2Protocols ();\r
1356 if (EFI_ERROR (Status)) {\r
1357 return Status;\r
1358 }\r
1359\r
1360 //\r
1361 // Prepare <ConfigRequest>\r
1362 //\r
1363 ConfigHdr = mFakeConfigHdr;\r
1364 HeaderLen = StrLen (ConfigHdr);\r
1365\r
1366 if (RequestElement == NULL) {\r
1367 //\r
1368 // RequestElement not specified, use "&OFFSET=0&WIDTH=<BufferSize>" as <BlockName>\r
1369 //\r
1370 BlockName[0] = L'\0';\r
1371 StrCpy (BlockName, L"&OFFSET=0&WIDTH=");\r
1372\r
1373 //\r
1374 // String lenghth of L"&OFFSET=0&WIDTH=" is 16\r
1375 //\r
1376 StringPtr = BlockName + 16;\r
1377 BufferLen = sizeof (BlockName) - (16 * sizeof (CHAR16));\r
1378 BufToHexString (StringPtr, &BufferLen, (UINT8 *) &BufferSize, sizeof (UINTN));\r
1379\r
1380 Request = BlockName;\r
1381 } else {\r
1382 Request = RequestElement;\r
1383 }\r
1384\r
1385 BufferLen = HeaderLen * sizeof (CHAR16) + StrSize (Request);\r
1386 ConfigRequest = AllocateZeroPool (BufferLen);\r
1387 if (ConfigRequest == NULL) {\r
1388 return EFI_OUT_OF_RESOURCES;\r
1389 }\r
1390\r
1391 CopyMem (ConfigRequest, ConfigHdr, HeaderLen * sizeof (CHAR16));\r
1392 StringPtr = ConfigRequest + HeaderLen;\r
1393 StrCpy (StringPtr, Request);\r
1394\r
1395 //\r
1396 // Convert buffer to <ConfigResp>\r
1397 //\r
1398 Status = mIfrSupportLibHiiConfigRouting->BlockToConfig (\r
1399 mIfrSupportLibHiiConfigRouting,\r
1400 ConfigRequest,\r
1401 Buffer,\r
1402 BufferSize,\r
1403 &ConfigResp,\r
1404 &Progress\r
1405 );\r
1406 if (EFI_ERROR (Status)) {\r
1407 FreePool (ConfigRequest);\r
1408 return Status;\r
1409 }\r
1410\r
1411 //\r
1412 // Skip <ConfigHdr> and '&'\r
1413 //\r
1414 StringPtr = ConfigResp + HeaderLen + 1;\r
1415\r
1416 //\r
1417 // Change uncommitted data in Browser\r
1418 //\r
1419 Status = mFormBrowser2->BrowserCallback (\r
1420 mFormBrowser2,\r
1421 &BufferSize,\r
1422 StringPtr,\r
1423 FALSE,\r
1424 VariableGuid,\r
1425 VariableName\r
1426 );\r
1427 FreePool (ConfigRequest);\r
1428 return Status;\r
1429}\r
ac7e320c
LG
1430\r
1431/**\r
1432 Test if a Unicode character is a hexadecimal digit. If true, the input\r
1433 Unicode character is converted to a byte. \r
1434\r
1435 This function tests if a Unicode character is a hexadecimal digit. If true, the input\r
1436 Unicode character is converted to a byte. For example, Unicode character\r
1437 L'A' will be converted to 0x0A. \r
1438\r
1439 If Digit is NULL, then ASSERT.\r
1440\r
1441 @param Digit The output hexadecimal digit.\r
1442\r
1443 @param Char The input Unicode character.\r
1444\r
1445 @retval TRUE Char is in the range of Hexadecimal number. Digit is updated\r
1446 to the byte value of the number.\r
1447 @retval FALSE Char is not in the range of Hexadecimal number. Digit is keep\r
1448 intact.\r
1449\r
1450**/\r
1451BOOLEAN\r
1452EFIAPI\r
1453IsHexDigit (\r
1454 OUT UINT8 *Digit,\r
1455 IN CHAR16 Char\r
1456 )\r
1457{\r
1458 ASSERT (Digit != NULL);\r
1459 \r
1460 if ((Char >= L'0') && (Char <= L'9')) {\r
1461 *Digit = (UINT8) (Char - L'0');\r
1462 return TRUE;\r
1463 }\r
1464\r
1465 if ((Char >= L'A') && (Char <= L'F')) {\r
1466 *Digit = (UINT8) (Char - L'A' + 0x0A);\r
1467 return TRUE;\r
1468 }\r
1469\r
1470 if ((Char >= L'a') && (Char <= L'f')) {\r
1471 *Digit = (UINT8) (Char - L'a' + 0x0A);\r
1472 return TRUE;\r
1473 }\r
1474\r
1475 return FALSE;\r
1476}\r
1477\r
1478/** \r
1479 Convert binary buffer to a Unicode String in a specified sequence. \r
1480\r
1481 This function converts bytes in the memory block pointed by Buffer to a Unicode String Str. \r
1482 Each byte will be represented by two Unicode characters. For example, byte 0xA1 will \r
1483 be converted into two Unicode character L'A' and L'1'. In the output String, the Unicode Character \r
1484 for the Most Significant Nibble will be put before the Unicode Character for the Least Significant\r
1485 Nibble. The output string for the buffer containing a single byte 0xA1 will be L"A1". \r
1486 For a buffer with multiple bytes, the Unicode character produced by the first byte will be put into the \r
1487 the last character in the output string. The one next to first byte will be put into the\r
1488 character before the last character. This rules applies to the rest of the bytes. The Unicode\r
1489 character by the last byte will be put into the first character in the output string. For example,\r
1490 the input buffer for a 64-bits unsigned integer 0x12345678abcdef1234 will be converted to\r
1491 a Unicode string equal to L"12345678abcdef1234".\r
1492\r
1493 @param String On input, String is pointed to the buffer allocated for the convertion.\r
1494 @param StringLen The Length of String buffer to hold the output String. The length must include the tailing '\0' character.\r
1495 The StringLen required to convert a N bytes Buffer will be a least equal to or greater \r
1496 than 2*N + 1.\r
1497 @param Buffer The pointer to a input buffer.\r
1498 @param BufferSizeInBytes Length in bytes of the input buffer.\r
1499\r
1500\r
1501 @retval EFI_SUCCESS The convertion is successful. All bytes in Buffer has been convert to the corresponding\r
1502 Unicode character and placed into the right place in String.\r
1503 @retval EFI_BUFFER_TOO_SMALL StringSizeInBytes is smaller than 2 * N + 1the number of bytes required to\r
1504 complete the convertion. \r
1505**/\r
1506RETURN_STATUS\r
1507EFIAPI\r
1508BufToHexString (\r
1509 IN OUT CHAR16 *String,\r
1510 IN OUT UINTN *StringLen,\r
1511 IN CONST UINT8 *Buffer,\r
1512 IN UINTN BufferSizeInBytes\r
1513 )\r
1514{\r
1515 UINTN Idx;\r
1516 UINT8 Byte;\r
1517 UINTN StrLen;\r
1518\r
1519 //\r
1520 // Make sure string is either passed or allocate enough.\r
1521 // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.\r
1522 // Plus the Unicode termination character.\r
1523 //\r
1524 StrLen = BufferSizeInBytes * 2;\r
1525 if (StrLen > ((*StringLen) - 1)) {\r
1526 *StringLen = StrLen + 1;\r
1527 return RETURN_BUFFER_TOO_SMALL;\r
1528 }\r
1529\r
1530 *StringLen = StrLen + 1;\r
1531 //\r
1532 // Ends the string.\r
1533 //\r
1534 String[StrLen] = L'\0'; \r
1535\r
1536 for (Idx = 0; Idx < BufferSizeInBytes; Idx++) {\r
1537 Byte = Buffer[Idx];\r
1538 String[StrLen - 1 - Idx * 2] = mIfrSupportLibHexStr [Byte & 0xF];\r
1539 String[StrLen - 2 - Idx * 2] = mIfrSupportLibHexStr [Byte >> 4];\r
1540 }\r
1541\r
1542 return RETURN_SUCCESS;\r
1543}\r
1544\r
1545\r
1546/**\r
1547 Convert a Unicode string consisting of hexadecimal characters to a output byte buffer.\r
1548\r
1549 This function converts a Unicode string consisting of characters in the range of Hexadecimal\r
1550 character (L'0' to L'9', L'A' to L'F' and L'a' to L'f') to a output byte buffer. The function will stop\r
1551 at the first non-hexadecimal character or the NULL character. The convertion process can be\r
1552 simply viewed as the reverse operations defined by BufToHexString. Two Unicode characters will be \r
1553 converted into one byte. The first Unicode character represents the Most Significant Nibble and the\r
1554 second Unicode character represents the Least Significant Nibble in the output byte. \r
1555 The first pair of Unicode characters represents the last byte in the output buffer. The second pair of Unicode \r
1556 characters represent the the byte preceding the last byte. This rule applies to the rest pairs of bytes. \r
1557 The last pair represent the first byte in the output buffer. \r
1558\r
1559 For example, a Unciode String L"12345678" will be converted into a buffer wil the following bytes \r
1560 (first byte is the byte in the lowest memory address): "0x78, 0x56, 0x34, 0x12".\r
1561\r
1562 If String has N valid hexadecimal characters for conversion, the caller must make sure Buffer is at least \r
1563 N/2 (if N is even) or (N+1)/2 (if N if odd) bytes. \r
1564\r
4a1102c9 1565 If either Buffer, BufferSizeInBytes or String is NULL, then ASSERT ().\r
1566\r
ac7e320c
LG
1567 @param Buffer The output buffer allocated by the caller.\r
1568 @param BufferSizeInBytes On input, the size in bytes of Buffer. On output, it is updated to \r
1569 contain the size of the Buffer which is actually used for the converstion.\r
1570 For Unicode string with 2*N hexadecimal characters (not including the \r
1571 tailing NULL character), N bytes of Buffer will be used for the output.\r
1572 @param String The input hexadecimal string.\r
1573 @param ConvertedStrLen The number of hexadecimal characters used to produce content in output\r
1574 buffer Buffer.\r
1575\r
1576 @retval RETURN_BUFFER_TOO_SMALL The input BufferSizeInBytes is too small to hold the output. BufferSizeInBytes\r
1577 will be updated to the size required for the converstion.\r
1578 @retval RETURN_SUCCESS The convertion is successful or the first Unicode character from String\r
1579 is hexadecimal. If ConvertedStrLen is not NULL, it is updated\r
1580 to the number of hexadecimal character used for the converstion.\r
1581**/\r
1582RETURN_STATUS\r
1583EFIAPI\r
1584HexStringToBuf (\r
1585 OUT UINT8 *Buffer, \r
1586 IN OUT UINTN *BufferSizeInBytes,\r
1587 IN CONST CHAR16 *String,\r
1588 OUT UINTN *ConvertedStrLen OPTIONAL\r
1589 )\r
1590{\r
1591 UINTN HexCnt;\r
1592 UINTN Idx;\r
1593 UINTN BufferLength;\r
1594 UINT8 Digit;\r
1595 UINT8 Byte;\r
1596\r
4a1102c9 1597 ASSERT (Buffer != NULL);\r
1598 ASSERT (BufferSizeInBytes != NULL);\r
1599 ASSERT (String != NULL);\r
1600\r
ac7e320c
LG
1601 //\r
1602 // Find out how many hex characters the string has.\r
1603 //\r
1604 for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, String[Idx]); Idx++, HexCnt++);\r
1605\r
1606 if (HexCnt == 0) {\r
1607 *ConvertedStrLen = 0;\r
1608 return RETURN_SUCCESS;\r
1609 }\r
1610 //\r
1611 // Two Unicode characters make up 1 buffer byte. Round up.\r
1612 //\r
1613 BufferLength = (HexCnt + 1) / 2; \r
1614\r
1615 //\r
1616 // Test if buffer is passed enough.\r
1617 //\r
1618 if (BufferLength > (*BufferSizeInBytes)) {\r
1619 *BufferSizeInBytes = BufferLength;\r
1620 return RETURN_BUFFER_TOO_SMALL;\r
1621 }\r
1622\r
1623 *BufferSizeInBytes = BufferLength;\r
1624\r
1625 for (Idx = 0; Idx < HexCnt; Idx++) {\r
1626\r
1627 IsHexDigit (&Digit, String[HexCnt - 1 - Idx]);\r
1628\r
1629 //\r
1630 // For odd charaters, write the lower nibble for each buffer byte,\r
1631 // and for even characters, the upper nibble.\r
1632 //\r
1633 if ((Idx & 1) == 0) {\r
1634 Byte = Digit;\r
1635 } else {\r
1636 Byte = Buffer[Idx / 2];\r
1637 Byte &= 0x0F;\r
1638 Byte = (UINT8) (Byte | Digit << 4);\r
1639 }\r
1640\r
1641 Buffer[Idx / 2] = Byte;\r
1642 }\r
1643\r
1644 if (ConvertedStrLen != NULL) {\r
1645 *ConvertedStrLen = HexCnt;\r
1646 }\r
1647\r
1648 return RETURN_SUCCESS;\r
1649}\r