blob: 7d0482799ee427240a5c51334cf30bed4b5b8512 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * (C) Copyright 2016, ZIXC Corporation.
3 *
4 */
5
6#include <common.h>
7#include <errno.h>
8#include <command.h>
9#include <malloc.h>
10#include <jffs2/load_kernel.h>
11#include <linux/list.h>
12#include <linux/ctype.h>
13#include <linux/err.h>
14#include <linux/mtd/mtd.h>
15#include <linux/mtd/partitions.h>
16#include <asm/io.h>
17#include <image.h>
18#include <partition_table.h>
19#include <board.h>
20#include <mmc.h>
21#include <boot_mode.h>
22#include <led.h>
23
24#if defined(CONFIG_CMD_NAND)
25#include <linux/mtd/nand.h>
26#include <nand.h>
27#endif
28
29#include <config.h>
30#include <load_image.h>
31#include <asm/arch/cpu.h>
32#include <secure_verify.h>
33#include <linux/mtd/nor_spifc.h>
34
35#if LOAD_IMAGE_DEBUG
36#define load_debug_printf(fmt,args...) printf (fmt ,##args)
37#else
38#define load_debug_printf(fmt,args...)
39#endif /* LOAD_IMAGE_DEBUG */
40
41#if TIME_DEBUG
42#define time_debug_reset(fmt) fmt = get_timer(0)
43#define time_debug_printf(fmt, val) printf(fmt ,get_timer(val))
44#else
45#define time_debug_reset(fmt)
46#define time_debug_printf(fmt, val)
47#endif /* TIME_DEBUG */
48#define reg16(addr) (*(volatile unsigned short*)(addr))
49
50DECLARE_GLOBAL_DATA_PTR;
51
52#define ZSP_IMAGE_PATH "/evb_cpuphy.bin"
53#define M0_IMAGE_PATH "/evb_cpurpm.img"
54#define FOTAFLAG_PATH "/fotaflag"
55
56#if defined(CONFIG_ZX297520V3E_JFFS2_COMPRESS)
57typedef struct lzmaheader_p{
58 uint8_t p_properties;
59 uint8_t p_dict[4];
60 uint8_t p_uncompress_size[8];
61}lzma_header_t;
62
63extern uint32_t ztelzma_compresssize;
64extern int lzmanodeflag;
65#endif
66
67#ifdef CONFIG_ZX297520V3E_MDL_AB
68extern int imagefs_flag;
69static uint32_t flags;
70#endif
71extern int nand_curr_device;
72extern nand_info_t nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
73extern partition_table_t * g_partition_table;
74extern int flash_dmabuf_disable_flag;
75extern struct fsl_qspi spi_nor_flash;
76
77/* Secure Verify Flag. 1->Disable, 0->Enable */
78extern unsigned int guiEfuseStatus;
79
80uint32_t arm_ps_ep = 0; /* Entry Point Address */
81uint32_t arm_cpucap_ep = 0;
82static uint32_t arm_phy_ep = 0; /* Entry Point Address */
83
84static uint32_t fota_upflag = FOTA_NORMAL;
85static uint32_t fota_psup_flag = FOTA_PS_NORMAL;
86
87uint32_t g_gmac_init_flag = 0;
88uint32_t g_gmac_init_overtime = 0;
89
90static uint32_t sys_ddr_kernel_start = 0; /* kernel ÔËÐпռä Ê×µØÖ· */
91
92master_header_t *master_head = NULL;
93int update_start_time = 0;
94int update_recent_time = 0;
95int led_state = 0;
96
97int rd_offset = 0;
98int rd_size = 0;
99
100
101/* ================================================================================
102 * page_align : Ò³¶ÔÆë
103 */
104static uint32_t page_align(uint32_t offset)
105{
106 struct flash_ops *flash = NULL;
107 uint32_t page_size = 0;
108
109 flash = get_flash_ops();
110 page_size = flash->page_size;
111 if( offset & (page_size - 1) )
112 {
113 offset &= (~(page_size - 1));
114 offset += page_size;
115 }
116 return offset;
117}
118
119/* ================================================================================
120 * page_align : Ò³¶ÔÆë
121 */
122int set_entry_point(uchar * part_name, uint32_t entry_point)
123{
124 if ( strcmp( (char *)ARM_PS_IMAGE, (char *)part_name ) == 0 )
125 {
126 arm_ps_ep = entry_point;
127 return 0;
128 }
129 else if ( strcmp( (char *)ARM_PHY_IMAGE, (char *)part_name ) == 0 )
130 {
131 arm_phy_ep = entry_point;
132 return 0;
133 }
134 else if ( strcmp( (char *)ARM_RAMDISK_IMAGE, (char *)part_name ) == 0 )
135 {
136 return 0;
137 }
138 else
139 {
140 return 1;
141 }
142}
143
144static int arm_image_crc_calc(char* pcPartName,
145 uint32_t uiCRCChkSum,
146 uint32_t uiEntryPoint,
147 uint32_t uiImgSize)
148{
149 uint32_t uiCRCCalRes = 0;
150
151 BOOT_PRINTF(UBOOT_NOTICE, "(%s)CRC Calculate start\n",pcPartName);
152 uiCRCCalRes = crc32(0, (unsigned char*)uiEntryPoint, uiImgSize);
153 BOOT_PRINTF(UBOOT_NOTICE, "(%s) CRC Calculate Res=0x%0x, Size=%d Bytes\n",
154 pcPartName, uiCRCCalRes, uiImgSize);
155 if(uiCRCChkSum != uiCRCCalRes||uiCRCCalRes == 0)
156 {
157 BOOT_PRINTF(UBOOT_ERR, "(%s) CRC Check Failed!\n", pcPartName);
158 return 1;
159 }
160 BOOT_PRINTF(UBOOT_NOTICE, "(%s) CRC Check PASS!\n", pcPartName);
161 return 0;
162}
163
164/* ================================================================================
165 * reform_zsp_image : ´ÓDDRÖжÁÈ¡ZSPÏà¹ØµÄ°æ±¾
166 * @return 0 : ³É¹¦
167 * @return 1 : ʧ°Ü
168 */
169static int reform_zsp_image(uint32_t addr)
170{
171 int ret = 0;
172 int offSet = 0;
173 int lenToRead = 0;
174 int length = 0;
175
176 offSet = addr; /*DDR address for zsp,0x25000000*/
177 uint32_t arm_size = ((reg16(offSet))+(reg16(offSet+2)<<16));/*zsp bin length*/
178
179 /*move zsp's bin from zsp buf to ddr address*/
180 offSet = offSet + 0x4;
181
182 while(1)
183 {
184 if(length >= arm_size - 0x4)
185 {
186 break;
187 }
188 if((reg16(offSet+0x4)+(reg16(offSet+0x4+2)<<16))*2 <0x20000000)//not in ddr
189 {
190 lenToRead = (reg16(offSet+0x8)+(reg16(offSet+0x8+2)<<16))*2;
191 length += lenToRead + 0xc;
192 offSet = offSet + lenToRead+0xc;
193 continue;
194 }
195
196 lenToRead = (reg16(offSet+0x8)+(reg16(offSet+0x8+2)<<16))*2;
197 memcpy((void*)((reg16(offSet+0x4)+(reg16(offSet+0x4+2)<<16))*2),(const void *)(offSet+0xc),lenToRead);
198 length += lenToRead + 0xc;
199 offSet = offSet + lenToRead+0xc;
200 }
201 return 0;
202
203}
204
205
206/* ================================================================================
207 * load_arm_image : ´ÓFLASHÖжÁÈ¡ARM°æ±¾
208 * @return 0 : ³É¹¦
209 * @return 1 : ʧ°Ü
210 */
211int load_arm_image( uchar * part_name )
212{
213 int ret = 0;
214 struct flash_ops *flash = NULL;
215
216#if TIME_DEBUG
217 ulong start_time = 0;
218#endif
219
220 flush_dcache_all();
221 flash_dmabuf_disable_flag = 1;
222 flash = get_flash_ops();
223 time_debug_reset(start_time);
224 load_debug_printf("\n");
225
226 /* ѰÕÒ·ÖÇø */
227 partition_entry_t * entry = find_partition_para(part_name);
228 if( entry == NULL )
229 {
230 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
231 return 1;
232 }
233
234 /* »ñµÃ·ÖÇøÊ×µØÖ· */
235 uint32_t part_offset = entry->part_offset;
236 nand_info_t *nand = &nand_info[nand_curr_device];
237
238 /* ¶ÁÈ¡°æ±¾ËùÔÚµØÖ·µÄµÚÒ»Ò³Êý¾Ý */
239 uint32_t page_size = flash->page_size; /*¶ÁÈ¡arm °æ±¾mmcµÚÒ»¿éÊý¾Ý*/
240 ret = flash->read(nand,(loff_t)part_offset,&page_size,(u_char *)CONFIG_SYS_SDRAM_TEMP_BASE);
241 if( ret != 0 )
242 {
243 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the first page error!\n", part_name);
244 return 1;
245 }
246
247 /* »ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
248 image_header_t *header = (image_header_t *)CONFIG_SYS_SDRAM_TEMP_BASE;
249 if( ___htonl(header->ih_magic) != IH_MAGIC )
250 {
251 BOOT_PRINTF(UBOOT_ERR, "[%s]: NO bin !!!\n", part_name );
252 return 1;
253 }
254
255 uint32_t arm_size = ___htonl(header->ih_size);
256 uint32_t entey_point = ___htonl(header->ih_ep);
257 if( set_entry_point(part_name, entey_point) )
258 {
259 BOOT_PRINTF(UBOOT_ERR, "[%s][set_entry_point]: error!\n", part_name );
260 }
261
262 BOOT_PRINTF(UBOOT_NOTICE, "[%s] [size=0x%0x] from [0x%0x] to [0x%0x]\n",
263 part_name, arm_size, part_offset, entey_point);
264
265#if LOAD_IMAGE_CRC
266 uint32_t crc_bin = ___htonl(header->ih_dcrc);
267 BOOT_PRINTF(UBOOT_NOTICE, "[%s][crc_bin] ------------------- [0x%0x]\n", part_name, crc_bin);
268#endif
269
270 /* Õû¸öÍ·ºÍ·ÖÇø±íµÄ´óС */
271 uint32_t image_header_size = sizeof(image_header_t);
272 uint32_t arm_is_read_size = flash->page_size - image_header_size;
273 uint32_t arm_size_left = arm_size - arm_is_read_size; /* »¹Ã»ÓжÁÈ¡µÄ³¤¶È */
274 arm_size_left = page_align(arm_size_left); /* Ò³¶ÔÆë */
275
276 /* ¸´ÖƵÚÒ»Ò³ÖеÄÊý¾Ý */
277 memcpy((uchar *)entey_point, (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + image_header_size), arm_is_read_size);
278 /* ¶ÁȡʣÓàµÄÊý¾Ý */
279
280 ret = flash->read(nand,(loff_t)(part_offset + page_size), &arm_size_left,(u_char *)(entey_point + arm_is_read_size));
281 if( ret != 0 )
282 {
283 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the others page error...\n", part_name);
284 return 1;
285 }
286
287 BOOT_PRINTF(UBOOT_NOTICE, "[loading...] -------------------- takes [%ld] us\n", start_time);
288
289#if LOAD_IMAGE_CRC
290 time_debug_reset(start_time);
291 uint32_t crc_cal = crc32(0,(unsigned char*)entey_point, arm_size);
292 BOOT_PRINTF(UBOOT_NOTICE, "[%s][crc_bin] ------------------- [0x%0x]\n", part_name, crc_cal);
293 if( crc_bin != crc_cal )
294 {
295 BOOT_PRINTF(UBOOT_ERR, "[%s] ---------------- crc error...\n", part_name);
296 return 1;
297 }
298 BOOT_PRINTF(UBOOT_NOTICE, "[crc...] ------------------------ takes [%ld] us\n", start_time);
299#endif /* LOAD_IMAGE_CRC */
300
301 flash_dmabuf_disable_flag = 0;
302
303 return 0;
304}
305
306
307int fs_load_m0_image(void)
308{
309 char cmd[64] = {0};
310 int remap = 0;
311
312 remap = readl(0x140000);
313 remap |= 0x800000;
314 writel(remap,0x140000);
315
316 /*1¡¢½«m0 imgÎļþloadµ½ÁÙʱµØÖ·*/
317#ifdef CONFIG_ZX297520V3E_MDL_AB
318 if(imagefs_flag == 1)
319 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
320 else
321 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
322#else
323 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
324#endif
325 run_command(cmd, 0);
326 flush_dcache_all();
327
328 /*2¡¢ÉèÖÃM0µÄÈë¿ÚµØÖ·ÒÔ¼°M0°æ±¾°áÔËÍê³Éflagµ½iramÖÐ */
329 writel(1, M0_IMAGE_READY_FLAG_ADDR);
330
331 /*3¡¢µÈ´ýM0¿½±´Íê³É*/
332 while(readl(M0_IMAGE_READY_FLAG_ADDR));
333
334 printf("M0 image load success!\n");
335
336 return 0;
337}
338
339
340#if defined(CONFIG_ZX297520V3E_JFFS2_COMPRESS)
341uint32_t lzma_header(uint32_t *ztelzma_dict,
342 int *ztelzma_lc,
343 int *ztelzma_lp,
344 int *ztelzma_pb)
345{
346 uint8_t temp_properties = 0;
347 uint32_t temp_dict = 0;
348 uint32_t temp_uncompress_size = 0;
349 uint32_t lzma_uncompresssize = 0;
350 lzma_header_t *lzmaheader;
351
352 lzmaheader = (lzma_header_t *)CONFIG_SYS_SDRAM_TEMP_BASE;
353
354 temp_properties = lzmaheader->p_properties;
355 temp_dict = (lzmaheader->p_dict[3]<<24)
356 |(lzmaheader->p_dict[2]<<16)
357 |(lzmaheader->p_dict[1]<<8)
358 |lzmaheader->p_dict[0];
359 temp_uncompress_size = (lzmaheader->p_uncompress_size[3]<<24)
360 |(lzmaheader->p_uncompress_size[2]<<16)
361 |(lzmaheader->p_uncompress_size[1]<<8)
362 |lzmaheader->p_uncompress_size[0];
363
364 *ztelzma_pb = temp_properties / (9 * 5);
365 temp_properties -= *ztelzma_pb * 9 * 5;
366 *ztelzma_lp = temp_properties / 9 ;
367 *ztelzma_lc = temp_properties - *ztelzma_lp * 9;
368 *ztelzma_dict = temp_dict;
369 lzma_uncompresssize = temp_uncompress_size;
370
371 return lzma_uncompresssize;
372}
373
374int fs_load_zsp_image(void)
375{
376 uint32_t image_tmp_buf = 0;
377 int zspimagenum=0;
378 uint32_t ztelzma_uncompresssize = 0;
379 uint32_t ztelzma_dict=0;
380 int ztelzma_lc=0;
381 int ztelzma_lp=0;
382 int ztelzma_pb=0;
383 int firstlzma_flag = 0;
384 int lzma_init_flag = 0;
385 int lzmainit_ret = 0;
386 int lzmadecompr_ret = 1;
387 uint32_t lzmaoffsize = 0;
388 char cmd[64] = {0};
389 uint32_t uiImgHdrlzma = 13;
390
391 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load begin...\n");
392
393 lzmanodeflag = 1;
394
395 /* ÁÙʱ´æ·Å½âѹºóµÄzsp°æ±¾ */
396 image_tmp_buf = CONFIG_SYS_SDRAM_TEMP_LZMA;
397
398 while(lzmanodeflag == 1)
399 {
400 /* ½«zsp imgÎļþloadµ½ÁÙʱµØÖ·*/
401#ifdef CONFIG_ZX297520V3E_MDL_AB
402 if(imagefs_flag == 1)
403 sprintf(cmd, "fsload imagefs 0x%x cpuphy_%02d.lzma",
404 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
405 zspimagenum);
406 else
407 sprintf(cmd, "fsload imagefs2 0x%x cpuphy_%02d.lzma",
408 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
409 zspimagenum);
410#else
411 sprintf(cmd, "fsload imagefs 0x%x cpuphy_%02d.lzma",
412 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
413 zspimagenum);
414#endif
415 run_command(cmd, 0);
416
417 if(ztelzma_compresssize)
418 {
419 /* ½âѹС°üÎļþ*/
420 ztelzma_uncompresssize=lzma_header(&ztelzma_dict,
421 &ztelzma_lc,
422 &ztelzma_lp,
423 &ztelzma_pb);
424
425 if(lzma_init_flag == 0)
426 {
427 lzmainit_ret = lzma_init(&ztelzma_dict,
428 &ztelzma_lc,
429 &ztelzma_lp,
430 &ztelzma_pb);
431 if(lzmainit_ret < 0)
432 {
433 printf("lzma_init failed\n");
434 return -1;
435 }
436 else
437 {
438 lzma_init_flag = 1;
439 }
440 }
441
442 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrlzma),
443 (unsigned char *)(image_tmp_buf + lzmaoffsize),
444 ztelzma_compresssize,
445 ztelzma_uncompresssize);
446 if(lzmadecompr_ret)
447 {
448 printf("lzma_decompress failed\n");
449 return -1;
450 }
451 lzmaoffsize += ztelzma_uncompresssize ;
452 zspimagenum++;
453 }
454 }
455
456 /* °´ÕÕZSPµÄ¸ñÊ½ÖØÅÅ */
457 reform_zsp_image(image_tmp_buf);
458 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load finished.\n");
459
460 return 0;
461}
462
463int fs_load_arm_image_linux(char* image_name)
464{
465 char cmd[64] = {0};
466 int ret = 0;
467 int lzmainit_ret = 0;
468 int lzmadecompr_ret = 1;
469 u8 ucRet = 0;
470 int apimagenum=0;
471 uint32_t uiImgSize = 0;
472 uint32_t uiEntryPoint = 0;
473 uint32_t uiLoadPoint = 0;
474 uint32_t lzmaLoadPoint = 0;
475 uint32_t ztelzma_uncompresssize = 0;
476 uint32_t ztelzma_dict=0;
477 int ztelzma_lc=0;
478 int ztelzma_lp=0;
479 int ztelzma_pb=0;
480 int firstlzma_flag = 0;
481#if LOAD_IMAGE_CRC
482 uint32_t uiCRCChkSum = 0;
483 uint32_t uiCRCCalRes = 0;
484#endif
485 uint32_t uiImgHdrSizeOld = sizeof(image_header_t);
486 uint32_t uiImgHdrSizeNew = 0;
487 uint32_t uiImgHdrlzma = 13;
488 uint32_t lastlzmaSize = 0;
489
490 int lzma_init_flag = 0;
491 lzmanodeflag = 1;
492
493 BOOT_PRINTF(UBOOT_NOTICE, "AP image load begin...\n");
494
495 uiImgHdrSizeNew = sizeof(sImageHeader);
496
497 image_header_t *psImgHdrOld = NULL;
498
499 while(lzmanodeflag == 1)
500 {
501 /*1¡¢½«ap imgÎļþloadµ½ÁÙʱµØÖ·*/
502#ifdef CONFIG_ZX297520V3E_MDL_AB
503 if(imagefs_flag == 1)
504 sprintf(cmd, "fsload imagefs 0x%x /%s_%02d.lzma",
505 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
506 else
507 sprintf(cmd, "fsload imagefs2 0x%x /%s_%02d.lzma",
508 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
509#else
510 sprintf(cmd, "fsload imagefs 0x%x /%s_%02d.lzma",
511#endif
512 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
513
514 run_command(cmd, 0);
515
516 if(ztelzma_compresssize)
517 {
518 /*2¡¢½âѹС°üÎļþ*/
519 ztelzma_uncompresssize=lzma_header(&ztelzma_dict,
520 &ztelzma_lc,
521 &ztelzma_lp,
522 &ztelzma_pb);
523
524 if(lzma_init_flag == 0)
525 {
526 lzmainit_ret = lzma_init(&ztelzma_dict,
527 &ztelzma_lc,
528 &ztelzma_lp,
529 &ztelzma_pb);
530 if(lzmainit_ret < 0)
531 {
532 printf("lzma_init failed\n");
533 return -1;
534 }
535 else
536 {
537 lzma_init_flag = 1;
538 }
539 }
540
541 /*3¡¢»ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
542 if(firstlzma_flag == 0)
543 {
544 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE+uiImgHdrlzma),
545 (unsigned char *)(CONFIG_SYS_SDRAM_TEMP_LZMA),
546 ztelzma_compresssize,
547 ztelzma_uncompresssize);
548 if(lzmadecompr_ret)
549 {
550 printf("lzma_decompress failed\n");
551 return -1;
552 }
553
554 /*»ñÈ¡¹«Ô¿´«µÝ¸ø´ó°æ±¾Ê¹ÓÃ*/
555 memcpy(CFG_SECURE_PUK_ADDR,CONFIG_SYS_SDRAM_TEMP_LZMA,256);
556
557 psImgHdrOld = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_LZMA + uiImgHdrSizeNew);
558
559 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
560 {
561 BOOT_PRINTF(UBOOT_ERR, "Magic Num Check Failed,Maybe no AP bin !!!\n");
562 return 1;
563 }
564
565 uiImgSize = ___htonl(psImgHdrOld->ih_size);
566 uiEntryPoint = ___htonl(psImgHdrOld->ih_ep);
567 uiLoadPoint = ___htonl(psImgHdrOld->ih_load) - uiImgHdrSizeOld; /* ÕâÀïʹÓÃLOADµØÖ· */
568 lzmaLoadPoint = uiLoadPoint;
569 BOOT_PRINTF(UBOOT_NOTICE, "Load AP image, Size=0x%0x, to 0x%0x.\n",
570 uiImgSize, uiLoadPoint);
571
572#if LOAD_IMAGE_CRC
573 uiCRCChkSum = ___htonl(psImgHdrOld->ih_dcrc);
574 BOOT_PRINTF(UBOOT_NOTICE, "AP image CRC Checksum=0x%0x.\n", uiCRCChkSum);
575#endif
576 /*4¡¢¿½±´µÚÒ»°ü°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
577 memcpy((uchar *)(uiLoadPoint - uiImgHdrSizeNew),
578 (uchar *)(CONFIG_SYS_SDRAM_TEMP_LZMA),
579 ztelzma_uncompresssize);
580 firstlzma_flag = 1;
581 lastlzmaSize = ztelzma_uncompresssize - uiImgHdrSizeNew;
582 }
583 else
584 {
585 lzmaLoadPoint = lzmaLoadPoint + lastlzmaSize ;
586 lastlzmaSize = ztelzma_uncompresssize;
587 /*5¡¢¿½±´Ê£Óà°ü°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
588
589 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE+uiImgHdrlzma),
590 (unsigned char *)(lzmaLoadPoint),
591 ztelzma_compresssize, lastlzmaSize);
592 if(lzmadecompr_ret)
593 {
594 printf("lzma_decompress failed\n");
595 return -1;
596 }
597 }
598 apimagenum++;
599 }
600 }
601
602 BOOT_PRINTF(UBOOT_NOTICE, "AP image load finished\n");
603
604 if(guiEfuseStatus == 0) //Secure Verify.
605 {
606 BOOT_PRINTF(UBOOT_DBG, "AP image Start Secure Verify...\n");
607
608 ucRet = secure_verify((u32 )uiLoadPoint - uiImgHdrSizeNew);
609 if(ucRet != 0)
610 {
611 BOOT_PRINTF(UBOOT_ERR, "AP image Secure Verify FAILED!\n");
612 return 1;
613 }
614 BOOT_PRINTF(UBOOT_NOTICE, "AP image Secure Verify PASS!\n");
615 }
616 else
617 {
618 BOOT_PRINTF(UBOOT_NOTICE, "AP image Skip Secure Verify...\n");
619 }
620
621 if(strncmp((const char *)image_name, "cpucap", 6) == 0)
622 {
623 arm_cpucap_ep = uiEntryPoint; /* usually */
624 }
625 else
626 {
627 sys_ddr_kernel_start = uiEntryPoint - 0x8000; /* usually */
628 gd->bd->bi_boot_params = sys_ddr_kernel_start + 0x100;
629 sprintf((char *)cmd," bootm 0x%0x", uiLoadPoint);
630 setenv("bootcmd", (char *)cmd);
631 }
632
633#if LOAD_IMAGE_CRC
634 //invalidate_dcache_range(uiEntryPoint, uiEntryPoint + uiImgSize);
635 ret = arm_image_crc_calc(ARM_APP_IMAGE, uiCRCChkSum, uiEntryPoint, uiImgSize);
636 if(ret != 0)
637 {
638 BOOT_PRINTF(UBOOT_ERR, "ap image crc calc failed, ret = %d! \n", ret);
639 return -1;
640 }
641#endif
642
643 return 0;
644
645}
646#else
647
648int fs_load_zsp_image(void)
649{
650 char cmd[64] = {0};
651 uint32_t image_tmp_buf = 0;
652
653 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load begin...\n");
654 image_tmp_buf = CONFIG_SYS_SDRAM_TEMP_BASE;
655#ifdef CONFIG_ZX297520V3E_MDL_AB
656 if(imagefs_flag == 1)
657 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
658 else
659 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
660#else
661 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
662#endif
663 run_command(cmd, 0);
664
665 reform_zsp_image(image_tmp_buf);
666 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load finished.\n");
667
668 return 0;
669}
670
671int fs_load_arm_image_linux(char* image_name )
672{
673 char cmd[64] = {0};
674 int ret = 0;
675 u8 ucRet = 0;
676
677 uint32_t uiImgSize = 0;
678 uint32_t uiEntryPoint = 0;
679 uint32_t uiLoadPoint = 0;
680#if LOAD_IMAGE_CRC
681 uint32_t uiCRCChkSum = 0;
682 uint32_t uiCRCCalRes = 0;
683#endif
684 uint32_t uiImgHdrSizeOld = sizeof(image_header_t);
685 uint32_t uiImgHdrSizeNew = 0;
686
687 uiImgHdrSizeNew = sizeof(sImageHeader);
688
689 image_header_t *psImgHdrOld = NULL;
690
691 /*1¡¢½«ap imgÎļþloadµ½ÁÙʱµØÖ·*/
692#ifdef CONFIG_ZX297520V3E_MDL_AB
693 if(imagefs_flag == 1)
694 sprintf(cmd, "fsload imagefs 0x%x /ap_%s.bin",
695 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
696 else
697 sprintf(cmd, "fsload imagefs2 0x%x /ap_%s.bin",
698 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
699#else
700 sprintf(cmd, "fsload imagefs 0x%x /ap_%s.bin",
701 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
702#endif
703 run_command(cmd, 0);
704
705 /*»ñÈ¡¹«Ô¿´«µÝ¸ø´ó°æ±¾Ê¹ÓÃ*/
706 memcpy(CFG_SECURE_PUK_ADDR,CONFIG_SYS_SDRAM_TEMP_BASE,256);
707
708 /*2¡¢»ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
709 psImgHdrOld = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew);
710 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
711 {
712 BOOT_PRINTF(UBOOT_ERR, "Magic Num Check Failed,Maybe no AP bin !!!\n");
713 return 1;
714 }
715
716 uiImgSize = ___htonl(psImgHdrOld->ih_size);
717 uiEntryPoint = ___htonl(psImgHdrOld->ih_ep);
718 uiLoadPoint = ___htonl(psImgHdrOld->ih_load) - uiImgHdrSizeOld; /* ÕâÀïʹÓÃLOADµØÖ· */
719
720 BOOT_PRINTF(UBOOT_NOTICE, "Load AP image, Size=0x%0x, to 0x%0x.\n",
721 uiImgSize, uiLoadPoint);
722
723#if LOAD_IMAGE_CRC
724 uiCRCChkSum = ___htonl(psImgHdrOld->ih_dcrc);
725 BOOT_PRINTF(UBOOT_NOTICE, "AP image CRC Checksum=0x%0x.\n", uiCRCChkSum);
726#endif
727 BOOT_PRINTF(UBOOT_NOTICE, "AP image uiLoadPoint=0x%0x.\n", uiLoadPoint);
728
729 /*3¡¢¿½±´°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
730 memcpy((uchar *)uiLoadPoint,
731 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew),
732 uiImgSize +uiImgHdrSizeOld);
733
734 BOOT_PRINTF(UBOOT_NOTICE, "AP image load image finished\n");
735
736 if(guiEfuseStatus == 0) //Secure Verify.
737 {
738 BOOT_PRINTF(UBOOT_DBG, "AP image Start Secure Verify...\n");
739
740 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
741 if(ucRet != 0)
742 {
743 BOOT_PRINTF(UBOOT_ERR, "AP image Secure Verify FAILED!\n");
744 return 1;
745 }
746 BOOT_PRINTF(UBOOT_NOTICE, "AP image Secure Verify PASS!\n");
747 }
748 else
749 {
750 BOOT_PRINTF(UBOOT_NOTICE, "AP image Skip Secure Verify...\n");
751 }
752
753 sys_ddr_kernel_start = uiEntryPoint - 0x8000; /* usually */
754 gd->bd->bi_boot_params = sys_ddr_kernel_start + 0x100;
755
756 sprintf((char *)cmd," bootm 0x%0x", uiLoadPoint);
757 setenv("bootcmd", (char *)cmd);
758
759#if LOAD_IMAGE_CRC
760 //invalidate_dcache_range(uiEntryPoint, uiEntryPoint + uiImgSize);
761 ret = arm_image_crc_calc(ARM_APP_IMAGE, uiCRCChkSum, uiEntryPoint, uiImgSize);
762 if(ret != 0)
763 {
764 BOOT_PRINTF(UBOOT_ERR, "ap image crc calc failed, ret = %d! \n", ret);
765 return -1;
766 }
767#endif
768
769 return 0;
770}
771
772#endif
773
774int load_imagefs(uchar * part_name)
775{
776 int ret = 0;
777 int type = 0;
778 struct flash_ops *flash = NULL;
779 uint32_t i = 0;
780 uint32_t bad_nums = 0;
781 uint32_t part_block_nums = 0;
782 uint32_t part_offset = 0;
783 uint32_t part_size = 0;
784 nand_info_t *nand = &nand_info[nand_curr_device];
785 struct fsl_qspi *nor = &spi_nor_flash;
786
787 /* ´Ë´¦±ØÐëÇå0xFF£¬±£Ö¤¾µÏñÍêÕû£¬²»ÊÜÉÏ´ÎÒÅÁôÊý¾ÝÓ°Ïì*/
788 memset(CONFIG_SYS_SDRAM_IMAGEFS_BASE, 0xFF, CONFIG_SYS_SDRAM_IMAGEFS_SIZE);
789 flush_dcache_all();
790
791 flash_dmabuf_disable_flag = 1;
792 flash = get_flash_ops();
793 type = read_boot_flashtype();
794
795 /* ѰÕÒ·ÖÇø */
796 partition_entry_t * entry = find_partition_para(part_name);
797 if( entry == NULL )
798 {
799 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
800 return 1;
801 }
802
803 /* »ñµÃ·ÖÇøÊ×µØÖ· */
804 part_offset = entry->part_offset;
805 part_size = entry->part_size;
806
807 if(part_size > CONFIG_SYS_SDRAM_IMAGEFS_SIZE)
808 {
809 BOOT_PRINTF(UBOOT_ERR, "[%s] part size more than SDRAM IMAGEFS space !!!\n", part_name);
810 return 1;
811 }
812
813 if(type == IF_TYPE_NAND || type == IF_TYPE_SPI_NAND)
814 {
815 /* ²éѯµ±Ç°·ÖÇø»µ¿éÊý*/
816 bad_nums = 0;
817 part_block_nums = entry->part_size / nand->erasesize;
818 for(i = 0; i < part_block_nums; i++)
819 {
820 if(nand_block_isbad (nand, entry->part_offset + (loff_t)i * nand->erasesize))
821 {
822 printf("bad block addr = 0x%x\n", (entry->part_offset + i * nand->erasesize));
823 bad_nums++;
824 }
825 }
826
827 part_size = part_size - bad_nums * nand->erasesize;
828 ret = flash->read(nand,(loff_t)part_offset,
829 &part_size,(u_char *)CONFIG_SYS_SDRAM_IMAGEFS_BASE);
830 if( ret != 0 )
831 {
832 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the imagefs error!\n", part_name);
833 return 1;
834 }
835 }
836 else if(type == IF_TYPE_NOR)
837 {
838 ret = nand_read(&(nor->nor[0].mtd), (loff_t)part_offset,
839 &part_size, (u_char *)CONFIG_SYS_SDRAM_IMAGEFS_BASE);
840 if(ret)
841 {
842 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the imagefs error!\n", part_name);
843 return -1;
844 }
845 }
846
847 flash_dmabuf_disable_flag = 0;
848 flush_dcache_all();
849
850 return 0;
851}
852
853#ifdef CONFIG_ZX297520V3E_MDL_AB
854int load_flags(T_FOTA_FLAG_INFO *fotaFlagInfo)
855{
856 int ret = 0;
857 int type = 0;
858 uchar * part_name = "flags";
859 struct flash_ops *flash = NULL;
860 uint32_t i = 0;
861 uint32_t off = 0;
862 uint32_t bad_nums = 0;
863 uint32_t part_block_nums = 0;
864 uint32_t part_offset = 0;
865 uint32_t part_size = 0;
866 uint32_t blockNum = 0;
867 uint32_t work_area_offset = 0;
868 uint32_t backup_area_offset = 0;
869 uint32_t flag_one = 0;
870 uint32_t fota_size = sizeof(T_FOTA_FLAG_INFO);
871 nand_info_t *nand = &nand_info[nand_curr_device];
872 struct fsl_qspi *nor = &spi_nor_flash;
873
874 flush_dcache_all();
875 flash_dmabuf_disable_flag = 1;
876 flash = get_flash_ops();
877 type = read_boot_flashtype();
878
879 /* ѰÕÒ·ÖÇø */
880 partition_entry_t * entry = find_partition_para(part_name);
881 if( entry == NULL )
882 {
883 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
884 return 1;
885 }
886
887 /* »ñµÃ·ÖÇøÊ×µØÖ· */
888 part_offset = entry->part_offset;
889 part_size = entry->part_size;
890 //while(flag);
891 if(type == IF_TYPE_NAND || type == IF_TYPE_SPI_NAND)
892 {
893 /*È·¶¨¹¤×÷ÇøºÍ±¸·ÝÇøÆ«ÒÆµØÖ·*/
894 for (off = part_offset; off < part_offset+part_size; off += nand->erasesize)
895 {
896 if (!(nand_block_isbad(nand,off)))
897 {
898 blockNum += 1;
899 }
900
901 if((blockNum == 1) && (flag_one == 0))
902 {
903 work_area_offset = off;
904 flag_one = 1;
905 }
906
907 else if(blockNum == 2)
908 {
909 backup_area_offset = off;
910 break;
911 }
912 }
913
914 if(blockNum < 2)
915 {
916 printf("flags partition have not enough space!\n");
917
918 return -1;
919 }
920
921 ret = flash->read(nand,(loff_t)work_area_offset,
922 &fota_size,(u_char *)(fotaFlagInfo));
923 if( ret != 0 )
924 {
925 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the flags error!\n", part_name);
926 return 1;
927 }
928
929 if(fotaFlagInfo->boot_flag.magic != FLAGS_MAGIC)
930 {
931 flush_dcache_all();
932 ret = flash->read(nand,(loff_t)backup_area_offset,
933 &fota_size,(u_char *)(fotaFlagInfo));
934 if(ret != 0)
935 {
936 printf("read flags backup partition err.\n");
937
938 return -1;
939 }
940
941 if(fotaFlagInfo->boot_flag.magic != FLAGS_MAGIC)
942 {
943 printf("flags magic err.\n");
944 return -1;
945 }
946 }
947 }
948 else if(type == IF_TYPE_NOR)
949 {
950 work_area_offset = part_offset;
951 backup_area_offset = part_offset + nor->nor[0].mtd.erasesize;
952 ret = nand_read(&(nor->nor[0].mtd), (loff_t)work_area_offset,
953 &fota_size, (u_char *)(fotaFlagInfo));
954 if(ret)
955 {
956 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the flags error!\n", part_name);
957 return -1;
958 }
959 if(fotaFlagInfo->boot_flag.magic != FLAGS_MAGIC)
960 {
961 flush_dcache_all();
962 ret = nand_read(&(nor->nor[0].mtd), (loff_t)backup_area_offset,
963 &fota_size, (u_char *)(fotaFlagInfo));
964 if(ret != 0)
965 {
966 printf("read flags backup partition err.\n");
967
968 return -1;
969 }
970
971 if(fotaFlagInfo->boot_flag.magic != FLAGS_MAGIC)
972 {
973 printf("flags magic err.\n");
974 return -1;
975 }
976 }
977 }
978
979 flash_dmabuf_disable_flag = 0;
980 flush_dcache_all();
981
982 return 0;
983}
984
985#endif
986/* ================================================================================
987 * get_fota_update_flag :
988 * @return 0 : not fota update
989 * @return 1 : fota update
990 * @return -1 : error
991 */
992int get_fota_update_flag( void )
993{
994 char cmd[64] = {0};
995#ifdef CONFIG_ZX297520V3E_MDL_AB
996 if(imagefs_flag == 1)
997 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
998 else
999 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
1000#else
1001 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
1002#endif
1003 run_command(cmd, 0);
1004
1005 if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_FOTA_FLAG, strlen(ARM_FOTA_FLAG)) == 0)
1006 {
1007 fota_upflag = FOTA_UPDATE;
1008 }
1009 else if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_LOCAL_UPDATE_FLAG, strlen(ARM_LOCAL_UPDATE_FLAG)) == 0)
1010 {
1011 fota_upflag = FOTA_LOCALUPDATE;
1012 }
1013 else if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_RECOVERY_FLAG, strlen(ARM_RECOVERY_FLAG)) == 0)
1014 {
1015 fota_upflag = FOTA_RECOVERY;
1016 }
1017 else
1018 {
1019 fota_upflag = FOTA_NORMAL;
1020 }
1021
1022 BOOT_PRINTF(UBOOT_NOTICE, "fota_upflag=%d\n", fota_upflag);
1023
1024 return 0;
1025}
1026
1027/* ================================================================================
1028 * get_gmac_init_flag :
1029 * @return 0 :
1030 * @return 1 :
1031 * @return -1 :
1032 */
1033int get_gmac_init_flag( void )
1034{
1035 //hsy
1036 return 0;
1037}
1038
1039/* ================================================================================
1040 * read_gmac_init_flag :
1041 * @return 0 : not init
1042 * @return 1 : init
1043 */
1044int read_gmac_init_flag( void )
1045{
1046 return g_gmac_init_flag;
1047}
1048
1049/* ================================================================================
1050 * read_gmac_init_overtime :
1051 * @return : Ãë
1052 */
1053int read_gmac_init_overtime( void )
1054{
1055 return g_gmac_init_overtime;
1056}
1057
1058/* ================================================================================
1059 * read_fota_update_flag :
1060 * @return 0 : not fota update
1061 * @return 1 : fota update
1062 */
1063int read_fota_update_flag( void )
1064{
1065 return fota_upflag;
1066}
1067
1068/* ================================================================================
1069 * read_fota_update_flag :
1070 * @return 0 : not fota update
1071 * @return 1 : fota update
1072 */
1073
1074int read_fota_psup_flag( void )
1075{
1076 return fota_psup_flag;
1077}
1078
1079/* ================================================================================
1080 * start_arm_ps : Æô¶¯ ARM_PS °æ±¾
1081 */
1082void start_arm_ps( void )
1083{
1084 void (*ps_start)();
1085
1086 printf("Starting the arm_ps ...\n");
1087 ps_start = (void (*)())arm_ps_ep;
1088 ps_start();
1089}
1090
1091/* ================================================================================
1092 * start_arm_phy : Æô¶¯ ARM_PHY °æ±¾
1093 */
1094void start_arm_phy( void )
1095{
1096 /* д PHY Ìø×ªÆô¶¯´úÂë */
1097 writel(0xE59ff000, SYS_IRAM3_BASE);
1098 writel(arm_phy_ep, SYS_IRAM3_BASE + 8);
1099
1100 /* ÊÍ·ÅÆô¶¯ ARM_PHY */
1101 printf("Starting the arm_phy ...\n");
1102 writel(CPU_PHY_SW_RSTEN, CPU_PHY_SUBSYS_CFG);
1103}
1104
1105/* ================================================================================
1106 * start_cpucap_cores : Æô¶¯°æ±¾
1107 */
1108void start_cpucap_cores( void )
1109{
1110 writel(0xE59ff000, SYS_CPUCAP_BOOT_BASE);
1111 writel(arm_cpucap_ep, SYS_CPUCAP_BOOT_BASE + 8);
1112
1113 BOOT_PRINTF(UBOOT_NOTICE, "Starting the cpu cap ...\n");
1114 writel(CPU_CAP_SW_RSTEN, CPU_CAP_SUBSYS_CFG);
1115}
1116
1117static void usdelay(volatile int count)
1118{
1119 volatile int cnt = 0;
1120 count =count *8;
1121
1122 while(cnt<count)
1123 {
1124 cnt++;
1125 }
1126 return;
1127}
1128void cap_poweron(void)
1129{
1130 u32 tmp;
1131
1132 BOOT_PRINTF(UBOOT_NOTICE, "cap_poweron ...\n");
1133
1134 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg on*/
1135 tmp |= (0x1<<3);
1136 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1137
1138 usdelay(1);
1139
1140 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg rst*/
1141 tmp &= ~(0x1<<4);
1142 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1143
1144 usdelay(1);
1145
1146 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg iso*/
1147 tmp &= ~(0x1<<5);
1148 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1149
1150 usdelay(1);
1151
1152 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG1) ;/*ap clk on*/
1153 tmp |= (0x1<<2);
1154 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG1);
1155
1156 tmp = readl(CORE_OUTPUT_SWITCH_CONFIG_REG) ; /*ap clk&mg control by hw*/
1157 tmp |= ((0x1<<2)|(0x1<<5));
1158 writel(tmp, CORE_OUTPUT_SWITCH_CONFIG_REG);
1159 //__REG(0x0013a0ac) |= ((0x1<<2)|(0x1<<5)); /*ap clk&mg control by hw*/
1160 //__REG(0x0013a0bc) |= (0x1<<3); /*ap mg off*/
1161 //__REG(0x0013a0bc) &= ~(0x1<<4); /*ap mg rst*/
1162 //__REG(0x0013a0bc) &= ~(0x1<<5); /*ap mg iso*/
1163 //__REG(0x0013a0b8) |= (0x1<<2); /*ap clk off*/
1164
1165}
1166
1167/* ================================================================================
1168 * read_sys_ddr_kernel_start : »ñÈ¡ kernel ÔËÐпռä Ê×µØÖ·
1169 */
1170uint32_t read_sys_ddr_kernel_start(void)
1171{
1172 /* Get Addr from Image Header. */
1173 return sys_ddr_kernel_start;
1174}
1175
1176/* ================================================================================
1177 * update_led_enable : enable led
1178 * @return 0 : ³É¹¦
1179 * @return 1 : ʧ°Ü
1180 */
1181int update_led_enable(int enable)
1182{
1183 int ret = 0;
1184
1185 if(enable)
1186 {
1187#ifdef CONFIG_BOARD_7520_UFI_956
1188 ret = sn3216_SetStatus(LED_CHANNEL_WAN_GREEN,SN3216_LED_STATUS_ON);
1189 ret = sn3216_SetStatus(LED_CHANNEL_LAN_GREEN,SN3216_LED_STATUS_ON);
1190 ret = sn3216_SetStatus(LED_CHANNEL_BAT_GREEN,SN3216_LED_STATUS_ON);
1191 ret = sn3216_SetStatus(LED_CHANNEL_SMS_GREEN,SN3216_LED_STATUS_ON);
1192#endif
1193 }
1194 else
1195 {
1196#ifdef CONFIG_BOARD_7520_UFI_956
1197 ret = sn3216_SetStatus(LED_CHANNEL_WAN_GREEN,SN3216_LED_STATUS_OFF);
1198 ret = sn3216_SetStatus(LED_CHANNEL_LAN_GREEN,SN3216_LED_STATUS_OFF);
1199 ret = sn3216_SetStatus(LED_CHANNEL_BAT_GREEN,SN3216_LED_STATUS_OFF);
1200 ret = sn3216_SetStatus(LED_CHANNEL_SMS_GREEN,SN3216_LED_STATUS_OFF);
1201#endif
1202 }
1203 return ret;
1204}
1205
1206/* ================================================================================
1207 * update_led_twinkle : config led in local update
1208 * @return 0 : ³É¹¦
1209 * @return 1 : ʧ°Ü
1210 */
1211void update_led_twinkle(void)
1212{
1213 int start_time = 0;
1214 if(update_start_time)
1215 {
1216 update_recent_time = get_timer(0);
1217 if(update_recent_time-update_start_time > 6000000) /*1s*/
1218 {
1219 led_state = (~led_state)& 0x00000001;
1220 update_led_enable(led_state); /*LED on/off*/
1221 update_start_time = update_recent_time;
1222 }
1223 }
1224 return ;
1225}
1226
1227/* ================================================================================
1228 * copy_write_part : write data to flash
1229 * @return 0 : ³É¹¦
1230 * @return 1 : ʧ°Ü
1231 */
1232int copy_write_part_gmac(int cnt)
1233{
1234 int ret = 0;
1235 nand_info_t *nand = &nand_info[nand_curr_device];
1236 image_bin_header_t * img_head = &(master_head->image[cnt]);
1237 int filesize = img_head->filelength;
1238 char *partname = (char *)(img_head->partitonname);
1239 int bin_offset = img_head->fileaddr;
1240 int part_offset = img_head->partitonoffset;
1241 //uchar * buf = malloc(12*1024);
1242 uchar *buf = NULL;
1243 partition_entry_t * part = NULL;
1244
1245 buf = kzalloc(12*1024, GFP_KERNEL);
1246 if(buf == NULL)
1247 {
1248 return -1;
1249 }
1250
1251 part = find_partition_para((uchar *)partname);
1252 if(part == NULL)
1253 {
1254 return -1;
1255 }
1256
1257 /* µ±Ç°ÊÇÔÚµçÄÔÉÏÖ´ÐУ¬Ã»ÓеôµçΣÏÕ£¬ËùÒÔ¿ÉÒÔ²Á³ýzloader */
1258 /* zloaderÓÐÁ½²¿·Ö×é³É: 8kµÄbin + 4kµÄ·ÖÇø±í */
1259 if(!strcmp(partname, "zloader"))
1260 {
1261 BOOT_PRINTF(UBOOT_NOTICE, "write part %s, offset=0x%x, filesize=0x%x.\n",
1262 partname, part->part_offset, filesize);
1263 ret = downloader_nand_erase(part,part->part_size);
1264
1265 //memcpy(buf,(u_char *)(CONFIG_SYS_SDRAM_BASE+bin_offset),8192);//zloader
1266 memcpy(buf, (u_char *)(CONFIG_LOADADDR+bin_offset), 8192);//zloader
1267 memcpy((char *)(buf+8192), g_partition_table, 4096);//part
1268
1269 int times = 12*1024/nand->writesize;
1270 int i;
1271 for(i = 0; i < times; i++)
1272 {
1273 ret += nand_write_page_with_no_ecc(nand,
1274 ((loff_t)i*(nand->writesize)),
1275 buf );
1276 buf += nand->writesize;
1277 }
1278 // free(buf);
1279 return ret;
1280 }
1281
1282 if(strcmp(img_head->partitontype,"nand") == 0)
1283 {
1284 BOOT_PRINTF(UBOOT_NOTICE, "write part %s, offset=0x%x, filesize=0x%x\n",
1285 partname, part->part_offset, filesize);
1286 ret = downloader_nand_erase(part,part->part_size);
1287 //ret = nand_write_skip_bad(nand, part->part_offset, &filesize, (u_char *)(CONFIG_SYS_SDRAM_BASE+bin_offset),0);
1288 ret = nand_write_skip_bad(nand, part->part_offset, &filesize, (u_char *)(CONFIG_LOADADDR+bin_offset),0);
1289
1290 }
1291
1292 return ret;
1293}
1294
1295/* ================================================================================
1296 * copy_ddr_allbin : webui local update
1297 * @return 0 : ³É¹¦
1298 * @return 1 : ʧ°Ü
1299 */
1300int copy_ddr_allbin(void)
1301{
1302 int ret = 0;
1303 int i = 0;
1304 int head_size = 0x2000; //total 6608 bytes
1305 nand_info_t *nand = &nand_info[nand_curr_device];
1306
1307 master_head = kzalloc(0x2000, GFP_KERNEL);
1308 if(master_head == NULL)
1309 {
1310 BOOT_PRINTF(UBOOT_ERR, "kzalloc fail!\n");
1311 return -1;
1312 }
1313
1314 memcpy(( u_char *)master_head,(char *)CONFIG_LOADADDR, head_size);
1315
1316 BOOT_PRINTF(UBOOT_NOTICE, "file number = %d\n", master_head->dwFileNumber);
1317
1318 for(i = 0; i < (sizeof(master_head->image)/sizeof(image_bin_header_t))
1319 && i < master_head->dwFileNumber; i++) /* update each partition */
1320 {
1321 ret = copy_write_part_gmac(i);
1322 if(ret != 0)
1323 {
1324 BOOT_PRINTF(UBOOT_ERR, "write from ddr to nand ERROR !!!\n");
1325 return -1;
1326 }
1327 }
1328
1329 return 0;
1330}
1331