Home | History | Annotate | Download | only in RuntimeDxe
      1 /** @file
      2   This file implements CalculateCrc32 Boot Services as defined in
      3   Platform Initialization specification 1.0 VOLUME 2 DXE Core Interface.
      4 
      5   This Boot Services is in the Runtime Driver because this service is
      6   also required by SetVirtualAddressMap() when the EFI System Table and
      7   EFI Runtime Services Table are converted from physical address to
      8   virtual addresses.  This requires that the 32-bit CRC be recomputed.
      9 
     10 Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
     11 This program and the accompanying materials
     12 are licensed and made available under the terms and conditions of the BSD License
     13 which accompanies this distribution.  The full text of the license may be found at
     14 http://opensource.org/licenses/bsd-license.php
     15 
     16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     18 
     19 **/
     20 
     21 
     22 #include <Uefi.h>
     23 
     24 UINT32  mCrcTable[256];
     25 
     26 /**
     27   Calculate CRC32 for target data.
     28 
     29   @param  Data                  The target data.
     30   @param  DataSize              The target data size.
     31   @param  CrcOut                The CRC32 for target data.
     32 
     33   @retval EFI_SUCCESS           The CRC32 for target data is calculated successfully.
     34   @retval EFI_INVALID_PARAMETER Some parameter is not valid, so the CRC32 is not
     35                                 calculated.
     36 
     37 **/
     38 EFI_STATUS
     39 EFIAPI
     40 RuntimeDriverCalculateCrc32 (
     41   IN  VOID    *Data,
     42   IN  UINTN   DataSize,
     43   OUT UINT32  *CrcOut
     44   )
     45 {
     46   UINT32  Crc;
     47   UINTN   Index;
     48   UINT8   *Ptr;
     49 
     50   if (Data == NULL || DataSize == 0 || CrcOut == NULL) {
     51     return EFI_INVALID_PARAMETER;
     52   }
     53 
     54   Crc = 0xffffffff;
     55   for (Index = 0, Ptr = Data; Index < DataSize; Index++, Ptr++) {
     56     Crc = (Crc >> 8) ^ mCrcTable[(UINT8) Crc ^ *Ptr];
     57   }
     58 
     59   *CrcOut = Crc ^ 0xffffffff;
     60   return EFI_SUCCESS;
     61 }
     62 
     63 
     64 /**
     65   This internal function reverses bits for 32bit data.
     66 
     67   @param  Value                 The data to be reversed.
     68 
     69   @return                       Data reversed.
     70 
     71 **/
     72 UINT32
     73 ReverseBits (
     74   UINT32  Value
     75   )
     76 {
     77   UINTN   Index;
     78   UINT32  NewValue;
     79 
     80   NewValue = 0;
     81   for (Index = 0; Index < 32; Index++) {
     82     if ((Value & (1 << Index)) != 0) {
     83       NewValue = NewValue | (1 << (31 - Index));
     84     }
     85   }
     86 
     87   return NewValue;
     88 }
     89 
     90 /**
     91   Initialize CRC32 table.
     92 
     93 **/
     94 VOID
     95 RuntimeDriverInitializeCrc32Table (
     96   VOID
     97   )
     98 {
     99   UINTN   TableEntry;
    100   UINTN   Index;
    101   UINT32  Value;
    102 
    103   for (TableEntry = 0; TableEntry < 256; TableEntry++) {
    104     Value = ReverseBits ((UINT32) TableEntry);
    105     for (Index = 0; Index < 8; Index++) {
    106       if ((Value & 0x80000000) != 0) {
    107         Value = (Value << 1) ^ 0x04c11db7;
    108       } else {
    109         Value = Value << 1;
    110       }
    111     }
    112 
    113     mCrcTable[TableEntry] = ReverseBits (Value);
    114   }
    115 }
    116