Skip to content

Commit 26390f3

Browse files
committed
Fix ROData pointers not getting resolved on IDf v5.2 onwards
Issue was caused due to desparancy between SHA obtained from firmware package and one being sent from esp. Disparancy was caused due the change in default value of `CONFIG_APP_RETRIEVE_LEN_ELF_SHA` from 16 to 9. `CONFIG_APP_RETRIEVE_LEN_ELF_SHA` is being used by `esp_app_get_elf_sha256()`. This PR fixes the issue by replacing `CONFIG_APP_RETRIEVE_LEN_ELF_SHA` with relevant macros in `rtc_store.h` and `esp_diagnostics.h` to avoid futur conflicts. It also uses `esp_app_get_description->app_elf_sha256` instead of `esp_app_get_elf_sha256()` to get SHA.
1 parent 6e3bca5 commit 26390f3

File tree

8 files changed

+30
-18
lines changed

8 files changed

+30
-18
lines changed

components/esp_diag_data_store/src/rtc_store/rtc_store.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ typedef struct {
9494
rbuf_data_t critical;
9595
rbuf_data_t non_critical;
9696
rtc_store_meta_header_t *meta_hdr;
97-
char sha_sum[2 * SHA_SIZE + 1];
97+
char sha_sum[RTC_STORE_HEX_SHA_SIZE + 1];
9898
} rtc_store_priv_data_t;
9999

100100
// have a strategy to invalidate data beyond this
@@ -504,6 +504,7 @@ static inline uint8_t to_int_digit(unsigned val)
504504
return (val <= '9') ? (val - '0') : (val - 'a' + 10);
505505
}
506506

507+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
507508
static void hex_to_bytes(uint8_t *src, uint8_t *dst, int out_len)
508509
{
509510
for (int i = 0; i < out_len; i++) {
@@ -512,6 +513,7 @@ static void hex_to_bytes(uint8_t *src, uint8_t *dst, int out_len)
512513
dst[i] = (val0 << 4) | (val1);
513514
}
514515
}
516+
#endif
515517

