SimpleLink CC3120/CC3220 Host Driver  Version 2.0.1.18
Simplifies the implementation of Internet connectivity
FileSystem

Provides file system capabilities to TI's CC31XX that can be used by both the CC31XX device and the user. More...

Functions

_i32 sl_FsOpen (const _u8 *pFileName, const _u32 AccessModeAndMaxSize, _u32 *pToken)
 open file for read or write from/to storage device More...
 
_i16 sl_FsClose (const _i32 FileHdl, const _u8 *pCeritificateFileName, const _u8 *pSignature, const _u32 SignatureLen)
 Close file in storage device. More...
 
_i32 sl_FsRead (const _i32 FileHdl, _u32 Offset, _u8 *pData, _u32 Len)
 Read block of data from a file in storage device. More...
 
_i32 sl_FsWrite (const _i32 FileHdl, _u32 Offset, _u8 *pData, _u32 Len)
 Write block of data to a file in storage device. More...
 
_i16 sl_FsGetInfo (const _u8 *pFileName, const _u32 Token, SlFsFileInfo_t *pFsFileInfo)
 Get information of a file. More...
 
_i16 sl_FsDel (const _u8 *pFileName, const _u32 Token)
 Delete specific file from a storage or all files from a storage (format) More...
 
_i32 sl_FsCtl (SlFsCtl_e Command, _u32 Token, _u8 *pFileName, const _u8 *pData, _u16 DataLen, _u8 *pOutputData, _u16 OutputDataLen, _u32 *pNewToken)
 Controls various file system operations. More...
 
_i32 sl_FsProgram (const _u8 *pData, _u16 Len, const _u8 *pKey, _u32 Flags)
 Enables to format and configure the device with pre-prepared configuration. More...
 
_i32 sl_FsGetFileList (_i32 *pIndex, _u8 Count, _u8 MaxEntryLen, _u8 *pBuff, SlFileListFlags_t Flags)
 The list of file names, the files are retrieve in chunks. More...
 

Enumerations

enum  SlFsTokenId_e {
  SL_FS_TOKEN_MASTER = 0,
  SL_FS_TOKEN_WRITE_READ = 1,
  SL_FS_TOKEN_WRITE_ONLY = 2,
  SL_FS_TOKEN_READ_ONLY = 3
}
 
enum  SlFsCtl_e {
  SL_FS_CTL_RESTORE = 0,
  SL_FS_CTL_ROLLBACK = 1,
  SL_FS_CTL_COMMIT = 2,
  SL_FS_CTL_RENAME = 3,
  SL_FS_CTL_GET_STORAGE_INFO = 5,
  SL_FS_CTL_BUNDLE_ROLLBACK = 6,
  SL_FS_CTL_BUNDLE_COMMIT = 7
}
 
enum  SlFsBundleState_e {
  SL_FS_BUNDLE_STATE_STOPPED = 0,
  SL_FS_BUNDLE_STATE_STARTED = 1,
  SL_FS_BUNDLE_STATE_PENDING_COMMIT = 3
}
 
enum  SlFsRetToFactoryOper_e {
  SL_FS_FACTORY_RET_TO_IMAGE = 0,
  SL_FS_FACTORY_RET_TO_DEFAULT = 2
}
 
enum  SlFileListFlags_t { SL_FS_GET_FILE_ATTRIBUTES = 0x1 }
 

Detailed Description

Provides file system capabilities to TI's CC31XX that can be used by both the CC31XX device and the user.

Function Documentation

§ sl_FsClose()

_i16 sl_FsClose ( const _i32  FileHdl,
const _u8 *  pCeritificateFileName,
const _u8 *  pSignature,
const _u32  SignatureLen 
)

Close file in storage device.

Parameters
[in]FileHdlPointer to the file (assigned from sl_FsOpen)
[in]pCeritificateFileNameCertificate file, or NULL if irrelevant.
[in]pSignatureThe signature is SHA-1, the certificate chain may include SHA-256
[in]SignatureLenThe signature actual length
Returns
Zero on success, or a negative value if an error occurred
See also
sl_FsRead sl_FsWrite sl_FsOpen
Note
Call the fs_Close with signature = 'A' signature len = 1 for activating an abort action
Creating signature : OpenSSL> dgst -binary -sha1 -sign <file-location><private_key>.pem -out <file-location><output>.sig <file-location><input>.txt
Warning
Examples
  • Closing file:
    _i16 RetVal;
    RetVal = sl_FsClose(FileHandle,0,0,0);

  • Aborting file:
    _u8 Signature;
    Signature = 'A';
    sl_FsClose(FileHandle,0,&Signature, 1);
Note
In case the file was opened as not secure file or as secure-not signed, any certificate or signature provided are ignored, those fields should be set to NULL.

Definition at line 244 of file fs.c.

