1 /*
2  * Copyright (C) 2015 NXP Semiconductors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16#ifndef ALA_H_
17#define ALA_H_
18
19#define NXP_LS_AID
20#include "data_types.h"
21#include "IChannel.h"
22#include <stdio.h>
23
24typedef struct Ala_ChannelInfo
25{
26    UINT8 channel_id;
27    bool  isOpend;
28}Ala_ChannelInfo_t;
29
30typedef struct Ala_TranscieveInfo
31{
32    INT32 timeout;
33    UINT8 sRecvData[1024];
34    UINT8 sSendData[1024];
35    INT32 sSendlength;
36    int   sRecvlength;
37    UINT8 sTemp_recvbuf[1024];
38}Ala_TranscieveInfo_t;
39
40#if(NXP_LDR_SVC_VER_2 == TRUE)
41typedef struct Ala_ImageInfo
42{
43    FILE                 *fp;
44    int                  fls_size;
45    char                 fls_path[384];
46    int                  bytes_read;
47    FILE                 *fResp;
48    int                  fls_RespSize;
49    char                 fls_RespPath[384];
50    int                  bytes_wrote;
51    Ala_ChannelInfo_t    Channel_Info[10];
52    UINT8                channel_cnt;
53}Ala_ImageInfo_t;
54typedef enum
55{
56    LS_Default = 0x00,
57    LS_Cert = 0x7F21,
58    LS_Sign = 0x60,
59    LS_Comm = 0x40
60}Ls_TagType;
61#else
62typedef struct Ala_ImageInfo
63{
64    FILE                 *fp;
65    int                  fls_size;
66    char                 fls_path[256];
67    int                  bytes_read;
68    Ala_ChannelInfo_t    Channel_Info[10];
69    UINT8                channel_cnt;
70}Ala_ImageInfo_t;
71#endif
72typedef struct Ala_lib_Context
73{
74    IChannel_t            *mchannel;
75    Ala_ImageInfo_t       Image_info;
76    Ala_TranscieveInfo_t  Transcv_Info;
77}Ala_Dwnld_Context_t,*pAla_Dwnld_Context_t;
78
79static UINT8 OpenChannel[] = {0x00, 0x70, 0x00, 0x00, 0x01};
80#if(NXP_LDR_SVC_VER_2 == TRUE)
81static UINT8 GetData[] = {0x80, 0xCA, 0x00, 0x46, 0x00};
82#ifndef NXP_LS_AID
83static UINT8 SelectAla[] = {0x00, 0xA4, 0x04, 0x00, 0x0D, 0xA0, 0x00, 0x00, 0x03, 0x96, 0x41, 0x4C, 0x41, 0x01, 0x43, 0x4F, 0x52, 0x01};
84#else
85static UINT8 SelectAla[] = {0x00, 0xA4, 0x04, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x03, 0x96, 0x54, 0x43, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0B
86, 0x00,0x01};
87#endif
88#else
89static UINT8 SelectAla[] = {0x00, 0xA4, 0x04, 0x00, 0x0D, 0xA0, 0x00, 0x00, 0x03, 0x96, 0x41, 0x4C, 0x41, 0x01, 0x43, 0x4F, 0x52, 0x01};
90#endif
91/*ALA2*/
92#if(NXP_LDR_SVC_VER_2 == TRUE)
93#define NOOFAIDS     0x03
94#define LENOFAIDS    0x16
95#ifndef NXP_LS_AID
96static UINT8 ArrayOfAIDs[NOOFAIDS][LENOFAIDS] = {
97        {0x12, 0x00, 0xA4, 0x04, 0x00, 0x0D, 0xA0, 0x00, 0x00, 0x03, 0x96, 0x41, 0x4C, 0x41, 0x01, 0x4C, 0x44, 0x52, 0x01,0x00,0x00,0x00},
98        {0x12, 0x00, 0xA4, 0x04, 0x00, 0x0D, 0xA0, 0x00, 0x00, 0x03, 0x96, 0x41, 0x4C, 0x41, 0x01, 0x43, 0x4F, 0x52, 0x01,0x00,0x00,0x00},
99        {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
100};
101#else
102static UINT8 ArrayOfAIDs[NOOFAIDS][LENOFAIDS] = {
103        {0x14, 0x00, 0xA4, 0x04, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x03, 0x96, 0x54, 0x43, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0B,0x00,0x02,0x00},
104        {0x14, 0x00, 0xA4, 0x04, 0x00, 0x0F, 0xA0, 0x00, 0x00, 0x03, 0x96, 0x54, 0x43, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0B,0x00,0x01,0x00},
105        {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
106};
107#endif
108#define TAG_CERTIFICATE     0x7F21
109#define TAG_LSES_RESP       0x4E
110#define TAG_LSES_RSPLEN     0x02
111#define TAG_SERIAL_NO       0x93
112#define TAG_LSRE_ID         0x42
113#define TAG_LSRE_SIGNID     0x45
114#define TAG_CERTFHOLD_ID    0x5F20
115#define TAG_KEY_USAGE       0x95
116#define TAG_EFF_DATE        0x5F25
117#define TAG_EXP_DATE        0x5F24
118#define TAG_CCM_PERMISSION  0x53
119#define TAG_SIG_RNS_COMP    0x5F37
120
121#define TAG_LS_VER1         0x9F
122#define TAG_LS_VER2         0x08
123#define LS_DEFAULT_STATUS   0x6340
124#define LS_SUCCESS_STATUS   0x9000
125#define TAG_RE_KEYID        0x65
126
127#define LS_ABORT_SW1        0x69
128#define LS_ABORT_SW2        0x87
129#define AID_MEM_PATH       "/data/vendor/ese/AID_MEM.txt"
130#define LS_STATUS_PATH     "/data/vendor/ese/LS_Status.txt"
131#define LS_SRC_BACKUP      "/data/vendor/ese/LS_Src_Backup.txt"
132#define LS_DST_BACKUP      "/data/vendor/ese/LS_Dst_Backup.txt"
133#define MAX_CERT_LEN        (255+137)
134
135#endif
136/*ALA2*/
137
138#define JCOP3_WR
139#define APPLET_PATH        "/data/ala/"
140#define MAX_SIZE            0xFF
141#define PARAM_P1_OFFSET     0x02
142#define FIRST_BLOCK         0x05
143#define LAST_BLOCK          0x84
144#define ONLY_BLOCK          0x85
145#define CLA_BYTE            0x80
146#define JSBL_HEADER_LEN     0x03
147#define ALA_CMD_HDR_LEN     0x02
148
149/* Definations for TAG ID's present in the script file*/
150#define TAG_SELECT_ID       0x6F
151#define TAG_ALA_ID          0x84
152#define TAG_PRO_DATA_ID     0xA5
153#define TAG_JSBL_HDR_ID     0x60
154#define TAG_JSBL_KEY_ID     0x61
155#define TAG_SIGNATURE_ID    0x41
156#define TAG_ALA_CMD_ID      0x40
157#define TAG_JSBL_CER_ID     0x44
158
159/*Definitions for Install for load*/
160#define INSTAL_LOAD_ID      0xE6
161#define LOAD_CMD_ID         0xE8
162#define LOAD_MORE_BLOCKS    0x00
163#define LOAD_LAST_BLOCK     0x80
164
165#define STORE_DATA_CLA      0x80
166#define STORE_DATA_INS      0xE2
167#define STORE_DATA_LEN      32
168#define STORE_DATA_TAG      0x4F
169
170/*******************************************************************************
171**
172** Function:        initialize
173**
174** Description:     Initialize all member variables.
175**                  native: Native data.
176**
177** Returns:         True if ok.
178**
179*******************************************************************************/
180BOOLEAN initialize (IChannel_t *channel);
181
182/*******************************************************************************
183**
184** Function:        finalize
185**
186** Description:     Release all resources.
187**
188** Returns:         None
189**
190*******************************************************************************/
191void finalize ();
192
193#if(NXP_LDR_SVC_VER_2 == TRUE)
194tJBL_STATUS Perform_ALA(const char *path,const char *dest, const UINT8 *pdata, UINT16 len, UINT8 *respSW);
195#else
196tJBL_STATUS Perform_ALA(const char *path, const UINT8 *pdata, UINT16 len);
197#endif
198tJBL_STATUS GetJsbl_Certificate_Refkey(UINT8 *pkey, INT32 *pKeylen);
199
200static tJBL_STATUS
201ALA_OpenChannel(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo);
202
203static tJBL_STATUS
204ALA_SelectAla(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo);
205
206static tJBL_STATUS
207ALA_StoreData(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo);
208
209static tJBL_STATUS
210ALA_loadapplet(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info);
211
212#if(NXP_LDR_SVC_VER_2 == TRUE)
213static tJBL_STATUS
214ALA_update_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo), const char *name, const char *dest);
215
216tJBL_STATUS GetLs_Version(UINT8 *pKey);
217
218tJBL_STATUS GetVer_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo));
219
220tJBL_STATUS Write_Response_To_OutFile(Ala_ImageInfo_t *image_info, UINT8* RecvData, INT32 recvlen, Ls_TagType tType);
221
222tJBL_STATUS Check_Certificate_Tag(UINT8 *read_buf, UINT16 *offset1);
223
224tJBL_STATUS Check_SerialNo_Tag(UINT8 *read_buf, UINT16 *offset1);
225
226tJBL_STATUS Check_LSRootID_Tag(UINT8 *read_buf, UINT16 *offset1);
227
228tJBL_STATUS Check_CertHoldID_Tag(UINT8 *read_buf, UINT16 *offset1);
229
230tJBL_STATUS Check_Date_Tag(UINT8 *read_buf, UINT16 *offset1);
231
232tJBL_STATUS Check_45_Tag(UINT8 *read_buf, UINT16 *offset1, UINT8 *tag45Len);
233
234tJBL_STATUS Certificate_Verification(Ala_ImageInfo_t *Os_info, Ala_TranscieveInfo_t
235*pTranscv_Info, UINT8 *read_buf, UINT16 *offset1, UINT8 *tag45Len);
236
237tJBL_STATUS Check_Complete_7F21_Tag(Ala_ImageInfo_t *Os_info,
238       Ala_TranscieveInfo_t *pTranscv_Info, UINT8 *read_buf, UINT16 *offset);
239BOOLEAN ALA_UpdateExeStatus(UINT16 status);
240tJBL_STATUS ALA_getAppletLsStatus(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info);
241tJBL_STATUS Get_LsStatus(UINT8 *pVersion);
242tJBL_STATUS Get_LsAppletStatus(UINT8 *pVersion);
243#else
244static tJBL_STATUS
245ALA_update_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo), const char *name);
246#endif
247static tJBL_STATUS
248JsblCerId_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t* pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo));
249
250tJBL_STATUS ALA_SendtoEse(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info);
251#if(NXP_LDR_SVC_VER_2 == TRUE)
252tJBL_STATUS GetLsStatus_seq_handler(tJBL_STATUS (*seq_handler[])(Ala_ImageInfo_t*
253        pContext, tJBL_STATUS status, Ala_TranscieveInfo_t* pInfo));
254tJBL_STATUS ALA_SendtoAla(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info, Ls_TagType tType);
255#else
256tJBL_STATUS ALA_SendtoAla(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info);
257#endif
258tJBL_STATUS ALA_CloseChannel(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info);
259#if(NXP_LDR_SVC_VER_2 == TRUE)
260tJBL_STATUS ALA_ProcessResp(Ala_ImageInfo_t *image_info, INT32 recvlen, Ala_TranscieveInfo_t *trans_info, Ls_TagType tType);
261#else
262tJBL_STATUS ALA_ProcessResp(Ala_ImageInfo_t *image_info, INT32 recvlen, Ala_TranscieveInfo_t *trans_info);
263#endif
264#if(NXP_LDR_SVC_VER_2 == TRUE)
265tJBL_STATUS ALA_Check_KeyIdentifier(Ala_ImageInfo_t *Os_info, tJBL_STATUS status,
266   Ala_TranscieveInfo_t *pTranscv_Info, UINT8* temp_buf, tJBL_STATUS flag,
267   INT32 wNewLen);
268#else
269tJBL_STATUS ALA_Check_KeyIdentifier(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info);
270#endif
271tJBL_STATUS ALA_ReadScript(Ala_ImageInfo_t *Os_info, UINT8 *read_buf);
272
273tJBL_STATUS Process_EseResponse(Ala_TranscieveInfo_t *pTranscv_Info, INT32 recv_len, Ala_ImageInfo_t *Os_info);
274
275tJBL_STATUS Process_SelectRsp(UINT8* Recv_data, INT32 Recv_len);
276#ifdef JCOP3_WR
277tJBL_STATUS Send_Backall_Loadcmds(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info);
278
279tJBL_STATUS Bufferize_load_cmds(Ala_ImageInfo_t *Os_info, tJBL_STATUS status, Ala_TranscieveInfo_t *pTranscv_Info);
280#endif
281UINT8 Numof_lengthbytes(UINT8 *read_buf, INT32 *wLen);
282#endif /*ALA_H*/
283