FreeCalypso > hg > leo2moko-debug
comparison g23m/condat/com/src/comlib/sec_drv_prim.c @ 0:509db1a7b7b8
initial import: leo2moko-r1
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Mon, 01 Jun 2015 03:24:05 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:509db1a7b7b8 |
---|---|
1 #include "general.h" | |
2 #include "typedefs.h" | |
3 #include "vsi.h" | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include "sec_drv.h" | |
7 #include "sec_drv_prim.h" | |
8 #include "ffs/ffs.h" | |
9 | |
10 #ifndef NON_ENCRYPTED_MODE | |
11 #include "cl_des.h" | |
12 #include "cl_imei.h" | |
13 #endif | |
14 | |
15 | |
16 /* | |
17 Layout of flash: | |
18 T_SEC_DRV_GLOBAL_CONF | |
19 T_SEC_DRV_KEY (failure count) | |
20 T_SEC_DRV_CONFIGURATION | |
21 { | |
22 T_SEC_DRV_KEY | |
23 T_SEC_DRV_CATEGORY (T_SEC_DRV_CAT_HDR + SEC_DRV_CAT_MAX_SIZE*UINT8) | |
24 }[configuration -> number of categories] | |
25 */ | |
26 | |
27 | |
28 /******************************************* | |
29 Global variables | |
30 *******************************************/ | |
31 | |
32 #ifndef FFS_MODE_ENABLED | |
33 /* SIM lock data area in flash */ | |
34 #pragma DATA_SECTION(d_mepd_data, ".mepd") | |
35 volatile const UINT8 d_mepd_data; // Adress of first byte in 8 kb buffer | |
36 #endif /* FFS_MODE_ENABLED */ | |
37 | |
38 static T_SEC_DRV_GLOBAL_CONF gGlobalConf; | |
39 static INT8 gGlobalConfRead = FALSE; | |
40 INT8 sec_drv_fast_write = FALSE; | |
41 static INT16 gMaxCategories = 0; | |
42 | |
43 #if (defined(RSA_HEAP_IS_INTERNAL) && defined(FIRSTBOOT_RSA_ENABLED)) || (defined(USE_RSAHEAP_FOR_FLASH) && !defined(FFS_MODE_ENABLED)) | |
44 #ifdef USE_RSAHEAP_FOR_FLASH | |
45 char sec_drv_buffer[SEC_FLASH_SIZE]; | |
46 #else /* USE_RSAHEAP_FOR_FLASH */ | |
47 char sec_drv_buffer[RSA_HEAP_SIZE]; | |
48 #endif /* USE_RSAHEAP_FOR_FLASH */ | |
49 #endif | |
50 | |
51 | |
52 /******************************************* | |
53 Trace functionality | |
54 *******************************************/ | |
55 #ifdef SEC_TRACES_ENABLED | |
56 static void _trace(const char *str) | |
57 { | |
58 T_FFS_FD test_file; | |
59 test_file = ffs_open("/testfile.txt", FFS_O_CREATE | FFS_O_WRONLY | FFS_O_APPEND); | |
60 if (test_file > 0) | |
61 { | |
62 ffs_write(test_file, (char *)str, strlen((char *)str)); | |
63 ffs_close(test_file); | |
64 } | |
65 } | |
66 void test_trace(const char *str) | |
67 { | |
68 char *s = (char *)str; | |
69 T_FFS_FD test_file; | |
70 test_file = ffs_open("/testfile.txt", FFS_O_CREATE | FFS_O_WRONLY | FFS_O_APPEND); | |
71 if (test_file > 0) | |
72 { | |
73 ffs_write(test_file, s, strlen(s)); | |
74 s = "\n"; | |
75 ffs_write(test_file, s, strlen(s)); | |
76 ffs_close(test_file); | |
77 } | |
78 } | |
79 void hex_trace(const char *str, const void *in, unsigned long int size) | |
80 { | |
81 char buffer[4]; | |
82 unsigned long int i; | |
83 _trace(str); | |
84 for (i=0; i<size; i++) | |
85 { | |
86 sprintf(buffer, "%02x ", ((volatile unsigned char *)in)[i]); | |
87 _trace(buffer); | |
88 } | |
89 _trace("\n"); | |
90 } | |
91 #endif /* SEC_TRACES_ENABLED */ | |
92 | |
93 | |
94 #ifdef FFS_MODE_ENABLED | |
95 /******************************************* | |
96 FFS functions | |
97 *******************************************/ | |
98 void sec_drv_flash_backup(void) | |
99 { } | |
100 | |
101 | |
102 void sec_drv_remove_backup(void) | |
103 { } | |
104 | |
105 | |
106 void sec_drv_erase_flash(void) | |
107 { } | |
108 | |
109 | |
110 void sec_drv_read_flash(UINT32 offset, void *pDest, UINT32 size) | |
111 { | |
112 T_FFS_FD file; | |
113 file = ffs_open(FFS_MEPD_FILENAME, FFS_O_RDONLY); | |
114 if (file <= 0) | |
115 { | |
116 memset(pDest, 0, size); | |
117 TRACE("sec_drv_read_flash - FAILed opening file!"); | |
118 } | |
119 else | |
120 { | |
121 ffs_seek(file, offset, FFS_SEEK_SET); | |
122 ffs_read(file, pDest, size); | |
123 ffs_close(file); | |
124 } | |
125 } | |
126 | |
127 | |
128 void sec_drv_write_flash(UINT32 offset, const void *pSrc, UINT32 size) | |
129 { | |
130 T_FFS_FD file; | |
131 file = ffs_open(FFS_MEPD_FILENAME, FFS_O_WRONLY ); | |
132 if (!(file <= 0)) | |
133 { | |
134 ffs_seek(file, offset, FFS_SEEK_SET); | |
135 ffs_write(file, (void *)pSrc, size); | |
136 ffs_close(file); | |
137 } | |
138 else | |
139 { | |
140 TRACE("sec_drv_write_flash - FAILed opening file!"); | |
141 } | |
142 } | |
143 | |
144 #else /* FFS_MODE_ENABLED */ | |
145 | |
146 /******************************************* | |
147 Memory functions | |
148 *******************************************/ | |
149 UINT32 gBackupOffset; | |
150 | |
151 void sec_drv_flash_backup(void) | |
152 { | |
153 if (!sec_drv_fast_write) | |
154 { | |
155 TRACE("sec_drv_flash_backup"); | |
156 #ifdef USE_RSAHEAP_FOR_FLASH | |
157 sec_drv_read_flash(0, (void *)SEC_DRV_RSA_HEAP, SEC_FLASH_SIZE); | |
158 #else /* USE_RSAHEAP_FOR_FLASH */ | |
159 #error "QUESTION: what if we don't have the space required?" | |
160 T_FFS_FD file = ffs_open(FLASH_BACKUP_NAME, FFS_O_CREATE | FFS_O_WRONLY | FFS_O_TRUNC); | |
161 ffs_write(file, (void *)SEC_BASE, SEC_FLASH_SIZE); | |
162 ffs_close(file); | |
163 #endif /* USE_RSAHEAP_FOR_FLASH */ | |
164 gBackupOffset = 0; | |
165 } | |
166 } | |
167 | |
168 | |
169 void sec_drv_remove_backup(void) | |
170 { | |
171 if (!sec_drv_fast_write) | |
172 { | |
173 #ifdef USE_RSAHEAP_FOR_FLASH | |
174 copy_to_flash(0, 0, 0, gBackupOffset, SEC_FLASH_SIZE); | |
175 TRACE("sec_drv_remove_backup"); | |
176 memset(SEC_DRV_RSA_HEAP, 0xFF, SEC_FLASH_SIZE); | |
177 #else /* USE_RSAHEAP_FOR_FLASH */ | |
178 #error "QUESTION: what if we don't have the space required?" | |
179 T_FFS_FD file; | |
180 void *buf; | |
181 | |
182 TRACE("sec_drv_remove_backup"); | |
183 buf = M_ALLOC(BACKUP_BUF_SIZE); | |
184 file = ffs_open(FLASH_BACKUP_NAME, FFS_O_RDONLY); | |
185 copy_to_flash(file, buf, BACKUP_BUF_SIZE, gBackupOffset, SEC_FLASH_SIZE); | |
186 ffs_close(file); | |
187 M_FREE(buf); | |
188 | |
189 ffs_remove(FLASH_BACKUP_NAME); | |
190 #endif /* USE_RSAHEAP_FOR_FLASH */ | |
191 } | |
192 } | |
193 | |
194 | |
195 void sec_drv_erase_flash(void) | |
196 { | |
197 if (!sec_drv_fast_write) | |
198 { | |
199 TRACE("sec_drv_erase_flash"); | |
200 sec_drv_amd_erase(); | |
201 } | |
202 } | |
203 | |
204 | |
205 void sec_drv_read_flash(UINT32 offset, void *pDest, UINT32 size) | |
206 { | |
207 void *pSrc = (void *)((UINT32)SEC_BASE + offset); | |
208 assert(pSrc != 0L); | |
209 assert(pDest != 0L); | |
210 assert(size > 0L); | |
211 assert(offset >= 0L && (offset+size) <= SEC_FLASH_SIZE); | |
212 | |
213 memcpy(pDest, pSrc, size); | |
214 } | |
215 | |
216 | |
217 static void copy_to_flash(T_FFS_FD file, void *pBuf, UINT32 bufSize, UINT32 offset, UINT32 maxOffset) | |
218 { | |
219 UINT32 size = maxOffset-offset; | |
220 TRACE("copy_to_flash"); | |
221 #ifdef USE_RSAHEAP_FOR_FLASH | |
222 sec_drv_amd_write((void *)((UINT32)SEC_BASE+offset), (const void *)((UINT32)SEC_DRV_RSA_HEAP+offset), size); | |
223 #else /* USE_RSAHEAP_FOR_FLASH */ | |
224 ffs_seek(file, offset, FFS_SEEK_SET); | |
225 while(offset < maxOffset) | |
226 { | |
227 size = (maxOffset-offset > bufSize)? bufSize : maxOffset-offset; | |
228 ffs_read(file, pBuf, size); | |
229 sec_drv_amd_write((void *)(SEC_BASE + offset), pBuf, size); | |
230 offset += size; | |
231 } | |
232 #endif /* USE_RSAHEAP_FOR_FLASH */ | |
233 } | |
234 | |
235 | |
236 void sec_drv_write_flash(UINT32 offset, const void *pSrc, UINT32 size) | |
237 { | |
238 assert(pSrc != 0L); | |
239 assert(size > 0L); | |
240 assert(offset >= 0L && (offset+size) <= SEC_FLASH_SIZE); | |
241 | |
242 if (sec_drv_fast_write) | |
243 { | |
244 TRACE("sec_drv_write_flash"); | |
245 sec_drv_amd_write((void *)((UINT32)SEC_BASE+offset), pSrc, size); | |
246 } | |
247 else | |
248 { | |
249 #ifdef USE_RSAHEAP_FOR_FLASH | |
250 copy_to_flash(0, 0, 0, gBackupOffset, offset); | |
251 HEXTRACE("sec_drv_write_flash - offset: ", &offset, sizeof(UINT32)); | |
252 sec_drv_amd_write((void *)((UINT32)SEC_BASE+offset), pSrc, size); | |
253 gBackupOffset = offset+size; | |
254 #else /* USE_RSAHEAP_FOR_FLASH */ | |
255 T_FFS_FD file; | |
256 void *buf; | |
257 TRACE("Using FFS"); | |
258 buf = M_ALLOC(BACKUP_BUF_SIZE); | |
259 file = ffs_open(FLASH_BACKUP_NAME, FFS_O_RDONLY); | |
260 copy_to_flash(file, buf, BACKUP_BUF_SIZE, gBackupOffset, offset); | |
261 sec_drv_amd_write((void *)((UINT32)SEC_BASE + offset), pSrc, size); | |
262 gBackupOffset += size; | |
263 ffs_close(file); | |
264 M_FREE(buf); | |
265 #endif /* USE_RSAHEAP_FOR_FLASH */ | |
266 } | |
267 } | |
268 | |
269 #endif /* FFS_MODE_ENABLED */ | |
270 | |
271 | |
272 /******************************************* | |
273 DES de-/encryption | |
274 *******************************************/ | |
275 | |
276 /** | |
277 * Returns the DES key from the DSP | |
278 * | |
279 * @return The DES key for use by de-encryption | |
280 */ | |
281 static const UINT8 *GetDESKey(void) | |
282 { | |
283 #ifdef DSP_DES_KEY_ENABLED | |
284 #error "Not yet implemented" | |
285 #else /* DSP_DES_KEY_ENABLED */ | |
286 static const UINT8 desKey[8] = {0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78}; | |
287 #ifndef NON_ENCRYPTED_MODE | |
288 #ifdef CL_IMEI_CALYPSO_PLATFORM | |
289 #ifdef FF_PROTECTED_IMEI | |
290 volatile USHORT *reg_p = (USHORT *) CL_IMEI_DIE_ID_REG; | |
291 USHORT *outBuf16 = (USHORT*)&desKey[0]; | |
292 INT8 i; | |
293 for (i = 0; i < CL_IMEI_DIE_ID_SIZE; i++) | |
294 { | |
295 /* Die ID is 4 BYTE long, extract it to 8 BYTE. */ | |
296 outBuf16[i] = (USHORT)(*(UINT8*)(reg_p)++); | |
297 } | |
298 #endif | |
299 #endif | |
300 #endif | |
301 return desKey; | |
302 #endif /* DSP_DES_KEY_ENABLED */ | |
303 } | |
304 | |
305 | |
306 /** | |
307 * Read a crypted block of the secure_area. | |
308 * | |
309 * @param offset Offset into the secure area from which to read the block. | |
310 * @param pDest Pointer to the receiving buffer. | |
311 * @param size Size of the block to read. Minimum 1. | |
312 */ | |
313 static void read_crypted_flash(UINT32 offset, void *pDest, UINT32 size) | |
314 { | |
315 #ifndef NON_ENCRYPTED_MODE | |
316 UINT8 inbuffer[CRYPT_BLOCK_SIZE]; | |
317 UINT8 outbuffer[CRYPT_BLOCK_SIZE]; | |
318 #endif | |
319 UINT32 blocksize; | |
320 assert(offset != 0L); | |
321 assert(pDest != 0L); | |
322 assert(size > 0L); | |
323 assert(offset >= 0L && (offset+size) <= SEC_FLASH_SIZE); | |
324 do | |
325 { | |
326 blocksize = (size>CRYPT_BLOCK_SIZE)? CRYPT_BLOCK_SIZE : size; | |
327 #ifdef NON_ENCRYPTED_MODE | |
328 sec_drv_read_flash(offset, pDest, blocksize); | |
329 #else | |
330 sec_drv_read_flash(offset, inbuffer, CRYPT_BLOCK_SIZE); | |
331 cl_des((UBYTE *)inbuffer, (UBYTE *)GetDESKey(), (UBYTE *)outbuffer, (UBYTE)CL_DES_DECRYPTION); | |
332 memcpy(pDest, outbuffer, blocksize); | |
333 #endif | |
334 offset += blocksize; | |
335 pDest = (void *)((UINT32)pDest + blocksize); | |
336 size -= blocksize; | |
337 } while(size); | |
338 } | |
339 | |
340 | |
341 /** | |
342 * Write a crypted block of the secure_area. | |
343 * | |
344 * @param offset Offset into the secure area from which to write the block. | |
345 * @param pDest Pointer to the input buffer. | |
346 * @param size Size of the block to write. Minimum 1. | |
347 */ | |
348 static void write_crypted_flash(UINT32 offset, const void *pSrc, UINT32 size) | |
349 { | |
350 UINT8 inbuffer[CRYPT_BLOCK_SIZE]; | |
351 UINT8 outbuffer[CRYPT_BLOCK_SIZE]; | |
352 UINT32 blocksize; | |
353 assert(offset != 0L); | |
354 assert(pSrc != 0L); | |
355 assert(size > 0L); | |
356 assert(offset >= 0L && (offset+size) <= SEC_FLASH_SIZE); | |
357 | |
358 sec_drv_flash_backup(); | |
359 sec_drv_erase_flash(); | |
360 do | |
361 { | |
362 blocksize = (size>CRYPT_BLOCK_SIZE)? CRYPT_BLOCK_SIZE : size; | |
363 memset(inbuffer, 0, CRYPT_BLOCK_SIZE); | |
364 memcpy(inbuffer, pSrc, blocksize); | |
365 #ifdef NON_ENCRYPTED_MODE | |
366 memcpy(outbuffer, inbuffer, CRYPT_BLOCK_SIZE); | |
367 #else | |
368 cl_des((UBYTE *)inbuffer, (UBYTE *)GetDESKey(), (UBYTE *)outbuffer, (UBYTE)CL_DES_ENCRYPTION); | |
369 #endif | |
370 /* write crypted datablock to flash */ | |
371 sec_drv_write_flash(offset, outbuffer, CRYPT_BLOCK_SIZE); | |
372 offset += blocksize; | |
373 pSrc = (void *)((UINT32)pSrc + blocksize); | |
374 size -= blocksize; | |
375 } while(size); | |
376 sec_drv_remove_backup(); | |
377 } | |
378 | |
379 | |
380 /******************************************* | |
381 Offset calculations | |
382 *******************************************/ | |
383 | |
384 static UINT32 offset_global_conf(void) | |
385 { | |
386 return 0; | |
387 } | |
388 | |
389 | |
390 static UINT32 offset_fc_key(void) | |
391 { | |
392 return ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_GLOBAL_CONF)); | |
393 } | |
394 | |
395 | |
396 static UINT32 offset_configuration(void) | |
397 { | |
398 return ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_GLOBAL_CONF)) + | |
399 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)); | |
400 } | |
401 | |
402 | |
403 static UINT32 offset_category_key(int rec_num) | |
404 { | |
405 return ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_GLOBAL_CONF)) + | |
406 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)) + | |
407 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_CONFIGURATION)) + | |
408 rec_num * (ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)) + | |
409 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_CAT_HDR)) + | |
410 ALIGN_CRYPT_BLOCK(SEC_DRV_CAT_MAX_SIZE)); | |
411 } | |
412 | |
413 | |
414 static UINT32 offset_category_header(int rec_num) | |
415 { | |
416 return ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_GLOBAL_CONF)) + | |
417 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)) + | |
418 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_CONFIGURATION)) + | |
419 rec_num * (ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)) + | |
420 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_CAT_HDR)) + | |
421 ALIGN_CRYPT_BLOCK(SEC_DRV_CAT_MAX_SIZE)) + | |
422 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)); | |
423 } | |
424 | |
425 | |
426 static UINT32 offset_category_body(int rec_num) | |
427 { | |
428 return ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_GLOBAL_CONF)) + | |
429 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)) + | |
430 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_CONFIGURATION)) + | |
431 rec_num * (ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)) + | |
432 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_CAT_HDR)) + | |
433 ALIGN_CRYPT_BLOCK(SEC_DRV_CAT_MAX_SIZE)) + | |
434 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_KEY)) + | |
435 ALIGN_CRYPT_BLOCK(sizeof(T_SEC_DRV_CAT_HDR)); | |
436 } | |
437 | |
438 | |
439 /******************************************* | |
440 Helper functions | |
441 *******************************************/ | |
442 | |
443 /** | |
444 * Check whether or not the secure area has been initialized. | |
445 * | |
446 * @return TRUE if the secure area has been initialized. | |
447 */ | |
448 static int check_firstboot_pattern_ok(void) | |
449 { | |
450 sec_prim_get_global_conf(0); | |
451 if (gGlobalConf.firstboot_pattern != SEC_PATTERN_INITIALIZED) | |
452 { | |
453 TRACE("check_firstboot_pattern_ok - FAIL"); | |
454 return FALSE; | |
455 } | |
456 return TRUE; | |
457 } | |
458 | |
459 | |
460 /** | |
461 * Check whether or not the given number of categories is within bounds. | |
462 * | |
463 * @return TRUE if the given number of categories is within bounds. | |
464 */ | |
465 static int check_number_categories(int catNum) | |
466 { | |
467 BOOL result; | |
468 if (!gMaxCategories) | |
469 { | |
470 T_SEC_DRV_CONFIGURATION conf; | |
471 if (sec_prim_get_configuration(&conf)) | |
472 { | |
473 gMaxCategories = conf.NumCategories; | |
474 HEXTRACE("check_number_categories - max: ", &gMaxCategories, sizeof(int)); | |
475 } | |
476 } | |
477 return (catNum >= 0) && (catNum < gMaxCategories); | |
478 } | |
479 | |
480 | |
481 /******************************************* | |
482 PRIMITIVES | |
483 *******************************************/ | |
484 | |
485 /** | |
486 * Read the global configuration for the secure flash area used to store | |
487 * SIM lock data. | |
488 * | |
489 * @param pGlobalConf Pointer where to store the global configuration. | |
490 * @return TRUE if the global configuration could be read. | |
491 */ | |
492 BOOL sec_prim_get_global_conf(T_SEC_DRV_GLOBAL_CONF *pGlobalConf) | |
493 { | |
494 if (!gGlobalConfRead) | |
495 { | |
496 sec_drv_read_flash(offset_global_conf(), &gGlobalConf, sizeof(T_SEC_DRV_GLOBAL_CONF)); | |
497 HEXTRACE("sec_prim_get_global_conf: ", &gGlobalConf, sizeof(T_SEC_DRV_GLOBAL_CONF)); | |
498 gGlobalConfRead = TRUE; | |
499 } | |
500 if (pGlobalConf != 0L) | |
501 { | |
502 memcpy(pGlobalConf, &gGlobalConf, sizeof(T_SEC_DRV_GLOBAL_CONF)); | |
503 return TRUE; | |
504 } | |
505 return FALSE; | |
506 } | |
507 | |
508 /** | |
509 * Store the global configuration for the secure flash area used to store | |
510 * SIM lock data. | |
511 * | |
512 * @param pGlobalConf Pointer to a global configuration. | |
513 * @return TRUE if the global configuration could be written. | |
514 */ | |
515 BOOL sec_prim_set_global_conf(const T_SEC_DRV_GLOBAL_CONF *pGlobalConf) | |
516 { | |
517 /* Calc total size needed for this configuration */ | |
518 if (pGlobalConf == 0L) return FALSE; | |
519 | |
520 HEXTRACE("sec_prim_set_global_conf: ", pGlobalConf, sizeof(T_SEC_DRV_GLOBAL_CONF)); | |
521 sec_drv_flash_backup(); | |
522 sec_drv_erase_flash(); | |
523 sec_drv_write_flash(offset_global_conf(), pGlobalConf, sizeof(T_SEC_DRV_GLOBAL_CONF)); | |
524 sec_drv_remove_backup(); | |
525 gGlobalConfRead = FALSE; | |
526 return TRUE; | |
527 } | |
528 | |
529 | |
530 /** | |
531 * Read the configuration for the MEPD structure. | |
532 * | |
533 * @param pConf Pointer to buffer where the result will be placed | |
534 * @Return 0 if flash area hasn't been configured. | |
535 */ | |
536 BOOL sec_prim_get_configuration(T_SEC_DRV_CONFIGURATION *pConf) | |
537 { | |
538 if (! check_firstboot_pattern_ok()) return FALSE; | |
539 | |
540 read_crypted_flash(offset_configuration(), pConf, sizeof(T_SEC_DRV_CONFIGURATION)); | |
541 HEXTRACE("sec_prim_get_configuration: ", pConf, sizeof(T_SEC_DRV_CONFIGURATION)); | |
542 return TRUE; | |
543 } | |
544 | |
545 | |
546 /** | |
547 * Store the configuration for the MEPD structure. | |
548 * | |
549 * @param pConf Pointer to configuration to set | |
550 * @Return TRUE if ok. | |
551 */ | |
552 BOOL sec_prim_set_configuration(const T_SEC_DRV_CONFIGURATION *pConf) | |
553 { | |
554 if (! check_firstboot_pattern_ok()) return FALSE; | |
555 if (pConf == 0L) return FALSE; | |
556 | |
557 HEXTRACE("sec_prim_set_configuration: ", pConf, sizeof(T_SEC_DRV_CONFIGURATION)); | |
558 write_crypted_flash(offset_configuration(), pConf, sizeof(T_SEC_DRV_CONFIGURATION)); | |
559 gMaxCategories = 0; | |
560 return TRUE; | |
561 } | |
562 | |
563 | |
564 /** | |
565 * Read the key for either the failure counter (category -1) or a | |
566 * lock-category (category 0..n). | |
567 * | |
568 * @param rec_num Category number to operate on | |
569 * @param pKey Pointer to keybuffer | |
570 * @Return TRUE if ok. | |
571 */ | |
572 BOOL sec_prim_get_key(int rec_num, T_SEC_DRV_KEY *pKey) | |
573 { | |
574 if (pKey!=0L) | |
575 { | |
576 if ((rec_num == -1) && (check_firstboot_pattern_ok())) | |
577 { | |
578 /* failure counter key */ | |
579 read_crypted_flash(offset_fc_key(), pKey, sizeof(T_SEC_DRV_KEY)); | |
580 HEXTRACE("sec_prim_get_key: ", pKey, sizeof(T_SEC_DRV_KEY)); | |
581 return TRUE; | |
582 } | |
583 else if (check_number_categories(rec_num)) | |
584 { | |
585 /* category key */ | |
586 read_crypted_flash(offset_category_key(rec_num), pKey, sizeof(T_SEC_DRV_KEY)); | |
587 HEXTRACE("sec_prim_get_key: ", &rec_num, sizeof(int)); | |
588 HEXTRACE("sec_prim_get_key: ", pKey, sizeof(T_SEC_DRV_KEY)); | |
589 return TRUE; | |
590 } | |
591 } | |
592 return FALSE; | |
593 } | |
594 | |
595 | |
596 /** | |
597 * Store the key for either the failure counter (category -1) or a | |
598 * lock-category (category 0..n). | |
599 * | |
600 * @param rec_num Category number to operate on | |
601 * @param pKey Pointer to keybuffer | |
602 * @Return TRUE if ok. | |
603 */ | |
604 BOOL sec_prim_set_key(int rec_num, const T_SEC_DRV_KEY *pKey) | |
605 { | |
606 if (pKey!=0L) | |
607 { | |
608 if ((rec_num == -1) && (check_firstboot_pattern_ok())) | |
609 { | |
610 /* failure counter key */ | |
611 HEXTRACE("sec_prim_set_key - FC: ", pKey, sizeof(T_SEC_DRV_KEY)); | |
612 write_crypted_flash(offset_fc_key(), pKey, sizeof(T_SEC_DRV_KEY)); | |
613 return TRUE; | |
614 } | |
615 else if (check_number_categories(rec_num)) | |
616 { | |
617 /* category key */ | |
618 HEXTRACE("sec_prim_set_key: ", &rec_num, sizeof(int)); | |
619 HEXTRACE("sec_prim_set_key: ", pKey, sizeof(T_SEC_DRV_KEY)); | |
620 write_crypted_flash(offset_category_key(rec_num), pKey, sizeof(T_SEC_DRV_KEY)); | |
621 return TRUE; | |
622 } | |
623 } | |
624 return FALSE; | |
625 } | |
626 | |
627 | |
628 /** | |
629 * Read a lock-category header. | |
630 * | |
631 * @param rec_num Category number to operate on | |
632 * @param pHdr Where to place the read category header? | |
633 * @Return TRUE if ok. | |
634 */ | |
635 BOOL sec_prim_get_cat_header(int rec_num, T_SEC_DRV_CAT_HDR *pHdr) | |
636 { | |
637 if (!check_number_categories(rec_num)) return FALSE; | |
638 if (pHdr == 0L) return FALSE; | |
639 | |
640 read_crypted_flash(offset_category_header(rec_num), pHdr, sizeof(T_SEC_DRV_CAT_HDR)); | |
641 HEXTRACE("sec_prim_get_cat_header: ", &rec_num, sizeof(int)); | |
642 HEXTRACE("sec_prim_get_cat_header: ", pHdr, sizeof(T_SEC_DRV_CAT_HDR)); | |
643 return TRUE; | |
644 } | |
645 | |
646 | |
647 /** | |
648 * Store a lock-category header. | |
649 * | |
650 * @param rec_num Category number to operate on | |
651 * @param pHdr Pointer to the category header | |
652 * @Return TRUE if ok. | |
653 */ | |
654 BOOL sec_prim_set_cat_header(int rec_num, const T_SEC_DRV_CAT_HDR *pHdr) | |
655 { | |
656 if (!check_number_categories(rec_num)) return FALSE; | |
657 if (pHdr == 0L) return FALSE; | |
658 | |
659 HEXTRACE("sec_prim_set_cat_header: ", &rec_num, sizeof(int)); | |
660 HEXTRACE("sec_prim_set_cat_header: ", pHdr, sizeof(T_SEC_DRV_CAT_HDR)); | |
661 write_crypted_flash(offset_category_header(rec_num), pHdr, sizeof(T_SEC_DRV_CAT_HDR)); | |
662 return TRUE; | |
663 } | |
664 | |
665 | |
666 /** | |
667 * Read a lock-category body (client data). | |
668 * | |
669 * @param rec_num Category number to operate on | |
670 * @param pBody Pointer to buffer where the result will be placed | |
671 * @param size Size of the buffer | |
672 * @Return TRUE if ok. | |
673 */ | |
674 BOOL sec_prim_get_cat_body(int rec_num, void *pBody, UINT16 size) | |
675 { | |
676 if (!check_number_categories(rec_num)) return FALSE; | |
677 if (pBody == 0L) return FALSE; | |
678 if (size==0 || size>SEC_DRV_CAT_MAX_SIZE) return FALSE; | |
679 | |
680 read_crypted_flash(offset_category_body(rec_num), pBody, size); | |
681 HEXTRACE("sec_prim_get_cat_body: ", &rec_num, sizeof(int)); | |
682 HEXTRACE("sec_prim_get_cat_body: ", pBody, size); | |
683 return TRUE; | |
684 } | |
685 | |
686 | |
687 /** | |
688 * Store a lock-category body (client data). | |
689 * | |
690 * @param rec_num Category number to operate on | |
691 * @param pBody Pointer to data-buffer | |
692 * @param size Size of the buffer | |
693 * @Return TRUE if ok. | |
694 */ | |
695 BOOL sec_prim_set_cat_body(int rec_num, const void *pBody, UINT16 size) | |
696 { | |
697 T_SEC_DRV_CAT_HDR hdr; | |
698 | |
699 if (!check_number_categories(rec_num)) return FALSE; | |
700 if (pBody == 0L) return FALSE; | |
701 | |
702 sec_prim_get_cat_header(rec_num, &hdr); | |
703 if (hdr.DataLen != size) | |
704 { | |
705 hdr.DataLen = size; | |
706 sec_prim_set_cat_header(rec_num, &hdr); | |
707 } | |
708 if (size) | |
709 { | |
710 HEXTRACE("sec_prim_set_cat_body: ", &rec_num, sizeof(int)); | |
711 HEXTRACE("sec_prim_set_cat_body: ", pBody, size); | |
712 write_crypted_flash(offset_category_body(rec_num), pBody, size); | |
713 } | |
714 return TRUE; | |
715 } |