245 {
246  _SlFsCloseMsg_u Msg;
247  _SlCmdExt_t ExtCtrl;
248 
249  _SlDrvMemZero(&Msg, (_u16)sizeof(SlFsCloseCommand_t));
250 
251  /* verify that this api is allowed. if not allowed then
252  ignore the API execution and return immediately with an error */
253  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
254 
255 
256  Msg.Cmd.FileHandle = (_u32)FileHdl;
257  if( pCeritificateFileName != NULL )
258  {
259  Msg.Cmd.CertificFileNameLength = (_u32)((_SlFsStrlen(pCeritificateFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
260  }
261  Msg.Cmd.SignatureLen = SignatureLen;
262 
263  _SlDrvMemZero(&ExtCtrl, (_u16)sizeof(_SlCmdExt_t));
264 
265  ExtCtrl.TxPayload1Len = (_u16)(((SignatureLen+3) & (~3))); /* align */
266  ExtCtrl.pTxPayload1 = (_u8*)pSignature;
267  ExtCtrl.RxPayloadLen = (_i16)Msg.Cmd.CertificFileNameLength;
268  ExtCtrl.pRxPayload = (_u8*)pCeritificateFileName; /* Add signature */
269 
270  if(ExtCtrl.pRxPayload != NULL && ExtCtrl.RxPayloadLen != 0)
271  {
272  ExtCtrl.RxPayloadLen = ExtCtrl.RxPayloadLen * (-1);
273  }
274 
275  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsCloseCmdCtrl, &Msg, &ExtCtrl));
276 
277  return (_i16)((_i16)Msg.Rsp.status);
278 }

§ sl_FsCtl()

_i32 sl_FsCtl ( SlFsCtl_e  Command,
_u32  Token,
_u8 *  pFileName,
const _u8 *  pData,
_u16  DataLen,
_u8 *  pOutputData,
_u16  OutputDataLen,
_u32 *  pNewToken 
)

Controls various file system operations.

Parameters
[in]Command,thecommand to execute,
See also
SlFsCtl_e SL_FS_CTL_RESTORE , Return to factory default, return to factory image , see fs programming SL_FS_CTL_ROLLBACK , Roll-back file which was created with 'SL_FS_WRITE_MUST_COMMIT' SL_FS_CTL_COMMIT,Commit file which was created with 'SL_FS_WRITE_MUST_COMMIT' SL_FS_CTL_RENAME, Rename file SL_FS_CTL_GET_STORAGE_INFO, Total size of storage , available size of storage SL_FS_CTL_BUNDLE_ROLLBACK, Rollback bundle files SL_FS_CTL_BUNDLE_COMMIT, Commit Bundle files
Parameters
[in]TokenSet to NULL if not relevant to the command
[in]pFileNameSet to NULL if not relevant to the command
[in]pDataThe data according the command.
[in]DataLenLength of data buffer
[out]pOutputDataBuffer for the output data
[out]OutputDataLenLength of the output data buffer
[out]pNewTokenThe new valid file token, if irrelevant can be set to NULL.
Returns
  • Zero on success, or a negative value if an error occurred
  • For SL_FS_CTL_BUNDLE_ROLLBACK, On success bundle the new bundle state is returned (see SlFsBundleState_e) else negative error number
  • For SL_FS_CTL_BUNDLE_COMMIT, On success the new bundle state is returned (see SlFsBundleState_e) else negative error number
See also
Note
belongs to ext_api
Warning
Examples
  • SL_FS_CTL_ROLLBACK:
    FsControl.IncludeFilters = 0;
    slRetVal = sl_FsCtl( (SlFsCtl_e)SL_FS_CTL_FILE_ROLLBACK, Token, NWPfileName ,(_u8 *)&FsControl, sizeof(SlFsControl_t), NULL, 0 , pNewToken);

  • SL_FS_CTL_COMMIT:
    FsControl.IncludeFilters = 0;
    slRetVal = sl_FsCtl(SL_FS_CTL_COMMIT, Token, NWPfileName ,(_u8 *)&FsControl, sizeof(SlFsControl_t), NULL, 0, pNewToken );

  • SL_FS_CTL_RENAME:
    slRetVal = sl_FsCtl(SL_FS_CTL_RENAME, Token, NWPfileName, NewFileName, 0, NULL, 0, NULL );

  • SL_FS_CTL_GET_STORAGE_INFO:
    _i32 GetStorageInfo( SlFsControlGetStorageInfoResponse_t* pSlFsControlGetStorageInfoResponse )
    {
    _i32 slRetVal;
    slRetVal = sl_FsCtl( ( SlFsCtl_e)SL_FS_CTL_GET_STORAGE_INFO, 0, NULL , NULL , 0, (_u8 *)pSlFsControlGetStorageInfoResponse, sizeof(SlFsControlGetStorageInfoResponse_t), NULL );
    return slRetVal;
    }

  • SL_FS_CTL_RESTORE:
    //Return 0 for OK, else Error
    _i32 ProgramRetToImage( )
    {
    _i32 slRetVal;
    SlFsRetToFactoryCommand_t RetToFactoryCommand;
    _i32 RetVal, ExtendedError;
    RetToFactoryCommand.Operation = SL_FS_FACTORY_RET_TO_IMAGE;
    slRetVal = sl_FsCtl( (SlFsCtl_e)SL_FS_CTL_RESTORE, 0, NULL , (_u8 *)&RetToFactoryCommand , sizeof(SlFsRetToFactoryCommand_t), NULL, 0 , NULL );
    if ((_i32)slRetVal < 0)
    {
    //Pay attention, for this function the slRetVal is composed from Signed RetVal & extended error
    RetVal = (_i16)slRetVal>> 16;
    ExtendedError = (_u16)slRetVal& 0xFFFF;
    printf("\tError SL_FS_FACTORY_RET_TO_IMAGE, 5d, %d\n", RetVal, ExtendedError);
    return slRetVal;
    }
    //Reset
    sl_Stop(0);
    Sleep(1000);
    sl_Start(NULL, NULL, NULL);
    return slRetVal;
    }

  • SL_FS_CTL_BUNDLE_ROLLBACK:
    //return 0 for O.K else negative
    _i32 BundleRollback()
    {
    _i32 slRetVal = 0;
    SlFsControl_t FsControl;
    FsControl.IncludeFilters = 0; //Use default behaviour
    slRetVal = sl_FsCtl( (SlFsCtl_e)SL_FS_CTL_BUNDLE_ROLLBACK, 0, NULL ,(_u8 *)&FsControl, sizeof(SlFsControl_t), NULL, 0 , NULL);
    return slRetVal;
    }

  • SL_FS_CTL_BUNDLE_COMMIT:
    //return 0 for O.K else negative
    _i32 BundleCommit()
    {
    _i32 slRetVal = 0;
    SlFsControl_t FsControl;
    FsControl.IncludeFilters = 0; //Use default behaviour
    slRetVal = sl_FsCtl( (SlFsCtl_e)SL_FS_CTL_BUNDLE_COMMIT, 0, NULL ,(_u8 *)&FsControl, sizeof(SlFsControl_t), NULL, 0 , NULL);
    return slRetVal;
    }

Definition at line 594 of file fs.c.

595 {
596  _SlFsFileSysControlMsg_u Msg;
597  _SlCmdExt_t CmdExt;
598 
599  /* verify that this api is allowed. if not allowed then
600  ignore the API execution and return immediately with an error */
601  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
602 
603  Msg.Cmd.Token = Token;
604  Msg.Cmd.Operation = (_u8)Command;
605 
606  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
607 
608  if ((SL_FS_CTL_ROLLBACK == Command) || (SL_FS_CTL_COMMIT == Command ))
609  {
610  Msg.Cmd.FileNameLength = _SlFsStrlen(pFileName) + 1 ;
611 
612  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
613  {
614  return SL_ERROR_FS_WRONG_FILE_NAME;
615  }
616 
617 
618  /*the data is aligned*/
619  CmdExt.RxPayloadLen = DataLen;
620  CmdExt.pRxPayload = (_u8 *)(pData);
621 
622  CmdExt.TxPayload1Len = (_SlFsStrlen(pFileName) + 4) & (~3);
623  CmdExt.pTxPayload1 = pFileName;
624 
625  Msg.Cmd.BufferLength = CmdExt.RxPayloadLen + CmdExt.TxPayload1Len;
626 
627  if(CmdExt.pRxPayload != NULL && CmdExt.RxPayloadLen != 0)
628  {
629  CmdExt.RxPayloadLen = CmdExt.RxPayloadLen * (-1);
630  }
631 
632 
633  }
634  else if( SL_FS_CTL_RENAME == Command )
635  {
636  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
637  {
638  return SL_ERROR_FS_WRONG_FILE_NAME;
639  }
640 
641  Msg.Cmd.FileNameLength = (_SlFsStrlen(pFileName) + 4) & (~3);
642 
643  /*current file name*/
644  CmdExt.RxPayloadLen = (_u16)Msg.Cmd.FileNameLength;
645  CmdExt.pRxPayload = pFileName;
646 
647 
648  /*New file name*/
649  CmdExt.TxPayload1Len = (_SlFsStrlen(pData) + 4) & (~3);;
650  CmdExt.pTxPayload1 = (_u8 *)(pData);
651 
652  Msg.Cmd.BufferLength = CmdExt.RxPayloadLen + CmdExt.TxPayload1Len;
653 
654  if(CmdExt.pRxPayload != NULL && CmdExt.RxPayloadLen != 0)
655  {
656  CmdExt.RxPayloadLen = CmdExt.RxPayloadLen * (-1);
657  }
658 
659  }
660  else
661  {
662  Msg.Cmd.FileNameLength = 0;
663 
664  CmdExt.TxPayload1Len = (DataLen + 3) & (~3);
665  CmdExt.pTxPayload1 = (_u8 *)(pData);
666 
667  CmdExt.RxPayloadLen = OutputDataLen;
668  CmdExt.pRxPayload = pOutputData;
669 
670  Msg.Cmd.BufferLength = CmdExt.TxPayload1Len;
671  }
672 
673 
674 
675  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsFileSysControlCmdCtrl, &Msg, &CmdExt));
676 
677  if( pNewToken != NULL )
678  {
679  *pNewToken = Msg.Rsp.Token;
680  }
681 
682  return (_i32)((_i32)Msg.Rsp.Status);
683 }

§ sl_FsDel()

_i16 sl_FsDel ( const _u8 *  pFileName,
const _u32  Token 
)

Delete specific file from a storage or all files from a storage (format)

Parameters
[in]pFileNameFile Name
[in]TokenFile token. if irrelevant set to 0
Returns
Zero on success, or a negative value if an error occurred
See also
Note
belongs to basic_api
Warning
Example
  • Deleting file:
    Status = sl_FsDel("FileName.html",Token);

Definition at line 546 of file fs.c.

547 {
548  _SlFsDeleteMsg_u Msg;
549  _SlCmdExt_t CmdExt;
550 
551 
552  /* verify that this api is allowed. if not allowed then
553  ignore the API execution and return immediately with an error */
554  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
555 
556  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
557  {
558  return SL_ERROR_FS_WRONG_FILE_NAME;
559  }
560 
561 
562  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
563 
564  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
565  CmdExt.pTxPayload1 = (_u8*)pFileName;
566  Msg.Cmd.Token = Token;
567 
568 
569  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsDeleteCmdCtrl, &Msg, &CmdExt));
570 
571  return (_i16)((_i16)Msg.Rsp.status);
572 }

§ sl_FsGetFileList()

_i32 sl_FsGetFileList ( _i32 *  pIndex,
_u8  Count,
_u8  MaxEntryLen,
_u8 *  pBuff,
SlFileListFlags_t  Flags 
)

The list of file names, the files are retrieve in chunks.

Parameters
[in,out]pIndexThe first chunk should start with value of -1, afterwards the Index from the previous call should be set as input
Returns current chunk intex, start the next chunk from that number
[in]CountNumber of entries to retrieve
[in]MaxEntryLenThe total size of the buffer is Count * MaxEntryLen
[out]pBuffThe buffer contains list of SlFileAttributes_t + file name
[in]FlagsIs to retrieve file attributes see SlFileAttributes_t.
Returns
The actual number of entries which are contained in the buffer. On error negative number which contains the error number.
See also
Note
belongs to ext_api
Warning
Example
  • Getting file list
    typedef struct
    {
    SlFileAttributes_t attribute;
    char fileName[SL_FS_MAX_FILE_NAME_LENGTH];
    }slGetfileList_t;
    #define COUNT 5
    void PrintFileListProperty(_u16 prop);
    INT32 GetFileList()
    {
    _i32 NumOfEntriesOrError = 1;
    _i32 Index = -1;
    slGetfileList_t File[COUNT];
    _i32 i;
    _i32 RetVal = 0;
    printf("%\n");
    while( NumOfEntriesOrError > 0 )
    {
    NumOfEntriesOrError = sl_FsGetFileList( &Index, COUNT, (_u8)(SL_FS_MAX_FILE_NAME_LENGTH + sizeof(SlFileAttributes_t)), (unsigned char*)File, SL_FS_GET_FILE_ATTRIBUTES);
    if (NumOfEntriesOrError < 0)
    {
    RetVal = NumOfEntriesOrError;//error
    break;
    }
    for (i = 0; i < NumOfEntriesOrError; i++)
    {
    printf("Name: %s\n", File[i].fileName);
    printf("AllocatedBlocks: %5d ",File[i].attribute.FileAllocatedBlocks);
    printf("MaxSize(byte): %5d \n", File[i].attribute.FileMaxSize);
    PrintFileListProperty((_u16)File[i].attribute.Properties);
    printf("%\n\n");
    }
    }
    printf("%\n");
    return RetVal;//0 means O.K
    }
    void PrintFileListProperty(_u16 prop)
    {
    printf("Flags : ");
    if (prop & SL_FS_INFO_MUST_COMMIT)
    printf("Open file commit,");
    if (prop & SL_FS_INFO_BUNDLE_FILE)
    printf("Open bundle commit,");
    if (prop & SL_FS_INFO_PENDING_COMMIT)
    printf("Pending file commit,");
    if (prop & SL_FS_INFO_PENDING_BUNDLE_COMMIT)
    printf("Pending bundle commit,");
    if (prop & SL_FS_INFO_SECURE)
    printf("Secure,");
    if (prop & SL_FS_INFO_NOT_FAILSAFE)
    printf("File safe,");
    if (prop & SL_FS_INFO_SYS_FILE)
    printf("System,");
    if (prop & SL_FS_INFO_NOT_VALID)
    printf("No valid copy,");
    if (prop & SL_FS_INFO_PUBLIC_WRITE)
    printf("Public write,");
    if (prop & SL_FS_INFO_PUBLIC_READ)
    printf("Public read,");
    }

Definition at line 811 of file fs.c.

812 {
813  _SlFsGetFileListMsg_u Msg;
814  _SlCmdExt_t CmdExt;
815  _u16 OutputBufferSize;
816 
817 
818  /* verify that this api is allowed. if not allowed then
819  ignore the API execution and return immediately with an error */
820  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
821 
822  _SlDrvResetCmdExt(&CmdExt);
823 
824  Msg.Cmd.Index = *pIndex;
825  Msg.Cmd.MaxEntryLen = MaxEntryLen & (~3); /* round to modulu 4 */
826  Msg.Cmd.Count = Count;
827  Msg.Cmd.Flags = (_u8)Flags;
828 
829  OutputBufferSize = Msg.Cmd.Count * Msg.Cmd.MaxEntryLen;
830  if( OutputBufferSize > MAX_NVMEM_CHUNK_SIZE )
831  {
832  return SL_ERROR_FS_WRONG_INPUT_SIZE;
833  }
834 
835  CmdExt.RxPayloadLen = OutputBufferSize;
836  CmdExt.pRxPayload = pBuff;
837 
838  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsGetFileListCmdCtrl, &Msg, &CmdExt));
839 
840 
841  *pIndex = Msg.Rsp.Index;
842 
843  return (_i32)((_i32)Msg.Rsp.NumOfEntriesOrError);
844 }

§ sl_FsGetInfo()

_i16 sl_FsGetInfo ( const _u8 *  pFileName,
const _u32  Token,
SlFsFileInfo_t pFsFileInfo 
)

Get information of a file.

Parameters
[in]pFileNameFile name
[in]TokenFile token. if irrelevant set to 0.
[out]pFsFileInfoReturns the File's Information (SlFsFileInfo_t)
  • Flags
  • File size
  • Allocated size
  • Tokens
Returns
Zero on success, negative error code on failure
When file not exists : SL_ERROR_FS_FILE_NOT_EXISTS
Note
  • If the return value is SL_ERROR_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY or SL_ERROR_FS_FILE_IS_ALREADY_OPENED information about the file is valid.
  • Belongs to basic_api
See also
sl_FsOpen
Warning
Example
  • Getting file info:
    Status = sl_FsGetInfo("FileName.html",Token,&FsFileInfo);

Definition at line 479 of file fs.c.

