SimpleLink CC3120/CC3220 Host Driver  Version 2.0.1.15
Simplifies the implementation of Internet connectivity
fs.c
1 /*
2  * Copyright (C) 2016 Texas Instruments Incorporated
3  *
4  * All rights reserved. Property of Texas Instruments Incorporated.
5  * Restricted rights to use, duplicate or disclose this code are
6  * granted through contract.
7  *
8  * The program may not be used without the written permission of
9  * Texas Instruments Incorporated or against the terms and conditions
10  * stipulated in the agreement under which this program has been supplied,
11  * and under no circumstances can it be used with non-TI connectivity device.
12  *
13  */
14 
15 
16 
17 /*****************************************************************************/
18 /* Include files */
19 /*****************************************************************************/
20 #include <ti/drivers/net/wifi/simplelink.h>
21 #include <ti/drivers/net/wifi/source/protocol.h>
22 #include <ti/drivers/net/wifi/source/driver.h>
23 
24 /*****************************************************************************/
25 /* Macro declarations */
26 /*****************************************************************************/
27 #define sl_min(a,b) (((a) < (b)) ? (a) : (b))
28 #define MAX_NVMEM_CHUNK_SIZE 1456 /*should be 16 bytes align, because of encryption data*/
29 
30 /*****************************************************************************/
31 /* Internal functions */
32 /*****************************************************************************/
33 
34 #ifndef SL_TINY
35 static _u16 _SlFsStrlen(const _u8 *buffer);
36 
37 static _u32 FsGetCreateFsMode(_u8 Mode, _u32 MaxSizeInBytes,_u32 AccessFlags);
38 
39 /*****************************************************************************/
40 /* _SlFsStrlen */
41 /*****************************************************************************/
42 static _u16 _SlFsStrlen(const _u8 *buffer)
43 {
44  _u16 len = 0;
45  if( buffer != NULL )
46  {
47  while(*buffer++) len++;
48  }
49  return len;
50 }
51 #endif
52 /*****************************************************************************/
53 /* _SlFsGetCreateFsMode */
54 /*****************************************************************************/
55 
56 
57 
58 /* Convert the user flag to the file System flag */
59 #define FS_CONVERT_FLAGS( ModeAndMaxSize ) (((_u32)ModeAndMaxSize & SL_FS_OPEN_FLAGS_BIT_MASK)>>SL_NUM_OF_MAXSIZE_BIT)
60 
61 
62 
63 typedef enum
64 {
65  FS_MODE_OPEN_READ = 0,
66  FS_MODE_OPEN_WRITE,
67  FS_MODE_OPEN_CREATE,
68  FS_MODE_OPEN_WRITE_CREATE_IF_NOT_EXIST
69 }FsFileOpenAccessType_e;
70 
71 #define FS_MODE_ACCESS_RESERVED_OFFSET (27)
72 #define FS_MODE_ACCESS_RESERVED_MASK (0x1F)
73 #define FS_MODE_ACCESS_FLAGS_OFFSET (16)
74 #define FS_MODE_ACCESS_FLAGS_MASK (0x7FF)
75 #define FS_MODE_ACCESS_OFFSET (12)
76 #define FS_MODE_ACCESS_MASK (0xF)
77 #define FS_MODE_OPEN_SIZE_GRAN_OFFSET (8)
78 #define FS_MODE_OPEN_SIZE_GRAN_MASK (0xF)
79 #define FS_MODE_OPEN_SIZE_OFFSET (0)
80 #define FS_MODE_OPEN_SIZE_MASK (0xFF)
81 #define FS_MAX_MODE_SIZE (0xFF)
82 
83 /* SizeGran is up to 4 bit , Size can be up to 8 bit */
84 #define FS_MODE(Access, SizeGran, Size,Flags) (_u32)(((_u32)((Access) &FS_MODE_ACCESS_MASK)<<FS_MODE_ACCESS_OFFSET) | \
85  ((_u32)((SizeGran) &FS_MODE_OPEN_SIZE_GRAN_MASK)<<FS_MODE_OPEN_SIZE_GRAN_OFFSET) | \
86  ((_u32)((Size) &FS_MODE_OPEN_SIZE_MASK)<<FS_MODE_OPEN_SIZE_OFFSET) | \
87  ((_u32)((Flags) &FS_MODE_ACCESS_FLAGS_MASK)<<FS_MODE_ACCESS_FLAGS_OFFSET))
88 
89 
90 typedef enum
91 {
92  FS_MODE_SIZE_GRAN_256B = 0, /* MAX_SIZE = 64K */
93  FS_MODE_SIZE_GRAN_1KB, /* MAX_SIZE = 256K */
94  FS_MODE_SIZE_GRAN_4KB, /* MAX_SZIE = 1M */
95  FS_MODE_SIZE_GRAN_16KB, /* MAX_SIZE = 4M */
96  FS_MODE_SIZE_GRAN_64KB, /* MAX_SIZE = 16M */
97  FS_MAX_MODE_SIZE_GRAN
98 }FsFileOpenMaxSizeGran_e;
99 
100 #ifndef SL_TINY
101 
102 
103 static _u32 FsGetCreateFsMode(_u8 Mode, _u32 MaxSizeInBytes,_u32 AccessFlags)
104 {
105  _u32 granIdx = 0;
106  _u32 granNum = 0;
107  _u32 granTable[FS_MAX_MODE_SIZE_GRAN] = {256,1024,4096,16384,65536};
108 
109  for(granIdx= FS_MODE_SIZE_GRAN_256B ;granIdx< FS_MAX_MODE_SIZE_GRAN;granIdx++)
110  {
111  if( granTable[granIdx]*255 >= MaxSizeInBytes )
112  break;
113  }
114  granNum = MaxSizeInBytes/granTable[granIdx];
115  if( MaxSizeInBytes % granTable[granIdx] != 0 )
116  granNum++;
117 
118  return (_u32)FS_MODE( Mode, granIdx, granNum, AccessFlags );
119 
120 }
121 
122 
123 
124 #endif
125 
126 /*****************************************************************************/
127 /* API functions */
128 /*****************************************************************************/
129 
130 /*****************************************************************************/
131 /* sl_FsOpen */
132 /*****************************************************************************/
133 typedef union
134 {
135  SlFsOpenCommand_t Cmd;
136  SlFsOpenResponse_t Rsp;
137 }_SlFsOpenMsg_u;
138 
139 
140 #if _SL_INCLUDE_FUNC(sl_FsOpen)
141 
142 static const _SlCmdCtrl_t _SlFsOpenCmdCtrl =
143 {
144  SL_OPCODE_NVMEM_FILEOPEN,
145  (_SlArgSize_t)sizeof(SlFsOpenCommand_t),
146  (_SlArgSize_t)sizeof(SlFsOpenResponse_t)
147 };
148 
149 _i32 sl_FsOpen(const _u8 *pFileName,const _u32 ModeAndMaxSize, _u32 *pToken)
150 {
151 
152  _SlFsOpenMsg_u Msg;
153  _SlCmdExt_t CmdExt;
154  _i32 FileHandle;
155  _u32 MaxSizeInBytes;
156  _u32 OpenMode;
157  _u8 CreateMode;
158 
159 
160  /* verify that this api is allowed. if not allowed then
161  ignore the API execution and return immediately with an error */
162  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
163 
164  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
165 
166  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
167  {
168  return SL_ERROR_FS_WRONG_FILE_NAME;
169  }
170 
171  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
172  CmdExt.pTxPayload1 = (_u8*)pFileName;
173 
174  OpenMode = ModeAndMaxSize & SL_FS_OPEN_MODE_BIT_MASK;
175 
176  /*convert from the interface flags to the device flags*/
177  if( OpenMode == SL_FS_READ )
178  {
179  Msg.Cmd.Mode = FS_MODE(FS_MODE_OPEN_READ, 0, 0, 0);
180  }
181  else if (( OpenMode == SL_FS_WRITE ) ||( OpenMode == SL_FS_OVERWRITE))
182  {
183  Msg.Cmd.Mode = FS_MODE(FS_MODE_OPEN_WRITE, 0, 0, FS_CONVERT_FLAGS ( ModeAndMaxSize));
184  }
185  /* one of the creation mode */
186  else if ( ( OpenMode == (SL_FS_CREATE | SL_FS_OVERWRITE )) || ( OpenMode == SL_FS_CREATE) ||(OpenMode == (SL_FS_CREATE | SL_FS_WRITE )))
187  {
188  /* test that the size is correct */
189  MaxSizeInBytes = (ModeAndMaxSize & SL_FS_OPEN_MAXSIZE_BIT_MASK) * 256;
190  if (MaxSizeInBytes > 0xFF0000 )
191  {
192  return SL_ERROR_FS_FILE_MAX_SIZE_EXCEEDED;
193  }
194 
195  CreateMode = ((OpenMode == (SL_FS_CREATE | SL_FS_OVERWRITE )) ? FS_MODE_OPEN_WRITE_CREATE_IF_NOT_EXIST : FS_MODE_OPEN_CREATE );
196 
197  Msg.Cmd.Mode = FsGetCreateFsMode( CreateMode ,MaxSizeInBytes, FS_CONVERT_FLAGS ( ModeAndMaxSize) );
198  }
199  else
200  {
201  return SL_ERROR_FS_UNVALID_FILE_MODE;
202  }
203 
204 
205 
206 
207  if(pToken != NULL)
208  {
209  Msg.Cmd.Token = *pToken;
210  }
211  else
212  {
213  Msg.Cmd.Token = 0;
214  }
215 
216  _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsOpenCmdCtrl, &Msg, &CmdExt);
217  FileHandle = (_i32)Msg.Rsp.FileHandle;
218  if (pToken != NULL)
219  {
220  *pToken = Msg.Rsp.Token;
221  }
222 
223  /* in case of an error, return the erros file handler as an error code */
224  return FileHandle;
225 }
226 #endif
227 
228 /*****************************************************************************/
229 /* sl_FsClose */
230 /*****************************************************************************/
231 typedef union
232 {
233  SlFsCloseCommand_t Cmd;
234  _BasicResponse_t Rsp;
235 }_SlFsCloseMsg_u;
236 
237 
238 #if _SL_INCLUDE_FUNC(sl_FsClose)
239 
240 static const _SlCmdCtrl_t _SlFsCloseCmdCtrl =
241 {
242  SL_OPCODE_NVMEM_FILECLOSE,
243  (_SlArgSize_t)sizeof(SlFsCloseCommand_t),
244  (_SlArgSize_t)sizeof(SlFsCloseResponse_t)
245 };
246 
247 _i16 sl_FsClose(const _i32 FileHdl, const _u8* pCeritificateFileName,const _u8* pSignature ,const _u32 SignatureLen)
248 {
249  _SlFsCloseMsg_u Msg;
250  _SlCmdExt_t ExtCtrl;
251 
252  _SlDrvMemZero(&Msg, (_u16)sizeof(SlFsCloseCommand_t));
253 
254  /* verify that this api is allowed. if not allowed then
255  ignore the API execution and return immediately with an error */
256  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
257 
258 
259  Msg.Cmd.FileHandle = (_u32)FileHdl;
260  if( pCeritificateFileName != NULL )
261  {
262  Msg.Cmd.CertificFileNameLength = (_u32)((_SlFsStrlen(pCeritificateFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
263  }
264  Msg.Cmd.SignatureLen = SignatureLen;
265 
266  _SlDrvMemZero(&ExtCtrl, (_u16)sizeof(_SlCmdExt_t));
267 
268  ExtCtrl.TxPayload1Len = (_u16)(((SignatureLen+3) & (~3))); /* align */
269  ExtCtrl.pTxPayload1 = (_u8*)pSignature;
270  ExtCtrl.RxPayloadLen = (_i16)Msg.Cmd.CertificFileNameLength;
271  ExtCtrl.pRxPayload = (_u8*)pCeritificateFileName; /* Add signature */
272 
273  if(ExtCtrl.pRxPayload != NULL && ExtCtrl.RxPayloadLen != 0)
274  {
275  ExtCtrl.RxPayloadLen = ExtCtrl.RxPayloadLen * (-1);
276  }
277 
278  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsCloseCmdCtrl, &Msg, &ExtCtrl));
279 
280  return (_i16)((_i16)Msg.Rsp.status);
281 }
282 #endif
283 
284 
285 /*****************************************************************************/
286 /* sl_FsRead */
287 /*****************************************************************************/
288 typedef union
289 {
290  SlFsReadCommand_t Cmd;
291  SlFsReadResponse_t Rsp;
292 }_SlFsReadMsg_u;
293 
294 #if _SL_INCLUDE_FUNC(sl_FsRead)
295 
296 
297 static const _SlCmdCtrl_t _SlFsReadCmdCtrl =
298 {
299  SL_OPCODE_NVMEM_FILEREADCOMMAND,
300  (_SlArgSize_t)sizeof(SlFsReadCommand_t),
301  (_SlArgSize_t)sizeof(SlFsReadResponse_t)
302 };
303 
304 _i32 sl_FsRead(const _i32 FileHdl,_u32 Offset, _u8* pData,_u32 Len)
305 {
306  _SlFsReadMsg_u Msg;
307  _SlCmdExt_t ExtCtrl;
308  _u16 ChunkLen;
309  _SlReturnVal_t RetVal =0;
310  _i32 RetCount = 0;
311 
312  /* verify that this api is allowed. if not allowed then
313  ignore the API execution and return immediately with an error */
314  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
315 
316  _SlDrvMemZero(&ExtCtrl, (_u16)sizeof(_SlCmdExt_t));
317 
318  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
319  ExtCtrl.RxPayloadLen = (_i16)ChunkLen;
320  ExtCtrl.pRxPayload = (_u8 *)(pData);
321  Msg.Cmd.Offset = Offset;
322  Msg.Cmd.Len = ChunkLen;
323  Msg.Cmd.FileHandle = (_u32)FileHdl;
324  do
325  {
326  RetVal = _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsReadCmdCtrl, &Msg, &ExtCtrl);
327  if(SL_OS_RET_CODE_OK == RetVal)
328  {
329  if( Msg.Rsp.status < 0)
330  {
331  if( RetCount > 0)
332  {
333  return RetCount;
334  }
335  else
336  {
337  return Msg.Rsp.status;
338  }
339  }
340  RetCount += (_i32)Msg.Rsp.status;
341  Len -= ChunkLen;
342  Offset += ChunkLen;
343  Msg.Cmd.Offset = Offset;
344  ExtCtrl.pRxPayload += ChunkLen;
345  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
346  ExtCtrl.RxPayloadLen = (_i16)ChunkLen;
347  Msg.Cmd.Len = ChunkLen;
348  Msg.Cmd.FileHandle = (_u32)FileHdl;
349  }
350  else
351  {
352  return RetVal;
353  }
354  }while(ChunkLen > 0);
355 
356  return (_i32)RetCount;
357 }
358 #endif
359 
360 /*****************************************************************************/
361 /* sl_FsWrite */
362 /*****************************************************************************/
363 typedef union
364 {
365  SlFsWriteCommand_t Cmd;
367 }_SlFsWriteMsg_u;
368 
369 
370 #if _SL_INCLUDE_FUNC(sl_FsWrite)
371 
372 static const _SlCmdCtrl_t _SlFsWriteCmdCtrl =
373 {
374  SL_OPCODE_NVMEM_FILEWRITECOMMAND,
375  (_SlArgSize_t)sizeof(SlFsWriteCommand_t),
376  (_SlArgSize_t)sizeof(SlFsWriteResponse_t)
377 };
378 
379 _i32 sl_FsWrite(const _i32 FileHdl,_u32 Offset, _u8* pData,_u32 Len)
380 {
381  _SlFsWriteMsg_u Msg;
382  _SlCmdExt_t ExtCtrl;
383  _u16 ChunkLen;
384  _SlReturnVal_t RetVal;
385  _i32 RetCount = 0;
386 
387  /* verify that this api is allowed. if not allowed then
388  ignore the API execution and return immediately with an error */
389  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
390 
391  _SlDrvMemZero(&ExtCtrl, (_u16)sizeof(_SlCmdExt_t));
392 
393  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
394  ExtCtrl.TxPayload1Len = ChunkLen;
395  ExtCtrl.pTxPayload1 = (_u8 *)(pData);
396  Msg.Cmd.Offset = Offset;
397  Msg.Cmd.Len = ChunkLen;
398  Msg.Cmd.FileHandle = (_u32)FileHdl;
399 
400  do
401  {
402 
403  RetVal = _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsWriteCmdCtrl, &Msg, &ExtCtrl);
404  if(SL_OS_RET_CODE_OK == RetVal)
405  {
406  if( Msg.Rsp.status < 0)
407  {
408  if( RetCount > 0)
409  {
410  return RetCount;
411  }
412  else
413  {
414  return Msg.Rsp.status;
415  }
416  }
417 
418  RetCount += (_i32)Msg.Rsp.status;
419  Len -= ChunkLen;
420  Offset += ChunkLen;
421  Msg.Cmd.Offset = Offset;
422  ExtCtrl.pTxPayload1 += ChunkLen;
423  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE,Len);
424  ExtCtrl.TxPayload1Len = ChunkLen;
425  Msg.Cmd.Len = ChunkLen;
426  Msg.Cmd.FileHandle = (_u32)FileHdl;
427  }
428  else
429  {
430  return RetVal;
431  }
432  }while(ChunkLen > 0);
433 
434  return (_i32)RetCount;
435 }
436 #endif
437 
438 /*****************************************************************************/
439 /* sl_FsGetInfo */
440 /*****************************************************************************/
441 typedef union
442 {
445 }_SlFsGetInfoMsg_u;
446 
447 
448 #if _SL_INCLUDE_FUNC(sl_FsGetInfo)
449 
450 
451 static const _SlCmdCtrl_t _SlFsGetInfoCmdCtrl =
452 {
453  SL_OPCODE_NVMEM_FILEGETINFOCOMMAND,
454  (_SlArgSize_t)sizeof(SlFsGetInfoCommand_t),
455  (_SlArgSize_t)sizeof(SlFsGetInfoResponse_t)
456 };
457 
458 
459 
460 const _u16 FlagsTranslate[] =
461 {
462  SL_FS_INFO_OPEN_WRITE,
463  SL_FS_INFO_OPEN_READ,
464  SL_FS_INFO_NOT_FAILSAFE,
465  SL_FS_INFO_NOT_VALID,
466  SL_FS_INFO_SYS_FILE,
467  SL_FS_INFO_MUST_COMMIT,
468  SL_FS_INFO_BUNDLE_FILE,
469  SL_FS_INFO_PENDING_COMMIT,
470  SL_FS_INFO_PENDING_BUNDLE_COMMIT,
471  0,
472  SL_FS_INFO_SECURE,
473  SL_FS_INFO_NOSIGNATURE,
474  SL_FS_INFO_PUBLIC_WRITE,
475  SL_FS_INFO_PUBLIC_READ,
476  0,
477  0
478 };
479 
480 
481 
482 _i16 sl_FsGetInfo(const _u8 *pFileName,const _u32 Token,SlFsFileInfo_t* pFsFileInfo)
483 {
484  _SlFsGetInfoMsg_u Msg;
485  _SlCmdExt_t CmdExt;
486  _u16 BitNum;
487 
488  /* verify that this api is allowed. if not allowed then
489  ignore the API execution and return immediately with an error */
490  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
491 
492  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
493 
494  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
495  {
496  return SL_ERROR_FS_WRONG_FILE_NAME;
497  }
498 
499 
500  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
501  CmdExt.pTxPayload1 = (_u8*)pFileName;
502 
503  Msg.Cmd.Token = Token;
504 
505  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsGetInfoCmdCtrl, &Msg, &CmdExt));
506 
507 
508  /* convert flags */
509  pFsFileInfo->Flags = 0;
510  for (BitNum = 0; BitNum < 16; BitNum++ )
511  {
512  if (( Msg.Rsp.Flags >> BitNum) & 0x1 )
513  {
514  pFsFileInfo->Flags |= FlagsTranslate[BitNum];
515  }
516  }
517 
518  pFsFileInfo->Len = Msg.Rsp.FileLen;
519  pFsFileInfo->MaxSize = Msg.Rsp.AllocatedLen;
520  pFsFileInfo->Token[0] = Msg.Rsp.Token[0];
521  pFsFileInfo->Token[1] = Msg.Rsp.Token[1];
522  pFsFileInfo->Token[2] = Msg.Rsp.Token[2];
523  pFsFileInfo->Token[3] = Msg.Rsp.Token[3];
524  pFsFileInfo->StorageSize = Msg.Rsp.FileStorageSize;
525  pFsFileInfo->WriteCounter = Msg.Rsp.FileWriteCounter;
526  return (_i16)((_i16)Msg.Rsp.Status);
527 }
528 #endif
529 
530 /*****************************************************************************/
531 /* sl_FsDel */
532 /*****************************************************************************/
533 typedef union
534 {
537 }_SlFsDeleteMsg_u;
538 
539 
540 #if _SL_INCLUDE_FUNC(sl_FsDel)
541 
542 static const _SlCmdCtrl_t _SlFsDeleteCmdCtrl =
543 {
544  SL_OPCODE_NVMEM_FILEDELCOMMAND,
545  (_SlArgSize_t)sizeof(SlFsDeleteCommand_t),
546  (_SlArgSize_t)sizeof(SlFsDeleteResponse_t)
547 };
548 
549 _i16 sl_FsDel(const _u8 *pFileName,const _u32 Token)
550 {
551  _SlFsDeleteMsg_u Msg;
552  _SlCmdExt_t CmdExt;
553 
554 
555  /* verify that this api is allowed. if not allowed then
556  ignore the API execution and return immediately with an error */
557  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
558 
559  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
560  {
561  return SL_ERROR_FS_WRONG_FILE_NAME;
562  }
563 
564 
565  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
566 
567  CmdExt.TxPayload1Len = (_u16)((_SlFsStrlen(pFileName)+4) & (~3)); /* add 4: 1 for NULL and the 3 for align */
568  CmdExt.pTxPayload1 = (_u8*)pFileName;
569  Msg.Cmd.Token = Token;
570 
571 
572  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsDeleteCmdCtrl, &Msg, &CmdExt));
573 
574  return (_i16)((_i16)Msg.Rsp.status);
575 }
576 #endif
577 
578 /*****************************************************************************/
579 /* sl_FsCtl */
580 /*****************************************************************************/
581 typedef union
582 {
585 }_SlFsFileSysControlMsg_u;
586 
587 #if _SL_INCLUDE_FUNC(sl_FsCtl)
588 
589 
590 const _SlCmdCtrl_t _SlFsFileSysControlCmdCtrl =
591 {
592  SL_OPCODE_NVMEM_NVMEMFILESYSTEMCONTROLCOMMAND,
595 };
596 
597 _i32 sl_FsCtl( SlFsCtl_e Command, _u32 Token, _u8 *pFileName, const _u8 *pData, _u16 DataLen, _u8 *pOutputData, _u16 OutputDataLen,_u32 *pNewToken )
598 {
599  _SlFsFileSysControlMsg_u Msg;
600  _SlCmdExt_t CmdExt;
601 
602  /* verify that this api is allowed. if not allowed then
603  ignore the API execution and return immediately with an error */
604  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
605 
606  Msg.Cmd.Token = Token;
607  Msg.Cmd.Operation = (_u8)Command;
608 
609  _SlDrvMemZero(&CmdExt, (_u16)sizeof(_SlCmdExt_t));
610 
611  if ((SL_FS_CTL_ROLLBACK == Command) || (SL_FS_CTL_COMMIT == Command ))
612  {
613  Msg.Cmd.FileNameLength = _SlFsStrlen(pFileName) + 1 ;
614 
615  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
616  {
617  return SL_ERROR_FS_WRONG_FILE_NAME;
618  }
619 
620 
621  /*the data is aligned*/
622  CmdExt.RxPayloadLen = DataLen;
623  CmdExt.pRxPayload = (_u8 *)(pData);
624 
625  CmdExt.TxPayload1Len = (_SlFsStrlen(pFileName) + 4) & (~3);
626  CmdExt.pTxPayload1 = pFileName;
627 
628  Msg.Cmd.BufferLength = CmdExt.RxPayloadLen + CmdExt.TxPayload1Len;
629 
630  if(CmdExt.pRxPayload != NULL && CmdExt.RxPayloadLen != 0)
631  {
632  CmdExt.RxPayloadLen = CmdExt.RxPayloadLen * (-1);
633  }
634 
635 
636  }
637  else if( SL_FS_CTL_RENAME == Command )
638  {
639  if ( _SlFsStrlen(pFileName) >= SL_FS_MAX_FILE_NAME_LENGTH )
640  {
641  return SL_ERROR_FS_WRONG_FILE_NAME;
642  }
643 
644  Msg.Cmd.FileNameLength = (_SlFsStrlen(pFileName) + 4) & (~3);
645 
646  /*current file name*/
647  CmdExt.RxPayloadLen = (_u16)Msg.Cmd.FileNameLength;
648  CmdExt.pRxPayload = pFileName;
649 
650 
651  /*New file name*/
652  CmdExt.TxPayload1Len = (_SlFsStrlen(pData) + 4) & (~3);;
653  CmdExt.pTxPayload1 = (_u8 *)(pData);
654 
655  Msg.Cmd.BufferLength = CmdExt.RxPayloadLen + CmdExt.TxPayload1Len;
656 
657  if(CmdExt.pRxPayload != NULL && CmdExt.RxPayloadLen != 0)
658  {
659  CmdExt.RxPayloadLen = CmdExt.RxPayloadLen * (-1);
660  }
661 
662  }
663  else
664  {
665  Msg.Cmd.FileNameLength = 0;
666 
667  CmdExt.TxPayload1Len = (DataLen + 3) & (~3);
668  CmdExt.pTxPayload1 = (_u8 *)(pData);
669 
670  CmdExt.RxPayloadLen = OutputDataLen;
671  CmdExt.pRxPayload = pOutputData;
672 
673  Msg.Cmd.BufferLength = CmdExt.TxPayload1Len;
674  }
675 
676 
677 
678  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsFileSysControlCmdCtrl, &Msg, &CmdExt));
679 
680  if( pNewToken != NULL )
681  {
682  *pNewToken = Msg.Rsp.Token;
683  }
684 
685  return (_i32)((_i32)Msg.Rsp.Status);
686 }
687 #endif
688 
689 
690 /*****************************************************************************/
691 /* sl_FsProgram */
692 /*****************************************************************************/
693 typedef union
694 {
697 }_SlFsProgrammingMsg_u;
698 
699 #if _SL_INCLUDE_FUNC(sl_FsProgram)
700 
701 
702 const _SlCmdCtrl_t _SlFsProgrammingCmdCtrl =
703 {
704  SL_OPCODE_NVMEM_NVMEMFSPROGRAMMINGCOMMAND,
705  sizeof(SlFsProgramCommand_t),
706  sizeof(SlFsProgramResponse_t)
707 };
708 
709 _i32 sl_FsProgram(const _u8* pData , _u16 DataLen ,const _u8 * pKey , _u32 Flags )
710 {
711  _SlFsProgrammingMsg_u Msg;
712  _SlCmdExt_t CmdExt;
713  _u16 ChunkLen;
714 
715  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
716 
717  Msg.Cmd.Flags = (_u32)Flags;
718 
719  _SlDrvResetCmdExt(&CmdExt);
720 
721  /* no data and no key, called only for extracting the image */
722  if( (DataLen == 0) && (pKey == NULL) )
723  {
724  Msg.Cmd.ChunkLen = 0;
725  Msg.Cmd.KeyLen = 0;
726  Msg.Cmd.Flags = Flags;
727  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsProgrammingCmdCtrl, &Msg, &CmdExt));
728  }
729  else if( (DataLen> 0) && ( pData == NULL))
730  {
731  return( ((_i32)SL_ERROR_FS_WRONG_INPUT_SIZE) << 16 );
732  }
733  else if( (DataLen == 0) && (pKey != NULL) )
734  {
735  Msg.Cmd.ChunkLen = 0;
736  Msg.Cmd.KeyLen = sizeof(SlFsKey_t);;
737  Msg.Cmd.Flags = Flags;
738  CmdExt.pTxPayload1 = (_u8*)pKey;
739  CmdExt.TxPayload1Len = sizeof(SlFsKey_t);
740  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsProgrammingCmdCtrl, &Msg, &CmdExt));
741  }
742  else /* DataLen > 0 */
743  {
744  if( (DataLen & 0xF) > 0)
745  {
746  return( ((_i32)SL_ERROR_FS_NOT_16_ALIGNED) << 16 );
747  }
748  Msg.Cmd.Flags = Flags;
749 
750  CmdExt.pTxPayload1 = (_u8 *)pData;
751  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE, DataLen);
752 
753  while(ChunkLen > 0)
754  {
755  Msg.Cmd.ChunkLen = ChunkLen;
756  CmdExt.TxPayload1Len = ChunkLen;
757  if( pKey != NULL )
758  {
759  Msg.Cmd.KeyLen = sizeof(SlFsKey_t);
760  CmdExt.RxPayloadLen = sizeof(SlFsKey_t);
761  CmdExt.pRxPayload = (_u8 *)pKey;
762 
763  if(CmdExt.pRxPayload != NULL && CmdExt.RxPayloadLen != 0)
764  {
765  CmdExt.RxPayloadLen = CmdExt.RxPayloadLen * (-1);
766  }
767  }
768  else /* No key */
769  {
770  Msg.Cmd.KeyLen = 0;
771  CmdExt.RxPayloadLen = 0;
772  CmdExt.pRxPayload = NULL;
773  }
774 
775  VERIFY_RET_OK( _SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsProgrammingCmdCtrl, &Msg, &CmdExt));
776 
777  if( Msg.Rsp.Status <= 0 ) /* Error or finished */
778  {
779  return (_i32)(Msg.Rsp.Status);
780  }
781 
782  DataLen -= ChunkLen;
783  CmdExt.pTxPayload1 += ChunkLen;
784 
785  ChunkLen = (_u16)sl_min(MAX_NVMEM_CHUNK_SIZE, DataLen);
786  }
787  }
788  return (_i32)(Msg.Rsp.Status);
789 }
790 #endif
791 
792 /*****************************************************************************/
793 /* sl_FsGetFileList */
794 /*****************************************************************************/
795 typedef union
796 {
799 }_SlFsGetFileListMsg_u;
800 
801 #if _SL_INCLUDE_FUNC(sl_FsGetFileList)
802 
803 
804 const _SlCmdCtrl_t _SlFsGetFileListCmdCtrl =
805 {
806  SL_OPCODE_NVMEM_NVMEMGETFILELISTCOMMAND,
807  sizeof(SlFsGetFileListCommand_t),
809 };
810 
811 
812 
813 
814 _i32 sl_FsGetFileList(_i32* pIndex, _u8 Count, _u8 MaxEntryLen , _u8* pBuff, SlFileListFlags_t Flags )
815 {
816  _SlFsGetFileListMsg_u Msg;
817  _SlCmdExt_t CmdExt;
818  _u16 OutputBufferSize;
819 
820 
821  /* verify that this api is allowed. if not allowed then
822  ignore the API execution and return immediately with an error */
823  VERIFY_API_ALLOWED(SL_OPCODE_SILO_FS);
824 
825  _SlDrvResetCmdExt(&CmdExt);
826 
827  Msg.Cmd.Index = *pIndex;
828  Msg.Cmd.MaxEntryLen = MaxEntryLen & (~3); /* round to modulu 4 */
829  Msg.Cmd.Count = Count;
830  Msg.Cmd.Flags = (_u8)Flags;
831 
832  OutputBufferSize = Msg.Cmd.Count * Msg.Cmd.MaxEntryLen;
833  if( OutputBufferSize > MAX_NVMEM_CHUNK_SIZE )
834  {
835  return SL_ERROR_FS_WRONG_INPUT_SIZE;
836  }
837 
838  CmdExt.RxPayloadLen = OutputBufferSize;
839  CmdExt.pRxPayload = pBuff;
840 
841  VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlFsGetFileListCmdCtrl, &Msg, &CmdExt));
842 
843 
844  *pIndex = Msg.Rsp.Index;
845 
846  return (_i32)((_i32)Msg.Rsp.NumOfEntriesOrError);
847 }
848 #endif
849 
_i32 sl_FsOpen(const _u8 *pFileName, const _u32 ModeAndMaxSize, _u32 *pToken)
open file for read or write from/to storage device
Definition: fs.c:149
_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:597
_i16 sl_FsGetInfo(const _u8 *pFileName, const _u32 Token, SlFsFileInfo_t *pFsFileInfo)
Get information of a file.
Definition: fs.c:482
_i16 sl_FsClose(const _i32 FileHdl, const _u8 *pCeritificateFileName, const _u8 *pSignature, const _u32 SignatureLen)
Close file in storage device.
Definition: fs.c:247
_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:709
_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:549
_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:814
_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:379
_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:304
Definition: fs.h:170