コード例 #1
0
ファイル: flash.c プロジェクト: StefanJia/test2
void EraseFlash(uint32_t Address)
{ 
  int sector = GetSector(Address);
  /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  be done by word */ 
  FLASH_Erase_Sector(sector, FLASH_VOLTAGE_RANGE_3);
}
コード例 #2
0
/**
  * @brief  Perform a mass erase or erase the specified FLASH memory sectors  with interrupt enabled
  * @param  pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
  *         contains the configuration information for the erasing.
  * 
  * @retval HAL Status
  */
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
{
  HAL_StatusTypeDef status = HAL_OK;

  /* Process Locked */
  __HAL_LOCK(&pFlash);

  /* Check the parameters */
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));

  /* Enable End of FLASH Operation interrupt */
  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  
  /* Enable Error source interrupt */
  __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
  
  /* Clear pending flags (if any) */  
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP    | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |\
                         FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);  
  
  if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  {
    /*Mass erase to be done*/
    pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE;
    pFlash.Bank = pEraseInit->Banks;
    FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);
  }
  else
  {
    /* Erase by sector to be done*/

    /* Check the parameters */
    assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));

    pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE;
    pFlash.NbSectorsToErase = pEraseInit->NbSectors;
    pFlash.Sector = pEraseInit->Sector;
    pFlash.VoltageForErase = (uint8_t)pEraseInit->VoltageRange;

    /*Erase 1st sector and wait for IT*/
    FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->VoltageRange);
  }

  return status;
}
コード例 #3
0
/**
  * @brief This function handles FLASH interrupt request.
  * @retval None
  */
void HAL_FLASH_IRQHandler(void)
{
  uint32_t addresstmp = 0U;
  
  /* Check FLASH operation error flags */
  if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
    FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
  {
    if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
    {
      /*return the faulty sector*/
      addresstmp = pFlash.Sector;
      pFlash.Sector = 0xFFFFFFFFU;
    }
    else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
    {
      /*return the faulty bank*/
      addresstmp = pFlash.Bank;
    }
    else
    {
      /*return the faulty address*/
      addresstmp = pFlash.Address;
    }
    
    /*Save the Error code*/
    FLASH_SetErrorCode();
    
    /* FLASH error interrupt user callback */
    HAL_FLASH_OperationErrorCallback(addresstmp);
    
    /*Stop the procedure ongoing*/
    pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  }
  
  /* Check FLASH End of Operation flag  */
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
  {
    /* Clear FLASH End of Operation pending bit */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
    
    if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
    {
      /*Nb of sector to erased can be decreased*/
      pFlash.NbSectorsToErase--;
      
      /* Check if there are still sectors to erase*/
      if(pFlash.NbSectorsToErase != 0U)
      {
        addresstmp = pFlash.Sector;
        /*Indicate user which sector has been erased*/
        HAL_FLASH_EndOfOperationCallback(addresstmp);
        
        /*Increment sector number*/
        pFlash.Sector++;
        addresstmp = pFlash.Sector;
        FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
      }
      else
      {
        /*No more sectors to Erase, user callback can be called.*/
        /*Reset Sector and stop Erase sectors procedure*/
        pFlash.Sector = addresstmp = 0xFFFFFFFFU;
        pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
        
        /* Flush the caches to be sure of the data consistency */
        FLASH_FlushCaches() ;
                
        /* FLASH EOP interrupt user callback */
        HAL_FLASH_EndOfOperationCallback(addresstmp);
      }
    }
    else 
    {
      if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) 
      {
        /* MassErase ended. Return the selected bank */
        /* Flush the caches to be sure of the data consistency */
        FLASH_FlushCaches() ;

        /* FLASH EOP interrupt user callback */
        HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
      }
      else
      {
        /*Program ended. Return the selected address*/
        /* FLASH EOP interrupt user callback */
        HAL_FLASH_EndOfOperationCallback(pFlash.Address);
      }
      pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
    }
  }
  
  if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  {
    /* Operation is completed, disable the PG, SER, SNB and MER Bits */
    CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));

    /* Disable End of FLASH Operation interrupt */
    __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
    
    /* Disable Error source interrupt */
    __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
    
    /* Process Unlocked */
    __HAL_UNLOCK(&pFlash);
  }
}
/**
  * @brief This function handles FLASH interrupt request.
  * @retval None
  */