480 {
481  _SlFsGetInfoMsg_u Msg;
482  _SlCmdExt_t CmdExt;
483  _u16 BitNum;
484 
485  /* verify that this api is allowed. if not allowed then
486  ignore the API execution and return immediately with an error */
487  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
488 
489  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
490 
491  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
492  {
493  return SL_ERROR_FS_WRONG_FILE_NAME;
494  }
495 
496 
497  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
498  CmdExt.pTxPayload1 = (_u8*)pFileName;
499 
500  Msg.Cmd.Token = Token;
501 
502  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsGetInfoCmdCtrl, &Msg, &CmdExt));
503 
504 
505  /* convert flags */
506  pFsFileInfo->Flags = 0;
507  for (BitNum = 0; BitNum < 16; BitNum++ )
508  {
509  if (( Msg.Rsp.Flags >> BitNum) & 0x1 )
510  {
511  pFsFileInfo->Flags |= FlagsTranslate[BitNum];
512  }
513  }
514 
515  pFsFileInfo->Len = Msg.Rsp.FileLen;
516  pFsFileInfo->MaxSize = Msg.Rsp.AllocatedLen;
517  pFsFileInfo->Token[0] = Msg.Rsp.Token[0];
518  pFsFileInfo->Token[1] = Msg.Rsp.Token[1];
519  pFsFileInfo->Token[2] = Msg.Rsp.Token[2];
520  pFsFileInfo->Token[3] = Msg.Rsp.Token[3];
521  pFsFileInfo->StorageSize = Msg.Rsp.FileStorageSize;
522  pFsFileInfo->WriteCounter = Msg.Rsp.FileWriteCounter;
523  return (_i16)((_i16)Msg.Rsp.Status);
524 }

§ sl_FsOpen()

_i32 sl_FsOpen ( const _u8 *  pFileName,
const _u32  AccessModeAndMaxSize,
_u32 *  pToken 
)

open file for read or write from/to storage device

Parameters
[in]pFileNameFile Name buffer pointer
[in]AccessModeAndMaxSizeOptions: As described below
[in]pTokeninput Token for read, output Token for write

AccessModeAndMaxSize possible input
SL_FS_READ - Read a file
SL_FS_WRITE - Open for write for an existing file (whole file content needs to be rewritten)
SL_FS_CREATE|maxSizeInBytes,accessModeFlags SL_FS_CREATE|SL_FS_OVERWRITE|maxSizeInBytes,accessModeFlags - Open for creating a new file. Max file size is defined in bytes.
For optimal FS size, use max size in 4K-512 bytes steps (e.g. 3584,7680,117760)
Several access modes bits can be combined together from SlFileOpenFlags_e enum

Returns
File handle on success. Negative error code on fail
See also
sl_FsRead sl_FsWrite sl_FsClose
Note
belongs to basic_api
Warning
Example
  • Creating file and writing data to it

    char* DeviceFileName = "MyFile.txt";
    unsigned long MaxSize = 63 * 1024; //62.5K is max file size
    long DeviceFileHandle = -1;
    _i32 RetVal; //negative retval is an error
    unsigned long Offset = 0;
    unsigned char InputBuffer[100];
    _u32 MasterToken = 0;
    // Create a file and write data. The file in this example is secured, without signature and with a fail safe commit
    //create a secure file if not exists and open it for write.
    DeviceFileHandle = sl_FsOpen(unsigned char *)DeviceFileName,
    SL_FS_CREATE|SL_FS_OVERWRITE | SL_FS_CREATE_SECURE | SL_FS_CREATE_NOSIGNATURE | SL_FS_CREATE_MAX_SIZE( MaxSize ),
    &MasterToken);
    Offset = 0;
    //Preferred in secure file that the Offset and the length will be aligned to 16 bytes.
    RetVal = sl_FsWrite( DeviceFileHandle, Offset, (unsigned char *)"HelloWorld", strlen("HelloWorld"));
    RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
    // open the same file for read, using the Token we got from the creation procedure above
    DeviceFileHandle = sl_FsOpen(unsigned char *)DeviceFileName,
    SL_FS_READ,
    &MasterToken);
    Offset = 0;
    RetVal = sl_FsRead( DeviceFileHandle, Offset, (unsigned char *)InputBuffer, strlen("HelloWorld"));
    RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);


    • Create a non secure file if not already exists and open it for write
      DeviceFileHandle = sl_FsOpen((unsigned char *)DeviceFileName,
      SL_FS_CREATE|SL_FS_OVERWRITE| SL_FS_CREATE_MAX_SIZE( MaxSize ),
      NULL);
    Note
    Some of the flags are creation flags and can only be set when the file is created. When opening the file for write the creation flags are ignored. For more information, refer to chapter 8 in the user manual.

Definition at line 149 of file fs.c.

150 {
151 
152  _SlFsOpenMsg_u Msg;
153  _SlCmdExt_t CmdExt;
154  _i32 FileHandle;
155  _u32 MaxSizeInBytes;
156  _u32 OpenMode;
157  _u8 CreateMode;
158 
159 
160  /* verify that this api is allowed. if not allowed then
161  ignore the API execution and return immediately with an error */
162  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
163 
164  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
165 
166  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
167  {
168  return SL_ERROR_FS_WRONG_FILE_NAME;
169  }
170 
171  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
172  CmdExt.pTxPayload1 = (_u8*)pFileName;
173 
174  OpenMode = ModeAndMaxSize & SL_FS_OPEN_MODE_BIT_MASK;
175 
176  /*convert from the interface flags to the device flags*/
177  if( OpenMode == SL_FS_READ )
178  {
179  Msg.Cmd.Mode = FS_MODE(FS_MODE_OPEN_READ, 0, 0, 0);
180  }
181  else if (( OpenMode == SL_FS_WRITE ) ||( OpenMode == SL_FS_OVERWRITE))
182  {
183  Msg.Cmd.Mode = FS_MODE(FS_MODE_OPEN_WRITE, 0, 0, FS_CONVERT_FLAGS ( ModeAndMaxSize));
184  }
185  /* one of the creation mode */
186  else if ( ( OpenMode == (SL_FS_CREATE | SL_FS_OVERWRITE )) || ( OpenMode == SL_FS_CREATE) ||(OpenMode == (SL_FS_CREATE | SL_FS_WRITE )))
187  {
188  /* test that the size is correct */
189  MaxSizeInBytes = (ModeAndMaxSize & SL_FS_OPEN_MAXSIZE_BIT_MASK) * 256;
190  if (MaxSizeInBytes > 0xFF0000 )
191  {
192  return SL_ERROR_FS_FILE_MAX_SIZE_EXCEEDED;
193  }
194 
195  CreateMode = ((OpenMode == (SL_FS_CREATE | SL_FS_OVERWRITE )) ? FS_MODE_OPEN_WRITE_CREATE_IF_NOT_EXIST : FS_MODE_OPEN_CREATE );
196 
197  Msg.Cmd.Mode = FsGetCreateFsMode( CreateMode ,MaxSizeInBytes, FS_CONVERT_FLAGS ( ModeAndMaxSize) );
198  }
199  else
200  {
201  return SL_ERROR_FS_UNVALID_FILE_MODE;
202  }
203 
204  if(pToken != NULL)
205  {
206  Msg.Cmd.Token = *pToken;
207  }
208  else
209  {
210  Msg.Cmd.Token = 0;
211  }
212 
213  _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsOpenCmdCtrl, &Msg, &CmdExt);
214  FileHandle = (_i32)Msg.Rsp.FileHandle;
215  if (pToken != NULL)
216  {
217  *pToken = Msg.Rsp.Token;
218  }
219 
220  /* in case of an error, return the erros file handler as an error code */
221  return FileHandle;
222 }

