Skip to content

Commit a2f028a

Browse files
committed
Merge branch 'feature/app_metadata_to_esp_system' into 'master'
app_update: Moved app metadata to new component `esp_app_format` Closes IDF-4808 See merge request espressif/esp-idf!18862
2 parents 1c25086 + 85d00a4 commit a2f028a

File tree

37 files changed

+344
-137
lines changed

37 files changed

+344
-137
lines changed

.gitlab/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
/components/driver/ @esp-idf-codeowners/peripherals
7979
/components/efuse/ @esp-idf-codeowners/system
8080
/components/esp_adc/ @esp-idf-codeowners/peripherals
81+
/components/esp_app_format/ @esp-idf-codeowners/system @esp-idf-codeowners/app-utilities
8182
/components/esp_common/ @esp-idf-codeowners/system
8283
/components/esp_eth/ @esp-idf-codeowners/network
8384
/components/esp_event/ @esp-idf-codeowners/system

components/app_update/CMakeLists.txt

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,8 @@
1-
idf_component_register(SRCS "esp_ota_ops.c"
2-
"esp_app_desc.c"
1+
idf_component_register(SRCS "esp_ota_ops.c" "esp_ota_app_desc.c"
32
INCLUDE_DIRS "include"
4-
REQUIRES spi_flash partition_table bootloader_support
3+
REQUIRES spi_flash partition_table bootloader_support esp_app_format
54
PRIV_REQUIRES esptool_py efuse)
65

7-
# esp_app_desc structure is added as an undefined symbol because otherwise the
8-
# linker will ignore this structure as it has no other files depending on it.
9-
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u esp_app_desc")
10-
11-
if(CONFIG_APP_PROJECT_VER_FROM_CONFIG)
12-
# Ignore current PROJECT_VER (which was set in __project_get_revision()).
13-
# Gets the version from the CONFIG_APP_PROJECT_VER.
14-
idf_build_set_property(PROJECT_VER "${CONFIG_APP_PROJECT_VER}")
15-
endif()
16-
17-
# cut PROJECT_VER and PROJECT_NAME to required 32 characters.
18-
idf_build_get_property(project_ver PROJECT_VER)
19-
idf_build_get_property(project_name PROJECT_NAME)
20-
string(SUBSTRING "${project_ver}" 0 31 PROJECT_VER_CUT)
21-
string(SUBSTRING "${project_name}" 0 31 PROJECT_NAME_CUT)
22-
message(STATUS "App \"${PROJECT_NAME_CUT}\" version: ${PROJECT_VER_CUT}")
23-
24-
set_source_files_properties(
25-
SOURCE "esp_app_desc.c"
26-
PROPERTIES COMPILE_DEFINITIONS
27-
"PROJECT_VER=\"${PROJECT_VER_CUT}\"; PROJECT_NAME=\"${PROJECT_NAME_CUT}\"")
28-
296
if(NOT BOOTLOADER_BUILD)
307
partition_table_get_partition_info(otadata_offset "--partition-type data --partition-subtype ota" "offset")
318
partition_table_get_partition_info(otadata_size "--partition-type data --partition-subtype ota" "size")
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <assert.h>
8+
#include <sys/param.h>
9+
#include "esp_ota_ops.h"
10+
#include "esp_attr.h"
11+
#include "sdkconfig.h"
12+
13+
const esp_app_desc_t *esp_ota_get_app_description(void)
14+
{
15+
return esp_app_get_description();
16+
}
17+
18+
int IRAM_ATTR esp_ota_get_app_elf_sha256(char* dst, size_t size)
19+
{
20+
return esp_app_get_elf_sha256(dst, size);
21+
}

components/app_update/esp_ota_ops.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,14 @@ esp_err_t esp_ota_get_partition_description(const esp_partition_t *partition, es
651651

652652
#ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
653653
static esp_err_t esp_ota_set_anti_rollback(void) {
654-
const esp_app_desc_t *app_desc = esp_ota_get_app_description();
655-
return esp_efuse_update_secure_version(app_desc->secure_version);
654+
const esp_partition_t* partition = esp_ota_get_running_partition();
655+
esp_app_desc_t app_desc = {0};
656+
657+
esp_err_t err = esp_ota_get_partition_description(partition, &app_desc);
658+
if (err == ESP_OK) {
659+
return esp_efuse_update_secure_version(app_desc.secure_version);
660+
}
661+
return err;
656662
}
657663
#endif
658664

components/app_update/include/esp_ota_ops.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <stddef.h>
1313
#include "esp_err.h"
1414
#include "esp_partition.h"
15-
#include "esp_image_format.h"
15+
#include "esp_app_desc.h"
1616
#include "esp_flash_partitions.h"
1717
#include "soc/soc_caps.h"
1818

@@ -44,20 +44,27 @@ typedef uint32_t esp_ota_handle_t;
4444
/**
4545
* @brief Return esp_app_desc structure. This structure includes app version.
4646
*
47+
* @note This API is present for backward compatibility reasons. Alternative function
48+
* with the same functionality is `esp_app_get_description`
49+
*
4750
* Return description for running app.
4851
* @return Pointer to esp_app_desc structure.
4952
*/
50-
const esp_app_desc_t *esp_ota_get_app_description(void);
53+
const esp_app_desc_t *esp_ota_get_app_description(void) __attribute__((deprecated("Please use esp_app_get_description instead")));
5154

5255
/**
5356
* @brief Fill the provided buffer with SHA256 of the ELF file, formatted as hexadecimal, null-terminated.
5457
* If the buffer size is not sufficient to fit the entire SHA256 in hex plus a null terminator,
5558
* the largest possible number of bytes will be written followed by a null.
59+
*
60+
* @note This API is present for backward compatibility reasons. Alternative function
61+
* with the same functionality is `esp_app_get_elf_sha256`
62+
*
5663
* @param dst Destination buffer
5764
* @param size Size of the buffer
5865
* @return Number of bytes written to dst (including null terminator)
5966
*/
60-
int esp_ota_get_app_elf_sha256(char* dst, size_t size);
67+
int esp_ota_get_app_elf_sha256(char* dst, size_t size) __attribute__((deprecated("Please use esp_app_get_elf_sha256 instead")));
6168

6269
/**
6370
* @brief Commence an OTA update writing to the specified partition.

components/app_update/test/test_ota_ops.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -13,7 +13,6 @@
1313
#include <unity.h>
1414
#include <test_utils.h>
1515
#include <esp_ota_ops.h>
16-
#include "bootloader_common.h"
1716

1817
/* These OTA tests currently don't assume an OTA partition exists
1918
on the device, so they're a bit limited
@@ -91,6 +90,8 @@ TEST_CASE("esp_ota_get_next_update_partition logic", "[ota]")
9190

9291
TEST_CASE("esp_ota_get_partition_description", "[ota]")
9392
{
93+
extern esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc);
94+
9495
const esp_partition_t *running = esp_ota_get_running_partition();
9596
TEST_ASSERT_NOT_NULL(running);
9697
esp_app_desc_t app_desc1, app_desc2;

components/bootloader_support/CMakeLists.txt

+11-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set(srcs
1919
if(BOOTLOADER_BUILD)
2020
set(include_dirs "include" "bootloader_flash/include"
2121
"private_include")
22-
set(priv_requires micro-ecc spi_flash efuse)
22+
set(priv_requires micro-ecc spi_flash efuse esp_app_format)
2323
list(APPEND srcs
2424
"src/bootloader_init.c"
2525
"src/bootloader_clock_loader.c"
@@ -37,7 +37,7 @@ else()
3737
set(include_dirs "include" "bootloader_flash/include")
3838
set(priv_include_dirs "private_include")
3939
# heap is required for `heap_memory_layout.h` header
40-
set(priv_requires spi_flash mbedtls efuse app_update heap)
40+
set(priv_requires spi_flash mbedtls efuse heap esp_app_format)
4141
endif()
4242

4343
if(BOOTLOADER_BUILD)
@@ -86,6 +86,15 @@ idf_component_register(SRCS "${srcs}"
8686
REQUIRES "${requires}"
8787
PRIV_REQUIRES "${priv_requires}")
8888

89+
if(NOT BOOTLOADER_BUILD)
90+
if(CONFIG_SECURE_SIGNED_ON_UPDATE)
91+
if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME OR CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME OR
92+
CONFIG_SECURE_SIGNED_APPS_ECDSA_V2_SCHEME)
93+
target_link_libraries(${COMPONENT_LIB} PRIVATE idf::app_update)
94+
endif()
95+
endif()
96+
endif()
97+
8998
if(CONFIG_SECURE_SIGNED_APPS AND (CONFIG_SECURE_BOOT_V1_ENABLED OR CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME))
9099
if(BOOTLOADER_BUILD)
91100
# Whether CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES or not, we need verification key to embed

components/bootloader_support/include/bootloader_common.h

-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#pragma once
88
#include "esp_flash_partitions.h"
99
#include "esp_image_format.h"
10-
#include "esp_app_format.h"
1110

1211
#ifdef __cplusplus
1312
extern "C" {
@@ -151,20 +150,6 @@ int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata);
151150
*/
152151
int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max);
153152

154-
/**
155-
* @brief Returns esp_app_desc structure for app partition. This structure includes app version.
156-
*
157-
* Returns a description for the requested app partition.
158-
* @param[in] partition App partition description.
159-
* @param[out] app_desc Structure of info about app.
160-
* @return
161-
* - ESP_OK: Successful.
162-
* - ESP_ERR_INVALID_ARG: The arguments passed are not valid.
163-
* - ESP_ERR_NOT_FOUND: app_desc structure is not found. Magic word is incorrect.
164-
* - ESP_FAIL: mapping is fail.
165-
*/
166-
esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc);
167-
168153
/**
169154
* @brief Get chip package
170155
*

components/bootloader_support/include/esp_app_format.h

-22
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,3 @@ typedef struct {
106106
} esp_image_segment_header_t;
107107

108108
#define ESP_IMAGE_MAX_SEGMENTS 16 /*!< Max count of segments in the image. */
109-
110-
#define ESP_APP_DESC_MAGIC_WORD 0xABCD5432 /*!< The magic word for the esp_app_desc structure that is in DROM. */
111-
112-
/**
113-
* @brief Description about application.
114-
*/
115-
typedef struct {
116-
uint32_t magic_word; /*!< Magic word ESP_APP_DESC_MAGIC_WORD */
117-
uint32_t secure_version; /*!< Secure version */
118-
uint32_t reserv1[2]; /*!< reserv1 */
119-
char version[32]; /*!< Application version */
120-
char project_name[32]; /*!< Project name */
121-
char time[16]; /*!< Compile time */
122-
char date[16]; /*!< Compile date*/
123-
char idf_ver[32]; /*!< Version IDF */
124-
uint8_t app_elf_sha256[32]; /*!< sha256 of elf file */
125-
uint32_t reserv2[20]; /*!< reserv2 */
126-
} esp_app_desc_t;
127-
128-
/** @cond */
129-
_Static_assert(sizeof(esp_app_desc_t) == 256, "esp_app_desc_t should be 256 bytes");
130-
/** @endcond */

