bootloader_flash.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #ifndef BOOTLOADER_BUILD
  18. /* Normal app version maps to esp_spi_flash.h operations...
  19. */
  20. static const char *TAG = "bootloader_mmap";
  21. static spi_flash_mmap_memory_t map;
  22. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  23. {
  24. if (map) {
  25. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  26. return NULL; /* existing mapping in use... */
  27. }
  28. const void *result = NULL;
  29. esp_err_t err = spi_flash_mmap(src_addr, size, SPI_FLASH_MMAP_DATA, &result, &map);
  30. if (err != ESP_OK) {
  31. result = NULL;
  32. }
  33. return result;
  34. }
  35. void bootloader_munmap(const void *mapping)
  36. {
  37. if(mapping && map) {
  38. spi_flash_munmap(map);
  39. }
  40. map = 0;
  41. }
  42. esp_err_t bootloader_flash_read(size_t src, void *dest, size_t size, bool allow_decrypt)
  43. {
  44. return spi_flash_read(src, dest, size);
  45. }
  46. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  47. {
  48. if (write_encrypted) {
  49. return spi_flash_write_encrypted(dest_addr, src, size);
  50. } else {
  51. return spi_flash_write(dest_addr, src, size);
  52. }
  53. }
  54. esp_err_t bootloader_flash_erase_sector(size_t sector)
  55. {
  56. return spi_flash_erase_sector(sector);
  57. }
  58. #else
  59. /* Bootloader version, uses ROM functions only */
  60. #include <soc/dport_reg.h>
  61. #include <rom/spi_flash.h>
  62. #include <rom/cache.h>
  63. static const char *TAG = "bootloader_flash";
  64. /* Use first 50 blocks in MMU for bootloader_mmap,
  65. 50th block for bootloader_flash_read
  66. */
  67. #define MMU_BLOCK0_VADDR 0x3f400000
  68. #define MMU_BLOCK50_VADDR 0x3f720000
  69. #define MMU_FLASH_MASK 0xffff0000
  70. #define MMU_BLOCK_SIZE 0x00010000
  71. static bool mapped;
  72. static uint32_t current_read_mapping = UINT32_MAX;
  73. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  74. {
  75. if (mapped) {
  76. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  77. return NULL; /* can't map twice */
  78. }
  79. if (size > 0x320000) {
  80. /* Allow mapping up to 50 of the 51 available MMU blocks (last one used for reads) */
  81. ESP_LOGE(TAG, "bootloader_mmap excess size %x", size);
  82. return NULL;
  83. }
  84. uint32_t src_addr_aligned = src_addr & MMU_FLASH_MASK;
  85. uint32_t count = (size + (src_addr - src_addr_aligned) + 0xffff) / MMU_BLOCK_SIZE;
  86. Cache_Read_Disable(0);
  87. Cache_Flush(0);
  88. ESP_LOGD(TAG, "mmu set paddr=%08x count=%d", src_addr_aligned, count );
  89. cache_flash_mmu_set( 0, 0, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count );
  90. Cache_Read_Enable( 0 );
  91. mapped = true;
  92. return (void *)(MMU_BLOCK0_VADDR + (src_addr - src_addr_aligned));
  93. }
  94. void bootloader_munmap(const void *mapping)
  95. {
  96. if (mapped) {
  97. /* Full MMU reset */
  98. Cache_Read_Disable(0);
  99. Cache_Flush(0);
  100. mmu_init(0);
  101. mapped = false;
  102. current_read_mapping = UINT32_MAX;
  103. }
  104. }
  105. static esp_err_t spi_to_esp_err(SpiFlashOpResult r)
  106. {
  107. switch(r) {
  108. case SPI_FLASH_RESULT_OK:
  109. return ESP_OK;
  110. case SPI_FLASH_RESULT_ERR:
  111. return ESP_ERR_FLASH_OP_FAIL;
  112. case SPI_FLASH_RESULT_TIMEOUT:
  113. return ESP_ERR_FLASH_OP_TIMEOUT;
  114. default:
  115. return ESP_FAIL;
  116. }
  117. }
  118. static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size)
  119. {
  120. Cache_Read_Disable(0);
  121. Cache_Flush(0);
  122. SpiFlashOpResult r = SPIRead(src_addr, dest, size);
  123. Cache_Read_Enable(0);
  124. return spi_to_esp_err(r);
  125. }
  126. static esp_err_t bootloader_flash_read_allow_decrypt(size_t src_addr, void *dest, size_t size)
  127. {
  128. uint32_t *dest_words = (uint32_t *)dest;
  129. /* Use the 51st MMU mapping to read from flash in 64KB blocks.
  130. (MMU will transparently decrypt if encryption is enabled.)
  131. */
  132. for (int word = 0; word < size / 4; word++) {
  133. uint32_t word_src = src_addr + word * 4; /* Read this offset from flash */
  134. uint32_t map_at = word_src & MMU_FLASH_MASK; /* Map this 64KB block from flash */
  135. uint32_t *map_ptr;
  136. if (map_at != current_read_mapping) {
  137. /* Move the 64KB mmu mapping window to fit map_at */
  138. Cache_Read_Disable(0);
  139. Cache_Flush(0);
  140. ESP_LOGD(TAG, "mmu set block paddr=0x%08x (was 0x%08x)", map_at, current_read_mapping);
  141. int e = cache_flash_mmu_set(0, 0, MMU_BLOCK50_VADDR, map_at, 64, 1);
  142. if (e != 0) {
  143. ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  144. Cache_Read_Enable(0);
  145. return ESP_FAIL;
  146. }
  147. current_read_mapping = map_at;
  148. Cache_Read_Enable(0);
  149. }
  150. map_ptr = (uint32_t *)(MMU_BLOCK50_VADDR + (word_src - map_at));
  151. dest_words[word] = *map_ptr;
  152. }
  153. return ESP_OK;
  154. }
  155. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt)
  156. {
  157. if (src_addr & 3) {
  158. ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
  159. return ESP_FAIL;
  160. }
  161. if (size & 3) {
  162. ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size);
  163. return ESP_FAIL;
  164. }
  165. if ((intptr_t)dest & 3) {
  166. ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
  167. return ESP_FAIL;
  168. }
  169. if (allow_decrypt) {
  170. return bootloader_flash_read_allow_decrypt(src_addr, dest, size);
  171. } else {
  172. return bootloader_flash_read_no_decrypt(src_addr, dest, size);
  173. }
  174. }
  175. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  176. {
  177. esp_err_t err;
  178. size_t alignment = write_encrypted ? 32 : 4;
  179. if ((dest_addr % alignment) != 0) {
  180. ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment);
  181. return ESP_FAIL;
  182. }
  183. if ((size % alignment) != 0) {
  184. ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment);
  185. return ESP_FAIL;
  186. }
  187. if (((intptr_t)src % 4) != 0) {
  188. ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src);
  189. return ESP_FAIL;
  190. }
  191. err = spi_to_esp_err(SPIUnlock());
  192. if (err != ESP_OK) {
  193. return err;
  194. }
  195. if (write_encrypted) {
  196. return spi_to_esp_err(SPI_Encrypt_Write(dest_addr, src, size));
  197. } else {
  198. return spi_to_esp_err(SPIWrite(dest_addr, src, size));
  199. }
  200. }
  201. esp_err_t bootloader_flash_erase_sector(size_t sector)
  202. {
  203. return spi_to_esp_err(SPIEraseSector(sector));
  204. }
  205. #endif