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