spl_start.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #include "hal_config.h"
  13. #include "boot_config.h"
  14. #include "boot_platform.h"
  15. #include "boot_fdl.h"
  16. #include "hal_chip.h"
  17. #include "boot_debuguart.h"
  18. #include "boot_mem.h"
  19. #include "boot_entry.h"
  20. #include "cmsis_core.h"
  21. #include "boot_secure.h"
  22. #include "boot_spi_flash.h"
  23. #include "boot_vfs.h"
  24. #include "boot_mmu.h"
  25. #include "boot_timer.h"
  26. #include "boot_irq.h"
  27. #include "boot_bsl_cmd.h"
  28. #include "boot_pdl.h"
  29. #include "hal_adi_bus.h"
  30. #include "hal_spi_flash.h"
  31. #include "flash_block_device.h"
  32. #include "fupdate.h"
  33. #include "fupdate_security.h"
  34. #include "fs_mount.h"
  35. #include "osi_log.h"
  36. #include "osi_api.h"
  37. #include "drv_names.h"
  38. #include "calclib/simage.h"
  39. #include <sys/reent.h>
  40. #include "string.h"
  41. #include "boot_efuse.h"
  42. #include "hwregs.h"
  43. #include "quec_proj_config.h"
  44. #include "quec_boot_log.h"
  45. #include "quec_boot_pff.h"
  46. #include "quec_internal_cfg.h"
  47. #ifdef CONFIG_QUEC_PROJECT_FEATURE_FOTA
  48. #include "quec_boot_fota.h"
  49. #include "quec_boot_pin_cfg.h"
  50. #endif
  51. #if (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_NAND_FLASH || defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_SPI6_NAND)
  52. #include "quec_boot_nand.h"
  53. #endif
  54. #ifdef CONFIG_QUEC_PROJECT_FEATURE_BOOT_URC
  55. #include "quec_boot_uart.h"
  56. #endif
  57. #ifdef QUEC_PROJECT_FEATURE_BOOT_SD_EMMC
  58. #include "quec_boot_sdmmc.h"
  59. #endif
  60. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SPI6_EXT_NOR
  61. #include "quec_spi6_ext_norflash_cfg.h"
  62. #endif
  63. #ifdef CONFIG_QUEC_PROJECT_FEATURE_BOOT_SPI4_EXTNSFFS
  64. #include "fs_mount_spiflash.h"
  65. #include "ql_boot_spi4_exnsffs_cfg.h"
  66. #endif
  67. #define ROM_LOADPAR_NORMAL (0)
  68. #define ROM_LOADPAR_FROM_BOOT2 (0x10)
  69. #define FLASH_SECTOR_SIZE (0x1000)
  70. #define FLASH_PAGE_SIZE (256)
  71. #define SLAVE_ACCESS_NO (0)
  72. #define SLAVE_ACCESS_SECURE_ONLY (1)
  73. #define SLAVE_ACCESS_NONSECURE_ONLY (2)
  74. #define SLAVE_ACCESS_SECURE_NONESECURE (3)
  75. #define AON_WDT_HZ (32768)
  76. #define RESTART_WDT_MARGIN_TIME (1800000)
  77. #define WDT_RESET_MIN (10)
  78. static void bootSetFirewall()
  79. {
  80. // unset firewall set by sysrom
  81. hwp_memFwSpiflash1->seg_0_first_addr = 0xffffffff;
  82. hwp_memFwSpiflash1->seg_0_last_addr = 0;
  83. hwp_memFwAonImem->seg_0_first_addr = 0xffffffff;
  84. hwp_memFwAonImem->seg_0_last_addr = 0;
  85. hwp_memFwApImem->seg_0_first_addr = 0xffffffff;
  86. hwp_memFwApImem->seg_0_last_addr = 0;
  87. hwp_slvFwApAhb->id0_first_addr_0 = 0xffffffff;
  88. hwp_slvFwApAhb->id0_last_addr_0 = 0;
  89. hwp_slvFwLpsIfc->id0_first_addr_0 = 0xffffffff;
  90. hwp_slvFwLpsIfc->id0_last_addr_0 = 0;
  91. hwp_slvFwApIfc->id0_first_addr_0 = 0xffffffff;
  92. hwp_slvFwApIfc->id0_last_addr_0 = 0;
  93. // unset flash,uart,keypad, usb firewall
  94. REGT_FIELD_CHANGE(hwp_slvFwApAhb->rd_sec_1, REG_SLV_FW_AP_AHB_RD_SEC_1_T,
  95. spiflash1_reg_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  96. REGT_FIELD_CHANGE(hwp_slvFwApAhb->wr_sec_1, REG_SLV_FW_AP_AHB_WR_SEC_1_T,
  97. spiflash1_reg_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  98. REGT_FIELD_CHANGE(hwp_slvFwApAhb->rd_sec_1, REG_SLV_FW_AP_AHB_RD_SEC_1_T,
  99. med_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  100. REGT_FIELD_CHANGE(hwp_slvFwApAhb->wr_sec_1, REG_SLV_FW_AP_AHB_WR_SEC_1_T,
  101. med_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  102. REGT_FIELD_CHANGE(hwp_slvFwLpsIfc->rd_sec_0, REG_SLV_FW_LPS_IFC_RD_SEC_0_T,
  103. keypad_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  104. REGT_FIELD_CHANGE(hwp_slvFwLpsIfc->wr_sec_0, REG_SLV_FW_LPS_IFC_WR_SEC_0_T,
  105. keypad_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  106. REGT_FIELD_CHANGE(hwp_slvFwApIfc->rd_sec_0, REG_SLV_FW_AP_IFC_RD_SEC_0_T,
  107. uart4_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  108. REGT_FIELD_CHANGE(hwp_slvFwApIfc->wr_sec_0, REG_SLV_FW_AP_IFC_WR_SEC_0_T,
  109. uart4_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  110. REGT_FIELD_CHANGE(hwp_slvFwLpsIfc->rd_sec_0, REG_SLV_FW_LPS_IFC_RD_SEC_0_T,
  111. uart1_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  112. REGT_FIELD_CHANGE(hwp_slvFwLpsIfc->wr_sec_0, REG_SLV_FW_LPS_IFC_WR_SEC_0_T,
  113. uart1_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  114. REGT_FIELD_CHANGE(hwp_slvFwApAhb->rd_sec_1, REG_SLV_FW_AP_AHB_RD_SEC_1_T,
  115. usb_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  116. REGT_FIELD_CHANGE(hwp_slvFwApAhb->wr_sec_1, REG_SLV_FW_AP_AHB_WR_SEC_1_T,
  117. usb_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  118. // unset emmc firewall
  119. REGT_FIELD_CHANGE(hwp_slvFwApAhb->rd_sec_0, REG_SLV_FW_AP_AHB_RD_SEC_0_T,
  120. emmc_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  121. REGT_FIELD_CHANGE(hwp_slvFwApAhb->wr_sec_0, REG_SLV_FW_AP_AHB_WR_SEC_0_T,
  122. emmc_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  123. REGT_FIELD_CHANGE(hwp_mstCtrlAp->rd_sec_0, REG_MST_CTRL_AP_RD_SEC_0_T,
  124. emmc_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  125. REGT_FIELD_CHANGE(hwp_mstCtrlAp->wr_sec_0, REG_MST_CTRL_AP_WR_SEC_0_T,
  126. emmc_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  127. REGT_FIELD_CHANGE(hwp_slvFwApAhb->rd_sec_1, REG_SLV_FW_AP_AHB_RD_SEC_1_T,
  128. ce_sec_rd_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  129. REGT_FIELD_CHANGE(hwp_slvFwApAhb->wr_sec_1, REG_SLV_FW_AP_AHB_WR_SEC_1_T,
  130. ce_sec_wr_sec, SLAVE_ACCESS_SECURE_NONESECURE);
  131. }
  132. static void bootUsbParamCalibration()
  133. {
  134. uint32_t usb_cfg;
  135. uint8_t tuneotg = 0;
  136. uint8_t tfregres = 0;
  137. uint8_t tfhsres = 0;
  138. bool low = bootEfuseRead(true, HAL_EFUSE_DOUBLE_BLOCK_USB, &usb_cfg);
  139. if(low && (usb_cfg & 0x1) == 1)
  140. {
  141. tfhsres = usb_cfg >> 4;
  142. tfregres = usb_cfg >> 9;
  143. tuneotg = usb_cfg >> 1;
  144. hwp_analogG2->analog_usb20_usb20_trimming &= ~ANALOG_G2_ANALOG_USB20_USB20_TFHSRES(0x1f);
  145. hwp_analogG2->analog_usb20_usb20_trimming &= ~ANALOG_G2_ANALOG_USB20_USB20_TFREGRES(0x3f);
  146. hwp_analogG2->analog_usb20_usb20_trimming &= ~ANALOG_G2_ANALOG_USB20_USB20_TUNEOTG(0x7);
  147. hwp_analogG2->analog_usb20_usb20_trimming |= ANALOG_G2_ANALOG_USB20_USB20_TFHSRES(tfhsres);
  148. hwp_analogG2->analog_usb20_usb20_trimming |= ANALOG_G2_ANALOG_USB20_USB20_TFREGRES(tfregres);
  149. hwp_analogG2->analog_usb20_usb20_trimming |= ANALOG_G2_ANALOG_USB20_USB20_TUNEOTG(tuneotg);
  150. }
  151. }
  152. OSI_UNUSED static bool bootWdtStart(uint32_t reset_ms)
  153. {
  154. if (reset_ms < WDT_RESET_MIN)
  155. return false;
  156. uint32_t reload = (reset_ms * (uint64_t)AON_WDT_HZ) / 1000;
  157. REG_PMIC_RTC_ANA_MODULE_EN0_T module_en0;
  158. REG_PMIC_RTC_ANA_RTC_CLK_EN0_T rtc_clk_en0;
  159. REG_PMIC_WDT_WDG_CTRL_T wdg_ctrl;
  160. halAdiBusBatchChange(
  161. &hwp_pmicWdt->wdg_ctrl,
  162. HAL_ADI_BUS_OVERWITE(0), // wdg_run
  163. &hwp_pmicRtcAna->module_en0,
  164. REG_FIELD_MASKVAL1(module_en0, wdg_en, 1),
  165. &hwp_pmicRtcAna->rtc_clk_en0,
  166. REG_FIELD_MASKVAL1(rtc_clk_en0, rtc_wdg_en, 1),
  167. &hwp_pmicWdt->wdg_load_high,
  168. HAL_ADI_BUS_OVERWITE(reload >> 16),
  169. &hwp_pmicWdt->wdg_load_low,
  170. HAL_ADI_BUS_OVERWITE(reload & 0xffff),
  171. &hwp_pmicWdt->wdg_ctrl,
  172. REG_FIELD_MASKVAL2(wdg_ctrl, wdg_open, 1, wdg_rst_en, 1),
  173. HAL_ADI_BUS_CHANGE_END);
  174. return true;
  175. }
  176. OSI_UNUSED static void bootWdtStop()
  177. {
  178. REG_PMIC_RTC_ANA_MODULE_EN0_T module_en0;
  179. REG_PMIC_RTC_ANA_RTC_CLK_EN0_T rtc_clk_en0;
  180. halAdiBusBatchChange(&hwp_pmicWdt->wdg_ctrl,HAL_ADI_BUS_OVERWITE(0), // wdg_run
  181. &hwp_pmicRtcAna->module_en0,REG_FIELD_MASKVAL1(module_en0, wdg_en, 0),
  182. &hwp_pmicRtcAna->rtc_clk_en0,REG_FIELD_MASKVAL1(rtc_clk_en0, rtc_wdg_en, 0),
  183. HAL_ADI_BUS_CHANGE_END);
  184. }
  185. static void bootWdtInit()
  186. {
  187. #ifdef CONFIG_WDT_ENABLE
  188. bootWdtStart(RESTART_WDT_MARGIN_TIME);
  189. #else
  190. // WDT is enabled by default. Later complete WDT management is needed.
  191. bootWdtStop();
  192. #endif
  193. }
  194. OSI_UNUSED static bool prvCheckNormalPowerUp()
  195. {
  196. #ifndef CONFIG_CHARGER_POWERUP
  197. return (bootPowerOnCause() != OSI_BOOTCAUSE_CHARGE);
  198. #endif
  199. return true;
  200. }
  201. void bootSetUartIomux()
  202. {
  203. #ifdef CONFIG_CHIP_8850_V3_BOARD
  204. hwp_iomux->gpio_2 = 0x1c; //UART2_cts
  205. hwp_iomux->keyout_4 = 0xc; // UART_4_RX
  206. hwp_iomux->keyout_5 = 0xc; // UART_4_TX
  207. hwp_iomux->keyout_4 &= ~IOMUX_PAD_KEYOUT_4_DRV(0x3);
  208. hwp_iomux->keyout_4 |= IOMUX_PAD_KEYOUT_4_DRV(2);
  209. hwp_iomux->keyout_4 &= ~IOMUX_WPU;
  210. hwp_iomux->keyout_4 |= IOMUX_WPU;
  211. hwp_iomux->keyout_5 &= ~IOMUX_PAD_KEYOUT_5_DRV(0x3);
  212. hwp_iomux->keyout_5 |= IOMUX_PAD_KEYOUT_5_DRV(2);
  213. #else
  214. hwp_iomux->gpio_16 = 0x18; //UART2
  215. hwp_iomux->uart_2_cts = 0x10; // UART_4_RX
  216. hwp_iomux->uart_2_rts = 0x10; // UART_4_TX
  217. hwp_iomux->pad_uart_2_cts &= ~IOMUX_PAD_UART_2_CTS_DRV(0x3);
  218. hwp_iomux->pad_uart_2_cts |= IOMUX_PAD_UART_2_CTS_DRV(2);
  219. hwp_iomux->pad_uart_2_cts &= ~IOMUX_WPU;
  220. hwp_iomux->pad_uart_2_cts |= IOMUX_WPU;
  221. hwp_iomux->pad_uart_2_rts &= ~IOMUX_PAD_UART_2_RTS_DRV(0x3);
  222. hwp_iomux->pad_uart_2_rts |= IOMUX_PAD_UART_2_RTS_DRV(2);
  223. #endif
  224. }
  225. OSI_UNUSED static bool prvFlashCopy(uint32_t fromaddr, uint32_t toaddr, uint32_t psize)
  226. {
  227. //OSI_LOGI(0xffffffff, "dual boot copy from 0x%x to 0x%x, size 0x%x", fromaddr, toaddr, psize);
  228. bootSpiFlash_t *flash = bootSpiFlashOpen(DRV_NAME_SPI_FLASH);
  229. if (flash == NULL)
  230. return false;
  231. uint32_t to_off = HAL_FLASH_OFFSET(toaddr);
  232. uint32_t esize = OSI_ALIGN_UP(psize, FLASH_SECTOR_SIZE);
  233. if (!bootSpiFlashErase(flash, to_off, esize))
  234. return false;
  235. // bootSpiFlashWrite data can't be located in the same flash
  236. // fromaddr should be always sector aligned
  237. uint8_t ramdata[FLASH_PAGE_SIZE];
  238. const uint8_t *data = (const uint8_t *)fromaddr;
  239. while (psize > 0)
  240. {
  241. unsigned wr = OSI_MIN(unsigned, psize, FLASH_PAGE_SIZE);
  242. memcpy(ramdata, data, wr);
  243. if (!bootSpiFlashWrite(flash, to_off, ramdata, wr))
  244. return false;
  245. psize -= wr;
  246. to_off += wr;
  247. data += wr;
  248. }
  249. return true;
  250. }
  251. static void prvDualSplCheck(uint32_t param)
  252. {
  253. //此处不需要检测,展锐代码有问题,spl超过64KB就会死机,所以这里直接跳过
  254. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  255. return;
  256. #endif
  257. if (param == ROM_LOADPAR_NORMAL)
  258. {
  259. const simageHeader_t *header = (const simageHeader_t *)CONFIG_SPL1_FLASH_ADDRESS;
  260. if (header->image_size > CONFIG_SPL_SIZE)
  261. osiPanic();
  262. if (memcmp((void *)CONFIG_SPL1_FLASH_ADDRESS,
  263. (void *)CONFIG_SPL2_FLASH_ADDRESS,
  264. header->image_size) == 0)
  265. return;
  266. if (!prvFlashCopy(CONFIG_SPL1_FLASH_ADDRESS,
  267. CONFIG_SPL2_FLASH_ADDRESS,
  268. header->image_size))
  269. osiPanic();
  270. }
  271. else if (param == ROM_LOADPAR_FROM_BOOT2)
  272. {
  273. const simageHeader_t *header = (const simageHeader_t *)CONFIG_SPL2_FLASH_ADDRESS;
  274. if (header->image_size > CONFIG_SPL_SIZE)
  275. osiPanic();
  276. // Most likely, they are different. However, comparing them anyway.
  277. if (memcmp((void *)CONFIG_SPL2_FLASH_ADDRESS,
  278. (void *)CONFIG_SPL1_FLASH_ADDRESS,
  279. header->image_size) == 0)
  280. return;
  281. if (!prvFlashCopy(CONFIG_SPL2_FLASH_ADDRESS,
  282. CONFIG_SPL1_FLASH_ADDRESS,
  283. header->image_size))
  284. osiPanic();
  285. }
  286. }
  287. static void prvFirmwareUpdatePreProgress(int block_count, int block)
  288. {
  289. fupdateContentType_t type = FUPDATE_CONTENT_INVALID;
  290. type = fupdateGetUpdatingType();
  291. if (type == FUPDATE_CONTENT_FLASH)
  292. {
  293. fupdateFlashContent_t flash_content = {};
  294. bool result = fupdateGetFlashUpdatingContent(&flash_content);
  295. OSI_LOGI(0x10009078, "FUPDATE PreProgress ret %d flash name/offset %4c/0x%x",
  296. result, flash_content.device_name, flash_content.offset);
  297. }
  298. else
  299. {
  300. OSI_LOGI(0x10009079, "FUPDATE PreProgress current type %d", type);
  301. }
  302. }
  303. static void prvFirmwareUpdateProgress(int block_count, int block)
  304. {
  305. OSI_LOGI(0x10009056, "FUPDATE block: %d/%d", block, block_count);
  306. #ifdef CONFIG_QUEC_PROJECT_FEATURE_BOOT_URC
  307. quec_boot_fota_urc_output(block_count, block);
  308. #endif
  309. }
  310. #ifdef QUEC_PROJECT_FEATURE_BOOT_SD_EMMC
  311. extern ql_boot_sdmmc_cfg_t ql_boot_sdmmc_cfg; //sdmmc配置
  312. #endif
  313. #ifdef CONFIG_QUEC_PROJECT_FEATURE_FOTA
  314. extern quec_boot_fs_type_e quec_boot_fs_type;
  315. extern ql_boot_fat_mount_pt_e quec_partition_num;
  316. static void quec_boot_ext_flash_init()
  317. {
  318. switch(quec_boot_fs_type)
  319. {
  320. case QUEC_BOOT_SFFS_EXT:
  321. #if (defined CONFIG_QUEC_PROJECT_FEATURE_SPI6_EXT_NOR) && (!defined CONFIG_APP_FLASH2_ENABLED) && (!defined CONFIG_APPIMG_FLASH2_ENABLED)
  322. quec_boot_spi6_ext_norflash_init();
  323. #endif
  324. break;
  325. case QUEC_BOOT_FAT_SDMMC:
  326. //SDMMC1
  327. #if (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_SDMMC)
  328. ql_boot_sdmmc_cfg.power_mv = POWER_LEVEL_3200MV; //default sdcard voltage is 3.2 V
  329. ql_boot_sdmmc_cfg.sdmmc_clk = QL_BOOT_SDMMC_CLK_FREQ_SDHC; //400000 <= freq <= 50000000,
  330. //default sdcard clk is 50MHz(SDHC) or 25MHz(NOT SDHC).
  331. #if (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT_RDONLY)
  332. quec_boot_sdmmc_init(quec_partition_num);
  333. #elif (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT_RW)
  334. quec_boot_sdmmc_init_ex(quec_partition_num);
  335. #endif /* CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT */
  336. #elif (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_EMMC)
  337. ql_boot_sdmmc_cfg.power_mv = POWER_LEVEL_1800MV; //default emmc voltage is 1.8 V
  338. ql_boot_sdmmc_cfg.sdmmc_clk = QL_BOOT_SDMMC_CLK_FREQ_SD; //default emmc clk is 25MHz
  339. #if (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT_RDONLY)
  340. quec_boot_emmc_init(quec_partition_num);
  341. #elif (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT_RW)
  342. quec_boot_emmc_init_ex(quec_partition_num);
  343. #endif /* CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT */
  344. //SDMMC2
  345. #elif (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_SDMMC2)
  346. ql_boot_sdmmc_cfg.sdmmc_clk = QL_BOOT_SDMMC_CLK_FREQ_SDHC; //400000 <= freq <= 50000000,
  347. //default sdcard clk is 50MHz(SDHC) or 25MHz(NOT SDHC).
  348. #if (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT_RDONLY)
  349. quec_boot_sdmmc2_init(quec_partition_num);
  350. #elif (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT_RW)
  351. quec_boot_sdmmc2_init_ex(quec_partition_num);
  352. #endif /* CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT */
  353. #elif (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_EMMC2)
  354. ql_boot_sdmmc_cfg.sdmmc_clk = QL_BOOT_SDMMC_CLK_FREQ_SD; //default emmc clk is 25MHz
  355. #if (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT_RDONLY)
  356. quec_boot_emmc2_init(quec_partition_num);
  357. #elif (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT_RW)
  358. quec_boot_emmc2_init_ex(quec_partition_num);
  359. #endif /* CONFIG_QUEC_PROJECT_FEATURE_BOOT_FAT */
  360. #endif /* CONFIG_QUEC_PROJECT_FEATURE_BOOT_SDMMC */
  361. break;
  362. case QUEC_BOOT_FAT_EXNAND_FLASH:
  363. #ifdef CONFIG_QUEC_PROJECT_FEATURE_BOOT_NAND_FLASH
  364. quec_boot_nand_init(QL_BOOT_SPI_PORT_1);
  365. #elif (defined CONFIG_QUEC_PROJECT_FEATURE_BOOT_SPI6_NAND)
  366. quec_boot_nand_init_spi6(QL_BOOT_SPI_PORT_1,QL_BOOT_NAND_TYPE_QUAD_SPI);
  367. //quec_boot_nand_init_spi6(QL_BOOT_SPI_PORT_1,QL_BOOT_NAND_TYPE_DUAL_SPI);
  368. /*ql_boot_spi6_nand_config_s config = {
  369. .port = QL_BOOT_SPI_PORT_1,
  370. .clk = BOOT_SPI6_NAND_FLASH_CLK_200MHZ, //时钟源
  371. .clk_div = 5, //再分频 ,flash时钟=clk/clk_div
  372. .quad_mode = 1,
  373. .sample_delay = 3, //此值与clk和驱动能力相关,需要配置波形设置(主要用于调节数据的采样点在时钟的有效位置,尽可能的在中间),一般频率越高此值越大
  374. .isslow = 0,
  375. };
  376. quec_boot_nand_init_spi6_ex(config);*/
  377. #endif
  378. break;
  379. case QUEC_BOOT_SFFS_SPI4EXTNOR:
  380. #ifdef CONFIG_QUEC_PROJECT_FEATURE_BOOT_SPI4_EXTNSFFS
  381. ql_boot_spi4_nor_flash_init();
  382. fsMountGeneralSpiFlash(false);
  383. #endif
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. #endif
  390. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SPI6_EXT_NOR
  391. //是否在boot中跳过初始化Spi6ExtNor,为了兼容不使用Spi6ExtNor,用作其他功能的用户
  392. extern bool quec_skip_spi6_ext_nor_flash_init;
  393. #endif
  394. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  395. extern void quec_boot_fota_force_finish(void);
  396. #endif
  397. OSI_UNUSED static fupdateResult_t prvFirmwareUpdate(void)
  398. {
  399. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SPI6_EXT_NOR
  400. //如果是外置flash挂载成UFS分区,不能在挂载成EFS所以要跳过挂载EFS分区。
  401. //如果是外置flash不挂载成UFS分区且使能xip和自动挂载功能,fsMountAll时需要将外置flash挂载成EFS(/ext)。
  402. if(ql_ext_flash_cfg.xip_flag && ql_ext_flash_cfg.mount_sffs_flag && QUEC_EXT_FLASH_TO_UFS != ql_ext_flash_cfg.ufs_mapping_select)
  403. {
  404. //if defined CONFIG_APP_FLASH2_ENABLED || CONFIG_APPIMG_FLASH2_ENABLED,it will check ext flash during fota upgrade.
  405. //so,here need to initialize ext flash first,otherwise old flash crc mismatch.
  406. quec_skip_spi6_ext_nor_flash_init = false;//在fsMountAll时挂载外置flash
  407. }
  408. else
  409. {
  410. quec_skip_spi6_ext_nor_flash_init = true;//在fsMountAll时跳过Spi6ExtNor初始化和挂载
  411. }
  412. #endif
  413. fsMountSetScenario(FS_SCENRARIO_BOOTLOADER);
  414. if (!fsMountAll())
  415. return FUPDATE_RESULT_NOT_READY;
  416. #ifdef CONFIG_QUEC_PROJECT_FEATURE_BOOT_SPI4_NORFLASH
  417. void quec_boot_spi4_nor_flash_example();
  418. //quec_boot_spi4_nor_flash_example();
  419. #endif
  420. #ifdef CONFIG_QUEC_PROJECT_FEATURE_FOTA
  421. quec_boot_fota_init();
  422. quec_boot_ext_flash_init();
  423. #endif
  424. //默认关闭,如果需要检查签名可以在components\firmwareupdate\Kconfig或者target_config中自行打开CONFIG_FUPDATE_SUPPORT_SIGNATURE_CHECK宏
  425. #ifdef CONFIG_FUPDATE_SUPPORT_SIGNATURE_CHECK //quectel update
  426. bool check = fupdateCheckSignedPack();
  427. if (!check)
  428. {
  429. OSI_LOGE(0x10009057, "FUPDATE sign check failed");
  430. fsUmountAll();
  431. return FUPDATE_RESULT_NOT_READY;
  432. }
  433. #endif
  434. #ifdef CONFIG_FUPDATE_SUPPORT_CHECK_TMP_SPACE //默认关闭,在kernel中检查
  435. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  436. fupdateStatus_t state = fupdateGetStatus();
  437. if (state == FUPDATE_STATUS_READY)
  438. #else
  439. if (fupdateGetStatus() == FUPDATE_STATUS_READY)
  440. #endif
  441. {
  442. bool fcheck = fupdateIsTmpSpaceEnough(); //quectel update
  443. if (!fcheck)
  444. {
  445. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  446. if (state == FUPDATE_STATUS_READY)
  447. {
  448. //防止校验不通过,开不了机
  449. quec_boot_fota_force_finish();
  450. }
  451. #endif
  452. fsUmountAll();
  453. return FUPDATE_RESULT_CANNT_START;
  454. }
  455. }
  456. #endif
  457. #ifdef CONFIG_QUEC_PROJECT_FEATURE_BOOT_URC
  458. quec_boot_urc_init();
  459. #endif
  460. //urc fota start
  461. #ifdef CONFIG_QUEC_PROJECT_FEATURE_FOTA
  462. //Check whether the FOTA package is correct in boot.This code cannot be masked.
  463. quec_boot_fota_pack_verify();
  464. #endif
  465. #ifdef CONFIG_QL_OPEN_EXPORT_PKG
  466. #if 0
  467. /*
  468. 可视情况使用,如果按正常流程,内核验证完后,复位,中途不会有pack包的修改操作,不用开
  469. 这部分代码会占用boot 1632bytes
  470. */
  471. bool space_ok = fupdateIsTmpSpaceEnough();
  472. if (!space_ok)
  473. {
  474. OSI_LOGI(0, "no space");
  475. return;
  476. }
  477. #endif
  478. #endif
  479. //fupdateResult_t result = fupdateRun(prvFirmwareUpdateProgress);
  480. fupdateResult_t result = fupdateRunV2(prvFirmwareUpdatePreProgress, prvFirmwareUpdateProgress);
  481. OSI_LOGI(0x10009058, "FUPDATE: %d", result);
  482. //urc fota end err_code
  483. if (result == FUPDATE_RESULT_FAILED)
  484. bootReset(BOOT_RESET_NORMAL);
  485. // 解决FOTA升级后,查询FOTA空间多出fota.type文件的问题
  486. #ifndef CONFIG_QUEC_PROJECT_FEATURE_FOTA
  487. else if (result == FUPDATE_RESULT_FINISHED)
  488. fupdateSetBootAfterFota(FUPDATE_RESULT_FINISHED);
  489. #endif
  490. fsUmountAll();
  491. return result;
  492. }
  493. static bool splVerifySimage(simageHeader_t *header, unsigned max_size)
  494. {
  495. if (!simageValid(header, max_size))
  496. return false;
  497. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SECURE_BOOT
  498. return bootSimageCheckSign(header);
  499. #else
  500. return true;
  501. #endif
  502. }
  503. // check the signatures of SML, TOS, Boot, App
  504. static simageHeader_t *splCheckSignatures()
  505. {
  506. simageHeader_t *header_go = NULL;
  507. simageHeader_t *header_verify = NULL;
  508. // boot
  509. header_verify = (simageHeader_t *)CONFIG_APP_RAM_ADDRESS;
  510. memcpy(header_verify, (void *)CONFIG_BOOT_FLASH_ADDRESS, CONFIG_BOOT_FLASH_SIZE);
  511. if (!splVerifySimage(header_verify, CONFIG_BOOT_FLASH_SIZE))
  512. return NULL;
  513. header_go = header_verify;
  514. // app
  515. header_verify = (simageHeader_t *)CONFIG_APP_FLASH_ADDRESS;
  516. if (!splVerifySimage(header_verify, CONFIG_APP_FLASH_SIZE))
  517. return NULL;
  518. #ifdef CONFIG_TRUSTZONE_SUPPORT
  519. // sml
  520. header_verify = (simageHeader_t *)CONFIG_TZ_SML_FLASH_ADDRESS;
  521. if (!splVerifySimage(header_verify, CONFIG_TZ_SML_FLASH_SIZE))
  522. return NULL;
  523. memcpy((void *)header_verify->data_entry,
  524. (void *)(CONFIG_TZ_SML_FLASH_ADDRESS + header_verify->data_offset),
  525. header_verify->data_size);
  526. header_go = header_verify;
  527. // tos
  528. header_verify = (simageHeader_t *)CONFIG_TZ_TOS_FLASH_ADDRESS;
  529. if (!splVerifySimage(header_verify, CONFIG_TZ_TOS_FLASH_SIZE))
  530. return NULL;
  531. memcpy((void *)header_verify->data_entry,
  532. (void *)(CONFIG_TZ_TOS_FLASH_ADDRESS + header_verify->data_offset),
  533. header_verify->data_size);
  534. #endif
  535. return header_go;
  536. }
  537. static void prvSetFlashWriteProhibit(void)
  538. {
  539. // ATTENTION: This will set write prohibit for bootloader
  540. //
  541. // If there are requiement (though not reasonable) to change bootloader
  542. // the followings should be changed. And when there are more regions are
  543. // known never changed, more regions can be added.
  544. bootSpiFlash_t *flash = bootSpiFlashOpen(HAL_FLASH_DEVICE_NAME(CONFIG_BOOT_FLASH_ADDRESS));
  545. bootSpiFlashSetRangeWriteProhibit(flash, HAL_FLASH_OFFSET(CONFIG_BOOT_FLASH_ADDRESS),
  546. HAL_FLASH_OFFSET(CONFIG_BOOT_FLASH_ADDRESS) + CONFIG_BOOT_FLASH_SIZE);
  547. }
  548. /*********************************************************************************************************************************
  549. * example: how to set pin function as gpio and how to set or get gpio level (take GPIO0 as an example)
  550. * Attention:
  551. 1. the register name can refer to quec_pin_cfg_ECXXXG.c;
  552. 2. gpio corresponding function can refer to quec_pin_cfg_ECXXXG.c;
  553. 3. some pins need to open the voltage domain before use , V_PAD_1V8 voltage domain is on by default and does not need to be set
  554. ***********************************************************************************************************************************/
  555. void quec_boot_gpio_example()
  556. {
  557. //set GPIO_0 as gpio function
  558. hwp_iomux->gpio_0 = (0 << 2);
  559. /*** Output High Level ***/
  560. //set GPIO_0 direction to output
  561. // if GPIO num < 16
  562. hwp_gpio1->gpio_clr_reg = (1 << 0);
  563. hwp_gpio1->gpio_oen_set_out = (1 << 0);
  564. // if GPIO num > 16 (example GPIO17)
  565. //hwp_gpio2->gpio_clr_reg = (1 << (17 - 16));
  566. //hwp_gpio2->gpio_oen_set_out = (1 << (17 - 16));
  567. //set GPIO_0 output high level
  568. // if GPIO num < 16
  569. hwp_gpio1->gpio_set_reg = (1 << 0);
  570. // if GPIO num > 16 (example GPIO17)
  571. //hwp_gpio2->gpio_set_reg = (1 << (17 - 16));
  572. //set GPIO_0 output low level
  573. // if GPIO num < 16
  574. //hwp_gpio1->gpio_clr_reg = (1 << 0);
  575. // if GPIO num > 16 (example GPIO17)
  576. //hwp_gpio2->gpio_clr_reg = (1 << (17 - 16));
  577. /*** Input Pull Up ***/
  578. //set GPIO_0 direction to input
  579. // if GPIO num < 16
  580. //hwp_gpio1->gpio_oen_set_in = (1 << 0);
  581. // if GPIO num > 16 (example GPIO17)
  582. //hwp_gpio2->gpio_oen_set_in = (1 << (17 - 16));
  583. //set GPIO_0 input pull_up
  584. // Attention!!! It's anareg!!! have a extra [pad] in reg name!!!
  585. //hwp_iomux->pad_gpio_0 &= ~(1<<6); //wpdo = 0
  586. //hwp_iomux->pad_gpio_0 |= (1<<7); //wpu = 1
  587. //hwp_iomux->pad_gpio_0 &= ~(1<<12); //wpus = 0
  588. //get gpio level status, gpio_lvl=1 indicates high level, gpio_lvl=0 indicates low level
  589. // if GPIO num < 16
  590. uint32_t reg_value = hwp_gpio1->gpio_set_reg & (1 << 0);
  591. // if GPIO num > 16 (example GPIO17)
  592. //uint32_t reg_value = hwp_gpio2->gpio_set_reg & (1 << 17);
  593. int gpio_lvl = (reg_value >> 0) & 0x1;
  594. OSI_LOGI(0, "gpio_lvl=%d", gpio_lvl);
  595. }
  596. /*********************************************************************************************************************************
  597. * example: how to use spi4 nor flash in boot
  598. * Attention:
  599. 1. the SPI port currently used in boot,it can be configured in quec_boot_pin_cfg.h;
  600. ***********************************************************************************************************************************/
  601. #ifdef CONFIG_QUEC_PROJECT_FEATURE_BOOT_SPI4_NORFLASH
  602. #include "quec_boot_spi.h"
  603. #include "ql_boot_spi4_nor_flash.h"
  604. //Note:the SPI port currently used in boot,it can be configured in quec_boot_pin_cfg.h
  605. extern uint8_t ql_boot_cur_spi4_port;
  606. void quec_boot_spi4_nor_flash_example()
  607. {
  608. //Initialize pin to SPI function
  609. quec_boot_spi_pin_init(ql_boot_cur_spi4_port);
  610. //Configure nor Flash initialization parameters
  611. ql_boot_spi_nor_config_s flash_config = {0};
  612. flash_config.port = ql_boot_cur_spi4_port;
  613. flash_config.spiclk = QL_BOOT_SPI_CLK_25MHZ;
  614. flash_config.input_sel = QL_BOOT_SPI_DI_1;
  615. flash_config.transmode = QL_BOOT_SPI_DIRECT_POLLING;//BOOT中不支持DMA模式
  616. flash_config.cs = QL_BOOT_SPI_CS0;
  617. ql_boot_spi_nor_init_ext(flash_config);
  618. OSI_LOGI(0, "ql_boot_spi4_nor_flash_init");
  619. unsigned int addr = 0x000008;
  620. unsigned int len = 0;
  621. unsigned char *data = NULL;
  622. int i = 0;
  623. if(ql_boot_spi_nor_erase_sector(ql_boot_cur_spi4_port, addr) != QL_BOOT_SPI_SUCCESS)
  624. {
  625. return;
  626. }
  627. len = 1024;
  628. data = bootExtRamCalloc(1,len+1);
  629. if (data == NULL)
  630. {
  631. return;
  632. }
  633. for (i = 0; i < len; i++)
  634. {
  635. data[i] = 'b';
  636. }
  637. QUEC_BOOT_LOG("write data:%s", data);
  638. if(ql_boot_spi_nor_write(ql_boot_cur_spi4_port, data, addr, len) == QL_BOOT_SPI_SUCCESS)
  639. {
  640. memset(data,0x00,len+1);
  641. ql_boot_spi_nor_read(ql_boot_cur_spi4_port, data, addr, len);
  642. QUEC_BOOT_LOG("read data:%s", data);
  643. }
  644. bootFree(data);
  645. }
  646. #endif
  647. void bootStart(uint32_t param)
  648. {
  649. OSI_CLEAR_SECTION(bss);
  650. bootSpiFlashOpen(DRV_NAME_SPI_FLASH); // ensure accessible
  651. halClockInit(HAL_CLOCK_INIT_BOOTLOADER);
  652. bootSetFirewall();
  653. bootUsbParamCalibration();
  654. halRamInit();
  655. bootMmuEnable();
  656. __FPU_Enable();
  657. _REENT_INIT_PTR_ZEROED(_impure_ptr);
  658. halAdiBusInit();
  659. bootResetPinEnable();
  660. bootWdtInit();
  661. bootGetEfuseCtx();
  662. #if 0
  663. if (bootIsFromPsmSleep())
  664. {
  665. #ifdef CONFIG_TRUSTZONE_SUPPORT
  666. simageHeader_t *header = bootloaderJumpImageEntry();
  667. if(header == NULL)
  668. goto failed;
  669. SmlJumpImageEntry();
  670. #else
  671. simageHeader_t *header = (simageHeader_t *)CONFIG_APP_FLASH_ADDRESS;
  672. simageJumpEntry(header, OSI_SHUTDOWN_PSM_SLEEP); // never return
  673. #endif
  674. }
  675. #endif
  676. if (!prvCheckNormalPowerUp())
  677. {
  678. osiDelayUS(1000 * 10);
  679. bootPowerOff();
  680. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  681. while(1);
  682. // 上电后,若插上USB,boot层会跑到这里满足if条件,执行强制关机
  683. // 但关机需要一段时间,boot层代码会继续执行到prvFirmwareUpdate中的fsMountAll才完成关机
  684. // 添加while(1)是为了避免boot层代码的继续执行,在此等待关机完成
  685. #endif
  686. }
  687. bool trace_enable = false;
  688. #ifdef CONFIG_BOOT_LOG_ENABLED
  689. trace_enable = true;
  690. #endif
  691. bootSetUartIomux();
  692. bootTraceInit(trace_enable);
  693. bootPlatformInit();
  694. extern uint32_t __sram_heap_start[];
  695. extern uint32_t __sram_heap_end[];
  696. extern uint32_t __ram_heap_start[];
  697. extern uint32_t __ram_heap_end[];
  698. unsigned sram_heap_size = OSI_PTR_DIFF(__sram_heap_end, __sram_heap_start);
  699. unsigned ram_heap_size = OSI_PTR_DIFF(__ram_heap_end, __ram_heap_start);
  700. bootHeapInit(__sram_heap_start, sram_heap_size, __ram_heap_start, ram_heap_size);
  701. bootHeapDefaultExtRam();
  702. #ifndef CONFIG_QUEC_PROJECT_FEATURE
  703. halPmuExtFlashPowerOn();
  704. #else
  705. #if (defined CONFIG_QUEC_PROJECT_FEATURE_EXT_MAPPING_UFS || defined CONFIG_QUEC_PROJECT_FEATURE_SPI6_EXT_NOR)
  706. //1、如果是外置flash挂载成UFS分区,需要先初始化外置flash,否则fsMountAll时会有问题
  707. //2、如果使能外置flash使用xip功能,都初始化外置flash
  708. if(ql_ext_flash_cfg.xip_flag)
  709. {
  710. halPmuExtFlashPowerOn();
  711. }
  712. #endif
  713. #endif
  714. fupdateResult_t result = prvFirmwareUpdate();
  715. if(result == FUPDATE_RESULT_FINISHED)
  716. {
  717. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  718. OSI_LOGI(0, "update done rst");
  719. osiDelayUS(1000 * 10); //加点延时避免有log来不及打印就复位了
  720. #endif
  721. bootReset(BOOT_RESET_NORMAL);
  722. }
  723. prvDualSplCheck(param);
  724. //bootloader 写保护功能默认开启
  725. prvSetFlashWriteProhibit();
  726. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SECURE_BOOT
  727. if (!bootUpdateVersion())
  728. goto failed;
  729. #endif
  730. simageHeader_t *header = splCheckSignatures();
  731. if(header == NULL)
  732. goto failed;
  733. simageJumpEntry(header, 0); // never return
  734. failed:
  735. OSI_DEAD_LOOP;
  736. }