bootloader_flash_priv.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef __BOOTLOADER_FLASH_H
  7. #define __BOOTLOADER_FLASH_H
  8. #include <stddef.h>
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. #include <esp_err.h>
  12. #include <spi_flash_mmap.h> /* including in bootloader for error values */
  13. #include "sdkconfig.h"
  14. #include "bootloader_flash.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #define FLASH_SECTOR_SIZE 0x1000
  19. #define FLASH_BLOCK_SIZE 0x10000
  20. #define MMAP_ALIGNED_MASK (SPI_FLASH_MMU_PAGE_SIZE - 1)
  21. #define MMU_FLASH_MASK (~(SPI_FLASH_MMU_PAGE_SIZE - 1))
  22. /**
  23. * MMU mapping must always be in the unit of a SPI_FLASH_MMU_PAGE_SIZE
  24. * This macro is a helper for you to get needed page nums to be mapped. e.g.:
  25. * Let's say SPI_FLASH_MMU_PAGE_SIZE is 64KB.
  26. * - v_start = 0x4200_0004
  27. * - size = 4 * 64KB
  28. *
  29. * You should map from 0x4200_0000, then map 5 pages.
  30. */
  31. #define GET_REQUIRED_MMU_PAGES(size, v_start) ((size + (v_start - (v_start & MMU_FLASH_MASK)) + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE)
  32. /* SPI commands (actual on-wire commands not SPI controller bitmasks)
  33. Suitable for use with the bootloader_execute_flash_command static function.
  34. */
  35. #define CMD_RDID 0x9F
  36. #define CMD_WRSR 0x01
  37. #define CMD_WRSR2 0x31 /* Not all SPI flash uses this command */
  38. #define CMD_WRSR3 0x11 /* Not all SPI flash uses this command */
  39. #define CMD_WREN 0x06
  40. #define CMD_WRENVSR 0x50 /* Flash write enable for volatile SR bits */
  41. #define CMD_WRDI 0x04
  42. #define CMD_RDSR 0x05
  43. #define CMD_RDSR2 0x35 /* Not all SPI flash uses this command */
  44. #define CMD_RDSR3 0x15 /* Not all SPI flash uses this command */
  45. #define CMD_OTPEN 0x3A /* Enable OTP mode, not all SPI flash uses this command */
  46. #define CMD_RDSFDP 0x5A /* Read the SFDP of the flash */
  47. #define CMD_RESUME 0x7A /* Resume command to clear flash suspend bit */
  48. #define CMD_RESETEN 0x66
  49. #define CMD_RESET 0x99
  50. #define CMD_FASTRD_QIO_4B 0xEC
  51. #define CMD_FASTRD_QUAD_4B 0x6C
  52. #define CMD_FASTRD_DIO_4B 0xBC
  53. #define CMD_FASTRD_DUAL_4B 0x3C
  54. #define CMD_FASTRD_4B 0x0C
  55. #define CMD_SLOWRD_4B 0x13
  56. /* Provide a Flash API for bootloader_support code,
  57. that can be used from bootloader or app code.
  58. This header is available to source code in the bootloader &
  59. bootloader_support components only.
  60. */
  61. /**
  62. * @brief Get number of free pages
  63. *
  64. * @return Number of free pages
  65. */
  66. uint32_t bootloader_mmap_get_free_pages(void);
  67. /**
  68. * @brief Map a region of flash to data memory
  69. *
  70. * @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.
  71. *
  72. * @important In app code, these functions are not thread safe.
  73. *
  74. * Call bootloader_munmap once for each successful call to bootloader_mmap.
  75. *
  76. * In esp-idf app, this function maps directly to spi_flash_mmap.
  77. *
  78. * @param offset - Starting flash offset to map to memory.
  79. * @param length - Length of data to map.
  80. *
  81. * @return Pointer to mapped data memory (at src_addr), or NULL
  82. * if an allocation error occured.
  83. */
  84. const void *bootloader_mmap(uint32_t src_addr, uint32_t size);
  85. /**
  86. * @brief Unmap a previously mapped region of flash
  87. *
  88. * Call bootloader_munmap once for each successful call to bootloader_mmap.
  89. */
  90. void bootloader_munmap(const void *mapping);
  91. /**
  92. * @brief Read data from Flash.
  93. *
  94. *
  95. * @note All of src, dest and size have to be 4-byte aligned.
  96. *
  97. * @param src source address of the data in Flash.
  98. * @param dest pointer to the destination buffer
  99. * @param size length of data
  100. * @param allow_decrypt If true and flash encryption is enabled, data on flash
  101. * will be decrypted transparently as part of the read.
  102. *
  103. * @return ESP_OK on success, ESP_ERR_FLASH_OP_FAIL on SPI failure,
  104. * ESP_ERR_FLASH_OP_TIMEOUT on SPI timeout.
  105. */
  106. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt);
  107. /**
  108. * @brief Write data to Flash.
  109. *
  110. * @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.
  111. *
  112. * Note: In bootloader, when write_encrypted == true, the src buffer is encrypted in place.
  113. *
  114. * @param dest_addr Destination address to write in Flash.
  115. * @param src Pointer to the data to write to flash
  116. * @param size Length of data in bytes.
  117. * @param write_encrypted If true, data will be written encrypted on flash.
  118. *
  119. * @return ESP_OK on success, ESP_ERR_FLASH_OP_FAIL on SPI failure,
  120. * ESP_ERR_FLASH_OP_TIMEOUT on SPI timeout.
  121. */
  122. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted);
  123. /**
  124. * @brief Erase the Flash sector.
  125. *
  126. * @param sector Sector number, the count starts at sector 0, 4KB per sector.
  127. *
  128. * @return esp_err_t
  129. */
  130. esp_err_t bootloader_flash_erase_sector(size_t sector);
  131. /**
  132. * @brief Erase the Flash range.
  133. *
  134. * @param start_addr start address of flash offset
  135. * @param size sector aligned size to be erased
  136. *
  137. * @return esp_err_t
  138. */
  139. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size);
  140. /**
  141. * @brief Execute a user command on the flash
  142. *
  143. * @param command The command value to execute.
  144. * @param mosi_data MOSI data to send
  145. * @param mosi_len Length of MOSI data, in bits
  146. * @param miso_len Length of MISO data to receive, in bits
  147. * @return Received MISO data
  148. */
  149. uint32_t bootloader_execute_flash_command(uint8_t command, uint32_t mosi_data, uint8_t mosi_len, uint8_t miso_len);
  150. /**
  151. * @brief Read the SFDP of the flash
  152. *
  153. * @param sfdp_addr Address of the parameter to read
  154. * @param miso_byte_num Bytes to read
  155. * @return The read SFDP, little endian, 4 bytes at most
  156. */
  157. uint32_t bootloader_flash_read_sfdp(uint32_t sfdp_addr, unsigned int miso_byte_num);
  158. /**
  159. * @brief Enable the flash write protect (WEL bit).
  160. */
  161. void bootloader_enable_wp(void);
  162. /**
  163. * @brief Once this function is called,
  164. * any on-going internal operations will be terminated and the device will return to its default power-on
  165. * state and lose all the current volatile settings, such as Volatile Status Register bits, Write Enable Latch
  166. * (WEL) status, Program/Erase Suspend status, etc.
  167. */
  168. void bootloader_spi_flash_reset(void);
  169. #ifdef __cplusplus
  170. }
  171. #endif
  172. #endif