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 
17 #include "data_types.h"
18 #include "IChannel.h"
19 #include <stdio.h>
20 
21 typedef struct JcopOs_TranscieveInfo
22 {
23     INT32 timeout;
24     UINT8 sRecvData[1024];
25     UINT8 *sSendData;
26     INT32 sSendlength;
27     int sRecvlength;
28 }JcopOs_TranscieveInfo_t;
29 
30 typedef struct JcopOs_Version_Info
31 {
32     UINT8 osid;
33     UINT8 ver1;
34     UINT8 ver0;
35     UINT8 OtherValid;
36     UINT8 ver_status;
37 }JcopOs_Version_Info_t;
38 typedef struct JcopOs_ImageInfo
39 {
40     FILE *fp;
41     int   fls_size;
42     char  fls_path[256];
43     int   index;
44     UINT8 cur_state;
45     JcopOs_Version_Info_t    version_info;
46 }JcopOs_ImageInfo_t;
47 typedef struct JcopOs_Dwnld_Context
48 {
49     JcopOs_Version_Info_t    version_info;
50     JcopOs_ImageInfo_t       Image_info;
51     JcopOs_TranscieveInfo_t  pJcopOs_TransInfo;
52     IChannel_t               *channel;
53 }JcopOs_Dwnld_Context_t,*pJcopOs_Dwnld_Context_t;
54 
55 
56 static UINT8 Trigger_APDU[] = {0x4F, 0x70, 0x80, 0x13, 0x04, 0xDE, 0xAD, 0xBE, 0xEF, 0x00};
57 static UINT8 GetInfo_APDU[] = {0x00, //CLA
58                                0xA4, 0x04, 0x00, 0x0C, //INS, P1, P2, Lc
59                                0xD2, 0x76, 0x00, 0x00, 0x85, 0x41, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,   //Data
60                                0x00 //Le
61                               };
62 static UINT8 GetInfo_Data[] = {0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x72, 0x4F, 0x53};
63 
64 #define OSID_OFFSET  9
65 #define VER1_OFFSET  10
66 #define VER0_OFFSET  11
67 #define JCOPOS_HEADER_LEN 5
68 
69 #define JCOP_UPDATE_STATE0 0
70 #define JCOP_UPDATE_STATE1 1
71 #define JCOP_UPDATE_STATE2 2
72 #define JCOP_UPDATE_STATE3 3
73 #define JCOP_MAX_RETRY_CNT 3
74 #define JCOP_INFO_PATH  "/data/vendor/ese/jcop_info.txt"
75 
76 #define JCOP_MAX_BUF_SIZE 10240
77 
78 class JcopOsDwnld
79 {
80 public:
81 
82 /*******************************************************************************
83 **
84 ** Function:        getInstance
85 **
86 ** Description:     Get the SecureElement singleton object.
87 **
88 ** Returns:         SecureElement object.
89 **
90 *******************************************************************************/
91 static JcopOsDwnld* getInstance ();
92 
93 
94 /*******************************************************************************
95 **
96 ** Function:        getJcopOsFileInfo
97 **
98 ** Description:     Verify all the updater files required for download
99 **                  are present or not
100 **
101 ** Returns:         True if ok.
102 **
103 *******************************************************************************/
104 bool getJcopOsFileInfo();
105 
106 /*******************************************************************************
107 **
108 ** Function:        initialize
109 **
110 ** Description:     Initialize all member variables.
111 **                  native: Native data.
112 **
113 ** Returns:         True if ok.
114 **
115 *******************************************************************************/
116 bool initialize (IChannel_t *channel);
117 
118 /*******************************************************************************
119 **
120 ** Function:        finalize
121 **
122 ** Description:     Release all resources.
123 **
124 ** Returns:         None
125 **
126 *******************************************************************************/
127 void finalize ();
128 
129 tJBL_STATUS JcopOs_Download();
130 
131 tJBL_STATUS TriggerApdu(JcopOs_ImageInfo_t* pVersionInfo, tJBL_STATUS status, JcopOs_TranscieveInfo_t* pTranscv_Info);
132 
133 tJBL_STATUS GetInfo(JcopOs_ImageInfo_t* pVersionInfo, tJBL_STATUS status, JcopOs_TranscieveInfo_t* pTranscv_Info);
134 
135 tJBL_STATUS load_JcopOS_image(JcopOs_ImageInfo_t *Os_info, tJBL_STATUS status, JcopOs_TranscieveInfo_t *pTranscv_Info);
136 
137 tJBL_STATUS JcopOs_update_seq_handler();
138 
139 IChannel_t *mchannel;
140 
141 private:
142 static JcopOsDwnld sJcopDwnld;
143 bool mIsInit;
144 tJBL_STATUS GetJcopOsState(JcopOs_ImageInfo_t *Os_info, UINT8 *counter);
145 tJBL_STATUS SetJcopOsState(JcopOs_ImageInfo_t *Os_info, UINT8 state);
146 };
147