516518
static esp_err_t rtc_store_meta_hdr_init()
517519
{
@@ -556,11 +558,12 @@ static esp_err_t rtc_store_meta_hdr_init()
556558

557559
// populate meta header
558560
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
559-
esp_app_get_elf_sha256(s_priv_data.sha_sum, sizeof(s_priv_data.sha_sum));
561+
const uint8_t* src = esp_app_get_description()->app_elf_sha256;
562+
memcpy((uint8_t *)s_priv_data.meta_hdr->sha_sum, src, RTC_STORE_SHA_SIZE);
560563
#else
561564
esp_ota_get_app_elf_sha256(s_priv_data.sha_sum, sizeof(s_priv_data.sha_sum));
565+
hex_to_bytes((uint8_t *) s_priv_data.sha_sum, (uint8_t *) s_priv_data.meta_hdr->sha_sum, RTC_STORE_SHA_SIZE);
562566
#endif
563-
hex_to_bytes((uint8_t *) s_priv_data.sha_sum, (uint8_t *) s_priv_data.meta_hdr->sha_sum, SHA_SIZE);
564567

565568
s_priv_data.meta_hdr->gen_id = gen_id;
566569
s_priv_data.meta_hdr->boot_cnt = boot_cnt;

components/esp_diag_data_store/src/rtc_store/rtc_store.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313
extern "C" {
1414
#endif
1515

16-
#define SHA_SIZE (CONFIG_APP_RETRIEVE_LEN_ELF_SHA / 2)
16+
#define RTC_STORE_HEX_SHA_SIZE 16 /* Length of ELF SHA as HEX string*/
17+
#define RTC_STORE_SHA_SIZE (RTC_STORE_HEX_SHA_SIZE / 2) /* Length of ELF SHA as raw bytes*/
1718

1819
/**
1920
* @brief header record to identify firmware/boot data a record represent
2021
*/
2122
typedef struct {
2223
uint8_t gen_id; // generated on each hard reset
2324
uint8_t boot_cnt; // updated on each soft reboot
24-
char sha_sum[SHA_SIZE]; // elf shasum
25+
char sha_sum[RTC_STORE_SHA_SIZE]; // elf shasum
2526
bool valid; //
2627
} rtc_store_meta_header_t;
2728

components/esp_diagnostics/include/esp_diagnostics.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ typedef struct {
126126
/**
127127
* @brief Device information structure
128128
*/
129+
#define DIAG_HEX_SHA_SIZE 16 /* Length of ELF SHA as HEX string*/
130+
#define DIAG_SHA_SIZE (DIAG_HEX_SHA_SIZE / 2) /* Length of ELF SHA as raw bytes*/
129131
typedef struct {
130132
uint32_t chip_model; /*!< Chip model */
131133
uint32_t chip_rev; /*!< Chip revision */
132134
uint32_t reset_reason; /*!< Reset reason */
133135
char app_version[32]; /*!< Application version */
134136
char project_name[32]; /*!< Project name */
135-
char app_elf_sha256[CONFIG_APP_RETRIEVE_LEN_ELF_SHA + 1]; /*!< SHA256 of application elf */
137+
char app_elf_sha256[DIAG_HEX_SHA_SIZE + 1]; /*!< SHA256 of application elf */
136138
} esp_diag_device_info_t;
137139

138140
/**

components/esp_diagnostics/src/esp_diagnostics_utils.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ esp_err_t esp_diag_device_info_get(esp_diag_device_info_t *device_info)
8282
device_info->reset_reason = esp_reset_reason();
8383
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
8484
app_desc = esp_app_get_description();
85-
esp_app_get_elf_sha256(device_info->app_elf_sha256, sizeof(device_info->app_elf_sha256));
85+
const uint8_t* src = app_desc->app_elf_sha256;
86+
memcpy((uint8_t *)device_info->app_elf_sha256, src, DIAG_SHA_SIZE);
8687
#else
8788
app_desc = esp_ota_get_app_description();
8889
esp_ota_get_app_elf_sha256(device_info->app_elf_sha256, sizeof(device_info->app_elf_sha256));

components/esp_insights/src/esp_insights.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#endif
4242

4343
#define INSIGHTS_DEBUG_ENABLED CONFIG_ESP_INSIGHTS_DEBUG_ENABLED
44-
#define APP_ELF_SHA256_LEN (CONFIG_APP_RETRIEVE_LEN_ELF_SHA + 1)
4544

4645
#if CONFIG_ESP_INSIGHTS_CLOUD_POST_MIN_INTERVAL_SEC > CONFIG_ESP_INSIGHTS_CLOUD_POST_MAX_INTERVAL_SEC
4746
#error "CONFIG_ESP_INSIGHTS_CLOUD_POST_MIN_INTERVAL_SEC must be less than or equal to CONFIG_ESP_INSIGHTS_CLOUD_POST_MAX_INTERVAL_SEC"
@@ -94,7 +93,7 @@ typedef struct {
9493
int data_msg_id;
9594
uint32_t data_msg_len;
9695
SemaphoreHandle_t data_lock;
97-
char app_sha256[APP_ELF_SHA256_LEN];
96+
char app_sha256[DIAG_HEX_SHA_SIZE + 1];
9897
bool data_sent;
9998
#if SEND_INSIGHTS_META
10099
#if INSIGHTS_CMD_RESP
@@ -924,7 +923,7 @@ esp_err_t esp_insights_enable(esp_insights_config_t *config)
924923
ESP_LOGE(TAG, "Failed to get device info");
925924
goto enable_err;
926925
}
927-
memcpy(s_insights_data.app_sha256, device_info.app_elf_sha256, sizeof(s_insights_data.app_sha256));
926+
memcpy((uint8_t *) s_insights_data.app_sha256, (uint8_t *) device_info.app_elf_sha256, sizeof(s_insights_data.app_sha256));
928927
err = esp_event_handler_register(INSIGHTS_EVENT, ESP_EVENT_ANY_ID, insights_event_handler, NULL);
929928
if (err != ESP_OK) {
930929
ESP_LOGE(TAG, "Failed to register event handler for INSIGHTS_EVENTS");

components/esp_insights/src/esp_insights_cbor_encoder.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,15 @@ static union encode_scratch_buf {
196196
esp_diag_data_pt_t data_pt;
197197
#endif
198198
esp_diag_log_data_t log_data_pt;
199-
char sha_sum[2 * SHA_SIZE + 1];
199+
char sha_sum[DIAG_HEX_SHA_SIZE + 1];
200200
} enc_scratch_buf;
201201

202202
static inline uint8_t to_hex_digit(unsigned val)
203203
{
204204
return (val < 10) ? ('0' + val) : ('a' + val - 10);
205205
}
206206

207-
static void bytes_to_hex(uint8_t *src, uint8_t *dst, int in_len)
207+
void bytes_to_hex(uint8_t *src, uint8_t *dst, int in_len)
208208
{
209209
for (int i = 0; i < in_len; i++) {
210210
dst[2 * i] = to_hex_digit(src[i] >> 4);
@@ -216,7 +216,7 @@ static void bytes_to_hex(uint8_t *src, uint8_t *dst, int in_len)
216216
static inline void _cbor_encode_meta_hdr(CborEncoder *hdr_map, const rtc_store_meta_header_t *hdr)
217217
{
218218
cbor_encode_text_stringz(hdr_map, "sha256");
219-
bytes_to_hex((uint8_t *) hdr->sha_sum, (uint8_t *) enc_scratch_buf.sha_sum, SHA_SIZE); // expand uint8 packed data to hex
219+
bytes_to_hex((uint8_t *) hdr->sha_sum, (uint8_t *) enc_scratch_buf.sha_sum, DIAG_SHA_SIZE); // expand uint8 packed data to hex
220220
cbor_encode_text_stringz(hdr_map, enc_scratch_buf.sha_sum);
221221
cbor_encode_text_stringz(hdr_map, "gen_id");
222222
cbor_encode_uint(hdr_map, hdr->gen_id);
@@ -684,8 +684,7 @@ void esp_insights_cbor_encode_meta_begin(void *data, size_t data_size, const cha
684684
cbor_encode_text_stringz(&s_diag_meta_map, version);
685685

686686
cbor_encode_text_stringz(&s_diag_meta_map, "ts");
687-
cbor_encode_uint(&s_diag_meta_map, esp_diag_timestamp_get());
688-
687+
cbor_encode_uint(&s_diag_meta_map, esp_diag_timestamp_get());
689688
cbor_encode_text_stringz(&s_diag_meta_map, "sha256");
690689
cbor_encode_text_stringz(&s_diag_meta_map, sha256);
691690
}

components/esp_insights/src/esp_insights_cbor_encoder.h

+4
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,7 @@ size_t esp_insights_cbor_encode_conf_meta_end(void *data);
8686
void esp_insights_cbor_encode_diag_conf_data_begin(void);
8787
void esp_insights_cbor_encode_diag_conf_data_end(void);
8888
void esp_insights_cbor_encode_diag_conf_data(void);
89+
90+
/* For converting 8 bytes sha256 to hex form */
91+
void bytes_to_hex(uint8_t *src, uint8_t *dst, int in_len);
92+

components/esp_insights/src/esp_insights_encoder.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ size_t esp_insights_encode_meta(uint8_t *out_data, size_t out_data_size, char *s
6161
if (!out_data || !out_data_size) {
6262
return 0;
6363
}
64-
64+
char sha[DIAG_HEX_SHA_SIZE + 1];
65+
bytes_to_hex((uint8_t *) sha256,(uint8_t *) sha, DIAG_SHA_SIZE);
6566
esp_insights_cbor_encode_meta_begin(out_data + TLV_OFFSET,
6667
out_data_size - TLV_OFFSET,
67-
INSIGHTS_META_VERSION, sha256);
68+
INSIGHTS_META_VERSION, sha);
6869
esp_insights_cbor_encode_meta_data_begin();
6970
esp_insights_encode_meta_data();
7071
esp_insights_cbor_encode_meta_data_end();
@@ -91,9 +92,11 @@ size_t esp_insights_encode_conf_meta(uint8_t *out_data, size_t out_data_size, ch
9192
if (!out_data || !out_data_size) {
9293
return 0;
9394
}
95+
char sha[DIAG_HEX_SHA_SIZE + 1];
96+
bytes_to_hex((uint8_t *) sha256,(uint8_t *) sha, DIAG_SHA_SIZE);
9497
esp_insights_cbor_encode_meta_begin(out_data + TLV_OFFSET,
9598
out_data_size - TLV_OFFSET,
96-
INSIGHTS_META_VERSION, sha256);
99+
INSIGHTS_META_VERSION, sha);
97100
esp_insights_cbor_encode_conf_meta_data_begin();
98101
/* TODO: Implement and collect diagnostics specific conf meta */
99102
// esp_insights_encode_conf_meta_data();

0 commit comments

Comments
 (0)