SimpleLink CC3120/CC3220 Host Driver  Version 2.0.1.27
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 256 of file fs.c.

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

§ 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 591 of file fs.c.

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

§ 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 547 of file fs.c.

548 {
549  _SlFsDeleteMsg_u Msg;
550  _SlCmdExt_t CmdExt;
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  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
562 
563  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
564  CmdExt.pTxPayload1 = (_u8*)pFileName;
565  Msg.Cmd.Token = Token;
566 
567  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsDeleteCmdCtrl, &Msg, &CmdExt));
568 
569  return (_i16)((_i16)Msg.Rsp.status);
570 }

§ 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 797 of file fs.c.

798 {
799  _SlFsGetFileListMsg_u Msg;
800  _SlCmdExt_t CmdExt;
801  _u16 OutputBufferSize;
802 
803  /* verify that this api is allowed. if not allowed then
804  ignore the API execution and return immediately with an error */
805  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
806 
807  _SlDrvResetCmdExt(&CmdExt);
808 
809  Msg.Cmd.Index = *pIndex;
810  Msg.Cmd.MaxEntryLen = MaxEntryLen & (~3); /* round to modulu 4 */
811  Msg.Cmd.Count = Count;
812  Msg.Cmd.Flags = (_u8)Flags;
813 
814  OutputBufferSize = Msg.Cmd.Count * Msg.Cmd.MaxEntryLen;
815  if( OutputBufferSize > MAX_NVMEM_CHUNK_SIZE )
816  {
817  return SL_ERROR_FS_WRONG_INPUT_SIZE;
818  }
819 
820  CmdExt.RxPayloadLen = OutputBufferSize;
821  CmdExt.pRxPayload = pBuff;
822 
823  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsGetFileListCmdCtrl, &Msg, &CmdExt));
824 
825  *pIndex = Msg.Rsp.Index;
826 
827  return (_i32)((_i32)Msg.Rsp.NumOfEntriesOrError);
828 }

§ 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 481 of file fs.c.

482 {
483  _SlFsGetInfoMsg_u Msg;
484  _SlCmdExt_t CmdExt;
485  _u16 BitNum;
486 
487  /* verify that this api is allowed. if not allowed then
488  ignore the API execution and return immediately with an error */
489  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
490 
491  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
492 
493  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
494  {
495  return SL_ERROR_FS_WRONG_FILE_NAME;
496  }
497 
498  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
499  CmdExt.pTxPayload1 = (_u8*)pFileName;
500 
501  Msg.Cmd.Token = Token;
502 
503  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsGetInfoCmdCtrl, &Msg, &CmdExt));
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 
524  return (_i16)((_i16)Msg.Rsp.Status);
525 }

§ 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 163 of file fs.c.

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

§ 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 695 of file fs.c.

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

§ 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 311 of file fs.c.

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

§ 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 385 of file fs.c.

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

Data Structure Documentation

§ SlFsFileInfo_t

struct SlFsFileInfo_t

Definition at line 142 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 175 of file fs.h.

Data Fields
_u32 Key[4]

§ SlFsFileNameIndex_t

struct SlFsFileNameIndex_t

Definition at line 180 of file fs.h.

Data Fields
_u8 Index

§ SlFsFileNameIndexOrError_t

union SlFsFileNameIndexOrError_t

Definition at line 185 of file fs.h.

Data Fields
_i32 ErrorNumber
SlFsFileNameIndex_t Index

§ SlFsRetToFactoryCommand_t

struct SlFsRetToFactoryCommand_t

Definition at line 200 of file fs.h.

Data Fields
_u32 Operation

§ SlFsControl_t

struct SlFsControl_t

Definition at line 207 of file fs.h.

Data Fields
_u32 IncludeFilters

§ SlFsControlDeviceUsage_t

struct SlFsControlDeviceUsage_t

Definition at line 213 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 225 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 242 of file fs.h.

Data Fields
SlFsControlDeviceUsage_t DeviceUsage
SlFsControlFilesUsage_t FilesUsage

§ SlFsControlGetCountersResponse_t

struct SlFsControlGetCountersResponse_t

Definition at line 248 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 271 of file fs.h.

Data Fields
_u32 FileAllocatedBlocks
_u32 FileMaxSize
_u32 Properties