secure_boot_main.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Flash encryption Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "sdkconfig.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "soc/efuse_reg.h"
  12. #include "esp_efuse.h"
  13. #include "esp_secure_boot.h"
  14. #include "esp_chip_info.h"
  15. #include "esp_flash.h"
  16. #include "esp_log.h"
  17. #include "esp_efuse_table.h"
  18. #include <string.h>
  19. static void example_print_chip_info(void);
  20. static void example_secure_boot_status(void);
  21. #define TAG "example_secure_boot"
  22. void app_main(void)
  23. {
  24. printf("\nExample to check Secure Boot status\n");
  25. example_print_chip_info();
  26. example_secure_boot_status();
  27. }
  28. static void example_print_chip_info(void)
  29. {
  30. /* Print chip information */
  31. esp_chip_info_t chip_info;
  32. uint32_t flash_size;
  33. esp_chip_info(&chip_info);
  34. printf("This is %s chip with %d CPU cores\n", CONFIG_IDF_TARGET, chip_info.cores);
  35. unsigned major_rev = chip_info.revision / 100;
  36. unsigned minor_rev = chip_info.revision % 100;
  37. printf("silicon revision v%d.%d, ", major_rev, minor_rev);
  38. if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
  39. printf("Get flash size failed");
  40. return;
  41. }
  42. printf("%dMB %s flash\n", flash_size / (1024 * 1024),
  43. (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
  44. }
  45. #define DIGEST_LEN 32
  46. static void example_secure_boot_status(void)
  47. {
  48. esp_secure_boot_key_digests_t trusted_keys = { 0};
  49. ESP_LOGI(TAG, "Checking for Secure Boot..");
  50. if(esp_secure_boot_enabled()) {
  51. ESP_LOGI(TAG, "Secure Boot is enabled");
  52. ESP_ERROR_CHECK( esp_secure_boot_read_key_digests(&trusted_keys) );
  53. unsigned total = 0;
  54. for (int i = 0; i < SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS; i++) {
  55. ESP_LOGI(TAG, "Key slot %d:", i);
  56. if (trusted_keys.key_digests[i]) {
  57. ESP_LOG_BUFFER_HEXDUMP("trusted key", trusted_keys.key_digests[i], DIGEST_LEN, ESP_LOG_INFO);
  58. total++;
  59. }
  60. }
  61. ESP_LOGI(TAG, "Total %d trusted public keys", total);
  62. } else {
  63. ESP_LOGI(TAG, "Secure Boot not enabled. Enable Secure Boot in menuconfig, build & flash again.");
  64. }
  65. }