blob: f54a24b30995b79fba4ed099f1a9b806a77ffe14 [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/*****************************************************************************
37 *
38 * Filename:
39 * ---------
40 * nvram_util.c
41 *
42 * Project:
43 * --------
44 * MAUI
45 *
46 * Description:
47 * ------------
48 * This is utility for NVRAM
49 *
50 * Author:
51 * -------
52 * -------
53 *
54 *============================================================================
55 * HISTORY
56 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
57 *------------------------------------------------------------------------------
58 * removed!
59 *
60 * removed!
61 * removed!
62 * removed!
63 *
64 * removed!
65 * removed!
66 * removed!
67 * removed!
68 *
69 * removed!
70 * removed!
71 * removed!
72 *
73 * removed!
74 * removed!
75 * removed!
76 * removed!
77 * removed!
78 * removed!
79 * removed!
80 *
81 * removed!
82 * removed!
83 * removed!
84 *
85 * removed!
86 * removed!
87 * removed!
88 * removed!
89 *
90 * removed!
91 * removed!
92 * removed!
93 * removed!
94 *
95 * removed!
96 * removed!
97 * removed!
98 * removed!
99 *
100 * removed!
101 * removed!
102 * removed!
103 * removed!
104 *
105 * removed!
106 * removed!
107 * removed!
108 *
109 * removed!
110 * removed!
111 * removed!
112 * removed!
113 * removed!
114 * removed!
115 * removed!
116 * removed!
117 * removed!
118 * removed!
119 * removed!
120 *
121 * removed!
122 * removed!
123 * removed!
124 * removed!
125 *
126 * removed!
127 * removed!
128 * removed!
129 * removed!
130 * removed!
131 * removed!
132 * removed!
133 * removed!
134 * removed!
135 * removed!
136 *
137 * removed!
138 * removed!
139 * removed!
140 * removed!
141 *
142 * removed!
143 * removed!
144 * removed!
145 * removed!
146 *
147 * removed!
148 * removed!
149 * removed!
150 * removed!
151 *
152 * removed!
153 * removed!
154 * removed!
155 * removed!
156 *
157 * removed!
158 * removed!
159 * removed!
160 * removed!
161 *
162 * removed!
163 * removed!
164 * removed!
165 * removed!
166 * removed!
167 * removed!
168 * removed!
169 * removed!
170 *
171 * removed!
172 * removed!
173 * removed!
174 *
175 * removed!
176 * removed!
177 *
178 * removed!
179 * removed!
180 * removed!
181 * removed!
182 *
183 * removed!
184 * removed!
185 *
186 * removed!
187 * removed!
188 * removed!
189 *
190 * removed!
191 * removed!
192 * removed!
193 * removed!
194 *
195 * removed!
196 * removed!
197 * removed!
198 * removed!
199 * removed!
200 *
201 * removed!
202 * removed!
203 * removed!
204 *
205 * removed!
206 * removed!
207 * removed!
208 *
209 * removed!
210 * removed!
211 * removed!
212 * removed!
213 *
214 * removed!
215 * removed!
216 * removed!
217 * removed!
218 * removed!
219 * removed!
220 *
221 * removed!
222 * removed!
223 * removed!
224 *
225 * removed!
226 * removed!
227 * removed!
228 *
229 * removed!
230 * removed!
231 * removed!
232 * removed!
233 *
234 * removed!
235 * removed!
236 *
237 * removed!
238 * removed!
239 * removed!
240 *
241 * removed!
242 * removed!
243 *
244 * removed!
245 * removed!
246 * removed!
247 *
248 * removed!
249 * removed!
250 * removed!
251 *
252 * removed!
253 * removed!
254 * removed!
255 *
256 * removed!
257 * removed!
258 * removed!
259 *
260 * removed!
261 * removed!
262 * removed!
263 *
264 * removed!
265 * removed!
266 * removed!
267 *
268 * removed!
269 * removed!
270 * removed!
271 *
272 * removed!
273 * removed!
274 * removed!
275 *
276 * removed!
277 * removed!
278 * removed!
279 *
280 * removed!
281 * removed!
282 * removed!
283 *
284 * removed!
285 * removed!
286 * removed!
287 *
288 * removed!
289 * removed!
290 * removed!
291 *
292 * removed!
293 * removed!
294 * removed!
295 *
296 * removed!
297 * removed!
298 * removed!
299 *
300 * removed!
301 * removed!
302 * removed!
303 *
304 * removed!
305 * removed!
306 * removed!
307 *
308 * removed!
309 * removed!
310 * removed!
311 *
312 * removed!
313 * removed!
314 * removed!
315 *
316 * removed!
317 * removed!
318 * removed!
319 *
320 * removed!
321 * removed!
322 * removed!
323 *
324 * removed!
325 * removed!
326 * removed!
327 *
328 * removed!
329 * removed!
330 * removed!
331 *
332 * removed!
333 * removed!
334 * removed!
335 *
336 * removed!
337 * removed!
338 *
339 *
340 * removed!
341 * removed!
342 *
343 *
344 * removed!
345 * removed!
346 *
347 *
348 * removed!
349 * removed!
350 *
351 *
352 * removed!
353 * removed!
354 *
355 *
356 * removed!
357 * removed!
358 *
359 *
360 * removed!
361 * removed!
362 *
363 *
364 * removed!
365 * removed!
366 *
367 *
368 * removed!
369 * removed!
370 *
371 *
372 * removed!
373 * removed!
374 *
375 *
376 * removed!
377 * removed!
378 *
379 *------------------------------------------------------------------------------
380 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
381 *============================================================================
382 ****************************************************************************/
383
384/*****************************************************************************
385* Include
386*****************************************************************************/
387#include "stdio.h"
388#include "string.h"
389#include "stdarg.h"
390#include "kal_general_types.h"
391#include "kal_internal_api.h" /* basename */
392#include "kal_public_api.h"
393#include "kal_trace.h"
394#include "kal_public_defs.h" //MSBB change #include "stack_config.h"
395#include "nvram_main.h"
396#include "che_api.h"
397#include "dcl.h"
398#include "che_api.h" // to calculate checksum
399#if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
400#include "dcl_gpt.h"
401#endif
402#include <ex_public.h>
403
404#ifdef __NVRAM_LID_CACHE__
405#include "nvram_cache_interface.h"
406#endif
407#include "us_timer.h"
408#include "stdlib.h"
409#include "ex_public.h"
410
411#if defined(__NVRAM_STRUCTURE_CHANGE_RESET__) || defined(__NVRAM_DEFVAL_CHANGE_RESET__)
412#if defined(__MTK_TARGET__) //target load
413#include "nvram_checksum_reset.h"
414#else //modis load define a dummy table to avoid build error
415const checksum_reset_struct lid_default_value_chksum[] ={
416 {NVRAM_EF_CHKSUM_RESET_REC_LID, {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}},
417};
418
419const checksum_reset_struct lid_structure_chksum[] ={
420 {NVRAM_EF_CHKSUM_RESET_REC_LID, {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}},
421};
422
423kal_uint32 lid_structure_chksum_num = sizeof(lid_structure_chksum)/sizeof(checksum_reset_struct);
424kal_uint32 lid_default_value_chksum_num = sizeof(lid_default_value_chksum)/sizeof(checksum_reset_struct);
425#endif
426#endif
427
428
429#ifdef __NVRAM_INIT_TIME_UT__
430kal_uint16 init_time_tick[10] = {0};
431nvram_init_flow_trace init_trace = 0;
432#endif
433#ifdef __NVRAM_INIT_LID_BUFFER__
434typedef enum
435{
436 NVRAM_INIT_LID_BUF_INVALID = 0,
437 NVRAM_INIT_LID_BUF_ENPTY,
438 NVRAM_INIT_LID_BUF_READ,
439 NVRAM_INIT_LID_BUF_VALID,
440 NVRAM_INIT_LID_BUF_DIRTY,
441} nvram_init_lid_buffer_status;
442
443typedef struct
444{
445 nvram_lid_enum LID;
446 nvram_ltable_entry_struct *ldi_ptr;
447 kal_uint8 *buffer;
448 nvram_init_lid_buffer_status status;
449} nvram_init_lid_buffer_struct;
450
451nvram_init_lid_buffer_struct lid_buffer_list[] = {
452 {NVRAM_EF_LTECSR_PROFILE_LID, NULL, NULL, 0},
453 {NVRAM_EF_IMS_PROFILE_LID, NULL, NULL, 0},
454 {NVRAM_EF_VDM_ADS_PROFILE_LID, NULL, NULL, 0},
455 {NVRAM_EF_SDM_ADS_PROFILE_LID, NULL, NULL, 0},
456 {NVRAM_EF_SBP_MODEM_DATA_CONFIG_LID, NULL, NULL, 0},
457 {NVRAM_EF_SBP_MODEM_CONFIG_LID, NULL, NULL, 0},
458 {NVRAM_EF_UMTS_USIME_RRC_DYNAMIC_CAP_FDD_LID, NULL, NULL, 0},
459 {NVRAM_EF_UMTS_USIME_RRC_DYNAMIC_CAP_TDD_LID, NULL, NULL, 0},
460 {NVRAM_EF_LTE_PREFERENCE_LID, NULL, NULL, 0},
461 {NVRAM_EF_REGIONAL_PHONE_MODE_LID, NULL, NULL, 0},
462 {NVRAM_EF_MSCAP_LID, NULL, NULL, 0},
463 {NVRAM_EF_ERRC_PERFORMANCE_PARA_LID, NULL, NULL, 0},
464 {NVRAM_EF_UE_EUTRA_CAP_CSFB_LID, NULL, NULL, 0},
465 {NVRAM_EF_NVRAM_SEC_CHECK_LID, NULL, NULL, 0},
466};
467kal_uint16 lid_buffer_list_num = sizeof(lid_buffer_list)/sizeof(nvram_init_lid_buffer_struct);
468kal_uint8 *nvram_init_lid_buffer = NULL;
469kal_bool nvram_init_lid_buffer_en = KAL_FALSE;
470#endif
471
472kal_int8 g_access_trace_setting_file_result = 0;
473kal_uint32 g_nvram_trace_setting= 0;
474#ifdef __NV_CHKSUM_ENHANCE__
475extern nvram_algo_info *chksum_algo_ptr;
476#endif
477
478extern void InitDebugPrint(void);
479extern void DebugPrint(kal_uint8, kal_uint8, kal_uint8*);
480
481#if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
482extern void nvram_gpt_timeout_callback(void *data);
483#endif
484
485
486#define NVRAM_TRC_SIZE 256
487
488/*
489 * External variables
490 */
491extern kal_mutexid g_nvram_impt_mutex;
492extern kal_mutexid g_nvram_fs_mutex;
493extern ilm_struct *g_ilm_ptr;
494extern kal_uint32 g_nvram_task_idx;
495extern kal_bool is_nvram_in_ota_flow;
496#if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
497extern DCL_HANDLE nvram_gpt_handle;
498#endif
499extern kal_bool is_nvram_factory_reset;
500
501
502extern nvram_ee_info_type* nvram_ee_info;
503extern kal_char nvram_trace_dump_temp_buffer[];
504extern kal_char nvram_trace_dump_buffer[];
505extern kal_mutexid g_nvram_dump_trace_mutex;
506extern kal_wchar nvram_trace_filename[];
507extern FS_HANDLE nvram_trace_file_hdl;
508extern kal_uint32 nvram_trace_dump_buffer_offset;
509
510extern module_type stack_get_active_module_id( void );
511
512static kal_uint8 g_util_chksum[128] = {0};
513
514
515#if defined(__NVRAM_STRUCTURE_CHANGE_RESET__) || defined(__NVRAM_DEFVAL_CHANGE_RESET__)
516kal_int32 nvram_get_structure_chksum_index(nvram_lid_enum LID)
517{
518 kal_int32 index = 0;
519
520 for(; index < lid_structure_chksum_num && LID != lid_structure_chksum[index].LID; index++)
521 {}
522
523 if(index >= lid_structure_chksum_num)
524 {
525 index = -1;
526 }
527
528 return index;
529}
530
531kal_int32 nvram_get_defval_chksum_index(nvram_lid_enum LID)
532{
533 kal_int32 index = 0;
534
535 for(; index < lid_default_value_chksum_num && LID != lid_default_value_chksum[index].LID; index++)
536 {}
537
538 if(index >= lid_default_value_chksum_num)
539 {
540 index = -1;
541 }
542
543 return index;
544}
545#endif
546
547
548kal_bool nvram_util_next_data_item(nvram_ltable_entry_struct **entry)
549{
550 /*----------------------------------------------------------------*/
551 /* Local Variables */
552 /*----------------------------------------------------------------*/
553 nvram_ltable_entry_struct *ldi = *entry;
554 kal_uint32 end = (kal_uint32)logical_data_item_table + nvram_ptr->ltable.area_size;
555 /*----------------------------------------------------------------*/
556 /* Code Body */
557 /*----------------------------------------------------------------*/
558 if (ldi == NULL)
559 {
560 ldi = &logical_data_item_table[0];
561 if (ldi->fileprefix[0])
562 {
563 *entry = ldi;
564 return KAL_TRUE;
565 }
566 }
567
568 while((kal_uint32)++ldi < end)
569 {
570 if (ldi->fileprefix[0])
571 {
572 *entry = ldi;
573 return KAL_TRUE;
574 }
575 }
576
577 *entry = NULL;
578 return KAL_FALSE;
579}
580
581
582kal_bool nvram_util_get_data_item(nvram_ltable_entry_struct **ldi, nvram_lid_enum LID)
583{
584 /*----------------------------------------------------------------*/
585 /* Local Variables */
586 /*----------------------------------------------------------------*/
587 kal_uint32 i = 0, count = 0;
588 kal_uint32 end = (kal_uint32)logical_data_item_table + nvram_ptr->ltable.area_size;
589
590 do
591 {
592 if (logical_data_item_table[i].fileprefix[0])
593 {
594 count++;
595
596 if (logical_data_item_table[i].LID == LID)
597 {
598 if (ldi) {
599 *ldi = &logical_data_item_table[i];
600 }
601 return KAL_TRUE;
602 }
603 }
604 i++;
605 }while(count < nvram_ptr->ltable.total_LID && ((kal_uint32)&logical_data_item_table[i] < end));
606
607 if (ldi)
608 *ldi = NULL;
609
610 return KAL_FALSE;
611}
612
613kal_bool nvram_util_get_data_item_by_fileprefix(nvram_ltable_entry_struct **ldi, kal_char *fileprefix)
614{
615 /*----------------------------------------------------------------*/
616 /* Local Variables */
617 /*----------------------------------------------------------------*/
618 kal_uint32 i = 0, count = 0;
619 kal_uint32 end = (kal_uint32)logical_data_item_table + nvram_ptr->ltable.area_size;
620
621 do
622 {
623 if (logical_data_item_table[i].fileprefix[0])
624 {
625 count++;
626 //if (!memcmp(logical_data_item_table[i].fileprefix, fileprefix, FILE_PREFIX_LEN))
627 //speed up search
628 if((kal_mem_cmp(fileprefix,logical_data_item_table[i].fileprefix,FILE_PREFIX_LEN) == 0) && (logical_data_item_table[i].LID != NVRAM_EF_READ_RESERVED_LID))
629 {
630 if (ldi) {
631 *ldi = &logical_data_item_table[i];
632 return KAL_TRUE;
633 }
634 }
635 }
636 i++;
637 }while(count < nvram_ptr->ltable.total_LID && ((kal_uint32)&logical_data_item_table[i] < end));
638
639 if (ldi)
640 *ldi = NULL;
641
642 return KAL_FALSE;
643}
644
645
646/*****************************************************************************
647 * FUNCTION
648 * nvram_drval_caculate_checksum
649 * DESCRIPTION
650 * produce the check sum of the given record.
651 * PARAMETERS
652 * checksum [?]
653 * buf [?]
654 * size [IN]
655 * RETURNS
656 * checksum(?)
657 *****************************************************************************/
658kal_uint16 nvram_util_cal_checksum_simple(const kal_uint8 *buf, kal_uint32 size)
659{
660 /*----------------------------------------------------------------*/
661 /* Local Variables */
662 /*----------------------------------------------------------------*/
663 kal_uint32 i;
664 kal_uint16 chksum = 0;
665 kal_uint8 *byte_chksum = (kal_uint8*) &chksum;
666
667 /*----------------------------------------------------------------*/
668 /* Code Body */
669 /*----------------------------------------------------------------*/
670
671 if (buf == NVRAM_EF_ZERO_DEFAULT)
672 {
673 return 0xCDEF;
674 }
675 else if (buf == NVRAM_EF_FF_DEFAULT)
676 {
677 for (i = 0 ;i < size; i++)
678 {
679 if (i & 0x1)
680 {
681 *(byte_chksum + 1) += 0xFF;
682 }
683 else
684 {
685 *byte_chksum += 0xFF;
686 }
687 }
688 return chksum;
689 }
690
691 for (i = 0; i < size; i++)
692 {
693 if (i & 0x1)
694 {
695 *(byte_chksum + 1) += *(buf + i);
696 }
697 else
698 {
699 *byte_chksum += *(buf + i);
700 }
701 }
702
703 //Replace conflict chksum
704 if (chksum == 0)
705 return 0xCDEF;
706 else if(chksum == 0xCDEF)
707 return 0xCEDF;
708 else
709 return chksum;
710}
711
712kal_bool nvram_util_zero_data_check(const kal_uint8 *buf, kal_uint32 size)
713{
714 while(size > 0) {
715 if((size < 7) || ((kal_uint32)buf & 0x3)) {
716 if(*buf != 0) {
717 return KAL_FALSE;
718 }
719 buf += 1;
720 size -= 1;
721 }
722 else {
723 if(*(kal_uint64*)buf != 0) {
724 return KAL_FALSE;
725 }
726 buf += 8;
727 size -= 8;
728 }
729 }
730 return KAL_TRUE;
731}
732
733kal_uint16 nvram_util_md5_checksum_convert(const kal_uint8 *checksum_buf_8B, kal_uint8 *checksum_buf_2B, kal_bool Check_CDEF)
734{
735 kal_uint8 i;
736 kal_uint64 const EmptyChksum = 0xCDEF;
737 kal_uint16 val = 0;
738 kal_uint16 *chk_ptr = (kal_uint16*)checksum_buf_8B;
739 kal_uint32 nvram_chksum_size = 0;
740 #ifdef __NV_CHKSUM_ENHANCE__
741 nvram_chksum_size = MD5_CHKSUM_LENGTH_8;
742 #else
743 nvram_chksum_size = NVRAM_CHKSUM_SIZE;
744 #endif
745
746 //Check Empty
747 if(Check_CDEF && !kal_mem_cmp(checksum_buf_8B, (kal_uint8*)&EmptyChksum, nvram_chksum_size)) {
748 if(checksum_buf_2B != NULL) {
749 checksum_buf_2B[0] = 0xEF;
750 checksum_buf_2B[1] = 0xCD;
751 }
752 return 0xCDEF;
753 }
754
755 //Merge 8B MD5 to 2B
756 for(i = 0; i < (nvram_chksum_size / 2); i++){
757 val += chk_ptr[i];
758 }
759
760 //Replace conflict chksum
761 if(val == 0xCDEF) {
762 val = 0xCEDF;
763 }
764
765 //Return
766 if(checksum_buf_2B != NULL) {
767 kal_mem_cpy(checksum_buf_2B, (kal_uint8*)&val, 2);
768 }
769 return val;
770}
771
772kal_uint16 nvram_util_caculate_checksum(nvram_ltable_entry_struct *ldi, const kal_uint8 *buf, kal_uint32 size, kal_uint8* checksum_buf)
773{
774 kal_uint8 i=0;
775 kal_uint16 chksum_2B = 0;
776 nvram_lid_chksum_info lid_chksum_info = {0};
777 kal_uint64 const EmptyChksum = 0xCDEF;
778 kal_uint8 *byte_chksum = (kal_uint8*) &chksum_2B;
779 kal_uint8 *temp_chksum_buf = NULL;
780 kal_uint32 temp_chksum_buf_size = 0;
781 NVRAM_ALGO_TYPE chksum_algo_type = NVRAM_MD5;
782
783 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
784
785 #ifdef __NV_CHKSUM_ENHANCE__
786 if(lid_chksum_info.algo_info.chksum_algo_type == chksum_algo_ptr->cur_algo.def_algo_type)
787 {
788 temp_chksum_buf_size = chksum_algo_ptr->cur_algo.def_algo_length;
789 }
790 else if(lid_chksum_info.algo_info.chksum_algo_type == chksum_algo_ptr->cur_algo.en_algo_type)
791 {
792 temp_chksum_buf_size = chksum_algo_ptr->cur_algo.en_algo_length;
793 }
794 chksum_algo_type = lid_chksum_info.algo_info.chksum_algo_type;
795 #else
796 temp_chksum_buf_size = NVRAM_CHKSUM_SIZE * 2;
797 chksum_algo_type = NVRAM_MD5;
798 #endif
799
800 if (buf == NVRAM_EF_ZERO_DEFAULT || nvram_util_zero_data_check(buf, size))
801 {
802 if(temp_chksum_buf_size < sizeof(EmptyChksum))
803 {
804 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[NVUT_UTIL_CAL]%s(): LDI<0x%x> chksize(%d) too small\n\r", __FUNCTION__, ldi->LID, temp_chksum_buf_size);
805 kal_prompt_trace(MOD_NVRAM,"[NVUT_UTIL_CAL] %s(): LDI<0x%x> chksize(%d) too small.\n\r", __FUNCTION__, ldi->LID, temp_chksum_buf_size);
806 NVRAM_EXT_ASSERT(KAL_FALSE, ldi->LID, NVRAM_LOC_CHKSUM_ALGO_MISSMATCH_FAIL, chksum_algo_type, temp_chksum_buf_size);
807 }
808 kal_mem_cpy(checksum_buf, (kal_uint8*)&EmptyChksum, sizeof(EmptyChksum));
809 return 0xCDEF;
810 }
811
812 if(INT_QueryExceptionStatus()== KAL_TRUE)
813 {
814 temp_chksum_buf = (kal_uint8 *)g_util_chksum;
815 }
816 else
817 {
818 temp_chksum_buf = (kal_uint8*) get_ctrl_buffer(temp_chksum_buf_size);
819 }
820 kal_mem_set(temp_chksum_buf, 0, temp_chksum_buf_size);
821 nvram_calculate_checksum(chksum_algo_type, (kal_uint8 *)buf, size, temp_chksum_buf);
822
823 //Merge 16B MD5 to 8B
824 if(chksum_algo_type == NVRAM_MD5)
825 {
826 for(i = 0; i< NVRAM_CHKSUM_SIZE; i++)
827 {
828 checksum_buf[i] = temp_chksum_buf[i]^temp_chksum_buf[i+8];
829 }
830
831 //Merge 8B MD5 to 2B
832 nvram_util_md5_checksum_convert(checksum_buf, byte_chksum, KAL_FALSE);
833 }
834 else
835 {
836 kal_mem_cpy(checksum_buf, temp_chksum_buf, temp_chksum_buf_size);
837 }
838
839 if(INT_QueryExceptionStatus() != KAL_TRUE)
840 {
841 free_ctrl_buffer(temp_chksum_buf);
842 }
843
844 return chksum_2B;
845}
846
847
848
849
850/*****************************************************************************
851 * FUNCTION
852 * nvram_make_lid_filename
853 * DESCRIPTION
854 * PARAMETERS
855 * RETURNS
856 *****************************************************************************/
857void nvram_util_make_lid_filename(nvram_ltable_entry_struct *ldi, NVRAM_FILE_NAME nvramname, kal_bool first_copy)
858{
859 /*----------------------------------------------------------------*/
860 /* Local Variables */
861 /*----------------------------------------------------------------*/
862
863 /*----------------------------------------------------------------*/
864 /* Code Body */
865 /*----------------------------------------------------------------*/
866
867 if (ldi == NULL)
868 {
869 EXT_ASSERT(KAL_FALSE,(kal_uint32)ldi,NVRAM_LOC_LID_PTR_IS_NULL_2, 0);
870 return;
871 }
872
873#ifdef __NVRAM_PSEUDO_MERGE__
874 if (NVRAM_IS_ATTR_PACKAGE(ldi->attr))
875 {
876 nvram_make_package_filename(nvramname, 'A');
877 return;
878 }
879#endif
880
881 if (NVRAM_IS_ATTR_MULTIPLE(ldi->attr))
882 {
883 if (first_copy)
884 {
885 nvram_util_make_filename(nvramname, ldi->fileprefix, 'A', ldi->fileverno);
886 }
887 else
888 {
889 #if defined(__NVRAM_WRITE_PROTECT_ENABLE__) && defined(__NVRAM_IMPORTANT_PARTITIONS__) && defined(__CCCIFS_SUPPORT__)
890 nvram_util_make_filename(nvramname, ldi->fileprefix, 'A', ldi->fileverno);
891 #else
892 nvram_util_make_filename(nvramname, ldi->fileprefix, 'B', ldi->fileverno);
893 #endif
894 }
895 }
896 else
897 {
898 nvram_util_make_filename(nvramname, ldi->fileprefix, '_', ldi->fileverno);
899 }
900
901 return;
902
903}
904
905/*****************************************************************************
906 * FUNCTION
907 * nvram_make_lid_filename
908 * DESCRIPTION
909 * PARAMETERS
910 * RETURNS
911 *****************************************************************************/
912void nvram_util_make_filename(NVRAM_FILE_NAME buf, kal_char* prefix, kal_char M, kal_char* verno)
913{
914 buf[0] = prefix[0];
915 buf[1] = prefix[1];
916 buf[2] = prefix[2];
917 buf[3] = prefix[3];
918 buf[4] = M;
919 buf[5] = verno[0];
920 buf[6] = verno[1];
921 buf[7] = verno[2];
922 buf[8] = '\0';
923};
924
925
926/*****************************************************************************
927 * FUNCTION
928 * nvram_util_is_usbboot
929 * DESCRIPTION
930 * old function name: INT_USBBoot
931 * PARAMETERS
932 * RETURNS
933 *****************************************************************************/
934kal_bool nvram_util_is_usbboot(void)
935{
936 /*----------------------------------------------------------------*/
937 /* Local Variables */
938 /*----------------------------------------------------------------*/
939 PW_CTRL_IS_USB_BOOT CtrlVal;
940 DCL_HANDLE handle;
941
942 /*----------------------------------------------------------------*/
943 /* Code Body */
944 /*----------------------------------------------------------------*/
945 handle=DclPW_Open(DCL_PW, FLAGS_NONE);
946 DclPW_Control(handle, PW_CMD_IS_USB_BOOT,(DCL_CTRL_DATA_T *)&CtrlVal);
947 DclPW_Close(handle);
948 return ((kal_bool)CtrlVal.val);
949}
950
951/*****************************************************************************
952 * FUNCTION
953 * nvram_util_poweron
954 * DESCRIPTION
955 * old function name: DRV_POWERON
956 * PARAMETERS
957 * RETURNS
958 *****************************************************************************/
959kal_bool nvram_util_poweron(void)
960{
961 /*----------------------------------------------------------------*/
962 /* Local Variables */
963 /*----------------------------------------------------------------*/
964 DCL_HANDLE handle;
965
966 /*----------------------------------------------------------------*/
967 /* Code Body */
968 /*----------------------------------------------------------------*/
969 handle=DclPW_Open(DCL_PW, FLAGS_NONE);
970 DclPW_Control(handle,PW_CMD_POWERON,NULL);
971 DclPW_Close(handle);
972 return KAL_TRUE;
973}
974
975/*****************************************************************************
976 * FUNCTION
977 * nvram_util_poweron
978 * DESCRIPTION
979 * old function name: DRV_POWERON
980 * PARAMETERS
981 * RETURNS
982 *****************************************************************************/
983void nvram_util_poweroff(void)
984{
985 /*----------------------------------------------------------------*/
986 /* Local Variables */
987 /*----------------------------------------------------------------*/
988 DCL_HANDLE handle;
989
990 /*----------------------------------------------------------------*/
991 /* Code Body */
992 /*----------------------------------------------------------------*/
993 handle=DclPW_Open(DCL_PW, FLAGS_NONE);
994 DclPW_Control(handle,PW_CMD_POWEROFF,NULL);
995 DclPW_Close(handle);
996}
997
998/*****************************************************************************
999 * FUNCTION
1000 * nvram_get_total_num_lid
1001 * DESCRIPTION
1002 * Get the total LID number
1003 * PARAMETERS
1004 * void
1005 * RETURNS
1006 * nvram_lid_enum
1007 *****************************************************************************/
1008nvram_lid_enum nvram_get_total_num_lid(void)
1009{
1010 return nvram_ptr->ltable.total_LID;
1011}
1012
1013nvram_lid_enum nvram_util_get_IMEISV_LID(void)
1014{
1015 return NVRAM_EF_IMEI_IMEISV_LID;
1016}
1017
1018kal_uint32 nvram_util_get_IMEISV_size(void)
1019{
1020 return NVRAM_EF_IMEI_IMEISV_SIZE;
1021}
1022
1023kal_uint32 nvram_util_get_IMEI_size(void)
1024{
1025 return NVRAM_EF_IMEI_IMEI_SIZE;
1026}
1027
1028void nvram_util_take_mutex(kal_mutexid ext_mutex_id_ptr)
1029{
1030 if (!INT_QueryExceptionStatus() && !kal_query_systemInit() && ext_mutex_id_ptr)
1031 {
1032 kal_take_mutex(ext_mutex_id_ptr);
1033 }
1034}
1035
1036void nvram_util_give_mutex(kal_mutexid ext_mutex_id_ptr)
1037{
1038 if (!INT_QueryExceptionStatus() && !kal_query_systemInit() && ext_mutex_id_ptr)
1039 {
1040 kal_give_mutex(ext_mutex_id_ptr);
1041 }
1042}
1043
1044#ifdef __NVRAM_INIT_TIME_UT__
1045void nvram_init_time_ut_save(void)
1046{
1047 kal_uint8 i;
1048
1049 for(i = 1; i < 9; i++) {
1050 if(init_time_tick[i]) {
1051 init_time_tick[i] -= init_time_tick[0];
1052 }
1053 }
1054 //init_time_tick[1] = ((init_time_tick[1] & 0x0FFF) | (init_trace << 12));
1055 init_time_tick[9] = init_trace;
1056 nvram_external_write_data(NVRAM_EF_SYS_CACHE_OCTET_LID,
1057 NVRAM_SYS_NVRAM_INIT_TIME_UT,
1058 (kal_uint8*)&init_time_tick[1],
1059 NVRAM_EF_SYS_CACHE_OCTET_SIZE);
1060 nvram_external_write_data(NVRAM_EF_SYS_CACHE_OCTET_LID,
1061 NVRAM_SYS_NVRAM_INIT_TIME_UT2,
1062 (kal_uint8*)&init_time_tick[5],
1063 NVRAM_EF_SYS_CACHE_OCTET_SIZE);
1064}
1065#endif /* __NVRAM_INIT_TIME_UT__ */
1066
1067void * nvram_memset(void* dest, kal_int32 value, kal_uint32 size)
1068{
1069 kal_uint8 *dest_ptr = (kal_uint8*)dest;
1070
1071 while(size)
1072 {
1073 if(size > (64*1024)) {
1074 kal_mem_set(dest_ptr, value, (64*1024));
1075 size -= (64*1024);
1076 dest_ptr += (64*1024);
1077 }
1078 else {
1079 return kal_mem_set(dest_ptr, value, size);
1080 }
1081 }
1082 return NULL;
1083}
1084
1085#if defined(__NVRAM_CREATE_FILE_ON_WRITE__)
1086#define NVRAM_INFO_FILE_MAGIC_START 0x4F464E49 // "INFO"
1087#define NVRAM_INFO_FILE_STAT_READY 0xFDFDFBFB
1088#define MD5_CHECKSUM_LEN 16
1089#define NVRAM_LID_BITMAP_SIZE ((NVRAM_LID_GRP_MAX-(NVRAM_LID_2ND_GRP_START-NVRAM_LID_1ST_GRP_END)+7)>>3)
1090
1091typedef struct {
1092 kal_uint32 magic_start;
1093 kal_uint32 file_stat;
1094 kal_uint8 check_sum[MD5_CHECKSUM_LEN];
1095} nvram_info_file_hdr_struct;
1096
1097typedef struct {
1098 nvram_info_file_hdr_struct hdr;
1099 kal_uint8 bitmap[NVRAM_LID_BITMAP_SIZE];
1100 kal_bool changed;
1101} nvram_created_file_info_struct;
1102
1103kal_mutexid nvram_bitmap_mutex = NULL;
1104kal_mutexid nvram_pre_write_check_mutex = NULL;
1105nvram_created_file_info_struct nvram_created_file_info;
1106kal_uint8 *nvram_bitmap_ptr = nvram_created_file_info.bitmap;
1107
1108static WCHAR *nvram_util_get_info_file_path()
1109{
1110 static WCHAR fileName[NVRAM_MAX_PATH_LEN];
1111
1112 NVRAM_FS_MAKE_ROOT_PATH(fileName);
1113
1114 kal_wstrcat(fileName, L"\\INFO_FILE");
1115
1116 return fileName;
1117}
1118
1119void nvram_util_lid_bimtap_set(kal_uint8 *bitmap, kal_uint32 lid)
1120{
1121 kal_uint32 offset;
1122 kal_uint32 bit = lid&0x7;
1123
1124 if(!bitmap) {
1125 EXT_ASSERT(KAL_FALSE, (kal_uint32)bitmap, NVRAM_LOC_BITMAP_INVALID, lid);
1126 }
1127 if (lid < NVRAM_LID_1ST_GRP_END) {
1128 offset = lid>>3;
1129 } else if (lid >= NVRAM_LID_2ND_GRP_START){
1130 offset = (NVRAM_LID_1ST_GRP_END>>3) + ((lid-NVRAM_LID_2ND_GRP_START)>>3);
1131 } else {
1132 EXT_ASSERT(KAL_FALSE, (kal_uint32)bitmap, NVRAM_LOC_BITMAP_ACCESS_OOR, lid);
1133 }
1134
1135 bitmap[offset] |= (0x1<<bit);
1136 nvram_created_file_info.changed = KAL_TRUE;
1137}
1138
1139void nvram_util_lid_bitmap_clr(kal_uint8 *bitmap, kal_uint32 lid)
1140{
1141 kal_uint32 offset;
1142 kal_uint32 bit = lid&0x7;
1143
1144 if(!bitmap) {
1145 EXT_ASSERT(KAL_FALSE, (kal_uint32)bitmap, NVRAM_LOC_BITMAP_INVALID, lid);
1146 }
1147 if (lid < NVRAM_LID_1ST_GRP_END) {
1148 offset = lid>>3;
1149 } else if (lid >= NVRAM_LID_2ND_GRP_START){
1150 offset = (NVRAM_LID_1ST_GRP_END>>3) + ((lid-NVRAM_LID_2ND_GRP_START)>>3);
1151 } else {
1152 EXT_ASSERT(KAL_FALSE, (kal_uint32)bitmap, NVRAM_LOC_BITMAP_ACCESS_OOR, lid);
1153 }
1154
1155 bitmap[offset] &= ~(0x1<<bit);
1156 nvram_created_file_info.changed = KAL_TRUE;
1157}
1158
1159kal_bool nvram_util_lid_bitmap_check(kal_uint8 *bitmap, kal_uint32 lid)
1160{
1161 kal_uint32 offset;
1162 kal_uint32 bit = lid&0x7;
1163
1164 if(!bitmap) {
1165 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)bitmap, NVRAM_LOC_BITMAP_INVALID, lid);
1166 }
1167 if (lid < NVRAM_LID_1ST_GRP_END) {
1168 offset = lid>>3;
1169 } else if (lid >= NVRAM_LID_2ND_GRP_START){
1170 offset = (NVRAM_LID_1ST_GRP_END>>3) + ((lid-NVRAM_LID_2ND_GRP_START)>>3);
1171 } else {
1172 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)bitmap, NVRAM_LOC_BITMAP_ACCESS_OOR, lid);
1173 }
1174
1175 return ((bitmap[offset] & (0x1<<bit)) != 0)?KAL_TRUE:KAL_FALSE;
1176}
1177
1178void nvram_utile_reset_lid_bitmap()
1179{
1180 nvram_created_file_info.hdr.magic_start = NVRAM_INFO_FILE_MAGIC_START;
1181 nvram_created_file_info.hdr.file_stat = NVRAM_INFO_FILE_STAT_READY;
1182 nvram_created_file_info.changed = KAL_FALSE;
1183 kal_mem_set(nvram_created_file_info.bitmap, 0, sizeof(nvram_created_file_info.bitmap));
1184}
1185
1186static void nvram_util_calc_md5_checksum(kal_uint8 *data_buf, kal_uint32 data_buf_len, kal_uint8 *chksum_buf)
1187{
1188
1189 // MD5 of bit map
1190 nvram_calculate_checksum(NVRAM_MD5, data_buf, data_buf_len, chksum_buf);
1191
1192 return;
1193}
1194
1195
1196kal_bool nvram_write_bitmap_into_file()
1197{
1198 kal_uint32 buf_len = 0;
1199 kal_uint8 *write_buf = NULL;
1200 WCHAR *file_path = NULL;
1201 FS_HANDLE file_handle = FS_INVALID_FILE_HANDLE;
1202 kal_int32 result = FS_NO_ERROR;
1203 kal_uint32 write_len;
1204
1205 //Reduce the time of nvram init at first bootup by void writing bitmap file many times
1206 if (nvram_ptr->state != NVRAM_STATE_READY) //before nvram init done, cann't write bitmap
1207 {
1208 return KAL_TRUE;
1209 }
1210 if(!nvram_created_file_info.changed) //Must After nvram init,can write bitmap when first boot or version change
1211 {
1212 return KAL_TRUE;
1213 }
1214 file_path = nvram_util_get_info_file_path();
1215 NVRAM_FS_START_EX(FS_OP_OPEN,file_path);
1216 file_handle = FS_Open(file_path, FS_READ_WRITE | FS_OPEN_NO_DIR | FS_CREATE_ALWAYS);
1217 NVRAM_FS_END(FS_OP_OPEN,file_handle);
1218 if (file_handle < FS_NO_ERROR) {
1219 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Open bitmap fail at %d,file_handle=%d\r\n",__FUNCTION__,__LINE__,file_handle);
1220 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)file_handle, NVRAM_LOC_BITMAP_OPEN_FAIL, buf_len);
1221 }
1222
1223 nvram_created_file_info.hdr.file_stat = NVRAM_INFO_FILE_STAT_READY;
1224 nvram_util_calc_md5_checksum(nvram_created_file_info.bitmap, sizeof(nvram_created_file_info.bitmap), nvram_created_file_info.hdr.check_sum);
1225
1226 write_buf = (kal_uint8 *)&nvram_created_file_info;
1227 buf_len = sizeof(nvram_created_file_info);
1228 NVRAM_FS_START(FS_OP_WRITE);
1229 result = FS_Write(file_handle, write_buf, buf_len, &write_len);
1230 NVRAM_FS_END(FS_OP_WRITE,result);
1231 if (result < FS_NO_ERROR) {
1232 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s->FS_Write fail at %d,result=%d\r\n",__FUNCTION__,__LINE__,result);
1233 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_LOC_BITMAP_WRITE_FAIL, buf_len, FS_Close(file_handle));
1234 }
1235 NVRAM_FS_START(FS_OP_CLOSE);
1236 result = FS_Close(file_handle);
1237 NVRAM_FS_END(FS_OP_CLOSE,result);
1238 nvram_created_file_info.changed = KAL_FALSE;
1239 return KAL_TRUE;
1240}
1241
1242kal_bool nvram_delete_bitmap_file()
1243{
1244 WCHAR *file_path = NULL;
1245 kal_int32 result = FS_NO_ERROR;
1246 file_path = nvram_util_get_info_file_path();
1247 NVRAM_FS_START_EX(FS_OP_DELETE,file_path);
1248 result = FS_Delete(file_path);
1249 NVRAM_FS_END(FS_OP_DELETE,result);
1250 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s result=%d\r\n",__FUNCTION__,result);
1251 return KAL_TRUE;
1252}
1253kal_taskid pre_write_check_mutex_owner = (kal_taskid)-1; /* to avoid recursive lock */
1254void nvram_util_pre_write_lid(nvram_ltable_entry_struct *ldi)
1255{
1256 if (pre_write_check_mutex_owner != kal_get_current_task()) {
1257 nvram_util_take_mutex(nvram_pre_write_check_mutex);
1258 pre_write_check_mutex_owner = kal_get_current_task();
1259
1260 if (!nvram_util_has_file_created(ldi) && !nvram_util_always_gen_default(ldi))
1261 {
1262 if(NVRAM_IS_ATTR_RAW_DATA(ldi->attr))
1263 {
1264 // no need reset RAW data file
1265 nvram_util_post_reset_gen_default_lid(ldi);
1266 }
1267 else
1268 {
1269 // mark always create, then reset the lid
1270 ldi->attr |= NVRAM_ATTR_GEN_DEFAULT;
1271
1272 // reset the LID, bitmap will be set after reset ok
1273 nvram_reset_data_items(NVRAM_RESET_CERTAIN, 0, ldi, 1, ldi->total_records);
1274 // clear the mark
1275 ldi->attr &= ~NVRAM_ATTR_GEN_DEFAULT;
1276 }
1277 }
1278
1279 pre_write_check_mutex_owner = (kal_taskid)-1;
1280 nvram_util_give_mutex(nvram_pre_write_check_mutex);
1281 }
1282}
1283
1284void nvram_util_post_write_lid(nvram_ltable_entry_struct *ldi)
1285{
1286
1287}
1288
1289void nvram_util_pre_reset_gen_default_lid(nvram_ltable_entry_struct *ldi)
1290{
1291 if (nvram_util_has_file_created(ldi)) {
1292 // do nothing
1293 return;
1294 }
1295
1296 // prepare reset
1297}
1298
1299void nvram_util_post_reset_gen_default_lid(nvram_ltable_entry_struct *ldi)
1300{
1301 nvram_lid_enum lid = ldi->LID;
1302
1303 if (nvram_util_has_file_created(ldi)) {
1304 // do nothing
1305 return;
1306 }
1307
1308 // reset lid finish, file generated success fully
1309 #ifdef __NVRAM_LID_CACHE__
1310 nvram_util_take_mutex(g_nvram_fs_mutex);
1311 #endif
1312 nvram_util_take_mutex(nvram_bitmap_mutex);
1313 nvram_util_lid_bimtap_set(nvram_created_file_info.bitmap, lid);
1314 nvram_write_bitmap_into_file();
1315 nvram_util_give_mutex(nvram_bitmap_mutex);
1316 #ifdef __NVRAM_LID_CACHE__
1317 nvram_util_give_mutex(g_nvram_fs_mutex);
1318 #endif
1319}
1320
1321kal_bool nvram_util_has_file_created(nvram_ltable_entry_struct *ldi)
1322{
1323 nvram_lid_enum lid = ldi->LID;
1324
1325 if (NVRAM_IS_CATEGORY_CUSTOM_DISK(ldi->category) ||
1326 NVRAM_IS_CATEGORY_OTP(ldi->category)) {
1327 return KAL_TRUE;
1328 }
1329#ifdef __NVRAM_READ_RESERVED_FILE__
1330 if(ldi->LID == NVRAM_EF_READ_RESERVED_LID) {
1331 return KAL_TRUE;
1332 }
1333#endif
1334
1335 // read only
1336 return nvram_util_lid_bitmap_check(nvram_created_file_info.bitmap, lid);
1337}
1338
1339kal_bool nvram_util_always_gen_default(nvram_ltable_entry_struct *ldi)
1340{
1341 if ((ldi->attr & (NVRAM_ATTR_GEN_DEFAULT)) != 0) {
1342 return KAL_TRUE;
1343 }
1344
1345 return KAL_FALSE;
1346}
1347
1348void nvram_util_mark_file_uncreated(nvram_ltable_entry_struct *ldi)
1349{
1350 nvram_lid_enum lid = ldi->LID;
1351 kal_uint32 idx;
1352 kal_int32 result = FS_NO_ERROR;
1353 // remove record
1354 nvram_util_take_mutex(nvram_bitmap_mutex);
1355 if (nvram_util_lid_bitmap_check(nvram_created_file_info.bitmap, lid)) {
1356 nvram_util_lid_bitmap_clr(nvram_created_file_info.bitmap, lid);
1357
1358 // delete the LID's NVRAM File
1359 for (idx = 0; idx < 2; idx++) {
1360 NVRAM_FILE_NAME nvramname;
1361 WCHAR filename[NVRAM_MAX_PATH_LEN];
1362 nvram_folder_enum folder_index;
1363
1364 nvram_util_make_lid_filename(ldi, nvramname, (idx==0));
1365 folder_index = nvram_query_folder_index_ex(ldi->category, (idx==0));
1366 nvram_query_file_name(folder_index, nvramname, filename);
1367 NVRAM_FS_START_EX(FS_OP_DELETE,filename);
1368 result = FS_Delete(filename);
1369 NVRAM_FS_END(FS_OP_DELETE,result);
1370 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s->FS_Delete %s result=%d\r\n",__FUNCTION__,nvramname,result);
1371 if (!(ldi->attr & NVRAM_ATTR_MULTIPLE)) {
1372 break;
1373 }
1374 }
1375
1376 // update info file
1377 nvram_write_bitmap_into_file();
1378 }
1379 nvram_util_give_mutex(nvram_bitmap_mutex);
1380}
1381
1382static kal_bool nvram_util_reconstruct_info_file()
1383{
1384 /*----------------------------------------------------------------*/
1385 /* Local Variables */
1386 /*----------------------------------------------------------------*/
1387 FS_FileOpenHint Hint;
1388 NVRAM_FILE_NAME nvramname;
1389 kal_int32 file_size = 0;
1390 kal_wchar filename[NVRAM_MAX_PATH_LEN];
1391 nvram_folder_enum folder_index;
1392 kal_uint32 i;
1393 nvram_ltable_entry_struct *ldi = &logical_data_item_table[0];
1394 kal_bool file_exist;
1395 nvram_lid_enum lid;
1396 kal_int32 result = FS_NO_ERROR;
1397 /*----------------------------------------------------------------*/
1398 /* Code Body */
1399 /*----------------------------------------------------------------*/
1400 /* Must clear up Hint beforehand */
1401 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s ====>\r\n",__FUNCTION__);
1402 kal_mem_set(&Hint, 0, sizeof(Hint));
1403 nvram_util_take_mutex(nvram_bitmap_mutex);
1404 nvram_utile_reset_lid_bitmap();
1405
1406 do
1407 {
1408 file_exist = KAL_FALSE;
1409 lid = ldi->LID;
1410
1411 for (i = 0 ; i < 2; i++)
1412 {
1413 folder_index = nvram_query_folder_index(ldi->category);
1414
1415 if (i == 0) // first copy
1416 {
1417 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE);
1418 }
1419 else // second copy
1420 {
1421 nvram_util_make_lid_filename(ldi, nvramname, KAL_FALSE);
1422
1423 #ifdef __NVRAM_BACKUP_DISK_FAT__
1424 if (NVRAM_IS_ATTR_BACKUP_FAT(ldi->attr))
1425 {
1426 folder_index = NVRAM_NVD_BAK;
1427 }
1428 #endif
1429 }
1430
1431 file_size = nvram_drv_fat_get_record_size(nvramname, &Hint, folder_index);
1432
1433 if (file_size == 0)
1434 {
1435 // delete empty file
1436 nvram_query_file_name(folder_index, nvramname, filename);
1437 NVRAM_FS_START_EX(FS_OP_DELETE,filename);
1438 result = FS_Delete(filename);
1439 NVRAM_FS_END(FS_OP_DELETE,result);
1440 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s->FS_Delete %s result=%d\r\n",__FUNCTION__,nvramname,result);
1441 }
1442
1443 if (file_size > 0) {
1444 file_exist = KAL_TRUE;
1445 break;
1446 }
1447
1448 if (!NVRAM_IS_ATTR_MULTIPLE(ldi->attr))
1449 {
1450 break;
1451 }
1452 }
1453
1454 if (file_exist == KAL_TRUE) {
1455 nvram_util_lid_bimtap_set(nvram_created_file_info.bitmap, lid);
1456 }
1457 }while(nvram_util_next_data_item(&ldi));
1458 nvram_created_file_info.changed = KAL_TRUE;
1459 nvram_write_bitmap_into_file();
1460 nvram_util_give_mutex(nvram_bitmap_mutex);
1461 if(nvram_ee_info != NULL){
1462 nvram_ee_info->nvram_init_step = NVRAM_CORE_INIT_FACTORY_BOOT_CONSTRUCT_BITMAP_DONE;
1463 }
1464 NVRAM_DEBUG_DUMP(NVRAM_WARNING_DUMP,"%s <====\r\n",__FUNCTION__);
1465 return KAL_TRUE;
1466}
1467
1468
1469kal_bool nvram_get_created_file_info()
1470{
1471 WCHAR *file_path = NULL;
1472 FS_HANDLE file_handle = FS_INVALID_FILE_HANDLE;
1473 kal_uint32 read_len;
1474 kal_int32 result = FS_NO_ERROR;
1475 nvram_info_file_hdr_struct *hdr = &nvram_created_file_info.hdr;
1476 kal_uint8 temp_checksum[MD5_CHECKSUM_LEN];
1477
1478
1479 file_path = nvram_util_get_info_file_path();
1480 NVRAM_FS_START_EX(FS_OP_OPEN,file_path);
1481 file_handle = FS_Open(file_path, FS_READ_ONLY|FS_OPEN_NO_DIR);
1482 NVRAM_FS_END(FS_OP_OPEN,file_handle);
1483 if (file_handle < FS_NO_ERROR) {
1484 // file not exist, maybe deleted
1485 is_nvram_factory_reset = KAL_TRUE;
1486 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s bitmap file is not exist\r\n",__FUNCTION__);
1487 return nvram_util_reconstruct_info_file();
1488 }
1489 NVRAM_FS_START(FS_OP_READ);
1490 result = FS_Read(file_handle, hdr, sizeof(*hdr), &read_len);
1491 NVRAM_FS_END(FS_OP_READ,result);
1492
1493 if (result < FS_NO_ERROR || read_len != sizeof(*hdr)) {
1494 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s->FS_Read result=%d,read_len=%d \r\n",__FUNCTION__,result,read_len);
1495 NVRAM_FS_START(FS_OP_CLOSE);
1496 result = FS_Close(file_handle);
1497 NVRAM_FS_END(FS_OP_CLOSE,result);
1498 return nvram_util_reconstruct_info_file();
1499 }
1500
1501 if (hdr->magic_start != NVRAM_INFO_FILE_MAGIC_START) {
1502 // Invalid header
1503 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s bitmap file:Invalid header\r\n",__FUNCTION__);
1504 NVRAM_FS_START(FS_OP_CLOSE);
1505 result = FS_Close(file_handle);
1506 NVRAM_FS_END(FS_OP_CLOSE,result);
1507 return nvram_util_reconstruct_info_file();
1508 }
1509
1510 if (hdr->file_stat != NVRAM_INFO_FILE_STAT_READY) {
1511 // File is updating but power lost
1512 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s bitmap file:data is corruption\r\n",__FUNCTION__);
1513 NVRAM_FS_START(FS_OP_CLOSE);
1514 result = FS_Close(file_handle);
1515 NVRAM_FS_END(FS_OP_CLOSE,result);
1516 return nvram_util_reconstruct_info_file();
1517 }
1518
1519 NVRAM_FS_START(FS_OP_READ);
1520 result = FS_Read(file_handle, nvram_created_file_info.bitmap, sizeof(nvram_created_file_info.bitmap), &read_len);
1521 NVRAM_FS_END(FS_OP_READ,result);
1522 if (result < FS_NO_ERROR || read_len != sizeof(nvram_created_file_info.bitmap)) {
1523 // Read Error
1524 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s->FS_Read fail,result=%d\r\n",__FUNCTION__,result);
1525 NVRAM_FS_START(FS_OP_CLOSE);
1526 result = FS_Close(file_handle);
1527 NVRAM_FS_END(FS_OP_CLOSE,result);
1528 return nvram_util_reconstruct_info_file();
1529 }
1530 NVRAM_FS_START(FS_OP_CLOSE);
1531 result = FS_Close(file_handle);
1532 NVRAM_FS_END(FS_OP_CLOSE,result);
1533
1534 nvram_util_calc_md5_checksum(nvram_created_file_info.bitmap, sizeof(nvram_created_file_info.bitmap), temp_checksum);
1535 if (memcmp(nvram_created_file_info.hdr.check_sum, temp_checksum, MD5_CHECKSUM_LEN) != 0) {
1536 // checksum check fail
1537 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s bitmap file:checksum error\r\n",__FUNCTION__);
1538 return nvram_util_reconstruct_info_file();
1539 }
1540
1541 return KAL_TRUE;
1542}
1543
1544kal_bool nvram_util_init_info_file()
1545{
1546 // MUST call once @ nvram_init
1547 static kal_bool inited = KAL_FALSE;
1548
1549 if (!inited) {
1550 inited = KAL_TRUE;
1551
1552 nvram_bitmap_mutex = kal_create_mutex("NV_INFO");
1553 nvram_pre_write_check_mutex = kal_create_mutex("NV_PREW");
1554
1555 if (nvram_ptr->sw_status & NVRAM_SW_EMPTY_FAT) {
1556 nvram_utile_reset_lid_bitmap();
1557 nvram_write_bitmap_into_file();
1558 } else {
1559 nvram_get_created_file_info();
1560 }
1561 }
1562
1563 return KAL_TRUE;
1564}
1565
1566#endif
1567
1568DECLARE_MIPS32
1569static kal_uint16 nvram_data_header_checksum(kal_uint8 *buf, kal_uint32 size)
1570{
1571 /*----------------------------------------------------------------*/
1572 /* Local Variables */
1573 /*----------------------------------------------------------------*/
1574 kal_uint32 i;
1575 kal_uint16 chksum = *(kal_uint16*)buf;
1576 kal_uint8 *byte_chksum = (kal_uint8*)&chksum;
1577 kal_uint8 value;
1578
1579 /*----------------------------------------------------------------*/
1580 /* Code Body */
1581 /*----------------------------------------------------------------*/
1582
1583 for (i = 0; i < size; i++)
1584 {
1585 value = *(buf + i);
1586 while(value) {
1587 if(value & 0x1) {
1588 #if defined(__MTK_TARGET__)
1589 __asm__ __volatile__
1590 (
1591 "rol %0, %0, 1\r\n"
1592 ::"r"(chksum)
1593 );
1594 #else
1595 __asm {ROL [chksum],1};
1596 #endif
1597 }
1598 value >>= 1;
1599 }
1600 #if defined(__MTK_TARGET__)
1601 __asm__ __volatile__
1602 (
1603 "rol %0, %0, 4\r\n"
1604 ::"r"(chksum)
1605 );
1606 #else
1607 __asm {ROL [chksum],4};
1608 #endif
1609
1610 *byte_chksum += *(buf + i);
1611 }
1612
1613 return chksum;
1614}
1615
1616kal_uint32 nvram_appendix_header_offset(nvram_ltable_entry_struct *ldi)
1617{
1618 kal_uint32 size = 0;
1619 kal_uint32 nvram_chksum_size = 0;
1620 nvram_lid_chksum_info lid_chksum_info = {0};
1621
1622 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
1623 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
1624
1625 if ((ldi->attr & NVRAM_ATTR_CONFIDENTIAL)
1626 #ifdef __NVRAM_BIND_TO_CHIP_CIPHER__
1627 || (ldi->attr & NVRAM_ATTR_MSP)
1628 #endif
1629 )
1630 {
1631 /* 16 byte alignment */
1632 size = NVRAM_MSP_ALIGNMENT_REMAINDER(ldi->size + nvram_chksum_size) + ldi->size + nvram_chksum_size;
1633 }
1634 else
1635 {
1636 size = (ldi->size + nvram_chksum_size);
1637 }
1638 size = size * ldi->total_records;
1639 return (NVRAM_LDI_HEADER_SIZE + size);
1640}
1641
1642static __inline kal_bool nvram_prepare_ota_header(nvram_ldi_ota_header *ldi_ota_header, nvram_ltable_entry_struct *ldi)
1643{
1644 #if defined(__NVRAM_DEFVAL_CHANGE_RESET__)
1645 kal_int32 chksum_index;
1646 #endif
1647
1648 memcpy(ldi_ota_header->header, "LDI", 4);
1649 ldi_ota_header->LID = ldi->LID;
1650 ldi_ota_header->ldi_attr = ldi->attr;
1651 ldi_ota_header->ldi_category= ldi->category;
1652 ldi_ota_header->record_size = ldi->size;
1653 ldi_ota_header->total_records = ldi->total_records;
1654 //check appendix header offset
1655 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE) {
1656 ldi->append_offset = nvram_appendix_header_offset(ldi);
1657 }
1658 #if defined(__NVRAM_DEFVAL_CHANGE_RESET__)
1659 chksum_index = nvram_get_defval_chksum_index(ldi->LID);
1660 if(-1 != chksum_index)
1661 {
1662 kal_mem_cpy((char *)(ldi_ota_header->defval_chkrst_h), (const char*)(lid_default_value_chksum[chksum_index].chksum), RST_CHKSUM_SIZE-6);
1663 }
1664 #endif
1665 ldi_ota_header->checksum = nvram_data_header_checksum((kal_uint8*)ldi_ota_header, (NVRAM_LDI_OTA_HEADER_SIZE - sizeof(ldi_ota_header->checksum)));
1666 return KAL_TRUE;
1667}
1668
1669static __inline kal_bool nvram_prepare_debug_header(nvram_ldi_debug_header *ldi_debug_header, nvram_ltable_entry_struct *ldi)
1670{
1671 #if defined(__NVRAM_STRUCTURE_CHANGE_RESET__) || defined(__NVRAM_DEFVAL_CHANGE_RESET__)
1672 kal_int32 chksum_index;
1673 #endif
1674
1675 ldi_debug_header->last_write_taskID = kal_get_current_task_index();
1676 ldi_debug_header->last_write_time = kal_get_systicks();
1677 ldi_debug_header->write_times += 1;
1678
1679 #if defined(__NVRAM_STRUCTURE_CHANGE_RESET__)
1680 chksum_index = nvram_get_structure_chksum_index(ldi->LID);
1681 if(-1 != chksum_index)
1682 {
1683 kal_mem_cpy((char *)(ldi_debug_header->struct_chkrst), (const char*)(lid_structure_chksum[chksum_index].chksum), RST_CHKSUM_SIZE);
1684 }
1685 #endif
1686
1687 #if defined(__NVRAM_DEFVAL_CHANGE_RESET__)
1688 chksum_index = nvram_get_defval_chksum_index(ldi->LID);
1689 if(-1 != chksum_index)
1690 {
1691 kal_mem_cpy((char *)(ldi_debug_header->defval_chkrst_l),(const char*)(&(lid_default_value_chksum[chksum_index].chksum[RST_CHKSUM_SIZE-6])), 6);
1692 }
1693 #endif
1694
1695 return KAL_TRUE;
1696}
1697
1698kal_bool nvram_prepare_data_header(nvram_ltable_entry_struct *ldi,kal_uint8 *ldi_hd_buffer)
1699{
1700 nvram_ldi_ota_header *ldi_ota_header = (nvram_ldi_ota_header*)ldi_hd_buffer;
1701 nvram_ldi_debug_header *ldi_debug_header = (nvram_ldi_debug_header*)(ldi_hd_buffer + NVRAM_LDI_OTA_HEADER_SIZE);
1702
1703 nvram_prepare_ota_header(ldi_ota_header,ldi);
1704 nvram_prepare_debug_header(ldi_debug_header, ldi);
1705
1706 return KAL_TRUE;
1707}
1708
1709/* =====================================================================
1710APDX_hdr1 chksum type data_offset1
1711APDX_hdr2 chksum type data_offset2
1712data1 ......
1713data2 ......
1714
1715 * FUNCTION
1716 * nvram_prepare_appendix_header
1717 * DESCRIPTION
1718 * This function is used to read the checksum of data item for external module.
1719 * Please must check with NVRAM owner before you use this API.
1720 * PARAMETERS
1721 * type: [IN] header type
1722 * ldi_append_header: [IN] header buffer
1723 * ldi: [IN] NVRAM LID
1724 * data_offset: [IN] current APDX data offset
1725 * RETURNS
1726 * the next APDX data start offset
1727=======================================================================*/
1728kal_uint32 nvram_prepare_appendix_header(nvram_appendix_type_enum type, nvram_ldi_appendix_header *ldi_append_header, nvram_ltable_entry_struct *ldi, kal_uint32 data_offset)
1729{
1730 kal_uint32 nvram_chksum_size = 0;
1731 nvram_lid_chksum_info lid_chksum_info = {0};
1732
1733 nvram_get_lid_chksum_algo_info(ldi, &lid_chksum_info, KAL_FALSE, KAL_FALSE);
1734 nvram_chksum_size = lid_chksum_info.algo_info.chksum_algo_length;
1735
1736 memset(ldi_append_header, 0, NVRAM_LDI_APPENDIX_HEADER_SIZE);
1737 memcpy(ldi_append_header->header, "APDX", 4);
1738
1739 if(type == NVRAM_APPEND_TYPE_CHKSUM) {
1740 ldi_append_header->type = NVRAM_APPEND_TYPE_CHKSUM;
1741 ldi_append_header->data_offset = data_offset;
1742 //caculate next data start
1743 data_offset = (data_offset + (ldi->total_records * nvram_chksum_size));
1744 data_offset = NVRAM_MSP_ALIGNMENT_CEILING(data_offset);
1745 }
1746 else {
1747 ldi_append_header->type = NVRAM_APPEND_TYPE_END;
1748 data_offset = 0;
1749 }
1750 ldi_append_header->checksum = nvram_data_header_checksum((kal_uint8*)ldi_append_header, NVRAM_LDI_APPENDIX_HEADER_SIZE);
1751 return data_offset;
1752}
1753
1754kal_bool nvram_write_data_header(nvram_ltable_entry_struct *ldi, nvram_header_section_enum section)
1755{
1756
1757 NVRAM_FILE_NAME nvramname;
1758 kal_wchar filename[NVRAM_MAX_PATH_LEN];
1759 nvram_folder_enum nvram_folder;
1760 FS_HANDLE file_handle = FS_INVALID_FILE_HANDLE;
1761 kal_int32 result = FS_NO_ERROR;
1762 kal_int32 backup_file_num = 1;
1763 kal_bool mulpiple = KAL_FALSE;
1764 kal_bool ret_val = KAL_TRUE;
1765 nvram_ldi_header nv_header;
1766 nvram_ldi_appendix_header nv_appendix_header;
1767 nvram_ldi_ota_header *ldi_ota_header = &(nv_header.nv_ota_header);
1768 nvram_ldi_debug_header *ldi_debug_header = &(nv_header.nv_dbg_header);
1769 nvram_ldi_appendix_header *ldi_append_header = &nv_appendix_header;
1770 kal_uint32 ldi_hd_buffer_size = 0;
1771 kal_uint32 ldi_hd_offset = 0;
1772 kal_uint32 ldi_checksum_hd_offset = 0;
1773 #ifdef __NVRAM_LID_CACHE__
1774 kal_uint32 cache_offset = 0;
1775 kal_uint32 openOption = FS_READ_WRITE | FS_OPEN_NO_DIR | FS_CREATE;
1776 #endif
1777
1778 /* NVRAM GPT timeout assert start timer */
1779 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1780 start.u2Tick= NVRAM_WRITE_GPT_TIMEOUT;
1781 start.pfCallback=nvram_gpt_timeout_callback;
1782 start.vPara=NULL;
1783 #endif
1784
1785 kal_mem_set(&nv_header, 0x0, sizeof(nv_header));
1786 kal_mem_set(&nv_appendix_header, 0x0, sizeof(nv_appendix_header));
1787
1788 if(NVRAM_IS_ATTR_MULTIPLE(ldi->attr) || NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category))
1789 {
1790 mulpiple = KAL_TRUE;
1791 backup_file_num = 2;
1792 }
1793 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE);
1794 nvram_folder = nvram_query_folder_index(ldi->category);
1795 nvram_query_file_name(nvram_folder, nvramname, filename);
1796
1797 if(section == LDI_HEADER_ALL_SECTION) {
1798 nvram_prepare_ota_header(ldi_ota_header,ldi);
1799 nvram_prepare_debug_header(ldi_debug_header,ldi);
1800 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE){
1801 nvram_prepare_appendix_header(NVRAM_APPEND_TYPE_CHKSUM, ldi_append_header, ldi, NVRAM_LDI_APPENDIX_HEADER_SIZE);
1802 }
1803 ldi_hd_buffer_size = NVRAM_LDI_OTA_HEADER_SIZE + NVRAM_LDI_DEBUG_HEADER_SIZE;
1804 }
1805 else
1806 {
1807 if(NVRAM_IS_CATEGORY_CALIBRAT(ldi->category) || NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category)) {
1808 //CALIBRATION and IMPT_L4 need update debug section
1809 }
1810 else {
1811 return KAL_FALSE;
1812 }
1813 nvram_util_take_mutex(g_nvram_fs_mutex);
1814 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1815 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_START, (DCL_CTRL_DATA_T*)&start); //start timer
1816 #endif
1817 nvram_read_data_header(filename, LDI_HEADER_ALL_SECTION, (void*)&nv_header, NVRAM_LDI_HEADER_SIZE);
1818 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1819 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_STOP, (DCL_CTRL_DATA_T*)NULL); //stop timer
1820 #endif
1821 nvram_util_give_mutex(g_nvram_fs_mutex);
1822 }
1823 if(section == LDI_HEADER_DBG_SECTION)
1824 {
1825 if(NVRAM_IS_CATEGORY_CALIBRAT(ldi->category) || NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category)) {
1826 //CALIBRATION and IMPT_L4 need update debug section
1827 }
1828 else {
1829 return KAL_FALSE;
1830 }
1831 nvram_prepare_debug_header(ldi_debug_header,ldi);
1832 ldi_hd_offset = NVRAM_LDI_OTA_HEADER_SIZE;
1833 ldi_hd_buffer_size = NVRAM_LDI_DEBUG_HEADER_SIZE;
1834 }
1835 else if(section == LDI_HEADER_OTA_SECTION)
1836 {
1837 nvram_prepare_ota_header(ldi_ota_header,ldi);
1838 ldi_hd_offset = 0;
1839 ldi_hd_buffer_size = NVRAM_LDI_OTA_HEADER_SIZE;
1840 }
1841
1842 nvram_util_take_mutex(g_nvram_fs_mutex);
1843
1844 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
1845 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_START, (DCL_CTRL_DATA_T*)&start); //start timer
1846 #endif
1847
1848 do{
1849 #ifdef __NVRAM_LID_CACHE__
1850 if((!NVRAM_IS_ATTR_RAW_DATA(ldi->attr)) && NVRAM_WR_WITH_CACHE(ldi->LID))
1851 {
1852 if(NVRAM_IO_ERRNO_OK != (result = get_lid_cache_base_address(ldi, &cache_offset))) {
1853 ret_val = KAL_FALSE;
1854 break;
1855 }
1856 if(NVRAM_IO_ERRNO_OK != (result = nvram_write_data_to_cache(ldi, (void *)(&nv_header), NVRAM_LDI_HEADER_SIZE, cache_offset))){
1857 kal_prompt_trace(MOD_NVRAM, "NVRAM write header write fail:0x%x\n\r", result);
1858 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
1859 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
1860 kal_prompt_trace(MOD_NVRAM, "section:%d,cache_offset:%d\n\r", section,cache_offset);
1861
1862 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM write header at %d write fail:%d\n\r", __LINE__,result);
1863 if(NVRAM_IS_ATTR_FAULT_ASSERT(ldi->attr)) {
1864 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", ldi->LID, ldi->total_records, ldi->size);
1865 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
1866 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
1867 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "section:%d,cache_offset:%d\r\n", section,cache_offset);
1868 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_WRITE_HEADER_FAIL, ldi->LID);
1869 }
1870 ret_val = KAL_FALSE;
1871 break;
1872 }
1873 if(section == LDI_HEADER_ALL_SECTION)
1874 {
1875 set_reset_flag_by_ltable_entry(ldi);
1876 }
1877 mask_dirty_bit_by_ltable_entry(ldi, 0, 1);
1878 mask_valid_bit_by_ltable_entry(ldi, 0, 1);
1879
1880 //write appendix header
1881 ldi_checksum_hd_offset = nvram_appendix_header_offset(ldi);
1882 if((section == LDI_HEADER_ALL_SECTION) && (ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE)) {
1883 cache_offset = cache_offset + ldi_checksum_hd_offset;
1884 if (NVRAM_IO_ERRNO_OK != (result = nvram_write_data_to_cache(ldi, (void *)ldi_append_header, NVRAM_LDI_APPENDIX_HEADER_SIZE, cache_offset))) {
1885 kal_prompt_trace(MOD_NVRAM, "NVRAM appendix header write fail:0x%x\n\r", result);
1886 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
1887 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
1888 kal_prompt_trace(MOD_NVRAM, "cache_offset:%d\r\n",cache_offset);
1889
1890 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM appendix header at %d write fail :%d\n\r", __LINE__,result);
1891 if(NVRAM_IS_ATTR_FAULT_ASSERT(ldi->attr)) {
1892 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", ldi->LID, ldi->total_records, ldi->size);
1893 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
1894 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
1895 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "cache_offset:%d\r\n",cache_offset);
1896 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)result, NVRAM_ERROR_LOC_NVCACHE_ERRNO_WRITE_HEADER_FAIL, ldi->LID);
1897 }
1898 ret_val = KAL_FALSE;
1899 break;
1900 }
1901
1902 }
1903 nvram_cache_enqueue(ldi, 0, 1, openOption);
1904 send_event_to_nvram_cache();
1905 }
1906 else
1907 #endif
1908 {
1909 NVRAM_FS_START_EX(FS_OP_OPEN,filename);
1910 file_handle = FS_Open(filename, FS_READ_WRITE | FS_OPEN_NO_DIR | FS_CREATE);
1911 NVRAM_FS_END(FS_OP_OPEN,file_handle);
1912 if (file_handle < FS_NO_ERROR) {
1913 kal_prompt_trace(MOD_NVRAM, "NVRAM write header open fail:0x%x\n\r", file_handle);
1914 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
1915 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
1916 kal_prompt_trace(MOD_NVRAM, "section:%d\n\r", section);
1917
1918 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM write header at %d open fail:%d\n\r", __LINE__,result);
1919 if(NVRAM_IS_ATTR_FAULT_ASSERT(ldi->attr)) {
1920 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", ldi->LID, ldi->total_records, ldi->size);
1921 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
1922 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
1923 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "section:%d\r\n",section);
1924 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)file_handle, NVRAM_LOC_OPEN_NV_FOLDER_FAIL_4, ldi->LID);
1925 }
1926 ret_val = KAL_FALSE;
1927 }
1928 if(ldi_hd_offset && (file_handle > FS_NO_ERROR)) {
1929 NVRAM_FS_START(FS_OP_SEEK);
1930 result = FS_Seek(file_handle, ldi_hd_offset, FS_FILE_BEGIN);
1931 NVRAM_FS_END(FS_OP_SEEK,result);
1932 }
1933
1934 if (file_handle > FS_NO_ERROR){
1935 NVRAM_FS_START(FS_OP_WRITE);
1936 result = FS_Write(file_handle, (void *)(((kal_uint32)(&nv_header)) + ldi_hd_offset), ldi_hd_buffer_size, &ldi_hd_buffer_size);
1937 NVRAM_FS_END(FS_OP_WRITE,result);
1938 if (result < FS_NO_ERROR){
1939 kal_prompt_trace(MOD_NVRAM, "NVRAM write header write fail:0x%x\n\r", result);
1940 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
1941 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
1942 kal_prompt_trace(MOD_NVRAM, "section:%d\n\r", section);
1943
1944 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM write header at %d write fail:%d\n\r", __LINE__,result);
1945 if(NVRAM_IS_ATTR_FAULT_ASSERT(ldi->attr)) {
1946 NVRAM_FS_START(FS_OP_CLOSE);
1947 result = FS_Close(file_handle);
1948 NVRAM_FS_END(FS_OP_CLOSE,result);
1949 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", ldi->LID, ldi->total_records, ldi->size);
1950 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
1951 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
1952 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "section:%d\r\n",section);
1953 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)file_handle, NVRAM_LOC_OPEN_NV_FOLDER_FAIL_5, ldi->LID, result);
1954 }
1955 ret_val = KAL_FALSE;
1956 }
1957 #ifdef __NVRAM_LID_CACHE__
1958 else
1959 {
1960 if((!NVRAM_IS_ATTR_RAW_DATA(ldi->attr)) && NVRAM_WR_2_FILE_WITH_2_CACHE(ldi->LID))
1961 {
1962 get_lid_cache_base_address(ldi, &cache_offset);
1963 nvram_write_data_to_cache(ldi, (void *)(&nv_header), NVRAM_LDI_HEADER_SIZE, cache_offset);
1964 mask_valid_bit_by_ltable_entry(ldi, 0, 1);
1965 }
1966 }
1967 #endif
1968 }
1969
1970 //write appendix header
1971 ldi_checksum_hd_offset = nvram_appendix_header_offset(ldi);
1972 if((section == LDI_HEADER_ALL_SECTION) && (ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE)) {
1973 if(file_handle > FS_NO_ERROR){
1974 NVRAM_FS_START(FS_OP_WRITE);
1975 result = FS_Seek(file_handle, ldi_checksum_hd_offset, FS_FILE_BEGIN);
1976 NVRAM_FS_END(FS_OP_WRITE,result);
1977 NVRAM_FS_START(FS_OP_WRITE);
1978 result = FS_Write(file_handle, ldi_append_header, NVRAM_LDI_APPENDIX_HEADER_SIZE, &ldi_hd_buffer_size);
1979 NVRAM_FS_END(FS_OP_WRITE,result);
1980 }
1981 if ((file_handle > FS_NO_ERROR) &&(result < FS_NO_ERROR)) {
1982 kal_prompt_trace(MOD_NVRAM, "NVRAM appendix header write fail:0x%x\n\r", result);
1983 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
1984 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
1985
1986 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "[Error]NVRAM appendix header at %d write fail:%d\n\r", __LINE__,result);
1987 if(NVRAM_IS_ATTR_FAULT_ASSERT(ldi->attr)) {
1988 NVRAM_FS_START(FS_OP_CLOSE);
1989 result = FS_Close(file_handle);
1990 NVRAM_FS_END(FS_OP_CLOSE,result);
1991 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "LID:0x%04X, total_records:%d, record_size:%d\r\n", ldi->LID, ldi->total_records, ldi->size);
1992 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
1993 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP, "fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
1994 NVRAM_EXT_ASSERT(KAL_FALSE, (kal_uint32)file_handle, NVRAM_LOC_WRITE_FILE_FAIL_4, ldi->LID, result);
1995 }
1996 ret_val = KAL_FALSE;
1997 }
1998 #ifdef __NVRAM_LID_CACHE__
1999 else
2000 {
2001 if((!NVRAM_IS_ATTR_RAW_DATA(ldi->attr)) && NVRAM_WR_2_FILE_WITH_2_CACHE(ldi->LID))
2002 {
2003 get_lid_cache_base_address(ldi, &cache_offset);
2004 cache_offset = cache_offset + ldi_checksum_hd_offset;
2005 nvram_write_data_to_cache(ldi, ldi_append_header, NVRAM_LDI_APPENDIX_HEADER_SIZE, cache_offset);
2006
2007 }
2008 }
2009 #endif
2010
2011 }
2012 if(file_handle > FS_NO_ERROR) {
2013 NVRAM_FS_START(FS_OP_CLOSE);
2014 result = FS_Close(file_handle);
2015 NVRAM_FS_END(FS_OP_CLOSE,result);
2016 }
2017 }
2018 backup_file_num --;
2019 #if defined(__NVRAM_WRITE_PROTECT_ENABLE__)
2020 if (NVRAM_IS_CATEGORY_IMPORTANT_L4(ldi->category)) {
2021 // Don't write protect2
2022 break;
2023 }
2024 #endif
2025 if(!mulpiple)
2026 {
2027 break;
2028 }
2029 nvram_folder = nvram_query_folder_index_ex(ldi->category,KAL_FALSE);
2030 nvram_util_make_lid_filename(ldi, nvramname, KAL_FALSE);
2031 nvram_query_file_name(nvram_folder, nvramname, filename);
2032
2033 }while(backup_file_num > 0);
2034
2035 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
2036 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_STOP, (DCL_CTRL_DATA_T*)NULL); //stop timer
2037 #endif
2038
2039 nvram_util_give_mutex(g_nvram_fs_mutex);
2040 return ret_val;
2041}
2042
2043kal_bool nvram_read_data_header(const kal_wchar *filename, nvram_header_section_enum section, void* buffer, kal_uint32 buffer_size)
2044{
2045 kal_char s_filename[NVRAM_MAX_PATH_LEN];
2046 FS_HANDLE file_handle = FS_INVALID_FILE_HANDLE;
2047 kal_int32 result = FS_NO_ERROR;
2048 kal_bool ret_val = KAL_TRUE;
2049
2050 kal_uint32 ldi_hd_buffer_size = 0;
2051 kal_uint32 ldi_hd_buffer_data_size = 0;
2052 kal_uint32 ldi_hd_offset = 0;
2053 nvram_ldi_ota_header *ldi_ota_header;
2054
2055 #ifdef __NVRAM_LID_CACHE__
2056 nvram_ldi_appendix_header nv_appendix_header;
2057 nvram_ldi_appendix_header *ldi_append_header = &nv_appendix_header;
2058 kal_uint32 ldi_checksum_hd_offset = 0;
2059 kal_uint32 ldi_checksum_hd_buffer_size = NVRAM_LDI_APPENDIX_HEADER_SIZE;
2060 kal_uint32 ldi_checksum_hd_data_size = 0;
2061 kal_uint32 cache_offset = 0;
2062 kal_wchar wcfilename[NVRAM_FILE_LEN + 1];
2063 kal_char cfilename[NVRAM_FILE_LEN + 1];
2064
2065 nvram_ltable_entry_struct *ldi = NULL;
2066 #endif
2067
2068 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
2069 SGPT_CTRL_START_T start;
2070 #endif
2071
2072 if(section == LDI_HEADER_OTA_SECTION) {
2073 //ldi_hd_offset = 0;
2074 ldi_hd_buffer_size += NVRAM_LDI_OTA_HEADER_SIZE;
2075 }
2076 else if(section == LDI_HEADER_DBG_SECTION) {
2077 ldi_hd_offset = NVRAM_LDI_OTA_HEADER_SIZE;
2078 ldi_hd_buffer_size += NVRAM_LDI_DEBUG_HEADER_SIZE;
2079 }
2080 else if(section == LDI_HEADER_ALL_SECTION) {
2081 //ldi_hd_offset = 0;
2082 ldi_hd_buffer_size += (NVRAM_LDI_OTA_HEADER_SIZE + NVRAM_LDI_DEBUG_HEADER_SIZE);
2083 }
2084 else {
2085 kal_prompt_trace(MOD_NVRAM, "nvram_read_data_header:section 0x%x is wrong\n\r", section);
2086 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]%s section 0x%x is wrong\r\n",__FUNCTION__,section);
2087 return KAL_FALSE;
2088 }
2089
2090
2091 if(!buffer || (buffer_size < ldi_hd_buffer_size)) {
2092 return KAL_FALSE;
2093 }
2094
2095 /* NVRAM GPT timeout assert start timer */
2096 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
2097 start.u2Tick= NVRAM_READ_GPT_TIMEOUT;
2098 start.pfCallback=nvram_gpt_timeout_callback;
2099 start.vPara=NULL;
2100 #endif
2101
2102
2103 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
2104 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_START, (DCL_CTRL_DATA_T*)&start); //start timer
2105 #endif
2106
2107 #ifdef __NVRAM_LID_CACHE__
2108 kal_mem_set(cfilename,0,NVRAM_FILE_LEN + 1);
2109 kal_mem_set(wcfilename,0,(NVRAM_FILE_LEN + 1)*sizeof(kal_wchar));
2110 nvram_query_file_name_by_path((kal_wchar *)filename, wcfilename);
2111 kal_dchar2char((WCHAR *)wcfilename, cfilename);
2112
2113 nvram_util_get_data_item_by_fileprefix(&ldi, cfilename);
2114
2115 if(ldi == NULL)
2116 {
2117 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s Can't find the LID entry at %d,cfilename:%s\r\n",__FUNCTION__,__LINE__,cfilename);
2118 if(nvram_ptr->state == NVRAM_STATE_READY)
2119 {
2120 kal_prompt_trace(MOD_NVRAM, "cfilename:%s\n\r", cfilename);
2121 kal_prompt_trace(MOD_NVRAM, "Can not find the LID entry in the LID table:\n\r");
2122 NVRAM_EXT_ASSERT(KAL_FALSE,0 , NVRAM_LOC_LID_PTR_IS_NULL_4, ldi);
2123 }else
2124 {
2125 kal_prompt_trace(MOD_NVRAM, "cfilename:%s\n\r", cfilename);
2126 goto non_cache_read;
2127 }
2128 }
2129 if(NVRAM_RD_WITH_CACHE(ldi->LID))
2130 {
2131 if((!(NVRAM_IS_ATTR_RAW_DATA(ldi->attr))) && (check_valid_bit_by_ltable_entry(ldi, 0) || check_dirty_bit_by_ltable_entry(ldi, 0)))
2132 {
2133 do{
2134 get_lid_cache_base_address(ldi, &cache_offset);
2135 cache_offset = cache_offset + ldi_hd_offset;
2136 if ((result = nvram_read_header_from_cache(ldi, buffer, ldi_hd_buffer_size, cache_offset)) != KAL_TRUE) {
2137 kal_dchar2char((WCHAR *)filename, s_filename);
2138 kal_prompt_trace(MOD_NVRAM, "NVRAM read header read fail: 0x%x\n\r", result);
2139 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
2140 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
2141 kal_prompt_trace(MOD_NVRAM, "filename:%s\n\r", s_filename);
2142 kal_prompt_trace(MOD_NVRAM, "section:%d,cache_offset:%d\n\r", section,cache_offset);
2143
2144 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]NVRAM read header at %d read fail:%d\r\n", __LINE__,result);
2145 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
2146 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
2147 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"filename:%s\r\n", s_filename);
2148 NVRAM_DEBUG_DUMP(NVRAM_INFO_DUMP,"section:%d,cache_offset:%d\r\n", section,cache_offset);
2149 ret_val = KAL_FALSE;
2150 break;
2151 }
2152 }while(0);
2153 }else
2154 {
2155 goto non_cache_read;
2156 }
2157 }else
2158 #endif
2159 {
2160 #ifdef __NVRAM_LID_CACHE__
2161 non_cache_read:
2162 #endif
2163 #if 0// (defined(__NVRAM_FS_OPERATION_COMPACT__) && defined(__CCCIFS_SUPPORT__) && defined(__MTK_TARGET__)) || defined(__NVRAM_FS_CMPT_SIMULATION__)
2164/* under construction !*/
2165/* under construction !*/
2166/* under construction !*/
2167/* under construction !*/
2168/* under construction !*/
2169/* under construction !*/
2170/* under construction !*/
2171/* under construction !*/
2172/* under construction !*/
2173/* under construction !*/
2174/* under construction !*/
2175/* under construction !*/
2176/* under construction !*/
2177/* under construction !*/
2178/* under construction !*/
2179/* under construction !*/
2180/* under construction !*/
2181/* under construction !*/
2182/* under construction !*/
2183/* under construction !*/
2184/* under construction !*/
2185/* under construction !*/
2186/* under construction !*/
2187/* under construction !*/
2188/* under construction !*/
2189/* under construction !*/
2190 #else
2191 NVRAM_FS_START_EX(FS_OP_OPEN,filename);
2192 file_handle = FS_Open(filename, FS_READ_ONLY | FS_OPEN_SHARED |FS_OPEN_NO_DIR);
2193 NVRAM_FS_END(FS_OP_OPEN,file_handle);
2194 if (file_handle < FS_NO_ERROR) {
2195 kal_dchar2char((WCHAR *)filename, s_filename);
2196 kal_prompt_trace(MOD_NVRAM, "NVRAM read header open fail:0x%x\n\r", file_handle);
2197 kal_prompt_trace(MOD_NVRAM, "filename:%s\n\r", s_filename);
2198 kal_prompt_trace(MOD_NVRAM, "section:%d\n\r", section);
2199
2200 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]NVRAM read header at %d open fail:%d\r\n", __LINE__,file_handle);
2201 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"filename:%s\r\n", s_filename);
2202 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"section:%d\r\n", section);
2203 ret_val = KAL_FALSE;
2204 }
2205 if(ldi_hd_offset && (file_handle > FS_NO_ERROR)) {
2206 NVRAM_FS_START(FS_OP_SEEK);
2207 result = FS_Seek(file_handle, ldi_hd_offset, FS_FILE_BEGIN);
2208 NVRAM_FS_END(FS_OP_SEEK,result);
2209 }
2210
2211 if (file_handle > FS_NO_ERROR) {
2212 NVRAM_FS_START(FS_OP_READ);
2213 result = FS_Read(file_handle, buffer, ldi_hd_buffer_size, &ldi_hd_buffer_data_size);
2214 NVRAM_FS_END(FS_OP_READ,result);
2215 if((result < FS_NO_ERROR) || (ldi_hd_buffer_size != ldi_hd_buffer_data_size)){
2216 kal_dchar2char((WCHAR *)filename, s_filename);
2217 kal_prompt_trace(MOD_NVRAM, "NVRAM read header read fail:0x%x, 0x%x\n\r", file_handle, result);
2218 kal_prompt_trace(MOD_NVRAM, "filename:%s\n\r", s_filename);
2219 kal_prompt_trace(MOD_NVRAM, "section:%d\n\r", section);
2220
2221 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]NVRAM read header at %d read fail:%d\r\n", __LINE__,result);
2222 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"filename:%s\r\n", s_filename);
2223 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"section:%d\r\n", section);
2224 ret_val = KAL_FALSE;
2225 }
2226 }
2227 if(file_handle > FS_NO_ERROR) {
2228 NVRAM_FS_START(FS_OP_CLOSE);
2229 result = FS_Close(file_handle);
2230 NVRAM_FS_END(FS_OP_CLOSE,result);
2231 }
2232 #endif
2233
2234 #ifdef __NVRAM_LID_CACHE__
2235 if((ldi != NULL) && (!NVRAM_IS_ATTR_RAW_DATA(ldi->attr)) && NVRAM_RD_WITH_CACHE(ldi->LID))
2236 {
2237 if(ret_val == KAL_TRUE)
2238 {
2239 update_cache_header(ldi, buffer, ldi_hd_offset, ldi_hd_buffer_size);
2240 mask_valid_bit_by_ltable_entry(ldi, 0, 1);
2241
2242 //read appendix header
2243 if(ldi->attr & NVRAM_ATTR_CHKSUM_INTEGRATE)
2244 {
2245 ldi_checksum_hd_offset = nvram_appendix_header_offset(ldi);
2246 NVRAM_FS_START_EX(FS_OP_OPEN, filename);
2247 file_handle = FS_Open(filename, FS_READ_ONLY | FS_OPEN_SHARED |FS_OPEN_NO_DIR);
2248 NVRAM_FS_END(FS_OP_OPEN,file_handle);
2249
2250 if (file_handle < FS_NO_ERROR) {
2251 kal_dchar2char((WCHAR *)filename, s_filename);
2252 kal_prompt_trace(MOD_NVRAM, "NVRAM read header open fail:0x%x\n\r", file_handle);
2253 kal_prompt_trace(MOD_NVRAM, "filename:%s\n\r", s_filename);
2254 kal_prompt_trace(MOD_NVRAM, "section:%d\n\r", section);
2255
2256 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]NVRAM read header at %d open fail:%d\r\n", __LINE__,file_handle);
2257 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"filename:%s\r\n", s_filename);
2258 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"section:%d\r\n", section);
2259 ret_val = KAL_FALSE;
2260 }
2261
2262 if(file_handle > FS_NO_ERROR)
2263 {
2264 NVRAM_FS_START(FS_OP_SEEK);
2265 result = FS_Seek(file_handle, ldi_checksum_hd_offset, FS_FILE_BEGIN);
2266 NVRAM_FS_END(FS_OP_SEEK,result);
2267 }
2268
2269 if (file_handle > FS_NO_ERROR) {
2270 NVRAM_FS_START(FS_OP_READ);
2271 result = FS_Read(file_handle, ldi_append_header, ldi_checksum_hd_buffer_size, &ldi_checksum_hd_data_size);
2272 NVRAM_FS_END(FS_OP_READ,result);
2273 if( (result < FS_NO_ERROR) || (ldi_checksum_hd_buffer_size != ldi_checksum_hd_data_size)){
2274 kal_dchar2char((WCHAR *)filename, s_filename);
2275 kal_prompt_trace(MOD_NVRAM, "NVRAM read header read fail:0x%x, 0x%x\n\r", file_handle, result);
2276 kal_prompt_trace(MOD_NVRAM, "filename:%s\n\r", s_filename);
2277 kal_prompt_trace(MOD_NVRAM, "section:%d\n\r", section);
2278
2279 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]NVRAM read header at %d read fail:%d\r\n", __LINE__,result);
2280 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"filename:%s\r\n", s_filename);
2281 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"section:%d\r\n", section);
2282 ret_val = KAL_FALSE;
2283 }
2284 }
2285
2286 if(file_handle > FS_NO_ERROR)
2287 {
2288 NVRAM_FS_START(FS_OP_CLOSE);
2289 kal_int32 fs_result = FS_Close(file_handle);
2290 NVRAM_FS_END(FS_OP_CLOSE,fs_result);
2291 }
2292 if(ret_val)
2293 {
2294 update_cache_header(ldi, ldi_append_header, ldi_checksum_hd_offset, ldi_checksum_hd_buffer_size);
2295 }
2296 }
2297 }
2298 }
2299 #endif
2300 }
2301
2302 #if defined(__NVRAM_ACCESS_TIMEOUT_ASSERT__)
2303 DclSGPT_Control(nvram_gpt_handle, SGPT_CMD_STOP, (DCL_CTRL_DATA_T*)NULL); //stop timer
2304 #endif
2305
2306 if((is_nvram_in_ota_flow != KAL_TRUE) && (ret_val != KAL_FALSE) && (section & LDI_HEADER_OTA_SECTION)) {
2307 ldi_ota_header = (nvram_ldi_ota_header*)buffer;
2308 ldi_hd_offset = nvram_data_header_checksum((kal_uint8*)ldi_ota_header, (NVRAM_LDI_OTA_HEADER_SIZE - sizeof(ldi_ota_header->checksum)));
2309 if((ldi_ota_header->checksum != ldi_hd_offset) ||
2310 memcmp(ldi_ota_header->header, "LDI", 4)) {
2311 ret_val = KAL_FALSE;
2312 }
2313 }
2314
2315 return ret_val;
2316}
2317
2318#ifdef __NVRAM_LID_CACHE__
2319kal_bool nvram_cache_read_ota_header(const kal_wchar *filename, FS_HANDLE hFile, void* buffer, kal_uint32 buffer_size)
2320{
2321 kal_uint32 len;
2322 nvram_ldi_ota_header *ldi_ota_header;
2323 nvram_ldi_header nv_header;
2324 kal_uint16 checksum;
2325 kal_uint32 cache_offset;
2326 kal_wchar wcfilename[NVRAM_FILE_LEN + 1];
2327 kal_char cfilename[NVRAM_FILE_LEN + 1];
2328 nvram_ltable_entry_struct *ldi = NULL;
2329 kal_int32 result = FS_NO_ERROR;
2330 kal_bool ret_val = KAL_TRUE;
2331 kal_int32 ret = FS_NO_ERROR;
2332
2333 kal_mem_set(cfilename,0,NVRAM_FILE_LEN + 1);
2334 kal_mem_set(wcfilename,0,(NVRAM_FILE_LEN + 1)*sizeof(kal_wchar));
2335
2336 nvram_query_file_name_by_path((kal_wchar *)filename, wcfilename);
2337 kal_dchar2char((WCHAR *)wcfilename, cfilename);
2338 nvram_util_get_data_item_by_fileprefix(&ldi, cfilename);
2339 if(ldi == NULL)
2340 {
2341 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s Can not find the LID entry at %d,cfilename:%s\r\n",__FUNCTION__,__LINE__,cfilename);
2342 if(nvram_ptr->state == NVRAM_STATE_READY)
2343 {
2344 kal_prompt_trace(MOD_NVRAM, "cfilename:%s\n\r", cfilename);
2345 kal_prompt_trace(MOD_NVRAM, "Can not find the LID entry in the LID table:\n\r");
2346 NVRAM_EXT_ASSERT(KAL_FALSE,0 , NVRAM_LOC_LID_PTR_IS_NULL_5, ldi);
2347 }
2348 else
2349 {
2350 kal_prompt_trace(MOD_NVRAM, "cfilename:%s\n\r", cfilename);
2351 goto ota_non_cache;
2352 }
2353 }
2354 if(NVRAM_RD_WITH_CACHE(ldi->LID))
2355 {
2356 if((!(NVRAM_IS_ATTR_RAW_DATA(ldi->attr))) && (check_valid_bit_by_ltable_entry(ldi, 0) || check_dirty_bit_by_ltable_entry(ldi, 0)))
2357 {
2358 do{
2359 if(NVRAM_IO_ERRNO_OK != (result = get_lid_cache_base_address(ldi, &cache_offset))) {
2360 ret_val = KAL_FALSE;
2361 break;
2362 }
2363
2364 if ((result = nvram_read_header_from_cache(ldi, buffer, buffer_size, cache_offset)) != KAL_TRUE) {
2365 kal_prompt_trace(MOD_NVRAM, "NVRAM read header read fail:0x%x\n\r", result);
2366 kal_prompt_trace(MOD_NVRAM, "category:0x%x, attr:0x%x\n\r", ldi->category, ldi->attr);
2367 kal_prompt_trace(MOD_NVRAM, "fileprefix:%s, fileverno:%s\n\r", ldi->fileprefix, ldi->fileverno);
2368 kal_prompt_trace(MOD_NVRAM, "filename:%s\n\r", cfilename);
2369
2370 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"[Error]NVRAM read header at %d read fail:%d\r\n", __LINE__,result);
2371 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"category:0x%08X, attr:0x%08X\r\n", ldi->category, ldi->attr);
2372 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"fileprefix:%s, fileverno:%s\r\n", ldi->fileprefix, ldi->fileverno);
2373 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"filename:%s\r\n", cfilename);
2374 ret_val = KAL_FALSE;
2375 break;
2376 }
2377 }while(0);
2378 }else
2379 {
2380 goto ota_non_cache;
2381 }
2382 }else
2383 {
2384 ota_non_cache:
2385 kal_mem_set(&nv_header, 0x0, sizeof(nv_header));
2386 NVRAM_FS_START(FS_OP_SEEK);
2387 ret = FS_Seek(hFile, 0x0, FS_FILE_BEGIN);
2388 NVRAM_FS_END(FS_OP_SEEK,ret);
2389 NVRAM_FS_START(FS_OP_READ);
2390 result = FS_Read(hFile, (void *)(&nv_header), NVRAM_LDI_HEADER_SIZE, &len);
2391 NVRAM_FS_END(FS_OP_READ,result);
2392 if((result >= FS_NO_ERROR) && (len == NVRAM_LDI_HEADER_SIZE))
2393 {
2394 kal_mem_cpy((char *)buffer, (char *)(&nv_header), buffer_size);
2395 ret_val = KAL_TRUE;
2396 }
2397
2398 if((ldi != NULL) && NVRAM_RD_WITH_CACHE(ldi->LID))
2399 {
2400 if(ret_val == KAL_TRUE)
2401 {
2402 update_cache_header(ldi, (void *)(&nv_header), 0, NVRAM_LDI_HEADER_SIZE);
2403 mask_valid_bit_by_ltable_entry(ldi, 0, 1);
2404 }
2405 }
2406
2407 }
2408
2409 ldi_ota_header = (nvram_ldi_ota_header*)buffer;
2410 checksum = nvram_data_header_checksum((kal_uint8*)ldi_ota_header, (NVRAM_LDI_OTA_HEADER_SIZE - sizeof(ldi_ota_header->checksum)));
2411 if((ldi_ota_header->checksum != checksum) ||
2412 memcmp(ldi_ota_header->header, "LDI", 4)) {
2413 return KAL_FALSE;
2414 }
2415
2416 return ret_val;
2417}
2418#endif
2419
2420kal_bool nvram_read_ota_header(FS_HANDLE hFile, void* buffer, kal_uint32 buffer_size)
2421{
2422 kal_uint32 len;
2423 nvram_ldi_ota_header *ldi_ota_header;
2424 kal_uint16 checksum;
2425 kal_int32 fs_result = FS_NO_ERROR;
2426
2427 NVRAM_FS_START(FS_OP_SEEK);
2428 fs_result = FS_Seek(hFile, 0x0, FS_FILE_BEGIN);
2429 NVRAM_FS_END(FS_OP_SEEK,fs_result);
2430 NVRAM_FS_START(FS_OP_READ);
2431 fs_result = FS_Read(hFile, buffer, buffer_size, &len);
2432 NVRAM_FS_END(FS_OP_READ,fs_result);
2433 ldi_ota_header = (nvram_ldi_ota_header*)buffer;
2434 checksum = nvram_data_header_checksum((kal_uint8*)ldi_ota_header, (NVRAM_LDI_OTA_HEADER_SIZE - sizeof(ldi_ota_header->checksum)));
2435 if((ldi_ota_header->checksum != checksum) ||
2436 memcmp(ldi_ota_header->header, "LDI", 4)) {
2437 return KAL_FALSE;
2438 }
2439 return KAL_TRUE;
2440}
2441
2442
2443static kal_uint32 nvram_ota_wstrtol(kal_wchar *wstr, kal_uint8 length)
2444{
2445 kal_uint32 ret = 0;
2446 kal_char c;
2447
2448 for(;length > 0; length--, wstr++) {
2449 ret <<= 4;
2450 c = (kal_char)*wstr;
2451 if(c >= '0' && c <= '9') {
2452 c -= '0';
2453 }
2454 else if(c >= 'a' && c <= 'f') {
2455 c -= 'a';
2456 c += 10;
2457 }
2458 else if(c >= 'A' && c <= 'F') {
2459 c -= 'A';
2460 c += 10;
2461 }
2462 else {
2463 return 0;
2464 }
2465 ret += (kal_uint8)c;
2466 }
2467 return ret;
2468}
2469
2470static void nvram_ota_convert_filename(kal_wchar *filename, kal_uint16 *verno, kal_uint32 *size)
2471{
2472 NVRAM_OTA_FILE_NAME *ota_file = (NVRAM_OTA_FILE_NAME*)filename;
2473
2474 *verno = nvram_ota_wstrtol(ota_file->fileverno, FILE_VERNO_LEN);
2475 *size = nvram_ota_wstrtol(ota_file->record, 8);
2476}
2477static kal_uint8 nvram_ota_search_overdue_file(NVRAM_FULL_PATH *filenameArry, kal_wchar *filename)
2478{
2479 kal_uint8 i, ret = 0;
2480 kal_uint16 verno[NVRAM_OTA_RESERVE_CNT];
2481 kal_uint32 size[NVRAM_OTA_RESERVE_CNT];
2482 kal_wchar *s_filename = filename;
2483 kal_uint16 val_v;
2484 kal_uint32 val_s;
2485
2486 // remove path
2487 filename = kal_wstrrchr(s_filename, '\\');
2488 if(filename)
2489 filename++;
2490
2491 // find same filename
2492 for(i=0; i<NVRAM_OTA_RESERVE_CNT; i++) {
2493 if(!kal_wstrcmp(filenameArry[i], filename)) {
2494 return i;
2495 }
2496 nvram_ota_convert_filename(filenameArry[i], &verno[i], &size[i]);
2497 }
2498
2499 // find the oldest verno
2500 val_v = 0xFFFF;
2501 for(i=0; i<NVRAM_OTA_RESERVE_CNT; i++) {
2502 if(verno[i] < val_v) {
2503 val_v = verno[i];
2504 ret = i;
2505 }
2506 }
2507
2508 // find the smallest record & size
2509 val_s = 0xFFFFFFFF;
2510 for(i=0; i<NVRAM_OTA_RESERVE_CNT; i++) {
2511 if((verno[i] == val_v) && (size[i] < val_s)) {
2512 val_s = size[i];
2513 ret = i;
2514 }
2515 }
2516
2517 return ret;
2518}
2519
2520kal_bool nvram_ota_backup_file(const kal_wchar *s_filename, nvram_ldi_ota_header *ldi_ota_header)
2521{
2522 NVRAM_FULL_PATH d_filename;
2523 NVRAM_FULL_PATH f_filename;
2524 kal_wchar *filename;
2525 NVRAM_FULL_PATH filenameArry[NVRAM_OTA_RESERVE_CNT];
2526 FS_HANDLE handle;
2527 FS_DOSDirEntry fileinfo;
2528 kal_uint8 cnt = 0;
2529 kal_int32 result = FS_NO_ERROR;
2530 // create destination filename
2531 filename = kal_wstrrchr(s_filename, '\\');
2532 if(filename++) {
2533 // skip B file
2534 if(filename[FILE_PREFIX_LEN] == 'B') {
2535 NVRAM_FS_START_EX(FS_OP_DELETE,s_filename);
2536 result = FS_Delete(s_filename);
2537 NVRAM_FS_END(FS_OP_DELETE,result);
2538 return KAL_TRUE;
2539 }
2540 kal_wsprintf(d_filename, "%s\\%w_%04X%04X", NVRAM_FS_OTA_DATAITEM_PATH, filename, ldi_ota_header->total_records, ldi_ota_header->record_size);
2541 }
2542 else {
2543 return KAL_FALSE;
2544 }
2545
2546 // scan backup file
2547 kal_wsprintf(f_filename, "%s\\%w", NVRAM_FS_OTA_DATAITEM_PATH, filename);
2548 filename = kal_wstrrchr(f_filename, '\\');
2549 kal_wstrcpy((filename + (FILE_PREFIX_LEN + 1)), L"*");
2550 filename = filenameArry[0];
2551 handle = FS_FindFirst(f_filename, 0, FS_ATTR_DIR, &fileinfo, filename, NVRAM_MAX_PATH_LEN);
2552 if(handle > FS_NO_ERROR) {
2553 for(cnt = 1; cnt < NVRAM_OTA_RESERVE_CNT; cnt++) {
2554 filename = filenameArry[cnt];
2555 NVRAM_FS_START(FS_OP_FINDNEXT);
2556 result = FS_FindNext(handle, &fileinfo, filename, NVRAM_MAX_PATH_LEN);
2557 NVRAM_FS_END(FS_OP_FINDNEXT,result);
2558 if(result != FS_NO_ERROR) {
2559 break;
2560 }
2561 }
2562 NVRAM_FS_START(FS_OP_FINDCLOSE);
2563 result = FS_FindClose(handle);
2564 NVRAM_FS_END(FS_OP_FINDCLOSE,result);
2565 }
2566
2567 // delete overdue file
2568 if(cnt >= NVRAM_OTA_RESERVE_CNT) {
2569 cnt = nvram_ota_search_overdue_file(filenameArry,(kal_wchar *)d_filename);
2570 kal_wsprintf(f_filename, "%s\\%w", NVRAM_FS_OTA_DATAITEM_PATH, filenameArry[cnt]);
2571 NVRAM_FS_START_EX(FS_OP_DELETE,f_filename);
2572 result = FS_Delete(f_filename);
2573 NVRAM_FS_END(FS_OP_DELETE,result);
2574 }
2575
2576 // backup file
2577 NVRAM_FS_START_EX(FS_OP_MOVE,d_filename);
2578 result = FS_Move(s_filename, d_filename, FS_MOVE_COPY, NULL, NULL, 0);
2579 NVRAM_FS_END(FS_OP_MOVE,result);
2580 if(result != FS_NO_ERROR) {
2581 return KAL_FALSE;
2582 }
2583 NVRAM_FS_START_EX(FS_OP_DELETE,s_filename);
2584 result = FS_Delete(s_filename);
2585 NVRAM_FS_END(FS_OP_DELETE,result);
2586 return KAL_TRUE;
2587}
2588
2589kal_bool nvram_ota_search_backup_file(const kal_char *s_filename, NVRAM_FULL_PATH *r_filename)
2590{
2591 FS_HANDLE handle;
2592 FS_DOSDirEntry fileinfo;
2593 kal_wchar *filename_start;
2594 kal_int32 result = FS_NO_ERROR;
2595 nvram_query_file_name(NVRAM_NVD_BACKUP, (kal_char *)s_filename, (kal_wchar*)r_filename);
2596 filename_start = kal_wstrrchr((const WCHAR*)r_filename, '\\');
2597 NVRAM_FS_START_EX(FS_OP_FINDFIRST,r_filename);
2598 handle = FS_FindFirst((const WCHAR*)r_filename, 0, FS_ATTR_DIR, &fileinfo, (filename_start + 1), NVRAM_MAX_PATH_LEN);
2599 NVRAM_FS_END(FS_OP_FINDFIRST,handle);
2600 if(handle > FS_NO_ERROR) {
2601 NVRAM_FS_START(FS_OP_FINDCLOSE);
2602 result = FS_FindClose(handle);
2603 NVRAM_FS_END(FS_OP_FINDCLOSE,result);
2604 return KAL_TRUE;
2605 }
2606 return KAL_FALSE;
2607}
2608kal_bool nvram_ota_restore_file(nvram_ltable_entry_struct *ldi)
2609{
2610 NVRAM_FILE_NAME nvramname;
2611 kal_wchar s_filename[NVRAM_MAX_PATH_LEN];
2612 kal_wchar d_filename[NVRAM_MAX_PATH_LEN];
2613 nvram_folder_enum nvram_folder;
2614 nvram_ldi_ota_header ota_hdr;
2615 kal_bool hdr_ret;
2616 kal_int32 result = FS_NO_ERROR;
2617 nvram_util_make_lid_filename(ldi, nvramname, KAL_TRUE);
2618 nvram_folder = nvram_query_folder_index(ldi->category);
2619 nvram_query_file_name(nvram_folder, nvramname, d_filename);
2620
2621 kal_wsprintf(s_filename, "%s\\%s_%04X%04X", NVRAM_FS_OTA_DATAITEM_PATH, nvramname, ldi->total_records, ldi->size);
2622 hdr_ret = nvram_read_data_header(s_filename, LDI_HEADER_OTA_SECTION, (void*)&ota_hdr, NVRAM_LDI_OTA_HEADER_SIZE);
2623 if( (hdr_ret == KAL_TRUE) &&
2624 (ota_hdr.LID == ldi->LID) &&
2625 (ota_hdr.total_records == ldi->total_records) &&
2626 (ota_hdr.record_size == ldi->size) &&
2627 (ota_hdr.ldi_category == ldi->category) )
2628 {
2629 //LDI recover hit
2630 NVRAM_FS_START_EX(FS_OP_MOVE,d_filename);
2631 result = FS_Move(s_filename, d_filename, FS_MOVE_COPY, NULL, NULL, 0);
2632 NVRAM_FS_END(FS_OP_MOVE,result);
2633 if(result != FS_NO_ERROR) {
2634 return KAL_FALSE;
2635 }
2636
2637 if(NVRAM_IS_ATTR_MULTIPLE(ldi->attr)) {
2638 nvram_util_make_lid_filename(ldi, nvramname, KAL_FALSE);
2639 nvram_query_file_name(nvram_folder, nvramname, d_filename);
2640 NVRAM_FS_START_EX(FS_OP_MOVE,d_filename);
2641 result = FS_Move(s_filename, d_filename, FS_MOVE_COPY, NULL, NULL, 0);
2642 NVRAM_FS_END(FS_OP_MOVE,result);
2643 }
2644 return KAL_TRUE;
2645 }
2646
2647 return KAL_FALSE;
2648}
2649
2650kal_uint32 nvram_ota_buffer_size()
2651{
2652 return NVRAM_LID_BITMAP_SIZE;
2653}
2654
2655#ifdef __NVRAM_INIT_LID_BUFFER__
2656void nvram_init_lid_buffer_prepare(void)
2657{
2658 kal_uint32 size = 0;
2659 kal_uint16 i;
2660 nvram_ltable_entry_struct *ldi = NULL;
2661
2662 for(i = 0; i < lid_buffer_list_num; i++) {
2663 if(!nvram_util_get_data_item(&ldi, lid_buffer_list[i].LID)) {
2664 continue;
2665 }
2666 lid_buffer_list[i].ldi_ptr = ldi;
2667 size += (ldi->size * ldi->total_records);
2668 }
2669 NVRAM_EXT_ASSERT((size < MAX_NVRAM_RECORD_SIZE), size, NVRAM_LOC_INIT_BUFFER_OVERFLOW, MAX_NVRAM_RECORD_SIZE);
2670 nvram_init_lid_buffer = (kal_uint8 *)get_ctrl_buffer(size);
2671 for(size = 0, i = 0; i < lid_buffer_list_num; i++) {
2672 if(lid_buffer_list[i].ldi_ptr == NULL) {
2673 continue;
2674 }
2675 lid_buffer_list[i].buffer = (nvram_init_lid_buffer + size);
2676 size += (lid_buffer_list[i].ldi_ptr->size * lid_buffer_list[i].ldi_ptr->total_records);
2677 lid_buffer_list[i].status = NVRAM_INIT_LID_BUF_ENPTY;
2678 }
2679 nvram_init_lid_buffer_en = KAL_TRUE;
2680}
2681
2682nvram_errno_enum nvram_init_lid_buffer_read(nvram_lid_enum LID, kal_uint32 rec_index, kal_uint16 rec_amount, kal_uint8 *buffer, kal_uint32 buffer_size)
2683{
2684 kal_uint16 i;
2685 kal_uint32 size, offset;
2686
2687 if(!nvram_init_lid_buffer_en) {
2688 return NVRAM_ERRNO_NOT_READY;
2689 }
2690 for(i = 0; i < lid_buffer_list_num; i++) {
2691 if(LID == lid_buffer_list[i].LID) {
2692 if(lid_buffer_list[i].ldi_ptr == NULL ||
2693 lid_buffer_list[i].status == NVRAM_INIT_LID_BUF_INVALID ||
2694 lid_buffer_list[i].status == NVRAM_INIT_LID_BUF_READ ) {
2695 return NVRAM_ERRNO_FAIL;
2696 }
2697 if(lid_buffer_list[i].status == NVRAM_INIT_LID_BUF_ENPTY) {
2698 lid_buffer_list[i].status = NVRAM_INIT_LID_BUF_READ;
2699 if(NVRAM_ERRNO_SUCCESS != nvram_read_data_item(lid_buffer_list[i].ldi_ptr,
2700 1,
2701 lid_buffer_list[i].ldi_ptr->total_records,
2702 lid_buffer_list[i].buffer,
2703 (lid_buffer_list[i].ldi_ptr->size * lid_buffer_list[i].ldi_ptr->total_records) ))
2704 {
2705 lid_buffer_list[i].status = NVRAM_INIT_LID_BUF_INVALID;
2706 return NVRAM_ERRNO_FAIL;
2707 }
2708 lid_buffer_list[i].status = NVRAM_INIT_LID_BUF_VALID;
2709 }
2710 size = (lid_buffer_list[i].ldi_ptr->size * rec_amount);
2711 offset = ((rec_index - 1) * lid_buffer_list[i].ldi_ptr->size);
2712 if((size > buffer_size) ||
2713 ((rec_amount + rec_index - 1) > lid_buffer_list[i].ldi_ptr->total_records) )
2714 {
2715 return NVRAM_ERRNO_FAIL;
2716 }
2717 nvram_util_take_mutex(g_nvram_fs_mutex);
2718 kal_mem_cpy(buffer, (lid_buffer_list[i].buffer + offset), size);
2719 nvram_util_give_mutex(g_nvram_fs_mutex);
2720 return NVRAM_ERRNO_SUCCESS;
2721 }
2722 }
2723 return NVRAM_IO_ERRNO_INVALID_LID;
2724}
2725
2726nvram_errno_enum nvram_init_lid_buffer_write(nvram_lid_enum LID, kal_uint32 rec_index, kal_uint16 rec_amount, kal_uint8 *buffer, kal_uint32 buffer_size)
2727{
2728 kal_uint16 i;
2729 kal_uint32 size, offset;
2730
2731 if(!nvram_init_lid_buffer_en) {
2732 return NVRAM_ERRNO_NOT_READY;
2733 }
2734 for(i = 0; i < lid_buffer_list_num; i++) {
2735 if(LID == lid_buffer_list[i].LID) {
2736 if(lid_buffer_list[i].ldi_ptr == NULL ||
2737 lid_buffer_list[i].status == NVRAM_INIT_LID_BUF_INVALID ||
2738 lid_buffer_list[i].status == NVRAM_INIT_LID_BUF_ENPTY)
2739 {
2740 return NVRAM_ERRNO_FAIL;
2741 }
2742 size = (lid_buffer_list[i].ldi_ptr->size * rec_amount);
2743 offset = ((rec_index - 1) * lid_buffer_list[i].ldi_ptr->size);
2744 if((buffer_size > size) ||
2745 ((rec_amount + rec_index - 1) > lid_buffer_list[i].ldi_ptr->total_records) )
2746 {
2747 return NVRAM_ERRNO_FAIL;
2748 }
2749 nvram_util_take_mutex(g_nvram_fs_mutex);
2750 kal_mem_cpy((lid_buffer_list[i].buffer + offset), buffer, size);
2751 lid_buffer_list[i].status = NVRAM_INIT_LID_BUF_DIRTY;
2752 nvram_util_give_mutex(g_nvram_fs_mutex);
2753 return NVRAM_ERRNO_SUCCESS;
2754 }
2755 }
2756 return NVRAM_IO_ERRNO_INVALID_LID;
2757}
2758
2759void nvram_init_lid_buffer_writeback(void)
2760{
2761 kal_uint16 i,idx;
2762 kal_uint32 offset;
2763
2764 if(!nvram_init_lid_buffer_en) {
2765 return;
2766 }
2767 nvram_init_lid_buffer_en = KAL_FALSE;
2768 for(i = 0; i < lid_buffer_list_num; i++) {
2769 if((lid_buffer_list[i].ldi_ptr == NULL) || (lid_buffer_list[i].status != NVRAM_INIT_LID_BUF_DIRTY)) {
2770 continue;
2771 }
2772 for(idx = 0; idx < lid_buffer_list[i].ldi_ptr->total_records; idx++) {
2773 offset = (idx * lid_buffer_list[i].ldi_ptr->size);
2774 nvram_write_data_item(lid_buffer_list[i].ldi_ptr, (idx + 1), (lid_buffer_list[i].buffer + offset), KAL_FALSE);
2775 }
2776 }
2777 free_ctrl_buffer(nvram_init_lid_buffer);
2778}
2779#endif
2780
2781void nvram_get_trace_configuration()
2782{
2783 WCHAR filename[NVRAM_MAX_PATH_LEN];
2784 FS_HANDLE file_handle = FS_INVALID_FILE_HANDLE;
2785 kal_int32 result = FS_NO_ERROR;
2786 kal_char trace_config[4]= {0};
2787 kal_uint32 len = 0;
2788
2789 kal_mem_set(filename,0,NVRAM_MAX_PATH_LEN * sizeof(WCHAR));
2790 kal_wsprintf(filename, "%s\\%s", NVRAM_FS_IMPORTNT_DATAITEM_PATH,"md_nv_config.txt");
2791
2792 kal_mem_set(nvram_trace_filename,0,NVRAM_MAX_PATH_LEN * sizeof(kal_wchar));
2793 kal_wsprintf(nvram_trace_filename, "Z:\\%s","nvram_trace_log.log");
2794
2795 NVRAM_FS_START(FS_OP_OPEN);
2796 file_handle = FS_Open(filename, FS_READ_ONLY | FS_OPEN_SHARED |FS_OPEN_NO_DIR);
2797 NVRAM_FS_END(FS_OP_OPEN,file_handle);
2798 if (file_handle < FS_NO_ERROR) {
2799 if(nvram_ee_info != NULL){
2800 nvram_ee_info->nvram_trace_setting = 0;
2801 nvram_ee_info->nvram_access_trace_setting_file_val = NVRAM_OPEN_TRACE_SETTING_FILE_FAIL;
2802 g_access_trace_setting_file_result = NVRAM_OPEN_TRACE_SETTING_FILE_FAIL;
2803 g_nvram_trace_setting = 0;
2804 }
2805 return;
2806 }
2807 if (file_handle > FS_NO_ERROR ) {
2808
2809 NVRAM_FS_START(FS_OP_GETFILESIZE);
2810 result = FS_GetFileSize(file_handle, &len);
2811 NVRAM_FS_END(FS_OP_GETFILESIZE,file_handle);
2812
2813 kal_mem_set(trace_config,0,4);
2814
2815 NVRAM_FS_START(FS_OP_READ);
2816 result = FS_Read(file_handle, trace_config, len, &len);
2817 NVRAM_FS_END(FS_OP_READ,result);
2818 if(result < FS_NO_ERROR )
2819 {
2820 if(nvram_ee_info != NULL){
2821 nvram_ee_info->nvram_access_trace_setting_file_val = NVRAM_READ_TRACE_SETTING_FILE_FAIL;
2822 g_access_trace_setting_file_result = NVRAM_READ_TRACE_SETTING_FILE_FAIL;
2823 g_nvram_trace_setting = 0;
2824 }
2825 }
2826 else
2827 {
2828 if(nvram_ee_info != NULL){
2829 nvram_ee_info->nvram_trace_setting = atoi(trace_config);
2830 nvram_ee_info->nvram_access_trace_setting_file_val = NVRAM_READ_TRACE_SETTING_FILE_SUCCESS;
2831 g_access_trace_setting_file_result = NVRAM_READ_TRACE_SETTING_FILE_SUCCESS;
2832 g_nvram_trace_setting = nvram_ee_info->nvram_trace_setting;
2833 }
2834 }
2835 }
2836 if(file_handle > FS_NO_ERROR) {
2837 NVRAM_FS_START(FS_OP_CLOSE);
2838 result = FS_Close(file_handle);
2839 NVRAM_FS_END(FS_OP_CLOSE,result);
2840 }
2841 if(nvram_ee_info != NULL){
2842 nvram_ee_info->nvram_init_step = NVRAM_PRE_INIT_GET_TRACE_SETTING_DONE;
2843 }
2844 return;
2845}
2846
2847/*****************************************************************************
2848 * FUNCTION
2849 * nvram_get_lid_chksum_algo_info
2850 * DESCRIPTION
2851 * get cache LID from cache table
2852 * PARAMETERS
2853 * ldi [IN]
2854 * RETURNS
2855 * success or fail
2856 *****************************************************************************/
2857nvram_errno_enum nvram_get_lid_chksum_algo_info(nvram_ltable_entry_struct *ldi, nvram_lid_chksum_info * lid_chksum_info, kal_bool chksum_only, kal_bool read_2B)
2858{
2859 /*----------------------------------------------------------------*/
2860 /* Local Variables */
2861 /*----------------------------------------------------------------*/
2862
2863 /*----------------------------------------------------------------*/
2864 /* Code Body */
2865 /*----------------------------------------------------------------*/
2866 if((NULL == ldi) || (NULL == lid_chksum_info))
2867 {
2868 kal_prompt_trace(MOD_NVRAM,"%s->(%d)NVRAM_LOC_BUFFER_PTR_IS_NULL_42 NULL \r\n",__FUNCTION__, __LINE__);
2869 NVRAM_DEBUG_DUMP(NVRAM_CRITICAL_DUMP,"%s()[Error]:NVRAM_LOC_BUFFER_PTR_IS_NULL_42 NULL %d.\r\n", __FUNCTION__,__LINE__);
2870 NVRAM_EXT_ASSERT(KAL_FALSE,0 ,NVRAM_LOC_BUFFER_PTR_IS_NULL_42, 0, __LINE__);
2871 return NVRAM_ERRNO_FAIL;
2872 }
2873
2874 #ifdef __NV_CHKSUM_ENHANCE__
2875 if(NVRAM_IS_ATTR_CHKSUM_ENHANC_ALGRTHM(ldi->attr))
2876 {
2877 lid_chksum_info->algo_info.chksum_algo_type = chksum_algo_ptr->cur_algo.en_algo_type;
2878 if(NVRAM_MD5 == lid_chksum_info->algo_info.chksum_algo_type)
2879 {
2880 lid_chksum_info->algo_info.chksum_algo_length = read_2B? MD5_CHKSUM_LENGTH_2: MD5_CHKSUM_LENGTH_8;
2881 }else
2882 {
2883 lid_chksum_info->algo_info.chksum_algo_length = chksum_algo_ptr->cur_algo.en_algo_length;
2884 }
2885 }else
2886 {
2887 lid_chksum_info->algo_info.chksum_algo_type = chksum_algo_ptr->cur_algo.def_algo_type;
2888 if(NVRAM_MD5 == lid_chksum_info->algo_info.chksum_algo_type)
2889 {
2890 lid_chksum_info->algo_info.chksum_algo_length = read_2B? MD5_CHKSUM_LENGTH_2: MD5_CHKSUM_LENGTH_8;
2891 }else
2892 {
2893 lid_chksum_info->algo_info.chksum_algo_length = chksum_algo_ptr->cur_algo.def_algo_length;
2894 }
2895 }
2896 #else
2897 lid_chksum_info->algo_info.chksum_algo_type = NVRAM_MD5;
2898 lid_chksum_info->algo_info.chksum_algo_length = read_2B? MD5_CHKSUM_LENGTH_2: MD5_CHKSUM_LENGTH_8;
2899 #endif
2900
2901 if((lid_chksum_info->algo_info.chksum_algo_type == NVRAM_MD5) && (read_2B == KAL_TRUE))
2902 {
2903 lid_chksum_info->read_chksum_type = (chksum_only? NVRAM_DRV_READ_TYPE_CHKSUM_ONLY_2B: NVRAM_DRV_READ_TYPE_CHKSUM_2B);
2904 }
2905 else
2906 {
2907 lid_chksum_info->read_chksum_type = chksum_only? NVRAM_DRV_READ_TYPE_CHKSUM_ONLY: NVRAM_DRV_READ_TYPE_CHKSUM;
2908 }
2909
2910 return NVRAM_IO_ERRNO_OK;
2911}
2912