bootloader_flash.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stddef.h>
  14. #include <bootloader_flash.h>
  15. #include <esp_log.h>
  16. #include <esp_spi_flash.h> /* including in bootloader for error values */
  17. #include <esp_flash_encrypt.h>
  18. #ifndef BOOTLOADER_BUILD
  19. /* Normal app version maps to esp_spi_flash.h operations...
  20. */
  21. static const char *TAG = "bootloader_mmap";
  22. static spi_flash_mmap_memory_t map;
  23. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  24. {
  25. if (map) {
  26. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  27. return NULL; /* existing mapping in use... */
  28. }
  29. const void *result = NULL;
  30. uint32_t src_page = src_addr & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  31. size += (src_addr - src_page);
  32. esp_err_t err = spi_flash_mmap(src_page, size, SPI_FLASH_MMAP_DATA, &result, &map);
  33. if (err != ESP_OK) {
  34. result = NULL;
  35. }
  36. return (void *)((intptr_t)result + (src_addr - src_page));
  37. }
  38. void bootloader_munmap(const void *mapping)
  39. {
  40. if(mapping && map) {
  41. spi_flash_munmap(map);
  42. }
  43. map = 0;
  44. }
  45. esp_err_t bootloader_flash_read(size_t src, void *dest, size_t size, bool allow_decrypt)
  46. {
  47. if (allow_decrypt && esp_flash_encryption_enabled()) {
  48. return spi_flash_read_encrypted(src, dest, size);
  49. } else {
  50. return spi_flash_read(src, dest, size);
  51. }
  52. }
  53. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  54. {
  55. if (write_encrypted) {
  56. return spi_flash_write_encrypted(dest_addr, src, size);
  57. } else {
  58. return spi_flash_write(dest_addr, src, size);
  59. }
  60. }
  61. esp_err_t bootloader_flash_erase_sector(size_t sector)
  62. {
  63. return spi_flash_erase_sector(sector);
  64. }
  65. #else
  66. /* Bootloader version, uses ROM functions only */
  67. #include <soc/dport_reg.h>
  68. #include <rom/spi_flash.h>
  69. #include <rom/cache.h>
  70. static const char *TAG = "bootloader_flash";
  71. /* Use first 50 blocks in MMU for bootloader_mmap,
  72. 50th block for bootloader_flash_read
  73. */
  74. #define MMU_BLOCK0_VADDR 0x3f400000
  75. #define MMU_BLOCK50_VADDR 0x3f720000
  76. #define MMU_FLASH_MASK 0xffff0000
  77. #define MMU_BLOCK_SIZE 0x00010000
  78. static bool mapped;
  79. // Current bootloader mapping (ab)used for bootloader_read()
  80. static uint32_t current_read_mapping = UINT32_MAX;
  81. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  82. {
  83. if (mapped) {
  84. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  85. return NULL; /* can't map twice */
  86. }
  87. if (size > 0x320000) {
  88. /* Allow mapping up to 50 of the 51 available MMU blocks (last one used for reads) */
  89. ESP_LOGE(TAG, "bootloader_mmap excess size %x", size);
  90. return NULL;
  91. }
  92. uint32_t src_addr_aligned = src_addr & MMU_FLASH_MASK;
  93. uint32_t count = (size + (src_addr - src_addr_aligned) + 0xffff) / MMU_BLOCK_SIZE;
  94. Cache_Read_Disable(0);
  95. Cache_Flush(0);
  96. ESP_LOGD(TAG, "mmu set paddr=%08x count=%d", src_addr_aligned, count );
  97. int e = cache_flash_mmu_set(0, 0, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count);
  98. if (e != 0) {
  99. ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  100. Cache_Read_Enable(0);
  101. return NULL;
  102. }
  103. Cache_Read_Enable(0);
  104. mapped = true;
  105. return (void *)(MMU_BLOCK0_VADDR + (src_addr - src_addr_aligned));
  106. }
  107. void bootloader_munmap(const void *mapping)
  108. {
  109. if (mapped) {
  110. /* Full MMU reset */
  111. Cache_Read_Disable(0);
  112. Cache_Flush(0);
  113. mmu_init(0);
  114. mapped = false;
  115. current_read_mapping = UINT32_MAX;
  116. }
  117. }
  118. static esp_err_t spi_to_esp_err(esp_rom_spiflash_result_t r)
  119. {
  120. switch(r) {
  121. case ESP_ROM_SPIFLASH_RESULT_OK:
  122. return ESP_OK;
  123. case ESP_ROM_SPIFLASH_RESULT_ERR:
  124. return ESP_ERR_FLASH_OP_FAIL;
  125. case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
  126. return ESP_ERR_FLASH_OP_TIMEOUT;
  127. default:
  128. return ESP_FAIL;
  129. }
  130. }
  131. static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size)
  132. {
  133. Cache_Read_Disable(0);
  134. Cache_Flush(0);
  135. esp_rom_spiflash_result_t r = esp_rom_spiflash_read(src_addr, dest, size);
  136. Cache_Read_Enable(0);
  137. return spi_to_esp_err(r);
  138. }
  139. static esp_err_t bootloader_flash_read_allow_decrypt(size_t src_addr, void *dest, size_t size)
  140. {
  141. uint32_t *dest_words = (uint32_t *)dest;
  142. /* Use the 51st MMU mapping to read from flash in 64KB blocks.
  143. (MMU will transparently decrypt if encryption is enabled.)
  144. */
  145. for (int word = 0; word < size / 4; word++) {
  146. uint32_t word_src = src_addr + word * 4; /* Read this offset from flash */
  147. uint32_t map_at = word_src & MMU_FLASH_MASK; /* Map this 64KB block from flash */
  148. uint32_t *map_ptr;
  149. if (map_at != current_read_mapping) {
  150. /* Move the 64KB mmu mapping window to fit map_at */
  151. Cache_Read_Disable(0);
  152. Cache_Flush(0);
  153. ESP_LOGD(TAG, "mmu set block paddr=0x%08x (was 0x%08x)", map_at, current_read_mapping);
  154. int e = cache_flash_mmu_set(0, 0, MMU_BLOCK50_VADDR, map_at, 64, 1);
  155. if (e != 0) {
  156. ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  157. Cache_Read_Enable(0);
  158. return ESP_FAIL;
  159. }
  160. current_read_mapping = map_at;
  161. Cache_Read_Enable(0);
  162. }
  163. map_ptr = (uint32_t *)(MMU_BLOCK50_VADDR + (word_src - map_at));
  164. dest_words[word] = *map_ptr;
  165. }
  166. return ESP_OK;
  167. }
  168. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt)
  169. {
  170. if (src_addr & 3) {
  171. ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
  172. return ESP_FAIL;
  173. }
  174. if (size & 3) {
  175. ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size);
  176. return ESP_FAIL;
  177. }
  178. if ((intptr_t)dest & 3) {
  179. ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
  180. return ESP_FAIL;
  181. }
  182. if (allow_decrypt) {
  183. return bootloader_flash_read_allow_decrypt(src_addr, dest, size);
  184. } else {
  185. return bootloader_flash_read_no_decrypt(src_addr, dest, size);
  186. }
  187. }
  188. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  189. {
  190. esp_err_t err;
  191. size_t alignment = write_encrypted ? 32 : 4;
  192. if ((dest_addr % alignment) != 0) {
  193. ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment);
  194. return ESP_FAIL;
  195. }
  196. if ((size % alignment) != 0) {
  197. ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment);
  198. return ESP_FAIL;
  199. }
  200. if (((intptr_t)src % 4) != 0) {
  201. ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src);
  202. return ESP_FAIL;
  203. }
  204. err = spi_to_esp_err(esp_rom_spiflash_unlock());
  205. if (err != ESP_OK) {
  206. return err;
  207. }
  208. if (write_encrypted) {
  209. return spi_to_esp_err(esp_rom_spiflash_write_encrypted(dest_addr, src, size));
  210. } else {
  211. return spi_to_esp_err(esp_rom_spiflash_write(dest_addr, src, size));
  212. }
  213. }
  214. esp_err_t bootloader_flash_erase_sector(size_t sector)
  215. {
  216. return spi_to_esp_err(esp_rom_spiflash_erase_sector(sector));
  217. }
  218. #endif