flash_encrypt.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include <string.h>
  14. #include "esp_types.h"
  15. #include "esp_attr.h"
  16. #include "esp_log.h"
  17. #include "esp_err.h"
  18. #include "rom/cache.h"
  19. #include "rom/ets_sys.h"
  20. #include "rom/spi_flash.h"
  21. #include "soc/dport_reg.h"
  22. #include "soc/io_mux_reg.h"
  23. #include "soc/efuse_reg.h"
  24. #include "soc/rtc_cntl_reg.h"
  25. #include "sdkconfig.h"
  26. #include "bootloader_config.h"
  27. #include "esp_image_format.h"
  28. static const char* TAG = "flash_encrypt";
  29. /**
  30. * @function : bitcount
  31. * @description: calculate bit 1 in flash_crypt_cnt
  32. * if it's even number, need encrypt flash data, and burn efuse
  33. *
  34. * @inputs: n flash_crypt_cnt
  35. * @return: number of 1 in flash_crypt_cnt
  36. *
  37. */
  38. int bitcount(int n){
  39. int count = 0;
  40. while (n > 0) {
  41. count += n & 1;
  42. n >>= 1;
  43. }
  44. return count;
  45. }
  46. /**
  47. * @function : flash_encrypt_write
  48. * @description: write encrypted data in flash
  49. *
  50. * @inputs: pos address in flash
  51. * len size of data need encrypt
  52. * @return: return true, if the write flash success
  53. *
  54. */
  55. bool flash_encrypt_write(uint32_t pos, uint32_t len)
  56. {
  57. SpiFlashOpResult spiRet;
  58. uint32_t buf[1024];
  59. int i = 0;
  60. Cache_Read_Disable(0);
  61. for (i = 0;i<((len-1)/0x1000 + 1);i++) {
  62. spiRet = SPIRead(pos, buf, SPI_SEC_SIZE);
  63. if (spiRet != SPI_FLASH_RESULT_OK) {
  64. Cache_Read_Enable(0);
  65. ESP_LOGE(TAG, SPI_ERROR_LOG);
  66. return false;
  67. }
  68. spiRet = SPIEraseSector(pos/SPI_SEC_SIZE);
  69. if (spiRet != SPI_FLASH_RESULT_OK) {
  70. Cache_Read_Enable(0);
  71. ESP_LOGE(TAG, SPI_ERROR_LOG);
  72. return false;
  73. }
  74. spiRet = SPI_Encrypt_Write(pos, buf, SPI_SEC_SIZE);
  75. if (spiRet != SPI_FLASH_RESULT_OK) {
  76. Cache_Read_Enable(0);
  77. ESP_LOGE(TAG, SPI_ERROR_LOG);
  78. return false;
  79. }
  80. pos += SPI_SEC_SIZE;
  81. }
  82. Cache_Read_Enable(0);
  83. return true;
  84. }
  85. /**
  86. * @function : flash_encrypt
  87. * @description: encrypt 2nd boot ,partition table ,factory bin ��test bin (if use)��ota bin
  88. * ��OTA info sector.
  89. *
  90. * @inputs: bs bootloader state structure used to save the data
  91. *
  92. * @return: return true, if the encrypt flash success
  93. *
  94. */
  95. bool flash_encrypt(bootloader_state_t *bs)
  96. {
  97. esp_err_t err;
  98. uint32_t image_len = 0;
  99. uint32_t flash_crypt_cnt = REG_GET_FIELD(EFUSE_BLK0_RDATA0_REG, EFUSE_FLASH_CRYPT_CNT);
  100. uint8_t count = bitcount(flash_crypt_cnt);
  101. ESP_LOGD(TAG, "flash encrypt cnt %x, bitcount %d", flash_crypt_cnt, count);
  102. if ((count % 2) == 0) {
  103. /* encrypt iv and abstract */
  104. if (false == flash_encrypt_write(0, SPI_SEC_SIZE)) {
  105. ESP_LOGE(TAG, "encrypt iv and abstract error");
  106. return false;
  107. }
  108. /* encrypt bootloader image */
  109. err = esp_image_basic_verify(0x1000, &image_len);
  110. if(err == ESP_OK && image_len != 0) {
  111. if (false == flash_encrypt_write(0x1000, image_len)) {
  112. ESP_LOGE(TAG, "encrypt 2nd boot error");
  113. return false;
  114. }
  115. } else {
  116. ESP_LOGE(TAG, "2nd boot len error");
  117. return false;
  118. }
  119. /* encrypt partition table */
  120. if (false == flash_encrypt_write(ESP_PARTITION_TABLE_ADDR, SPI_SEC_SIZE)) {
  121. ESP_LOGE(TAG, "encrypt partition table error");
  122. return false;
  123. }
  124. /* encrypt write factory bin */
  125. if(bs->factory.offset != 0 && bs->factory.size != 0) {
  126. ESP_LOGD(TAG, "have factory bin");
  127. if (false == flash_encrypt_write(bs->factory.offset, bs->factory.size)) {
  128. ESP_LOGE(TAG, "encrypt factory bin error");
  129. return false;
  130. }
  131. }
  132. /* encrypt write test bin */
  133. if(bs->test.offset != 0 && bs->test.size != 0) {
  134. ESP_LOGD(TAG, "have test bin");
  135. if (false == flash_encrypt_write(bs->test.offset, bs->test.size)) {
  136. ESP_LOGE(TAG, "encrypt test bin error");
  137. return false;
  138. }
  139. }
  140. /* encrypt write ota bin */
  141. for (int i = 0; i < 16; i++) {
  142. if(bs->ota[i].offset != 0 && bs->ota[i].size != 0) {
  143. ESP_LOGD(TAG, "have ota[%d] bin",i);
  144. if (false == flash_encrypt_write(bs->ota[i].offset, bs->ota[i].size)) {
  145. ESP_LOGE(TAG, "encrypt ota bin error");
  146. return false;
  147. }
  148. }
  149. }
  150. /* encrypt write ota info bin */
  151. if (false == flash_encrypt_write(bs->ota_info.offset, 2*SPI_SEC_SIZE)) {
  152. ESP_LOGE(TAG, "encrypt ota info error");
  153. return false;
  154. }
  155. REG_SET_FIELD(EFUSE_BLK0_WDATA0_REG, EFUSE_FLASH_CRYPT_CNT, 0x04);
  156. REG_WRITE(EFUSE_CONF_REG, 0x5A5A); /* efuse_pgm_op_ena, force no rd/wr disable */
  157. REG_WRITE(EFUSE_CMD_REG, 0x02); /* efuse_pgm_cmd */
  158. while (REG_READ(EFUSE_CMD_REG)); /* wait for efuse_pagm_cmd=0 */
  159. ESP_LOGW(TAG, "burn flash_crypt_cnt");
  160. REG_WRITE(EFUSE_CONF_REG, 0x5AA5); /* efuse_read_op_ena, release force */
  161. REG_WRITE(EFUSE_CMD_REG, 0x01); /* efuse_read_cmd */
  162. while (REG_READ(EFUSE_CMD_REG)); /* wait for efuse_read_cmd=0 */
  163. return true;
  164. } else {
  165. ESP_LOGI(TAG, "flash already encrypted.");
  166. return true;
  167. }
  168. }