1 /*
2  * Copyright 2022-2023 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "fsl_silicon_id.h"
8 #if defined(SIM)
9 #include "fsl_sim.h"
10 #elif defined(FSL_FEATURE_SYSCON_IAP_ENTRY_LOCATION)
11 #include "fsl_iap.h"
12 #elif (defined(FSL_FEATURE_SYSCON_ROMAPI) && (FSL_FEATURE_SYSCON_ROMAPI == 1))
13 #include "fsl_flash.h"
14 #include "fsl_flash_ffr.h"
15 #endif
16 
17 /* Component ID definition, used by tools. */
18 #ifndef FSL_COMPONENT_ID
19 #define FSL_COMPONENT_ID "component.silicon_id"
20 #endif
21 
SILICONID_GetID(uint8_t * siliconId,uint32_t * idLen)22 status_t SILICONID_GetID(uint8_t *siliconId, uint32_t *idLen)
23 {
24     assert((siliconId != NULL) && (idLen != NULL) && (*idLen != 0U));
25 
26     status_t result = kStatus_Fail;
27 
28 #if defined(SIM)
29     assert(sizeof(sim_uid_t) <= SILICONID_MAX_LENGTH);
30     uint32_t readBytes = *idLen;
31     sim_uid_t uid;
32     SIM_GetUniqueId(&uid);
33     readBytes = (readBytes > sizeof(sim_uid_t)) ? sizeof(sim_uid_t) : readBytes;
34     (void)memcpy((void *)&siliconId[0], (void *)(uint8_t *)&uid, readBytes);
35     *idLen = readBytes;
36     result = kStatus_Success;
37 #elif defined(FSL_FEATURE_SYSCON_IAP_ENTRY_LOCATION)
38     uint32_t readBytes = *idLen;
39     uint8_t uid[16];
40     result = IAP_ReadUniqueID((uint32_t *)(uintptr_t)&uid[0]);
41     if (result == kStatus_Success)
42     {
43         readBytes = (readBytes > 16U) ? 16U : readBytes;
44         (void)memcpy((void *)&siliconId[0], (void *)&uid[0], readBytes);
45         *idLen = readBytes;
46     }
47     else
48     {
49         *idLen = 0;
50     }
51 #elif (defined(FSL_FEATURE_SYSCON_ROMAPI) && FSL_FEATURE_SYSCON_ROMAPI)
52     flash_config_t s_flashDriver;
53     (void)memset(&s_flashDriver, 0, sizeof(flash_config_t));
54 
55     result = FLASH_Init(&s_flashDriver);
56 
57     if (result == kStatus_Success)
58     {
59         result = FFR_Init(&s_flashDriver);
60 
61         if (result == kStatus_Success)
62         {
63             result = FFR_GetUUID(&s_flashDriver, siliconId);
64             *idLen = 4;
65         }
66     }
67 #else
68     extern status_t SILICONID_ReadUniqueID(uint8_t *siliconId, uint32_t *idLen);
69     result = SILICONID_ReadUniqueID(&siliconId[0], idLen);
70 #endif
71 
72     return result;
73 }
74 
SILICONID_ConvertToMacAddr(uint8_t (* macAddr)[6])75 status_t SILICONID_ConvertToMacAddr(uint8_t (*macAddr)[6])
76 {
77     status_t result;
78 
79     uint8_t siliconId[SILICONID_MAX_LENGTH];
80     uint32_t idLen = sizeof(siliconId);
81 
82     result = SILICONID_GetID(&siliconId[0], &idLen);
83     if (result != kStatus_Success)
84     {
85         return result;
86     }
87 
88     /* Valid ID should > 3. */
89     assert(idLen >= 3U);
90 
91     /* Set NXP OUI. */
92     macAddr[0][0] = 0x54;
93     macAddr[0][1] = 0x27;
94     macAddr[0][2] = 0x8d;
95 
96     /* Set with silicon id. */
97     macAddr[0][3] = siliconId[0];
98     macAddr[0][4] = siliconId[1];
99     macAddr[0][5] = siliconId[2];
100 
101     return result;
102 }
103