flash_ops_esp32.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2018 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include "esp_spi_flash.h"
  16. #include "esp32/rom/spi_flash.h"
  17. #include "esp32/rom/cache.h"
  18. static inline void IRAM_ATTR spi_flash_guard_start(void)
  19. {
  20. const spi_flash_guard_funcs_t *ops = spi_flash_guard_get();
  21. if (ops && ops->start) {
  22. ops->start();
  23. }
  24. }
  25. static inline void IRAM_ATTR spi_flash_guard_end(void)
  26. {
  27. const spi_flash_guard_funcs_t *ops = spi_flash_guard_get();
  28. if (ops && ops->end) {
  29. ops->end();
  30. }
  31. }
  32. extern void IRAM_ATTR flash_rom_init(void);
  33. esp_rom_spiflash_result_t IRAM_ATTR spi_flash_write_encrypted_chip(size_t dest_addr, const void *src, size_t size)
  34. {
  35. const uint8_t *ssrc = (const uint8_t *)src;
  36. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  37. assert((dest_addr % 16) == 0);
  38. assert((size % 16) == 0);
  39. /* esp_rom_spiflash_write_encrypted encrypts data in RAM as it writes,
  40. so copy to a temporary buffer - 32 bytes at a time.
  41. Each call to esp_rom_spiflash_write_encrypted takes a 32 byte "row" of
  42. data to encrypt, and each row is two 16 byte AES blocks
  43. that share a key (as derived from flash address).
  44. */
  45. uint8_t encrypt_buf[32] __attribute__((aligned(4)));
  46. uint32_t row_size;
  47. for (size_t i = 0; i < size; i += row_size) {
  48. uint32_t row_addr = dest_addr + i;
  49. if (i == 0 && (row_addr % 32) != 0) {
  50. /* writing to second block of a 32 byte row */
  51. row_size = 16;
  52. row_addr -= 16;
  53. /* copy to second block in buffer */
  54. memcpy(encrypt_buf + 16, ssrc + i, 16);
  55. /* decrypt the first block from flash, will reencrypt to same bytes */
  56. spi_flash_read_encrypted(row_addr, encrypt_buf, 16);
  57. } else if (size - i == 16) {
  58. /* 16 bytes left, is first block of a 32 byte row */
  59. row_size = 16;
  60. /* copy to first block in buffer */
  61. memcpy(encrypt_buf, ssrc + i, 16);
  62. /* decrypt the second block from flash, will reencrypt to same bytes */
  63. spi_flash_read_encrypted(row_addr + 16, encrypt_buf + 16, 16);
  64. } else {
  65. /* Writing a full 32 byte row (2 blocks) */
  66. row_size = 32;
  67. memcpy(encrypt_buf, ssrc + i, 32);
  68. }
  69. spi_flash_guard_start();
  70. flash_rom_init();
  71. rc = esp_rom_spiflash_write_encrypted(row_addr, (uint32_t *)encrypt_buf, 32);
  72. spi_flash_guard_end();
  73. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  74. break;
  75. }
  76. }
  77. bzero(encrypt_buf, sizeof(encrypt_buf));
  78. return rc;
  79. }