summaryrefslogtreecommitdiff
path: root/EdkCompatibilityPkg/Foundation/Library/Dxe/EfiUiLib/EfiUiLib.c
blob: 9d4260768c3f10569c94a25c641d9ff86e494457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*++

Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:
  EfiUiLib.c

Abstract:
  Collection of usefull UI functions.

Revision History:

--*/

#include "EfiUiLib.h"

#define IS_DIGIT(Ch)  (((Ch) >= L'0') && ((Ch) <= L'9'))

EFI_STATUS
EfiStringToValue (
  OUT UINT64        *Val,
  IN  CHAR16        *String,
  OUT UINT8         *EndIdx OPTIONAL
  )
/*++

Routine Description:
  Parses and converts Unicode string to decimal value.
  The returned value is 64-bit.
  The string is expected in decimal format,
  the string is parsed and format verified.

Arguments:
  Val    - pointer to the variable to store the value to
  String - string that contains the value to parse and convert
  EndIdx - index on which the parsing stopped. It points to the
           first character that was not part of the returned Val.
           It's valid only if the function returns success.
           It's optional and it could be NULL.

Returns:
  EFI_SUCCESS           - if successful
  EFI_INVALID_PARAMETER - if String is in unexpected format

--*/
{
  UINT8   i;
  UINT64  TempVal;

  TempVal = 0;
  //
  // Iterate upto 20 digits, only so many could fit in the UINT64
  //
  for (i = 0; i <= 20; i++) {
    //
    // test if the next character is not a digit
    //
    if (!IS_DIGIT (String[i])) {
      //
      // If here, there is no more digits,
      // return with success if there was at least one to process
      //
      if (i == 0) {
        break;
      }

      *Val = TempVal;

      if (EndIdx != NULL) {
        *EndIdx = i;
      }

      return EFI_SUCCESS;
    }
    //
    // If here, there is a digit to process
    //
    TempVal = MultU64x32 (TempVal, 10) + String[i] - L'0';
  }
  //
  // if here, there was some sort of format error
  //
  return EFI_INVALID_PARAMETER;
}

CHAR16 *
StrHzToString (
  OUT CHAR16          *String,
  IN  UINT64          Val
  )
/*++

Routine Description:
  Converts frequency in Hz to Unicode string. 
  Three significant digits are delivered. 
  Used for things like processor info display.

Arguments:
  String - string that will contain the frequency.
  Val    - value to convert, minimum is  100000 i.e., 0.1 MHz.

--*/
// GC_TODO: function comment is missing 'Returns:'
{
  CHAR16        HlpStr[8];
  UINT32        i;
  UINT32        IdxPoint;
  UINT32        IdxUnits;
  static CHAR16 *FreqUnits[] = { L" Hz", L" kHz", L" MHz", L" GHz", L" THz", L" PHz" };

  //
  // Normalize to 9999 or less.
  //
  i = 0;
  while (Val >= 10000) {
    Val = DivU64x32 (Val, 10, NULL);
    i++;
  }
  //
  // Make it rounded to the nearest, but only by
  // a .3. This assures that .6 is not rounded.
  //
  if (Val >= 1000) {
    Val += 3;
    Val = DivU64x32 (Val, 10, NULL);
    i++;
  }

  EfiValueToString (String, Val, 0, 0);

  //
  // Get rid of that cursed number!
  //
  if (!EfiStrCmp (&String[1], L"66")) {
    String[2] = L'7';
  }
  //
  // Compute index to the units substrings.
  //
  IdxUnits = (i + 2) / 3;

  if (IdxUnits >= (sizeof (FreqUnits) / sizeof (FreqUnits)[0])) {
    //
    // Frequency is too high.
    //
    EfiStrCpy (String, L"OVERFLOW");
    return String;
  }
  //
  // Compute the position of the decimal point.
  //
  IdxPoint = i % 3;

  //
  // Test if decimal point needs to be inserted.
  //
  if (IdxPoint != 0) {
    //
    // Save the part after decimal point.
    //
    EfiStrCpy (HlpStr, &String[IdxPoint]);

    //
    // Insert the point.
    //
    String[IdxPoint] = L'.';

    //
    // Reattach the saved part.
    //
    EfiStrCpy (&String[IdxPoint + 1], HlpStr);

    //
    // Clear the insignificant zero.
    //
    if (String[3] == L'0') {
      String[4 - IdxPoint] = L'\0';
    }
  }
  //
  // Attach units.
  //
  EfiStrCat (String, FreqUnits[IdxUnits]);

  return String;
}

CHAR16 *
StrBytesToString (
  OUT CHAR16          *String,
  IN  UINT64          Val
  )
/*++

Routine Description:
  Converts size in bytes to Unicode string.
  Used for memory/cache size display.

Arguments:
  String - string that will contain the value
  Val    - value to convert in bytes

--*/
// GC_TODO: function comment is missing 'Returns:'
{
  UINTN         i;
  UINTN         Rem;
  static CHAR16 *SizeUnits[] = { L" B", L" kB", L" MB", L" GB", L" TB", L" PB" };

  for (i = 0; i < (sizeof (SizeUnits) / sizeof (SizeUnits)[0]); i++) {

    DivU64x32 (Val, 1024, &Rem);

    //
    // Done if:
    // 1. less than 1k
    // 2. less than 8k and there are fractions of 1k
    //
    if ((Val < 1024) || ((Val < 8192) && (Rem != 0))) {

      EfiValueToString (String, Val, 0, 0);

      //
      // attach units
      //
      EfiStrCat (String, SizeUnits[i]);
      return String;
    }
    //
    // prescale down by 1k with rounding to the nearest
    //
    Val = DivU64x32 (Val + 511, 1024, NULL);
  }

  EfiStrCpy (String, L"OVERFLOW");

  return String;
}

