blob: 827201d3009360b603d1f606f634124035497106 [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
xf.lie31de8b2023-12-26 23:38:58 -080068extern void crc32init_le(void);
69extern unsigned int crc32_le(unsigned int crc, unsigned char const *p, size_t len);
70
lh9ed821d2023-04-07 01:36:19 -070071extern uint32_t ztelzma_compresssize;
72extern int lzmanodeflag;
73#endif
74
75//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -070076#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -070077extern int imagefs_flag;
78static uint32_t flags;
79#endif
80extern int nand_curr_device;
81extern nand_info_t nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
82extern partition_table_t * g_partition_table;
83extern int flash_dmabuf_disable_flag;
84extern struct fsl_qspi spi_nor_flash;
85
86/* Secure Verify Flag. 1->Disable, 0->Enable */
87extern unsigned int guiEfuseStatus;
xf.liaa4d92f2023-09-13 00:18:58 -070088extern unsigned int guiOtpStatus;
lh9ed821d2023-04-07 01:36:19 -070089
90uint32_t arm_ps_ep = 0; /* Entry Point Address */
91uint32_t arm_cpucap_ep = 0;
92static uint32_t arm_phy_ep = 0; /* Entry Point Address */
93
94static uint32_t fota_upflag = FOTA_NORMAL;
95static uint32_t fota_psup_flag = FOTA_PS_NORMAL;
96
97uint32_t g_gmac_init_flag = 0;
98uint32_t g_gmac_init_overtime = 0;
99
100static uint32_t sys_ddr_kernel_start = 0; /* kernel ÔËÐпռä Ê×µØÖ· */
101
102master_header_t *master_head = NULL;
103int update_start_time = 0;
104int update_recent_time = 0;
105int led_state = 0;
106
107int rd_offset = 0;
108int rd_size = 0;
109
xf.liaa4d92f2023-09-13 00:18:58 -0700110int rootfs_flag = 0;
111int m0_flag = 0;
112int zsp_flag = 0;
lh9ed821d2023-04-07 01:36:19 -0700113/* ================================================================================
114 * page_align : Ò³¶ÔÆë
115 */
116static uint32_t page_align(uint32_t offset)
117{
118 struct flash_ops *flash = NULL;
119 uint32_t page_size = 0;
120
121 flash = get_flash_ops();
122 page_size = flash->page_size;
123 if( offset & (page_size - 1) )
124 {
125 offset &= (~(page_size - 1));
126 offset += page_size;
127 }
128 return offset;
129}
130
131/* ================================================================================
132 * page_align : Ò³¶ÔÆë
133 */
134int set_entry_point(uchar * part_name, uint32_t entry_point)
135{
136 if ( strcmp( (char *)ARM_PS_IMAGE, (char *)part_name ) == 0 )
137 {
138 arm_ps_ep = entry_point;
139 return 0;
140 }
141 else if ( strcmp( (char *)ARM_PHY_IMAGE, (char *)part_name ) == 0 )
142 {
143 arm_phy_ep = entry_point;
144 return 0;
145 }
146 else if ( strcmp( (char *)ARM_RAMDISK_IMAGE, (char *)part_name ) == 0 )
147 {
148 return 0;
149 }
150 else
151 {
152 return 1;
153 }
154}
155
156static int arm_image_crc_calc(char* pcPartName,
157 uint32_t uiCRCChkSum,
158 uint32_t uiEntryPoint,
159 uint32_t uiImgSize)
160{
161 uint32_t uiCRCCalRes = 0;
162
163 BOOT_PRINTF(UBOOT_NOTICE, "(%s)CRC Calculate start\n",pcPartName);
164 uiCRCCalRes = crc32(0, (unsigned char*)uiEntryPoint, uiImgSize);
165 BOOT_PRINTF(UBOOT_NOTICE, "(%s) CRC Calculate Res=0x%0x, Size=%d Bytes\n",
166 pcPartName, uiCRCCalRes, uiImgSize);
167 if(uiCRCChkSum != uiCRCCalRes||uiCRCCalRes == 0)
168 {
169 BOOT_PRINTF(UBOOT_ERR, "(%s) CRC Check Failed!\n", pcPartName);
170 return 1;
171 }
172 BOOT_PRINTF(UBOOT_NOTICE, "(%s) CRC Check PASS!\n", pcPartName);
173 return 0;
174}
175
176/* ================================================================================
177 * reform_zsp_image : ´ÓDDRÖжÁÈ¡ZSPÏà¹ØµÄ°æ±¾
178 * @return 0 : ³É¹¦
179 * @return 1 : ʧ°Ü
180 */
181static int reform_zsp_image(uint32_t addr)
182{
183 int ret = 0;
184 int offSet = 0;
185 int lenToRead = 0;
186 int length = 0;
187
188 offSet = addr; /*DDR address for zsp,0x25000000*/
189 uint32_t arm_size = ((reg16(offSet))+(reg16(offSet+2)<<16));/*zsp bin length*/
190
191 /*move zsp's bin from zsp buf to ddr address*/
192 offSet = offSet + 0x4;
193
194 while(1)
195 {
196 if(length >= arm_size - 0x4)
197 {
198 break;
199 }
200 if((reg16(offSet+0x4)+(reg16(offSet+0x4+2)<<16))*2 <0x20000000)//not in ddr
201 {
202 lenToRead = (reg16(offSet+0x8)+(reg16(offSet+0x8+2)<<16))*2;
203 length += lenToRead + 0xc;
204 offSet = offSet + lenToRead+0xc;
205 continue;
206 }
207
208 lenToRead = (reg16(offSet+0x8)+(reg16(offSet+0x8+2)<<16))*2;
209 memcpy((void*)((reg16(offSet+0x4)+(reg16(offSet+0x4+2)<<16))*2),(const void *)(offSet+0xc),lenToRead);
210 length += lenToRead + 0xc;
211 offSet = offSet + lenToRead+0xc;
212 }
213 return 0;
214
215}
216
217
218/* ================================================================================
219 * load_arm_image : ´ÓFLASHÖжÁÈ¡ARM°æ±¾
220 * @return 0 : ³É¹¦
221 * @return 1 : ʧ°Ü
222 */
223int load_arm_image( uchar * part_name )
224{
225 int ret = 0;
226 struct flash_ops *flash = NULL;
227
228#if TIME_DEBUG
229 ulong start_time = 0;
230#endif
231
232 flush_dcache_all();
233 flash_dmabuf_disable_flag = 1;
234 flash = get_flash_ops();
235 time_debug_reset(start_time);
236 load_debug_printf("\n");
237
238 /* ѰÕÒ·ÖÇø */
239 partition_entry_t * entry = find_partition_para(part_name);
240 if( entry == NULL )
241 {
242 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
243 return 1;
244 }
245
246 /* »ñµÃ·ÖÇøÊ×µØÖ· */
247 uint32_t part_offset = entry->part_offset;
248 nand_info_t *nand = &nand_info[nand_curr_device];
249
250 /* ¶ÁÈ¡°æ±¾ËùÔÚµØÖ·µÄµÚÒ»Ò³Êý¾Ý */
251 uint32_t page_size = flash->page_size; /*¶ÁÈ¡arm °æ±¾mmcµÚÒ»¿éÊý¾Ý*/
252 ret = flash->read(nand,(loff_t)part_offset,&page_size,(u_char *)CONFIG_SYS_SDRAM_TEMP_BASE);
253 if( ret != 0 )
254 {
255 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the first page error!\n", part_name);
256 return 1;
257 }
258
259 /* »ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
260 image_header_t *header = (image_header_t *)CONFIG_SYS_SDRAM_TEMP_BASE;
261 if( ___htonl(header->ih_magic) != IH_MAGIC )
262 {
263 BOOT_PRINTF(UBOOT_ERR, "[%s]: NO bin !!!\n", part_name );
264 return 1;
265 }
266
267 uint32_t arm_size = ___htonl(header->ih_size);
268 uint32_t entey_point = ___htonl(header->ih_ep);
269 if( set_entry_point(part_name, entey_point) )
270 {
271 BOOT_PRINTF(UBOOT_ERR, "[%s][set_entry_point]: error!\n", part_name );
272 }
273
274 BOOT_PRINTF(UBOOT_NOTICE, "[%s] [size=0x%0x] from [0x%0x] to [0x%0x]\n",
275 part_name, arm_size, part_offset, entey_point);
276
277#if LOAD_IMAGE_CRC
278 uint32_t crc_bin = ___htonl(header->ih_dcrc);
279 BOOT_PRINTF(UBOOT_NOTICE, "[%s][crc_bin] ------------------- [0x%0x]\n", part_name, crc_bin);
280#endif
281
282 /* Õû¸öÍ·ºÍ·ÖÇø±íµÄ´óС */
283 uint32_t image_header_size = sizeof(image_header_t);
284 uint32_t arm_is_read_size = flash->page_size - image_header_size;
285 uint32_t arm_size_left = arm_size - arm_is_read_size; /* »¹Ã»ÓжÁÈ¡µÄ³¤¶È */
286 arm_size_left = page_align(arm_size_left); /* Ò³¶ÔÆë */
287
288 /* ¸´ÖƵÚÒ»Ò³ÖеÄÊý¾Ý */
289 memcpy((uchar *)entey_point, (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + image_header_size), arm_is_read_size);
290 /* ¶ÁȡʣÓàµÄÊý¾Ý */
291
292 ret = flash->read(nand,(loff_t)(part_offset + page_size), &arm_size_left,(u_char *)(entey_point + arm_is_read_size));
293 if( ret != 0 )
294 {
295 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the others page error...\n", part_name);
296 return 1;
297 }
298
299 BOOT_PRINTF(UBOOT_NOTICE, "[loading...] -------------------- takes [%ld] us\n", start_time);
300
301#if LOAD_IMAGE_CRC
302 time_debug_reset(start_time);
303 uint32_t crc_cal = crc32(0,(unsigned char*)entey_point, arm_size);
304 BOOT_PRINTF(UBOOT_NOTICE, "[%s][crc_bin] ------------------- [0x%0x]\n", part_name, crc_cal);
305 if( crc_bin != crc_cal )
306 {
307 BOOT_PRINTF(UBOOT_ERR, "[%s] ---------------- crc error...\n", part_name);
308 return 1;
309 }
310 BOOT_PRINTF(UBOOT_NOTICE, "[crc...] ------------------------ takes [%ld] us\n", start_time);
311#endif /* LOAD_IMAGE_CRC */
312
313 flash_dmabuf_disable_flag = 0;
314
315 return 0;
316}
317
xf.li6c8fc1e2023-08-12 00:11:09 -0700318#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700319int fs_load_dtb_image(void)
320{
321 char cmd[64] = {0};
322 /*1¡¢½«dtbÎļþloadµ½ÁÙʱµØÖ·*/
xf.li6c8fc1e2023-08-12 00:11:09 -0700323#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700324 if(imagefs_flag == 1)
325 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, DTB_IMAGE_PATH);
326 else
327 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, DTB_IMAGE_PATH);
328#else
329 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, DTB_IMAGE_PATH);
330#endif
331 run_command(cmd, 0);
332 flush_dcache_all();
333 /*2¡¢¿½±´°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
xf.libdd93d52023-05-12 07:10:14 -0700334 memcpy((uchar *)DDR_BASE_CAP_DTB_ADDR,
lh9ed821d2023-04-07 01:36:19 -0700335 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE),
336 CAP_DTB_LEN);
xf.liaa4d92f2023-09-13 00:18:58 -0700337
338 BOOT_PRINTF(UBOOT_NOTICE, "dtb load image finished.\n");
lh9ed821d2023-04-07 01:36:19 -0700339
340 return 0;
341}
xf.libdd93d52023-05-12 07:10:14 -0700342#endif
lh9ed821d2023-04-07 01:36:19 -0700343
344int fs_load_m0_image(void)
345{
346 char cmd[64] = {0};
347 int remap = 0;
xf.liaa4d92f2023-09-13 00:18:58 -0700348 int ret = 0;
349 u8 ucRet = 0;
lh9ed821d2023-04-07 01:36:19 -0700350 remap = readl(0x140000);
351 remap |= 0x800000;
352 writel(remap,0x140000);
353
354 /*1¡¢½«m0 imgÎļþloadµ½ÁÙʱµØÖ·*/
355//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700356#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700357 if(imagefs_flag == 1)
358 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
359 else
360 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
361#else
362 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, M0_IMAGE_PATH);
363#endif
xf.liaa4d92f2023-09-13 00:18:58 -0700364 ret = run_command(cmd, 0);
365 if(ret < 0)
366 return ret;
lh9ed821d2023-04-07 01:36:19 -0700367 flush_dcache_all();
xf.liaa4d92f2023-09-13 00:18:58 -0700368
369#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
370 m0_flag = 1;
371 if(g_nor_flag == 1)
372 {
373 if(guiOtpStatus == 0) //Secure Verify.
374 {
375 BOOT_PRINTF(UBOOT_DBG, "rpm image Start Secure Verify...\n");
376
377 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
378 if(ucRet != 0)
379 {
380 BOOT_PRINTF(UBOOT_ERR, "rpm image Secure Verify FAILED!\n");
381 return 1;
382 }
383 BOOT_PRINTF(UBOOT_NOTICE, "rpm image Secure Verify PASS!\n");
384 }
385 else
386 {
387 BOOT_PRINTF(UBOOT_NOTICE, "rpm image Skip Secure Verify...\n");
388 }
389 }
390 else
391 {
392 if(guiEfuseStatus == 0) //Secure Verify.
393 {
394 BOOT_PRINTF(UBOOT_DBG, "rpm image Start Secure Verify...\n");
395
396 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
397 if(ucRet != 0)
398 {
399 BOOT_PRINTF(UBOOT_ERR, "rpm image Secure Verify FAILED!\n");
400 return 1;
401 }
402 BOOT_PRINTF(UBOOT_NOTICE, "rpm image Secure Verify PASS!\n");
403 }
404 else
405 {
406 BOOT_PRINTF(UBOOT_NOTICE, "rpm image Skip Secure Verify...\n");
407 }
408 }
409 m0_flag = 0;
410#endif
411
lh9ed821d2023-04-07 01:36:19 -0700412 /*2¡¢ÉèÖÃM0µÄÈë¿ÚµØÖ·ÒÔ¼°M0°æ±¾°áÔËÍê³Éflagµ½iramÖÐ */
413 writel(1, M0_IMAGE_READY_FLAG_ADDR);
414
415 /*3¡¢µÈ´ýM0¿½±´Íê³É*/
416 while(readl(M0_IMAGE_READY_FLAG_ADDR));
417
418 printf("M0 image load success!\n");
419
420 return 0;
421}
422
xf.liaa4d92f2023-09-13 00:18:58 -0700423int load_rootfs(void)
424{
425 int ret = 0;
426 int type = 0;
427 struct flash_ops *flash = NULL;
428 uint32_t i = 0;
429 uint32_t bad_nums = 0;
430 uint32_t part_block_nums = 0;
431 uint32_t part_offset = 0;
432 uint32_t part_size = 0;
433 uchar * part_name = "rootfs";;
434 nand_info_t *nand = &nand_info[nand_curr_device];
435 flush_dcache_all();
436
437 flash_dmabuf_disable_flag = 1;
438 flash = get_flash_ops();
439 type = read_boot_flashtype();
440
441#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
442
443 if(imagefs_flag == 1)
444 {
445 part_name = "rootfs";
446 }
447 else
448 {
449 part_name = "rootfs2";
450 }
451
452#endif
453 /* ѰÕÒ·ÖÇø */
454 partition_entry_t * entry = find_partition_para(part_name);
455 if( entry == NULL )
456 {
457 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
458 return 1;
459 }
460
461 /* »ñµÃ·ÖÇøÊ×µØÖ· */
462 part_offset = entry->part_offset;
463 part_size = entry->part_size;
464
465 /* ²éѯµ±Ç°·ÖÇø»µ¿éÊý*/
466 bad_nums = 0;
467 part_block_nums = entry->part_size / nand->erasesize;
468 for(i = 0; i < part_block_nums; i++)
469 {
470 if(nand_block_isbad (nand, entry->part_offset + (loff_t)i * nand->erasesize))
471 {
472 printf("bad block addr = 0x%x\n", (entry->part_offset + i * nand->erasesize));
473 bad_nums++;
474 }
475 }
476
477 part_size = part_size - bad_nums * nand->erasesize;
478 ret = flash->read(nand,(loff_t)part_offset,
479 &part_size,(u_char *)CONFIG_SYS_SDRAM_ROOTFS_BASE);
480 if( ret != 0 )
481 {
482 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the rootfs error!\n", part_name);
483 return 1;
484 }
485 memcpy((uchar *)(CONFIG_SYS_SDRAM_ROOTFS_BASE - sizeof(image_header_t)),
486 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + sizeof(sImageNewHeader) + sizeof(sImageNewHeader)),
487 sizeof(image_header_t));
488 flash_dmabuf_disable_flag = 0;
489 flush_dcache_all();
490
491 return 0;
492}
lh9ed821d2023-04-07 01:36:19 -0700493
494#if defined(CONFIG_ZX297520V3E_JFFS2_COMPRESS)
495uint32_t lzma_header(uint32_t *ztelzma_dict,
496 int *ztelzma_lc,
497 int *ztelzma_lp,
498 int *ztelzma_pb)
499{
500 uint8_t temp_properties = 0;
501 uint32_t temp_dict = 0;
502 uint32_t temp_uncompress_size = 0;
503 uint32_t lzma_uncompresssize = 0;
504 lzma_header_t *lzmaheader;
505
506 lzmaheader = (lzma_header_t *)CONFIG_SYS_SDRAM_TEMP_BASE;
507
508 temp_properties = lzmaheader->p_properties;
509 temp_dict = (lzmaheader->p_dict[3]<<24)
510 |(lzmaheader->p_dict[2]<<16)
511 |(lzmaheader->p_dict[1]<<8)
512 |lzmaheader->p_dict[0];
513 temp_uncompress_size = (lzmaheader->p_uncompress_size[3]<<24)
514 |(lzmaheader->p_uncompress_size[2]<<16)
515 |(lzmaheader->p_uncompress_size[1]<<8)
516 |lzmaheader->p_uncompress_size[0];
517
518 *ztelzma_pb = temp_properties / (9 * 5);
519 temp_properties -= *ztelzma_pb * 9 * 5;
520 *ztelzma_lp = temp_properties / 9 ;
521 *ztelzma_lc = temp_properties - *ztelzma_lp * 9;
522 *ztelzma_dict = temp_dict;
523 lzma_uncompresssize = temp_uncompress_size;
524
525 return lzma_uncompresssize;
526}
527
528int fs_load_zsp_image(void)
529{
530 uint32_t image_tmp_buf = 0;
531 int zspimagenum=0;
532 uint32_t ztelzma_uncompresssize = 0;
533 uint32_t ztelzma_dict=0;
534 int ztelzma_lc=0;
535 int ztelzma_lp=0;
536 int ztelzma_pb=0;
537 int firstlzma_flag = 0;
538 int lzma_init_flag = 0;
539 int lzmainit_ret = 0;
540 int lzmadecompr_ret = 1;
541 uint32_t lzmaoffsize = 0;
542 char cmd[64] = {0};
543 uint32_t uiImgHdrlzma = 13;
544
545 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load begin...\n");
546
547 lzmanodeflag = 1;
548
549 /* ÁÙʱ´æ·Å½âѹºóµÄzsp°æ±¾ */
550 image_tmp_buf = CONFIG_SYS_SDRAM_TEMP_LZMA;
551
552 while(lzmanodeflag == 1)
553 {
554 /* ½«zsp imgÎļþloadµ½ÁÙʱµØÖ·*/
555//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700556#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700557 if(imagefs_flag == 1)
558 sprintf(cmd, "fsload imagefs 0x%x cpuphy_%02d.lzma",
559 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
560 zspimagenum);
561 else
562 sprintf(cmd, "fsload imagefs2 0x%x cpuphy_%02d.lzma",
563 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
564 zspimagenum);
565#else
566 sprintf(cmd, "fsload imagefs 0x%x cpuphy_%02d.lzma",
567 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE,
568 zspimagenum);
569#endif
570 run_command(cmd, 0);
571
572 if(ztelzma_compresssize)
573 {
574 /* ½âѹС°üÎļþ*/
575 ztelzma_uncompresssize=lzma_header(&ztelzma_dict,
576 &ztelzma_lc,
577 &ztelzma_lp,
578 &ztelzma_pb);
579
580 if(lzma_init_flag == 0)
581 {
582 lzmainit_ret = lzma_init(&ztelzma_dict,
583 &ztelzma_lc,
584 &ztelzma_lp,
585 &ztelzma_pb);
586 if(lzmainit_ret < 0)
587 {
588 printf("lzma_init failed\n");
589 return -1;
590 }
591 else
592 {
593 lzma_init_flag = 1;
594 }
595 }
596
597 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrlzma),
598 (unsigned char *)(image_tmp_buf + lzmaoffsize),
599 ztelzma_compresssize,
600 ztelzma_uncompresssize);
601 if(lzmadecompr_ret)
602 {
603 printf("lzma_decompress failed\n");
604 return -1;
605 }
606 lzmaoffsize += ztelzma_uncompresssize ;
607 zspimagenum++;
608 }
609 }
610
611 /* °´ÕÕZSPµÄ¸ñÊ½ÖØÅÅ */
xf.liaa4d92f2023-09-13 00:18:58 -0700612 reform_zsp_image(image_tmp_buf + sizeof(sImageNewHeader) + sizeof(image_header_t));
lh9ed821d2023-04-07 01:36:19 -0700613 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load finished.\n");
614
615 return 0;
616}
617
618int fs_load_arm_image_linux(char* image_name)
619{
620 char cmd[64] = {0};
621 int ret = 0;
622 int lzmainit_ret = 0;
623 int lzmadecompr_ret = 1;
624 u8 ucRet = 0;
625 int apimagenum=0;
626 uint32_t uiImgSize = 0;
627 uint32_t uiEntryPoint = 0;
628 uint32_t uiLoadPoint = 0;
629 uint32_t lzmaLoadPoint = 0;
630 uint32_t ztelzma_uncompresssize = 0;
631 uint32_t ztelzma_dict=0;
632 int ztelzma_lc=0;
633 int ztelzma_lp=0;
634 int ztelzma_pb=0;
635 int firstlzma_flag = 0;
636#if LOAD_IMAGE_CRC
637 uint32_t uiCRCChkSum = 0;
638 uint32_t uiCRCCalRes = 0;
639#endif
640 uint32_t uiImgHdrSizeOld = sizeof(image_header_t);
641 uint32_t uiImgHdrSizeNew = 0;
xf.liaa4d92f2023-09-13 00:18:58 -0700642 uint32_t uiRootfsHdrSizeOld = sizeof(image_header_t);
643 uint32_t uiRootfsHdrSizeNew = sizeof(sImageNewHeader);
lh9ed821d2023-04-07 01:36:19 -0700644 uint32_t uiImgHdrlzma = 13;
645 uint32_t lastlzmaSize = 0;
646
647 int lzma_init_flag = 0;
648 lzmanodeflag = 1;
649
650 BOOT_PRINTF(UBOOT_NOTICE, "AP image load begin...\n");
651
xf.liaa4d92f2023-09-13 00:18:58 -0700652 uiImgHdrSizeNew = sizeof(sImageNewHeader);
lh9ed821d2023-04-07 01:36:19 -0700653
654 image_header_t *psImgHdrOld = NULL;
655
656 while(lzmanodeflag == 1)
657 {
658 /*1¡¢½«ap imgÎļþloadµ½ÁÙʱµØÖ·*/
659//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700660#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700661 if(imagefs_flag == 1)
662 sprintf(cmd, "fsload imagefs 0x%x /%s_%02d.lzma",
663 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
664 else
665 sprintf(cmd, "fsload imagefs2 0x%x /%s_%02d.lzma",
666 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
667#else
668 sprintf(cmd, "fsload imagefs 0x%x /%s_%02d.lzma",
669#endif
670 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name, apimagenum);
671
672 run_command(cmd, 0);
673
674 if(ztelzma_compresssize)
675 {
676 /*2¡¢½âѹС°üÎļþ*/
677 ztelzma_uncompresssize=lzma_header(&ztelzma_dict,
678 &ztelzma_lc,
679 &ztelzma_lp,
680 &ztelzma_pb);
681
682 if(lzma_init_flag == 0)
683 {
684 lzmainit_ret = lzma_init(&ztelzma_dict,
685 &ztelzma_lc,
686 &ztelzma_lp,
687 &ztelzma_pb);
688 if(lzmainit_ret < 0)
689 {
690 printf("lzma_init failed\n");
691 return -1;
692 }
693 else
694 {
695 lzma_init_flag = 1;
696 }
697 }
698
699 /*3¡¢»ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
700 if(firstlzma_flag == 0)
701 {
702 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE+uiImgHdrlzma),
703 (unsigned char *)(CONFIG_SYS_SDRAM_TEMP_LZMA),
704 ztelzma_compresssize,
705 ztelzma_uncompresssize);
706 if(lzmadecompr_ret)
707 {
708 printf("lzma_decompress failed\n");
709 return -1;
710 }
711
xf.liaa4d92f2023-09-13 00:18:58 -0700712 /*»ñÈ¡¹«Ô¿´«µÝ¸ø´ó°æ±¾Ê¹ÓÃ*/
713 if(reg16(CONFIG_SYS_SDRAM_TEMP_LZMA)== RSA_1024)
714 {
715 memset(CFG_SECURE_PUK_ADDR,0,256);
716 memcpy(CFG_SECURE_PUK_ADDR+124,CONFIG_SYS_SDRAM_TEMP_BASE+12,132);
717 }else{
718 printf("signtype is %s\n", reg16(CONFIG_SYS_SDRAM_TEMP_BASE)== RSA_2048 ? "RSA2048":"UNKNOWN");
719 }
lh9ed821d2023-04-07 01:36:19 -0700720
xf.liaa4d92f2023-09-13 00:18:58 -0700721 psImgHdrOld = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_LZMA + uiImgHdrSizeNew + uiRootfsHdrSizeNew + uiRootfsHdrSizeOld);
lh9ed821d2023-04-07 01:36:19 -0700722
723 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
724 {
725 BOOT_PRINTF(UBOOT_ERR, "Magic Num Check Failed,Maybe no AP bin !!!\n");
726 return 1;
xf.liaa4d92f2023-09-13 00:18:58 -0700727 }
728
lh9ed821d2023-04-07 01:36:19 -0700729 uiImgSize = ___htonl(psImgHdrOld->ih_size);
730 uiEntryPoint = ___htonl(psImgHdrOld->ih_ep);
xf.liaa4d92f2023-09-13 00:18:58 -0700731 uiLoadPoint = ___htonl(psImgHdrOld->ih_load) - uiImgHdrSizeOld - uiRootfsHdrSizeOld - uiRootfsHdrSizeNew; /* ÕâÀïʹÓÃLOADµØÖ· */
732
lh9ed821d2023-04-07 01:36:19 -0700733 lzmaLoadPoint = uiLoadPoint;
734 BOOT_PRINTF(UBOOT_NOTICE, "Load AP image, Size=0x%0x, to 0x%0x.\n",
735 uiImgSize, uiLoadPoint);
736
737#if LOAD_IMAGE_CRC
738 uiCRCChkSum = ___htonl(psImgHdrOld->ih_dcrc);
739 BOOT_PRINTF(UBOOT_NOTICE, "AP image CRC Checksum=0x%0x.\n", uiCRCChkSum);
740#endif
741 /*4¡¢¿½±´µÚÒ»°ü°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
742 memcpy((uchar *)(uiLoadPoint - uiImgHdrSizeNew),
743 (uchar *)(CONFIG_SYS_SDRAM_TEMP_LZMA),
744 ztelzma_uncompresssize);
745 firstlzma_flag = 1;
746 lastlzmaSize = ztelzma_uncompresssize - uiImgHdrSizeNew;
747 }
748 else
749 {
750 lzmaLoadPoint = lzmaLoadPoint + lastlzmaSize ;
751 lastlzmaSize = ztelzma_uncompresssize;
752 /*5¡¢¿½±´Ê£Óà°ü°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
753
754 lzmadecompr_ret=lzma_decompress((unsigned char *)(CONFIG_SYS_SDRAM_TEMP_BASE+uiImgHdrlzma),
755 (unsigned char *)(lzmaLoadPoint),
756 ztelzma_compresssize, lastlzmaSize);
757 if(lzmadecompr_ret)
758 {
759 printf("lzma_decompress failed\n");
760 return -1;
761 }
762 }
763 apimagenum++;
764 }
765 }
766
767 BOOT_PRINTF(UBOOT_NOTICE, "AP image load finished\n");
768
769 if(guiEfuseStatus == 0) //Secure Verify.
770 {
771 BOOT_PRINTF(UBOOT_DBG, "AP image Start Secure Verify...\n");
772
773 ucRet = secure_verify((u32 )uiLoadPoint - uiImgHdrSizeNew);
774 if(ucRet != 0)
775 {
776 BOOT_PRINTF(UBOOT_ERR, "AP image Secure Verify FAILED!\n");
777 return 1;
778 }
779 BOOT_PRINTF(UBOOT_NOTICE, "AP image Secure Verify PASS!\n");
780 }
781 else
782 {
783 BOOT_PRINTF(UBOOT_NOTICE, "AP image Skip Secure Verify...\n");
784 }
785
786 if(strncmp((const char *)image_name, "cpucap", 6) == 0)
787 {
788 arm_cpucap_ep = uiEntryPoint; /* usually */
789 }
790 else
791 {
792 sys_ddr_kernel_start = uiEntryPoint - 0x8000; /* usually */
793 gd->bd->bi_boot_params = sys_ddr_kernel_start + 0x100;
xf.liaa4d92f2023-09-13 00:18:58 -0700794 uiLoadPoint += (uiRootfsHdrSizeOld + uiRootfsHdrSizeNew);
lh9ed821d2023-04-07 01:36:19 -0700795 sprintf((char *)cmd," bootm 0x%0x", uiLoadPoint);
796 setenv("bootcmd", (char *)cmd);
797 }
798
799#if LOAD_IMAGE_CRC
800 //invalidate_dcache_range(uiEntryPoint, uiEntryPoint + uiImgSize);
801 ret = arm_image_crc_calc(ARM_APP_IMAGE, uiCRCChkSum, uiEntryPoint, uiImgSize);
802 if(ret != 0)
803 {
804 BOOT_PRINTF(UBOOT_ERR, "ap image crc calc failed, ret = %d! \n", ret);
805 return -1;
806 }
807#endif
808
809 return 0;
810
811}
812#else
813
814int fs_load_zsp_image(void)
815{
816 char cmd[64] = {0};
817 uint32_t image_tmp_buf = 0;
xf.liaa4d92f2023-09-13 00:18:58 -0700818 int ret = 0;
819 u8 ucRet = 0;
lh9ed821d2023-04-07 01:36:19 -0700820 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load begin...\n");
821 image_tmp_buf = CONFIG_SYS_SDRAM_TEMP_BASE;
822//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700823#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700824 if(imagefs_flag == 1)
825 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
826 else
827 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
828#else
829 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)image_tmp_buf, ZSP_IMAGE_PATH);
830#endif
xf.liaa4d92f2023-09-13 00:18:58 -0700831 ret = run_command(cmd, 0);
832 if(ret < 0)
833 return ret;
834
835#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
836 zsp_flag = 1;
837 if(g_nor_flag == 1)
838 {
839 if(guiOtpStatus == 0) //Secure Verify.
840 {
841 BOOT_PRINTF(UBOOT_DBG, "zsp image Start Secure Verify...\n");
842
843 ucRet = secure_verify((u32 )image_tmp_buf);
844 if(ucRet != 0)
845 {
846 BOOT_PRINTF(UBOOT_ERR, "zsp image Secure Verify FAILED!\n");
847 return 1;
848 }
849 BOOT_PRINTF(UBOOT_NOTICE, "zsp image Secure Verify PASS!\n");
850 }
851 else
852 {
853 BOOT_PRINTF(UBOOT_NOTICE, "zsp image Skip Secure Verify...\n");
854 }
855 }
856 else
857 {
858 if(guiEfuseStatus == 0) //Secure Verify.
859 {
860 BOOT_PRINTF(UBOOT_DBG, "zsp image Start Secure Verify...\n");
861
862 ucRet = secure_verify((u32 )image_tmp_buf);
863 if(ucRet != 0)
864 {
865 BOOT_PRINTF(UBOOT_ERR, "zsp image Secure Verify FAILED!\n");
866 return 1;
867 }
868 BOOT_PRINTF(UBOOT_NOTICE, "zsp image Secure Verify PASS!\n");
869 }
870 else
871 {
872 BOOT_PRINTF(UBOOT_NOTICE, "zsp image Skip Secure Verify...\n");
873 }
874 }
875 zsp_flag = 0;
lh9ed821d2023-04-07 01:36:19 -0700876
xf.liaa4d92f2023-09-13 00:18:58 -0700877#endif
878
879 reform_zsp_image(image_tmp_buf + sizeof(sImageNewHeader) + sizeof(image_header_t));
880
lh9ed821d2023-04-07 01:36:19 -0700881 BOOT_PRINTF(UBOOT_NOTICE, "zsp image load finished.\n");
882
883 return 0;
884}
885
886int fs_load_arm_image_linux(char* image_name )
887{
888 char cmd[64] = {0};
889 int ret = 0;
890 u8 ucRet = 0;
891
892 uint32_t uiImgSize = 0;
893 uint32_t uiEntryPoint = 0;
894 uint32_t uiLoadPoint = 0;
895#if LOAD_IMAGE_CRC
896 uint32_t uiCRCChkSum = 0;
897 uint32_t uiCRCCalRes = 0;
898#endif
899 uint32_t uiImgHdrSizeOld = sizeof(image_header_t);
xf.liaa4d92f2023-09-13 00:18:58 -0700900 uint32_t uiRootfsHdrSizeOld = sizeof(image_header_t);
lh9ed821d2023-04-07 01:36:19 -0700901
xf.liaa4d92f2023-09-13 00:18:58 -0700902 uint32_t uiImgHdrSizeNew = sizeof(sImageNewHeader);
903 uint32_t uiRootfsHdrSizeNew = sizeof(sImageNewHeader);
lh9ed821d2023-04-07 01:36:19 -0700904
905 image_header_t *psImgHdrOld = NULL;
xf.liaa4d92f2023-09-13 00:18:58 -0700906 image_header_t *psRootfsHdrOld = NULL;
lh9ed821d2023-04-07 01:36:19 -0700907
908 /*1¡¢½«ap imgÎļþloadµ½ÁÙʱµØÖ·*/
909//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -0700910#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -0700911 if(imagefs_flag == 1)
912 sprintf(cmd, "fsload imagefs 0x%x /ap_%s.bin",
913 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
914 else
915 sprintf(cmd, "fsload imagefs2 0x%x /ap_%s.bin",
916 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
917#else
918 sprintf(cmd, "fsload imagefs 0x%x /ap_%s.bin",
919 (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, image_name);
920#endif
xf.liaa4d92f2023-09-13 00:18:58 -0700921 ret = run_command(cmd, 0);
922 if(ret < 0)
923 return ret;
lh9ed821d2023-04-07 01:36:19 -0700924
xf.liaa4d92f2023-09-13 00:18:58 -0700925 if(strncmp((const char *)image_name, "cpuap", 5) == 0)
lh9ed821d2023-04-07 01:36:19 -0700926 {
xf.liaa4d92f2023-09-13 00:18:58 -0700927 rootfs_flag = 1;
lh9ed821d2023-04-07 01:36:19 -0700928
xf.liaa4d92f2023-09-13 00:18:58 -0700929 if(g_nor_flag == 1)
lh9ed821d2023-04-07 01:36:19 -0700930 {
xf.liaa4d92f2023-09-13 00:18:58 -0700931 /*»ñÈ¡¹«Ô¿´«µÝ¸ø´ó°æ±¾Ê¹ÓÃ*/
932 memcpy(OTP_SECURE_PUK_BASE,(uchar *)CONFIG_SYS_SDRAM_TEMP_BASE+4,380);
933 //printf("OTP_SECURE_PUK_BASE is 0x%x\n",OTP_SECURE_PUK_BASE);
934
935 /*°²È«Ð£Ñérootfs*/
936 if(guiOtpStatus == 0) //Secure Verify.
937 {
938 BOOT_PRINTF(UBOOT_DBG, "AP rootfs Start Secure Verify...\n");
939
940 /*¼ÓÔØrootfs¾µÏñ*/
941 ret = load_rootfs();
942 if(ret != 0)
943 {
944 printf("rootfs load error.\n");
945 return 1;
946 }
947
948 ucRet = rootfs_secure_verify((u32 )(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew));
949 if(ucRet != 0)
950 {
951 BOOT_PRINTF(UBOOT_ERR, "AP rootfs Secure Verify FAILED!\n");
952 return 1;
953 }
954 BOOT_PRINTF(UBOOT_NOTICE, "AP rootfs Secure Verify PASS!\n");
955 }
956 else
957 {
958 BOOT_PRINTF(UBOOT_NOTICE, "AP rootfs Skip Secure Verify...\n");
959 }
960 }
961 else
962 {
963 /*»ñÈ¡¹«Ô¿´«µÝ¸ø´ó°æ±¾Ê¹ÓÃ*/
964 if(reg16(CONFIG_SYS_SDRAM_TEMP_BASE)== RSA_1024)
965 {
966 memset(CFG_SECURE_PUK_ADDR,0,256);
967 memcpy(CFG_SECURE_PUK_ADDR+124,CONFIG_SYS_SDRAM_TEMP_BASE+12,132);
968 }else{
969 printf("signtype is %s\n", reg16(CONFIG_SYS_SDRAM_TEMP_BASE)== RSA_2048 ? "RSA2048":"UNKNOWN");
970 //printf("pub key is illegal...\n");
971 }
972 /*°²È«Ð£Ñérootfs*/
973 if(guiEfuseStatus == 0) //Secure Verify.
974 {
975 BOOT_PRINTF(UBOOT_DBG, "AP rootfs Start Secure Verify...\n");
976
977 /*¼ÓÔØrootfs¾µÏñ*/
978 ret = load_rootfs();
979 if(ret != 0)
980 {
981 printf("rootfs load error.\n");
982 return 1;
983 }
984
985 ucRet = rootfs_secure_verify((u32 )(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew));
986 if(ucRet != 0)
987 {
988 BOOT_PRINTF(UBOOT_ERR, "AP rootfs Secure Verify FAILED!\n");
989 return 1;
990 }
991 BOOT_PRINTF(UBOOT_NOTICE, "AP rootfs Secure Verify PASS!\n");
992 }
993 else
994 {
995 BOOT_PRINTF(UBOOT_NOTICE, "AP rootfs Skip Secure Verify...\n");
996 }
997
998 }
999
1000
1001 /*2¡¢»ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
1002 psImgHdrOld = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew + uiRootfsHdrSizeNew + uiRootfsHdrSizeOld);
1003 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
1004 {
1005 BOOT_PRINTF(UBOOT_ERR, "Magic Num Check Failed,Maybe no AP bin !!!\n");
lh9ed821d2023-04-07 01:36:19 -07001006 return 1;
1007 }
xf.liaa4d92f2023-09-13 00:18:58 -07001008
1009 uiImgSize = ___htonl(psImgHdrOld->ih_size);
1010 uiEntryPoint = ___htonl(psImgHdrOld->ih_ep);
1011 uiLoadPoint = ___htonl(psImgHdrOld->ih_load) - uiImgHdrSizeOld - uiRootfsHdrSizeOld - uiRootfsHdrSizeNew; /* ÕâÀïʹÓÃLOADµØÖ· */
1012
1013 BOOT_PRINTF(UBOOT_NOTICE, "Load AP image, Size=0x%0x, to 0x%0x.\n",
1014 uiImgSize, uiLoadPoint);
1015
1016#if LOAD_IMAGE_CRC
1017 uiCRCChkSum = ___htonl(psImgHdrOld->ih_dcrc);
1018 BOOT_PRINTF(UBOOT_NOTICE, "AP image CRC Checksum=0x%0x.\n", uiCRCChkSum);
1019#endif
1020 BOOT_PRINTF(UBOOT_NOTICE, "AP image uiLoadPoint=0x%0x.\n", uiLoadPoint);
1021
1022 /*3¡¢¿½±´°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
1023 memcpy((uchar *)uiLoadPoint,
1024 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew),
1025 uiImgSize + uiImgHdrSizeOld + uiRootfsHdrSizeOld + uiRootfsHdrSizeNew);
1026
1027 BOOT_PRINTF(UBOOT_NOTICE, "AP image load image finished\n");
1028 }
1029 else
1030 {
1031 /*2¡¢»ñÈ¡°æ±¾µÄ´óСºÍÔËÐеØÖ· */
1032 psImgHdrOld = (image_header_t *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew);
1033 if(___htonl(psImgHdrOld->ih_magic) != IH_MAGIC)
1034 {
1035 BOOT_PRINTF(UBOOT_ERR, "Magic Num Check Failed,Maybe no AP bin !!!\n");
1036 return 1;
1037 }
1038
1039 uiImgSize = ___htonl(psImgHdrOld->ih_size);
1040 uiEntryPoint = ___htonl(psImgHdrOld->ih_ep);
1041 uiLoadPoint = ___htonl(psImgHdrOld->ih_load) - uiImgHdrSizeOld; /* ÕâÀïʹÓÃLOADµØÖ· */
1042
1043 BOOT_PRINTF(UBOOT_NOTICE, "Load AP image, Size=0x%0x, to 0x%0x.\n",
1044 uiImgSize, uiLoadPoint);
1045
1046#if LOAD_IMAGE_CRC
1047 uiCRCChkSum = ___htonl(psImgHdrOld->ih_dcrc);
1048 BOOT_PRINTF(UBOOT_NOTICE, "AP image CRC Checksum=0x%0x.\n", uiCRCChkSum);
1049#endif
1050 BOOT_PRINTF(UBOOT_NOTICE, "AP image uiLoadPoint=0x%0x.\n", uiLoadPoint);
1051
1052 /*3¡¢¿½±´°æ±¾Êý¾Ýµ½ÔËÐеØÖ· */
1053 memcpy((uchar *)uiLoadPoint,
1054 (uchar *)(CONFIG_SYS_SDRAM_TEMP_BASE + uiImgHdrSizeNew),
1055 uiImgSize +uiImgHdrSizeOld);
1056
1057 BOOT_PRINTF(UBOOT_NOTICE, "AP image load image finished\n");
1058 }
1059
1060 if(g_nor_flag == 1)
1061 {
1062 if(guiOtpStatus == 0) //Secure Verify.
1063 {
1064 BOOT_PRINTF(UBOOT_DBG, "AP image Start Secure Verify...\n");
1065
1066 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
1067 if(ucRet != 0)
1068 {
1069 BOOT_PRINTF(UBOOT_ERR, "AP image Secure Verify FAILED!\n");
1070 return 1;
1071 }
1072 BOOT_PRINTF(UBOOT_NOTICE, "AP image Secure Verify PASS!\n");
1073 }
1074 else
1075 {
1076 BOOT_PRINTF(UBOOT_NOTICE, "AP image Skip Secure Verify...\n");
1077 }
lh9ed821d2023-04-07 01:36:19 -07001078 }
1079 else
1080 {
xf.liaa4d92f2023-09-13 00:18:58 -07001081 if(guiEfuseStatus == 0) //Secure Verify.
1082 {
1083 BOOT_PRINTF(UBOOT_DBG, "AP image Start Secure Verify...\n");
lh9ed821d2023-04-07 01:36:19 -07001084
xf.liaa4d92f2023-09-13 00:18:58 -07001085 ucRet = secure_verify((u32 )CONFIG_SYS_SDRAM_TEMP_BASE);
1086 if(ucRet != 0)
1087 {
1088 BOOT_PRINTF(UBOOT_ERR, "AP image Secure Verify FAILED!\n");
1089 return 1;
1090 }
1091 BOOT_PRINTF(UBOOT_NOTICE, "AP image Secure Verify PASS!\n");
1092 }
1093 else
1094 {
1095 BOOT_PRINTF(UBOOT_NOTICE, "AP image Skip Secure Verify...\n");
1096 }
1097 }
1098
1099 if(strncmp((const char *)image_name, "cpucap", 6) == 0)
lh9ed821d2023-04-07 01:36:19 -07001100 {
1101 arm_cpucap_ep = uiEntryPoint; /* usually */
1102 printf("cpucap load finish....\n");
1103 }
1104 else
1105 {
1106 sys_ddr_kernel_start = uiEntryPoint - 0x8000; /* usually */
1107 gd->bd->bi_boot_params = sys_ddr_kernel_start + 0x100;
xf.liaa4d92f2023-09-13 00:18:58 -07001108 if(1 == rootfs_flag)
1109 uiLoadPoint += (uiRootfsHdrSizeOld + uiRootfsHdrSizeNew);
lh9ed821d2023-04-07 01:36:19 -07001110 sprintf((char *)cmd," bootm 0x%0x", uiLoadPoint);
1111 setenv("bootcmd", (char *)cmd);
1112 }
1113#if LOAD_IMAGE_CRC
1114 //invalidate_dcache_range(uiEntryPoint, uiEntryPoint + uiImgSize);
1115 ret = arm_image_crc_calc(ARM_APP_IMAGE, uiCRCChkSum, uiEntryPoint, uiImgSize);
1116 if(ret != 0)
1117 {
1118 BOOT_PRINTF(UBOOT_ERR, "ap image crc calc failed, ret = %d! \n", ret);
1119 return -1;
1120 }
1121#endif
1122
1123 return 0;
1124}
1125
1126#endif
1127
1128int load_imagefs(uchar * part_name)
1129{
1130 int ret = 0;
1131 int type = 0;
1132 struct flash_ops *flash = NULL;
1133 uint32_t i = 0;
1134 uint32_t bad_nums = 0;
1135 uint32_t part_block_nums = 0;
1136 uint32_t part_offset = 0;
1137 uint32_t part_size = 0;
1138 nand_info_t *nand = &nand_info[nand_curr_device];
1139 struct fsl_qspi *nor = &spi_nor_flash;
1140
1141 /* ´Ë´¦±ØÐëÇå0xFF£¬±£Ö¤¾µÏñÍêÕû£¬²»ÊÜÉÏ´ÎÒÅÁôÊý¾ÝÓ°Ïì*/
1142 memset(CONFIG_SYS_SDRAM_IMAGEFS_BASE, 0xFF, CONFIG_SYS_SDRAM_IMAGEFS_SIZE);
1143 flush_dcache_all();
1144
1145 flash_dmabuf_disable_flag = 1;
1146 flash = get_flash_ops();
1147 type = read_boot_flashtype();
1148
1149 /* ѰÕÒ·ÖÇø */
1150 partition_entry_t * entry = find_partition_para(part_name);
1151 if( entry == NULL )
1152 {
1153 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
1154 return 1;
1155 }
1156
1157 /* »ñµÃ·ÖÇøÊ×µØÖ· */
1158 part_offset = entry->part_offset;
1159 part_size = entry->part_size;
1160
1161 if(part_size > CONFIG_SYS_SDRAM_IMAGEFS_SIZE)
1162 {
1163 BOOT_PRINTF(UBOOT_ERR, "[%s] part size more than SDRAM IMAGEFS space !!!\n", part_name);
1164 return 1;
1165 }
1166
1167 if(type == IF_TYPE_NAND || type == IF_TYPE_SPI_NAND)
1168 {
1169 /* ²éѯµ±Ç°·ÖÇø»µ¿éÊý*/
1170 bad_nums = 0;
1171 part_block_nums = entry->part_size / nand->erasesize;
1172 for(i = 0; i < part_block_nums; i++)
1173 {
1174 if(nand_block_isbad (nand, entry->part_offset + (loff_t)i * nand->erasesize))
1175 {
1176 printf("bad block addr = 0x%x\n", (entry->part_offset + i * nand->erasesize));
1177 bad_nums++;
1178 }
1179 }
1180
1181 part_size = part_size - bad_nums * nand->erasesize;
1182 ret = flash->read(nand,(loff_t)part_offset,
1183 &part_size,(u_char *)CONFIG_SYS_SDRAM_IMAGEFS_BASE);
1184 if( ret != 0 )
1185 {
1186 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the imagefs error!\n", part_name);
1187 return 1;
1188 }
1189 }
1190 else if(type == IF_TYPE_NOR)
1191 {
1192 ret = nand_read(&(nor->nor[0].mtd), (loff_t)part_offset,
1193 &part_size, (u_char *)CONFIG_SYS_SDRAM_IMAGEFS_BASE);
1194 if(ret)
1195 {
1196 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the imagefs error!\n", part_name);
1197 return -1;
1198 }
1199 }
1200
1201 flash_dmabuf_disable_flag = 0;
1202 flush_dcache_all();
1203
1204 return 0;
1205}
1206
1207//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -07001208#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
1209int load_flags(T_FLAGS_INFO *fotaFlagInfo)
lh9ed821d2023-04-07 01:36:19 -07001210{
1211 int ret = 0;
1212 int type = 0;
1213 uchar * part_name = "flags";
1214 struct flash_ops *flash = NULL;
1215 uint32_t i = 0;
1216 uint32_t off = 0;
1217 uint32_t bad_nums = 0;
1218 uint32_t part_block_nums = 0;
1219 uint32_t part_offset = 0;
1220 uint32_t part_size = 0;
1221 uint32_t blockNum = 0;
1222 uint32_t work_area_offset = 0;
1223 uint32_t backup_area_offset = 0;
1224 uint32_t flag_one = 0;
xf.li6c8fc1e2023-08-12 00:11:09 -07001225 uint32_t fota_size = sizeof(T_FLAGS_INFO);
lh9ed821d2023-04-07 01:36:19 -07001226 nand_info_t *nand = &nand_info[nand_curr_device];
1227 struct fsl_qspi *nor = &spi_nor_flash;
xf.lie31de8b2023-12-26 23:38:58 -08001228 unsigned long crc = 0;
1229 unsigned long crc_cal = 0;
1230 u32 crc_size = sizeof(T_FLAGS_INFO);
xf.li7ccf8372024-03-07 00:08:02 -08001231 char name[IMG_NAME_LEN];
lh9ed821d2023-04-07 01:36:19 -07001232
1233 flush_dcache_all();
xf.li7ccf8372024-03-07 00:08:02 -08001234 //flash_dmabuf_disable_flag = 1;
1235
lh9ed821d2023-04-07 01:36:19 -07001236 flash = get_flash_ops();
1237 type = read_boot_flashtype();
1238
1239 /* ѰÕÒ·ÖÇø */
1240 partition_entry_t * entry = find_partition_para(part_name);
1241 if( entry == NULL )
1242 {
1243 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", part_name);
1244 return 1;
1245 }
1246
1247 /* »ñµÃ·ÖÇøÊ×µØÖ· */
1248 part_offset = entry->part_offset;
1249 part_size = entry->part_size;
1250 //while(flag);
xf.li7ccf8372024-03-07 00:08:02 -08001251
1252 /*È·¶¨¹¤×÷ÇøºÍ±¸·ÝÇøÆ«ÒÆµØÖ·*/
1253 for (off = part_offset; off < part_offset+part_size; off += nand->erasesize)
lh9ed821d2023-04-07 01:36:19 -07001254 {
xf.li7ccf8372024-03-07 00:08:02 -08001255 if (!(nand_block_isbad(nand,off)))
lh9ed821d2023-04-07 01:36:19 -07001256 {
xf.li7ccf8372024-03-07 00:08:02 -08001257 blockNum += 1;
1258 }
1259
1260 if((blockNum == 1) && (flag_one == 0))
1261 {
1262 work_area_offset = off;
1263 flag_one = 1;
1264 }
lh9ed821d2023-04-07 01:36:19 -07001265
xf.li7ccf8372024-03-07 00:08:02 -08001266 else if(blockNum == 2)
1267 {
1268 backup_area_offset = off;
1269 break;
1270 }
1271 }
1272
1273 if(blockNum < 2)
1274 {
1275 printf("flags partition have not enough space!\n");
lh9ed821d2023-04-07 01:36:19 -07001276
xf.li7ccf8372024-03-07 00:08:02 -08001277 return -1;
1278 }
1279
1280 ret = flash->read(nand,(loff_t)work_area_offset,
1281 &fota_size,(u_char *)(fotaFlagInfo));
1282 if( ret != 0 )
1283 {
1284 BOOT_PRINTF(UBOOT_ERR, "[%s]: read the flags error!\n", part_name);
1285 return 1;
1286 }
lh9ed821d2023-04-07 01:36:19 -07001287
xf.li7ccf8372024-03-07 00:08:02 -08001288 /*crc32УÑé*/
1289 crc = fotaFlagInfo->crc32_1;
1290 fotaFlagInfo->crc32_1 = 0;
1291 crc32init_le();
1292 crc_cal = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
1293 printf("crc is 0x%x,crc_cal is 0x%x\n",crc,crc_cal);
1294 //printf("work_area_offset is 0x%x,backup_area_offset is 0x%x,crc_size is 0x%x\n",work_area_offset,backup_area_offset,crc_size);
xf.lie31de8b2023-12-26 23:38:58 -08001295
xf.li7ccf8372024-03-07 00:08:02 -08001296 memset(name, 0xff, IMG_NAME_LEN);
1297 if(!memcmp(fotaFlagInfo->img_size[0].name, name, IMG_NAME_LEN)){
1298 printf("image name is null.\n");
1299 if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
xf.libdd93d52023-05-12 07:10:14 -07001300 {
xf.li7ccf8372024-03-07 00:08:02 -08001301 printf("work_area_offset magic err.\n");
xf.libdd93d52023-05-12 07:10:14 -07001302 flush_dcache_all();
lh9ed821d2023-04-07 01:36:19 -07001303 ret = flash->read(nand,(loff_t)backup_area_offset,
1304 &fota_size,(u_char *)(fotaFlagInfo));
1305 if(ret != 0)
1306 {
1307 printf("read flags backup partition err.\n");
1308
1309 return -1;
1310 }
1311
xf.li6c8fc1e2023-08-12 00:11:09 -07001312 if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
lh9ed821d2023-04-07 01:36:19 -07001313 {
1314 printf("flags magic err.\n");
1315 return -1;
xf.li7ccf8372024-03-07 00:08:02 -08001316 }
lh9ed821d2023-04-07 01:36:19 -07001317 }
xf.li7ccf8372024-03-07 00:08:02 -08001318
lh9ed821d2023-04-07 01:36:19 -07001319 }
xf.li7ccf8372024-03-07 00:08:02 -08001320 else if(fotaFlagInfo->magic_start != FLAGS_MAGIC || (crc!= crc_cal && crc != 0))
1321 {
1322 flush_dcache_all();
1323 printf("work_area_offset magic is 0x%x.\n",fotaFlagInfo->magic_start);
1324 ret = flash->read(nand,(loff_t)backup_area_offset,
1325 &fota_size,(u_char *)(fotaFlagInfo));
1326 if(ret != 0)
lh9ed821d2023-04-07 01:36:19 -07001327 {
xf.li7ccf8372024-03-07 00:08:02 -08001328 printf("read flags backup partition err.\n");
1329
lh9ed821d2023-04-07 01:36:19 -07001330 return -1;
1331 }
xf.li7ccf8372024-03-07 00:08:02 -08001332
xf.li6c8fc1e2023-08-12 00:11:09 -07001333 if(fotaFlagInfo->magic_start != FLAGS_MAGIC)
lh9ed821d2023-04-07 01:36:19 -07001334 {
xf.li7ccf8372024-03-07 00:08:02 -08001335 printf("backup_area_offset magic is 0x%x.\n",fotaFlagInfo->magic_start);
1336 printf("flags magic err.\n");
1337 return -1;
lh9ed821d2023-04-07 01:36:19 -07001338 }
xf.li7ccf8372024-03-07 00:08:02 -08001339
1340 /*crc32УÑé*/
1341 crc = fotaFlagInfo->crc32_1;
1342 fotaFlagInfo->crc32_1 = 0;
1343 crc32init_le();
1344 crc_cal = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
1345
1346 if(crc!= crc_cal && crc != 0)
1347 {
1348 printf("flags crc err.\n");
1349 return -1;
1350 }
1351
lh9ed821d2023-04-07 01:36:19 -07001352 }
xf.li7ccf8372024-03-07 00:08:02 -08001353
lh9ed821d2023-04-07 01:36:19 -07001354 flash_dmabuf_disable_flag = 0;
1355 flush_dcache_all();
1356
1357 return 0;
1358}
1359
xf.li7ccf8372024-03-07 00:08:02 -08001360int write_flags(T_FLAGS_INFO *fotaFlagInfo)
1361{
1362 uint32_t off = 0;
1363 uint32_t flags_size = 0;/*flags·ÖÇø2M*/
1364 uint32_t blockNum = 0;
1365 uint32_t work_area_offset = 0;
1366 uint32_t backup_area_offset = 0;
1367 int ret = 0;
1368 int type = 0;
1369 uchar * flags_name = "flags";
1370 u32 fota_size = sizeof(T_FLAGS_INFO);
1371 //fota_size = page_align(fota_size);
1372 unsigned long crc = 0;
1373 u32 crc_size = sizeof(T_FLAGS_INFO);
1374 char name[IMG_NAME_LEN];
1375
1376 nand_info_t *nand = &nand_info[nand_curr_device];
1377 struct fsl_qspi *nor = &spi_nor_flash;
1378 uint32_t part_offset = 0;
1379 struct flash_ops *flash = NULL;
1380 nand_erase_options_t opts;
1381
1382 flush_dcache_all();
1383 //flash_dmabuf_disable_flag = 1;
1384
1385 flash = get_flash_ops();
1386 type = read_boot_flashtype();
1387
1388 /* ѰÕÒ·ÖÇø */
1389 partition_entry_t * entry = find_partition_para(flags_name);
1390 if( entry == NULL )
1391 {
1392 BOOT_PRINTF(UBOOT_ERR, "[%s]: can't find the partition...\n", flags_name);
1393 return -1;
1394 }
1395 /* »ñµÃ·ÖÇøÊ×µØÖ·ºÍ´óС */
1396 part_offset = entry->part_offset;
1397 flags_size = entry->part_size;
1398
1399 /*crc32*/
1400 memset(name, 0xff, IMG_NAME_LEN);
1401 if(!memcmp(fotaFlagInfo->img_size[0].name, name, IMG_NAME_LEN)){
1402 printf("the image name is null.\n");
1403 fotaFlagInfo->crc32_1 = 0;
1404 memset((unsigned char*)(fotaFlagInfo->img_size[0].name), 0, (crc_size - 512));
1405 crc32init_le();
1406 crc = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
1407 fotaFlagInfo->crc32_1 = crc;
1408 printf("flags partition crc32_1 is 0x%x\n",crc);
1409
1410 }else{
1411 fotaFlagInfo->crc32 = 0;
1412 crc32init_le();
1413 crc = crc32_le(0,(unsigned char*)fotaFlagInfo,512);
1414 fotaFlagInfo->crc32 = crc;
1415
1416 fotaFlagInfo->crc32_1 = 0;
1417 crc32init_le();
1418 crc = crc32_le(0,(unsigned char*)fotaFlagInfo,crc_size);
1419 fotaFlagInfo->crc32_1 = crc;
1420 printf("flags partition crc32 is 0x%x,crc32_1 is 0x%x\n",fotaFlagInfo->crc32,fotaFlagInfo->crc32_1);
1421 }
1422
1423 /*дÈëflags·ÖÇø*/
1424
1425 /*È·¶¨¹¤×÷ÇøºÍ±¸·ÝÇøÆ«ÒÆµØÖ·*/
1426 for (off = part_offset; off < part_offset+flags_size; off += nand->erasesize)
1427 {
1428 if (!(nand_block_isbad(nand,off)))
1429 blockNum += 1;
1430
1431 if(blockNum == 1)
1432 work_area_offset = off;
1433
1434 else if(blockNum == 2)
1435 {
1436 backup_area_offset = off;
1437 break;
1438 }
1439 }
1440
1441 if(blockNum < 2)
1442 {
1443 printf("flags partition have not enough space!\n");
1444
1445 return -1;
1446 }
1447
1448 //printf("work_area_offset is 0x%x,backup_area_offset is 0x%x,crc_size is 0x%x\n",work_area_offset,backup_area_offset,crc_size);
1449
1450 /*²Á³ýflagsÖ÷·ÖÇø*/
1451 memset(&opts, 0, sizeof(opts));
1452 opts.offset = work_area_offset;
1453 opts.length = fota_size;
1454 ret = flash->erase(nand,&opts);
1455
1456 if( ret != 0 )
1457 {
1458 BOOT_PRINTF(UBOOT_ERR, "[%s]: erase the flags imagefs work area error!\n", flags_name);
1459 return 1;
1460 }
1461
1462 ret = flash->write(nand,(loff_t)(work_area_offset),
1463 &fota_size,(u_char *)fotaFlagInfo,0);
1464
1465 if( ret != 0 )
1466 {
1467 BOOT_PRINTF(UBOOT_ERR, "[%s]: write the flags imagefs work area error!\n", flags_name);
1468 return 1;
1469 }
1470
1471 /*²Á³ýflags±¸ÓÃÇø*/
1472 memset(&opts, 0, sizeof(opts));
1473 opts.offset = backup_area_offset;
1474 opts.length = fota_size;
1475 ret += flash->erase(nand,&opts);
1476
1477 if( ret != 0 )
1478 {
1479 BOOT_PRINTF(UBOOT_ERR, "[%s]: erase the flags imagefs backup area error!\n", flags_name);
1480 return 1;
1481 }
1482
1483 ret += flash->write(nand,(loff_t)(backup_area_offset),
1484 &fota_size,(u_char *)fotaFlagInfo,0);
1485 if( ret != 0 )
1486 {
1487 BOOT_PRINTF(UBOOT_ERR, "[%s]: write the flags imagefs backup area error!\n", flags_name);
1488 return 1;
1489 }
1490
1491 flash_dmabuf_disable_flag = 0;
1492 flush_dcache_all();
1493 return 0;
1494}
1495
lh9ed821d2023-04-07 01:36:19 -07001496#endif
1497/* ================================================================================
1498 * get_fota_update_flag :
1499 * @return 0 : not fota update
1500 * @return 1 : fota update
1501 * @return -1 : error
1502 */
1503int get_fota_update_flag( void )
1504{
1505 char cmd[64] = {0};
1506//#ifdef CONFIG_ZX297520V3E_MDL_AB
xf.li6c8fc1e2023-08-12 00:11:09 -07001507#if defined(CONFIG_ZX297520V3E_MDL_AB) || defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
lh9ed821d2023-04-07 01:36:19 -07001508 if(imagefs_flag == 1)
1509 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
1510 else
1511 sprintf(cmd, "fsload imagefs2 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
1512#else
1513 sprintf(cmd, "fsload imagefs 0x%x %s", (ulong)CONFIG_SYS_SDRAM_TEMP_BASE, FOTAFLAG_PATH);
1514#endif
1515 run_command(cmd, 0);
1516
1517 if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_FOTA_FLAG, strlen(ARM_FOTA_FLAG)) == 0)
1518 {
1519 fota_upflag = FOTA_UPDATE;
1520 }
1521 else if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_LOCAL_UPDATE_FLAG, strlen(ARM_LOCAL_UPDATE_FLAG)) == 0)
1522 {
1523 fota_upflag = FOTA_LOCALUPDATE;
1524 }
1525 else if( strncmp( (char *)CONFIG_SYS_SDRAM_TEMP_BASE, (char *)ARM_RECOVERY_FLAG, strlen(ARM_RECOVERY_FLAG)) == 0)
1526 {
1527 fota_upflag = FOTA_RECOVERY;
1528 }
1529 else
1530 {
1531 fota_upflag = FOTA_NORMAL;
1532 }
1533
1534 BOOT_PRINTF(UBOOT_NOTICE, "fota_upflag=%d\n", fota_upflag);
1535
1536 return 0;
1537}
1538
1539/* ================================================================================
1540 * get_gmac_init_flag :
1541 * @return 0 :
1542 * @return 1 :
1543 * @return -1 :
1544 */
1545int get_gmac_init_flag( void )
1546{
1547 //hsy
1548 return 0;
1549}
1550
1551/* ================================================================================
1552 * read_gmac_init_flag :
1553 * @return 0 : not init
1554 * @return 1 : init
1555 */
1556int read_gmac_init_flag( void )
1557{
1558 return g_gmac_init_flag;
1559}
1560
1561/* ================================================================================
1562 * read_gmac_init_overtime :
1563 * @return : Ãë
1564 */
1565int read_gmac_init_overtime( void )
1566{
1567 return g_gmac_init_overtime;
1568}
1569
1570/* ================================================================================
1571 * read_fota_update_flag :
1572 * @return 0 : not fota update
1573 * @return 1 : fota update
1574 */
1575int read_fota_update_flag( void )
1576{
1577 return fota_upflag;
1578}
1579
1580/* ================================================================================
1581 * read_fota_update_flag :
1582 * @return 0 : not fota update
1583 * @return 1 : fota update
1584 */
1585
1586int read_fota_psup_flag( void )
1587{
1588 return fota_psup_flag;
1589}
1590
1591/* ================================================================================
1592 * start_arm_ps : Æô¶¯ ARM_PS °æ±¾
1593 */
1594void start_arm_ps( void )
1595{
1596 void (*ps_start)();
1597
1598 printf("Starting the arm_ps ...\n");
1599 ps_start = (void (*)())arm_ps_ep;
1600 ps_start();
1601}
1602
1603/* ================================================================================
1604 * start_arm_phy : Æô¶¯ ARM_PHY °æ±¾
1605 */
1606void start_arm_phy( void )
1607{
1608 /* д PHY Ìø×ªÆô¶¯´úÂë */
1609 writel(0xE59ff000, SYS_IRAM3_BASE);
1610 writel(arm_phy_ep, SYS_IRAM3_BASE + 8);
1611
1612 /* ÊÍ·ÅÆô¶¯ ARM_PHY */
1613 printf("Starting the arm_phy ...\n");
1614 writel(CPU_PHY_SW_RSTEN, CPU_PHY_SUBSYS_CFG);
1615}
1616
1617/* ================================================================================
1618 * start_cpucap_cores : Æô¶¯°æ±¾
1619 */
1620void start_cpucap_cores( void )
1621{
1622 writel(0xE59ff000, SYS_CPUCAP_BOOT_BASE);
1623 writel(arm_cpucap_ep, SYS_CPUCAP_BOOT_BASE + 8);
1624 printf("cap addr is 0x%x\n",arm_cpucap_ep);
1625 BOOT_PRINTF(UBOOT_NOTICE, "Starting the cpu cap ...\n");
1626 writel(CPU_CAP_SW_RSTEN, CPU_CAP_SUBSYS_CFG);
1627}
1628
1629static void usdelay(volatile int count)
1630{
1631 volatile int cnt = 0;
1632 count =count *8;
1633
1634 while(cnt<count)
1635 {
1636 cnt++;
1637 }
1638 return;
1639}
1640void cap_poweron(void)
1641{
1642 u32 tmp;
1643
1644 BOOT_PRINTF(UBOOT_NOTICE, "cap_poweron ...\n");
1645
1646 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg on*/
1647 tmp |= (0x1<<3);
1648 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1649
1650 usdelay(1);
1651
1652 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg rst*/
1653 tmp &= ~(0x1<<4);
1654 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1655
1656 usdelay(1);
1657
1658 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG2) ;/*ap mg iso*/
1659 tmp &= ~(0x1<<5);
1660 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG2);
1661
1662 usdelay(1);
1663
1664 tmp = readl(CORE_OUTPUT_SW_CONFIG_REG1) ;/*ap clk on*/
1665 tmp |= (0x1<<2);
1666 writel(tmp, CORE_OUTPUT_SW_CONFIG_REG1);
1667
1668 tmp = readl(CORE_OUTPUT_SWITCH_CONFIG_REG) ; /*ap clk&mg control by hw*/
1669 tmp |= ((0x1<<2)|(0x1<<5));
1670 writel(tmp, CORE_OUTPUT_SWITCH_CONFIG_REG);
1671 //__REG(0x0013a0ac) |= ((0x1<<2)|(0x1<<5)); /*ap clk&mg control by hw*/
1672 //__REG(0x0013a0bc) |= (0x1<<3); /*ap mg off*/
1673 //__REG(0x0013a0bc) &= ~(0x1<<4); /*ap mg rst*/
1674 //__REG(0x0013a0bc) &= ~(0x1<<5); /*ap mg iso*/
1675 //__REG(0x0013a0b8) |= (0x1<<2); /*ap clk off*/
1676
1677}
1678
1679/* ================================================================================
1680 * read_sys_ddr_kernel_start : »ñÈ¡ kernel ÔËÐпռä Ê×µØÖ·
1681 */
1682uint32_t read_sys_ddr_kernel_start(void)
1683{
1684 /* Get Addr from Image Header. */
1685 return sys_ddr_kernel_start;
1686}
1687
1688/* ================================================================================
1689 * update_led_enable : enable led
1690 * @return 0 : ³É¹¦
1691 * @return 1 : ʧ°Ü
1692 */
1693int update_led_enable(int enable)
1694{
1695 int ret = 0;
1696
1697 if(enable)
1698 {
1699#ifdef CONFIG_BOARD_7520_UFI_956
1700 ret = sn3216_SetStatus(LED_CHANNEL_WAN_GREEN,SN3216_LED_STATUS_ON);
1701 ret = sn3216_SetStatus(LED_CHANNEL_LAN_GREEN,SN3216_LED_STATUS_ON);
1702 ret = sn3216_SetStatus(LED_CHANNEL_BAT_GREEN,SN3216_LED_STATUS_ON);
1703 ret = sn3216_SetStatus(LED_CHANNEL_SMS_GREEN,SN3216_LED_STATUS_ON);
1704#endif
1705 }
1706 else
1707 {
1708#ifdef CONFIG_BOARD_7520_UFI_956
1709 ret = sn3216_SetStatus(LED_CHANNEL_WAN_GREEN,SN3216_LED_STATUS_OFF);
1710 ret = sn3216_SetStatus(LED_CHANNEL_LAN_GREEN,SN3216_LED_STATUS_OFF);
1711 ret = sn3216_SetStatus(LED_CHANNEL_BAT_GREEN,SN3216_LED_STATUS_OFF);
1712 ret = sn3216_SetStatus(LED_CHANNEL_SMS_GREEN,SN3216_LED_STATUS_OFF);
1713#endif
1714 }
1715 return ret;
1716}
1717
1718/* ================================================================================
1719 * update_led_twinkle : config led in local update
1720 * @return 0 : ³É¹¦
1721 * @return 1 : ʧ°Ü
1722 */
1723void update_led_twinkle(void)
1724{
1725 int start_time = 0;
1726 if(update_start_time)
1727 {
1728 update_recent_time = get_timer(0);
1729 if(update_recent_time-update_start_time > 6000000) /*1s*/
1730 {
1731 led_state = (~led_state)& 0x00000001;
1732 update_led_enable(led_state); /*LED on/off*/
1733 update_start_time = update_recent_time;
1734 }
1735 }
1736 return ;
1737}
1738
1739/* ================================================================================
1740 * copy_write_part : write data to flash
1741 * @return 0 : ³É¹¦
1742 * @return 1 : ʧ°Ü
1743 */
1744int copy_write_part_gmac(int cnt)
1745{
1746 int ret = 0;
1747 nand_info_t *nand = &nand_info[nand_curr_device];
1748 image_bin_header_t * img_head = &(master_head->image[cnt]);
1749 int filesize = img_head->filelength;
1750 char *partname = (char *)(img_head->partitonname);
1751 int bin_offset = img_head->fileaddr;
1752 int part_offset = img_head->partitonoffset;
1753 //uchar * buf = malloc(12*1024);
1754 uchar *buf = NULL;
1755 partition_entry_t * part = NULL;
1756
1757 buf = kzalloc(12*1024, GFP_KERNEL);
1758 if(buf == NULL)
1759 {
1760 return -1;
1761 }
1762
1763 part = find_partition_para((uchar *)partname);
1764 if(part == NULL)
1765 {
1766 return -1;
1767 }
1768
1769 /* µ±Ç°ÊÇÔÚµçÄÔÉÏÖ´ÐУ¬Ã»ÓеôµçΣÏÕ£¬ËùÒÔ¿ÉÒÔ²Á³ýzloader */
1770 /* zloaderÓÐÁ½²¿·Ö×é³É: 8kµÄbin + 4kµÄ·ÖÇø±í */
1771 if(!strcmp(partname, "zloader"))
1772 {
1773 BOOT_PRINTF(UBOOT_NOTICE, "write part %s, offset=0x%x, filesize=0x%x.\n",
1774 partname, part->part_offset, filesize);
1775 ret = downloader_nand_erase(part,part->part_size);
1776
1777 //memcpy(buf,(u_char *)(CONFIG_SYS_SDRAM_BASE+bin_offset),8192);//zloader
1778 memcpy(buf, (u_char *)(CONFIG_LOADADDR+bin_offset), 8192);//zloader
1779 memcpy((char *)(buf+8192), g_partition_table, 4096);//part
1780
1781 int times = 12*1024/nand->writesize;
1782 int i;
1783 for(i = 0; i < times; i++)
1784 {
1785 ret += nand_write_page_with_no_ecc(nand,
1786 ((loff_t)i*(nand->writesize)),
1787 buf );
1788 buf += nand->writesize;
1789 }
1790 // free(buf);
1791 return ret;
1792 }
1793
1794 if(strcmp(img_head->partitontype,"nand") == 0)
1795 {
1796 BOOT_PRINTF(UBOOT_NOTICE, "write part %s, offset=0x%x, filesize=0x%x\n",
1797 partname, part->part_offset, filesize);
1798 ret = downloader_nand_erase(part,part->part_size);
1799 //ret = nand_write_skip_bad(nand, part->part_offset, &filesize, (u_char *)(CONFIG_SYS_SDRAM_BASE+bin_offset),0);
1800 ret = nand_write_skip_bad(nand, part->part_offset, &filesize, (u_char *)(CONFIG_LOADADDR+bin_offset),0);
1801
1802 }
1803
1804 return ret;
1805}
1806
1807/* ================================================================================
1808 * copy_ddr_allbin : webui local update
1809 * @return 0 : ³É¹¦
1810 * @return 1 : ʧ°Ü
1811 */
1812int copy_ddr_allbin(void)
1813{
1814 int ret = 0;
1815 int i = 0;
1816 int head_size = 0x2000; //total 6608 bytes
1817 nand_info_t *nand = &nand_info[nand_curr_device];
1818
1819 master_head = kzalloc(0x2000, GFP_KERNEL);
1820 if(master_head == NULL)
1821 {
1822 BOOT_PRINTF(UBOOT_ERR, "kzalloc fail!\n");
1823 return -1;
1824 }
1825
1826 memcpy(( u_char *)master_head,(char *)CONFIG_LOADADDR, head_size);
1827
1828 BOOT_PRINTF(UBOOT_NOTICE, "file number = %d\n", master_head->dwFileNumber);
1829
1830 for(i = 0; i < (sizeof(master_head->image)/sizeof(image_bin_header_t))
1831 && i < master_head->dwFileNumber; i++) /* update each partition */
1832 {
1833 ret = copy_write_part_gmac(i);
1834 if(ret != 0)
1835 {
1836 BOOT_PRINTF(UBOOT_ERR, "write from ddr to nand ERROR !!!\n");
1837 return -1;
1838 }
1839 }
1840
1841 return 0;
1842}
1843