Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_plat_update.c Source File

pal_plat_update.c

00001 /*******************************************************************************
00002  * Copyright 2016, 2017 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 #include "pal.h"
00018 #include "pal_plat_update.h"
00019 
00020 #include "fsl_device_registers.h"
00021 #include "fsl_debug_console.h"
00022 #include "board.h"
00023 #include "clock_config.h"
00024 #include "fsl_flash.h"
00025 
00026 #define TRACE_GROUP "PAL"
00027 
00028 #define PAL_UPDATE_JOURNAL_SIZE 0x80000UL
00029 #define PAL_UPDATE_JOURNAL_START_OFFSET 0x80000UL
00030 
00031 #if (!defined(PAL_UPDATE_ACTIVE_METADATA_HEADER_OFFSET))
00032 #define PAL_UPDATE_ACTIVE_METADATA_HEADER_OFFSET 0x80000UL
00033 #endif
00034 
00035 #define SIZEOF_SHA256 256/8
00036 #define FIRMWARE_HEADER_MAGIC   0x5a51b3d4UL
00037 #define FIRMWARE_HEADER_VERSION 1
00038 
00039 PAL_PRIVATE FirmwareHeader_t g_palFirmwareHeader;
00040 PAL_PRIVATE bool g_headerWasWritten = false;
00041 
00042 PAL_PRIVATE flash_config_t g_flashDriver;
00043 
00044 
00045 /*
00046  * call back functions
00047  *
00048  */
00049 
00050 PAL_PRIVATE palImageSignalEvent_t g_palUpdateServiceCBfunc;
00051 
00052 /*
00053  * WARNING: please do not change this function!
00054  * this function loads a call back function received from the upper layer (service).
00055  * the call back should be called at the end of each function (except pal_plat_imageGetDirectMemAccess)
00056  * the call back receives the event type that just happened defined by the ENUM  palImageEvents_t.
00057  *
00058  * if you will not call the call back at the end the service behaver will be undefined
00059  */
00060 palStatus_t pal_plat_imageInitAPI(palImageSignalEvent_t CBfunction)
00061 {
00062     if (!CBfunction)
00063     {
00064         return PAL_ERR_INVALID_ARGUMENT ;
00065     }
00066     g_palUpdateServiceCBfunc = CBfunction;
00067     g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_INIT);
00068 
00069     return PAL_SUCCESS;
00070 }
00071 
00072 palStatus_t pal_plat_imageDeInit(void)
00073 {
00074     palStatus_t status = PAL_SUCCESS;
00075     return status;
00076 }
00077 
00078 
00079 palStatus_t pal_plat_imageGetMaxNumberOfImages(uint8_t *imageNumber)
00080 {
00081     return PAL_ERR_NOT_IMPLEMENTED;
00082 }
00083 
00084 palStatus_t pal_plat_imageSetVersion(palImageId_t imageId, const palConstBuffer_t* version)
00085 {
00086     return PAL_ERR_NOT_IMPLEMENTED;
00087 }
00088 
00089 palStatus_t pal_plat_imageGetDirectMemAccess(palImageId_t imageId, void** imagePtr, size_t *imageSizeInBytes)
00090 {
00091     return PAL_ERR_NOT_IMPLEMENTED;
00092 }
00093 
00094 palStatus_t pal_plat_imageActivate(palImageId_t imageId)
00095 {
00096     return PAL_ERR_NOT_IMPLEMENTED;
00097 }
00098 
00099 
00100 palStatus_t pal_plat_imageGetActiveHash(palBuffer_t *hash)
00101 {
00102 
00103     return PAL_ERR_NOT_IMPLEMENTED;
00104 }
00105 
00106 //Retrieve the version of the active image to version buffer with size set to version bufferLength.
00107 palStatus_t pal_plat_imageGetActiveVersion (palBuffer_t* version)
00108 {
00109     return PAL_ERR_NOT_IMPLEMENTED;
00110 }
00111 
00112 //Writing the value of active image hash stored in hashValue to memory
00113 palStatus_t pal_plat_imageWriteHashToMemory (const palConstBuffer_t* const hashValue)
00114 {
00115     return PAL_ERR_NOT_IMPLEMENTED;
00116 }
00117 
00118 
00119 /*
00120  * init apis
00121  */
00122 
00123 
00124 
00125 //fill the image header data for writing, the writing will occur in the 1st image write
00126 palStatus_t pal_plat_imageSetHeader(palImageId_t imageId,palImageHeaderDeails_t *details)
00127 {
00128     PAL_LOG_DBG(">>%s\r\n",__FUNCTION__);
00129     palStatus_t status = PAL_SUCCESS;
00130     g_headerWasWritten = false; // set that the image was not written yet
00131     memset(&g_palFirmwareHeader,0,sizeof(g_palFirmwareHeader));
00132     g_palFirmwareHeader.totalSize = details->imageSize + sizeof(FirmwareHeader_t);
00133     g_palFirmwareHeader.magic = FIRMWARE_HEADER_MAGIC;
00134     g_palFirmwareHeader.version = FIRMWARE_HEADER_VERSION;
00135     g_palFirmwareHeader.firmwareVersion = details->version;
00136 
00137     memcpy(g_palFirmwareHeader.firmwareSHA256,details->hash.buffer,SIZEOF_SHA256);
00138     /*
00139      * calculating and setting the checksum of the header.
00140      * Have to call to crcInit  before use.
00141      */
00142    // crcInit();
00143    // g_palFirmwareHeader.checksum =  crcFast((const unsigned char *)&g_palFirmwareHeader, (int)sizeof(g_palFirmwareHeader));
00144     return  status;
00145 }
00146 
00147 
00148 
00149 palStatus_t pal_plat_imageReserveSpace(palImageId_t imageId, size_t imageSize)
00150 {
00151     status_t result = PAL_SUCCESS;
00152     memset(&g_flashDriver, 0, sizeof(g_flashDriver));
00153     result = FLASH_Init(&g_flashDriver);
00154     if (kStatus_FLASH_Success != result)
00155     {
00156         g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_ERROR);
00157         return PAL_ERR_UPDATE_ERROR ;
00158     }
00159 
00160     result = FLASH_Erase(&g_flashDriver, PAL_UPDATE_JOURNAL_START_OFFSET, imageSize, kFLASH_apiEraseKey);
00161     if (kStatus_FLASH_Success != result)
00162     {
00163         g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_ERROR);
00164         return PAL_ERR_UPDATE_ERROR ;
00165     }
00166 
00167     result = FLASH_VerifyErase(&g_flashDriver, PAL_UPDATE_JOURNAL_START_OFFSET, imageSize, kFLASH_marginValueUser);
00168     if (kStatus_FLASH_Success != result)
00169     {
00170         g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_ERROR);
00171         return PAL_ERR_UPDATE_ERROR ;
00172     }
00173     g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_PREPARE);
00174 
00175     return PAL_SUCCESS;
00176 }
00177 
00178 
00179 
00180 
00181 
00182 /*
00183  * write API
00184  */
00185 
00186 
00187 
00188 
00189 
00190 palStatus_t pal_plat_imageWrite(palImageId_t imageId, size_t offset, palConstBuffer_t *chunk)
00191 {
00192     status_t result = PAL_SUCCESS;
00193     uint32_t failAddr, failDat;
00194     //if header was not written - write header
00195     if (!g_headerWasWritten)
00196     {
00197         result = FLASH_Program(
00198             &g_flashDriver,
00199             PAL_UPDATE_JOURNAL_START_OFFSET,
00200             (uint32_t *)(&g_palFirmwareHeader),
00201             sizeof(g_palFirmwareHeader)
00202             );
00203         if (kStatus_FLASH_Success != result)
00204         {
00205             g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_ERROR);
00206             return PAL_ERR_UPDATE_ERROR ;
00207         }
00208         g_headerWasWritten = true;
00209     }
00210     result = FLASH_Program(
00211         &g_flashDriver,
00212         (PAL_UPDATE_JOURNAL_START_OFFSET + sizeof(g_palFirmwareHeader) + offset),
00213         (uint32_t *)(chunk->buffer),
00214         chunk->bufferLength
00215         );
00216     if (kStatus_FLASH_Success != result)
00217     {
00218         g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_ERROR);
00219         return PAL_ERR_UPDATE_ERROR ;
00220     }
00221 
00222     /* Program Check user margin levels */
00223     result = FLASH_VerifyProgram(
00224         &g_flashDriver,
00225         (PAL_UPDATE_JOURNAL_START_OFFSET + sizeof(g_palFirmwareHeader) + offset),
00226         chunk->bufferLength,
00227         (const uint32_t *)(chunk->buffer),
00228         kFLASH_marginValueUser,
00229         &failAddr,
00230         &failDat
00231         );
00232     if (kStatus_FLASH_Success != result)
00233     {
00234         g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_ERROR);
00235         return PAL_ERR_UPDATE_ERROR ;
00236     }
00237     g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_WRITE);
00238     return PAL_SUCCESS;
00239 }
00240 
00241 
00242 /*
00243  * read APIs
00244  */
00245 
00246 
00247 
00248 
00249 palStatus_t pal_plat_imageReadToBuffer(palImageId_t imageId, size_t offset, palBuffer_t *chunk)
00250 {
00251     uint32_t imageSize = g_palFirmwareHeader.totalSize - sizeof(g_palFirmwareHeader); //totalSize - headerSize
00252     if ((offset + chunk->maxBufferLength) <= imageSize)
00253     {
00254         chunk->bufferLength = chunk->maxBufferLength;
00255     }
00256     else
00257     {
00258         chunk->bufferLength =chunk->maxBufferLength + imageSize - offset;
00259     }
00260     memcpy(chunk->buffer, (void*)(PAL_UPDATE_JOURNAL_START_OFFSET + sizeof(g_palFirmwareHeader) + offset) , chunk->bufferLength);
00261     g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_READTOBUFFER);
00262     return PAL_SUCCESS;
00263 }
00264 
00265 
00266 
00267 /*
00268  * commit functions
00269  * */
00270 
00271 
00272 
00273 palStatus_t pal_plat_imageFlush(palImageId_t package_id)
00274 {
00275     PAL_LOG_DBG(">>%s\r\n",__FUNCTION__);
00276     g_palUpdateServiceCBfunc(PAL_IMAGE_EVENT_FINALIZE);
00277     PAL_LOG_DBG("<<%s\r\n",__FUNCTION__);
00278     return PAL_SUCCESS;
00279 }