void HAL_FLASH_IRQHandler(void)
{
  uint32_t temp = 0;
  
  /* If the program operation is completed, disable the PG Bit */
  FLASH->CR &= (~FLASH_CR_PG);

  /* If the erase operation is completed, disable the SER Bit */
  FLASH->CR &= (~FLASH_CR_SER);
  FLASH->CR &= SECTOR_MASK; 

  /* if the erase operation is completed, disable the MER Bit */
  FLASH->CR &= (~FLASH_MER_BIT);

  /* Check FLASH End of Operation flag  */
  if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
  {
    switch (pFlash.ProcedureOnGoing)
    {
      case FLASH_PROC_SECTERASE :
      {
        /* Nb of sector to erased can be decreased */
        pFlash.NbSectorsToErase--;

        /* Check if there are still sectors to erase */
        if(pFlash.NbSectorsToErase != 0)
        {
          temp = pFlash.Sector;
          /* Indicate user which sector has been erased */
          HAL_FLASH_EndOfOperationCallback(temp);

          /* Clear pending flags (if any) */  
          __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);  

          /* Increment sector number */
          temp = ++pFlash.Sector;
          FLASH_Erase_Sector(temp, pFlash.VoltageForErase);
        }
        else
        {
          /* No more sectors to Erase, user callback can be called.*/
          /* Reset Sector and stop Erase sectors procedure */
          pFlash.Sector = temp = 0xFFFFFFFF;
          /* FLASH EOP interrupt user callback */
          HAL_FLASH_EndOfOperationCallback(temp);
          /* Sector Erase procedure is completed */
          pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
          /* Clear FLASH End of Operation pending bit */
          __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
        }
        break;
      }
    
      case FLASH_PROC_MASSERASE :
      {
        /* MassErase ended. Return the selected bank : in this product we don't have Banks */
        /* FLASH EOP interrupt user callback */
        HAL_FLASH_EndOfOperationCallback(0);
        /* MAss Erase procedure is completed */
        pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
        /* Clear FLASH End of Operation pending bit */
        __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
        break;
      }

      case FLASH_PROC_PROGRAM :
      {
        /*Program ended. Return the selected address*/
        /* FLASH EOP interrupt user callback */
        HAL_FLASH_EndOfOperationCallback(pFlash.Address);
        /* Programming procedure is completed */
        pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
        /* Clear FLASH End of Operation pending bit */
        __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
        break;
      }
      default :
        break;
    }
  }
  
  /* Check FLASH operation error flags */
  if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR  | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR )) != RESET)
  {
    switch (pFlash.ProcedureOnGoing)
    {
      case FLASH_PROC_SECTERASE :
      {
        /* return the faulty sector */
        temp = pFlash.Sector;
        pFlash.Sector = 0xFFFFFFFF;
        break;
      }
      case FLASH_PROC_MASSERASE :
      {
        /* No return in case of Mass Erase */
        temp = 0;
        break;
      }
      case FLASH_PROC_PROGRAM :
      {
        /*return the faulty address*/
        temp = pFlash.Address;
        break;
      }
    default :
      break;
    }
    /*Save the Error code*/
    FLASH_SetErrorCode();

    /* FLASH error interrupt user callback */
    HAL_FLASH_OperationErrorCallback(temp);
    /* Clear FLASH error pending bits */
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR  | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR );

    /*Stop the procedure ongoing */
    pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
  }
  
  if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
  {
    /* Disable End of FLASH Operation interrupt */
    __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);

    /* Disable Error source interrupt */
    __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);

    /* Process Unlocked */
    __HAL_UNLOCK(&pFlash);
  }
  
}
コード例 #5
0
/**
  * @brief  Perform a mass erase or erase the specified FLASH memory sectors with interrupt enabled
  * @param  pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  *         contains the configuration information for the erasing.
  * 
  * @retval HAL Status
  */
HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit)
{
  HAL_StatusTypeDef status = HAL_OK;

  /* Process Locked */
  __HAL_LOCK(&pFlash);

  /* Check the parameters */
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  assert_param(IS_VOLTAGERANGE(pEraseInit->VoltageRange));
  assert_param(IS_FLASH_BANK(pEraseInit->Banks));

  if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1) 
  {  
    /* Clear bank 1 pending flags (if any) */ 
    __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK1); 
  
    /* Enable End of Operation and Error interrupts for Bank 1 */
    __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1    | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
                                FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1); 
  }
  if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2) 
  {  
    /* Clear bank 2 pending flags (if any) */ 
    __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2 | FLASH_FLAG_ALL_ERRORS_BANK2); 
  
    /* Enable End of Operation and Error interrupts for Bank 2 */
    __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2    | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
                                FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2); 
  }
  
  if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
  {
    /*Mass erase to be done*/
    if(pEraseInit->Banks == FLASH_BANK_1)
    {
      pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE_BANK1;
    }
    else if(pEraseInit->Banks == FLASH_BANK_2)
    {
      pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE_BANK2;
    }
    else
    {
      pFlash.ProcedureOnGoing = FLASH_PROC_ALLBANK_MASSERASE;  
    }

    FLASH_MassErase(pEraseInit->VoltageRange, pEraseInit->Banks);
  }
  else
  {
    /* Erase by sector to be done*/

    /* Check the parameters */
    assert_param(IS_FLASH_BANK_EXCLUSIVE(pEraseInit->Banks));
    assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));

    if(pEraseInit->Banks == FLASH_BANK_1)
    {
      pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE_BANK1;  
    }
    else
    {
      pFlash.ProcedureOnGoing = FLASH_PROC_SECTERASE_BANK2;    
    }

    pFlash.NbSectorsToErase = pEraseInit->NbSectors;
    pFlash.Sector = pEraseInit->Sector;
    pFlash.VoltageForErase = pEraseInit->VoltageRange;

    /*Erase 1st sector and wait for IT*/
    FLASH_Erase_Sector(pEraseInit->Sector, pEraseInit->Banks, pEraseInit->VoltageRange);
  }

  return status;
}
コード例 #6
0
/**
  * @brief  Perform a mass erase or erase the specified FLASH memory sectors 
  * @param[in]  pEraseInit pointer to an FLASH_EraseInitTypeDef structure that
  *         contains the configuration information for the erasing.
  * 
  * @param[out]  SectorError pointer to variable  that
  *         contains the configuration information on faulty sector in case of error 
  *         (0xFFFFFFFF means that all the sectors have been correctly erased)
  * 
  * @retval HAL Status
  */
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
{
  HAL_StatusTypeDef status = HAL_OK;
  uint32_t index = 0;
  
  /* Process Locked */
  __HAL_LOCK(&pFlash);

  /* Check the parameters */
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));
  assert_param(IS_VOLTAGERANGE(pEraseInit->VoltageRange));
  assert_param(IS_FLASH_BANK(pEraseInit->Banks));
  
  
  /* Wait for last operation to be completed */
  if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1) 
  {
    status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1);
  }

  if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2) 
  {
    status |= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2);
  }
  
  if(status == HAL_OK)
  {
    /*Initialization of SectorError variable*/
    *SectorError = 0xFFFFFFFF;
    
    if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
    {
      /*Mass erase to be done*/
      FLASH_MassErase(pEraseInit->VoltageRange, pEraseInit->Banks);

      /* Wait for last operation to be completed */
      if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
      {
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1);
        /* if the erase operation is completed, disable the Bank1 BER Bit */
        FLASH->CR1 &= (~FLASH_CR_BER);
      }
      if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
      {
        status |= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2);
        /* if the erase operation is completed, disable the Bank2 BER Bit */
        FLASH->CR2 &= (~FLASH_CR_BER);    
      }
    }
    else
    {
      /* Check the parameters */
      assert_param(IS_FLASH_BANK_EXCLUSIVE(pEraseInit->Banks));
      assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));

      /* Erase by sector by sector to be done*/
      for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
      {
        FLASH_Erase_Sector(index, pEraseInit->Banks, pEraseInit->VoltageRange);

        if((pEraseInit->Banks & FLASH_BANK_1) == FLASH_BANK_1)
        {
          /* Wait for last operation to be completed */
          status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1);
        
          /* If the erase operation is completed, disable the SER Bit */
          FLASH->CR1 &= (~(FLASH_CR_SER | FLASH_CR_SNB));
        }
        if((pEraseInit->Banks & FLASH_BANK_2) == FLASH_BANK_2)
        {
          /* Wait for last operation to be completed */
          status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2);
        
          /* If the erase operation is completed, disable the SER Bit */
          FLASH->CR2 &= (~(FLASH_CR_SER | FLASH_CR_SNB));
        }

        if(status != HAL_OK)
        {
          /* In case of error, stop erase procedure and return the faulty sector*/
          *SectorError = index;
          break;
        }
      }
    }
  }

  /* Process Unlocked */
  __HAL_UNLOCK(&pFlash);

  return status;
}
コード例 #7
0
/**
  * @brief  Perform a mass erase or erase the specified FLASH memory sectors 
  * @param[in]  pEraseInit: pointer to an FLASH_EraseInitTypeDef structure that
  *         contains the configuration information for the erasing.
  * 
  * @param[out]  SectorError: pointer to variable  that
  *         contains the configuration information on faulty sector in case of error 
  *         (0xFFFFFFFF means that all the sectors have been correctly erased)
  * 
  * @retval HAL Status
  */
HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError)
{
  HAL_StatusTypeDef status = HAL_ERROR;
  uint32_t index = 0;
  
  /* Process Locked */
  __HAL_LOCK(&pFlash);

  /* Check the parameters */
  assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase));

  /* Wait for last operation to be completed */
  status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);

  if(status == HAL_OK)
  {
    /*Initialization of SectorError variable*/
    *SectorError = 0xFFFFFFFF;
    
    if(pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE)
    {
      /*Mass erase to be done*/
      FLASH_MassErase((uint8_t) pEraseInit->VoltageRange, pEraseInit->Banks);

      /* Wait for last operation to be completed */
      status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
      
      /* if the erase operation is completed, disable the MER Bit */
      FLASH->CR &= (~FLASH_MER_BIT);
    }
    else
    {
      /* Check the parameters */
      assert_param(IS_FLASH_NBSECTORS(pEraseInit->NbSectors + pEraseInit->Sector));

      /* Erase by sector by sector to be done*/
      for(index = pEraseInit->Sector; index < (pEraseInit->NbSectors + pEraseInit->Sector); index++)
      {
        FLASH_Erase_Sector(index, (uint8_t) pEraseInit->VoltageRange);

        /* Wait for last operation to be completed */
        status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
        
        /* If the erase operation is completed, disable the SER and SNB Bits */
        CLEAR_BIT(FLASH->CR, (FLASH_CR_SER | FLASH_CR_SNB));

        if(status != HAL_OK) 
        {
          /* In case of error, stop erase procedure and return the faulty sector*/
          *SectorError = index;
          break;
        }
      }
    }
    /* Flush the caches to be sure of the data consistency */
    FLASH_FlushCaches();    
  }

  /* Process Unlocked */
  __HAL_UNLOCK(&pFlash);

  return status;
}
コード例 #8
0
ファイル: TpmUtil.cpp プロジェクト: DivyaMahadevan/security
uint32_t
TpmUtilClearAndProvision(
    void
    )
{
    uint32_t retVal = 0;
    ANY_OBJECT appPayloadAuthority = {0};
    ANY_OBJECT platformAuthority = {0};
    TPM2B_MAX_NV_BUFFER rawPolicy = {0};
    TPMT_SIGNATURE authorizationSignature = {0};

    if((retVal = TpmClearControl(0x00)) != TPM_RC_SUCCESS)
    {
        printf("TpmClearControl() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmClearControl() complete.\r\n");

    if((retVal = TpmClear()) != TPM_RC_SUCCESS)
    {
        printf("TpmClear() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmClear() complete.\r\n");

    if((retVal = TpmUtilCreateSrk()) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilCreateSrk() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilCreateSrk() complete.\r\n");

    if((retVal = TpmUtilCreateEk()) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilCreateEk() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilCreateEk() complete.\r\n");

    if((retVal = TpmUtilCreateCounters()) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilCreateCounters() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilCreateCounters() complete.\r\n");

    if((retVal = TpmUtilCreateAuthority("PlatformAuthority", &platformAuthority)) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilCreateAuthority(PlatformAuthority) failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilCreateAuthority(PlatformAuthority) complete.\r\n");
    persistedData.platformAuthorityName = platformAuthority.obj.name;

    if((retVal = TpmUtilCreateAuthority("AppPayloadAuthority", &appPayloadAuthority)) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilCreateAuthority(AppPayloadAuthority) failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilCreateAuthority(AppPayloadAuthority) complete.\r\n");

    if((retVal = TpmUtilBuildSamplePolicy(&appPayloadAuthority, &rawPolicy)) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilBuildSamplePolicy() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilBuildSamplePolicy() complete.\r\n");

    if((retVal = TpmUtilIssueBootPolicy(&platformAuthority, &rawPolicy, &persistedData.ekName, &authorizationSignature)) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilIssueBootPolicy() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilIssueBootPolicy() complete.\r\n");

    if((retVal = TpmUtilWriteBootPolicy(&rawPolicy, &platformAuthority.obj.publicArea, &authorizationSignature)) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilWriteBootPolicy() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilWriteBootPolicy() complete.\r\n");

    if((retVal = StartEkSeededSession()) != TPM_RC_SUCCESS)
    {
        printf("StartEkSeededSession() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("StartEkSeededSession() complete.\r\n");

    HAL_FLASH_Unlock();
    FLASH_Erase_Sector(FLASH_SECTOR_23, FLASH_VOLTAGE_RANGE_3);
    persistedData.magic = RAZORCLAMPERSISTEDDATA;
    persistedData.version = RAZORCLAMPERSISTEDVERSION;
    persistedData.size = sizeof(RazorClamPersistentDataType);
    persistedData.compoundIdentity.t.size = CryptGenerateRandom(SHA256_DIGEST_SIZE, persistedData.compoundIdentity.t.buffer);

    if((retVal = TpmUtilSignAppPayload(&appPayloadAuthority)) != TPM_RC_SUCCESS)
    {
        printf("TpmUtilSignAppPayload() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("TpmUtilSignAppPayload() complete.\r\n");

    if((retVal = SetTpmAuthValues()) != TPM_RC_SUCCESS)
    {
        printf("SetTpmAuthValues() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("SetTpmAuthValues() complete.\r\n");

    if((retVal = MeasureEventConfidential(HR_PCR + 0, persistedData.compoundIdentity.t.size, persistedData.compoundIdentity.t.buffer)) != TPM_RC_SUCCESS)
    {
        printf("MeasureEventConfidential() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }

    if((retVal = CreatePlatformDataProtectionKey()) != TPM_RC_SUCCESS)
    {
        printf("CreatePlatformDataProtectionKey() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }
    printf("CreatePlatformDataProtectionKey() complete.\r\n");

    // Encrypt the authValues to be persisted
    if(((retVal = ProtectPlatformData(persistedData.lockoutAuth.t.buffer, persistedData.lockoutAuth.t.size, NO)) != TPM_RC_SUCCESS) ||
       ((retVal = ProtectPlatformData(persistedData.endorsementAuth.t.buffer, persistedData.endorsementAuth.t.size, NO)) != TPM_RC_SUCCESS) ||
       ((retVal = ProtectPlatformData(persistedData.storageAuth.t.buffer, persistedData.storageAuth.t.size, NO)) != TPM_RC_SUCCESS))
    {
        printf("ProtectPlatformData() failed with 0x%03x.\r\n", retVal);
        goto Cleanup;
    }

    // Persist the data in flash
    for(uint32_t n = 0; n < sizeof(persistedData); n++)
    {
        if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, ADDR_FLASH_SECTOR_23 + n, ((uint8_t*)&persistedData)[n]) != HAL_OK)
        {
            printf("Flash Write Error @ 0x%08x\r\n", ADDR_FLASH_SECTOR_23 + n);
        }
    }
    HAL_FLASH_Lock();

Cleanup:
    if(platformAuthority.obj.handle != 0)
    {
        FlushContext(&platformAuthority);
    }
    if(appPayloadAuthority.obj.handle != 0)
    {
        FlushContext(&appPayloadAuthority);
    }
    if(volatileData.ekSeededSession.handle != 0)
    {
        FlushContext((ANY_OBJECT*)&volatileData.ekSeededSession);
        MemorySet(&volatileData.ekSeededSession, 0x00, sizeof(volatileData.ekSeededSession));
    }
    return retVal;
}
コード例 #9
0
ファイル: eeprom.c プロジェクト: javifercep/MuBABoot
/**
  * @brief  Restore the pages to a known good state in case of page's status
  *   corruption after a power loss.
  * @param  None.
  * @retval - Flash error code: on write Flash error
  *         - FLASH_COMPLETE: on success
  */
uint32_t EE_Init(void)
{
  uint16_t PageStatus0 = 6, PageStatus1 = 6;
  uint16_t VarIdx = 0;
  uint16_t EepromStatus = 0, ReadStatus = 0;
  int16_t x = -1;
  HAL_StatusTypeDef OperationStatus;

  /* Get Page0 status */
  PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
  /* Get Page1 status */
  PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);

  /* Check for invalid header states and repair if necessary */
  switch (PageStatus0)
  {
    case ERASED:
      if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
      {
        /* Erase Page0 */
        FLASH_Erase_Sector(PAGE0_ID,VOLTAGE_RANGE);

        OperationStatus = FLASH_WaitForLastOperation(1000);
        if(OperationStatus == HAL_ERROR)
        {
        	return HAL_FLASH_GetError();
        }
      }
      else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
      {
        /* Erase Page0 */
        FLASH_Erase_Sector(PAGE0_ID,VOLTAGE_RANGE);
        /* If erase operation was failed, a Flash error code is returned */
        OperationStatus = FLASH_WaitForLastOperation(1000);
        if(OperationStatus == HAL_ERROR)
        {
        	return HAL_FLASH_GetError();
        }
        /* Mark Page1 as valid */
        OperationStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,PAGE1_BASE_ADDRESS, VALID_PAGE);
        /* If program operation was failed, a Flash error code is returned */
        if(OperationStatus == HAL_ERROR)
		{
			return HAL_FLASH_GetError();
		}
      }
      else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
      {
        /* Erase both Page0 and Page1 and set Page0 as valid page */
    	  OperationStatus = EE_Format();
        /* If erase/program operation was failed, a Flash error code is returned */
        if (OperationStatus == HAL_ERROR)
        {
        	return HAL_FLASH_GetError();
        }
      }
      break;

    case RECEIVE_DATA:
      if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
      {
        /* Transfer data from Page1 to Page0 */
        for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
        {
          if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
          {
            x = VarIdx;
          }
          if (VarIdx != x)
          {
            /* Read the last variables' updates */
            ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
            /* In case variable corresponding to the virtual address was found */
            if (ReadStatus != 0x1)
            {
              /* Transfer the variable to the Page0 */
              EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
              /* If program operation was failed, a Flash error code is returned */
              if (EepromStatus != HAL_OK)
              {
                return HAL_FLASH_GetError();
              }
            }
          }
        }
        /* Mark Page0 as valid */
        OperationStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
        /* If program operation was failed, a Flash error code is returned */
        if (OperationStatus != HAL_OK)
        {
          return HAL_FLASH_GetError();;
        }
        /* Erase Page1 */
        FLASH_Erase_Sector(PAGE1_ID, VOLTAGE_RANGE);
        /* If erase operation was failed, a Flash error code is returned */
        OperationStatus = FLASH_WaitForLastOperation(1000);
        if(OperationStatus == HAL_ERROR)
        {
        	return HAL_FLASH_GetError();
        }
      }
      else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
      {
        /* Erase Page1 */
		FLASH_Erase_Sector(PAGE1_ID, VOLTAGE_RANGE);
		/* If erase operation was failed, a Flash error code is returned */
		OperationStatus = FLASH_WaitForLastOperation(1000);
		if(OperationStatus == HAL_ERROR)
		{
			return HAL_FLASH_GetError();
		}
        /* Mark Page0 as valid */
		OperationStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
        /* If program operation was failed, a Flash error code is returned */
        if (OperationStatus != HAL_OK)
        {
          return HAL_FLASH_GetError();
        }
      }
      else /* Invalid state -> format eeprom */
      {
        /* Erase both Page0 and Page1 and set Page0 as valid page */
    	OperationStatus = EE_Format();
        /* If erase/program operation was failed, a Flash error code is returned */
        if (OperationStatus != HAL_OK)
        {
        	return HAL_FLASH_GetError();
        }
      }
      break;

    case VALID_PAGE:
      if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
      {
        /* Erase both Page0 and Page1 and set Page0 as valid page */
        OperationStatus = EE_Format();
        /* If erase/program operation was failed, a Flash error code is returned */
        if (OperationStatus != HAL_OK)
        {
        	return HAL_FLASH_GetError();
        }
      }
      else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
      {
        /* Erase Page1 */
        FLASH_Erase_Sector(PAGE1_ID, VOLTAGE_RANGE);
        /* If erase operation was failed, a Flash error code is returned */
		OperationStatus = FLASH_WaitForLastOperation(1000);
		if(OperationStatus == HAL_ERROR)
		{
			return HAL_FLASH_GetError();
		}
      }
      else /* Page0 valid, Page1 receive */
      {
        /* Transfer data from Page0 to Page1 */
        for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
        {
          if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
          {
            x = VarIdx;
          }
          if (VarIdx != x)
          {
            /* Read the last variables' updates */
            ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
            /* In case variable corresponding to the virtual address was found */
            if (ReadStatus != 0x1)
            {
              /* Transfer the variable to the Page1 */
              EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
              /* If program operation was failed, a Flash error code is returned */
              if (EepromStatus != HAL_OK)
              {
                return HAL_FLASH_GetError();
              }
            }
          }
        }
        /* Mark Page1 as valid */
        OperationStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
        /* If program operation was failed, a Flash error code is returned */
		if(OperationStatus == HAL_ERROR)
		{
			return HAL_FLASH_GetError();
		}
        /* Erase Page0 */
        FLASH_Erase_Sector(PAGE0_ID, VOLTAGE_RANGE);
        /* If erase operation was failed, a Flash error code is returned */
		OperationStatus = FLASH_WaitForLastOperation(1000);
		if(OperationStatus == HAL_ERROR)
		{
			return HAL_FLASH_GetError();
		}
      }
      break;

    default:  /* Any other state -> format eeprom */
      /* Erase both Page0 and Page1 and set Page0 as valid page */
      OperationStatus = EE_Format();
      /* If erase/program operation was failed, a Flash error code is returned */
      if (OperationStatus != HAL_OK)
      {
        return OperationStatus;
      }
      break;
  }

  return HAL_OK;
}
コード例 #10
0
ファイル: eeprom.c プロジェクト: javifercep/MuBABoot
/**
  * @brief  Transfers last updated variables data from the full Page to
  *   an empty one.
  * @param  VirtAddress: 16 bit virtual address of the variable
  * @param  Data: 16 bit data to be written as variable value
  * @retval Success or error status:
  *           - FLASH_COMPLETE: on success
  *           - PAGE_FULL: if valid page is full
  *           - NO_VALID_PAGE: if no valid page was found
  *           - Flash error code: on write Flash error
  */
static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
{
  HAL_StatusTypeDef FlashStatus = HAL_OK;
  uint32_t NewPageAddress = EEPROM_START_ADDRESS;
  uint16_t OldPageId=0;
  uint16_t ValidPage = PAGE0, VarIdx = 0;
  uint16_t EepromStatus = 0, ReadStatus = 0;

  /* Get active Page for read operation */
  ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);

  if (ValidPage == PAGE1)       /* Page1 valid */
  {
    /* New page address where variable will be moved to */
    NewPageAddress = PAGE0_BASE_ADDRESS;

    /* Old page ID where variable will be taken from */
    OldPageId = PAGE1_ID;
  }
  else if (ValidPage == PAGE0)  /* Page0 valid */
  {
    /* New page address  where variable will be moved to */
    NewPageAddress = PAGE1_BASE_ADDRESS;

    /* Old page ID where variable will be taken from */
    OldPageId = PAGE0_ID;
  }
  else
  {
    return NO_VALID_PAGE;       /* No valid Page */
  }

  /* Set the new Page status to RECEIVE_DATA status */
  FlashStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, NewPageAddress, RECEIVE_DATA);
  /* If program operation was failed, a Flash error code is returned */
  if (FlashStatus != HAL_OK)
  {
    return (uint16_t)FlashStatus;
  }

  /* Write the variable passed as parameter in the new active page */
  EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
  /* If program operation was failed, a Flash error code is returned */
  if (EepromStatus != HAL_OK)
  {
    return EepromStatus;
  }

  /* Transfer process: transfer variables from old to the new active page */
  for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
  {
    if (VirtAddVarTab[VarIdx] != VirtAddress)  /* Check each variable except the one passed as parameter */
    {
      /* Read the other last variable updates */
      ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
      /* In case variable corresponding to the virtual address was found */
      if (ReadStatus != 0x1)
      {
        /* Transfer the variable to the new active page */
        EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
        /* If program operation was failed, a Flash error code is returned */
        if (EepromStatus != HAL_OK)
        {
          return EepromStatus;
        }
      }
    }
  }

  /* Erase the old Page: Set old Page status to ERASED status */
  FLASH_Erase_Sector(OldPageId, VOLTAGE_RANGE);
  /* If erase operation was failed, a Flash error code is returned */
   FlashStatus = FLASH_WaitForLastOperation(1000);
  if(FlashStatus != HAL_OK)
  {
  	return FlashStatus;
  }

  /* Set new Page status to VALID_PAGE status */
  FlashStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, NewPageAddress, VALID_PAGE);
  /* If program operation was failed, a Flash error code is returned */
  if (FlashStatus != HAL_OK)
  {
    return FlashStatus;
  }

  /* Return last operation flash status */
  return FlashStatus;
}
コード例 #11
0
static void
stm32f4_flash_erase_sector_id(int sector_id)
{
    FLASH_Erase_Sector(sector_id, FLASH_VOLTAGE_RANGE_1);
}
コード例 #12
0
ファイル: m_eep.cpp プロジェクト: jackeyjiang/meizi_f7disc
/*
 *  Erase Sector in Flash Memory
 *    Parameter:      adr:  Sector Address
 *    Return Value:   0 - OK,  1 - Failed
 */
int c_eep::EraseSector (unsigned long adr) {
    FLASH_Erase_Sector(adr,FLASH_VOLTAGE_RANGE_1);//?
}
コード例 #13
-1
ファイル: eeprom.c プロジェクト: javifercep/MuBABoot
/**
  * @brief  Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE
  * @param  None
  * @retval Status of the last operation (Flash write or erase) done during
  *         EEPROM formating
  */
static HAL_StatusTypeDef EE_Format(void)
{
  HAL_StatusTypeDef OperationStatus = HAL_OK;

  /* Erase Page0 */
  FLASH_Erase_Sector(PAGE0_ID, VOLTAGE_RANGE);
  /* If erase operation was failed, a Flash error code is returned */
  OperationStatus = FLASH_WaitForLastOperation(1000);
  if(OperationStatus != HAL_OK)
  {
  	return OperationStatus;
  }
  /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
  OperationStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);

  /* If program operation was failed, a Flash error code is returned */
  if (OperationStatus != HAL_OK)
  {
    return OperationStatus;
  }

  /* Erase Page1 */
  FLASH_Erase_Sector(PAGE1_ID, VOLTAGE_RANGE);
  OperationStatus = FLASH_WaitForLastOperation(1000);
  if(OperationStatus != HAL_OK)
  {
  	return OperationStatus;
  }

  /* Return Page1 erase operation status */
  return OperationStatus;
}
コード例 #14
-1
int hacs_pstore_set(hacs_pstore_type_t ent, uint8_t *wbuf, uint32_t byte_len)
{
  uint32_t ent_addr = hacs_pstore_ent_rel_addr[ent];
  hacs_pstore_entry_t *ent_header;
  hacs_pstore_header_t *bank_header;
  uint8_t *buf_ptr;
  uint32_t counter;
  uint32_t temp;
  uint32_t sector;
  int retval;

  // Read in the entire current bank
  memcpy(ram_buf, (uint8_t*)current_bank_addr, HACS_PSTORE_BANK_SIZE);

  // Check entry size. Make sure we are not writing something too big.
  retval = -HACS_INVALID_ARGS;
  HACS_REQUIRES(byte_len <= hacs_pstore_ent_size[ent], done);

  // Update entry content
  buf_ptr = (uint8_t*)(ram_buf + ent_addr + sizeof(hacs_pstore_entry_t));
  memcpy(buf_ptr, wbuf, byte_len);

  // Update entry header
  ent_header = (hacs_pstore_entry_t*)(ram_buf + ent_addr);
  ent_header->byte_len = byte_len;
  ent_header->crc = hacs_crc32_calc(wbuf, byte_len); // each entry has its own CRC

  // Update bank header
  bank_header = (hacs_pstore_header_t *)ram_buf;
  bank_header->bank_version++;
  bank_header->layout_version = HACS_PSTORE_LAYOUT_VERSION;

  // Write the ram buffer to the alternate bank
  HAL_FLASH_Unlock();
  sector = (alternate_bank_addr == HACS_PSTORE_0_ADDR) ? HACS_PSTORE_0_SECTOR : HACS_PSTORE_1_SECTOR;
  FLASH_Erase_Sector(sector, VOLTAGE_RANGE_3);
  // Write the ram buffer word by word.
  counter = 0;
  while(counter < sizeof(ram_buf)) {
    HAL_FLASH_Program(TYPEPROGRAM_WORD, alternate_bank_addr+counter,
                      *(uint32_t*)(ram_buf+counter)); // unaligned access supported
    counter += 4;
  }
  // Note that I exploit the fact that the ram buffer is word-aligned 
  // (its length is multiple of 4).
  assert(counter == sizeof(ram_buf));
  HAL_FLASH_Lock();

  // Switch current bank and alternate bank
  temp = alternate_bank_addr;
  alternate_bank_addr = current_bank_addr;
  current_bank_addr = temp;

  retval = HACS_NO_ERROR;
done:
  return retval;
}
コード例 #15
-1
ファイル: TpmUtil.cpp プロジェクト: DivyaMahadevan/security
HAL_StatusTypeDef
TpmUtilStorePersistedData(
    void
    )
{
    HAL_StatusTypeDef retVal = HAL_OK;
    HAL_FLASH_Unlock();
    FLASH_Erase_Sector(FLASH_SECTOR_23, FLASH_VOLTAGE_RANGE_3);
    for(uint32_t n = 0; n < sizeof(persistedData); n++)
    {
        if((retVal = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, ADDR_FLASH_SECTOR_23 + n, ((uint8_t*)&persistedData)[n])) != HAL_OK)
        {
            printf("Flash Write Error @ 0x%08x\r\n", ADDR_FLASH_SECTOR_23 + n);
            goto Cleanup;
        }
    }
Cleanup:
    HAL_FLASH_Lock();
}