bl_ImageLoader.c 43.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   bl_ImageLoader.c
 *
 * Project:
 * --------
 *   NFB - Bootloader
 *
 * Description:
 * ------------
 *   Reponsible for loading image.
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 * removed!
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/
 
#include "kal_general_types.h" 

#if defined(__FOTA_DM__)
#include "fue_err.h"
#include "fota.h"
#include "custom_fota.h"
#include "custom_fota.h"
#include "custom_img_config.h"
#include "SSF_fue_support.h"
#include "fue.h"
#endif /* __FOTA_DM__ */

#include "bl_setting.h"
#include "bl_Init.h"
#include "bl_FOTA.h"
#include "bl_nfi_wrapper.h"
#include "bl_loader.h"
#include <string.h>
#include "SST_secure.h"

#include "bl_common.h"

#include "br_time_stamp.h"
#ifdef __TIME_STAMP__
#include "br_time_stamp_cb.h"
#include "bl_time_stamp_id.h"
#endif

#if defined(__EXT_BL_UPDATE__)
#include "custom_img_config.h"
#endif

#include "SSF_ROMInfo_type.h"

#ifndef __SV5_ENABLED__ 


/*************************************************************************
 * Global variables definition
*************************************************************************/
#define MAX_BOOTLOADER_SIZE 1024*1024

#if defined(_NAND_FLASH_BOOTING_)

#define TOTAL_BIN_PACK   20

const kal_char exblID[] = "EXT_BOOTLOADER!";

const kal_char   SUPER_BLOCK_PATTERN[]             = "MTK_FOTA_SUPER_BLOCK_HEADER_V01";
const kal_uint32 IMAGE_LIST_BLOCK_TAIL_PATTERN[]   = MTK_ILB_TAIL_TAG;
const kal_uint32 IMAGE_LIST_BLOCK_TEMP_PATTERN[]   = MTK_ILB_TEMP_TAG;
const kal_uint32 IMAGE_LIST_BLOCK_BACKUP_PATTERN[] = MTK_ILB_BAK_TAG;
const kal_uint32 IMAGE_LIST_BLOCK_FOTA_PATTERN[]   = MTK_ILB_FOTA_TAG;
const kal_uint32 IMAGE_LIST_BLOCK_DLPKG_PATTERN[]  = MTK_ILB_DLPKG_TAG;

/* FOTA on ENFB */
const kal_char IMAGE_ID_FOTA[]  = "FOTA";
const kal_char IMAGE_ID_ENFB[]  = "ENFB";
const kal_char IMAGE_ID_MTK[]   = "@MTK";
const kal_char IMAGE_ID_VERNO[] = "V01";

EXT_BOOTL_HEADER EBLHeader;

#endif /* _NAND_FLASH_BOOTING_ */

#if defined(__EXT_BL_UPDATE__)
const kal_uint32 EXT_BL_END_MARK[] = MTK_EXT_BL_END_MARK;
#endif /* __EXT_BL_UPDATE__ */

const kal_char BL_TAIL_PATTERN[] = "MTK_BL_ROMINFO_TAIL_V";
kal_uint32 g_ExtBootloaderLen = 0;

/*************************************************************************
 * Macro and const definition
 *************************************************************************/
#define IMGBLK_LOAD_ADDR (SYSRAM_BASE+0x1000)

/*************************************************************************
 * External reference definition
 *************************************************************************/
extern kal_bool bl_secure_nfb_usbdl;

extern const kal_uint32 CHECKSUM_SEED;
extern _RET_CODE NFI_PhyPageRead(kal_uint32 *destination, kal_uint32 *parity, kal_uint32 length,
                       kal_uint16 addr_no, kal_uint32 addr1, kal_uint16 addr2, kal_uint16 pageSize,
                       kal_bool ECC, kal_bool continous);

extern BL_Info_Wrapper_st BL_Shared_info;

extern const kal_char blID1[];
extern const kal_char blID2[];
extern kal_uint32 g_bootupStatus;
 
/*************************************************************************
 * Forward declaration
 *************************************************************************/
kal_int32 RelocateExtBootloader(kal_uint32 srcAddr, kal_uint32 srcLen, kal_uint32 searchAddr, kal_uint32 *pExtBootloaderLen);
kal_bool CheckExtBLChecksum(kal_uint32 eblAddr, kal_uint32 eblLen);


/*************************************************************************
 * Code region
 *************************************************************************/

#if defined(_NAND_FLASH_BOOTING_)
/**********************************************************
Description : Get the item in image list
Input       : None
Output      : None
***********************************************************/
Nand_ImageInfo_S *GetImageInfo(kal_uint32 n)
{
   if (n < 10)
      return &BL_Shared_info.m_bl_image_list.m_image[n];
   else
      return &BL_Shared_info.m_bl_image_list.m_image_ext[n-10];
}

/**********************************************************
Description : Get the first block next to bootloader region
Input       : None
Output      : None
***********************************************************/

kal_uint32 GetMainRegionStartBlock()
{
   return EBLHeader.lastLogicalBlock+1;
}

/**********************************************************
Description : Procedure to read NAND pages with logical addresses
Input       : None
Output      : None
***********************************************************/

kal_bool ReadPage(kal_uint32 *dest, kal_uint32 length, kal_uint32 pageNo, kal_uint32 type)
{
   _RET_CODE status;
   kal_uint32 spare[64>>2];
   kal_uint32 addr1 = pageNo << PAGE_SHIFT;
   kal_uint16 addr2 = 0;

   BL_DEBUG_PRINT("ReadPage dest=%x, pNo=%x, len=%d, type=%d\n\r", dest, pageNo, length, type);

   if ( BLHeader.NFIinfo.addressCycle>4 )
   {
      if ( pageNo&0xffff0000 )
         addr2 = (kal_uint32)(pageNo >> PAGE_SHIFT);
   }

#ifdef BIT_ERROR_TEST
   EmulateBitError(dest, spare, BLHeader.NFIinfo.addressCycle, addr1, addr2, BLHeader.NFIinfo.pageSize);
#endif

#ifdef BL_ECC_ENABLE

#ifdef __EXT_BOOTLOADER__
   if ( type == LOGICAL_BLOCK_ADDRESS )
   {
      status = NFI_PageRead(dest, spare, BLHeader.NFIinfo.pageSize, \
                            BLHeader.NFIinfo.addressCycle, addr1, addr2, \
                            BLHeader.NFIinfo.pageSize, KAL_TRUE, KAL_FALSE);
   }
   else
#endif /* __EXT_BOOTLOADER__ */  
   {
      status = NFI_PhyPageRead(dest, spare, BLHeader.NFIinfo.pageSize, \
                            BLHeader.NFIinfo.addressCycle, addr1, addr2, \
                            BLHeader.NFIinfo.pageSize, KAL_TRUE, KAL_FALSE);
   }

   if ( (status!=NFI_SUCCESS) && (status!=NFI_ECC_1BIT_CORRECT))
      _errorExit(status, pageNo, dest);
   
   #if (!defined (__NFI_VERSION2__) && !defined (__NFI_VERSION3__) && !defined(__NFI_VERSION3_1__))
   status = NFI_ParityCheck(dest, spare, BLHeader.NFIinfo.addressCycle, addr1, addr2, \
                            BLHeader.NFIinfo.pageSize, BLHeader.NFIinfo.IOInterface, \
                            BLHeader.NFIinfo.pageSize*BLHeader.pagesPerBlock);                          

   if (status == NFI_ECC_2BITS_ERR)
      return KAL_FALSE;
   
   if ( status<0)
      _errorExit(status, pageNo, dest);
   #endif
#else

#ifdef __EXT_BOOTLOADER__
   if ( type == LOGICAL_BLOCK_ADDRESS )
   {
      status = NFI_PageRead(dest, spare, BLHeader.NFIinfo.pageSize, \
                            BLHeader.NFIinfo.addressCycle, addr1, addr2, \
                            BLHeader.NFIinfo.pageSize, KAL_FALSE, KAL_FALSE);
   }
   else
#endif /* __EXT_BOOTLOADER__ */
   {
      status = NFI_PhyPageRead(dest, spare, BLHeader.NFIinfo.pageSize, \
                            BLHeader.NFIinfo.addressCycle, addr1, addr2, \
                            BLHeader.NFIinfo.pageSize, KAL_FALSE, KAL_FALSE);
   }

   if (status == NFI_ECC_2BITS_ERR)
      return KAL_FALSE;
   
   if ( status!=NFI_SUCCESS )
      _errorExit(status, pageNo, dest);
#endif /* BL_ECC_ENABLE */

   return KAL_TRUE;

}



/**********************************************************
Description : To remove some information before regionInit, or it will be destroyed
Input       : None
Output      : None
***********************************************************/

void PreserveExtBootloaderHeader()
{
   //Search and copy the EXT_Bootloader header to a temp space, or it may be destroyed in Region Init
   extern kal_uint32 Load$$DUMMY_END$$Base;
   extern kal_uint32 Image$$DUMMY_END$$Length;   

   //Search from the end of bootloader
   kal_uint32 *p = (kal_uint32*)((kal_uint32)(&Load$$DUMMY_END$$Base) + (kal_uint32)(&Image$$DUMMY_END$$Length));
   kal_uint32 *end = p + (512>>2);

   kal_uint32 *pEBLHAddr = ((kal_uint32*)BOOTLOADER_HEADER_ADDRESS) + (sizeof(BOOTL_HEADER)>>2);
   
   for (; p<end; p++)
   {
      if (*(p+0) == *((kal_uint32*)exblID+0) &&
          *(p+1) == *((kal_uint32*)exblID+1) &&
          *(p+2) == *((kal_uint32*)exblID+2) &&
          *(p+3) == *((kal_uint32*)exblID+3) )
      {
         end = p + (sizeof(EXT_BOOTL_HEADER)>>2);
         for (; p<end; p++, pEBLHAddr++)
         {
            *pEBLHAddr = *p;
         }
         return;
      }
   }

   //Not found
   *pEBLHAddr = 0x00587858;   //Debug tag
}



kal_uint32 LoadExtBootloaderBody(kal_uint32 *pBlLoadBase, EXT_BOOTL_LINKADDR *pLinkAddr, kal_uint32 linkAddrCount, kal_uint32 *pLen)
{
   kal_uint32 i;
   kal_uint32 curPosition; /* in terms of pages */
   kal_uint32 start_addr = INVALID_START_ADDR;
   kal_uint32 len = MAX_BOOTLOADER_SIZE;   
   kal_uint32 pagePerBlockShift = (BLHeader.blockShift - PAGE_SHIFT);
   kal_uint32 read;
   
   for (read=0, i=0; i<linkAddrCount && pLinkAddr[i].blockNo && read<len; i++)
   {
      kal_uint32 toRead;
      kal_uint32 j = BLHeader.pagesPerBlock;
      
      WacthDogRestart();

      if(NFB_CheckGoodBlock_NoTT(&BLHeader.NFIinfo, pLinkAddr[i].blockNo) != 0)
      {
         BL_PRINT(LOG_INFO, "Bad block found @ %d\n\r", pLinkAddr[i].blockNo);
         continue;
      }

      curPosition = (pLinkAddr[i].blockNo << pagePerBlockShift);

      while(j && read<len)
      {
         if (len-read > BLHeader.NFIinfo.pageSize)
         {
            toRead = BLHeader.NFIinfo.pageSize;
         }
         else
         {
            toRead = len-read;
         }

         if(ReadPage(pBlLoadBase+read/4, toRead, curPosition, PHYSICAL_BLOCK_ADDRESS) == KAL_FALSE)
         {
            BL_PRINT(LOG_ERROR, "Load ExtBootloader failed, flash error?\n\r");
            return INVALID_START_ADDR;
         }

         if(start_addr == INVALID_START_ADDR)
         {
            start_addr = RelocateExtBootloader((kal_uint32)pBlLoadBase, read+toRead, read, &len);
            
            if(start_addr != INVALID_START_ADDR)
            {
               pBlLoadBase = (kal_uint32*)start_addr;
            }
         }

         read += toRead;

         j--;
         curPosition++;
      }
   }

   if (read != len)
   {
      if(start_addr == INVALID_START_ADDR)
      {
         BL_PRINT(LOG_ERROR, "BL_ROM_INFO not found yet\n\r");
      }
      
      BL_PRINT(LOG_ERROR, "Failed when load ExtBootloader!, blAddr=%x, blLen=%d, read=%d\n\r", start_addr, len, read);
      
      return INVALID_START_ADDR;
   }  

   if(pLen)
   {
      *pLen = read;
   }
   
   return start_addr;
}

#if defined(__EXT_BL_UPDATE__)

