bootloader_flash_priv.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef __BOOTLOADER_FLASH_H
  14. #define __BOOTLOADER_FLASH_H
  15. #include <stddef.h>
  16. #include <stdbool.h>
  17. #include <stdint.h>
  18. #include <esp_err.h>
  19. #include <esp_spi_flash.h> /* including in bootloader for error values */
  20. #include "sdkconfig.h"
  21. #include "bootloader_flash.h"
  22. #define FLASH_SECTOR_SIZE 0x1000
  23. #define FLASH_BLOCK_SIZE 0x10000
  24. #define MMAP_ALIGNED_MASK 0x0000FFFF
  25. /* SPI commands (actual on-wire commands not SPI controller bitmasks)
  26. Suitable for use with the bootloader_execute_flash_command static function.
  27. */
  28. #define CMD_RDID 0x9F
  29. #define CMD_WRSR 0x01
  30. #define CMD_WRSR2 0x31 /* Not all SPI flash uses this command */
  31. #define CMD_WREN 0x06
  32. #define CMD_WRDI 0x04
  33. #define CMD_RDSR 0x05
  34. #define CMD_RDSR2 0x35 /* Not all SPI flash uses this command */
  35. #define CMD_OTPEN 0x3A /* Enable OTP mode, not all SPI flash uses this command */
  36. #define CMD_RDSFDP 0x5A /* Read the SFDP of the flash */
  37. #define CMD_WRAP 0x77 /* Set burst with wrap command */
  38. #define CMD_RESUME 0x7A /* Resume command to clear flash suspend bit */
  39. /* Provide a Flash API for bootloader_support code,
  40. that can be used from bootloader or app code.
  41. This header is available to source code in the bootloader &
  42. bootloader_support components only.
  43. */
  44. /**
  45. * @brief Get number of free pages
  46. *
  47. * @return Number of free pages
  48. */
  49. uint32_t bootloader_mmap_get_free_pages(void);
  50. /**
  51. * @brief Map a region of flash to data memory
  52. *
  53. * @important In bootloader code, only one region can be bootloader_mmaped at once. The previous region must be bootloader_munmapped before another region is mapped.
  54. *
  55. * @important In app code, these functions are not thread safe.
  56. *
  57. * Call bootloader_munmap once for each successful call to bootloader_mmap.
  58. *
  59. * In esp-idf app, this function maps directly to spi_flash_mmap.
  60. *
  61. * @param offset - Starting flash offset to map to memory.
  62. * @param length - Length of data to map.
  63. *
  64. * @return Pointer to mapped data memory (at src_addr), or NULL
  65. * if an allocation error occured.
  66. */
  67. const void *bootloader_mmap(uint32_t src_addr, uint32_t size);
  68. /**
  69. * @brief Unmap a previously mapped region of flash
  70. *
  71. * Call bootloader_munmap once for each successful call to bootloader_mmap.
  72. */
  73. void bootloader_munmap(const void *mapping);
  74. /**
  75. * @brief Read data from Flash.
  76. *
  77. *
  78. * @note All of src, dest and size have to be 4-byte aligned.
  79. *
  80. * @param src source address of the data in Flash.
  81. * @param dest pointer to the destination buffer
  82. * @param size length of data
  83. * @param allow_decrypt If true and flash encryption is enabled, data on flash
  84. * will be decrypted transparently as part of the read.
  85. *
  86. * @return ESP_OK on success, ESP_ERR_FLASH_OP_FAIL on SPI failure,
  87. * ESP_ERR_FLASH_OP_TIMEOUT on SPI timeout.
  88. */
  89. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt);
  90. /**
  91. * @brief Write data to Flash.
  92. *
  93. * @note All of dest_addr, src and size have to be 4-byte aligned. If write_encrypted is set, dest_addr and size must be 32-byte aligned.
  94. *
  95. * Note: In bootloader, when write_encrypted == true, the src buffer is encrypted in place.
  96. *
  97. * @param dest_addr Destination address to write in Flash.
  98. * @param src Pointer to the data to write to flash
  99. * @param size Length of data in bytes.
  100. * @param write_encrypted If true, data will be written encrypted on flash.
  101. *
  102. * @return ESP_OK on success, ESP_ERR_FLASH_OP_FAIL on SPI failure,
  103. * ESP_ERR_FLASH_OP_TIMEOUT on SPI timeout.
  104. */
  105. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted);
  106. /**
  107. * @brief Erase the Flash sector.
  108. *
  109. * @param sector Sector number, the count starts at sector 0, 4KB per sector.
  110. *
  111. * @return esp_err_t
  112. */
  113. esp_err_t bootloader_flash_erase_sector(size_t sector);
  114. /**
  115. * @brief Erase the Flash range.
  116. *
  117. * @param start_addr start address of flash offset
  118. * @param size sector aligned size to be erased
  119. *
  120. * @return esp_err_t
  121. */
  122. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size);
  123. /* Cache MMU block size */
  124. #define MMU_BLOCK_SIZE 0x00010000
  125. /* Cache MMU address mask (MMU tables ignore bits which are zero) */
  126. #define MMU_FLASH_MASK (~(MMU_BLOCK_SIZE - 1))
  127. /**
  128. * @brief Calculate the number of cache pages to map
  129. * @param size size of data to map
  130. * @param vaddr virtual address where data will be mapped
  131. * @return number of cache MMU pages required to do the mapping
  132. */
  133. static inline uint32_t bootloader_cache_pages_to_map(uint32_t size, uint32_t vaddr)
  134. {
  135. return (size + (vaddr - (vaddr & MMU_FLASH_MASK)) + MMU_BLOCK_SIZE - 1) / MMU_BLOCK_SIZE;
  136. }
  137. /**
  138. * @brief Execute a user command on the flash
  139. *
  140. * @param command The command value to execute.
  141. * @param mosi_data MOSI data to send
  142. * @param mosi_len Length of MOSI data, in bits
  143. * @param miso_len Length of MISO data to receive, in bits
  144. * @return Received MISO data
  145. */
  146. uint32_t bootloader_execute_flash_command(uint8_t command, uint32_t mosi_data, uint8_t mosi_len, uint8_t miso_len);
  147. /**
  148. * @brief Read the SFDP of the flash
  149. *
  150. * @param sfdp_addr Address of the parameter to read
  151. * @param miso_byte_num Bytes to read
  152. * @return The read SFDP, little endian, 4 bytes at most
  153. */
  154. uint32_t bootloader_flash_read_sfdp(uint32_t sfdp_addr, unsigned int miso_byte_num);
  155. /**
  156. * @brief Enable the flash write protect (WEL bit).
  157. */
  158. void bootloader_enable_wp(void);
  159. #endif