Home | History | Annotate | Download | only in Common
      1 /*++
      2 
      3 Copyright (c) 2004, Intel Corporation. All rights reserved.<BR>
      4 This program and the accompanying materials
      5 are licensed and made available under the terms and conditions of the BSD License
      6 which accompanies this distribution.  The full text of the license may be found at
      7 http://opensource.org/licenses/bsd-license.php
      8 
      9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     11 
     12 Module Name:
     13 
     14   CommonLib.c
     15 
     16 Abstract:
     17 
     18   Common Library Functions
     19 
     20 --*/
     21 
     22 #include "TianoCommon.h"
     23 #include "PeiHob.h"
     24 #include <stdio.h>
     25 #include <string.h>
     26 #include <stdlib.h>
     27 #include "CommonLib.h"
     28 
     29 VOID
     30 PeiZeroMem (
     31   IN VOID   *Buffer,
     32   IN UINTN  Size
     33   )
     34 /*++
     35 
     36 Routine Description:
     37 
     38   Set Buffer to zero for Size bytes.
     39 
     40 Arguments:
     41 
     42   Buffer  - Memory to set.
     43 
     44   Size    - Number of bytes to set
     45 
     46 Returns:
     47 
     48   None
     49 
     50 --*/
     51 {
     52   INT8  *Ptr;
     53 
     54   Ptr = Buffer;
     55   while (Size--) {
     56     *(Ptr++) = 0;
     57   }
     58 }
     59 
     60 VOID
     61 PeiCopyMem (
     62   IN VOID   *Destination,
     63   IN VOID   *Source,
     64   IN UINTN  Length
     65   )
     66 /*++
     67 
     68 Routine Description:
     69 
     70   Copy Length bytes from Source to Destination.
     71 
     72 Arguments:
     73 
     74   Destination - Target of copy
     75 
     76   Source      - Place to copy from
     77 
     78   Length      - Number of bytes to copy
     79 
     80 Returns:
     81 
     82   None
     83 
     84 --*/
     85 {
     86   CHAR8 *Destination8;
     87   CHAR8 *Source8;
     88 
     89   Destination8  = Destination;
     90   Source8       = Source;
     91   while (Length--) {
     92     *(Destination8++) = *(Source8++);
     93   }
     94 }
     95 
     96 VOID
     97 ZeroMem (
     98   IN VOID   *Buffer,
     99   IN UINTN  Size
    100   )
    101 {
    102   PeiZeroMem (Buffer, Size);
    103 }
    104 
    105 VOID
    106 CopyMem (
    107   IN VOID   *Destination,
    108   IN VOID   *Source,
    109   IN UINTN  Length
    110   )
    111 {
    112   PeiCopyMem (Destination, Source, Length);
    113 }
    114 
    115 INTN
    116 CompareGuid (
    117   IN EFI_GUID     *Guid1,
    118   IN EFI_GUID     *Guid2
    119   )
    120 /*++
    121 
    122 Routine Description:
    123 
    124   Compares to GUIDs
    125 
    126 Arguments:
    127 
    128   Guid1 - guid to compare
    129   Guid2 - guid to compare
    130 
    131 Returns:
    132   =  0  if Guid1 == Guid2
    133   != 0  if Guid1 != Guid2
    134 
    135 --*/
    136 {
    137   INT32 *g1;
    138   INT32 *g2;
    139   INT32 r;
    140 
    141   //
    142   // Compare 32 bits at a time
    143   //
    144   g1  = (INT32 *) Guid1;
    145   g2  = (INT32 *) Guid2;
    146 
    147   r   = g1[0] - g2[0];
    148   r |= g1[1] - g2[1];
    149   r |= g1[2] - g2[2];
    150   r |= g1[3] - g2[3];
    151 
    152   return r;
    153 }
    154 
    155 EFI_STATUS
    156 GetFileImage (
    157   IN CHAR8    *InputFileName,
    158   OUT CHAR8   **InputFileImage,
    159   OUT UINT32  *BytesRead
    160   )
    161 /*++
    162 
    163 Routine Description:
    164 
    165   This function opens a file and reads it into a memory buffer.  The function
    166   will allocate the memory buffer and returns the size of the buffer.
    167 
    168 Arguments:
    169 
    170   InputFileName     The name of the file to read.
    171   InputFileImage    A pointer to the memory buffer.
    172   BytesRead         The size of the memory buffer.
    173 
    174 Returns:
    175 
    176   EFI_SUCCESS              The function completed successfully.
    177   EFI_INVALID_PARAMETER    One of the input parameters was invalid.
    178   EFI_ABORTED              An error occurred.
    179   EFI_OUT_OF_RESOURCES     No resource to complete operations.
    180 
    181 --*/
    182 {
    183   FILE    *InputFile;
    184   UINT32  FileSize;
    185 
    186   //
    187   // Verify input parameters.
    188   //
    189   if (InputFileName == NULL || strlen (InputFileName) == 0 || InputFileImage == NULL) {
    190     return EFI_INVALID_PARAMETER;
    191   }
    192   //
    193   // Open the file and copy contents into a memory buffer.
    194   //
    195   //
    196   // Open the file
    197   //
    198   InputFile = fopen (InputFileName, "rb");
    199   if (InputFile == NULL) {
    200     printf ("ERROR: Could not open input file \"%s\".\n", InputFileName);
    201     return EFI_ABORTED;
    202   }
    203   //
    204   // Go to the end so that we can determine the file size
    205   //
    206   if (fseek (InputFile, 0, SEEK_END)) {
    207     printf ("ERROR: System error reading input file \"%s\".\n", InputFileName);
    208     fclose (InputFile);
    209     return EFI_ABORTED;
    210   }
    211   //
    212   // Get the file size
    213   //
    214   FileSize = ftell (InputFile);
    215   if (FileSize == -1) {
    216     printf ("ERROR: System error parsing input file \"%s\".\n", InputFileName);
    217     fclose (InputFile);
    218     return EFI_ABORTED;
    219   }
    220   //
    221   // Allocate a buffer
    222   //
    223   *InputFileImage = malloc (FileSize);
    224   if (*InputFileImage == NULL) {
    225     fclose (InputFile);
    226     return EFI_OUT_OF_RESOURCES;
    227   }
    228   //
    229   // Reset to the beginning of the file
    230   //
    231   if (fseek (InputFile, 0, SEEK_SET)) {
    232     printf ("ERROR: System error reading input file \"%s\".\n", InputFileName);
    233     fclose (InputFile);
    234     free (*InputFileImage);
    235     *InputFileImage = NULL;
    236     return EFI_ABORTED;
    237   }
    238   //
    239   // Read all of the file contents.
    240   //
    241   *BytesRead = fread (*InputFileImage, sizeof (UINT8), FileSize, InputFile);
    242   if (*BytesRead != sizeof (UINT8) * FileSize) {
    243     printf ("ERROR: Reading file \"%s\"%i.\n", InputFileName);
    244     fclose (InputFile);
    245     free (*InputFileImage);
    246     *InputFileImage = NULL;
    247     return EFI_ABORTED;
    248   }
    249   //
    250   // Close the file
    251   //
    252   fclose (InputFile);
    253 
    254   return EFI_SUCCESS;
    255 }
    256 
    257 UINT8
    258 CalculateChecksum8 (
    259   IN UINT8        *Buffer,
    260   IN UINTN        Size
    261   )
    262 /*++
    263 
    264 Routine Description:
    265 
    266   This function calculates the value needed for a valid UINT8 checksum
    267 
    268 Arguments:
    269 
    270   Buffer      Pointer to buffer containing byte data of component.
    271   Size        Size of the buffer
    272 
    273 Returns:
    274 
    275   The 8 bit checksum value needed.
    276 
    277 --*/
    278 {
    279   return (UINT8) (0x100 - CalculateSum8 (Buffer, Size));
    280 }
    281 
    282 UINT8
    283 CalculateSum8 (
    284   IN UINT8  *Buffer,
    285   IN UINT32 Size
    286   )
    287 /*++
    288 
    289 Routine Description::
    290 
    291   This function calculates the UINT8 sum for the requested region.
    292 
    293 Arguments:
    294 
    295   Buffer      Pointer to buffer containing byte data of component.
    296   Size        Size of the buffer
    297 
    298 Returns:
    299 
    300   The 8 bit checksum value needed.
    301 
    302 --*/
    303 {
    304   UINTN Index;
    305   UINT8 Sum;
    306 
    307   Sum = 0;
    308 
    309   //
    310   // Perform the byte sum for buffer
    311   //
    312   for (Index = 0; Index < Size; Index++) {
    313     Sum = (UINT8) (Sum + Buffer[Index]);
    314   }
    315 
    316   return Sum;
    317 }
    318 
    319 UINT16
    320 CalculateChecksum16 (
    321   IN UINT16       *Buffer,
    322   IN UINTN        Size
    323   )
    324 /*++
    325 
    326 Routine Description::
    327 
    328   This function calculates the value needed for a valid UINT16 checksum
    329 
    330 Arguments:
    331 
    332   Buffer      Pointer to buffer containing byte data of component.
    333   Size        Size of the buffer
    334 
    335 Returns:
    336 
    337   The 16 bit checksum value needed.
    338 
    339 --*/
    340 {
    341   return (UINT16) (0x10000 - CalculateSum16 (Buffer, Size));
    342 }
    343 
    344 UINT16
    345 CalculateSum16 (
    346   IN UINT16       *Buffer,
    347   IN UINTN        Size
    348   )
    349 /*++
    350 
    351 Routine Description:
    352 
    353   This function calculates the UINT16 sum for the requested region.
    354 
    355 Arguments:
    356 
    357   Buffer      Pointer to buffer containing byte data of component.
    358   Size        Size of the buffer
    359 
    360 Returns:
    361 
    362   The 16 bit checksum
    363 
    364 --*/
    365 {
    366   UINTN   Index;
    367   UINT16  Sum;
    368 
    369   Sum = 0;
    370 
    371   //
    372   // Perform the word sum for buffer
    373   //
    374   for (Index = 0; Index < Size; Index++) {
    375     Sum = (UINT16) (Sum + Buffer[Index]);
    376   }
    377 
    378   return (UINT16) Sum;
    379 }
    380 
    381 EFI_STATUS
    382 PrintGuid (
    383   IN EFI_GUID *Guid
    384   )
    385 /*++
    386 
    387 Routine Description:
    388 
    389   This function prints a GUID to STDOUT.
    390 
    391 Arguments:
    392 
    393   Guid    Pointer to a GUID to print.
    394 
    395 Returns:
    396 
    397   EFI_SUCCESS             The GUID was printed.
    398   EFI_INVALID_PARAMETER   The input was NULL.
    399 
    400 --*/
    401 {
    402   if (Guid == NULL) {
    403     printf ("ERROR: PrintGuid called with a NULL value.\n");
    404     return EFI_INVALID_PARAMETER;
    405   }
    406 
    407   printf (
    408     "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
    409     Guid->Data1,
    410     Guid->Data2,
    411     Guid->Data3,
    412     Guid->Data4[0],
    413     Guid->Data4[1],
    414     Guid->Data4[2],
    415     Guid->Data4[3],
    416     Guid->Data4[4],
    417     Guid->Data4[5],
    418     Guid->Data4[6],
    419     Guid->Data4[7]
    420     );
    421   return EFI_SUCCESS;
    422 }
    423 
    424 EFI_STATUS
    425 PrintGuidToBuffer (
    426   IN EFI_GUID     *Guid,
    427   IN OUT UINT8    *Buffer,
    428   IN UINT32       BufferLen,
    429   IN BOOLEAN      Uppercase
    430   )
    431 /*++
    432 
    433 Routine Description:
    434 
    435   This function prints a GUID to a buffer
    436 
    437 Arguments:
    438 
    439   Guid      - Pointer to a GUID to print.
    440   Buffer    - Pointer to a user-provided buffer to print to
    441   BufferLen - Size of the Buffer
    442   Uppercase - If use upper case.
    443 
    444 Returns:
    445 
    446   EFI_SUCCESS             The GUID was printed.
    447   EFI_INVALID_PARAMETER   The input was NULL.
    448   EFI_BUFFER_TOO_SMALL    The input buffer was not big enough
    449 
    450 --*/
    451 {
    452   if (Guid == NULL) {
    453     printf ("ERROR: PrintGuidToBuffer() called with a NULL value\n");
    454     return EFI_INVALID_PARAMETER;
    455   }
    456 
    457   if (BufferLen < PRINTED_GUID_BUFFER_SIZE) {
    458     printf ("ERORR: PrintGuidToBuffer() called with invalid buffer size\n");
    459     return EFI_BUFFER_TOO_SMALL;
    460   }
    461 
    462   if (Uppercase) {
    463     sprintf (
    464       Buffer,
    465       "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
    466       Guid->Data1,
    467       Guid->Data2,
    468       Guid->Data3,
    469       Guid->Data4[0],
    470       Guid->Data4[1],
    471       Guid->Data4[2],
    472       Guid->Data4[3],
    473       Guid->Data4[4],
    474       Guid->Data4[5],
    475       Guid->Data4[6],
    476       Guid->Data4[7]
    477       );
    478   } else {
    479     sprintf (
    480       Buffer,
    481       "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
    482       Guid->Data1,
    483       Guid->Data2,
    484       Guid->Data3,
    485       Guid->Data4[0],
    486       Guid->Data4[1],
    487       Guid->Data4[2],
    488       Guid->Data4[3],
    489       Guid->Data4[4],
    490       Guid->Data4[5],
    491       Guid->Data4[6],
    492       Guid->Data4[7]
    493       );
    494   }
    495 
    496   return EFI_SUCCESS;
    497 }
    498