kal_bool FindAndCompareExtBootloaderEndMark(EXT_BOOTL_LINKADDR *pExtBlLinkAddr)
{
   kal_int32 i;
   for(i=0; i<sizeof(EBLHeader.linkAddr)/sizeof(*EBLHeader.linkAddr) && pExtBlLinkAddr[i].blockNo; i++)
   {
      ;
   }

   for(i--; i>=0; i--)
   {
      if(NFB_CheckGoodBlock_NoTT(&BLHeader.NFIinfo, pExtBlLinkAddr[i].blockNo) == 0)
      {
         //Read the last page and check the mark
         kal_uint32 pagePerBlockShift = (BLHeader.blockShift - PAGE_SHIFT);
         kal_uint32 curPosition = ((pExtBlLinkAddr[i].blockNo+1) << pagePerBlockShift)-1;

         if(ReadPage((kal_uint32*)IMGBLK_LOAD_ADDR, BLHeader.NFIinfo.pageSize, curPosition, PHYSICAL_BLOCK_ADDRESS) == KAL_TRUE)
         {
            if(CompareEndMarkWithError((kal_uint32*)(IMGBLK_LOAD_ADDR+BLHeader.NFIinfo.pageSize-sizeof(EXT_BL_END_MARK)), EXT_BL_END_MARK, BIT_ERROR_THRESHOLD) == KAL_TRUE)
            {
               return KAL_TRUE;
            }
         }
      }
   }

   return KAL_FALSE;
}
#endif /* __EXT_BL_UPDATE__ */

/**********************************************************
Description : Find and load Ext Bootloader to external RAM
Input       : None
Output      : None
***********************************************************/

kal_int32 LoadExtBootloader(void)
{
   kal_uint32 i;
   kal_uint32 start_addr = INVALID_START_ADDR;
   kal_uint32 *ptr_source;
   kal_uint32 *ptr_dest;
   kal_uint32 len;

   TS_BEGIN(TSID_BL_LOAD_EXT_BOOTLOADER);

   BL_DEBUG_PRINT("LoadExBootloader %s\n\r", BootLDVerno);
  
   // Copy the Bootloader Header from predefined space
   ptr_source = (kal_uint32 *)(BOOTLOADER_HEADER_ADDRESS);
   ptr_dest = (kal_uint32 *)&BLHeader;
   for (i=0; i<(sizeof(BLHeader)>>2); i++)  // Need to make sure BLHeader is 4 bytes multiple.
      *ptr_dest++ = *ptr_source++;

   // Copy the EXT Bootloader Header from backup space
   ptr_dest = (kal_uint32 *)&EBLHeader;
   for (i=0; i<(sizeof(EBLHeader)>>2); i++)
      *ptr_dest++ = *ptr_source++;

   if ( strcmp(blID1, BLHeader.ID1) || strcmp(blID2, BLHeader.ID2) )
   {
      BL_PRINT(LOG_ERROR, "\nBootloader Header ID incorrect, quit!\n\r");
      BL_PRINT(LOG_ERROR, "\nID1(default %s) = %s\n\r", blID1, BLHeader.ID1);
      BL_PRINT(LOG_ERROR, "\nID2(default %s) = %s\n\n\n\r", blID2, BLHeader.ID2);
      return INVALID_START_ADDR;
   }

#if defined(__NFI_VERSION3__) || defined(__NFI_VERSION3_1__)
	{
	    NFI_SUBMENU   sub_menu;
	    kal_uint32    sec_num = 0;
	
     /* configure NFI according to extracted bootloader header */
     sec_num = BLHeader.NFIinfo.pageSize>>NAND_SECTOR_SHIFT;
     sub_menu.spareSize = BLHeader.spareSize/(sec_num ? sec_num : 1);
     sub_menu.blockSize = BLHeader.pagesPerBlock;
     NFI_Config(&BLHeader.NFIinfo, &sub_menu);   
	}
   
#endif
   
   if (strcmp(EBLHeader.ID, exblID) == 0)
   {
      kal_uint32 *pBlLoadBase;
      EXT_BOOTL_LINKADDR *pExtBlLinkAddr = EBLHeader.linkAddr;

      BL_DEBUG_PRINT("ExtBootloader Info:\n\r");
      BL_DEBUG_PRINT(" ID=%s\n\r version=%s\n\r", EBLHeader.ID, EBLHeader.version);

#if defined(__EBL_UPDATE_TOOL_SUPPORT_NAND__)

      if(memcmp(EBLHeader.version, "V006", 4) != 0)
      {
         BL_PRINT(LOG_ERROR, "EBL version mismatch(%d), possible old tool used?\r\n", BLHeader.version[3]);
         return INVALID_START_ADDR;
      }
   
#endif

      //Image list and Ex bootloader header are both ready. Go!
      BL_DEBUG_PRINT("Start loading ExtBootloader...\n\r");
      
      pBlLoadBase = (kal_uint32*)custom_EXTBL_baseaddr();

      //Find ExtBootloader and try to load it  
      for(i=0; i<2; i++)
      {
#if defined(__EXT_BL_UPDATE__)         
         if(FindAndCompareExtBootloaderEndMark(pExtBlLinkAddr) == KAL_FALSE)
         {
            BL_PRINT(LOG_INFO, "End mark is not found\r\n");
         }
         else
#endif /* __EXT_BL_UPDATE__ */
         {
            TS_BEGIN(TSID_BL_LOAD_EXT_BOOTLOADER_BODY);
            
            start_addr = LoadExtBootloaderBody(pBlLoadBase, pExtBlLinkAddr, sizeof(EBLHeader.linkAddr)/sizeof(*EBLHeader.linkAddr), &len);

            TS_END(TSID_BL_LOAD_EXT_BOOTLOADER_BODY);
            TS_INFO(TSID_BL_LOAD_EXT_BOOTLOADER_BODY, 0, read, 0);

            if(start_addr != INVALID_START_ADDR)
            {
               if(CheckExtBLChecksum(start_addr, len) == KAL_TRUE)
               {
#if !defined(__EBL_UPDATE_TOOL_SUPPORT_NAND__)
                  ASSERT(len == EBLHeader.length); //BL_TODO: Sanity test
#endif
               }
               else
               {
                  BL_PRINT(LOG_INFO, "Bad ExtBL checksum\r\n");
                  start_addr = INVALID_START_ADDR;
               }
            }
         }

#if defined(__EXT_BL_UPDATE__)

#ifdef __EBL_UPDATE_TOOL_SUPPORT_NAND__
         if(start_addr != INVALID_START_ADDR)
         {
            g_extBlUpdateInfo.m_current_bl          = (i==0) ? E_EXTBL_PRIMARY_REGION : E_EXTBL_BACKUP_REGION;
            g_extBlUpdateInfo.m_current_bl_size     = len;
            g_extBlUpdateInfo.m_ext_bl_addr         = EBLHeader.linkAddr[0].blockNo*BLOCK_SIZE;
            g_extBlUpdateInfo.m_ext_bl_max_size     = BL_IMG_MAX_SIZE;
            g_extBlUpdateInfo.m_ext_bl_bak_addr     = EBLHeader.linkAddrBak[0].blockNo*BLOCK_SIZE;
            g_extBlUpdateInfo.m_ext_bl_bak_max_size = BL_IMG_MAX_SIZE;
            break;
         }
         else
         {
            pExtBlLinkAddr = EBLHeader.linkAddrBak;
            BL_PRINT(LOG_INFO, "No valid ExtBl exists at region %d...\r\n", i);
         }
#else
         break;
#endif

#else
         //No need to to further process if ext bootloader is not going to be updated
         break;
#endif
      }
   }
   else
   {
      BL_PRINT(LOG_ERROR, "ExtBootloader Header ID incorrect, quit!\n\r");
      BL_PRINT(LOG_ERROR, "ID(default %s) = %s\n\r", exblID, EBLHeader.ID);
      return INVALID_START_ADDR;      
   }

   TS_END(TSID_BL_LOAD_EXT_BOOTLOADER);

   g_ExtBootloaderLen = len;

   return start_addr;
}


#else

kal_int32 LoadExtBootloader(void)
{
   kal_uint32 extblFlashAddr = custom_EXTBL_baseaddr();
   kal_uint32 extblAddr;
   kal_uint32 eblLen;
   kal_int32 i;

#if !defined(__EXT_BL_UPDATE__)

   extblAddr = RelocateExtBootloader(extblFlashAddr, MAX_BOOTLOADER_SIZE, extblFlashAddr, &eblLen);

#else

   for(i=0; i<2; i++)
   {
      if(CompareEndMarkWithError((kal_uint32*)(extblFlashAddr+BL_IMG_MAX_SIZE-sizeof(EXT_BL_END_MARK)), EXT_BL_END_MARK, BIT_ERROR_THRESHOLD) == KAL_TRUE)
      {
         extblAddr = RelocateExtBootloader(extblFlashAddr, MAX_BOOTLOADER_SIZE, extblFlashAddr, &eblLen);         

         if(extblAddr != INVALID_START_ADDR &&
            CheckExtBLChecksum(extblAddr, eblLen) == KAL_TRUE)
         {
            g_extBlUpdateInfo.m_current_bl      = (i==0) ? E_EXTBL_PRIMARY_REGION : E_EXTBL_BACKUP_REGION;
            g_extBlUpdateInfo.m_current_bl_size = eblLen;
            g_extBlUpdateInfo.m_ext_bl_addr     = extblFlashAddr;
            g_extBlUpdateInfo.m_ext_bl_size     = BL_IMG_MAX_SIZE;
            g_extBlUpdateInfo.m_ext_bl_bak_addr = extblFlashAddr + BL_IMG_MAX_SIZE;
            g_extBlUpdateInfo.m_ext_bl_bak_size = BL_IMG_MAX_SIZE;
            break;
         }
      }

      extblAddr = INVALID_START_ADDR;
      extblFlashAddr += BL_IMG_MAX_SIZE;
      
      BL_PRINT(LOG_INFO, "No valid ExtBl exists at region %d...\r\n", i);
   }

#endif /* __EXT_BL_UPDATE__ */

   g_ExtBootloaderLen = eblLen;

   if(extblAddr == INVALID_START_ADDR)
   {
      BL_PRINT(LOG_ERROR, "Can not find ExtBootloader\r\n");
   }

   return extblAddr;
}

#endif /*_NAND_FLASH_BOOTING_*/


#if defined(__EXT_BL_UPDATE__) || defined(_NAND_FLASH_BOOTING_)

#pragma arm section code = "INTERNCODE", rodata = "INTERNDATA", rwdata = "INTERNDATA", zidata = "INTERNDATA"

static const kal_uint32 CRC_TBL_LEN = 256;
static kal_uint32 crc_init = 0;
static kal_uint32 crc_table[CRC_TBL_LEN]; /* Table of CRCs of all 8-bit messages. */

static void Build_CRC_TBL(void)
{
    kal_uint32 c;
    kal_int32 n, k;

    for (n = 0; n < CRC_TBL_LEN; n++)
    {

        c = (kal_uint32)n;

        for (k = 0; k < 8; k++)
        {

            if (c & 1)
            {

                c = 0xedb88320L ^ (c >> 1);
            }
            else
            {

                c = c >> 1;
            }
        }

        crc_table[n] = c;
    }
}

static kal_uint32 Update_CRC(kal_uint32 crc, kal_uint8 *buf, kal_uint32 len)
{
    kal_uint32 n, c = crc;
    kal_uint32 *p = (kal_uint32*)buf;
    
    //It's for checking ExtBL's integrity, which should be on 4B boundary
    ASSERT((kal_uint32)buf%4 == 0);
    
    if(!crc_init)
    {
       Build_CRC_TBL();
       crc_init = 702;
    }

    for (n = 0; n < len/4; n++)
    {
       kal_uint32 d = p[n];

       c = crc_table[(c ^ (d >>  0)) & 0xff] ^ (c >> 8);
       c = crc_table[(c ^ (d >>  8)) & 0xff] ^ (c >> 8);
       c = crc_table[(c ^ (d >> 16)) & 0xff] ^ (c >> 8);
       c = crc_table[(c ^ (d >> 24)) & 0xff] ^ (c >> 8);
    }
    
    for (n = 0; n < len%4; n++)
    {
        c = crc_table[(c ^ buf[(len/4)*4+n]) & 0xff] ^ (c >> 8);
    }

    return c;
}

#pragma arm section code, rodata, rwdata, zidata

kal_bool CheckExtBLChecksum(kal_uint32 eblAddr, kal_uint32 eblLen)
{
   kal_bool ret = KAL_FALSE;
   
   if(eblAddr != INVALID_START_ADDR)
   {
      //Check the CRC of the body of ExtBootloader
      kal_uint32 bodyLen = eblLen - sizeof(MTK_BL_ROMInfo_Tail_v1_ST);

      MTK_BL_ROMInfo_Tail_v1_ST *pTail = (MTK_BL_ROMInfo_Tail_v1_ST*)(eblAddr + bodyLen);

      if(memcmp(pTail->m_bl_header.m_super_identifier, BL_TAIL_PATTERN, sizeof(BL_TAIL_PATTERN)-1) == 0)
      {
         kal_uint32 calCrc;

         TS_BEGIN(TSID_CALC_EXT_BOOTLOADER_CRC);
            
         calCrc = Update_CRC(288, (kal_uint8*)eblAddr, bodyLen);

         TS_END(TSID_CALC_EXT_BOOTLOADER_CRC);
         TS_INFO(TSID_CALC_EXT_BOOTLOADER_CRC, 0, bodyLen, eblAddr);         

         if(calCrc == pTail->m_bl_tail_info.m_bl_crc)
         {
            ret = KAL_TRUE;
         }
         else
         {
            BL_PRINT(LOG_ERROR, "ExtBl's CRC check failed, crc=%x, calCrc=%x\n\r", pTail->m_bl_tail_info.m_bl_crc, calCrc);
         }
      }
      else
      {
         BL_PRINT(LOG_ERROR, "Cannot find the ExtBL ROM Info tail\n\r");
      }
   }

   return ret;
}

#endif /* __EXT_BL_UPDATE__ || _NAND_FLASH_BOOTING_ */

static kal_uint32 Calc_Chksum(kal_uint32 buff_addr, kal_uint32 buff_len)
{
   kal_uint32   idx       = 0;
   kal_uint32   sum_val   = 0;
   kal_uint32   tmp_val   = 0;
   kal_uint8    *byte_tmp = (kal_uint8 *)&tmp_val;
   kal_uint8    *byte_ptr = 0;
   kal_uint32   *buff_ptr = (kal_uint32 *)buff_addr;

   ASSERT(!(buff_len & 0x03));

   if( ((kal_uint32)buff_ptr) & 0x03 )
   {
      byte_ptr = (kal_uint8 *)buff_ptr;
      for( idx = 0 ; idx < (buff_len>>2) ; idx++)
      {
         byte_tmp[0] = byte_ptr[0];
         byte_tmp[1] = byte_ptr[1];
         byte_tmp[2] = byte_ptr[2];
         byte_tmp[3] = byte_ptr[3];
         sum_val += tmp_val;;
      }
   }
   else
   {
      for( idx = 0 ; idx < (buff_len>>2) ; idx++)
      {
         sum_val += buff_ptr[idx];
      }
   }
   return sum_val;
}

/**********************************************************
Description : Find the marker to know where to move the Ext Bootloader
Input       : Current Ext Bootloader location, read len
Output      : 0xffffffff: No marker found/no relocation done
              other:      The new location of bootloader
***********************************************************/
kal_int32 RelocateExtBootloader(kal_uint32 srcAddr, kal_uint32 srcLen, kal_uint32 searchAddr, kal_uint32 *pExtBootloaderLen)
{
   MTK_BL_ROMInfo_v1_ST *pBlRomInfo = NULL;

   kal_uint32 extblLoadAddr = INVALID_START_ADDR;
   kal_uint32 extblLoadLen  = 0;

   kal_uint32 *p = (kal_uint32*)searchAddr;
   kal_uint32 *pSearchEnd = (kal_uint32*)(srcAddr + srcLen - sizeof(MTK_BL_ROMInfo_v1_ST));

   if(srcLen < sizeof(MTK_BL_ROMInfo_v1_ST))
   {
      return INVALID_START_ADDR;
   }

   //Rewind for the partial read
   if(p  >= (kal_uint32*)(srcAddr + sizeof(MTK_BL_ROMInfo_v1_ST)))
   {
      p -= sizeof(MTK_BL_ROMInfo_v1_ST)/4;
   }
   else
   {
      p = (kal_uint32*)srcAddr;
   }

   for(; p<=pSearchEnd; p++)
   {
      if(memcmp(p, "MTK_BOOT_LOADER_ROMINFO_V01", sizeof("MTK_BOOT_LOADER_ROMINFO_V01")) == 0)
      {
         kal_uint32 calcChecksum = Calc_Chksum((kal_uint32)p, PATTERN_ID_LEN);
         MTK_BL_ROMInfo_v1_ST *pBlRomInfo = (MTK_BL_ROMInfo_v1_ST*)p;

         BL_PRINT(LOG_DEBUG, "Addr=%x, calc=%d, val=%d\n\r", p, calcChecksum, pBlRomInfo->m_bl_header.m_super_id_chksum);
         BL_PRINT(LOG_DEBUG, "length=%d, addr=%x\n\r", pBlRomInfo->m_bl_length, pBlRomInfo->m_bl_load_address);

         if(calcChecksum == pBlRomInfo->m_bl_header.m_super_id_chksum)
         {
            extblLoadAddr = pBlRomInfo->m_bl_load_address;
            extblLoadLen  = pBlRomInfo->m_bl_length + sizeof(MTK_BL_ROMInfo_Tail_v1_ST);
            break;
         }
      }
   }

   if(extblLoadAddr != INVALID_START_ADDR)
   {
      if(extblLoadAddr != srcAddr)
      {
         kal_uint32 copyLen = extblLoadLen<srcLen ? extblLoadLen : srcLen;
         
         /* Make sure the load address is in bank 0 */
         if(extblLoadAddr >= 0x10000000 && extblLoadAddr+copyLen > 0x10000000)
         {
            BL_PRINT(LOG_ERROR, "Invalad load addr %x\n\r", extblLoadAddr);
            return INVALID_START_ADDR;
         }

         memmove((void*)extblLoadAddr, (void*)srcAddr, copyLen);
      }

      if(pExtBootloaderLen)
      {
         *pExtBootloaderLen = extblLoadLen;
      }
   }

   return extblLoadAddr;

}



#ifdef __EXT_BOOTLOADER__

#if defined(_NAND_FLASH_BOOTING_)
/**********************************************************
Description : Recognize the ILB tail by computing their distance
Input       : tags of tail1 and tail2
Output      : return if they are the same
***********************************************************/
kal_bool CompareILBTailTag(const kal_uint32 *pTag1, const kal_uint32 *pTag2)
{
   return CompareEndMarkWithError(pTag1, pTag2, BIT_ERROR_THRESHOLD);
}

/**********************************************************
Description : load image list information.
Input       : start_blk: start block of searching
Output      : Return if Image List or backup is loaded
Assumptions : FUE image is programmed to flash in block aligned manner
***********************************************************/
kal_bool LoadImageListblock(kal_uint32 start_blk)
{
   extern kal_bool g_bootupDisabled;
   extern kal_bool g_usbdlDisabled;
   extern kal_bool g_carddlDisabled;
   extern kal_bool g_enterCarddl;
   
   kal_uint32 blk_idx      = 0;
   kal_uint32 end_blk      = 0;
   kal_uint32 curPosition; /* in terms of pages */
   kal_uint32 addr1;
   kal_uint16 addr2;
   kal_uint32 target_ilb = 0;
   kal_bool   finished = KAL_FALSE;

   kal_uint32 main_ILB_pos   = 0;
   kal_uint32 temp_ILB_pos   = 0;
   kal_uint32 backup_ILB_pos = 0;
   kal_uint32 fota_ILB_pos   = 0;
   kal_uint32 dlpkg_ILB_pos  = 0;

   kal_int32 i;

   TS_BEGIN(TSID_BL_LOAD_ILB_CBR);

   BL_Shared_info.m_bl_ilb_info.m_bl_ilb_start = start_blk;

   end_blk = start_blk + BL_SEARCH_IMAGE_BLOCK_RANGE/BLOCK_SIZE;

   for ( blk_idx = start_blk ; blk_idx < end_blk && finished==KAL_FALSE ; blk_idx++ )
   {
      BL_DEBUG_PRINT("Procressing block %x\n\r", blk_idx);          
      /* Current position in terms of page */
      curPosition = blk_idx << PAGE_PER_BLOCK_SHIFT;
      
      /* Exact position in terms of pages */
      addr1 = curPosition << PAGE_SHIFT;
      addr2 = 0;
      if ( BLHeader.NFIinfo.addressCycle>4 )
      {
         if ( curPosition&0xffff0000 )
           addr2 = (kal_uint32)(curPosition >> PAGE_SHIFT);
      }
      
      if ( GoodBlockChecking(&BLHeader.NFIinfo, addr1, addr2) == KAL_FALSE )
      {
         dbg_print("Bad block found @ %d\n\r", blk_idx);
         continue;
      }

      if ( main_ILB_pos != 0)
      {
         //This is the last block of searching
         finished = KAL_TRUE;
      }

      if ( ReadPage((kal_uint32 *)IMGBLK_LOAD_ADDR, BLHeader.NFIinfo.pageSize, curPosition+ILB_HEADER_PAGE_INDEX, LOGICAL_BLOCK_ADDRESS) == KAL_FALSE)
      {
         BL_DEBUG_PRINT("Empty block found?\n\r", blk_idx);
         continue;
      }

      // detect if it is a image list block
      if (0 != strcmp((kal_char *)IMGBLK_LOAD_ADDR, SUPER_BLOCK_PATTERN))
      {
         //Not image list block
         BL_DEBUG_PRINT("Unrecognized block, skip it\n\r");
         continue;
      }

      //Read the tail
      if ( ReadPage((kal_uint32 *)IMGBLK_LOAD_ADDR, BLHeader.NFIinfo.pageSize, curPosition+BLHeader.pagesPerBlock-1, LOGICAL_BLOCK_ADDRESS) == KAL_FALSE )
      {
         dbg_print("ILB tail read failed\n\r");
         continue;
      }

      if( !main_ILB_pos && CompareILBTailTag((kal_uint32*)IMGBLK_LOAD_ADDR, IMAGE_LIST_BLOCK_TAIL_PATTERN) )
      {
         main_ILB_pos = blk_idx;
         BL_Shared_info.m_bl_ilb_info.m_bl_ilb_blk = blk_idx;

      }
      else if( !temp_ILB_pos && CompareILBTailTag((kal_uint32*)IMGBLK_LOAD_ADDR, IMAGE_LIST_BLOCK_TEMP_PATTERN) )
      {
         temp_ILB_pos = blk_idx;
      }
      else if( !backup_ILB_pos && CompareILBTailTag((kal_uint32*)IMGBLK_LOAD_ADDR, IMAGE_LIST_BLOCK_BACKUP_PATTERN) )      
      {
         backup_ILB_pos = blk_idx;
         BL_Shared_info.m_bl_ilb_info.m_bl_ilb_start = blk_idx+1;
      }
      else if( !fota_ILB_pos && CompareILBTailTag((kal_uint32*)IMGBLK_LOAD_ADDR, IMAGE_LIST_BLOCK_FOTA_PATTERN) )
      {
         fota_ILB_pos = blk_idx;
      }
      else if( !fota_ILB_pos && CompareILBTailTag((kal_uint32*)IMGBLK_LOAD_ADDR, IMAGE_LIST_BLOCK_DLPKG_PATTERN) )
      {
         dlpkg_ILB_pos = blk_idx;
      }
      else
      {
         dbg_print("Corrupted ILB found @ %d\n\r", blk_idx); 
      }
   }

   dbg_print("Main=%x, Tmp=%x, Fota=%x, DL=%x, Bak=%x\n\r", main_ILB_pos, temp_ILB_pos, fota_ILB_pos, dlpkg_ILB_pos, backup_ILB_pos);

   //Little check
   if ( main_ILB_pos && backup_ILB_pos && (backup_ILB_pos > main_ILB_pos))
   {
      return KAL_FALSE;
   }

   //Determine what we can do according what we have
   
   if ( fota_ILB_pos )
   {
      if ( temp_ILB_pos || dlpkg_ILB_pos )
      {
         //Impossible case
         return KAL_FALSE;
      }
      else
      {
         //FOTA is under procressing, suppress USBDL
         target_ilb = fota_ILB_pos;
         g_usbdlDisabled = KAL_TRUE;
         g_carddlDisabled = KAL_TRUE;
      }
   }
   else
   {
      if ( main_ILB_pos && !temp_ILB_pos && !dlpkg_ILB_pos)
      {
         //Normal bootup case
         target_ilb = main_ILB_pos;
      }
      else
      {
         //USBDL/Secure USBDL/CARD Download is under procressing, or after first download BL in factory => suppress Bootup & FOTA
         g_bootupDisabled = KAL_TRUE;
         g_bootupStatus = BL_BOOTUP_FAIL_REASON_UPDATE_IN_PROGRESS;

         if( temp_ILB_pos && dlpkg_ILB_pos)
         {
            //Impossible case
            return KAL_FALSE;
         }

         if( dlpkg_ILB_pos )
         {
            //Card download needs the version info
            target_ilb = dlpkg_ILB_pos;
            g_enterCarddl = KAL_TRUE;
         }
         else if( backup_ILB_pos )
         {
            //Secure USBDL enabled, load image list for it
            target_ilb = temp_ILB_pos ? temp_ILB_pos : backup_ILB_pos;
         }

         if( !main_ILB_pos && !dlpkg_ILB_pos)
         {
            g_carddlDisabled = KAL_TRUE;
         }
      }
      
      if( backup_ILB_pos )
      {
         bl_secure_nfb_usbdl = KAL_TRUE;            
      }
      
   }

   if(target_ilb != 0)
   {
      TS_BEGIN(TSID_BL_READ_FLASH_LAYOUT_INFO);
      
      curPosition = target_ilb << PAGE_PER_BLOCK_SHIFT;

      //Read ImageList from the 3rd page
      if ( ReadPage((kal_uint32 *)IMGBLK_LOAD_ADDR, BLHeader.NFIinfo.pageSize, curPosition+IMAGE_LIST_PAGE_INDEX, LOGICAL_BLOCK_ADDRESS) == KAL_FALSE )
      {
         return KAL_FALSE;
      }
      memcpy(&BL_Shared_info.m_bl_image_list, (kal_uint32*)IMGBLK_LOAD_ADDR, sizeof(BL_Shared_info.m_bl_image_list));
      
      if ( BL_Shared_info.m_bl_image_list.m_image_count > TOTAL_BIN_PACK )
      {
         dbg_print("Illegal Image Header Found (image count=%d), Quit!\n\r", BL_Shared_info.m_bl_image_list.m_image_count);
         return KAL_FALSE;
      }
      
      dbg_print("\nNumber of image segments = %d\n\r", BL_Shared_info.m_bl_image_list.m_image_count);
      for (i=0; i<BL_Shared_info.m_bl_image_list.m_image_count; i++)
      {
         Nand_ImageInfo_S *pImageInfo = GetImageInfo(i);
         dbg_print("..[%d] Image size = %dBytes, start addr. =%x\n\r", i, pImageInfo->m_length, pImageInfo->m_load_addr);
      }

      //Read Version Info from the 4th page
      if ( ReadPage((kal_uint32 *)IMGBLK_LOAD_ADDR, BLHeader.NFIinfo.pageSize, curPosition+VERSION_INFO_PAGE_INDEX, LOGICAL_BLOCK_ADDRESS) == KAL_FALSE )
      {
         return KAL_FALSE;         
      }
      memcpy((kal_uint32*)GetImageInfo(ROMINFO_INDEX)->m_load_addr, (kal_uint32*)IMGBLK_LOAD_ADDR, GetImageInfo(ROMINFO_INDEX)->m_length);

#if defined(__FOTA_DM__)
      //Read Space Info from the 2nd page
      if (ReadPage((kal_uint32 *)IMGBLK_LOAD_ADDR, BLHeader.NFIinfo.pageSize, curPosition+IMAGE_SPACE_PAGE_INDEX, LOGICAL_BLOCK_ADDRESS) == KAL_FALSE )
      {
         return KAL_FALSE;         
      }
      memcpy(&BL_Shared_info.m_bl_image_space, (kal_uint32*)IMGBLK_LOAD_ADDR, sizeof(BL_Shared_info.m_bl_image_space));
#endif /* __FOTA_DM__ */

      if(bl_secure_nfb_usbdl)
      {
         if(ReadPage((kal_uint32 *)IMGBLK_LOAD_ADDR, BLHeader.NFIinfo.pageSize, curPosition+EXTRA_INFO_PAGE_INDEX, LOGICAL_BLOCK_ADDRESS) == KAL_FALSE)
         {
            return KAL_FALSE;         
         }
         memcpy((kal_uint32*)GetImageInfo(0)->m_load_addr, (kal_uint32*)IMGBLK_LOAD_ADDR, GetImageInfo(0)->m_length);
      }

      TS_END(TSID_BL_READ_FLASH_LAYOUT_INFO);
   }

   TS_END(TSID_BL_LOAD_ILB_CBR);

   return KAL_TRUE;
}

/**********************************************************
Description : MultiPageReading
Input       : None
Output      : None
***********************************************************/
static kal_uint32 MultiPageReading(kal_uint8 index, kal_uint32 startPage)
{
   _RET_CODE   status;
   kal_uint32  *ptr;
   kal_int32   length;
   kal_uint32  blockNo, maskI;
   kal_uint32  curPosition; /* in terms of pages */
   kal_uint32  addr1;
   kal_uint16  addr2;   
   kal_uint32  spare[64>>2];
   kal_uint32  offset = 0;
   Nand_ImageInfo_S *pImageInfo = GetImageInfo(index);
   
   TS_BEGIN(TSID_BL_MULTIPLE_PAGE_READ);
   
   blockNo = pImageInfo->m_start_block;
   
   maskI = (0xffffffff >> (32-(BLHeader.blockShift - PAGE_SHIFT)));
   /* Current position in terms of page */
   curPosition = (blockNo << (BLHeader.blockShift - PAGE_SHIFT)) + startPage;

   if(pImageInfo->m_reserved && pImageInfo->m_reserved != FFBR_ID_IMAGEHEADER)
   {
      curPosition += (pImageInfo->m_reserved / BLHeader.NFIinfo.pageSize);
      offset = pImageInfo->m_reserved % BLHeader.NFIinfo.pageSize;
   }

   length = pImageInfo->m_length;   
   ptr = (kal_uint32 *)pImageInfo->m_load_addr;   
      
   TS_INFO(TSID_BL_MULTIPLE_PAGE_READ, index, length, (kal_uint32)ptr);
   
   /* Exact position in terms of pages */
   addr1 = curPosition << PAGE_SHIFT;
   addr2 = 0;
   if ( BLHeader.NFIinfo.addressCycle>4 )
   {
      if ( curPosition&0xffff0000 )
         addr2 = (kal_uint32)(curPosition >> PAGE_SHIFT);
   }
   
   WacthDogRestart();
   while ( length>0 )
   {

#ifdef BIT_ERROR_TEST
      EmulateBitError((kal_uint32 *)ptr, (kal_uint32 *)&spare[0], 
      BLHeader.NFIinfo.addressCycle, addr1, addr2, BLHeader.NFIinfo.pageSize);
#endif

#ifdef BL_ECC_ENABLE     
      status = NFI_PageRead(ptr, &spare[0], BLHeader.NFIinfo.pageSize, \
                            BLHeader.NFIinfo.addressCycle, addr1, addr2, \
                            BLHeader.NFIinfo.pageSize, KAL_TRUE, KAL_FALSE);
      if ( (status!=NFI_SUCCESS)&& (status!=NFI_ECC_1BIT_CORRECT))
         _errorExit(status, curPosition, ptr);
     #if (!defined (__NFI_VERSION2__) && !defined (__NFI_VERSION3__) && !defined(__NFI_VERSION3_1__))
      status = NFI_ParityCheck(ptr, &spare[0], BLHeader.NFIinfo.addressCycle, addr1, addr2, \
                               BLHeader.NFIinfo.pageSize, BLHeader.NFIinfo.IOInterface, \
                               BLHeader.NFIinfo.pageSize*BLHeader.pagesPerBlock);                          
      if ( status<0 )
         _errorExit(status, curPosition, ptr);
      #endif
#else
      status = NFI_PageRead(ptr, &spare[0], BLHeader.NFIinfo.pageSize, \
                            BLHeader.NFIinfo.addressCycle, addr1, addr2, \
                            BLHeader.NFIinfo.pageSize, KAL_FALSE, KAL_FALSE);
      if ( status!=NFI_SUCCESS )
         _errorExit(status, curPosition, ptr);
#endif /* BL_ECC_ENABLE */     
         
      if(offset)
      {
         memmove((kal_uint8 *)ptr, (kal_uint8 *)ptr+offset, BLHeader.NFIinfo.pageSize-offset);
         length -= (BLHeader.NFIinfo.pageSize - offset);
         offset = 0;
         ptr = (kal_uint32 *)((kal_uint32)ptr + BLHeader.NFIinfo.pageSize - offset);
      }
      else
      {
         ptr = (kal_uint32 *)((kal_uint32)ptr + BLHeader.NFIinfo.pageSize);
         length -= BLHeader.NFIinfo.pageSize;
      }

      curPosition++;
      
      /* Looking for the next good block */
      if ( (curPosition & maskI)==0 )
      {
         do
         {
            WacthDogRestart();

            blockNo++;
            
            curPosition = (blockNo << (BLHeader.blockShift - PAGE_SHIFT));
            addr1 = curPosition << PAGE_SHIFT;
            addr2 = 0;      
            
            if ( BLHeader.NFIinfo.addressCycle>4 )
            {
               if ( curPosition&0xffff0000 )
                  addr2 = (kal_uint32)(curPosition >> PAGE_SHIFT);
            }
            
            if ( GoodBlockChecking(&BLHeader.NFIinfo, addr1, addr2) == KAL_TRUE )  
               break;

#ifdef BL_DEBUG               
            dbg_print("\n\rBad block at block %d\n\r", blockNo);
#endif /* BL_DEBUG */               
               /* Skip the bad block */
         } while ( 1 );
         
         dbg_print("..");       
      }
      else
      {
         addr1 = curPosition << PAGE_SHIFT;      
         addr2 = 0;      
         if ( BLHeader.NFIinfo.addressCycle>4 )
         {
            if ( curPosition&0xffff0000 )
               addr2 = (kal_uint32)(curPosition >> PAGE_SHIFT);
         }
      }
      
   } 
      
   TS_END(TSID_BL_MULTIPLE_PAGE_READ);
      
   return blockNo;
}



