]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Foundation/Library/Dxe/EfiUiLib/EfiUiLib.c
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Dxe / EfiUiLib / EfiUiLib.c
CommitLineData
3eb9473e 1/*++\r
2\r
4ea9375a
HT
3Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials \r
3eb9473e 5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13 EfiUiLib.c\r
14\r
15Abstract:\r
16 Collection of usefull UI functions.\r
17\r
18Revision History:\r
19\r
20--*/\r
21\r
22#include "EfiUiLib.h"\r
23\r
24#define IS_DIGIT(Ch) (((Ch) >= L'0') && ((Ch) <= L'9'))\r
25\r
26STATIC\r
27EFI_STATUS\r
28EfiStringToValue (\r
29 OUT UINT64 *Val,\r
30 IN CHAR16 *String,\r
31 OUT UINT8 *EndIdx OPTIONAL\r
32 )\r
33/*++\r
34\r
35Routine Description:\r
36 Parses and converts Unicode string to decimal value.\r
37 The returned value is 64-bit.\r
38 The string is expected in decimal format,\r
39 the string is parsed and format verified.\r
40 This function is missing from the libs. One day it maybe\r
41 replaced with a lib function when it'll become available.\r
42\r
43Arguments:\r
44 Val - pointer to the variable to store the value to\r
45 String - string that contains the value to parse and convert\r
46 EndIdx - index on which the parsing stopped. It points to the\r
47 first character that was not part of the returned Val.\r
48 It's valid only if the function returns success.\r
49 It's optional and it could be NULL.\r
50\r
51Returns:\r
52 EFI_SUCCESS - if successful\r
53 EFI_INVALID_PARAMETER - if String is in unexpected format\r
54\r
55--*/\r
56{\r
57 UINT8 i;\r
58 UINT64 TempVal;\r
59\r
60 TempVal = 0;\r
61 //\r
62 // Iterate upto 20 digits, only so many could fit in the UINT64\r
63 //\r
64 for (i = 0; i <= 20; i++) {\r
65 //\r
66 // test if the next character is not a digit\r
67 //\r
68 if (!IS_DIGIT (String[i])) {\r
69 //\r
70 // If here, there is no more digits,\r
71 // return with success if there was at least one to process\r
72 //\r
73 if (i == 0) {\r
74 break;\r
75 }\r
76\r
77 *Val = TempVal;\r
78\r
79 if (EndIdx != NULL) {\r
80 *EndIdx = i;\r
81 }\r
82\r
83 return EFI_SUCCESS;\r
84 }\r
85 //\r
86 // If here, there is a digit to process\r
87 //\r
88 TempVal = MultU64x32 (TempVal, 10) + String[i] - L'0';\r
89 }\r
90 //\r
91 // if here, there was some sort of format error\r
92 //\r
93 return EFI_INVALID_PARAMETER;\r
94}\r
95\r
96CHAR16 *\r
97StrHzToString (\r
98 OUT CHAR16 *String,\r
99 IN UINT64 Val\r
100 )\r
101/*++\r
102\r
103Routine Description:\r
104 Converts frequency in Hz to Unicode string. \r
105 Three significant digits are delivered. \r
106 Used for things like processor info display.\r
107\r
108Arguments:\r
109 String - string that will contain the frequency.\r
110 Val - value to convert, minimum is 100000 i.e., 0.1 MHz.\r
111\r
112--*/\r
113// GC_TODO: function comment is missing 'Returns:'\r
114{\r
115 CHAR16 HlpStr[8];\r
116 UINT32 i;\r
117 UINT32 IdxPoint;\r
118 UINT32 IdxUnits;\r
119 static CHAR16 *FreqUnits[] = { L" Hz", L" kHz", L" MHz", L" GHz", L" THz", L" PHz" };\r
120\r
121 //\r
122 // Normalize to 9999 or less.\r
123 //\r
124 i = 0;\r
125 while (Val >= 10000) {\r
126 Val = DivU64x32 (Val, 10, NULL);\r
127 i++;\r
128 }\r
129 //\r
130 // Make it rounded to the nearest, but only by\r
131 // a .3. This assures that .6 is not rounded.\r
132 //\r
133 if (Val >= 1000) {\r
134 Val += 3;\r
135 Val = DivU64x32 (Val, 10, NULL);\r
136 i++;\r
137 }\r
138\r
139 EfiValueToString (String, Val, 0, 0);\r
140\r
141 //\r
142 // Get rid of that cursed number!\r
143 //\r
144 if (!EfiStrCmp (&String[1], L"66")) {\r
145 String[2] = L'7';\r
146 }\r
147 //\r
148 // Compute index to the units substrings.\r
149 //\r
150 IdxUnits = (i + 2) / 3;\r
151\r
152 if (IdxUnits >= (sizeof (FreqUnits) / sizeof (FreqUnits)[0])) {\r
153 //\r
154 // Frequency is too high.\r
155 //\r
156 EfiStrCpy (String, L"OVERFLOW");\r
157 return String;\r
158 }\r
159 //\r
160 // Compute the position of the decimal point.\r
161 //\r
162 IdxPoint = i % 3;\r
163\r
164 //\r
165 // Test if decimal point needs to be inserted.\r
166 //\r
167 if (IdxPoint != 0) {\r
168 //\r
169 // Save the part after decimal point.\r
170 //\r
171 EfiStrCpy (HlpStr, &String[IdxPoint]);\r
172\r
173 //\r
174 // Insert the point.\r
175 //\r
176 String[IdxPoint] = L'.';\r
177\r
178 //\r
179 // Reattach the saved part.\r
180 //\r
181 EfiStrCpy (&String[IdxPoint + 1], HlpStr);\r
182\r
183 //\r
184 // Clear the insignificant zero.\r
185 //\r
186 if (String[3] == L'0') {\r
187 String[4 - IdxPoint] = L'\0';\r
188 }\r
189 }\r
190 //\r
191 // Attach units.\r
192 //\r
193 EfiStrCat (String, FreqUnits[IdxUnits]);\r
194\r
195 return String;\r
196}\r
197\r
198CHAR16 *\r
199StrBytesToString (\r
200 OUT CHAR16 *String,\r
201 IN UINT64 Val\r
202 )\r
203/*++\r
204\r
205Routine Description:\r
206 Converts size in bytes to Unicode string.\r
207 Used for memory/cache size display.\r
208\r
209Arguments:\r
210 String - string that will contain the value\r
211 Val - value to convert in bytes\r
212\r
213--*/\r
214// GC_TODO: function comment is missing 'Returns:'\r
215{\r
216 UINTN i;\r
217 UINTN Rem;\r
218 static CHAR16 *SizeUnits[] = { L" B", L" kB", L" MB", L" GB", L" TB", L" PB" };\r
219\r
220 for (i = 0; i < (sizeof (SizeUnits) / sizeof (SizeUnits)[0]); i++) {\r
221\r
222 DivU64x32 (Val, 1024, &Rem);\r
223\r
224 //\r
225 // Done if:\r
226 // 1. less than 1k\r
227 // 2. less than 8k and there are fractions of 1k\r
228 //\r
229 if ((Val < 1024) || ((Val < 8192) && (Rem != 0))) {\r
230\r
231 EfiValueToString (String, Val, 0, 0);\r
232\r
233 //\r
234 // attach units\r
235 //\r
236 EfiStrCat (String, SizeUnits[i]);\r
237 return String;\r
238 }\r
239 //\r
240 // prescale down by 1k with rounding to the nearest\r
241 //\r
242 Val = DivU64x32 (Val + 511, 1024, NULL);\r
243 }\r
244\r
245 EfiStrCpy (String, L"OVERFLOW");\r
246\r
247 return String;\r
248}\r
249\r
250CHAR16 *\r
251StrVersionToString (\r
252 OUT CHAR16 *String,\r
253 IN UINT8 Version\r
254 )\r
255/*++\r
256\r
257Routine Description:\r
258 Converts 8 bit version value to Unicode string.\r
259 The upper nibble contains the upper part, the lower nibble contains the minor part.\r
260 The output format is <major>.<minor>.\r
261\r
262Arguments:\r
263 String - string that will contain the value\r
264 Version - value to convert\r
265\r
266--*/\r
267// GC_TODO: function comment is missing 'Returns:'\r
268{\r
269 CHAR16 HlpStr[4];\r
270\r
271 EfiValueToString (String, 0x0F & Version, 0, 0);\r
272 EfiStrCat (String, L".");\r
273 EfiValueToString (HlpStr, 0x0F & (Version >> 4), 0, 0);\r
274 EfiStrCat (String, HlpStr);\r
275\r
276 return String;\r
277}\r
278\r
279CHAR16 *\r
280StrMacToString (\r
281 OUT CHAR16 *String,\r
282 IN EFI_MAC_ADDRESS *MacAddr,\r
283 IN UINT32 AddrSize\r
284 )\r
285/*++\r
286\r
287Routine Description:\r
288 Converts MAC address to Unicode string.\r
289 The value is 64-bit and the resulting string will be 12\r
290 digit hex number in pairs of digits separated by dashes.\r
291\r
292Arguments:\r
293 String - string that will contain the value\r
294 Val - value to convert\r
295\r
296--*/\r
297// GC_TODO: function comment is missing 'Returns:'\r
298// GC_TODO: MacAddr - add argument and description to function comment\r
299// GC_TODO: AddrSize - add argument and description to function comment\r
300{\r
301 UINT32 i;\r
302\r
303 for (i = 0; i < AddrSize; i++) {\r
304\r
305 EfiValueToHexStr (\r
306 &String[2 * i],\r
307 MacAddr->Addr[i] & 0xFF,\r
308 PREFIX_ZERO,\r
309 2\r
310 );\r
311 }\r
312 //\r
313 // Terminate the string.\r
314 //\r
315 String[2 * AddrSize] = L'\0';\r
316\r
317 return String;\r
318}\r
319\r
320CHAR16 *\r
321StrIp4AdrToString (\r
322 OUT CHAR16 *String,\r
323 IN EFI_IPv4_ADDRESS *Ip4Addr\r
324 )\r
325/*++\r
326\r
327Routine Description:\r
328 Converts IP v4 address to Unicode string.\r
329 The value is 64-bit and the resulting string will\r
330 be four decimal values 0-255 separated by dots.\r
331\r
332Arguments:\r
333 String - string that will contain the value\r
334 Ip4Addr - value to convert from\r
335\r
336--*/\r
337// GC_TODO: function comment is missing 'Returns:'\r
338{\r
339 INT32 i;\r
340 CHAR16 HlpStr[4];\r
341\r
342 String[0] = L'\0';\r
343\r
344 for (i = 0; i < 4; i++) {\r
345\r
346 EfiValueToString (HlpStr, Ip4Addr->Addr[i], 0, 0);\r
347 EfiStrCat (String, HlpStr);\r
348\r
349 if (i < 3) {\r
350 EfiStrCat (String, L".");\r
351 }\r
352 }\r
353\r
354 return String;\r
355}\r
356\r
357EFI_STATUS\r
358StrStringToIp4Adr (\r
359 OUT EFI_IPv4_ADDRESS *Ip4Addr,\r
360 IN CHAR16 *String\r
361 )\r
362/*++\r
363\r
364Routine Description:\r
365 Parses and converts Unicode string to IP v4 address.\r
366 The value will 64-bit.\r
367 The string must be four decimal values 0-255 separated by dots.\r
368 The string is parsed and format verified.\r
369\r
370Arguments:\r
371 Ip4Addr - pointer to the variable to store the value to\r
372 String - string that contains the value to parse and convert\r
373\r
374Returns:\r
375 EFI_SUCCESS - if successful\r
376 EFI_INVALID_PARAMETER - if String contains invalid IP v4 format\r
377\r
378--*/\r
379{\r
380 EFI_STATUS Status;\r
381\r
382 EFI_IPv4_ADDRESS RetVal;\r
383 UINT64 TempVal;\r
384 UINT8 Idx;\r
385 UINT8 i;\r
386\r
57d40fe2 387 Idx = 0;\r
388 TempVal = 0;\r
3eb9473e 389 //\r
390 // Iterate the decimal values separated by dots\r
391 //\r
392 for (i = 0; i < 4; i++) {\r
393 //\r
394 // get the value of a decimal\r
395 //\r
396 Status = EfiStringToValue (&TempVal, String, &Idx);\r
397 if ((EFI_ERROR (Status)) || (TempVal > 255)) {\r
398 break;\r
399 }\r
400\r
401 RetVal.Addr[i] = (UINT8) TempVal;\r
402 String += Idx;\r
403\r
404 //\r
405 // test if it is the last decimal value\r
406 //\r
407 if (i == 3) {\r
408 if (String[0] != L'\0') {\r
409 //\r
410 // the string must end with string termination character\r
411 //\r
412 break;\r
413 }\r
414\r
415 *Ip4Addr = RetVal;\r
416 return EFI_SUCCESS;\r
417 }\r
418 //\r
419 // Test for presence of a dot, it is required between the values\r
420 //\r
421 if (String++[0] != L'.') {\r
422 break;\r
423 }\r
424 }\r
425\r
426 return EFI_INVALID_PARAMETER;\r
427}\r
428\r
429CHAR16 *\r
430Ascii2Unicode (\r
431 OUT CHAR16 *UnicodeStr,\r
432 IN CHAR8 *AsciiStr\r
433 )\r
434/*++\r
435\r
436Routine Description:\r
437 Converts ASCII characters to Unicode.\r
438\r
439Arguments:\r
440 UnicodeStr - the Unicode string to be written to. The buffer must be large enough.\r
441 AsciiStr - The ASCII string to be converted.\r
442\r
443Returns:\r
444 The address to the Unicode string - same as UnicodeStr.\r
445\r
446--*/\r
447{\r
448 CHAR16 *Str;\r
449\r
450 Str = UnicodeStr;\r
451\r
452 while (TRUE) {\r
453\r
454 *(UnicodeStr++) = (CHAR16) *AsciiStr;\r
455\r
456 if (*(AsciiStr++) == '\0') {\r
457 return Str;\r
458 }\r
459 }\r
460}\r
461\r
462CHAR8 *\r
463Unicode2Ascii (\r
464 OUT CHAR8 *AsciiStr,\r
465 IN CHAR16 *UnicodeStr\r
466 )\r
467/*++\r
468\r
469Routine Description:\r
470 Converts ASCII characters to Unicode.\r
471 Assumes that the Unicode characters are only these defined in the ASCII set.\r
472\r
473Arguments:\r
474 AsciiStr - The ASCII string to be written to. The buffer must be large enough.\r
475 UnicodeStr - the Unicode string to be converted.\r
476\r
477Returns:\r
478 The address to the ASCII string - same as AsciiStr.\r
479\r
480--*/\r
481{\r
482 CHAR8 *Str;\r
483\r
484 Str = AsciiStr;\r
485\r
486 while (TRUE) {\r
487\r
488 *AsciiStr = (CHAR8) *(UnicodeStr++);\r
489\r
490 if (*(AsciiStr++) == '\0') {\r
491 return Str;\r
492 }\r
493 }\r
494}\r