partition_target.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <sys/lock.h>
  11. #include "sdkconfig.h"
  12. #include "esp_flash_partitions.h"
  13. #include "esp_attr.h"
  14. #include "esp_flash.h"
  15. #include "esp_partition.h"
  16. #include "esp_flash_encrypt.h"
  17. #include "esp_log.h"
  18. #include "esp_rom_md5.h"
  19. #include "spi_flash_mmap.h"
  20. #include "bootloader_common.h"
  21. #include "esp_ota_ops.h"
  22. #define HASH_LEN 32 /* SHA-256 digest length */
  23. esp_err_t esp_partition_read(const esp_partition_t *partition,
  24. size_t src_offset, void *dst, size_t size)
  25. {
  26. assert(partition != NULL);
  27. if (src_offset > partition->size) {
  28. return ESP_ERR_INVALID_ARG;
  29. }
  30. if (src_offset + size > partition->size) {
  31. return ESP_ERR_INVALID_SIZE;
  32. }
  33. if (!partition->encrypted) {
  34. return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
  35. }
  36. #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  37. if (partition->flash_chip != esp_flash_default_chip) {
  38. return ESP_ERR_NOT_SUPPORTED;
  39. }
  40. /* Encrypted partitions need to be read via a cache mapping */
  41. const void *buf;
  42. esp_partition_mmap_handle_t handle;
  43. esp_err_t err = esp_partition_mmap(partition, src_offset, size,
  44. SPI_FLASH_MMAP_DATA, &buf, &handle);
  45. if (err != ESP_OK) {
  46. return err;
  47. }
  48. memcpy(dst, buf, size);
  49. esp_partition_munmap(handle);
  50. return ESP_OK;
  51. #else
  52. return ESP_ERR_NOT_SUPPORTED;
  53. #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  54. }
  55. esp_err_t esp_partition_write(const esp_partition_t *partition,
  56. size_t dst_offset, const void *src, size_t size)
  57. {
  58. assert(partition != NULL);
  59. if (partition->readonly) {
  60. return ESP_ERR_NOT_ALLOWED;
  61. }
  62. if (dst_offset > partition->size) {
  63. return ESP_ERR_INVALID_ARG;
  64. }
  65. if (dst_offset + size > partition->size) {
  66. return ESP_ERR_INVALID_SIZE;
  67. }
  68. dst_offset = partition->address + dst_offset;
  69. if (!partition->encrypted) {
  70. return esp_flash_write(partition->flash_chip, src, dst_offset, size);
  71. }
  72. #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  73. if (partition->flash_chip != esp_flash_default_chip) {
  74. return ESP_ERR_NOT_SUPPORTED;
  75. }
  76. return esp_flash_write_encrypted(partition->flash_chip, dst_offset, src, size);
  77. #else
  78. return ESP_ERR_NOT_SUPPORTED;
  79. #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  80. }
  81. esp_err_t esp_partition_read_raw(const esp_partition_t *partition,
  82. size_t src_offset, void *dst, size_t size)
  83. {
  84. assert(partition != NULL);
  85. if (src_offset > partition->size) {
  86. return ESP_ERR_INVALID_ARG;
  87. }
  88. if (src_offset + size > partition->size) {
  89. return ESP_ERR_INVALID_SIZE;
  90. }
  91. return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
  92. }
  93. esp_err_t esp_partition_write_raw(const esp_partition_t *partition,
  94. size_t dst_offset, const void *src, size_t size)
  95. {
  96. assert(partition != NULL);
  97. if (partition->readonly) {
  98. return ESP_ERR_NOT_ALLOWED;
  99. }
  100. if (dst_offset > partition->size) {
  101. return ESP_ERR_INVALID_ARG;
  102. }
  103. if (dst_offset + size > partition->size) {
  104. return ESP_ERR_INVALID_SIZE;
  105. }
  106. dst_offset = partition->address + dst_offset;
  107. return esp_flash_write(partition->flash_chip, src, dst_offset, size);
  108. }
  109. esp_err_t esp_partition_erase_range(const esp_partition_t *partition,
  110. size_t offset, size_t size)
  111. {
  112. assert(partition != NULL);
  113. if (partition->readonly) {
  114. return ESP_ERR_NOT_ALLOWED;
  115. }
  116. if (offset > partition->size) {
  117. return ESP_ERR_INVALID_ARG;
  118. }
  119. if (offset + size > partition->size) {
  120. return ESP_ERR_INVALID_SIZE;
  121. }
  122. if (size % SPI_FLASH_SEC_SIZE != 0) {
  123. return ESP_ERR_INVALID_SIZE;
  124. }
  125. if (offset % SPI_FLASH_SEC_SIZE != 0) {
  126. return ESP_ERR_INVALID_ARG;
  127. }
  128. return esp_flash_erase_region(partition->flash_chip, partition->address + offset, size);
  129. }
  130. /*
  131. * Note: current implementation ignores the possibility of multiple regions in the same partition being
  132. * mapped. Reference counting and address space re-use is delegated to spi_flash_mmap.
  133. *
  134. * If this becomes a performance issue (i.e. if we need to map multiple regions within the partition),
  135. * we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of
  136. * mmaped pointers, and a single handle for all these regions.
  137. */
  138. esp_err_t esp_partition_mmap(const esp_partition_t *partition, size_t offset, size_t size,
  139. esp_partition_mmap_memory_t memory,
  140. const void **out_ptr, esp_partition_mmap_handle_t *out_handle)
  141. {
  142. assert(partition != NULL);
  143. if (offset > partition->size) {
  144. return ESP_ERR_INVALID_ARG;
  145. }
  146. if (offset + size > partition->size) {
  147. return ESP_ERR_INVALID_SIZE;
  148. }
  149. if (partition->flash_chip != esp_flash_default_chip) {
  150. return ESP_ERR_NOT_SUPPORTED;
  151. }
  152. size_t phys_addr = partition->address + offset;
  153. // offset within mmu page size block
  154. size_t region_offset = phys_addr & (CONFIG_MMU_PAGE_SIZE - 1);
  155. size_t mmap_addr = phys_addr & ~(CONFIG_MMU_PAGE_SIZE - 1);
  156. esp_err_t rc = spi_flash_mmap(mmap_addr, size + region_offset, (spi_flash_mmap_memory_t) memory, out_ptr, (spi_flash_mmap_handle_t*) out_handle);
  157. // adjust returned pointer to point to the correct offset
  158. if (rc == ESP_OK) {
  159. *out_ptr = (void *) (((ptrdiff_t) * out_ptr) + region_offset);
  160. }
  161. return rc;
  162. }
  163. void esp_partition_munmap(esp_partition_mmap_handle_t handle)
  164. {
  165. spi_flash_munmap((spi_flash_mmap_handle_t) handle);
  166. }
  167. esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sha_256)
  168. {
  169. return bootloader_common_get_sha256_of_partition(partition->address, partition->size, partition->type, sha_256);
  170. }
  171. bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2)
  172. {
  173. uint8_t sha_256[2][HASH_LEN] = { 0 };
  174. if (esp_partition_get_sha256(partition_1, sha_256[0]) == ESP_OK &&
  175. esp_partition_get_sha256(partition_2, sha_256[1]) == ESP_OK) {
  176. if (memcmp(sha_256[0], sha_256[1], HASH_LEN) == 0) {
  177. // The partitions are identity
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. bool esp_partition_is_flash_region_writable(size_t addr, size_t size)
  184. {
  185. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
  186. for (; it != NULL; it = esp_partition_next(it)) {
  187. const esp_partition_t *p = esp_partition_get(it);
  188. if (p->readonly) {
  189. if (addr >= p->address && addr < p->address + p->size) {
  190. return false;
  191. }
  192. if (addr < p->address && addr + size > p->address) {
  193. return false;
  194. }
  195. }
  196. }
  197. return true;
  198. }
  199. bool esp_partition_main_flash_region_safe(size_t addr, size_t size)
  200. {
  201. if (addr <= ESP_PARTITION_TABLE_OFFSET + ESP_PARTITION_TABLE_MAX_LEN) {
  202. return false;
  203. }
  204. const esp_partition_t *p = esp_ota_get_running_partition();
  205. if (addr >= p->address && addr < p->address + p->size) {
  206. return false;
  207. }
  208. if (addr < p->address && addr + size > p->address) {
  209. return false;
  210. }
  211. return true;
  212. }