components/bootloader_support/src/bootloader_common_loader.c

-24
Original file line numberDiff line numberDiff line change
@@ -112,30 +112,6 @@ int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata,
112112
return active_otadata;
113113
}
114114

115-
esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc)
116-
{
117-
if (partition == NULL || app_desc == NULL || partition->offset == 0) {
118-
return ESP_ERR_INVALID_ARG;
119-
}
120-
121-
const uint32_t app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t);
122-
const uint32_t mmap_size = app_desc_offset + sizeof(esp_app_desc_t);
123-
const uint8_t *image = bootloader_mmap(partition->offset, mmap_size);
124-
if (image == NULL) {
125-
ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, mmap_size);
126-
return ESP_FAIL;
127-
}
128-
129-
memcpy(app_desc, image + app_desc_offset, sizeof(esp_app_desc_t));
130-
bootloader_munmap(image);
131-
132-
if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) {
133-
return ESP_ERR_NOT_FOUND;
134-
}
135-
136-
return ESP_OK;
137-
}
138-
139115
#if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )
140116

141117
#define RTC_RETAIN_MEM_ADDR (SOC_RTC_DRAM_HIGH - sizeof(rtc_retain_mem_t))

components/bootloader_support/src/bootloader_utility.c

+26
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
#include "esp_cpu.h"
6262
#include "esp_image_format.h"
63+
#include "esp_app_desc.h"
6364
#include "esp_secure_boot.h"
6465
#include "esp_flash_encrypt.h"
6566
#include "esp_flash_partitions.h"
@@ -119,6 +120,31 @@ static esp_err_t read_otadata(const esp_partition_pos_t *ota_info, esp_ota_selec
119120
return ESP_OK;
120121
}
121122

123+
esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc)
124+
{
125+
if (partition == NULL || app_desc == NULL || partition->offset == 0) {
126+
return ESP_ERR_INVALID_ARG;
127+
}
128+
129+
const uint32_t app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t);
130+
const uint32_t mmap_size = app_desc_offset + sizeof(esp_app_desc_t);
131+
const uint8_t *image = bootloader_mmap(partition->offset, mmap_size);
132+
if (image == NULL) {
133+
ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, mmap_size);
134+
return ESP_FAIL;
135+
}
136+
137+
memcpy(app_desc, image + app_desc_offset, sizeof(esp_app_desc_t));
138+
bootloader_munmap(image);
139+
140+
if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) {
141+
return ESP_ERR_NOT_FOUND;
142+
}
143+
144+
return ESP_OK;
145+
}
146+
147+
122148
bool bootloader_utility_load_partition_table(bootloader_state_t *bs)
123149
{
124150
const esp_partition_info_t *partitions;
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
idf_component_register(SRCS "esp_app_desc.c"
2+
INCLUDE_DIRS "include")
3+
4+
# esp_app_desc structure is added as an undefined symbol because otherwise the
5+
# linker will ignore this structure as it has no other files depending on it.
6+
if(NOT BOOTLOADER_BUILD)
7+
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u esp_app_desc")
8+
endif()
9+
10+
if(CONFIG_APP_PROJECT_VER_FROM_CONFIG)
11+
# Ignore current PROJECT_VER (which was set in __project_get_revision()).
12+
# Gets the version from the CONFIG_APP_PROJECT_VER.
13+
idf_build_set_property(PROJECT_VER "${CONFIG_APP_PROJECT_VER}")
14+
endif()
15+
16+
# cut PROJECT_VER and PROJECT_NAME to required 32 characters.
17+
idf_build_get_property(project_ver PROJECT_VER)
18+
idf_build_get_property(project_name PROJECT_NAME)
19+
string(SUBSTRING "${project_ver}" 0 31 PROJECT_VER_CUT)
20+
string(SUBSTRING "${project_name}" 0 31 PROJECT_NAME_CUT)
21+
message(STATUS "App \"${PROJECT_NAME_CUT}\" version: ${PROJECT_VER_CUT}")
22+
23+
set_source_files_properties(
24+
SOURCE "esp_app_desc.c"
25+
PROPERTIES COMPILE_DEFINITIONS
26+
"PROJECT_VER=\"${PROJECT_VER_CUT}\"; PROJECT_NAME=\"${PROJECT_NAME_CUT}\"")

components/app_update/esp_app_desc.c renamed to components/esp_app_format/esp_app_desc.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/*
2-
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

77
#include <assert.h>
88
#include <sys/param.h>
9-
#include "esp_ota_ops.h"
9+
#include "esp_app_desc.h"
1010
#include "esp_attr.h"
1111
#include "sdkconfig.h"
1212

13+
1314
// Application version info
1415
const __attribute__((section(".rodata_desc"))) esp_app_desc_t esp_app_desc = {
1516
.magic_word = ESP_APP_DESC_MAGIC_WORD,
@@ -50,7 +51,7 @@ _Static_assert(sizeof(IDF_VER) <= sizeof(esp_app_desc.idf_ver), "IDF_VER is long
5051
_Static_assert(sizeof(PROJECT_NAME) <= sizeof(esp_app_desc.project_name), "PROJECT_NAME is longer than project_name field in structure");
5152
#endif
5253

53-
const esp_app_desc_t *esp_ota_get_app_description(void)
54+
const esp_app_desc_t *esp_app_get_description(void)
5455
{
5556
return &esp_app_desc;
5657
}
@@ -64,18 +65,18 @@ static inline char IRAM_ATTR to_hex_digit(unsigned val)
6465
return (val < 10) ? ('0' + val) : ('a' + val - 10);
6566
}
6667

67-
__attribute__((constructor)) void esp_ota_init_app_elf_sha256(void)
68+
__attribute__((constructor)) void esp_init_app_elf_sha256(void)
6869
{
69-
esp_ota_get_app_elf_sha256(NULL, 0);
70+
esp_app_get_elf_sha256(NULL, 0);
7071
}
7172

7273
/* The esp_app_desc.app_elf_sha256 should be possible to print in panic handler during cache is disabled.
7374
* But because the cache is disabled the reading esp_app_desc.app_elf_sha256 is not right and
7475
* can lead to a complete lock-up of the CPU.
75-
* For this reason we do a reading of esp_app_desc.app_elf_sha256 while start up in esp_ota_init_app_elf_sha256()
76+
* For this reason we do a reading of esp_app_desc.app_elf_sha256 while start up in esp_init_app_elf_sha256()
7677
* and keep it in the static s_app_elf_sha256 value.
7778
*/
78-
int IRAM_ATTR esp_ota_get_app_elf_sha256(char* dst, size_t size)
79+
int IRAM_ATTR esp_app_get_elf_sha256(char* dst, size_t size)
7980
{
8081
static char s_app_elf_sha256[CONFIG_APP_RETRIEVE_LEN_ELF_SHA / 2];
8182
static bool first_call = true;

0 commit comments

Comments
 (0)