SimpleLink CC3120/CC3220 Host Driver  Version 2.0.1.15
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

_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:
    1 _i16 RetVal;
    2 RetVal = sl_FsClose(FileHandle,0,0,0);

  • Aborting file:
    1 _u8 Signature;
    2 Signature = 'A';
    3 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 247 of file fs.c.

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

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

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

  • SL_FS_CTL_GET_STORAGE_INFO:
    1 _i32 GetStorageInfo( SlFsControlGetStorageInfoResponse_t* pSlFsControlGetStorageInfoResponse )
    2 {
    3  _i32 slRetVal;
    4 
    5  slRetVal = sl_FsCtl( ( SlFsCtl_e)SL_FS_CTL_GET_STORAGE_INFO, 0, NULL , NULL , 0, (_u8 *)pSlFsControlGetStorageInfoResponse, sizeof(SlFsControlGetStorageInfoResponse_t), NULL );
    6  return slRetVal;
    7 
    8 }

  • SL_FS_CTL_RESTORE:
    1 //Return 0 for OK, else Error
    2 _i32 ProgramRetToImage( )
    3 {
    4  _i32 slRetVal;
    5  SlFsRetToFactoryCommand_t RetToFactoryCommand;
    6  _i32 RetVal, ExtendedError;
    7 
    8  RetToFactoryCommand.Operation = SL_FS_FACTORY_RET_TO_IMAGE;
    9  slRetVal = sl_FsCtl( (SlFsCtl_e)SL_FS_CTL_RESTORE, 0, NULL , (_u8 *)&RetToFactoryCommand , sizeof(SlFsRetToFactoryCommand_t), NULL, 0 , NULL );
    10  if ((_i32)slRetVal < 0)
    11  {
    12  //Pay attention, for this function the slRetVal is composed from Signed RetVal & extended error
    13  RetVal = (_i16)slRetVal>> 16;
    14  ExtendedError = (_u16)slRetVal& 0xFFFF;
    15  printf("\tError SL_FS_FACTORY_RET_TO_IMAGE, 5d, %d\n", RetVal, ExtendedError);
    16  return slRetVal;
    17  }
    18  //Reset
    19  sl_Stop(0);
    20  Sleep(1000);
    21  sl_Start(NULL, NULL, NULL);
    22 
    23  return slRetVal;
    24 }

  • SL_FS_CTL_BUNDLE_ROLLBACK:
    1 //return 0 for O.K else negative
    2 _i32 BundleRollback()
    3 {
    4  _i32 slRetVal = 0;
    5  SlFsControl_t FsControl;
    6  FsControl.IncludeFilters = 0; //Use default behaviour
    7  slRetVal = sl_FsCtl( (SlFsCtl_e)SL_FS_CTL_BUNDLE_ROLLBACK, 0, NULL ,(_u8 *)&FsControl, sizeof(SlFsControl_t), NULL, 0 , NULL);
    8  return slRetVal;
    9 }

  • SL_FS_CTL_BUNDLE_COMMIT:
    1 //return 0 for O.K else negative
    2 _i32 BundleCommit()
    3 {
    4  _i32 slRetVal = 0;
    5  SlFsControl_t FsControl;
    6  FsControl.IncludeFilters = 0; //Use default behaviour
    7  slRetVal = sl_FsCtl( (SlFsCtl_e)SL_FS_CTL_BUNDLE_COMMIT, 0, NULL ,(_u8 *)&FsControl, sizeof(SlFsControl_t), NULL, 0 , NULL);
    8  return slRetVal;
    9 }

Definition at line 597 of file fs.c.

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

Definition at line 549 of file fs.c.

550 {
551  _SlFsDeleteMsg_u Msg;
552  _SlCmdExt_t CmdExt;
553 
554 
555  /* verify that this api is allowed. if not allowed then
556  ignore the API execution and return immediately with an error */
557  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
558 
559  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
560  {
561  return SL_ERROR_FS_WRONG_FILE_NAME;
562  }
563 
564 
565  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
566 
567  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
568  CmdExt.pTxPayload1 = (_u8*)pFileName;
569  Msg.Cmd.Token = Token;
570 
571 
572  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsDeleteCmdCtrl, &Msg, &CmdExt));
573 
574  return (_i16)((_i16)Msg.Rsp.status);
575 }
_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
    1 typedef struct
    2 {
    3  SlFileAttributes_t attribute;
    4  char fileName[SL_FS_MAX_FILE_NAME_LENGTH];
    5 }slGetfileList_t;
    6 
    7 #define COUNT 5
    8 
    9 void PrintFileListProperty(_u16 prop);
    10 
    11 INT32 GetFileList()
    12 {
    13  _i32 NumOfEntriesOrError = 1;
    14  _i32 Index = -1;
    15  slGetfileList_t File[COUNT];
    16  _i32 i;
    17  _i32 RetVal = 0;
    18 
    19  printf("%\n");
    20  while( NumOfEntriesOrError > 0 )
    21  {
    22  NumOfEntriesOrError = sl_FsGetFileList( &Index, COUNT, (_u8)(SL_FS_MAX_FILE_NAME_LENGTH + sizeof(SlFileAttributes_t)), (unsigned char*)File, SL_FS_GET_FILE_ATTRIBUTES);
    23  if (NumOfEntriesOrError < 0)
    24  {
    25  RetVal = NumOfEntriesOrError;//error
    26  break;
    27  }
    28  for (i = 0; i < NumOfEntriesOrError; i++)
    29  {
    30  printf("Name: %s\n", File[i].fileName);
    31  printf("AllocatedBlocks: %5d ",File[i].attribute.FileAllocatedBlocks);
    32  printf("MaxSize(byte): %5d \n", File[i].attribute.FileMaxSize);
    33  PrintFileListProperty((_u16)File[i].attribute.Properties);
    34  printf("%\n\n");
    35  }
    36  }
    37  printf("%\n");
    38  return RetVal;//0 means O.K
    39 }
    40 
    41 void PrintFileListProperty(_u16 prop)
    42 {
    43  printf("Flags : ");
    44  if (prop & SL_FS_INFO_MUST_COMMIT)
    45  printf("Open file commit,");
    46  if (prop & SL_FS_INFO_BUNDLE_FILE)
    47  printf("Open bundle commit,");
    48  if (prop & SL_FS_INFO_PENDING_COMMIT)
    49  printf("Pending file commit,");
    50  if (prop & SL_FS_INFO_PENDING_BUNDLE_COMMIT)
    51  printf("Pending bundle commit,");
    52  if (prop & SL_FS_INFO_SECURE)
    53  printf("Secure,");
    54  if (prop & SL_FS_INFO_NOT_FAILSAFE)
    55  printf("File safe,");
    56  if (prop & SL_FS_INFO_SYS_FILE)
    57  printf("System,");
    58  if (prop & SL_FS_INFO_NOT_VALID)
    59  printf("No valid copy,");
    60  if (prop & SL_FS_INFO_PUBLIC_WRITE)
    61  printf("Public write,");
    62  if (prop & SL_FS_INFO_PUBLIC_READ)
    63  printf("Public read,");
    64 }

Definition at line 814 of file fs.c.

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

