blob: 2c9c8e706ba8b145395b28687c893f34f8c20d03 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -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>
xf.li6c8fc1e2023-08-12 00:11:09 -070034#include "pub_flags.h"
lh9ed821d2023-04-07 01:36:19 -070035
36#if LOAD_IMAGE_DEBUG
37#define load_debug_printf(fmt,args...) printf (fmt ,##args)
38#else
39#define load_debug_printf(fmt,args...)
40#endif /* LOAD_IMAGE_DEBUG */
41
42#if TIME_DEBUG
43#define time_debug_reset(fmt) fmt = get_timer(0)
44#define time_debug_printf(fmt, val) printf(fmt ,get_timer(val))
45#else
46#define time_debug_reset(fmt)
47#define time_debug_printf(fmt, val)
48#endif /* TIME_DEBUG */
49#define reg16(addr) (*(volatile unsigned short*)(addr))
xf.liaa4d92f2023-09-13 00:18:58 -070050#define reg32(addr) (*(volatile unsigned long *)(addr))
51#define RSA_1024 10
52#define RSA_2048 11
lh9ed821d2023-04-07 01:36:19 -070053
54DECLARE_GLOBAL_DATA_PTR;
55
56#define ZSP_IMAGE_PATH "/evb_cpuphy.bin"
57#define M0_IMAGE_PATH "/evb_cpurpm.img"
58#define DTB_IMAGE_PATH "/ap_cpucap.dtb"
59#define FOTAFLAG_PATH "/fotaflag"
60
61#if defined(CONFIG_ZX297520V3E_JFFS2_COMPRESS)
62typedef struct lzmaheader_p{
63 uint8_t p_properties;
64 uint8_t p_dict[4];
65 uint8_t p_uncompress_size[8];
66}lzma_header_t;
67
68extern uint32_t ztelzma_compresssize;
69extern int lzmanodeflag;
70#endif
71
72//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -070073#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -070074extern int imagefs_flag;
75static uint32_t flags;
76#endif
77extern int nand_curr_device;
78extern nand_info_t nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
79extern partition_table_t * g_partition_table;
80extern int flash_dmabuf_disable_flag;
81extern struct fsl_qspi spi_nor_flash;
82
83/* Secure Verify Flag. 1->Disable, 0->Enable */
84extern unsigned int guiEfuseStatus;
xf.liaa4d92f2023-09-13 00:18:58 -070085extern unsigned int guiOtpStatus;
lh9ed821d2023-04-07 01:36:19 -070086
87uint32_t arm_ps_ep = 0; /* Entry Point Address */
88uint32_t arm_cpucap_ep = 0;
89static uint32_t arm_phy_ep = 0; /* Entry Point Address */
90
91static uint32_t fota_upflag = FOTA_NORMAL;
92static uint32_t fota_psup_flag = FOTA_PS_NORMAL;
93
94uint32_t g_gmac_init_flag = 0;
95uint32_t g_gmac_init_overtime = 0;
96
97static uint32_t sys_ddr_kernel_start = 0; /* kernel ÔËÐпռä Ê×µØÖ· */
98
99master_header_t *master_head = NULL;
100int update_start_time = 0;
101int update_recent_time = 0;
102int led_state = 0;
103
104int rd_offset = 0;
105int rd_size = 0;
106
xf.liaa4d92f2023-09-13 00:18:58 -0700107int rootfs_flag = 0;
108int m0_flag = 0;
109int zsp_flag = 0;
lh9ed821d2023-04-07 01:36:19 -0700110/* ================================================================================
111 * page_align : Ò³¶ÔÆë
112 */
113static uint32_t page_align(uint32_t offset)
114{
115 struct flash_ops *flash = NULL;
116 uint32_t page_size = 0;
117
118 flash = get_flash_ops();
119 page_size = flash->page_size;
120 if( offset & (page_size - 1) )
121 {
122 offset &= (~(page_size - 1));
123 offset += page_size;
124 }
125 return offset;
126}
127
128/* ================================================================================
129 * page_align : Ò³¶ÔÆë
130 */
131int set_entry_point(uchar * part_name, uint32_t entry_point)
132{
133 if ( strcmp( (char *)ARM_PS_IMAGE, (char *)part_name ) == 0 )
134 {
135 arm_ps_ep = entry_point;
136 return 0;
137 }
138 else if ( strcmp( (char *)ARM_PHY_IMAGE, (char *)part_name ) == 0 )
139 {
140 arm_phy_ep = entry_point;
141 return 0;
142 }
143 else if ( strcmp( (char *)ARM_RAMDISK_IMAGE, (char *)part_name ) == 0 )
144 {
145 return 0;
146 }
147 else
148 {
149 return 1;
150 }
151}
152
153static int arm_image_crc_calc(char* pcPartName,
154 uint32_t uiCRCChkSum,
155 uint32_t uiEntryPoint,
156 uint32_t uiImgSize)
157{
158 uint32_t uiCRCCalRes = 0;
159
160 BOOT_PRINTF(UBOOT_NOTICE, "(%s)CRC Calculate start\n",pcPartName);
161 uiCRCCalRes = crc32(0, (unsigned char*)uiEntryPoint, uiImgSize);
162 BOOT_PRINTF(UBOOT_NOTICE, "(%s) CRC Calculate Res=0x%0x, Size=%d Bytes\n",
163 pcPartName, uiCRCCalRes, uiImgSize);
164 if(uiCRCChkSum != uiCRCCalRes||uiCRCCalRes == 0)
165 {
166 BOOT_PRINTF(UBOOT_ERR, "(%s) CRC Check Failed!\n", pcPartName);
167 return 1;
168 }
169 BOOT_PRINTF(UBOOT_NOTICE, "(%s) CRC Check PASS!\n", pcPartName);
170 return 0;
171}
172
173/* ================================================================================
174 * reform_zsp_image : ´ÓDDRÖжÁÈ¡ZSPÏà¹ØµÄ°æ±¾
175 * @return 0 : ³É¹¦
176 * @return 1 : ʧ°Ü
177 */
178static int reform_zsp_image(uint32_t addr)
179{
180 int ret = 0;
181 int offSet = 0;
182 int lenToRead = 0;
183 int length = 0;
184
185 offSet = addr; /*DDR address for zsp,0x25000000*/
186 uint32_t arm_size = ((reg16(offSet))+(reg16(offSet+2)<<16));/*zsp bin length*/
187
188 /*move zsp's bin from zsp buf to ddr address*/
189 offSet = offSet + 0x4;
190
191 while(1)
192 {
193 if(length >= arm_size - 0x4)
194 {
195 break;
196 }
197 if((reg16(offSet+0x4)+(reg16(offSet+0x4+2)<<16))*2 <0x20000000)//not in ddr
198 {
199 lenToRead = (reg16(offSet+0x8)+(reg16(offSet+0x8+2)<<16))*2;
200 length += lenToRead + 0xc;
201 offSet = offSet + lenToRead+0xc;
202 continue;
203 }
204
205 lenToRead = (reg16(offSet+0x8)+(reg16(offSet+0x8+2)<<16))*2;
206 memcpy((void*)((reg16(offSet+0x4)+(reg16(offSet+0x4+2)<<16))*2),(const void *)(offSet+0xc),lenToRead);
207 length += lenToRead + 0xc;
208 offSet = offSet + lenToRead+0xc;
209 }
210 return 0;
211
212}
213
214
215/* ================================================================================
216 * load_arm_image : ´ÓFLASHÖжÁÈ¡ARM°æ±¾
217 * @return 0 : ³É¹¦
218 * @return 1 : ʧ°Ü
219 */
220int load_arm_image( uchar * part_name )
221{
222 int ret = 0;
223 struct flash_ops *flash = NULL;
224
225#if TIME_DEBUG
226 ulong start_time = 0;
227#endif
228
229 flush_dcache_all();
230 flash_dmabuf_disable_flag = 1;
231 flash = get_flash_ops();
232 time_debug_reset(start_time);
233 load_debug_printf("\n");
234
235 /* ѰÕÒ·ÖÇø */
236 partition_entry_t * entry = find_partition_para(part_name);
237 if( entry == NULL )
238 {
239 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
240 return 1;
241 }
242
243 /* »ñµÃ·ÖÇøÊ×µØÖ· */
244 uint32_t part_offset = entry->part_offset;
245 nand_info_t *nand = &nand_info[nand_curr_device];
246
247 /* ¶ÁÈ¡°æ±¾ËùÔÚµØÖ·µÄµÚÒ»Ò³Êý¾Ý */
248 uint32_t page_size = flash->page_size; /*¶ÁÈ¡arm °æ±¾mmcµÚÒ»¿éÊý¾Ý*/
249 ret = flash->read(nand,(loff_t)part_offset,&page_size,(u_char *)CONFIG_SYS_SDRAM_TEMP_BASE);
250 if( ret != 0 )
251 {
252 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the first page error!\n", part_name);
253 return 1;
254 }
255
256 /* »ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
257 image_header_t *header = (image_header_t *)CONFIG_SYS_SDRAM_TEMP_BASE;
258 if( ___htonl(header->ih_magic) != IH_MAGIC )
259 {
260 BOOT_PRINTF(UBOOT_ERR, "[%s]: NO bin !!!\n", part_name );
261 return 1;
262 }
263
264 uint32_t arm_size = ___htonl(header->ih_size);
265 uint32_t entey_point = ___htonl(header->ih_ep);
266 if( set_entry_point(part_name, entey_point) )
267 {
268 BOOT_PRINTF(UBOOT_ERR, "[%s][set_entry_point]: error!\n", part_name );
269 }
270
271 BOOT_PRINTF(UBOOT_NOTICE, "[%s] [size=0x%0x] from [0x%0x] to [0x%0x]\n",
272 part_name, arm_size, part_offset, entey_point);
273
274#if LOAD_IMAGE_CRC
275 uint32_t crc_bin = ___htonl(header->ih_dcrc);
276 BOOT_PRINTF(UBOOT_NOTICE, "[%s][crc_bin] ------------------- [0x%0x]\n", part_name, crc_bin);
277#endif
278
279 /* Õû¸öÍ·ºÍ·ÖÇø±íµÄ´óС */
280 uint32_t image_header_size = sizeof(image_header_t);
281 uint32_t arm_is_read_size = flash->page_size - image_header_size;
282 uint32_t arm_size_left = arm_size - arm_is_read_size; /* »¹Ã»ÓжÁÈ¡µÄ³¤¶È */
283 arm_size_left = page_align(arm_size_left); /* Ò³¶ÔÆë */
284
285 /* ¸´ÖƵÚÒ»Ò³ÖеÄÊý¾Ý */
286 memcpy((uchar *)entey_point, (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + image_header_size), arm_is_read_size);
287 /* ¶ÁȡʣÓàµÄÊý¾Ý */
288
289 ret = flash->read(nand,(loff_t)(part_offset + page_size), &arm_size_left,(u_char *)(entey_point + arm_is_read_size));
290 if( ret != 0 )
291 {
292 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the others page error...\n", part_name);
293 return 1;
294 }
295
296 BOOT_PRINTF(UBOOT_NOTICE, "[loading...] -------------------- takes [%ld] us\n", start_time);
297
298#if LOAD_IMAGE_CRC
299 time_debug_reset(start_time);
300 uint32_t crc_cal = crc32(0,(unsigned char*)entey_point, arm_size);
301 BOOT_PRINTF(UBOOT_NOTICE, "[%s][crc_bin] ------------------- [0x%0x]\n", part_name, crc_cal);
302 if( crc_bin != crc_cal )
303 {
304 BOOT_PRINTF(UBOOT_ERR, "[%s] ---------------- crc error...\n", part_name);
305 return 1;
306 }
307 BOOT_PRINTF(UBOOT_NOTICE, "[crc...] ------------------------ takes [%ld] us\n", start_time);
308#endif /* LOAD_IMAGE_CRC */
309
310 flash_dmabuf_disable_flag = 0;
311
312 return 0;
313}
314
xf.li6c8fc1e2023-08-12 00:11:09 -0700315#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700316int fs_load_dtb_image(void)
317{
318 char cmd[64] = {0};
319 /*1¡¢½«dtbÎļþloadµ½ÁÙʱµØÖ·*/
xf.li6c8fc1e2023-08-12 00:11:09 -0700320#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700321 if(imagefs_flag == 1)
322 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, DTB_IMAGE_PATH);
323 else
324 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, DTB_IMAGE_PATH);
325#else
326 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, DTB_IMAGE_PATH);
327#endif
328 run_command(cmd, 0);
329 flush_dcache_all();
330 /*2¡¢¿½±´°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
xf.libdd93d52023-05-12 07:10:14 -0700331 memcpy((uchar *)DDR_BASE_CAP_DTB_ADDR,
lh9ed821d2023-04-07 01:36:19 -0700332 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE),
333 CAP_DTB_LEN);
xf.liaa4d92f2023-09-13 00:18:58 -0700334
335 BOOT_PRINTF(UBOOT_NOTICE, "dtb load image finished.\n");
lh9ed821d2023-04-07 01:36:19 -0700336
337 return 0;
338}
xf.libdd93d52023-05-12 07:10:14 -0700339#endif
lh9ed821d2023-04-07 01:36:19 -0700340
341int fs_load_m0_image(void)
342{
343 char cmd[64] = {0};
344 int remap = 0;
xf.liaa4d92f2023-09-13 00:18:58 -0700345 int ret = 0;
346 u8 ucRet = 0;
lh9ed821d2023-04-07 01:36:19 -0700347 remap = readl(0x140000);
348 remap |= 0x800000;
349 writel(remap,0x140000);
350
351 /*1¡¢½«m0 imgÎļþloadµ½ÁÙʱµØÖ·*/
352//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700353#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700354 if(imagefs_flag == 1)
355 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
356 else
357 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
358#else
359 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
360#endif
xf.liaa4d92f2023-09-13 00:18:58 -0700361 ret = run_command(cmd, 0);
362 if(ret < 0)
363 return ret;
lh9ed821d2023-04-07 01:36:19 -0700364 flush_dcache_all();
xf.liaa4d92f2023-09-13 00:18:58 -0700365
366#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
367 m0_flag = 1;
368 if(g_nor_flag == 1)
369 {
370 if(guiOtpStatus == 0) //Secure Verify.
371 {
372 BOOT_PRINTF(UBOOT_DBG, "rpm image Start Secure Verify...\n");
373
374 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
375 if(ucRet != 0)
376 {
377 BOOT_PRINTF(UBOOT_ERR, "rpm image Secure Verify FAILED!\n");
378 return 1;
379 }
380 BOOT_PRINTF(UBOOT_NOTICE, "rpm image Secure Verify PASS!\n");
381 }
382 else
383 {
384 BOOT_PRINTF(UBOOT_NOTICE, "rpm image Skip Secure Verify...\n");
385 }
386 }
387 else
388 {
389 if(guiEfuseStatus == 0) //Secure Verify.
390 {
391 BOOT_PRINTF(UBOOT_DBG, "rpm image Start Secure Verify...\n");
392
393 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
394 if(ucRet != 0)
395 {
396 BOOT_PRINTF(UBOOT_ERR, "rpm image Secure Verify FAILED!\n");
397 return 1;
398 }
399 BOOT_PRINTF(UBOOT_NOTICE, "rpm image Secure Verify PASS!\n");
400 }
401 else
402 {
403 BOOT_PRINTF(UBOOT_NOTICE, "rpm image Skip Secure Verify...\n");
404 }
405 }
406 m0_flag = 0;
407#endif
408
lh9ed821d2023-04-07 01:36:19 -0700409 /*2¡¢ÉèÖÃM0µÄÈë¿ÚµØÖ·ÒÔ¼°M0°æ±¾°áÔËÍê³Éflagµ½iramÖÐ */
410 writel(1, M0_IMAGE_READY_FLAG_ADDR);
411
412 /*3¡¢µÈ´ýM0¿½±´Íê³É*/
413 while(readl(M0_IMAGE_READY_FLAG_ADDR));
414
415 printf("M0 image load success!\n");
416
417 return 0;
418}
419
xf.liaa4d92f2023-09-13 00:18:58 -0700420int load_rootfs(void)
421{
422 int ret = 0;
423 int type = 0;
424 struct flash_ops *flash = NULL;
425 uint32_t i = 0;
426 uint32_t bad_nums = 0;
427 uint32_t part_block_nums = 0;
428 uint32_t part_offset = 0;
429 uint32_t part_size = 0;
430 uchar * part_name = "rootfs";;
431 nand_info_t *nand = &nand_info[nand_curr_device];
432 flush_dcache_all();
433
434 flash_dmabuf_disable_flag = 1;
435 flash = get_flash_ops();
436 type = read_boot_flashtype();
437
438#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
439
440 if(imagefs_flag == 1)
441 {
442 part_name = "rootfs";
443 }
444 else
445 {
446 part_name = "rootfs2";
447 }
448
449#endif
450 /* ѰÕÒ·ÖÇø */
451 partition_entry_t * entry = find_partition_para(part_name);
452 if( entry == NULL )
453 {
454 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
455 return 1;
456 }
457
458 /* »ñµÃ·ÖÇøÊ×µØÖ· */
459 part_offset = entry->part_offset;
460 part_size = entry->part_size;
461
462 /* ²éѯµ±Ç°·ÖÇø»µ¿éÊý*/
463 bad_nums = 0;
464 part_block_nums = entry->part_size / nand->erasesize;
465 for(i = 0; i < part_block_nums; i++)
466 {
467 if(nand_block_isbad (nand, entry->part_offset + (loff_t)i * nand->erasesize))
468 {
469 printf("bad block addr = 0x%x\n", (entry->part_offset + i * nand->erasesize));
470 bad_nums++;
471 }
472 }
473
474 part_size = part_size - bad_nums * nand->erasesize;
475 ret = flash->read(nand,(loff_t)part_offset,
476 &part_size,(u_char *)CONFIG_SYS_SDRAM_ROOTFS_BASE);
477 if( ret != 0 )
478 {
479 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the rootfs error!\n", part_name);
480 return 1;
481 }
482 memcpy((uchar *)(CONFIG_SYS_SDRAM_ROOTFS_BASE - sizeof(image_header_t)),
483 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + sizeof(sImageNewHeader) + sizeof(sImageNewHeader)),
484 sizeof(image_header_t));
485 flash_dmabuf_disable_flag = 0;
486 flush_dcache_all();
487
488 return 0;
489}
lh9ed821d2023-04-07 01:36:19 -0700490
491#if defined(CONFIG_ZX297520V3E_JFFS2_COMPRESS)
492uint32_t lzma_header(uint32_t *ztelzma_dict,
493 int *ztelzma_lc,
494 int *ztelzma_lp,
495 int *ztelzma_pb)
496{
497 uint8_t temp_properties = 0;
498 uint32_t temp_dict = 0;
499 uint32_t temp_uncompress_size = 0;
500 uint32_t lzma_uncompresssize = 0;
501 lzma_header_t *lzmaheader;
502
503 lzmaheader = (lzma_header_t *)CONFIG_SYS_SDRAM_TEMP_BASE;
504
505 temp_properties = lzmaheader->p_properties;
506 temp_dict = (lzmaheader->p_dict[3]<<24)
507 |(lzmaheader->p_dict[2]<<16)
508 |(lzmaheader->p_dict[1]<<8)
509 |lzmaheader->p_dict[0];
510 temp_uncompress_size = (lzmaheader->p_uncompress_size[3]<<24)
511 |(lzmaheader->p_uncompress_size[2]<<16)
512 |(lzmaheader->p_uncompress_size[1]<<8)
513 |lzmaheader->p_uncompress_size[0];
514
515 *ztelzma_pb = temp_properties / (9 * 5);
516 temp_properties -= *ztelzma_pb * 9 * 5;
517 *ztelzma_lp = temp_properties / 9 ;
518 *ztelzma_lc = temp_properties - *ztelzma_lp * 9;
519 *ztelzma_dict = temp_dict;
520 lzma_uncompresssize = temp_uncompress_size;
521
522 return lzma_uncompresssize;
523}
524
525int fs_load_zsp_image(void)
526{
527 uint32_t image_tmp_buf = 0;
528 int zspimagenum=0;
529 uint32_t ztelzma_uncompresssize = 0;
530 uint32_t ztelzma_dict=0;
531 int ztelzma_lc=0;
532 int ztelzma_lp=0;
533 int ztelzma_pb=0;
534 int firstlzma_flag = 0;
535 int lzma_init_flag = 0;
536 int lzmainit_ret = 0;
537 int lzmadecompr_ret = 1;
538 uint32_t lzmaoffsize = 0;
539 char cmd[64] = {0};
540 uint32_t uiImgHdrlzma = 13;
541
542 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load begin...\n");
543
544 lzmanodeflag = 1;
545
546 /* ÁÙʱ´æ·Å½âѹºóµÄzsp°æ±¾ */
547 image_tmp_buf = CONFIG_SYS_SDRAM_TEMP_LZMA;
548
549 while(lzmanodeflag == 1)
550 {
551 /* ½«zsp imgÎļþloadµ½ÁÙʱµØÖ·*/
552//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700553#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700554 if(imagefs_flag == 1)
555 sprintf(cmd, "fsload imagefs 0x%x cpuphy_%02d.lzma",
556 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
557 zspimagenum);
558 else
559 sprintf(cmd, "fsload imagefs2 0x%x cpuphy_%02d.lzma",
560 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
561 zspimagenum);
562#else
563 sprintf(cmd, "fsload imagefs 0x%x cpuphy_%02d.lzma",
564 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
565 zspimagenum);
566#endif
567 run_command(cmd, 0);
568
569 if(ztelzma_compresssize)
570 {
571 /* ½âѹС°üÎļþ*/
572 ztelzma_uncompresssize=lzma_header(&ztelzma_dict,
573 &ztelzma_lc,
574 &ztelzma_lp,
575 &ztelzma_pb);
576
577 if(lzma_init_flag == 0)
578 {
579 lzmainit_ret = lzma_init(&ztelzma_dict,
580 &ztelzma_lc,
581 &ztelzma_lp,
582 &ztelzma_pb);
583 if(lzmainit_ret < 0)
584 {
585 printf("lzma_init failed\n");
586 return -1;
587 }
588 else
589 {
590 lzma_init_flag = 1;
591 }
592 }
593
594 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrlzma),
595 (unsigned char *)(image_tmp_buf + lzmaoffsize),
596 ztelzma_compresssize,
597 ztelzma_uncompresssize);
598 if(lzmadecompr_ret)
599 {
600 printf("lzma_decompress failed\n");
601 return -1;
602 }
603 lzmaoffsize += ztelzma_uncompresssize ;
604 zspimagenum++;
605 }
606 }
607
608 /* °´ÕÕZSPµÄ¸ñÊ½ÖØÅÅ */
xf.liaa4d92f2023-09-13 00:18:58 -0700609 reform_zsp_image(image_tmp_buf + sizeof(sImageNewHeader) + sizeof(image_header_t));
lh9ed821d2023-04-07 01:36:19 -0700610 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load finished.\n");
611
612 return 0;
613}
614
615int fs_load_arm_image_linux(char* image_name)
616{
617 char cmd[64] = {0};
618 int ret = 0;
619 int lzmainit_ret = 0;
620 int lzmadecompr_ret = 1;
621 u8 ucRet = 0;
622 int apimagenum=0;
623 uint32_t uiImgSize = 0;
624 uint32_t uiEntryPoint = 0;
625 uint32_t uiLoadPoint = 0;
626 uint32_t lzmaLoadPoint = 0;
627 uint32_t ztelzma_uncompresssize = 0;
628 uint32_t ztelzma_dict=0;
629 int ztelzma_lc=0;
630 int ztelzma_lp=0;
631 int ztelzma_pb=0;
632 int firstlzma_flag = 0;
633#if LOAD_IMAGE_CRC
634 uint32_t uiCRCChkSum = 0;
635 uint32_t uiCRCCalRes = 0;
636#endif
637 uint32_t uiImgHdrSizeOld = sizeof(image_header_t);
638 uint32_t uiImgHdrSizeNew = 0;
xf.liaa4d92f2023-09-13 00:18:58 -0700639 uint32_t uiRootfsHdrSizeOld = sizeof(image_header_t);
640 uint32_t uiRootfsHdrSizeNew = sizeof(sImageNewHeader);
lh9ed821d2023-04-07 01:36:19 -0700641 uint32_t uiImgHdrlzma = 13;
642 uint32_t lastlzmaSize = 0;
643
644 int lzma_init_flag = 0;
645 lzmanodeflag = 1;
646
647 BOOT_PRINTF(UBOOT_NOTICE, "AP image load begin...\n");
648
xf.liaa4d92f2023-09-13 00:18:58 -0700649 uiImgHdrSizeNew = sizeof(sImageNewHeader);
lh9ed821d2023-04-07 01:36:19 -0700650
651 image_header_t *psImgHdrOld = NULL;
652
653 while(lzmanodeflag == 1)
654 {
655 /*1¡¢½«ap imgÎļþloadµ½ÁÙʱµØÖ·*/
656//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700657#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700658 if(imagefs_flag == 1)
659 sprintf(cmd, "fsload imagefs 0x%x /%s_%02d.lzma",
660 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
661 else
662 sprintf(cmd, "fsload imagefs2 0x%x /%s_%02d.lzma",
663 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
664#else
665 sprintf(cmd, "fsload imagefs 0x%x /%s_%02d.lzma",
666#endif
667 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
668
669 run_command(cmd, 0);
670
671 if(ztelzma_compresssize)
672 {
673 /*2¡¢½âѹС°üÎļþ*/
674 ztelzma_uncompresssize=lzma_header(&ztelzma_dict,
675 &ztelzma_lc,
676 &ztelzma_lp,
677 &ztelzma_pb);
678
679 if(lzma_init_flag == 0)
680 {
681 lzmainit_ret = lzma_init(&ztelzma_dict,
682 &ztelzma_lc,
683 &ztelzma_lp,
684 &ztelzma_pb);
685 if(lzmainit_ret < 0)
686 {
687 printf("lzma_init failed\n");
688 return -1;
689 }
690 else
691 {
692 lzma_init_flag = 1;
693 }
694 }
695
696 /*3¡¢»ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
697 if(firstlzma_flag == 0)
698 {
699 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE+uiImgHdrlzma),
700 (unsigned char *)(CONFIG_SYS_SDRAM_TEMP_LZMA),
701 ztelzma_compresssize,
702 ztelzma_uncompresssize);
703 if(lzmadecompr_ret)
704 {
705 printf("lzma_decompress failed\n");
706 return -1;
707 }
708
xf.liaa4d92f2023-09-13 00:18:58 -0700709 /*»ñÈ¡¹«Ô¿´«µÝ¸ø´ó°æ±¾Ê¹ÓÃ*/
710 if(reg16(CONFIG_SYS_SDRAM_TEMP_LZMA)== RSA_1024)
711 {
712 memset(CFG_SECURE_PUK_ADDR,0,256);
713 memcpy(CFG_SECURE_PUK_ADDR+124,CONFIG_SYS_SDRAM_TEMP_BASE+12,132);
714 }else{
715 printf("signtype is %s\n", reg16(CONFIG_SYS_SDRAM_TEMP_BASE)== RSA_2048 ? "RSA2048":"UNKNOWN");
716 }
lh9ed821d2023-04-07 01:36:19 -0700717
xf.liaa4d92f2023-09-13 00:18:58 -0700718 psImgHdrOld = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_LZMA + uiImgHdrSizeNew + uiRootfsHdrSizeNew + uiRootfsHdrSizeOld);
lh9ed821d2023-04-07 01:36:19 -0700719
720 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
721 {
722 BOOT_PRINTF(UBOOT_ERR, "Magic Num Check Failed,Maybe no AP bin !!!\n");
723 return 1;
xf.liaa4d92f2023-09-13 00:18:58 -0700724 }
725
lh9ed821d2023-04-07 01:36:19 -0700726 uiImgSize = ___htonl(psImgHdrOld->ih_size);
727 uiEntryPoint = ___htonl(psImgHdrOld->ih_ep);
xf.liaa4d92f2023-09-13 00:18:58 -0700728 uiLoadPoint = ___htonl(psImgHdrOld->ih_load) - uiImgHdrSizeOld - uiRootfsHdrSizeOld - uiRootfsHdrSizeNew; /* ÕâÀïʹÓÃLOADµØÖ· */
729
lh9ed821d2023-04-07 01:36:19 -0700730 lzmaLoadPoint = uiLoadPoint;
731 BOOT_PRINTF(UBOOT_NOTICE, "Load AP image, Size=0x%0x, to 0x%0x.\n",
732 uiImgSize, uiLoadPoint);
733
734#if LOAD_IMAGE_CRC
735 uiCRCChkSum = ___htonl(psImgHdrOld->ih_dcrc);
736 BOOT_PRINTF(UBOOT_NOTICE, "AP image CRC Checksum=0x%0x.\n", uiCRCChkSum);
737#endif
738 /*4¡¢¿½±´µÚÒ»°ü°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
739 memcpy((uchar *)(uiLoadPoint - uiImgHdrSizeNew),
740 (uchar *)(CONFIG_SYS_SDRAM_TEMP_LZMA),
741 ztelzma_uncompresssize);
742 firstlzma_flag = 1;
743 lastlzmaSize = ztelzma_uncompresssize - uiImgHdrSizeNew;
744 }
745 else
746 {
747 lzmaLoadPoint = lzmaLoadPoint + lastlzmaSize ;
748 lastlzmaSize = ztelzma_uncompresssize;
749 /*5¡¢¿½±´Ê£Óà°ü°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
750
751 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE+uiImgHdrlzma),
752 (unsigned char *)(lzmaLoadPoint),
753 ztelzma_compresssize, lastlzmaSize);
754 if(lzmadecompr_ret)
755 {
756 printf("lzma_decompress failed\n");
757 return -1;
758 }
759 }
760 apimagenum++;
761 }
762 }
763
764 BOOT_PRINTF(UBOOT_NOTICE, "AP image load finished\n");
765
766 if(guiEfuseStatus == 0) //Secure Verify.
767 {
768 BOOT_PRINTF(UBOOT_DBG, "AP image Start Secure Verify...\n");
769
770 ucRet = secure_verify((u32 )uiLoadPoint - uiImgHdrSizeNew);
771 if(ucRet != 0)
772 {
773 BOOT_PRINTF(UBOOT_ERR, "AP image Secure Verify FAILED!\n");
774 return 1;
775 }
776 BOOT_PRINTF(UBOOT_NOTICE, "AP image Secure Verify PASS!\n");
777 }
778 else
779 {
780 BOOT_PRINTF(UBOOT_NOTICE, "AP image Skip Secure Verify...\n");
781 }
782
783 if(strncmp((const char *)image_name, "cpucap", 6) == 0)
784 {
785 arm_cpucap_ep = uiEntryPoint; /* usually */
786 }
787 else
788 {
789 sys_ddr_kernel_start = uiEntryPoint - 0x8000; /* usually */
790 gd->bd->bi_boot_params = sys_ddr_kernel_start + 0x100;
xf.liaa4d92f2023-09-13 00:18:58 -0700791 uiLoadPoint += (uiRootfsHdrSizeOld + uiRootfsHdrSizeNew);
lh9ed821d2023-04-07 01:36:19 -0700792 sprintf((char *)cmd," bootm 0x%0x", uiLoadPoint);
793 setenv("bootcmd", (char *)cmd);
794 }
795
796#if LOAD_IMAGE_CRC
797 //invalidate_dcache_range(uiEntryPoint, uiEntryPoint + uiImgSize);
798 ret = arm_image_crc_calc(ARM_APP_IMAGE, uiCRCChkSum, uiEntryPoint, uiImgSize);
799 if(ret != 0)
800 {
801 BOOT_PRINTF(UBOOT_ERR, "ap image crc calc failed, ret = %d! \n", ret);
802 return -1;
803 }
804#endif
805
806 return 0;
807
808}
809#else
810
811int fs_load_zsp_image(void)
812{
813 char cmd[64] = {0};
814 uint32_t image_tmp_buf = 0;
xf.liaa4d92f2023-09-13 00:18:58 -0700815 int ret = 0;
816 u8 ucRet = 0;
lh9ed821d2023-04-07 01:36:19 -0700817 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load begin...\n");
818 image_tmp_buf = CONFIG_SYS_SDRAM_TEMP_BASE;
819//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700820#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700821 if(imagefs_flag == 1)
822 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
823 else
824 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
825#else
826 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
827#endif
xf.liaa4d92f2023-09-13 00:18:58 -0700828 ret = run_command(cmd, 0);
829 if(ret < 0)
830 return ret;
831
832#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
833 zsp_flag = 1;
834 if(g_nor_flag == 1)
835 {
836 if(guiOtpStatus == 0) //Secure Verify.
837 {
838 BOOT_PRINTF(UBOOT_DBG, "zsp image Start Secure Verify...\n");
839
840 ucRet = secure_verify((u32 )image_tmp_buf);
841 if(ucRet != 0)
842 {
843 BOOT_PRINTF(UBOOT_ERR, "zsp image Secure Verify FAILED!\n");
844 return 1;
845 }
846 BOOT_PRINTF(UBOOT_NOTICE, "zsp image Secure Verify PASS!\n");
847 }
848 else
849 {
850 BOOT_PRINTF(UBOOT_NOTICE, "zsp image Skip Secure Verify...\n");
851 }
852 }
853 else
854 {
855 if(guiEfuseStatus == 0) //Secure Verify.
856 {
857 BOOT_PRINTF(UBOOT_DBG, "zsp image Start Secure Verify...\n");
858
859 ucRet = secure_verify((u32 )image_tmp_buf);
860 if(ucRet != 0)
861 {
862 BOOT_PRINTF(UBOOT_ERR, "zsp image Secure Verify FAILED!\n");
863 return 1;
864 }
865 BOOT_PRINTF(UBOOT_NOTICE, "zsp image Secure Verify PASS!\n");
866 }
867 else
868 {
869 BOOT_PRINTF(UBOOT_NOTICE, "zsp image Skip Secure Verify...\n");
870 }
871 }
872 zsp_flag = 0;
lh9ed821d2023-04-07 01:36:19 -0700873
xf.liaa4d92f2023-09-13 00:18:58 -0700874#endif
875
876 reform_zsp_image(image_tmp_buf + sizeof(sImageNewHeader) + sizeof(image_header_t));
877
lh9ed821d2023-04-07 01:36:19 -0700878 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load finished.\n");
879
880 return 0;
881}
882
883int fs_load_arm_image_linux(char* image_name )
884{
885 char cmd[64] = {0};
886 int ret = 0;
887 u8 ucRet = 0;
888
889 uint32_t uiImgSize = 0;
890 uint32_t uiEntryPoint = 0;
891 uint32_t uiLoadPoint = 0;
892#if LOAD_IMAGE_CRC
893 uint32_t uiCRCChkSum = 0;
894 uint32_t uiCRCCalRes = 0;
895#endif
896 uint32_t uiImgHdrSizeOld = sizeof(image_header_t);
xf.liaa4d92f2023-09-13 00:18:58 -0700897 uint32_t uiRootfsHdrSizeOld = sizeof(image_header_t);
lh9ed821d2023-04-07 01:36:19 -0700898
xf.liaa4d92f2023-09-13 00:18:58 -0700899 uint32_t uiImgHdrSizeNew = sizeof(sImageNewHeader);
900 uint32_t uiRootfsHdrSizeNew = sizeof(sImageNewHeader);
lh9ed821d2023-04-07 01:36:19 -0700901
902 image_header_t *psImgHdrOld = NULL;
xf.liaa4d92f2023-09-13 00:18:58 -0700903 image_header_t *psRootfsHdrOld = NULL;
lh9ed821d2023-04-07 01:36:19 -0700904
905 /*1¡¢½«ap imgÎļþloadµ½ÁÙʱµØÖ·*/
906//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700907#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700908 if(imagefs_flag == 1)
909 sprintf(cmd, "fsload imagefs 0x%x /ap_%s.bin",
910 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
911 else
912 sprintf(cmd, "fsload imagefs2 0x%x /ap_%s.bin",
913 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
914#else
915 sprintf(cmd, "fsload imagefs 0x%x /ap_%s.bin",
916 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
917#endif
xf.liaa4d92f2023-09-13 00:18:58 -0700918 ret = run_command(cmd, 0);
919 if(ret < 0)
920 return ret;
lh9ed821d2023-04-07 01:36:19 -0700921
xf.liaa4d92f2023-09-13 00:18:58 -0700922 if(strncmp((const char *)image_name, "cpuap", 5) == 0)
lh9ed821d2023-04-07 01:36:19 -0700923 {
xf.liaa4d92f2023-09-13 00:18:58 -0700924 rootfs_flag = 1;
lh9ed821d2023-04-07 01:36:19 -0700925
xf.liaa4d92f2023-09-13 00:18:58 -0700926 if(g_nor_flag == 1)
lh9ed821d2023-04-07 01:36:19 -0700927 {
xf.liaa4d92f2023-09-13 00:18:58 -0700928 /*»ñÈ¡¹«Ô¿´«µÝ¸ø´ó°æ±¾Ê¹ÓÃ*/
929 memcpy(OTP_SECURE_PUK_BASE,(uchar *)CONFIG_SYS_SDRAM_TEMP_BASE+4,380);
930 //printf("OTP_SECURE_PUK_BASE is 0x%x\n",OTP_SECURE_PUK_BASE);
931
932 /*°²È«Ð£Ñérootfs*/
933 if(guiOtpStatus == 0) //Secure Verify.
934 {
935 BOOT_PRINTF(UBOOT_DBG, "AP rootfs Start Secure Verify...\n");
936
937 /*¼ÓÔØrootfs¾µÏñ*/
938 ret = load_rootfs();
939 if(ret != 0)
940 {
941 printf("rootfs load error.\n");
942 return 1;
943 }
944
945 ucRet = rootfs_secure_verify((u32 )(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew));
946 if(ucRet != 0)
947 {
948 BOOT_PRINTF(UBOOT_ERR, "AP rootfs Secure Verify FAILED!\n");
949 return 1;
950 }
951 BOOT_PRINTF(UBOOT_NOTICE, "AP rootfs Secure Verify PASS!\n");
952 }
953 else
954 {
955 BOOT_PRINTF(UBOOT_NOTICE, "AP rootfs Skip Secure Verify...\n");
956 }
957 }
958 else
959 {
960 /*»ñÈ¡¹«Ô¿´«µÝ¸ø´ó°æ±¾Ê¹ÓÃ*/
961 if(reg16(CONFIG_SYS_SDRAM_TEMP_BASE)== RSA_1024)
962 {
963 memset(CFG_SECURE_PUK_ADDR,0,256);
964 memcpy(CFG_SECURE_PUK_ADDR+124,CONFIG_SYS_SDRAM_TEMP_BASE+12,132);
965 }else{
966 printf("signtype is %s\n", reg16(CONFIG_SYS_SDRAM_TEMP_BASE)== RSA_2048 ? "RSA2048":"UNKNOWN");
967 //printf("pub key is illegal...\n");
968 }
969 /*°²È«Ð£Ñérootfs*/
970 if(guiEfuseStatus == 0) //Secure Verify.
971 {
972 BOOT_PRINTF(UBOOT_DBG, "AP rootfs Start Secure Verify...\n");
973
974 /*¼ÓÔØrootfs¾µÏñ*/
975 ret = load_rootfs();
976 if(ret != 0)
977 {
978 printf("rootfs load error.\n");
979 return 1;
980 }
981
982 ucRet = rootfs_secure_verify((u32 )(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew));
983 if(ucRet != 0)
984 {
985 BOOT_PRINTF(UBOOT_ERR, "AP rootfs Secure Verify FAILED!\n");
986 return 1;
987 }
988 BOOT_PRINTF(UBOOT_NOTICE, "AP rootfs Secure Verify PASS!\n");
989 }
990 else
991 {
992 BOOT_PRINTF(UBOOT_NOTICE, "AP rootfs Skip Secure Verify...\n");
993 }
994
995 }
996
997
998 /*2¡¢»ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
999 psImgHdrOld = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew + uiRootfsHdrSizeNew + uiRootfsHdrSizeOld);
1000 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
1001 {
1002 BOOT_PRINTF(UBOOT_ERR, "Magic Num Check Failed,Maybe no AP bin !!!\n");
lh9ed821d2023-04-07 01:36:19 -07001003 return 1;
1004 }
xf.liaa4d92f2023-09-13 00:18:58 -07001005
1006 uiImgSize = ___htonl(psImgHdrOld->ih_size);
1007 uiEntryPoint = ___htonl(psImgHdrOld->ih_ep);
1008 uiLoadPoint = ___htonl(psImgHdrOld->ih_load) - uiImgHdrSizeOld - uiRootfsHdrSizeOld - uiRootfsHdrSizeNew; /* ÕâÀïʹÓÃLOADµØÖ· */
1009
1010 BOOT_PRINTF(UBOOT_NOTICE, "Load AP image, Size=0x%0x, to 0x%0x.\n",
1011 uiImgSize, uiLoadPoint);
1012
1013#if LOAD_IMAGE_CRC
1014 uiCRCChkSum = ___htonl(psImgHdrOld->ih_dcrc);
1015 BOOT_PRINTF(UBOOT_NOTICE, "AP image CRC Checksum=0x%0x.\n", uiCRCChkSum);
1016#endif
1017 BOOT_PRINTF(UBOOT_NOTICE, "AP image uiLoadPoint=0x%0x.\n", uiLoadPoint);
1018
1019 /*3¡¢¿½±´°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
1020 memcpy((uchar *)uiLoadPoint,
1021 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew),
1022 uiImgSize + uiImgHdrSizeOld + uiRootfsHdrSizeOld + uiRootfsHdrSizeNew);
1023
1024 BOOT_PRINTF(UBOOT_NOTICE, "AP image load image finished\n");
1025 }
1026 else
1027 {
1028 /*2¡¢»ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
1029 psImgHdrOld = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew);
1030 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
1031 {
1032 BOOT_PRINTF(UBOOT_ERR, "Magic Num Check Failed,Maybe no AP bin !!!\n");
1033 return 1;
1034 }
1035
1036 uiImgSize = ___htonl(psImgHdrOld->ih_size);
1037 uiEntryPoint = ___htonl(psImgHdrOld->ih_ep);
1038 uiLoadPoint = ___htonl(psImgHdrOld->ih_load) - uiImgHdrSizeOld; /* ÕâÀïʹÓÃLOADµØÖ· */
1039
1040 BOOT_PRINTF(UBOOT_NOTICE, "Load AP image, Size=0x%0x, to 0x%0x.\n",
1041 uiImgSize, uiLoadPoint);
1042
1043#if LOAD_IMAGE_CRC
1044 uiCRCChkSum = ___htonl(psImgHdrOld->ih_dcrc);
1045 BOOT_PRINTF(UBOOT_NOTICE, "AP image CRC Checksum=0x%0x.\n", uiCRCChkSum);
1046#endif
1047 BOOT_PRINTF(UBOOT_NOTICE, "AP image uiLoadPoint=0x%0x.\n", uiLoadPoint);
1048
1049 /*3¡¢¿½±´°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
1050 memcpy((uchar *)uiLoadPoint,
1051 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew),
1052 uiImgSize +uiImgHdrSizeOld);
1053
1054 BOOT_PRINTF(UBOOT_NOTICE, "AP image load image finished\n");
1055 }
1056
1057 if(g_nor_flag == 1)
1058 {
1059 if(guiOtpStatus == 0) //Secure Verify.
1060 {
1061 BOOT_PRINTF(UBOOT_DBG, "AP image Start Secure Verify...\n");
1062
1063 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
1064 if(ucRet != 0)
1065 {
1066 BOOT_PRINTF(UBOOT_ERR, "AP image Secure Verify FAILED!\n");
1067 return 1;
1068 }
1069 BOOT_PRINTF(UBOOT_NOTICE, "AP image Secure Verify PASS!\n");
1070 }
1071 else
1072 {
1073 BOOT_PRINTF(UBOOT_NOTICE, "AP image Skip Secure Verify...\n");
1074 }
lh9ed821d2023-04-07 01:36:19 -07001075 }
1076 else
1077 {
xf.liaa4d92f2023-09-13 00:18:58 -07001078 if(guiEfuseStatus == 0) //Secure Verify.
1079 {
1080 BOOT_PRINTF(UBOOT_DBG, "AP image Start Secure Verify...\n");
lh9ed821d2023-04-07 01:36:19 -07001081
xf.liaa4d92f2023-09-13 00:18:58 -07001082 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
1083 if(ucRet != 0)
1084 {
1085 BOOT_PRINTF(UBOOT_ERR, "AP image Secure Verify FAILED!\n");
1086 return 1;
1087 }
1088 BOOT_PRINTF(UBOOT_NOTICE, "AP image Secure Verify PASS!\n");
1089 }
1090 else
1091 {
1092 BOOT_PRINTF(UBOOT_NOTICE, "AP image Skip Secure Verify...\n");
1093 }
1094 }
1095
1096 if(strncmp((const char *)image_name, "cpucap", 6) == 0)
lh9ed821d2023-04-07 01:36:19 -07001097 {
1098 arm_cpucap_ep = uiEntryPoint; /* usually */
1099 printf("cpucap load finish....\n");
1100 }
1101 else
1102 {
1103 sys_ddr_kernel_start = uiEntryPoint - 0x8000; /* usually */
1104 gd->bd->bi_boot_params = sys_ddr_kernel_start + 0x100;
xf.liaa4d92f2023-09-13 00:18:58 -07001105 if(1 == rootfs_flag)
1106 uiLoadPoint += (uiRootfsHdrSizeOld + uiRootfsHdrSizeNew);
lh9ed821d2023-04-07 01:36:19 -07001107 sprintf((char *)cmd," bootm 0x%0x", uiLoadPoint);
1108 setenv("bootcmd", (char *)cmd);
1109 }
1110#if LOAD_IMAGE_CRC
1111 //invalidate_dcache_range(uiEntryPoint, uiEntryPoint + uiImgSize);
1112 ret = arm_image_crc_calc(ARM_APP_IMAGE, uiCRCChkSum, uiEntryPoint, uiImgSize);
1113 if(ret != 0)
1114 {
1115 BOOT_PRINTF(UBOOT_ERR, "ap image crc calc failed, ret = %d! \n", ret);
1116 return -1;
1117 }
1118#endif
1119
1120 return 0;
1121}
1122
1123#endif
1124
1125int load_imagefs(uchar * part_name)
1126{
1127 int ret = 0;
1128 int type = 0;
1129 struct flash_ops *flash = NULL;
1130 uint32_t i = 0;
1131 uint32_t bad_nums = 0;
1132 uint32_t part_block_nums = 0;
1133 uint32_t part_offset = 0;
1134 uint32_t part_size = 0;
1135 nand_info_t *nand = &nand_info[nand_curr_device];
1136 struct fsl_qspi *nor = &spi_nor_flash;
1137
1138 /* ´Ë´¦±ØÐëÇå0xFF£¬±£Ö¤¾µÏñÍêÕû£¬²»ÊÜÉÏ´ÎÒÅÁôÊý¾ÝÓ°Ïì*/
1139 memset(CONFIG_SYS_SDRAM_IMAGEFS_BASE, 0xFF, CONFIG_SYS_SDRAM_IMAGEFS_SIZE);
1140 flush_dcache_all();
1141
1142 flash_dmabuf_disable_flag = 1;
1143 flash = get_flash_ops();
1144 type = read_boot_flashtype();
1145
1146 /* ѰÕÒ·ÖÇø */
1147 partition_entry_t * entry = find_partition_para(part_name);
1148 if( entry == NULL )
1149 {
1150 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
1151 return 1;
1152 }
1153
1154 /* »ñµÃ·ÖÇøÊ×µØÖ· */
1155 part_offset = entry->part_offset;
1156 part_size = entry->part_size;
1157
1158 if(part_size > CONFIG_SYS_SDRAM_IMAGEFS_SIZE)
1159 {
1160 BOOT_PRINTF(UBOOT_ERR, "[%s] part size more than SDRAM IMAGEFS space !!!\n", part_name);
1161 return 1;
1162 }
1163
1164 if(type == IF_TYPE_NAND || type == IF_TYPE_SPI_NAND)
1165 {
1166 /* ²éѯµ±Ç°·ÖÇø»µ¿éÊý*/
1167 bad_nums = 0;
1168 part_block_nums = entry->part_size / nand->erasesize;
1169 for(i = 0; i < part_block_nums; i++)
1170 {
1171 if(nand_block_isbad (nand, entry->part_offset + (loff_t)i * nand->erasesize))
1172 {
1173 printf("bad block addr = 0x%x\n", (entry->part_offset + i * nand->erasesize));
1174 bad_nums++;
1175 }
1176 }
1177
1178 part_size = part_size - bad_nums * nand->erasesize;
1179 ret = flash->read(nand,(loff_t)part_offset,
1180 &part_size,(u_char *)CONFIG_SYS_SDRAM_IMAGEFS_BASE);
1181 if( ret != 0 )
1182 {
1183 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the imagefs error!\n", part_name);
1184 return 1;
1185 }
1186 }
1187 else if(type == IF_TYPE_NOR)
1188 {
1189 ret = nand_read(&(nor->nor[0].mtd), (loff_t)part_offset,
1190 &part_size, (u_char *)CONFIG_SYS_SDRAM_IMAGEFS_BASE);
1191 if(ret)
1192 {
1193 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the imagefs error!\n", part_name);
1194 return -1;
1195 }
1196 }
1197
1198 flash_dmabuf_disable_flag = 0;
1199 flush_dcache_all();
1200
1201 return 0;
1202}
1203
1204//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -07001205#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
1206int load_flags(T_FLAGS_INFO *fotaFlagInfo)
lh9ed821d2023-04-07 01:36:19 -07001207{
1208 int ret = 0;
1209 int type = 0;
1210 uchar * part_name = "flags";
1211 struct flash_ops *flash = NULL;
1212 uint32_t i = 0;
1213 uint32_t off = 0;
1214 uint32_t bad_nums = 0;
1215 uint32_t part_block_nums = 0;
1216 uint32_t part_offset = 0;
1217 uint32_t part_size = 0;
1218 uint32_t blockNum = 0;
1219 uint32_t work_area_offset = 0;
1220 uint32_t backup_area_offset = 0;
1221 uint32_t flag_one = 0;
xf.li6c8fc1e2023-08-12 00:11:09 -07001222 uint32_t fota_size = sizeof(T_FLAGS_INFO);
lh9ed821d2023-04-07 01:36:19 -07001223 nand_info_t *nand = &nand_info[nand_curr_device];
1224 struct fsl_qspi *nor = &spi_nor_flash;
1225
1226 flush_dcache_all();
1227 flash_dmabuf_disable_flag = 1;
1228 flash = get_flash_ops();
1229 type = read_boot_flashtype();
1230
1231 /* ѰÕÒ·ÖÇø */
1232 partition_entry_t * entry = find_partition_para(part_name);
1233 if( entry == NULL )
1234 {
1235 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
1236 return 1;
1237 }
1238
1239 /* »ñµÃ·ÖÇøÊ×µØÖ· */
1240 part_offset = entry->part_offset;
1241 part_size = entry->part_size;
1242 //while(flag);
1243 if(type == IF_TYPE_NAND || type == IF_TYPE_SPI_NAND)
1244 {
1245 /*È·¶¨¹¤×÷ÇøºÍ±¸·ÝÇøÆ«ÒÆµØÖ·*/
1246 for (off = part_offset; off < part_offset+part_size; off += nand->erasesize)
1247 {
1248 if (!(nand_block_isbad(nand,off)))
1249 {
1250 blockNum += 1;
1251 }
1252
1253 if((blockNum == 1) && (flag_one == 0))
1254 {
1255 work_area_offset = off;
1256 flag_one = 1;
1257 }
1258
1259 else if(blockNum == 2)
1260 {
1261 backup_area_offset = off;
1262 break;
1263 }
1264 }
1265
1266 if(blockNum < 2)
1267 {
1268 printf("flags partition have not enough space!\n");
1269
1270 return -1;
1271 }
1272
1273 ret = flash->read(nand,(loff_t)work_area_offset,
1274 &fota_size,(u_char *)(fotaFlagInfo));
1275 if( ret != 0 )
1276 {
1277 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the flags error!\n", part_name);
1278 return 1;
1279 }
1280
xf.li6c8fc1e2023-08-12 00:11:09 -07001281 if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
xf.libdd93d52023-05-12 07:10:14 -07001282 {
1283 flush_dcache_all();
lh9ed821d2023-04-07 01:36:19 -07001284 ret = flash->read(nand,(loff_t)backup_area_offset,
1285 &fota_size,(u_char *)(fotaFlagInfo));
1286 if(ret != 0)
1287 {
1288 printf("read flags backup partition err.\n");
1289
1290 return -1;
1291 }
1292
xf.li6c8fc1e2023-08-12 00:11:09 -07001293 if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
lh9ed821d2023-04-07 01:36:19 -07001294 {
1295 printf("flags magic err.\n");
1296 return -1;
1297 }
1298 }
1299 }
1300 else if(type == IF_TYPE_NOR)
1301 {
1302 work_area_offset = part_offset;
1303 backup_area_offset = part_offset + nor->nor[0].mtd.erasesize;
1304 ret = nand_read(&(nor->nor[0].mtd), (loff_t)work_area_offset,
1305 &fota_size, (u_char *)(fotaFlagInfo));
1306 if(ret)
1307 {
1308 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the flags error!\n", part_name);
1309 return -1;
1310 }
xf.li6c8fc1e2023-08-12 00:11:09 -07001311 if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
lh9ed821d2023-04-07 01:36:19 -07001312 {
xf.libdd93d52023-05-12 07:10:14 -07001313 flush_dcache_all();
lh9ed821d2023-04-07 01:36:19 -07001314 ret = nand_read(&(nor->nor[0].mtd), (loff_t)backup_area_offset,
1315 &fota_size, (u_char *)(fotaFlagInfo));
1316 if(ret != 0)
1317 {
1318 printf("read flags backup partition err.\n");
1319
1320 return -1;
1321 }
1322
xf.li6c8fc1e2023-08-12 00:11:09 -07001323 if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
lh9ed821d2023-04-07 01:36:19 -07001324 {
1325 printf("flags magic err.\n");
1326 return -1;
1327 }
1328 }
1329 }
1330
1331 flash_dmabuf_disable_flag = 0;
1332 flush_dcache_all();
1333
1334 return 0;
1335}
1336
1337#endif
1338/* ================================================================================
1339 * get_fota_update_flag :
1340 * @return 0 : not fota update
1341 * @return 1 : fota update
1342 * @return -1 : error
1343 */
1344int get_fota_update_flag( void )
1345{
1346 char cmd[64] = {0};
1347//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -07001348#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -07001349 if(imagefs_flag == 1)
1350 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
1351 else
1352 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
1353#else
1354 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
1355#endif
1356 run_command(cmd, 0);
1357
1358 if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_FOTA_FLAG, strlen(ARM_FOTA_FLAG)) == 0)
1359 {
1360 fota_upflag = FOTA_UPDATE;
1361 }
1362 else if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_LOCAL_UPDATE_FLAG, strlen(ARM_LOCAL_UPDATE_FLAG)) == 0)
1363 {
1364 fota_upflag = FOTA_LOCALUPDATE;
1365 }
1366 else if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_RECOVERY_FLAG, strlen(ARM_RECOVERY_FLAG)) == 0)
1367 {
1368 fota_upflag = FOTA_RECOVERY;
1369 }
1370 else
1371 {
1372 fota_upflag = FOTA_NORMAL;
1373 }
1374
1375 BOOT_PRINTF(UBOOT_NOTICE, "fota_upflag=%d\n", fota_upflag);
1376
1377 return 0;
1378}
1379
1380/* ================================================================================
1381 * get_gmac_init_flag :
1382 * @return 0 :
1383 * @return 1 :
1384 * @return -1 :
1385 */
1386int get_gmac_init_flag( void )
1387{
1388 //hsy
1389 return 0;
1390}
1391
1392/* ================================================================================
1393 * read_gmac_init_flag :
1394 * @return 0 : not init
1395 * @return 1 : init
1396 */
1397int read_gmac_init_flag( void )
1398{
1399 return g_gmac_init_flag;
1400}
1401
1402/* ================================================================================
1403 * read_gmac_init_overtime :
1404 * @return : Ãë
1405 */
1406int read_gmac_init_overtime( void )
1407{
1408 return g_gmac_init_overtime;
1409}
1410
1411/* ================================================================================
1412 * read_fota_update_flag :
1413 * @return 0 : not fota update
1414 * @return 1 : fota update
1415 */
1416int read_fota_update_flag( void )
1417{
1418 return fota_upflag;
1419}
1420
1421/* ================================================================================
1422 * read_fota_update_flag :
1423 * @return 0 : not fota update
1424 * @return 1 : fota update
1425 */
1426
1427int read_fota_psup_flag( void )
1428{
1429 return fota_psup_flag;
1430}
1431
1432/* ================================================================================
1433 * start_arm_ps : Æô¶¯ ARM_PS °æ±¾
1434 */
1435void start_arm_ps( void )
1436{
1437 void (*ps_start)();
1438
1439 printf("Starting the arm_ps ...\n");
1440 ps_start = (void (*)())arm_ps_ep;
1441 ps_start();
1442}
1443
1444/* ================================================================================
1445 * start_arm_phy : Æô¶¯ ARM_PHY °æ±¾
1446 */
1447void start_arm_phy( void )
1448{
1449 /* д PHY Ìø×ªÆô¶¯´úÂë */
1450 writel(0xE59ff000, SYS_IRAM3_BASE);
1451 writel(arm_phy_ep, SYS_IRAM3_BASE + 8);
1452
1453 /* ÊÍ·ÅÆô¶¯ ARM_PHY */
1454 printf("Starting the arm_phy ...\n");
1455 writel(CPU_PHY_SW_RSTEN, CPU_PHY_SUBSYS_CFG);
1456}
1457
1458/* ================================================================================
1459 * start_cpucap_cores : Æô¶¯°æ±¾
1460 */
1461void start_cpucap_cores( void )
1462{
1463 writel(0xE59ff000, SYS_CPUCAP_BOOT_BASE);
1464 writel(arm_cpucap_ep, SYS_CPUCAP_BOOT_BASE + 8);
1465 printf("cap addr is 0x%x\n",arm_cpucap_ep);
1466 BOOT_PRINTF(UBOOT_NOTICE, "Starting the cpu cap ...\n");
1467 writel(CPU_CAP_SW_RSTEN, CPU_CAP_SUBSYS_CFG);
1468}
1469
1470static void usdelay(volatile int count)
1471{
1472 volatile int cnt = 0;
1473 count =count *8;
1474
1475 while(cnt<count)
1476 {
1477 cnt++;
1478 }
1479 return;
1480}
1481void cap_poweron(void)
1482{
1483 u32 tmp;
1484
1485 BOOT_PRINTF(UBOOT_NOTICE, "cap_poweron ...\n");
1486
1487 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg on*/
1488 tmp |= (0x1<<3);
1489 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1490
1491 usdelay(1);
1492
1493 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg rst*/
1494 tmp &= ~(0x1<<4);
1495 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1496
1497 usdelay(1);
1498
1499 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg iso*/
1500 tmp &= ~(0x1<<5);
1501 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1502
1503 usdelay(1);
1504
1505 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG1) ;/*ap clk on*/
1506 tmp |= (0x1<<2);
1507 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG1);
1508
1509 tmp = readl(CORE_OUTPUT_SWITCH_CONFIG_REG) ; /*ap clk&mg control by hw*/
1510 tmp |= ((0x1<<2)|(0x1<<5));
1511 writel(tmp, CORE_OUTPUT_SWITCH_CONFIG_REG);
1512 //__REG(0x0013a0ac) |= ((0x1<<2)|(0x1<<5)); /*ap clk&mg control by hw*/
1513 //__REG(0x0013a0bc) |= (0x1<<3); /*ap mg off*/
1514 //__REG(0x0013a0bc) &= ~(0x1<<4); /*ap mg rst*/
1515 //__REG(0x0013a0bc) &= ~(0x1<<5); /*ap mg iso*/
1516 //__REG(0x0013a0b8) |= (0x1<<2); /*ap clk off*/
1517
1518}
1519
1520/* ================================================================================
1521 * read_sys_ddr_kernel_start : »ñÈ¡ kernel ÔËÐпռä Ê×µØÖ·
1522 */
1523uint32_t read_sys_ddr_kernel_start(void)
1524{
1525 /* Get Addr from Image Header. */
1526 return sys_ddr_kernel_start;
1527}
1528
1529/* ================================================================================
1530 * update_led_enable : enable led
1531 * @return 0 : ³É¹¦
1532 * @return 1 : ʧ°Ü
1533 */
1534int update_led_enable(int enable)
1535{
1536 int ret = 0;
1537
1538 if(enable)
1539 {
1540#ifdef CONFIG_BOARD_7520_UFI_956
1541 ret = sn3216_SetStatus(LED_CHANNEL_WAN_GREEN,SN3216_LED_STATUS_ON);
1542 ret = sn3216_SetStatus(LED_CHANNEL_LAN_GREEN,SN3216_LED_STATUS_ON);
1543 ret = sn3216_SetStatus(LED_CHANNEL_BAT_GREEN,SN3216_LED_STATUS_ON);
1544 ret = sn3216_SetStatus(LED_CHANNEL_SMS_GREEN,SN3216_LED_STATUS_ON);
1545#endif
1546 }
1547 else
1548 {
1549#ifdef CONFIG_BOARD_7520_UFI_956
1550 ret = sn3216_SetStatus(LED_CHANNEL_WAN_GREEN,SN3216_LED_STATUS_OFF);
1551 ret = sn3216_SetStatus(LED_CHANNEL_LAN_GREEN,SN3216_LED_STATUS_OFF);
1552 ret = sn3216_SetStatus(LED_CHANNEL_BAT_GREEN,SN3216_LED_STATUS_OFF);
1553 ret = sn3216_SetStatus(LED_CHANNEL_SMS_GREEN,SN3216_LED_STATUS_OFF);
1554#endif
1555 }
1556 return ret;
1557}
1558
1559/* ================================================================================
1560 * update_led_twinkle : config led in local update
1561 * @return 0 : ³É¹¦
1562 * @return 1 : ʧ°Ü
1563 */
1564void update_led_twinkle(void)
1565{
1566 int start_time = 0;
1567 if(update_start_time)
1568 {
1569 update_recent_time = get_timer(0);
1570 if(update_recent_time-update_start_time > 6000000) /*1s*/
1571 {
1572 led_state = (~led_state)& 0x00000001;
1573 update_led_enable(led_state); /*LED on/off*/
1574 update_start_time = update_recent_time;
1575 }
1576 }
1577 return ;
1578}
1579
1580/* ================================================================================
1581 * copy_write_part : write data to flash
1582 * @return 0 : ³É¹¦
1583 * @return 1 : ʧ°Ü
1584 */
1585int copy_write_part_gmac(int cnt)
1586{
1587 int ret = 0;
1588 nand_info_t *nand = &nand_info[nand_curr_device];
1589 image_bin_header_t * img_head = &(master_head->image[cnt]);
1590 int filesize = img_head->filelength;
1591 char *partname = (char *)(img_head->partitonname);
1592 int bin_offset = img_head->fileaddr;
1593 int part_offset = img_head->partitonoffset;
1594 //uchar * buf = malloc(12*1024);
1595 uchar *buf = NULL;
1596 partition_entry_t * part = NULL;
1597
1598 buf = kzalloc(12*1024, GFP_KERNEL);
1599 if(buf == NULL)
1600 {
1601 return -1;
1602 }
1603
1604 part = find_partition_para((uchar *)partname);
1605 if(part == NULL)
1606 {
1607 return -1;
1608 }
1609
1610 /* µ±Ç°ÊÇÔÚµçÄÔÉÏÖ´ÐУ¬Ã»ÓеôµçΣÏÕ£¬ËùÒÔ¿ÉÒÔ²Á³ýzloader */
1611 /* zloaderÓÐÁ½²¿·Ö×é³É: 8kµÄbin + 4kµÄ·ÖÇø±í */
1612 if(!strcmp(partname, "zloader"))
1613 {
1614 BOOT_PRINTF(UBOOT_NOTICE, "write part %s, offset=0x%x, filesize=0x%x.\n",
1615 partname, part->part_offset, filesize);
1616 ret = downloader_nand_erase(part,part->part_size);
1617
1618 //memcpy(buf,(u_char *)(CONFIG_SYS_SDRAM_BASE+bin_offset),8192);//zloader
1619 memcpy(buf, (u_char *)(CONFIG_LOADADDR+bin_offset), 8192);//zloader
1620 memcpy((char *)(buf+8192), g_partition_table, 4096);//part
1621
1622 int times = 12*1024/nand->writesize;
1623 int i;
1624 for(i = 0; i < times; i++)
1625 {
1626 ret += nand_write_page_with_no_ecc(nand,
1627 ((loff_t)i*(nand->writesize)),
1628 buf );
1629 buf += nand->writesize;
1630 }
1631 // free(buf);
1632 return ret;
1633 }
1634
1635 if(strcmp(img_head->partitontype,"nand") == 0)
1636 {
1637 BOOT_PRINTF(UBOOT_NOTICE, "write part %s, offset=0x%x, filesize=0x%x\n",
1638 partname, part->part_offset, filesize);
1639 ret = downloader_nand_erase(part,part->part_size);
1640 //ret = nand_write_skip_bad(nand, part->part_offset, &filesize, (u_char *)(CONFIG_SYS_SDRAM_BASE+bin_offset),0);
1641 ret = nand_write_skip_bad(nand, part->part_offset, &filesize, (u_char *)(CONFIG_LOADADDR+bin_offset),0);
1642
1643 }
1644
1645 return ret;
1646}
1647
1648/* ================================================================================
1649 * copy_ddr_allbin : webui local update
1650 * @return 0 : ³É¹¦
1651 * @return 1 : ʧ°Ü
1652 */
1653int copy_ddr_allbin(void)
1654{
1655 int ret = 0;
1656 int i = 0;
1657 int head_size = 0x2000; //total 6608 bytes
1658 nand_info_t *nand = &nand_info[nand_curr_device];
1659
1660 master_head = kzalloc(0x2000, GFP_KERNEL);
1661 if(master_head == NULL)
1662 {
1663 BOOT_PRINTF(UBOOT_ERR, "kzalloc fail!\n");
1664 return -1;
1665 }
1666
1667 memcpy(( u_char *)master_head,(char *)CONFIG_LOADADDR, head_size);
1668
1669 BOOT_PRINTF(UBOOT_NOTICE, "file number = %d\n", master_head->dwFileNumber);
1670
1671 for(i = 0; i < (sizeof(master_head->image)/sizeof(image_bin_header_t))
1672 && i < master_head->dwFileNumber; i++) /* update each partition */
1673 {
1674 ret = copy_write_part_gmac(i);
1675 if(ret != 0)
1676 {
1677 BOOT_PRINTF(UBOOT_ERR, "write from ddr to nand ERROR !!!\n");
1678 return -1;
1679 }
1680 }
1681
1682 return 0;
1683}
1684