blob: c1d81fbd4bb5b5951d9746c988c594df96a00e51 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/**
2 *
3 * @file amtnv.c
4 * @brief
5 * This file is part of FTM.
6 * AMTÄ£¿éNV²Ù×÷½Ó¿Ú
7 *
8 * @details
9 * @author Tools Team.
10 * @email
11 * @copyright Copyright (C) 2013 Sanechips Technology Co., Ltd.
12 * @warning
13 * @date 2019/02/02
14 * @version 1.1
15 * @pre
16 * @post
17 *
18 * @par
19 * Change History :
20 * ---------------------------------------------------------------------------
21 * date version author description
22 * ---------------------------------------------------------------------------
23 * 2018/04/28 1.0 liu.xin Create file
24 * 2019/02/02 1.1 jiang.fenglin ÐÞ¸Ä×¢ÊÍ·½Ê½Îªdoxygen
25 * ---------------------------------------------------------------------------
26 *
27 *
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <assert.h>
33#include <errno.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <string.h>
37#include <unistd.h>
38#include "amtnv.h"
39#include "libcpnv.h"
40#include "cfg_api.h"
41#include "NvParam_drv.h"
42
43/**
44 * È«¾Ö±äÁ¿¶¨Òå
45 */
46
47typedef struct
48{
49 UINT32 dwMsgID;
50 UINT32 dwAddress;
51 UINT32 dwSize;
52} T_zAMT_CommNv_Info;
53
54const T_zAMT_CommNv_Info g_amtCommNvCfg[] =
55{
56 { ABIMEI_NVPARAM, OS_FLASH_AMT_COMM_RO_IMEI_ADDRESS, OS_FLASH_AMT_COMM_RO_IMEI_SIZE },
57 { ABIMEISV_NVPARAM, OS_FLASH_AMT_COMM_RO_IMEISV_ADDRESS, OS_FLASH_AMT_COMM_RO_IMEISV_SIZE },
58 { ABBORDNUm_NVPARAM, OS_FLASH_AMT_COMM_RO_BOARDNNUM_ADDRESS, OS_FLASH_AMT_COMM_RO_BOARDNNUM_SIZE },
59 { ABMSERIALNUM_NVPARAM, OS_FLASH_AMT_COMM_RO_MSerialNum_ADDRESS, OS_FLASH_AMT_COMM_RO_MSerialNum_SIZE },
60 { ABVALIDFLAG_NVPARAM, OS_FLASH_AMT_COMM_RO_ValidFlag_ADDRESS, OS_FLASH_AMT_COMM_RO_ValidFlag_SIZE },
61 { ABMAC_INT_NVPARAM, OS_FLASH_AMT_COMM_RO_InternalMAC_ADDRESS, OS_FLASH_AMT_COMM_RO_InternalMAC_SIZE },
62 { ABMAC_EXT_NVPARAM, OS_FLASH_AMT_COMM_RO_ExternalMAC_ADDRESS, OS_FLASH_AMT_COMM_RO_ExternalMAC_SIZE },
63 { ABMAC_WIFI_NVPARAM, OS_FLASH_AMT_COMM_RO_WIFIMAC_ADDRESS, OS_FLASH_AMT_COMM_RO_WIFIMAC_SIZE },
64 { ABNV_VERSION_NVPARAM, OS_FLASH_AMT_COMM_RO_NvVersion_ADDRESS, OS_FLASH_AMT_COMM_RO_NvVersion_SIZE },
65 { ABTESTINFO_NVPARAM, OS_FLASH_AMT_COMM_RO_TestInfo_ADDRESS, OS_FLASH_AMT_COMM_RO_TestInfo_SIZE },
66 { ABSOFTVERSION_NVPARAM, OS_FLASH_AMT_COMM_RO_SOFTVERSION_ADDRESS, OS_FLASH_AMT_COMM_RO_SOFTVERSION_SIZE },
67 { ABNET_V4KEY_NVPARAM, OS_FLASH_AMT_COMM_RO_V4Key_ADDRESS, OS_FLASH_AMT_COMM_RO_V4Key_SIZE },
68 { ABSALE_STATE_NVPARAM, OS_FLASH_AMT_COMM_RO_SaleState_ADDRESS, OS_FLASH_AMT_COMM_RO_SaleState_SIZE },
69 { ABBANDBITMAP_NVPARAM, OS_FLASH_AMT_COMM_RO_BANDBITMAP_ADDRESS, OS_FLASH_AMT_COMM_RO_BANDBITMAP_SIZE },
70 { ABMAC_ETH_NVPARAM, OS_FLASH_AMT_COMM_RO_ETHMAC_ADDRESS, OS_FLASH_AMT_COMM_RO_ETHMAC_SIZE },
71 { ABNET_LCOKKEY_NVPARAM, OS_FLASH_AMT_COMM_RO_LockKey_ADDRESS, OS_FLASH_AMT_COMM_RO_LockKey_SIZE },
72 { ABMAC_WIFI2_NVPARAM, OS_FLASH_AMT_COMM_RO_WIFIMAC2_ADDRESS, OS_FLASH_AMT_COMM_RO_WIFIMAC2_SIZE },
73 { ABMAC_USBMAC_NVPARAM, OS_FLASH_AMT_COMM_RO_USBMAC_ADDRESS, OS_FLASH_AMT_COMM_RO_USBMAC_SIZE },
74 { ABMAC_GMAC_NVPARAM, OS_FLASH_AMT_COMM_RO_GMAC_ADDRESS, OS_FLASH_AMT_COMM_RO_GMAC_SIZE },
75 { ABFEATURE_KEY_MD5_NVPARAM, OS_FLASH_AMT_COMM_RO_FEATURE_KEY_MD5_ADDRESS, OS_FLASH_AMT_COMM_RO_FEATURE_KEY_MD5_SIZE},
76 { ABAUTH_KEY_NVPARAM, OS_FLASH_AMT_COMM_RO_AUTH_KEY_ADDRESS, OS_FLASH_AMT_COMM_RO_AUTH_KEY_SIZE },
77 { ABSOFTDOG_CIPHER_TEXT_NVPARAM, OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_ADDRESS, OS_FLASH_AMT_COMM_RO_SOFTDOG_CIPHER_TEXT_SIZE },
78 { ABGBDATA_NVPARAM, OS_FLASH_AMT_COMM_RO_GBDATA_ADDRESS, OS_FLASH_AMT_COMM_RO_GBDATA_SIZE },
79};
80
81SINT32 GetBitOffset(SINT32 testItem)
82{
83 return 2 * (3 - (testItem % 4));
84}
85
86/**
87 * @brief дnvro
88 * @param dwStart nvÆðʼµØÖ·Æ«ÒÆ,0 ~ 2M-1 bytes
89 * @param dwLen nv³¤¶È,1 ~ 2M bytes
90 * @param from ´ýдÈëµÄÊý¾Ý
91 * @return 0 ³É¹¦
92 * @return -1 ʧ°Ü
93 * @note
94 * @warning
95 */
96unsigned int amt_nvro_write(unsigned int dwStart, unsigned int dwLen, unsigned char *from)
97{
98 UINT32 retCode = CPNV_ERROR;
99
100 /*ÕâÀïÊ×Ïȵ÷ÓÃÇý¶¯½Ó¿Ú·Å¿ª¶ÔÖ»¶ÁÇøÓòд²Ù×÷µÄÏÞÖÆ*/
101 /*amtģʽÏ£¬zte_amtÓ¦ÓÃÆðÀ´Ö®ºóÒѾ­¹Ò³É¿ÉдµÄÁË*/
102 if(!is_amt_mode())
103 {
104 retCode = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
105 if(CPNV_OK != retCode)
106 {
107 return retCode;
108 }
109 }
110
111 retCode = cpnv_NvItemWriteNvro(dwStart, from, dwLen);
112 if(CPNV_OK != retCode)
113 {
114 return retCode;
115 }
116
117 cpnv_FsGcWait(FS_NVROFS);
118 if(!is_amt_mode())
119 {
120 retCode = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 0);
121 if(CPNV_OK != retCode)
122 {
123 return retCode;
124 }
125 }
126 return retCode;
127}
128
129/**
130 * @brief ¶Ánvro
131 * @param dwStart nvÆðʼµØÖ·Æ«ÒÆ,0 ~ 2M-1 bytes
132 * @param dwLen nv³¤¶È,1 ~ 2M bytes
133 * @param to ¶Á³öµÄÊý¾Ý
134 * @return 0 ³É¹¦
135 * @return -1 ʧ°Ü
136 * @note
137 * @warning
138 */
139unsigned int amt_nvro_read(unsigned int dwStart, unsigned int dwLen, unsigned char *to)
140{
141 unsigned int ret = CPNV_ERROR;
142 unsigned int max_length = OS_FLASH_RO_NVRAM_SIZE + OS_FLASH_ROW_NVRAM_SIZE;
143
144 if (dwStart >= max_length || dwLen > max_length || (dwStart + dwLen) > max_length)
145 {
146 return -1;
147 }
148
149 ret = cpnv_NvItemRead(dwStart, to, dwLen);
150
151 if(ret == CPNV_ERROR)
152 {
153 return -1;
154 }
155 else
156 {
157 return 0;
158 }
159
160}
161
162/**
163 * @brief дһÏîNVROÊý¾Ý
164 * @param nvParam ¸ÃNVÏîÀàÐÍ
165 * @param NvItemData ´ýдÈëÊý¾ÝµÄµØÖ·
166 * @param NvItemLen Êý¾Ý³¤¶È
167 * @return 0 ³É¹¦
168 * @return -1 ʧ°Ü
169 * @note
170 * @warning
171 */
172int amt_write_nv_item(NvParam_AMT nvParam, UINT8 *NvItemData, UINT32 NvItemLen)
173{
174 UINT32 retCode = CPNV_ERROR;
175 UINT32 i = 0;
176
177 if (nvParam >= MAXABNVPARAM_NVPARAM || NULL == NvItemData || NvItemLen == 0)
178 {
179 return -1;
180 }
181
182 for (i = 0; i < (sizeof(g_amtCommNvCfg) / sizeof (g_amtCommNvCfg[0])); i++)
183 {
184 if ( nvParam == g_amtCommNvCfg[i].dwMsgID )
185 {
186 if (NvItemLen > g_amtCommNvCfg[i].dwSize)
187 {
188 NvItemLen = g_amtCommNvCfg[i].dwSize;
189 }
190
191 retCode = amt_nvro_write(g_amtCommNvCfg[i].dwAddress, NvItemLen, NvItemData);
192
193 break;
194 }
195 }
196
197 if (retCode != CPNV_OK)
198 {
199 assert(0);
200 }
201 return 0;
202}
203
204/**
205 * @brief ¶Á³öÒ»ÏîNVROÊý¾Ý
206 * @param nvParam ¸ÃNVÏîÀàÐÍ
207 * @param NvItemData ¶Á³öµÄÊý¾Ý
208 * @param NvItemLen Êý¾Ý³¤¶È
209 * @return 0 ³É¹¦
210 * @return -1 ʧ°Ü
211 * @note
212 * @warning
213 */
214int amt_read_nv_item(NvParam_AMT nvParam,UINT8 *NvItemData, UINT32 NvItemLen)
215{
216 UINT32 retCode = CPNV_ERROR;
217 UINT32 i = 0;
218
219 // zOss_Printf(0x7f, 0x2,"[zPS_NvAMTItemRead]nvParam is %d and NvItemLen is %d\n",nvParam,NvItemLen);
220 if (nvParam >= MAXABNVPARAM_NVPARAM || NULL == NvItemData || NvItemLen == 0)
221 {
222 return -1;
223 }
224
225 /* if (ABBANDBITMAP_NVPARAM == nvParam)
226 {
227 BYTE band_bit_map_flag = 0;
228 zPS_NvAMFlagsTItemRead(BAND_BIT_MAP_FLAG, &band_bit_map_flag);
229
230 if (band_bit_map_flag != '1')
231 {
232 retCode = zPs_NvCheckBandBitmap(NvItemData, NvItemLen);
233
234 return retCode;
235 }
236 }*/
237 for (i = 0; i < (sizeof(g_amtCommNvCfg) / sizeof (g_amtCommNvCfg[0])); i++)
238 {
239 if ( nvParam == g_amtCommNvCfg[i].dwMsgID )
240 {
241 if (NvItemLen > g_amtCommNvCfg[i].dwSize)
242 {
243 NvItemLen = g_amtCommNvCfg[i].dwSize;
244 }
245 retCode = cpnv_NvItemRead(g_amtCommNvCfg[i].dwAddress, NvItemData, NvItemLen);
246 break;
247 }
248 }
249 if(retCode == CPNV_ERROR)
250 {
251 return -1;
252 }
253 else
254 {
255 return 0;
256 }
257}
258
259/**
260 * @brief дÉú²ú²âÊÔ±ê־λ
261 * @param nvParam Éú²ú²âÊÔ±ê־λÏîö¾ÙÖµ
262 * @param NvItemData ´ýдÈëÊý¾Ý
263 * @return 0 ³É¹¦
264 * @return -1 ʧ°Ü
265 * @note
266 * @warning
267 */
268int amt_write_test_flag_item(NvParam_AMTFlags nvParam,UINT8 *NvItemData)
269{
270 UINT32 retCode = CPNV_ERROR;
271 UINT32 AMTFlagAddress = 0;
272 BYTE pTestInfoBuf[1];
273 BYTE *pByte;
274 int byteIndex = 0;
275
276 if (nvParam >= MAXAMTFALGS_NVPARAM)
277 {
278 return -1;
279 }
280 memset(pTestInfoBuf, 0, sizeof(pTestInfoBuf));
281 byteIndex = nvParam/4; //ҪдµÄ±ê־λÏà¶ÔÓÚ±ê־λÊ×µØÖ·µÄÆ«ÒÆ
282 AMTFlagAddress = OS_FLASH_AMT_COMM_RO_TestInfo_ADDRESS + byteIndex;
283
284 /*Ê×ÏȽ«NVÖеÄÄÚÈݶÁµ½pTestInfoBufÖÐ*/
285 retCode = cpnv_NvItemRead(AMTFlagAddress , pTestInfoBuf,1);
286 if (CPNV_ERROR ==retCode )
287 {
288 return -1;
289 }
290 /*ÐÞ¸ÄÏàÓ¦µÄ±ê־λ*/
291 pByte = pTestInfoBuf ;
292 *pByte &= ~(0x03 << GetBitOffset(nvParam));
293 *pByte |= (*NvItemData-0x30) << GetBitOffset(nvParam);
294
295
296 /*ÐÞ¸ÄÖ®ºóдÈë*/
297 retCode = amt_nvro_write(AMTFlagAddress, 1, pTestInfoBuf);
298
299 if (CPNV_ERROR ==retCode )
300 {
301 assert(0);
302 }
303 return 0;
304}
305
306/**
307 * @brief ¶ÁÉú²ú²âÊÔ±ê־λ
308 * @param nvParam Éú²ú²âÊÔ±ê־λÏîö¾ÙÖµ
309 * @param NvItemData ¶Á³öµÄÊý¾Ý
310 * @return 0 ³É¹¦
311 * @return -1 ʧ°Ü
312 * @note
313 * @warning
314 */
315int amt_read_test_flag_item(NvParam_AMTFlags nvParam,UINT8 *NvItemData)
316{
317 UINT32 retCode = CPNV_ERROR;
318 UINT32 AMTFlagAddress = 0;
319 BYTE pTestInfoBuf[1];
320 BYTE byteVal = 0;
321 BYTE byteRet = 0;
322 int byteIndex = 0;
323
324 if (nvParam >= MAXAMTFALGS_NVPARAM)
325 {
326 return -1;
327 }
328
329 byteIndex = nvParam/4; //Òª¶ÁµÄ±ê־λÏà¶ÔÓÚ±ê־λÊ×µØÖ·µÄÆ«ÒÆ
330 AMTFlagAddress = OS_FLASH_AMT_COMM_RO_TestInfo_ADDRESS + byteIndex;
331 /*Ê×ÏȽ«NVÖеÄÄÚÈݶÁµ½pTestInfoBufÖÐ*/
332 retCode = cpnv_NvItemRead(AMTFlagAddress,pTestInfoBuf, 1);
333 if (CPNV_ERROR ==retCode )
334 {
335 return -1;
336 }
337 /*¶ÁÈ¡¶ÔÓ¦µÄbitλ*/
338
339 byteVal =( (*pTestInfoBuf ) >> GetBitOffset(nvParam)) & 0x03;
340
341 switch (byteVal)
342 {
343 case 0:
344 byteRet = 0x30;
345 break;
346 case 1:
347 byteRet = 0x31;
348 break;
349 case 2:
350 byteRet = 0x32;
351 break;
352 case 3:
353 byteRet = 0x33;
354 break;
355 default:
356 break;
357 }
358
359 memcpy(NvItemData, &byteRet, sizeof(byteRet));
360 #if 0
361 if (CPNV_ERROR ==retCode )
362 {
363 assert(0);
364 }
365 #endif
366 return 0;
367}
368
369/**
370 * @brief NVROÓû§×Ô¶¨ÒåÇøÓò¶Áº¯Êý
371 * @param dwStart nvÆðʼµØÖ·Æ«ÒÆ,0 ~ 1023
372 * @param dwLen nv³¤¶È,1 ~ 1024 bytes
373 * @param from ´ýдÈëµÄÊý¾Ý
374 * @return 0 ³É¹¦
375 * @return -1 ʧ°Ü
376 * @note
377 * @warning
378 */
379 int amt_nvro_user_write(UINT32 dwStart, UINT32 dwLen, UINT8* from)
380 {
381 unsigned int ret = CPNV_ERROR;
382
383 if (dwStart >= OS_FLASH_AMT_COMM_RO_USER_DEFINE_SIZE || dwLen > OS_FLASH_AMT_COMM_RO_USER_DEFINE_SIZE || (dwStart + dwLen) > OS_FLASH_AMT_COMM_RO_USER_DEFINE_SIZE)
384 {
385 return -1;
386 }
387
388 ret = amt_nvro_write(dwStart + OS_FLASH_AMT_COMM_RO_USER_DEFINE_ADDRESS, dwLen, from);
389
390 cpnv_FsGcWait(FS_NVROFS);
391
392 if(ret == CPNV_ERROR)
393 {
394 return -1;
395 }
396 else
397 {
398 return 0;
399 }
400
401 }
402
403/**
404 * @brief NVROÓû§×Ô¶¨ÒåÇøÓòдº¯Êý
405 * @param dwStart nvÆðʼµØÖ·Æ«ÒÆ,0 ~ 1023
406 * @param dwLen nv³¤¶È,1 ~ 1024 bytes
407 * @param from ¶Á³öµÄÊý¾Ý
408 * @return 0 ³É¹¦
409 * @return -1 ʧ°Ü
410 * @note
411 * @warning
412 */
413int amt_nvro_user_read(UINT32 dwStart, UINT32 dwLen, UINT8* to)
414{
415 unsigned int ret = CPNV_ERROR;
416
417 if (dwStart >= OS_FLASH_AMT_COMM_RO_USER_DEFINE_SIZE || dwLen > OS_FLASH_AMT_COMM_RO_USER_DEFINE_SIZE || (dwStart + dwLen) > OS_FLASH_AMT_COMM_RO_USER_DEFINE_SIZE)
418 {
419 return -1;
420 }
421
422 ret = cpnv_NvItemRead(dwStart + OS_FLASH_AMT_COMM_RO_USER_DEFINE_ADDRESS, to,dwLen);
423
424 if(ret == CPNV_ERROR)
425 {
426 return -1;
427 }
428 else
429 {
430 return 0;
431 }
432}
433
434/**
435 * @brief NVRO²Î¿¼Éè¼Æ×Ô¶¨ÒåÇøÓòдº¯Êý
436 * @param dwStart nvÆðʼµØÖ·Æ«ÒÆ,0 ~ 1023
437 * @param dwLen nv³¤¶È,1 ~ 1024 bytes
438 * @param from ´ýдÈëµÄÊý¾Ý
439 * @return 0 ³É¹¦
440 * @return -1 ʧ°Ü
441 * @note
442 * @warning
443 */
444int amt_nvro_ref_write(UINT32 dwStart, UINT32 dwLen, UINT8* from)
445{
446 unsigned int ret = CPNV_ERROR;
447
448 if (dwStart >= OS_FLASH_AMT_COMM_RO_REF_DEFINE_SIZE || dwLen > OS_FLASH_AMT_COMM_RO_REF_DEFINE_SIZE || (dwStart + dwLen) > OS_FLASH_AMT_COMM_RO_REF_DEFINE_SIZE)
449 {
450 return -1;
451 }
452
453 ret = amt_nvro_write(dwStart + OS_FLASH_AMT_COMM_RO_REF_DEFINE_ADDRESS, dwLen, from);
454
455 if(ret == CPNV_ERROR)
456 {
457 return -1;
458 }
459 else
460 {
461 return 0;
462 }
463}
464
465/**
466 * @brief NVRO²Î¿¼Éè¼Æ×Ô¶¨ÒåÇøÓò¶Áº¯Êý
467 * @param dwStart nvÆðʼµØÖ·Æ«ÒÆ,0 ~ 1023
468 * @param dwLen nv³¤¶È,1 ~ 1024 bytes
469 * @param to ¶Á³öµÄÊý¾Ý
470 * @return 0 ³É¹¦
471 * @return -1 ʧ°Ü
472 * @note
473 * @warning
474 */
475int amt_nvro_ref_read(UINT32 dwStart, UINT32 dwLen, UINT8* to)
476{
477 unsigned int ret = CPNV_ERROR;
478
479 if (dwStart >= OS_FLASH_AMT_COMM_RO_REF_DEFINE_SIZE || dwLen > OS_FLASH_AMT_COMM_RO_REF_DEFINE_SIZE || (dwStart + dwLen) > OS_FLASH_AMT_COMM_RO_REF_DEFINE_SIZE)
480 {
481 return -1;
482 }
483
484 ret = cpnv_NvItemRead(dwStart + OS_FLASH_AMT_COMM_RO_REF_DEFINE_ADDRESS, to,dwLen);
485
486 if(ret == CPNV_ERROR)
487 {
488 return -1;
489 }
490 else
491 {
492 return 0;
493 }
494}
495
496
497 /**
498 * @brief Ó¦ÓòãÅжϵ±Ç°ÊÇ·ñÊÇamtģʽ
499 * @param
500 * @return 1 amtģʽ
501 * @return 0 normalģʽ
502 * @note
503 * @warning
504 */
505int is_amt_mode(void)
506{
507 char verMode[4] = {0};
508 int ret = 0;
509
510 ret = sc_cfg_get("ver_mode", verMode, sizeof(verMode));
511 if(!strncmp(verMode, "0", strlen("0")))
512 {
513 ret = 1;
514 }
515 else
516 {
517 ret = 0;
518 }
519
520 return ret;
521}
522
523/**
524 * @brief ÉèÖÃbootÆô¶¯Ä£Ê½
525 * @param bootmode 2¸ö×ֽڵıêʶ:
526 * {0x54,0x00} : user,
527 * {0x54,0x01} : debug,
528 * {0x54,0x02} : factory,
529 * {0x54,0x4D} : amt,
530 * @return 0 ³É¹¦
531 * @return -1 ʧ°Ü
532 * @note
533 * @warning
534 */
535unsigned int amt_set_bootmode(unsigned char bootmode[])
536{
537 unsigned int ret = CPNV_OK;
538
539 if(CPNV_ERROR == cpnv_ChangeFsPartitionAttr(FS_IMAGEFS, 1))
540 {
541 printf("cpnv_ChangeFsPartitionAttr failed\r\n");
542 return -1;
543 }
544 ret = cpnv_NvItemWrite(OS_FLASH_TSP_RW_NONFAC_OFFSET_FROM_NV, bootmode, 2);
545 if(CPNV_ERROR == ret)
546 {
547 printf("cpnv_NvItemWrite bootmode fail !\n");
548 return ret;
549 }
550
551 cpnv_NvItemWriteFactory(OS_FLASH_TSP_RW_NONFAC_OFFSET_FROM_NV, bootmode, 2);
552 ret = cpnv_NvramFlush();
553 if(CPNV_ERROR == ret)
554 {
555 printf("cpnv_NvramFlush bootmode fail !\n");
556 }
557
558 cpnv_FsGcWait(FS_IMAGEFS);
559 if(CPNV_ERROR == cpnv_ChangeFsPartitionAttr(FS_IMAGEFS, 0))
560 {
561 printf("cpnv_ChangeFsPartitionAttr failed\r\n");
562 return -1;
563 }
564
565 return ret;
566}
567
568unsigned int amt_set_batdet_flag(int batdet_value)
569{
570 unsigned int ret = CPNV_OK;
571
572 if(CPNV_ERROR == cpnv_ChangeFsPartitionAttr(FS_IMAGEFS, 1))
573 {
574 printf("cpnv_ChangeFsPartitionAttr failed\r\n");
575 return -1;
576 }
577 ret = cpnv_NvItemWrite(DRV_PERI_NV_ADDR,(unsigned char*)&batdet_value,1);
578 if(CPNV_ERROR == ret)
579 {
580 printf("cpnv_NvItemWrite batdet_value fail !\n");
581 return ret;
582 }
583
584 cpnv_NvItemWriteFactory(DRV_PERI_NV_ADDR, (unsigned char*)&batdet_value,1);
585 ret = cpnv_NvramFlush();
586 if(CPNV_ERROR == ret)
587 {
588 printf("cpnv_NvramFlush batdet_value fail !\n");
589 }
590
591 cpnv_FsGcWait(FS_IMAGEFS);
592 if(CPNV_ERROR == cpnv_ChangeFsPartitionAttr(FS_IMAGEFS, 0))
593 {
594 printf("cpnv_ChangeFsPartitionAttr failed\r\n");
595 return -1;
596 }
597
598 return ret;
599}
600
601unsigned int amt_get_batdet_flag(int *pbatdet_value)
602{
603 unsigned int retCode= CPNV_ERROR;
604
605 retCode =cpnv_NvItemRead(DRV_PERI_NV_ADDR, (unsigned char *)pbatdet_value, 1);
606
607 return retCode;
608
609}
610
611unsigned int amt_set_amt_atmode(unsigned char bootmode[],unsigned char atmode[])
612{
613 unsigned int ret = CPNV_OK;
614
615 if(CPNV_ERROR == cpnv_ChangeFsPartitionAttr(FS_IMAGEFS, 1))
616 {
617 printf("cpnv_ChangeFsPartitionAttr failed\r\n");
618 return -1;
619 }
620 ret = cpnv_NvItemWrite(OS_FLASH_TSP_RW_NONFAC_OFFSET_FROM_NV, bootmode, 2);
621 if(CPNV_ERROR == ret)
622 {
623 printf("cpnv_NvItemWrite bootmode fail !\n");
624 return ret;
625 }
626 cpnv_NvItemWriteFactory(OS_FLASH_TSP_RW_NONFAC_OFFSET_FROM_NV, bootmode, 2);
627
628 ret = cpnv_NvItemWrite(OS_FLASH_TSP_RW_NONFAC_AT_MODE_ADDR, atmode, 2);
629 if(CPNV_ERROR == ret)
630 {
631 printf("cpnv_NvItemWrite at mode fail !\n");
632 return ret;
633 }
634 cpnv_NvItemWriteFactory(OS_FLASH_TSP_RW_NONFAC_AT_MODE_ADDR, atmode, 2);
635 ret = cpnv_NvramFlush();
636 if(CPNV_ERROR == ret)
637 {
638 printf("cpnv_NvramFlush fail !\n");
639 }
640
641 cpnv_FsGcWait(FS_IMAGEFS);
642 if(CPNV_ERROR == cpnv_ChangeFsPartitionAttr(FS_IMAGEFS, 0))
643 {
644 printf("cpnv_ChangeFsPartitionAttr failed\r\n");
645 return -1;
646 }
647
648 return ret;
649
650}
651
652unsigned int is_amt_atmode()
653{
654 unsigned int ret = CPNV_OK;
655 unsigned char mode[4] = {0};
656
657 ret = cpnv_NvItemRead(OS_FLASH_TSP_RW_NONFAC_BASE_ADDR, mode, 2);
658 if(CPNV_ERROR == ret)
659 {
660 printf("cpnv_NvItemRead at mode fail !\n");
661 return ret;
662 }
663
664 ret = cpnv_NvItemRead(OS_FLASH_TSP_RW_NONFAC_AT_MODE_ADDR, mode+2, 2);
665 if(CPNV_ERROR == ret)
666 {
667 printf("cpnv_NvItemRead at mode fail !\n");
668 return ret;
669 }
670
671 printf("mode[0] = %x,mode[1] = %x,mode[2] = %x,mode[3] = %x!\n",mode[0],mode[1],mode[2],mode[3]);
672
673 if((mode[0] == 0x54)&&(mode[1] == 0x4D)&&(mode[2] == 0x41)&&(mode[3] == 0x54))
674 {
675 printf("mode = 1\n");
676 return 1;
677 }
678 else
679 {
680 printf("mode = 0\n");
681 return 0;
682 }
683
684}
685
686
687
688
689