secure_boot.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_attr.h"
  15. #include "esp_types.h"
  16. #include "esp_log.h"
  17. #include "rom/cache.h"
  18. #include "rom/ets_sys.h"
  19. #include "rom/spi_flash.h"
  20. #include "rom/secure_boot.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_flash.h"
  27. #include "esp_image_format.h"
  28. #include "esp_secure_boot.h"
  29. static const char* TAG = "secure_boot";
  30. #define HASH_BLOCK_SIZE 128
  31. #define IV_LEN HASH_BLOCK_SIZE
  32. #define DIGEST_LEN 64
  33. /**
  34. * @function : secure_boot_generate
  35. * @description: generate boot digest (aka "abstract") & iv
  36. *
  37. * @inputs: image_len - length of image to calculate digest for
  38. */
  39. static bool secure_boot_generate(uint32_t image_len){
  40. SpiFlashOpResult spiRet;
  41. /* buffer is uint32_t not uint8_t to meet ROM SPI API signature */
  42. uint32_t buf[IV_LEN / sizeof(uint32_t)];
  43. const void *image;
  44. /* hardware secure boot engine only takes full blocks, so round up the
  45. image length. The additional data should all be 0xFF.
  46. */
  47. if (image_len % HASH_BLOCK_SIZE != 0) {
  48. image_len = (image_len / HASH_BLOCK_SIZE + 1) * HASH_BLOCK_SIZE;
  49. }
  50. ets_secure_boot_start();
  51. ets_secure_boot_rd_iv(buf);
  52. ets_secure_boot_hash(NULL);
  53. Cache_Read_Disable(0);
  54. /* iv stored in sec 0 */
  55. spiRet = SPIEraseSector(0);
  56. if (spiRet != SPI_FLASH_RESULT_OK)
  57. {
  58. ESP_LOGE(TAG, "SPI erase failed %d", spiRet);
  59. return false;
  60. }
  61. Cache_Read_Enable(0);
  62. /* write iv to flash, 0x0000, 128 bytes (1024 bits) */
  63. ESP_LOGD(TAG, "write iv to flash.");
  64. spiRet = SPIWrite(0, buf, IV_LEN);
  65. if (spiRet != SPI_FLASH_RESULT_OK)
  66. {
  67. ESP_LOGE(TAG, "SPI write failed %d", spiRet);
  68. return false;
  69. }
  70. bzero(buf, sizeof(buf));
  71. /* generate digest from image contents */
  72. image = bootloader_mmap(0x1000, image_len);
  73. if (!image) {
  74. ESP_LOGE(TAG, "bootloader_mmap(0x1000, 0x%x) failed", image_len);
  75. return false;
  76. }
  77. for (int i = 0; i < image_len; i+= HASH_BLOCK_SIZE) {
  78. ets_secure_boot_hash(image + i/sizeof(void *));
  79. }
  80. bootloader_munmap(image);
  81. ets_secure_boot_obtain();
  82. ets_secure_boot_rd_abstract(buf);
  83. ets_secure_boot_finish();
  84. ESP_LOGD(TAG, "write digest to flash.");
  85. spiRet = SPIWrite(0x80, buf, DIGEST_LEN);
  86. if (spiRet != SPI_FLASH_RESULT_OK) {
  87. ESP_LOGE(TAG, "SPI write failed %d", spiRet);
  88. return false;
  89. }
  90. ESP_LOGD(TAG, "write digest to flash.");
  91. Cache_Read_Enable(0);
  92. return true;
  93. }
  94. /* Burn values written to the efuse write registers */
  95. static inline void burn_efuses()
  96. {
  97. #ifdef CONFIG_SECURE_BOOT_TEST_MODE
  98. ESP_LOGE(TAG, "SECURE BOOT TEST MODE. Not really burning any efuses!");
  99. #else
  100. REG_WRITE(EFUSE_CONF_REG, 0x5A5A); /* efuse_pgm_op_ena, force no rd/wr disable */
  101. REG_WRITE(EFUSE_CMD_REG, 0x02); /* efuse_pgm_cmd */
  102. while (REG_READ(EFUSE_CMD_REG)); /* wait for efuse_pagm_cmd=0 */
  103. REG_WRITE(EFUSE_CONF_REG, 0x5AA5); /* efuse_read_op_ena, release force */
  104. REG_WRITE(EFUSE_CMD_REG, 0x01); /* efuse_read_cmd */
  105. while (REG_READ(EFUSE_CMD_REG)); /* wait for efuse_read_cmd=0 */
  106. #endif
  107. }
  108. esp_err_t esp_secure_boot_permanently_enable(void) {
  109. esp_err_t err;
  110. uint32_t image_len = 0;
  111. if (esp_secure_boot_enabled())
  112. {
  113. ESP_LOGI(TAG, "bootloader secure boot is already enabled, continuing..");
  114. return ESP_OK;
  115. }
  116. err = esp_image_basic_verify(0x1000, &image_len);
  117. if (err != ESP_OK) {
  118. ESP_LOGE(TAG, "bootloader image appears invalid! error %d", err);
  119. return err;
  120. }
  121. uint32_t dis_reg = REG_READ(EFUSE_BLK0_RDATA0_REG);
  122. bool efuse_key_read_protected = dis_reg & EFUSE_RD_DIS_BLK2;
  123. bool efuse_key_write_protected = dis_reg & EFUSE_WR_DIS_BLK2;
  124. if (efuse_key_read_protected == false
  125. && efuse_key_write_protected == false
  126. && REG_READ(EFUSE_BLK2_RDATA0_REG) == 0
  127. && REG_READ(EFUSE_BLK2_RDATA1_REG) == 0
  128. && REG_READ(EFUSE_BLK2_RDATA2_REG) == 0
  129. && REG_READ(EFUSE_BLK2_RDATA3_REG) == 0
  130. && REG_READ(EFUSE_BLK2_RDATA4_REG) == 0
  131. && REG_READ(EFUSE_BLK2_RDATA5_REG) == 0
  132. && REG_READ(EFUSE_BLK2_RDATA6_REG) == 0
  133. && REG_READ(EFUSE_BLK2_RDATA7_REG) == 0) {
  134. ESP_LOGI(TAG, "Generating new secure boot key...");
  135. /* reuse the secure boot IV generation function to generate
  136. the key, as this generator uses the hardware RNG. */
  137. uint32_t buf[32];
  138. ets_secure_boot_start();
  139. ets_secure_boot_rd_iv(buf);
  140. ets_secure_boot_finish();
  141. for (int i = 0; i < 8; i++) {
  142. ESP_LOGV(TAG, "EFUSE_BLK2_WDATA%d_REG = 0x%08x", i, buf[i]);
  143. REG_WRITE(EFUSE_BLK2_WDATA0_REG + 4*i, buf[i]);
  144. }
  145. bzero(buf, sizeof(buf));
  146. burn_efuses();
  147. ESP_LOGI(TAG, "Read & write protecting new key...");
  148. REG_WRITE(EFUSE_BLK0_WDATA0_REG, EFUSE_WR_DIS_BLK2 | EFUSE_RD_DIS_BLK2);
  149. burn_efuses();
  150. efuse_key_read_protected = true;
  151. efuse_key_write_protected = true;
  152. } else {
  153. ESP_LOGW(TAG, "Using pre-loaded secure boot key in EFUSE block 2");
  154. }
  155. ESP_LOGI(TAG, "Generating secure boot digest...");
  156. if (false == secure_boot_generate(image_len)){
  157. ESP_LOGE(TAG, "secure boot generation failed");
  158. return ESP_FAIL;
  159. }
  160. ESP_LOGI(TAG, "Digest generation complete.");
  161. if (!efuse_key_read_protected) {
  162. ESP_LOGE(TAG, "Pre-loaded key is not read protected. Refusing to blow secure boot efuse.");
  163. return ESP_ERR_INVALID_STATE;
  164. }
  165. if (!efuse_key_write_protected) {
  166. ESP_LOGE(TAG, "Pre-loaded key is not write protected. Refusing to blow secure boot efuse.");
  167. return ESP_ERR_INVALID_STATE;
  168. }
  169. ESP_LOGI(TAG, "blowing secure boot efuse...");
  170. ESP_LOGD(TAG, "before updating, EFUSE_BLK0_RDATA6 %x", REG_READ(EFUSE_BLK0_RDATA6_REG));
  171. uint32_t new_wdata6 = EFUSE_RD_ABS_DONE_0;
  172. #ifdef CONFIG_SECURE_BOOT_DISABLE_JTAG
  173. ESP_LOGI(TAG, "disabling JTAG...");
  174. new_wdata6 |= EFUSE_RD_DISABLE_JTAG;
  175. #endif
  176. #ifdef CONFIG_SECURE_BOOT_DISABLE_UART_BOOTLOADER
  177. ESP_LOGI(TAG, "disabling UART bootloader...");
  178. new_wdata6 |= EFUSE_RD_CONSOLE_DEBUG_DISABLE_S;
  179. #endif
  180. REG_WRITE(EFUSE_BLK0_WDATA6_REG, new_wdata6);
  181. burn_efuses();
  182. uint32_t after = REG_READ(EFUSE_BLK0_RDATA6_REG);
  183. ESP_LOGD(TAG, "after updating, EFUSE_BLK0_RDATA6 %x", after);
  184. if (after & EFUSE_RD_ABS_DONE_0) {
  185. ESP_LOGI(TAG, "secure boot is now enabled for bootloader image");
  186. return ESP_OK;
  187. } else {
  188. #ifdef CONFIG_SECURE_BOOT_TEST_MODE
  189. ESP_LOGE(TAG, "secure boot not enabled due to test mode");
  190. #else
  191. ESP_LOGE(TAG, "secure boot not enabled for bootloader image, EFUSE_RD_ABS_DONE_0 is probably write protected!");
  192. #endif
  193. return ESP_ERR_INVALID_STATE;
  194. }
  195. }