1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #pragma once
7 
8 #include <inttypes.h>
9 #include "esp_assert.h"
10 
11 /**
12  * @brief ESP chip ID
13  *
14  */
15 typedef enum {
16     ESP_CHIP_ID_ESP32 = 0x0000,  /*!< chip ID: ESP32 */
17     ESP_CHIP_ID_ESP32S2 = 0x0002,  /*!< chip ID: ESP32-S2 */
18     ESP_CHIP_ID_ESP32C3 = 0x0005, /*!< chip ID: ESP32-C3 */
19     ESP_CHIP_ID_ESP32S3 = 0x0009, /*!< chip ID: ESP32-S3 */
20     ESP_CHIP_ID_ESP32H2 = 0x000A, /*!< chip ID: ESP32-H2 */  // ESP32H2-TODO: IDF-3475
21     ESP_CHIP_ID_INVALID = 0xFFFF /*!< Invalid chip ID (we defined it to make sure the esp_chip_id_t is 2 bytes size) */
22 } __attribute__((packed)) esp_chip_id_t;
23 
24 /** @cond */
25 ESP_STATIC_ASSERT(sizeof(esp_chip_id_t) == 2, "esp_chip_id_t should be 16 bit");
26 /** @endcond */
27 
28 /**
29  * @brief SPI flash mode, used in esp_image_header_t
30  */
31 typedef enum {
32     ESP_IMAGE_SPI_MODE_QIO,         /*!< SPI mode QIO */
33     ESP_IMAGE_SPI_MODE_QOUT,        /*!< SPI mode QOUT */
34     ESP_IMAGE_SPI_MODE_DIO,         /*!< SPI mode DIO */
35     ESP_IMAGE_SPI_MODE_DOUT,        /*!< SPI mode DOUT */
36     ESP_IMAGE_SPI_MODE_FAST_READ,   /*!< SPI mode FAST_READ */
37     ESP_IMAGE_SPI_MODE_SLOW_READ    /*!< SPI mode SLOW_READ */
38 } esp_image_spi_mode_t;
39 
40 /**
41  * @brief SPI flash clock frequency
42  */
43 typedef enum {
44     ESP_IMAGE_SPI_SPEED_40M,        /*!< SPI clock frequency 40 MHz */
45     ESP_IMAGE_SPI_SPEED_26M,        /*!< SPI clock frequency 26 MHz */
46     ESP_IMAGE_SPI_SPEED_20M,        /*!< SPI clock frequency 20 MHz */
47     ESP_IMAGE_SPI_SPEED_80M = 0xF   /*!< SPI clock frequency 80 MHz */
48 } esp_image_spi_freq_t;
49 
50 /**
51  * @brief Supported SPI flash sizes
52  */
53 typedef enum {
54     ESP_IMAGE_FLASH_SIZE_1MB = 0,   /*!< SPI flash size 1 MB */
55     ESP_IMAGE_FLASH_SIZE_2MB,       /*!< SPI flash size 2 MB */
56     ESP_IMAGE_FLASH_SIZE_4MB,       /*!< SPI flash size 4 MB */
57     ESP_IMAGE_FLASH_SIZE_8MB,       /*!< SPI flash size 8 MB */
58     ESP_IMAGE_FLASH_SIZE_16MB,      /*!< SPI flash size 16 MB */
59     ESP_IMAGE_FLASH_SIZE_32MB,      /*!< SPI flash size 32 MB */
60     ESP_IMAGE_FLASH_SIZE_64MB,      /*!< SPI flash size 64 MB */
61     ESP_IMAGE_FLASH_SIZE_128MB,     /*!< SPI flash size 128 MB */
62     ESP_IMAGE_FLASH_SIZE_MAX        /*!< SPI flash size MAX */
63 } esp_image_flash_size_t;
64 
65 #define ESP_IMAGE_HEADER_MAGIC 0xE9 /*!< The magic word for the esp_image_header_t structure. */
66 
67 /**
68  * @brief Main header of binary image
69  */
70 typedef struct {
71     uint8_t magic;              /*!< Magic word ESP_IMAGE_HEADER_MAGIC */
72     uint8_t segment_count;      /*!< Count of memory segments */
73     uint8_t spi_mode;           /*!< flash read mode (esp_image_spi_mode_t as uint8_t) */
74     uint8_t spi_speed: 4;       /*!< flash frequency (esp_image_spi_freq_t as uint8_t) */
75     uint8_t spi_size: 4;        /*!< flash chip size (esp_image_flash_size_t as uint8_t) */
76     uint32_t entry_addr;        /*!< Entry address */
77     uint8_t wp_pin;            /*!< WP pin when SPI pins set via efuse (read by ROM bootloader,
78                                 * the IDF bootloader uses software to configure the WP
79                                 * pin and sets this field to 0xEE=disabled) */
80     uint8_t spi_pin_drv[3];     /*!< Drive settings for the SPI flash pins (read by ROM bootloader) */
81     esp_chip_id_t chip_id;      /*!< Chip identification number */
82     uint8_t min_chip_rev;       /*!< Minimal chip revision supported by image
83                                  * After the Major and Minor revision eFuses were introduced into the chips, this field is no longer used.
84                                  * But for compatibility reasons, we keep this field and the data in it.
85                                  * Use min_chip_rev_full instead.
86                                  * The software interprets this as a Major version for most of the chips and as a Minor version for the ESP32-C3.
87                                  */
88     uint16_t min_chip_rev_full; /*!< Minimal chip revision supported by image, in format: major * 100 + minor */
89     uint16_t max_chip_rev_full; /*!< Maximal chip revision supported by image, in format: major * 100 + minor */
90     uint8_t reserved[4];        /*!< Reserved bytes in additional header space, currently unused */
91     uint8_t hash_appended;      /*!< If 1, a SHA256 digest "simple hash" (of the entire image) is appended after the checksum.
92                                  * Included in image length. This digest
93                                  * is separate to secure boot and only used for detecting corruption.
94                                  * For secure boot signed images, the signature
95                                  * is appended after this (and the simple hash is included in the signed data). */
96 } __attribute__((packed))  esp_image_header_t;
97 
98 /** @cond */
99 ESP_STATIC_ASSERT(sizeof(esp_image_header_t) == 24, "binary image header should be 24 bytes");
100 /** @endcond */
101 
102 
103 /**
104  * @brief Header of binary image segment
105  */
106 typedef struct {
107     uint32_t load_addr;     /*!< Address of segment */
108     uint32_t data_len;      /*!< Length of data */
109 } esp_image_segment_header_t;
110 
111 #define ESP_IMAGE_MAX_SEGMENTS 16           /*!< Max count of segments in the image. */
112 
113 #define ESP_APP_DESC_MAGIC_WORD 0xABCD5432  /*!< The magic word for the esp_app_desc structure that is in DROM. */
114 
115 /**
116  * @brief Description about application.
117  */
118 typedef struct {
119     uint32_t magic_word;        /*!< Magic word ESP_APP_DESC_MAGIC_WORD */
120     uint32_t secure_version;    /*!< Secure version */
121     uint32_t reserv1[2];        /*!< reserv1 */
122     char version[32];           /*!< Application version */
123     char project_name[32];      /*!< Project name */
124     char time[16];              /*!< Compile time */
125     char date[16];              /*!< Compile date*/
126     char idf_ver[32];           /*!< Version IDF */
127     uint8_t app_elf_sha256[32]; /*!< sha256 of elf file */
128     uint32_t reserv2[20];       /*!< reserv2 */
129 } esp_app_desc_t;
130 
131 /** @cond */
132 _Static_assert(sizeof(esp_app_desc_t) == 256, "esp_app_desc_t should be 256 bytes");
133 /** @endcond */
134