bootloader_flash_priv.h 5.7 KB

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