Advertisement
Guest User

Chromecast Marvell Bootloader

a guest
Jan 29th, 2018
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 77.87 KB | None | 0 0
  1. /*
  2.  * (C) Copyright Marvell Semiconductors, Inc 2008 All rightes reserved
  3.  */
  4. #include "common.h"
  5. #include "chipctrl_const.h"
  6. #ifdef CONFIG_SM
  7. #include "sm_const.h"
  8. #include "SysMgr.h"
  9. #endif // CONFIG_SM
  10. #include "figo.h"
  11. #include "config.h"
  12. #include "nand_priv.h"
  13. #include "util.h"
  14. #include "cold_linuxparam.h"
  15. #include "SystemManager.h"
  16. #include "SysMgr.h"
  17. #include "system_config.h"
  18. #include "global.h"
  19. #include "gpio.h"
  20. #include "led_pwm.h"
  21. #include "apb_perf_base.h"
  22. #include "apb_gpio.h"
  23. #include "galois_speed.h"
  24. #include "pin_settings.h"
  25. #include "wol.h"
  26. #include "berlin_bootinfo.h"
  27. #include "mv_nand.h"
  28. #include "drmdmx.h"
  29. #include "basic_inc.h"
  30. #include "timer.h"
  31. #if 0 // BOOTLOADER_FASTLOGO
  32. #include "thinvpp_api.h"
  33. #include "avpll.h"
  34. #include "api_avio_dhub.h"
  35. #endif
  36. #include "fastlogo.h"
  37. #include "nand_block0_layout_A0.h"
  38. #include "image2_header_A0.h"
  39.  
  40. #include "image3_header.h"
  41. #include "version_table.h"
  42. #if BG2CDP
  43. #include "MctrlSS.h"
  44. #include "cpu_ca7.h"
  45. #include "block1_layout.h"
  46. #include "bcmREG.h"
  47. #include "bcm_mailbox.h"
  48. #define BCM_MAILBOX          MEMMAP_BCM_REG_BASE //0xF7930000
  49. #define BCM_PI_IMAGE_VERIFY  0x004E
  50. #define BCM_IMG_KERNEL_TYPE  4
  51. #define BCM_IMG_USBIMG_TYPE  5
  52.  
  53. #define SYSPLL_CTRL_BASE        (MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_sysPll)
  54. #define CPUPLL_CTRL_BASE        (MEMMAP_CA7_REG_BASE + RA_CA7PllReg_PllReg)
  55. #define MEMPLL_CTRL_BASE        (MEMMAP_MCTRLSS_REG_BASE + RA_MctrlSS_memPll)
  56.  
  57. #define VCORE_MASTER_ID         (0x00)
  58. #define LED_MASTER_ID (0x00)
  59.  
  60. #define VCORE_VOLT_INDEX        (0x0A) // set to 1.15v
  61. #endif
  62.  
  63. #if ANDROID_BOOT
  64. #include "android/bootimg.h"
  65. #include "android/flash_ts.h"
  66. #include "board_config.h"
  67. #endif
  68.  
  69. #include "nflash_drv.h"
  70.  
  71. #include "linux_param.h"
  72.  
  73. #include "bootloader_customize.h"
  74.  
  75. #ifdef CONFIG_USB
  76. #include "usb_storage.h"
  77. #include "usb_key.h"
  78. #endif
  79. #include "cec.h"
  80.  
  81. // disable crash counter. b/10478589.
  82. #define DISABLE_CRASH_COUNTER 1
  83.  
  84. //#define CONFIG_FORCE_ENCRYPTION
  85.  
  86. #define BOOT_SRC_NAND     1
  87. #define BOOT_SRC_USB      2
  88.  
  89. /* on a USB storage device, leave first 4KB for MBR/part table
  90.  * and start boot image at a 4KB offset.
  91.  */
  92. #define USB_BOOTIMG_START      (4 * 1024)
  93. #define USB_BLOCK_SIZ          512
  94.  
  95. #define CPU_IMG_OFFS_MAGIC       4
  96. #define CPU_IMG_OFFS_USRDATA     10
  97. #define CPU_IMG_OFFS_IMGSIZ      40
  98. #define CPU_IMG_OFFS_IMGSTA      332
  99.  
  100. #define CPU_IMG_USB_USRDATA      0xA33A
  101. #define CPU_IMG_NAND_USRDATA     0x0
  102. #define CPU_IMG_CODE_MAGIC       0xC0DE
  103.  
  104. #define MAX_ANDROID_IMG_SIZE (32 * 1024 * 1024)
  105. #define MIN_ANDROID_IMG_SIZE (1 * 1024 * 1024)
  106. #define MAX_LOGO_FRM_SIZE (4 * 1024 * 1024)
  107.  
  108. // The bootloader needs some temporary memory for loading the android image
  109. // (during verification) and for the fast logo frame buffer.  This memory is
  110. // is not needed once we boot the kernel, but it should not conflict with the
  111. // bootloader or where we relocate the kernel and ramdisk.  Furthermore,
  112. // the very top of memory is off limits (protected by the trust zone).
  113. //
  114. // Memory maps, starting at address 0:
  115. //
  116. // At kernel load time:
  117. //   [ TZ resident memory ] - size 17MB
  118. //   [ Bootloader + data (including bcm console) ] - starting at 22MB
  119. //   [ Unused memory ]
  120. //     Fast logo frame buffer - max size 4MB
  121. //     Android image buffer - max size 32MB
  122. //   [ secure memory ] - starts at MEMTOP - 8MB, size 8MB
  123. //
  124. // At kernel launch time:
  125. //   [ TZ resident memory ] - size 17MB
  126. //   [ Linux memory ] - total size ~302MB
  127. //       Kernel copied to 17MB, (note: *must* be < 5MB)
  128. //       Bootloader image is at 22MB
  129. //       Ramdisk copied to 96MB
  130. //   [ cache memory ] - size 168MB
  131. //   [ secure memory ] - starts at (BOARD_MEM_SIZE - 8MB), size 8MB
  132.  
  133. // Define an arbitrary maximum secure memory size which should be
  134. // more than enough to avoid problems in case of expansion.  The
  135. // actual size is determined by the TZ and the kernel.
  136. #define MAX_SECURE_MEMORY (32 * 1024 * 1024)
  137.  
  138. // Define the start of bootloader temporary memory.
  139. #define BOOTLOADER_TEMP_MEMORY (BOARD_MEM_SIZE - MAX_SECURE_MEMORY - \
  140.     (MAX_LOGO_FRM_SIZE + MAX_ANDROID_IMG_SIZE))
  141.  
  142. // Logo is at the start of temporary memory.
  143. #define LOGO_FRM_BUF (BOOTLOADER_TEMP_MEMORY)
  144.  
  145. // Android image buffer begins right after the frame buffer.
  146. #define ANDROID_IMG_BUF (BOOTLOADER_TEMP_MEMORY + MAX_LOGO_FRM_SIZE)
  147.  
  148. #define CHIP_VERSION   "BG2CDP"
  149. #if !CDP_A0
  150. #define MV88DE3100_CHIPID_REG1  (0xF7CFF030)
  151. #define MV88DE3100_CHIPID_REG2  (0xF7CFF034)
  152. #else
  153. #define MV88DE3100_CHIPID_REG1  (0xF7CFF060)
  154. #define MV88DE3100_CHIPID_REG2  (0xF7CFF064)
  155. #endif
  156.  
  157. #define PRODUCT_CHIP_EXT_ID     (MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_ProductId_ext)
  158. #define MV88DE3XXX_OTP_STS_REG 0xF7CC08C8
  159. #define MV88DE3XXX_OTP_LKG_ID 0XF7CC07DD
  160. #define MV88DE3XXX_SOC_REV_REG 0XF7CC07D4
  161. #define MV88DE3XXX_SOC_REV_MASK 0x3
  162. #define MV88DE3XXX_SOC_REV_SHIFT 29
  163.  
  164. #define MAX_RAMDISK_IMPLICT_SIZE    (0x2000000)
  165.  
  166. #define PRNG_DATA_SIZE 64
  167.  
  168. #define BERLIN_BOOTINFO_ADDR    0x01010000
  169.  
  170. /*
  171.  * Macro to remove unused variable warning for function args not used for
  172.  * specific platform.
  173.  */
  174. #define UNUSED(var) do { (void)(var); } while(0)
  175.  
  176. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  177.  
  178. enum sm_ldo_ctrl_vout_sel_e {
  179.  
  180.     SM_LDO_CTRL_VOUT_0_900V = 0,
  181.     SM_LDO_CTRL_VOUT_0_789V,
  182.     SM_LDO_CTRL_VOUT_0_826V,
  183.     SM_LDO_CTRL_VOUT_0_863V,
  184.     SM_LDO_CTRL_VOUT_0_937V,
  185.     SM_LDO_CTRL_VOUT_0_974V,
  186.     SM_LDO_CTRL_VOUT_1_011V,
  187.     SM_LDO_CTRL_VOUT_1_048V,
  188.     SM_LDO_CTRL_VOUT_MAX
  189. };
  190.  
  191. extern int __bss_start;
  192. extern int __bss_end;
  193.  
  194. #define BSS_START  ((unsigned int)(&__bss_start))
  195. #define BSS_END    ((unsigned int)(&__bss_end))
  196.  
  197. #if ANDROID_BOOT
  198. #define FTS_NAME        "fts"
  199. #define KERNEL_NAME     "kernel"
  200. #define RECOVERY_NAME   "recovery"
  201.  
  202. #define FTS_KEY_MACADDR         "macaddr"
  203. #define FTS_WIFI_MFG_MODE "wifi_mfg_mode"
  204. #define FTS_DEVICE_CFGED  "device_configured"
  205.  
  206. #define BOOTLOADER_COMMAND_KEY      "bootloader.command"
  207. #define ANDROID_CRASH_COUNTER_KEY   "crashcounter.android"
  208. #define MAX_CRASH_COUNT                 5
  209. #define BOOTMODE_NORMAL                 0
  210. #define BOOTMODE_RECOVERY               1
  211. #define BOOTMODE_BOOTUSB                2
  212. #define BOOTMODE_BACKUPSYS              3
  213. const char kBootcmdRecovery[] = "recovery";
  214. const char kFtsBootcmdRecovery[] = "boot-recovery";
  215. const char kFtsBootcmdBackupsys[] = "boot-backupsys";
  216.  
  217. #endif /* #if ANDROID_BOOT */
  218.  
  219. #define STR(x) #x
  220. #define XSTR(x) STR(x)
  221.  
  222. //#if defined(DEBUG)
  223. //#define debug_printf(a...) lgpl_printf(a)
  224. //#else
  225. //#define debug_printf(a...)
  226. //#endif
  227.  
  228. //#define debug_printf(a...) lgpl_printf(a)
  229. extern struct mv_nand_data nand_data ;
  230. block0_t block0;
  231.  
  232. ver_table_entry_t vt_img3;
  233. //ver_table_entry_t vt_sysconf;
  234. //ver_table_entry_t vt_flashless;
  235. #if ANDROID_BOOT
  236. static const ver_table_entry_t vt_fts = {
  237.     .part1_start_blkind = FTS_BLK_START,
  238.     .part1_blks = FTS_BLK_CNT,
  239. };
  240. static const ver_table_entry_t vt_recovery = {
  241.     .part1_start_blkind = RECOVERY_BLK_START,
  242.     .part1_blks = RECOVERY_BLK_CNT,
  243. };
  244. static const ver_table_entry_t vt_kernel = {
  245.     .part1_start_blkind = KERNEL_BLK_START,
  246.     .part1_blks = KERNEL_BLK_CNT,
  247. };
  248. static const ver_table_entry_t vt_backupsys = {
  249.     .part1_start_blkind = BACKUPSYS_BLK_START,
  250.     .part1_blks = BACKUPSYS_BLK_CNT,
  251. };
  252.  
  253. static unsigned int fts_dev_id = 0;
  254.  
  255. static boot_img_hdr Mkbootimg_hdr;
  256. void (*TheKernel)(int zero, int arch, unsigned int params);
  257. static int Bootmode = BOOTMODE_NORMAL;
  258. #endif /* #if ANDROID_BOOT */
  259.  
  260. Image3_Attr img3_hdr;
  261. sm_image_attr *sm_hdr;
  262. cpu0_image_attr *cpu0_hdr;
  263. cpu1_image_attr *cpu1_hdr;
  264. recovery_ou_attr *recou_hdr;
  265. Encryption_Header en_hdr;
  266. gen_header_t sysconf_hdr;
  267.  
  268. Mem_Layout *sMem_Layout ;
  269.  
  270. #define REBOOT_MSG_EXTRA_BUF_ADDR   0xa00000    // TODO: need a buffer address
  271. #define REBOOT_MSG_EXTRA_BUF_SIZE   512
  272.  
  273. char reset_msg_extra_buf[REBOOT_MSG_EXTRA_BUF_SIZE];
  274.  
  275. unsigned int uiWarmDown_2_Linux_Addr = 0;
  276.  
  277. void setup_linux_bootparam(u32 mem_start, u32 mem_size, char *cmdline,
  278.                            u8 chip_rev, u8 board_rev);
  279. extern void Jump(unsigned int, unsigned int, unsigned int, unsigned int);
  280. extern void Copy_Code_To_Figo_Sram();
  281. extern int MV_SM_Send_Msg(int, void *, int);
  282. extern void APB_UART_init(unsigned int , unsigned int , unsigned int ) ;
  283.  
  284. extern char gp_cust_figo_image[];
  285. extern int MV_DRMLIB_Load_Customer_Key( unsigned char *, int);
  286. extern int lgpl_printf(const char *format, ...);
  287. extern void AVPLL_Enable(void);
  288.  
  289. inline char itoc(char i);
  290.  
  291. HRESULT MV_SM_Dev_Init(UCHAR * pucData_Buffer,UINT32 uiData_Size);
  292.  
  293. void abort(){printf("Abort\n"); while(1);}
  294.  
  295. int IS_Z1() { return 0; return (readl(PRODUCT_CHIP_EXT_ID) & 0xFF) == 0; }
  296. int IS_Z2() { return 0; return (readl(PRODUCT_CHIP_EXT_ID) & 0xFF) == 2; }
  297. int IS_B0() { return 1; return (readl(PRODUCT_CHIP_EXT_ID) & 0xFF) >= 0xB0; }
  298. #ifdef ENABLE_CONNECT_BTN
  299. extern int is_EnterRecovery;
  300. extern void Check_connectbtnGPIO();
  301. #endif
  302.  
  303. #if BG2CDP
  304.  
  305. int bcm_image_verify(unsigned int type, unsigned int src, unsigned int dst);
  306.  
  307. #define VT_OFFSET_CDP (nand_data.szofblk - 4096) // CDP: version table at last 4KiB of the block
  308.  
  309. #define SZ_MV_BOOT_IMAGE_HEAD     1024
  310. #define SZW_MV_BOOT_IMAGE_HEAD    (SZ_MV_BOOT_IMAGE_HEAD/sizeof(int))
  311. #define SZ_MV_KEY_STORE_TYPE_AESK 64
  312. #define SZ_MV_KEY_STORE_TYPE_RSAK (1024 - 2*SZ_MV_KEY_STORE_TYPE_AESK)
  313. #define MV_KEY_STORE_TYPE_MASK    0x0000ffff
  314. #define MV_KEY_STORE_TYPE_RSAK    0x0000a2e1
  315. #define MV_KEY_STORE_TYPE_AESK    0x0000c237
  316. #define MV_KEY_STORE_TYPE_ENDK    0x0f01c0de
  317.  
  318. typedef struct _MV_KEY_STORE_HEAD
  319. {
  320.     unsigned int version;
  321.     unsigned int type;
  322. } MV_KEY_STORE_HEAD;
  323.  
  324. typedef struct _MV_LASTK_STORE
  325. {
  326.     union
  327.     {
  328.         MV_KEY_STORE_HEAD custk;
  329.         unsigned char custk_bytes[SZ_MV_KEY_STORE_TYPE_AESK];
  330.     };
  331.     union
  332.     {
  333.         MV_KEY_STORE_HEAD custk_kernel;
  334.         unsigned char custk_kernel_bytes[SZ_MV_KEY_STORE_TYPE_AESK];
  335.     };
  336.     union
  337.     {
  338.         MV_KEY_STORE_HEAD extrsak;
  339.         unsigned char extrsak_bytes[SZ_MV_KEY_STORE_TYPE_RSAK];
  340.     };
  341. } MV_LASTK_STORE;
  342.  
  343. typedef struct _MV_BOOT_IMAGE_HEAD
  344. {
  345.     union
  346.     {
  347.         MV_KEY_STORE_HEAD head;
  348.         unsigned char bytes[SZ_MV_BOOT_IMAGE_HEAD];
  349.         unsigned int words[SZW_MV_BOOT_IMAGE_HEAD];
  350.     };
  351. } MV_BOOT_IMAGE_HEAD;
  352.  
  353. typedef struct _MV_LASTK_IMAGE
  354. {
  355.     union
  356.     {
  357.         struct
  358.         {
  359.             MV_BOOT_IMAGE_HEAD image;
  360.         } h1;
  361.         struct
  362.         {
  363.             MV_BOOT_IMAGE_HEAD custk;
  364.             MV_BOOT_IMAGE_HEAD image;
  365.         } h2;
  366.         struct
  367.         {
  368.             MV_BOOT_IMAGE_HEAD extrsak;
  369.             MV_BOOT_IMAGE_HEAD image;
  370.         } h2a;
  371.         struct
  372.         {
  373.             MV_BOOT_IMAGE_HEAD custk;
  374.             MV_BOOT_IMAGE_HEAD extrsak;
  375.             MV_BOOT_IMAGE_HEAD image;
  376.         } h3;
  377.     };
  378. } MV_LASTK_IMAGE;
  379.  
  380. int load_lastk(void *key_store)
  381. {
  382.     MV_LASTK_STORE *lastk = (MV_LASTK_STORE *) key_store;
  383.     int err = 0;
  384.  
  385.     if ((lastk->custk_kernel.type& MV_KEY_STORE_TYPE_MASK) == MV_KEY_STORE_TYPE_AESK)
  386.     {
  387.         // there is an custk for kernel image; let's load it
  388.         MV_LASTK_IMAGE *header = (MV_LASTK_IMAGE *) &lastk->custk_kernel;
  389.         MV_KEY_STORE_HEAD *bi;
  390.  
  391.         if (lastk->extrsak.type != MV_KEY_STORE_TYPE_RSAK)
  392.         {
  393.             // there is no extrsak for kernel image; use h2 format
  394.             bi = &header->h2.image.head;
  395.         }
  396.         else
  397.         {
  398.             // there is also a extrsak for kernel image; use h3 format
  399.             UtilMemCpy(header->h3.extrsak.bytes, lastk->extrsak_bytes, sizeof(lastk->extrsak_bytes));
  400.             bi = &header->h3.image.head;
  401.         }
  402.         bi->version = 1; // only version 1 is supported
  403.         bi->type = MV_KEY_STORE_TYPE_ENDK;
  404.         err = bcm_image_verify(0, (unsigned int) header, 0);
  405.         if (err == (int)0xe0000301) // we are expecting this return code
  406.             err = 0;
  407.     }
  408.     else
  409.     {
  410.         lgpl_printf("\tno need to load kernel keys\n");
  411.     }
  412.  
  413.     return err;
  414. }
  415.  
  416. #endif
  417.  
  418. void pad_init(void)
  419. {
  420. #if !BG2CD
  421.     int val;
  422.     T32Gbl_PadSelect reg;
  423.     GA_REG_WORD32_READ(MEMMAP_CHIP_CTRL_REG_BASE+ RA_Gbl_PadSelect, &(reg.u32));
  424.     reg.uPadSelect_DVIO_OEN = Gbl_PadSelect_DVIO_OEN_Enable;
  425. #if 0 //def CYPRESS_EMI // default always disable
  426.     reg.uPadSelect_DVIO0_V18EN = 1;     //BG_FIX for EMI issue
  427. #else
  428.     reg.uPadSelect_DVIO0_V18EN = 0;
  429. #endif
  430.     reg.uPadSelect_CLK0_ZN = 7;
  431.     reg.uPadSelect_CLK0_ZP = 7;
  432.     reg.uPadSelect_DVIO0_ZN = 7;
  433. #if (BERLIN_CHIP_VERSION < BERLIN_BG2)
  434.     reg.uPadSelect_DVIO0_ZP = 7;
  435. #endif
  436.     GA_REG_WORD32_WRITE(MEMMAP_CHIP_CTRL_REG_BASE+ RA_Gbl_PadSelect, (reg.u32));
  437.  
  438. #if (BERLIN_CHIP_VERSION >= BERLIN_BG2)
  439.     T32Gbl_PadSelect1 reg1;
  440.     GA_REG_WORD32_READ(MEMMAP_CHIP_CTRL_REG_BASE+ RA_Gbl_PadSelect1, &(reg1.u32));
  441.     reg1.uPadSelect_DVIO0_ZP = 7;
  442.     GA_REG_WORD32_WRITE(MEMMAP_CHIP_CTRL_REG_BASE+ RA_Gbl_PadSelect1, (reg1.u32));
  443. #endif
  444. #endif
  445. }
  446.  
  447. #if CDP_A0==1
  448. #define SOC_SM_CEC_BASE            0xF7FE1000
  449. static int detect_reset_state(void)
  450. {
  451.     unsigned int value, warm_boot = 0;
  452.     GA_REG_WORD32_READ(SOC_SM_CEC_BASE + RA_Cec_CHIP_RESET_TRACKER, &value);
  453.  
  454.     if (value == 0) {
  455.         lgpl_printf("chip is cold boot.\n");
  456.     } else {
  457.         lgpl_printf("chip is warm boot.\n");
  458.         warm_boot = 1;
  459.     }
  460.  
  461.     return warm_boot;
  462. }
  463.  
  464. // Call this to make subsequent calls to detect_reset_state "warm"
  465. // Lasts until power cycle.
  466. static void set_reset_state_warm(void)
  467. {
  468.     GA_REG_WORD32_WRITE(SOC_SM_CEC_BASE + RA_Cec_CHIP_RESET_TRACKER, 0x01);
  469. }
  470.  
  471. #endif //CDP_A0==1
  472.  
  473. /* Initiailize GPIOs used by bootloader.
  474.  */
  475. static int board_gpio_init(void)
  476. {
  477.     int ret = 0;
  478.     if ((ret = GPIO_PortSetInOut(BUTTON_GPIO, 1)) != 0) {
  479.         lgpl_printf("ERROR: failed to set button GPIO: %d \n",
  480.                     BUTTON_GPIO);
  481.         return -1;
  482.     }
  483. #if (CFG_BOARD_NAME == BOARD_CHIRP_B1) \
  484.         || (CFG_BOARD_NAME == BOARD_CHIRP_B3) \
  485.         || (CFG_BOARD_NAME == BOARD_CHIRP_B4) \
  486.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B1) \
  487.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B3) \
  488.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  489.     if ((ret = GPIO_PortSetInOut(TOPBOARD_INTR_GPIO, 1)) != 0) {
  490.         lgpl_printf("ERROR: failed to set top board intr GPIO: %d \n",
  491.                     TOPBOARD_INTR_GPIO);
  492.         return -1;
  493.     }
  494. #endif // BOARD_CHIRP_B{1,3,4} BOARD_JOPLIN_B{1,3,4}
  495.     return 0;
  496. }
  497.  
  498. static void prng_init(char *kernel_param, size_t kernel_param_size)
  499. {
  500.     UINT8 rand_buf[PRNG_DATA_SIZE];
  501.     UINT32 rand_size = sizeof(rand_buf);
  502.         int ret = bcm_generate_random_number(rand_buf,rand_size);
  503.         if (ret != 0) {
  504.         debug_printf("ERROR: failed to generate random data. ret = %d\n",ret);
  505.         return;
  506.     }
  507.  
  508.     // Append 64 bytes (128-bytes in hex) to kernel command line "prng_data=".
  509.     strlcat(kernel_param, " prng_data=", kernel_param_size);
  510.     size_t i;
  511.     for (i = 0; i < sizeof(rand_buf); ++i) {
  512.         char tmp[3] = {0};
  513.         snprintf(tmp, sizeof(tmp), "%02x", rand_buf[i]);
  514.         strlcat(kernel_param, tmp, kernel_param_size);
  515.     }
  516.  
  517.     // Display random bits.
  518.     debug_printf("PRNG bytes\n");
  519.     for (i = 0; i < sizeof(rand_buf); i += 16) {
  520.         int j = 0;
  521.         debug_printf("\t");
  522.         for (j = 0; j < 16; ++j) {
  523.             debug_printf("%02x", rand_buf[i + j]);
  524.         }
  525.         debug_printf("\n");
  526.     }
  527. }
  528.  
  529. /* get button status
  530.  * !0 - pressed
  531.  *  0 - not pressed
  532.  */
  533. static int get_button_status(void)
  534. {
  535.     /* For CDp, read the status of bootstrap bit at start of boot process
  536.      * rather than reading current status of button. This will capture
  537.      * user's intent to boot in to USB early in boot process, even if
  538.      * the button was released in between IROM boot -> USB timeout
  539.      * -> sys_init -> bootloader transition.
  540.      */
  541.     int button = readl(CHIP_CTRL_REG_BASE + RA_Gbl_bootStrap) & 0x1;
  542.     return (button == 0);
  543. }
  544.  
  545. /* Get SOC revision information */
  546. static unsigned char get_soc_rev(void)
  547. {
  548.     /* Bits 30:29 are marked by Marvell to flag SoC revision.
  549.      * 0x0 - A0
  550.      * 0x1 - A1 (LVT + Metal fix)
  551.      * 0x2 - A2 (Metal fix only)
  552.      */
  553.     /*unsigned int rev = readl(MV88DE3XXX_SOC_REV_REG);
  554.     rev &= (MV88DE3XXX_SOC_REV_MASK << MV88DE3XXX_SOC_REV_SHIFT);
  555.     rev >>= MV88DE3XXX_SOC_REV_SHIFT;
  556.     return (unsigned char)rev;*/
  557.     return 0;
  558. }
  559.  
  560. #if ANDROID_BOOT
  561. //typedef unsigned int u32;
  562. void setup_linux_bootparam_with_ramdisk(u32 mem_start, u32 mem_size, char *cmdline,
  563.                                         u32 initrd_start, u32 initrd_size,
  564.                                         u8 chip_rev, u8 board_rev);
  565. static long long nand_read_generic(unsigned int start, unsigned int end, unsigned char* data_buff,
  566.                     unsigned int data_size);
  567. inline unsigned get_aligned(unsigned address, unsigned page_size) {
  568.     return (address + page_size - 1) / page_size * page_size;
  569. }
  570.  
  571. int bootimg_print_image_hdr (boot_img_hdr *hdr)
  572. {
  573.     int i;
  574.     UNUSED(hdr);
  575.     debug_printf ("   Image magic:   %s\n", hdr->magic);
  576.  
  577.     debug_printf ("   kernel_size:   0x%x\n", hdr->kernel_size);
  578.     debug_printf ("   kernel_addr:   0x%x\n", hdr->kernel_addr);
  579.  
  580.     debug_printf ("   rdisk_size:   0x%x\n", hdr->ramdisk_size);
  581.     debug_printf ("   rdisk_addr:   0x%x\n", hdr->ramdisk_addr);
  582.  
  583.     debug_printf ("   second_size:   0x%x\n", hdr->second_size);
  584.     debug_printf ("   second_addr:   0x%x\n", hdr->second_addr);
  585.  
  586.     debug_printf ("   tags_addr:   0x%x\n", hdr->tags_addr);
  587.     debug_printf ("   page_size:   0x%x\n", hdr->page_size);
  588.  
  589.     debug_printf ("   name:      %s\n", hdr->name);
  590.     debug_printf ("   cmdline:   %s\n", hdr->cmdline);
  591.  
  592.     for (i=0;i<8;i++)
  593.         debug_printf ("   id[%d]:   0x%x\n", i, hdr->id[i]);
  594.  
  595.     return 0;
  596. }
  597.  
  598. static int write_android_bootloader_message(const char *command,
  599.                     const char *status,
  600.                     const char *recovery)
  601. {
  602.     int res = flash_ts_set("bootloader.command", command);
  603.     if (!res)
  604.         res = flash_ts_set("bootloader.status", status);
  605.     if (!res)
  606.         res = flash_ts_set("bootloader.recovery", recovery);
  607.     else {
  608.         debug_printf("Failed to set bootloader command\n");
  609.     }
  610.  
  611.     return res;
  612. }
  613.  
  614. static unsigned long checked_usb_read(int device, unsigned long blknr,
  615.     unsigned long blkcnt, void *buffer) {
  616.     block_dev_desc_t desc;
  617.     if (get_usb_storage_desc(device, &desc) != 0) {
  618.         debug_printf("Failed to get device descriptor for usb storage "
  619.             "device %d\n", device);
  620.         return 0;
  621.     }
  622.  
  623.     if (desc.blksz != USB_BLOCK_SIZ) {
  624.         debug_printf("Invalid USB block size. Expected :%zu. Got: %zu",
  625.             (size_t)USB_BLOCK_SIZ, (size_t)desc.blksz);
  626.         return 0;
  627.     }
  628.  
  629.     return usb_stor_read(device, blknr, blkcnt, buffer);
  630. }
  631.  
  632. #ifndef DISABLE_CRASH_COUNTER
  633. void set_android_crash_counter(int cnt)
  634. {
  635.     char value[2];
  636.     value[0] = '0' + cnt;
  637.     value[1] = '\0';
  638.     flash_ts_set(ANDROID_CRASH_COUNTER_KEY, value);
  639. }
  640.  
  641. int increase_android_crash_counter(void)
  642. {
  643.     int crash_counter = flash_ts_get_int(ANDROID_CRASH_COUNTER_KEY, 0);
  644.     set_android_crash_counter(crash_counter + 1);
  645. }
  646. #endif  // !DISABLE_CRASH_COUNTER
  647.  
  648. int check_android_recovery_mode(void)
  649. {
  650.     char boot_command[256] = {0};
  651.     int button_status;
  652.  
  653. #ifndef DISABLE_CRASH_COUNTER
  654.     int crash_counter = flash_ts_get_int(ANDROID_CRASH_COUNTER_KEY, 0);
  655.     debug_printf ("fts: %s: %d\n", ANDROID_CRASH_COUNTER_KEY, crash_counter);
  656. #endif  // !DISABLE_CRASH_COUNTER
  657.  
  658.     flash_ts_get(BOOTLOADER_COMMAND_KEY, boot_command, sizeof(boot_command));
  659.     debug_printf ("fts: %s: %s\n", BOOTLOADER_COMMAND_KEY, boot_command);
  660.  
  661.     if (strncmp(boot_command, kFtsBootcmdRecovery, sizeof(kFtsBootcmdRecovery)) == 0) {
  662.         return BOOTMODE_RECOVERY;
  663.     }
  664.     if (strncmp(boot_command, kFtsBootcmdBackupsys,
  665.                 sizeof(kFtsBootcmdBackupsys)) == 0) {
  666.         return BOOTMODE_BACKUPSYS;
  667.     }
  668.     /* Boot in to USB on button press only if no other
  669.      * conditions for booting in to recovery are being met. Since
  670.      * button press is also used for factory reset, request to
  671.      * boot in to recovery in fts overrides button press.
  672.      */
  673.     button_status = get_button_status();
  674.     if (button_status){
  675.         lgpl_printf("Detected button press -- booting from USB\n");
  676.         return BOOTMODE_BOOTUSB;
  677.     }
  678. #ifndef DISABLE_CRASH_COUNTER
  679.     if (crash_counter > MAX_CRASH_COUNT) {
  680.         char dev_cfged[64] = {0,};
  681.         flash_ts_get(FTS_DEVICE_CFGED, dev_cfged, sizeof(dev_cfged));
  682.         /* Attempt to boot in to recovery after consecutive crashes
  683.          * only if the device is configured at least once.
  684.          */
  685.         if (!flash_ts_get_int("crashcounter-disable", 0) &&
  686.             !strcmp("true", dev_cfged)) {
  687.             /* TODO(kolla): determine if we need to send a special message
  688.              * to recovery to recover from crash.
  689.              */
  690.             return BOOTMODE_RECOVERY;
  691.         }
  692.     }
  693. #endif  // !DISABLE_CRASH_COUNTER
  694.  
  695.     return BOOTMODE_NORMAL;
  696. }
  697.  
  698.  
  699. #include "sha1.h"
  700.  
  701. static unsigned int swap32(unsigned int x)
  702. {
  703.     unsigned y;
  704.     y = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0x00FF00FF);
  705.     y = (y >> 16) | (y << 16);
  706.     return y;
  707. }
  708.  
  709. int verify_android_image(const unsigned char *img_start, unsigned int len)
  710. {
  711.     int i;
  712.     SHA1Context sha;        /* SHA-1 context                 */
  713.     const boot_img_hdr *hdr = (const boot_img_hdr *)img_start;
  714.     const unsigned char *kernel_data = img_start + hdr->page_size;
  715.     const unsigned char *ramdisk_data = kernel_data + get_aligned(hdr->kernel_size, hdr->page_size);
  716.     const unsigned char *second_data = ramdisk_data + get_aligned(hdr->ramdisk_size, hdr->page_size);
  717.     SHA1Reset(&sha);
  718.     SHA1Input(&sha, kernel_data, hdr->kernel_size);
  719.     SHA1Input(&sha, (const unsigned char *)&hdr->kernel_size, sizeof(hdr->kernel_size));
  720.     SHA1Input(&sha, ramdisk_data, hdr->ramdisk_size);
  721.     SHA1Input(&sha, (const unsigned char *)&hdr->ramdisk_size, sizeof(hdr->ramdisk_size));
  722.     SHA1Input(&sha, second_data, hdr->second_size);
  723.     SHA1Input(&sha, (const unsigned char *)&hdr->second_size, sizeof(hdr->second_size));
  724.  
  725.     if (!SHA1Result(&sha))
  726.         printf("bootimg SHA: could not compute message digest\n");
  727.     else {
  728.         /* convert it into mkbootimg header's byte sequence */
  729.         for (i = 0; i < 5; i++)
  730.             sha.Message_Digest[i] = swap32(sha.Message_Digest[i]);
  731.  
  732.         for (i = 0; i < 5; i++)
  733.             if (sha.Message_Digest[i] != hdr->id[i]) {
  734.                 printf("bootimg SHA1 verify fail: start 0x%08x, size %d\n"
  735.                         "\tOrig %08X %08X %08X %08X %08X\n"
  736.                         "\tCalc %08X %08X %08X %08X %08X\n",
  737.                         (unsigned int)img_start, len,
  738.                         hdr->id[0],
  739.                         hdr->id[1],
  740.                         hdr->id[2],
  741.                         hdr->id[3],
  742.                         hdr->id[4],
  743.                         sha.Message_Digest[0],
  744.                         sha.Message_Digest[1],
  745.                         sha.Message_Digest[2],
  746.                         sha.Message_Digest[3],
  747.                         sha.Message_Digest[4]);
  748.  
  749.                 return -1;
  750.             }
  751.         return 0;
  752.     }
  753.  
  754.     return -1;
  755. }
  756.  
  757. static int bootimg_hdr_verify(unsigned char *img, int boot_src)
  758. {
  759.     UNUSED(img);
  760.     UNUSED(boot_src);
  761. #if (CFG_BOARD_NAME == BOARD_LEXX_B4) \
  762.     || (CFG_BOARD_NAME == BOARD_EARTH_B4) \
  763.     || (CFG_BOARD_NAME == BOARD_HENDRIX_B4) \
  764.     || (CFG_BOARD_NAME == BOARD_CHIRP_B4) \
  765.     || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  766.     unsigned short img_magic = *(short *)(img + CPU_IMG_OFFS_MAGIC);
  767.     unsigned char code_type = img[7];
  768.     unsigned short img_udata = *(short *)(img + CPU_IMG_OFFS_USRDATA);
  769.  
  770.     if (boot_src == BOOT_SRC_USB){
  771.         /* TODO(kolla): Enable checks for ver, mkt_id etc.*/
  772.         if (img_magic != CPU_IMG_CODE_MAGIC)
  773.             return -1;
  774.         if (code_type != BCM_IMG_USBIMG_TYPE)
  775.             return -1;
  776.         if (img_udata != CPU_IMG_USB_USRDATA)
  777.             return -1;
  778.     } else {
  779.         /* TODO(kolla): Enable robust header checks for NAND image.*/
  780.         if (img_magic != CPU_IMG_CODE_MAGIC)
  781.             return -1;
  782.         if (code_type != BCM_IMG_KERNEL_TYPE)
  783.             return -1;
  784.         if (img_udata != CPU_IMG_NAND_USRDATA)
  785.             return -1;
  786.     }
  787. #endif // BOARD_{LEXX,EARTH,HENDRIX,CHIRP,JOPLIN}_B4
  788.     return 0;
  789. }
  790.  
  791. static int load_android_image(int bootmode, int boot_src)
  792. {
  793.     ver_table_entry_t *vt_android;
  794.     int iPageSize;
  795.     int iBlockSize;
  796.     unsigned int android_start, android_end;
  797.     int ret;
  798.     unsigned mkbootimg_page;
  799.     unsigned char* k_buff;
  800.     unsigned int k_buff_size;
  801.     unsigned int k_buff_offset;
  802.     unsigned int cpu_img_siz = 0;
  803.     int usb_stor_cnt;
  804.     unsigned char* k_buff_img;
  805.     unsigned int bcm_img_type = BCM_IMG_KERNEL_TYPE;
  806.     unsigned long hdr_blk_cnt = get_aligned(CPU_IMG_OFFS_IMGSTA,
  807.                                   USB_BLOCK_SIZ)/USB_BLOCK_SIZ;
  808.  
  809.     UNUSED(iPageSize);
  810.     /* Use memory near the end of DDR for loading
  811.      * Android image for verification. Bootloader has
  812.      * configured MMU for flat memory map, i.e. VA == PA.
  813.      */
  814.     k_buff = (unsigned char *)ANDROID_IMG_BUF;
  815.     /* Actual image may start with an offset in k_buff */
  816.     k_buff_img = k_buff;
  817.  
  818.     /* Wipe the scratch memory first to make sure nothing */
  819.     /* is present from the last boot attempt. */
  820.     memset(k_buff, 0, MAX_ANDROID_IMG_SIZE);
  821.  
  822.     if (boot_src == BOOT_SRC_USB){
  823. #ifdef CONFIG_USB
  824.         android_start = USB_BOOTIMG_START/USB_BLOCK_SIZ;
  825.         ret = usb_init();
  826.         if (ret < 0){
  827.             lgpl_printf("ERROR: Failed to usb_init ret %d\n", ret);
  828.             return -1;
  829.         }
  830.         /* recognize storage devices */
  831.         usb_stor_cnt = usb_stor_scan();
  832.         if (!usb_stor_cnt){
  833.             lgpl_printf("ERROR: No USB storage device detected\n");
  834.             return -1;
  835.         }
  836. #if (CFG_BOARD_NAME == BOARD_LEXX_B4) \
  837.         || (CFG_BOARD_NAME == BOARD_EARTH_B4) \
  838.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B4) \
  839.         || (CFG_BOARD_NAME == BOARD_CHIRP_B4) \
  840.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  841.         /* Prepend keystores used for sign verification and decryption
  842.          * to the image being read.
  843.          */
  844.         /* TODO(kolla): Fix CUSTK, encryption of RSAK */
  845.         memcpy(k_buff_img, usb_extrsak_store, sizeof(usb_extrsak_store));
  846.         k_buff_img += USB_EXT_RSAKSTORE_LEN;
  847. #endif  // BOARD_{LEXX,EARTH,HENDRIX,CHIRP,JOPLIN}_B4
  848.         lgpl_printf("Reading %d blocks from USB device \n", hdr_blk_cnt);
  849.         /* Read from first USB device */
  850.         if(checked_usb_read(0, android_start, hdr_blk_cnt, k_buff_img) != hdr_blk_cnt) {
  851.             lgpl_printf("ERROR: Failed to read CPU image header from USB \n");
  852.             return -1;
  853.         }
  854.         lgpl_printf("Read %d blocks from USB device \n", hdr_blk_cnt);
  855. #else
  856.         debug_printf("ERROR: USB boot is not supported\n");
  857.         return -1;
  858. #endif  // CONFIG_USB
  859.     }else{
  860.  
  861.         if (bootmode == BOOTMODE_RECOVERY)
  862.             vt_android = (ver_table_entry_t *)&vt_recovery;
  863.         else if (bootmode == BOOTMODE_BACKUPSYS)
  864.             vt_android = (ver_table_entry_t *)&vt_backupsys;
  865.         else
  866.             vt_android = (ver_table_entry_t *)&vt_kernel;
  867.  
  868.         iPageSize = nand_data.szofpg ;
  869.         iBlockSize = nand_data.szofblk;
  870.  
  871.         android_start = vt_android->part1_start_blkind;
  872.         android_end = android_start + vt_android->part1_blks;
  873.         android_start *= iBlockSize;
  874.         android_end *= iBlockSize;
  875.         debug_printf("NAND: Start to read CPU image header!\n");
  876.  
  877.         /* Read CPU image header to determing length of the image */
  878.         ret = nand_read_generic(android_start ,
  879.                     android_end,
  880.                     k_buff_img, USB_BLOCK_SIZ);
  881.         if (ret < 0){
  882.             debug_printf("NAND: Failed to read CPU image header!\n");
  883.             return -1;
  884.         }
  885.     }
  886.  
  887.     cpu_img_siz = *((unsigned int *)(k_buff_img + CPU_IMG_OFFS_IMGSIZ));
  888.     cpu_img_siz = get_aligned(cpu_img_siz, 16);
  889.     cpu_img_siz += CPU_IMG_OFFS_IMGSTA;
  890.  
  891.     /* Add min size check to be safe */
  892.     int reqd_buf_siz = cpu_img_siz + k_buff_img - k_buff;
  893.     if (reqd_buf_siz > MAX_ANDROID_IMG_SIZE || reqd_buf_siz < MIN_ANDROID_IMG_SIZE) {
  894.         debug_printf("ERROR: aligned CPU img_siz %u is invalid\n",
  895.                      reqd_buf_siz);
  896.         return -1;
  897.     }
  898.  
  899.     if (boot_src == BOOT_SRC_USB){
  900. #ifdef CONFIG_USB
  901.         int blk_cnt;
  902.         int ret;
  903.  
  904.         blk_cnt = get_aligned(cpu_img_siz, USB_BLOCK_SIZ)/USB_BLOCK_SIZ;
  905.         /* Always read from first USB storage device */
  906.         lgpl_printf("Reading %d blocks from USB device \n", blk_cnt);
  907.  
  908.         ret = checked_usb_read(0,
  909.                             (android_start + hdr_blk_cnt),
  910.                             (blk_cnt - hdr_blk_cnt),
  911.                             (k_buff_img + hdr_blk_cnt * USB_BLOCK_SIZ));
  912.         if (ret != (int)(blk_cnt - hdr_blk_cnt)){
  913.             lgpl_printf("ERROR: Failed to read CPU image blk_cnt %d ret %d\n",
  914.                         (blk_cnt - hdr_blk_cnt), ret);
  915.             return -1;
  916.         }
  917.         lgpl_printf("Read %d blocks from USB device \n", blk_cnt);
  918.         /* There can be trailing bytes beyond the end of the image */
  919.         /* due to rounding up to USB_BLOCK_SIZ (and which would */
  920.         /* otherwise be unverified).  Might as well zero all remaining */
  921.         /* memory to be safer. */
  922.         int trailing_bytes = MAX_ANDROID_IMG_SIZE - reqd_buf_siz;
  923.         if (trailing_bytes > 0) {
  924.             lgpl_printf("NOTE: %x trailing bytes zeroed\n", trailing_bytes);
  925.             memset(k_buff_img + cpu_img_siz, 0, trailing_bytes);
  926.         }
  927.         bcm_img_type = BCM_IMG_USBIMG_TYPE;
  928. #endif  // CONFIG_USB
  929.     }else{
  930.         ret = nand_read_generic(android_start,
  931.                     android_end,
  932.                     k_buff_img,
  933.                     cpu_img_siz);
  934.         if (ret < 0){
  935.             lgpl_printf("ERROR: Failed to read CPU image ret %d\n",
  936.                         ret);
  937.             return -1;
  938.         }
  939.     }
  940.     /* Verify image header */
  941.     ret = bootimg_hdr_verify(k_buff_img, boot_src);
  942.     if (ret) {
  943.       lgpl_printf("ERROR: Boot image verify header failed!ret=0x%x\n", ret);
  944.       return -1;
  945.     }
  946.     ret = bcm_image_verify(bcm_img_type, (unsigned) k_buff, (unsigned) k_buff);
  947.     if (ret) {
  948.         lgpl_printf("ERROR: Verify k_buff image failed!ret=0x%x\n", ret);
  949.         return -1;
  950.     }
  951.     memcpy(&Mkbootimg_hdr, k_buff, sizeof(Mkbootimg_hdr));
  952.  
  953.     if (strncmp((const char *)Mkbootimg_hdr.magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
  954.         printf("ERROR: Invalid mkbootimg header!\n");
  955.         return -1;
  956.     }
  957.  
  958.     /* force to use predefined kernel addr (CPU0_START_ADDR) in config.
  959.      * We don't use the kernel_addr from kernel partition for scurity
  960.      * consideration.
  961.      */
  962.     Mkbootimg_hdr.kernel_addr = CPU0_START_ADDR;
  963.  
  964.     TheKernel = (void (*)(int, int, unsigned int))(Mkbootimg_hdr.kernel_addr);
  965.  
  966.     mkbootimg_page = Mkbootimg_hdr.page_size;
  967.     // align by page_size and add one page for skipping header.
  968.     k_buff_size = mkbootimg_page +
  969.         get_aligned(Mkbootimg_hdr.kernel_size, mkbootimg_page) +
  970.         get_aligned(Mkbootimg_hdr.ramdisk_size, mkbootimg_page) +
  971.         get_aligned(Mkbootimg_hdr.second_size, mkbootimg_page);
  972.  
  973.     /* Check bootloader has enough memory to be able to
  974.      * read the firmware image for verification.
  975.      */
  976.     if (k_buff_size > MAX_ANDROID_IMG_SIZE){
  977.         debug_printf("BOOTLOADER: Firmware image is too big %d for "
  978.                      "verification\n",
  979.                      k_buff_size);
  980.         return -1;
  981.     }
  982.  
  983.     UtilMemCpy((void *)Mkbootimg_hdr.kernel_addr, k_buff + mkbootimg_page, Mkbootimg_hdr.kernel_size);
  984.     if (Mkbootimg_hdr.ramdisk_size) {
  985.         k_buff_offset = get_aligned(Mkbootimg_hdr.kernel_size + mkbootimg_page, mkbootimg_page);
  986.         UtilMemCpy((void *)Mkbootimg_hdr.ramdisk_addr, k_buff + k_buff_offset, Mkbootimg_hdr.ramdisk_size);
  987.     }
  988.  
  989.         bootimg_print_image_hdr(&Mkbootimg_hdr);
  990.     return 0;
  991. }
  992.  
  993. // Set mac address for 8801.
  994. static void make_wifi_macaddr(char *wifi_mac_addr_buf)
  995. {
  996.     long chipid_reg1;
  997.     char *p;
  998.     int i;
  999.  
  1000.     // Use lowest 24 bit from chip ID.
  1001.     chipid_reg1 = readl(MV88DE3100_CHIPID_REG1) ;
  1002.     strcpy(wifi_mac_addr_buf, "F8:8F:CA");
  1003.     p = wifi_mac_addr_buf + strlen(wifi_mac_addr_buf);
  1004.  
  1005.     for(i = 0;i < 3; ++i) {
  1006.         char hex;
  1007.         hex = chipid_reg1 >> (16 - 8 * i);
  1008.         *p++ = ':';
  1009.         *p++ = itoc(hex>>4);
  1010.         *p++ = itoc(hex);
  1011.     }
  1012.     *p = '\0';
  1013. }
  1014.  
  1015. void setup_android_kernel_param(int boot_mode)
  1016. {
  1017.     char kernel_param[1024];
  1018.     char wifi_mac_addr[64] = {0};
  1019.     char tmp_buf[256];
  1020.  
  1021.     //add flash_ts dev_id info
  1022.     snprintf(tmp_buf, sizeof(tmp_buf),
  1023.         " flash_ts.dev_id=%d flash_ts.size=%d flash_ts.erasesize=%d flash_ts.writesize=%d",
  1024.             fts_dev_id, vt_fts.part1_blks * nand_data.szofblk, nand_data.szofblk, nand_data.szofpg);
  1025.  
  1026.     lgpl_printf("mkbootimg bootargs: %s\n", Mkbootimg_hdr.cmdline);
  1027.  
  1028.     strlcpy(kernel_param, (CHAR *)Mkbootimg_hdr.cmdline,
  1029.         sizeof(kernel_param));
  1030.     /* Append mac address and fts command line params to kernel
  1031.      * command line.
  1032.      */
  1033.     strlcat(kernel_param, " ", sizeof(kernel_param));
  1034.     strlcat(kernel_param, tmp_buf, sizeof(kernel_param));
  1035.  
  1036.     if (Mkbootimg_hdr.ramdisk_size) {
  1037.       memset(tmp_buf, 0, sizeof(tmp_buf));
  1038.       strlcat(kernel_param, tmp_buf, sizeof(kernel_param));
  1039.     }
  1040.     lgpl_printf("Generated bootargs: %s\n", kernel_param);
  1041.  
  1042.     /* Setup androidboot.hardware and androidboot.bootmode
  1043.      * params needed by kernel. Allow boot from USB to gain all
  1044.      * permissions a recovery image would have.
  1045.      */
  1046.     strlcat(kernel_param, " androidboot.hardware="XSTR(BOARD_NAME),
  1047.         sizeof(kernel_param));
  1048.     if (boot_mode == BOOTMODE_NORMAL)
  1049.         strlcat(kernel_param, " androidboot.mode=normal",
  1050.             sizeof(kernel_param));
  1051.     else if ((boot_mode == BOOTMODE_RECOVERY) ||
  1052.             (boot_mode == BOOTMODE_BOOTUSB))
  1053.         strlcat(kernel_param, " androidboot.mode=recovery",
  1054.             sizeof(kernel_param));
  1055.     else if (boot_mode == BOOTMODE_BACKUPSYS)
  1056.         strlcat(kernel_param, " androidboot.mode=backupsys",
  1057.             sizeof(kernel_param));
  1058.     else
  1059.         lgpl_printf("ERROR: unknown boot mode %d \n", boot_mode);
  1060.  
  1061.     /* Pass bootloader version info to kernel */
  1062.     strlcat(kernel_param, " androidboot.bootloader="BOOTLOADER_VERSION,
  1063.         sizeof(kernel_param));
  1064.  
  1065.     /* Pass NAND boot path source info to kernel */
  1066.     {
  1067.         const volatile BERLIN_BOOTINFO *pinfo = (BERLIN_BOOTINFO *)BERLIN_BOOTINFO_ADDR;
  1068.         snprintf(tmp_buf, sizeof(tmp_buf),
  1069.             " nandboot.src_block_num=0x%08x%08x",
  1070.             pinfo->nand_src_blk_num[1], pinfo->nand_src_blk_num[0]);
  1071.         strlcat(kernel_param, tmp_buf, sizeof(kernel_param));
  1072.     }
  1073.  
  1074.     /* If the fts param for wifi mfg mode is set, pass module
  1075.      * params to enable wifi mfg mode.
  1076.      */
  1077.     flash_ts_get(FTS_WIFI_MFG_MODE, tmp_buf, sizeof(tmp_buf));
  1078.     if ((boot_mode == BOOTMODE_NORMAL) && !strcmp("1", tmp_buf)) {
  1079. #if (CFG_BOARD_NAME == BOARD_EARTH_B1) \
  1080.         || (CFG_BOARD_NAME == BOARD_EARTH_B2) \
  1081.         || (CFG_BOARD_NAME == BOARD_EARTH_B3) \
  1082.         || (CFG_BOARD_NAME == BOARD_EARTH_B4) \
  1083.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B1) \
  1084.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B3) \
  1085.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B4) \
  1086.         || (CFG_BOARD_NAME == BOARD_CHIRP_B1) \
  1087.         || (CFG_BOARD_NAME == BOARD_CHIRP_B3) \
  1088.         || (CFG_BOARD_NAME == BOARD_CHIRP_B4) \
  1089.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B1) \
  1090.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B3) \
  1091.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  1092.         strlcat(kernel_param, " sd8887.mfg_mode=1 sd8887.drv_mode=1",
  1093.             sizeof(kernel_param));
  1094.         strlcat(kernel_param, " sd8887.fw_name=mrvl/sd8887_mfg_wlan.bin",
  1095.             sizeof(kernel_param));
  1096.         strlcat(kernel_param, " bt8xxx.fw_name=mrvl/sd8887_mfg_bt.bin",
  1097.             sizeof(kernel_param));
  1098.         strlcat(kernel_param, " sd8887.cal_data_cfg=mrvl/WlanCalData_sd8887-b1.conf",
  1099.             sizeof(kernel_param));
  1100.         strlcat(kernel_param, " sd8887.cfg80211_drcs=0",
  1101.             sizeof(kernel_param));
  1102.         strlcat(kernel_param, " sd8887.ps_mode=2",
  1103.             sizeof(kernel_param));
  1104.         strlcat(kernel_param, " sd8887.uap_oper_ctrl=0x20001",
  1105.             sizeof(kernel_param));
  1106. #else
  1107.         strlcat(kernel_param, " sd8801.mfg_mode=1 sd8801.drv_mode=1",
  1108.             sizeof(kernel_param));
  1109.         strlcat(kernel_param, " sd8801.fw_name=mrvl/sd8801_mfg.bin",
  1110.             sizeof(kernel_param));
  1111.         strlcat(kernel_param, " sd8801.cfg80211_drcs=0",
  1112.             sizeof(kernel_param));
  1113.         strlcat(kernel_param, " sd8801.ps_mode=2",
  1114.             sizeof(kernel_param));
  1115.         strlcat(kernel_param, " sd8801.hw_test=1",
  1116.             sizeof(kernel_param));
  1117. #endif
  1118.     } else {
  1119.         // Set MAC address only in non-MFG mode.
  1120.         make_wifi_macaddr(wifi_mac_addr);
  1121.  
  1122.         // Set calibration config file.
  1123. #if (CFG_BOARD_NAME == BOARD_LEXX_B3) \
  1124.         || (CFG_BOARD_NAME == BOARD_LEXX_B4)
  1125.         strlcat(kernel_param, " sd8801.cal_data_cfg=mrvl/WlanCalData_sd8801-b2.conf",
  1126.             sizeof(kernel_param));
  1127.         strlcat(kernel_param, " sd8801.mac_addr=", sizeof(kernel_param));
  1128.         strlcat(kernel_param, wifi_mac_addr, sizeof(kernel_param));
  1129.         strlcat(kernel_param, " sd8801.txpwrlimit_cfg=mrvl/txpwr",
  1130.             sizeof(kernel_param));
  1131.         strlcat(kernel_param, " sd8801.cfg80211_drcs=0",
  1132.             sizeof(kernel_param));
  1133.         strlcat(kernel_param, " sd8801.ps_mode=2", sizeof(kernel_param));
  1134.         strlcat(kernel_param, " sd8801.hw_test=1", sizeof(kernel_param));
  1135. #elif (CFG_BOARD_NAME == BOARD_EARTH_B1) \
  1136.         || (CFG_BOARD_NAME == BOARD_EARTH_B2) \
  1137.         || (CFG_BOARD_NAME == BOARD_EARTH_B3) \
  1138.         || (CFG_BOARD_NAME == BOARD_EARTH_B4) \
  1139.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B1) \
  1140.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B3) \
  1141.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B4) \
  1142.         || (CFG_BOARD_NAME == BOARD_CHIRP_B1) \
  1143.         || (CFG_BOARD_NAME == BOARD_CHIRP_B3) \
  1144.         || (CFG_BOARD_NAME == BOARD_CHIRP_B4) \
  1145.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B1) \
  1146.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B3) \
  1147.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  1148.         strlcat(kernel_param, " sd8887.cal_data_cfg=mrvl/WlanCalData_sd8887-b1.conf", sizeof(kernel_param));
  1149.         strlcat(kernel_param, " sd8887.fw_name=mrvl/sd8887_wlan_a2.bin",
  1150.             sizeof(kernel_param));
  1151.         strlcat(kernel_param, " sd8887.auto_ds=2",
  1152.             sizeof(kernel_param));
  1153.         strlcat(kernel_param, " sd8887.mac_addr=",
  1154.             sizeof(kernel_param));
  1155.         strlcat(kernel_param, wifi_mac_addr, sizeof(kernel_param));
  1156.         strlcat(kernel_param, " sd8887.txpwrlimit_cfg=mrvl/txpwr",
  1157.             sizeof(kernel_param));
  1158.         strlcat(kernel_param, " sd8887.cfg80211_drcs=0",
  1159.             sizeof(kernel_param));
  1160.         strlcat(kernel_param, " sd8887.ps_mode=2",
  1161.             sizeof(kernel_param));
  1162.         strlcat(kernel_param, " sd8887.uap_oper_ctrl=0x20001",
  1163.             sizeof(kernel_param));
  1164.         strlcat(kernel_param, " bt8xxx.fw_name=mrvl/sd8887_bt_a2.bin",
  1165.             sizeof(kernel_param));
  1166.         strlcat(kernel_param, " bt8xxx.bt_fw_serial=0",
  1167.             sizeof(kernel_param));
  1168. #endif
  1169.     }
  1170.  
  1171. #if CDP_A0==1
  1172.     int rv = detect_reset_state();
  1173.     snprintf(tmp_buf, sizeof(tmp_buf), " warm_boot=%d", rv);
  1174.     strlcat(kernel_param, tmp_buf, sizeof(kernel_param));
  1175. #endif  // CDP_A0==1
  1176.  
  1177.     prng_init(kernel_param, sizeof(kernel_param));
  1178.  
  1179.     setup_linux_bootparam_with_ramdisk(Mkbootimg_hdr.kernel_addr - 0x8000,
  1180.                                        MEMORY_SIZE - Mkbootimg_hdr.kernel_addr,
  1181.                                        kernel_param,
  1182.                                        Mkbootimg_hdr.ramdisk_addr,
  1183.                                        Mkbootimg_hdr.ramdisk_size,
  1184.                                        get_soc_rev(), BOARD_REV);
  1185. }
  1186.  
  1187. // TODO(yichunko): Joplin uses only led_frame_15
  1188. #if (CFG_BOARD_NAME == BOARD_CHIRP_B1) \
  1189.         || (CFG_BOARD_NAME == BOARD_CHIRP_B3) \
  1190.         || (CFG_BOARD_NAME == BOARD_CHIRP_B4) \
  1191.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B1) \
  1192.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B3) \
  1193.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  1194. static void set_topboard_leds(unsigned char* led_frame_15,
  1195.                               size_t led_frame_15_size,
  1196.                               unsigned char* led_frame_17,
  1197.                               size_t led_frame_17_size) {
  1198.     if (detect_reset_state() == 1) {
  1199.         // This is a warm boot, do not show LEDs
  1200.         return;
  1201.     }
  1202.  
  1203.     // Boot is cold. Show LEDs.
  1204.     // See mapping at
  1205.     // https://docs.google.com/document/d/1L_DVMgrjZeU3MMt8VjrJeZIAtCuu-HxV1DrQcBH0fTg
  1206.     diag_i2c_led_set_frame(LED_MASTER_ID, 0x15, led_frame_15,
  1207.                            led_frame_15_size);
  1208.     diag_i2c_led_set_frame(LED_MASTER_ID, 0x17, led_frame_17,
  1209.                            led_frame_17_size);
  1210. }
  1211. #endif  // BOARD_CHIRP_B{1,3,4} BOARD_JOPLIN_B{1,3,4}
  1212.  
  1213. void start_android_kernel(void)
  1214. {
  1215.     // TODO: Set appropriate arch parameter.
  1216.     unsigned arch = 0;
  1217.     unsigned params = Mkbootimg_hdr.kernel_addr - 0x8000 + 0x100;
  1218.  
  1219.     if (Bootmode == BOOTMODE_NORMAL) {
  1220.         lgpl_printf("Boot normal GTV image\n");
  1221.         // crash counter will be cleared after completing GTV boot (by CrashCounter app)
  1222. #ifndef DISABLE_CRASH_COUNTER
  1223.         increase_android_crash_counter();
  1224. #endif  // !DISABLE_CRASH_COUNTER
  1225.     } else {
  1226.         lgpl_printf("Boot recovery image\n");
  1227. #ifndef DISABLE_CRASH_COUNTER
  1228.         set_android_crash_counter(0);
  1229. #endif  // !DISABLE_CRASH_COUNTER
  1230.     }
  1231. // TODO(yichunko): Joplin uses only led_frame_15, P1 has 21 leds and P2 has 12
  1232. #if (CFG_BOARD_NAME == BOARD_CHIRP_B1) \
  1233.         || (CFG_BOARD_NAME == BOARD_CHIRP_B3)  \
  1234.         || (CFG_BOARD_NAME == BOARD_CHIRP_B4)  \
  1235.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B1) \
  1236.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B3) \
  1237.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  1238.     unsigned char led_frame_15[18] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1239.                                       0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
  1240.                                       0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF};
  1241.     unsigned char led_frame_17[18] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1242.                                       0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
  1243.                                       0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00};
  1244.     set_topboard_leds(led_frame_15, ARRAY_SIZE(led_frame_15),
  1245.                           led_frame_17, ARRAY_SIZE(led_frame_17));
  1246. #endif  // BOARD_CHIRP_B{1,3,4} BOARD_JOPLIN_B{1,3,4}
  1247. #if CDP_A0==1
  1248.     set_reset_state_warm();
  1249. #endif  //CDP_A0==1
  1250.     lgpl_printf("Start kernel at 0x%08x\n", Mkbootimg_hdr.kernel_addr);
  1251.     __reset_cpu((int)TheKernel , 1330, params) ;
  1252.     TheKernel (0, arch, params);
  1253. }
  1254.  
  1255. #endif  // ANDROID_BOOT
  1256.  
  1257. /*
  1258.  * return: the block address after the last block of the image
  1259.  */
  1260. static long long nand_read_generic(unsigned int start, unsigned int end, unsigned char* data_buff,
  1261.                     unsigned int data_size)
  1262. {
  1263.     unsigned int i,j,k;
  1264.     unsigned int block_size, page_size;
  1265.     int page_buff[8448/4];
  1266.     unsigned int residual, br;
  1267.  
  1268.     block_size = nand_data.szofblk;
  1269.     page_size = nand_data.szofpg;
  1270.  
  1271.     if((start & (block_size - 1)) || (end & (block_size - 1)))
  1272.         return -1;
  1273.     if( (end < start) || (end - start < data_size))
  1274.         return -1;
  1275.     j=0;
  1276.     for(i=start;i<end;i+=block_size) {
  1277.         residual = data_size - j;
  1278.         br = residual < block_size ? residual : block_size;
  1279.         if(j >= data_size)
  1280.             break;
  1281.         if(is_block_bad((loff_t)i)) {
  1282.             debug_printf("Bad block found @0x%08x.\n", i);
  1283.             continue;
  1284.         }
  1285.         for(k=0;k<br;k+=page_size) {
  1286.             unsigned int resd = br - k;
  1287.             unsigned int cp_size = resd < page_size ? resd : page_size;
  1288.             //get_nand_partition_type((loff_t)i + k);
  1289.             //if(NFlash_PageRead((loff_t)i + k, page_buff))
  1290.             //int mv_nand_read_large_page(loff_t srcx , char *buf, int oob)
  1291.             if (page_size != (UINT32)mv_nand_read_block( (loff_t)i+k, (CHAR *)page_buff, page_size))
  1292.             {
  1293.                 lgpl_printf("Read failed @ 0x%08x\n", i + k);
  1294.                 return -1;
  1295.             }
  1296.             UtilMemCpy(data_buff + j + k, page_buff, cp_size);
  1297.         }
  1298.         j+=block_size;
  1299.     }
  1300.     if(j < data_size)
  1301.         return -1;
  1302.  
  1303.     return i;
  1304. }
  1305.  
  1306. #define NW_START 0x01000000
  1307. /*
  1308.  * ECC strength parameter is for A0
  1309.  * fix addr_cycle to 5
  1310.  */
  1311. int init_nfc(int block_size, int page_size, int ecc_strength)
  1312. {
  1313.     // int ret=0;
  1314.     // configure device
  1315.     unsigned int nand_param_buff[4];
  1316.     nand_data.szofpg = page_size;
  1317.     nand_data.szofblk = block_size;
  1318.     nand_data.t = ecc_strength;
  1319.  
  1320.     if((nand_data.szofpg != 512) && (nand_data.szofpg != 2048) &&
  1321.         (nand_data.szofpg != 4096) && (nand_data.szofpg != 8192)) {
  1322.  
  1323.         UtilMemCpy((char*)nand_param_buff, (char *)(NW_START + 16), 16);
  1324.         nand_data.szofpg = nand_param_buff[0];
  1325.         nand_data.t = nand_param_buff[1];
  1326.         nand_data.szofblk = nand_param_buff[2];
  1327.         printf("[%s:%d] get nand parameters from tz_loder (block_size=%d, page_size=%d, ecc=%d)\n",
  1328.             __func__, __LINE__, nand_data.szofblk, nand_data.szofpg, nand_data.t);
  1329.     }
  1330.  
  1331.     if((nand_data.szofpg != 512) && (nand_data.szofpg != 2048) &&
  1332.         (nand_data.szofpg != 4096) && (nand_data.szofpg != 8192)) {
  1333.     #if 1
  1334.         nand_data.szofpg = 0x1000;
  1335.         nand_data.szofblk = 0x100000;
  1336.         nand_data.t = 48;
  1337.         printf("[%s:%d] force nand parameters(block_size=%d, page_size=%d, ecc=%d)\n",
  1338.             __func__, __LINE__, nand_data.szofblk, nand_data.szofpg, nand_data.t);
  1339.     #else
  1340.         printf("[%s:%d] error nand parameters(block_size=%d, page_size=%d, ecc=%d)\n",
  1341.             __func__, __LINE__, block_size, page_size, ecc_strength);
  1342.         return -1;
  1343.         #endif
  1344.     }
  1345.  
  1346.     debug_printf("NAND block size %d, page size %d, ecc_strength %d\n",
  1347.         nand_data.szofblk, nand_data.szofpg, nand_data.t);
  1348.  
  1349.     NFlash_Init();
  1350. #if 0
  1351.     if(NFlash_ResetDevice()){
  1352.         lgpl_printf("Reset NAND error!\n");;
  1353. //      while(1);
  1354.     }
  1355.     if( (ret = NAND_ReadData( (unsigned char *)0,
  1356.                 (unsigned char *)(&block0), sizeof(block0_t) )) != 0 ) {
  1357.         lgpl_printf("Read NAND block0 error, ret = %u!\n", ret);;
  1358. //      while(1);
  1359.         return ret;
  1360.     }
  1361.  
  1362.     nand_data.ndtr0cs0 = block0.ndtr0cs0;
  1363.     nand_data.ndtr1cs0 = block0.ndtr1cs0;
  1364.     nand_data.ndredel = block0.ndredel;
  1365.  
  1366.     NFlash_Init();
  1367.     if(NFlash_ResetDevice()){
  1368.         lgpl_printf("reinit NAND error!\n");;
  1369.         while(1);
  1370.     }
  1371. #endif
  1372.     return 0;
  1373. }
  1374.  
  1375.  
  1376.  
  1377. void DisableInterrupt(void)
  1378. {
  1379.     // int my_cpsr ;
  1380.  
  1381.     __asm__ __volatile__(
  1382.             "MRS            r0, CPSR"   "\n\t"      //get CRPR
  1383.             "BIC            r0, #0xC0"  "\n\t"      // disable IRQ and FIQ
  1384.             "MSR            CPSR, r0"  "\n\t"
  1385.             :
  1386.             :
  1387.             :"r0"
  1388.         );
  1389. }
  1390.  
  1391. inline int ctoi(char c)
  1392. {
  1393.     if((c>='0')&&(c<='9'))
  1394.         return c-'0';
  1395.     else if((c>='a')&&(c<='f'))
  1396.         return c-'a' + 10;
  1397.     else if((c>='A')&&(c<='F'))
  1398.         return c-'A' + 10;
  1399.     else
  1400.         return -1;
  1401. }
  1402.  
  1403. inline char itoc(char i)
  1404. {
  1405.     int j = i&0xf;
  1406.     if(j<10)
  1407.         return j+'0';
  1408.     else
  1409.         return j-10+'A';
  1410. }
  1411.  
  1412. #define SOC_WOL_BASE    0xf7fe0000
  1413. void set_wol_macaddr(char* mac)
  1414. {
  1415.     int i=0;
  1416.     int value[2];
  1417.     char *p=(char*)value;
  1418.  
  1419.     printf("WOL MAC address: %s\n", mac);
  1420.     for(i=0;i<6;i++){
  1421.         p[i]=(ctoi(mac[3*i])<<4) + ctoi(mac[3*i+1]);
  1422.     }
  1423. #if !CDP_A0
  1424.     writel(value[0], (SOC_WOL_BASE + RA_WOL_MAC_ADDR_LO));
  1425.     writel(value[1], (SOC_WOL_BASE + RA_WOL_MAC_ADDR_HI));
  1426. #endif /* FIXME: A0 bring up */
  1427. }
  1428.  
  1429. #if !ANDROID_BOOT
  1430. /*
  1431.  * Get MAC address from NAND
  1432.  */
  1433. static int get_mac_addr(char* macaddr)
  1434. {
  1435.     int i;
  1436.  
  1437. #define NAND_UBOOT_ENV_BLOCK_START  (12)
  1438.     int uboot_env_start = NAND_UBOOT_ENV_BLOCK_START * nand_data.szofblk;
  1439.     int uboot_env_end = 0x4000000;
  1440.     int uboot_env_size = 0x1000;
  1441.     char *env_buff = UtilMemAlloc(uboot_env_size);
  1442.     int blk_size = nand_data.szofblk;
  1443.     char *tmp_buf;
  1444.  
  1445.     for(i=0;i<4;i++) {
  1446.         if(nand_read_generic(uboot_env_start + i*blk_size,
  1447.                              uboot_env_end, (unsigned char *)env_buff,
  1448.                              uboot_env_size) >= 0) {
  1449.             break;
  1450.         }
  1451.     }
  1452.  
  1453.     if(i==4) {
  1454.         UtilMemCpy(macaddr, "macaddr=00:11:22:33:44:55", 25);
  1455.         macaddr[25]='\0';
  1456.         return -1;
  1457.     }
  1458.  
  1459.     i=0;
  1460.     while(i<uboot_env_size) {
  1461.         tmp_buf = strstr(env_buff, "ethaddr=");
  1462.         if(tmp_buf)
  1463.             break;
  1464.         i += strlen(env_buff);
  1465.         env_buff += strlen(env_buff);
  1466.         i++;
  1467.         env_buff++;
  1468.     }
  1469.     // lgpl_printf("uboot env: %s\n", env_buff);
  1470.     if(i>=uboot_env_size) {
  1471.         UtilMemCpy(macaddr, "macaddr=00:11:22:33:44:55", 25);
  1472.         macaddr[25]='\0';
  1473.         return -1;
  1474.     }
  1475.     strcpy(macaddr, "macaddr=");
  1476.     UtilMemCpy(macaddr+8, tmp_buf+8, 17);
  1477.     macaddr[25]='\0';
  1478.     return 0;
  1479. }
  1480. #endif  // !ANDROID_BOOT
  1481.  
  1482. #if !ANDROID_BOOT
  1483. static void init_mac_address(long long chipid, const char* old_param, char* new_param)
  1484. {
  1485.     int len;
  1486.     char *new = new_param;
  1487.  
  1488.     debug_printf("cpu id:%08x %08x\n", (int)(chipid>>32), (int)(chipid));
  1489.     len = strlen(old_param);
  1490.     UtilMemCpy(new, old_param, len);
  1491.     new+=len;
  1492.     *new = ' ';
  1493.     new++;
  1494.     make_mac_from_chipid(chipid, new);
  1495.     set_wol_macaddr(new+8);
  1496. /*
  1497.     UtilMemCpy(new, " macaddr=",sizeof(" macaddr=")-1);
  1498.     new+=sizeof(" macaddr=")-1;
  1499.     *new++='0';
  1500.     *new++='0';
  1501.     for(i=0;i<5;i++) {
  1502.         char hex;
  1503.         hex = chipid >> (32-8*i);
  1504.         *new++=':';
  1505.         *new++=itoc(hex>>4);
  1506.         *new++=itoc(hex);
  1507.     }
  1508.     *new='\0';
  1509. */
  1510. }
  1511.  
  1512. static long long get_next_img_addr(unsigned int start, unsigned int end, unsigned int data_size)
  1513. {
  1514.     unsigned int i,j;
  1515.     unsigned int block_size;
  1516.  
  1517.     block_size = nand_data.szofblk;
  1518.     j = (data_size + block_size - 1) & ~(block_size - 1);
  1519.     for(i=start;i<end;i+=block_size) {
  1520.         if(j==0)
  1521.             break;
  1522.         if(is_block_bad((loff_t)i)) {
  1523.             debug_printf("Bad block found @0x%08x.\n", i);
  1524.             continue;
  1525.         }
  1526.         j-=block_size;
  1527.     }
  1528.     if(i >= end)
  1529.         return -1;
  1530.  
  1531.     return i;
  1532. }
  1533. #endif  // !ANDROID_BOOT
  1534.  
  1535. #if BG2CDP
  1536. typedef struct BTLD_CTX {
  1537.     //unsigned char * gp_vt_buf; // VT_BUF_ADDR
  1538.     //unsigned char *gp_figo_buf; // FIGO_BUF_ADDR;
  1539.     //unsigned char *gp_key_buf; // KEY_BUF_ADDR;
  1540.     //unsigned char * gp_loader_buf; // Loader_BUF_ADDR;
  1541.     //int iVT_OFFSET;
  1542.     //int iLoader_OFFSET;
  1543.     unsigned blk_addr;
  1544.  
  1545.     //unsigned has_lastk;
  1546.     unsigned char lastk[1024*3];
  1547.     char partition_info_buff[4096];
  1548. } BTLD_CTX;
  1549. BTLD_CTX g;
  1550.  
  1551.  
  1552.  
  1553. unsigned berlin_delay(unsigned int tick)
  1554. {
  1555.     volatile unsigned int itick = tick * 250;
  1556.     while (itick--);
  1557.     return itick;
  1558. }
  1559.  
  1560. #define BCM_STATUS_BCM_FAULT               (1<<10)
  1561. #define BCM_STATUS_BOOTSTRAP_IN_PROGRESS   (1<<9)
  1562. #define BCM_STATUS_BCM_READY               (1<<8)
  1563. #define BCM_STATUS_BCM_CMD_FLIP            (1<<7)
  1564.  
  1565. int bcm_image_verify(unsigned int type, unsigned int src, unsigned int dst)
  1566. {
  1567.     unsigned status;
  1568.     unsigned waitCount;
  1569.     unsigned i, *p32;
  1570.     volatile NOT_MAILBOX volatile *mb = (NOT_MAILBOX *) BCM_MAILBOX;
  1571.  
  1572.     p32 = (unsigned *) src;
  1573.     lgpl_printf("\tverify 0k:0x%x, 1k:0x%x, 2k:0x%x\n", p32[1], p32[1+1024/4], p32[1+2048/4]);
  1574.  
  1575.     status = mb->command_fifo_status;
  1576.     // lgpl_printf("bcm fifo staus = 0x%x\n", status);
  1577.  
  1578.     // older erom will have BCM_STATUS_BOOTSTRAP_IN_PROGRESS set
  1579.     if (0 == (status & BCM_STATUS_BOOTSTRAP_IN_PROGRESS))
  1580.     {
  1581.         // wait for bcm ready
  1582.         // lgpl_printf("wait for bcm ready...\n");
  1583.         // for (waitCount=~255; waitCount; waitCount--)
  1584.         while (0 == (status & BCM_STATUS_BCM_READY))
  1585.         {
  1586.             if (status != mb->command_fifo_status)
  1587.             {
  1588.                 status = mb->command_fifo_status;
  1589.                 // lgpl_printf("bcm fifo staus = 0x%x\n", status);
  1590.             }
  1591.         }
  1592.     }
  1593.  
  1594.     //lgpl_printf("send BCM cmd: 0x%x 0x%x 0x%x 0x%x: 0x%x\n", BCM_PI_IMAGE_VERIFY, type, src, dst, mb-> host_interrupt_register);
  1595.  
  1596.     mb->primitive_command_parameter0 = type;
  1597.     mb->primitive_command_parameter1 = src;
  1598.     mb->primitive_command_parameter2 = dst;
  1599.     mb->secure_processor_command = BCM_PI_IMAGE_VERIFY;
  1600.  
  1601.     //for (waitCount=0; waitCount<~255; waitCount++) // Wait_For_WTM_Complete( 0x10000, pCtrl );
  1602.     for (waitCount=0; ; waitCount++) // Wait_For_WTM_Complete( 0x10000, pCtrl );
  1603.     {
  1604.         //if ((mb->command_fifo_status & BCM_STATUS_BCM_CMD_FLIP) != status)
  1605.             //break;
  1606.         // wait for "command complete" or timeout
  1607.         if( mb-> host_interrupt_register & 0x1 )
  1608.             break;
  1609.         berlin_delay(100);
  1610.     }
  1611.  
  1612.     mb-> host_interrupt_register = 0x70001; // Clear_WTM_Interrupts( 0xffffffff, pCtrl );
  1613.     status = mb->command_return_status;
  1614.     i = mb->command_status_0;
  1615.  
  1616.     lgpl_printf("\tverify image %x, size=%d, waitcount=%d\n", status, i, waitCount);
  1617.  
  1618.     return status;
  1619. }
  1620. #endif  // BG2CDP
  1621.  
  1622. #if ANDROID_BOOT
  1623. /*
  1624.  * For Android not need to load SM from bootimgs.
  1625.  * Currently, we use this solution. Because it can save the space of flash
  1626.  * memory of bootimgs & bootimgs-B. And currently, there is a bug in recoery
  1627.  * scripts, it doesn't update bootimgs-B.
  1628.  */
  1629.  
  1630. int Image_Load_And_Start(void)
  1631. {
  1632.     int ret= 0;
  1633. #ifdef ENABLE_CONNECT_BTN
  1634.         /* the second time to check connect btn */
  1635. #ifndef BG2CDP
  1636.         Check_connectbtnGPIO();
  1637. #endif
  1638. #endif
  1639.  
  1640. #ifdef ENABLE_CONNECT_BTN
  1641.         /* the third time to check connect btn */
  1642. #ifndef BG2CDP
  1643.         Check_connectbtnGPIO();
  1644. #endif
  1645. #endif
  1646.     lgpl_printf("**************************************************************** \n");
  1647.     lgpl_printf("                  Read and setup kernel Image                    \n");
  1648.     lgpl_printf("**************************************************************** \n");
  1649.     Bootmode = check_android_recovery_mode();
  1650.     if (Bootmode == BOOTMODE_BOOTUSB)
  1651.         ret = load_android_image(Bootmode, BOOT_SRC_USB);
  1652.     else
  1653.         ret = load_android_image(Bootmode, BOOT_SRC_NAND);
  1654.     if (ret != 0 && Bootmode == BOOTMODE_NORMAL) {
  1655.         lgpl_printf("Invalid normal boot image! Boot recovery image...\n");
  1656.         write_android_bootloader_message("boot-recovery", "", "recovery\n--show_text\n");
  1657.         Bootmode = BOOTMODE_RECOVERY;
  1658.         ret = load_android_image(Bootmode, BOOT_SRC_NAND);
  1659.     } else if (ret != 0 && Bootmode == BOOTMODE_RECOVERY) {
  1660.         lgpl_printf("Invalid recovery boot image! Boot normal image...\n");
  1661.         /* Note: we don't clear the recovery flags, we just let device
  1662.          *       to enter recovery mode if recovery mode is avaliable.
  1663.          * write_android_bootloader_message("", "", "");
  1664.          */
  1665.  
  1666.         Bootmode = BOOTMODE_NORMAL;
  1667.         ret = load_android_image(BOOTMODE_NORMAL, BOOT_SRC_NAND);
  1668.     }
  1669.     if (ret != 0) {
  1670.         /* Booting from NAND failed, try boot from USB */
  1671.         lgpl_printf("Booting from NAND failed, booting from USB....!\n");
  1672.         Bootmode = BOOTMODE_BOOTUSB;
  1673.         ret = load_android_image(Bootmode, BOOT_SRC_USB);
  1674.     }
  1675.     if (ret != 0){
  1676.         lgpl_printf("FATAL ERROR! There is no bootable image on this machine!\n");
  1677.         return -1;
  1678.     }
  1679.  
  1680.     /* Turn on Red LED indicating booting */
  1681.     set_led_backlight(LED_RED_PWM, 100);
  1682.  
  1683.     /*
  1684.      * setup linux boot parameter
  1685.      */
  1686.     setup_android_kernel_param(Bootmode);
  1687.  
  1688.     return 0;
  1689. }
  1690.  
  1691. #else /* !#if ANDROID_BOOT */
  1692. /* For
  1693.  * - Linux
  1694.  * - Android needs to load SM from bootimgs
  1695.  */
  1696. int Image_Load_And_Start()
  1697. {
  1698.     int iPageSize = 0;
  1699.     int iBlockSize  ;
  1700.  
  1701.     int ret= 0;
  1702.  
  1703.     unsigned int img3_start, img3_end;
  1704.     unsigned int en_size;
  1705.  
  1706.     char *sm_buff, *cpu0_buff, *cpu1_buff, *en_buff;
  1707.     int sm_addr, cpu0_addr, cpu1_addr, recou_addr, en_addr;
  1708.  
  1709.     long long chipid;
  1710.  
  1711.     iPageSize = nand_data.szofpg ;
  1712.     iBlockSize = nand_data.szofblk;
  1713.  
  1714. #ifdef ENABLE_CONNECT_BTN
  1715.         /* the second time to check connect btn */
  1716.         Check_connectbtnGPIO();
  1717. #endif
  1718.  
  1719.     if((vt_img3.part1_version.major_version > vt_img3.part2_version.major_version)
  1720.         || ((vt_img3.part1_version.major_version == vt_img3.part2_version.major_version)
  1721.          &&(vt_img3.part1_version.minor_version > vt_img3.part2_version.minor_version)))
  1722.     {
  1723.         img3_start = vt_img3.part1_start_blkind;
  1724.         img3_end = img3_start + vt_img3.part1_blks;
  1725.         img3_start *= iBlockSize;
  1726.         img3_end *= iBlockSize;
  1727.     }
  1728.     else
  1729.     {
  1730.         img3_start = vt_img3.part2_start_blkind;
  1731.         img3_end = img3_start + vt_img3.part2_blks;
  1732.         img3_start *= iBlockSize;
  1733.         img3_end *= iBlockSize;
  1734.     }
  1735.  
  1736.     /****************************************************************
  1737.      * Read Image 3 Header
  1738.      ****************************************************************/
  1739.     debug_printf("Image3 start:%08x, end: %08x\n", img3_start, img3_end);
  1740.  
  1741.     debug_printf("Reading image3 attribute ...\n");
  1742.  
  1743.     sm_addr = nand_read_generic(img3_start, img3_end, &img3_hdr, sizeof(Image3_Attr));
  1744.     if(sm_addr < 0)
  1745.     {
  1746.         lgpl_printf("Read image3 header error!\n");
  1747.         return -1;
  1748.     }
  1749.     //strcpy(img3_hdr.linux_bootargs, LINUX_PARAM_COMMON_PART);
  1750.     UtilMemCpy(img3_hdr.linux_bootargs, linux_bootargs, ((sizeof(linux_bootargs)+3)&~3));
  1751.  
  1752.     sm_hdr = &img3_hdr.sm_param;
  1753.     cpu0_hdr = &img3_hdr.cpu0_param;
  1754.     cpu1_hdr = &img3_hdr.cpu1_param;
  1755.     recou_hdr = &img3_hdr.recou_param;
  1756.     sMem_Layout = &img3_hdr.mem_layout;
  1757.  
  1758. #ifdef CONFIG_FORCE_ENCRYPTION
  1759.     sm_hdr->bsm_image_encrypt = 1;
  1760.     cpu0_hdr->bcpu0_image_encrypt = 1;
  1761.     cpu1_hdr->bcpu1_image_encrypt = 1;
  1762. #endif  // CONFIG_FORCE_ENCRYPTION
  1763.     en_size = sm_hdr->bsm_image_encrypt | cpu0_hdr->bcpu0_image_encrypt | cpu1_hdr->bcpu1_image_encrypt;
  1764.  
  1765.     cpu0_addr = get_next_img_addr(sm_addr, img3_end, sm_hdr->uism_image_final_size);
  1766.     cpu1_addr = get_next_img_addr(cpu0_addr, img3_end, cpu0_hdr->uicpu0_image_final_size);
  1767.     recou_addr = get_next_img_addr(cpu1_addr, img3_end, cpu1_hdr->uicpu1_image_final_size);
  1768.     en_addr = get_next_img_addr(recou_addr, img3_end, recou_hdr->size);
  1769.  
  1770.     debug_printf("sm_addr: %08x, cpu0_addr: %08x, cpu1_addr: %08x, recou_addr: %08x, en_addr: %08x\n",
  1771.             sm_addr, cpu0_addr, cpu1_addr, recou_addr, en_addr);
  1772.     debug_printf("Memory map: CPU0 %uM, CPU1 %uM, ZSP: %uM, SHR %uM\n",
  1773.             sMem_Layout->uicpu0_mem_size>>20, sMem_Layout->uicpu1_mem_size>>20,
  1774.             sMem_Layout->uizsp_mem_size>>20, sMem_Layout->uishm_mem_size>>20);
  1775.     if((cpu0_addr < 0) || (cpu1_addr < 0) || (recou_addr < 0) || (en_addr < 0))
  1776.     {
  1777.         lgpl_printf("Scan image3 error.\n");
  1778.         lgpl_printf("sm_addr: %08x, cpu0_addr: %08x, cpu1_addr: %08x, recou_addr: %08x, en_addr: %08x\n",
  1779.             sm_addr, cpu0_addr, cpu1_addr, recou_addr, en_addr);
  1780.         return -1;
  1781.     }
  1782.  
  1783.     if (!en_size)
  1784.     {
  1785.       UtilMemSet((void *)&en_hdr, 0, sizeof(en_hdr));
  1786.     }
  1787.     else
  1788.     {
  1789.  
  1790.       if(nand_read_generic(en_addr, img3_end, &en_hdr, sizeof(en_hdr)) < 0)
  1791.       {
  1792.         lgpl_printf("Read image3 header error!\n");
  1793.         return -1;
  1794.       }
  1795.  
  1796.  
  1797.       //    debug_printf("Encryption atrribute:\n");
  1798.       //    for(k=0;k<sizeof(en_hdr)/4;k++) debug_printf("0x%08x.\n", ((int *)&en_hdr)[k]);
  1799.       // currently, we don't support CPU1 image
  1800.       en_size = en_hdr.uiCPU0_Encrypt_Image_Header_Offset + en_hdr.uiCPU0_Encrypt_Image_Header_Size;
  1801.       debug_printf("Encryption image size= %d\n", en_size);
  1802.       en_buff = UtilMemAlloc(en_size);
  1803.       if(!en_buff) {
  1804.         lgpl_printf("Malloc failed.\n");
  1805.         return -1;
  1806.       }
  1807.       if(nand_read_generic(en_addr, img3_end, (unsigned char *)en_buff, en_size) < 0)
  1808.       {
  1809.         lgpl_printf("Read image3 header error!\n");
  1810.         return -1;
  1811.       }
  1812.     }
  1813.  
  1814.     debug_printf("encryption header copied\n");
  1815. #ifdef BG2CDP
  1816. #else
  1817.     /****************************************************************
  1818.      * Load and start SM
  1819.      ****************************************************************/
  1820.     {
  1821.           // make Beetle "DDR_Off" case like the other warmups: there's no need to reload
  1822.           // SM. Even though DDR is gone, it doesn't affect SM.
  1823.  
  1824.                 /*  ( MV_SoC_STATE_SYSRESET != uiBoot ) */ ))
  1825.     {
  1826.         //  for cold boot up, Image-2 load SM's image and kick off Sm
  1827.  
  1828.  
  1829.         //  when box product release is defined, SM will shut down SoC after power on.
  1830.         //  this can be done by write the cold boot flag to SM's register
  1831. #ifdef  BOX_PRODUCT_RELEASE
  1832.         {
  1833. #else
  1834.         if(false) {
  1835. #endif
  1836.             debug_printf("Set cold boot flag: %08x@%08x\n", MV_SM_RSTFLAG_COLD, SM_BOOT_FLAG_REG);
  1837.             writel(MV_SM_RSTFLAG_COLD, SM_BOOT_FLAG_REG);
  1838.         }
  1839.  
  1840.         // copy SM image from NAND
  1841.         debug_printf("Loading SM image... final size %u\n", sm_hdr->uism_image_final_size);
  1842.         if(!sm_hdr->bsm_image_encrypt)
  1843.             en_hdr.uiSM_Encrypt_Image_Header_Size = 0;
  1844.  
  1845.         sm_buff = UtilMemAlloc(sm_hdr->uism_image_final_size + en_hdr.uiSM_Encrypt_Image_Header_Size);
  1846.         if(!sm_buff) {
  1847.             lgpl_printf("Malloc failed.\n");
  1848.             return -1;
  1849.         }
  1850.  
  1851.         UtilMemCpy(sm_buff, en_buff + en_hdr.uiSM_Encrypt_Image_Header_Offset, en_hdr.uiSM_Encrypt_Image_Header_Size);
  1852.  
  1853.         if(nand_read_generic(sm_addr, cpu0_addr, (sm_buff+en_hdr.uiSM_Encrypt_Image_Header_Size), sm_hdr->uism_image_final_size) < 0)
  1854.         {
  1855.             lgpl_printf("Read sm image error!\n");
  1856.             return -1;
  1857.         }
  1858.  
  1859.         if(sm_hdr->bsm_image_encrypt) {
  1860.             debug_printf("Encryption key index: %u\n", sm_buff[0]);
  1861.             ret = VerifyImage((unsigned int)sm_buff, sm_hdr->uism_encrypt_image_size, (unsigned int)sm_buff);
  1862.             if(ret) {
  1863.                 lgpl_printf("Verify SM image failed! ret=%d\n", ret);
  1864.                 return -1;
  1865.             }
  1866.             debug_printf("verify passed.\n");
  1867.         }
  1868.  
  1869.         if(sm_hdr->bsm_image_bss_init)
  1870.             UtilMemSet(sm_buff + sm_hdr->uism_bss_start_addr - sm_hdr->uism_image_load_addr, 0, sm_hdr->uism_bss_length);
  1871.  
  1872. #ifdef ENABLE_CONNECT_BTN
  1873.         /* the third time to check connect btn */
  1874.         Check_connectbtnGPIO();
  1875. #endif
  1876.  
  1877.         debug_printf("Done.\n");
  1878.         //  download SM image from DDR to ITCM and kick off SM
  1879.         MV_SM_Dev_Init((unsigned char*)sm_buff, sm_hdr->uism_image_ori_size ) ;
  1880.         debug_printf("SM CPU is kicked off.\n");
  1881.  
  1882.         //  send msg to SM to power off SoC
  1883.         //  FIGO_SRAM_ENTRY() ;
  1884.     }
  1885. #endif  // CONFIG_SM
  1886.  
  1887.     /****************************************************************
  1888.      * Read and setup kernel Image
  1889.      ****************************************************************/
  1890.  
  1891. #define mem_buff cpu0_buff
  1892.     cpu0_buff = (char *)cpu0_hdr->uicpu0_image_load_addr - sizeof(linux_hdr_t);
  1893.     {
  1894.  
  1895.         debug_printf("Loading CPU0 image... load address: 0x%x, final size %u\n", (int)cpu0_buff,cpu0_hdr->uicpu0_image_final_size);
  1896.         if(cpu0_hdr->bcpu0_image_encrypt)
  1897.             UtilMemCpy(cpu0_buff, en_buff + en_hdr.uiCPU0_Encrypt_Image_Header_Offset, en_hdr.uiCPU0_Encrypt_Image_Header_Size);
  1898.         else
  1899.             en_hdr.uiCPU0_Encrypt_Image_Header_Size = 0;
  1900.  
  1901.         if(nand_read_generic(cpu0_addr, cpu1_addr, (cpu0_buff+en_hdr.uiCPU0_Encrypt_Image_Header_Size), cpu0_hdr->uicpu0_image_final_size) < 0)
  1902.         {
  1903.             lgpl_printf("Read CPU0 image error!\n");
  1904.             return -1;
  1905.         }
  1906.  
  1907.         if(cpu0_hdr->bcpu0_image_encrypt) {
  1908.             #if BG2CDP
  1909.             ret = load_lastk(g.lastk);
  1910.             if (ret)
  1911.             {
  1912.                 lgpl_printf("load last key failed!ret=0x%x\n", ret);
  1913.                 return -1;
  1914.             }
  1915.             ret = bcm_image_verify(BCM_IMG_KERNEL_TYPE, (unsigned) mem_buff, (unsigned) cpu0_buff);
  1916.             if (ret)
  1917.             {
  1918.                 lgpl_printf("Verify cpu0 image failed!ret=0x%x\n", ret);
  1919.                 return -1;
  1920.             }
  1921.             #else
  1922.             debug_printf("Encryption key index: %u\n", cpu0_buff[0]);
  1923.             ret = VerifyImage((unsigned int)cpu0_buff, cpu0_hdr->uicpu0_encrypt_image_size, (unsigned int)cpu0_buff);
  1924.             if(ret) {
  1925.                 lgpl_printf("Verify cpu0 image failed!ret=%d\n", ret);
  1926.                 return -1;
  1927.             }
  1928.             #endif
  1929.             debug_printf("verify passed.\n");
  1930.         }
  1931.  
  1932.     //  Xiufeng:    do not use ramdisk for BG2
  1933. #if 1
  1934.     {
  1935.         linux_hdr_t *lhdr = cpu0_buff;
  1936.         int len;
  1937.  
  1938.         debug_printf("kernel_size=%d, ramdisk_size=%d, ramdisk_addr=%x\n",
  1939.                 lhdr->kernel_size, lhdr->ramdisk_size, lhdr->ramdisk_addr);
  1940.         debug_printf("reloading ramdisk...");
  1941.  
  1942.         UtilMemCpy(lhdr->ramdisk_addr,
  1943.                 cpu0_hdr->uicpu0_image_load_addr+lhdr->kernel_size,
  1944.                 lhdr->ramdisk_size);
  1945.  
  1946.  
  1947.         debug_printf("DONE!\n");
  1948.  
  1949.         debug_printf("append initrd to linux bootargs\n");
  1950.         len = strlen((char *)img3_hdr.linux_bootargs);
  1951.         snprintf(img3_hdr.linux_bootargs + len,
  1952.             sizeof(img3_hdr.linux_bootargs) - len, " initrd=0x%x,0x%x",
  1953.                 lhdr->ramdisk_addr, lhdr->ramdisk_size);
  1954. #if defined(ROOTFS_SQUASHFS)
  1955.         debug_printf("append rootfs type to linux bootargs\n");
  1956.         len = strlen((char *)img3_hdr.linux_bootargs);
  1957.         snprintf(img3_hdr.linux_bootargs + len,
  1958.             sizeof(img3_hdr.linux_bootargs) - len),
  1959.             " rootfstype=squashfs");
  1960. #endif
  1961.         debug_printf("%s\n", img3_hdr.linux_bootargs);
  1962.     }
  1963. #endif
  1964.     if(cpu0_hdr->bcpu0_image_bss_init)
  1965.         UtilMemSet((void*)cpu0_hdr->uicpu0_bss_start_addr, 0, cpu0_hdr->uicpu0_bss_length);
  1966.  
  1967.     }
  1968.     debug_printf("Done.\n");
  1969.  
  1970.         /*
  1971.          * setup linux boot parameter
  1972.          */
  1973.         //  if MV88DE3010, try to use CPUID as MAC address
  1974. #if !CDP_A0
  1975.         chipid = readl(MV88DE3100_CHIPID_REG1) ;
  1976.         chipid += (long long)(readl(MV88DE3100_CHIPID_REG2)) << 32;
  1977. #else
  1978.         chipid = 0x12345678abcdef0;
  1979. #endif /* FIXME: A0 bring up */
  1980.  
  1981.         if(chipid && block0.first_burn_flag){
  1982.             char tmp_param[1024];
  1983.             char tmp_buf[64];
  1984.             init_mac_address(chipid, (char *)(&img3_hdr.linux_bootargs), tmp_param);
  1985.             debug_printf("Real bootargs: %s\n", tmp_param);
  1986.             setup_linux_bootparam(cpu0_hdr->uicpu0_image_load_addr-0x8000, sMem_Layout->uicpu0_mem_size, tmp_param);
  1987.         } else {
  1988.             char tmp_param1[1024];
  1989.             char mac_addr[32];
  1990.  
  1991.             get_mac_addr(mac_addr);
  1992.             set_wol_macaddr(&mac_addr[8]);
  1993.  
  1994.             strlcpy(tmp_param1, img3_hdr.linux_bootargs, sizeof(tmp_param1));
  1995.             strcat(tmp_param1, " ");
  1996.             strncat(tmp_param1, mac_addr, sizeof(mac_addr));
  1997.             {
  1998.                 char tmp_buf[64];
  1999.                 //  Xiufeng:    Fix me
  2000.                 //  The No.2 parameter uicpu0_mem_size, is not used by Linux.
  2001.  
  2002.                 debug_printf("REal bootargs: %s\n", tmp_param1);
  2003.                 setup_linux_bootparam(cpu0_hdr->uicpu0_image_load_addr-0x8000,
  2004.                     sMem_Layout->uicpu0_mem_size, tmp_param1);
  2005.  
  2006.             }
  2007.             debug_printf("%d: Done!\n", __LINE__);
  2008.         }
  2009. #if 0
  2010.             setup_linux_bootparam(cpu0_hdr->uicpu0_image_load_addr-0x8000,
  2011.                     sMem_Layout->uicpu0_mem_size, LINUX_BOOTARGS);
  2012.  
  2013. #endif
  2014.         //  Xiufeng: Here we set CHIP_CTRL_SW_GENERIC3_REG to 1 to keep consistent with Berlin
  2015.         //       In ideal case, BG2 Linux does not need NFC sync, but bootflow does not
  2016.         //       rely on this
  2017.         reg(CHIP_CTRL_SW_GENERIC3_REG) = 1;//allow Linux to use NFC
  2018.  
  2019.     return 0;
  2020. }
  2021. #endif  // ANDROID_BOOT
  2022.  
  2023. #include "bcm_primitive.h"
  2024. #include "bcm_status.h"
  2025. #include "bcm_mailbox.h"
  2026.  
  2027. // if here, Image-3 is copied into DDR
  2028. int Image_3_Verify_Jump(void)
  2029. {
  2030.     // write 0xBF0A0001 to CHIP_CTRL_SW_GENERIC0_REG
  2031.     // This is the flag to eCos passed by Bootflow. This value means the AVPLL
  2032.     // has been set by bootflow, eCos shall not reset AVPLL
  2033.     GA_REG_WORD32_WRITE(CHIP_CTRL_SW_GENERIC0_REG, 0xBF0A0001) ;
  2034.  
  2035.     // any state reach here should start/resume kernel
  2036. #if ANDROID_BOOT
  2037.     {
  2038.         /* flush the console output */
  2039.         putchar('\n');
  2040.         bcm_usb_console_func(EROM_TURN_OFF_USB_DEVICE);
  2041.         start_android_kernel();
  2042.     }
  2043. #else /* #if ANDROID_BOOT */
  2044.  
  2045.     reg(CHIP_CTRL_SW_GENERIC3_REG) = 0x1 ;
  2046.     {
  2047.         lgpl_printf("JUMp to kernel entrance 0x%08x\n", cpu0_hdr->uicpu0_image_load_addr);
  2048.         //parameter0: Linux load address
  2049.         //parameter1: 1330
  2050.         //parameter2: Linux Command line address
  2051.  
  2052.         /* flush the console output */
  2053.         putchar('\n');
  2054.         bcm_usb_console_func(EROM_TURN_OFF_USB_DEVICE);
  2055.         __reset_cpu(cpu0_hdr->uicpu0_image_load_addr, 1330, cpu0_hdr->uicpu0_image_load_addr-0x8000) ;
  2056.     }
  2057. #endif  // ANDROID_BOOT
  2058.  
  2059.     return 0;
  2060. }
  2061.  
  2062. void dump_version_entry(ver_table_entry_t * vte)
  2063. {
  2064.     printf("%s: part1(start=%d, blks=%d, version=%08u%04u), part2(start=%d, blks=%d, version=%08u%04u)\n", vte->name,
  2065.             vte->part1_start_blkind, vte->part1_blks, vte->part1_version.major_version, vte->part1_version.minor_version,
  2066.             vte->part2_start_blkind, vte->part2_blks, vte->part2_version.major_version, vte->part2_version.minor_version);
  2067. }
  2068.  
  2069. unsigned int crc32(unsigned int crc, unsigned char *buf, unsigned int len);
  2070. #define CRC32_SIZE      (4)
  2071. int get_version_table()
  2072. {
  2073.     unsigned int i,j,ret=0;
  2074.     int img3_found;
  2075. #if ANDROID_BOOT
  2076.     int android_kernel_found=0;
  2077.     int android_recovery_found=0;
  2078. #endif
  2079.     version_table_t *vt = (version_table_t *)g.partition_info_buff;
  2080.     ver_table_entry_t * vt_entry;
  2081.     unsigned vt_size = 0;
  2082.     unsigned int dev_id = 0;
  2083.     unsigned iVT_OFFSET;
  2084.  
  2085.     #if BG2CDP
  2086.     iVT_OFFSET = nand_data.szofblk - 4096;
  2087.     #else
  2088.     iVT_OFFSET = VT_OFFSET;
  2089.     #endif
  2090.  
  2091.  
  2092.     for(i=1; i<9;i++) {
  2093.         if(is_block_bad((loff_t)i*nand_data.szofblk)) {
  2094.             debug_printf("Bad block found @0x%08x.\n", i*nand_data.szofblk);
  2095.             continue;
  2096.         }
  2097.  
  2098.         ret = mv_nand_read_block( (long long)i*nand_data.szofblk + iVT_OFFSET,
  2099.             g.partition_info_buff, sizeof(g.partition_info_buff) );
  2100.         if( ret != sizeof(g.partition_info_buff) ) {
  2101.             debug_printf("Read version table failed, i=%u, ret=%u.\n", i, ret);
  2102.             continue;
  2103.         }
  2104.         vt_size = sizeof(version_table_t) + vt->num_entries * sizeof(ver_table_entry_t);
  2105.         /* verify the version table data */
  2106.         if(vt->magic != MAGIC_NUMBER || 0xffffffff != crc32(0, (UCHAR *)g.partition_info_buff, vt_size + CRC32_SIZE)) {
  2107.             lgpl_printf("error magic number! %x\n", vt->magic);
  2108.             continue;
  2109.         }
  2110.  
  2111.         UtilMemCpy(g.lastk, &g.partition_info_buff[3*1024], 1024);
  2112.  
  2113.         dev_id = 0;
  2114.         img3_found = 0;
  2115.         for(j=0;j<vt->num_entries;j++) {
  2116.             vt_entry = &vt->table[j];
  2117.             printf("[%02d,d%02d] ", j, dev_id);
  2118.             dump_version_entry(vt_entry);
  2119.             if(UtilMemCmp(IMG3_NAME, vt_entry->name, sizeof(IMG3_NAME)) == 0) {
  2120.                 UtilMemCpy(&vt_img3, vt_entry, sizeof(ver_table_entry_t));
  2121.                 img3_found = 1;
  2122.             }
  2123.             #if 0
  2124.             if(UtilMemCmp(SYSCONF_NAME, vt_entry->name, sizeof(SYSCONF_NAME)) == 0) {
  2125.                 UtilMemCpy(&vt_sysconf, vt_entry, sizeof(ver_table_entry_t));
  2126.             }
  2127.             if(UtilMemCmp(FLASHLESS_NAME, vt_entry->name, sizeof(FLASHLESS_NAME)) == 0) {
  2128.                 UtilMemCpy(&vt_flashless, vt_entry, sizeof(ver_table_entry_t));
  2129.             }
  2130.             #endif
  2131. #if ANDROID_BOOT
  2132.             if(UtilMemCmp(FTS_NAME, vt_entry->name, sizeof(FTS_NAME)) == 0) {
  2133.                 fts_dev_id = dev_id;
  2134.                 UtilMemCpy((void *)&vt_fts, vt_entry, sizeof(ver_table_entry_t));
  2135.             }
  2136.             if(UtilMemCmp(KERNEL_NAME, vt_entry->name, sizeof(KERNEL_NAME)) == 0) {
  2137.                 UtilMemCpy((void *)&vt_kernel, vt_entry, sizeof(ver_table_entry_t));
  2138.                 android_kernel_found = 1;
  2139.             }
  2140.             if(UtilMemCmp(RECOVERY_NAME, vt_entry->name, sizeof(RECOVERY_NAME)) == 0) {
  2141.                 UtilMemCpy((void *)&vt_recovery, vt_entry, sizeof(ver_table_entry_t));
  2142.                 android_recovery_found = 1;
  2143.             }
  2144. #endif
  2145.             if (vt_entry->part1_start_blkind != vt_entry->part2_start_blkind)
  2146.                 /* double copy of the partition */
  2147.                 dev_id += 2;
  2148.             else
  2149.                 dev_id += 1;
  2150.         }
  2151. #if ANDROID_BOOT
  2152.         if(img3_found && android_kernel_found && android_recovery_found)
  2153.             break;
  2154. #else
  2155.         if(img3_found)
  2156.             break;
  2157. #endif
  2158.     }
  2159.  
  2160.     if(i==9) {
  2161.         lgpl_printf("Scan version table error!\n");
  2162.         return -1;
  2163.     }
  2164.     return 0;
  2165. }
  2166. #if 0 //BG2CDP
  2167. MV_Block1_Item layout_item;
  2168. MV_Block1_Item *loader_item;
  2169.  
  2170. int get_block1_layout(void *buf)
  2171. {
  2172.     int i, type;
  2173.     int iVT_OFFSET = VT_OFFSET;
  2174.     MV_Block1 *Layout = (MV_Block1 *) buf;
  2175.  
  2176.     if (Layout->head.MagicNumber == 0xda7a)
  2177.     {
  2178.         for (i=0; i<8; i++)
  2179.         {
  2180.             type = Layout->item[i].param&0xF;
  2181.             if (type == BLOCK1_IMAGE_TYPE_VT)
  2182.             {
  2183.                 iVT_OFFSET = Layout->item[i].start;
  2184.                 //debug_printf("VT block @0x%08x.\n", iVT_OFFSET);
  2185.                 break;
  2186.             }
  2187.         }
  2188.     }
  2189.     return iVT_OFFSET;
  2190. }
  2191.  
  2192. unsigned status_32[17];
  2193. int bcm_image_verify(unsigned int type, unsigned int src, unsigned int dst)
  2194. {
  2195.     unsigned waitCount;
  2196.     unsigned i, *p32;
  2197.     NOT_MAILBOX volatile *mb = (NOT_MAILBOX *) BCM_MAILBOX;
  2198.  
  2199.     p32 = &mb->primitive_command_parameter0;
  2200.     p32[0] = type;
  2201.     p32[1] = src;
  2202.     p32[2] = dst;
  2203.     for (i=3; i<16; i++)
  2204.         p32[i] = 0;
  2205.     p32[i] = BCM_PI_IMAGE_VERIFY;
  2206.  
  2207.     lgpl_printf("Verifying cpu0 image @0x%x ... ", src);
  2208.  
  2209.     //for (waitCount=0x10000000; waitCount; waitCount--) // Wait_For_WTM_Complete( 0x10000, pCtrl );
  2210.     //for (waitCount=0; waitCount<0x1000000; waitCount++) // Wait_For_WTM_Complete( 0x10000, pCtrl );
  2211.     for (waitCount=0; ; waitCount++) // Wait_For_WTM_Complete( 0x10000, pCtrl );
  2212.     {
  2213.         // wait for "command complete" or timeout
  2214.         if( mb-> host_interrupt_register & 0x1 )
  2215.             break;
  2216.         //if (waitCount>~7)
  2217.             //break;
  2218.     }
  2219.     lgpl_printf("countup = 0x%08x.\n", waitCount);
  2220.  
  2221.     status_32[0] = ~0;
  2222.     status_32[1] = 0;
  2223.     //if( waitCount != 0 )
  2224.     {
  2225.         p32 = &mb-> command_return_status;
  2226.         for (i=0; i<17; ++i)
  2227.             status_32[i] = p32[i];
  2228.         //mb-> host_interrupt_register = ~0; // Clear Interrupts
  2229.     }
  2230.     mb-> host_interrupt_register = 0x70001; // Clear Interrupts
  2231.     lgpl_printf("Verify status=0x%x, size=%d.\n", status_32[0], status_32[1]);
  2232.  
  2233.     return status_32[0];
  2234. }
  2235. #endif
  2236.  
  2237. //char partition_info_buff[4096 + 32];
  2238. unsigned char partition_type_s[16];
  2239. #if defined(CONFIG_SLC)
  2240. extern unsigned int slc_partition_start;
  2241. #endif
  2242. int gen_nand_partition_info()
  2243. {
  2244.     unsigned int i, ret=0;
  2245.  
  2246.     version_table_t *vt = (version_table_t *)g.partition_info_buff;
  2247.     unsigned vt_size = 0;
  2248.     unsigned iVT_OFFSET;
  2249.  
  2250.     #if BG2CDP
  2251.     iVT_OFFSET = nand_data.szofblk - 4096;
  2252.     #else
  2253.     iVT_OFFSET = VT_OFFSET;
  2254.     #endif
  2255.  
  2256.     strncpy((CHAR *)partition_type_s, "eslc", 4);
  2257.  
  2258.     for(i=1; i<9;i++) {
  2259.         if(is_block_bad((loff_t)i*nand_data.szofblk)) {
  2260.             lgpl_printf("Bad block found @0x%08x.\n", i*nand_data.szofblk);
  2261.             continue;
  2262.         }
  2263.  
  2264.         // go get vt
  2265.         ret = mv_nand_read_block( (long long)i*nand_data.szofblk + iVT_OFFSET,
  2266.             (CHAR *)g.partition_info_buff, sizeof(g.partition_info_buff) );
  2267.         if( ret != sizeof(g.partition_info_buff) ) {
  2268.             lgpl_printf("get partition info failed, i=%u, ret=%u.\n", i, ret);
  2269.             continue;
  2270.         }
  2271.         vt_size = sizeof(version_table_t) + vt->num_entries * sizeof(ver_table_entry_t);
  2272.         /* verify the version table data */
  2273.         if(vt->magic != MAGIC_NUMBER || 0xffffffff != crc32(0, (UCHAR *)g.partition_info_buff, vt_size + CRC32_SIZE)){
  2274.             lgpl_printf("error magic number! %x\n", vt->magic);
  2275.         }else
  2276.             break;
  2277.     }
  2278.     if(i == 9) {
  2279.         lgpl_printf("Scan partition info error!\n");
  2280.         return -1;
  2281.     }
  2282.     return 0;
  2283. }
  2284.  
  2285. void dump_mem(unsigned int addr, int count)
  2286. {
  2287.     int i;
  2288.     for (i = 0; i < count; i++, addr += 4) {
  2289.         if (i % 4 == 0) {
  2290.             if (i)
  2291.                 printf("\n");
  2292.             printf("0x%08x: ", addr);
  2293.         }
  2294.         printf("%08x ", readl(addr));
  2295.     }
  2296.     printf("\n");
  2297. }
  2298.  
  2299. #if defined(DEBUG)
  2300. static unsigned int read_otp_status(void)
  2301. {
  2302.     return readl(MV88DE3XXX_OTP_STS_REG);
  2303. }
  2304.  
  2305. static unsigned int read_otp_lkg_curr(void)
  2306. {
  2307.     return readb(MV88DE3XXX_OTP_LKG_ID) * 4;
  2308. }
  2309. #endif
  2310.  
  2311. void reset_usb_console();
  2312.  
  2313. void StartBootLoader(int block_size, int page_size, int ecc_strength)
  2314. {
  2315.     int ret = 0;
  2316.     uiWarmDown_2_Linux_Addr = ~0; //readl((SM_WARMDOWN_2_LINUX_ADDR)) ;
  2317.  
  2318.     UtilMemSet((unsigned char *)BSS_START, 0, BSS_END - BSS_START);
  2319.  
  2320.     pin_init();
  2321.  
  2322.     reset_usb_console();
  2323.  
  2324. #if BG2CDP
  2325.     lgpl_printf("%s BG2CD+: %x\n", BOARD_NAME, BERLIN_CHIP_VERSION);
  2326.     /* unmask WDT reset signal */
  2327.     writel(0, 0xF7FCD03C);
  2328.     /* enable CA7 CPU Timer*/
  2329.     writel(1, 0xF7920034);
  2330.  
  2331.     /* set up APB Timer*/
  2332.     writel(1000*1000, 0xF7E82c00);  /* reload value */
  2333.     writel(7, 0xF7E82C08);      /* ENABLE | PERIODIC | IRQ_MASK*/
  2334.  
  2335. #if !CDP_A0
  2336.     /* pinmux */
  2337.     writel(0x09004800, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x00);
  2338.     writel(0x00000001, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x04);
  2339.     writel(0x09000000, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x08);
  2340.     writel(0x00001249, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x0c);
  2341.  
  2342.     writel(0x00000009, SM_SYS_CTRL_REG_BASE + RA_smSysCtl_smPinMuxCntlBus);
  2343.  
  2344.     /* invert the polarity of PMU IRQ */
  2345.     writel(0x01800000, 0xF7CA00B8);
  2346. #else /* BG2-CDP A0 */
  2347.     writel(0x08000000, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x04);
  2348.     /* invert the polarity of PMU IRQ */
  2349.     writel(0x01800000, MEMMAP_SOC_REG_BASE + 0x04);
  2350.  
  2351. #if (CFG_BOARD_NAME == BOARD_CHIRP_B1) \
  2352.         || (CFG_BOARD_NAME == BOARD_CHIRP_B3) \
  2353.         || (CFG_BOARD_NAME == BOARD_CHIRP_B4) \
  2354.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B1) \
  2355.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B3) \
  2356.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  2357.     GPIO_PortWrite(TOUCH_RESET_GPIO, 1);
  2358.     writel(0x00000252, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x00);
  2359.     // TODO may be for uniformity, find an absoute value to write to this pinmux register instead of having to read it first
  2360.     unsigned int reg_val = readl(MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x04);
  2361.     reg_val = reg_val | 0x00C0000;
  2362.     reg_val = reg_val & 0xFFEFFFFF;
  2363.     writel(reg_val, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x04);
  2364.     writel(0x0838A249, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x08);
  2365.     writel(0x00009B64, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x0c);
  2366.     writel(0x000000B6, SM_SYS_CTRL_REG_BASE + RA_smSysCtl_smPinMuxCntlBus);
  2367.     udelay(100); // keep TOUCH_RESET_GPIO HIGH for 100 usec
  2368.     GPIO_PortWrite(TOUCH_RESET_GPIO, 0);
  2369.     udelay(1000); // add 1 ms delay to allow ATMEL MCU to go to booloader mode TODO: test that delay is really 1 ms
  2370.     GPIO_PortWrite(TOUCH_RESET_GPIO, 1); //now toggle the reset pin back to 1
  2371. #elif (CFG_BOARD_NAME == BOARD_HENDRIX_B1) \
  2372.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B3) \
  2373.         || (CFG_BOARD_NAME == BOARD_HENDRIX_B4)
  2374.     GPIO_PortWrite(AUD_MUTE_GPIO, 0); // set AUD_MUTE# pin to output 0
  2375.     writel(0x00000252, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x00);
  2376.     writel(0x08388249, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x08);
  2377.     writel(0x00009B64, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x0c);
  2378.     writel(0x000001B6, SM_SYS_CTRL_REG_BASE + RA_smSysCtl_smPinMuxCntlBus);
  2379. #else
  2380.     writel(0x00000000, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x00);
  2381.     writel(0x08208249, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x08);
  2382.     writel(0x00009264, MEMMAP_CHIP_CTRL_REG_BASE + RA_Gbl_pinMuxCntlBus + 0x0c);
  2383.     writel(0x00000064, SM_SYS_CTRL_REG_BASE + RA_smSysCtl_smPinMuxCntlBus);
  2384. #endif
  2385.  
  2386. #endif /* CDP_A0 */
  2387. #else
  2388.     lgpl_printf("\n%s %s\n", BOARD_NAME, CHIP_VERSION);
  2389. #endif
  2390.     board_gpio_init();
  2391.     /* Turn on red LED */
  2392.     set_led_backlight(LED_RED_PWM, 100);
  2393. // TODO(yichunko): Joplin uses only led_frame_15 and has different # of LEDs
  2394. #if (CFG_BOARD_NAME == BOARD_CHIRP_B1) \
  2395.         || (CFG_BOARD_NAME == BOARD_CHIRP_B3) \
  2396.         || (CFG_BOARD_NAME == BOARD_CHIRP_B4) \
  2397.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B1) \
  2398.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B3) \
  2399.         || (CFG_BOARD_NAME == BOARD_JOPLIN_B4)
  2400.     diag_i2c_led_init(LED_MASTER_ID, 0x15);
  2401.     diag_i2c_led_init(LED_MASTER_ID, 0x17);
  2402.     // Bootloader fast logo
  2403.     unsigned char led_frame_15[18] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2404.                                       0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
  2405.                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  2406.     unsigned char led_frame_17[18] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2407.                                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2408.                                       0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00};
  2409.     set_topboard_leds(led_frame_15, ARRAY_SIZE(led_frame_15),
  2410.                           led_frame_17, ARRAY_SIZE(led_frame_17));
  2411. #endif // BOARD_CHIRP_B{1,3,4} BOARD_JOPLIN_B{1,3,4}
  2412.  
  2413.     power_tuning();
  2414.  
  2415.     /* show public OTP fields */
  2416.     {
  2417.         const volatile BERLIN_BOOTINFO *pinfo = (BERLIN_BOOTINFO *)BERLIN_BOOTINFO_ADDR;
  2418.         lgpl_printf("OTP/RKEK ID: ");
  2419.         lgpl_printf("%08X", pinfo->otp_rkek_id[0]);
  2420.         lgpl_printf("%08X", pinfo->otp_rkek_id[1]);
  2421.         lgpl_printf("\n");
  2422.  
  2423.         lgpl_printf("OTP/VERSION: ");
  2424.         lgpl_printf("%08X", pinfo->otp_version);
  2425.         lgpl_printf("\n");
  2426.  
  2427.         lgpl_printf("EROM ID      :%08X\n", pinfo->erom_id);
  2428.  
  2429.         lgpl_printf("RSA KEY INDEX:%1X\n", pinfo->rsa_key_index);
  2430.         lgpl_printf("USB BOOT TO  :%1X\n", pinfo->usb_boot_timeout);
  2431.         lgpl_printf("ROMK DISABLE :%1x\n", pinfo->romkey_disable);
  2432.         lgpl_printf("DIS USB BOOT :%1x\n", pinfo->disable_usb_boot);
  2433.         lgpl_printf("DIS BOOTSTRAP:%1x\n", pinfo->usb_disable_bootstrap);
  2434.         lgpl_printf("DIS BLANK MED:%1x\n", pinfo->usb_disable_blank_media);
  2435.  
  2436.         lgpl_printf("OTP/MID:     ");
  2437.         lgpl_printf("%08X", pinfo->otp_market_id);
  2438.         lgpl_printf("\n");
  2439.  
  2440.         lgpl_printf("OTP/ULT:     ");
  2441.         lgpl_printf("%08X", pinfo->otp_ult[0]);
  2442.         lgpl_printf("%08X", pinfo->otp_ult[1]);
  2443.         lgpl_printf("\n");
  2444.  
  2445.         lgpl_printf("OTP LOCK    :%1X\n", pinfo->otp_locked);
  2446.  
  2447.         lgpl_printf("RKEK CRC    :%08X\n", pinfo->rkek_crc);
  2448.         lgpl_printf("AESK0 CRC   :%08X\n", pinfo->aesk0_crc);
  2449.         lgpl_printf("SIGNK7 CRC  :%08X\n", pinfo->signk7_crc);
  2450.         lgpl_printf("MRVL SIGN R :%04X\n", pinfo->mrvl_sign_right);
  2451.         lgpl_printf("CUST SIGN R :%04X\n", pinfo->cust_sign_right);
  2452.     }
  2453.  
  2454.     #if BG2CDP
  2455.     dump_mem(SYSPLL_CTRL_BASE + RA_pll_ctrl, 4);    // SYSPLL Registers
  2456.     dump_mem(MEMPLL_CTRL_BASE + RA_pll_ctrl, 4);    // MEMPLL Registers
  2457.     dump_mem(CPUPLL_CTRL_BASE + RA_pll_ctrl, 4);    // CPUPLL Registers
  2458.     #else
  2459.     dump_mem(0xF7EA0014, 4);    // SYSPLL Registers
  2460.     dump_mem(0xF7EA0028, 4);    // MEMPLL Registers
  2461.     dump_mem(0xF7EA003C, 4);    // CPUPLL Registers
  2462.     #endif
  2463.  
  2464.     lgpl_printf("Clock configuration:\n");
  2465. #if BG2CDP
  2466.     list_speed(2);
  2467.  
  2468. #if !CDP_A0
  2469.     /* BG2-CDP Z1/Z2 */
  2470.     /* adjust vcore */
  2471.     diag_i2c_vcore_control(VCORE_MASTER_ID, VCORE_VOLT_INDEX);
  2472. #endif /* CDP_A0 */
  2473.  
  2474. #else
  2475.     show_speed();
  2476. #endif /* BG2CDP */
  2477.  
  2478.     // initialize nand flash
  2479.     init_nfc(block_size, page_size, ecc_strength);
  2480.     if(gen_nand_partition_info()){
  2481.         printf("Cannot get NAND partition info");
  2482.         while(1);
  2483.     }
  2484.  
  2485.     UtilMemReset();
  2486.  
  2487. #ifndef ANDROID_BOOT
  2488.     // always get version table for all the boot state
  2489.     if (get_version_table()) {
  2490.         lgpl_printf("get version table error!\n");
  2491.         while(1);
  2492.     }
  2493. #endif //! ANDROID_BOOT
  2494. #if ANDROID_BOOT
  2495.     ret = flash_ts_init(vt_fts.part1_start_blkind, vt_fts.part1_blks);
  2496.     if (ret){
  2497.         lgpl_printf("flash_ts_init() init fail.\n");
  2498.         while(1);
  2499.     }
  2500. #endif
  2501.  
  2502.     {
  2503.         // note: in the case of STATE_WARMUP_0/1/2, we expect RAM is OK. In the case of
  2504.         // Beetle DDR_OFF, it's not. So make sure Image_Load_And_Start is called for the Beetle case.
  2505.         // note: MV_SoC_STATE_BOOT_RECOVERY_KERNEL takes this branch.
  2506.         //TODO FOR BG2CDPbootloader_customize_load_image_preprocess(uiBoot);
  2507.  
  2508. #ifdef ENABLE_CONNECT_BTN
  2509. #ifndef BG2CDP
  2510.     Check_connectbtnGPIO();
  2511. #endif
  2512. #endif
  2513.         if(Image_Load_And_Start())
  2514.         {
  2515.             lgpl_printf("tbdzz---- Img_Ld_And_Start error! Spinning now!\n");
  2516. #if defined(CDP_A0) && defined(BOOTLOADER_SHOWLOGO)
  2517.             fastlogo_init();
  2518.             showlogo_start(1);
  2519. #endif
  2520.             while(1);
  2521.         }
  2522. #ifdef BG2CDP
  2523.         //TODO FOR BG2CDPbootloader_customize_load_image_preprocess(uiBoot);
  2524. #else
  2525.     bootloader_customize_load_image_preprocess(uiBoot);
  2526. #endif
  2527.  
  2528.     }
  2529.  
  2530. #ifdef BG2CDP
  2531.         //TODO FOR BG2CDPbootloader_customize_load_image_preprocess(uiBoot);
  2532. #else
  2533.     bootloader_customize_load_image_preprocess(uiBoot);
  2534. #endif
  2535.  
  2536. #if BOOTLOADER_SHOWLOGO
  2537. #if CDP_A0
  2538.     // Still needed. Otherwise kernel fast logo doesn't display.
  2539.     fastlogo_init();
  2540.     showlogo_start(0);
  2541.     showlogo_stop();
  2542. #endif
  2543. #else
  2544. #if CDP_A0
  2545.     AVPLL_Enable();
  2546. #endif
  2547. #endif  // BOOTLOADER_SHOWLOGO
  2548.     Image_3_Verify_Jump() ;
  2549.     while(1);
  2550. }
  2551.  
  2552. /* TODO(kolla): This seem to be a result of statically linking libgcc.a,
  2553.  * one of the u-boot forums recommended locally defining this function.
  2554.  * Find cleaner fix.
  2555.  */
  2556. void __aeabi_unwind_cpp_pr0(void)
  2557. {
  2558. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement