⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rnga.c

📁 Freescale ARM11系列CPU MX31的WINCE 5.0下的BSP
💻 C
字号:
//------------------------------------------------------------------------------
//
//  Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Copyright (C) 2006, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------
//
//  File:  rnga.c
//
//  Implementation for Random Number Generator and Accelerator
//
//-----------------------------------------------------------------------------
#include <bsp.h>

//-----------------------------------------------------------------------------
// External Functions
extern VOID OALClockSetGatingMode(DDK_CLOCK_GATE_INDEX index,
    DDK_CLOCK_GATE_MODE mode);

//-----------------------------------------------------------------------------
// External Variables


//-----------------------------------------------------------------------------
// Defines

//-----------------------------------------------------------------------------
// Local Functions

UINT32 RNGA_GetRandomData(void *pBuf, UINT32 dwNumBytes);

//-----------------------------------------------------------------------------
//
// Function:  RNGA_Init
//
// This function enables the RNGA if available.
//      Note: Once enabled the RGNA cannot be stopped until a hardware reset or secure mode change
//
// Parameters:
//      None.
//
// Returns:
//      Returns TRUE if RNGA is present & FALSE if it is not present.
//
//-----------------------------------------------------------------------------

BOOL RNGA_Init(void)
{

#if BSP_RNGA

    PCSP_RNGA_REGS pRNGAReg;

    pRNGAReg = (PCSP_RNGA_REGS) OALPAtoVA((CSP_BASE_REG_PA_RNGA), FALSE);

    // Enable clock to RNGA module
    OALClockSetGatingMode(DDK_CLOCK_GATE_INDEX_RNG,
                          DDK_CLOCK_GATE_MODE_ENABLED_ALL);

    // Mask error interrupt and enable security violation notification
    // Enable RNGA, put in sleep mode
    INSREG32BF(&pRNGAReg->RNG_CR, RNG_CR_INTMASK, RNG_CR_INTMASK_MASK);
    INSREG32BF(&pRNGAReg->RNG_CR, RNG_CR_HASSURE, RNG_CR_HASSURE_ENABLE);
    INSREG32BF(&pRNGAReg->RNG_CR, RNG_CR_SLEEP, RNG_CR_SLEEP_SET);
    INSREG32BF(&pRNGAReg->RNG_CR, RNG_CR_GO, RNG_CR_GO_SET);
    return TRUE;

#else

    return FALSE;
    
#endif
}

//-----------------------------------------------------------------------------
//
// Function:  OALIoCtlHalGetRandomSeed
//
// Implements the IOCTL_HAL_GET_RANDOM_SEED handler.
//
//  Parameters:
//      dwIoControlCode
//          [in] The control code specifying the command to execute.
//
//      lpInBuf
//          [in] Long pointer to a buffer that contains the data required to
//          perform the operation. Set to NULL if the dwIoControlCode parameter
//          specifies an operation that does not require input data.
//
//      nInBufSize
//          [in] Size, in bytes, of the buffer pointed to by pInBuf.
//
//      lpOutBuf
//          [out] Long pointer to a buffer that receives the output data for
//          the operation. Set to NULL if the dwIoControlCode parameter
//          specifies an operation that does not produce output data.
//
//      nOutBufSize
//          [in] Size, in bytes, of the buffer pointed to by pOutBuf.
//
//      lpBytesReturned
//          [out] Long pointer to a variable that receives the size, in bytes,
//          of the data stored into the buffer pointed to by pOutBuf. Even
//          when an operation produces no output data and pOutBuf is set to
//          NULL, the DeviceIoControl function uses the variable pointed to
//          by pBytesReturned. After such an operation, the value of the
//          variable has no meaning.
//
// Returns:
//      Returns TRUE if Successfull & FALSE upon error.
//
//-----------------------------------------------------------------------------
BOOL OALIoCtlHalGetRandomSeed(UINT32 dwIoControlCode, VOID *lpInBuf, UINT32 nInBufSize,
                              VOID *lpOutBuf, UINT32 nOutBufSize, UINT32* lpBytesReturned)
{
    BOOL ret = FALSE;
    
#if BSP_RNGA

    *lpBytesReturned = RNGA_GetRandomData((BYTE *)lpOutBuf, nOutBufSize);
    ret = ((*lpBytesReturned) > 0);

#endif

    return ret;
}

//-----------------------------------------------------------------------------
//
// Function:  RNGA_GetRandomData
//
// Retrieves the desired amount of random data from RNGA.
// Note: this function will block until the desired
//            amount of data is retrieved
//
// Parameters:
//      pBuf
//          pointer to a buffer that receives the output data for
//          the operation.
//
//      dwNumBytes
//          Size, in bytes, of the number of random bytes to get.
//
// Returns:
//      Returns TRUE if Successfull & FALSE upon error.
//
//-----------------------------------------------------------------------------
UINT32 RNGA_GetRandomData(BYTE *pBuf, UINT32 dwNumBytes )
{
    PCSP_RNGA_REGS pRNGAReg;
    UINT32 tempRand32;
    UINT32 bytesReqd;
    UINT32 minBytes;

    pRNGAReg = (PCSP_RNGA_REGS) OALPAtoVA((CSP_BASE_REG_PA_RNGA), FALSE);
    // Bring rnga out of sleep mode
    INSREG32BF(&pRNGAReg->RNG_CR, RNG_CR_SLEEP, RNG_CR_SLEEP_CLEAR);
    bytesReqd = dwNumBytes;

    while (bytesReqd > 0)
    {
  
        if (EXTREG32BF(&pRNGAReg->RNG_SR, RNG_SR_FIFOLEVEL))
        {
            tempRand32 = INREG32(&pRNGAReg->RNG_OFIFO);
            minBytes = (bytesReqd >= sizeof(tempRand32)) ? sizeof(tempRand32) : bytesReqd;
            memcpy (pBuf, &tempRand32, minBytes);
            bytesReqd -= minBytes;
            pBuf += minBytes;
        }
       
    }
    // Put back the RNGA into sleep mode
    INSREG32BF(&pRNGAReg->RNG_CR, RNG_CR_SLEEP, RNG_CR_SLEEP_SET);

    return dwNumBytes;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -