main.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein is
  5. * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
  6. * the prior written permission of MediaTek inc. and/or its licensors, any
  7. * reproduction, modification, use or disclosure of MediaTek Software, and
  8. * information contained herein, in whole or in part, shall be strictly
  9. * prohibited.
  10. *
  11. * MediaTek Inc. (C) 2010. All rights reserved.
  12. *
  13. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  14. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  15. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
  16. * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
  17. * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  19. * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
  20. * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
  21. * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
  22. * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
  23. * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
  24. * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
  25. * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
  26. * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  27. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
  28. * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
  29. * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
  30. * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
  31. * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  32. *
  33. * The following software/firmware and/or related documentation ("MediaTek
  34. * Software") have been modified by MediaTek Inc. All revisions are subject to
  35. * any receiver's applicable license agreements with MediaTek Inc.
  36. */
  37. #include "typedefs.h"
  38. #include "platform.h"
  39. #include "download.h"
  40. #include "meta.h"
  41. #include "sec.h"
  42. #include "partition_api.h"
  43. #include "dram_buffer.h"
  44. #include "wdt.h"
  45. #include "timer.h"
  46. #include "log_store_pl.h"
  47. //#include "mt_ptp2.h"
  48. #if CFG_ATF_SUPPORT
  49. #include "tz_init.h"
  50. #endif
  51. #include "tz_emi_mpu.h"
  52. #include "sec_efuse.h"
  53. #include <sec_boot.h>
  54. #if defined(MTK_AB_OTA_UPDATER)
  55. #include "bootctrl.h"
  56. #include "ab_bootcheck.h"
  57. #endif
  58. #include <partition_api.h>
  59. #include <partition_active.h>
  60. #include <pal_log.h>
  61. #include <pal_typedefs.h>
  62. /*============================================================================*/
  63. /* CONSTAND DEFINITIONS */
  64. /*============================================================================*/
  65. #define MOD "[BLDR]"
  66. #define ATF_DRAM_IMG_NAME "atf_dram"
  67. /*============================================================================*/
  68. /* MACROS DEFINITIONS */
  69. /*============================================================================*/
  70. #define CMD_MATCH(cmd1,cmd2) \
  71. (!strncmp((const char*)(cmd1->data), (cmd2), min(strlen(cmd2), cmd1->len)))
  72. /*============================================================================*/
  73. /* GLOBAL VARIABLES */
  74. /*============================================================================*/
  75. #if CFG_BOOT_ARGUMENT
  76. #define bootarg g_dram_buf->bootarg
  77. #endif
  78. /*============================================================================*/
  79. /* STATIC VARIABLES */
  80. /*============================================================================*/
  81. bl_param_t *p_bldr_param = NULL;
  82. static bl_param_t bldr_param;
  83. /*============================================================================*/
  84. /* EXTERN */
  85. /*============================================================================*/
  86. /*============================================================================*/
  87. /* INTERNAL FUNCTIONS */
  88. /*============================================================================*/
  89. #if defined(PL_PROFILING)
  90. U32 profiling_time = 0; //declare in main.c
  91. #endif
  92. #if MTK_TINYSYS_SSPM_SUPPORT
  93. #define SSPM_DBG_MODE 0
  94. #if SSPM_DBG_MODE
  95. #define sspm_dbg(f, ...) print("%s" f, MOD, ##__VA_ARGS__)
  96. #else
  97. #define sspm_dbg
  98. #endif
  99. #define SSPM_MPU_REGION_ID 3
  100. #define SSPM_MEM_SIZE 0x00080000 // 512K
  101. #define SSPM_MEM_ALIGN 0x00010000 // 64K (minimal size for EMI MPU)
  102. #define SSPM_MEM_LIMIT 0xC0000000
  103. #define SSPM_TCM_SZ 0x00028000 // 160K
  104. #define SSPM_RD_SZ 0x00010000 // 64K
  105. #define ROUNDUP(a,b) (((a) + ((b)-1)) & ~((b)-1))
  106. struct sspm_info_t {
  107. unsigned int sspm_dm_ofs;
  108. unsigned int sspm_dm_sz;
  109. unsigned int rd_ofs;
  110. unsigned int rd_sz;
  111. };
  112. #endif
  113. unsigned int bldr_load_loader_ext_etc(void);
  114. static void bldr_pre_process(void)
  115. {
  116. int isLocked = 0;
  117. u32 ret = 0;
  118. #ifdef MTK_EFUSE_WRITER_RESERVE_CODESIZE
  119. struct efuse_param param = {0};
  120. #endif
  121. #if CFG_USB_AUTO_DETECT
  122. platform_usbdl_flag_check();
  123. #endif
  124. #if CFG_EMERGENCY_DL_SUPPORT
  125. platform_safe_mode(1, CFG_EMERGENCY_DL_TIMEOUT_MS);
  126. #endif
  127. /* essential hardware initialization. e.g. timer, pll, uart... */
  128. platform_pre_init();
  129. print("\n%s Build Time: %s\n", MOD, BUILD_TIME);
  130. // config_DCC_Calin();
  131. // dump_dcc_regs();
  132. // disable_FBB_SW();
  133. g_boot_mode = NORMAL_BOOT;
  134. /* hardware initialization */
  135. platform_init();
  136. part_init();
  137. BOOTING_TIME_PROFILING_LOG("Part Init");
  138. store_switch_to_dram();
  139. BOOTING_TIME_PROFILING_LOG("store_switch_to_dram");
  140. part_dump();
  141. BOOTING_TIME_PROFILING_LOG("part_dump");
  142. sec_update_dram_init_status();
  143. #if CFG_PRELOADER_EXTENSION
  144. ret = bldr_load_loader_ext_etc();
  145. if (ret) {
  146. print("load loader_ext_etc fail (%x)\n", ret);
  147. ASSERT(0);
  148. }
  149. BOOTING_TIME_PROFILING_LOG("loader_ext_etc");
  150. #endif
  151. #ifdef MTK_EFUSE_WRITER_RESERVE_CODESIZE
  152. #ifdef MTK_EFUSE_WRITER_SUPPORT
  153. efuse_wdt_restart();
  154. param.magic_key1 = EFUSE_BLOW_KEY1;
  155. param.magic_key2 = EFUSE_BLOW_KEY2;
  156. param.enable_self_blow = TRUE;
  157. #else
  158. param.magic_key1 = 0xFEEEEEEF;
  159. param.magic_key2 = 0xFEEEEEEF;
  160. param.enable_self_blow = FALSE;
  161. #endif
  162. param.clear_part_dis = FALSE;
  163. ret = efuse_write_all(param);
  164. print("%s Efuse status(%x)\n", MOD, ret);
  165. BOOTING_TIME_PROFILING_LOG("EFUSE Self Blow");
  166. #else
  167. print("%s MTK_EFUSE_WRITER_RESERVE_CODESIZE off\n", MOD);
  168. #endif
  169. #ifdef MTK_FACTORY_LOCK_SUPPORT
  170. seclib_query_factory_lock(&isLocked);
  171. #endif
  172. #if CFG_UART_TOOL_HANDSHAKE
  173. /* init uart handshake for sending 'ready' to tool and receiving handshake
  174. * pattern from tool in the background and we'll see the pattern later.
  175. * this can reduce the handshake time.
  176. */
  177. boot_mode_t mode = NORMAL_BOOT;
  178. #ifdef MTK_SECURITY_SW_SUPPORT
  179. mode = seclib_brom_meta_mode();
  180. #endif
  181. if (!isLocked && mode == NORMAL_BOOT) {
  182. uart_handshake_init();
  183. BOOTING_TIME_PROFILING_LOG("UART handshake init");
  184. log_buf_ctrl(1); /* switch log buffer to dram */
  185. }
  186. #endif
  187. }
  188. static void bldr_post_process(void)
  189. {
  190. platform_post_init();
  191. }
  192. u32 seclib_get_devinfo_with_index(u32 index)
  193. {
  194. return internal_seclib_get_devinfo_with_index(index);
  195. }
  196. static bool wait_for_discon(struct comport_ops *comm, u32 tmo_ms)
  197. {
  198. bool ret;
  199. u8 discon[HSHK_DISCON_SZ];
  200. memset(discon, 0x0, HSHK_DISCON_SZ);
  201. print("[BLDR] DISCON...");
  202. if (ret = comm->recv(discon, HSHK_DISCON_SZ, tmo_ms)) {
  203. print("timeout\n");
  204. return ret;
  205. }
  206. if (0 == memcmp(discon, HSHK_DISCON, HSHK_DISCON_SZ))
  207. print("OK\n");
  208. else
  209. print("protocol mispatch\n");
  210. return ret;
  211. }
  212. int bldr_load_part(char *name, blkdev_t *bdev, u32 *addr, u32 *size)
  213. {
  214. part_t *part = part_get(name);
  215. if (NULL == part) {
  216. print("%s %s partition not found\n", MOD, name);
  217. return -1;
  218. }
  219. return part_load(bdev, part, addr, 0, size);
  220. }
  221. int bldr_load_part_lk(blkdev_t *bdev, u32 *addr, u32 *size)
  222. {
  223. unsigned long lk_active = 0, lk2_active = 0;
  224. // check the active bit of lk partition
  225. part_t *part_lk = part_get("lk");
  226. if (NULL == part_lk) {
  227. print("%s lk partition not found\n", MOD);
  228. return -1;
  229. } else {
  230. lk_active = mt_part_get_active_bit(part_lk);
  231. }
  232. // check the active bit of lk2 partition
  233. part_lk = part_get("lk2");
  234. if (NULL == part_lk) {
  235. print("%s lk2 partition not found\n", MOD);
  236. //return -1; Comment it since we could load lk partition to boot
  237. } else {
  238. lk2_active = mt_part_get_active_bit(part_lk);
  239. }
  240. print("%s lk active = %x, lk2 active = %x\n", MOD, lk_active, lk2_active);
  241. // load partition
  242. if (lk_active == 0 && lk2_active > 0) {
  243. print("%s Loading LK2 Partition...\n", MOD);
  244. part_lk = part_get("lk2");
  245. if (part_lk != NULL) {
  246. return part_load(bdev, part_lk, addr, 0, size);
  247. } else {
  248. return -1;
  249. }
  250. } else {
  251. print("%s Loading LK Partition...\n", MOD);
  252. part_lk = part_get("lk");
  253. if (part_lk != NULL) {
  254. return part_load(bdev, part_lk, addr, 0, size);
  255. } else {
  256. return -1;
  257. }
  258. }
  259. }
  260. int bldr_load_tee_part(char *name, blkdev_t *bdev, u32 *addr, u32 offset, u32 *size)
  261. {
  262. int ret;
  263. part_t *part = part_get(name);
  264. u32 next_offset;
  265. u32 atf_sram_size;
  266. u32 atf_dram_addr = 0;
  267. if (NULL == part) {
  268. print("%s %s part. not found\n", MOD, name);
  269. return -1;
  270. }
  271. ret = part_load(bdev, part, addr, offset, size);
  272. if (ret) {
  273. print("%s %s part. ATF load fail\n", MOD, name);
  274. return ret;
  275. }
  276. print("%s %s part. ATF load addr:0x%x, size:0x%x\n", MOD, name, *addr, *size);
  277. next_offset = sizeof(part_hdr_t) + *size;
  278. if (if_equal_img_name(bdev, part, next_offset, ATF_DRAM_IMG_NAME))
  279. {
  280. atf_sram_size = *size;
  281. ret = part_load(bdev, part, &atf_dram_addr, next_offset, size);
  282. if (ret) {
  283. print("%s %s part. ATF load fail\n", MOD, name);
  284. return ret;
  285. }
  286. print("%s %s part. ATF dram load addr:0x%x, size:0x%x\n", MOD, name, atf_dram_addr, *size);
  287. *size = *size + sizeof(part_hdr_t) + atf_sram_size;
  288. }
  289. /* header addr will be updated to entry point addr */
  290. #if CFG_TEE_SUPPORT
  291. {
  292. u32 tee_addr = 0;
  293. u32 next_offset = sizeof(part_hdr_t) + *size;
  294. ret = part_load(bdev, part, &tee_addr, next_offset, size);
  295. if (ret) {
  296. print("%s %s part. TEE load fail\n", MOD, name);
  297. return ret;
  298. }
  299. /* header addr will be updated to entry point addr */
  300. ret = tee_verify_image(&tee_addr);
  301. if (ret)
  302. print("%s %s part. TEE verify fail\n", MOD, name);
  303. /* set tee entry address */
  304. tee_set_entry(tee_addr);
  305. /* set hwuid. note that if you use cmm file, the parameter is empty. */
  306. tee_set_hwuid((u8*)&p_bldr_param->meid[0], sizeof(p_bldr_param->meid));
  307. }
  308. #endif
  309. return ret;
  310. }
  311. #if (MTK_TINYSYS_SSPM_SUPPORT || CFG_LOAD_SLT_SSPM)
  312. static void *sspm_memcpy(void *dest, const void *src, int count)
  313. {
  314. unsigned int *tmp = dest;
  315. const unsigned int *s = src;
  316. while (count > 0) {
  317. *tmp++ = *s++;
  318. count -= 4;
  319. }
  320. return dest;
  321. }
  322. #endif
  323. #if MTK_TINYSYS_SSPM_SUPPORT
  324. extern u32 g_ddr_reserve_enable;
  325. extern u32 g_ddr_reserve_success;
  326. static int bldr_load_sspm_part(blkdev_t *bdev, u32 *addr, u32 *size)
  327. {
  328. int ret;
  329. u32 tmp_addr;
  330. part_t *part;
  331. ptimg_hdr_t *hdr;
  332. unsigned char *img, *pmimg, *dmimg;
  333. unsigned int pmsize, dmsize;
  334. char *parts[] = { "sspm_1", "sspm_2" };
  335. struct sspm_info_t *info = (struct sspm_info_t *) *addr;
  336. #define PT_ID_SSPM_DM 0
  337. #define PT_ID_SSPM_PM 1
  338. #define SSPM_SW_RSTN 0x10A40000
  339. #define SSPM_CFGREG_GPR0 SSPM_SW_RSTN + 0x20
  340. #define SSPM_CFGREG_GPR1 SSPM_SW_RSTN + 0x24
  341. #define SSPM_CFGREG_GPR2 SSPM_SW_RSTN + 0x28
  342. #define SSPM_CFGREG_GPR3 SSPM_SW_RSTN + 0x2C
  343. #define SSPM_CFGREG_GPR4 SSPM_SW_RSTN + 0x30
  344. #define SSPM_CFGREG_GPR5 SSPM_SW_RSTN + 0x34
  345. #define ROUNDUP(a,b) (((a) + ((b)-1)) & ~((b)-1))
  346. for (ret = 0;ret < (sizeof(parts) / sizeof(*parts));ret++) {
  347. part = part_get(parts[ret]);
  348. if (part && mt_part_get_active_bit(part) > 0)
  349. break;
  350. }
  351. if (ret == (sizeof(parts) / sizeof(*parts)))
  352. ret = 0;
  353. /* decide partition by active bit */
  354. part = part_get(parts[ret]);
  355. if (NULL == part) {
  356. print("%s SSPM part. not found\n", MOD);
  357. return -1;
  358. }
  359. tmp_addr = *addr + ROUNDUP(sizeof(*info), 4);
  360. /* load and verify image */
  361. ret = part_load(bdev, part, &tmp_addr, 0, size);
  362. if (ret) {
  363. print("%s SSPM part. load fail\n", MOD);
  364. return ret;
  365. }
  366. sspm_dbg("Load SSPM partition to dram 0x%x (size: 0x%x)\n", tmp_addr, size);
  367. /* separate ptimg */
  368. pmimg = dmimg = NULL;
  369. hdr = (ptimg_hdr_t *) tmp_addr;
  370. while (hdr->magic == PT_MAGIC) {
  371. img = ((char *) hdr) + hdr->hdr_size;
  372. switch (hdr->id) {
  373. case PT_ID_SSPM_PM:
  374. pmimg = img;
  375. pmsize = hdr->img_size;
  376. break;
  377. case PT_ID_SSPM_DM:
  378. dmimg = img;
  379. dmsize = hdr->img_size;
  380. break;
  381. }
  382. img += ROUNDUP(hdr->img_size, hdr->align);
  383. hdr = (ptimg_hdr_t *) img;
  384. }
  385. if (!pmimg || !dmimg) {
  386. print("%s SSPM partition missing - PM:0x%x, DM:0x%x (@0x%x)\n", MOD, (u32)pmimg, (u32)dmimg, tmp_addr);
  387. return -1;
  388. }
  389. sspm_dbg("pmimg: 0x%x(size 0x%x), dmimg: 0x%x(size 0x%x)\n", (u32)pmimg, pmsize, (u32)dmimg, dmsize);
  390. DRV_WriteReg32(SSPM_SW_RSTN, 0x90000000);
  391. sspm_memcpy((unsigned char *) CFG_SSPMP_MEMADDR, pmimg, pmsize);
  392. memset(info, 0, ROUNDUP(sizeof(*info), 4));
  393. info->sspm_dm_ofs = (unsigned int)dmimg - (unsigned int)info;
  394. info->sspm_dm_sz = dmsize;
  395. info->rd_ofs = ROUNDUP(SSPM_TCM_SZ, SSPM_MEM_ALIGN);
  396. info->rd_sz = SSPM_RD_SZ;
  397. sspm_dbg("sspm_info @0x%x: dm offset 0x%x (0x%x), rd offset 0x%x(0x%x))\n", (u32)info,
  398. info->sspm_dm_ofs, info->sspm_dm_sz, info->rd_ofs, info->rd_sz);
  399. mtk_wdt_request_mode_set(MTK_WDT_STATUS_SSPM_RST, WD_REQ_RST_MODE);
  400. mtk_wdt_request_en_set(MTK_WDT_STATUS_SSPM_RST, WD_REQ_EN);
  401. #if SSPM_DBG_MODE
  402. //Setup GPIO pinmux SSPM JTAG
  403. DRV_SetReg32(0x102d0440, 0x00666660);
  404. //Setup GPIO pinmux SSPM UART
  405. DRV_SetReg32(0x102d0370, 0x10000000);
  406. DRV_SetReg32(0x102d0380, 0x00000001);
  407. #endif
  408. DRV_WriteReg32(SSPM_CFGREG_GPR0, (unsigned int) *addr);
  409. DRV_WriteReg32(SSPM_CFGREG_GPR1, ram_console_is_abnormal_boot()); // If not normal boot, notify sspm to backup
  410. DRV_SetReg32(SSPM_SW_RSTN, 0x1);
  411. print("%s SSPM Start! (with g_rgu_status 0x%x & %s ddr reserved mode)\n", MOD, g_rgu_status,
  412. (g_ddr_reserve_enable==1 && g_ddr_reserve_success==1) ? "with" : "without" );
  413. return 0;
  414. }
  415. #endif
  416. #if CFG_LOAD_SLT_SSPM
  417. static int bldr_load_sspm_part_slt(blkdev_t *bdev, u32 *addr, u32 *size)
  418. {
  419. int ret;
  420. ptimg_hdr_t *hdr;
  421. unsigned char *img, *pmimg, *dmimg;
  422. unsigned int pmsize, dmsize;
  423. char *parts = "SSPM";
  424. #define SSPM_IMG_TMP_OFFSET 0x28000 // 160K
  425. #define PT_ID_SSPM_DM 0
  426. #define PT_ID_SSPM_PM 1
  427. #define SSPM_SW_RSTN 0x10A40000
  428. /* load and verify image */
  429. ret = bldr_load_part(parts, bdev, addr, size);
  430. if (ret) {
  431. print("%s SSPM part. load fail\n", MOD);
  432. return ret;
  433. }
  434. /* separate ptimg */
  435. pmimg = dmimg = NULL;
  436. hdr = (ptimg_hdr_t *) *addr;
  437. while (hdr->magic == PT_MAGIC) {
  438. img = ((char *) hdr) + hdr->hdr_size;
  439. switch (hdr->id) {
  440. case PT_ID_SSPM_PM:
  441. pmimg = img;
  442. pmsize = hdr->img_size;
  443. break;
  444. case PT_ID_SSPM_DM:
  445. dmimg = img;
  446. dmsize = hdr->img_size;
  447. break;
  448. }
  449. img += ROUNDUP(hdr->img_size, hdr->align);
  450. hdr = (ptimg_hdr_t *) img;
  451. }
  452. if (!pmimg || !dmimg) {
  453. print("%s SSPM part missing: 0x%x, 0x%x\n", MOD, (u32) pmimg, (u32) dmimg);
  454. return -1;
  455. }
  456. // print("pmimg: 0x%x (0x%x), dmimg: 0x%x (0x%x)\n", (u32) pmimg, (u32) pmsize, (u32) dmimg, (u32) dmsize);
  457. DRV_WriteReg32(SSPM_SW_RSTN, 0x90000000);
  458. sspm_memcpy((unsigned char *) CFG_SSPMP_MEMADDR, pmimg, pmsize);
  459. sspm_memcpy((unsigned char *) CFG_SSPMD_MEMADDR, dmimg, dmsize);
  460. print("%s SSPM finished\n");
  461. return 0;
  462. }
  463. #endif
  464. #if CFG_PRELOADER_EXTENSION
  465. int bldr_load_loader_ext_dram(void)
  466. {
  467. blkdev_t *bdev = NULL;
  468. int ret = 0;
  469. part_hdr_t hdr;
  470. u32 sec_policy_idx = 0;
  471. u32 img_auth_required = 0;
  472. u32 ms = 0;
  473. u8 img_name[32] = {0};
  474. u8 part_name[32] = {0};
  475. u32 boot_part = 0;
  476. struct part_info_t loader_ext_info;
  477. bdev = blkdev_get(CFG_BOOT_DEV);
  478. if (NULL == bdev) {
  479. print("%s can't find boot device(%d)\n", MOD, CFG_BOOT_DEV);
  480. return -1;
  481. }
  482. /* Since preloader and loader_ext.img need to be the same build, we need
  483. * to ensure the followings after OTA
  484. * 1. boot part 1 loaded with loader_ext1/loader_ext_a
  485. * 2. boot part 2 loaded with loader_ext2/loader_ext_b
  486. */
  487. ret = mmc_get_boot_part(&boot_part);
  488. if (0 != ret) {
  489. print("get boot part fail: %d\n", ret);
  490. } else {
  491. print("boot_part: %d\n", boot_part);
  492. }
  493. #if defined(MTK_AB_OTA_UPDATER)
  494. if (boot_part == EMMC_PART_BOOT1) {
  495. memcpy(part_name, "loader_ext_a", 12);
  496. } else {
  497. memcpy(part_name, "loader_ext_b", 12);
  498. }
  499. part_name[11 + 1] = '\0'; //[0 ~ 11] is loader_ext_X
  500. #else
  501. if (boot_part == EMMC_PART_BOOT1) {
  502. memcpy(part_name, "loader_ext1", 11);
  503. } else {
  504. memcpy(part_name, "loader_ext2", 11);
  505. }
  506. part_name[10 + 1] = '\0'; //[0 ~ 10] is loader_extX
  507. #endif //MTK_AB_OTA_UPDATER
  508. ret = mt_get_part_info_by_name(part_name, &loader_ext_info);
  509. if (ret) {
  510. print("get %s_info error\n", part_name);
  511. }
  512. #ifdef MTK_SECURITY_SW_SUPPORT
  513. /* get security policy of current partition */
  514. print("part name=%s\n", part_name);
  515. sec_policy_idx = get_policy_entry_idx(part_name);
  516. img_auth_required = get_vfy_policy(sec_policy_idx);
  517. print("img_auth_required=%x\n", img_auth_required);
  518. ms = get_timer(0);
  519. if (img_auth_required) {
  520. sec_malloc_buf_reset();
  521. memcpy(img_name, "loader_ext_dram", 16);
  522. if (sec_img_auth_init(part_name, img_name)) {
  523. print("cert vfy fail\n");
  524. ASSERT(0);
  525. }
  526. #ifdef MTK_SECURITY_ANTI_ROLLBACK
  527. if (sec_rollback_check(0)) {
  528. print("ver check fail\n", MOD);
  529. ASSERT(0);
  530. }
  531. #endif
  532. }
  533. ms = get_timer(ms);
  534. print("img: %s cert vfy(%d ms)\n", img_name, ms);
  535. #endif
  536. print("load %s_dram\n", part_name);
  537. if (blkdev_read(bdev, loader_ext_info.addr, sizeof(part_hdr_t), (u8 *)&hdr, EMMC_PART_USER) != 0) {
  538. print("read header error\n");
  539. ASSERT(0);
  540. } else {
  541. if (hdr.info.magic != PART_MAGIC) {
  542. print("img not exist\n");
  543. ASSERT(0);
  544. }
  545. }
  546. if (blkdev_read(bdev, loader_ext_info.addr + sizeof(part_hdr_t), hdr.info.dsize, (u8 *)CFG_LOADER_EXT_DRAM_ADDR, EMMC_PART_USER) != 0) {
  547. print("load image error\n");
  548. ASSERT(0);
  549. }
  550. #ifdef MTK_SECURITY_SW_SUPPORT
  551. ms = get_timer(0);
  552. if (img_auth_required) {
  553. print("img vfy...");
  554. ret = sec_img_auth(CFG_LOADER_EXT_DRAM_ADDR, hdr.info.dsize);
  555. if (ret) {
  556. print("fail(0x%x)\n", ret);
  557. ASSERT(0);
  558. } else {
  559. print("ok\n");
  560. }
  561. }
  562. ms = get_timer(ms);
  563. print("img: %s vfy(%d ms)\n", "loader_ext_dram", ms);
  564. #endif
  565. if (ret) {
  566. print("load loader_ext_dram fail\n");
  567. }
  568. return ret;
  569. }
  570. unsigned int bldr_load_loader_ext_etc()
  571. {
  572. blkdev_t *bdev;
  573. part_t *part_loader_ext;
  574. u32 addr = CFG_LOADER_EXT_ETC_ADDR;
  575. u32 size = 0;
  576. u8 part_name[32] = {0};
  577. u8 img_name[32] = {0};
  578. u32 img_size = 0;
  579. u32 ret = 0;
  580. u32 boot_part = 0;
  581. /* Since preloader and loader_ext.img need to be the same build, we need
  582. * to ensure the followings after OTA
  583. * 1. boot part 1 loaded with loader_ext1/loader_ext_a
  584. * 2. boot part 2 loaded with loader_ext2/loader_ext_b
  585. */
  586. ret = mmc_get_boot_part(&boot_part);
  587. if (0 != ret) {
  588. print("get boot part fail: %d\n", ret);
  589. } else {
  590. print("boot_part: %d\n", boot_part);
  591. }
  592. #if defined(MTK_AB_OTA_UPDATER)
  593. if (boot_part == EMMC_PART_BOOT1) {
  594. memcpy(part_name, "loader_ext_a", 12);
  595. } else {
  596. memcpy(part_name, "loader_ext_b", 12);
  597. }
  598. part_name[11 + 1] = '\0'; //[0 ~ 11] is loader_ext_X
  599. #else
  600. if (boot_part == EMMC_PART_BOOT1) {
  601. memcpy(part_name, "loader_ext1", 11);
  602. } else {
  603. memcpy(part_name, "loader_ext2", 11);
  604. }
  605. part_name[10 + 1] = '\0'; //[0 ~ 10] is loader_extX
  606. #endif //MTK_AB_OTA_UPDATER
  607. /* loader_ext.img is as follow:
  608. * -------------------
  609. * | part_hdr_t |
  610. * |-----------------|
  611. * | loader_ext_dram |
  612. * |-----------------|
  613. * | part_hdr_t |
  614. * |-----------------|
  615. * | loader_ext_etc |
  616. * -------------------
  617. *
  618. * To load loader_ext_etc, we need to bypass the size of part_hdr_t and
  619. * loader_ext_dram.
  620. */
  621. memcpy(img_name, "loader_ext_dram", 16);
  622. bdev = blkdev_get(CFG_BOOT_DEV);
  623. if (NULL == bdev) {
  624. print("%s can't find boot device(%d)\n", MOD, CFG_BOOT_DEV);
  625. return -1;
  626. }
  627. print("load %s_etc\n", part_name);
  628. part_loader_ext = part_get(part_name);
  629. if (NULL == part_loader_ext) {
  630. print("%s not found\n", part_name);
  631. return -1;
  632. } else {
  633. ret = sec_get_img_size(part_name, img_name, &img_size);
  634. if (ret) {
  635. print("get %s size fail: %x", part_name, ret);
  636. }
  637. /* img_size = image size + signature */
  638. return part_load(bdev, part_loader_ext, &addr, sizeof(part_hdr_t) + img_size, &size);
  639. }
  640. }
  641. #endif
  642. static bool bldr_cmd_handler(struct bldr_command_handler *handler,
  643. struct bldr_command *cmd, struct bldr_comport *comport)
  644. {
  645. struct comport_ops *comm = comport->ops;
  646. u32 attr = handler->attr;
  647. #if CFG_DT_MD_DOWNLOAD
  648. if (CMD_MATCH(cmd, SWITCH_MD_REQ)) {
  649. /* SWITCHMD */
  650. if (attr & CMD_HNDL_ATTR_COM_FORBIDDEN)
  651. goto forbidden;
  652. comm->send((u8*)SWITCH_MD_ACK, strlen(SWITCH_MD_ACK));
  653. platform_modem_download();
  654. return TRUE;
  655. }
  656. #endif
  657. if (CMD_MATCH(cmd, ATCMD_PREFIX)) {
  658. /* "AT+XXX" */
  659. if (CMD_MATCH(cmd, ATCMD_NBOOT_REQ)) {
  660. /* return "AT+OK" to tool */
  661. comm->send((u8*)ATCMD_OK, strlen(ATCMD_OK));
  662. g_boot_mode = NORMAL_BOOT;
  663. g_boot_reason = BR_TOOL_BY_PASS_PWK;
  664. } else {
  665. /* return "AT+UNKONWN" to ack tool */
  666. comm->send((u8*)ATCMD_UNKNOWN, strlen(ATCMD_UNKNOWN));
  667. return FALSE;
  668. }
  669. } else if (CMD_MATCH(cmd, META_STR_REQ)) {
  670. para_t param;
  671. memset(&param, 0, sizeof(param)); /*init param*/
  672. #if CFG_BOOT_ARGUMENT
  673. bootarg.md_type[0] = 0;
  674. bootarg.md_type[1] = 0;
  675. #endif
  676. /* "METAMETA" */
  677. if (attr & CMD_HNDL_ATTR_COM_FORBIDDEN)
  678. goto forbidden;
  679. /* for backward compatibility */
  680. comm->recv((u8*)&param.v0001, sizeof(param.v0001), 2000);
  681. /* meta usb type use for ELT Port */
  682. print("meta_com_id = %d\n", param.v0001.usb_type);
  683. g_meta_com_id = param.v0001.usb_type;
  684. #if CFG_WORLD_PHONE_SUPPORT
  685. comm->send((u8*)META_ARG_VER_STR, strlen(META_ARG_VER_STR));
  686. if (0 == comm->recv((u8*)&param.v0001, sizeof(param.v0001), 5000)) {
  687. g_meta_com_id = param.v0001.usb_type;
  688. print("md_type[0] = %d \n", param.v0001.md0_type);
  689. print("md_type[1] = %d \n", param.v0001.md1_type);
  690. #if CFG_BOOT_ARGUMENT
  691. bootarg.md_type[0] = param.v0001.md0_type;
  692. bootarg.md_type[1] = param.v0001.md1_type;
  693. #endif
  694. }
  695. #endif
  696. comm->send((u8*)META_STR_ACK, strlen(META_STR_ACK));
  697. #if CFG_WORLD_PHONE_SUPPORT
  698. wait_for_discon(comm, 1000);
  699. #endif
  700. g_boot_mode = META_BOOT;
  701. } else if (CMD_MATCH(cmd, FACTORY_STR_REQ)) {
  702. para_t param;
  703. /* "FACTFACT" */
  704. if (attr & CMD_HNDL_ATTR_COM_FORBIDDEN)
  705. goto forbidden;
  706. if (0 == comm->recv((u8*)&param.v0001, sizeof(param.v0001), 5)) {
  707. g_meta_com_id = param.v0001.usb_type;
  708. }
  709. comm->send((u8*)FACTORY_STR_ACK, strlen(FACTORY_STR_ACK));
  710. g_boot_mode = FACTORY_BOOT;
  711. } else if (CMD_MATCH(cmd, META_ADV_REQ)) {
  712. /* "ADVEMETA" */
  713. if (attr & CMD_HNDL_ATTR_COM_FORBIDDEN)
  714. goto forbidden;
  715. comm->send((u8*)META_ADV_ACK, strlen(META_ADV_ACK));
  716. wait_for_discon(comm, 1000);
  717. g_boot_mode = ADVMETA_BOOT;
  718. } else if (CMD_MATCH(cmd, ATE_STR_REQ)) {
  719. para_t param;
  720. /* "FACTORYM" */
  721. if (attr & CMD_HNDL_ATTR_COM_FORBIDDEN)
  722. goto forbidden;
  723. if (0 == comm->recv((u8*)&param.v0001, sizeof(param.v0001), 5)) {
  724. g_meta_com_id = param.v0001.usb_type;
  725. }
  726. comm->send((u8*)ATE_STR_ACK, strlen(ATE_STR_ACK));
  727. g_boot_mode = ATE_FACTORY_BOOT;
  728. } else if (CMD_MATCH(cmd, FB_STR_REQ)) {
  729. /* "FASTBOOT" */
  730. comm->send((u8 *)FB_STR_ACK, strlen(FB_STR_ACK));
  731. g_boot_mode = FASTBOOT;
  732. } else {
  733. print("%s unknown received: \'%s\'\n", MOD, cmd->data);
  734. return FALSE;
  735. }
  736. print("%s '%s' received!\n", MOD, cmd->data);
  737. return TRUE;
  738. forbidden:
  739. comm->send((u8*)META_FORBIDDEN_ACK, strlen(META_FORBIDDEN_ACK));
  740. print("%s '%s' is forbidden!\n", MOD, cmd->data);
  741. return FALSE;
  742. }
  743. static int bldr_handshake(struct bldr_command_handler *handler)
  744. {
  745. boot_mode_t mode = NORMAL_BOOT;
  746. bool isSLA = 0;
  747. int isLocked = 0;
  748. #ifdef MTK_SECURITY_SW_SUPPORT
  749. /* get mode type */
  750. mode = seclib_brom_meta_mode();
  751. isSLA = seclib_sla_enabled();
  752. BOOTING_TIME_PROFILING_LOG("seclib_brom_meta_mode");
  753. #endif
  754. #ifdef MTK_FACTORY_LOCK_SUPPORT
  755. seclib_query_factory_lock(&isLocked);
  756. #endif
  757. switch (mode) {
  758. case NORMAL_BOOT:
  759. /* ------------------------- */
  760. /* security check */
  761. /* ------------------------- */
  762. if (TRUE == isSLA) {
  763. handler->attr |= CMD_HNDL_ATTR_COM_FORBIDDEN;
  764. print("%s META DIS\n", MOD);
  765. }
  766. if (!isLocked) {
  767. print("%s Tool connection is unlocked\n", MOD);
  768. #if CFG_USB_TOOL_HANDSHAKE
  769. if (TRUE == usb_handshake(handler))
  770. g_meta_com_type = META_USB_COM;
  771. BOOTING_TIME_PROFILING_LOG("USB handshake");
  772. #endif
  773. #if CFG_UART_TOOL_HANDSHAKE
  774. if (TRUE == uart_handshake(handler))
  775. g_meta_com_type = META_UART_COM;
  776. BOOTING_TIME_PROFILING_LOG("UART handshake");
  777. #endif
  778. }
  779. else {
  780. print("%s Tool connection is locked\n", MOD);
  781. bootarg.sec_limit.magic_num = SEC_LIMIT_MAGIC;
  782. bootarg.sec_limit.forbid_mode = F_FACTORY_MODE;
  783. }
  784. break;
  785. case META_BOOT:
  786. print("%s BR META BOOT\n", MOD);
  787. // init md_type for security chip + world phone project in BROM Meta case.
  788. #if CFG_BOOT_ARGUMENT
  789. bootarg.md_type[0] = 0;
  790. bootarg.md_type[1] = 0;
  791. #endif
  792. g_boot_mode = META_BOOT;
  793. if(!usb_cable_in())
  794. g_meta_com_type = META_UART_COM;
  795. else
  796. g_meta_com_type = META_USB_COM;
  797. break;
  798. case FACTORY_BOOT:
  799. print("%s BR FACTORY BOOT\n", MOD);
  800. g_boot_mode = FACTORY_BOOT;
  801. if(!usb_cable_in())
  802. g_meta_com_type = META_UART_COM;
  803. else
  804. g_meta_com_type = META_USB_COM;
  805. break;
  806. case ADVMETA_BOOT:
  807. print("%s BR ADVMETA BOOT\n", MOD);
  808. g_boot_mode = ADVMETA_BOOT;
  809. if(!usb_cable_in())
  810. g_meta_com_type = META_UART_COM;
  811. else
  812. g_meta_com_type = META_USB_COM;
  813. break;
  814. case ATE_FACTORY_BOOT:
  815. print("%s BR ATE FACTORY BOOT\n", MOD);
  816. g_boot_mode = ATE_FACTORY_BOOT;
  817. if(!usb_cable_in())
  818. g_meta_com_type = META_UART_COM;
  819. else
  820. g_meta_com_type = META_USB_COM;
  821. break;
  822. default:
  823. print("%s UNKNOWN MODE\n", MOD);
  824. break;
  825. }
  826. return 0;
  827. }
  828. static void bldr_wait_forever(void)
  829. {
  830. /* prevent wdt timeout and clear usbdl flag */
  831. mtk_wdt_disable();
  832. platform_safe_mode(0, 0);
  833. print("bldr_wait_forever\n");
  834. while(1);
  835. }
  836. static int bldr_load_images(u32 *jump_addr)
  837. {
  838. int ret = 0;
  839. blkdev_t *bootdev;
  840. u32 addr = 0;
  841. char *name;
  842. u32 size = 0;
  843. u32 spare0 = 0;
  844. u32 spare1 = 0;
  845. char active_part_name[PART_NAME_BUF_SZ] = {0};
  846. if (NULL == (bootdev = blkdev_get(CFG_BOOT_DEV))) {
  847. print("%s can't find boot device(%d)\n", MOD, CFG_BOOT_DEV);
  848. /* FIXME, should change to global error code */
  849. return -1;
  850. }
  851. #if CFG_LOAD_MD_ROM
  852. if (1 == aarch64_slt_done())
  853. {
  854. /* do not check the correctness */
  855. addr = CFG_MD1_ROM_MEMADDR;
  856. //bldr_load_part(PART_MD1_ROM, bootdev, &addr, &size);
  857. bldr_load_part("MD1_ROM", bootdev, &addr, &size);
  858. addr = CFG_MD2_ROM_MEMADDR;
  859. //bldr_load_part(PART_MD2_ROM, bootdev, &addr, &size);
  860. bldr_load_part("MD2_ROM", bootdev, &addr, &size);
  861. }
  862. #endif
  863. #if CFG_LOAD_MD_RAMDISK
  864. if (1 == aarch64_slt_done())
  865. {
  866. /* do not check the correctness */
  867. addr = CFG_MD1_RAMDISK_MEMADDR;
  868. bldr_load_part("MD1_RAMDISK", bootdev, &addr, &size);
  869. addr = CFG_MD2_RAMDISK_MEMADDR;
  870. bldr_load_part("MD2_RAMDISK", bootdev, &addr, &size);
  871. }
  872. #endif
  873. #if CFG_LOAD_MD_DSP
  874. if (1 == aarch64_slt_done())
  875. {
  876. addr = CFG_MD_DSP_MEMADDR;
  877. bldr_load_part("MD_DSP",bootdev, &addr, &size);
  878. }
  879. #endif
  880. #if CFG_LOAD_SLT_MD_RAMDISK
  881. if (1 == aarch64_slt_done())
  882. {
  883. /* do not check the correctness */
  884. addr = CFG_MD1_RAMDISK_MEMADDR;
  885. bldr_load_part("FDD_MD_RAMDISK", bootdev, &addr, &size);
  886. //addr = CFG_TDD_ONLY_MD_RAMDISK_MEMADDR;
  887. bldr_load_part("TDD_ONLY_MD_RAMDISK", bootdev, &addr, &size);
  888. //addr = CFG_MD2_RAMDISK_MEMADDR;
  889. bldr_load_part("MD2_RAMDISK", bootdev, &addr, &size);
  890. }
  891. #endif
  892. #if CFG_LOAD_SLT_MD_DSP
  893. if (1 == aarch64_slt_done())
  894. {
  895. addr = CFG_MD_DSP_MEMADDR;
  896. bldr_load_part("FDD_MD_DSP",bootdev, &addr, &size);
  897. //addr = CFG_TDD_ONLY_MD_DSP_MEMADDR;
  898. bldr_load_part("TDD_ONLY_MD_DSP",bootdev, &addr, &size);
  899. //addr = CFG_MD2_DSP_MEMADDR;
  900. bldr_load_part("MD2_DSP",bootdev, &addr, &size);
  901. }
  902. #endif
  903. #if CFG_LOAD_CONN_SYS
  904. if (1 == aarch64_slt_done())
  905. {
  906. addr = CFG_CONN_SYS_MEMADDR;
  907. //bldr_load_part(PART_CONN_SYS,bootdev, &addr, &size);
  908. bldr_load_part("CONN_SYS",bootdev, &addr, &size);
  909. }
  910. #endif
  911. #if CFG_LOAD_SLT_MD
  912. if (1 == aarch64_slt_done())
  913. {
  914. int *pass_size = NULL;
  915. addr = CFG_FDD_MD_ROM_MEMADDR; // work around
  916. /* HVT_MD_ROM */
  917. //addr = CFG_HVT_MD_ROM_MEMADDR;
  918. bldr_load_part("HVT_MD_ROM", bootdev, &addr, &size);
  919. /* MD_DVT_ROM */
  920. //addr = CFG_MD_DVT_ROM_MEMADDR;
  921. bldr_load_part("MD_DVT_ROM", bootdev, &addr, &size);
  922. /* FDD_MD_ROM */
  923. //addr = CFG_FDD_MD_ROM_MEMADDR;
  924. bldr_load_part("FDD_MD_ROM", bootdev, &addr, &size);
  925. pass_size = addr - 4;
  926. *pass_size = size;
  927. print("assign 0x%X value %d\n", pass_size, size);
  928. /* TDD_MD_ROM */
  929. //addr = CFG_TDD_ONLY_ROM_MEMADDR;
  930. bldr_load_part("TDD_ONLY_ROM", bootdev, &addr, &size);
  931. pass_size = addr - 4;
  932. *pass_size = size;
  933. print("assign 0x%X value %d\n", pass_size, size);
  934. /* MD2_ROM */
  935. //addr = CFG_MD2_ROM_MEMADDR;
  936. bldr_load_part("MD2_ROM", bootdev, &addr, &size);
  937. pass_size = addr - 4;
  938. *pass_size = size;
  939. print("assign 0x%X value %d\n", pass_size, size);
  940. }
  941. #endif
  942. #if CFG_LOAD_SLT_SCP
  943. if (1 == aarch64_slt_done())
  944. {
  945. DRV_WriteReg32(0x100A402C, 0);
  946. addr = 0;
  947. bldr_load_part("SCP",bootdev, &addr, &size);
  948. }
  949. #endif
  950. #if CFG_LOAD_SLT_MD32
  951. #define ReadREGMD32(_addr, _value) ((_value) = *(volatile unsigned int *)(_addr) )
  952. #define WriteREGMD32(_addr, _value) (*(volatile unsigned int *)(_addr) = (_value))
  953. if (1 == aarch64_slt_done())
  954. {
  955. u32 p_addr, d_addr;
  956. u32 src_addr, dest_addr;
  957. u32 count, value;
  958. //SPM power on MD32 and MD32 SRAM
  959. DRV_WriteReg32(0x10B00000, 0x0b160001);
  960. DRV_WriteReg32(0x10B00330, 0x0000000d);
  961. DRV_WriteReg32(0x10B00390, 0x00000032);
  962. //DRV_WriteReg32(0x100062c8, 0xfffffff0);
  963. p_addr = CFG_MD32P_ROM_MEMADDR;
  964. //bldr_load_part(PART_MD32_P,bootdev, &p_addr, &size);
  965. bldr_load_part("MD32_P",bootdev, &p_addr, &size);
  966. src_addr = p_addr;
  967. dest_addr = 0x10b30000;
  968. count = size;
  969. while (count > 0) {
  970. ReadREGMD32(src_addr,value);
  971. WriteREGMD32(dest_addr,value);
  972. count -= 4;
  973. src_addr += 4;
  974. dest_addr += 4;
  975. }
  976. print("MD32 loadPM, srcaddr check1:%x, value:%x\n", p_addr, value);
  977. src_addr = p_addr;
  978. dest_addr = 0x10b30000;
  979. count = size;
  980. while (count > 0) {
  981. ReadREGMD32(src_addr,value);
  982. WriteREGMD32(dest_addr,value);
  983. count -= 4;
  984. src_addr += 4;
  985. dest_addr += 4;
  986. }
  987. print("MD32 loadPM, srcaddr check2:%x, value:%x\n", p_addr, value);
  988. //memcpy((void *)0x10b30000, (void *)p_addr, sizeof(char)*size);
  989. d_addr = CFG_MD32D_ROM_MEMADDR;
  990. //bldr_load_part(PART_MD32_D,bootdev, &d_addr, &size);
  991. bldr_load_part("MD32_D",bootdev, &d_addr, &size);
  992. src_addr = d_addr;
  993. dest_addr = 0x10b38000;
  994. count = size;
  995. while (count > 0) {
  996. ReadREGMD32(src_addr,value);
  997. WriteREGMD32(dest_addr,value);
  998. count -= 4;
  999. src_addr += 4;
  1000. dest_addr += 4;
  1001. }
  1002. print("MD32 loadDM, srcaddr:%x, value:%x\n", d_addr, value);
  1003. //memcpy((void *)0x10b38000, (void *)d_addr, sizeof(char)*size);
  1004. DRV_WriteReg32(0x10B00330, 0x00000000);
  1005. }
  1006. #endif
  1007. #if MTK_TINYSYS_SSPM_SUPPORT
  1008. addr = (u32)mblock_reserve(&bootarg.mblock_info,
  1009. (u64)SSPM_MEM_SIZE, (u64)SSPM_MEM_ALIGN,
  1010. SSPM_MEM_LIMIT, RANKMAX);
  1011. if (!addr)
  1012. return -1;
  1013. ret = bldr_load_sspm_part(bootdev, &addr, &size);
  1014. if (ret)
  1015. return ret;
  1016. #if (!SSPM_DBG_MODE)
  1017. emi_mpu_set_region_protection((u64)addr, (u64)addr + ROUNDUP(SSPM_TCM_SZ, SSPM_MEM_ALIGN) - 1, SSPM_MPU_REGION_ID,
  1018. SET_ACCESS_PERMISSON(UNLOCK, FORBIDDEN, FORBIDDEN, FORBIDDEN, FORBIDDEN, SEC_RW, FORBIDDEN, FORBIDDEN, FORBIDDEN));
  1019. #endif
  1020. #endif
  1021. #if CFG_LOAD_SLT_SSPM
  1022. #define SSPM_MEM_SIZE 0x00080000 // 512K
  1023. #define SSPM_MEM_ALIGN 0x00010000 // 64K (minimal size for EMI MPU)
  1024. #define SSPM_MEM_LIMIT 0xC0000000
  1025. if (1 == aarch64_slt_done())
  1026. {
  1027. addr = (u32)mblock_reserve(&bootarg.mblock_info,
  1028. (u64)SSPM_MEM_SIZE, (u64)SSPM_MEM_ALIGN,
  1029. SSPM_MEM_LIMIT, RANKMAX);
  1030. if (!addr)
  1031. return -1;
  1032. ret = bldr_load_sspm_part_slt(bootdev, &addr, &size);
  1033. if (ret)
  1034. return ret;
  1035. }
  1036. #endif
  1037. #if CFG_LOAD_AP_ROM
  1038. if (1 == aarch64_slt_done())
  1039. {
  1040. u32 p_addr;
  1041. addr = CFG_AP_ROM_MEMADDR;
  1042. //ret = bldr_load_part(PART_AP_ROM, bootdev, &addr, &size);
  1043. ret = bldr_load_part("AP_ROM", bootdev, &addr, &size);
  1044. if (ret)
  1045. return ret;
  1046. *jump_addr = addr;
  1047. }
  1048. #elif CFG_LOAD_UBOOT
  1049. #if defined(MTK_AB_OTA_UPDATER)
  1050. addr = CFG_UBOOT_MEMADDR;
  1051. const char *ab_suffix = get_suffix();
  1052. if (ab_suffix == NULL)
  1053. return -1;
  1054. if (!memcmp(ab_suffix, BOOTCTRL_SUFFIX_A, 2)) {
  1055. ret = bldr_load_part("lk_a", bootdev, &addr, &size);
  1056. } else {
  1057. ret = bldr_load_part("lk_b", bootdev, &addr, &size);
  1058. }
  1059. if (ret)
  1060. return ret;
  1061. *jump_addr = addr;
  1062. #else
  1063. addr = CFG_UBOOT_MEMADDR;
  1064. ret = bldr_load_part_lk(bootdev, &addr, &size);
  1065. print("LK addr: 0x%x, size: 0x%x\n", addr, size);
  1066. if (addr < CFG_UBOOT_MEMADDR || (addr + size) > (CFG_UBOOT_MEMADDR + 0x900000))
  1067. print("Warning: LK out of boundary.\n");
  1068. if (ret)
  1069. return ret;
  1070. *jump_addr = addr;
  1071. #endif //MTK_AB_OTA_UPDATER
  1072. #endif
  1073. #if CFG_LOAD_SLT_AARCH64_KERNEL
  1074. if (0 == aarch64_slt_done())
  1075. {
  1076. addr = CFG_BOOTA64_MEMADDR;
  1077. ret = bldr_load_part("boota64", bootdev, &addr, &size);
  1078. addr = CFG_DTB_MEMADDR;
  1079. ret = bldr_load_part("DTB", bootdev, &addr, &size);
  1080. addr = CFG_IMAGE_AARCH64_MEMADDR;
  1081. ret = bldr_load_part("Image_aarch64", bootdev, &addr, &size);
  1082. }
  1083. #endif
  1084. BOOTING_TIME_PROFILING_LOG("before load TEE");
  1085. #if CFG_ATF_SUPPORT
  1086. #if defined(MTK_AB_OTA_UPDATER)
  1087. addr = CFG_ATF_ROM_MEMADDR;
  1088. if (!memcmp(ab_suffix, BOOTCTRL_SUFFIX_A, 2)) {
  1089. ret = bldr_load_tee_part("tee_a", bootdev, &addr, 0, &size);
  1090. } else {
  1091. ret = bldr_load_tee_part("tee_b", bootdev, &addr, 0, &size);
  1092. }
  1093. print("%s bldr load tee part ret=0x%x, addr=0x%x\n", MOD, ret, addr);
  1094. #else
  1095. addr = CFG_ATF_ROM_MEMADDR;
  1096. if (strlen("tee") >= PART_NAME_BUF_SZ)
  1097. return -1;
  1098. memset(active_part_name, 0x0, PART_NAME_BUF_SZ);
  1099. memcpy(active_part_name, "tee", strlen("tee"));
  1100. ret = partition_get_gpt_active(active_part_name, PART_NAME_BUF_SZ, GET_ACTIVE);
  1101. if (ret)
  1102. return ret;
  1103. ret = bldr_load_tee_part(active_part_name, bootdev, &addr, 0, &size);
  1104. if (ret)
  1105. return ret;
  1106. print("%s bldr load tee part ret=0x%x, addr=0x%x\n", MOD, ret, addr);
  1107. #endif //MTK_AB_OTA_UPDATER
  1108. #endif
  1109. BOOTING_TIME_PROFILING_LOG("after load TEE");
  1110. return ret;
  1111. }
  1112. void mt_usb_phy_recover(void);
  1113. void mu3d_hal_rst_dev(void);
  1114. /*============================================================================*/
  1115. /* GLOBAL FUNCTIONS */
  1116. /*============================================================================*/
  1117. void bldr_jump(u32 addr, u32 arg1, u32 arg2)
  1118. {
  1119. platform_wdt_kick();
  1120. /* disable preloader safe mode */
  1121. platform_safe_mode(0, 0);
  1122. print("\n%s jump to 0x%x\n", MOD, addr);
  1123. print("%s <0x%x>=0x%x\n", MOD, addr, *(u32*)addr);
  1124. print("%s <0x%x>=0x%x\n", MOD, addr + 4, *(u32*)(addr + 4));
  1125. jump(addr, arg1, arg2);
  1126. }
  1127. void bldr_jump64(u32 addr, u32 arg1, u32 arg2)
  1128. {
  1129. platform_wdt_kick();
  1130. /* disable preloader safe mode */
  1131. platform_safe_mode(0, 0);
  1132. print("\n%s jump to 0x%x\n", MOD, addr);
  1133. #if !CFG_BYPASS_EMI
  1134. print("%s <0x%x>=0x%x\n", MOD, addr, *(u32*)addr);
  1135. print("%s <0x%x>=0x%x\n", MOD, addr + 4, *(u32*)(addr + 4));
  1136. #endif
  1137. #if CFG_ATF_SUPPORT
  1138. trustzone_jump(addr, arg1, arg2);
  1139. #else
  1140. print("%s trustzone is not supported!\n", MOD);
  1141. #if CFG_LOAD_SLT_AARCH64_KERNEL
  1142. print("%s jump to 64 bit SLT kernel!\n", MOD);
  1143. jumparch64_slt();
  1144. #endif
  1145. #endif
  1146. }
  1147. void main(u32 *arg)
  1148. {
  1149. struct bldr_command_handler handler;
  1150. u32 jump_addr, jump_arg;
  1151. uint32_t ret = 0;
  1152. /* get the bldr argument */
  1153. p_bldr_param = &bldr_param;
  1154. memcpy((void *)p_bldr_param, (void *)*arg, sizeof(bl_param_t));
  1155. #ifdef MTK_SECURITY_SW_SUPPORT
  1156. /* note that if you use cmm file, these parameters are empty. */
  1157. ret |= seclib_set_pl_load_addr(p_bldr_param->bl_loadinfo[0].bl_load_addr);
  1158. ret |= seclib_set_cc_status(p_bldr_param->cc_lcs, p_bldr_param->cc_flags);
  1159. ret |= seclib_set_sctrl_info(p_bldr_param->sctrl_cert_file_addr, p_bldr_param->sctrl_cert_file_len);
  1160. ret |= seclib_set_tool_auth_info(p_bldr_param->tool_auth_file_addr, p_bldr_param->tool_auth_file_len);
  1161. ret |= seclib_set_me_id(p_bldr_param->meid, ME_IDENTITY_LEN);
  1162. ret |= seclib_set_soc_id(p_bldr_param->socid, SOC_ID_LEN);
  1163. ret |= seclib_set_prov_key(p_bldr_param->prov_key, PROVISIONING_KEY_LEN);
  1164. if (ret) {
  1165. pal_log_err("Fail to init chip info.\n");
  1166. ASSERT(0);
  1167. }
  1168. #endif
  1169. #if CFG_CANCEL_BWDT_TIMEOUT
  1170. /* Cancel BWDT timeout, otherwise it would reset in 2 second. */
  1171. DRV_WriteReg32(0x100070A4, DRV_Reg32(0x100070A4) | 0x66000001);
  1172. #endif
  1173. mtk_uart_init(UART_SRC_CLK_FRQ, CFG_LOG_BAUDRATE);
  1174. bldr_pre_process();
  1175. #ifdef HW_INIT_ONLY
  1176. #if !CFG_FPGA_PLATFORM
  1177. /*
  1178. * The following is requested by MD: Ying Hsu and Jim Chou
  1179. * Set VCORE and VMODEM as 1.19375V respectively.
  1180. * Read the settings back and print the results.
  1181. */
  1182. pmic_config_interface(0x152A, 0x6C, 0x7F,0);
  1183. pmic_config_interface(0x15AA, 0x6F, 0x7F,0);
  1184. unsigned int val = 0;
  1185. pmic_read_interface(0x152A, &val, 0x7F,0);
  1186. print("VCORE: %d\n", val);
  1187. pmic_read_interface(0x15AA, &val, 0x7F,0);
  1188. print("VMODEM: %d\n", val);
  1189. #endif
  1190. bldr_wait_forever();
  1191. #endif
  1192. handler.priv = NULL;
  1193. handler.attr = 0;
  1194. handler.cb = bldr_cmd_handler;
  1195. BOOTING_TIME_PROFILING_LOG("before bldr_handshake");
  1196. bldr_handshake(&handler);
  1197. BOOTING_TIME_PROFILING_LOG("bldr_handshake");
  1198. #if !CFG_FPGA_PLATFORM
  1199. /* security check */
  1200. device_APC_dom_setup();
  1201. #endif
  1202. BOOTING_TIME_PROFILING_LOG("sec_boot_check");
  1203. #if CFG_ATF_SUPPORT
  1204. trustzone_pre_init();
  1205. #endif
  1206. BOOTING_TIME_PROFILING_LOG("before load image");
  1207. #if !(CFG_BYPASS_LOAD_IMG_FORCE_ATF)
  1208. /* Do not load ATF, lk, load by JTAG */
  1209. if (0 != bldr_load_images(&jump_addr)) {
  1210. print("%s Second Bootloader Load Failed\n", MOD);
  1211. goto error;
  1212. }
  1213. #else
  1214. jump_addr = CFG_UBOOT_MEMADDR;
  1215. #endif
  1216. BOOTING_TIME_PROFILING_LOG("load image");
  1217. bldr_post_process();
  1218. #ifdef SLT
  1219. mt_usb_phy_recover();
  1220. //mu3d_hal_rst_dev();
  1221. #endif
  1222. #if CFG_ATF_SUPPORT
  1223. trustzone_post_init();
  1224. #endif
  1225. #if CFG_LOAD_SLT_AARCH64_KERNEL
  1226. if (0 == aarch64_slt_done())
  1227. {
  1228. *(unsigned int*) AARCH64_SLT_DONE_ADDRESS = AARCH64_SLT_DONE_MAGIC;
  1229. jump_addr = CFG_BOOTA64_MEMADDR;
  1230. //set up slave cpu reset address
  1231. *(unsigned int*) 0x10200040 = CFG_BOOTA64_MEMADDR; //cpu1
  1232. *(unsigned int*) 0x10200048 = CFG_BOOTA64_MEMADDR; //cpu2
  1233. *(unsigned int*) 0x10200050 = CFG_BOOTA64_MEMADDR; //cpu3
  1234. *(unsigned int*) 0x10200238 = CFG_BOOTA64_MEMADDR; //cpu4
  1235. *(unsigned int*) 0x10200240 = CFG_BOOTA64_MEMADDR; //cpu5
  1236. *(unsigned int*) 0x10200248 = CFG_BOOTA64_MEMADDR; //cpu6
  1237. *(unsigned int*) 0x10200250 = CFG_BOOTA64_MEMADDR; //cpu7
  1238. print("%s Aarch64 Kernel SLT , jump to 64 bit kernel, address: 0x%x\n", MOD,jump_addr);
  1239. bldr_jump64(jump_addr, (u32)&bootarg, sizeof(boot_arg_t));
  1240. }
  1241. #endif
  1242. #if CFG_BOOT_ARGUMENT_BY_ATAG
  1243. jump_arg = (u32)&(g_dram_buf->boottag);
  1244. #else
  1245. jump_arg = (u32)&bootarg;
  1246. #endif
  1247. /* 64S3,32S1,32S1 (MTK_ATF_BOOT_OPTION = 0)
  1248. * re-loader jump to LK directly and then LK jump to kernel directly */
  1249. #if CFG_ATF_SUPPORT
  1250. print("%s Others, jump to ATF\n", MOD);
  1251. bldr_jump64(jump_addr, jump_arg, sizeof(boot_arg_t));
  1252. #else
  1253. bldr_jump(jump_addr, jump_arg, sizeof(boot_arg_t));
  1254. #endif
  1255. error:
  1256. platform_error_handler();
  1257. }