Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_plat_rot_sotp.c Source File

pal_plat_rot_sotp.c

00001 /*******************************************************************************
00002  * Copyright 2016-2018 ARM Ltd.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *******************************************************************************/
00016 
00017 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00018 #include "pal.h"
00019 #include "pal_plat_rot.h"
00020 
00021 // If there is no "HW ROT", this code is enabled. It will in practice either use a pre-generated
00022 // ROT in SOTP or generate it once on the fly.
00023 #if (PAL_USE_HW_ROT == 0)
00024 
00025 #include "sotp.h"
00026 
00027 #define TRACE_GROUP "PAL"
00028 
00029 //Error Translation from SOTP module to PAL
00030 PAL_PRIVATE palStatus_t pal_osSotpErrorTranslation(sotp_result_e err)
00031 {
00032     palStatus_t ret;
00033     switch(err)
00034     {
00035         case SOTP_SUCCESS:
00036             ret = PAL_SUCCESS;
00037             break;
00038         case SOTP_BAD_VALUE:
00039             ret = PAL_ERR_INVALID_ARGUMENT ;
00040             break;
00041 
00042         case SOTP_BUFF_TOO_SMALL:
00043             ret = PAL_ERR_BUFFER_TOO_SMALL ;
00044             break;
00045 
00046         case SOTP_BUFF_NOT_ALIGNED:
00047             ret = PAL_ERR_RTOS_BUFFER_NOT_ALIGNED ;
00048             break;
00049 
00050         case SOTP_NOT_FOUND:
00051             ret = PAL_ERR_ITEM_NOT_EXIST ;
00052             break;
00053 
00054         case SOTP_READ_ERROR:
00055         case SOTP_DATA_CORRUPT:
00056         case SOTP_OS_ERROR:
00057         default:
00058             ret = PAL_ERR_GENERIC_FAILURE ;
00059             break;
00060     }
00061     return ret;
00062 }
00063 
00064 palStatus_t pal_plat_osGetRoT(uint8_t * key, size_t keyLenBytes)
00065 {
00066     sotp_result_e sotpStatus;
00067     uint16_t actual_size;
00068 
00069     sotpStatus = sotp_get(SOTP_TYPE_ROT, keyLenBytes, (uint32_t *)key, &actual_size);
00070     return pal_osSotpErrorTranslation(sotpStatus);
00071 
00072 }
00073 
00074 palStatus_t pal_plat_osSetRoT(uint8_t * key, size_t keyLenBytes)
00075 {
00076     sotp_result_e sotpStatus;
00077     uint16_t actual_size;
00078     uint8_t rotBuffer[PAL_DEVICE_KEY_SIZE_IN_BYTES] __attribute__((aligned(4))) = { 0 };
00079 
00080     if (keyLenBytes != PAL_DEVICE_KEY_SIZE_IN_BYTES || key == NULL) {
00081         return PAL_ERR_INVALID_ARGUMENT ;
00082     }
00083 
00084     //Check if Rot already exists
00085     sotpStatus = sotp_get(SOTP_TYPE_ROT, keyLenBytes, (uint32_t *)rotBuffer, &actual_size);
00086     if (sotpStatus == SOTP_SUCCESS) {
00087         return PAL_ERR_ITEM_EXIST ;
00088     }
00089 
00090     //Copy RoT to aligned buffer
00091     memcpy(rotBuffer, key, PAL_DEVICE_KEY_SIZE_IN_BYTES);
00092 
00093     //Set RoT
00094     sotpStatus = sotp_set(SOTP_TYPE_ROT, PAL_DEVICE_KEY_SIZE_IN_BYTES, (uint32_t *)rotBuffer);
00095     return pal_osSotpErrorTranslation(sotpStatus);
00096 
00097 }
00098 #endif // (PAL_USE_HW_ROT == 0)
00099 #endif //#ifndef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT