SimpleLink CC3120/CC3220 Host Driver  Version 2.0.1.27
Simplifies the implementation of Internet connectivity
fs.c
1 /*
2  * fs.c - CC31xx/CC32xx Host Driver Implementation
3  *
4  * Copyright (C) 2017 Texas Instruments Incorporated - https://www.ti.com/
5  *
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution.
18  *
19  * Neither the name of Texas Instruments Incorporated nor the names of
20  * its contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35 */
36 
37 
38 /*****************************************************************************/
39 /* Include files */
40 /*****************************************************************************/
41 #include <ti/drivers/net/wifi/simplelink.h>
42 #include <ti/drivers/net/wifi/source/protocol.h>
43 #include <ti/drivers/net/wifi/source/driver.h>
44 
45 /*****************************************************************************/
46 /* Macro declarations */
47 /*****************************************************************************/
48 #define sl_min(a,b) (((a) < (b)) ? (a) : (b))
49 #define MAX_NVMEM_CHUNK_SIZE 1456 /*should be 16 bytes align, because of encryption data*/
50 
51 /*****************************************************************************/
52 /* Internal functions */
53 /*****************************************************************************/
54 
55 #ifndef SL_TINY
56 static _u16 _SlFsStrlen(const _u8 *buffer);
57 
58 static _u32 FsGetCreateFsMode(_u8 Mode, _u32 MaxSizeInBytes,_u32 AccessFlags);
59 
60 /*****************************************************************************/
61 /* _SlFsStrlen */
62 /*****************************************************************************/
63 static _u16 _SlFsStrlen(const _u8 *buffer)
64 {
65  _u16 len = 0;
66  if( buffer != NULL )
67  {
68  while(*buffer++) len++;
69  }
70  return len;
71 }
72 #endif
73 /*****************************************************************************/
74 /* _SlFsGetCreateFsMode */
75 /*****************************************************************************/
76 
77 /* Convert the user flag to the file System flag */
78 #define FS_CONVERT_FLAGS( ModeAndMaxSize ) (((_u32)ModeAndMaxSize & SL_FS_OPEN_FLAGS_BIT_MASK)>>SL_NUM_OF_MAXSIZE_BIT)
79 
80 typedef enum
81 {
82  FS_MODE_OPEN_READ = 0,
83  FS_MODE_OPEN_WRITE,
84  FS_MODE_OPEN_CREATE,
85  FS_MODE_OPEN_WRITE_CREATE_IF_NOT_EXIST
86 }FsFileOpenAccessType_e;
87 
88 #define FS_MODE_ACCESS_RESERVED_OFFSET (27)
89 #define FS_MODE_ACCESS_RESERVED_MASK (0x1F)
90 #define FS_MODE_ACCESS_FLAGS_OFFSET (16)
91 #define FS_MODE_ACCESS_FLAGS_MASK (0x7FF)
92 #define FS_MODE_ACCESS_OFFSET (12)
93 #define FS_MODE_ACCESS_MASK (0xF)
94 #define FS_MODE_OPEN_SIZE_GRAN_OFFSET (8)
95 #define FS_MODE_OPEN_SIZE_GRAN_MASK (0xF)
96 #define FS_MODE_OPEN_SIZE_OFFSET (0)
97 #define FS_MODE_OPEN_SIZE_MASK (0xFF)
98 #define FS_MAX_MODE_SIZE (0xFF)
99 
100 /* SizeGran is up to 4 bit , Size can be up to 8 bit */
101 #define FS_MODE(Access, SizeGran, Size,Flags) (_u32)(((_u32)((Access) &FS_MODE_ACCESS_MASK)<<FS_MODE_ACCESS_OFFSET) | \
102  ((_u32)((SizeGran) &FS_MODE_OPEN_SIZE_GRAN_MASK)<<FS_MODE_OPEN_SIZE_GRAN_OFFSET) | \
103  ((_u32)((Size) &FS_MODE_OPEN_SIZE_MASK)<<FS_MODE_OPEN_SIZE_OFFSET) | \
104  ((_u32)((Flags) &FS_MODE_ACCESS_FLAGS_MASK)<<FS_MODE_ACCESS_FLAGS_OFFSET))
105 
106 
107 typedef enum
108 {
109  FS_MODE_SIZE_GRAN_256B = 0, /* MAX_SIZE = 64K */
110  FS_MODE_SIZE_GRAN_1KB, /* MAX_SIZE = 256K */
111  FS_MODE_SIZE_GRAN_4KB, /* MAX_SZIE = 1M */
112  FS_MODE_SIZE_GRAN_16KB, /* MAX_SIZE = 4M */
113  FS_MODE_SIZE_GRAN_64KB, /* MAX_SIZE = 16M */
114  FS_MAX_MODE_SIZE_GRAN
115 }FsFileOpenMaxSizeGran_e;
116 
117 #ifndef SL_TINY
118 
119 
120 static _u32 FsGetCreateFsMode(_u8 Mode, _u32 MaxSizeInBytes,_u32 AccessFlags)
121 {
122  _u32 granIdx = 0;
123  _u32 granNum = 0;
124  _u32 granTable[FS_MAX_MODE_SIZE_GRAN] = {256,1024,4096,16384,65536};
125 
126  for(granIdx= FS_MODE_SIZE_GRAN_256B ;granIdx< FS_MAX_MODE_SIZE_GRAN;granIdx++)
127  {
128  if( granTable[granIdx]*255 >= MaxSizeInBytes )
129  break;
130  }
131  granNum = MaxSizeInBytes/granTable[granIdx];
132  if( MaxSizeInBytes % granTable[granIdx] != 0 )
133  granNum++;
134 
135  return (_u32)FS_MODE( Mode, granIdx, granNum, AccessFlags );
136 
137 }
138 
139 #endif
140 
141 /*****************************************************************************/
142 /* API functions */
143 /*****************************************************************************/
144 
145 /*****************************************************************************/
146 /* sl_FsOpen */
147 /*****************************************************************************/
148 typedef union
149 {
150  SlFsOpenCommand_t Cmd;
151  SlFsOpenResponse_t Rsp;
152 }_SlFsOpenMsg_u;
153 
154 #if _SL_INCLUDE_FUNC(sl_FsOpen)
155 
156 static const _SlCmdCtrl_t _SlFsOpenCmdCtrl =
157 {
158  SL_OPCODE_NVMEM_FILEOPEN,
159  (_SlArgSize_t)sizeof(SlFsOpenCommand_t),
160  (_SlArgSize_t)sizeof(SlFsOpenResponse_t)
161 };
162 
163 _i32 sl_FsOpen(const _u8 *pFileName,const _u32 ModeAndMaxSize, _u32 *pToken)
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 }
236 #endif
237 
238 /*****************************************************************************/
239 /* sl_FsClose */
240 /*****************************************************************************/
241 typedef union
242 {
243  SlFsCloseCommand_t Cmd;
244  _BasicResponse_t Rsp;
245 }_SlFsCloseMsg_u;
246 
247 #if _SL_INCLUDE_FUNC(sl_FsClose)
248 
249 static const _SlCmdCtrl_t _SlFsCloseCmdCtrl =
250 {
251  SL_OPCODE_NVMEM_FILECLOSE,
252  (_SlArgSize_t)sizeof(SlFsCloseCommand_t),
253  (_SlArgSize_t)sizeof(SlFsCloseResponse_t)
254 };
255 
256 _i16 sl_FsClose(const _i32 FileHdl, const _u8* pCeritificateFileName,const _u8* pSignature ,const _u32 SignatureLen)
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 }
290 #endif
291 
292 
293 /*****************************************************************************/
294 /* sl_FsRead */
295 /*****************************************************************************/
296 typedef union
297 {
298  SlFsReadCommand_t Cmd;
299  SlFsReadResponse_t Rsp;
300 }_SlFsReadMsg_u;
301 
302 #if _SL_INCLUDE_FUNC(sl_FsRead)
303 
304 static const _SlCmdCtrl_t _SlFsReadCmdCtrl =
305 {
306  SL_OPCODE_NVMEM_FILEREADCOMMAND,
307  (_SlArgSize_t)sizeof(SlFsReadCommand_t),
308  (_SlArgSize_t)sizeof(SlFsReadResponse_t)
309 };
310 
311 _i32 sl_FsRead(const _i32 FileHdl,_u32 Offset, _u8* pData,_u32 Len)
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 }
365 #endif
366 
367 /*****************************************************************************/
368 /* sl_FsWrite */
369 /*****************************************************************************/
370 typedef union
371 {
372  SlFsWriteCommand_t Cmd;
374 }_SlFsWriteMsg_u;
375 
376 #if _SL_INCLUDE_FUNC(sl_FsWrite)
377 
378 static const _SlCmdCtrl_t _SlFsWriteCmdCtrl =
379 {
380  SL_OPCODE_NVMEM_FILEWRITECOMMAND,
381  (_SlArgSize_t)sizeof(SlFsWriteCommand_t),
382  (_SlArgSize_t)sizeof(SlFsWriteResponse_t)
383 };
384 
385 _i32 sl_FsWrite(const _i32 FileHdl,_u32 Offset, _u8* pData,_u32 Len)
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 }
441 #endif
442 
443 /*****************************************************************************/
444 /* sl_FsGetInfo */
445 /*****************************************************************************/
446 typedef union
447 {
450 }_SlFsGetInfoMsg_u;
451 
452 #if _SL_INCLUDE_FUNC(sl_FsGetInfo)
453 
454 static const _SlCmdCtrl_t _SlFsGetInfoCmdCtrl =
455 {
456  SL_OPCODE_NVMEM_FILEGETINFOCOMMAND,
457  (_SlArgSize_t)sizeof(SlFsGetInfoCommand_t),
458  (_SlArgSize_t)sizeof(SlFsGetInfoResponse_t)
459 };
460 
461 const _u16 FlagsTranslate[] =
462 {
463  SL_FS_INFO_OPEN_WRITE,
464  SL_FS_INFO_OPEN_READ,
465  SL_FS_INFO_NOT_FAILSAFE,
466  SL_FS_INFO_NOT_VALID,
467  SL_FS_INFO_SYS_FILE,
468  SL_FS_INFO_MUST_COMMIT,
469  SL_FS_INFO_BUNDLE_FILE,
470  SL_FS_INFO_PENDING_COMMIT,
471  SL_FS_INFO_PENDING_BUNDLE_COMMIT,
472  0,
473  SL_FS_INFO_SECURE,
474  SL_FS_INFO_NOSIGNATURE,
475  SL_FS_INFO_PUBLIC_WRITE,
476  SL_FS_INFO_PUBLIC_READ,
477  0,
478  0
479 };
480 
481 _i16 sl_FsGetInfo(const _u8 *pFileName,const _u32 Token,SlFsFileInfo_t* pFsFileInfo)
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 }
526 #endif
527 
528 /*****************************************************************************/
529 /* sl_FsDel */
530 /*****************************************************************************/
531 typedef union
532 {
535 }_SlFsDeleteMsg_u;
536 
537 
538 #if _SL_INCLUDE_FUNC(sl_FsDel)
539 
540 static const _SlCmdCtrl_t _SlFsDeleteCmdCtrl =
541 {
542  SL_OPCODE_NVMEM_FILEDELCOMMAND,
543  (_SlArgSize_t)sizeof(SlFsDeleteCommand_t),
544  (_SlArgSize_t)sizeof(SlFsDeleteResponse_t)
545 };
546 
547 _i16 sl_FsDel(const _u8 *pFileName,const _u32 Token)
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 }
571 #endif
572 
573 /*****************************************************************************/
574 /* sl_FsCtl */
575 /*****************************************************************************/
576 typedef union
577 {
580 }_SlFsFileSysControlMsg_u;
581 
582 #if _SL_INCLUDE_FUNC(sl_FsCtl)
583 
584 const _SlCmdCtrl_t _SlFsFileSysControlCmdCtrl =
585 {
586  SL_OPCODE_NVMEM_NVMEMFILESYSTEMCONTROLCOMMAND,
589 };
590 
591 _i32 sl_FsCtl( SlFsCtl_e Command, _u32 Token, _u8 *pFileName, const _u8 *pData, _u16 DataLen, _u8 *pOutputData, _u16 OutputDataLen,_u32 *pNewToken )
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 }
674 #endif
675 
676 
677 /*****************************************************************************/
678 /* sl_FsProgram */
679 /*****************************************************************************/
680 typedef union
681 {
684 }_SlFsProgrammingMsg_u;
685 
686 #if _SL_INCLUDE_FUNC(sl_FsProgram)
687 
688 const _SlCmdCtrl_t _SlFsProgrammingCmdCtrl =
689 {
690  SL_OPCODE_NVMEM_NVMEMFSPROGRAMMINGCOMMAND,
691  sizeof(SlFsProgramCommand_t),
692  sizeof(SlFsProgramResponse_t)
693 };
694 
695 _i32 sl_FsProgram(const _u8* pData , _u16 DataLen ,const _u8 * pKey , _u32 Flags )
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 }
777 #endif
778 
779 /*****************************************************************************/
780 /* sl_FsGetFileList */
781 /*****************************************************************************/
782 typedef union
783 {
786 }_SlFsGetFileListMsg_u;
787 
788 #if _SL_INCLUDE_FUNC(sl_FsGetFileList)
789 
790 const _SlCmdCtrl_t _SlFsGetFileListCmdCtrl =
791 {
792  SL_OPCODE_NVMEM_NVMEMGETFILELISTCOMMAND,
793  sizeof(SlFsGetFileListCommand_t),
795 };
796 
797 _i32 sl_FsGetFileList(_i32* pIndex, _u8 Count, _u8 MaxEntryLen , _u8* pBuff, SlFileListFlags_t Flags )
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 }
829 #endif
830 
_i32 sl_FsOpen(const _u8 *pFileName, const _u32 ModeAndMaxSize, _u32 *pToken)
open file for read or write from/to storage device
Definition: fs.c:163
_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.
Definition: fs.c:591
_i16 sl_FsGetInfo(const _u8 *pFileName, const _u32 Token, SlFsFileInfo_t *pFsFileInfo)
Get information of a file.
Definition: fs.c:481
_i16 sl_FsClose(const _i32 FileHdl, const _u8 *pCeritificateFileName, const _u8 *pSignature, const _u32 SignatureLen)
Close file in storage device.
Definition: fs.c:256
_i32 sl_FsProgram(const _u8 *pData, _u16 DataLen, const _u8 *pKey, _u32 Flags)
Enables to format and configure the device with pre-prepared configuration.
Definition: fs.c:695
_i16 sl_FsDel(const _u8 *pFileName, const _u32 Token)
Delete specific file from a storage or all files from a storage (format)
Definition: fs.c:547
_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.
Definition: fs.c:797
_i32 sl_FsWrite(const _i32 FileHdl, _u32 Offset, _u8 *pData, _u32 Len)
Write block of data to a file in storage device.
Definition: fs.c:385
_i32 sl_FsRead(const _i32 FileHdl, _u32 Offset, _u8 *pData, _u32 Len)
Read block of data from a file in storage device.
Definition: fs.c:311
Definition: fs.h:175