soc_boot.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2021 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Actions LARK family boot related infomation public interfaces.
  9. */
  10. #include <kernel.h>
  11. #include <device.h>
  12. #include <string.h>
  13. #include "soc.h"
  14. #include <linker/linker-defs.h>
  15. #include <partition/partition.h>
  16. #include <drivers/flash.h>
  17. #include <board_cfg.h>
  18. #include "spicache.h"
  19. /*mbrec mem layout*/
  20. /*
  21. 0x2ff60000-0x2ff60200 //sram 1KB save boot_info(0x20) +NANDID
  22. 0x2ff60020-0x2ff60400 // NANDID
  23. */
  24. #define BOOT_INFO_SRAM_ADDR (CONFIG_SRAM_BASE_ADDRESS-0x400)
  25. uint32_t soc_boot_get_part_tbl_addr(void)
  26. {
  27. return soc_boot_get_info()->param_phy_addr;
  28. }
  29. uint32_t soc_boot_get_fw_ver_addr(void)
  30. {
  31. return (soc_boot_get_part_tbl_addr() + SOC_BOOT_FIRMWARE_VERSION_OFFSET);
  32. }
  33. const boot_info_t *soc_boot_get_info(void)
  34. {
  35. return (const boot_info_t *)BOOT_INFO_SRAM_ADDR;
  36. }
  37. uint32_t soc_boot_get_nandid_tbl_addr(void)
  38. {
  39. const boot_info_t *p_boot_info = soc_boot_get_info();
  40. return p_boot_info->nand_id_offs;
  41. }
  42. u32_t soc_boot_get_reboot_reason(void)
  43. {
  44. const boot_info_t *p_boot_info = soc_boot_get_info();
  45. return p_boot_info->reboot_reason;
  46. }
  47. bool soc_boot_get_watchdog_is_reboot(void)
  48. {
  49. const boot_info_t *p_boot_info = soc_boot_get_info();
  50. return !!p_boot_info->watchdog_reboot;
  51. }
  52. void soc_boot_set_system_phy_addr(uint32_t system_phy_addr)
  53. {
  54. boot_info_t *p_boot_info = (boot_info_t*)soc_boot_get_info();
  55. p_boot_info->system_phy_addr = system_phy_addr;
  56. }
  57. #ifdef CONFIG_SPI_FLASH_ACTS
  58. void sys_norflash_exit_4b(void);
  59. #endif
  60. #ifdef CONFIG_PM_DEVICE
  61. extern int pm_power_off_devices(void);
  62. #endif
  63. #ifdef CONFIG_SOC_SIGN_ENABLE
  64. #include "soc_rsa.c"
  65. static uint8_t g_key_data[0x400] __aligned(8);
  66. static uint16_t g_key_len;
  67. #define BOOT_RUN_ADDR 0x11000000
  68. #define APP_RUN_ADDR 0x10000000
  69. void dbg_printk_uint(unsigned int val);
  70. static uint32_t app_part_offset;
  71. __ramfunc const void *cache_to_dma_addr(const void * vaddr)
  72. {
  73. uint32_t addr = (uint32_t)vaddr;
  74. dbg_printk_uint(addr);
  75. if(addr < SPI0_UNCACHE_ADDR) {// nor
  76. vaddr = (const void *)(SPI0_UNCACHE_ADDR + addr - APP_RUN_ADDR + app_part_offset);
  77. dbg_printk_uint((uint32_t)vaddr);
  78. sys_set_bit(SPICACHE_CTL, 9);
  79. }
  80. return vaddr;
  81. }
  82. static void sign_load_key(void)
  83. {
  84. uint16_t key_len;
  85. uint8_t *key;
  86. key = p_brom_api->p_image_tlv_find((void*)BOOT_RUN_ADDR, IMAGE_TLV_PUBKEY, &key_len, 1);
  87. if(key == NULL){
  88. printk("sign_load_key fail\n");
  89. g_key_len = 0;
  90. }else{
  91. g_key_len = key_len;
  92. memcpy(g_key_data, key, key_len);
  93. printk("sign_load_key ok, key_len=0x%x, 0x%x\n", key_len, g_key_data[0]);
  94. }
  95. }
  96. /*0 is ok else fail*/
  97. __ramfunc int image_security_data_check(image_head_t *ih_app)
  98. {
  99. uint8_t *sig_data;
  100. uint32_t all_len, t;
  101. uint16_t sig_len;
  102. int ret;
  103. //sys_write32(0x1, T0_CTL);//stop and clear pending // for brom p_verify_signature api
  104. //sys_write32(0xffffffff, T0_VAL);
  105. /* enable timer */
  106. //sys_write32(0x24, T0_CTL); // for brom p_verify_signature api
  107. sig_data =p_brom_api->p_image_tlv_find(ih_app, IMAGE_TLV_RSA2048_PSS, &sig_len, 0);
  108. if(sig_data == NULL){
  109. return 0x1001;
  110. }
  111. if(!g_key_len)
  112. return 0x1002;
  113. all_len = ih_app->ih_hdr_size + ih_app->ih_img_size + ih_app->ih_ptlv_size;
  114. dbg_printk_uint(all_len);
  115. t = sys_read32(T2_CNT);
  116. //return p_brom_api->p_verify_signature(g_key_data, sig_data, (const uint8_t *)(cache_to_dma_addr(ih_app)), all_len);
  117. #ifdef CONFIG_SHA256_HARDWARE
  118. ret = verify_signature(g_key_data, sig_data, (const uint8_t *)(cache_to_dma_addr(ih_app)), all_len);
  119. #else
  120. dbg_printk_uint((uint32_t)(ih_app));
  121. ret = verify_signature(g_key_data, sig_data, (const uint8_t *)(ih_app), all_len);
  122. #endif
  123. dbg_printk_uint(sys_read32(T2_CNT)-t);
  124. return ret;
  125. }
  126. #else
  127. __ramfunc int image_security_data_check(image_head_t *ih_app)
  128. {
  129. return 0;
  130. }
  131. static void sign_load_key(void)
  132. {
  133. }
  134. #endif
  135. extern uint32_t libboot_version_dump(void);
  136. int boot_to_part(int part_type, int mirror_id, int flashid)
  137. {
  138. const struct partition_entry *part;
  139. int crc_is_enabled;
  140. #ifdef CONFIG_ACTIONS_PRINTK_DMA
  141. printk_dma_switch(0);
  142. #endif
  143. part = partition_get_part_by_type_mirrorid(part_type, mirror_id);
  144. if(part == NULL){
  145. printk("not find app parition--\n");
  146. return -1;
  147. }
  148. crc_is_enabled = part->flag & PARTITION_FLAG_ENABLE_CRC ? 1 : 0;
  149. printk("app offset=0x%x ,crc=%d\n", part->file_offset, crc_is_enabled);
  150. soc_boot_set_system_phy_addr(part->file_offset);
  151. #ifdef CONFIG_SOC_SIGN_ENABLE
  152. app_part_offset = part->file_offset;
  153. #endif
  154. if(flashid == BOOT_FLASH_ID_NAND){
  155. boot_nand(part->file_offset, 0);
  156. }else if(flashid == BOOT_FLASH_ID_EXTNOR){
  157. #ifdef CONFIG_SPI_FLASH_ACTS
  158. #ifdef CONFIG_SPI_NOR_FLASH_4B_ADDRESS
  159. sys_norflash_exit_4b();
  160. #endif
  161. #endif
  162. boot_ext_nor(part->file_offset, crc_is_enabled, 0);
  163. }else{
  164. boot_nor(part->file_offset, crc_is_enabled, 0);
  165. }
  166. printk("boot fail\n");
  167. __asm__ volatile("cpsie i");
  168. __asm__ volatile("dsb");
  169. return 0;
  170. }
  171. static void boot_clear_soc(void)
  172. {
  173. int i;
  174. for(i = 0; i < CONFIG_NUM_IRQS; i++) // all irq disable
  175. irq_disable(i);
  176. for(i = 0; i < GPIO_MAX_IRQ_GRP; i++ ){ // clear all gpio pending
  177. sys_write32(0xffffffff, GPIO_REG_IRQ_PD(GPIO_REG_BASE, i*32));
  178. }
  179. }
  180. int boot_to_app(int mirror_id, int flashid)
  181. {
  182. libboot_version_dump();
  183. printk("boot main=%d \n", flashid);
  184. irq_lock();
  185. sign_load_key();
  186. boot_clear_soc();
  187. #ifdef CONFIG_PM_DEVICE
  188. printk("dev power off\n");
  189. pm_power_off_devices();
  190. printk("dev power end\n");
  191. #endif
  192. #ifdef CONFIG_SPI_XIP_READ
  193. soc_memctrl_unmapping(CONFIG_SPI_XIP_VADDR);
  194. #endif
  195. return boot_to_part(PARTITION_TYPE_SYSTEM, mirror_id, flashid);
  196. }