SimpleLink CC31xx/CC32xx Host Driver  Version 3.0.1.55
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 either SHA-1 or SHA-256, 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 250 of file fs.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

§ 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 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  RetVal = _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsWriteCmdCtrl, &Msg, &ExtCtrl);
403  if(SL_OS_RET_CODE_OK == RetVal)
404  {
405  if( Msg.Rsp.status < 0)
406  {
407  if( RetCount > 0)
408  {
409  return RetCount;
410  }
411  else
412  {
413  return Msg.Rsp.status;
414  }
415  }
416 
417  RetCount += (_i32)Msg.Rsp.status;
418  Len -= ChunkLen;
419  Offset += ChunkLen;
420  Msg.Cmd.Offset = Offset;
421  ExtCtrl.pTxPayload1 += ChunkLen;
422  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
423  ExtCtrl.TxPayload1Len = ChunkLen;
424  Msg.Cmd.Len = ChunkLen;
425  Msg.Cmd.FileHandle = (_u32)FileHdl;
426  }
427  else
428  {
429  return RetVal;
430  }
431  }while(ChunkLen > 0);
432 
433  return (_i32)RetCount;
434 }

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