§ sl_FsProgram()

_i32 sl_FsProgram ( const _u8 *  pData,
_u16  Len,
const _u8 *  pKey,
_u32  Flags 
)

Enables to format and configure the device with pre-prepared configuration.

Parameters
[in]FlagsFor future use
[in]pKeyIn case the ucf is encrypted the encryption key, otherwise NULL
[in]pDataThe file is download in data chunks, the chunk size should be aligned to 16 bytes, if no data Set to NULL
[in]LenThe length of pData in bytes
Returns
The return value is:
  • On error < 0 , contains the error number and extended error number
  • On success > 0, represent the number of bytes received
  • On successful end == 0 , when all file chunks are download
See also
Note
belongs to ext_api
Warning
Example
  • FS programming:
    //Return 0 for OK, else Error
    _i32 ProgramImage( char* UcfFileName, char * KeyFileName )
    {
    #define PROGRAMMING_CHUNK_SIZE 4096
    _i32 slRetVal = 0;
    SlFsKey_t Key;
    FILE *hostFileHandle = NULL;
    _u16 bytesRead;
    _u8 DataBuf[PROGRAMMING_CHUNK_SIZE];
    FILE *KeyFileHandle = NULL;
    short ErrorNum;
    unsigned short ExtendedErrorNum;
    time_t start,end;
    double dif;
    _u8* pKey = NULL;
    errno_t err;
    if (KeyFileName != "")
    {
    //Read key
    err = fopen_s( &KeyFileHandle, KeyFileName, "rb");
    if (err != 0)
    {
    return __LINE__;//error
    }
    fread((_u8*)&Key, 1, sizeof(SlFsKey_t), KeyFileHandle);
    fclose(KeyFileHandle);
    pKey = (_u8*)&Key;
    }
    // Downlaoding the Data with the key, the key can be set only in the first chunk,no need to download it with each chunk
    if (UcfFileName != "")
    {
    //Read data
    err = fopen_s( &hostFileHandle, UcfFileName, "rb");
    if (err != 0)
    {
    return __LINE__;//error
    }
    time (&start);
    bytesRead = fread(DataBuf, 1, PROGRAMMING_CHUNK_SIZE, hostFileHandle);
    while ( bytesRead )
    {
    slRetVal = sl_FsProgram( DataBuf , bytesRead , (_u8*)pKey, 0 );
    if(slRetVal == SL_API_ABORTED)//timeout
    {
    return( slRetVal );
    }
    else if (slRetVal < 0 )//error
    {
    ErrorNum = (long)slRetVal >> 16;
    ExtendedErrorNum = (_u16)(slRetVal & 0xFFFF);
    printf("\tError sl_FsProgram = %d , %d \n", ErrorNum, ExtendedErrorNum);
    fclose(hostFileHandle);
    return( ErrorNum );
    }
    if(slRetVal == 0)//finished succesfully
    break;
    pKey = NULL;//no need to download the key with each chunk;
    bytesRead = fread(DataBuf, 1, PROGRAMMING_CHUNK_SIZE, hostFileHandle);
    }
    time (&end);
    dif = difftime (end,start);
    #ifdef PRINT
    printf ("\tProgramming took %.2lf seconds to run.\n", dif );
    #endif
    //The file was downloaded but it was not detected by the programming as the EOF.
    if((bytesRead == 0 ) && (slRetVal > 0 ))
    {
    return __LINE__;//error
    }
    fclose(hostFileHandle);
    }//if (UcfFileName != "")
    //this scenario is in case the image was already "burned" to the SFLASH by external tool and only the key is downloaded
    else if (KeyFileName != "")
    {
    slRetVal = sl_FsProgram(NULL , 0 , (_u8*)pKey, 0 );
    if (slRetVal < 0)//error
    {
    ErrorNum = (long)slRetVal >> 16;
    ExtendedErrorNum = (_u16)slRetVal && 0xFF;;
    printf("\tError sl_FsProgram = %d , %d \n", ErrorNum, ExtendedErrorNum);
    fclose(hostFileHandle);
    return( ErrorNum );
    }
    }
    if( slRetVal == 0 )
    {
    //Reset the nWP
    sl_Stop(100);
    Sleep(1000);
    sl_Start(NULL, NULL, NULL);
    Sleep(2000);
    }
    return slRetVal;
    }

