bootloader.ld 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /** Simplified memory map for the bootloader.
  7. * Make sure the bootloader can load into main memory without overwriting itself.
  8. *
  9. * ESP32-C2 ROM static data usage is as follows:
  10. * - 0x3fccb264 - 0x3fcdcb70: Shared buffers, used in UART/USB/SPI download mode only
  11. * - 0x3fcdcb70 - 0x3fcdeb70: PRO CPU stack, can be reclaimed as heap after RTOS startup
  12. * - 0x3fcdeb70 - 0x3fce0000: ROM .bss and .data (not easily reclaimable)
  13. *
  14. * The 2nd stage bootloader can take space up to the end of ROM shared
  15. * buffers area (0x3fcdcb70).
  16. */
  17. /* The offset between Dbus and Ibus. Used to convert between 0x403xxxxx and 0x3fcxxxxx addresses. */
  18. iram_dram_offset = 0x6e0000;
  19. /* We consider 0x3fcdcb70 to be the last usable address for 2nd stage bootloader stack overhead, dram_seg,
  20. * and work out iram_seg and iram_loader_seg addresses from there, backwards.
  21. */
  22. /* These lengths can be adjusted, if necessary: */
  23. bootloader_usable_dram_end = 0x3fcdcb70;
  24. bootloader_stack_overhead = 0x2000; /* For safety margin between bootloader data section and startup stacks */
  25. bootloader_dram_seg_len = 0x5000;
  26. bootloader_iram_loader_seg_len = 0x7000;
  27. bootloader_iram_seg_len = 0x2000;
  28. /* Start of the lower region is determined by region size and the end of the higher region */
  29. bootloader_dram_seg_end = bootloader_usable_dram_end - bootloader_stack_overhead;
  30. bootloader_dram_seg_start = bootloader_dram_seg_end - bootloader_dram_seg_len;
  31. bootloader_iram_loader_seg_start = bootloader_dram_seg_start - bootloader_iram_loader_seg_len + iram_dram_offset;
  32. bootloader_iram_seg_start = bootloader_iram_loader_seg_start - bootloader_iram_seg_len;
  33. MEMORY
  34. {
  35. iram_seg (RWX) : org = bootloader_iram_seg_start, len = bootloader_iram_seg_len
  36. iram_loader_seg (RWX) : org = bootloader_iram_loader_seg_start, len = bootloader_iram_loader_seg_len
  37. dram_seg (RW) : org = bootloader_dram_seg_start, len = bootloader_dram_seg_len
  38. }
  39. /* The app may use RAM for static allocations up to the start of iram_loader_seg.
  40. * If you have changed something above and this assert fails:
  41. * 1. Check what the new value of bootloader_iram_loader_seg start is.
  42. * 2. Update the value in this assert.
  43. * 3. Update (SRAM_DRAM_END + I_D_SRAM_OFFSET) in components/esp_system/ld/esp32c2/memory.ld.in to the same value.
  44. */
  45. ASSERT(bootloader_iram_loader_seg_start == 0x403aeb70, "bootloader_iram_loader_seg_start inconsistent with SRAM_DRAM_END");
  46. /* Default entry point: */
  47. ENTRY(call_start_cpu0);
  48. SECTIONS
  49. {
  50. .iram_loader.text :
  51. {
  52. . = ALIGN (16);
  53. _loader_text_start = ABSOLUTE(.);
  54. *(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
  55. *(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
  56. *liblog.a:(.literal .text .literal.* .text.*)
  57. /* we use either libgcc or compiler-rt, so put similar entries for them here */
  58. *libgcc.a:(.literal .text .literal.* .text.*)
  59. *libclang_rt.builtins.a:(.literal .text .literal.* .text.*)
  60. *libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*)
  61. *libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*)
  62. *libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*)
  63. *libbootloader_support.a:bootloader_random.*(.literal .text .literal.* .text.*)
  64. *libbootloader_support.a:bootloader_random*.*(.literal.bootloader_random_disable .text.bootloader_random_disable)
  65. *libbootloader_support.a:bootloader_efuse.*(.literal .text .literal.* .text.*)
  66. *libbootloader_support.a:bootloader_utility.*(.literal .text .literal.* .text.*)
  67. *libbootloader_support.a:bootloader_sha.*(.literal .text .literal.* .text.*)
  68. *libbootloader_support.a:bootloader_console_loader.*(.literal .text .literal.* .text.*)
  69. *libbootloader_support.a:bootloader_panic.*(.literal .text .literal.* .text.*)
  70. *libbootloader_support.a:bootloader_soc.*(.literal .text .literal.* .text.*)
  71. *libbootloader_support.a:esp_image_format.*(.literal .text .literal.* .text.*)
  72. *libbootloader_support.a:flash_encrypt.*(.literal .text .literal.* .text.*)
  73. *libbootloader_support.a:flash_encryption_secure_features.*(.literal .text .literal.* .text.*)
  74. *libbootloader_support.a:flash_partitions.*(.literal .text .literal.* .text.*)
  75. *libbootloader_support.a:secure_boot.*(.literal .text .literal.* .text.*)
  76. *libbootloader_support.a:secure_boot_secure_features.*(.literal .text .literal.* .text.*)
  77. *libbootloader_support.a:secure_boot_signatures_bootloader.*(.literal .text .literal.* .text.*)
  78. *libmicro-ecc.a:*.*(.literal .text .literal.* .text.*)
  79. *libspi_flash.a:*.*(.literal .text .literal.* .text.*)
  80. *libhal.a:wdt_hal_iram.*(.literal .text .literal.* .text.*)
  81. *libhal.a:mmu_hal.*(.literal .text .literal.* .text.*)
  82. *libhal.a:cache_hal.*(.literal .text .literal.* .text.*)
  83. *libhal.a:efuse_hal.*(.literal .text .literal.* .text.*)
  84. *libesp_hw_support.a:rtc_clk.*(.literal .text .literal.* .text.*)
  85. *libesp_hw_support.a:rtc_time.*(.literal .text .literal.* .text.*)
  86. *libesp_hw_support.a:regi2c_ctrl.*(.literal .text .literal.* .text.*)
  87. *libefuse.a:*.*(.literal .text .literal.* .text.*)
  88. *(.fini.literal)
  89. *(.fini)
  90. *(.gnu.version)
  91. _loader_text_end = ABSOLUTE(.);
  92. } > iram_loader_seg
  93. .iram.text :
  94. {
  95. . = ALIGN (16);
  96. *(.entry.text)
  97. *(.init.literal)
  98. *(.init)
  99. } > iram_seg
  100. /* Shared RAM */
  101. .dram0.bss (NOLOAD) :
  102. {
  103. . = ALIGN (8);
  104. _dram_start = ABSOLUTE(.);
  105. _bss_start = ABSOLUTE(.);
  106. *(.dynsbss)
  107. *(.sbss)
  108. *(.sbss.*)
  109. *(.gnu.linkonce.sb.*)
  110. *(.scommon)
  111. *(.sbss2)
  112. *(.sbss2.*)
  113. *(.gnu.linkonce.sb2.*)
  114. *(.dynbss)
  115. *(.bss)
  116. *(.bss.*)
  117. *(.gnu.linkonce.b.*)
  118. *(COMMON)
  119. . = ALIGN (8);
  120. _bss_end = ABSOLUTE(.);
  121. } > dram_seg
  122. .dram0.bootdesc : ALIGN(0x10)
  123. {
  124. _data_start = ABSOLUTE(.);
  125. *(.data_bootloader_desc .data_bootloader_desc.*) /* Should be the first. Bootloader version info. DO NOT PUT ANYTHING BEFORE IT! */
  126. } > dram_seg
  127. .dram0.data :
  128. {
  129. *(.data)
  130. *(.data.*)
  131. *(.gnu.linkonce.d.*)
  132. *(.data1)
  133. *(.sdata)
  134. *(.sdata.*)
  135. *(.gnu.linkonce.s.*)
  136. *(.gnu.linkonce.s2.*)
  137. *(.jcr)
  138. _data_end = ABSOLUTE(.);
  139. } > dram_seg
  140. .dram0.rodata :
  141. {
  142. _rodata_start = ABSOLUTE(.);
  143. *(.rodata)
  144. *(.rodata.*)
  145. *(.gnu.linkonce.r.*)
  146. *(.rodata1)
  147. *(.sdata2 .sdata2.* .srodata .srodata.*)
  148. __XT_EXCEPTION_TABLE_ = ABSOLUTE(.);
  149. *(.xt_except_table)
  150. *(.gcc_except_table)
  151. *(.gnu.linkonce.e.*)
  152. *(.gnu.version_r)
  153. *(.eh_frame)
  154. . = (. + 3) & ~ 3;
  155. /* C++ constructor and destructor tables, properly ordered: */
  156. __init_array_start = ABSOLUTE(.);
  157. KEEP (*crtbegin.*(.ctors))
  158. KEEP (*(EXCLUDE_FILE (*crtend.*) .ctors))
  159. KEEP (*(SORT(.ctors.*)))
  160. KEEP (*(.ctors))
  161. __init_array_end = ABSOLUTE(.);
  162. KEEP (*crtbegin.*(.dtors))
  163. KEEP (*(EXCLUDE_FILE (*crtend.*) .dtors))
  164. KEEP (*(SORT(.dtors.*)))
  165. KEEP (*(.dtors))
  166. /* C++ exception handlers table: */
  167. __XT_EXCEPTION_DESCS_ = ABSOLUTE(.);
  168. *(.xt_except_desc)
  169. *(.gnu.linkonce.h.*)
  170. __XT_EXCEPTION_DESCS_END__ = ABSOLUTE(.);
  171. *(.xt_except_desc_end)
  172. *(.dynamic)
  173. *(.gnu.version_d)
  174. _rodata_end = ABSOLUTE(.);
  175. /* Literals are also RO data. */
  176. _lit4_start = ABSOLUTE(.);
  177. *(*.lit4)
  178. *(.lit4.*)
  179. *(.gnu.linkonce.lit4.*)
  180. _lit4_end = ABSOLUTE(.);
  181. . = ALIGN(4);
  182. _dram_end = ABSOLUTE(.);
  183. } > dram_seg
  184. .iram.text :
  185. {
  186. _stext = .;
  187. _text_start = ABSOLUTE(.);
  188. *(.literal .text .literal.* .text.* .stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
  189. *(.iram .iram.*) /* catch stray IRAM_ATTR */
  190. *(.fini.literal)
  191. *(.fini)
  192. *(.gnu.version)
  193. /** CPU will try to prefetch up to 16 bytes of
  194. * of instructions. This means that any configuration (e.g. MMU, PMS) must allow
  195. * safe access to up to 16 bytes after the last real instruction, add
  196. * dummy bytes to ensure this
  197. */
  198. . += 16;
  199. _text_end = ABSOLUTE(.);
  200. _etext = .;
  201. } > iram_seg
  202. }
  203. /**
  204. * Appendix: Memory Usage of ROM bootloader
  205. *
  206. * 0x3fccb264 ------------------> _dram0_0_start
  207. * | |
  208. * | |
  209. * | | 1. Large buffers that are only used in certain boot modes, see shared_buffers.h
  210. * | |
  211. * | |
  212. * 0x3fcdcb70 ------------------> __stack_sentry
  213. * | |
  214. * | | 2. Startup pro cpu stack (freed when IDF app is running)
  215. * | |
  216. * 0x3fcdeb70 ------------------> __stack (pro cpu)
  217. * | |
  218. * | |
  219. * | | 3. Shared memory only used in startup code or nonos/early boot*
  220. * | | (can be freed when IDF runs)
  221. * | |
  222. * | |
  223. * 0x3fcdf4bc ------------------> _dram0_rtos_reserved_start
  224. * | |
  225. * | |
  226. * | | 4. Shared memory used in startup code and when IDF runs
  227. * | |
  228. * | |
  229. * 0x3fcdfa70 ------------------> _dram0_rtos_reserved_end
  230. * | |
  231. * 0x3fcdfa74 ------------------> _data_start_interface
  232. * | |
  233. * | | 5. End of DRAM is the 'interface' data with constant addresses (ECO compatible)
  234. * | |
  235. * 0x3fce0000 ------------------> _data_end_interface
  236. */