]>
Commit | Line | Data |
---|---|---|
3eb9473e | 1 | /*++\r |
2 | \r | |
3 | Copyright (c) 2007, Intel Corporation \r | |
4 | All rights reserved. This program and the accompanying materials \r | |
5 | are licensed and made available under the terms and conditions of the BSD License \r | |
6 | which accompanies this distribution. The full text of the license may be found at \r | |
7 | http://opensource.org/licenses/bsd-license.php \r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
11 | \r | |
12 | Module Name:\r | |
13 | EfiUiLib.c\r | |
14 | \r | |
15 | Abstract:\r | |
16 | Collection of usefull UI functions.\r | |
17 | \r | |
18 | Revision 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 | |
26 | STATIC\r | |
27 | EFI_STATUS\r | |
28 | EfiStringToValue (\r | |
29 | OUT UINT64 *Val,\r | |
30 | IN CHAR16 *String,\r | |
31 | OUT UINT8 *EndIdx OPTIONAL\r | |
32 | )\r | |
33 | /*++\r | |
34 | \r | |
35 | Routine 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 | |
43 | Arguments:\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 | |
51 | Returns:\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 | |
96 | CHAR16 *\r | |
97 | StrHzToString (\r | |
98 | OUT CHAR16 *String,\r | |
99 | IN UINT64 Val\r | |
100 | )\r | |
101 | /*++\r | |
102 | \r | |
103 | Routine 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 | |
108 | Arguments:\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 | |
198 | CHAR16 *\r | |
199 | StrBytesToString (\r | |
200 | OUT CHAR16 *String,\r | |
201 | IN UINT64 Val\r | |
202 | )\r | |
203 | /*++\r | |
204 | \r | |
205 | Routine Description:\r | |
206 | Converts size in bytes to Unicode string.\r | |
207 | Used for memory/cache size display.\r | |
208 | \r | |
209 | Arguments:\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 | |
250 | CHAR16 *\r | |
251 | StrVersionToString (\r | |
252 | OUT CHAR16 *String,\r | |
253 | IN UINT8 Version\r | |
254 | )\r | |
255 | /*++\r | |
256 | \r | |
257 | Routine 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 | |
262 | Arguments:\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 | |
279 | CHAR16 *\r | |
280 | StrMacToString (\r | |
281 | OUT CHAR16 *String,\r | |
282 | IN EFI_MAC_ADDRESS *MacAddr,\r | |
283 | IN UINT32 AddrSize\r | |
284 | )\r | |
285 | /*++\r | |
286 | \r | |
287 | Routine 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 | |
292 | Arguments:\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 | |
320 | CHAR16 *\r | |
321 | StrIp4AdrToString (\r | |
322 | OUT CHAR16 *String,\r | |
323 | IN EFI_IPv4_ADDRESS *Ip4Addr\r | |
324 | )\r | |
325 | /*++\r | |
326 | \r | |
327 | Routine 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 | |
332 | Arguments:\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 | |
357 | EFI_STATUS\r | |
358 | StrStringToIp4Adr (\r | |
359 | OUT EFI_IPv4_ADDRESS *Ip4Addr,\r | |
360 | IN CHAR16 *String\r | |
361 | )\r | |
362 | /*++\r | |
363 | \r | |
364 | Routine 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 | |
370 | Arguments:\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 | |
374 | Returns:\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 | |
387 | //\r | |
388 | // Iterate the decimal values separated by dots\r | |
389 | //\r | |
390 | for (i = 0; i < 4; i++) {\r | |
391 | //\r | |
392 | // get the value of a decimal\r | |
393 | //\r | |
394 | Status = EfiStringToValue (&TempVal, String, &Idx);\r | |
395 | if ((EFI_ERROR (Status)) || (TempVal > 255)) {\r | |
396 | break;\r | |
397 | }\r | |
398 | \r | |
399 | RetVal.Addr[i] = (UINT8) TempVal;\r | |
400 | String += Idx;\r | |
401 | \r | |
402 | //\r | |
403 | // test if it is the last decimal value\r | |
404 | //\r | |
405 | if (i == 3) {\r | |
406 | if (String[0] != L'\0') {\r | |
407 | //\r | |
408 | // the string must end with string termination character\r | |
409 | //\r | |
410 | break;\r | |
411 | }\r | |
412 | \r | |
413 | *Ip4Addr = RetVal;\r | |
414 | return EFI_SUCCESS;\r | |
415 | }\r | |
416 | //\r | |
417 | // Test for presence of a dot, it is required between the values\r | |
418 | //\r | |
419 | if (String++[0] != L'.') {\r | |
420 | break;\r | |
421 | }\r | |
422 | }\r | |
423 | \r | |
424 | return EFI_INVALID_PARAMETER;\r | |
425 | }\r | |
426 | \r | |
427 | CHAR16 *\r | |
428 | Ascii2Unicode (\r | |
429 | OUT CHAR16 *UnicodeStr,\r | |
430 | IN CHAR8 *AsciiStr\r | |
431 | )\r | |
432 | /*++\r | |
433 | \r | |
434 | Routine Description:\r | |
435 | Converts ASCII characters to Unicode.\r | |
436 | \r | |
437 | Arguments:\r | |
438 | UnicodeStr - the Unicode string to be written to. The buffer must be large enough.\r | |
439 | AsciiStr - The ASCII string to be converted.\r | |
440 | \r | |
441 | Returns:\r | |
442 | The address to the Unicode string - same as UnicodeStr.\r | |
443 | \r | |
444 | --*/\r | |
445 | {\r | |
446 | CHAR16 *Str;\r | |
447 | \r | |
448 | Str = UnicodeStr;\r | |
449 | \r | |
450 | while (TRUE) {\r | |
451 | \r | |
452 | *(UnicodeStr++) = (CHAR16) *AsciiStr;\r | |
453 | \r | |
454 | if (*(AsciiStr++) == '\0') {\r | |
455 | return Str;\r | |
456 | }\r | |
457 | }\r | |
458 | }\r | |
459 | \r | |
460 | CHAR8 *\r | |
461 | Unicode2Ascii (\r | |
462 | OUT CHAR8 *AsciiStr,\r | |
463 | IN CHAR16 *UnicodeStr\r | |
464 | )\r | |
465 | /*++\r | |
466 | \r | |
467 | Routine Description:\r | |
468 | Converts ASCII characters to Unicode.\r | |
469 | Assumes that the Unicode characters are only these defined in the ASCII set.\r | |
470 | \r | |
471 | Arguments:\r | |
472 | AsciiStr - The ASCII string to be written to. The buffer must be large enough.\r | |
473 | UnicodeStr - the Unicode string to be converted.\r | |
474 | \r | |
475 | Returns:\r | |
476 | The address to the ASCII string - same as AsciiStr.\r | |
477 | \r | |
478 | --*/\r | |
479 | {\r | |
480 | CHAR8 *Str;\r | |
481 | \r | |
482 | Str = AsciiStr;\r | |
483 | \r | |
484 | while (TRUE) {\r | |
485 | \r | |
486 | *AsciiStr = (CHAR8) *(UnicodeStr++);\r | |
487 | \r | |
488 | if (*(AsciiStr++) == '\0') {\r | |
489 | return Str;\r | |
490 | }\r | |
491 | }\r | |
492 | }\r |