Definition at line 706 of file fs.c.

707 {
708  _SlFsProgrammingMsg_u Msg;
709  _SlCmdExt_t CmdExt;
710  _u16 ChunkLen;
711 
712  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
713 
714  Msg.Cmd.Flags = (_u32)Flags;
715 
716  _SlDrvResetCmdExt(&CmdExt);
717 
718  /* no data and no key, called only for extracting the image */
719  if( (DataLen == 0) && (pKey == NULL) )
720  {
721  Msg.Cmd.ChunkLen = 0;
722  Msg.Cmd.KeyLen = 0;
723  Msg.Cmd.Flags = Flags;
724  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsProgrammingCmdCtrl, &Msg, &CmdExt));
725  }
726  else if( (DataLen> 0) && ( pData == NULL))
727  {
728  return( ((_i32)SL_ERROR_FS_WRONG_INPUT_SIZE) << 16 );
729  }
730  else if( (DataLen == 0) && (pKey != NULL) )
731  {
732  Msg.Cmd.ChunkLen = 0;
733  Msg.Cmd.KeyLen = sizeof(SlFsKey_t);;
734  Msg.Cmd.Flags = Flags;
735  CmdExt.pTxPayload1 = (_u8*)pKey;
736  CmdExt.TxPayload1Len = sizeof(SlFsKey_t);
737  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsProgrammingCmdCtrl, &Msg, &CmdExt));
738  }
739  else /* DataLen > 0 */
740  {
741  if( (DataLen & 0xF) > 0)
742  {
743  return( ((_i32)SL_ERROR_FS_NOT_16_ALIGNED) << 16 );
744  }
745  Msg.Cmd.Flags = Flags;
746 
747  CmdExt.pTxPayload1 = (_u8 *)pData;
748  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE, DataLen);
749 
750  while(ChunkLen > 0)
751  {
752  Msg.Cmd.ChunkLen = ChunkLen;
753  CmdExt.TxPayload1Len = ChunkLen;
754  if( pKey != NULL )
755  {
756  Msg.Cmd.KeyLen = sizeof(SlFsKey_t);
757  CmdExt.RxPayloadLen = sizeof(SlFsKey_t);
758  CmdExt.pRxPayload = (_u8 *)pKey;
759 
760  if(CmdExt.pRxPayload != NULL && CmdExt.RxPayloadLen != 0)
761  {
762  CmdExt.RxPayloadLen = CmdExt.RxPayloadLen * (-1);
763  }
764  }
765  else /* No key */
766  {
767  Msg.Cmd.KeyLen = 0;
768  CmdExt.RxPayloadLen = 0;
769  CmdExt.pRxPayload = NULL;
770  }
771 
772  VERIFY_RET_OK( _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsProgrammingCmdCtrl, &Msg, &CmdExt));
773 
774  if( Msg.Rsp.Status <= 0 ) /* Error or finished */
775  {
776  return (_i32)(Msg.Rsp.Status);
777  }
778 
779  DataLen -= ChunkLen;
780  CmdExt.pTxPayload1 += ChunkLen;
781 
782  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE, DataLen);
783  }
784  }
785  return (_i32)(Msg.Rsp.Status);
786 }
Definition: fs.h:170

§ sl_FsRead()

_i32 sl_FsRead ( const _i32  FileHdl,
_u32  Offset,
_u8 *  pData,
_u32  Len 
)

Read block of data from a file in storage device.

Parameters
[in]FileHdlPointer to the file (assigned from sl_FsOpen)
[in]OffsetOffset to specific read block
[out]pDataPointer for the received data
[in]LenLength of the received data
Returns
Number of read bytes on success, negative error code on failure
See also
sl_FsClose sl_FsWrite sl_FsOpen
Note
belongs to basic_api
Warning
Example
  • Reading File:
    Status = sl_FsRead(FileHandle, 0, &readBuff[0], readSize);

Definition at line 301 of file fs.c.

302 {
303  _SlFsReadMsg_u Msg;
304  _SlCmdExt_t ExtCtrl;
305  _u16 ChunkLen;
306  _SlReturnVal_t RetVal =0;
307  _i32 RetCount = 0;
308 
309  /* verify that this api is allowed. if not allowed then
310  ignore the API execution and return immediately with an error */
311  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
312 
313  _SlDrvMemZero(&ExtCtrl, (_u16)sizeof(_SlCmdExt_t));
314 
315  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
316  ExtCtrl.RxPayloadLen = (_i16)ChunkLen;
317  ExtCtrl.pRxPayload = (_u8 *)(pData);
318  Msg.Cmd.Offset = Offset;
319  Msg.Cmd.Len = ChunkLen;
320  Msg.Cmd.FileHandle = (_u32)FileHdl;
321  do
322  {
323  RetVal = _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsReadCmdCtrl, &Msg, &ExtCtrl);
324  if(SL_OS_RET_CODE_OK == RetVal)
325  {
326  if( Msg.Rsp.status < 0)
327  {
328  if( RetCount > 0)
329  {
330  return RetCount;
331  }
332  else
333  {
334  return Msg.Rsp.status;
335  }
336  }
337  RetCount += (_i32)Msg.Rsp.status;
338  Len -= ChunkLen;
339  Offset += ChunkLen;
340  Msg.Cmd.Offset = Offset;
341  ExtCtrl.pRxPayload += ChunkLen;
342  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
343  ExtCtrl.RxPayloadLen = (_i16)ChunkLen;
344  Msg.Cmd.Len = ChunkLen;
345  Msg.Cmd.FileHandle = (_u32)FileHdl;
346  }
347  else
348  {
349  return RetVal;
350  }
351  }while(ChunkLen > 0);
352 
353  return (_i32)RetCount;
354 }

