From c3300b12a466a72c36c4c81905dd904f83425902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Fri, 8 Nov 2024 10:15:04 +0000 Subject: [PATCH] Adding support for OEM SKU and Serial Numbers (#3030) --- .../Include/nanoHAL_ConfigurationManager.h | 14 +++++++- src/HAL/nanoHAL_ConfigurationManager.c | 14 ++++++++ src/HAL/nanoHAL_ConfigurationManager_stubs.c | 17 +++++++++- src/HAL/nanoHAL_SystemInformation.cpp | 14 ++++---- .../targetHAL_ConfigurationManager.cpp | 8 +++++ .../targetHAL_ConfigurationManager.cpp | 8 +++++ .../targetHAL_ConfigurationManager.cpp | 33 +++++++++++++++++++ .../targetHAL_ConfigurationManager.cpp | 16 +++++++++ .../targetHAL_ConfigurationManager.cpp | 20 +++++++++++ .../targetHAL_ConfigurationManager.cpp | 8 +++++ ...etHAL_ConfigurationManager_CC13xx_26xx.cpp | 8 +++++ .../targetHAL_ConfigurationManager_CC32xx.cpp | 8 +++++ 12 files changed, 160 insertions(+), 8 deletions(-) diff --git a/src/HAL/Include/nanoHAL_ConfigurationManager.h b/src/HAL/Include/nanoHAL_ConfigurationManager.h index 27351f4eb1..dc15304b15 100644 --- a/src/HAL/Include/nanoHAL_ConfigurationManager.h +++ b/src/HAL/Include/nanoHAL_ConfigurationManager.h @@ -1,4 +1,4 @@ -// +// // Copyright (c) .NET Foundation and Contributors // See LICENSE file in the project root for full license information. // @@ -218,6 +218,18 @@ extern "C" // gets the HAL_Configuration_NetworkInterface configuration block that has the SpecificConfig Id, if that exists int32_t ConfigurationManager_FindNetworkConfigurationMatchingWirelessConfigurationFromId(uint32_t configurationId); + // Gets the OEM model SKU. + // This is defined as weak to allow the target/platform to provide the implementation. + void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize); + + // Gets the module serial number. + // This is defined as weak to allow the target/platform to provide the implementation. + void ConfigurationManager_GetModuleSerialNumber(char *serialNumber, size_t serialNumberSize); + + // Gets the system serial number. + // This is defined as weak to allow the target/platform to provide the implementation. + void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize); + #ifdef __cplusplus } #endif diff --git a/src/HAL/nanoHAL_ConfigurationManager.c b/src/HAL/nanoHAL_ConfigurationManager.c index d874a26181..cd678aab4a 100644 --- a/src/HAL/nanoHAL_ConfigurationManager.c +++ b/src/HAL/nanoHAL_ConfigurationManager.c @@ -339,3 +339,17 @@ __nfweak bool ConfigurationManager_CheckExistingConfigurationBlock( return memcmp(cursor1, cursor2, existingConfigBlockSize) == 0; } + +__nfweak void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize) +{ + // default implementation + // this is weak so the target can provide the implementation + memset(model, 0, modelSkuSize); +} + +__nfweak void ConfigurationManager_GetModuleSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + // default implementation + // this is weak so a manufacturer can provide a strong implementation + memset(serialNumber, 0, serialNumberSize); +} diff --git a/src/HAL/nanoHAL_ConfigurationManager_stubs.c b/src/HAL/nanoHAL_ConfigurationManager_stubs.c index 394f1255a8..18e3ea207f 100644 --- a/src/HAL/nanoHAL_ConfigurationManager_stubs.c +++ b/src/HAL/nanoHAL_ConfigurationManager_stubs.c @@ -5,7 +5,7 @@ #include -__nfweak void ConfigurationManager_Initialize(){}; +__nfweak void ConfigurationManager_Initialize() {}; __nfweak void *ConfigurationManager_FindNetworkConfigurationBlocks(uint32_t startAddress, uint32_t endAddress) { @@ -70,3 +70,18 @@ __nfweak bool ConfigurationManager_CheckExistingConfigurationBlock( return false; } + +__nfweak void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize) +{ + memset(model, 0, modelSkuSize); +} + +__nfweak void ConfigurationManager_GetModuleSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + memset(serialNumber, 0, serialNumberSize); +} + +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + memset(serialNumber, 0, serialNumberSize); +} diff --git a/src/HAL/nanoHAL_SystemInformation.cpp b/src/HAL/nanoHAL_SystemInformation.cpp index 4550ac7903..7bc18c5de0 100644 --- a/src/HAL/nanoHAL_SystemInformation.cpp +++ b/src/HAL/nanoHAL_SystemInformation.cpp @@ -32,15 +32,17 @@ bool GetHalSystemInfo(HalSystemInfo &systemInfo) len = sizeof(systemInfo.m_releaseInfo.TargetName); hal_strncpy_s((char *)&systemInfo.m_releaseInfo.TargetName[0], len, PLATFORMNAMESTRING, len - 1); - // we are not supporting this at this time // OEM_MODEL_SKU: - // memcpy((void*)&(systemInfo.m_OemModelInfo), (void*)&(g_ConfigurationSector.OEM_Model_SKU), - // sizeof(OEM_MODEL_SKU)); + ConfigurationManager_GetOemModelSku((char *)&systemInfo.m_OemModelInfo, sizeof(systemInfo.m_OemModelInfo)); - // we are not supporting this at this time // OEM_SERIAL_NUMBERS: - // memcpy((void*)&(systemInfo.m_OemSerialNumbers), (void*)&(g_ConfigurationSector.OemSerialNumbers), - // sizeof(OEM_SERIAL_NUMBERS)); + ConfigurationManager_GetModuleSerialNumber( + (char *)&systemInfo.m_OemSerialNumbers.module_serial_number, + sizeof(systemInfo.m_OemSerialNumbers.module_serial_number)); + + ConfigurationManager_GetSystemSerialNumber( + (char *)&systemInfo.m_OemSerialNumbers.system_serial_number, + sizeof(systemInfo.m_OemSerialNumbers.system_serial_number)); return TRUE; #endif diff --git a/targets/AzureRTOS/Maxim/_common/targetHAL_ConfigurationManager.cpp b/targets/AzureRTOS/Maxim/_common/targetHAL_ConfigurationManager.cpp index 8586764784..24c0ce0cf6 100644 --- a/targets/AzureRTOS/Maxim/_common/targetHAL_ConfigurationManager.cpp +++ b/targets/AzureRTOS/Maxim/_common/targetHAL_ConfigurationManager.cpp @@ -526,3 +526,11 @@ __nfweak bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface // can't create a "default" network config because we are lacking definition of a MAC address return FALSE; } + +// default implementation +// this is weak so a manufacturer can provide a strong implementation +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + // do the thing to get unique device ID + memset(serialNumber, 0, serialNumberSize); +} diff --git a/targets/AzureRTOS/ST/_common/targetHAL_ConfigurationManager.cpp b/targets/AzureRTOS/ST/_common/targetHAL_ConfigurationManager.cpp index 8746cdb2e6..c9f2d7d087 100644 --- a/targets/AzureRTOS/ST/_common/targetHAL_ConfigurationManager.cpp +++ b/targets/AzureRTOS/ST/_common/targetHAL_ConfigurationManager.cpp @@ -766,3 +766,11 @@ int32_t ConfigurationManager_FindNetworkConfigurationMatchingWirelessConfigurati // not found return -1; } + +// default implementation +// this is weak so a manufacturer can provide a strong implementation +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + // do the thing to get unique device ID + memset(serialNumber, 0, serialNumberSize); +} diff --git a/targets/AzureRTOS/SiliconLabs/_common/targetHAL_ConfigurationManager.cpp b/targets/AzureRTOS/SiliconLabs/_common/targetHAL_ConfigurationManager.cpp index da8eb29692..1d74024dd2 100644 --- a/targets/AzureRTOS/SiliconLabs/_common/targetHAL_ConfigurationManager.cpp +++ b/targets/AzureRTOS/SiliconLabs/_common/targetHAL_ConfigurationManager.cpp @@ -7,6 +7,7 @@ #include #include // #include +#include #if defined(WIFI_DRIVER_ISM43362) && defined(I_AM_NANOCLR) #include @@ -773,3 +774,35 @@ int32_t ConfigurationManager_FindNetworkConfigurationMatchingWirelessConfigurati // not found return -1; } + +// default implementation +// this is weak so a manufacturer can provide a strong implementation +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + memset(serialNumber, 0, serialNumberSize); + + // Use the device Unique ID which is 64 bits long + // Put it in the LSB of the serial number + int startOfId = serialNumberSize - 8; + + // high 32 bits + uint32_t rawId = DEVINFO->UNIQUEH; + for (int i = 3; i >= 0; --i) + { + serialNumber[startOfId + i] = rawId & 0xFF; + rawId >>= 8; + } + + // low 32 bits + rawId = DEVINFO->UNIQUEL; + for (int i = 7; i >= 4; --i) + { + serialNumber[startOfId + i] = rawId & 0xFF; + rawId >>= 8; + } + + // Disambiguation is needed because the hardware-specific identifier used to create the + // default serial number on other platforms may be in the same range. + // Set the first byte to a number that is unique (within the nanoFramework CLR) for the Giant Gecko. + serialNumber[0] = 3; +} diff --git a/targets/ChibiOS/_common/targetHAL_ConfigurationManager.cpp b/targets/ChibiOS/_common/targetHAL_ConfigurationManager.cpp index 680d2f4f69..1649fdef6f 100644 --- a/targets/ChibiOS/_common/targetHAL_ConfigurationManager.cpp +++ b/targets/ChibiOS/_common/targetHAL_ConfigurationManager.cpp @@ -641,3 +641,19 @@ __nfweak bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface // can't create a "default" network config because we are lacking definition of a MAC address return FALSE; } + +// default implementation +// this is weak so a manufacturer can provide a strong implementation +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + // do the thing to get unique device ID + memset(serialNumber, 0, serialNumberSize); + // Use the 96 bit unique device ID => 12 bytes + // memory copy from the address pointed by UID_BASE define (from STM32 HAL) + memcpy(&serialNumber[serialNumberSize - 12], ((uint8_t *)UID_BASE), 12); + + // Disambiguation is needed because the hardware-specific identifier used to create the + // default serial number on other platforms may be in the same range. + // Set the first byte to a number that is unique (within the nanoFramework CLR) for STM32. + serialNumber[0] = 2; +} diff --git a/targets/ESP32/_common/targetHAL_ConfigurationManager.cpp b/targets/ESP32/_common/targetHAL_ConfigurationManager.cpp index 2c024928e1..c45c8d8c16 100644 --- a/targets/ESP32/_common/targetHAL_ConfigurationManager.cpp +++ b/targets/ESP32/_common/targetHAL_ConfigurationManager.cpp @@ -896,3 +896,23 @@ HAL_Configuration_X509DeviceCertificate *ConfigurationManager_GetDeviceCertifica // not found, or failed to allocate memory return NULL; } + +// default implementation +// this is weak so a manufacturer can provide a strong implementation +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + memset(serialNumber, 0, serialNumberSize); + + // Use the factory-provided MAC address as unique ID + uint8_t macAddress[6]; + esp_err_t err = esp_read_mac(macAddress, ESP_MAC_EFUSE_FACTORY); + if (err == ESP_OK) + { + memcpy(&serialNumber[serialNumberSize - sizeof(macAddress)], macAddress, sizeof(macAddress)); + + // Disambiguation is needed because the hardware-specific identifier used to create the + // default serial number on other platforms may be in the same range. + // Set the first byte to a number that is unique (within the nanoFramework CLR) for ESP32. + serialNumber[0] = 1; + } +} diff --git a/targets/FreeRTOS/NXP/_common/targetHAL_ConfigurationManager.cpp b/targets/FreeRTOS/NXP/_common/targetHAL_ConfigurationManager.cpp index 36f4cc0e6c..5e13cd372b 100644 --- a/targets/FreeRTOS/NXP/_common/targetHAL_ConfigurationManager.cpp +++ b/targets/FreeRTOS/NXP/_common/targetHAL_ConfigurationManager.cpp @@ -420,3 +420,11 @@ bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface *pconfig, // can't create a "default" network config because we are lacking definition of a MAC address return FALSE; } + +// default implementation +// this is weak so a manufacturer can provide a strong implementation +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + // do the thing to get unique device ID + memset(serialNumber, 0, serialNumberSize); +} diff --git a/targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC13xx_26xx.cpp b/targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC13xx_26xx.cpp index 4681cd610a..4261ae5257 100644 --- a/targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC13xx_26xx.cpp +++ b/targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC13xx_26xx.cpp @@ -594,3 +594,11 @@ bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface *pconfig, return true; } + +// default implementation +// this is weak so a manufacturer can provide a strong implementation +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + // do the thing to get unique device ID + memset(serialNumber, 0, serialNumberSize); +} diff --git a/targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC32xx.cpp b/targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC32xx.cpp index 157fe133cc..f4ad372220 100644 --- a/targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC32xx.cpp +++ b/targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC32xx.cpp @@ -593,3 +593,11 @@ bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface *pconfig, return true; } + +// default implementation +// this is weak so a manufacturer can provide a strong implementation +__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize) +{ + // do the thing to get unique device ID + memset(serialNumber, 0, serialNumberSize); +}