bootloader_flash.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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"
  20. #define FLASH_SECTOR_SIZE 0x1000
  21. #define FLASH_BLOCK_SIZE 0x10000
  22. /* Provide a Flash API for bootloader_support code,
  23. that can be used from bootloader or app code.
  24. This header is available to source code in the bootloader &
  25. bootloader_support components only.
  26. */
  27. /**
  28. * @brief Map a region of flash to data memory
  29. *
  30. * @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.
  31. *
  32. * @important In app code, these functions are not thread safe.
  33. *
  34. * Call bootloader_munmap once for each successful call to bootloader_mmap.
  35. *
  36. * In esp-idf app, this function maps directly to spi_flash_mmap.
  37. *
  38. * @param offset - Starting flash offset to map to memory.
  39. * @param length - Length of data to map.
  40. *
  41. * @return Pointer to mapped data memory (at src_addr), or NULL
  42. * if an allocation error occured.
  43. */
  44. const void *bootloader_mmap(uint32_t src_addr, uint32_t size);
  45. /**
  46. * @brief Unmap a previously mapped region of flash
  47. *
  48. * Call bootloader_munmap once for each successful call to bootloader_mmap.
  49. */
  50. void bootloader_munmap(const void *mapping);
  51. /**
  52. * @brief Read data from Flash.
  53. *
  54. *
  55. * @note All of src, dest and size have to be 4-byte aligned.
  56. *
  57. * @param src source address of the data in Flash.
  58. * @param dest pointer to the destination buffer
  59. * @param size length of data
  60. * @param allow_decrypt If true and flash encryption is enabled, data on flash
  61. * will be decrypted transparently as part of the read.
  62. *
  63. * @return ESP_OK on success, ESP_ERR_FLASH_OP_FAIL on SPI failure,
  64. * ESP_ERR_FLASH_OP_TIMEOUT on SPI timeout.
  65. */
  66. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt);
  67. /**
  68. * @brief Write data to Flash.
  69. *
  70. * @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.
  71. *
  72. * Note: In bootloader, when write_encrypted == true, the src buffer is encrypted in place.
  73. *
  74. * @param dest_addr Destination address to write in Flash.
  75. * @param src Pointer to the data to write to flash
  76. * @param size Length of data in bytes.
  77. * @param write_encrypted If true, data will be written encrypted on flash.
  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_write(size_t dest_addr, void *src, size_t size, bool write_encrypted);
  83. /**
  84. * @brief Erase the Flash sector.
  85. *
  86. * @param sector Sector number, the count starts at sector 0, 4KB per sector.
  87. *
  88. * @return esp_err_t
  89. */
  90. esp_err_t bootloader_flash_erase_sector(size_t sector);
  91. /**
  92. * @brief Erase the Flash range.
  93. *
  94. * @param start_addr start address of flash offset
  95. * @param size sector aligned size to be erased
  96. *
  97. * @return esp_err_t
  98. */
  99. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size);
  100. #endif