comparison gsm-fw/comlib/cl_imei.c @ 664:d36f647c2432

gsm-fw/comlib: initial import of TI's source cl_des.c and cl_imei.c are from the Leonardo semi-src; others are from LoCosto
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 28 Sep 2014 05:09:53 +0000
parents
children b34e5351438e
comparison
equal deleted inserted replaced
663:643379e7e141 664:d36f647c2432
1 /*
2 +-----------------------------------------------------------------------------
3 | Project : COMLIB
4 | Modul : cl_imei.c
5 +-----------------------------------------------------------------------------
6 | Copyright 2002 Texas Instruments Berlin, AG
7 | All rights reserved.
8 |
9 | This file is confidential and a trade secret of Texas
10 | Instruments Berlin, AG
11 | The receipt of or possession of this file does not convey
12 | any rights to reproduce or disclose its contents or to
13 | manufacture, use, or sell anything it may describe, in
14 | whole, or in part, without the specific written consent of
15 | Texas Instruments Berlin, AG.
16 +-----------------------------------------------------------------------------
17 | Purpose : Definitions of common library functions: IMEI decryption with
18 DES algorithm
19 +-----------------------------------------------------------------------------
20 */
21 /*
22 * Version 1.0
23 */
24
25 /**********************************************************************************/
26
27 /*
28 NOTE:
29 */
30
31 /**********************************************************************************/
32
33 #ifndef CL_IMEI_C
34 #define CL_IMEI_C
35
36 #include "typedefs.h"
37 #include "vsi.h" /* to get a lot of macros */
38
39 #ifndef _SIMULATION_
40 #include "ffs/ffs.h"
41 #include "config/chipset.cfg"
42 #include "config/board.cfg"
43 #include "memif/mem.h"
44 #include "pcm.h"
45 #endif /* ifdef SIMULATION */
46
47 #include "cl_imei.h"
48 #include "cl_des.h"
49 #include <string.h>
50
51 #if defined(CL_IMEI_CALYPSO_PLUS_PLATFORM) && defined(FF_PROTECTED_IMEI)
52 #include "secure_rom/secure_rom.h"
53 #endif
54
55 static UBYTE stored_imei[CL_IMEI_SIZE]; /* when the imei is read once, the value
56 is stored in this buffer */
57 static UBYTE imei_flag = 0; /* this flag indicates, if IMEI was successful read
58 and is stored in the stored_imei buffer */
59
60 #if !defined (CL_IMEI_CALYPSO_PLUS_PLATFORM)
61 /* Default IMEISV for D-Sample 00440000-350-111-20 */
62 const UBYTE C_DEFAULT_IMEISV_DSAMPLE[CL_IMEI_SIZE] =
63 {0x00, 0x44, 0x00, 0x00, 0x35, 0x01, 0x11, 0x20};
64 #define CL_IMEI_FFS_PATH "/gsm/imei.enc"
65 #endif /* CL_IMEI_CALYPSO_PLATFORM */
66
67 #ifdef CL_IMEI_CALYPSO_PLUS_PLATFORM
68 /* Default IMEISV for E-Sample 00440000-351-222-30 */
69 const UBYTE C_DEFAULT_IMEISV_ESAMPLE[CL_IMEI_SIZE] =
70 {0x00, 0x44, 0x00, 0x00, 0x35, 0x12, 0x22, 0x30};
71 #endif /* CL_IMEI_CALYPSO_PLUS_PLATFORM */
72
73 /*==== FUNCTIONS ==================================================*/
74 #ifdef FF_PROTECTED_IMEI
75
76 #ifdef CL_IMEI_CALYPSO_PLATFORM
77 /*
78 +------------------------------------------------------------------------------
79 | Function : get_dieID
80 +------------------------------------------------------------------------------
81 | Description : the function reads the Die-ID from base band processor and
82 | extracts it from 4 BYTEs to 8 BYTEs.
83 |
84 | Parameters : inBufSize - size of buffer where to store Die ID, min.8 BYTE
85 | *outBufPtr - pointer to buffer where to store the Die ID
86 | Return : void
87 +------------------------------------------------------------------------------
88 */
89 LOCAL void get_dieID(USHORT inBufSize, UBYTE *outBufPtr)
90 {
91 int i;
92 USHORT *outBuf16 = (USHORT*)&outBufPtr[0];
93 volatile USHORT *reg_p = (USHORT *) CL_IMEI_DIE_ID_REG;
94
95 TRACE_FUNCTION("get_dieID()");
96
97 if(inBufSize < CL_IMEI_DIE_ID_SIZE){
98 TRACE_ERROR("CL IMEI ERROR: buffer size for Die ID to short!");
99 }
100 #ifdef IMEI_DEBUG
101 TRACE_EVENT_P1("CL IMEI INFO: Die-ID address(0x%x)", CL_IMEI_DIE_ID_REG);
102 #endif
103 for (i = 0; i < CL_IMEI_DIE_ID_SIZE; i++) {
104 /* Die ID is 4 BYTE long, extract it to 8 BYTE. */
105 outBuf16[i] = (USHORT)(*(UINT8*)(reg_p)++);
106 }
107 }
108
109 /*
110 +------------------------------------------------------------------------------
111 | Function : ffs_get_imeisv
112 +------------------------------------------------------------------------------
113 | Description : the function reads IMEI from FFS
114 |
115 | Parameters : inBufSize - size of buffer where to store IMEI, min. 8 BYTE
116 | *outBufPtr - pointer to buffer where to store the IMEI
117 | Return : 0 - OK
118 | <0 - ERROR
119 +------------------------------------------------------------------------------
120 */
121 LOCAL BYTE ffs_get_imeisv (USHORT inBufSize, UBYTE *outBufPtr)
122 {
123 UBYTE i;
124 UBYTE isdid_buf[CL_IMEI_ISDID_SIZE];
125 UBYTE r_dieId[CL_DES_KEY_SIZE]; /* read Die ID */
126 UBYTE d_dieId[CL_DES_KEY_SIZE]; /* deciphered Die ID */
127 SHORT ret;
128
129 TRACE_FUNCTION("ffs_get_imeisv()");
130
131 if(inBufSize < CL_IMEI_SIZE){
132 TRACE_ERROR("CL IMEI ERROR: buffer size for IMEI to short!");
133 return CL_IMEI_ERROR;
134 }
135
136 /*
137 * Read ISDID(enciphered IMEISV+DieID) from FFS.
138 */
139 if((ret = ffs_file_read(CL_IMEI_FFS_PATH, isdid_buf, CL_IMEI_ISDID_SIZE)) >= EFFS_OK)
140 {
141 /*
142 * Read Die ID for using as DES key
143 */
144 get_dieID(CL_DES_KEY_SIZE, r_dieId);
145 /*
146 * Call DES algorithm routine
147 */
148 /* decipher first 8 BYTEs */
149 cl_des(&isdid_buf[0], r_dieId, outBufPtr, CL_DES_DECRYPTION);
150 /* decipher the rest 8 BYTEs */
151 cl_des(&isdid_buf[CL_DES_BUFFER_SIZE], r_dieId, d_dieId, CL_DES_DECRYPTION);
152 if(!memcmp(d_dieId, r_dieId, CL_DES_KEY_SIZE))
153 {
154 /* Die ID is valid */
155 ret = CL_IMEI_OK;
156 } else {/* Die ID is corrupted */
157 char pr_buf[126];
158 TRACE_ERROR("CL IMEI ERROR: Die ID is corrupted");
159 sprintf(pr_buf,"Read DieID: %02x %02x %02x %02x %02x %02x %02x %02x",
160 r_dieId[0], r_dieId[1], r_dieId[2], r_dieId[3],
161 r_dieId[4], r_dieId[5], r_dieId[6], r_dieId[7]);
162 TRACE_ERROR(pr_buf);
163 sprintf(pr_buf,"Deciphered DieID: %02x %02x %02x %02x %02x %02x %02x %02x",
164 d_dieId[0], d_dieId[1], d_dieId[2], d_dieId[3],
165 d_dieId[4], d_dieId[5], d_dieId[6], d_dieId[7]);
166 TRACE_ERROR(pr_buf);
167
168 ret = CL_IMEI_INVALID_DIE_ID;
169 }
170 } else {
171 ret = CL_IMEI_READ_IMEI_FAILED;
172 }
173
174 return ret;
175
176 }/* ffs_get_imeisv() */
177 #endif /* CL_IMEI_CALYPSO_PLATFORM */
178
179
180 #ifdef CL_IMEI_CALYPSO_PLUS_PLATFORM
181 /*
182 +------------------------------------------------------------------------------
183 | Function : securerom_get_imeisv
184 +------------------------------------------------------------------------------
185 | Description : the function reads IMEI from Secure ROM
186 |
187 | Parameters : inBufSize - size of buffer where to store IMEI, min. 8 BYTE
188 | *outBufPtr - pointer to buffer where to store the IMEI
189 | Return : 0 - OK
190 | <0 - ERROR
191 +------------------------------------------------------------------------------
192 */
193 LOCAL BYTE securerom_get_imeisv (USHORT inBufSize, UBYTE *outBufPtr)
194 {
195 BYTE ret;
196
197 TRACE_FUNCTION("securerom_get_imeisv()");
198
199 if((ret = securerom_drv(inBufSize, outBufPtr)) == CL_IMEI_OK){
200 return CL_IMEI_OK;
201 } else {
202 return CL_IMEI_READ_IMEI_FAILED;
203 }
204 }
205 #endif /* CL_IMEI_CALYPSO_PLUS_PLATFORM */
206
207 /*
208 +------------------------------------------------------------------------------
209 | Function : get_secure_imeisv
210 +------------------------------------------------------------------------------
211 | Description : the function reads IMEI either from FFS or from Secure ROM of
212 | from other locations depended on hardware platform
213 |
214 | Parameters : inBufSize - size of buffer where to store IMEI, min. 8 BYTE
215 | *outBufPtr - pointer to buffer where to store the IMEI
216 | Return : 0 - OK
217 | negative value - ERROR
218 +------------------------------------------------------------------------------
219 */
220 LOCAL BYTE get_secure_imeisv(USHORT inBufSize, UBYTE *outBufPtr)
221 {
222 BYTE ret = 0;
223 UBYTE chipset = CHIPSET;
224 UBYTE board = BOARD;
225
226 TRACE_FUNCTION("get_secure_imeisv()");
227
228 /*
229 * SW is running on Calypso platform (D-Sample)
230 */
231 #ifdef CL_IMEI_CALYPSO_PLATFORM
232 /*
233 * Read IMEI from FFS inclusive deciphering with DES.
234 */
235 if((ret = ffs_get_imeisv (inBufSize, outBufPtr)) == CL_IMEI_OK)
236 {
237 /* store IMEI */
238 memcpy(stored_imei, outBufPtr, CL_IMEI_SIZE);
239 imei_flag = 1;
240 return CL_IMEI_OK;
241 }
242 #else /* CL_IMEI_CALYPSO_PLATFORM */
243 /*
244 * SW is running on Calypso plus platform (E-Sample)
245 */
246 #ifdef CL_IMEI_CALYPSO_PLUS_PLATFORM
247 if((ret = securerom_get_imeisv (inBufSize, outBufPtr)) == CL_IMEI_OK)
248 {
249 /* store IMEI */
250 memcpy(stored_imei, outBufPtr, CL_IMEI_SIZE);
251 imei_flag = 1;
252 return CL_IMEI_OK;
253 }
254 #else /* CL_IMEI_CALYPSO_PLUS_PLATFORM */
255 /*
256 * SW is running on an other platform (neither Calypso nor Calypso plus)
257 */
258 #ifdef CL_IMEI_OTHER_PLATFORM
259 {
260 TRACE_EVENT_P2("CL IMEI WARNING: unknown hardware: board=%d, chipset=%d, return default imei",
261 board, chipset);
262 memcpy(outBufPtr, C_DEFAULT_IMEISV_DSAMPLE, CL_IMEI_SIZE);
263 return CL_IMEI_OK;
264 }
265 #endif /* CL_IMEI_OTHER_PLATFORM */
266 #endif /* CL_IMEI_CALYPSO_PLUS_PLATFORM */
267 #endif /* CL_IMEI_CALYPSO_PLATFORM */
268
269 return ret; /* get_secure_imeisv is failed, return error code */
270
271 } /* get_secure_imeisv() */
272
273
274 #endif /* FF_PROTECTED_IMEI */
275
276 /*
277 +------------------------------------------------------------------------------
278 | Function : cl_get_imeisv
279 +------------------------------------------------------------------------------
280 | Description : Common IMEI getter function
281 |
282 | Parameters : imeiBufSize - size of buffer where to store IMEI, min 8 BYTEs
283 | *imeiBufPtr - pointer to buffer where to store the IMEI
284 | imeiType - indicates, if the IMEI should be read from
285 | FFS/Secure ROM (value=CL_IMEI_GET_SECURE_IMEI) or
286 | if the already read and stored IMEI (if available)
287 | should be delivered (value=CL_IMEI_GET_STORED_IMEI)
288 | The second option should be used only by ACI or
289 | BMI to show the IMEISV on mobile's display or
290 | in terminal window, e.g. if user calls *#06#.
291 | For IMEI Control reason (used by ACI), the value
292 | has to be CL_IMEI_CONTROL_IMEI
293 | Return : OK - 0
294 | ERROR - negative values
295 +------------------------------------------------------------------------------
296 */
297 extern BYTE cl_get_imeisv(USHORT imeiBufSize, UBYTE *imeiBufPtr, UBYTE imeiType)
298 {
299 BYTE ret = 0;
300
301 TRACE_FUNCTION("cl_get_imeisv()");
302
303 #ifdef _SIMULATION_
304 memcpy(imeiBufPtr, C_DEFAULT_IMEISV_DSAMPLE, CL_IMEI_SIZE);
305 return CL_IMEI_OK;
306 #else /* _SIMULATION_ */
307
308 #ifdef FF_PROTECTED_IMEI
309 /*
310 * The user has required a stored IMEI. If it has been already read
311 * and stored, so return stored IMEI
312 */
313 if((imeiType == CL_IMEI_GET_STORED_IMEI) && (imei_flag == 1)){
314 memcpy(imeiBufPtr, stored_imei, CL_IMEI_SIZE);
315 return CL_IMEI_OK;
316 }
317 /*
318 * The user has required a secure IMEI. Read IMEI from FFS or from Secure ROM
319 */
320 if((ret = get_secure_imeisv(imeiBufSize, imeiBufPtr)) == CL_IMEI_OK)
321 {
322 return CL_IMEI_OK;
323 } else {
324 TRACE_ERROR("CL IMEI FATAL ERROR: IMEI not available!");
325 /*
326 * Notify the Frame entity about FATAL ERROR, but not in the case,
327 * if ACI is checking IMEI, because ACI will take trouble about it.
328 */
329 if (imeiType != CL_IMEI_CONTROL_IMEI){
330 TRACE_ASSERT(ret == CL_IMEI_OK);
331 }
332 return ret;
333 }
334 /*
335 * The feature flag FF_PROTECTED_IMEI is not enabled.
336 */
337 #else /* FF_PROTECTED_IMEI */
338
339 /*
340 * Return default CALYPSO+ IMEISV value
341 */
342 #ifdef CL_IMEI_CALYPSO_PLUS_PLATFORM
343
344 TRACE_EVENT("CL IMEI INFO: return default IMEI-SV number");
345 memcpy(imeiBufPtr, C_DEFAULT_IMEISV_ESAMPLE, CL_IMEI_SIZE);
346
347 /*
348 * CL_IMEI_CALYPSO_PLATFORM or CL_IMEI_OTHER_PLATFORM is defined.
349 * Try to read the IMEI number from the old ffs:/pcm/IMEI file,
350 * if it failes, return default CALYPSO IMEISV value
351 */
352 #else /* CL_IMEI_CALYPSO_PLUS_PLATFORM */
353 {
354 UBYTE version;
355 USHORT ret;
356 UBYTE buf[SIZE_EF_IMEI];
357
358 ret = pcm_ReadFile ((UBYTE *)EF_IMEI_ID, SIZE_EF_IMEI, buf, &version);
359 if(ret == PCM_OK){
360 TRACE_EVENT("CL IMEI INFO: return IMEI-SV number from ffs:/pcm/IMEI");
361 /*
362 * swap digits
363 */
364 imeiBufPtr[0] = ((buf[0] & 0xf0) >> 4) | ((buf[0] & 0x0f) << 4);
365 imeiBufPtr[1] = ((buf[1] & 0xf0) >> 4) | ((buf[1] & 0x0f) << 4);
366 imeiBufPtr[2] = ((buf[2] & 0xf0) >> 4) | ((buf[2] & 0x0f) << 4);
367 imeiBufPtr[3] = ((buf[3] & 0xf0) >> 4) | ((buf[3] & 0x0f) << 4);
368 imeiBufPtr[4] = ((buf[4] & 0xf0) >> 4) | ((buf[4] & 0x0f) << 4);
369 imeiBufPtr[5] = ((buf[5] & 0xf0) >> 4) | ((buf[5] & 0x0f) << 4);
370 imeiBufPtr[6] = ((buf[6] & 0xf0) >> 4) | ((buf[6] & 0x0f) << 4);
371 imeiBufPtr[7] = ((buf[7] & 0xf0) >> 4) | ((buf[7] & 0x0f) << 4);
372 /* store IMEI */
373 memcpy(stored_imei, imeiBufPtr, CL_IMEI_SIZE);
374 imei_flag = 1;
375
376 }else{
377 TRACE_EVENT("CL IMEI INFO: return default IMEI-SV number");
378 memcpy(imeiBufPtr, C_DEFAULT_IMEISV_DSAMPLE, CL_IMEI_SIZE);
379 }
380 }
381 #endif /* CL_IMEI_CALYPSO_PLUS_PLATFORM */
382
383 return CL_IMEI_OK;
384
385 #endif /* FF_PROTECTED_IMEI */
386 #endif /* _SIMULATION_ */
387 }
388
389
390 #endif /* CL_IMEI_C */