blob: f73326054126c6b9ae693b63e6b309fca19157cb [file] [log] [blame]
yu.dongc33b3072024-08-21 23:14:49 -07001/*****************************************************************************
2* Copyright Statement:
3* --------------------
4* This software is protected by Copyright and the information contained
5* herein is confidential. The software may not be copied and the information
6* contained herein may not be used or disclosed except with the written
7* permission of MediaTek Inc. (C) 2005
8*
9* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
10* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
11* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
12* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
13* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
15* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
16* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
17* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
18* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
19* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
20* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
21*
22* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
23* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
24* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
25* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
26* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
27*
28* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
29* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
30* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
31* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
32* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
33*
34*****************************************************************************/
35/*
36 * Include
37 */
38#include <string.h>
39
40#include "kal_general_types.h"
41#include "kal_internal_api.h"
42#include "kal_public_defs.h"
43#include "kal_public_api.h"
44#include "kal_trace.h"
45
46#include "syscomp_config.h"
47#include "task_config.h"
48#include "sysconf_statistics.h" /* stack_statistics_struct */
49#include "custom_em.h"
50 /* Factory mode, should not send ADC calibration data to BMT */
51#ifdef __MULTI_BOOT__
52#include "multiboot_config.h"
53#include "intrCtrl.h" /* INT_BootMode */
54#endif /* __MULTI_BOOT__ */
55
56#ifdef __CCCIFS_SUPPORT__
57#include "ccci.h"
58#endif
59
60#if defined(__MTK_TARGET__)
61#include "SST_secure.h"
62#endif
63
64#include "fs_type.h" /* FS_HANDLE */
65#include "fs_func.h" /* FS_Delete */
66#include "fs_errcode.h" /* FS_NO_ERROR */
67#include "ex_item.h" /* EX_LOG_T */
68
69#include "tst_msgid.h"
70#include "sbp_public_utility.h"
71#if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
72#include "dcl_gpt.h"
73#endif
74
75#include "nvram_cache_interface.h"
76#ifdef __NV_CHKSUM_ENHANCE__
77#include "nvram_chksum_algorithm.h"
78#endif
79
80
81#if defined(__HIF_CCCI_SUPPORT__)
82#include "ccci_if.h"
83#endif
84#include "us_timer.h"
85#include "ex_public.h"
86
87#if defined(__MTK_TARGET__)
88#include "ostd_public.h"
89#endif
90
91/*******************************************************
92 * External Function
93 *******************************************************/
94extern kal_int32 nvram_recover_data_item(nvram_ltable_entry_struct *ldi);
95/*extern nvram_errno_enum nvram_read_data_item_multiple(
96 nvram_ltable_entry_struct *ldi,
97 kal_uint32 rec_index,
98 kal_uint16 rec_amount,
99 kal_uint8 *buffer,
100 kal_uint32 buffer_size);
101*/
102
103extern nvram_drv_status_enum nvram_drv_fat_write_multRec(nvram_ltable_entry_struct *ldi,
104 kal_char *nvramname,
105 nvram_folder_enum nvram_folder,
106 kal_uint32 file_offset,
107 kal_uint16 rec_index,
108 kal_uint16 rec_amount,
109 kal_uint32 rec_size,
110 const kal_uint8 *buffer,
111 kal_bool initialize);
112
113extern kal_int32 nvram_get_defval_chksum_index(nvram_lid_enum LID);
114extern nvram_folder_enum nvram_query_folder_index_ex(nvram_category_enum category, kal_bool first_copy);
115extern void nvram_util_make_lid_filename(nvram_ltable_entry_struct *ldi, NVRAM_FILE_NAME nvramname, kal_bool first_copy);
116kal_uint8 const * nvram_get_lid_default_value_to_write(
117 nvram_ltable_entry_struct *ldi,
118 kal_uint16 rec_index,
119 kal_uint8 *buffer,
120 kal_uint32 buffer_size);
121
122extern module_type stack_get_active_module_id( void );
123
124/*******************************************************
125 * Define
126 *******************************************************/
127
128
129/*******************************************************
130 * Typedef
131 *******************************************************/
132
133
134/*******************************************************
135 * Global Variable
136 *******************************************************/
137kal_bool g_nvram_cache_ready = KAL_FALSE;
138kal_bool g_nvram_cache_SHM_support = KAL_FALSE;
139
140
141kal_mutexid g_nvram_cache_mutex = NULL;
142
143static kal_int32 nvram_cache_last_err;
144static kal_uint32 nvram_cache_last_line;
145extern kal_bool bResetNvramData;
146
147kal_uint8* g_nvcache_base_address = NULL;
148kal_uint32 g_nvcache_memory_size = 0;
149
150#if ((!defined(__MTK_TARGET__)) && (!defined(__UE_SIMULATOR__)))
151ltable_type assgn_ltable = {0};
152nvram_ltable_entry_struct *assgn_logical_data_item_table = NULL;
153extern nvram_ltable_entry_struct _nvram_ltable_start;
154extern nvram_ltable_entry_struct the_nvram_ltable_end;
155#endif
156
157extern nvram_ee_info_type* nvram_ee_info;
158extern kal_char nvram_trace_dump_temp_buffer[];
159extern kal_char nvram_trace_dump_buffer[];
160extern kal_mutexid g_nvram_dump_trace_mutex;
161extern kal_wchar nvram_trace_filename[];
162extern FS_HANDLE nvram_trace_file_hdl;
163extern kal_uint32 nvram_trace_dump_buffer_offset;
164
165/*******************************************************
166 * Local Function
167 *******************************************************/
168
169
170/*******************************************************
171 * Local Variable
172 *******************************************************/
173
174/*****************************************************************************
175 * FUNCTION
176 * get_lid_cache_index_item
177 * DESCRIPTION
178 * get cache LID from cache table
179 * PARAMETERS
180 * ldi [IN]
181 * cache_ldi [OUT]
182 * RETURNS
183 * success or fail
184 *****************************************************************************/
185kal_bool get_lid_cache_index_item(nvram_lid_enum LID, nvram_lid_cache_table_struct** cache_ldi)
186{
187 /*----------------------------------------------------------------*/
188 /* Local Variables */
189 /*----------------------------------------------------------------*/
190 kal_bool ret_val = KAL_FALSE;
191 kal_uint32 low = 0;
192 kal_uint32 mid;
193 kal_uint32 high = cache_info_header.cache_lid_num -1;
194 while (low<=high) {
195
196 mid = low + ((high - low)/2);
197 if(cache_info_table[mid].LID < LID) {
198 low = mid +1;
199 }else if(cache_info_table[mid].LID > LID) {
200 high = mid -1;
201 }else {
202 if (cache_ldi) {
203 *cache_ldi = &cache_info_table[mid];
204 }
205 ret_val = KAL_TRUE;
206 break;
207 }
208 }
209
210 return ret_val;
211}
212
213/*****************************************************************************
214 * FUNCTION
215 * get_lid_cache_base_address
216 * DESCRIPTION
217 * get LID cache region base address
218 * PARAMETERS
219 * ldi [IN]
220 * RETURNS
221 * address
222 *****************************************************************************/
223nvram_errno_enum get_lid_cache_base_address(nvram_ltable_entry_struct* ldi, kal_uint32* cache_offset)
224{
225 nvram_lid_cache_table_struct *cache_ldi = NULL;
226 kal_bool result = KAL_FALSE;
227
228 if (!(result = get_lid_cache_index_item(ldi->LID , &cache_ldi))) {
229 kal_prompt_trace(MOD_NVRAM, "[%s]search lid failed from cache table index: 0x%x\n\r", __FUNCTION__,ldi->LID);
230 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
231 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
232
233 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error][%s]search failed from cache table\r\n", __FUNCTION__);
234 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"LID 0x%04X category:0x%08X, attr:0x%08X\r\n",ldi->LID,ldi->category, ldi->attr);
235 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
236 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_INVALID_LID_1, ldi->LID);
237 return NVRAM_IO_ERRNO_INVALID_LID;
238 }
239
240 *cache_offset = (kal_uint32)(g_nvcache_base_address + ((cache_info_header.cache_table_offset) + cache_ldi->cache_offset));
241 return NVRAM_IO_ERRNO_OK;
242
243}
244
245/*****************************************************************************
246 * FUNCTION
247 * get_lid_record_cache_offset
248 * DESCRIPTION
249 * get LID record cache region address
250 * PARAMETERS
251 * ldi [IN]
252 * rec_index [IN]
253 * section_size [IN]
254 * RETURNS
255 * record cache address
256 *****************************************************************************/
257nvram_errno_enum get_lid_record_cache_offset(nvram_ltable_entry_struct* ldi, kal_uint16 rec_index, kal_uint32 section_size, kal_uint32* cache_offset)
258{
259 nvram_lid_cache_table_struct *cache_ldi = NULL;
260 kal_bool result = KAL_FALSE;
261
262 if (!(result = get_lid_cache_index_item(ldi->LID , &cache_ldi))) {
263 kal_prompt_trace(MOD_NVRAM, "[%s]search lid failed from cache table index: 0x%x\n\r", __FUNCTION__, ldi->LID);
264 kal_prompt_trace(MOD_NVRAM, "category:0x%x,attr:0x%x\n\r", ldi->category, ldi->attr);
265 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s,fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
266
267 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error][%s]search lid failed from cache table\r\n", __FUNCTION__);
268 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"LID 0x%04X,category:0x%08X,attr:0x%08X\r\n",ldi->LID, ldi->category, ldi->attr);
269 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s,fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
270 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_INVALID_LID_2, ldi->LID);
271 return NVRAM_IO_ERRNO_INVALID_LID;
272 }
273
274 if (rec_index == 0) {
275 *cache_offset = (kal_uint32)(g_nvcache_base_address + ((cache_info_header.cache_table_offset) + cache_ldi->cache_offset));
276 return NVRAM_IO_ERRNO_OK;
277 }else{
278 *cache_offset = (kal_uint32)(g_nvcache_base_address + (((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + NVRAM_LDI_HEADER_SIZE) + (rec_index-1) * section_size));
279 return NVRAM_IO_ERRNO_OK;
280 }
281}
282
283/*****************************************************************************
284 * FUNCTION
285 * nvram_write_data_to_cache
286 * DESCRIPTION
287 * write data to cache
288 * PARAMETERS
289 * ldi [IN]
290 * src_buffer [IN]
291 * size [IN]
292 * cache_offset [IN]
293 * RETURNS
294 * success or fail
295 *****************************************************************************/
296nvram_errno_enum nvram_write_data_to_cache(nvram_ltable_entry_struct* ldi, void* src_buffer, kal_uint32 size, kal_uint32 cache_offset)
297{
298 nvram_lid_cache_table_struct *cache_ldi = NULL;
299 kal_uint8* dest_buffer = NULL;
300 kal_bool result = KAL_FALSE;
301 kal_uint32 cache_temp_addr = 0;
302
303 if (!(result = get_lid_cache_index_item(ldi->LID , &cache_ldi))) {
304 kal_prompt_trace(MOD_NVRAM, "[%s]search lid failed from cache table index: 0x%x\n\r", __FUNCTION__, ldi->LID);
305 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
306 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
307
308 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error][%s]search lid failed from cache table\r\n", __FUNCTION__);
309 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"LID 0x%04X,category:0x%08X, attr:0x%08X\r\n",ldi->LID,ldi->category, ldi->attr);
310 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
311 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_INVALID_LID_3, ldi->LID);
312 return NVRAM_IO_ERRNO_INVALID_LID;
313 }
314
315 nvram_util_take_mutex(g_nvram_cache_mutex);
316 cache_temp_addr = (kal_uint32)(g_nvcache_base_address + (cache_info_header.cache_table_offset + cache_ldi->cache_offset + cache_ldi->file_length));
317 if(cache_temp_addr < (cache_offset + size))
318 {
319 nvram_util_give_mutex(g_nvram_cache_mutex);
320 kal_prompt_trace(MOD_NVRAM, "NVCACHE WRITE SHM CROSS BORDE:0x%x < 0x%x\n\r", cache_temp_addr, (cache_offset + size));
321 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)cache_temp_addr, NVRAM_ERROR_LOC_NVCACHE_WRITE_SHM_CROSS_BORDER, ldi->LID);
322 }
323 dest_buffer = (kal_uint8*)cache_offset;
324 kal_mem_cpy((void *)dest_buffer, src_buffer, size);
325 nvram_util_give_mutex(g_nvram_cache_mutex);
326
327 return NVRAM_IO_ERRNO_OK;
328
329}
330
331/*****************************************************************************
332 * FUNCTION
333 * nvram_read_data_from_cache
334 * DESCRIPTION
335 * read data from cache
336 * PARAMETERS
337 * ldi [IN]
338 * nvram_param [IN]
339 * RETURNS
340 * success or fail
341 *****************************************************************************/
342nvram_errno_enum nvram_read_data_from_cache(nvram_ltable_entry_struct* ldi, NVRAM_FS_PARAM_CMPT_T *nvram_param)
343{
344 nvram_lid_cache_table_struct *cache_ldi = NULL;
345 kal_bool result = KAL_FALSE;
346 kal_uint32 cache_temp_addr = 0;
347
348 if (!(result = get_lid_cache_index_item(ldi->LID , &cache_ldi))) {
349 kal_prompt_trace(MOD_NVRAM, "[%s]search lid failed from cache table index: 0x%x\n\r", __FUNCTION__, ldi->LID);
350 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
351 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
352
353 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error][%s]search lid failed from cache table index\r\n", __FUNCTION__);
354 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"LID 0x%04X category:0x%08X, attr:0x%08X\r\n", ldi->LID, ldi->category, ldi->attr);
355 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
356 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_INVALID_LID_4, ldi->LID);
357 return NVRAM_IO_ERRNO_INVALID_LID;
358 }
359
360 nvram_util_take_mutex(g_nvram_cache_mutex);
361 cache_temp_addr = (kal_uint32)(g_nvcache_base_address + (cache_info_header.cache_table_offset + cache_ldi->cache_offset));
362 if((cache_temp_addr + cache_ldi->file_length) < (cache_temp_addr + nvram_param->Offset + nvram_param->Length))
363 {
364 nvram_util_give_mutex(g_nvram_cache_mutex);
365 kal_prompt_trace(MOD_NVRAM, "NVCACHE READ SHM CROSS BORDE:0x%x < 0x%x\n\r", (cache_temp_addr + cache_ldi->file_length), (cache_temp_addr + nvram_param->Offset + nvram_param->Length));
366 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)cache_temp_addr, NVRAM_ERROR_LOC_NVCACHE_READ_SHM_CROSS_BORDER, ldi->LID);
367 }
368 kal_mem_cpy((void *)nvram_param->DataPtr, (void *)(g_nvcache_base_address + ((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + nvram_param->Offset)), nvram_param->Length);
369 *(nvram_param->Read) = nvram_param->Length;
370 *(nvram_param->FileSize) = cache_ldi->file_length;
371 nvram_param->ret[0] = nvram_param->opid_map;
372 nvram_param->ret[1] = 0;
373 nvram_util_give_mutex(g_nvram_cache_mutex);
374
375 return NVRAM_IO_ERRNO_OK;
376}
377
378/*****************************************************************************
379 * FUNCTION
380 * nvram_read_data_from_cache
381 * DESCRIPTION
382 * read data from cache
383 * PARAMETERS
384 * ldi [IN]
385 * nvram_param [IN]
386 * RETURNS
387 * success or fail
388 *****************************************************************************/
389kal_bool nvram_read_header_from_cache(nvram_ltable_entry_struct* ldi, void* buffer, kal_uint32 buffer_size, kal_uint32 cache_offset)
390{
391 nvram_lid_cache_table_struct *cache_ldi = NULL;
392 kal_uint8* src_buffer = NULL;
393 kal_bool result = KAL_FALSE;
394
395 if (!(result = get_lid_cache_index_item(ldi->LID , &cache_ldi))) {
396 kal_prompt_trace(MOD_NVRAM, "[%s]search lid failed from cache table index: 0x%x\n\r", __FUNCTION__, ldi->LID);
397 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
398 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
399
400 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error][%s]search lid failed from cache table\r\n", __FUNCTION__);
401 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"LID 0x%04X,category:0x%08X, attr:0x%08X\r\n", ldi->LID, ldi->category, ldi->attr);
402 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
403 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_INVALID_LID_5, ldi->LID);
404 return NVRAM_IO_ERRNO_INVALID_LID;
405 }
406
407 nvram_util_take_mutex(g_nvram_cache_mutex);
408 src_buffer = (kal_uint8*)cache_offset;
409 kal_mem_cpy((void *)buffer, (void *)src_buffer, buffer_size);
410 nvram_util_give_mutex(g_nvram_cache_mutex);
411
412 return KAL_TRUE;
413}
414
415DECLARE_MIPS32
416static kal_uint16 nvram_cache_data_header_checksum(kal_uint8 *buf, kal_uint32 size)
417{
418 /*----------------------------------------------------------------*/
419 /* Local Variables */
420 /*----------------------------------------------------------------*/
421 kal_uint32 i;
422 kal_uint16 chksum = *(kal_uint16*)buf;
423 kal_uint8 *byte_chksum = (kal_uint8*)&chksum;
424 kal_uint8 value;
425
426 /*----------------------------------------------------------------*/
427 /* Code Body */
428 /*----------------------------------------------------------------*/
429
430 for (i = 0; i < size; i++)
431 {
432 value = *(buf + i);
433 while(value) {
434 if(value & 0x1) {
435 #if defined(__MTK_TARGET__)
436 __asm__ __volatile__
437 (
438 "rol %0, %0, 1\r\n"
439 ::"r"(chksum)
440 );
441 #else
442 __asm {ROL [chksum],1};
443 #endif
444 }
445 value >>= 1;
446 }
447 #if defined(__MTK_TARGET__)
448 __asm__ __volatile__
449 (
450 "rol %0, %0, 4\r\n"
451 ::"r"(chksum)
452 );
453 #else
454 __asm {ROL [chksum],4};
455 #endif
456
457 *byte_chksum += *(buf + i);
458 }
459
460 return chksum;
461}
462
463kal_uint32 nvram_cache_appendix_header_offset(nvram_ltable_entry_struct *ldi)
464{
465 kal_uint32 size = 0;
466 kal_uint32 nvram_chksum_size = 0;
467 nvram_lid_chksum_info lid_chksum_info = {0};
468
469 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
470 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
471
472 if ((ldi->attr & NVRAM_ATTR_CONFIDENTIAL)
473 #ifdef __NVRAM_BIND_TO_CHIP_CIPHER__
474 || (ldi->attr & NVRAM_ATTR_MSP)
475 #endif
476 ){ /* 16 byte alignment */
477 size = NVRAM_MSP_ALIGNMENT_REMAINDER(ldi->size + nvram_chksum_size) + ldi->size + nvram_chksum_size;
478 }
479 else {
480 size = (ldi->size + nvram_chksum_size);
481 }
482 size = size * ldi->total_records;
483 return (NVRAM_LDI_HEADER_SIZE + size);
484}
485
486/*****************************************************************************
487 * FUNCTION
488 * nvram_drv_fat_prepare_data
489 * DESCRIPTION
490 * copy data into buffer
491 * PARAMETERS
492 * source: [IN] source data
493 * offset: [IN] offset in data
494 * buffer: [OUT] buffer
495 * buffer_size: [IN] size of buffer
496 * RETURNS
497 * void
498 *****************************************************************************/
499void nvram_cache_drv_fat_prepare_data(kal_uint8 *buffer, const kal_uint8 *source, kal_uint32 offset, kal_uint32 buffer_size)
500{
501 /*----------------------------------------------------------------*/
502 /* Local Variables */
503 /*----------------------------------------------------------------*/
504
505 /*----------------------------------------------------------------*/
506 /* Code Body */
507 /*----------------------------------------------------------------*/
508
509 if (source == NVRAM_EF_ZERO_DEFAULT)
510 {
511 nvram_memset(buffer, 0x00, buffer_size);
512 }
513 else if (source == NVRAM_EF_FF_DEFAULT)
514 {
515 nvram_memset(buffer, 0xFF, buffer_size);
516 }
517 else
518 {
519 kal_mem_cpy(buffer, source+offset, buffer_size);
520 }
521}
522
523kal_bool nvram_cache_prepare_ota_header(nvram_ldi_ota_header *ldi_ota_header, nvram_ltable_entry_struct *ldi)
524{
525 #if defined(__NVRAM_DEFVAL_CHANGE_RESET__)
526 kal_int32 chksum_index;
527 #endif
528
529 memcpy(ldi_ota_header->header, "LDI", 4);
530 ldi_ota_header->LID = ldi->LID;
531 ldi_ota_header->ldi_attr = ldi->attr;
532 ldi_ota_header->ldi_category= ldi->category;
533 ldi_ota_header->record_size = ldi->size;
534 ldi_ota_header->total_records = ldi->total_records;
535 //check appendix header offset
536 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
537 ldi->append_offset = nvram_cache_appendix_header_offset(ldi);
538 }
539 #if defined(__NVRAM_DEFVAL_CHANGE_RESET__)
540 chksum_index = nvram_get_defval_chksum_index(ldi->LID);
541 if(-1 != chksum_index)
542 {
543 kal_mem_cpy((char *)(ldi_ota_header->defval_chkrst_h), (const char*)(lid_default_value_chksum[chksum_index].chksum), RST_CHKSUM_SIZE-6);
544 }
545 #endif
546 ldi_ota_header->checksum = nvram_cache_data_header_checksum((kal_uint8*)ldi_ota_header, (NVRAM_LDI_OTA_HEADER_SIZE - sizeof(ldi_ota_header->checksum)));
547 return KAL_TRUE;
548}
549
550kal_bool nvram_cache_prepare_debug_header(nvram_ldi_debug_header *ldi_debug_header, nvram_ltable_entry_struct *ldi)
551{
552 #if defined(__NVRAM_STRUCTURE_CHANGE_RESET__) || defined(__NVRAM_DEFVAL_CHANGE_RESET__)
553 kal_int32 chksum_index;
554 #endif
555
556 ldi_debug_header->last_write_taskID = kal_get_current_task_index();
557 ldi_debug_header->last_write_time = kal_get_systicks();
558 ldi_debug_header->write_times += 1;
559
560 #if defined(__NVRAM_STRUCTURE_CHANGE_RESET__)
561 chksum_index = nvram_get_structure_chksum_index(ldi->LID);
562 if(-1 != chksum_index)
563 {
564 kal_mem_cpy((char *)(ldi_debug_header->struct_chkrst), (const char*)(lid_structure_chksum[chksum_index].chksum), RST_CHKSUM_SIZE);
565 }
566 #endif
567
568 #if defined(__NVRAM_DEFVAL_CHANGE_RESET__)
569 chksum_index = nvram_get_defval_chksum_index(ldi->LID);
570 if(-1 != chksum_index)
571 {
572 kal_mem_cpy((char *)(ldi_debug_header->defval_chkrst_l),(const char*)(&(lid_default_value_chksum[chksum_index].chksum[RST_CHKSUM_SIZE-6])), 6);
573 }
574 #endif
575
576 return KAL_TRUE;
577}
578
579kal_bool nvram_cache_prepare_data_header(nvram_ltable_entry_struct *ldi,kal_uint8 *ldi_hd_buffer)
580{
581 nvram_ldi_ota_header *ldi_ota_header = (nvram_ldi_ota_header*)ldi_hd_buffer;
582 nvram_ldi_debug_header *ldi_debug_header = (nvram_ldi_debug_header*)(ldi_hd_buffer + NVRAM_LDI_OTA_HEADER_SIZE);
583
584 nvram_cache_prepare_ota_header(ldi_ota_header,ldi);
585 nvram_cache_prepare_debug_header(ldi_debug_header, ldi);
586
587 return KAL_TRUE;
588}
589
590kal_uint32 nvram_cache_prepare_appendix_header(nvram_appendix_type_enum type, nvram_ldi_appendix_header *ldi_append_header, nvram_ltable_entry_struct *ldi, kal_uint32 data_offset)
591{
592 kal_uint32 nvram_chksum_size = 0;
593 nvram_lid_chksum_info lid_chksum_info = {0};
594
595 memset(ldi_append_header, 0, NVRAM_LDI_APPENDIX_HEADER_SIZE);
596 memcpy(ldi_append_header->header, "APDX", 4);
597
598 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
599 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
600
601 if(type == NVRAM_APPEND_TYPE_CHKSUM) {
602 ldi_append_header->type = NVRAM_APPEND_TYPE_CHKSUM;
603 ldi_append_header->data_offset = data_offset;
604 //caculate next data start
605 data_offset = (data_offset + (ldi->total_records * (nvram_chksum_size)));
606 data_offset = NVRAM_MSP_ALIGNMENT_CEILING(data_offset);
607 }
608 else {
609 ldi_append_header->type = NVRAM_APPEND_TYPE_END;
610 data_offset = 0;
611 }
612 ldi_append_header->checksum = nvram_cache_data_header_checksum((kal_uint8*)ldi_append_header, NVRAM_LDI_APPENDIX_HEADER_SIZE);
613 return data_offset;
614}
615
616
617/*****************************************************************************
618 * FUNCTION
619 * nvram_cache_reset_header
620 * DESCRIPTION
621 * reset one LID header to default value
622 * PARAMETERS
623 * ldi [IN]
624 * rec_index [IN]
625 * rec_amount [IN]
626 * RETURNS
627 * success or fail
628 *****************************************************************************/
629
630kal_bool nvram_cache_reset_header(nvram_ltable_entry_struct *ldi, nvram_header_section_enum section)
631{
632
633 NVRAM_FILE_NAME nvramname;
634 kal_wchar filename[NVRAM_MAX_PATH_LEN];
635 nvram_folder_enum nvram_folder;
636 FS_HANDLE file_handle = FS_INVALID_FILE_HANDLE;
637 kal_int32 result = FS_NO_ERROR;
638 kal_int32 backup_file_num = 1;
639 kal_bool mulpiple = KAL_FALSE;
640 kal_bool ret_val = KAL_TRUE;
641 nvram_ldi_header nv_header;
642 nvram_ldi_appendix_header nv_appendix_header;
643 nvram_ldi_ota_header *ldi_ota_header = NULL;
644 nvram_ldi_debug_header *ldi_debug_header = NULL;
645 nvram_ldi_appendix_header *ldi_append_header = &nv_appendix_header;
646 kal_uint32 ldi_hd_buffer_size = 0;
647 kal_uint32 ldi_hd_offset = 0;
648 kal_uint32 ldi_checksum_hd_offset = 0;
649 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
650 SGPT_CTRL_START_T start;
651 #endif
652
653
654 kal_mem_set(filename, 0x0, NVRAM_MAX_PATH_LEN * sizeof(kal_wchar));
655 kal_mem_set(&nv_header, 0x0, sizeof(nv_header));
656 kal_mem_set(&nv_appendix_header, 0x0, sizeof(nv_appendix_header));
657
658 ldi_ota_header = &(nv_header.nv_ota_header);
659 ldi_debug_header = &(nv_header.nv_dbg_header);
660
661 ldi_hd_buffer_size += NVRAM_LDI_HEADER_SIZE;
662
663
664 if(NVRAM_IS_ATTR_MULTIPLE(ldi->attr) || NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category))
665 {
666 mulpiple = KAL_TRUE;
667 backup_file_num = 2;
668 }
669 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE);
670 nvram_folder = nvram_query_folder_index(ldi->category);
671 nvram_query_file_name(nvram_folder, nvramname, filename);
672
673 /* NVRAM GPT timeout assert start timer */
674 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
675 start.u2Tick= NVRAM_WRITE_GPT_TIMEOUT;
676 start.pfCallback=nvram_gpt_timeout_callback;
677 start.vPara=NULL;
678 #endif
679
680 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
681 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_START, (DCL_CTRL_DATA_T*)&start); //start timer
682 #endif
683 do {
684 NVRAM_FS_START_EX(FS_OP_OPEN, filename);
685 file_handle = FS_Open(filename, FS_READ_WRITE | FS_OPEN_NO_DIR | FS_CREATE);
686 NVRAM_FS_END(FS_OP_OPEN,file_handle);
687 if (file_handle < FS_NO_ERROR) {
688 kal_prompt_trace(MOD_NVRAM, "NVRAM write header open fail:0x%x\n\r", file_handle);
689 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
690 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
691 kal_prompt_trace(MOD_NVRAM, "section:%d\n\r", section);
692
693 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"NVRAM write header open fail:%d\r\n", file_handle);
694 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"LID 0x%04X,category:0x%08X, attr:0x%08X\r\n", ldi->LID, ldi->category, ldi->attr);
695 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"fileprefix:%s,fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
696 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"section:%d\r\n", section);
697 if(NVRAM_IS_ATTR_FAULT_ASSERT(ldi->attr)) {
698 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)file_handle, NVRAM_ERROR_LOC_NVCACHE_OPEN_NV_FOLDER_FAIL_3, ldi->LID);
699 }
700 ret_val = KAL_FALSE;
701 goto CH_RESRET_FINAL;
702 }
703 if(ldi_hd_offset && (file_handle > FS_NO_ERROR)) {
704 NVRAM_FS_START(FS_OP_SEEK);
705 result = FS_Seek(file_handle, ldi_hd_offset, FS_FILE_BEGIN);
706 NVRAM_FS_END(FS_OP_SEEK,result);
707 }
708
709 if(section & LDI_HEADER_DBG_SECTION) {
710 nvram_cache_prepare_debug_header(ldi_debug_header,ldi);
711 }
712 if(section & LDI_HEADER_OTA_SECTION) {
713 nvram_cache_prepare_ota_header(ldi_ota_header,ldi);
714 }
715 NVRAM_FS_START(FS_OP_WRITE);
716 result = FS_Write(file_handle, (void *)(&nv_header), ldi_hd_buffer_size, &ldi_hd_buffer_size);
717 NVRAM_FS_END(FS_OP_WRITE,result);
718 if (result < FS_NO_ERROR) {
719 kal_prompt_trace(MOD_NVRAM, "NVRAM write header write fail:0x%x\n\r", result);
720 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
721 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
722 kal_prompt_trace(MOD_NVRAM, "section:%d\n\r", section);
723
724 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"NVRAM write header write fail:%d\r\n", file_handle);
725 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"LID 0x%04X,category:0x%08X, attr:0x%08X\r\n", ldi->LID, ldi->category, ldi->attr);
726 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
727 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"section:%d\r\n", section);
728 if(NVRAM_IS_ATTR_FAULT_ASSERT(ldi->attr)) {
729 NVRAM_FS_START(FS_OP_CLOSE);
730 result = FS_Close(file_handle);
731 NVRAM_FS_END(FS_OP_CLOSE,result);
732 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)file_handle, NVRAM_ERROR_LOC_NVCACHE_OPEN_NV_FOLDER_FAIL_2, ldi->LID, result);
733 }
734 ret_val = KAL_FALSE;
735 goto CH_RESRET_FINAL;
736 }
737 //write appendix header
738 ldi_checksum_hd_offset = nvram_cache_appendix_header_offset(ldi);
739 if((section == LDI_HEADER_ALL_SECTION) && (ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE)) {
740 nvram_prepare_appendix_header(NVRAM_APPEND_TYPE_CHKSUM, ldi_append_header, ldi, NVRAM_LDI_APPENDIX_HEADER_SIZE);
741 NVRAM_FS_START(FS_OP_SEEK);
742 result = FS_Seek(file_handle, ldi_checksum_hd_offset, FS_FILE_BEGIN);
743 NVRAM_FS_END(FS_OP_SEEK,result);
744 NVRAM_FS_START(FS_OP_WRITE);
745 result = FS_Write(file_handle, ldi_append_header, NVRAM_LDI_APPENDIX_HEADER_SIZE, &ldi_hd_buffer_size);
746 NVRAM_FS_END(FS_OP_WRITE,result);
747 if (result < FS_NO_ERROR) {
748 kal_prompt_trace(MOD_NVRAM, "NVRAM appendix header write fail:0x%x\n\r", result);
749 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
750 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
751
752 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"NVRAM write header write fail:%d\r\n", file_handle);
753 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"LID 0x%04X,category:0x%08X, attr:0x%08X\r\n", ldi->LID, ldi->category, ldi->attr);
754 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
755 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"section:%d\r\n", section);
756 if(NVRAM_IS_ATTR_FAULT_ASSERT(ldi->attr)) {
757 NVRAM_FS_START(FS_OP_CLOSE);
758 result = FS_Close(file_handle);
759 NVRAM_FS_END(FS_OP_CLOSE,result);
760 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)file_handle, NVRAM_LOC_WRITE_FILE_FAIL_7, ldi->LID, result);
761 }
762 ret_val = KAL_FALSE;
763 goto CH_RESRET_FINAL;
764 }
765
766 }
767
768 CH_RESRET_FINAL:
769
770 if(file_handle > FS_NO_ERROR) {
771 NVRAM_FS_START(FS_OP_CLOSE);
772 result = FS_Close(file_handle);
773 NVRAM_FS_END(FS_OP_CLOSE,result);
774 }
775 backup_file_num --;
776 #if defined(__NVRAM_WRITE_PROTECT_ENABLE__)
777 if (NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category)) {
778 // Don't write protect2
779 break;
780 }
781 #endif
782 if(!mulpiple)
783 {
784 break;
785 }
786 nvram_folder = nvram_query_folder_index_ex(ldi->category,KAL_FALSE);
787 nvram_util_make_lid_filename(ldi, nvramname, KAL_FALSE);
788 nvram_query_file_name(nvram_folder, nvramname, filename);
789 }while(backup_file_num > 0);
790
791 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
792 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_STOP, (DCL_CTRL_DATA_T*)NULL); //stop timer
793 #endif
794
795 return ret_val;
796}
797
798/*****************************************************************************
799 * FUNCTION
800 * nvram_cache_drv_fat_backup
801 * DESCRIPTION
802 * To make a backup.
803 * PARAMETERS
804 * prefix [IN] file prefix
805 * verno [IN] file verno
806 * a_to_b [IN] direction
807 * RETURNS
808 * error code
809 *****************************************************************************/
810kal_int32 nvram_cache_drv_fat_backup(nvram_ltable_entry_struct *ldi, kal_bool a_to_b)
811{
812 /*----------------------------------------------------------------*/
813 /* Local Variables */
814 /*----------------------------------------------------------------*/
815 kal_wchar src_path[NVRAM_MAX_PATH_LEN], dest_path[NVRAM_MAX_PATH_LEN];
816 NVRAM_FILE_NAME nvramname;
817 kal_int32 result = FS_NO_ERROR;
818
819 /*----------------------------------------------------------------*/
820 /* Code Body */
821 /*----------------------------------------------------------------*/
822 if (a_to_b)
823 {
824 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE); // A
825 nvram_query_file_name(nvram_query_folder_index_ex(ldi->category, KAL_TRUE), nvramname, src_path);
826
827 #ifdef __NVRAM_BACKUP_DISK_FAT__
828 if (NVRAM_IS_ATTR_BACKUP_FAT(ldi->attr))
829 {
830 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE);
831 nvram_query_file_name(NVRAM_NVD_BAK, nvramname, dest_path);
832 }
833 else
834 #endif
835 {
836 nvram_util_make_lid_filename(ldi, nvramname, KAL_FALSE); // B
837 nvram_query_file_name(nvram_query_folder_index_ex(ldi->category, KAL_FALSE), nvramname, dest_path);
838 }
839 }
840 else
841 {
842 #ifdef __NVRAM_BACKUP_DISK_FAT__
843 if (NVRAM_IS_ATTR_BACKUP_FAT(ldi->attr))
844 {
845 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE);
846 nvram_query_file_name(NVRAM_NVD_BAK, nvramname, src_path);
847 }
848 else
849 #endif
850 {
851 nvram_util_make_lid_filename(ldi, nvramname, KAL_FALSE); // B
852 nvram_query_file_name(nvram_query_folder_index_ex(ldi->category, KAL_FALSE), nvramname, src_path);
853
854 }
855
856 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE); // A
857 nvram_query_file_name(nvram_query_folder_index_ex(ldi->category, KAL_TRUE), nvramname, dest_path);
858 }
859 NVRAM_FS_START_EX(FS_OP_DELETE, dest_path);
860 result = FS_Delete(dest_path);
861 NVRAM_FS_END(FS_OP_DELETE,result);
862 NVRAM_FS_START_EX(FS_OP_MOVE, dest_path);
863 result = FS_Move(src_path, dest_path, FS_MOVE_COPY, NULL, NULL, 0);
864 NVRAM_FS_END(FS_OP_MOVE,result);
865 return result;
866}
867
868
869/*****************************************************************************
870 * FUNCTION
871 * nvram_reset_data_item
872 * DESCRIPTION
873 * This is nvram_cache_reset_data_item() function of NVRAM module.
874 * PARAMETERS
875 * ldi [IN] MUST be 1 ~ (total_LID - 1)
876 * index [IN] MUST be 1 ~ total_records
877 * data [?]
878 * is_init [IN]
879 * buffer_size(?) [IN] MUST be even-bytes aligned: ie, ((ldi->size + 1) / 2) * 2 Note that content of `data' could be changed due to encyrption!!
880 * buffer(?) [IN] MUST be even-bytes aligned.
881 * RETURNS
882 * NVRAM_IO_ERRNO_INVALID_LID
883 * NVRAM_IO_ERRNO_INVALID_RECORD
884 * NVRAM_IO_ERRNO_INVALID_SIZE
885 * NVRAM_IO_ERRNO_CHK if all copies are failed to write
886 * NVRAM_IO_ERRNO_OK at least one valid copy is written.
887 *****************************************************************************/
888nvram_errno_enum nvram_cache_reset_data_item(kal_char *nvramname,
889 nvram_folder_enum nvram_folder,
890 kal_uint32 file_offset,
891 kal_uint16 rec_index,
892 kal_uint16 rec_amount,
893 kal_uint32 rec_size,
894 const kal_uint8 *buffer,
895 nvram_ltable_entry_struct *ldi,
896 kal_bool initialize)
897{
898 kal_uint32 openOption = FS_READ_WRITE | FS_OPEN_NO_DIR;
899 kal_wchar filename[NVRAM_MAX_PATH_LEN];
900 FS_HANDLE hFile = 0;
901 kal_uint32 len = 0;
902 kal_int32 result = FS_NO_ERROR;
903 nvram_ldi_ota_header ota_header;
904 kal_int32 ret = FS_NO_ERROR;
905 kal_uint32 nvram_chksum_size = 0;
906 nvram_lid_chksum_info lid_chksum_info = {0};
907 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
908 SGPT_CTRL_START_T start;
909 #endif
910
911 /* NVRAM GPT timeout assert start timer */
912 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
913 start.u2Tick= NVRAM_WRITE_GPT_TIMEOUT;
914 start.pfCallback=nvram_gpt_timeout_callback;
915 start.vPara=NULL;
916 #endif
917 kal_mem_set(filename, 0x0, NVRAM_MAX_PATH_LEN * sizeof(kal_wchar));
918
919 nvram_util_take_mutex(g_nvram_fs_mutex);
920 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
921 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_START, (DCL_CTRL_DATA_T*)&start); //start timer
922 #endif
923
924 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
925 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
926
927 do
928 {
929 /* translate record id to filename */
930 nvram_query_file_name(nvram_folder, nvramname, filename);
931
932 openOption |= FS_CREATE;
933 if (initialize != KAL_TRUE) {
934 #if !defined(_NAND_FLASH_BOOTING_) && !defined(__FS_SYSDRV_ON_NAND__) && !defined(__EMMC_BOOTING__)
935 openOption |= FS_PROTECTION_MODE; /* boot from NAND and single bank NOR don't support this */
936 #endif
937 }
938
939 #if defined(__CCCIFS_SUPPORT__) && defined(__MTK_TARGET__)
940 if (ldi->attr & NVRAM_ATTR_COMMITTED) {
941 openOption |= FS_COMMITTED;
942 }
943 #endif
944 NVRAM_FS_START_EX(FS_OP_OPEN,filename);
945 hFile = FS_Open((const kal_wchar*)filename, openOption);
946 NVRAM_FS_END(FS_OP_OPEN,hFile);
947
948 if (hFile == FS_FILE_NOT_FOUND) {
949 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Open %s fail at %d,FS_FILE_NOT_FOUND\r\n",__FUNCTION__,nvramname,__LINE__);
950 result = NVRAM_DRV_EMPTY_RECORD;
951 nvram_cache_last_line = __LINE__;
952 break;
953 }
954 else if (hFile <= FS_NO_ERROR) {
955 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Open %s fail\r\n",__FUNCTION__,nvramname);
956 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]Fail at %d,hFile=%d\r\n",__LINE__,hFile);
957 result = hFile;
958 nvram_cache_last_line = __LINE__;
959 break;
960 }
961
962 //Get appdenix header info
963 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
964 ldi->append_buffer = get_ctrl_buffer(rec_amount * nvram_chksum_size);
965 }
966 if (file_offset) {
967 NVRAM_FS_START(FS_OP_SEEK);
968 result = FS_Seek(hFile, file_offset, FS_FILE_BEGIN);
969 NVRAM_FS_END(FS_OP_SEEK,result);
970 if(FS_NO_ERROR > result){
971 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Seek fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
972 result = hFile;
973 nvram_cache_last_line = __LINE__;
974 break;
975 }
976 }
977
978 result = nvram_drv_fat_write_section(hFile, buffer, rec_index, rec_amount, rec_size, ldi, file_offset);
979 }while(0);
980
981 //write-back appdenix header info
982 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
983 if(ldi->append_offset == 0) {
984 if(KAL_TRUE == nvram_read_ota_header(hFile, &ota_header, NVRAM_LDI_OTA_HEADER_SIZE) &&
985 (ota_header.ldi_attr & NVRAM_ATTR_CHKSUM_INTEGRATE) )
986 {
987 ldi->append_offset = nvram_appendix_header_offset(ldi);
988 }
989 else {
990 ldi->append_offset = -1;
991 }
992 }
993 if(ldi->append_offset > 0) {
994 NVRAM_FS_START(FS_OP_SEEK);
995 ret = FS_Seek(hFile, (ldi->append_offset + NVRAM_LDI_APPENDIX_HEADER_SIZE + ((rec_index - 1) * nvram_chksum_size)), FS_FILE_BEGIN);
996 NVRAM_FS_END(FS_OP_SEEK,ret);
997 NVRAM_FS_START(FS_OP_WRITE);
998 ret = FS_Write(hFile, ldi->append_buffer, (rec_amount * nvram_chksum_size), &len);
999 NVRAM_FS_END(FS_OP_WRITE,ret);
1000 }
1001 if(ldi->append_buffer) {
1002 free_ctrl_buffer(ldi->append_buffer);
1003 ldi->append_buffer = NULL;
1004 }
1005 }
1006
1007 if (hFile > FS_NO_ERROR) {
1008 NVRAM_FS_START(FS_OP_CLOSE);
1009 ret = FS_Close(hFile);
1010 NVRAM_FS_END(FS_OP_CLOSE,ret);
1011 }
1012
1013 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1014 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_STOP, (DCL_CTRL_DATA_T*)NULL); //stop timer
1015 #endif
1016
1017 nvram_util_give_mutex(g_nvram_fs_mutex);
1018
1019 if (result < FS_NO_ERROR) {
1020 nvram_cache_last_err = result;
1021 MD_TRC_FUNC_NVRAM_DRV_FAT_THROW_EXCEPTION(nvram_cache_last_err, nvram_cache_last_line);
1022 }
1023 return NVRAM_IO_ERRNO_OK;
1024}
1025
1026/*****************************************************************************
1027 * FUNCTION
1028 * nvram_cache_write_data_item
1029 * DESCRIPTION
1030 * This is nvram_cache_write_data_item() function of NVRAM module.
1031 * PARAMETERS
1032 * ldi [IN] MUST be 1 ~ (total_LID - 1)
1033 * index [IN] MUST be 1 ~ total_records
1034 * data [?]
1035 * is_init [IN]
1036 * buffer_size(?) [IN] MUST be even-bytes aligned: ie, ((ldi->size + 1) / 2) * 2 Note that content of `data' could be changed due to encyrption!!
1037 * buffer(?) [IN] MUST be even-bytes aligned.
1038 * RETURNS
1039 * NVRAM_IO_ERRNO_INVALID_LID
1040 * NVRAM_IO_ERRNO_INVALID_RECORD
1041 * NVRAM_IO_ERRNO_INVALID_SIZE
1042 * NVRAM_IO_ERRNO_CHK if all copies are failed to write
1043 * NVRAM_IO_ERRNO_OK at least one valid copy is written.
1044 *****************************************************************************/
1045nvram_errno_enum nvram_cache_write_data_item(nvram_ltable_entry_struct *ldi, kal_uint32 index, kal_uint8 *data, kal_bool is_init)
1046{
1047 /*----------------------------------------------------------------*/
1048 /* Local Variables */
1049 /*----------------------------------------------------------------*/
1050 nvram_errno_enum status = NVRAM_IO_ERRNO_OK;
1051 nvram_drv_status_enum drv_status[2] = {NVRAM_DRV_OK, NVRAM_DRV_OK};
1052
1053 kal_uint32 record_ID =0;
1054 NVRAM_FILE_NAME nvramname;
1055 kal_int32 recovery_status = NVRAM_DRV_OK;
1056 kal_uint32 file_offset;
1057 nvram_folder_enum folder_index;
1058 kal_bool multiple = KAL_FALSE;
1059
1060 /*----------------------------------------------------------------*/
1061 /* Code Body */
1062 /*----------------------------------------------------------------*/
1063 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s ====>\r\n",__FUNCTION__);
1064 if (NVRAM_IS_ATTR_MULTIPLE(ldi->attr) || NVRAM_IS_ATTR_BACKUP_FAT(ldi->attr) || NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category))
1065 {
1066 multiple = KAL_TRUE;
1067 }
1068 folder_index = nvram_query_folder_index(ldi->category);
1069
1070 file_offset = NVRAM_LDI_HEADER_SIZE;
1071
1072 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE);
1073 for (record_ID = 0; record_ID < 2; record_ID++)
1074 {
1075 #if defined(__MTK_TARGET__) && defined(__NVRAM_IMPORTANT_PARTITIONS__) && defined(__CCCIFS_SUPPORT__)
1076 if (record_ID == 1)
1077 {
1078 #ifdef __NVRAM_WRITE_PROTECT_ENABLE__
1079 extern kal_bool smu_is_write_protect2(nvram_lid_enum file_idx);
1080 if (NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category))
1081 {
1082 // Aussme protect2 is always locked, acctually it only unlocked at first boot in factory
1083 if (drv_status[0] == NVRAM_DRV_OK)
1084 {
1085 if (smu_is_write_protect2(ldi->LID)) {
1086 // only trigger backup for specific LIDs when they are written legally (shoudn't be frequently)
1087 // trigger backup, this will write sync pattern in AP side
1088 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(ldi->LID, 0xFFFF, 0x0001,__LINE__);
1089 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"LID 0x%04X write sync pattern to AP\r\n",ldi->LID);
1090 ccci_send_message(CCMSG_ID_SYSMSGSVC_MD_UNPROTECT_PART_REQ, 0xABC);
1091
1092 } else {
1093 // Don't trigger backup.
1094 // think about this scenario:
1095 // some LID will update at known time (every md bootup time)
1096 // this will leave sync pattern in AP if we trigger backup,
1097 // then hacker can deleted all files on protect1 before reboot the phone
1098 // the SML data on protect2 will lost after phone reboot ...
1099 }
1100 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1101 return NVRAM_IO_ERRNO_OK;
1102 }
1103 else
1104 {
1105 // don't trigger backup due to protect1 write failed
1106 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1107 return NVRAM_IO_ERRNO_CHK;
1108 }
1109 }
1110 #endif
1111 folder_index = nvram_query_folder_index_ex(ldi->category, KAL_FALSE);
1112 }
1113 #endif
1114 // if LID == SYS_LID, file can be empty & index is the index of record needs to write (amount is 1)
1115 // if is_init == TRUE, file can be empty & index is the # of record needs to write (index always 1)
1116 // if is_init == FALSE, file should not be empty & index is the index of record needs to write (amount always 1)
1117
1118 if(is_init && NVRAM_EF_SYS_LID != ldi->LID)
1119 {
1120
1121 drv_status[record_ID] = nvram_cache_reset_data_item(
1122 nvramname,
1123 folder_index,
1124 file_offset,
1125 1,
1126 index,
1127 ldi->size,
1128 data,
1129 ldi,
1130 is_init);
1131
1132 }
1133 else
1134 {
1135 drv_status[record_ID] = nvram_cache_reset_data_item(
1136 nvramname,
1137 folder_index,
1138 file_offset,
1139 index,
1140 1,
1141 ldi->size,
1142 data,
1143 ldi,
1144 is_init);
1145 }
1146
1147 /* Try to reset data if it is not a initial case */
1148 if (drv_status[record_ID] != NVRAM_DRV_OK)
1149 {
1150 MD_TRC_IO_WRITE_DATA_ITEM_MULTIPLE(ldi->LID, drv_status, nvram_drv_fat_get_last_err(), __LINE__);
1151 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"drv_status[%d] =%d\r\n",record_ID,drv_status[record_ID]);
1152 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1153 status = NVRAM_IO_ERRNO_CHK;
1154 }
1155
1156 if (!multiple)
1157 {
1158 break;
1159 }
1160
1161 nvram_util_make_lid_filename(ldi, nvramname, KAL_FALSE);
1162
1163 }
1164
1165 if (status != NVRAM_IO_ERRNO_OK && multiple == KAL_TRUE)
1166 {
1167 if (drv_status[0] != NVRAM_DRV_OK && drv_status[1] == NVRAM_DRV_OK)
1168 {
1169 recovery_status = nvram_cache_drv_fat_backup(ldi, KAL_FALSE); /* A <-- B */
1170 }
1171 else if (drv_status[0] == NVRAM_DRV_OK && drv_status[1] != NVRAM_DRV_OK)
1172 {
1173 recovery_status = nvram_cache_drv_fat_backup(ldi, KAL_TRUE); /* A --> B */
1174 }
1175 else
1176 {
1177 // Both A&B write fail
1178 recovery_status = drv_status[0];
1179 }
1180
1181 if (recovery_status == NVRAM_DRV_OK)
1182 {
1183 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1184 return NVRAM_IO_ERRNO_OK;
1185 }
1186 }
1187 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1188 return status;
1189}
1190
1191/*****************************************************************************
1192 * FUNCTION
1193 * nvram_drv_fat_write_multiple
1194 * DESCRIPTION
1195 * write record(s) to FAT.
1196 * PARAMETERS
1197 * hFile [?]
1198 * buffer [?]
1199 * rec_amount [IN]
1200 * rec_size [IN]
1201 * RETURNS
1202 * void
1203 *****************************************************************************/
1204kal_int32 nvram_cache_drv_fat_write_multiple(
1205 FS_HANDLE hFile,
1206 const kal_uint8 *buffer,
1207 kal_uint16 rec_index,
1208 kal_uint16 rec_amount,
1209 kal_uint32 rec_size,
1210 nvram_ltable_entry_struct *ldi)
1211{
1212 /*----------------------------------------------------------------*/
1213 /* Local Variables */
1214 /*----------------------------------------------------------------*/
1215 kal_uint32 len = 0, remainLen = 0;
1216 kal_uint8 *chksum = NULL;
1217 kal_uint32 max_rec_amount;
1218 kal_uint32 section_size;
1219 kal_uint32 working_buffer_size;
1220 kal_uint8 *working_buffer = NULL;
1221 kal_int32 result = NVRAM_DRV_OK;
1222 kal_uint32 i;
1223 kal_uint32 rec_in_block;
1224 kal_uint32 nvram_chksum_size = 0;
1225 nvram_lid_chksum_info lid_chksum_info = {0};
1226
1227 /*----------------------------------------------------------------*/
1228 /* Code Body */
1229 /*----------------------------------------------------------------*/
1230 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s ====>\r\n",__FUNCTION__);
1231
1232 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
1233 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
1234
1235#ifdef __NVRAM_BIND_TO_CHIP_CIPHER__
1236 if ((ldi->attr & NVRAM_ATTR_MSP)||(ldi->attr & NVRAM_ATTR_CONFIDENTIAL))
1237 {
1238 /* 16 byte alignment */
1239 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(rec_size + nvram_chksum_size);
1240 }
1241#else
1242 if (ldi->attr & NVRAM_ATTR_CONFIDENTIAL)
1243 {
1244 /* 16 byte alignment */
1245 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(rec_size + nvram_chksum_size);
1246 }
1247#endif
1248
1249 //16bytes alignment, limitation: msp data will be oversize
1250 section_size = rec_size + nvram_chksum_size + remainLen;
1251 working_buffer_size = section_size * rec_amount;
1252
1253 if (rec_index > 1)
1254 {
1255 NVRAM_FS_START(FS_OP_SEEK);
1256 result = FS_Seek(hFile, (rec_index - 1) * section_size, FS_FILE_CURRENT);
1257 NVRAM_FS_END(FS_OP_SEEK,result);
1258 if (FS_NO_ERROR > result)
1259 {
1260 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Seek fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
1261 nvram_cache_last_line = __LINE__;
1262 goto final;
1263 }
1264 }
1265
1266 if (working_buffer_size > MAX_NVRAM_RECORD_SIZE)
1267 {
1268 working_buffer_size = MAX_NVRAM_RECORD_SIZE;
1269 }
1270
1271 working_buffer = (kal_uint8*) get_ctrl_buffer(working_buffer_size);
1272 max_rec_amount = working_buffer_size/section_size;
1273 chksum = (kal_uint8*) get_ctrl_buffer(nvram_chksum_size);
1274
1275 if (!(ldi->attr & NVRAM_ATTR_MSP) && !(ldi->attr & NVRAM_ATTR_CONFIDENTIAL) &&
1276 buffer != NVRAM_EF_ZERO_DEFAULT && buffer != NVRAM_EF_FF_DEFAULT)
1277 {
1278 for(i = 0; i < rec_amount; i++)
1279 {
1280 if (ldi->attr & NVRAM_ATTR_MULTI_DEFAULT)
1281 {
1282 //rec_index start from 1
1283 kal_mem_set(chksum, 0, nvram_chksum_size);
1284 nvram_util_caculate_checksum(ldi, buffer + (i+rec_index-1)*rec_size, rec_size,chksum);
1285 kal_mem_cpy(working_buffer + i*(rec_size + nvram_chksum_size), buffer + (i+rec_index-1)*rec_size, rec_size);
1286 kal_mem_cpy(working_buffer + i*(rec_size+ nvram_chksum_size)+rec_size, chksum, nvram_chksum_size);
1287 }
1288 else
1289 {
1290 if (i==0)
1291 {
1292 kal_mem_set(chksum, 0, nvram_chksum_size);
1293 nvram_util_caculate_checksum(ldi, buffer, rec_size,chksum);
1294 }
1295 kal_mem_cpy(working_buffer + i*(rec_size+ nvram_chksum_size), buffer, rec_size);
1296 kal_mem_cpy(working_buffer + i*(rec_size+ nvram_chksum_size)+rec_size, chksum, nvram_chksum_size);
1297 }
1298
1299 //record integrated checksum
1300 if((ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) && ldi->append_buffer) {
1301 kal_mem_cpy((void *)(ldi->append_buffer + (i * nvram_chksum_size)), (void *)chksum, nvram_chksum_size);
1302 }
1303
1304 if (i == (rec_amount - 1))
1305 {
1306 NVRAM_FS_START(FS_OP_WRITE);
1307 result = FS_Write(hFile, working_buffer, section_size * rec_amount, &len);
1308 NVRAM_FS_END(FS_OP_WRITE,result);
1309 if (FS_NO_ERROR > result)
1310 {
1311 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Write fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
1312 nvram_cache_last_line = __LINE__;
1313 goto final;
1314 }
1315 }
1316 }
1317 goto final;
1318 }
1319
1320 rec_in_block = 0;
1321 max_rec_amount = working_buffer_size / section_size;
1322
1323 for (i = 0; i < rec_amount; i ++)
1324 {
1325 nvram_cache_drv_fat_prepare_data(working_buffer + rec_in_block * section_size, buffer, (i+rec_index-1) * rec_size, rec_size);
1326 kal_mem_set(chksum, 0, nvram_chksum_size);
1327 nvram_util_caculate_checksum(ldi, working_buffer + rec_in_block * section_size, rec_size, chksum);
1328
1329 kal_mem_cpy(working_buffer + rec_in_block * section_size + rec_size, chksum, nvram_chksum_size);
1330
1331 //record integrated checksum
1332 if((ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) && ldi->append_buffer) {
1333 kal_mem_cpy((void *)(ldi->append_buffer + (i * nvram_chksum_size)), (void *)chksum, nvram_chksum_size);
1334 }
1335
1336 if (remainLen)
1337 {
1338 kal_mem_set(working_buffer + rec_in_block * section_size + rec_size + nvram_chksum_size , 0x00, remainLen);
1339 }
1340 if (ldi->attr & NVRAM_ATTR_CONFIDENTIAL)
1341 {
1342 //custom_nvram_encrypt(nvram_ptr->secret_key, working_buffer + rec_in_block * section_size, rec_size, rec_size);
1343 nvram_AES_encrypt(working_buffer + rec_in_block * section_size, section_size);
1344 }
1345 #ifdef __NVRAM_BIND_TO_CHIP_CIPHER__
1346 if (ldi->attr & NVRAM_ATTR_MSP)
1347 {
1348 /* this solution is only for work arround */
1349 #if (defined(__SMART_PHONE_MODEM__) || defined(__CCCIFS_SUPPORT__))
1350 kal_uint8 *working_buffer2 = (kal_uint8*) get_ctrl_buffer(section_size);
1351 kal_uint8 *working_buffer3 = (kal_uint8*) get_ctrl_buffer(section_size);
1352 if (working_buffer2 == NULL)
1353 {
1354 if (working_buffer)
1355 {
1356 free_ctrl_buffer(working_buffer);
1357 working_buffer = NULL;
1358 }
1359 kal_prompt_trace(MOD_NVRAM, "%s Can not get the memory from control buffer @line %d\n\r",__FUNCTION__,__LINE__);
1360 }
1361 if (working_buffer3 == NULL)
1362 {
1363 if (working_buffer)
1364 {
1365 free_ctrl_buffer(working_buffer);
1366 working_buffer = NULL;
1367 }
1368 free_ctrl_buffer(working_buffer2);
1369 working_buffer2 = NULL;
1370 kal_prompt_trace(MOD_NVRAM, "%s Can not get the memory from control buffer @line %d\n\r",__FUNCTION__,__LINE__);
1371 }
1372 //copy the original data from working_buffer to working_buffer2
1373 memcpy(working_buffer2, working_buffer + rec_in_block * section_size, section_size);
1374
1375 do
1376 {
1377 nvram_trace_to_file(__LINE__, 999, 0, 0, 0, 0);
1378 nvram_trace_to_file(nvram_ptr->secret_key[0], nvram_ptr->secret_key[1], nvram_ptr->secret_key[2], nvram_ptr->secret_key[3], 0, 0);
1379 nvram_trace_to_file(working_buffer[rec_in_block*section_size], working_buffer[rec_in_block*section_size + 1], working_buffer[rec_in_block*section_size + 2], working_buffer[rec_in_block*section_size + 3], 0, 0);
1380
1381 //encrypt working_buffer
1382 SST_Secure_Algo(NVRAM_MSP_ENCRYPT, (kal_uint32)working_buffer + rec_in_block * section_size, section_size, nvram_ptr->secret_key, working_buffer + rec_in_block * section_size);
1383 nvram_trace_to_file(working_buffer[rec_in_block*section_size], working_buffer[rec_in_block*section_size + 1], working_buffer[rec_in_block*section_size + 2], working_buffer[rec_in_block*section_size + 3], 0, 0);
1384
1385 //copy the encrypted data from working_buffer to working_buffer3
1386 memcpy(working_buffer3, working_buffer + rec_in_block * section_size, section_size);
1387 //decrypt the working_buffer3
1388 SST_Secure_Algo(NVRAM_MSP_DECRYPT, (kal_uint32)working_buffer3, section_size, nvram_ptr->secret_key, working_buffer3);
1389
1390 //compare the data between the working_buffer2 & working_buffer3
1391 if (memcmp(working_buffer2, working_buffer3, section_size) == 0)
1392 {
1393 //encrypt PASS
1394 break;
1395 }
1396 else
1397 {
1398 //encrypt FAIL, try again, WTF
1399 memcpy(working_buffer + rec_in_block * section_size, working_buffer2, section_size);
1400 }
1401 }while(1);
1402
1403 free_ctrl_buffer(working_buffer2);
1404 free_ctrl_buffer(working_buffer3);
1405 working_buffer2 = NULL;
1406 working_buffer3 = NULL;
1407 #else
1408 SST_Secure_Algo(NVRAM_MSP_ENCRYPT, (kal_uint32)working_buffer + rec_in_block * section_size, section_size, nvram_ptr->secret_key, working_buffer + rec_in_block * section_size);
1409
1410 #endif
1411 }
1412 #endif
1413
1414 /* if this is not multi default, no need to prepare data anymore */
1415 if (!(ldi->attr & NVRAM_ATTR_MULTI_DEFAULT))
1416 {
1417 break;
1418 }
1419
1420 rec_in_block ++;
1421
1422 if (rec_in_block == max_rec_amount || i == rec_amount - 1)
1423 {
1424 NVRAM_FS_START(FS_OP_WRITE);
1425 result = FS_Write(hFile, working_buffer, section_size * rec_in_block, &len);
1426 NVRAM_FS_END(FS_OP_WRITE,result);
1427 if (FS_NO_ERROR > result)
1428 {
1429 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Write fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
1430 nvram_cache_last_line = __LINE__;
1431 goto final;
1432 }
1433 rec_in_block = 0;
1434 }
1435 }
1436
1437 /* special handling for not multi default */
1438 if (!(ldi->attr & NVRAM_ATTR_MULTI_DEFAULT))
1439 {
1440 for (i = 0; i < rec_amount; i++)
1441 {
1442 //record integrated checksum
1443 if((ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) && ldi->append_buffer) {
1444 kal_mem_cpy((void *)(ldi->append_buffer + (i * nvram_chksum_size)), (void *)(working_buffer+rec_size), nvram_chksum_size);
1445 }
1446 NVRAM_FS_START(FS_OP_WRITE);
1447 result = FS_Write(hFile, working_buffer, section_size, &len);
1448 NVRAM_FS_END(FS_OP_WRITE,result);
1449 if (FS_NO_ERROR > result)
1450 {
1451 nvram_cache_last_line = __LINE__;
1452 goto final;
1453 }
1454 }
1455 }
1456
1457final:
1458
1459 if (working_buffer)
1460 {
1461 free_ctrl_buffer(working_buffer);
1462 working_buffer = NULL;
1463 }
1464
1465 if (chksum)
1466 {
1467 free_ctrl_buffer(chksum);
1468 chksum = NULL;
1469 }
1470
1471 if (FS_NO_ERROR > result)
1472 {
1473 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
1474 nvram_cache_last_err = result;
1475 MD_TRC_FUNC_NVRAM_DRV_FAT_THROW_EXCEPTION(nvram_cache_last_err, nvram_cache_last_line);
1476 return result;
1477 }
1478
1479 return NVRAM_DRV_OK;
1480}
1481
1482/*****************************************************************************
1483 * FUNCTION
1484 * nvram_drv_fat_write_multRec
1485 * DESCRIPTION
1486 * write record(s) to FAT,now it only used at initiation stage
1487 * PARAMETERS
1488 * buffer [?]
1489 * nvramname [?]
1490 * section_number [IN]
1491 * size [IN]
1492 * initialize [IN] true for reset, false for normal write.
1493 * RETURNS
1494 * void
1495 *****************************************************************************/
1496nvram_drv_status_enum nvram_cache_drv_fat_write_multRec(nvram_ltable_entry_struct *ldi,
1497 kal_char *nvramname,
1498 nvram_folder_enum nvram_folder,
1499 kal_uint32 file_offset,
1500 kal_uint16 rec_index,
1501 kal_uint16 rec_amount,
1502 kal_uint32 rec_size,
1503 const kal_uint8 *buffer,
1504 kal_bool initialize)
1505{
1506
1507 /*----------------------------------------------------------------*/
1508 /* Local Variables */
1509 /*----------------------------------------------------------------*/
1510 kal_wchar filename[NVRAM_MAX_PATH_LEN];
1511 FS_HANDLE hFile = 0;
1512 kal_int32 result = FS_NO_ERROR;
1513 kal_uint32 openOption = FS_READ_WRITE | FS_OPEN_NO_DIR;
1514 kal_uint32 len;
1515 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1516 SGPT_CTRL_START_T start;
1517 #endif
1518 nvram_ldi_ota_header ota_header;
1519 kal_int32 ret = FS_NO_ERROR;
1520 kal_uint32 nvram_chksum_size = 0;
1521 nvram_lid_chksum_info lid_chksum_info = {0};
1522
1523 /*----------------------------------------------------------------*/
1524 /* Code Body */
1525 /*----------------------------------------------------------------*/
1526
1527 /* NVRAM GPT timeout assert start timer */
1528 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1529 start.u2Tick= NVRAM_WRITE_GPT_TIMEOUT;
1530 start.pfCallback=nvram_gpt_timeout_callback;
1531 start.vPara=NULL;
1532 #endif
1533
1534 kal_mem_set(filename, 0x0, NVRAM_MAX_PATH_LEN * sizeof(kal_wchar));
1535
1536 nvram_util_take_mutex(g_nvram_fs_mutex);
1537
1538 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1539 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_START, (DCL_CTRL_DATA_T*)&start); //start timer
1540 #endif
1541
1542 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
1543 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
1544
1545 do
1546 {
1547 if (rec_index < 1 || rec_amount < 1)
1548 {
1549 nvram_cache_last_line = __LINE__;
1550 result = NVRAM_DRV_INVALID_RECORD_ID;
1551 goto final;
1552 }
1553
1554 /* translate record id to filename */
1555 nvram_query_file_name(nvram_folder, nvramname, filename);
1556
1557 /* set the attribute to empty before write data
1558 sometime the files may be read only if the nvram lock is turn on
1559 ex: software update when nvram lock is turned on
1560 But it is not a good solution here, we should unlock it in io layer */
1561
1562 openOption |= FS_CREATE;
1563
1564 #if (defined(__SMART_PHONE_MODEM__) || defined(__CCCIFS_SUPPORT__)) && defined(__MTK_TARGET__)
1565 if (ldi->attr & NVRAM_ATTR_COMMITTED)
1566 {
1567 openOption |= FS_COMMITTED;
1568 }
1569 #endif
1570 //Get appdenix header info
1571 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE)
1572 {
1573 ldi->append_buffer = get_ctrl_buffer(rec_amount * nvram_chksum_size);
1574 }
1575
1576 nvram_cache_reset_header(ldi, LDI_HEADER_ALL_SECTION);
1577 NVRAM_FS_START_EX(FS_OP_OPEN,filename);
1578 hFile = FS_Open((const kal_wchar*)filename, openOption);
1579 NVRAM_FS_END(FS_OP_OPEN,hFile);
1580
1581 if (hFile == FS_FILE_NOT_FOUND)
1582 {
1583 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Open %s fail at %d,FS_FILE_NOT_FOUND\r\n",__FUNCTION__,nvramname,__LINE__);
1584 nvram_cache_last_line = __LINE__;
1585 result = NVRAM_DRV_EMPTY_RECORD;
1586 goto final;
1587 }
1588 else if (hFile <= FS_NO_ERROR)
1589 {
1590 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Open %s fail\r\n",__FUNCTION__,nvramname);
1591 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]Fail at %d,hFile=%d\r\n",__LINE__,hFile);
1592 nvram_cache_last_line = __LINE__;
1593 result = hFile;
1594 goto final;
1595 }
1596 NVRAM_FS_START(FS_OP_SEEK);
1597 result = FS_Seek(hFile, NVRAM_LDI_HEADER_SIZE, FS_FILE_BEGIN);
1598 NVRAM_FS_END(FS_OP_SEEK,result);
1599 if(FS_NO_ERROR > result){
1600 nvram_cache_last_line = __LINE__;
1601 goto final;
1602 }
1603 result = nvram_cache_drv_fat_write_multiple(hFile, buffer, rec_index, rec_amount, rec_size, ldi);
1604 }while(0);
1605
1606 //write-back appdenix header info
1607 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
1608 //Write appendix info
1609 if(ldi->append_offset == 0) {
1610 kal_mem_set(&ota_header, 0, NVRAM_LDI_APPENDIX_HEADER_SIZE);
1611 if(KAL_TRUE == nvram_read_ota_header(hFile, &ota_header, NVRAM_LDI_OTA_HEADER_SIZE) &&
1612 (ota_header.ldi_attr & NVRAM_ATTR_CHKSUM_INTEGRATE) )
1613 {
1614 ldi->append_offset = nvram_cache_appendix_header_offset(ldi);
1615 }
1616 else {
1617 ldi->append_offset = -1;
1618 }
1619
1620 }
1621 if(ldi->append_offset > 0) {
1622 NVRAM_FS_START(FS_OP_SEEK);
1623 ret = FS_Seek(hFile, (ldi->append_offset + NVRAM_LDI_APPENDIX_HEADER_SIZE + ((rec_index - 1) * nvram_chksum_size)), FS_FILE_BEGIN);
1624 NVRAM_FS_END(FS_OP_SEEK,ret);
1625 NVRAM_FS_START(FS_OP_WRITE);
1626 ret = FS_Write(hFile, ldi->append_buffer, (rec_amount * nvram_chksum_size), &len);
1627 NVRAM_FS_END(FS_OP_WRITE,ret);
1628 }
1629
1630 }
1631final:
1632 if(ldi->append_buffer) {
1633 free_ctrl_buffer(ldi->append_buffer);
1634 ldi->append_buffer = NULL;
1635 }
1636
1637 if (hFile > FS_NO_ERROR)
1638 {
1639 NVRAM_FS_START(FS_OP_CLOSE);
1640 ret = FS_Close(hFile);
1641 NVRAM_FS_END(FS_OP_CLOSE,ret);
1642 }
1643 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1644 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_STOP, (DCL_CTRL_DATA_T*)NULL); //stop timer
1645 #endif
1646
1647 nvram_util_give_mutex(g_nvram_fs_mutex);
1648
1649 /* Set the attribute back to original attribute */
1650 if (result < FS_NO_ERROR)
1651 {
1652 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
1653 nvram_cache_last_err = result;
1654 MD_TRC_FUNC_NVRAM_DRV_FAT_THROW_EXCEPTION(nvram_cache_last_err, nvram_cache_last_line);
1655 }
1656
1657 return result;
1658}
1659#if defined(__MTK_TARGET__) && defined(__NVRAM_WRITE_WITH_FILE_SIZE__) && !defined(__NVRAM_WRITE_PROTECT_ENABLE__)
1660/*****************************************************************************
1661 * FUNCTION
1662 * nvram__cache_write_fs_data_item_multiple
1663 * DESCRIPTION
1664 * This is nvram_write_data_item_multiple() function of NVRAM module.
1665 * PARAMETERS
1666 * ldi [?]
1667 * index [IN]
1668 * buffer [?]
1669 * buffer_size [IN]
1670 * is_init [IN]
1671 * RETURNS
1672 * NVRAM_IO_ERRNO_CHK if all copies are failed to write
1673 * NVRAM_IO_ERRNO_OK at least one valid copy is written.
1674 *****************************************************************************/
1675static nvram_errno_enum nvram_cache_write_fs_data_item_multiple
1676 (nvram_ltable_entry_struct *ldi,
1677 kal_uint16 index,
1678 kal_uint16 rec_mount,
1679 const kal_uint8 *buffer,
1680 kal_bool is_init)
1681{
1682 /*----------------------------------------------------------------*/
1683 /* Local Variables */
1684 /*----------------------------------------------------------------*/
1685 nvram_errno_enum status = NVRAM_IO_ERRNO_OK;
1686 nvram_drv_status_enum drv_status[2] = {NVRAM_DRV_OK, NVRAM_DRV_OK};
1687 kal_uint32 record_ID;
1688 NVRAM_FILE_NAME nvramname;
1689 kal_uint32 file_offset;
1690 nvram_folder_enum folder_index;
1691 kal_bool multiple = KAL_FALSE;
1692
1693 /*----------------------------------------------------------------*/
1694 /* Code Body */
1695 /*----------------------------------------------------------------*/
1696 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s ====>\r\n",__FUNCTION__);
1697 if (NVRAM_IS_ATTR_MULTIPLE(ldi->attr) || NVRAM_IS_ATTR_BACKUP_FAT(ldi->attr) || NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category))
1698 {
1699 multiple = KAL_TRUE;
1700 }
1701
1702 folder_index = nvram_query_folder_index(ldi->category);
1703
1704 file_offset = NVRAM_LDI_HEADER_SIZE;
1705 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE);
1706
1707 for (record_ID = 0; record_ID < 2; record_ID++)
1708 {
1709
1710 #if defined(__MTK_TARGET__) && defined(__NVRAM_IMPORTANT_PARTITIONS__) && (defined(__SMART_PHONE_MODEM__) || defined(__CCCIFS_SUPPORT__))
1711 if (record_ID == 1)
1712 {
1713 #ifdef __NVRAM_WRITE_PROTECT_ENABLE__
1714 extern kal_bool smu_is_write_protect2(nvram_lid_enum file_idx);
1715 if (NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category))
1716 {
1717 // Aussme protect2 is always locked, acctually it only unlocked at first boot in factory
1718 if (drv_status[0] == NVRAM_DRV_OK)
1719 {
1720 if (smu_is_write_protect2(ldi->LID)) {
1721 // only trigger backup for specific LIDs when they are written legally (shoudn't be frequently)
1722 // trigger backup, this will write sync pattern in AP side
1723 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(ldi->LID, 0xFFFF, 0x0001,__LINE__);
1724 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"LID:0x%04X write sync pattern to AP\r\n",ldi->LID);
1725 ccci_send_message(CCMSG_ID_SYSMSGSVC_MD_UNPROTECT_PART_REQ, 0xABC);
1726
1727 } else {
1728 // Don't trigger backup.
1729 // think about this scenario:
1730 // some LID will update at known time (every md bootup time)
1731 // this will leave sync pattern in AP if we trigger backup,
1732 // then hacker can deleted all files on protect1 before reboot the phone
1733 // the SML data on protect2 will lost after phone reboot ...
1734 }
1735 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1736 return NVRAM_IO_ERRNO_OK;
1737 }
1738 else
1739 {
1740 // don't trigger backup due to protect1 write failed
1741 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1742 return NVRAM_IO_ERRNO_CHK;
1743 }
1744 }
1745 #endif
1746 folder_index = nvram_query_folder_index_ex(ldi->category, KAL_FALSE);
1747 }
1748 #endif
1749
1750 drv_status[record_ID] = nvram_cache_drv_fat_write_multRec(
1751 ldi,
1752 nvramname,
1753 folder_index,
1754 file_offset,
1755 index,
1756 rec_mount,
1757 ldi->size,
1758 buffer,
1759 is_init);
1760
1761 /* Try to reset data if it is not a initial case */
1762 if (drv_status[record_ID] != NVRAM_DRV_OK)
1763 {
1764 MD_TRC_IO_WRITE_DATA_ITEM_MULTIPLE(ldi->LID, drv_status, nvram_drv_fat_get_last_err(), __LINE__);
1765 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP," drv_status[%d] =%d\r\n",record_ID,drv_status[record_ID]);
1766 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1767 status = NVRAM_IO_ERRNO_CHK;
1768 }
1769
1770 if (!multiple)
1771 {
1772 break;
1773 }
1774
1775 nvram_util_make_lid_filename(ldi, nvramname, KAL_FALSE);
1776
1777 }
1778
1779 if (status != NVRAM_IO_ERRNO_OK && multiple == KAL_TRUE)
1780 {
1781 kal_int32 recovery_status;
1782 if (drv_status[0] != NVRAM_DRV_OK && drv_status[1] == NVRAM_DRV_OK)
1783 {
1784 recovery_status = nvram_drv_fat_backup(ldi, KAL_FALSE); /* A <-- B */
1785 }
1786 else if (drv_status[0] == NVRAM_DRV_OK && drv_status[1] != NVRAM_DRV_OK)
1787 {
1788 recovery_status = nvram_drv_fat_backup(ldi, KAL_TRUE); /* A --> B */
1789 }
1790 else
1791 {
1792 // Both A&B write fail
1793 recovery_status = drv_status[0];
1794 }
1795
1796 if (recovery_status == NVRAM_DRV_OK)
1797 {
1798 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1799 return NVRAM_IO_ERRNO_OK;
1800 }
1801 }
1802 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1803 return status;
1804}
1805
1806/*****************************************************************************
1807 * FUNCTION
1808 * nvram_write_mutilpe_data_item
1809 * DESCRIPTION
1810 * write data with lid size but not a record.Support write 1..amount records
1811 * one time if (lid->size+checksum) * amount < MAX_NVRAM_RECORD_SIZE
1812 * PARAMETERS
1813 * ldi [IN] the data item to be write
1814 * rec_idnex [IN] the start record index will be write
1815 * rec_amount [IN] the amount of the records will be write
1816 * RETURNS
1817 * void
1818 *****************************************************************************/
1819static nvram_errno_enum nvram_cache_write_default_data_item_mutilpe(nvram_ltable_entry_struct *ldi,
1820 kal_uint16 rec_index,
1821 kal_uint16 rec_amount)
1822{
1823
1824 /*----------------------------------------------------------------*/
1825 /* Local Variables */
1826 /*----------------------------------------------------------------*/
1827 nvram_errno_enum status = NVRAM_IO_ERRNO_OK;
1828
1829 /*----------------------------------------------------------------*/
1830 /* Code Body */
1831 /*----------------------------------------------------------------*/
1832
1833 MD_TRC_IO_WRITE_DATA_ITEM_START(ldi->LID, rec_index, rec_amount);
1834 MD_TRC_INFO_NVRAM_DATA_ITEM(ldi->LID, ldi->size);
1835
1836 /******************************************************
1837 * Device Broken
1838 ******************************************************/
1839 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s ====>\r\n",__FUNCTION__);
1840 if (nvram_ptr->dev_broken)
1841 {
1842 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(ldi->LID, NVRAM_IO_ERRNO_DRV_BROKEN, __LINE__, ldi->LID);
1843 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s NVRAM_IO_ERRNO_DRV_BROKEN\r\n",__FUNCTION__);
1844 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1845 return status;
1846 }
1847
1848 /* prepare data */
1849 if( ldi->default_value == NVRAM_EF_FF_DEFAULT ||
1850 ldi->default_value == NVRAM_EF_ZERO_DEFAULT)
1851 {
1852 kal_uint8 const *default_value = ldi->default_value;
1853 status = nvram_cache_write_fs_data_item_multiple(ldi,rec_index,rec_amount,default_value,KAL_TRUE);
1854 }
1855 else if(ldi->default_value == NULL)
1856 {
1857 kal_uint8 const *default_value = NVRAM_EF_FF_DEFAULT;
1858 status = nvram_cache_write_fs_data_item_multiple(ldi,rec_index,rec_amount,default_value,KAL_TRUE);
1859 }
1860 else
1861 {
1862 kal_uint8 *default_value_buffer = NULL;
1863 kal_uint32 i;
1864 kal_uint8 *default_value_p;
1865 kal_uint8 *def_buff;
1866
1867 default_value_buffer = (kal_uint8*) get_ctrl_buffer(ldi->size * rec_amount + NVRAM_BUFFER_TAIL_SIZE);
1868 kal_mem_cpy((default_value_buffer + (ldi->size * rec_amount)), NVRAM_BUFFER_TAIL_MARK, NVRAM_BUFFER_TAIL_SIZE);
1869
1870 if((ldi->category & NVRAM_CATEGORY_FUNC_DEFAULT) && (ldi->attr & NVRAM_ATTR_MULTI_DEFAULT))
1871 {
1872 nvram_get_lid_default_value_to_write(ldi, 0, default_value_buffer, (ldi->size * rec_amount));
1873 }
1874 else
1875 {
1876 def_buff = (kal_uint8*) get_ctrl_buffer(ldi->size + NVRAM_BUFFER_TAIL_SIZE);
1877 kal_mem_cpy((def_buff + ldi->size), NVRAM_BUFFER_TAIL_MARK, NVRAM_BUFFER_TAIL_SIZE);
1878 for (i = 0; i < rec_amount; i++)
1879 {
1880 default_value_p = (kal_uint8*) nvram_get_lid_default_value_to_write(ldi, i, NULL, 0);
1881 if (default_value_p == NULL)
1882 {
1883 nvram_get_lid_default_value_to_write(ldi, i, def_buff, ldi->size);
1884 default_value_p = def_buff;
1885 }
1886 kal_mem_cpy(default_value_buffer + i*(ldi->size), default_value_p, ldi->size);
1887 default_value_p = NULL;
1888 }
1889
1890 if(kal_mem_cmp((def_buff + ldi->size), NVRAM_BUFFER_TAIL_MARK, NVRAM_BUFFER_TAIL_SIZE) != 0)
1891 {
1892 // Return default value is out of buffer.
1893 MD_TRC_FUNC_NVRAM_RESET_DATA_ITEMS(ldi->LID, (ldi->size * rec_amount), __LINE__, 0,0);
1894 NVRAM_EXT_ASSERT(KAL_FALSE, (ldi->size * ldi->total_records), NVRAM_LOC_SPACE_NOT_ENOUGH_4, ldi->LID, free_ctrl_buffer(def_buff);free_ctrl_buffer(default_value_buffer));
1895 }
1896 free_ctrl_buffer(def_buff);
1897 }
1898
1899 if(kal_mem_cmp((default_value_buffer + (ldi->size * rec_amount)), NVRAM_BUFFER_TAIL_MARK, NVRAM_BUFFER_TAIL_SIZE) != 0)
1900 {
1901 // Return default value is out of buffer.
1902 MD_TRC_FUNC_NVRAM_RESET_DATA_ITEMS(ldi->LID, (ldi->size * rec_amount), __LINE__, 0,0);
1903 NVRAM_EXT_ASSERT(KAL_FALSE, (ldi->size * ldi->total_records), NVRAM_LOC_SPACE_NOT_ENOUGH_3, ldi->LID, free_ctrl_buffer(default_value_buffer));
1904 }
1905
1906 status = nvram_cache_write_fs_data_item_multiple(ldi,rec_index,rec_amount,default_value_buffer,KAL_TRUE);
1907 free_ctrl_buffer(default_value_buffer);
1908 }
1909
1910 if (status != NVRAM_IO_ERRNO_OK)
1911 {
1912 /* IMEI and SML */
1913 if ((NVRAM_IS_CATEGORY_IMPORTANT(ldi->category)
1914 #if (defined(__SMART_PHONE_MODEM__) || defined(__CCCIFS_SUPPORT__))
1915 #if defined(__MTK_TARGET__) && defined(__NVRAM_IMPORTANT_PARTITIONS__)
1916 || NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category))
1917 #else
1918 )
1919 #endif
1920 #else
1921 )
1922 #endif
1923 )
1924 {
1925 kal_prompt_trace(MOD_NVRAM, "NVRAM ASSERT ERROR NVRAM_ERROR_LOC_NVCACHE_WRITE_IMPORTANT_DATA_FAIL_1:%d\n\r", DISPLAY_ERROR(status));
1926 kal_prompt_trace(MOD_NVRAM, "LID:0x%x, total_records:%d, record_size:%d\n\r", ldi->LID, ldi->total_records, ldi->size);
1927 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
1928 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
1929
1930 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM ASSERT ERROR NVRAM_ERROR_LOC_NVCACHE_WRITE_IMPORTANT_DATA_FAIL_1:%d\r\n", DISPLAY_ERROR(status));
1931 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", ldi->LID, ldi->total_records, ldi->size);
1932 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
1933 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
1934 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1935 NVRAM_EXT_ASSERT (KAL_FALSE, DISPLAY_ERROR(status),NVRAM_ERROR_LOC_NVCACHE_WRITE_IMPORTANT_DATA_FAIL_1 , ldi->LID);
1936 return status;
1937 }
1938
1939 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(ldi->LID, status, __LINE__, KAL_TRUE);
1940 }
1941
1942 if (status != NVRAM_IO_ERRNO_OK)
1943 {
1944 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(ldi->LID, status, __LINE__, KAL_TRUE);
1945 kal_prompt_trace(MOD_NVRAM, "NVRAM ASSERT ERROR NVRAM_ERROR_LOC_NVCACGE_WRITE_AND_RESET_FAIL_1:%d\n\r", DISPLAY_ERROR(status));
1946 kal_prompt_trace(MOD_NVRAM, "LID:0x%x, total_records:%d, record_size:%d\n\r", ldi->LID, ldi->total_records, ldi->size);
1947 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
1948 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
1949
1950 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM_ERROR_LOC_NVCACGE_WRITE_AND_RESET_FAIL_1:%d\r\n", DISPLAY_ERROR(status));
1951 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", ldi->LID, ldi->total_records, ldi->size);
1952 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
1953 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
1954 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1955 NVRAM_EXT_ASSERT(KAL_FALSE, DISPLAY_ERROR(status), NVRAM_ERROR_LOC_NVCACGE_WRITE_AND_RESET_FAIL_1, ldi->LID);
1956 }
1957 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1958 return status;
1959}
1960#endif
1961/*****************************************************************************
1962 * FUNCTION
1963 * nvram_cache_reset_one_data_item
1964 * DESCRIPTION
1965 * reset one LID to default value
1966 * PARAMETERS
1967 * ldi [IN]
1968 * rec_index [IN]
1969 * rec_amount [IN]
1970 * RETURNS
1971 * success or fail
1972 *****************************************************************************/
1973nvram_errno_enum nvram_cache_reset_one_data_item(nvram_ltable_entry_struct* ldi, kal_uint16 rec_index, kal_uint16 rec_amount)
1974{
1975 kal_uint8 *default_value = NULL;
1976 kal_uint32 i;
1977 kal_uint32 start = rec_index;
1978 kal_uint32 end = rec_amount;
1979 kal_uint8 *default_value_buffer = NULL;
1980 #if defined(__MTK_TARGET__) && defined(__NVRAM_WRITE_WITH_FILE_SIZE__) && !defined(__NVRAM_WRITE_PROTECT_ENABLE__)
1981 kal_uint32 remainLen = 0;
1982 #endif
1983 kal_uint32 working_buffer_size = 0;
1984 nvram_errno_enum result = NVRAM_IO_ERRNO_OK;
1985 kal_uint32 nvram_chksum_size = 0;
1986 nvram_lid_chksum_info lid_chksum_info = {0};
1987
1988 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s ====>\r\n",__FUNCTION__);
1989
1990 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
1991 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
1992
1993 #if defined(__MTK_TARGET__) && defined(__NVRAM_WRITE_WITH_FILE_SIZE__) && !defined(__NVRAM_WRITE_PROTECT_ENABLE__)
1994 do
1995 {
1996 if (KAL_FALSE == bResetNvramData
1997 || KAL_FALSE == kal_query_systemInit() //initiation
1998 || (ldi->size + nvram_chksum_size) > MAX_NVRAM_RECORD_SIZE //large record
1999 || ldi->LID == NVRAM_EF_SYS_LID
2000 #ifdef __NVRAM_OTP__
2001 || NVRAM_IS_CATEGORY_OTP(ldi->category)
2002 #endif
2003 #ifdef __NVRAM_CUSTOM_DISK__
2004 || NVRAM_IS_CATEGORY_CUSTOM_DISK(ldi->category)
2005 #endif
2006 #ifdef __NVRAM_CRYPT_TEST__
2007 || ldi->LID == NVRAM_EF_NVRAM_MSP_TEST_LID
2008 #endif
2009 )
2010 {
2011 break;
2012 }
2013
2014 // only support reset 1..total_records
2015
2016 #ifdef __NVRAM_BIND_TO_CHIP_CIPHER__
2017 if (ldi->attr & NVRAM_ATTR_MSP)
2018 {
2019 /* 4 byte alignment */
2020 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(ldi->size + nvram_chksum_size);
2021 }
2022 #endif
2023
2024 working_buffer_size = (ldi->size + nvram_chksum_size + remainLen) * ldi->total_records;
2025
2026 if (working_buffer_size <= MAX_NVRAM_RECORD_SIZE)
2027 {
2028 result = nvram_cache_write_default_data_item_mutilpe(ldi,1,ldi->total_records);
2029 goto final;
2030 }
2031
2032 }while(0);
2033 #endif
2034 working_buffer_size = MAX_NVRAM_RECORD_SIZE;
2035 default_value_buffer = (kal_uint8*) get_ctrl_buffer(working_buffer_size);
2036 //nvram_debug_write_dump(ldi, 0x701, rec_index, rec_amount, NULL, ldi->size, NVRAM_IO_ERRNO_CHK);
2037
2038 nvram_util_take_mutex(g_nvram_fs_mutex);
2039 nvram_cache_reset_header(ldi, LDI_HEADER_ALL_SECTION);
2040 nvram_util_give_mutex(g_nvram_fs_mutex);
2041
2042 for (i = start; i <= end; i++)
2043 {
2044 /* when ldi size too big, we cannot operator it on buffer,
2045 use original default value pointer directly */
2046
2047 default_value = (kal_uint8*) nvram_get_default_value_to_write(ldi, i, NULL, 0);
2048
2049 /* the default_value is not assigned in table , ex: L1 LID */
2050 if (default_value == NULL)
2051 {
2052 if((i == 1 && end == ldi->total_records) && (NVRAM_EF_SYS_LID != ldi->LID))
2053 {
2054 nvram_get_default_value_to_write(ldi, i, default_value_buffer, working_buffer_size);
2055 }
2056 else
2057 {
2058 nvram_memset(default_value_buffer, (kal_uint8) NVRAM_EF_ZERO_DEFAULT_VALUE, working_buffer_size);
2059 nvram_get_default_value_to_write(ldi, i, default_value_buffer, ldi->size);
2060 }
2061
2062 default_value = default_value_buffer;
2063 }
2064
2065 /* initial case, we reset it directly to enhance performance*/
2066 if(NVRAM_EF_SYS_LID == ldi->LID)
2067 {
2068 result = nvram_cache_write_data_item(ldi, i, default_value, KAL_TRUE);
2069 if (result != NVRAM_IO_ERRNO_OK)
2070 {
2071 break;
2072 }
2073 }
2074 else
2075 {
2076 result = nvram_cache_write_data_item(ldi, ldi->total_records, default_value, KAL_TRUE);
2077 break;
2078 }
2079 }
2080#if defined(__MTK_TARGET__) && defined(__NVRAM_WRITE_WITH_FILE_SIZE__) && !defined(__NVRAM_WRITE_PROTECT_ENABLE__)
2081final:
2082#endif
2083 if(default_value_buffer)
2084 {
2085 free_ctrl_buffer(default_value_buffer);
2086 default_value_buffer = NULL;
2087 }
2088
2089 if(result == NVRAM_IO_ERRNO_OK) {
2090
2091 unmask_valid_bit_by_ltable_entry(ldi, 0, (ldi->total_records + 1));
2092 }
2093 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
2094 return result;
2095}
2096
2097/*****************************************************************************
2098 * FUNCTION
2099 * update_cache_header
2100 * DESCRIPTION
2101 * update cache data for read
2102 * PARAMETERS
2103 * ldi [IN]
2104 * rec_index [IN]
2105 * rec_amount [IN]
2106 * working_buffer [IN]
2107 * RETURNS
2108 * success or fail
2109 *****************************************************************************/
2110nvram_errno_enum update_cache_header(nvram_ltable_entry_struct* ldi, void* src_buffer, kal_uint32 ldi_hd_offset, kal_uint32 ldi_hd_buffer_size)
2111{
2112 nvram_lid_cache_table_struct *cache_ldi = NULL;
2113 kal_bool result = KAL_FALSE;
2114
2115 if (!(result = get_lid_cache_index_item(ldi->LID , &cache_ldi))) {
2116 kal_prompt_trace(MOD_NVRAM, "[%s]search lid failed from cache table index: 0x%x\n\r", __FUNCTION__, ldi->LID);
2117 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
2118 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
2119
2120 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error][%s]search lid failed from cache table\r\n", __FUNCTION__);
2121 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"LID 0x%04X category:0x%08X, attr:0x%08X\r\n",ldi->LID,ldi->category, ldi->attr);
2122 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
2123 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s <====\r\n",__FUNCTION__);
2124 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_INVALID_LID_6, ldi->LID);
2125 return NVRAM_IO_ERRNO_INVALID_LID;
2126 }
2127
2128 nvram_util_take_mutex(g_nvram_cache_mutex);
2129 kal_mem_cpy((void *)(g_nvcache_base_address + ((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + ldi_hd_offset)), src_buffer, ldi_hd_buffer_size);
2130 nvram_util_give_mutex(g_nvram_cache_mutex);
2131
2132 return NVRAM_IO_ERRNO_OK;
2133}
2134
2135
2136/*****************************************************************************
2137 * FUNCTION
2138 * update_cache_data
2139 * DESCRIPTION
2140 * update cache data for read
2141 * PARAMETERS
2142 * ldi [IN]
2143 * rec_index [IN]
2144 * rec_amount [IN]
2145 * working_buffer [IN]
2146 * RETURNS
2147 * success or fail
2148 *****************************************************************************/
2149nvram_errno_enum update_cache_data(nvram_ltable_entry_struct* ldi, kal_uint16 rec_index, kal_uint16 rec_amount, NVRAM_FS_PARAM_CMPT_T* nvram_param, kal_bool is_only_chksum)
2150{
2151 nvram_lid_cache_table_struct *cache_ldi = NULL;
2152 kal_bool result = KAL_FALSE;
2153 kal_uint8* cache_data_boundary = NULL;
2154 kal_uint32 remainLen = 0;
2155 kal_uint32 section_size;
2156 kal_uint16 temp_rec_index = rec_index;
2157 kal_uint16 i, j;
2158 kal_uint8* temp_dataPtr = (kal_uint8 *)(nvram_param->DataPtr);
2159 kal_bool total_valid_bit = KAL_TRUE;
2160 kal_uint32 nvram_chksum_size = 0;
2161 nvram_lid_chksum_info lid_chksum_info = {0};
2162
2163 if (!(result = get_lid_cache_index_item(ldi->LID , &cache_ldi))) {
2164 kal_prompt_trace(MOD_NVRAM, "[%s]search lid failed from cache table index: 0x%x\n\r", __FUNCTION__, ldi->LID);
2165 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
2166 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
2167
2168 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error][%s]search lid failed from cache table\n\r", __FUNCTION__);
2169 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"LID 0x%04X,category:0x%08X,attr:0x%08X\r\n",ldi->LID,ldi->category, ldi->attr);
2170 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
2171 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_INVALID_LID_7, ldi->LID);
2172 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s <====\r\n",__FUNCTION__);
2173 return NVRAM_IO_ERRNO_INVALID_LID;
2174 }
2175
2176 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
2177 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
2178
2179 #ifdef __NVRAM_BIND_TO_CHIP_CIPHER__
2180 if ((ldi->attr & NVRAM_ATTR_MSP)||(ldi->attr & NVRAM_ATTR_CONFIDENTIAL))
2181 {
2182 /* 16 byte alignment */
2183 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(ldi->size + nvram_chksum_size);
2184 }
2185 #else
2186 if (ldi->attr & NVRAM_ATTR_CONFIDENTIAL)
2187 {
2188 /* 16 byte alignment */
2189 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(ldi->size + nvram_chksum_size);
2190 }
2191 #endif
2192 section_size = ldi->size+ nvram_chksum_size + remainLen;
2193
2194 nvram_util_take_mutex(g_nvram_cache_mutex);
2195 cache_data_boundary = g_nvcache_base_address + cache_info_header.cache_table_offset + cache_info_header.cache_table_size;
2196 if((g_nvcache_base_address + cache_info_header.cache_table_offset + cache_ldi->cache_offset + nvram_param->Offset + (*(nvram_param->Read))) > cache_data_boundary)
2197 {
2198 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)(g_nvcache_base_address + (cache_info_header.cache_table_offset + cache_ldi->cache_offset) + nvram_param->Offset + (*(nvram_param->Read))), NVRAM_ERROR_LOC_NVCACHE_ERRNO_BIT_BOUNDARY_9, cache_ldi->LID);
2199 }
2200 nvram_util_give_mutex(g_nvram_cache_mutex);
2201 for(i = 0; i < rec_amount; i++)
2202 {
2203 if(is_only_chksum)
2204 {
2205 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE)
2206 {
2207 total_valid_bit = KAL_FALSE;
2208 nvram_util_take_mutex(g_nvram_cache_mutex);
2209 for(j = 0; j < nvram_chksum_size; j++)
2210 {
2211 if(*((kal_uint8*)(g_nvcache_base_address + ((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + (nvram_param->Offset + (i* nvram_chksum_size)+ j)))) != 0)
2212 {
2213 total_valid_bit = KAL_TRUE;
2214 break;
2215 }
2216 }
2217 nvram_util_give_mutex(g_nvram_cache_mutex);
2218 if (check_dirty_bit_by_cache_table(cache_ldi, temp_rec_index + i) && total_valid_bit)
2219 {
2220 nvram_util_take_mutex(g_nvram_cache_mutex);
2221 kal_mem_cpy((void *)(temp_dataPtr + (i* nvram_chksum_size)),
2222 (void *)(g_nvcache_base_address + ((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + (nvram_param->Offset + (i* nvram_chksum_size)))), nvram_chksum_size);
2223 nvram_util_give_mutex(g_nvram_cache_mutex);
2224 }else if(!check_valid_bit_by_cache_table(cache_ldi, temp_rec_index + i) &&(total_valid_bit == KAL_FALSE))
2225 {
2226 nvram_util_take_mutex(g_nvram_cache_mutex);
2227 kal_mem_cpy((void *)(g_nvcache_base_address + ((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + (nvram_param->Offset + (i* nvram_chksum_size)))),
2228 (void *)(temp_dataPtr + (i* nvram_chksum_size)), nvram_chksum_size);
2229 nvram_util_give_mutex(g_nvram_cache_mutex);
2230 }
2231 }else
2232 {
2233 nvram_util_take_mutex(g_nvram_cache_mutex);
2234 kal_mem_cpy((void *)(g_nvcache_base_address + ((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + (nvram_param->Offset + (i* section_size)))),
2235 (void *)(temp_dataPtr + (i* nvram_chksum_size)), nvram_chksum_size);
2236 nvram_util_give_mutex(g_nvram_cache_mutex);
2237 }
2238 }else
2239 {
2240 if (check_dirty_bit_by_cache_table(cache_ldi, temp_rec_index + i))
2241 {
2242 nvram_util_take_mutex(g_nvram_cache_mutex);
2243 kal_mem_cpy((void *)(temp_dataPtr + (i* section_size)), (void *)(g_nvcache_base_address + ((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + (nvram_param->Offset + (i*section_size )))), section_size);
2244 nvram_util_give_mutex(g_nvram_cache_mutex);
2245 }else if(!check_valid_bit_by_cache_table(cache_ldi, temp_rec_index + i))
2246 {
2247 nvram_util_take_mutex(g_nvram_cache_mutex);
2248 kal_mem_cpy((void *)(g_nvcache_base_address + ((cache_info_header.cache_table_offset + cache_ldi->cache_offset) + (nvram_param->Offset + (i* section_size)))), (void *)(temp_dataPtr + (i* section_size)), section_size);
2249 nvram_util_give_mutex(g_nvram_cache_mutex);
2250 mask_valid_bit_by_cache_table(cache_ldi, temp_rec_index + i, 1);
2251 }
2252 }
2253 }
2254
2255 return NVRAM_IO_ERRNO_OK;
2256}
2257
2258/*****************************************************************************
2259 * FUNCTION
2260 * get_cache_data
2261 * DESCRIPTION
2262 * update cache data for read
2263 * PARAMETERS
2264 * ldi [IN]
2265 * rec_index [IN]
2266 * rec_amount [IN]
2267 * working_buffer [IN]
2268 * RETURNS
2269 * success or fail
2270 *****************************************************************************/
2271nvram_errno_enum get_cache_data(kal_uint32 cache_address, void *dest_buffer, kal_uint32 working_buffer_size)
2272{
2273 kal_uint8* src_buffer = (kal_uint8*)cache_address;
2274
2275 nvram_util_take_mutex(g_nvram_cache_mutex);
2276 kal_mem_set(dest_buffer,0,working_buffer_size);
2277 kal_mem_cpy(dest_buffer, (void *)src_buffer, working_buffer_size);
2278 nvram_util_give_mutex(g_nvram_cache_mutex);
2279
2280 return NVRAM_IO_ERRNO_OK;
2281}
2282
2283/*****************************************************************************
2284 * FUNCTION
2285 * nvram_flush_cache_data_to_file
2286 * DESCRIPTION
2287 * nvram flush cache data to file
2288 * PARAMETERS
2289 * ldi [IN]
2290 * rec_index [IN]
2291 * rec_amount [IN]
2292 * working_buffer [IN]
2293 * RETURNS
2294 * success or fail
2295 *****************************************************************************/
2296nvram_errno_enum nvram_flush_cache_data_to_file(nvram_cache_write_item *cache_queue_ldi)
2297{
2298 /*----------------------------------------------------------------*/
2299 /* Local Variables */
2300 /*----------------------------------------------------------------*/
2301 kal_uint32 len = 0;
2302 kal_uint32 section_size = 0;
2303 kal_uint32 working_buffer_size = 0;
2304 kal_uint8 *working_buffer = NULL;
2305 kal_int32 result = NVRAM_DRV_OK;
2306 nvram_drv_status_enum drv_status[2] = {NVRAM_DRV_OK, NVRAM_DRV_OK};
2307 kal_bool ret_val = KAL_FALSE;
2308 kal_uint32 i, j;
2309 kal_uint32 rec_size;
2310 kal_uint16 rec_amount;
2311 kal_uint32 openOption = FS_READ_WRITE | FS_OPEN_NO_DIR;
2312 nvram_lid_cache_table_struct *cache_ldi = NULL;
2313 kal_uint32 start_record = 0;
2314 kal_uint32 end_record = 0;
2315 kal_int32 flush_length = 0;
2316 kal_int32 flush_append_length = 0;
2317 kal_uint32 file_offset = 0;
2318 kal_uint32 file_append_offset = 0;
2319 kal_uint32 temp_offset = 0;
2320 kal_uint32 remainLen = 0;
2321 kal_bool multiple = KAL_FALSE;
2322 kal_uint32 multiple_ID = 0;
2323
2324 FS_HANDLE hFile = 0;
2325 kal_wchar filename[NVRAM_MAX_PATH_LEN];
2326 kal_wchar src_path[NVRAM_MAX_PATH_LEN];
2327 kal_wchar dest_path[NVRAM_MAX_PATH_LEN];
2328 NVRAM_FILE_NAME nvramname;
2329 nvram_folder_enum nvram_folder;
2330 kal_uint32 file_sz = 0;
2331 kal_int32 ret = NVRAM_DRV_OK;
2332 kal_uint32 nvram_chksum_size = 0;
2333 nvram_lid_chksum_info lid_chksum_info = {0};
2334
2335 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s ====>\r\n",__FUNCTION__);
2336 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"LID 0x%04X cache_queue_ldi->rec_index=%d cache_queue_ldi->rec_amount=%d\r\n",cache_queue_ldi->ldi->LID,cache_queue_ldi->rec_index,cache_queue_ldi->rec_amount);
2337 rec_size = cache_queue_ldi->ldi->size;
2338 rec_amount = cache_queue_ldi->ldi->total_records;
2339 openOption = cache_queue_ldi->openoption;
2340
2341 kal_mem_set(filename, 0x0, NVRAM_MAX_PATH_LEN * sizeof(kal_wchar));
2342
2343 working_buffer = (kal_uint8*) get_ctrl_buffer(MD_CCCI_LIMIT_SIZE);
2344 kal_mem_set(working_buffer, 0x0, MD_CCCI_LIMIT_SIZE);
2345
2346 nvram_get_lid_chksum_algo_info(cache_queue_ldi->ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
2347 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
2348
2349 #ifdef __NVRAM_BIND_TO_CHIP_CIPHER__
2350 if ((cache_queue_ldi->ldi->attr & NVRAM_ATTR_MSP)||(cache_queue_ldi->ldi->attr & NVRAM_ATTR_CONFIDENTIAL))
2351 {
2352 /* 16 byte alignment */
2353 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(rec_size + nvram_chksum_size);
2354 }
2355 #else
2356 if (cache_queue_ldi->ldi->attr & NVRAM_ATTR_CONFIDENTIAL)
2357 {
2358 /* 16 byte alignment */
2359 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(rec_size + nvram_chksum_size);
2360 }
2361 #endif
2362
2363 section_size = rec_size + nvram_chksum_size + remainLen;
2364
2365 if (!(ret_val = get_lid_cache_index_item(cache_queue_ldi->ldi->LID , &cache_ldi))) {
2366 if (working_buffer)
2367 {
2368 free_ctrl_buffer(working_buffer);
2369 working_buffer = NULL;
2370 }
2371 kal_prompt_trace(MOD_NVRAM, "[%s]search lid failed from cache table index: 0x%x\n\r", __FUNCTION__, cache_queue_ldi->ldi->LID);
2372 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", cache_queue_ldi->ldi->category, cache_queue_ldi->ldi->attr);
2373 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", cache_queue_ldi->ldi->fileprefix, cache_queue_ldi->ldi->fileverno);
2374
2375 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s search LID 0x%04X failed from cache table index:\r\n",__FUNCTION__,cache_queue_ldi->ldi->LID);
2376 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"category:0x%08X, attr:0x%08X \r\n", cache_queue_ldi->ldi->category, cache_queue_ldi->ldi->attr);
2377 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s, fileverno:%s\n\r", cache_queue_ldi->ldi->fileprefix, cache_queue_ldi->ldi->fileverno);
2378 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_INVALID_LID_8, cache_queue_ldi->ldi->LID);
2379 result = NVRAM_IO_ERRNO_INVALID_LID;
2380 nvram_cache_last_line = __LINE__;
2381 goto final;
2382 }
2383 if (NVRAM_IS_ATTR_MULTIPLE(cache_queue_ldi->ldi->attr) || NVRAM_IS_ATTR_BACKUP_FAT(cache_queue_ldi->ldi->attr) || NVRAM_IS_CATEGORY_IMPORTANT_L4(cache_queue_ldi->ldi->category))
2384 {
2385 multiple = KAL_TRUE;
2386 }
2387
2388 nvram_util_take_mutex(g_nvram_fs_mutex);
2389
2390 nvram_folder = nvram_query_folder_index(cache_queue_ldi->ldi->category);
2391 for (multiple_ID = 0; multiple_ID < 2; multiple_ID++)
2392 {
2393
2394 #if defined(__MTK_TARGET__) && defined(__NVRAM_IMPORTANT_PARTITIONS__) && defined(__CCCIFS_SUPPORT__)
2395 if (multiple_ID == 1)
2396 {
2397 #ifdef __NVRAM_WRITE_PROTECT_ENABLE__
2398 extern kal_bool smu_is_write_protect2(nvram_lid_enum file_idx);
2399 if (NVRAM_IS_CATEGORY_IMPORTANT_L4(cache_queue_ldi->ldi->category))
2400 {
2401 // Aussme protect2 is always locked, acctually it only unlocked at first boot in factory
2402 if (drv_status[0] == NVRAM_DRV_OK)
2403 {
2404 if (smu_is_write_protect2(cache_queue_ldi->ldi->LID)) {
2405 // only trigger backup for specific LIDs when they are written legally (shoudn't be frequently)
2406 // trigger backup, this will write sync pattern in AP side
2407 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(cache_queue_ldi->ldi->LID, 0xFFFF, 0x0001,__LINE__);
2408 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"LID:0x%04X write sync pattern to AP\r\n",cache_queue_ldi->ldi->LID);
2409 ccci_send_message(CCMSG_ID_SYSMSGSVC_MD_UNPROTECT_PART_REQ, 0xABC);
2410
2411 } else {
2412 // Don't trigger backup.
2413 // think about this scenario:
2414 // some LID will update at known time (every md bootup time)
2415 // this will leave sync pattern in AP if we trigger backup,
2416 // then hacker can deleted all files on protect1 before reboot the phone
2417 // the SML data on protect2 will lost after phone reboot ...
2418 }
2419 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
2420 return NVRAM_IO_ERRNO_OK;
2421 }
2422 else
2423 {
2424 // don't trigger backup due to protect1 write failed
2425 return NVRAM_IO_ERRNO_CHK;
2426 }
2427 }
2428 #endif
2429 nvram_folder = nvram_query_folder_index_ex(cache_queue_ldi->ldi->category, KAL_FALSE);
2430 }
2431 #endif
2432
2433 if (multiple_ID == 0)
2434 {
2435 nvram_util_make_lid_filename(cache_queue_ldi->ldi, nvramname, KAL_TRUE);
2436 }else
2437 {
2438 nvram_util_make_lid_filename(cache_queue_ldi->ldi, nvramname, KAL_FALSE);
2439 }
2440
2441 nvram_query_file_name(nvram_folder, nvramname, filename);
2442 NVRAM_FS_START_EX(FS_OP_OPEN,filename);
2443 hFile = FS_Open((const kal_wchar*)filename, openOption);
2444 NVRAM_FS_END(FS_OP_OPEN,hFile);
2445 if (hFile == FS_FILE_NOT_FOUND) {
2446 drv_status[multiple_ID] = hFile;
2447 nvram_cache_last_line = __LINE__;
2448 goto FS_OP_ERROR;
2449 } else if (hFile <= FS_NO_ERROR) {
2450 drv_status[multiple_ID] = hFile;
2451 nvram_cache_last_line = __LINE__;
2452 goto FS_OP_ERROR;
2453 }
2454 NVRAM_FS_START(FS_OP_GETFILESIZE);
2455 result = FS_GetFileSize(hFile, &file_sz);
2456 NVRAM_FS_END(FS_OP_GETFILESIZE,result);
2457 if((cache_ldi->is_reset != 1) && (file_sz < NVRAM_LDI_HEADER_SIZE))
2458 {
2459 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s->FS_GetFileSize fail at %d,result=%d file_sz =%d\r\n",__FUNCTION__,__LINE__,result,file_sz);
2460 drv_status[multiple_ID] = FS_FILE_NOT_FOUND;
2461 nvram_cache_last_line = __LINE__;
2462 goto FS_OP_ERROR;
2463 }
2464
2465 for (i = 0; i <= rec_amount; i ++) {
2466 if (check_dirty_bit_by_cache_table(cache_ldi, i)) {
2467 start_record = i;
2468 if(i == 0) {
2469 flush_length += NVRAM_LDI_HEADER_SIZE;
2470 if (cache_queue_ldi->ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
2471 flush_append_length += NVRAM_LDI_APPENDIX_HEADER_SIZE;
2472 }
2473 }else {
2474 flush_length += section_size;
2475 if (cache_queue_ldi->ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
2476 flush_append_length += nvram_chksum_size;
2477 }
2478 }
2479 if(i < rec_amount)
2480 {
2481 for (j = i+1; j <= rec_amount; j++)
2482 {
2483 if (check_dirty_bit_by_cache_table(cache_ldi, j)) {
2484 flush_length += section_size;
2485 if (cache_queue_ldi->ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
2486 flush_append_length += nvram_chksum_size;
2487 }
2488 if(j == rec_amount) {
2489 end_record = j;
2490 i = j;
2491 break;
2492 }
2493 }else {
2494 end_record = j-1;
2495 i = j-1;
2496 break;
2497 }
2498 }
2499 }
2500 else
2501 {
2502 end_record = i;
2503 }
2504
2505 if (start_record== 0) {
2506
2507 file_offset = 0;
2508 if (cache_queue_ldi->ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
2509 file_append_offset = NVRAM_LDI_HEADER_SIZE + (section_size * rec_amount) ;
2510 }
2511 }else{
2512
2513 file_offset = NVRAM_LDI_HEADER_SIZE + (start_record-1) * section_size;
2514 if (cache_queue_ldi->ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
2515 file_append_offset = NVRAM_LDI_HEADER_SIZE + (section_size * rec_amount) + NVRAM_LDI_APPENDIX_HEADER_SIZE +((start_record-1) * nvram_chksum_size);
2516 }
2517 }
2518 if (file_offset) {
2519 NVRAM_FS_START(FS_OP_SEEK);
2520 drv_status[multiple_ID] = FS_Seek(hFile, file_offset, FS_FILE_BEGIN);
2521 NVRAM_FS_END(FS_OP_SEEK,drv_status[multiple_ID]);
2522 if (FS_NO_ERROR > drv_status[multiple_ID]){
2523 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Seek fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,drv_status[multiple_ID]);
2524 nvram_cache_last_line = __LINE__;
2525 goto FS_OP_ERROR;
2526 }
2527 }
2528
2529 temp_offset = (kal_uint32)((g_nvcache_base_address + (cache_info_header.cache_table_offset + cache_ldi->cache_offset)) + file_offset);
2530 len = 0;
2531 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"LID 0x%04X start_record=%d end_record=%d\r\n",cache_queue_ldi->ldi->LID,start_record,end_record);
2532 while(flush_length > 0) {
2533 if(flush_length > MD_CCCI_LIMIT_SIZE) {
2534 working_buffer_size = MD_CCCI_LIMIT_SIZE;
2535 }else{
2536 working_buffer_size = flush_length;
2537 }
2538
2539 get_cache_data(temp_offset, working_buffer, working_buffer_size);
2540 NVRAM_FS_START(FS_OP_WRITE);
2541 drv_status[multiple_ID] = FS_Write(hFile, working_buffer, working_buffer_size, &len);
2542 NVRAM_FS_END(FS_OP_WRITE,drv_status[multiple_ID]);
2543
2544 if (FS_NO_ERROR > drv_status[multiple_ID]) {
2545 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Write fail at %d,drv_status[%d]=%d\r\n",__FUNCTION__,__LINE__,multiple_ID,drv_status[multiple_ID]);
2546 nvram_cache_last_line = __LINE__;
2547 goto FS_OP_ERROR;
2548 }
2549
2550 temp_offset = temp_offset + len;
2551 flush_length = flush_length - len;
2552 }
2553
2554 if (cache_queue_ldi->ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE)
2555 {
2556 working_buffer_size = flush_append_length;
2557 NVRAM_FS_START(FS_OP_SEEK);
2558 drv_status[multiple_ID] = FS_Seek(hFile, file_append_offset, FS_FILE_BEGIN);
2559 NVRAM_FS_END(FS_OP_SEEK,drv_status[multiple_ID]);
2560 if (file_append_offset && FS_NO_ERROR > drv_status[multiple_ID]) {
2561 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Seek fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
2562 nvram_cache_last_line = __LINE__;
2563 goto FS_OP_ERROR;
2564 }
2565
2566 temp_offset = (kal_uint32)((g_nvcache_base_address + (cache_info_header.cache_table_offset + cache_ldi->cache_offset)) + file_append_offset);
2567 len = 0;
2568
2569 while(flush_append_length > 0) {
2570
2571 if(flush_append_length > MD_CCCI_LIMIT_SIZE) {
2572
2573 working_buffer_size = MD_CCCI_LIMIT_SIZE;
2574 }else{
2575
2576 working_buffer_size = flush_append_length;
2577 }
2578
2579 get_cache_data(temp_offset, working_buffer, working_buffer_size);
2580 NVRAM_FS_START(FS_OP_WRITE);
2581 drv_status[multiple_ID] = FS_Write(hFile, working_buffer, working_buffer_size, &len);
2582 NVRAM_FS_END(FS_OP_WRITE,drv_status[multiple_ID]);
2583 if (FS_NO_ERROR > drv_status[multiple_ID]) {
2584 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Write fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
2585 nvram_cache_last_line = __LINE__;
2586 goto FS_OP_ERROR;
2587 }
2588
2589 temp_offset = temp_offset + len;
2590 flush_append_length = flush_append_length - len;
2591
2592 }
2593 }
2594
2595 unmask_dirty_bit_by_cache_table(cache_ldi, start_record, (end_record - start_record)+1);
2596 mask_valid_bit_by_cache_table(cache_ldi, start_record, (end_record - start_record)+1);
2597 }
2598
2599 }
2600
2601FS_OP_ERROR:
2602
2603 if(hFile > FS_NO_ERROR)
2604 {
2605 NVRAM_FS_START(FS_OP_CLOSE);
2606 ret = FS_Close(hFile);
2607 NVRAM_FS_END(FS_OP_CLOSE,ret);
2608 }
2609
2610 if(drv_status[multiple_ID] < FS_NO_ERROR)
2611 {
2612 if(!multiple)
2613 {
2614 result = drv_status[multiple_ID];
2615 goto final;
2616 }else
2617 {
2618 if(multiple_ID == 1)
2619 {
2620 result = drv_status[multiple_ID];
2621 goto final;
2622 }
2623 }
2624 }else
2625 {
2626 if(multiple)
2627 {
2628 kal_mem_set(src_path, 0x0, NVRAM_MAX_PATH_LEN * sizeof(kal_wchar));
2629 kal_mem_set(dest_path, 0x0, NVRAM_MAX_PATH_LEN * sizeof(kal_wchar));
2630
2631 if(multiple_ID == 0)
2632 {
2633 nvram_util_make_lid_filename(cache_queue_ldi->ldi, nvramname, KAL_TRUE); // A
2634 nvram_query_file_name(nvram_query_folder_index_ex(cache_queue_ldi->ldi->category, KAL_TRUE), nvramname, src_path);
2635
2636 nvram_util_make_lid_filename(cache_queue_ldi->ldi, nvramname, KAL_FALSE); // B
2637 nvram_query_file_name(nvram_query_folder_index_ex(cache_queue_ldi->ldi->category, KAL_FALSE), nvramname, dest_path);
2638 NVRAM_FS_START_EX(FS_OP_MOVE, dest_path);
2639 ret = FS_Move(src_path, dest_path, FS_MOVE_COPY, NULL, NULL, 0); //A to B
2640 NVRAM_FS_END(FS_OP_MOVE,ret);
2641 }else
2642 {
2643 nvram_util_make_lid_filename(cache_queue_ldi->ldi, nvramname, KAL_FALSE); // B
2644 nvram_query_file_name(nvram_query_folder_index_ex(cache_queue_ldi->ldi->category, KAL_FALSE), nvramname, src_path);
2645
2646 nvram_util_make_lid_filename(cache_queue_ldi->ldi, nvramname, KAL_TRUE); // A
2647 nvram_query_file_name(nvram_query_folder_index_ex(cache_queue_ldi->ldi->category, KAL_TRUE), nvramname, dest_path);
2648
2649 NVRAM_FS_START_EX(FS_OP_MOVE, dest_path);
2650 ret = FS_Move(src_path, dest_path, FS_MOVE_COPY, NULL, NULL, 0); //B to A
2651 NVRAM_FS_END(FS_OP_MOVE,ret);
2652 }
2653 result = drv_status[multiple_ID];
2654 goto final;
2655
2656 }else
2657 {
2658 result = drv_status[multiple_ID];
2659 goto final;
2660 }
2661
2662 }
2663
2664 }
2665
2666final:
2667
2668 nvram_util_give_mutex(g_nvram_fs_mutex);
2669
2670 if (working_buffer)
2671 {
2672 free_ctrl_buffer(working_buffer);
2673 }
2674 if (FS_NO_ERROR > result)
2675 {
2676 nvram_cache_last_err = result;
2677 MD_TRC_FUNC_NVRAM_DRV_FAT_THROW_EXCEPTION(nvram_cache_last_err, nvram_cache_last_line);
2678 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"Failed at %d,result=%d\r\n",__LINE__,result);
2679 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
2680 return NVRAM_ERRNO_FAIL;
2681 }
2682
2683 if(cache_ldi->is_reset == 1)
2684 {
2685 cache_ldi->is_reset = 0;
2686 }
2687 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
2688 return NVRAM_IO_ERRNO_OK;
2689}
2690
2691#ifdef __NVRAM_UT_TEST__
2692static kal_bool check_cache_lid_all_record_is_undirty_bit(nvram_ltable_entry_struct* ldi)
2693{
2694 kal_uint8 i;
2695 for(i = 1; i <= ldi->total_records; i++)
2696 {
2697 if(check_dirty_bit_by_ltable_entry(ldi,i)!= KAL_FALSE)
2698 {
2699 return KAL_FALSE;
2700 }
2701 }
2702 return KAL_TRUE;
2703}
2704#endif
2705
2706/*****************************************************************************
2707 * FUNCTION
2708 * nvram_flush_cache_handler
2709 * DESCRIPTION
2710 * nvram flush cache handler
2711 * PARAMETERS
2712 * ldi [IN]
2713 * rec_index [IN]
2714 * rec_amount [IN]
2715 * working_buffer [IN]
2716 * RETURNS
2717 * success or fail
2718 *****************************************************************************/
2719kal_bool nvram_flush_cache_handler(void)
2720{
2721 /*----------------------------------------------------------------*/
2722 /* Local Variables */
2723 /*----------------------------------------------------------------*/
2724 nvram_cache_write_item cache_queue_ldi;
2725 nvram_errno_enum status = NVRAM_IO_ERRNO_OK;
2726 nvram_ltable_entry_struct tmp_ldi;
2727 FS_HANDLE hFile = 0;
2728 kal_uint8 *filename = NULL;
2729 mcf_ota_result_e mcf_ota_ret = MCF_OTA_R_SUCCESS;
2730 kal_int32 result = FS_NO_ERROR;
2731 #if defined(__MTK_TARGET__)
2732 kal_uint32 temp_queue_length = 0;
2733 ostd_ap_core_status_enum temp_ostd_status = OSTD_AP_CORE_UNKNOWN;
2734 #endif
2735
2736 kal_mem_set(&cache_queue_ldi, 0x0, sizeof(nvram_cache_write_item));
2737
2738 do {
2739
2740 #if defined(__MTK_TARGET__)
2741 if((temp_queue_length = nvram_cache_queue_usage_rates()) < CACHE_QUEUE_BOUNDARY_SIZE)
2742 {
2743 if((temp_ostd_status = OSTD_return_AP_status()) == OSTD_AP_CORE_SUSPEND)
2744 {
2745 kal_prompt_trace(MOD_NVRAM, "queue_length:%d, ostd_status:%d\n\r", temp_queue_length, temp_ostd_status);
2746 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(temp_queue_length, temp_ostd_status, __LINE__);
2747 break;
2748 }
2749 }
2750
2751 kal_prompt_trace(MOD_NVRAM, "queue_length:%d, ostd_status:%d\n\r", temp_queue_length, temp_ostd_status);
2752 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(temp_queue_length, temp_ostd_status, __LINE__);
2753 #endif
2754
2755 if (!nvram_cache_dequeue(&cache_queue_ldi)) {
2756
2757 break;
2758 }
2759
2760 #ifdef __NVRAM_UT_TEST__
2761 if(check_cache_lid_all_record_is_undirty_bit(cache_queue_ldi.ldi))
2762 {
2763 continue;
2764 }
2765 if (NVRAM_FLUSH_CACHE_CHECK(cache_queue_ldi.ldi->LID))
2766 {
2767 kal_prompt_trace(MOD_NVRAM, "%s @%d NVCACHE flush bypass whilte list LID=%d\n\r",__FUNCTION__,__LINE__ , cache_queue_ldi.ldi->LID);
2768 NVRAM_EXT_ASSERT(KAL_FALSE, __LINE__, NVRAM_LOC_INVALID_LID_5, cache_queue_ldi.ldi->LID);
2769 }
2770 #endif
2771 status = nvram_flush_cache_data_to_file(&cache_queue_ldi);
2772
2773 /*
2774 If write fail and the LID is located in NVRAM folder's subfolder,
2775 first check the root folder is OK or not.
2776 */
2777
2778 if(status != NVRAM_IO_ERRNO_OK)
2779 {
2780 if(NVRAM_IS_CATEGORY_INTERNAL(cache_queue_ldi.ldi->category) || NVRAM_IS_CATEGORY_CALIBRAT(cache_queue_ldi.ldi->category) ||
2781 NVRAM_IS_CATEGORY_IMPORTANT(cache_queue_ldi.ldi->category) || (NVRAM_NVD_DATA == nvram_query_folder_index(cache_queue_ldi.ldi->category)))
2782 {
2783 filename = (kal_uint8 *) get_ctrl_buffer(NVRAM_MAX_PATH_LEN * sizeof(kal_wchar));
2784 NVRAM_FS_MAKE_ROOT_PATH((kal_wchar*)filename);
2785 nvram_util_take_mutex(g_nvram_fs_mutex);
2786 NVRAM_FS_START_EX(FS_OP_OPEN,filename);
2787 hFile = FS_Open((const kal_wchar*)filename, FS_READ_ONLY | FS_OPEN_DIR);
2788 NVRAM_FS_END(FS_OP_OPEN,hFile);
2789 free_ctrl_buffer(filename);
2790 filename = NULL;
2791 if(hFile >= FS_NO_ERROR) /* Open NVRAM root folder Success */
2792 {
2793 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"%s->FS_Open Success at %d,hFile=%d\r\n",__FUNCTION__,__LINE__,hFile);
2794 NVRAM_FS_START(FS_OP_CLOSE);
2795 result = FS_Close(hFile);
2796 NVRAM_FS_END(FS_OP_CLOSE,result);
2797 }
2798 else if (hFile == FS_FILE_NOT_FOUND || hFile == FS_PATH_NOT_FOUND)
2799 {
2800 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s NVRAM main folder lost\r\n",__FUNCTION__);
2801 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s NVRAM_ERROR_LOC_NVCACHE_OPEN_NV_FOLDER_FAIL_1\r\n",__FUNCTION__);
2802 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)hFile, NVRAM_ERROR_LOC_NVCACHE_OPEN_NV_FOLDER_FAIL_1, 0);
2803 }
2804 else
2805 {
2806 kal_prompt_trace(MOD_NVRAM, "Open NVRAM root folder result:%d\n\r", (kal_uint32)hFile);
2807 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"Open NVRAM root folder result:%d\r\n",(kal_uint32)hFile);
2808 }
2809 nvram_util_give_mutex(g_nvram_fs_mutex);
2810 }
2811 }
2812
2813 memcpy(&tmp_ldi,cache_queue_ldi.ldi,sizeof(nvram_ltable_entry_struct));
2814 if (status != NVRAM_IO_ERRNO_OK)
2815 {
2816#if (defined(__CCCIFS_SUPPORT__) && defined(__MTK_TARGET__)) || defined(__NVRAM_BIN_REGION_SIMULATION__)
2817 if (NVRAM_IS_CATEGORY_IN_BIN_REGION(cache_queue_ldi.ldi->category))
2818 {
2819 // try restore from bin region
2820 if (nvram_recover_data_item(cache_queue_ldi.ldi) == NVRAM_IO_ERRNO_OK)
2821 {
2822 if(nvram_ptr->state == NVRAM_STATE_READY)
2823 {
2824 mcf_ota_ret = mcf_do_ota_by_lid(cache_queue_ldi.ldi->LID,1, cache_queue_ldi.ldi->total_records,&tmp_ldi);
2825 kal_prompt_trace(MOD_NVRAM, "%s @%d mcr_ota_ret=%d\n\r",__FUNCTION__,__LINE__ , mcf_ota_ret);
2826 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"mcf_do_ota_by_lid return %d\r\n",mcf_ota_ret);
2827 }
2828 status = nvram_flush_cache_data_to_file(&cache_queue_ldi);
2829 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"nvram_flush_cache_data_to_file return %d\r\n",status);
2830 if (status == NVRAM_IO_ERRNO_OK)
2831 {
2832 goto end;
2833 }
2834 }
2835 }
2836#endif
2837
2838 /* IMEI and SML */
2839 if (NVRAM_IS_ATTR_FAULT_ASSERT(cache_queue_ldi.ldi->attr))
2840 {
2841 kal_prompt_trace(MOD_NVRAM, "NVRAM ASSERT ERROR NVRAM_ERROR_LOC_NVCACHE_WRITE_IMPORTANT_DATA_FAIL_2:%d\n\r", DISPLAY_ERROR(status));
2842 kal_prompt_trace(MOD_NVRAM, "LID:0x%x, total_records:%d, record_size:%d\n\r", cache_queue_ldi.ldi->LID, cache_queue_ldi.ldi->total_records, cache_queue_ldi.ldi->size);
2843 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", cache_queue_ldi.ldi->category, cache_queue_ldi.ldi->attr);
2844 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", cache_queue_ldi.ldi->fileprefix, cache_queue_ldi.ldi->fileverno);
2845 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM_ERROR_LOC_NVCACHE_WRITE_IMPORTANT_DATA_FAIL_2:%d\r\n", DISPLAY_ERROR(status));
2846 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", cache_queue_ldi.ldi->LID, cache_queue_ldi.ldi->total_records, cache_queue_ldi.ldi->size);
2847 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", cache_queue_ldi.ldi->category, cache_queue_ldi.ldi->attr);
2848 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", cache_queue_ldi.ldi->fileprefix, cache_queue_ldi.ldi->fileverno);
2849 NVRAM_EXT_ASSERT (KAL_FALSE, DISPLAY_ERROR(status),NVRAM_ERROR_LOC_NVCACHE_WRITE_IMPORTANT_DATA_FAIL_2 , cache_queue_ldi.ldi->LID);
2850 return status;
2851 }
2852
2853
2854 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(cache_queue_ldi.ldi->LID, status, __LINE__);
2855
2856 if (KAL_TRUE
2857 #ifdef __NVRAM_CUSTOM_DISK__
2858 && !NVRAM_IS_CATEGORY_CUSTOM_DISK(cache_queue_ldi.ldi->category)
2859 #endif
2860 #ifdef __NVRAM_OTP__
2861 && !NVRAM_IS_CATEGORY_OTP(cache_queue_ldi.ldi->category)
2862 #endif
2863 )
2864 {
2865 /* Try to reset data if it is not a initial case */
2866 if ((status = nvram_cache_reset_one_data_item(cache_queue_ldi.ldi, 1, cache_queue_ldi.ldi->total_records)) == NVRAM_IO_ERRNO_OK)
2867 {
2868 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(cache_queue_ldi.ldi->LID, status, __LINE__);
2869 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s->nvram_cache_reset_one_data_item successfully\r\n",__FUNCTION__);
2870 if(nvram_ptr->state == NVRAM_STATE_READY)
2871 {
2872 mcf_ota_ret = mcf_do_ota_by_lid(cache_queue_ldi.ldi->LID,1, cache_queue_ldi.ldi->total_records,&tmp_ldi);
2873 kal_prompt_trace(MOD_NVRAM, "%s @%d mcr_ota_ret=%d\n\r",__FUNCTION__,__LINE__ , mcf_ota_ret);
2874 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s->mcf_do_ota_by_lid return %d\r\n",__FUNCTION__,mcf_ota_ret);
2875 }
2876 status = nvram_flush_cache_data_to_file(&cache_queue_ldi);
2877 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"nvram_flush_cache_data_to_file after reset status:%d\r\n",status);
2878
2879 }
2880 //nvram_trace(TRACE_FUNC, IO_WRITE_DATA_ITEM_RESULT, ldi->LID, status, __LINE__, is_init);
2881 }
2882 }
2883
2884 if (status != NVRAM_IO_ERRNO_OK)
2885 {
2886 MD_TRC_IO_WRITE_DATA_ITEM_RESULT(cache_queue_ldi.ldi->LID, status, __LINE__);
2887 kal_prompt_trace(MOD_NVRAM, "NVRAM ASSERT ERROR NVRAM_ERROR_LOC_NVCACGE_WRITE_AND_RESET_FAIL_2:%d\n\r", DISPLAY_ERROR(status));
2888 kal_prompt_trace(MOD_NVRAM, "LID:0x%x, total_records:%d, record_size:%d\n\r", cache_queue_ldi.ldi->LID, cache_queue_ldi.ldi->total_records, cache_queue_ldi.ldi->size);
2889 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", cache_queue_ldi.ldi->category, cache_queue_ldi.ldi->attr);
2890 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", cache_queue_ldi.ldi->fileprefix, cache_queue_ldi.ldi->fileverno);
2891
2892 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM_ERROR_LOC_NVCACGE_WRITE_AND_RESET_FAIL_2:%d\r\n", DISPLAY_ERROR(status));
2893 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", cache_queue_ldi.ldi->LID, cache_queue_ldi.ldi->total_records, cache_queue_ldi.ldi->size);
2894 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", cache_queue_ldi.ldi->category, cache_queue_ldi.ldi->attr);
2895 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", cache_queue_ldi.ldi->fileprefix, cache_queue_ldi.ldi->fileverno);
2896 NVRAM_EXT_ASSERT(KAL_FALSE, DISPLAY_ERROR(status), NVRAM_ERROR_LOC_NVCACGE_WRITE_AND_RESET_FAIL_2, cache_queue_ldi.ldi->LID);
2897 }
2898#if (defined(__CCCIFS_SUPPORT__) && defined(__MTK_TARGET__)) || defined(__NVRAM_BIN_REGION_SIMULATION__)
2899 end:
2900#endif
2901#if defined(__NVRAM_CREATE_FILE_ON_WRITE__)
2902 if (status == NVRAM_IO_ERRNO_OK)
2903 {
2904 nvram_util_post_write_lid(cache_queue_ldi.ldi);
2905 }
2906#endif
2907 } while(1);
2908
2909 return KAL_TRUE;
2910}
2911
2912/*****************************************************************************
2913 * FUNCTION
2914 * mark_cache_task_ready
2915 * DESCRIPTION
2916 * mark NVRAM cache task ready
2917 * PARAMETERS
2918 * flag [IN]
2919 * RETURNS
2920 * success or fail
2921 *****************************************************************************/
2922kal_bool mark_nvram_cache_ready(void)
2923{
2924 g_nvram_cache_ready = KAL_TRUE;
2925 return KAL_TRUE;
2926}
2927
2928/*****************************************************************************
2929 * FUNCTION
2930 * unmark_nvram_cache_task_ready
2931 * DESCRIPTION
2932 * unmark NVRAM cache task ready
2933 * PARAMETERS
2934 * flag [IN]
2935 * RETURNS
2936 * success or fail
2937 *****************************************************************************/
2938kal_bool unmark_nvram_cache_ready(void)
2939{
2940 g_nvram_cache_ready = KAL_FALSE;
2941
2942 return KAL_TRUE;
2943}
2944
2945
2946/*****************************************************************************
2947 * FUNCTION
2948 * check_nvram_cache_task_ready
2949 * DESCRIPTION
2950 * check NVRAM cache task whether ready
2951 * PARAMETERS
2952 * void
2953 * RETURNS
2954 * TRUE or FALSE
2955 *****************************************************************************/
2956kal_bool check_nvram_cache_ready(void)
2957{
2958 kal_bool ready = KAL_FALSE;
2959 ready = ((g_nvram_cache_ready? KAL_TRUE : KAL_FALSE) && (KAL_FALSE == kal_query_systemInit())
2960 && (kal_query_boot_mode() != FACTORY_BOOT) && (stack_get_active_module_id() != MOD_FT));
2961 return ready;
2962}
2963
2964/*****************************************************************************
2965 * FUNCTION
2966 * check_nvram_cache_initialized
2967 * DESCRIPTION
2968 * check NVRAM cache task whether initialized
2969 * PARAMETERS
2970 * void
2971 * RETURNS
2972 * TRUE or FALSE
2973 *****************************************************************************/
2974kal_bool check_nvram_cache_initialized(void)
2975{
2976 kal_bool initialzied = KAL_FALSE;
2977 initialzied = ((g_nvram_cache_ready? KAL_TRUE : KAL_FALSE) && (kal_query_boot_mode() != FACTORY_BOOT) && (stack_get_active_module_id() != MOD_FT));
2978 return initialzied;
2979}
2980
2981#if ((!defined(__MTK_TARGET__)) && (!defined(__UE_SIMULATOR__)))
2982
2983static void nvcache_assgn_ltable(void)
2984{
2985 kal_uint32 i, offset = 0;
2986 nvram_ltable_entry_struct *ltable_entry = NULL;
2987 nvram_ltable_entry_struct *the_start = &_nvram_ltable_start;
2988 kal_uint32 start_addr = (kal_uint32)the_start, end_addr = (kal_uint32)&the_nvram_ltable_end;
2989
2990 #if defined(_MSC_VER) && !defined(L1_SIM)
2991 //skip session gap
2992 for (offset = 0; offset < 0x1000; offset += 32) // __declspec(align(32))
2993 {
2994 ltable_entry = (nvram_ltable_entry_struct*)((kal_uint32)&the_start[0] + offset);
2995 if(ltable_entry->fileprefix[0]) {
2996 break;
2997 }
2998 }
2999 #endif
3000
3001 for (i = 0;(kal_uint32)ltable_entry <= end_addr; i++)
3002 {
3003 ltable_entry = (nvram_ltable_entry_struct*)((kal_uint32)&the_start[i] + offset);
3004
3005 if (assgn_logical_data_item_table == NULL && ltable_entry->fileprefix[0])
3006 {
3007 assgn_logical_data_item_table = ltable_entry;
3008 assgn_ltable.table = &assgn_logical_data_item_table;
3009 }
3010
3011 if (ltable_entry->fileprefix[0])
3012 {
3013 //printf("%03d: ltable[%d]:%x filename = %s\n", nvram_ptr->ltable.total_LID, ltable_entry->LID, ltable_entry, ltable_entry->fileprefix);
3014 assgn_ltable.total_LID++;
3015 }
3016 else
3017 {
3018 ltable_entry = (nvram_ltable_entry_struct*)((kal_uint32)&the_start[i] + offset + 32);
3019 if (ltable_entry->fileprefix[0])
3020 {
3021 //printf("%03d: ltable[%d]:%x filename = %s\n", nvram_ptr->ltable.total_LID, ltable_entry->LID, ltable_entry, ltable_entry->fileprefix);
3022 assgn_ltable.total_LID++;
3023 }
3024 }
3025 }
3026
3027 assgn_ltable.area_size = (kal_uint32)ltable_entry - (kal_uint32)assgn_logical_data_item_table;
3028
3029}
3030
3031kal_bool nvcache_util_get_data_item(nvram_ltable_entry_struct **ldi, nvram_lid_enum LID)
3032{
3033 /*----------------------------------------------------------------*/
3034 /* Local Variables */
3035 /*----------------------------------------------------------------*/
3036 kal_uint32 i = 0, count = 0;
3037 kal_uint32 end = (kal_uint32)assgn_logical_data_item_table + assgn_ltable.area_size;
3038
3039 do
3040 {
3041 if (assgn_logical_data_item_table[i].fileprefix[0])
3042 {
3043 count++;
3044
3045 if (assgn_logical_data_item_table[i].LID == LID)
3046 {
3047 if (ldi) {
3048 *ldi = &assgn_logical_data_item_table[i];
3049 }
3050 return KAL_TRUE;
3051 }
3052 }
3053 i++;
3054 }while(count < assgn_ltable.total_LID && ((kal_uint32)&assgn_logical_data_item_table[i] < end));
3055
3056 if (ldi)
3057 *ldi = NULL;
3058
3059 return KAL_FALSE;
3060}
3061
3062#endif
3063
3064/*****************************************************************************
3065 * FUNCTION
3066 * nvram_cache_task_main
3067 * DESCRIPTION
3068 * NVRAM cache task
3069 * PARAMETERS
3070 * task_entry_ptr [?]
3071 * RETURNS
3072 * void
3073 *****************************************************************************/
3074void nvram_cache_main(task_entry_struct *task_entry_ptr)
3075{
3076
3077 kal_uint32 count = 0; // for debg, CVD var error, can remove
3078 //kal_uint32 retrieved_events = 0;
3079 kal_status status;
3080 if(g_nvram_cache_SHM_support == KAL_TRUE)
3081 {
3082 //kal_set_active_module_id();
3083 while(1) {
3084 count ++;
3085 //wait for some events, then do something.
3086 if((status = nvram_cache_retrieve_event()) == KAL_SUCCESS) {
3087 nvram_flush_cache_handler();
3088 }else {
3089 //NVRAM_EXT_ASSERT(KAL_FALSE, DISPLAY_ERROR(status),1,1,1);
3090 break;
3091 }
3092 /**do your own things*/
3093 }
3094 }
3095}
3096
3097/*****************************************************************************
3098 * FUNCTION
3099 * nvram_cache_task_init
3100 * DESCRIPTION
3101 * This is init() function of NVRAM cache task of NVRAM module.
3102 * This function initialize all the context variables required for NVRAM cache task of NVRAM module
3103 * PARAMETERS
3104 * task_indx [IN]
3105 * RETURNS
3106 * True if succeed.
3107 *****************************************************************************/
3108kal_bool nvram_cache_init(void)
3109{
3110 #if defined(__HIF_CCCI_SUPPORT__)
3111 CCCI_RUNTIME_FEATURE_SUPPORT_T feature_support = {0};
3112 CCCI_RUNTIME_SHARE_MEMORY_FORMAT_T shm = {0};
3113 #endif
3114
3115 #if (!defined(__MTK_TARGET__)) && (!defined(__UE_SIMULATOR__))
3116 kal_uint32 temp_offset = 0;
3117 kal_int32 i;
3118 nvram_ltable_entry_struct *ldi = NULL;
3119 kal_uint32 remainLen = 0;
3120 kal_uint32 nvram_chksum_size = 0;
3121 #ifdef __NVRAM_UT_TEST__
3122 extern nvram_checksum_config NVRAM_CHK_CONFIG;
3123 #else
3124 extern const nvram_checksum_config NVRAM_CHK_CONFIG;
3125 #endif
3126
3127 nvcache_assgn_ltable();
3128
3129 for(i = 0; i < cache_info_header.cache_lid_num; i++)
3130 {
3131 remainLen = 0;
3132 if(nvcache_util_get_data_item(&ldi, cache_info_table[i].LID))
3133 {
3134 #ifdef __NV_CHKSUM_ENHANCE__
3135 if(NVRAM_IS_ATTR_CHKSUM_ENHANC_ALGRTHM(ldi->attr))
3136 {
3137 if(NVRAM_MD5 == NVRAM_CHK_CONFIG.enhance_algo_type)
3138 {
3139 nvram_chksum_size = MD5_CHKSUM_LENGTH_8;
3140 }else
3141 {
3142 nvram_chksum_size = NVRAM_CHK_CONFIG.enhance_algo_size;
3143 }
3144 }else
3145 {
3146 if(NVRAM_MD5 == NVRAM_CHK_CONFIG.default_algo_type)
3147 {
3148 nvram_chksum_size = MD5_CHKSUM_LENGTH_8;
3149 }else
3150 {
3151 nvram_chksum_size = NVRAM_CHK_CONFIG.default_algo_size;
3152 }
3153 }
3154 #else
3155 nvram_chksum_size = MD5_CHKSUM_LENGTH_8;
3156 #endif
3157
3158 #ifdef __NVRAM_BIND_TO_CHIP_CIPHER__
3159 if ((ldi->attr & NVRAM_ATTR_MSP)||(ldi->attr & NVRAM_ATTR_CONFIDENTIAL))
3160 {
3161 /* 16 byte alignment */
3162 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(ldi->size + nvram_chksum_size);
3163 }
3164 #else
3165 if (ldi->attr & NVRAM_ATTR_CONFIDENTIAL)
3166 {
3167 /* 16 byte alignment */
3168 remainLen = NVRAM_MSP_ALIGNMENT_REMAINDER(ldi->size + nvram_chksum_size);
3169 }
3170 #endif
3171 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE)
3172 {
3173 cache_info_table[i].file_length = nvram_cache_appendix_header_offset(ldi) + NVRAM_LDI_APPENDIX_HEADER_SIZE + (ldi->total_records * nvram_chksum_size);
3174 }else
3175 {
3176 cache_info_table[i].file_length = NVRAM_LDI_HEADER_SIZE + ((ldi->size + nvram_chksum_size + remainLen) * ldi->total_records);
3177 }
3178 cache_info_table[i].cache_offset = temp_offset;
3179 temp_offset = ((cache_info_table[i].file_length + temp_offset + (4 - 1)) & (~(4-1)));
3180 }
3181 else
3182 {
3183 cache_info_table[i].cache_offset = temp_offset;
3184 temp_offset = ((cache_info_table[i].file_length + temp_offset + (4 - 1)) & (~(4-1)));
3185 //return KAL_FALSE;
3186 }
3187 }
3188 cache_info_header.cache_table_size = temp_offset;
3189 #endif
3190
3191 #if defined(__MTK_TARGET__)
3192
3193 #if defined(__HIF_CCCI_SUPPORT__)
3194 feature_support = ccci_runtime_data_query(AP_CCCI_RUNTIME_NVRAM_CACHE_SHARE_MEMORY, &shm, sizeof(CCCI_RUNTIME_SHARE_MEMORY_FORMAT_T));
3195 if(feature_support.support_mask == CCCI_RUNTIME_FEATURE_MUST_SUPPORT)
3196 {
3197 g_nvcache_base_address = (kal_uint8 *)(shm.addr);
3198 g_nvcache_memory_size = shm.size;
3199 if ((cache_info_header.cache_table_offset + cache_info_header.cache_table_size) >= shm.size)
3200 {
3201 g_nvram_cache_SHM_support = KAL_FALSE;
3202 MD_TRC_FUNC_NVRAM_DRV_FAT_THROW_EXCEPTION(shm.addr, shm.size);
3203 NVRAM_EXT_ASSERT (KAL_FALSE, shm.addr, shm.size, (cache_info_header.cache_table_offset + cache_info_header.cache_table_size));
3204 }
3205 kal_mem_set((void *)g_nvcache_base_address,0,shm.size);
3206 MD_TRC_FUNC_NVRAM_DRV_FAT_THROW_EXCEPTION(shm.addr, shm.size);
3207 }else
3208 {
3209 g_nvram_cache_SHM_support = KAL_FALSE;
3210 unmark_nvram_cache_ready();
3211 NVRAM_EXT_ASSERT (KAL_FALSE, feature_support.support_mask, NVRAM_ERROR_LOC_NVCACHE_ERRNO_SHM_GET_FAILED , __LINE__);
3212 return KAL_TRUE;
3213 }
3214 #else
3215 g_nvram_cache_SHM_support = KAL_FALSE;
3216 unmark_nvram_cache_ready();
3217 return KAL_TRUE;
3218 #endif
3219 kal_mem_cpy(g_nvcache_base_address, &cache_info_header,sizeof(nvram_lid_cache_header));
3220 kal_mem_cpy((g_nvcache_base_address + cache_info_header.table_index_offset), cache_info_table, cache_info_header.table_index_size);
3221
3222 #elif (!defined(__UE_SIMULATOR__))
3223 g_nvcache_base_address =(kal_uint8 *) malloc(CACHE_SHARE_MEMORY_SIZE);
3224 if(g_nvcache_base_address == NULL)
3225 {
3226 g_nvram_cache_SHM_support = KAL_FALSE;
3227 unmark_nvram_cache_ready();
3228 NVRAM_EXT_ASSERT (KAL_FALSE, KAL_FALSE, NVRAM_ERROR_LOC_NVCACHE_ERRNO_SHM_MALLOC_FAILED , __LINE__);
3229 return KAL_TRUE;
3230 }
3231 kal_mem_set((void *)g_nvcache_base_address,0,CACHE_SHARE_MEMORY_SIZE);
3232 kal_mem_cpy(g_nvcache_base_address, &cache_info_header,sizeof(nvram_lid_cache_header));
3233 kal_mem_cpy((g_nvcache_base_address + cache_info_header.table_index_offset), cache_info_table, cache_info_header.table_index_size);
3234 #else
3235 g_nvram_cache_SHM_support = KAL_FALSE;
3236 unmark_nvram_cache_ready();
3237 return KAL_TRUE;
3238 #endif
3239
3240 g_nvram_cache_SHM_support = KAL_TRUE;
3241 g_nvram_cache_mutex = kal_create_mutex("NV_CACHE");
3242 nvram_cache_event_init();
3243 nvram_cache_queue_init();
3244
3245 mark_nvram_cache_ready();
3246
3247 return KAL_TRUE;
3248}
3249
3250/*****************************************************************************
3251 * FUNCTION
3252 * nvram_cache_task_reset
3253 * DESCRIPTION
3254 * This is reset() function of NVRAM cache task of NVRAM module.
3255 * PARAMETERS
3256 * task_indx [IN]
3257 * RETURNS
3258 * void
3259 *****************************************************************************/
3260kal_bool nvram_cache_reset(void)
3261{
3262 /*----------------------------------------------------------------*/
3263 /* Local Variables */
3264 /*----------------------------------------------------------------*/
3265
3266 /*----------------------------------------------------------------*/
3267 /* Code Body */
3268 /*----------------------------------------------------------------*/
3269 g_nvram_cache_ready = KAL_FALSE;
3270 #if defined (__NVRAM_UT_TEST__)
3271 nvram_cache_queue_init();
3272
3273 kal_mem_set(g_nvcache_base_address + cache_info_header.dirty_mapping_offset,0,cache_info_header.dirty_mapping_size);
3274 kal_mem_set(g_nvcache_base_address + cache_info_header.valid_mapping_offset,0,cache_info_header.dirty_mapping_size);
3275 #endif
3276 return KAL_TRUE;
3277} /* end of nvram_reset function */
3278
3279/*****************************************************************************
3280 * FUNCTION
3281 * nvram_cache_task_create
3282 * DESCRIPTION
3283 * NVRAM cache task create function
3284 * PARAMETERS
3285 * handle [IN]
3286 * RETURNS
3287 * success or fail
3288 *****************************************************************************/
3289kal_bool nvram_cache_create(comptask_handler_struct **handle)
3290{
3291 /*----------------------------------------------------------------*/
3292 /* Local Variables */
3293 /*----------------------------------------------------------------*/
3294 static const comptask_handler_struct nvram_cache_task_handler_info =
3295 {
3296 nvram_cache_main, /* task entry function */
3297 nvram_cache_init, /* task initialization function */
3298 nvram_cache_reset /* task reset handler */
3299 };
3300
3301 /*----------------------------------------------------------------*/
3302 /* Code Body */
3303 /*----------------------------------------------------------------*/
3304 *handle = (comptask_handler_struct*) & nvram_cache_task_handler_info;
3305 return KAL_TRUE;
3306}
3307