/**********************************************************
Description : Load and verify P-MAUI
Input       : None
Output      : None
***********************************************************/

kal_uint32 LoadPrimariMAUI(void)
{
   extern void SwitchMAUIToCacheable(kal_uint32 on, kal_uint32 param);
   
   kal_uint32 start_index, i;

   kal_uint32 targetBinary = 0;

   if(IsMetaModeEnabled())
   {
      targetBinary = FFBR_ID_IMAGEHEADER;
   }

   TS_BEGIN(TSID_BL_VERIFY_NFB_IMAGE_CONTENT);
   start_index = SST_VerifyNFBImageContent((void *)MultiPageReading, (void*)SwitchMAUIToCacheable, targetBinary);
   TS_END(TSID_BL_VERIFY_NFB_IMAGE_CONTENT);
   
   dbg_print("NFB image index: %d...\n\r", start_index);
   if ((kal_int32)start_index < 0)
   {
      return 0xffffffff;
   }

   if(BL_Shared_info.m_bl_image_list.m_image[start_index].m_reserved == FFBR_ID_IMAGEHEADER)
   {
      BL_PRINT(LOG_INFO, "Launching FactoryBin in Combine mode...\r\n");
   }

#if defined(__APPLICATION_PROCESSOR__)
   /* If Running on AP Core, Load all other images */
   for (i=start_index+1 ; i < BL_Shared_info.m_bl_image_list.m_image_count; i++)
   {
      dbg_print("AP Core BootLoader -- Loading image index: %d...\n\r", i);
      MultiPageReading(i, 0);
   }
#endif /* __APPLICATION_PROCESSOR__ */

   return BL_Shared_info.m_bl_image_list.m_image[start_index].m_load_addr;
   
}

/* Utilities */
void* GetVersionInfoAddress()
{
   return (void*)(GetImageInfo(ROMINFO_INDEX)->m_load_addr);
}

#endif /* _NAND_FLASH_BOOTING_ */



#if defined(__NOR_FLASH_BOOTING__)
void NorFlashBooting()
{
   volatile kal_uint32 *src, *dst;
   kal_uint32 i;

   /* copy 8MB from NOR to SDRAM */
   src = (volatile kal_uint32 *)0x40000;
   dst = (volatile kal_uint32 *)0x08000000;
   for (i = 0x800000; i > 0; i -= 4, src++, dst++) {
      *dst = *src;
   }

   WacthDogRestart();
}
#endif //__NOR_FLASH_BOOTING__   

#if defined(__MTK_SECURE_PLATFORM__) && defined(_NAND_FLASH_BOOTING_)
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
#endif /* __MTK_SECURE_PLATFORM__ && _NAND_FLASH_BOOTING_ */


#endif /* __EXT_BOOTLOADER__ */


#endif /* __SV5_ENABLED__ */