partition_target.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 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. spi_flash_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. spi_flash_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 (dst_offset > partition->size) {
  60. return ESP_ERR_INVALID_ARG;
  61. }
  62. if (dst_offset + size > partition->size) {
  63. return ESP_ERR_INVALID_SIZE;
  64. }
  65. dst_offset = partition->address + dst_offset;
  66. if (!partition->encrypted) {
  67. return esp_flash_write(partition->flash_chip, src, dst_offset, size);
  68. }
  69. #if CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  70. if (partition->flash_chip != esp_flash_default_chip) {
  71. return ESP_ERR_NOT_SUPPORTED;
  72. }
  73. return esp_flash_write_encrypted(partition->flash_chip, dst_offset, src, size);
  74. #else
  75. return ESP_ERR_NOT_SUPPORTED;
  76. #endif // CONFIG_SPI_FLASH_ENABLE_ENCRYPTED_READ_WRITE
  77. }
  78. esp_err_t esp_partition_read_raw(const esp_partition_t *partition,
  79. size_t src_offset, void *dst, size_t size)
  80. {
  81. assert(partition != NULL);
  82. if (src_offset > partition->size) {
  83. return ESP_ERR_INVALID_ARG;
  84. }
  85. if (src_offset + size > partition->size) {
  86. return ESP_ERR_INVALID_SIZE;
  87. }
  88. return esp_flash_read(partition->flash_chip, dst, partition->address + src_offset, size);
  89. }
  90. esp_err_t esp_partition_write_raw(const esp_partition_t *partition,
  91. size_t dst_offset, const void *src, size_t size)
  92. {
  93. assert(partition != NULL);
  94. if (dst_offset > partition->size) {
  95. return ESP_ERR_INVALID_ARG;
  96. }
  97. if (dst_offset + size > partition->size) {
  98. return ESP_ERR_INVALID_SIZE;
  99. }
  100. dst_offset = partition->address + dst_offset;
  101. return esp_flash_write(partition->flash_chip, src, dst_offset, size);
  102. }
  103. esp_err_t esp_partition_erase_range(const esp_partition_t *partition,
  104. size_t offset, size_t size)
  105. {
  106. assert(partition != NULL);
  107. if (offset > partition->size) {
  108. return ESP_ERR_INVALID_ARG;
  109. }
  110. if (offset + size > partition->size) {
  111. return ESP_ERR_INVALID_SIZE;
  112. }
  113. if (size % SPI_FLASH_SEC_SIZE != 0) {
  114. return ESP_ERR_INVALID_SIZE;
  115. }
  116. if (offset % SPI_FLASH_SEC_SIZE != 0) {
  117. return ESP_ERR_INVALID_ARG;
  118. }
  119. return esp_flash_erase_region(partition->flash_chip, partition->address + offset, size);
  120. }
  121. /*
  122. * Note: current implementation ignores the possibility of multiple regions in the same partition being
  123. * mapped. Reference counting and address space re-use is delegated to spi_flash_mmap.
  124. *
  125. * If this becomes a performance issue (i.e. if we need to map multiple regions within the partition),
  126. * we can add esp_partition_mmapv which will accept an array of offsets and sizes, and return array of
  127. * mmaped pointers, and a single handle for all these regions.
  128. */
  129. esp_err_t esp_partition_mmap(const esp_partition_t *partition, size_t offset, size_t size,
  130. spi_flash_mmap_memory_t memory,
  131. const void **out_ptr, spi_flash_mmap_handle_t *out_handle)
  132. {
  133. assert(partition != NULL);
  134. if (offset > partition->size) {
  135. return ESP_ERR_INVALID_ARG;
  136. }
  137. if (offset + size > partition->size) {
  138. return ESP_ERR_INVALID_SIZE;
  139. }
  140. if (partition->flash_chip != esp_flash_default_chip) {
  141. return ESP_ERR_NOT_SUPPORTED;
  142. }
  143. size_t phys_addr = partition->address + offset;
  144. // offset within mmu page size block
  145. size_t region_offset = phys_addr & (CONFIG_MMU_PAGE_SIZE - 1);
  146. size_t mmap_addr = phys_addr & ~(CONFIG_MMU_PAGE_SIZE - 1);
  147. esp_err_t rc = spi_flash_mmap(mmap_addr, size + region_offset, memory, out_ptr, out_handle);
  148. // adjust returned pointer to point to the correct offset
  149. if (rc == ESP_OK) {
  150. *out_ptr = (void *) (((ptrdiff_t) * out_ptr) + region_offset);
  151. }
  152. return rc;
  153. }
  154. esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sha_256)
  155. {
  156. return bootloader_common_get_sha256_of_partition(partition->address, partition->size, partition->type, sha_256);
  157. }
  158. bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2)
  159. {
  160. uint8_t sha_256[2][HASH_LEN] = { 0 };
  161. if (esp_partition_get_sha256(partition_1, sha_256[0]) == ESP_OK &&
  162. esp_partition_get_sha256(partition_2, sha_256[1]) == ESP_OK) {
  163. if (memcmp(sha_256[0], sha_256[1], HASH_LEN) == 0) {
  164. // The partitions are identity
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. bool esp_partition_main_flash_region_safe(size_t addr, size_t size)
  171. {
  172. bool result = true;
  173. if (addr <= ESP_PARTITION_TABLE_OFFSET + ESP_PARTITION_TABLE_MAX_LEN) {
  174. return false;
  175. }
  176. const esp_partition_t *p = esp_ota_get_running_partition();
  177. if (addr >= p->address && addr < p->address + p->size) {
  178. return false;
  179. }
  180. if (addr < p->address && addr + size > p->address) {
  181. return false;
  182. }
  183. return result;
  184. }