/**************************************************************************//**
* @file uid.c
* @brief
* APROM bind UID source file
*
* @note
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "uid.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Define global variables and constants */
/*---------------------------------------------------------------------------------------------------------*/
const uint32_t g_au32Password[FMC_FLASH_PAGE_SIZE / 4] __attribute__((at(PASSWORD_ADDR))) = {INIT_PASSWORD_0, INIT_PASSWORD_1, INIT_PASSWORD_2};
/**
* @brief Read UID.
* @param[in] au32Uid Buffer to storage UID.
* [url=home.php?mod=space&uid=266161]@return[/url] None.
*/
void UID_Read(uint32_t au32Uid[3])
{
uint8_t i;
FMC_Open();
for(i = 0; i < 3; i++)
{
au32Uid[i] = FMC_ReadUID(i);
}
FMC_Close();
}
/**
* @brief UID Encrypt.
* @param[in] au32Uid Buffer to storage UID.
* @return None.
* [url=home.php?mod=space&uid=1543424]@Details[/url] Encrypt the UID, users can change it according to their encryption algorithm.
*/
void UID_Encrypt(uint32_t au32Uid[3])
{
uint8_t i;
/* Encrypt the UID, users can change it according to their encryption algorithm */
for(i = 0; i < 3; i++)
{
au32Uid[i] = (~au32Uid[i]) * 5 / i;
}
}
/**
* @brief Write password to APROM.
* @param[in] au32Password The Buffer of the password.
* @return None.
*/
void UID_PasswordWrite(uint32_t au32Password[3])
{
FMC_Open();
FMC_ENABLE_AP_UPDATE();
FMC_Erase((uint32_t)g_au32Password);
FMC_Write((uint32_t)&g_au32Password[0], au32Password[0]);
FMC_Write((uint32_t)&g_au32Password[1], au32Password[1]);
FMC_Write((uint32_t)&g_au32Password[2], au32Password[2]);
FMC_DISABLE_AP_UPDATE();
FMC_Close();
}
/**
* @brief Check password.
* @param[in] au32Uid Buffer to storage UID.
* @return Password right or not.
* @retval 0 Password error.
* @retval 1 Password right.
*/
uint8_t UID_PasswordCheck(void)
{
uint8_t i;
uint32_t au32Uid[3];
uint32_t *pu32Password = (uint32_t *)g_au32Password;
if((pu32Password[0] == INIT_PASSWORD_0) && (pu32Password[1] == INIT_PASSWORD_1) && (pu32Password[2] == INIT_PASSWORD_2))
{
UID_Read(au32Uid);
UID_Encrypt(au32Uid);
UID_PasswordWrite(au32Uid);
}
else
{
UID_Read(au32Uid);
UID_Encrypt(au32Uid);
for(i = 0; i < 3; i++)
{
if(au32Uid[i] != pu32Password[i])
{
return 0;
}
}
}
return 1;
}
/*** (C) COPYRIGHT 2021 Nuvoton Technology Corp. ***/
|