§ sl_FsWrite()

_i32 sl_FsWrite ( const _i32  FileHdl,
_u32  Offset,
_u8 *  pData,
_u32  Len 
)

Write block of data to a file in storage device.

Parameters
[in]FileHdlPointer to the file (assigned from sl_FsOpen)
[in]OffsetOffset to specific block to be written
[in]pDataPointer the transmitted data to the storage device
[in]LenLength of the transmitted data
Returns
Number of wireted bytes on success, negative error code on failure
See also
Note
belongs to basic_api
Warning
Example
  • Writing file:
    Status = sl_FsWrite(FileHandle, 0, &buff[0], readSize);

Definition at line 376 of file fs.c.

377 {
378  _SlFsWriteMsg_u Msg;
379  _SlCmdExt_t ExtCtrl;
380  _u16 ChunkLen;
381  _SlReturnVal_t RetVal;
382  _i32 RetCount = 0;
383 
384  /* verify that this api is allowed. if not allowed then
385  ignore the API execution and return immediately with an error */
386  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
387 
388  _SlDrvMemZero(&ExtCtrl, (_u16)sizeof(_SlCmdExt_t));
389 
390  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
391  ExtCtrl.TxPayload1Len = ChunkLen;
392  ExtCtrl.pTxPayload1 = (_u8 *)(pData);
393  Msg.Cmd.Offset = Offset;
394  Msg.Cmd.Len = ChunkLen;
395  Msg.Cmd.FileHandle = (_u32)FileHdl;
396 
397  do
398  {
399 
400  RetVal = _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsWriteCmdCtrl, &Msg, &ExtCtrl);
401  if(SL_OS_RET_CODE_OK == RetVal)
402  {
403  if( Msg.Rsp.status < 0)
404  {
405  if( RetCount > 0)
406  {
407  return RetCount;
408  }
409  else
410  {
411  return Msg.Rsp.status;
412  }
413  }
414 
415  RetCount += (_i32)Msg.Rsp.status;
416  Len -= ChunkLen;
417  Offset += ChunkLen;
418  Msg.Cmd.Offset = Offset;
419  ExtCtrl.pTxPayload1 += ChunkLen;
420  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
421  ExtCtrl.TxPayload1Len = ChunkLen;
422  Msg.Cmd.Len = ChunkLen;
423  Msg.Cmd.FileHandle = (_u32)FileHdl;
424  }
425  else
426  {
427  return RetVal;
428  }
429  }while(ChunkLen > 0);
430 
431  return (_i32)RetCount;
432 }

Data Structure Documentation

§ SlFsFileInfo_t

struct SlFsFileInfo_t

Definition at line 80 of file fs.h.

Data Fields
_u16 Flags
_u32 Len
_u32 MaxSize
_u32 StorageSize
_u32 Token[4]
_u32 WriteCounter

§ SlFsKey_t

struct SlFsKey_t

Definition at line 170 of file fs.h.

Data Fields
_u32 Key[4]

§ SlFsFileNameIndex_t

struct SlFsFileNameIndex_t

Definition at line 177 of file fs.h.

Data Fields
_u8 Index

§ SlFsFileNameIndexOrError_t

union SlFsFileNameIndexOrError_t

Definition at line 182 of file fs.h.

Data Fields
_i32 ErrorNumber
SlFsFileNameIndex_t Index

§ SlFsRetToFactoryCommand_t

struct SlFsRetToFactoryCommand_t

Definition at line 199 of file fs.h.

Data Fields
_u32 Operation

§ SlFsControl_t

struct SlFsControl_t

Definition at line 212 of file fs.h.

Data Fields
_u32 IncludeFilters

§ SlFsControlDeviceUsage_t

struct SlFsControlDeviceUsage_t

Definition at line 220 of file fs.h.

Data Fields
_u16 DeviceBlocksCapacity
_u16 DeviceBlockSize
_u16 LargestAllocatedGapInBlocks
_u16 NumOfAllocatedBlocks
_u16 NumOfAvailableBlocksForUserFiles
_u16 NumOfReservedBlocks
_u16 NumOfReservedBlocksForSystemfiles
_u8 Padding[2]

§ SlFsControlFilesUsage_t

struct SlFsControlFilesUsage_t

Definition at line 232 of file fs.h.

Data Fields
_u8 ActualNumOfSysFiles
_u8 ActualNumOfUserFiles
_u8 Bundlestate
_u16 FATWriteCounter
_u8 IsDevlopmentFormatType
_u8 MaxFsFiles
_u8 MaxFsFilesReservedForSysFiles
_u32 NumOfAlerts
_u32 NumOfAlertsThreshold
_u8 Padding
_u16 Padding2
_u8 Reserved

§ SlFsControlGetStorageInfoResponse_t

struct SlFsControlGetStorageInfoResponse_t

Definition at line 252 of file fs.h.

Data Fields
SlFsControlDeviceUsage_t DeviceUsage
SlFsControlFilesUsage_t FilesUsage

§ SlFsControlGetCountersResponse_t

struct SlFsControlGetCountersResponse_t

Definition at line 259 of file fs.h.

Data Fields
_u8 ClosedFilesCnt
_u8 ClosedFilesCntWithValidFailSafeImage
_u32 IncludeFilters
_u8 OpenedForWriteCnt
_u8 OpenedForWriteCntWithValidFailSafeImage
_u8 OpeneForReadCnt
_u8 OpeneForReadCntWithValidFailSafeImage
_u8 padding[2]

§ SlFileAttributes_t

struct SlFileAttributes_t

Definition at line 285 of file fs.h.

Data Fields
_u32 FileAllocatedBlocks
_u32 FileMaxSize
_u32 Properties