xref: /utopia/UTPA2-700.0.x/modules/security/drv/cipher/mdrvCIPHER.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ///////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2010 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ///////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file   mdrvCIPHER.c
98 /// @brief   API Interface
99 /// @author MStar Semiconductor,Inc.
100 /// @attention
101 ///////////////////////////////////////////////////////////////////////////////////////////////////
102 
103 
104 //-------------------------------------------------------------------------------------------------
105 //  Include Files
106 //-------------------------------------------------------------------------------------------------
107 #ifdef MSOS_TYPE_LINUX_KERNEL
108 #include <linux/string.h>
109 #include <linux/uaccess.h>
110 #include <linux/compat.h>
111 #else
112 #include <stdio.h>
113 #include <string.h>
114 #endif
115 
116 #include "MsTypes.h"
117 
118 #include "drvCIPHER_v2.h"
119 #include "drvCIPHER_private.h"
120 
121 #include "utopia.h"
122 #include "utopia_dapi.h"
123 #include "MsOS.h"
124 
125 //-------------------------------------------------------------------------------------------------
126 //  Driver Compiler Options
127 //-------------------------------------------------------------------------------------------------
128 
129 //-------------------------------------------------------------------------------------------------
130 //  Global Variables
131 //-------------------------------------------------------------------------------------------------
132 void* pModuleCipher = NULL;
133 
134 //-------------------------------------------------------------------------------------------------
135 //  Local Defines
136 //-------------------------------------------------------------------------------------------------
137 #define CIPHER_MSG(_f, _a...)                             //printf(_f, ##_a)
138 
139 //-------------------------------------------------------------------------------------------------
140 //  Local Structures
141 //-------------------------------------------------------------------------------------------------
142 
143 //-------------------------------------------------------------------------------------------------
144 //  Local Variables
145 //-------------------------------------------------------------------------------------------------
146 MS_U16 _u16ThreadID = 1;
147 MS_U32 _u32CallbackID = 1;
148 
149 //-------------------------------------------------------------------------------------------------
150 //  Local Functions
151 //-------------------------------------------------------------------------------------------------
152 
153 
154 //-------------------------------------------------------------------------------------------------
155 //  Global Functions
156 //-------------------------------------------------------------------------------------------------
CIPHERRegisterToUtopia(void)157 void CIPHERRegisterToUtopia(void)
158 {
159     CIPHER_RESOURCE_PRIVATE * pResPri = NULL;
160     void* psResource = NULL;
161 
162     // 1. deal with module
163 
164     // create a DSCMB module
165     // a. size = 8
166     // b. Dst: pModuleDscmb
167     UtopiaModuleCreate(MODULE_CIPHER, 8, &pModuleCipher);
168     // register a dscmb module
169     UtopiaModuleRegister(pModuleCipher);
170     //setup module functions
171     UtopiaModuleSetupFunctionPtr(pModuleCipher, (FUtopiaOpen)CIPHEROpen, (FUtopiaClose)CIPHERClose, (FUtopiaIOctl)CIPHERIoctl);
172 
173     // 2. deal with resource
174     // create and allocate resource private data(drv Global shared data)
175     UtopiaResourceCreate("CIPHER", sizeof(CIPHER_RESOURCE_PRIVATE), &psResource);
176     UtopiaResourceRegister(pModuleCipher, psResource, E_CIPHER_RESOURCE);
177     UtopiaModuleAddResourceEnd(pModuleCipher, E_CIPHER_RESOURCE);
178 
179     if(UtopiaResourceObtain(pModuleCipher, E_CIPHER_RESOURCE, &psResource) != UTOPIA_STATUS_SUCCESS)
180     {
181          CIPHER_MSG("UtopiaResourceObtain fail\n");
182          return ;
183     }
184     UtopiaResourceGetPrivate(psResource, (void**)&pResPri);
185 
186     UtopiaResourceRelease(psResource);
187 }
188 
CIPHEROpen(void ** ppInstance,MS_U32 u32ModuleVersion,void * pAttribute)189 MS_U32 CIPHEROpen(void** ppInstance, MS_U32 u32ModuleVersion, void* pAttribute)
190 {
191     CIPHER_MSG("CIPHER open \n");
192 
193     if(ppInstance == 0)
194     {
195         return UTOPIA_STATUS_PARAMETER_ERROR;
196     }
197 
198     CIPHER_INSTANT_PRIVATE*    psCipherInstPri = NULL;
199 
200     //create instance
201     //1.  Allocate instant private data, prepare argument variable.
202     //2.  Dst: ppInstance
203     UtopiaInstanceCreate(sizeof(CIPHER_INSTANT_PRIVATE), ppInstance);
204 
205     //get instant private data pointer
206     // 1. Dst: psCipherInstPri
207     UtopiaInstanceGetPrivate(*ppInstance, (void**)&psCipherInstPri);
208 
209     return UTOPIA_STATUS_SUCCESS;
210 }
211 
CIPHERIoctl(void * pInstance,MS_U32 u32Cmd,void * pu32Args)212 MS_U32 CIPHERIoctl(void* pInstance, MS_U32 u32Cmd, void* pu32Args)
213 {
214     void* pstRes = NULL;
215     MS_U32 u32Ret = UTOPIA_STATUS_FAIL;
216     CIPHER_RESOURCE_PRIVATE* pstResPri  = NULL;
217     //CIPHER_INSTANT_PRIVATE*  pstInstPri = NULL;
218 
219     if(UtopiaResourceObtain(pModuleCipher, E_CIPHER_RESOURCE, &pstRes) != UTOPIA_STATUS_SUCCESS)
220     {
221          CIPHER_MSG("UtopiaResourceObtain fail\n");
222          return UTOPIA_STATUS_ERR_RESOURCE;
223     }
224     UtopiaResourceGetPrivate(pstRes, (void**)&pstResPri);
225 
226     if((FALSE == pstResPri->bInited_Drv)  &&
227         (u32Cmd != E_MDRV_CMD_CIPHER_Init) /*&&          // which command don't need do it after init?
228         (u32Cmd != E_MDRV_CMD_DSCMB_QueryCap) &&
229         (u32Cmd != E_MDRV_CMD_DSCMB_SetDBGLevel) &&
230         (u32Cmd != E_MDRV_CMD_DSCMB_GetLibVer) &&
231         (u32Cmd != E_MDRV_CMD_DSCMB_Exit)*/)
232     {
233         UtopiaResourceRelease(pstRes);
234         return UTOPIA_STATUS_FAIL;
235     }
236 
237     //set value from resource private to local file variable
238     //the local file pointer variable will extern to drvCIPHER.c
239     if(_u16ThreadID < pstResPri->u16ThreadID)
240         _u16ThreadID = pstResPri->u16ThreadID;
241     else
242         pstResPri->u16ThreadID = _u16ThreadID;
243 
244     if(_u32CallbackID < pstResPri->u32CallbackID)
245         _u32CallbackID = pstResPri->u32CallbackID;
246     else
247         pstResPri->u32CallbackID = _u32CallbackID;
248 
249     switch(u32Cmd)
250     {
251         // General API
252         case E_MDRV_CMD_CIPHER_Init:
253         {
254             if(pstResPri->bInited_Drv == FALSE)
255             {
256                 if(DRV_CIPHER_OK == _MDrv_CIPHER_Init())
257                 {
258                      pstResPri->bInited_Drv = TRUE;
259                      u32Ret = UTOPIA_STATUS_SUCCESS;
260                 }
261                 else
262                 {
263                     CIPHER_MSG("E_MDRV_CMD_CIPHER_Init fail\n");
264                     u32Ret = UTOPIA_STATUS_FAIL;
265                 }
266             }
267             else
268             {
269                 if(DRV_CIPHER_OK == _MDrv_CIPHER_InitBySWFlag(TRUE))
270                 {
271                     u32Ret = UTOPIA_STATUS_SUCCESS;
272                 }
273                 else
274                 {
275                     CIPHER_MSG("E_MDRV_CMD_CIPHER_Init fail\n");
276                     u32Ret = UTOPIA_STATUS_FAIL;
277                 }
278             }
279 
280             CIPHER_MSG("E_MDRV_CMD_CIPHER_Init\n");
281         }
282         break;
283 
284         case E_MDRV_CMD_CIPHER_Reset:
285         {
286             if(DRV_CIPHER_OK == _MDrv_CIPHER_Reset())
287             {
288                 u32Ret = UTOPIA_STATUS_SUCCESS;
289             }
290             else
291             {
292                 CIPHER_MSG("E_MDRV_CMD_CIPHER_Reset fail\n");
293                 u32Ret = UTOPIA_STATUS_FAIL;
294             }
295 
296             CIPHER_MSG("E_MDRV_CMD_CIPHER_Reset\n");
297         }
298         break;
299 
300         case E_MDRV_CMD_CIPHER_ResetKey:
301         {
302             CIPHER_RESETKEY* pstRestKey = (CIPHER_RESETKEY*)pu32Args;
303 
304             pstRestKey->ret = _MDrv_CIPHER_ResetKey(pstRestKey->u32CAVid, pstRestKey->u32KeyIdx);
305             if(DRV_CIPHER_OK == pstRestKey->ret)
306             {
307                 u32Ret = UTOPIA_STATUS_SUCCESS;
308             }
309             else
310             {
311                 CIPHER_MSG("E_MDRV_CMD_CIPHER_ResetKey fail\n");
312                 u32Ret = UTOPIA_STATUS_FAIL;
313             }
314 
315             CIPHER_MSG("E_MDRV_CMD_CIPHER_ResetKey\n");
316         }
317         break;
318 
319         case E_MDRV_CMD_CIPHER_DbgLevel:
320         {
321             CIPHER_DBGLEVEL* pstDbgLevel = (CIPHER_DBGLEVEL*)pu32Args;
322 
323             if(DRV_CIPHER_OK == _MDrv_CIPHER_SetDbgLevel(pstDbgLevel->eDBGMsgLevel))
324             {
325                 u32Ret = UTOPIA_STATUS_SUCCESS;
326             }
327             else
328             {
329                 CIPHER_MSG("E_MDRV_CMD_CIPHER_DbgLevel fail\n");
330                 u32Ret = UTOPIA_STATUS_FAIL;
331             }
332 
333             CIPHER_MSG("E_MDRV_CMD_CIPHER_DbgLevel\n");
334         }
335         break;
336 
337         case E_MDRV_CMD_CIPHER_DMACalc:
338         {
339             CIPHER_DMACALC* pstDMACalc = (CIPHER_DMACALC*)pu32Args;
340 
341             pstDMACalc->ret = _MDrv_CIPHER_DMACalc(pstDMACalc->stCfg, pstDMACalc->pu32CmdId);
342             if(DRV_CIPHER_OK == pstDMACalc->ret)
343             {
344                 u32Ret = UTOPIA_STATUS_SUCCESS;
345             }
346             else
347             {
348                 CIPHER_MSG("E_MDRV_CMD_CIPHER_DMACalc fail\n");
349                 u32Ret = UTOPIA_STATUS_FAIL;
350             }
351 
352             CIPHER_MSG("E_MDRV_CMD_CIPHER_DMACalc\n");
353         }
354         break;
355 
356         case E_MDRV_CMD_CIPHER_HASH:
357         {
358             CIPHER_HASHCALC* pstHASHCalc = (CIPHER_HASHCALC*)pu32Args;
359 
360             pstHASHCalc->ret = _MDrv_CIPHER_HASH(pstHASHCalc->stCfg, pstHASHCalc->pu32CmdId);
361             if(DRV_CIPHER_OK == pstHASHCalc->ret)
362             {
363                 u32Ret = UTOPIA_STATUS_SUCCESS;
364             }
365             else
366             {
367                 CIPHER_MSG("E_MDRV_CMD_CIPHER_HASH fail\n");
368                 u32Ret = UTOPIA_STATUS_FAIL;
369             }
370 
371             CIPHER_MSG("E_MDRV_CMD_CIPHER_HASH\n");
372         }
373         break;
374 
375         case E_MDRV_CMD_CIPHER_IsDMADone:
376         {
377             CIPHER_ISDMADONE* pstIsDMADone = (CIPHER_ISDMADONE*)pu32Args;
378 
379             if(TRUE == _MDrv_CIPHER_IsDMADone(pstIsDMADone->u32CmdId, pstIsDMADone->pu32Exception))
380             {
381                 u32Ret = UTOPIA_STATUS_SUCCESS;
382             }
383             else
384             {
385                 CIPHER_MSG("E_MDRV_CMD_CIPHER_IsDMADone fail\n");
386                 u32Ret = UTOPIA_STATUS_FAIL;
387             }
388 
389             CIPHER_MSG("E_MDRV_CMD_CIPHER_IsDMADone\n");
390         }
391         break;
392 
393         case E_MDRV_CMD_CIPHER_IsHASHDone:
394         {
395             CIPHER_ISHASHDONE* pstIsHASHDone = (CIPHER_ISHASHDONE*)pu32Args;
396 
397             if(TRUE == _MDrv_CIPHER_IsHASHDone(pstIsHASHDone->u32CmdId, pstIsHASHDone->pu32Exception))
398             {
399                 u32Ret = UTOPIA_STATUS_SUCCESS;
400             }
401             else
402             {
403                 CIPHER_MSG("E_MDRV_CMD_CIPHER_IsHASHDone fail\n");
404                 u32Ret = UTOPIA_STATUS_FAIL;
405             }
406 
407             CIPHER_MSG("E_MDRV_CMD_CIPHER_IsHASHDone\n");
408         }
409         break;
410 
411         default:
412             CIPHER_MSG("CIPHERIoctl - Unknown commend 0x%x!!!\n", (int)u32Cmd);
413             return UTOPIA_STATUS_FAIL;
414     }
415 
416     pstResPri->u16ThreadID = _u16ThreadID;
417     pstResPri->u32CallbackID = _u32CallbackID;
418 
419     UtopiaResourceRelease(pstRes);
420 
421     return u32Ret;
422 }
423 
CIPHERClose(void * pInstance)424 MS_U32 CIPHERClose(void* pInstance)
425 {
426     UtopiaInstanceDelete(pInstance);
427 
428     return UTOPIA_STATUS_SUCCESS;
429 }
430