CHAR16 *
StrVersionToString (
  OUT CHAR16          *String,
  IN  UINT8           Version
  )
/*++

Routine Description:
  Converts 8 bit version value to Unicode string.
  The upper nibble contains the upper part, the lower nibble contains the minor part.
  The output format is <major>.<minor>.

Arguments:
  String  - string that will contain the value
  Version - value to convert

--*/
// GC_TODO: function comment is missing 'Returns:'
{
  CHAR16  HlpStr[4];

  EfiValueToString (String, 0x0F & Version, 0, 0);
  EfiStrCat (String, L".");
  EfiValueToString (HlpStr, 0x0F & (Version >> 4), 0, 0);
  EfiStrCat (String, HlpStr);

  return String;
}

CHAR16 *
StrMacToString (
  OUT CHAR16              *String,
  IN  EFI_MAC_ADDRESS     *MacAddr,
  IN  UINT32              AddrSize
  )
/*++

Routine Description:
  Converts MAC address to Unicode string.
  The value is 64-bit and the resulting string will be 12
  digit hex number in pairs of digits separated by dashes.

Arguments:
  String - string that will contain the value
  Val    - value to convert

--*/
// GC_TODO: function comment is missing 'Returns:'
// GC_TODO:    MacAddr - add argument and description to function comment
// GC_TODO:    AddrSize - add argument and description to function comment
{
  UINT32  i;

  for (i = 0; i < AddrSize; i++) {

    EfiValueToHexStr (
      &String[2 * i],
      MacAddr->Addr[i] & 0xFF,
      PREFIX_ZERO,
      2
      );
  }
  //
  // Terminate the string.
  //
  String[2 * AddrSize] = L'\0';

  return String;
}

CHAR16 *
StrIp4AdrToString (
  OUT CHAR16             *String,
  IN  EFI_IPv4_ADDRESS   *Ip4Addr
  )
/*++

Routine Description:
  Converts IP v4 address to Unicode string.
  The value is 64-bit and the resulting string will
  be four decimal values 0-255 separated by dots.

Arguments:
  String  - string that will contain the value
  Ip4Addr - value to convert from

--*/
// GC_TODO: function comment is missing 'Returns:'
{
  INT32   i;
  CHAR16  HlpStr[4];

  String[0] = L'\0';

  for (i = 0; i < 4; i++) {

    EfiValueToString (HlpStr, Ip4Addr->Addr[i], 0, 0);
    EfiStrCat (String, HlpStr);

    if (i < 3) {
      EfiStrCat (String, L".");
    }
  }

  return String;
}

EFI_STATUS
StrStringToIp4Adr (
  OUT EFI_IPv4_ADDRESS   *Ip4Addr,
  IN  CHAR16             *String
  )
/*++

Routine Description:
  Parses and converts Unicode string to IP v4 address.
  The value will 64-bit.
  The string must be four decimal values 0-255 separated by dots.
  The string is parsed and format verified.

Arguments:
  Ip4Addr - pointer to the variable to store the value to
  String  - string that contains the value to parse and convert

Returns:
  EFI_SUCCESS           - if successful
  EFI_INVALID_PARAMETER - if String contains invalid IP v4 format

--*/
{
  EFI_STATUS        Status;

  EFI_IPv4_ADDRESS  RetVal;
  UINT64            TempVal;
  UINT8             Idx;
  UINT8             i;

  Idx = 0;
  TempVal = 0;
  //
  // Iterate the decimal values separated by dots
  //
  for (i = 0; i < 4; i++) {
    //
    // get the value of a decimal
    //
    Status = EfiStringToValue (&TempVal, String, &Idx);
    if ((EFI_ERROR (Status)) || (TempVal > 255)) {
      break;
    }

    RetVal.Addr[i] = (UINT8) TempVal;
    String += Idx;

    //
    // test if it is the last decimal value
    //
    if (i == 3) {
      if (String[0] != L'\0') {
        //
        // the string must end with string termination character
        //
        break;
      }

      *Ip4Addr = RetVal;
      return EFI_SUCCESS;
    }
    //
    // Test for presence of a dot, it is required between the values
    //
    if (String++[0] != L'.') {
      break;
    }
  }

  return EFI_INVALID_PARAMETER;
}

CHAR16 *
Ascii2Unicode (
  OUT CHAR16         *UnicodeStr,
  IN  CHAR8          *AsciiStr
  )
/*++

Routine Description:
  Converts ASCII characters to Unicode.

Arguments:
  UnicodeStr - the Unicode string to be written to. The buffer must be large enough.
  AsciiStr   - The ASCII string to be converted.

Returns:
  The address to the Unicode string - same as UnicodeStr.

--*/
{
  CHAR16  *Str;

  Str = UnicodeStr;

  while (TRUE) {

    *(UnicodeStr++) = (CHAR16) *AsciiStr;

    if (*(AsciiStr++) == '\0') {
      return Str;
    }
  }
}

CHAR8 *
Unicode2Ascii (
  OUT CHAR8          *AsciiStr,
  IN  CHAR16         *UnicodeStr
  )
/*++

Routine Description:
  Converts ASCII characters to Unicode.
  Assumes that the Unicode characters are only these defined in the ASCII set.

Arguments:
  AsciiStr   - The ASCII string to be written to. The buffer must be large enough.
  UnicodeStr - the Unicode string to be converted.

Returns:
  The address to the ASCII string - same as AsciiStr.

--*/
{
  CHAR8 *Str;

  Str = AsciiStr;

  while (TRUE) {

    *AsciiStr = (CHAR8) *(UnicodeStr++);

    if (*(AsciiStr++) == '\0') {
      return Str;
    }
  }
}