bootloader_flash_priv.h 5.5 KB

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