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