Definition at line 482 of file fs.c.

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

    1 char* DeviceFileName = "MyFile.txt";
    2 unsigned long MaxSize = 63 * 1024; //62.5K is max file size
    3 long DeviceFileHandle = -1;
    4 _i32 RetVal; //negative retval is an error
    5 unsigned long Offset = 0;
    6 unsigned char InputBuffer[100];
    7 _u32 MasterToken = 0;
    8 
    9 // Create a file and write data. The file in this example is secured, without signature and with a fail safe commit
    10 
    11 //create a secure file if not exists and open it for write.
    12 DeviceFileHandle = sl_FsOpen(unsigned char *)DeviceFileName,
    13  SL_FS_CREATE|SL_FS_OVERWRITE | SL_FS_CREATE_SECURE | SL_FS_CREATE_NOSIGNATURE | SL_FS_CREATE_MAX_SIZE( MaxSize ),
    14  &MasterToken);
    15 
    16 Offset = 0;
    17 //Preferred in secure file that the Offset and the length will be aligned to 16 bytes.
    18 RetVal = sl_FsWrite( DeviceFileHandle, Offset, (unsigned char *)"HelloWorld", strlen("HelloWorld"));
    19 
    20 RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
    21 
    22 // open the same file for read, using the Token we got from the creation procedure above
    23 DeviceFileHandle = sl_FsOpen(unsigned char *)DeviceFileName,
    24  SL_FS_READ,
    25  &MasterToken);
    26 
    27 Offset = 0;
    28 RetVal = sl_FsRead( DeviceFileHandle, Offset, (unsigned char *)InputBuffer, strlen("HelloWorld"));
    29 
    30 RetVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);


    • Create a non secure file if not already exists and open it for write
      1 DeviceFileHandle = sl_FsOpen((unsigned char *)DeviceFileName,
      2  SL_FS_CREATE|SL_FS_OVERWRITE| SL_FS_CREATE_MAX_SIZE( MaxSize ),
      3  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 
205 
206 
207  if(pToken != NULL)
208  {
209  Msg.Cmd.Token = *pToken;
210  }
211  else
212  {
213  Msg.Cmd.Token = 0;
214  }
215 
216  _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsOpenCmdCtrl, &Msg, &CmdExt);
217  FileHandle = (_i32)Msg.Rsp.FileHandle;
218  if (pToken != NULL)
219  {
220  *pToken = Msg.Rsp.Token;
221  }
222 
223  /* in case of an error, return the erros file handler as an error code */
224  return FileHandle;
225 }
_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:
    1 //Return 0 for OK, else Error
    2 _i32 ProgramImage( char* UcfFileName, char * KeyFileName )
    3 {
    4 #define PROGRAMMING_CHUNK_SIZE 4096
    5  _i32 slRetVal = 0;
    6  SlFsKey_t Key;
    7  FILE *hostFileHandle = NULL;
    8  _u16 bytesRead;
    9  _u8 DataBuf[PROGRAMMING_CHUNK_SIZE];
    10  FILE *KeyFileHandle = NULL;
    11  short ErrorNum;
    12  unsigned short ExtendedErrorNum;
    13  time_t start,end;
    14  double dif;
    15  _u8* pKey = NULL;
    16  errno_t err;
    17 
    18 
    19 
    20  if (KeyFileName != "")
    21  {
    22  //Read key
    23  err = fopen_s( &KeyFileHandle, KeyFileName, "rb");
    24  if (err != 0)
    25  {
    26  return __LINE__;//error
    27  }
    28  fread((_u8*)&Key, 1, sizeof(SlFsKey_t), KeyFileHandle);
    29  fclose(KeyFileHandle);
    30  pKey = (_u8*)&Key;
    31  }
    32 
    33  // Downlaoding the Data with the key, the key can be set only in the first chunk,no need to download it with each chunk
    34  if (UcfFileName != "")
    35  {
    36  //Read data
    37  err = fopen_s( &hostFileHandle, UcfFileName, "rb");
    38  if (err != 0)
    39  {
    40  return __LINE__;//error
    41  }
    42 
    43 
    44  time (&start);
    45 
    46  bytesRead = fread(DataBuf, 1, PROGRAMMING_CHUNK_SIZE, hostFileHandle);
    47 
    48  while ( bytesRead )
    49  {
    50  slRetVal = sl_FsProgram( DataBuf , bytesRead , (_u8*)pKey, 0 );
    51  if(slRetVal == SL_API_ABORTED)//timeout
    52  {
    53  return( slRetVal );
    54  }
    55  else if (slRetVal < 0 )//error
    56  {
    57  ErrorNum = (long)slRetVal >> 16;
    58  ExtendedErrorNum = (_u16)(slRetVal & 0xFFFF);
    59  printf("\tError sl_FsProgram = %d , %d \n", ErrorNum, ExtendedErrorNum);
    60  fclose(hostFileHandle);
    61  return( ErrorNum );
    62  }
    63  if(slRetVal == 0)//finished succesfully
    64  break;
    65  pKey = NULL;//no need to download the key with each chunk;
    66  bytesRead = fread(DataBuf, 1, PROGRAMMING_CHUNK_SIZE, hostFileHandle);
    67  }
    68 
    69 
    70  time (&end);
    71  dif = difftime (end,start);
    72  #ifdef PRINT
    73  printf ("\tProgramming took %.2lf seconds to run.\n", dif );
    74  #endif
    75  //The file was downloaded but it was not detected by the programming as the EOF.
    76  if((bytesRead == 0 ) && (slRetVal > 0 ))
    77  {
    78  return __LINE__;//error
    79  }
    80 
    81 
    82  fclose(hostFileHandle);
    83  }//if (UcfFileName != "")
    84 
    85  //this scenario is in case the image was already "burned" to the SFLASH by external tool and only the key is downloaded
    86  else if (KeyFileName != "")
    87  {
    88  slRetVal = sl_FsProgram(NULL , 0 , (_u8*)pKey, 0 );
    89  if (slRetVal < 0)//error
    90  {
    91  ErrorNum = (long)slRetVal >> 16;
    92  ExtendedErrorNum = (_u16)slRetVal && 0xFF;;
    93  printf("\tError sl_FsProgram = %d , %d \n", ErrorNum, ExtendedErrorNum);
    94  fclose(hostFileHandle);
    95  return( ErrorNum );
    96  }
    97  }
    98 
    99  if( slRetVal == 0 )
    100  {
    101  //Reset the nWP
    102  sl_Stop(100);
    103  Sleep(1000);
    104  sl_Start(NULL, NULL, NULL);
    105  Sleep(2000);
    106  }
    107 
    108  return slRetVal;
    109 
    110 }

Definition at line 709 of file fs.c.

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

Definition at line 304 of file fs.c.

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

Definition at line 379 of file fs.c.

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

Data Structure Documentation

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
struct SlFsKey_t

Definition at line 170 of file fs.h.

Data Fields
_u32 Key[4]
struct SlFsFileNameIndex_t

Definition at line 177 of file fs.h.

Data Fields
_u8 Index
union SlFsFileNameIndexOrError_t

Definition at line 182 of file fs.h.

Data Fields
_i32 ErrorNumber
SlFsFileNameIndex_t Index
struct SlFsRetToFactoryCommand_t

Definition at line 199 of file fs.h.

Data Fields
_u32 Operation
struct SlFsControl_t

Definition at line 212 of file fs.h.

Data Fields
_u32 IncludeFilters
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]
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
struct SlFsControlGetStorageInfoResponse_t

Definition at line 252 of file fs.h.

Data Fields
SlFsControlDeviceUsage_t DeviceUsage
SlFsControlFilesUsage_t FilesUsage
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]
struct SlFileAttributes_t

Definition at line 285 of file fs.h.

Data Fields
_u32 FileAllocatedBlocks
_u32 FileMaxSize
_u32 Properties