bootloader_flash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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)
  43. {
  44. return spi_flash_read(src, dest, size);
  45. }
  46. #else
  47. /* Bootloader version, uses ROM functions only */
  48. #include <rom/spi_flash.h>
  49. #include <rom/cache.h>
  50. static const char *TAG = "bootloader_flash";
  51. static bool mapped;
  52. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  53. {
  54. if (mapped) {
  55. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  56. return NULL; /* can't map twice */
  57. }
  58. uint32_t src_addr_aligned = src_addr & 0xffff0000;
  59. uint32_t count = (size + (src_addr - src_addr_aligned) + 0xffff) / 0x10000;
  60. Cache_Read_Disable(0);
  61. Cache_Flush(0);
  62. ESP_LOGD(TAG, "mmu set paddr=%08x count=%d", src_addr_aligned, count );
  63. cache_flash_mmu_set( 0, 0, 0x3f400000, src_addr_aligned, 64, count );
  64. Cache_Read_Enable( 0 );
  65. mapped = true;
  66. return (void *)(0x3f400000 + (src_addr - src_addr_aligned));
  67. }
  68. void bootloader_munmap(const void *mapping)
  69. {
  70. if (mapped) {
  71. /* Full MMU reset */
  72. Cache_Read_Disable(0);
  73. Cache_Flush(0);
  74. mmu_init(0);
  75. mapped = false;
  76. }
  77. }
  78. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size)
  79. {
  80. if(src_addr & 3) {
  81. ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
  82. return ESP_FAIL;
  83. }
  84. if((intptr_t)dest & 3) {
  85. ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
  86. return ESP_FAIL;
  87. }
  88. Cache_Read_Disable(0);
  89. Cache_Flush(0);
  90. SpiFlashOpResult r = SPIRead(src_addr, dest, size);
  91. Cache_Read_Enable(0);
  92. switch(r) {
  93. case SPI_FLASH_RESULT_OK:
  94. return ESP_OK;
  95. case SPI_FLASH_RESULT_ERR:
  96. return ESP_ERR_FLASH_OP_FAIL;
  97. case SPI_FLASH_RESULT_TIMEOUT:
  98. return ESP_ERR_FLASH_OP_TIMEOUT;
  99. default:
  100. return ESP_FAIL;
  101. }
  102. }
  103. #endif